mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 20:24:57 +00:00

This is similar to "unique" shapes, which were removed in commit
3d92c26445
.
The key difference is that dictionary shapes don't have a serial number,
but instead have a "cacheable" flag.
Shapes become dictionaries after 64 transitions have occurred, at which
point no further transitions occur.
As long as properties are only added to a dictionary shape, it remains
cacheable. (Since if we've cached the shape pointer in an IC somewhere,
we know the IC is still valid.)
Deleting a property from a dictionary shape causes it to become an
uncacheable dictionary.
Note that deleting a property from a non-dictionary shape still performs
a delete transition.
This fixes an issue on Discord where Object.freeze() would eventually
OOM us, since they add more than 16000 properties to a single object
before freezing it.
It also yields a 15% speedup on Octane/pdfjs.js :^)
85 lines
2.4 KiB
JavaScript
85 lines
2.4 KiB
JavaScript
test("length is 1", () => {
|
|
expect(Object.freeze).toHaveLength(1);
|
|
});
|
|
|
|
describe("normal behavior", () => {
|
|
test("returns given argument", () => {
|
|
const o = {};
|
|
expect(Object.freeze(42)).toBe(42);
|
|
expect(Object.freeze("foobar")).toBe("foobar");
|
|
expect(Object.freeze(o)).toBe(o);
|
|
});
|
|
|
|
test("prevents addition of new properties", () => {
|
|
const o = {};
|
|
expect(o.foo).toBeUndefined();
|
|
Object.freeze(o);
|
|
o.foo = "bar";
|
|
expect(o.foo).toBeUndefined();
|
|
});
|
|
|
|
test("prevents deletion of existing properties", () => {
|
|
const o = { foo: "bar" };
|
|
expect(o.foo).toBe("bar");
|
|
Object.freeze(o);
|
|
delete o.foo;
|
|
expect(o.foo).toBe("bar");
|
|
});
|
|
|
|
test("prevents changing attributes of existing properties", () => {
|
|
const o = { foo: "bar" };
|
|
Object.freeze(o);
|
|
expect(Object.defineProperty(o, "foo", {})).toBe(o);
|
|
expect(Object.defineProperty(o, "foo", { configurable: false })).toBe(o);
|
|
expect(() => {
|
|
Object.defineProperty(o, "foo", { configurable: true });
|
|
}).toThrowWithMessage(TypeError, "Object's [[DefineOwnProperty]] method returned false");
|
|
});
|
|
|
|
test("prevents changing value of existing properties", () => {
|
|
const o = { foo: "bar" };
|
|
expect(o.foo).toBe("bar");
|
|
Object.freeze(o);
|
|
o.foo = "baz";
|
|
expect(o.foo).toBe("bar");
|
|
});
|
|
|
|
// #6469
|
|
test("works with indexed properties", () => {
|
|
const a = ["foo"];
|
|
expect(a[0]).toBe("foo");
|
|
Object.freeze(a);
|
|
a[0] = "bar";
|
|
expect(a[0]).toBe("foo");
|
|
});
|
|
|
|
test("works with properties that are already non-configurable", () => {
|
|
const o = {};
|
|
Object.defineProperty(o, "foo", {
|
|
value: "bar",
|
|
configurable: false,
|
|
writable: true,
|
|
enumerable: true,
|
|
});
|
|
expect(o.foo).toBe("bar");
|
|
Object.freeze(o);
|
|
o.foo = "baz";
|
|
expect(o.foo).toBe("bar");
|
|
});
|
|
});
|
|
|
|
test("does not override frozen function name", () => {
|
|
const func = Object.freeze(function () {
|
|
return 12;
|
|
});
|
|
const obj = Object.freeze({ name: func });
|
|
expect(obj.name()).toBe(12);
|
|
});
|
|
|
|
test("freeze with huge number of properties doesn't crash", () => {
|
|
const o = {};
|
|
for (let i = 0; i < 50_000; ++i) {
|
|
o["prop" + i] = 1;
|
|
}
|
|
Object.freeze(o);
|
|
});
|