1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:47:35 +00:00

LibJS: Add Array.prototype.keys()

This commit is contained in:
davidot 2021-06-12 14:43:58 +02:00 committed by Linus Groh
parent dc65f54c06
commit e044a3e428
3 changed files with 56 additions and 0 deletions

View file

@ -62,6 +62,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.values, values, 0, attr); define_native_function(vm.names.values, values, 0, attr);
define_native_function(vm.names.flat, flat, 0, attr); define_native_function(vm.names.flat, flat, 0, attr);
define_native_function(vm.names.at, at, 1, attr); define_native_function(vm.names.at, at, 1, attr);
define_native_function(vm.names.keys, keys, 0, attr);
// Use define_property here instead of define_native_function so that // Use define_property here instead of define_native_function so that
// Object.is(Array.prototype[Symbol.iterator], Array.prototype.values) // Object.is(Array.prototype[Symbol.iterator], Array.prototype.values)
@ -1033,6 +1034,16 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::values)
return ArrayIterator::create(global_object, this_object, Object::PropertyKind::Value); return ArrayIterator::create(global_object, this_object, Object::PropertyKind::Value);
} }
// 23.1.3.16 Array.prototype.keys ( ), https://tc39.es/ecma262/#sec-array.prototype.keys
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::keys)
{
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return {};
return ArrayIterator::create(global_object, this_object, Object::PropertyKind::Key);
}
static void recursive_array_flat(VM& vm, GlobalObject& global_object, Array& new_array, Object& array, double depth) static void recursive_array_flat(VM& vm, GlobalObject& global_object, Array& new_array, Object& array, double depth)
{ {
auto array_length = length_of_array_like(global_object, array); auto array_length = length_of_array_like(global_object, array);

View file

@ -48,6 +48,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(values); JS_DECLARE_NATIVE_FUNCTION(values);
JS_DECLARE_NATIVE_FUNCTION(flat); JS_DECLARE_NATIVE_FUNCTION(flat);
JS_DECLARE_NATIVE_FUNCTION(at); JS_DECLARE_NATIVE_FUNCTION(at);
JS_DECLARE_NATIVE_FUNCTION(keys);
}; };
} }

View file

@ -0,0 +1,44 @@
test("length", () => {
expect(Array.prototype.keys.length).toBe(0);
});
test("basic functionality", () => {
const a = ["a", "b", "c"];
const it = a.keys();
expect(it.next()).toEqual({ value: 0, done: false });
expect(it.next()).toEqual({ value: 1, done: false });
expect(it.next()).toEqual({ value: 2, done: false });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});
test("works when applied to non-object", () => {
[true, false, 9, 2n, Symbol()].forEach(primitive => {
const it = [].keys.call(primitive);
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});
});
test("item added to array before exhaustion is accessible", () => {
const a = ["a", "b"];
const it = a.keys();
expect(it.next()).toEqual({ value: 0, done: false });
expect(it.next()).toEqual({ value: 1, done: false });
a.push("c");
expect(it.next()).toEqual({ value: 2, done: false });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});
test("item added to array after exhaustion is inaccessible", () => {
const a = ["a", "b"];
const it = a.keys();
expect(it.next()).toEqual({ value: 0, done: false });
expect(it.next()).toEqual({ value: 1, done: false });
expect(it.next()).toEqual({ value: undefined, done: true });
a.push("c");
expect(it.next()).toEqual({ value: undefined, done: true });
});