summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2019-08-30 23:45:35 -0700
committerRyan Dahl <ry@tinyclouds.org>2019-08-31 02:45:35 -0400
commit07c3c76d0d7585dc49ffde79d38c406ca9f6e7ca (patch)
treeeb5646745a1c2dfb1c0026a9a5c73d1858fedc78
parent54a3b54138df607585061854ca27282a6cf15c07 (diff)
Save last execution result in for REPL (#2843)
-rw-r--r--js/repl.ts9
-rw-r--r--tools/repl_test.py7
2 files changed, 16 insertions, 0 deletions
diff --git a/js/repl.ts b/js/repl.ts
index ac6700657..cbba2b791 100644
--- a/js/repl.ts
+++ b/js/repl.ts
@@ -25,6 +25,7 @@ function replError(...args: unknown[]): void {
}
const helpMsg = [
+ "_ Print last execution output",
"exit Exit the REPL",
"help Print this help message"
].join("\n");
@@ -71,10 +72,15 @@ function isRecoverableError(e: Error): boolean {
// 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.
replLog(result);
} else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) {
// Recoverable compiler error
@@ -107,6 +113,9 @@ export async function replLoop(): Promise<void> {
exit(exitCode);
};
+ // Make _ a valid property on window to avoid confusing error.
+ window._ = undefined;
+
while (true) {
let code = "";
// Top level read
diff --git a/tools/repl_test.py b/tools/repl_test.py
index 77e54dfdf..870824661 100644
--- a/tools/repl_test.py
+++ b/tools/repl_test.py
@@ -58,6 +58,7 @@ class TestRepl(DenoTestCase):
def test_help_command(self):
out, err, code = self.input("help")
expectedOut = '\n'.join([
+ "_ Print last execution output",
"exit Exit the REPL",
"help Print this help message",
"",
@@ -150,6 +151,12 @@ class TestRepl(DenoTestCase):
self.assertTrue(err.startswith("Unable to save REPL history:"))
self.assertEqual(code, 0)
+ def test_save_last_output(self):
+ out, err, code = self.input("1", "_")
+ self.assertEqual(out, '1\n1\n')
+ self.assertEqual(err, '')
+ self.assertEqual(code, 0)
+
if __name__ == "__main__":
run_tests()