You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+60-3
Original file line number
Diff line number
Diff line change
@@ -5,16 +5,40 @@ MicroPython transmuted into Javascript (WASM) by Emscripten.
5
5
6
6
Official Repo https://github.com/micropython/micropython/tree/master/ports/javascript
7
7
8
+
Testing and Contribution Needed, feel free to make an issue or even better, a PR
9
+
10
+
11
+
What's New on 1.1
12
+
--------------------
13
+
14
+
- New Async/Await or Promise API
15
+
- New Python classes to expose JS API and Objects like DOM API, XHR, Node.JS require, and etc
16
+
17
+
8
18
Running with Node.js
9
19
--------------------
10
20
21
+
On Node.JS console
22
+
11
23
```javascript
12
-
var mp_js =require('micropython');
24
+
constmp_js=require('micropython');
13
25
14
26
mp_js.init(64*1024);
15
27
mp_js.do_str("print('hello world')\n");
16
28
```
17
29
30
+
On production/actual code use AsyncFunction or Promise to get the guaranteed result
31
+
32
+
```javascript
33
+
(async () => { //AsyncFunction
34
+
constmp_js=require('micropython');
35
+
36
+
mp_js.init(64*1024);
37
+
awaitmp_js.do_str("variable1 = {'data1': 1}");
38
+
awaitmp_js.do_str("variable1.get('data1')"); //Access variables for previous event loop
39
+
})();
40
+
```
41
+
18
42
Running with Webpack
19
43
-----------------
20
44
Running MicroPython on Webpack is a little bit tricky. It expects the firmware.wasm file at /static/js/firmware.wasm. So a simple solution is to make static and js folder on webpack's public directory and put firmware.wasm on it. (PR is accepted for a better solution)
@@ -28,8 +52,12 @@ And import it on your Javascript file
28
52
29
53
```javascript
30
54
importmp_jsfrom'micropython';
31
-
mp_js.init(64*1024);
32
-
mp_js.do_str("print('hello world')\n");
55
+
56
+
(async () => {
57
+
await mp_js;
58
+
mp_js.init(64*1024);
59
+
mp_js.do_str("print('hello world')\n");
60
+
})();
33
61
```
34
62
35
63
API
@@ -63,3 +91,32 @@ process_char(char)
63
91
64
92
Input character into MicroPython repl. `char` must be of type `number`. This
65
93
will execute MicroPython code when necessary.
94
+
95
+
```
96
+
init_python(stack_size)
97
+
```
98
+
99
+
NEW!! This function execute js.py to expose JS API to Python, even some helper function and experimental asynchronous queue/stack logic. Example:
100
+
101
+
```javascript
102
+
importmp_jsfrom'micropython';
103
+
104
+
(async () => {
105
+
await mp_js;
106
+
awaitmp_js.init_python(64*1024);
107
+
awaitmp_js.do_str(`
108
+
#This function executes every line one by one and await promise if it returns a promise
109
+
exec("""
110
+
111
+
require = JS('require')
112
+
fetch = require('node-fetch') #Or do JS('window.fetch') on browser
113
+
response = fetch('https://github.com')
114
+
response = wait(response) #This is the 'await' equivalent
0 commit comments