This site describes solve-engine as it is on main: 2.43.0, which npm does not have yet. npm installs 2.40.0, so a page may show an answer that version does not give yet.
BytecodeBuilder
Defined in: packages/engine/src/parser/BytecodeBuilder.ts:190
Direct-to-bytecode compiler for the Pratt parser.
Accumulates opcodes, numeric constants, and string references during parsing, then produces a BytecodeProgram for VM execution. Supports:
- Standard build via build
- Zero-copy build into pre-allocated buffers via buildInto
- In-place reset for reuse without reallocation
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new BytecodeBuilder(pluginFunctionIndex?): BytecodeBuilder;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:214
The per-engine plugin function name -> registry index map, wired in by
the engine so emitPluginCall can resolve a name at emit time. The
same object reference is shared with every builder in an engine (the pool
and each ad-hoc nested-body builder), and the engine populates it as
packages register, so a parselet never sees the numeric index.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
pluginFunctionIndex? | ReadonlyMap<string, number> |
Returns
Section titled “Returns”BytecodeBuilder
Accessors
Section titled “Accessors”currentLength
Section titled “currentLength”Get Signature
Section titled “Get Signature”get currentLength(): number;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:372
Number of opcodes/operands emitted so far, used to compute jump targets before patchJump.
Returns
Section titled “Returns”number
pluginIndexMap
Section titled “pluginIndexMap”Get Signature
Section titled “Get Signature”get pluginIndexMap(): ReadonlyMap<string, number> | undefined;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:217
The name->index map, so a nested-body builder inherits the same resolution.
Returns
Section titled “Returns”ReadonlyMap<string, number> | undefined
readsDocument
Section titled “readsDocument”Get Signature
Section titled “Get Signature”get readsDocument(): boolean;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:286
Whether a call emitted into this builder since its last reset reads other lines of the document (PluginCallOptions.readsDocument), so a held expression can say why it is refused.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”build()
Section titled “build()”build(): BytecodeProgram;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:438
Build the accumulated opcodes/numbers/strings into a BytecodeProgram. Creates new TypedArrays, the builder can be reused after this call.
Returns
Section titled “Returns”buildInto()
Section titled “buildInto()”buildInto(buf?): BytecodeProgram;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:467
Build directly into a pre-allocated buffer for zero-copy VM consumption.
When buf is provided and large enough, writes into it and returns
subarray views (not copies), the returned TypedArrays share the
buffer’s underlying ArrayBuffer. The caller MUST NOT mutate the buffer
until the returned BytecodeProgram is no longer needed.
If the caller intends to cache the result, they must copy the TypedArrays
(e.g. new Uint8Array(program.opcodes)) before reusing the buffer pool.
When buf is omitted or too small, allocates fresh TypedArrays.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
buf? | { numbers: Float64Array; opcodes: Uint8Array; } |
buf.numbers? | Float64Array |
buf.opcodes? | Uint8Array |
Returns
Section titled “Returns”emitAnonymousBody()
Section titled “emitAnonymousBody()”emitAnonymousBody(params, program): number;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:409
Register a compiled map/reduce anonymous transform body, returning
its index into this program’s anonymousBodies side-table, the
caller emits that index as MAP_INVOKE/REDUCE_INVOKE’s operand via
emitIndex. Same MAX_CONSTANT_POOL_INDEX bound as
emitUserFunctionBody.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
params | string[] |
program | BytecodeProgram |
Returns
Section titled “Returns”number
Throws
Section titled “Throws”If more than 256 anonymous bodies are registered on one program.
emitByte()
Section titled “emitByte()”emitByte(b): void;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:367
Emit a raw byte (0-255), used for fixed small operands like argument counts.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
b | number |
Returns
Section titled “Returns”void
emitIndex()
Section titled “emitIndex()”emitIndex(idx): void;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:362
Emit a raw numeric operand (0-255) following an opcode, e.g. a
plugin-function index for CALL_PLUGIN, or an argument count. Unlike
emitOpcode, this does not go through the OpCode enum, so
package authors use this (not an unsafe cast to OpCode) to push
operands their own opcode handler expects to read positionally.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
idx | number |
Returns
Section titled “Returns”void
emitNumber()
Section titled “emitNumber()”emitNumber(n): void;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:311
Emit a numeric literal: interns n into the program’s constant pool
(deduplicated, like emitString) and writes its index into the
opcode stream (read back by the VM as e.g. PUSH_NUMBER <idx>).
Deduplicated so that the 256-entry pool counts distinct values rather
than occurrences: a long line of repeated literals used to exhaust it
long before it held 256 different numbers. Negative zero keeps its own
slot, because 1 / -0 is not 1 / 0; NaN shares one, since every NaN
reads the same.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
n | number |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If the pool would exceed 256 distinct entries.
emitOpcode()
Section titled “emitOpcode()”emitOpcode(op): void;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:291
Emit an OpCode instruction.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
op | OpCode |
Returns
Section titled “Returns”void
emitPluginCall()
Section titled “emitPluginCall()”emitPluginCall( name, argCount, options?): void;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:239
Emit a plugin-function call by NAME. The engine assigns each registered
pluginFunctions entry an index at registration; this resolves the name to
that index and emits CALL_PLUGIN + index + argCount. Package authors emit
through this rather than a hand-allocated index.
A call marks the program as one that may wait for data (BytecodeProgram.hasAsync), since a handler is allowed to return a
promise, and every held expression (the expression of solve, a
function body, a map transform) refuses such a program. A handler that
never returns a promise, one that only builds a value from its
arguments (the constants package attaching gravity’s unit), passes
{ synchronous: true }, and the call leaves that mark alone.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
name | string | The plugin function’s registered name. |
argCount | number | How many values the call takes from the stack. |
options? | PluginCallOptions | synchronous: the handler always answers at once. |
Returns
Section titled “Returns”void
emitString()
Section titled “emitString()”emitString(s): void;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:338
Emit a string literal: interns s into the program’s string pool
(deduplicated via stringIndex) and writes its index into the opcode
stream. Subject to the same constant-pool bound as emitNumber,
but since strings ARE deduplicated, only distinct string values count
against the limit.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
s | string |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If the string pool would exceed 256 distinct entries.
emitUserFunctionBody()
Section titled “emitUserFunctionBody()”emitUserFunctionBody( name, params, program): number;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:387
Register a compiled user-defined-function body, returning its index
into this program’s userFunctionBodies side-table, the caller emits
that index as DEFINE_USER_FUNCTION’s operand via emitIndex.
Subject to the same MAX_CONSTANT_POOL_INDEX bound as
emitNumber/emitString (the index itself is a single
opcode-stream byte), in practice a single line defines at most a
handful of functions, so this limit is never realistically reached.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
name | string |
params | string[] |
program | BytecodeProgram |
Returns
Section titled “Returns”number
Throws
Section titled “Throws”If more than 256 function bodies are registered on one program.
patchJump()
Section titled “patchJump()”patchJump(position, target): void;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:423
Overwrite a previously-emitted placeholder operand at position with the real jump target, once known.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
position | number |
target | number |
Returns
Section titled “Returns”void
reset()
Section titled “reset()”reset(): void;Defined in: packages/engine/src/parser/BytecodeBuilder.ts:497
Returns
Section titled “Returns”void