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

Commit 2ccdca9

Browse files
committed
Introduce feature to only allow initialisation once.
1 parent 26b270d commit 2ccdca9

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "mozjs"
33
description = "Rust bindings to the Mozilla SpiderMonkey JavaScript engine."
44
repository = "https://github.com/servo/rust-mozjs"
5-
version = "0.9.3"
5+
version = "0.9.4"
66
authors = ["The Servo Project Developers"]
77
build = "build.rs"
88
license = "MPL-2.0"
@@ -42,6 +42,7 @@ doctest = false
4242

4343
[features]
4444
debugmozjs = ['mozjs_sys/debugmozjs']
45+
init_once = []
4546

4647
[dependencies]
4748
lazy_static = "1"

src/rust.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ lazy_static! {
128128
static ref PARENT: AtomicPtr<JSRuntime> = AtomicPtr::new(ptr::null_mut());
129129
static ref OUTSTANDING_RUNTIMES: AtomicUsize = AtomicUsize::new(0);
130130
static ref SHUT_DOWN: AtomicBool = AtomicBool::new(false);
131+
static ref JS_INIT_CALLED: AtomicBool = AtomicBool::new(false);
131132
}
132133

133134
/// A wrapper for the `JSContext` structure in SpiderMonkey.
@@ -157,7 +158,10 @@ impl Runtime {
157158
let js_context = if outstanding == 0 {
158159
// We are creating the first JSContext, so we need to initialize
159160
// the runtime.
160-
assert!(JS_Init());
161+
if cfg!(not(feature = "init_once")) || !JS_INIT_CALLED.load(Ordering::SeqCst) {
162+
assert!(JS_Init());
163+
JS_INIT_CALLED.store(true, Ordering::SeqCst);
164+
}
161165
let js_context = JS_NewContext(default_heapsize, ChunkSize as u32, ptr::null_mut());
162166
let parent_runtime = JS_GetRuntime(js_context);
163167
assert!(!parent_runtime.is_null());
@@ -265,8 +269,10 @@ impl Drop for Runtime {
265269

266270
if OUTSTANDING_RUNTIMES.fetch_sub(1, Ordering::SeqCst) == 1 {
267271
PARENT.store(ptr::null_mut(), Ordering::SeqCst);
268-
SHUT_DOWN.store(true, Ordering::SeqCst);
269-
JS_ShutDown();
272+
if cfg!(not(feature = "init_once")) {
273+
SHUT_DOWN.store(true, Ordering::SeqCst);
274+
JS_ShutDown();
275+
}
270276
}
271277
}
272278
}

0 commit comments

Comments
 (0)