mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
fix: REPL evaluates in strict mode (#5565)
Since everything that Deno loads is treated as an ES Module, it means that all code is treated as "use strict" except for when using the REPL. This PR changes that so code in the REPL is also always evaluated with "use strict". There are also a couple other places where we load code as scripts which should also use "use strict" just in case.
This commit is contained in:
parent
958f21e7ab
commit
2668637e9b
4 changed files with 32 additions and 3 deletions
|
@ -49,11 +49,18 @@ let lastThrownError: Value = undefined;
|
|||
// Returns true if code is consumed (no error/irrecoverable error).
|
||||
// Returns false if error is recoverable
|
||||
function evaluate(code: string): boolean {
|
||||
const [result, errInfo] = core.evalContext(code);
|
||||
// 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) {
|
||||
lastEvalResult = result;
|
||||
// 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(result);
|
||||
replLog(lastEvalResult);
|
||||
}
|
||||
} else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) {
|
||||
// Recoverable compiler error
|
||||
|
|
|
@ -693,6 +693,24 @@ fn repl_test_eof() {
|
|||
assert!(err.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repl_test_strict() {
|
||||
let (_, err) = util::run_and_collect_output(
|
||||
true,
|
||||
"repl",
|
||||
Some(vec![
|
||||
"let a = {};",
|
||||
"Object.preventExtensions(a);",
|
||||
"a.c = 1;",
|
||||
]),
|
||||
None,
|
||||
false,
|
||||
);
|
||||
assert!(err.contains(
|
||||
"Uncaught TypeError: Cannot add property c, object is not extensible"
|
||||
));
|
||||
}
|
||||
|
||||
const REPL_MSG: &str = "exit using ctrl+d or close()\n";
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
// understood by the TypeScript language service, so it allows type safety
|
||||
// checking in VSCode.
|
||||
|
||||
"use strict";
|
||||
|
||||
const ASSETS = "$asset$";
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
// This is a specialised implementation of a System module loader.
|
||||
|
||||
"use strict";
|
||||
|
||||
// @ts-nocheck
|
||||
/* eslint-disable */
|
||||
let System, __instantiateAsync, __instantiate;
|
||||
|
|
Loading…
Reference in a new issue