micropython/ports/webassembly
Damien George 625b17a410 webassembly: Implement runCLI() for a Node-based CLI.
This allows running MicroPython webassembly from the command line using:

    node micropython.mjs

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 14:31:25 +11:00
..
variants/standard webassembly: Clean up Makefile and add variant support. 2024-03-22 13:37:09 +11:00
Makefile webassembly: Implement runPythonAsync() for top-level async code. 2024-03-22 13:37:47 +11:00
README.md
api.js webassembly: Implement runCLI() for a Node-based CLI. 2024-03-22 14:31:25 +11:00
lexer_dedent.c webassembly: Add JavaScript proxying, and js and jsffi modules. 2024-03-22 13:37:47 +11:00
lexer_dedent.h webassembly: Add JavaScript proxying, and js and jsffi modules. 2024-03-22 13:37:47 +11:00
library.h webassembly: Enable time localtime, gmtime, time, time_ns. 2024-03-22 13:05:54 +11:00
library.js webassembly: Use POSIX write for output and add stderr. 2024-03-22 13:06:39 +11:00
main.c webassembly: Implement runPythonAsync() for top-level async code. 2024-03-22 13:37:47 +11:00
modjs.c webassembly: Add JavaScript proxying, and js and jsffi modules. 2024-03-22 13:37:47 +11:00
modjsffi.c webassembly: Add JavaScript proxying, and js and jsffi modules. 2024-03-22 13:37:47 +11:00
modtime.c webassembly: Enable time localtime, gmtime, time, time_ns. 2024-03-22 13:05:54 +11:00
mpconfigport.h webassembly: Implement runPythonAsync() for top-level async code. 2024-03-22 13:37:47 +11:00
mphalport.c webassembly: Use POSIX write for output and add stderr. 2024-03-22 13:06:39 +11:00
mphalport.h webassembly: Enable time localtime, gmtime, time, time_ns. 2024-03-22 13:05:54 +11:00
node_run.sh
objjsproxy.c webassembly: Implement runPythonAsync() for top-level async code. 2024-03-22 13:37:47 +11:00
objpyproxy.js webassembly: Implement runPythonAsync() for top-level async code. 2024-03-22 13:37:47 +11:00
proxy_c.c webassembly: Implement runPythonAsync() for top-level async code. 2024-03-22 13:37:47 +11:00
proxy_c.h webassembly: Add JavaScript proxying, and js and jsffi modules. 2024-03-22 13:37:47 +11:00
proxy_js.js webassembly: Implement runPythonAsync() for top-level async code. 2024-03-22 13:37:47 +11:00
qstrdefsport.h
wrapper.js webassembly: Move MP_JS_EPOCH init to library postset. 2024-03-22 13:05:54 +11:00

README.md

MicroPython WebAssembly

MicroPython for WebAssembly.

Dependencies

Building webassembly port bears the same requirements as the standard MicroPython ports with the addition of Emscripten (and uglify-js for the minified file).

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

Build instructions

In order to build micropython.js, run:

$ make

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

$ make min

Running with Node.js

Access the repl with:

$ node build/micropython.js

Stack size may be modified using:

$ node build/micropython.js -X stack=64K

Where stack size may be represented in Bytes, KiB or MiB.

MicroPython scripts may be executed using:

$ node build/micropython.js hello.py

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

var mp_js = require('./build/micropython.js');

mp_js_init(64 * 1024);
await mp_js_do_str("print('hello world')\n");

Running with HTML

The prerequisite for browser operation of micropython.js is to listen to the micropython-print event, which is passed data when MicroPython code prints something to stdout. The following code demonstrates basic functionality:

<!doctype html>
<html>
  <head>
    <script src="build/micropython.js"></script>
  </head>
  <body>
    <pre id="micropython-stdout"></pre>
    <script>
      document.addEventListener("micropython-print", function(e) {
        let output = document.getElementById("micropython-stdout");
        output.innerText += new TextDecoder().decode(e.detail);
      }, false);

      var mp_js_startup = Module["onRuntimeInitialized"];
      Module["onRuntimeInitialized"] = async function() {
        mp_js_startup();
        mp_js_init(64 * 1024);
        await mp_js_do_str("print('hello world')");
      };
    </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.

mp_js_init(stack_size)

Initialize MicroPython with the given stack size in bytes. This must be called before attempting to interact with MicroPython.

await mp_js_do_str(code)

Execute the input code. code must be a string.

mp_js_init_repl()

Initialize MicroPython repl. Must be called before entering characters into the repl.

await mp_js_process_char(char)

Input character into MicroPython repl. char must be of type number. This will execute MicroPython code when necessary.