Skip to main content

Module bjson

Module bjson 

Source
Expand description

JS_WriteObject/JS_ReadObject: a serialised copy of a value’s graph.

§The format is ours

The reference’s binary JSON is an implementation detail of its own bytecode compiler: an atom table, then a tree of tags, tuned so that a bundle of precompiled modules is small. Nothing outside the engine reads it, and tests/test_bjson.js — the acceptance driver — asserts a round trip (toStr(write(x)) == toStr(read(write(x)))) with no hard-coded bytes anywhere: the one place that printed hex is inside if (0).

So this writes its own format, and says so rather than pretending to be

header   : u8 version
tag      : one of the `TAG_*` bytes below
length   : unsigned LEB128

What it does keep from the reference is the property that makes the format worth having at all: with JS_WRITE_OBJ_REFERENCE, an object is written once and referred to by index afterwards, so a cycle survives a round trip and two properties that were the same object are still the same object afterwards.

§What cannot be written

A function, a module, a proxy, a regular expression or a host class instance has no serialisation: the reference answers TypeError: unsupported object class for exactly the same set (its default arm in JS_WriteObjectRec). Writing one would need something to point at in a process that may not have the code any more.

Constants§

READ_OBJ_BYTECODE
JS_READ_OBJ_BYTECODE: accepted and ignored, like WRITE_OBJ_BYTECODE.
READ_OBJ_REFERENCE
JS_READ_OBJ_REFERENCE: resolve TAG_REFERENCE against the objects read so far.
READ_OBJ_SAB
See WRITE_OBJ_SAB.
WRITE_OBJ_BSWAP
JS_WRITE_OBJ_BSWAP: byte-swapped output is a big-endian host’s problem, and this engine writes little-endian always. Accepted, ignored.
WRITE_OBJ_BYTECODE
JS_WRITE_OBJ_BYTECODE: accepted and ignored — this engine serialises values only.
WRITE_OBJ_REFERENCE
JS_WRITE_OBJ_REFERENCE: write shared objects once and refer to them by index.
WRITE_OBJ_SAB
JS_WRITE_OBJ_SAB/JS_READ_OBJ_SAB: no SharedArrayBuffer exists in this engine.

Functions§

JS_GetArrayBuffer⚠
JS_GetArrayBuffer: the bytes of an ArrayBuffer, for a C module to read.
JS_NewArrayBufferCopy⚠
JS_NewArrayBufferCopy: a new ArrayBuffer holding a copy of len bytes at buf.
JS_ReadObject⚠
JS_ReadObject: rebuild the value buf describes.
JS_WriteObject⚠
JS_WriteObject: serialise obj into a block the caller frees with js_free.
read
Read a value written by [write].
write
Serialise value.