ladybird/Userland/Libraries/LibJS/Tests/functions/function-name.js
Andreas Kling dc8817638e LibJS: Only update anonymous function names when necessary
Previously we would generate function names for anonymous functions
on every AssignmentExpression, even if we weren't assigning a function.

We were also setting names of anonymous functions in arrays, which is
apparently a SpiderMonkey specific behavior not supported by V8, JSC
or required by ECMA262. This patch removes that behavior.

This is a huge performance improvement on the CanvasCycle demo! :^)
2021-03-21 21:39:39 +01:00

50 lines
1.3 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");
o.c = function () {};
expect(o.c.name).toBe("c");
});
test("names of native functions", () => {
expect(console.debug.name).toBe("debug");
expect((console.debug.name = "warn")).toBe("warn");
expect(console.debug.name).toBe("debug");
});