1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use js::jsapi::{JSBool, JSContext};
use js::jsapi::{JS_ValueToUint64, JS_ValueToInt64};
use js::jsapi::{JS_ValueToECMAUint32, JS_ValueToECMAInt32};
use js::jsapi::{JS_ValueToUint16, JS_ValueToNumber, JS_ValueToBoolean};
use js::jsval::JSVal;
use js::jsval::{NullValue, BooleanValue, Int32Value, UInt32Value};
use js::glue::RUST_JS_NumberValue;
pub trait ToJSValConvertible {
fn to_jsval(&self, cx: *JSContext) -> JSVal;
}
pub trait FromJSValConvertible<T> {
fn from_jsval(cx: *JSContext, val: JSVal, option: T) -> Result<Self, ()>;
}
unsafe fn convert_from_jsval<T: Default>(
cx: *JSContext, value: JSVal,
convert_fn: extern "C" unsafe fn(*JSContext, JSVal, *T) -> JSBool) -> Result<T, ()> {
let mut ret = Default::default();
if convert_fn(cx, value, &mut ret as *mut T as *T) == 0 {
Err(())
} else {
Ok(ret)
}
}
impl ToJSValConvertible for bool {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
BooleanValue(*self)
}
}
impl FromJSValConvertible<()> for bool {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<bool, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToBoolean) };
result.map(|b| b != 0)
}
}
impl ToJSValConvertible for i8 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
Int32Value(*self as i32)
}
}
impl FromJSValConvertible<()> for i8 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<i8, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as i8)
}
}
impl ToJSValConvertible for u8 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
Int32Value(*self as i32)
}
}
impl FromJSValConvertible<()> for u8 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<u8, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as u8)
}
}
impl ToJSValConvertible for i16 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
Int32Value(*self as i32)
}
}
impl FromJSValConvertible<()> for i16 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<i16, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as i16)
}
}
impl ToJSValConvertible for u16 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
Int32Value(*self as i32)
}
}
impl FromJSValConvertible<()> for u16 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<u16, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToUint16) }
}
}
impl ToJSValConvertible for i32 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
Int32Value(*self)
}
}
impl FromJSValConvertible<()> for i32 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<i32, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }
}
}
impl ToJSValConvertible for u32 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
UInt32Value(*self)
}
}
impl FromJSValConvertible<()> for u32 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<u32, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAUint32) }
}
}
impl ToJSValConvertible for i64 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self as f64)
}
}
}
impl FromJSValConvertible<()> for i64 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<i64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToInt64) }
}
}
impl ToJSValConvertible for u64 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self as f64)
}
}
}
impl FromJSValConvertible<()> for u64 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<u64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToUint64) }
}
}
impl ToJSValConvertible for f32 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self as f64)
}
}
}
impl FromJSValConvertible<()> for f32 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<f32, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) };
result.map(|f| f as f32)
}
}
impl ToJSValConvertible for f64 {
fn to_jsval(&self, _cx: *JSContext) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self)
}
}
}
impl FromJSValConvertible<()> for f64 {
fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result<f64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) }
}
}
impl<T: ToJSValConvertible> ToJSValConvertible for Option<T> {
fn to_jsval(&self, cx: *JSContext) -> JSVal {
match self {
&Some(ref value) => value.to_jsval(cx),
&None => NullValue(),
}
}
}
impl<X: Default, T: FromJSValConvertible<X>> FromJSValConvertible<()> for Option<T> {
fn from_jsval(cx: *JSContext, value: JSVal, _: ()) -> Result<Option<T>, ()> {
if value.is_null_or_undefined() {
Ok(None)
} else {
let option: X = Default::default();
let result: Result<T, ()> = FromJSValConvertible::from_jsval(cx, value, option);
result.map(Some)
}
}
}
|