Skip to main content

Crate quickjs

Crate quickjs 

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

  • jsvalue is the bit half: tags, encode, decode. No unsafe.
  • alloc is the memory half: the header that puts ref_count four 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
ArrayBuffer and 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 JSTypedArrayEnum and the engine’s ElementType.
calls
Calling JavaScript from C: JS_Call, JS_Invoke, JS_CallConstructor, the JS_Is* predicates, and JS_NewCFunctionData.
cdefs
The C structs whose bytes C fills in and we read.
conversions
Conversions: the JS_To* family, and the constructors that make a JSValue out of a Rust primitive.
convert
The bridge: engine Value ⇄ C JsValue.
host
The host session: what makes a C function callable from a script qjs is 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_NewCModule and friends, and the dlopen that finds one.
objects
Objects, properties and C functions: the JS_NewObject family.
properties
Properties and atoms: the JS_GetPropertyInternal family, JS_DefineProperty*, JS_DeleteProperty*, and the atom table’s C surface.
runtime
JSRuntime and JSContext: 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_FreeValue⚠
__JS_FreeValue: drop a value whose count just reached zero.
__JS_FreeValueRT⚠
__JS_FreeValueRT: the runtime-level spelling of __JS_FreeValue.
js_free⚠
js_free: release a block from js_malloc, or do nothing for a null pointer.
js_free_rt⚠
js_free_rt: the runtime-level spelling of js_free.
js_malloc⚠
js_malloc: size bytes owned by the runtime, zeroed by nobody.
js_malloc_rt⚠
js_malloc_rt: the runtime-level spelling of js_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 of js_malloc_usable_size.
js_mallocz⚠
js_mallocz: js_malloc, zeroed.
js_mallocz_rt⚠
js_mallocz_rt: the runtime-level spelling of js_mallocz.
js_realloc⚠
js_realloc: resize a block from js_malloc, keeping its contents.
js_realloc2⚠
js_realloc2: js_realloc that also reports the slack.
js_realloc_rt⚠
js_realloc_rt: the runtime-level spelling of js_realloc.