mirror of
https://github.com/denoland/deno.git
synced 2025-07-30 00:24:08 +00:00
Add repl functions "help" and "exit" (#1563)
This commit is contained in:
parent
457e65bc2f
commit
240ca25617
2 changed files with 31 additions and 6 deletions
24
js/repl.ts
24
js/repl.ts
|
@ -10,6 +10,24 @@ import { globalEval } from "./global_eval";
|
||||||
|
|
||||||
const window = globalEval("this");
|
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 {
|
function startRepl(historyFile: string): number {
|
||||||
const builder = flatbuffers.createBuilder();
|
const builder = flatbuffers.createBuilder();
|
||||||
const historyFile_ = builder.createString(historyFile);
|
const historyFile_ = builder.createString(historyFile);
|
||||||
|
@ -54,6 +72,7 @@ export async function readline(rid: number, prompt: string): Promise<string> {
|
||||||
// @internal
|
// @internal
|
||||||
export async function replLoop(): Promise<void> {
|
export async function replLoop(): Promise<void> {
|
||||||
window.deno = deno; // FIXME use a new scope (rather than window).
|
window.deno = deno; // FIXME use a new scope (rather than window).
|
||||||
|
Object.defineProperties(window, replCommands);
|
||||||
|
|
||||||
const historyFile = "deno_history.txt";
|
const historyFile = "deno_history.txt";
|
||||||
const rid = startRepl(historyFile);
|
const rid = startRepl(historyFile);
|
||||||
|
@ -69,11 +88,6 @@ export async function replLoop(): Promise<void> {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (!code) {
|
|
||||||
continue;
|
|
||||||
} else if (code.trim() === ".exit") {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
evaluate(code);
|
evaluate(code);
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,11 +56,22 @@ class Repl(object):
|
||||||
assertEqual(code, 0)
|
assertEqual(code, 0)
|
||||||
|
|
||||||
def test_exit_command(self):
|
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(out, '')
|
||||||
assertEqual(err, '')
|
assertEqual(err, '')
|
||||||
assertEqual(code, 0)
|
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):
|
def test_function(self):
|
||||||
out, err, code = self.input("deno.writeFileSync")
|
out, err, code = self.input("deno.writeFileSync")
|
||||||
assertEqual(out, '[Function: writeFileSync]\n')
|
assertEqual(out, '[Function: writeFileSync]\n')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue