Skip to main content

Bytecode Operations

QuickJS supports loading and executing pre-compiled bytecode, which is faster than parsing source code and allows for smaller executables when the compiler is not needed.

js_std_eval_binary

Evaluate compiled bytecode from quickjs-libc.

Parameters

  • ctx - The JavaScript context
  • buf - Pointer to bytecode buffer
  • buf_len - Length of bytecode buffer
  • flags - Load flags

Example

Low-Level Bytecode API

JS_WriteObject

Serialize a JavaScript value to bytecode.

Parameters

  • ctx - The JavaScript context
  • psize - Pointer to receive the bytecode size
  • obj - The value to serialize (typically a function or module)
  • flags - Write flags

Write Flags

Returns

Returns a buffer containing the bytecode (must be freed with js_free()), or NULL on error.

Example

JS_ReadObject

Deserialize bytecode to a JavaScript value.

Parameters

  • ctx - The JavaScript context
  • buf - Pointer to bytecode buffer
  • buf_len - Length of bytecode
  • flags - Read flags

Read Flags

Returns

Returns the deserialized value (function or module), or JS_EXCEPTION on error.

Example

JS_EvalFunction

Execute a bytecode function or module.
See the JS_Eval documentation for details on evaluation flags.

Module Bytecode

JS_ResolveModule

Load the dependencies of a module.

Parameters

  • ctx - The JavaScript context
  • obj - A module object (from JS_ReadObject())

Returns

Returns 0 on success, -1 on error.

Example

Complete Example: Compile and Execute

Using qjsc Compiler

The qjsc tool compiles JavaScript to C code with embedded bytecode:
Generated C code:
Use in your program:

Security Considerations

Warning: The bytecode format is linked to a specific QuickJS version. No security checks are performed before execution. Only load bytecode from trusted sources.
  • Bytecode is version-specific
  • No signature verification
  • No sandboxing of bytecode execution
  • Malicious bytecode can compromise the runtime

Notes

  • Bytecode is faster to load than source code (no parsing)
  • Smaller executables when compiler is excluded
  • Use JS_WRITE_OBJ_STRIP_SOURCE and JS_WRITE_OBJ_STRIP_DEBUG to reduce bytecode size
  • Bytecode format may change between QuickJS versions
  • For modules, use JS_ResolveModule() before JS_EvalFunction()