dart_monty_wasm

Part of dart_monty — pure Dart bindings for Monty, a restricted, sandboxed Python interpreter built in Rust.

Live Demo | GitHub | Monty

Bob This package is co-designed by human and AI — nearly all code is AI-generated.

Pure Dart web WASM implementation of dart_monty using dart:js_interop and @pydantic/monty. Runs the Monty Python interpreter in a Web Worker to avoid Chrome's synchronous WASM compile-size limit.

This package has no Flutter dependency and can be used in any Dart web project.

  • Most users should import dart_monty instead — it selects the native or web backend at compile time via conditional imports.
  • Direct usage is for projects that need explicit control over the web backend (e.g. custom bindings, testing).

Architecture

Dart (compiled to JS) -> MontyWasm (dart:js_interop)
  -> DartMontyBridge (monty_glue.js)
    -> Web Worker (dart_monty_worker.js)
      -> @pydantic/monty WASM (NAPI-RS)

Key Classes

Class Description
WasmBindings Abstract async interface for WASM operations
WasmBindingsJs Concrete JS interop implementation via Web Worker
MontyWasm MontyPlatform implementation using WasmBindings

Multi-Session Worker Pool (0.7.0)

Each session runs in its own Worker with isolated memory. NAPI-RS overhead has been stripped (asyncWorkPoolSize: 0, shared: false), reducing memory from 256 MB to 16 MB per session. Snapshots transfer as Uint8Array (binary, no JSON serialization). Cancellation calls Worker.terminate() for preemptive kill with full crash isolation.

Requirements

The web server must send COOP/COEP headers for SharedArrayBuffer support:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

Usage

import 'package:dart_monty_wasm/dart_monty_wasm.dart';

Future<void> main() async {
  final monty = MontyWasm();

  final result = await monty.run('2 + 2');
  print(result.value); // 4

  await monty.dispose();
}

See the main dart_monty repository for full documentation.

Libraries

dart_monty_wasm
Web WASM implementation of dart_monty.