2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-03-08 08:09:22 -04:00
|
|
|
import { exit } from "./ops/os.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { core } from "./core.ts";
|
2020-05-08 18:58:51 -04:00
|
|
|
import { version } from "./version.ts";
|
2020-03-11 05:53:06 -04:00
|
|
|
import { stringifyArgs } from "./web/console.ts";
|
2020-03-08 08:09:22 -04:00
|
|
|
import { startRepl, readline } from "./ops/repl.ts";
|
|
|
|
import { close } from "./ops/resources.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
|
2019-08-17 12:51:51 -04:00
|
|
|
function replLog(...args: unknown[]): void {
|
|
|
|
core.print(stringifyArgs(args) + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
function replError(...args: unknown[]): void {
|
|
|
|
core.print(stringifyArgs(args) + "\n", true);
|
|
|
|
}
|
2018-11-08 19:09:18 -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
|
2020-03-28 13:03:49 -04:00
|
|
|
"Unterminated template literal", // `template
|
2019-03-09 12:30:38 -05:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:33:11 -04:00
|
|
|
// Returns `true` if `close()` is called in REPL.
|
|
|
|
// We should quit the REPL when this function returns `true`.
|
|
|
|
function isCloseCalled(): boolean {
|
2020-06-02 00:24:44 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
return (globalThis as any).closed;
|
2020-05-19 13:33:11 -04:00
|
|
|
}
|
|
|
|
|
2019-08-31 15:17:56 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
type Value = any;
|
|
|
|
|
|
|
|
let lastEvalResult: Value = undefined;
|
|
|
|
let lastThrownError: Value = undefined;
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
// Evaluate code.
|
|
|
|
// Returns true if code is consumed (no error/irrecoverable error).
|
|
|
|
// Returns false if error is recoverable
|
|
|
|
function evaluate(code: string): boolean {
|
2020-05-29 06:24:06 -04:00
|
|
|
// each evalContext is a separate function body, and we want strict mode to
|
|
|
|
// work, so we should ensure that the code starts with "use strict"
|
|
|
|
const [result, errInfo] = core.evalContext(`"use strict";\n\n${code}`);
|
2019-03-09 12:30:38 -05:00
|
|
|
if (!errInfo) {
|
2020-05-29 06:24:06 -04:00
|
|
|
// when a function is eval'ed with just "use strict" sometimes the result
|
|
|
|
// is "use strict" which should be discarded
|
|
|
|
lastEvalResult =
|
|
|
|
typeof result === "string" && result === "use strict"
|
|
|
|
? undefined
|
|
|
|
: result;
|
2020-05-19 13:33:11 -04:00
|
|
|
if (!isCloseCalled()) {
|
2020-05-29 06:24:06 -04:00
|
|
|
replLog(lastEvalResult);
|
2020-05-19 13:33:11 -04:00
|
|
|
}
|
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 {
|
2019-08-31 15:17:56 -04:00
|
|
|
lastThrownError = errInfo.thrown;
|
2019-03-09 12:30:38 -05:00
|
|
|
if (errInfo.isNativeError) {
|
2020-03-02 17:20:16 -05:00
|
|
|
const formattedError = core.formatError(errInfo.thrown as Error);
|
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> {
|
2020-01-20 09:30:30 -05:00
|
|
|
const { console } = globalThis;
|
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);
|
|
|
|
};
|
|
|
|
|
2020-01-20 09:30:30 -05:00
|
|
|
// Configure globalThis._ to give the last evaluation result.
|
|
|
|
Object.defineProperty(globalThis, "_", {
|
2019-08-31 15:17:56 -04:00
|
|
|
configurable: true,
|
|
|
|
get: (): Value => lastEvalResult,
|
|
|
|
set: (value: Value): Value => {
|
2020-01-20 09:30:30 -05:00
|
|
|
Object.defineProperty(globalThis, "_", {
|
2019-08-31 15:17:56 -04:00
|
|
|
value: value,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
configurable: true,
|
2019-08-31 15:17:56 -04:00
|
|
|
});
|
|
|
|
console.log("Last evaluation result is no longer saved to _.");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-08-31 15:17:56 -04:00
|
|
|
});
|
|
|
|
|
2020-01-20 09:30:30 -05:00
|
|
|
// Configure globalThis._error to give the last thrown error.
|
|
|
|
Object.defineProperty(globalThis, "_error", {
|
2019-08-31 15:17:56 -04:00
|
|
|
configurable: true,
|
|
|
|
get: (): Value => lastThrownError,
|
|
|
|
set: (value: Value): Value => {
|
2020-01-20 09:30:30 -05:00
|
|
|
Object.defineProperty(globalThis, "_error", {
|
2019-08-31 15:17:56 -04:00
|
|
|
value: value,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
configurable: true,
|
2019-08-31 15:17:56 -04:00
|
|
|
});
|
|
|
|
console.log("Last thrown error is no longer saved to _error.");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-08-31 15:17:56 -04:00
|
|
|
});
|
2019-08-31 02:45:35 -04:00
|
|
|
|
2020-05-08 18:58:51 -04:00
|
|
|
replLog(`Deno ${version.deno}`);
|
|
|
|
replLog("exit using ctrl+d or close()");
|
|
|
|
|
2018-11-05 12:55:59 -05:00
|
|
|
while (true) {
|
2020-05-19 13:33:11 -04:00
|
|
|
if (isCloseCalled()) {
|
|
|
|
quitRepl(0);
|
|
|
|
}
|
|
|
|
|
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.
|
2020-03-02 17:20:16 -05:00
|
|
|
const formattedError = core.formatError(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.
|
2020-03-02 17:20:16 -05:00
|
|
|
const formattedError = core.formatError(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|