2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-11-05 12:55:59 -05:00
|
|
|
import { close } from "./files";
|
|
|
|
import { exit } from "./os";
|
2019-02-19 21:42:19 -05:00
|
|
|
import { window } from "./window";
|
2019-03-26 08:22:07 -04:00
|
|
|
import { core } from "./core";
|
2019-02-09 16:55:40 -05:00
|
|
|
import { formatError } from "./format_error";
|
2019-08-17 12:51:51 -04:00
|
|
|
import { stringifyArgs } from "./console";
|
2019-08-26 08:50:21 -04:00
|
|
|
import * as dispatch from "./dispatch";
|
|
|
|
import { sendSync, sendAsync } from "./dispatch_json";
|
2019-08-17 12:51:51 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* REPL logging.
|
|
|
|
* In favor of console.log to avoid unwanted indentation
|
|
|
|
*/
|
|
|
|
function replLog(...args: unknown[]): void {
|
|
|
|
core.print(stringifyArgs(args) + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REPL logging for errors.
|
|
|
|
* In favor of console.error to avoid unwanted indentation
|
|
|
|
*/
|
|
|
|
function replError(...args: unknown[]): void {
|
|
|
|
core.print(stringifyArgs(args) + "\n", true);
|
|
|
|
}
|
2018-11-08 19:09:18 -05:00
|
|
|
|
2019-01-29 14:41:12 -05:00
|
|
|
const helpMsg = [
|
|
|
|
"exit Exit the REPL",
|
|
|
|
"help Print this help message"
|
|
|
|
].join("\n");
|
|
|
|
|
|
|
|
const replCommands = {
|
|
|
|
exit: {
|
2019-04-21 16:40:10 -04:00
|
|
|
get(): void {
|
2019-01-29 14:41:12 -05:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
help: {
|
2019-04-21 16:40:10 -04:00
|
|
|
get(): string {
|
2019-01-29 14:41:12 -05:00
|
|
|
return helpMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-05 12:55:59 -05:00
|
|
|
function startRepl(historyFile: string): number {
|
2019-08-26 08:50:21 -04:00
|
|
|
return sendSync(dispatch.OP_REPL_START, { historyFile });
|
2018-11-05 12:55:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
2018-11-28 04:07:22 -05:00
|
|
|
export async function readline(rid: number, prompt: string): Promise<string> {
|
2019-08-26 08:50:21 -04:00
|
|
|
return sendAsync(dispatch.OP_REPL_READLINE, { rid, prompt });
|
2018-11-05 12:55:59 -05:00
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
// Error messages that allow users to continue input
|
|
|
|
// instead of throwing an error to REPL
|
|
|
|
// ref: https://github.com/v8/v8/blob/master/src/message-template.h
|
|
|
|
// TODO(kevinkassimo): this list might not be comprehensive
|
|
|
|
const recoverableErrorMessages = [
|
|
|
|
"Unexpected end of input", // { or [ or (
|
|
|
|
"Missing initializer in const declaration", // const a
|
|
|
|
"Missing catch or finally after try", // try {}
|
|
|
|
"missing ) after argument list", // console.log(1
|
|
|
|
"Unterminated template literal" // `template
|
|
|
|
// TODO(kevinkassimo): need a parser to handling errors such as:
|
|
|
|
// "Missing } in template expression" // `${ or `${ a 123 }`
|
|
|
|
];
|
|
|
|
|
|
|
|
function isRecoverableError(e: Error): boolean {
|
|
|
|
return recoverableErrorMessages.includes(e.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate code.
|
|
|
|
// Returns true if code is consumed (no error/irrecoverable error).
|
|
|
|
// Returns false if error is recoverable
|
|
|
|
function evaluate(code: string): boolean {
|
2019-03-26 08:22:07 -04:00
|
|
|
const [result, errInfo] = core.evalContext(code);
|
2019-03-09 12:30:38 -05:00
|
|
|
if (!errInfo) {
|
2019-08-17 12:51:51 -04:00
|
|
|
replLog(result);
|
2019-03-09 12:30:38 -05:00
|
|
|
} else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) {
|
|
|
|
// Recoverable compiler error
|
|
|
|
return false; // don't consume code.
|
|
|
|
} else {
|
|
|
|
if (errInfo.isNativeError) {
|
|
|
|
const formattedError = formatError(
|
2019-03-26 08:22:07 -04:00
|
|
|
core.errorToJSON(errInfo.thrown as Error)
|
2019-03-09 12:30:38 -05:00
|
|
|
);
|
2019-08-17 12:51:51 -04:00
|
|
|
replError(formattedError);
|
2019-03-09 12:30:38 -05:00
|
|
|
} else {
|
2019-08-17 12:51:51 -04:00
|
|
|
replError("Thrown:", errInfo.thrown);
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:55:59 -05:00
|
|
|
// @internal
|
2018-11-28 04:07:22 -05:00
|
|
|
export async function replLoop(): Promise<void> {
|
2019-01-29 14:41:12 -05:00
|
|
|
Object.defineProperties(window, replCommands);
|
2018-11-05 12:55:59 -05:00
|
|
|
|
|
|
|
const historyFile = "deno_history.txt";
|
|
|
|
const rid = startRepl(historyFile);
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
const quitRepl = (exitCode: number): void => {
|
2019-02-11 14:01:28 -05:00
|
|
|
// Special handling in case user calls deno.close(3).
|
|
|
|
try {
|
|
|
|
close(rid); // close signals Drop on REPL and saves history.
|
|
|
|
} catch {}
|
|
|
|
exit(exitCode);
|
|
|
|
};
|
|
|
|
|
2018-11-05 12:55:59 -05:00
|
|
|
while (true) {
|
2019-02-11 14:01:28 -05:00
|
|
|
let code = "";
|
|
|
|
// Top level read
|
2018-11-05 12:55:59 -05:00
|
|
|
try {
|
2019-02-11 14:01:28 -05:00
|
|
|
code = await readline(rid, "> ");
|
|
|
|
if (code.trim() === "") {
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-05 12:55:59 -05:00
|
|
|
} catch (err) {
|
|
|
|
if (err.message === "EOF") {
|
2019-02-11 14:01:28 -05:00
|
|
|
quitRepl(0);
|
|
|
|
} else {
|
|
|
|
// If interrupted, don't print error.
|
|
|
|
if (err.message !== "Interrupted") {
|
|
|
|
// e.g. this happens when we have deno.close(3).
|
|
|
|
// We want to display the problem.
|
2019-03-26 08:22:07 -04:00
|
|
|
const formattedError = formatError(core.errorToJSON(err));
|
2019-08-17 12:51:51 -04:00
|
|
|
replError(formattedError);
|
2019-02-11 14:01:28 -05:00
|
|
|
}
|
|
|
|
// Quit REPL anyways.
|
|
|
|
quitRepl(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Start continued read
|
|
|
|
while (!evaluate(code)) {
|
|
|
|
code += "\n";
|
|
|
|
try {
|
|
|
|
code += await readline(rid, " ");
|
|
|
|
} catch (err) {
|
|
|
|
// If interrupted on continued read,
|
|
|
|
// abort this read instead of quitting.
|
|
|
|
if (err.message === "Interrupted") {
|
|
|
|
break;
|
|
|
|
} else if (err.message === "EOF") {
|
|
|
|
quitRepl(0);
|
|
|
|
} else {
|
|
|
|
// e.g. this happens when we have deno.close(3).
|
|
|
|
// We want to display the problem.
|
2019-03-26 08:22:07 -04:00
|
|
|
const formattedError = formatError(core.errorToJSON(err));
|
2019-08-17 12:51:51 -04:00
|
|
|
replError(formattedError);
|
2019-02-11 14:01:28 -05:00
|
|
|
quitRepl(1);
|
|
|
|
}
|
2018-11-05 12:55:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|