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 LEB128What 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, likeWRITE_OBJ_BYTECODE.- READ_
OBJ_ REFERENCE JS_READ_OBJ_REFERENCE: resolveTAG_REFERENCEagainst 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: noSharedArrayBufferexists in this engine.
Functions§
- JS_
GetArray ⚠Buffer JS_GetArrayBuffer: the bytes of anArrayBuffer, for a C module to read.- JS_
NewArray ⚠Buffer Copy JS_NewArrayBufferCopy: a newArrayBufferholding a copy oflenbytes atbuf.- JS_
Read ⚠Object JS_ReadObject: rebuild the valuebufdescribes.- JS_
Write ⚠Object JS_WriteObject: serialiseobjinto a block the caller frees withjs_free.- read
- Read a value written by [
write]. - write
- Serialise
value.