2020-07-04 10:09:48 -07:00
|
|
|
describe("basic functionality", () => {
|
2020-07-06 07:37:45 -07:00
|
|
|
test("length", () => {
|
|
|
|
|
expect(Object.entries).toHaveLength(1);
|
|
|
|
|
expect(Object.entries(true)).toHaveLength(0);
|
|
|
|
|
expect(Object.entries(45)).toHaveLength(0);
|
|
|
|
|
expect(Object.entries(-998)).toHaveLength(0);
|
|
|
|
|
expect(Object.entries("abcd")).toHaveLength(4);
|
|
|
|
|
expect(Object.entries([1, 2, 3])).toHaveLength(3);
|
|
|
|
|
expect(Object.entries({ a: 1, b: 2, c: 3 })).toHaveLength(3);
|
|
|
|
|
});
|
2020-04-29 18:59:23 -07:00
|
|
|
|
2020-07-06 07:37:45 -07:00
|
|
|
test("entries with object", () => {
|
|
|
|
|
let entries = Object.entries({ foo: 1, bar: 2, baz: 3 });
|
2020-04-29 18:59:23 -07:00
|
|
|
|
2020-07-06 07:37:45 -07:00
|
|
|
expect(entries).toEqual([
|
|
|
|
|
["foo", 1],
|
|
|
|
|
["bar", 2],
|
|
|
|
|
["baz", 3],
|
|
|
|
|
]);
|
|
|
|
|
});
|
2020-04-29 18:59:23 -07:00
|
|
|
|
2020-07-07 21:39:36 -07:00
|
|
|
test("entries with objects with symbol keys", () => {
|
|
|
|
|
let entries = Object.entries({ foo: 1, [Symbol("bar")]: 2, baz: 3 });
|
|
|
|
|
|
|
|
|
|
expect(entries).toEqual([
|
|
|
|
|
["foo", 1],
|
|
|
|
|
["baz", 3],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-06 07:37:45 -07:00
|
|
|
test("entries with array", () => {
|
|
|
|
|
entries = Object.entries(["a", "b", "c"]);
|
|
|
|
|
expect(entries).toEqual([
|
|
|
|
|
["0", "a"],
|
|
|
|
|
["1", "b"],
|
|
|
|
|
["2", "c"],
|
|
|
|
|
]);
|
|
|
|
|
});
|
2020-04-29 18:59:23 -07:00
|
|
|
|
2020-07-06 07:37:45 -07:00
|
|
|
test("ignores non-enumerable properties", () => {
|
|
|
|
|
let obj = { foo: 1 };
|
|
|
|
|
Object.defineProperty(obj, "getFoo", {
|
|
|
|
|
value: function () {
|
|
|
|
|
return this.foo;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
let entries = Object.entries(obj);
|
|
|
|
|
expect(entries).toEqual([["foo", 1]]);
|
2020-07-04 10:09:48 -07:00
|
|
|
});
|
LibJS: Fast-path own-property enumeration and reduce descriptor lookups
Before this change, PropertyNameIterator (used by for..in) and
`Object::enumerable_own_property_names()` (used by `Object.keys()`,
`Object.values()`, and `Object.entries()`) enumerated an object's own
enumerable properties exactly as the spec prescribes:
- Call `internal_own_property_keys()`, allocating a list of JS::Value
keys.
- For each key, call internal_get_own_property() to obtain a
descriptor and check `[[Enumerable]]`.
While that is required in the general case (e.g. for Proxy objects or
platform/exotic objects that override `[[OwnPropertyKeys]]`), it's
overkill for ordinary JS objects that store their own properties in the
shape table and indexed-properties storage.
This change introduces `for_each_own_property_with_enumerability()`,
which, for objects where
`eligible_for_own_property_enumeration_fast_path()` is `true`, lets us
read the enumerability directly from shape metadata (and from
indexed-properties storage) without a per-property descriptor lookup.
When we cannot avoid `internal_get_own_property()`, we still
benefit by skipping the temporary `Vector<Value>` of keys and avoiding
the unnecessary round-trip between PropertyKey and Value.
2025-09-19 16:49:53 +02:00
|
|
|
|
|
|
|
|
test("delete key from getter", () => {
|
|
|
|
|
const obj = {
|
|
|
|
|
get a() {
|
|
|
|
|
delete this.b;
|
|
|
|
|
return 1;
|
|
|
|
|
},
|
|
|
|
|
b: 2,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
expect(Object.entries(obj)).toEqual([["a", 1]]);
|
|
|
|
|
});
|
2020-07-04 10:09:48 -07:00
|
|
|
});
|
2020-04-29 18:59:23 -07:00
|
|
|
|
2020-07-04 10:09:48 -07:00
|
|
|
describe("errors", () => {
|
2020-07-06 07:37:45 -07:00
|
|
|
test("null argument", () => {
|
|
|
|
|
expect(() => {
|
|
|
|
|
Object.entries(null);
|
|
|
|
|
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
|
|
|
|
});
|
2020-04-29 18:59:23 -07:00
|
|
|
|
2020-07-06 07:37:45 -07:00
|
|
|
test("undefined argument", () => {
|
|
|
|
|
expect(() => {
|
|
|
|
|
Object.entries(undefined);
|
|
|
|
|
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
|
|
|
|
});
|
2020-07-04 10:09:48 -07:00
|
|
|
});
|