summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/eval-basic.js
diff options
context:
space:
mode:
authorAnonymous <yeeter@yeeters.org>2021-06-19 20:13:53 -0700
committerLinus Groh <mail@linusgroh.de>2021-06-23 09:38:33 +0100
commit2822da8c8f01f9177148eaedd282d25f452c04ea (patch)
treeaf2a204b9b5e9de3e807e3b2cccd4f8be7f9ba49 /Userland/Libraries/LibJS/Tests/eval-basic.js
parent5d24b5f4bec315cba22746f8a0985b1c0877a315 (diff)
downloadserenity-2822da8c8f01f9177148eaedd282d25f452c04ea.zip
LibJS: Correct behaviour of direct vs. indirect eval
eval only has direct access to the local scope when accessed through the name eval. This includes locals named eval, because of course it does.
Diffstat (limited to 'Userland/Libraries/LibJS/Tests/eval-basic.js')
-rw-r--r--Userland/Libraries/LibJS/Tests/eval-basic.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/eval-basic.js b/Userland/Libraries/LibJS/Tests/eval-basic.js
index 43b75028c6..0443d39662 100644
--- a/Userland/Libraries/LibJS/Tests/eval-basic.js
+++ b/Userland/Libraries/LibJS/Tests/eval-basic.js
@@ -30,3 +30,46 @@ test("returns 1st argument unless 1st argument is a string", () => {
var stringObject = new String("1 + 2");
expect(eval(stringObject)).toBe(stringObject);
});
+
+// These eval scope tests use function expressions due to bug #8198
+var testValue = "outer";
+test("eval only touches locals if direct use", function () {
+ var testValue = "inner";
+ expect(globalThis.eval("testValue")).toEqual("outer");
+});
+
+test("alias to eval works as a global eval", function () {
+ var testValue = "inner";
+ var eval1 = globalThis.eval;
+ expect(eval1("testValue")).toEqual("outer");
+});
+
+test("eval evaluates all args", function () {
+ var i = 0;
+ expect(eval("testValue", i++, i++, i++)).toEqual("outer");
+ expect(i).toEqual(3);
+});
+
+test("eval tests for exceptions", function () {
+ var i = 0;
+ expect(function () {
+ eval("testValue", i++, i++, j, i++);
+ }).toThrowWithMessage(ReferenceError, "'j' is not defined");
+ expect(i).toEqual(2);
+});
+
+test("direct eval inherits non-strict evaluation", function () {
+ expect(eval("01")).toEqual(1);
+});
+
+test("direct eval inherits strict evaluation", function () {
+ "use strict";
+ expect(() => {
+ eval("01");
+ }).toThrowWithMessage(SyntaxError, "Unprefixed octal number not allowed in strict mode");
+});
+
+test("global eval evaluates as non-strict", function () {
+ "use strict";
+ expect(globalThis.eval("01"));
+});