Expand description
JSRuntime and JSContext: the objects C holds, and how a callback finds its way back.
§The re-entrancy problem
A C function installed by a module looks like this:
static JSValue js_point_ctor(JSContext *ctx, JSValueConst new_target,
int argc, JSValueConst *argv) {
JSPointData *s = js_mallocz(ctx, sizeof(*s));
if (JS_ToInt32(ctx, &s->x, argv[0])) goto fail;
JSValue obj = JS_NewObjectProtoClass(ctx, proto, js_point_class_id);
...
}Every line of it calls back into us with nothing but a JSContext *. Meanwhile the
engine is inside a call — it holds a &mut Interp while it invokes the function, and
that same interpreter is what JS_ToInt32 has to use. There is no second interpreter to
hand out.
So a callback needs a channel back to the live interpreter that does not go through the
Context (which is mutably borrowed for the whole call). That channel is a
thread-local raw pointer, set around each C function invocation
(InterpGuard) and read by the API functions (current_interp). It is
expressible only with unsafe, and it is the reason this crate is on the unsafe
allow-list — see docs/m15-plan.md §3.
§Why the context is boxed at a stable address
engine::Context<'rt> borrows the runtime. JS_NewContext therefore has to promise the
borrow outlives the context, and it does so the only way an FFI layer can: the runtime
lives in a Box owned by the JSRuntime, and the context keeps &'static Runtime to
it. Free order is enforced — JS_FreeRuntime drops every context before the runtime —
so the reference never dangles, and the 'static is a promise about the C handle’s
lifetime rather than a lie about the Rust types.
Structs§
- Class
Decl - One class, as the C side declared it.
- Interp
Guard - Publishes a live interpreter for the duration of a C callback.
- JsContext
JSContext: one interpreter, its pending exception, and its runtime back-pointer.- JsRuntime
- The engine runtime plus the tables whose lifetime is the runtime’s, not a context’s.
- Module
Loader Hooks - What
JS_SetModuleLoaderFunc/JS_SetModuleLoaderFunc2installed.
Enums§
- Context
Source - Where a context’s interpreter comes from.
Constants§
- FINALIZER_
SENTINEL - The payload of the
JSValuea finalizer is handed.
Functions§
- JS_Eval⚠
JS_Eval: compile and runinputas global (or module) code.- JS_
Free ⚠Context JS_FreeContext: drop one context, leaving its runtime alive.- JS_
Free ⚠Runtime JS_FreeRuntime: drop the runtime and every context it still owns.- JS_
GetException ⚠ JS_GetException: the pending exception as a new reference, ornullwhen there is none.- JS_
IsException JS_IsException— exported for the rare caller that takes its address.- JS_
NewContext ⚠ JS_NewContext.- JS_
NewRuntime JS_NewRuntime.- JS_
Throw ⚠ JS_Throw: replace the pending exception withobj, answeringJS_EXCEPTION.- allocate_
class_ id - Allocate a class id (
JS_NewClassID). - argument⚠
- Decode an argument a C caller passed in.
- current_
context - The context of the C code currently running.
- current_
interp - The interpreter of the C code currently running.
- finalizer_
opaque - The payload a finalizer’s sentinel value stands for, when
class_idmatches. - finalizer_
runtime - The runtime a finalizer is running for.
- is_
finalizer_ sentinel - Whether
valueis the sentinel a finalizer is handed. - publish
- Publish
interp(andcontext) for as long as the guard lives. - release⚠
- Release the reference a returned
JSValueheld, after taking what we need from it. - result_
value - Turn an engine result into the
JSValuea C caller expects. - throw_
error JS_ThrowRangeError-style error, stored as the pending exception ofcontext.