diff options
author | Linus Groh <mail@linusgroh.de> | 2020-04-13 16:21:54 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-14 13:40:04 +0200 |
commit | 9fab52a39021c9d42c455e8165e4e30c8b408f57 (patch) | |
tree | 9dc593c74741bf5051d7974bcf5c1c56b5e1ed63 /Libraries | |
parent | d74ad81402c27113b2ed5586bc369703d6be61c4 (diff) | |
download | serenity-9fab52a39021c9d42c455e8165e4e30c8b408f57.zip |
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.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/AST.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Array.cpp | 19 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Array.h | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.cpp | 27 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ObjectConstructor.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/DocumentWrapper.cpp | 2 |
6 files changed, 18 insertions, 40 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 0b1ff217c9..318d909dfc 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -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; } diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index 57589264bb..78341ca338 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -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()); diff --git a/Libraries/LibJS/Runtime/Array.h b/Libraries/LibJS/Runtime/Array.h index 10906e0256..19d97416b6 100644 --- a/Libraries/LibJS/Runtime/Array.h +++ b/Libraries/LibJS/Runtime/Array.h @@ -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; } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 26fefa99e3..b8b1f32a72 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -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) diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Libraries/LibJS/Runtime/ObjectConstructor.cpp index 21bd5c18b3..f660df2bb9 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -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; } diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp index 9a5bcb60c5..df9c84dcb2 100644 --- a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp +++ b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp @@ -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; } |