Files
micropython/ports/webassembly
webreflection c72a3e528d
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
webassembly/objpyproxy: Avoid throwing on implicit symbols access.
This commit improves get handling by guarding against implicit unknown
symbols accessed directly by specific JS native APIs.

Fixes issue #17657.

Signed-off-by: Andrea Giammarchi <andrea.giammarchi@gmail.com>
2025-07-17 14:37:53 +10:00
..

MicroPython WebAssembly

MicroPython for WebAssembly.

Dependencies

Building the webassembly port bears the same requirements as the standard MicroPython ports with the addition of Emscripten, and optionally terser for the minified file.

The output includes micropython.mjs (a JavaScript wrapper for the MicroPython runtime) and micropython.wasm (actual MicroPython compiled to WASM).

Build instructions

In order to build micropython.mjs, run:

$ make

To generate the minified file micropython.min.mjs, run:

$ make min

Running with Node.js

Access the repl with:

$ make repl

This is the same as running:

$ node build-standard/micropython.mjs

The initial MicroPython GC heap size may be modified using:

$ node build-standard/micropython.mjs -X heapsize=64k

Where stack size may be represented in bytes, or have a k or m suffix.

MicroPython scripts may be executed using:

$ node build-standard/micropython.mjs hello.py

Alternatively micropython.mjs may by accessed by other JavaScript programs in node using the require command and the general API outlined below. For example:

const mp_mjs = await import("micropython.mjs");
const mp = await mp_mjs.loadMicroPython();

mp.runPython("print('hello world')");

Or without await notation:

import("micropython.mjs").then((mp_mjs) => {
    mp_mjs.loadMicroPython().then((mp) => {
        mp.runPython("print('hello world')");
    });
});

Running with HTML

The following code demonstrates the simplest way to load micropython.mjs in a browser, create an interpreter context, and run some Python code:

<!doctype html>
<html>
  <head>
    <script src="build-standard/micropython.mjs" type="module"></script>
  </head>
  <body>
    <script type="module">
      const mp = await loadMicroPython();
      mp.runPython("print('hello world')");
    </script>
  </body>
</html>

The output in the above example will go to the JavaScript console. It's possible to instead capture the output and print it somewhere else, for example in an HTML element. The following example shows how to do this, and also demonstrates the use of top-level await and the js module:

<!doctype html>
<html>
  <head>
    <script src="build-standard/micropython.mjs" type="module"></script>
  </head>
  <body>
    <pre id="micropython-stdout"></pre>
    <script type="module">
      const stdoutWriter = (line) => {
        document.getElementById("micropython-stdout").innerText += line + "\n";
      };
      const mp = await loadMicroPython({stdout:stdoutWriter});
      await mp.runPythonAsync(`
        import js
        url = "https://api.github.com/users/micropython"
        print(f"fetching {url}...")
        res = await js.fetch(url)
        json = await res.json()
        for i in dir(json):
          print(f"{i}: {json[i]}")
      `);
    </script>
  </body>
</html>

MicroPython code execution will suspend the browser so be sure to atomize usage within this environment. Unfortunately interrupts have not been implemented for the browser.

Testing

Run the test suite using:

$ make test

API

The following functions have been exposed to JavaScript through the interpreter context, created and returned by loadMicroPython().

  • PyProxy: the type of the object that proxies Python objects.

  • FS: the Emscripten filesystem object.

  • globals: an object exposing the globals from the Python __main__ module, with methods get(key), set(key, value) and delete(key).

  • registerJsModule(name, module): register a JavaScript object as importable from Python with the given name.

  • pyimport: import a Python module and return it.

  • runPython(code): execute Python code and return the result.

  • runPythonAsync(code): execute Python code and return the result, allowing for top-level await expressions (this call must be await'ed on the JavaScript side).

  • replInit(): initialise the REPL.

  • replProcessChar(chr): process an incoming character at the REPL.

  • replProcessCharWithAsyncify(chr): process an incoming character at the REPL, for use when ASYNCIFY is enabled.

Type conversions

Read-only objects (booleanns, numbers, strings, etc) are converted when passed between Python and JavaScript. The conversions are:

  • JavaScript null converts to/from Python None.
  • JavaScript undefined converts to/from Python js.undefined.

The conversion between null and None matches the behaviour of the Python json module.

Proxying

A Python dict instance is proxied such that:

for (const key in dict) {
    print(key, dict[key]);
}

works as expected on the JavaScript side and iterates through the keys of the Python dict. Furthermore, when JavaScript accesses a key that does not exist in the Python dict, the JavaScript code receives undefined instead of a KeyError exception being raised.