]> git.kaiwu.me - quickjs.git/commitdiff
fixed Promise return in the REPL by using a wrapper object in async std.evalScript...
authorFabrice Bellard <fabrice@bellard.org>
Sat, 3 Feb 2024 14:47:42 +0000 (15:47 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Sat, 3 Feb 2024 14:47:42 +0000 (15:47 +0100)
Makefile
doc/quickjs.texi
quickjs.c
repl.js

index 57cdd7eff32be1a7fe77fe978f78820e71cfc525..3d541eb8cf5c809966ae6a3eefed85556b03b1b0 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -28,7 +28,7 @@ endif
 # Windows cross compilation from Linux
 #CONFIG_WIN32=y
 # use link time optimization (smaller and faster executables but slower build)
-CONFIG_LTO=y
+#CONFIG_LTO=y
 # consider warnings as errors (for development)
 #CONFIG_WERROR=y
 # force 32 bit build for some utilities
index 898d46c82d023a831d3e6e8a630521db573d7d64..9814210a7ffbd0b8dfec38ebc66a85d792333bd3 100644 (file)
@@ -379,7 +379,9 @@ optional properties:
   stack frames below the evalScript.
   @item async
   Boolean (default = false). If true, @code{await} is accepted in the
-  script and a promise is returned.
+  script and a promise is returned. The promise is resolved with an
+  object whose @code{value} property holds the value returned by the
+  script.
   @end table
 
 @item loadScript(filename)
index 0738f9679eb5c91374a18ae404cbe64dc04b861e..5923a870ff395f75badfd9fdd4fb1dd0115057ea 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -34182,9 +34182,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);
diff --git a/repl.js b/repl.js
index 62714a99fb335f7bc1d024d1cbda1308d66fd87b..6b96339f374454106c10e30acace74aae06fbc63 100644 (file)
--- a/repl.js
+++ b/repl.js
@@ -1310,7 +1310,7 @@ import * as os from "os";
                 /* result is a promise */
                 result.then(print_eval_result, print_eval_error);
             } else {
-                print_eval_result(result);
+                print_eval_result({ value: result });
             }
         } catch (error) {
             print_eval_error(error);
@@ -1318,6 +1318,7 @@ import * as os from "os";
     }
 
     function print_eval_result(result) {
+        result = result.value;
         eval_time = os.now() - eval_start_time;
         std.puts(colors[styles.result]);
         print(result);