Skip to main content

JsContext

Struct JsContext 

Source
pub struct JsContext { /* private fields */ }
Expand description

JSContext: one interpreter, its pending exception, and its runtime back-pointer.

Implementations§

Source§

impl JsContext

Source

pub fn raise_refs(&self)

JS_DupContext: one more release is owed before this context dies.

Source

pub unsafe fn new(runtime: *mut JsRuntime) -> *mut JsContext

JS_NewContext.

§Safety

runtime must be a live pointer from Box::into_raw(JsRuntime::new()).

Source

pub fn attached(runtime: *mut JsRuntime) -> JsContext

A context standing for whatever interpreter is running.

Used by the native module loader: js_init_module is called from inside a script’s import, so the JSContext * it receives has to address the interpreter that is already executing. The returned handle borrows nothing for the same reason — it has no interpreter of its own, and only ever reads the published one.

Source

pub fn opaque(&self) -> *mut c_void

JS_GetContextOpaque.

Source

pub fn set_opaque(&self, opaque: *mut c_void)

JS_SetContextOpaque.

Source

pub fn value(&self, idx: c_int) -> Option<Value>

JS_GetContextValue: the value at slot idx, if one was stored.

Source

pub fn set_value(&self, idx: c_int, value: Value)

JS_SetContextValue.

Source

pub fn runtime(&self) -> *mut JsRuntime

The runtime this context belongs to.

Source

pub fn with_current_interp<R>( &self, f: impl FnOnce(&mut Interp<'static>) -> R, ) -> R

Run f with the interpreter a C API call should use.

Two cases, and they are the whole re-entrancy story:

  • a C callback is running — current_interp has the interpreter the engine is inside, and re-borrowing this context’s would panic;
  • nothing is running — the call came from C at top level, so this context’s own interpreter is the right one and JsContext::with_interp can lend it out.

Every exported function that has to run JavaScript goes through here, which is why none of them carries its own “am I inside a callback” check.

Source

pub fn with_context<R>(&self, f: impl FnOnce(&mut Context<'static>) -> R) -> R

Run f with the engine context, publishing its interpreter for the C code f may call.

Every entry point — JS_Eval, and later JS_Call — goes through here rather than straight to the engine. Without the guard a script that calls a C function arrives with no current context, and every API call inside that function answers an exception it cannot even describe. The failure looks like a broken C module rather than a missed guard, which is why this is the one helper everything funnels through.

Source

pub fn set_stack_limit(&self, limit: usize)

Set the interpreter’s stack budget, for a limit set after the context was created.

Source

pub fn install_interrupt_hook(&self)

Install the interrupt closure into this context’s interpreter, if it has one.

Called from JS_SetInterruptHandler, because a context that already evaluated something owns its interpreter and would otherwise never see a handler set afterwards.

Source

pub fn with_interp<R>(&self, f: impl FnOnce(&mut Interp<'static>) -> R) -> R

Run f with the interpreter, publishing it for the C code f may call.

The guard is what makes a nested API call work: while f runs, JS_* functions called from C find this interpreter through current_interp. f receives the same &mut the guard published, so nothing is aliased that f itself did not hand to C.

Source

pub fn take_pending(&self) -> Option<Value>

JS_GetException: take the pending exception, if there is one.

Source

pub fn set_pending(&self, value: Value)

Store an exception for JS_GetException to return.

Source

pub fn has_pending(&self) -> bool

Whether a call left an exception behind.

Source§

impl JsContext

Source

pub fn module_table(&self) -> &RefCell<Vec<*mut JsModuleDef>>

The C modules this context created.

Source

pub fn throw_out_of_memory(&self) -> Value

A RangeError/InternalError for a failed allocation, as a pending exception.

Auto Trait Implementations§

§

impl !Freeze for JsContext

§

impl !RefUnwindSafe for JsContext

§

impl !Send for JsContext

§

impl !Sync for JsContext

§

impl !UnwindSafe for JsContext

§

impl Forget for JsContext

§

impl Move for JsContext

§

impl Unpin for JsContext
where Box<Context<'static>>: Unpin, *const GcBox<JsString> is !null: Unpin, *const GcBox<JsObject> is !null: Unpin, *const GcBox<JsSymbol> is !null: Unpin, *const GcBox<BigInt> is !null: Unpin, *mut JsRuntime: Unpin, *const u8 is !null: Unpin, *mut JsModuleDef: Unpin, *mut c_void: Unpin, *const LeafNode<i32, Value> is !null: Unpin, Box<(i32, Value)>: Unpin,

§

impl UnsafeUnpin for JsContext

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.