Skip to main content

Module serialize

Module serialize 

Source
Expand description

Serialisation of compiled bytecode.

The format is our own — it deliberately does not interoperate with the C implementation’s binary blobs — but it keeps the same semantics: a compiled function can be written out and read back without the source, which is what qjsc relies on for ahead-of-time compilation.

Layout, all integers big-endian:

magic    4 bytes  "QJSB"
version  u16      format version
function          one function record, recursively

function record:
  name        string (u32 length + UTF-8 bytes)
  kind        u8
  flags       u8    bit 0 = strict, bit 1 = arrow
  line, column u32 each
  defined_args u32  arity that counts towards `.length`
  args        u32 count, then name/flags records
  vars        u32 count, then name/flags/scope records
  closures    u32 count, then name/flags/type/index records
  cpool       u32 count, then constants
  code        u32 length + bytes
  stack_size  u32
  var_refs    u32
  positions   u32 count, then pc/line/column u32 each
  filename    optional string (tag u8: 0 none, 1 + string)
  source      optional string, present only when the writer kept it
  module      u8 tag: 0 = a script, 1 = module tables follow
                requests  u32 count, then specifier + attribute pairs
                imports   u32 count, then module/var_idx/name/is_star
                exports   u32 count, then a tagged entry

The last three are not bytecode. They are what the host needs in order to run the bytecode without the source: the file name a stack frame prints and a module records, the source text Function.prototype.toString reports (dropped unless the writer asks, which is what qjsc’s --keep-source is for), and the module tables — data the host wires imports up from before the body runs.

A version bump is required for any change to this layout; MAGIC plus VERSION make a stale blob fail loudly instead of being misinterpreted.

Structs§

Options
What to keep besides the bytecode.

Enums§

DecodeError
Why a blob could not be read.

Constants§

MAGIC
File magic.
VERSION
Format version. Bumped whenever the layout below changes.

Functions§

deserialize
Read a compiled function back.
serialize
Write a compiled function, including its nested functions, without its source text.
serialize_with
serialize, with control over what is kept.