From 06113f89993436e9a427380bf30d426248229ad1 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 22 Jul 2014 11:03:07 +0200 Subject: [PATCH 1/3] Link NSPR. In order to support threadsafe builds (which are necessary for thread-safety assertions and which will become the only available configuration in future versions), we need to link to NSPR for threading primitives. --- linkhack.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linkhack.rs b/linkhack.rs index 17e7c3692..26848b80e 100644 --- a/linkhack.rs +++ b/linkhack.rs @@ -6,12 +6,14 @@ #[cfg(target_os = "linux")] #[link(name = "pthread")] +#[link(name = "nspr4")] #[link(name = "js_static")] #[link(name = "stdc++")] #[link(name = "z")] extern { } #[cfg(target_os = "macos")] +#[link(name = "nspr4")] #[link(name = "js_static")] #[link(name = "stdc++")] #[link(name = "z")] From c61450fa669cc14aaada96ef8ec1bffc7d2e9846 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 22 Jul 2014 11:19:40 +0200 Subject: [PATCH 2/3] Implement JSAutoRequest. In threadsafe builds, we should enter a request whenever we call into SpiderMonkey. The JSAutoRequest class makes that simpler. --- rust.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust.rs b/rust.rs index df2c640f0..f3ccc650c 100644 --- a/rust.rs +++ b/rust.rs @@ -174,6 +174,7 @@ pub extern fn reportError(_cx: *mut JSContext, msg: *c_char, report: *mut JSErro pub fn with_compartment(cx: *mut JSContext, object: *mut JSObject, cb: || -> R) -> R { unsafe { + let _ar = JSAutoRequest::new(cx); let call = JS_EnterCrossCompartmentCall(cx, object); let result = cb(); JS_LeaveCrossCompartmentCall(call); @@ -181,6 +182,30 @@ pub fn with_compartment(cx: *mut JSContext, object: *mut JSObject, cb: || -> } } + +pub struct JSAutoRequest { + cx: *mut JSContext, +} + +impl JSAutoRequest { + pub fn new(cx: *mut JSContext) -> JSAutoRequest { + unsafe { + JS_BeginRequest(cx); + } + JSAutoRequest { + cx: cx, + } + } +} + +impl Drop for JSAutoRequest { + fn drop(&mut self) { + unsafe { + JS_EndRequest(self.cx); + } + } +} + #[cfg(test)] pub mod test { use super::rt; From ecec92c73bf061485e0ab4969f54f56dd4466fa2 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 22 Jul 2014 12:15:47 +0200 Subject: [PATCH 3/3] Remove the GC callback. Consumers should be using native rather than green tasks, to avoid calling into the same JSRuntime from multiple OS threads. For native tasks, SpiderMonkey can figure out the correct stack bounds for itself. Rust, on the other hand, apparently cannot; the existing code leads to crashes during conservative stack marking. --- rust.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/rust.rs b/rust.rs index f3ccc650c..2406069c8 100644 --- a/rust.rs +++ b/rust.rs @@ -6,9 +6,7 @@ use libc::types::os::arch::c95::{size_t, c_uint}; use libc::c_char; -use std::cmp; use std::rc; -use std::rt::Runtime; use jsapi::*; use jsval::{JSVal, NullValue}; use default_stacksize; @@ -18,7 +16,6 @@ use JSOPTION_METHODJIT; use JSOPTION_TYPE_INFERENCE; use ERR; use std::str::raw::from_c_str; -use green::task::GreenTask; // ___________________________________________________________________________ // friendly Rustic API to runtimes @@ -56,22 +53,9 @@ impl RtUtils for rc::Rc { } } -extern fn gc_callback(rt: *mut JSRuntime, _status: JSGCStatus) { - use std::rt::local::Local; - use std::rt::task::Task; - unsafe { - let mut task = Local::borrow(None::); - let green_task: Box = task.maybe_take_runtime().unwrap(); - let (start, end) = green_task.stack_bounds(); - JS_SetNativeStackBounds(rt, cmp::min(start, end), cmp::max(start, end)); - task.put_runtime(green_task); - } -} - pub fn rt() -> rt { unsafe { let runtime = JS_Init(default_heapsize); - JS_SetGCCallback(runtime, Some(gc_callback)); return new_runtime(runtime); } }