Expand description
JSValue: the 8-byte NaN-boxed value the C API hands across the boundary.
§Why this has to be bit-compatible
Half of quickjs.h’s “functions” are static inline and expand in the caller:
static inline void JS_FreeValue(JSContext *ctx, JSValue v) {
if (JS_VALUE_HAS_REF_COUNT(v)) {
JSRefCountHeader *p = __js_rc(JS_VALUE_GET_PTR(v)); /* (uint32_t *)ptr - 1 */
if (--p->ref_count <= 0) __JS_FreeValue(ctx, v);
}
}So compiled C code never asks us how a JSValue is laid out — it decodes the bits
itself and writes through the pointer. A handle table (a JSValue that is really an
index) segfaults on the first JS_DupValue, which is why this module reproduces the
reference’s encoding exactly rather than round-tripping through a private form.
Everything here is integer arithmetic: no unsafe, and no dependence on the engine’s
own 16-byte Value. This is the bit half of the boundary; crate::alloc is the
memory half.
§Layout
bits 47..64 are the tag, read as a signed 32-bit integer; bits 0..47 are the
payload. A negative tag means the payload is a pointer to a reference-counted
allocation. Floats fit because the tag range -8..=7 is checked first: anything
outside it is a double whose bits had FLOAT64_ADDEND subtracted on the way in.
The constants below are the reference’s, and tests/abi.rs pins each one against a
value gcc printed for the same expression.
Structs§
- JsValue
- A
JSValue: the eight bytes the C API passes by value.
Constants§
- ENGINE_
BOX_ TAG - The tag a reference-counted engine value is boxed under.
- ENGINE_
STRING_ TAG - The tag an engine string is boxed under.
- EXCEPTION
JS_EXCEPTION.- FALSE
JS_FALSE.- FLOA
T64_ ADDEND JS_FLOAT64_ADDEND: what a double’s bits have added to them so the result lands outside the integer tag range.- NAN
JS_NAN: the canonical NaN. Every NaN is normalised to this one value, which is what makesNaN === NaNbehaviour depend on the interpreter rather than on bit patterns.- NULL
JS_NULL.- PAYLOAD_
MASK - The payload mask:
(1 << TAG_SHIFT) - 1. - TAG_
BIG_ INT JS_TAG_BIG_INT.- TAG_
BOOL JS_TAG_BOOL.- TAG_
CATCH_ OFFSET JS_TAG_CATCH_OFFSET: the reference’s operand-stack marker for acatchtarget.- TAG_
EXCEPTION JS_TAG_EXCEPTION: the value a failed call answers. Never observable from script.- TAG_
FIRST JS_TAG_FIRST: the most negative tag, and the first one that carries a reference count.- TAG_
FLOA T64 JS_TAG_FLOAT64.- TAG_
FUNCTION_ BYTECODE JS_TAG_FUNCTION_BYTECODE: internal to the reference’s bytecode objects.- TAG_INT
JS_TAG_INT.- TAG_
MODULE JS_TAG_MODULE: internal to the reference’s module table.- TAG_
NULL JS_TAG_NULL.- TAG_
OBJECT JS_TAG_OBJECT.- TAG_
SHIFT JS_NAN_BOX_TAG_SHIFT: how far the tag is shifted.- TAG_
SHORT_ BIG_ INT JS_TAG_SHORT_BIG_INT: aBigIntwhose value fits in 32 bits.- TAG_
STRING JS_TAG_STRING.- TAG_
STRING_ ROPE JS_TAG_STRING_ROPE: the reference’s concatenation node. Never produced here — the engine’s strings are flat — but every reader inquickjs.hknows the tag, so a value arriving with it must be recognised rather than mistaken for a double.- TAG_
SYMBOL JS_TAG_SYMBOL.- TAG_
UNDEFINED JS_TAG_UNDEFINED.- TAG_
UNINITIALIZED JS_TAG_UNINITIALIZED: aletbefore its declaration ran.- TRUE
JS_TRUE.- UNDEFINED
JS_UNDEFINED.- UNINITIALIZED
JS_UNINITIALIZED.
Functions§
- float64_
bits - The encoding
new_float64performs, exposed because the allocator’s tests and the ABI table want the bits rather than a value. - is_
engine_ tag - Whether a C value’s tag is one this layer can decode back into an engine value.
- new_
bool JS_NewBool.- new_
float64 JS_NewFloat64(which is the inline__JS_NewFloat64): a double, with every NaN normalised toNAN.- new_
int32 JS_NewInt32: an integer, which is the bare payload with no tag bits at all.