Skip to content

Commit 3997532

Browse files
committed
webassembly/proxy_c: Ensure return value of async fun is passed to JS.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 87d821a commit 3997532

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

ports/webassembly/proxy_c.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,11 @@ void proxy_c_to_js_get_dict(uint32_t c_ref, uint32_t *out) {
289289

290290
static const mp_obj_fun_builtin_var_t resume_obj;
291291

292-
EM_JS(void, js_then_resolve, (uint32_t * resolve, uint32_t * reject), {
292+
EM_JS(void, js_then_resolve, (uint32_t * ret_value, uint32_t * resolve, uint32_t * reject), {
293+
const ret_value_js = proxy_convert_mp_to_js_obj_jsside(ret_value);
293294
const resolve_js = proxy_convert_mp_to_js_obj_jsside(resolve);
294295
const reject_js = proxy_convert_mp_to_js_obj_jsside(reject);
295-
resolve_js(null);
296+
resolve_js(ret_value_js);
296297
});
297298

298299
EM_JS(void, js_then_reject, (uint32_t * resolve, uint32_t * reject), {
@@ -321,7 +322,9 @@ static mp_obj_t proxy_resume_execute(mp_obj_t self_in, mp_obj_t value, mp_obj_t
321322
proxy_convert_mp_to_js_obj_cside(reject, out_reject);
322323

323324
if (ret_kind == MP_VM_RETURN_NORMAL) {
324-
js_then_resolve(out_resolve, out_reject);
325+
uint32_t out_ret_value[PVN];
326+
proxy_convert_mp_to_js_obj_cside(ret_value, out_ret_value);
327+
js_then_resolve(out_ret_value, out_resolve, out_reject);
325328
return mp_const_none;
326329
} else if (ret_kind == MP_VM_RETURN_YIELD) {
327330
// ret_value should be a JS thenable
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Test JavaScript await'ing on Python async functions.
2+
3+
const mp = await (await import(process.argv[2])).loadMicroPython();
4+
5+
globalThis.asyncTimeout = (ms) =>
6+
new Promise((resolve) => setTimeout(resolve, ms));
7+
8+
mp.runPython(`
9+
import js
10+
11+
def f0():
12+
print("f0 run")
13+
return 1
14+
15+
async def f1():
16+
print("f1 run")
17+
return 2
18+
19+
async def f2():
20+
print("f2 start")
21+
await js.asyncTimeout(0)
22+
print("f2 end")
23+
return 3
24+
25+
async def f3():
26+
print("f3 start")
27+
ret = await f2()
28+
print("f3 end")
29+
return ret + 1
30+
`);
31+
32+
console.log("f0 return:", await mp.globals.get("f0")());
33+
console.log("f1 return:", await mp.globals.get("f1")());
34+
console.log("f2 return:", await mp.globals.get("f2")());
35+
console.log("f3 return:", await mp.globals.get("f3")());
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
f0 run
2+
f0 return: 1
3+
f1 run
4+
f1 return: 2
5+
f2 start
6+
f2 end
7+
f2 return: 3
8+
f3 start
9+
f2 start
10+
f2 end
11+
f3 end
12+
f3 return: 4

0 commit comments

Comments
 (0)