Skip to content

Commit

Permalink
Allow returning null context pointers from Runtime::get(). (#545)
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Matthews <[email protected]>
  • Loading branch information
jdm authored Dec 25, 2024
1 parent 1765a7c commit 0081fc4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 3 additions & 4 deletions mozjs/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::ffi::CStr;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
use std::ptr;
use std::ptr::{self, NonNull};
use std::slice;
use std::str;
use std::sync::atomic::{AtomicU32, Ordering};
Expand Down Expand Up @@ -303,10 +303,9 @@ pub struct Runtime {

impl Runtime {
/// Get the `JSContext` for this thread.
pub fn get() -> *mut JSContext {
pub fn get() -> Option<NonNull<JSContext>> {
let cx = CONTEXT.with(|context| context.get());
assert!(!cx.is_null());
cx
NonNull::new(cx)
}

/// Create a [`ThreadSafeJSContext`] that can detect when this `Runtime` is destroyed.
Expand Down
4 changes: 2 additions & 2 deletions mozjs/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ fn runtime() {
let (sender, receiver) = channel();
thread::spawn(move || {
let runtime = unsafe { Runtime::create_with_parent(parent) };
assert!(!Runtime::get().is_null());
assert!(Runtime::get().is_some());
drop(runtime);
let _ = sender.send(());
});
let _ = receiver.recv();
}

unsafe extern "C" fn finalize(_fop: *mut GCContext, _object: *mut JSObject) {
assert!(!Runtime::get().is_null());
assert!(Runtime::get().is_some());
}

static CLASS_OPS: JSClassOps = JSClassOps {
Expand Down

0 comments on commit 0081fc4

Please sign in to comment.