summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/permanently-screwed-by-eval.js
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-10-07 01:06:21 +0200
committerAndreas Kling <kling@serenityos.org>2021-10-07 11:53:18 +0200
commit41a072bded25b0993f315bc86e11cae8419f3231 (patch)
tree59de353aa7622da579ecc4e7c5627a07e8370f6e /Userland/Libraries/LibJS/Tests/permanently-screwed-by-eval.js
parent421845b0cd109d3532beb9dfc06e44169b846505 (diff)
downloadserenity-41a072bded25b0993f315bc86e11cae8419f3231.zip
LibJS: Fast non-local variable access :^)
This patch introduces the "environment coordinate" concept, which encodes the distance from a variable access to the binding it ends up resolving to. EnvironmentCoordinate has two fields: - hops: The number of hops up the lexical environment chain we have to make before getting to the resolved binding. - index: The index of the resolved binding within its declarative environment record. Whenever a variable lookup resolves somewhere inside a declarative environment, we now cache the coordinates and reuse them in subsequent lookups. This is achieved via a coordinate cache in JS::Identifier. Note that non-strict direct eval() breaks this optimization and so it will not be performed if the resolved environment has been permanently screwed by eval(). This makes variable access *significantly* faster. :^)
Diffstat (limited to 'Userland/Libraries/LibJS/Tests/permanently-screwed-by-eval.js')
-rw-r--r--Userland/Libraries/LibJS/Tests/permanently-screwed-by-eval.js13
1 files changed, 13 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/permanently-screwed-by-eval.js b/Userland/Libraries/LibJS/Tests/permanently-screwed-by-eval.js
new file mode 100644
index 0000000000..c611fe4441
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/permanently-screwed-by-eval.js
@@ -0,0 +1,13 @@
+test("basic that non-strict direct eval() prevents non-local access caching", () => {
+ function foo(do_eval) {
+ var c = 1;
+ function bar(do_eval) {
+ if (do_eval) eval("var c = 2;");
+ return c;
+ }
+ return bar(do_eval);
+ }
+
+ expect(foo(false)).toBe(1);
+ expect(foo(true)).toBe(2);
+});