summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/eval-basic.js
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-03-16 09:12:34 +0100
committerAndreas Kling <kling@serenityos.org>2021-03-16 10:08:07 +0100
commitc499239137337967dc803921bf236cadc74a99b9 (patch)
tree69e2a9c65e84584c4b45c425671ca0d25593de2a /Userland/Libraries/LibJS/Tests/eval-basic.js
parentdadf2e8251389f048b246fd23d5557308d16fedb (diff)
downloadserenity-c499239137337967dc803921bf236cadc74a99b9.zip
LibJS: Implement non-value-producing statements properly
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.
Diffstat (limited to 'Userland/Libraries/LibJS/Tests/eval-basic.js')
-rw-r--r--Userland/Libraries/LibJS/Tests/eval-basic.js7
1 files changed, 7 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/eval-basic.js b/Userland/Libraries/LibJS/Tests/eval-basic.js
index e00c7fcbd9..e3f8a8cb5e 100644
--- a/Userland/Libraries/LibJS/Tests/eval-basic.js
+++ b/Userland/Libraries/LibJS/Tests/eval-basic.js
@@ -9,6 +9,13 @@ test("basic eval() functionality", () => {
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("{");