Skip to content

Latest commit

 

History

History
67 lines (52 loc) · 2.84 KB

README.md

File metadata and controls

67 lines (52 loc) · 2.84 KB

small-python

Run Small Python, a small Python 3.15 engine written in C, from JavaScript. The engine is compiled to WebAssembly and each runtime runs in a Worker thread of its own: code in it sees only the functions and modules you give it, cannot touch files, the network, or your objects, and can be stopped at any time.

npm install small-python
import { Runtime } from "small-python";

const rt = await Runtime.create({ memoryLimit: 64 << 20, timeout: 5000 });
await rt.define("fetchUser", async (id) => ({ id, name: "Ada" }));
console.log(await rt.eval("fetchUser(7)['name'].upper()"));  // ADA
rt.close();

Values

Plain data is copied in both directions: null, booleans, numbers, bigints, strings, Uint8Array, arrays, Set, Map, and plain objects. A Python dict comes back as a Map, an int beyond 2⁵³ as a bigint. Functions you pass in become Python functions; Python functions, instances, and other values come back as PyObject proxies whose methods (get, call, getItem, ...) return promises.

Host functions are called with positional arguments. Wrap one with keywords(fn) to receive Python keyword arguments as a final object. They may be async; the engine waits.

Limits and safety

Option Effect
stepLimit RuntimeError in Python after that many steps per eval/exec
memoryLimit MemoryError in Python beyond that many bytes
outputLimit RuntimeError in Python after printing that many bytes per eval/exec
timeout terminates the worker after that many milliseconds; rejects with Timeout
messageLimit largest value that may cross between JavaScript and Python

A policy(event, args) function is asked before Python opens a file ("open", file and mode), imports a module for the first time ("import", name), executes source text with exec, eval or compile ("exec", source), or reads input ("input", prompt). Throwing refuses the event; an error not named after a Python exception raises PermissionError.

A crash or a trap in the engine ends only its Worker: the operation rejects with WorkerError and the runtime is closed.

Platforms

Node.js 20 or later. Where WebAssembly JSPI is available (Node.js 24 and later, current browsers), the worker waits on promises and needs nothing else; elsewhere it uses SharedArrayBuffer, so a browser page must be cross-origin isolated (COOP and COEP headers).

With JSPI, Runtime.create({ worker: false }) runs the engine on the calling thread, for runtimes without Worker threads such as Cloudflare Workers. The thread is busy while Python computes; timeout, stepLimit and memoryLimit still stop the engine, from inside.

The full specification is docs/packaging/javascript.md.