Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit c8f4cab

Browse files
committed
Add test.
1 parent c88b8df commit c8f4cab

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ name = "evaluate"
1717
[[test]]
1818
name = "panic"
1919
[[test]]
20+
name = "rooting"
21+
[[test]]
2022
name = "typedarray"
2123
[[test]]
2224
name = "stack_limit"

tests/rooting.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#![feature(const_fn)]
6+
#![cfg(feature = "debugmozjs")]
7+
8+
#[macro_use]
9+
extern crate js;
10+
extern crate libc;
11+
12+
use js::jsapi::CompartmentOptions;
13+
use js::jsapi::JSAutoCompartment;
14+
use js::jsapi::JSClass;
15+
use js::jsapi::JSContext;
16+
use js::jsapi::JSFunctionSpec;
17+
use js::jsapi::JS_GetObjectPrototype;
18+
use js::jsapi::JSNativeWrapper;
19+
use js::jsapi::JS_NewGlobalObject;
20+
use js::jsapi::JS_NewObjectWithUniqueType;
21+
use js::jsapi::JSPROP_ENUMERATE;
22+
use js::jsapi::JS_SetGCZeal;
23+
use js::jsapi::OnNewGlobalHookOption;
24+
use js::jsapi::Value;
25+
use js::rust::{Runtime, SIMPLE_GLOBAL_CLASS, define_methods};
26+
use std::ptr;
27+
28+
#[test]
29+
fn rooting() {
30+
unsafe {
31+
let runtime = Runtime::new();
32+
JS_SetGCZeal(runtime.rt(), 2, 1);
33+
34+
let cx = runtime.cx();
35+
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
36+
let c_option = CompartmentOptions::default();
37+
38+
rooted!(in(cx) let global = JS_NewGlobalObject(cx,
39+
&SIMPLE_GLOBAL_CLASS,
40+
ptr::null_mut(),
41+
h_option,
42+
&c_option));
43+
let _ac = JSAutoCompartment::new(cx, global.get());
44+
rooted!(in(cx) let prototype_proto = JS_GetObjectPrototype(cx, global.handle()));
45+
rooted!(in(cx) let proto = JS_NewObjectWithUniqueType(cx,
46+
&CLASS as *const _,
47+
prototype_proto.handle()));
48+
define_methods(cx, proto.handle(), METHODS).unwrap();
49+
}
50+
}
51+
52+
unsafe extern "C" fn generic_method(_: *mut JSContext, _: u32, _: *mut Value) -> bool {
53+
true
54+
}
55+
56+
const METHODS: &'static [JSFunctionSpec] = &[
57+
JSFunctionSpec {
58+
name: b"addEventListener\0" as *const u8 as *const libc::c_char,
59+
call: JSNativeWrapper { op: Some(generic_method), info: ptr::null() },
60+
nargs: 2,
61+
flags: JSPROP_ENUMERATE as u16,
62+
selfHostedName: 0 as *const libc::c_char
63+
},
64+
JSFunctionSpec {
65+
name: b"removeEventListener\0" as *const u8 as *const libc::c_char,
66+
call: JSNativeWrapper { op: Some(generic_method), info: ptr::null() },
67+
nargs: 2,
68+
flags: JSPROP_ENUMERATE as u16,
69+
selfHostedName: 0 as *const libc::c_char
70+
},
71+
JSFunctionSpec {
72+
name: b"dispatchEvent\0" as *const u8 as *const libc::c_char,
73+
call: JSNativeWrapper { op: Some(generic_method), info: ptr::null() },
74+
nargs: 1,
75+
flags: JSPROP_ENUMERATE as u16,
76+
selfHostedName: 0 as *const libc::c_char
77+
},
78+
JSFunctionSpec {
79+
name: ptr::null(),
80+
call: JSNativeWrapper { op: None, info: ptr::null() },
81+
nargs: 0,
82+
flags: 0,
83+
selfHostedName: ptr::null()
84+
}
85+
];
86+
87+
static CLASS: JSClass = JSClass {
88+
name: b"EventTargetPrototype\0" as *const u8 as *const libc::c_char,
89+
flags: 0,
90+
cOps: 0 as *const _,
91+
reserved: [0 as *mut _; 3]
92+
};

0 commit comments

Comments
 (0)