From 240ca25617bb57e4071193dbc040ab03a27f3a6c Mon Sep 17 00:00:00 2001 From: Sergey Golovin Date: Tue, 29 Jan 2019 22:41:12 +0300 Subject: [PATCH] Add repl functions "help" and "exit" (#1563) --- js/repl.ts | 24 +++++++++++++++++++----- tools/repl_test.py | 13 ++++++++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/js/repl.ts b/js/repl.ts index 4fae31eabc..6fb395e1bc 100644 --- a/js/repl.ts +++ b/js/repl.ts @@ -10,6 +10,24 @@ import { globalEval } from "./global_eval"; const window = globalEval("this"); +const helpMsg = [ + "exit Exit the REPL", + "help Print this help message" +].join("\n"); + +const replCommands = { + exit: { + get() { + exit(0); + } + }, + help: { + get() { + return helpMsg; + } + } +}; + function startRepl(historyFile: string): number { const builder = flatbuffers.createBuilder(); const historyFile_ = builder.createString(historyFile); @@ -54,6 +72,7 @@ export async function readline(rid: number, prompt: string): Promise { // @internal export async function replLoop(): Promise { window.deno = deno; // FIXME use a new scope (rather than window). + Object.defineProperties(window, replCommands); const historyFile = "deno_history.txt"; const rid = startRepl(historyFile); @@ -69,11 +88,6 @@ export async function replLoop(): Promise { console.error(err); exit(1); } - if (!code) { - continue; - } else if (code.trim() === ".exit") { - break; - } evaluate(code); } diff --git a/tools/repl_test.py b/tools/repl_test.py index 620888e4bc..1b589de67e 100644 --- a/tools/repl_test.py +++ b/tools/repl_test.py @@ -56,11 +56,22 @@ class Repl(object): assertEqual(code, 0) def test_exit_command(self): - out, err, code = self.input(".exit", "'ignored'", exit=False) + out, err, code = self.input("exit", "'ignored'", exit=False) assertEqual(out, '') assertEqual(err, '') assertEqual(code, 0) + def test_help_command(self): + out, err, code = self.input("help") + expectedOut = '\n'.join([ + "exit Exit the REPL", + "help Print this help message", + "", + ]) + assertEqual(out, expectedOut) + assertEqual(err, '') + assertEqual(code, 0) + def test_function(self): out, err, code = self.input("deno.writeFileSync") assertEqual(out, '[Function: writeFileSync]\n')