Skip to content

Commit 0081fc4

Browse files
authored
Allow returning null context pointers from Runtime::get(). (#545)
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
1 parent 1765a7c commit 0081fc4

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

mozjs/src/rust.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::ffi::CStr;
1212
use std::marker::PhantomData;
1313
use std::mem::MaybeUninit;
1414
use std::ops::{Deref, DerefMut};
15-
use std::ptr;
15+
use std::ptr::{self, NonNull};
1616
use std::slice;
1717
use std::str;
1818
use std::sync::atomic::{AtomicU32, Ordering};
@@ -303,10 +303,9 @@ pub struct Runtime {
303303

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

312311
/// Create a [`ThreadSafeJSContext`] that can detect when this `Runtime` is destroyed.

mozjs/tests/runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ fn runtime() {
4040
let (sender, receiver) = channel();
4141
thread::spawn(move || {
4242
let runtime = unsafe { Runtime::create_with_parent(parent) };
43-
assert!(!Runtime::get().is_null());
43+
assert!(Runtime::get().is_some());
4444
drop(runtime);
4545
let _ = sender.send(());
4646
});
4747
let _ = receiver.recv();
4848
}
4949

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

5454
static CLASS_OPS: JSClassOps = JSClassOps {

0 commit comments

Comments
 (0)