Skip to content

load

Loads and executes Do bytecode and registers runtime import handlers.

Types

TypeDescription
Bytecode Decoded and verified Do bytecode, produced by load.
BytecodeError Raised when bytes are not valid Do bytecode.
ImportHandler A registered runtime import handler.

Functions

import name -> Value

Imports a module through the VM's ordinary global import resolution.

Use this from a program-specific importer to fall back explicitly to native modules, cached modules, and registered import handlers.

Parameters

NameTypeDescription
name Str Module name to import.

Returns

The imported module value.

Errors

ImportError if no global importer accepts name.

Example

import load:
  import: global_import

class Virtual
  pub field answer = 42

let importer = do |name|
  if (name == "virtual")
    Virtual()
  else
    global_import $name

load.run $bytecode importer: $importer

import_handler callback -> ImportHandler

Registers a module import handler.

A resolution considers the handlers registered when it starts. Handlers registered during a callback participate in subsequent resolutions.

Handlers run after native modules and cached Do modules. The first successful handler supplies the imported value. Raise ImportError to decline a name; any other error aborts the import.

Parameters

NameTypeDescription
callback ((Str, <Value, >Value) -> Value) Called with the requested module name. Input ends immediately and output is discarded while the handler runs.

Example

class Demo
  pub field answer = 42

let handle = import_handler do |name|
  if (name == "demo")
    Demo()
  else
    throw std.ImportError(name)

import demo
assert_eq $demo.answer 42
handle.unregister()

load bytecode -> Bytecode

Decodes and verifies compiled Do bytecode without running it.

Parameters

NameTypeDescription
bytecode Bin Compiled Do bytecode.

Errors

BytecodeError if bytecode is not valid Do bytecode.

Example

let program = load $ (compile.compile "example.dol" "(1 + 1)").emit()
assert_eq $program.run() 2

run bytecode … -> Value

Executes compiled Do bytecode.

The loaded program uses the caller's input and output. Imports run with input ended and output discarded, including code executed by their importers.

The optional importer belongs to the loaded program and remains active for retained functions and modules. Imports call it directly, bypassing native modules, caching, and registered handlers. Results are not cached, and an ImportError does not trigger automatic fallback.

Parameters

NameTypeDescription
bytecode (Bin | Bytecode) Compiled Do bytecode, or bytecode already decoded by load.
:importer? ((Str, <Value, >Value) -> Value) Program-specific module importer, called with the requested module name. Input ends immediately and output is discarded while the importer runs.
< Iter[Value]
> Sink[Value]

Returns

The result of executing the bytecode.

Errors

Exception Condition
BytecodeError bytecode is a Bin that is not valid Do bytecode
StateError bytecode is a Bytecode that has already run

Example

let result = run $ (compile.compile "example.dol" "(1 + 1)").emit()
assert_eq $result 2