diff options
| author | Damien George <damien@micropython.org> | 2025-09-19 00:02:54 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-09-30 11:04:05 +1000 |
| commit | 68ca22bbc882cda735d10f921769beff8aa60d77 (patch) | |
| tree | e241c6fce0fbdf53d9c7ca4763f42c500795f3ac /tests | |
| parent | beb4c3188f5c26ede14134db1d34f817793b7e46 (diff) | |
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 <damien@micropython.org>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ports/webassembly/register_js_module.js | 39 | ||||
| -rw-r--r-- | tests/ports/webassembly/register_js_module.js.exp | 10 |
2 files changed, 49 insertions, 0 deletions
diff --git a/tests/ports/webassembly/register_js_module.js b/tests/ports/webassembly/register_js_module.js index b512f2c0d..f58d12e50 100644 --- a/tests/ports/webassembly/register_js_module.js +++ b/tests/ports/webassembly/register_js_module.js @@ -1,6 +1,45 @@ +// Test the registerJsModule() public API method. + import(process.argv[2]).then((mp) => { mp.loadMicroPython().then((py) => { + // Simple module. py.registerJsModule("js_module", { y: 2 }); py.runPython("import js_module; print(js_module); print(js_module.y)"); + + // Module with functions. + // In particular test how "this" behaves. + py.registerJsModule("js_module2", { + yes: true, + add1(x) { + return x + 1; + }, + getThis() { + return this; + }, + }); + + console.log("===="); + + // Test using simple import. + py.runPython(` +import js_module2 + +print(js_module2.yes) +print(js_module2.add1(1)) +print(js_module2.getThis()) +print(js_module2.getThis().yes) +`); + + console.log("===="); + + // Test using "from ... import". + py.runPython(` +from js_module2 import yes, add1, getThis + +print(yes) +print(add1(2)) +print(getThis()) +print(getThis().yes) +`); }); }); diff --git a/tests/ports/webassembly/register_js_module.js.exp b/tests/ports/webassembly/register_js_module.js.exp index 6e2bad3ce..34bfd345d 100644 --- a/tests/ports/webassembly/register_js_module.js.exp +++ b/tests/ports/webassembly/register_js_module.js.exp @@ -1,2 +1,12 @@ <JsProxy 2> 2 +==== +True +2 +<JsProxy 3> +True +==== +True +3 +<JsProxy 3> +True |
