2020-03-20 16:29:57 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-06-06 19:25:33 -03:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2020-03-20 16:29:57 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-20 16:29:57 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-03-28 20:37:33 -03:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-18 05:27:57 -03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-20 16:29:57 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 10.4.2.2 ArrayCreate ( length [ , proto ] ), https://tc39.es/ecma262/#sec-arraycreate
|
2021-07-03 19:36:44 -03:00
|
|
|
Array* Array::create(GlobalObject& global_object, size_t length, Object* prototype)
|
2020-03-20 16:29:57 -03:00
|
|
|
{
|
2021-07-03 19:36:44 -03:00
|
|
|
auto& vm = global_object.vm();
|
2021-06-06 19:25:33 -03:00
|
|
|
if (length > NumericLimits<u32>::max()) {
|
|
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-07-03 19:36:44 -03:00
|
|
|
if (!prototype)
|
|
|
|
|
prototype = global_object.array_prototype();
|
|
|
|
|
auto* array = global_object.heap().allocate<Array>(global_object, *prototype);
|
|
|
|
|
array->put(vm.names.length, Value(length));
|
2021-06-06 19:25:33 -03:00
|
|
|
return array;
|
2020-04-17 13:24:01 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 7.3.17 CreateArrayFromList ( elements ), https://tc39.es/ecma262/#sec-createarrayfromlist
|
|
|
|
|
Array* Array::create_from(GlobalObject& global_object, const Vector<Value>& elements)
|
2021-04-06 16:39:17 -03:00
|
|
|
{
|
2021-07-03 19:36:44 -03:00
|
|
|
auto* array = Array::create(global_object, 0);
|
2021-06-12 20:22:35 -03:00
|
|
|
for (size_t i = 0; i < elements.size(); ++i)
|
|
|
|
|
array->define_property(i, elements[i]);
|
2021-04-06 16:39:17 -03:00
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 13:24:01 -03:00
|
|
|
Array::Array(Object& prototype)
|
2020-06-23 12:21:53 -03:00
|
|
|
: Object(prototype)
|
2021-02-24 05:48:29 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Array::initialize(GlobalObject& global_object)
|
2020-04-17 13:24:01 -03:00
|
|
|
{
|
2020-10-13 18:49:19 -03:00
|
|
|
auto& vm = this->vm();
|
2021-02-24 05:48:29 -03:00
|
|
|
Object::initialize(global_object);
|
2020-10-13 18:49:19 -03:00
|
|
|
define_native_property(vm.names.length, length_getter, length_setter, Attribute::Writable);
|
2020-03-20 16:29:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array::~Array()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 13:36:49 -03:00
|
|
|
Array* Array::typed_this(VM& vm, GlobalObject& global_object)
|
2020-03-28 20:37:33 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
2020-03-28 20:37:33 -03:00
|
|
|
if (!this_object)
|
|
|
|
|
return {};
|
2020-04-22 20:33:13 -03:00
|
|
|
if (!this_object->is_array()) {
|
2020-09-27 13:36:49 -03:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotAn, "Array");
|
2020-04-22 20:33:13 -03:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return static_cast<Array*>(this_object);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_GETTER(Array::length_getter)
|
2020-04-22 20:33:13 -03:00
|
|
|
{
|
2021-06-25 11:57:12 -03:00
|
|
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
|
|
|
|
if (!this_object)
|
2020-04-22 20:33:13 -03:00
|
|
|
return {};
|
2021-06-25 11:57:12 -03:00
|
|
|
|
|
|
|
|
// TODO: could be incorrect if receiver/this_value is fixed or changed
|
|
|
|
|
if (!this_object->is_array()) {
|
|
|
|
|
Value val = this_object->get_own_property(vm.names.length.to_string_or_symbol(), this_object);
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
return {};
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Value(this_object->indexed_properties().array_like_size());
|
2020-03-28 20:37:33 -03:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_SETTER(Array::length_setter)
|
2020-03-28 20:37:33 -03:00
|
|
|
{
|
2021-06-25 11:57:12 -03:00
|
|
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
|
|
|
|
if (!this_object)
|
2020-04-22 20:33:13 -03:00
|
|
|
return;
|
2021-06-25 11:57:12 -03:00
|
|
|
|
|
|
|
|
// TODO: could be incorrect if receiver/this_value is fixed or changed
|
|
|
|
|
if (!this_object->is_array()) {
|
|
|
|
|
this_object->define_property(vm.names.length.to_string_or_symbol(), value, default_attributes);
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
return;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 13:36:49 -03:00
|
|
|
auto length = value.to_number(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-05-17 20:28:00 -03:00
|
|
|
return;
|
2021-06-25 11:57:12 -03:00
|
|
|
|
|
|
|
|
u32 val = length.as_double();
|
|
|
|
|
|
|
|
|
|
if (val != length.as_double()
|
|
|
|
|
|| length.is_nan()
|
|
|
|
|
|| length.is_infinity()
|
|
|
|
|
|| length.as_double() < 0
|
|
|
|
|
|| length.as_double() > NumericLimits<u32>::max()) {
|
2020-12-02 09:41:51 -03:00
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
|
2020-04-22 20:33:13 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2021-06-25 11:57:12 -03:00
|
|
|
this_object->indexed_properties().set_array_like_size(val);
|
2020-03-28 20:37:33 -03:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 16:29:57 -03:00
|
|
|
}
|