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

Commit 5baf536

Browse files
author
bors-servo
authored
Auto merge of #447 - paulrouget:initonce, r=jdm
Introduce feature to only allow initialization once. See #22039 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-mozjs/447) <!-- Reviewable:end -->
2 parents c16e7e2 + 2ccdca9 commit 5baf536

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 9 additions & 3 deletions
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)