Expand description
quickjs-capi — the QuickJS C API on top of the pure-Rust engine.
The library this crate produces is libquickjs.so, and it is meant to be a drop-in
replacement for the reference implementation’s at link time: same exported symbols,
same JSValue bit layout, same struct offsets. docs/m15-plan.md is the reasoning;
this is the code.
§How the two representations meet
The engine’s Value is a 16-byte enum. C’s JSValue is 8 bytes of NaN boxing. The
mapping has to be bit-compatible rather than a conversion at the boundary, because
quickjs.h ships 119 of its “functions” as static inline and they run in the
caller’s translation unit — JS_DupValue increments a count through a raw pointer that
only exists because we put it there.
jsvalueis the bit half: tags, encode, decode. Nounsafe.allocis the memory half: the header that putsref_countfour bytes before the payload, and the boxes that hold engine values.
§unsafe is the whole point of this crate
Everything a C caller can do to a JSValue — duplicate it, free it, read a field of a
struct we handed over — happens outside Rust’s type system. scripts/check-unsafe.sh
lists this crate as a deliberate exception for that reason, and each unsafe block
below carries the argument for why it is sound.
§Status
M15’s S2: the value/allocator layer plus the js_malloc family of exports. The
remaining symbols are S3–S7 in docs/m15-plan.md §5. This crate is publish = false
until that surface is complete — a libquickjs missing half its symbols is worse than
no libquickjs.
Re-exports§
pub use jsvalue::JsValue;pub use runtime::JsContext;pub use runtime::JsRuntime;pub use runtime::argument;pub use runtime::release;
Modules§
- alloc
- The allocator the C side shares with us, and the boxes that hold engine values.
- atoms
- The atom table:
JSAtom, the C API’s interned property keys. - bjson
JS_WriteObject/JS_ReadObject: a serialised copy of a value’s graph.- buffers
ArrayBufferand the typed-array views, from C.- builtins_
typed - The typed-array assembly the C API needs: build a buffer, build a view over one, and
translate between
JSTypedArrayEnumand the engine’sElementType. - calls
- Calling JavaScript from C:
JS_Call,JS_Invoke,JS_CallConstructor, theJS_Is*predicates, andJS_NewCFunctionData. - cdefs
- The C structs whose bytes C fills in and we read.
- conversions
- Conversions: the
JS_To*family, and the constructors that make aJSValueout of a Rust primitive. - convert
- The bridge: engine
Value⇄ CJsValue. - host
- The host session: what makes a C function callable from a script
qjsis running. - intrinsics
JS_AddIntrinsic*: installing the built-in objects a context needs.- jsvalue
JSValue: the 8-byte NaN-boxed value the C API hands across the boundary.- module_
api - The module API’s remaining half: private values, names, namespaces, the loader hooks, and
JS_EvalThis/JS_DetectModule/JS_WriteObject2. - modules
- C modules:
JS_NewCModuleand friends, and thedlopenthat finds one. - objects
- Objects, properties and C functions: the
JS_NewObjectfamily. - properties
- Properties and atoms: the
JS_GetPropertyInternalfamily,JS_DefineProperty*,JS_DeleteProperty*, and the atom table’s C surface. - runtime
JSRuntimeandJSContext: the objects C holds, and how a callback finds its way back.- state
- Runtime and context state: limits, GC, opaque pointers, context values, memory reporting
and
JS_PrintValue. - value_
helpers - The two directions of the bridge, as free functions.
Functions§
- __
JS_ ⚠Free Value __JS_FreeValue: drop a value whose count just reached zero.- __
JS_ ⚠Free ValueRT __JS_FreeValueRT: the runtime-level spelling of__JS_FreeValue.- js_free⚠
js_free: release a block fromjs_malloc, or do nothing for a null pointer.- js_
free_ ⚠rt js_free_rt: the runtime-level spelling ofjs_free.- js_
malloc ⚠ js_malloc:sizebytes owned by the runtime, zeroed by nobody.- js_
malloc_ ⚠rt js_malloc_rt: the runtime-level spelling ofjs_malloc.- js_
malloc_ ⚠usable_ size js_malloc_usable_size: how big a block actually is.- js_
malloc_ ⚠usable_ size_ rt js_malloc_usable_size_rt: the runtime-level spelling ofjs_malloc_usable_size.- js_
mallocz ⚠ js_mallocz:js_malloc, zeroed.- js_
mallocz_ ⚠rt js_mallocz_rt: the runtime-level spelling ofjs_mallocz.- js_
realloc ⚠ js_realloc: resize a block fromjs_malloc, keeping its contents.- js_
realloc2 ⚠ js_realloc2:js_reallocthat also reports the slack.- js_
realloc_ ⚠rt js_realloc_rt: the runtime-level spelling ofjs_realloc.