Created
January 30, 2018 14:50
-
-
Save daschl/f42ab08db1c34d13ab001ac18ec674cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate mozjs; | |
use mozjs::jsapi::CompartmentOptions; | |
use mozjs::jsapi::JS_NewGlobalObject; | |
use mozjs::jsapi::OnNewGlobalHookOption; | |
use mozjs::jsval::{Int32Value, UndefinedValue}; | |
use mozjs::rust::{Runtime, SIMPLE_GLOBAL_CLASS}; | |
use std::ptr; | |
fn main() { | |
let rt = Runtime::new().unwrap(); | |
let cx = rt.cx(); | |
unsafe { | |
rooted!(in(cx) let global = | |
JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS, ptr::null_mut(), | |
OnNewGlobalHookOption::FireOnNewGlobalHook, | |
&CompartmentOptions::default()) | |
); | |
rooted!(in(cx) let mut rval = UndefinedValue()); | |
rt.evaluate_script( | |
global.handle(), | |
"function cb() { return 1 + 2; }", | |
"test", | |
1, | |
rval.handle_mut(), | |
).expect("Could not evaluate script"); | |
rooted!(in(cx) let mut rval2 = Int32Value(0)); | |
rt.evaluate_script(global.handle(), "cb()", "test", 1, rval2.handle_mut()) | |
.expect("Could not evaluate script"); | |
println!("{:?}", rval2.to_number()); // prints 3! :) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment