Skip to main content

Module jsvalue

Module jsvalue 

Source
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.
FLOAT64_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 makes NaN === NaN behaviour 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 a catch target.
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_FLOAT64
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: a BigInt whose 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 in quickjs.h knows 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: a let before its declaration ran.
TRUE
JS_TRUE.
UNDEFINED
JS_UNDEFINED.
UNINITIALIZED
JS_UNINITIALIZED.

Functions§

float64_bits
The encoding new_float64 performs, 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 to NAN.
new_int32
JS_NewInt32: an integer, which is the bare payload with no tag bits at all.