Skip to content

Commit 68ca22b

Browse files
committed
tests/ports/webassembly: Expand test for registerJsModule.
This tests `from mod import foo` where `mod` is a module registered using the webassembly API `registerJsModule(mod)`, and where `foo` is a JavaScript function. Prior to the parent commit, this would fail. Signed-off-by: Damien George <[email protected]>
1 parent beb4c31 commit 68ca22b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
1+
// Test the registerJsModule() public API method.
2+
13
import(process.argv[2]).then((mp) => {
24
mp.loadMicroPython().then((py) => {
5+
// Simple module.
36
py.registerJsModule("js_module", { y: 2 });
47
py.runPython("import js_module; print(js_module); print(js_module.y)");
8+
9+
// Module with functions.
10+
// In particular test how "this" behaves.
11+
py.registerJsModule("js_module2", {
12+
yes: true,
13+
add1(x) {
14+
return x + 1;
15+
},
16+
getThis() {
17+
return this;
18+
},
19+
});
20+
21+
console.log("====");
22+
23+
// Test using simple import.
24+
py.runPython(`
25+
import js_module2
26+
27+
print(js_module2.yes)
28+
print(js_module2.add1(1))
29+
print(js_module2.getThis())
30+
print(js_module2.getThis().yes)
31+
`);
32+
33+
console.log("====");
34+
35+
// Test using "from ... import".
36+
py.runPython(`
37+
from js_module2 import yes, add1, getThis
38+
39+
print(yes)
40+
print(add1(2))
41+
print(getThis())
42+
print(getThis().yes)
43+
`);
544
});
645
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
<JsProxy 2>
22
2
3+
====
4+
True
5+
2
6+
<JsProxy 3>
7+
True
8+
====
9+
True
10+
3
11+
<JsProxy 3>
12+
True

0 commit comments

Comments
 (0)