summaryrefslogtreecommitdiffstats
path: root/js/rust/tests/rooting.rs
blob: 868846fa021f5751eb13f16e9d6c6f6f9df2f21e (plain)
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
/* 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/. */

#![cfg(feature = "debugmozjs")]

#[macro_use]
extern crate js;
#[macro_use]
extern crate lazy_static;
extern crate libc;

use js::jsapi::*;
use js::rust::{define_methods, Runtime, SIMPLE_GLOBAL_CLASS};
use std::ptr;

#[test]
fn rooting() {
    unsafe {
        let runtime = Runtime::new(false).unwrap();
        JS_SetGCZeal(runtime.cx(), 2, 1);

        let cx = runtime.cx();
        let h_option = JS::OnNewGlobalHookOption::FireOnNewGlobalHook;
        let c_option = JS::RealmOptions::default();

        rooted!(in(cx) let global = JS_NewGlobalObject(cx,
                                                       &SIMPLE_GLOBAL_CLASS,
                                                       ptr::null_mut(),
                                                       h_option,
                                                       &c_option));
        let _ar = js::ar::AutoRealm::with_obj(cx, global.get());
        rooted!(in(cx) let prototype_proto = JS::GetRealmObjectPrototype(cx));
        rooted!(in(cx) let proto = JS_NewObjectWithGivenProto(cx,
                                                              &CLASS as *const _,
                                                              prototype_proto.handle()));
        define_methods(cx, proto.handle(), &METHODS[..]).unwrap();
    }
}

unsafe extern "C" fn generic_method(_: *mut JSContext, _: u32, _: *mut JS::Value) -> bool {
    true
}

lazy_static! {
    static ref METHODS: [JSFunctionSpec; 4] = [
        JSFunctionSpec {
            name: JSFunctionSpec_Name {
                string_: b"addEventListener\0" as *const u8 as *const libc::c_char,
            },
            call: JSNativeWrapper {
                op: Some(generic_method),
                info: ptr::null()
            },
            nargs: 2,
            flags: JSPROP_ENUMERATE as u16,
            selfHostedName: 0 as *const libc::c_char
        },
        JSFunctionSpec {
            name: JSFunctionSpec_Name {
                string_: b"removeEventListener\0" as *const u8 as *const libc::c_char,
            },
            call: JSNativeWrapper {
                op: Some(generic_method),
                info: ptr::null()
            },
            nargs: 2,
            flags: JSPROP_ENUMERATE as u16,
            selfHostedName: 0 as *const libc::c_char
        },
        JSFunctionSpec {
            name: JSFunctionSpec_Name {
                string_: b"dispatchEvent\0" as *const u8 as *const libc::c_char,
            },
            call: JSNativeWrapper {
                op: Some(generic_method),
                info: ptr::null()
            },
            nargs: 1,
            flags: JSPROP_ENUMERATE as u16,
            selfHostedName: 0 as *const libc::c_char
        },
        JSFunctionSpec {
            name: JSFunctionSpec_Name {
                string_: ptr::null(),
            },
            call: JSNativeWrapper {
                op: None,
                info: ptr::null()
            },
            nargs: 0,
            flags: 0,
            selfHostedName: ptr::null()
        }
    ];
}

static CLASS: JSClass = JSClass {
    name: b"EventTargetPrototype\0" as *const u8 as *const libc::c_char,
    flags: 0,
    cOps: 0 as *const _,
    spec: 0 as *mut _,
    ext: 0 as *mut _,
    oOps: 0 as *mut _,
};