2020-03-20 19:29:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-06-06 22:25:33 +00:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2020-03-20 19:29:57 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-20 19:29:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-03-28 23:37:33 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-18 08:27:57 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-20 19:29:57 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-06-06 22:25:33 +00:00
|
|
|
// 10.4.2.2 ArrayCreate, https://tc39.es/ecma262/#sec-arraycreate
|
|
|
|
Array* Array::create(GlobalObject& global_object, size_t length)
|
2020-03-20 19:29:57 +00:00
|
|
|
{
|
2021-06-06 22:25:33 +00:00
|
|
|
if (length > NumericLimits<u32>::max()) {
|
|
|
|
auto& vm = global_object.vm();
|
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto* array = global_object.heap().allocate<Array>(global_object, *global_object.array_prototype());
|
|
|
|
array->indexed_properties().set_array_like_size(length);
|
|
|
|
return array;
|
2020-04-17 16:24:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 19:39:17 +00:00
|
|
|
// 7.3.17 CreateArrayFromList, https://tc39.es/ecma262/#sec-createarrayfromlist
|
|
|
|
Array* Array::create_from(GlobalObject& global_object, const Vector<Value>& values)
|
|
|
|
{
|
|
|
|
auto* array = Array::create(global_object);
|
|
|
|
for (size_t i = 0; i < values.size(); ++i)
|
|
|
|
array->define_property(i, values[i]);
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2020-04-17 16:24:01 +00:00
|
|
|
Array::Array(Object& prototype)
|
2020-06-23 15:21:53 +00:00
|
|
|
: Object(prototype)
|
2021-02-24 08:48:29 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Array::initialize(GlobalObject& global_object)
|
2020-04-17 16:24:01 +00:00
|
|
|
{
|
2020-10-13 21:49:19 +00:00
|
|
|
auto& vm = this->vm();
|
2021-02-24 08:48:29 +00:00
|
|
|
Object::initialize(global_object);
|
2020-10-13 21:49:19 +00:00
|
|
|
define_native_property(vm.names.length, length_getter, length_setter, Attribute::Writable);
|
2020-03-20 19:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Array::~Array()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-27 16:36:49 +00:00
|
|
|
Array* Array::typed_this(VM& vm, GlobalObject& global_object)
|
2020-03-28 23:37:33 +00:00
|
|
|
{
|
2020-09-27 16:36:49 +00:00
|
|
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
2020-03-28 23:37:33 +00:00
|
|
|
if (!this_object)
|
|
|
|
return {};
|
2020-04-22 23:33:13 +00:00
|
|
|
if (!this_object->is_array()) {
|
2020-09-27 16:36:49 +00:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotAn, "Array");
|
2020-04-22 23:33:13 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return static_cast<Array*>(this_object);
|
|
|
|
}
|
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_GETTER(Array::length_getter)
|
2020-04-22 23:33:13 +00:00
|
|
|
{
|
2020-09-27 16:36:49 +00:00
|
|
|
auto* array = typed_this(vm, global_object);
|
2020-04-22 23:33:13 +00:00
|
|
|
if (!array)
|
|
|
|
return {};
|
2021-03-30 11:32:19 +00:00
|
|
|
return Value(array->indexed_properties().array_like_size());
|
2020-03-28 23:37:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_SETTER(Array::length_setter)
|
2020-03-28 23:37:33 +00:00
|
|
|
{
|
2020-09-27 16:36:49 +00:00
|
|
|
auto* array = typed_this(vm, global_object);
|
2020-04-22 23:33:13 +00:00
|
|
|
if (!array)
|
|
|
|
return;
|
2020-09-27 16:36:49 +00:00
|
|
|
auto length = value.to_number(global_object);
|
|
|
|
if (vm.exception())
|
2020-05-17 23:28:00 +00:00
|
|
|
return;
|
2020-04-22 23:33:13 +00:00
|
|
|
if (length.is_nan() || length.is_infinity() || length.as_double() < 0) {
|
2020-12-02 12:41:51 +00:00
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
|
2020-04-22 23:33:13 +00:00
|
|
|
return;
|
|
|
|
}
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 18:35:09 +00:00
|
|
|
array->indexed_properties().set_array_like_size(length.as_double());
|
2020-03-28 23:37:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 19:29:57 +00:00
|
|
|
}
|