Skip to content

Commit

Permalink
Save last execution result in for REPL (#2843)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo authored and ry committed Aug 31, 2019
1 parent 54a3b54 commit 07c3c76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions js/repl.ts
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tools/repl_test.py
Expand Up @@ -58,6 +58,7 @@ def test_exit_command(self):
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",
"",
Expand Down Expand Up @@ -150,6 +151,12 @@ def test_missing_deno_dir(self):
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()

0 comments on commit 07c3c76

Please sign in to comment.