diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/js/repl.ts | 13 | ||||
-rw-r--r-- | cli/tests/integration_tests.rs | 18 |
2 files changed, 28 insertions, 3 deletions
diff --git a/cli/js/repl.ts b/cli/js/repl.ts index 79273ed33..f38324d11 100644 --- a/cli/js/repl.ts +++ b/cli/js/repl.ts @@ -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 diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 606a40516..f892b9a87 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -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] |