summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-17 18:24:01 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-18 10:28:22 +0200
commit2d7b495244a76bf057a5c22032b1f5d49cef5637 (patch)
treedf6299ce31357b5cf32cfa0ac13d114200561b25 /Libraries
parentee5816b9c8d1da8f0e01eaaddb6e73150dfa827f (diff)
downloadserenity-2d7b495244a76bf057a5c22032b1f5d49cef5637.zip
LibJS: Make Array constructor take its prototype
Let's start moving towards native JS objects taking their prototype as a constructor argument. This will eventually allow us to move prototypes off of Interpreter and into GlobalObject.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/AST.cpp2
-rw-r--r--Libraries/LibJS/Runtime/Array.cpp12
-rw-r--r--Libraries/LibJS/Runtime/Array.h5
-rw-r--r--Libraries/LibJS/Runtime/ArrayConstructor.cpp6
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp6
-rw-r--r--Libraries/LibJS/Runtime/ObjectConstructor.cpp2
-rw-r--r--Libraries/LibWeb/Bindings/DocumentWrapper.cpp2
7 files changed, 23 insertions, 12 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp
index ada01e8917..6929ce43ea 100644
--- a/Libraries/LibJS/AST.cpp
+++ b/Libraries/LibJS/AST.cpp
@@ -985,7 +985,7 @@ void ArrayExpression::dump(int indent) const
Value ArrayExpression::execute(Interpreter& interpreter) const
{
- auto* array = interpreter.heap().allocate<Array>();
+ auto* array = Array::create(interpreter.global_object());
for (auto& element : m_elements) {
auto value = Value();
if (element) {
diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp
index 78341ca338..2d526e1081 100644
--- a/Libraries/LibJS/Runtime/Array.cpp
+++ b/Libraries/LibJS/Runtime/Array.cpp
@@ -27,13 +27,21 @@
#include <AK/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Array.h>
+#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/ArrayPrototype.h>
#include <LibJS/Runtime/Error.h>
namespace JS {
-Array::Array()
+Array* Array::create(GlobalObject& global_object)
{
- set_prototype(interpreter().array_prototype());
+ auto& interpreter = global_object.interpreter();
+ return interpreter.heap().allocate<Array>(*interpreter.array_prototype());
+}
+
+Array::Array(Object& prototype)
+{
+ set_prototype(&prototype);
put_native_property("length", length_getter, length_setter);
}
diff --git a/Libraries/LibJS/Runtime/Array.h b/Libraries/LibJS/Runtime/Array.h
index 19d97416b6..d27f407d3e 100644
--- a/Libraries/LibJS/Runtime/Array.h
+++ b/Libraries/LibJS/Runtime/Array.h
@@ -32,7 +32,9 @@ namespace JS {
class Array final : public Object {
public:
- Array();
+ static Array* create(GlobalObject&);
+
+ explicit Array(Object& prototype);
virtual ~Array() override;
i32 length() const { return static_cast<i32>(elements().size()); }
@@ -45,4 +47,5 @@ private:
static void length_setter(Interpreter&, Value);
};
+
}
diff --git a/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Libraries/LibJS/Runtime/ArrayConstructor.cpp
index bfaaccd05f..f2578c0b81 100644
--- a/Libraries/LibJS/Runtime/ArrayConstructor.cpp
+++ b/Libraries/LibJS/Runtime/ArrayConstructor.cpp
@@ -46,11 +46,11 @@ ArrayConstructor::~ArrayConstructor()
Value ArrayConstructor::call(Interpreter& interpreter)
{
- if (interpreter.argument_count() == 0)
- return interpreter.heap().allocate<Array>();
+ if (interpreter.argument_count() <= 0)
+ return Array::create(interpreter.global_object());
if (interpreter.argument_count() == 1) {
- auto* array = interpreter.heap().allocate<Array>();
+ auto* array = Array::create(interpreter.global_object());
array->elements().resize(interpreter.argument(0).to_i32());
return array;
}
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index ffa30ac1e9..15de7ffdac 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -92,7 +92,7 @@ Value ArrayPrototype::filter(Interpreter& interpreter)
return {};
auto this_value = interpreter.argument(1);
auto initial_array_size = array->elements().size();
- auto* new_array = interpreter.heap().allocate<Array>();
+ auto* new_array = Array::create(interpreter.global_object());
for (size_t i = 0; i < initial_array_size; ++i) {
if (i >= array->elements().size())
break;
@@ -141,7 +141,7 @@ Value ArrayPrototype::map(Interpreter& interpreter)
return {};
auto this_value = interpreter.argument(1);
auto initial_array_size = array->elements().size();
- auto* new_array = interpreter.heap().allocate<Array>();
+ auto* new_array = Array::create(interpreter.global_object());
for (size_t i = 0; i < initial_array_size; ++i) {
if (i >= array->elements().size())
break;
@@ -236,7 +236,7 @@ Value ArrayPrototype::concat(Interpreter& interpreter)
if (!array)
return {};
- auto* new_array = interpreter.heap().allocate<Array>();
+ auto* new_array = Array::create(interpreter.global_object());
new_array->elements().append(array->elements());
for (size_t i = 0; i < interpreter.argument_count(); ++i) {
diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Libraries/LibJS/Runtime/ObjectConstructor.cpp
index f660df2bb9..aa54de74b9 100644
--- a/Libraries/LibJS/Runtime/ObjectConstructor.cpp
+++ b/Libraries/LibJS/Runtime/ObjectConstructor.cpp
@@ -67,7 +67,7 @@ Value ObjectConstructor::get_own_property_names(Interpreter& interpreter)
auto* object = interpreter.argument(0).to_object(interpreter.heap());
if (interpreter.exception())
return {};
- auto* result = interpreter.heap().allocate<Array>();
+ auto* result = Array::create(interpreter.global_object());
for (size_t i = 0; i < object->elements().size(); ++i) {
if (!object->elements()[i].is_empty())
result->elements().append(js_string(interpreter, String::number(i)));
diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp
index df9c84dcb2..a9439e3593 100644
--- a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp
+++ b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp
@@ -96,7 +96,7 @@ JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter)
auto selector = arguments[0].to_string();
auto elements = document->query_selector_all(selector);
// FIXME: This should be a static NodeList, not a plain JS::Array.
- auto* node_list = interpreter.heap().allocate<JS::Array>();
+ auto* node_list = JS::Array::create(interpreter.global_object());
for (auto& element : elements) {
node_list->elements().append(wrap(interpreter.heap(), element));
}