From 3f34be69c77dfbd1533af1f04ea460d7da2d118a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 9 May 2024 15:07:11 +1000 Subject: [PATCH] webassembly/asyncio: Fix case where a Promise is resolved with no arg. Signed-off-by: Damien George --- ports/webassembly/asyncio/core.py | 2 +- .../asyncio_await_resolve_no_arg.mjs | 21 +++++++++++++++++++ .../asyncio_await_resolve_no_arg.mjs.exp | 5 +++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/ports/webassembly/asyncio_await_resolve_no_arg.mjs create mode 100644 tests/ports/webassembly/asyncio_await_resolve_no_arg.mjs.exp diff --git a/ports/webassembly/asyncio/core.py b/ports/webassembly/asyncio/core.py index a128bb6055..f6f9d19e5c 100644 --- a/ports/webassembly/asyncio/core.py +++ b/ports/webassembly/asyncio/core.py @@ -75,7 +75,7 @@ class ThenableEvent: self.waiting = None # Task waiting on completion of this thenable thenable.then(self.set) - def set(self, value): + def set(self, value=None): # Thenable/Promise is fulfilled, set result and schedule any waiting task. self.result = value if self.waiting: diff --git a/tests/ports/webassembly/asyncio_await_resolve_no_arg.mjs b/tests/ports/webassembly/asyncio_await_resolve_no_arg.mjs new file mode 100644 index 0000000000..0334a14f11 --- /dev/null +++ b/tests/ports/webassembly/asyncio_await_resolve_no_arg.mjs @@ -0,0 +1,21 @@ +// Test an asyncio task await'ing on a Promise that's resolved without an argument. + +const mp = await (await import(process.argv[2])).loadMicroPython(); + +globalThis.foo = new Promise((resolve) => { + console.log(1); + resolve(); // resolve without an argument + console.log(2); +}); + +mp.runPython(` +import asyncio +import js + +async def task(): + print(3) + print(await js.foo) + print(4) + +asyncio.create_task(task()) +`); diff --git a/tests/ports/webassembly/asyncio_await_resolve_no_arg.mjs.exp b/tests/ports/webassembly/asyncio_await_resolve_no_arg.mjs.exp new file mode 100644 index 0000000000..f3ca0f268a --- /dev/null +++ b/tests/ports/webassembly/asyncio_await_resolve_no_arg.mjs.exp @@ -0,0 +1,5 @@ +1 +2 +3 +None +4