Simplify eval_and_print code in REPL

We always use async eval.

Also clean up some console.log usage since we use std.puts all across.
This commit is contained in:
Saúl Ibarra Corretgé 2024-09-26 20:07:24 +02:00 committed by GitHub
parent b1dc19c4f7
commit 06175d1e0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 20 deletions

Binary file not shown.

20
repl.js
View file

@ -1549,29 +1549,21 @@ import * as bjson from "bjson";
} }
mexpr = ""; mexpr = "";
eval_and_print_start(expr, true); eval_and_print(expr);
return true; return true;
} }
function eval_and_print_start(expr, is_async) { function eval_and_print(expr) {
var result; var result;
try {
if (use_strict) if (use_strict)
expr = '"use strict"; void 0;' + expr; expr = '"use strict"; void 0;' + expr;
eval_start_time = os.now(); eval_start_time = os.now();
/* eval as a script */ /* eval as a script */
result = std.evalScript(expr, { backtrace_barrier: true, async: is_async }); result = std.evalScript(expr, { backtrace_barrier: true, async: true });
if (is_async) {
/* 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 {
print_eval_result({ value: result });
}
} catch (error) {
print_eval_error(error);
}
} }
function print_eval_result(result) { function print_eval_result(result) {
@ -1587,13 +1579,15 @@ import * as bjson from "bjson";
function print_eval_error(error) { function print_eval_error(error) {
std.puts(colors[styles.error]); std.puts(colors[styles.error]);
if (error instanceof Error) { if (error instanceof Error) {
console.log(error); std.puts(error);
std.puts('\n');
if (error.stack) { if (error.stack) {
std.puts(error.stack); std.puts(error.stack);
} }
} else { } else {
std.puts("Throw: "); std.puts("Throw: ");
console.log(error); std.puts(error);
std.puts('\n');
} }
std.puts(colors.none); std.puts(colors.none);