1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { exit } from "./ops/os.ts";
import { core } from "./core.ts";
import { version } from "./version.ts";
import { stringifyArgs } from "./web/console.ts";
import { startRepl, readline } from "./ops/repl.ts";
import { close } from "./ops/resources.ts";
function replLog(...args: unknown[]): void {
core.print(stringifyArgs(args) + "\n");
}
function replError(...args: unknown[]): void {
core.print(stringifyArgs(args) + "\n", true);
}
// 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);
}
// Returns `true` if `close()` is called in REPL.
// We should quit the REPL when this function returns `true`.
function isCloseCalled(): boolean {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (globalThis as any).closed;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Value = any;
let lastEvalResult: Value = undefined;
let lastThrownError: Value = undefined;
// Evaluate code.
// Returns true if code is consumed (no error/irrecoverable error).
// Returns false if error is recoverable
function evaluate(code: string): boolean {
// 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}`);
if (!errInfo) {
// 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;
if (!isCloseCalled()) {
replLog(lastEvalResult);
}
} else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) {
// Recoverable compiler error
return false; // don't consume code.
} else {
lastThrownError = errInfo.thrown;
if (errInfo.isNativeError) {
const formattedError = core.formatError(errInfo.thrown as Error);
replError(formattedError);
} else {
replError("Thrown:", errInfo.thrown);
}
}
return true;
}
// @internal
export async function replLoop(): Promise<void> {
const { console } = globalThis;
const historyFile = "deno_history.txt";
const rid = startRepl(historyFile);
const quitRepl = (exitCode: number): void => {
// Special handling in case user calls deno.close(3).
try {
close(rid); // close signals Drop on REPL and saves history.
} catch {}
exit(exitCode);
};
// Configure globalThis._ to give the last evaluation result.
Object.defineProperty(globalThis, "_", {
configurable: true,
get: (): Value => lastEvalResult,
set: (value: Value): Value => {
Object.defineProperty(globalThis, "_", {
value: value,
writable: true,
enumerable: true,
configurable: true,
});
console.log("Last evaluation result is no longer saved to _.");
},
});
// Configure globalThis._error to give the last thrown error.
Object.defineProperty(globalThis, "_error", {
configurable: true,
get: (): Value => lastThrownError,
set: (value: Value): Value => {
Object.defineProperty(globalThis, "_error", {
value: value,
writable: true,
enumerable: true,
configurable: true,
});
console.log("Last thrown error is no longer saved to _error.");
},
});
replLog(`Deno ${version.deno}`);
replLog("exit using ctrl+d or close()");
while (true) {
if (isCloseCalled()) {
quitRepl(0);
}
let code = "";
// Top level read
try {
code = await readline(rid, "> ");
if (code.trim() === "") {
continue;
}
} catch (err) {
if (err.message === "EOF") {
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.
const formattedError = core.formatError(err);
replError(formattedError);
}
// 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.
const formattedError = core.formatError(err);
replError(formattedError);
quitRepl(1);
}
}
}
}
}
|