Skip to main content

MontyRun

Primary interface for running Monty code. MontyRun supports two execution modes:
  • Simple execution: Use run() or run_no_limits() to run code to completion
  • Iterative execution: Use start() to start execution which will pause at external function calls and can be resumed later

Constructor

new()

Creates a new run snapshot by parsing the given code. This only parses and prepares the code - no heap or namespaces are created yet. Call run() with inputs to start execution.
String
required
The Python code to execute
&str
required
The script name for error messages
Vec<String>
required
Names of input variables
Returns the parsed MontyRun or an error if the code cannot be parsed

Execution Methods

run()

Executes the code to completion assuming no external functions or snapshotting. This is marginally faster than running with snapshotting enabled since we don’t need to track the position in code, but does not allow calling of external functions.
Vec<MontyObject>
required
Values to fill the first N slots of the namespace
impl ResourceTracker
required
Custom resource tracker implementation
&mut PrintWriter<'_>
required
Print output writer (mutably borrowed so Collect data is preserved)
Returns the final result value or an error if execution fails

run_no_limits()

Executes the code to completion with no resource limits, printing to stdout/stderr.
Vec<MontyObject>
required
Values to fill the first N slots of the namespace
Returns the final result value or an error if execution fails

start()

Starts execution with the given inputs and resource tracker, consuming self. For iterative execution, start() consumes self and returns a RunProgress:
  • RunProgress::FunctionCall(call) - external function call, call call.resume(return_value) to resume
  • RunProgress::Complete(value) - execution finished
This enables snapshotting execution state and returning control to the host application during long-running computations.
Vec<MontyObject>
required
Initial input values (must match length of input_names from new())
impl ResourceTracker
required
Resource tracker for the execution
&mut PrintWriter<'_>
required
Writer for print output
Returns a RunProgress enum indicating the next step in execution
Errors: Returns MontyException if:
  • The number of inputs doesn’t match the expected count
  • An input value is invalid (e.g., MontyObject::Repr)
  • A runtime error occurs during execution

Serialization Methods

dump()

Serializes the runner to a binary format. The serialized data can be stored and later restored with load(). This allows caching parsed code to avoid re-parsing on subsequent runs.
Returns the serialized bytes or an error if serialization fails

load()

Deserializes a runner from binary format.
&[u8]
required
The serialized runner data from dump()
Returns the deserialized MontyRun or an error if deserialization fails

Other Methods

code()

Returns the code that was parsed to create this snapshot.
Reference to the source code string