Skip to content

Commit c1513a0

Browse files
committed
tests/ports/webassembly: Add webassembly JS tests.
Signed-off-by: Damien George <[email protected]>
1 parent e41b571 commit c1513a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+757
-1
lines changed

tests/ports/webassembly/basic.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import(process.argv[2]).then((mp) => {
2+
mp.loadMicroPython().then((py) => {
3+
py.runPython("1");
4+
py.runPython("print('hello')");
5+
py.runPython("import sys; print(f'hello from {sys.platform}')");
6+
py.runPython("import collections; print(collections.OrderedDict)");
7+
});
8+
});

tests/ports/webassembly/basic.js.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
hello from webassembly
3+
<class 'OrderedDict'>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const mp = await (await import(process.argv[2])).loadMicroPython();
2+
3+
mp.FS.mkdir("/lib/");
4+
mp.FS.writeFile("/lib/testmod.py", "x = 1; print(__name__, x)");
5+
mp.runPython("import testmod");
6+
7+
mp.runPython("import sys; sys.modules.clear()");
8+
const testmod = mp.pyimport("testmod");
9+
console.log("testmod:", testmod, testmod.x);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
testmod 1
2+
testmod 1
3+
testmod: PyProxy { _ref: 3 } 1

tests/ports/webassembly/float.mjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Test passing floats between JavaScript and Python.
2+
3+
const mp = await (await import(process.argv[2])).loadMicroPython();
4+
5+
globalThis.a = 1 / 2;
6+
globalThis.b = Infinity;
7+
globalThis.c = NaN;
8+
9+
mp.runPython(`
10+
import js
11+
12+
# Test retrieving floats from JS.
13+
print(js.a)
14+
print(js.b)
15+
print(js.c)
16+
17+
# Test calling JS which returns a float.
18+
r = js.Math.random()
19+
print(type(r), 0 < r < 1)
20+
21+
x = 1 / 2
22+
y = float("inf")
23+
z = float("nan")
24+
25+
# Test passing floats to a JS function.
26+
js.console.log(x)
27+
js.console.log(x, y)
28+
js.console.log(x, y, z)
29+
`);
30+
31+
// Test retrieving floats from Python.
32+
console.log(mp.globals.get("x"));
33+
console.log(mp.globals.get("y"));
34+
console.log(mp.globals.get("z"));
35+
36+
// Test passing floats to a Python function.
37+
const mp_print = mp.pyimport("builtins").print;
38+
mp_print(globalThis.a);
39+
mp_print(globalThis.a, globalThis.b);
40+
mp_print(globalThis.a, globalThis.b, globalThis.c);
41+
42+
// Test calling Python which returns a float.
43+
console.log(mp.pyimport("math").sqrt(0.16));

tests/ports/webassembly/float.mjs.exp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
0.5
2+
inf
3+
nan
4+
<class 'float'> True
5+
0.5
6+
0.5 Infinity
7+
0.5 Infinity NaN
8+
0.5
9+
Infinity
10+
NaN
11+
0.5
12+
0.5 inf
13+
0.5 inf nan
14+
0.4

tests/ports/webassembly/fun_call.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Test calling JavaScript functions from Python.
2+
3+
const mp = await (await import(process.argv[2])).loadMicroPython();
4+
5+
globalThis.f = (a, b, c, d, e) => {
6+
console.log(a, b, c, d, e);
7+
};
8+
mp.runPython(`
9+
import js
10+
js.f()
11+
js.f(1)
12+
js.f(1, 2)
13+
js.f(1, 2, 3)
14+
js.f(1, 2, 3, 4)
15+
js.f(1, 2, 3, 4, 5)
16+
js.f(1, 2, 3, 4, 5, 6)
17+
`);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
undefined undefined undefined undefined undefined
2+
1 undefined undefined undefined undefined
3+
1 2 undefined undefined undefined
4+
1 2 3 undefined undefined
5+
1 2 3 4 undefined
6+
1 2 3 4 5
7+
1 2 3 4 5

tests/ports/webassembly/globals.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Test accessing Python globals dict via mp.globals.
2+
3+
const mp = await (await import(process.argv[2])).loadMicroPython();
4+
5+
mp.runPython("x = 1");
6+
console.log(mp.globals.get("x"));
7+
8+
mp.globals.set("y", 2);
9+
mp.runPython("print(y)");
10+
11+
mp.runPython("print('y' in globals())");
12+
mp.globals.delete("y");
13+
mp.runPython("print('y' in globals())");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
2
3+
True
4+
False

0 commit comments

Comments
 (0)