1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 03:58:12 +00: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:
Linus Groh 2020-04-13 16:21:54 +01:00 committed by Andreas Kling
parent d74ad81402
commit 9fab52a390
6 changed files with 18 additions and 40 deletions

View file

@ -977,7 +977,7 @@ Value ArrayExpression::execute(Interpreter& interpreter) const
auto value = element.execute(interpreter); auto value = element.execute(interpreter);
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
array->push(value); array->elements().append(value);
} }
return array; return array;
} }

View file

@ -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) Value Array::length_getter(Interpreter& interpreter)
{ {
auto* this_object = interpreter.this_value().to_object(interpreter.heap()); auto* this_object = interpreter.this_value().to_object(interpreter.heap());

View file

@ -37,10 +37,6 @@ public:
i32 length() const { return static_cast<i32>(elements().size()); } i32 length() const { return static_cast<i32>(elements().size()); }
Value shift();
Value pop();
void push(Value);
private: private:
virtual const char* class_name() const override { return "Array"; } virtual const char* class_name() const override { return "Array"; }
virtual bool is_array() const override { return true; } virtual bool is_array() const override { return true; }

View file

@ -62,32 +62,33 @@ static Array* array_from(Interpreter& interpreter)
Value ArrayPrototype::push(Interpreter& interpreter) Value ArrayPrototype::push(Interpreter& interpreter)
{ {
auto* this_object = interpreter.this_value().to_object(interpreter.heap()); auto* array = array_from(interpreter);
if (!this_object) if (!array)
return {}; return {};
ASSERT(this_object->is_array());
if (!interpreter.argument_count()) if (!interpreter.argument_count())
return js_undefined(); return js_undefined();
static_cast<Array*>(this_object)->push(interpreter.argument(0)); array->elements().append(interpreter.argument(0));
return Value(static_cast<const Array*>(this_object)->length()); return Value(array->length());
} }
Value ArrayPrototype::pop(Interpreter& interpreter) Value ArrayPrototype::pop(Interpreter& interpreter)
{ {
auto* this_object = interpreter.this_value().to_object(interpreter.heap()); auto* array = array_from(interpreter);
if (!this_object) if (!array)
return {}; return {};
ASSERT(this_object->is_array()); if (array->elements().is_empty())
return static_cast<Array*>(this_object)->pop(); return js_undefined();
return array->elements().take_last();
} }
Value ArrayPrototype::shift(Interpreter& interpreter) Value ArrayPrototype::shift(Interpreter& interpreter)
{ {
auto* this_object = interpreter.this_value().to_object(interpreter.heap()); auto* array = array_from(interpreter);
if (!this_object) if (!array)
return {}; return {};
ASSERT(this_object->is_array()); if (array->elements().is_empty())
return static_cast<Array*>(this_object)->shift(); return js_undefined();
return array->elements().take_first();
} }
Value ArrayPrototype::to_string(Interpreter& interpreter) Value ArrayPrototype::to_string(Interpreter& interpreter)

View file

@ -70,11 +70,11 @@ Value ObjectConstructor::get_own_property_names(Interpreter& interpreter)
auto* result = interpreter.heap().allocate<Array>(); auto* result = interpreter.heap().allocate<Array>();
for (size_t i = 0; i < object->elements().size(); ++i) { for (size_t i = 0; i < object->elements().size(); ++i) {
if (!object->elements()[i].is_empty()) 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()) for (auto& it : object->shape().property_table())
result->push(js_string(interpreter, it.key)); result->elements().append(js_string(interpreter, it.key));
return result; return result;
} }

View file

@ -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. // FIXME: This should be a static NodeList, not a plain JS::Array.
auto* node_list = interpreter.heap().allocate<JS::Array>(); auto* node_list = interpreter.heap().allocate<JS::Array>();
for (auto& element : elements) { for (auto& element : elements) {
node_list->push(wrap(interpreter.heap(), element)); node_list->elements().append(wrap(interpreter.heap(), element));
} }
return node_list; return node_list;
} }