Skip to main content

Module runtime

Module runtime 

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

ClassDecl
One class, as the C side declared it.
InterpGuard
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.
ModuleLoaderHooks
What JS_SetModuleLoaderFunc/JS_SetModuleLoaderFunc2 installed.

Enums§

ContextSource
Where a context’s interpreter comes from.

Constants§

FINALIZER_SENTINEL
The payload of the JSValue a finalizer is handed.

Functions§

JS_Eval⚠
JS_Eval: compile and run input as global (or module) code.
JS_FreeContext⚠
JS_FreeContext: drop one context, leaving its runtime alive.
JS_FreeRuntime⚠
JS_FreeRuntime: drop the runtime and every context it still owns.
JS_GetException⚠
JS_GetException: the pending exception as a new reference, or null when 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 with obj, answering JS_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_id matches.
finalizer_runtime
The runtime a finalizer is running for.
is_finalizer_sentinel
Whether value is the sentinel a finalizer is handed.
publish
Publish interp (and context) for as long as the guard lives.
release⚠
Release the reference a returned JSValue held, after taking what we need from it.
result_value
Turn an engine result into the JSValue a C caller expects.
throw_error
JS_ThrowRangeError-style error, stored as the pending exception of context.