Back to Monty

Classes across the sandbox boundary

examples/classes/README.md

0.0.233.5 KB
Original Source

Classes across the sandbox boundary

One concise, runnable file per behaviour, in Python and TypeScript.

Python

Each needs a dev build of the bindings and worker (make dev-py), then:

bash
uv run python examples/classes/class_instance.py
FileBehaviour
class_instance.pyExpose a host object with an explicit policy; identity round-trip
lazy_attrs.pyAttributes fetched from the host on demand; denial raises AttributeError
sandbox_copy.pySandbox mutations stay on the sandbox copy; the host object is untouched
convert_value.pyTransform / wrap values as they cross the boundary (each wrap is retained for the session)
class_type.pyLet the sandbox instantiate a host class (init=True)
class_type_members.pyClass constants and classmethods without instantiation
sandbox_classes.pyClasses defined inside Monty; instances return as MontyClassProxy
sandbox_round_trip.pyA MontyClassProxy passed back resolves to the original sandbox object; freed objects raise
async_methods.pyasync def methods awaited from sandbox code via AsyncMonty

TypeScript

The examples import @pydantic/monty from this repo's JS crate, so build it (make build-js), install the local link, build the worker binary (cargo build -p monty-runtime, which make build-js does not) and point the workers at it. They run directly under Node 24+ (native type stripping and await using):

bash
cd examples/classes && npm install && cd -
cargo build -p monty-runtime && MONTY_BIN=$PWD/target/debug/monty node examples/classes/class_instance.ts

MONTY_BIN matters when another monty is on PATH: a worker from a different release speaks a different protocol version and the pool reports it as a crash. npx tsc -p examples/classes type-checks them.

FileBehaviour
class_instance.tsExpose a host object with an explicit policy; identity round-trip
lazy_attrs.tsAttributes fetched from the host on demand; denial raises AttributeError
class_type.tsLet the sandbox instantiate a host class (init: true)
sandbox_classes.tsClasses defined inside Monty; instances return as MontyClassProxy
sandbox_round_trip.tsA MontyClassProxy passed back resolves to the original sandbox object; freed objects raise