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
|
/* 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/. */
#[macro_use]
extern crate js;
use js::ar::AutoRealm;
use js::conversions::ConversionBehavior;
use js::conversions::ConversionResult;
use js::conversions::FromJSValConvertible;
use js::conversions::ToJSValConvertible;
use js::jsapi::root::JS_NewGlobalObject;
use js::jsapi::root::JS::InitRealmStandardClasses;
use js::jsapi::root::JS::OnNewGlobalHookOption;
use js::jsapi::root::JS::RealmOptions;
use js::jsval::UndefinedValue;
use js::rust::{Runtime, SIMPLE_GLOBAL_CLASS};
use std::ptr;
fn assert_is_array(cx: *mut js::jsapi::root::JSContext, val: js::jsapi::root::JS::HandleValue) {
let mut is_array = false;
assert!(unsafe { js::jsapi::root::JS::IsArrayObject(cx, val, &mut is_array as *mut _) });
assert!(is_array);
}
#[test]
fn vec_conversion() {
let rt = Runtime::new(false).unwrap();
let cx = rt.cx();
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = RealmOptions::default();
unsafe {
let global = JS_NewGlobalObject(
cx,
&SIMPLE_GLOBAL_CLASS,
ptr::null_mut(),
h_option,
&c_option,
);
rooted!(in(cx) let global_root = global);
let global = global_root.handle();
let _ar = AutoRealm::with_obj(cx, global.get());
assert!(InitRealmStandardClasses(cx));
rooted!(in(cx) let mut rval = UndefinedValue());
let orig_vec: Vec<f32> = vec![1.0, 2.9, 3.0];
orig_vec.to_jsval(cx, rval.handle_mut());
assert_is_array(cx, rval.handle());
let converted = Vec::<f32>::from_jsval(cx, rval.handle(), ()).unwrap();
assert_eq!(&orig_vec, converted.get_success_value().unwrap());
let orig_vec: Vec<i32> = vec![1, 2, 3];
orig_vec.to_jsval(cx, rval.handle_mut());
assert_is_array(cx, rval.handle());
let converted =
Vec::<i32>::from_jsval(cx, rval.handle(), ConversionBehavior::Default).unwrap();
assert_eq!(&orig_vec, converted.get_success_value().unwrap());
rt.evaluate_script(global, "new Set([1, 2, 3])", "test", 1, rval.handle_mut())
.unwrap();
let converted =
Vec::<i32>::from_jsval(cx, rval.handle(), ConversionBehavior::Default).unwrap();
assert_eq!(&orig_vec, converted.get_success_value().unwrap());
rt.evaluate_script(global, "({})", "test", 1, rval.handle_mut())
.unwrap();
let converted = Vec::<i32>::from_jsval(cx, rval.handle(), ConversionBehavior::Default);
assert!(match converted {
Ok(ConversionResult::Failure(_)) => true,
_ => false,
});
}
}
|