
For various statements the spec states: Return NormalCompletion(empty). In those cases we have been returning undefined so far, which is incorrect. In other cases it states: Return Completion(UpdateEmpty(stmtCompletion, undefined)). Which essentially means a statement is evaluated and its completion value returned if non-empty, and undefined otherwise. While not actually noticeable in normal scripts as the VM's "last value" can't be accessed from JS code directly (with the exception of eval(), see below), it provided an inconsistent experience in the REPL: > if (true) 42; 42 > if (true) { 42; } undefined This also fixes the case where eval() would return undefined if the last executed statement is not a value-producing one: eval("1;;;;;") eval("1;{}") eval("1;var a;") As a consequence of the changes outlined above, these now all correctly return 1. See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation, "NOTE 2". Fixes #3609.
26 lines
653 B
JavaScript
26 lines
653 B
JavaScript
test("basic eval() functionality", () => {
|
|
expect(eval("1 + 2")).toBe(3);
|
|
|
|
function foo(a) {
|
|
var x = 5;
|
|
eval("x += a");
|
|
return x;
|
|
}
|
|
expect(foo(7)).toBe(12);
|
|
});
|
|
|
|
test("returns value of last value-producing statement", () => {
|
|
// See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
|
|
expect(eval("1;;;;;")).toBe(1);
|
|
expect(eval("1;{}")).toBe(1);
|
|
expect(eval("1;var a;")).toBe(1);
|
|
});
|
|
|
|
test("syntax error", () => {
|
|
expect(() => {
|
|
eval("{");
|
|
}).toThrowWithMessage(
|
|
SyntaxError,
|
|
"Unexpected token Eof. Expected CurlyClose (line: 1, column: 2)"
|
|
);
|
|
});
|