mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:48:12 +00:00
LibJS: Make Array.prototype.pop() generic
This commit is contained in:
parent
4334a1b208
commit
e9ee06b19e
2 changed files with 34 additions and 4 deletions
|
@ -205,12 +205,29 @@ Value ArrayPrototype::unshift(Interpreter& interpreter)
|
|||
|
||||
Value ArrayPrototype::pop(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter);
|
||||
if (!this_object)
|
||||
return {};
|
||||
if (array->elements().is_empty())
|
||||
if (this_object->is_array()) {
|
||||
auto* array = static_cast<Array*>(this_object);
|
||||
if (array->elements().is_empty())
|
||||
return js_undefined();
|
||||
return array->elements().take_last().value_or(js_undefined());
|
||||
}
|
||||
auto length = get_length(interpreter, *this_object);
|
||||
if (length == 0) {
|
||||
this_object->put("length", Value(0));
|
||||
return js_undefined();
|
||||
return array->elements().take_last().value_or(js_undefined());
|
||||
}
|
||||
auto index = length - 1;
|
||||
auto element = this_object->get_by_index(index).value_or(js_undefined());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
this_object->delete_property(PropertyName(index));
|
||||
this_object->put("length", Value((i32)index));
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
return element;
|
||||
}
|
||||
|
||||
Value ArrayPrototype::shift(Interpreter& interpreter)
|
||||
|
|
|
@ -12,6 +12,19 @@ try {
|
|||
assert(o[0] === "foo");
|
||||
assert(o[1] === "bar");
|
||||
assert(o[2] === "baz");
|
||||
|
||||
assert(Array.prototype.pop.call(o) === "baz");
|
||||
assert(o.length === 2);
|
||||
assert(Array.prototype.pop.call(o) === "bar");
|
||||
assert(o.length === 1);
|
||||
assert(Array.prototype.pop.call(o) === "foo");
|
||||
assert(o.length === 0);
|
||||
assert(Array.prototype.pop.call(o) === undefined);
|
||||
assert(o.length === 0);
|
||||
|
||||
o.length = length;
|
||||
assert(Array.prototype.pop.call(o) === undefined);
|
||||
assert(o.length === 0);
|
||||
});
|
||||
|
||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue