Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark Runtime::new() as unsafe. #253

Merged
merged 1 commit into from Apr 18, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -112,42 +112,45 @@ pub struct Runtime {

impl Runtime {
/// Creates a new `JSRuntime` and `JSContext`.
pub fn new() -> Runtime {
unsafe {
let js_runtime = JS_NewRuntime(default_heapsize, ChunkSize as u32, ptr::null_mut());
assert!(!js_runtime.is_null());

// Unconstrain the runtime's threshold on nominal heap size, to avoid
// triggering GC too often if operating continuously near an arbitrary
// finite threshold. This leaves the maximum-JS_malloc-bytes threshold
// still in effect to cause periodical, and we hope hygienic,
// last-ditch GCs from within the GC's allocator.
JS_SetGCParameter(js_runtime, JSGCParamKey::JSGC_MAX_BYTES, u32::MAX);

JS_SetNativeStackQuota(js_runtime,
STACK_QUOTA,
STACK_QUOTA - SYSTEM_CODE_BUFFER,
STACK_QUOTA - SYSTEM_CODE_BUFFER - TRUSTED_SCRIPT_BUFFER);

let js_context = JS_NewContext(js_runtime, default_stacksize as size_t);
assert!(!js_context.is_null());

let runtimeopts = RuntimeOptionsRef(js_runtime);
let contextopts = ContextOptionsRef(js_context);

(*runtimeopts).set_varObjFix_(true);
(*runtimeopts).set_baseline_(true);
(*runtimeopts).set_ion_(true);
(*runtimeopts).set_nativeRegExp_(true);

(*contextopts).set_dontReportUncaught_(true);
(*contextopts).set_autoJSAPIOwnsErrorReporting_(true);
JS_SetErrorReporter(js_runtime, Some(reportError));

Runtime {
rt: js_runtime,
cx: js_context,
}
///
/// # Safety
///
/// Calling this function concurrently can cause segfaults inside
/// SpiderMonkey
pub unsafe fn new() -> Runtime {
let js_runtime = JS_NewRuntime(default_heapsize, ChunkSize as u32, ptr::null_mut());
assert!(!js_runtime.is_null());

// Unconstrain the runtime's threshold on nominal heap size, to avoid
// triggering GC too often if operating continuously near an arbitrary
// finite threshold. This leaves the maximum-JS_malloc-bytes threshold
// still in effect to cause periodical, and we hope hygienic,
// last-ditch GCs from within the GC's allocator.
JS_SetGCParameter(js_runtime, JSGCParamKey::JSGC_MAX_BYTES, u32::MAX);

JS_SetNativeStackQuota(js_runtime,
STACK_QUOTA,
STACK_QUOTA - SYSTEM_CODE_BUFFER,
STACK_QUOTA - SYSTEM_CODE_BUFFER - TRUSTED_SCRIPT_BUFFER);

let js_context = JS_NewContext(js_runtime, default_stacksize as size_t);
assert!(!js_context.is_null());

let runtimeopts = RuntimeOptionsRef(js_runtime);
let contextopts = ContextOptionsRef(js_context);

(*runtimeopts).set_varObjFix_(true);
(*runtimeopts).set_baseline_(true);
(*runtimeopts).set_ion_(true);
(*runtimeopts).set_nativeRegExp_(true);

(*contextopts).set_dontReportUncaught_(true);
(*contextopts).set_autoJSAPIOwnsErrorReporting_(true);
JS_SetErrorReporter(js_runtime, Some(reportError));

Runtime {
rt: js_runtime,
cx: js_context,
}
}

@@ -29,11 +29,10 @@ use std::str;
fn callback() {
unsafe {
JS_Init();
}
let runtime = Runtime::new();
let context = runtime.cx();

unsafe {
let runtime = Runtime::new();
let context = runtime.cx();

let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = CompartmentOptions::default();
let _ar = JSAutoRequest::new(context);
@@ -17,16 +17,18 @@ use std::ptr;

#[test]
fn evaluate() {
unsafe { assert!(JS_Init()); }
let rt = Runtime::new();
let cx = rt.cx();
let _ar = JSAutoRequest::new(cx);
unsafe {
assert!(JS_Init());
let rt = Runtime::new();
let cx = rt.cx();
let _ar = JSAutoRequest::new(cx);

let global = RootedObject::new(cx, unsafe {
JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS, ptr::null_mut(),
OnNewGlobalHookOption::FireOnNewGlobalHook,
&CompartmentOptions::default())
});
assert!(rt.evaluate_script(global.handle(), "1 + 1".to_owned(),
"test".to_owned(), 1).is_ok());
let global = RootedObject::new(cx,
JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS, ptr::null_mut(),
OnNewGlobalHookOption::FireOnNewGlobalHook,
&CompartmentOptions::default())
);
assert!(rt.evaluate_script(global.handle(), "1 + 1".to_owned(),
"test".to_owned(), 1).is_ok());
}
}
@@ -18,22 +18,21 @@ use std::ptr;
fn stack_limit() {
unsafe {
assert!(JS_Init());
}

let rt = Runtime::new();
let cx = rt.cx();
let _ar = JSAutoRequest::new(cx);
let rt = Runtime::new();
let cx = rt.cx();
let _ar = JSAutoRequest::new(cx);

let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = CompartmentOptions::default();
let global = unsafe {
JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS, ptr::null_mut(), h_option, &c_option)
};
let global_root = Rooted::new(cx, global);
let global = global_root.handle();
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = CompartmentOptions::default();
let global = JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS,
ptr::null_mut(), h_option, &c_option);
let global_root = Rooted::new(cx, global);
let global = global_root.handle();

assert!(rt.evaluate_script(global,
"function f() { f.apply() } f()".to_string(),
"test".to_string(),
1).is_err());
assert!(rt.evaluate_script(global,
"function f() { f.apply() } f()".to_string(),
"test".to_string(),
1).is_err());
}
}
@@ -24,37 +24,33 @@ use std::ptr;
fn vec_conversion() {
unsafe {
assert!(JS_Init());
}

let rt = Runtime::new();
let cx = rt.cx();
let _ar = JSAutoRequest::new(cx);
let rt = Runtime::new();
let cx = rt.cx();
let _ar = JSAutoRequest::new(cx);

let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = CompartmentOptions::default();
let global = unsafe {
JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS, ptr::null_mut(), h_option, &c_option)
};
let global_root = Rooted::new(cx, global);
let global = global_root.handle();
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = CompartmentOptions::default();
let global = JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS,
ptr::null_mut(), h_option, &c_option);
let global_root = Rooted::new(cx, global);
let global = global_root.handle();

let _ac = JSAutoCompartment::new(cx, global.get());
let _ac = JSAutoCompartment::new(cx, global.get());

let mut rval = RootedValue::new(cx, UndefinedValue());
let mut rval = RootedValue::new(cx, UndefinedValue());

let orig_vec: Vec<f32> = vec![1.0, 2.9, 3.0];
let converted = unsafe {
let orig_vec: Vec<f32> = vec![1.0, 2.9, 3.0];
orig_vec.to_jsval(cx, rval.handle_mut());
Vec::<f32>::from_jsval(cx, rval.handle(), ()).unwrap()
};
let converted = Vec::<f32>::from_jsval(cx, rval.handle(), ()).unwrap();

assert_eq!(orig_vec, converted);
assert_eq!(orig_vec, converted);

let orig_vec: Vec<i32> = vec![1, 2, 3];
let converted = unsafe {
let orig_vec: Vec<i32> = vec![1, 2, 3];
orig_vec.to_jsval(cx, rval.handle_mut());
Vec::<i32>::from_jsval(cx, rval.handle(), ConversionBehavior::Default).unwrap()
};
let converted = Vec::<i32>::from_jsval(cx, rval.handle(),
ConversionBehavior::Default).unwrap();

assert_eq!(orig_vec, converted);
assert_eq!(orig_vec, converted);
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.