@@ -6,22 +6,22 @@ MicroPython for [WebAssembly](https://webassembly.org/).
6
6
Dependencies
7
7
------------
8
8
9
- Building webassembly port bears the same requirements as the standard
10
- MicroPython ports with the addition of Emscripten ( and uglify-js for the
11
- minified file) .
9
+ Building the webassembly port bears the same requirements as the standard
10
+ MicroPython ports with the addition of Emscripten, and optionally terser for
11
+ the minified file.
12
12
13
- The output includes ` micropython.js ` (a JavaScript wrapper for the
14
- MicroPython runtime) and ` firmware .wasm` (actual MicroPython compiled to
13
+ The output includes ` micropython.mjs ` (a JavaScript wrapper for the
14
+ MicroPython runtime) and ` micropython .wasm` (actual MicroPython compiled to
15
15
WASM).
16
16
17
17
Build instructions
18
18
------------------
19
19
20
- In order to build micropython.js , run:
20
+ In order to build ` micropython.mjs ` , run:
21
21
22
22
$ make
23
23
24
- To generate the minified file micropython.min.js , run:
24
+ To generate the minified file ` micropython.min.mjs ` , run:
25
25
26
26
$ make min
27
27
@@ -30,55 +30,90 @@ Running with Node.js
30
30
31
31
Access the repl with:
32
32
33
- $ node build/micropython.js
33
+ $ make repl
34
34
35
- Stack size may be modified using :
35
+ This is the same as running :
36
36
37
- $ node build/micropython.js -X stack=64K
37
+ $ node build-standard /micropython.mjs
38
38
39
- Where stack size may be represented in Bytes, KiB or MiB.
39
+ The initial MicroPython GC heap size may be modified using:
40
+
41
+ $ node build-standard/micropython.mjs -X heapsize=64k
42
+
43
+ Where stack size may be represented in bytes, or have a ` k ` or ` m ` suffix.
40
44
41
45
MicroPython scripts may be executed using:
42
46
43
- $ node build/micropython.js hello.py
47
+ $ node build-standard /micropython.mjs hello.py
44
48
45
- Alternatively micropython.js may by accessed by other javascript programs in node
49
+ Alternatively ` micropython.mjs ` may by accessed by other JavaScript programs in node
46
50
using the require command and the general API outlined below. For example:
47
51
48
52
``` javascript
49
- var mp_js = require (' ./build/micropython.js' );
53
+ const mp_mjs = await import (" micropython.mjs" );
54
+ const mp = await mp_mjs .loadMicroPython ();
55
+
56
+ mp .runPython (" print('hello world')" );
57
+ ```
50
58
51
- mp_js_init (64 * 1024 );
52
- await mp_js_do_str (" print('hello world')\n " );
59
+ Or without await notation:
60
+
61
+ ``` javascript
62
+ import (" micropython.mjs" ).then ((mp_mjs ) => {
63
+ mp_mjs .loadMicroPython ().then ((mp ) => {
64
+ mp .runPython (" print('hello world')" );
65
+ });
66
+ });
53
67
```
54
68
55
69
Running with HTML
56
70
-----------------
57
71
58
- The prerequisite for browser operation of micropython.js is to listen to the
59
- ` micropython-print ` event, which is passed data when MicroPython code prints
60
- something to stdout. The following code demonstrates basic functionality:
72
+ The following code demonstrates the simplest way to load ` micropython.mjs ` in a
73
+ browser, create an interpreter context, and run some Python code:
74
+
75
+ ``` html
76
+ <!doctype html>
77
+ <html >
78
+ <head >
79
+ <script src =" build-standard/micropython.mjs" type =" module" ></script >
80
+ </head >
81
+ <body >
82
+ <script type =" module" >
83
+ const mp = await loadMicroPython ();
84
+ mp .runPython (" print('hello world')" );
85
+ </script >
86
+ </body >
87
+ </html >
88
+ ```
89
+
90
+ The output in the above example will go to the JavaScript console. It's possible
91
+ to instead capture the output and print it somewhere else, for example in an
92
+ HTML element. The following example shows how to do this, and also demonstrates
93
+ the use of top-level await and the ` js ` module:
61
94
62
95
``` html
63
96
<!doctype html>
64
97
<html >
65
98
<head >
66
- <script src =" build/micropython.js " ></script >
99
+ <script src =" build-standard /micropython.mjs " type = " module " ></script >
67
100
</head >
68
101
<body >
69
102
<pre id =" micropython-stdout" ></pre >
70
- <script >
71
- document .addEventListener (" micropython-print" , function (e ) {
72
- let output = document .getElementById (" micropython-stdout" );
73
- output .innerText += new TextDecoder ().decode (e .detail );
74
- }, false );
75
-
76
- var mp_js_startup = Module[" onRuntimeInitialized" ];
77
- Module[" onRuntimeInitialized" ] = async function () {
78
- mp_js_startup ();
79
- mp_js_init (64 * 1024 );
80
- await mp_js_do_str (" print('hello world')" );
103
+ <script type =" module" >
104
+ const stdoutWriter = (line ) => {
105
+ document .getElementById (" micropython-stdout" ).innerText += line + " \n " ;
81
106
};
107
+ const mp = await loadMicroPython ({stdout: stdoutWriter});
108
+ await mp .runPythonAsync (`
109
+ import js
110
+ url = "https://api.github.com/users/micropython"
111
+ print(f"fetching {url}...")
112
+ res = await js.fetch(url)
113
+ json = await res.json()
114
+ for i in dir(json):
115
+ print(f"{i}: {json[i]}")
116
+ ` );
82
117
</script >
83
118
</body >
84
119
</html >
@@ -98,31 +133,41 @@ Run the test suite using:
98
133
API
99
134
---
100
135
101
- The following functions have been exposed to javascript.
136
+ The following functions have been exposed to JavaScript through the interpreter
137
+ context, created and returned by ` loadMicroPython() ` .
102
138
103
- ```
104
- mp_js_init(stack_size)
105
- ```
139
+ - ` PyProxy ` : the type of the object that proxies Python objects.
106
140
107
- Initialize MicroPython with the given stack size in bytes. This must be
108
- called before attempting to interact with MicroPython.
141
+ - ` FS ` : the Emscripten filesystem object.
109
142
110
- ```
111
- await mp_js_do_str(code)
112
- ```
143
+ - ` globals ` : an object exposing the globals from the Python ` __main__ ` module,
144
+ with methods ` get(key) ` , ` set(key, value) ` and ` delete(key) ` .
113
145
114
- Execute the input code. ` code ` must be a ` string ` .
146
+ - ` registerJsModule(name, module) ` : register a JavaScript object as importable
147
+ from Python with the given name.
115
148
116
- ```
117
- mp_js_init_repl()
118
- ```
149
+ - ` pyimport ` : import a Python module and return it.
119
150
120
- Initialize MicroPython repl. Must be called before entering characters into
121
- the repl.
151
+ - ` runPython(code) ` : execute Python code and return the result.
122
152
123
- ```
124
- await mp_js_process_char(char)
125
- ```
153
+ - ` runPythonAsync(code) ` : execute Python code and return the result, allowing for
154
+ top-level await expressions (this call must be await'ed on the JavaScript side).
155
+
156
+ - ` replInit() ` : initialise the REPL.
157
+
158
+ - ` replProcessChar(chr) ` : process an incoming character at the REPL.
159
+
160
+ - ` replProcessCharWithAsyncify(chr) ` : process an incoming character at the REPL,
161
+ for use when ASYNCIFY is enabled.
162
+
163
+ Proxying
164
+ --------
165
+
166
+ A Python ` dict ` instance is proxied such that:
167
+
168
+ for (const key in dict) {
169
+ print(key, dict[key]);
170
+ }
126
171
127
- Input character into MicroPython repl. ` char ` must be of type ` number ` . This
128
- will execute MicroPython code when necessary .
172
+ works as expected on the JavaScript side and iterates through the keys of the
173
+ Python ` dict ` .
0 commit comments