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.
This commit is contained in:
parent
ee5816b9c8
commit
2d7b495244
Notes:
sideshowbarker
2024-07-19 07:31:46 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/2d7b495244a
7 changed files with 23 additions and 12 deletions
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)));
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue