From 600cee2b429c3e9a206de8c963aeddb5f9210823 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 3 Feb 2024 15:47:42 +0100 Subject: fixed Promise return in the REPL by using a wrapper object in async std.evalScript() (github issue #231) --- lib/quickjs/quickjs.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/quickjs/quickjs.c b/lib/quickjs/quickjs.c index d3c1ca37..2348108d 100644 --- a/lib/quickjs/quickjs.c +++ b/lib/quickjs/quickjs.c @@ -34290,9 +34290,21 @@ static __exception int js_parse_program(JSParseState *s) if (!s->is_module) { /* return the value of the hidden variable eval_ret_idx */ - emit_op(s, OP_get_loc); - emit_u16(s, fd->eval_ret_idx); + if (fd->func_kind == JS_FUNC_ASYNC) { + /* wrap the return value in an object so that promises can + be safely returned */ + emit_op(s, OP_object); + emit_op(s, OP_dup); + emit_op(s, OP_get_loc); + emit_u16(s, fd->eval_ret_idx); + + emit_op(s, OP_put_field); + emit_atom(s, JS_ATOM_value); + } else { + emit_op(s, OP_get_loc); + emit_u16(s, fd->eval_ret_idx); + } emit_return(s, TRUE); } else { emit_return(s, FALSE); -- cgit 1.4.1-2-gfad0