mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:17:35 +00:00
LibJS: Make Array.prototype.shift generic
This commit is contained in:
parent
690eb3bb8a
commit
7c1e2adf8a
2 changed files with 51 additions and 6 deletions
|
@ -246,15 +246,50 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::pop)
|
||||||
// 23.1.3.24 Array.prototype.shift ( ), https://tc39.es/ecma262/#sec-array.prototype.shift
|
// 23.1.3.24 Array.prototype.shift ( ), https://tc39.es/ecma262/#sec-array.prototype.shift
|
||||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::shift)
|
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::shift)
|
||||||
{
|
{
|
||||||
auto* array = Array::typed_this(vm, global_object);
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
||||||
if (!array)
|
if (!this_object)
|
||||||
return {};
|
return {};
|
||||||
if (array->indexed_properties().is_empty())
|
auto length = length_of_array_like(global_object, *this_object);
|
||||||
return js_undefined();
|
|
||||||
auto result = array->indexed_properties().take_first(array);
|
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
return result.value.value_or(js_undefined());
|
if (length == 0) {
|
||||||
|
this_object->put(vm.names.length, Value(0));
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
return js_undefined();
|
||||||
|
}
|
||||||
|
auto first = this_object->get(0).value_or(js_undefined());
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
for (size_t k = 1; k < length; ++k) {
|
||||||
|
size_t from = k;
|
||||||
|
size_t to = k - 1;
|
||||||
|
bool from_present = this_object->has_property(from);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
if (from_present) {
|
||||||
|
auto from_value = this_object->get(from).value_or(js_undefined());
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
this_object->put(to, from_value);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
} else {
|
||||||
|
this_object->delete_property(to);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this_object->delete_property(length - 1);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
this_object->put(vm.names.length, Value(length - 1));
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 23.1.3.30 Array.prototype.toString ( ), https://tc39.es/ecma262/#sec-array.prototype.tostring
|
// 23.1.3.30 Array.prototype.toString ( ), https://tc39.es/ecma262/#sec-array.prototype.tostring
|
||||||
|
|
|
@ -100,6 +100,16 @@ describe("ability to work with generic non-array objects", () => {
|
||||||
expect(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo")).toBeTrue();
|
expect(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo")).toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("shift", () => {
|
||||||
|
expect(Array.prototype.shift.call({})).toBeUndefined();
|
||||||
|
expect(Array.prototype.shift.call({ length: 0 })).toBeUndefined();
|
||||||
|
|
||||||
|
const o = { length: 5, 0: "a", 1: "b", 3: "c" };
|
||||||
|
const front = Array.prototype.shift.call(o);
|
||||||
|
expect(front).toEqual("a");
|
||||||
|
expect(o).toEqual({ length: 4, 0: "b", 2: "c" });
|
||||||
|
});
|
||||||
|
|
||||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||||
|
|
||||||
test("every", () => {
|
test("every", () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue