pub struct JsContext { /* private fields */ }Expand description
JSContext: one interpreter, its pending exception, and its runtime back-pointer.
Implementations§
Source§impl JsContext
impl JsContext
Sourcepub fn raise_refs(&self)
pub fn raise_refs(&self)
JS_DupContext: one more release is owed before this context dies.
Sourcepub fn attached(runtime: *mut JsRuntime) -> JsContext
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.
Sourcepub fn set_opaque(&self, opaque: *mut c_void)
pub fn set_opaque(&self, opaque: *mut c_void)
JS_SetContextOpaque.
Sourcepub fn value(&self, idx: c_int) -> Option<Value>
pub fn value(&self, idx: c_int) -> Option<Value>
JS_GetContextValue: the value at slot idx, if one was stored.
Sourcepub fn with_current_interp<R>(
&self,
f: impl FnOnce(&mut Interp<'static>) -> R,
) -> R
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_interphas 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_interpcan 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.
Sourcepub fn with_context<R>(&self, f: impl FnOnce(&mut Context<'static>) -> R) -> R
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.
Sourcepub fn set_stack_limit(&self, limit: usize)
pub fn set_stack_limit(&self, limit: usize)
Set the interpreter’s stack budget, for a limit set after the context was created.
Sourcepub fn install_interrupt_hook(&self)
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.
Sourcepub fn with_interp<R>(&self, f: impl FnOnce(&mut Interp<'static>) -> R) -> R
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.
Sourcepub fn take_pending(&self) -> Option<Value>
pub fn take_pending(&self) -> Option<Value>
JS_GetException: take the pending exception, if there is one.
Sourcepub fn set_pending(&self, value: Value)
pub fn set_pending(&self, value: Value)
Store an exception for JS_GetException to return.
Sourcepub fn has_pending(&self) -> bool
pub fn has_pending(&self) -> bool
Whether a call left an exception behind.
Source§impl JsContext
impl JsContext
Sourcepub fn module_table(&self) -> &RefCell<Vec<*mut JsModuleDef>>
pub fn module_table(&self) -> &RefCell<Vec<*mut JsModuleDef>>
The C modules this context created.
Sourcepub fn throw_out_of_memory(&self) -> Value
pub fn throw_out_of_memory(&self) -> Value
A RangeError/InternalError for a failed allocation, as a pending exception.