summaryrefslogtreecommitdiff
path: root/ports/webassembly/api.js
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2023-06-24 17:19:05 +1000
committerDamien George <damien@micropython.org>2024-03-22 13:37:47 +1100
commit9b090603a04e69ec710e14f6c8bd93011516c5a1 (patch)
tree8c631d3aed4647f5bf9c40eddd6bafc863e33623 /ports/webassembly/api.js
parent39bd0b8a0a18d7bfc499acc7a11028da12f43edc (diff)
webassembly: Implement runPythonAsync() for top-level async code.
With this commit, `interpreter.runPythonAsync(code)` can now be used to run Python code that uses `await` at the top level. That will yield up to JavaScript and produce a thenable, which the JavaScript runtime can then resume. Also implemented is the ability for Python code to await on JavaScript promises/thenables. For example, outer JavaScript code can await on `runPythonAsync(code)` which then runs Python code that does `await js.fetch(url)`. The entire chain of calls will be suspended until the fetch completes. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'ports/webassembly/api.js')
-rw-r--r--ports/webassembly/api.js10
1 files changed, 10 insertions, 0 deletions
diff --git a/ports/webassembly/api.js b/ports/webassembly/api.js
index dfe756176..ec0601c61 100644
--- a/ports/webassembly/api.js
+++ b/ports/webassembly/api.js
@@ -140,6 +140,16 @@ export async function loadMicroPython(options) {
);
return proxy_convert_mp_to_js_obj_jsside_with_free(value);
},
+ runPythonAsync(code) {
+ const value = Module._malloc(3 * 4);
+ Module.ccall(
+ "mp_js_do_exec_async",
+ "number",
+ ["string", "pointer"],
+ [code, value],
+ );
+ return proxy_convert_mp_to_js_obj_jsside_with_free(value);
+ },
};
}