Skip to content

Commit

Permalink
fixed Promise return in the REPL by using a wrapper object in async s…
Browse files Browse the repository at this point in the history
…td.evalScript() (github issue #231)
  • Loading branch information
Fabrice Bellard committed Feb 3, 2024
1 parent c6cc6a9 commit 00967aa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion doc/quickjs.texi
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1310,14 +1310,15 @@ 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);
}
}

function print_eval_result(result) {
result = result.value;
eval_time = os.now() - eval_start_time;
std.puts(colors[styles.result]);
print(result);
Expand Down

2 comments on commit 00967aa

@xeioex
Copy link
Contributor

@xeioex xeioex commented on 00967aa Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason to disable LTO in this commit?

@chqrlie
Copy link
Collaborator

@chqrlie chqrlie commented on 00967aa Apr 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason justifying a commit. Fabrice might have disabled LTO on machine to reduce build time when working on something and committed this change inadvertently.

Please sign in to comment.