CurrentRealm

Struct CurrentRealm 

Source
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>

Source

pub fn assert(cx: &'cx mut JSContext) -> CurrentRealm<'cx>

Asserts that the current realm is valid and returns it.

Source

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.

Source

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);
}
Source

pub fn realm(&self) -> &NonNull<Realm>

Methods from Deref<Target = JSContext>§

Source

pub fn no_gc<'cx>(&'cx self) -> &'cx NoGC<'cx>

Returns NoGC token bounded to this JSContext. No function that accepts &mut JSContext (read: triggers GC) can be called while this is alive.

Source

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
}
Source

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>

Source§

type Target = JSContext

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<CurrentRealm<'cx> as Deref>::Target

Dereferences the value.
Source§

impl<'cx> DerefMut for CurrentRealm<'cx>

Source§

fn deref_mut(&mut self) -> &mut <CurrentRealm<'cx> as Deref>::Target

Mutably dereferences the value.
Source§

impl<'a, 'b> From<&'a mut CurrentRealm<'b>> for AlreadyInRealm

Source§

fn from(_: &'a mut CurrentRealm<'b>) -> AlreadyInRealm

Converts to this type from the input type.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsVoidPtr for T

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Filterable for T

Source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> MaybeBoxed<Box<T>> for T

Source§

fn maybe_boxed(self) -> Box<T>

Convert
Source§

impl<T> MaybeBoxed<T> for T

Source§

fn maybe_boxed(self) -> T

Convert
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T