Skip to main content

Overview

The Monty class is the main entry point for executing sandboxed Python code in JavaScript/TypeScript. It parses Python code and provides methods to execute it with configurable inputs, external functions, and resource limits.

Constructor

Creates a new Monty interpreter instance by parsing the given Python code.
string
required
The Python code to parse and prepare for execution
MontyOptions
Optional configuration for the interpreter
Throws:
  • MontySyntaxError - If the code has syntax errors
  • MontyTypingError - If type checking is enabled and finds errors

Example

Methods

run()

Executes the parsed Python code and returns the result of the last expression.
RunOptions
Optional execution configuration
any
The result of the last expression in the Python code
Throws:
  • MontyRuntimeError - If the code raises an exception during execution
Example:

start()

Starts iterative execution, pausing at external function calls or name lookups. This provides fine-grained control over execution.
StartOptions
Same as RunOptions - optional execution configuration
MontySnapshot | MontyNameLookup | MontyComplete
  • MontySnapshot - Execution paused at an external function call
  • MontyNameLookup - Execution paused at an undefined name lookup
  • MontyComplete - Execution completed successfully
Throws:
  • MontyRuntimeError - If the code raises an exception
Example:

typeCheck()

Performs static type checking on the code.
string
Optional code to prepend before type checking (e.g., type definitions)
Throws:
  • MontyTypingError - If type checking finds errors
Example:

dump()

Serializes the Monty instance to a binary format. This allows you to save the parsed code and avoid re-parsing later.
Buffer
Binary representation of the Monty instance
Example:

load() (static)

Deserializes a Monty instance from binary format.
Buffer
required
Binary data from dump()
Monty
Restored Monty instance ready to execute
Example:

Properties

scriptName

Returns the script name used in tracebacks and error messages.

inputs

Returns the array of declared input variable names.

Helper Function: runMontyAsync()

Helper function that runs a Monty script with support for both synchronous and asynchronous external functions.
Monty
required
The Monty instance to execute
RunMontyAsyncOptions
any
The output of the Monty script
Example:

MontyRepl

The MontyRepl class provides an incremental, no-replay REPL (Read-Eval-Print Loop) session. Each feed() call compiles and executes only the provided snippet against preserved heap/global state.

create() (static method)

Creates a REPL session directly from source code.
string
required
Initial Python code to execute
MontyOptions
Configuration options (scriptName, inputs, typeCheck, etc.)
StartOptions
Execution options for the initial code (inputs, limits)
MontyRepl
A new REPL session instance
Throws:
  • MontySyntaxError - If the code has syntax errors
  • MontyRuntimeError - If execution fails
  • MontyTypingError - If type checking is enabled and finds errors

scriptName (property)

Returns the script name for this REPL session.

feed()

Executes one incremental snippet and returns its output. The snippet is compiled and executed against the current session state without replaying previous code.
string
required
Python code snippet to execute
any
The result of the code snippet
Throws:
  • MontyRuntimeError - If execution fails
Example:

dump()

Serializes the REPL session to bytes for later restoration.
Buffer
Serialized REPL session data

load() (static method)

Restores a REPL session from bytes.
Buffer
required
Serialized REPL data from dump()
MontyRepl
Restored REPL session
Throws:
  • ValueError - If deserialization fails

repr()

Returns a string representation of the REPL session.