pub(crate) struct CurrentRealm<'cx> {
cx: &'cx mut JSContext,
realm: NonNull<Realm>,
}Expand description
Represents the current realm of JSContext (top realm on realm stack).
Similarly to AutoRealm, while you can access this type via &mut/&mut
we know that this realm is current (on top of realm stack).
use mozjs::context::JSContext;
use mozjs::jsapi::JSObject;
use mozjs::realm::{AutoRealm, CurrentRealm};
use std::ptr::NonNull;
fn f(current_realm: &mut CurrentRealm, target: NonNull<JSObject>) {
let mut realm = AutoRealm::new(current_realm, target);
let cx: &mut JSContext = &mut *current_realm; // we cannot use current realm while it's not current
}Fields§
§cx: &'cx mut JSContext§realm: NonNull<Realm>Implementations§
Source§impl<'cx> CurrentRealm<'cx>
impl<'cx> CurrentRealm<'cx>
Sourcepub fn assert(cx: &'cx mut JSContext) -> CurrentRealm<'cx>
pub fn assert(cx: &'cx mut JSContext) -> CurrentRealm<'cx>
Asserts that the current realm is valid and returns it.
Sourcepub fn global(&self) -> Handle<'_, *mut JSObject>
pub fn global(&self) -> Handle<'_, *mut JSObject>
Obtain the handle to the global object of the this realm. Because the handle is bounded with lifetime to realm, you cannot do this:
use mozjs::context::JSContext;
use mozjs::jsapi::JSObject;
use mozjs::realm::CurrentRealm;
use std::ptr::NonNull;
use mozjs::rust::Handle;
fn g(realm: &'_ mut CurrentRealm, global: Handle<'_, *mut JSObject>) {
}
fn f(realm: &mut CurrentRealm) {
let global = realm.global();
g(realm, global);
}instead use CurrentRealm::global_and_reborrow.
Sourcepub fn global_and_reborrow(
&mut self,
) -> (Handle<'_, *mut JSObject>, &mut CurrentRealm<'cx>)
pub fn global_and_reborrow( &mut self, ) -> (Handle<'_, *mut JSObject>, &mut CurrentRealm<'cx>)
Obtain the handle to the global object of this realm and reborrow the realm.
use mozjs::context::JSContext;
use mozjs::jsapi::JSObject;
use mozjs::realm::CurrentRealm;
use std::ptr::NonNull;
use mozjs::rust::Handle;
fn g(realm: &'_ mut CurrentRealm, global: Handle<'_, *mut JSObject>) {
}
fn f(realm: &mut CurrentRealm) {
let (global, realm) = realm.global_and_reborrow();
g(realm, global);
}pub fn realm(&self) -> &NonNull<Realm>
Methods from Deref<Target = JSContext>§
Sourcepub unsafe fn raw_cx(&mut self) -> *mut JSContext
pub unsafe fn raw_cx(&mut self) -> *mut JSContext
Obtain RawJSContext mutable pointer.
§Safety
No NoGC tokens should be constructed while returned pointer is available to user. In practices this means that one should use the result as direct argument to SpiderMonkey function and not store it in variable.
use mozjs::context::*;
use mozjs::jsapi::JSContext as RawJSContext;
fn SM_function_that_can_trigger_gc(_cx: *mut RawJSContext) {}
fn can_trigger_gc(cx: &mut JSContext) {
unsafe { SM_function_that_can_trigger_gc(cx.raw_cx()) } // returned pointer is immediately used
cx.no_gc(); // this is ok because no outstanding raw pointer is alive
}Sourcepub unsafe fn raw_cx_no_gc(&self) -> *mut JSContext
pub unsafe fn raw_cx_no_gc(&self) -> *mut JSContext
Obtain RawJSContext mutable pointer, that will not be used for GC.
§Safety
No &mut calls should be done on JSContext while returned pointer is available. In practices this means that one should use the result as direct argument to SpiderMonkey function and not store it in variable.
use mozjs::context::*;
use mozjs::jsapi::JSContext as RawJSContext;
fn SM_function_that_cannot_trigger_gc(_cx: *mut RawJSContext) {}
fn f(cx: &mut JSContext) {
unsafe { SM_function_that_cannot_trigger_gc(cx.raw_cx_no_gc()) } // returned pointer is immediately used
}Trait Implementations§
Source§impl<'cx> Deref for CurrentRealm<'cx>
impl<'cx> Deref for CurrentRealm<'cx>
Source§impl<'cx> DerefMut for CurrentRealm<'cx>
impl<'cx> DerefMut for CurrentRealm<'cx>
Source§impl<'a, 'b> From<&'a mut CurrentRealm<'b>> for AlreadyInRealm
impl<'a, 'b> From<&'a mut CurrentRealm<'b>> for AlreadyInRealm
Source§fn from(_: &'a mut CurrentRealm<'b>) -> AlreadyInRealm
fn from(_: &'a mut CurrentRealm<'b>) -> AlreadyInRealm
Auto Trait Implementations§
impl<'cx> Freeze for CurrentRealm<'cx>
impl<'cx> RefUnwindSafe for CurrentRealm<'cx>
impl<'cx> !Send for CurrentRealm<'cx>
impl<'cx> !Sync for CurrentRealm<'cx>
impl<'cx> Unpin for CurrentRealm<'cx>
impl<'cx> !UnwindSafe for CurrentRealm<'cx>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Filterable for T
impl<T> Filterable for T
Source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more