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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use std::{fmt, mem};
use chakracore_sys::*;
use context::{Context, ContextGuard};
use value;
macro_rules! downcast {
($predicate:ident, $predicate_doc:expr, $target:ident) => {
#[doc=$predicate_doc]
pub fn $predicate(&self) -> bool {
value::$target::is_same(self)
}
};
($predicate:ident, $predicate_doc:expr,
$conversion:ident, $conversion_doc:expr, $result:ident) => {
downcast!($predicate, $predicate_doc, $result);
#[doc=$conversion_doc]
pub fn $conversion(self) -> Option<super::$result> {
if self.$predicate() {
Some(unsafe { mem::transmute(self) })
} else {
None
}
}
};
}
macro_rules! nativecast {
($name:ident, $name_doc:expr, $result:ident, $into:ident, $represent:ident, $native:ident) => {
#[doc=$name_doc]
pub fn $name(&self, _guard: &ContextGuard) -> $result {
match self.clone().$into() {
None => self.$represent(_guard),
Some(value) => value,
}.$native()
}
}
}
macro_rules! representation {
($name:ident, $name_doc:expr, $result:ident, $function:ident) => {
#[doc=$name_doc]
pub fn $name(&self, _guard: &ContextGuard) -> super::$result {
let mut value = JsValueRef::new();
unsafe {
jsassert!($function(self.as_raw(), &mut value));
super::$result::from_raw(value)
}
}
}
}
pub struct Value(JsValueRef);
impl Value {
downcast!(is_undefined,
"Returns true if this value is `undefined`.",
Undefined);
downcast!(is_null, "Returns true if this value is `null`.", Null);
downcast!(is_number,
"Returns true if this value is a `Number`.",
into_number,
"Represent the value as a `Number`. Does not affect the underlying value.",
Number);
downcast!(is_string,
"Returns true if this value is a `String`.",
into_string,
"Represent the value as a `String`. Does not affect the underlying value.",
String);
downcast!(is_boolean,
"Returns true if this value is a `Boolean`.",
into_boolean,
"Represent the value as a `Boolean`. Does not affect the underlying value.",
Boolean);
downcast!(is_object,
"Returns true if this value is an `Object`.",
into_object,
"Represent the value as an `Object`. Does not affect the underlying value.",
Object);
downcast!(is_external,
"Returns true if this value is an `External`.",
into_external,
"Represent the value as an `External`. Does not affect the underlying value.",
External);
downcast!(is_function,
"Returns true if this value is a `Function`.",
into_function,
"Represent the value as a `Function`. Does not affect the underlying value.",
Function);
downcast!(is_array,
"Returns true if this value is an `Array`.",
into_array,
"Represent the value as an `Array`. Does not affect the underlying value.",
Array);
downcast!(is_array_buffer,
"Returns true if this value is an `ArrayBuffer`.",
into_array_buffer,
"Represent the value as an `ArrayBuffer`. Does not affect the underlying value.",
ArrayBuffer);
downcast!(is_promise,
"Returns true if this value is a `Promise`.",
into_promise,
"Represent the value as a `Promise`. Does not affect the underlying value.",
Promise);
nativecast!(to_string,
"Converts the value to a native string, containing the value's string representation.",
String,
into_string,
string_representation,
value);
nativecast!(to_integer,
"Converts the value to a native integer, containing the value's integer representation.",
i32,
into_number,
number_representation,
value);
nativecast!(to_double,
"Converts the value to a native double, containing the value's floating point representation.",
f64,
into_number,
number_representation,
value_double);
nativecast!(to_bool,
"Converts the value to a native boolean, containing the value's bool representation.",
bool,
into_boolean,
boolean_representation,
value);
pub fn to_json(&self, guard: &ContextGuard) -> ::error::Result<String> {
let stringify = ::script::eval(guard, "JSON.stringify")
.expect("retrieving JSON.stringify function")
.into_function()
.expect("converting JSON.stringify to function");
stringify.call(guard, &[self]).map(|v| v.to_string(guard))
}
representation!(boolean_representation,
"Creates a new boolean with this value represented as `Boolean`.",
Boolean,
JsConvertValueToBoolean);
representation!(number_representation,
"Creates a new number with this value represented as `Number`.",
Number,
JsConvertValueToNumber);
representation!(object_representation,
"Creates a new object with this value represented as `Object`.",
Object,
JsConvertValueToObject);
representation!(string_representation,
"Creates a new string with this value represented as `String`.",
String,
JsConvertValueToString);
pub fn get_type(&self) -> JsValueType {
let mut value_type = JsValueType::Undefined;
jsassert!(unsafe { JsGetValueType(self.as_raw(), &mut value_type) });
value_type
}
pub fn equals(&self, _guard: &ContextGuard, that: &Value) -> bool {
let mut result = false;
jsassert!(unsafe { JsEquals(self.as_raw(), that.as_raw(), &mut result) });
result
}
pub fn strict_equals(&self, _guard: &ContextGuard, that: &Value) -> bool {
let mut result = false;
jsassert!(unsafe { JsStrictEquals(self.as_raw(), that.as_raw(), &mut result) });
result
}
}
impl PartialEq for Value {
fn eq(&self, other: &Value) -> bool {
Context::exec_with_current(|guard| self.strict_equals(guard, other))
.expect("comparison to have an active context")
}
}
impl fmt::Debug for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Context::exec_with_current(|guard| {
let output = self.to_string(&guard);
let value_type = self.get_type();
match value_type {
JsValueType::String => write!(f, "Value({:?}: '{}')", value_type, output),
_ => write!(f, "Value({:?}: {})", value_type, output),
}
}).expect("value debug output without an active context")
}
}
reference!(Value);
#[cfg(test)]
mod tests {
use {test, value, Property};
#[test]
fn json_conversion() {
test::run_with_context(|guard| {
let object = value::Object::new(guard);
object.set(guard, &Property::new(guard, "foo"), &value::Number::new(guard, 1337));
assert_eq!(object.to_json(guard).unwrap(), r#"{"foo":1337}"#);
});
}
}