summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorNayeem Rahman <muhammed.9939@gmail.com>2019-08-31 20:17:56 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-08-31 15:17:56 -0400
commitdf2f54b2a66fa71012c5d263009652c98085b5ed (patch)
tree06ac06225caeb51240f0e18e69df2c0a4bfb6ccc /js
parentfdd4252d49ceb022761b92d953d24672ab67ab91 (diff)
Fix REPL '_' assignment, support '_error' (#2845)
Diffstat (limited to 'js')
-rw-r--r--js/repl.ts47
1 files changed, 39 insertions, 8 deletions
diff --git a/js/repl.ts b/js/repl.ts
index cbba2b791..08a4fb210 100644
--- a/js/repl.ts
+++ b/js/repl.ts
@@ -25,7 +25,8 @@ function replError(...args: unknown[]): void {
}
const helpMsg = [
- "_ Print last execution output",
+ "_ Get last evaluation result",
+ "_error Get last thrown error",
"exit Exit the REPL",
"help Print this help message"
].join("\n");
@@ -70,22 +71,25 @@ function isRecoverableError(e: Error): boolean {
return recoverableErrorMessages.includes(e.message);
}
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+type Value = any;
+
+let lastEvalResult: Value = undefined;
+let lastThrownError: Value = undefined;
+
// Evaluate code.
// Returns true if code is consumed (no error/irrecoverable error).
-// Also attempts setting window._ to last valid execute output.
// Returns false if error is recoverable
function evaluate(code: string): boolean {
const [result, errInfo] = core.evalContext(code);
if (!errInfo) {
- // Try setting `window._` to the returned result
- try {
- window._ = result;
- } catch {} // Silently fail on error.
+ lastEvalResult = result;
replLog(result);
} else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) {
// Recoverable compiler error
return false; // don't consume code.
} else {
+ lastThrownError = errInfo.thrown;
if (errInfo.isNativeError) {
const formattedError = formatError(
core.errorToJSON(errInfo.thrown as Error)
@@ -113,8 +117,35 @@ export async function replLoop(): Promise<void> {
exit(exitCode);
};
- // Make _ a valid property on window to avoid confusing error.
- window._ = undefined;
+ // Configure window._ to give the last evaluation result.
+ Object.defineProperty(window, "_", {
+ configurable: true,
+ get: (): Value => lastEvalResult,
+ set: (value: Value): Value => {
+ Object.defineProperty(window, "_", {
+ value: value,
+ writable: true,
+ enumerable: true,
+ configurable: true
+ });
+ console.log("Last evaluation result is no longer saved to _.");
+ }
+ });
+
+ // Configure window._error to give the last thrown error.
+ Object.defineProperty(window, "_error", {
+ configurable: true,
+ get: (): Value => lastThrownError,
+ set: (value: Value): Value => {
+ Object.defineProperty(window, "_error", {
+ value: value,
+ writable: true,
+ enumerable: true,
+ configurable: true
+ });
+ console.log("Last thrown error is no longer saved to _error.");
+ }
+ });
while (true) {
let code = "";