about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorFabrice Bellard <fabrice@bellard.org>2024-02-03 15:47:42 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-02 18:12:23 +0100
commit600cee2b429c3e9a206de8c963aeddb5f9210823 (patch)
tree5f8cd9bf6ace50f497dcb9e7b8f5b9d2696758ef /lib
parentc7d33e636e716b62c2342d1ead88880b5a5586ae (diff)
downloadchawan-600cee2b429c3e9a206de8c963aeddb5f9210823.tar.gz
fixed Promise return in the REPL by using a wrapper object in async std.evalScript() (github issue #231)
Diffstat (limited to 'lib')
-rw-r--r--lib/quickjs/quickjs.c16
1 files changed, 14 insertions, 2 deletions
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);