serenity/Userland/Libraries/LibJS/Tests/permanently-screwed-by-eval.js
Andreas Kling 41a072bded 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. :^)
2021-10-07 11:53:18 +02:00

13 lines
345 B
JavaScript

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);
});