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

Introduce feature to only allow initialization once. #447

Merged
merged 1 commit into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mozjs"
description = "Rust bindings to the Mozilla SpiderMonkey JavaScript engine."
repository = "https://github.com/servo/rust-mozjs"
version = "0.9.3"
version = "0.9.4"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I'm confused is that I've bumped the version to 0.9.4 at c16e7e2 but GitHub didn't show it 🤔

version = "0.9.4"

authors = ["The Servo Project Developers"]
build = "build.rs"
license = "MPL-2.0"
Expand Down Expand Up @@ -42,6 +42,7 @@ doctest = false

[features]
debugmozjs = ['mozjs_sys/debugmozjs']
init_once = []

[dependencies]
lazy_static = "1"
Expand Down
12 changes: 9 additions & 3 deletions src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ lazy_static! {
static ref PARENT: AtomicPtr<JSRuntime> = AtomicPtr::new(ptr::null_mut());
static ref OUTSTANDING_RUNTIMES: AtomicUsize = AtomicUsize::new(0);
static ref SHUT_DOWN: AtomicBool = AtomicBool::new(false);
static ref JS_INIT_CALLED: AtomicBool = AtomicBool::new(false);
}

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

if OUTSTANDING_RUNTIMES.fetch_sub(1, Ordering::SeqCst) == 1 {
PARENT.store(ptr::null_mut(), Ordering::SeqCst);
SHUT_DOWN.store(true, Ordering::SeqCst);
JS_ShutDown();
if cfg!(not(feature = "init_once")) {
SHUT_DOWN.store(true, Ordering::SeqCst);
JS_ShutDown();
}
}
}
}
Expand Down