Bridging Python and JavaScript: Inside the PythonMonkey Runtime

PythonMonkey is a SpiderMonkey‑based JavaScript runtime embedded in Python that lets developers run JavaScript inside Python and vice‑versa, offering WebAssembly support, a Node‑like PMJS environment, and a roadmap of cross‑language features aimed at speeding development and reducing maintenance overhead.

21CTO
21CTO
21CTO
Bridging Python and JavaScript: Inside the PythonMonkey Runtime

PythonMonkey is an innovative JavaScript runtime embedded in Python, built on Mozilla’s SpiderMonkey engine.

Developed by the Canadian cloud‑computing startup Distributive, it aims to bridge the gap between the two most popular programming languages by allowing developers to run JavaScript code inside a Python process and vice‑versa, cutting maintenance costs.

Website: https://spidermonkey.dev/

Key Features

Fast, memory‑efficient execution using SpiderMonkey’s JIT compiler.

Developers can choose to write code in either JavaScript or Python.

Direct cross‑language calls: Python can invoke JavaScript and JavaScript can import Python packages.

Same‑process execution of JavaScript and Python VMs without serialization or pipes.

Python lists/dicts behave like JavaScript arrays/objects and vice‑versa.

Built‑in support for WebAssembly, allowing sandboxed execution of WASM modules from C, C++, Rust, etc.

PMJS, a Node.js‑like environment that enables JavaScript to call Python libraries.

Simple Code Example

The following "hello world" snippet shows how to evaluate JavaScript from Python and retrieve the result.

>>> import pythonmonkey as pm
>>> hello = pm.eval(''Hello World''.toUpperCase());
>>> print(hello)
'HELLO WORLD'

A more complex example passes Python's print function to JavaScript and calls it from Python.

>>> import pythonmonkey as pm
>>> hello = pm.eval("(func) => { func('Hello World!')}")
>>> hello(print)
Hello World!

Running a JavaScript file with PMJS: pmjs main.js Example main.js that calls Python’s print from JavaScript:

const pyPrint = python.eval("print");
pyPrint("Hello, World!"); // 输出“Hello, World!”

Project Goals

High speed and low memory usage.

Allow developers to prioritize either JavaScript or Python for writing code.

Enable calling JavaScript from Python.

Enable using Python libraries from JavaScript.

Run both VMs in the same process, eliminating serialization overhead.

Make Python data structures behave like JavaScript equivalents.

Development Highlights

Web Stack API

Implemented XMLHttpRequest API for compatibility with libraries such as socket.io.

Added timer functions (setInterval, clearInterval, setImmediate, clearImmediate, setTimeout, clearTimeout) with Node.js‑style Timeout objects.

Completed console API to match the Web specification.

Provided atob and btoa utilities.

Cross‑Language Casting

JavaScript can now wrap or proxy arbitrary Python objects.

Improved iterator support across languages.

Exception Handling

Full cross‑language stack traces.

Enhanced nested exception handling and promise rejection support.

Uncaught exception handler.

JavaScript Engine Updates

SpiderMonkey upgraded to the latest version, bringing new JS and WASM features and better performance.

Contributed a patch to SpiderMonkey to fix a bug.

Developer Experience

Embedded debuggers (pmdb, WTFPythonMonkey) improve debugging.

Better Python type hints and documentation.

Module System

The module system makes it easy to port JavaScript libraries to Python and vice‑versa, reducing the cost of maintaining duplicate codebases.

Roadmap

Future enhancements include ESM syntax support for importing Python modules in JavaScript, a full XMLHttpRequest implementation, an independent event loop not tied to Python, and Node.js API coverage (fs, path, process) to enable use of popular NPM packages such as express.js and socket.io.

PMJS is planned to evolve into a fully integrated Node.js replacement that also allows JavaScript to use Python packages.

Compared with related projects such as JS2PY, PyV8, and Metacall, PythonMonkey offers superior performance and a richer feature set.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptWebAssemblyRuntimeCross-languagePythonMonkeySpiderMonkey
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.