mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 18:24:45 -05:00
LibJS: Remove shift, pop, push functions from Array object
This abstraction isn't really that useful, as we can access the underlying Vector<Value> using elements() and operate on it directly.
This commit is contained in:
parent
d74ad81402
commit
9fab52a390
Notes:
sideshowbarker
2024-07-19 07:36:36 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/9fab52a3902 Pull-request: https://github.com/SerenityOS/serenity/pull/1793
6 changed files with 18 additions and 40 deletions
|
@ -977,7 +977,7 @@ Value ArrayExpression::execute(Interpreter& interpreter) const
|
|||
auto value = element.execute(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
array->push(value);
|
||||
array->elements().append(value);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
|
|
@ -41,25 +41,6 @@ Array::~Array()
|
|||
{
|
||||
}
|
||||
|
||||
Value Array::shift()
|
||||
{
|
||||
if (elements().size() == 0)
|
||||
return js_undefined();
|
||||
return Value(elements().take_first());
|
||||
}
|
||||
|
||||
Value Array::pop()
|
||||
{
|
||||
if (elements().size() == 0)
|
||||
return js_undefined();
|
||||
return Value(elements().take_last());
|
||||
}
|
||||
|
||||
void Array::push(Value value)
|
||||
{
|
||||
elements().append(value);
|
||||
}
|
||||
|
||||
Value Array::length_getter(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
|
|
|
@ -37,10 +37,6 @@ public:
|
|||
|
||||
i32 length() const { return static_cast<i32>(elements().size()); }
|
||||
|
||||
Value shift();
|
||||
Value pop();
|
||||
void push(Value);
|
||||
|
||||
private:
|
||||
virtual const char* class_name() const override { return "Array"; }
|
||||
virtual bool is_array() const override { return true; }
|
||||
|
|
|
@ -62,32 +62,33 @@ static Array* array_from(Interpreter& interpreter)
|
|||
|
||||
Value ArrayPrototype::push(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
if (!this_object)
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
ASSERT(this_object->is_array());
|
||||
if (!interpreter.argument_count())
|
||||
return js_undefined();
|
||||
static_cast<Array*>(this_object)->push(interpreter.argument(0));
|
||||
return Value(static_cast<const Array*>(this_object)->length());
|
||||
array->elements().append(interpreter.argument(0));
|
||||
return Value(array->length());
|
||||
}
|
||||
|
||||
Value ArrayPrototype::pop(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
if (!this_object)
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
ASSERT(this_object->is_array());
|
||||
return static_cast<Array*>(this_object)->pop();
|
||||
if (array->elements().is_empty())
|
||||
return js_undefined();
|
||||
return array->elements().take_last();
|
||||
}
|
||||
|
||||
Value ArrayPrototype::shift(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
if (!this_object)
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
ASSERT(this_object->is_array());
|
||||
return static_cast<Array*>(this_object)->shift();
|
||||
if (array->elements().is_empty())
|
||||
return js_undefined();
|
||||
return array->elements().take_first();
|
||||
}
|
||||
|
||||
Value ArrayPrototype::to_string(Interpreter& interpreter)
|
||||
|
|
|
@ -70,11 +70,11 @@ Value ObjectConstructor::get_own_property_names(Interpreter& interpreter)
|
|||
auto* result = interpreter.heap().allocate<Array>();
|
||||
for (size_t i = 0; i < object->elements().size(); ++i) {
|
||||
if (!object->elements()[i].is_empty())
|
||||
result->push(js_string(interpreter, String::number(i)));
|
||||
result->elements().append(js_string(interpreter, String::number(i)));
|
||||
}
|
||||
|
||||
for (auto& it : object->shape().property_table())
|
||||
result->push(js_string(interpreter, it.key));
|
||||
result->elements().append(js_string(interpreter, it.key));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -98,7 +98,7 @@ JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter)
|
|||
// FIXME: This should be a static NodeList, not a plain JS::Array.
|
||||
auto* node_list = interpreter.heap().allocate<JS::Array>();
|
||||
for (auto& element : elements) {
|
||||
node_list->push(wrap(interpreter.heap(), element));
|
||||
node_list->elements().append(wrap(interpreter.heap(), element));
|
||||
}
|
||||
return node_list;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue