mirror of
https://github.com/RGBCube/serenity
synced 2025-10-24 11:42:33 +00:00
Before this we used an ad-hoc combination of references and 'variables'
stored in a hashmap. This worked in most cases but is not spec like.
Additionally hoisting, dynamically naming functions and scope analysis
was not done properly.
This patch fixes all of that by:
- Implement BindingInitialization for destructuring assignment.
- Implementing a new ScopePusher which tracks the lexical and var
scoped declarations. This hoists functions to the top level if no
lexical declaration name overlaps. Furthermore we do checking of
redeclarations in the ScopePusher now requiring less checks all over
the place.
- Add methods for parsing the directives and statement lists instead
of having that code duplicated in multiple places. This allows
declarations to pushed to the appropriate scope more easily.
- Remove the non spec way of storing 'variables' in
DeclarativeEnvironment and make Reference follow the spec instead of
checking both the bindings and 'variables'.
- Remove all scoping related things from the Interpreter. And instead
use environments as specified by the spec. This also includes fixing
that NativeFunctions did not produce a valid FunctionEnvironment
which could cause issues with callbacks and eval. All
FunctionObjects now have a valid NewFunctionEnvironment
implementation.
- Remove execute_statements from Interpreter and instead use
ASTNode::execute everywhere this simplifies AST.cpp as you no longer
need to worry about which method to call.
- Make ScopeNodes setup their own environment. This uses four
different methods specified by the spec
{Block, Function, Eval, Global}DeclarationInstantiation with the
annexB extensions.
- Implement and use NamedEvaluation where specified.
Additionally there are fixes to things exposed by these changes to eval,
{for, for-in, for-of} loops and assignment.
Finally it also fixes some tests in test-js which where passing before
but not now that we have correct behavior :^).
74 lines
1.9 KiB
JavaScript
74 lines
1.9 KiB
JavaScript
test("basic functionality", () => {
|
|
expect(function () {}.name).toBe("");
|
|
|
|
function bar() {}
|
|
expect(bar.name).toBe("bar");
|
|
expect((bar.name = "baz")).toBe("baz");
|
|
expect(bar.name).toBe("bar");
|
|
});
|
|
|
|
test("function assigned to variable", () => {
|
|
let foo = function () {};
|
|
expect(foo.name).toBe("foo");
|
|
expect((foo.name = "bar")).toBe("bar");
|
|
expect(foo.name).toBe("foo");
|
|
|
|
let a, b;
|
|
a = b = function () {};
|
|
expect(a.name).toBe("b");
|
|
expect(b.name).toBe("b");
|
|
});
|
|
|
|
test("functions in array assigned to variable", () => {
|
|
const arr = [function () {}, function () {}, function () {}];
|
|
expect(arr[0].name).toBe("");
|
|
expect(arr[1].name).toBe("");
|
|
expect(arr[2].name).toBe("");
|
|
});
|
|
|
|
test("functions in objects", () => {
|
|
let f;
|
|
let o = { a: function () {} };
|
|
|
|
expect(o.a.name).toBe("a");
|
|
f = o.a;
|
|
expect(f.name).toBe("a");
|
|
expect(o.a.name).toBe("a");
|
|
|
|
o = { ...o, b: f };
|
|
expect(o.a.name).toBe("a");
|
|
expect(o.b.name).toBe("a");
|
|
|
|
// Member expressions do not get named.
|
|
o.c = function () {};
|
|
expect(o.c.name).toBe("");
|
|
});
|
|
|
|
test("names of native functions", () => {
|
|
expect(console.debug.name).toBe("debug");
|
|
expect((console.debug.name = "warn")).toBe("warn");
|
|
expect(console.debug.name).toBe("debug");
|
|
});
|
|
|
|
describe("some anonymous functions get renamed", () => {
|
|
test("direct assignment does name new function expression", () => {
|
|
// prettier-ignore
|
|
let f1 = (function () {});
|
|
expect(f1.name).toBe("f1");
|
|
|
|
let f2 = false;
|
|
f2 ||= function () {};
|
|
expect(f2.name).toBe("f2");
|
|
});
|
|
|
|
test("assignment from variable does not name", () => {
|
|
const f1 = function () {};
|
|
let f3 = f1;
|
|
expect(f3.name).toBe("f1");
|
|
});
|
|
|
|
test("assignment via expression does not name", () => {
|
|
let f4 = false || function () {};
|
|
expect(f4.name).toBe("");
|
|
});
|
|
});
|