mirror of
https://github.com/bellard/quickjs.git
synced 2024-11-22 05:38:11 +08:00
fixed Promise return in the REPL by using a wrapper object in async std.evalScript() (github issue #231)
This commit is contained in:
parent
c6cc6a9a5e
commit
00967aac24
2
Makefile
2
Makefile
@ -28,7 +28,7 @@ endif
|
|||||||
# Windows cross compilation from Linux
|
# Windows cross compilation from Linux
|
||||||
#CONFIG_WIN32=y
|
#CONFIG_WIN32=y
|
||||||
# use link time optimization (smaller and faster executables but slower build)
|
# use link time optimization (smaller and faster executables but slower build)
|
||||||
CONFIG_LTO=y
|
#CONFIG_LTO=y
|
||||||
# consider warnings as errors (for development)
|
# consider warnings as errors (for development)
|
||||||
#CONFIG_WERROR=y
|
#CONFIG_WERROR=y
|
||||||
# force 32 bit build for some utilities
|
# force 32 bit build for some utilities
|
||||||
|
@ -379,7 +379,9 @@ optional properties:
|
|||||||
stack frames below the evalScript.
|
stack frames below the evalScript.
|
||||||
@item async
|
@item async
|
||||||
Boolean (default = false). If true, @code{await} is accepted in the
|
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
|
@end table
|
||||||
|
|
||||||
@item loadScript(filename)
|
@item loadScript(filename)
|
||||||
|
16
quickjs.c
16
quickjs.c
@ -34182,9 +34182,21 @@ static __exception int js_parse_program(JSParseState *s)
|
|||||||
|
|
||||||
if (!s->is_module) {
|
if (!s->is_module) {
|
||||||
/* return the value of the hidden variable eval_ret_idx */
|
/* return the value of the hidden variable eval_ret_idx */
|
||||||
emit_op(s, OP_get_loc);
|
if (fd->func_kind == JS_FUNC_ASYNC) {
|
||||||
emit_u16(s, fd->eval_ret_idx);
|
/* 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);
|
emit_return(s, TRUE);
|
||||||
} else {
|
} else {
|
||||||
emit_return(s, FALSE);
|
emit_return(s, FALSE);
|
||||||
|
3
repl.js
3
repl.js
@ -1310,7 +1310,7 @@ import * as os from "os";
|
|||||||
/* result is a promise */
|
/* result is a promise */
|
||||||
result.then(print_eval_result, print_eval_error);
|
result.then(print_eval_result, print_eval_error);
|
||||||
} else {
|
} else {
|
||||||
print_eval_result(result);
|
print_eval_result({ value: result });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
print_eval_error(error);
|
print_eval_error(error);
|
||||||
@ -1318,6 +1318,7 @@ import * as os from "os";
|
|||||||
}
|
}
|
||||||
|
|
||||||
function print_eval_result(result) {
|
function print_eval_result(result) {
|
||||||
|
result = result.value;
|
||||||
eval_time = os.now() - eval_start_time;
|
eval_time = os.now() - eval_start_time;
|
||||||
std.puts(colors[styles.result]);
|
std.puts(colors[styles.result]);
|
||||||
print(result);
|
print(result);
|
||||||
|
Loading…
Reference in New Issue
Block a user