2020-04-04 20:28:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-04-22 20:51:19 +00:00
|
|
|
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
|
2020-04-04 20:28:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-04 20:28:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2021-07-03 22:36:44 +00:00
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2020-04-04 20:28:21 +00:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
|
|
|
#include <LibJS/Runtime/ArrayConstructor.h>
|
2020-05-08 15:23:56 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-18 11:18:06 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-08-17 13:52:24 +00:00
|
|
|
#include <LibJS/Runtime/IteratorOperations.h>
|
2020-04-04 20:28:21 +00:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-06-20 13:40:48 +00:00
|
|
|
ArrayConstructor::ArrayConstructor(GlobalObject& global_object)
|
2021-06-28 00:45:49 +00:00
|
|
|
: NativeFunction(vm().names.Array.as_string(), *global_object.function_prototype())
|
2020-04-04 20:28:21 +00:00
|
|
|
{
|
2020-06-20 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ArrayConstructor::~ArrayConstructor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:50:18 +00:00
|
|
|
void ArrayConstructor::initialize(GlobalObject& global_object)
|
2020-06-20 13:40:48 +00:00
|
|
|
{
|
2020-10-13 21:49:19 +00:00
|
|
|
auto& vm = this->vm();
|
2020-07-22 15:50:18 +00:00
|
|
|
NativeFunction::initialize(global_object);
|
2020-06-20 13:40:48 +00:00
|
|
|
|
2021-06-12 23:22:35 +00:00
|
|
|
// 23.1.2.4 Array.prototype, https://tc39.es/ecma262/#sec-array.prototype
|
2021-07-05 23:15:08 +00:00
|
|
|
define_direct_property(vm.names.prototype, global_object.array_prototype(), 0);
|
2021-06-12 23:22:35 +00:00
|
|
|
|
2020-05-08 15:28:35 +00:00
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2021-10-19 22:40:40 +00:00
|
|
|
define_old_native_function(vm.names.from, from, 1, attr);
|
|
|
|
define_old_native_function(vm.names.isArray, is_array, 1, attr);
|
|
|
|
define_old_native_function(vm.names.of, of, 0, attr);
|
2021-06-07 16:31:32 +00:00
|
|
|
|
2021-06-12 23:22:35 +00:00
|
|
|
// 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
|
2021-10-19 22:40:40 +00:00
|
|
|
define_old_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
|
2021-07-07 23:49:53 +00:00
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-04-04 20:28:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:22:35 +00:00
|
|
|
// 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
|
2021-10-20 20:16:30 +00:00
|
|
|
ThrowCompletionOr<Value> ArrayConstructor::call()
|
2020-04-04 20:28:21 +00:00
|
|
|
{
|
2021-10-20 20:16:30 +00:00
|
|
|
return TRY(construct(*this));
|
2021-07-03 22:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
|
2021-10-20 20:16:30 +00:00
|
|
|
ThrowCompletionOr<Object*> ArrayConstructor::construct(FunctionObject& new_target)
|
2021-07-03 22:36:44 +00:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
2021-10-20 20:16:30 +00:00
|
|
|
auto* proto = TRY(get_prototype_from_constructor(global_object(), new_target, &GlobalObject::array_prototype));
|
2021-07-03 22:36:44 +00:00
|
|
|
|
|
|
|
if (vm.argument_count() == 0)
|
2021-10-21 22:34:06 +00:00
|
|
|
return MUST(Array::create(global_object(), 0, proto));
|
2021-07-03 22:36:44 +00:00
|
|
|
|
|
|
|
if (vm.argument_count() == 1) {
|
|
|
|
auto length = vm.argument(0);
|
2021-10-21 22:34:06 +00:00
|
|
|
auto* array = MUST(Array::create(global_object(), 0, proto));
|
2021-07-03 22:36:44 +00:00
|
|
|
size_t int_length;
|
|
|
|
if (!length.is_number()) {
|
2021-10-03 00:18:46 +00:00
|
|
|
MUST(array->create_data_property_or_throw(0, length));
|
2021-07-03 22:36:44 +00:00
|
|
|
int_length = 1;
|
|
|
|
} else {
|
2021-10-17 20:43:29 +00:00
|
|
|
int_length = MUST(length.to_u32(global_object()));
|
2021-10-20 20:16:30 +00:00
|
|
|
if (int_length != length.as_double())
|
|
|
|
return vm.throw_completion<RangeError>(global_object(), ErrorType::InvalidLength, "array");
|
2020-05-08 15:23:56 +00:00
|
|
|
}
|
2021-10-20 20:16:30 +00:00
|
|
|
TRY(array->set(vm.names.length, Value(int_length), Object::ShouldThrowExceptions::Yes));
|
2020-04-04 20:28:21 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2021-10-21 22:34:06 +00:00
|
|
|
auto* array = TRY(Array::create(global_object(), vm.argument_count(), proto));
|
2020-04-04 20:28:21 +00:00
|
|
|
|
2021-07-03 22:36:44 +00:00
|
|
|
for (size_t k = 0; k < vm.argument_count(); ++k)
|
2021-10-03 00:18:46 +00:00
|
|
|
MUST(array->create_data_property_or_throw(k, vm.argument(k)));
|
2021-07-03 22:36:44 +00:00
|
|
|
|
|
|
|
return array;
|
2020-04-04 20:28:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:22:35 +00:00
|
|
|
// 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
|
2021-10-19 17:18:01 +00:00
|
|
|
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::from)
|
2020-08-17 13:52:24 +00:00
|
|
|
{
|
2021-07-03 22:36:44 +00:00
|
|
|
auto constructor = vm.this_value(global_object);
|
2020-08-17 13:52:24 +00:00
|
|
|
|
2021-06-27 19:48:34 +00:00
|
|
|
FunctionObject* map_fn = nullptr;
|
2021-04-06 20:12:38 +00:00
|
|
|
if (!vm.argument(1).is_undefined()) {
|
|
|
|
auto callback = vm.argument(1);
|
|
|
|
if (!callback.is_function()) {
|
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
map_fn = &callback.as_function();
|
|
|
|
}
|
|
|
|
|
2021-04-12 08:59:26 +00:00
|
|
|
auto this_arg = vm.argument(2);
|
|
|
|
|
2021-07-03 22:36:44 +00:00
|
|
|
auto items = vm.argument(0);
|
2021-09-23 18:24:51 +00:00
|
|
|
auto using_iterator = TRY_OR_DISCARD(items.get_method(global_object, *vm.well_known_symbol_iterator()));
|
2021-07-03 22:36:44 +00:00
|
|
|
if (using_iterator) {
|
|
|
|
Value array;
|
|
|
|
if (constructor.is_constructor()) {
|
|
|
|
array = vm.construct(constructor.as_function(), constructor.as_function(), {});
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
} else {
|
2021-10-21 22:34:06 +00:00
|
|
|
array = MUST(Array::create(global_object, 0));
|
2021-07-03 22:36:44 +00:00
|
|
|
}
|
2021-04-06 20:12:38 +00:00
|
|
|
|
2021-10-20 12:24:54 +00:00
|
|
|
auto iterator = TRY_OR_DISCARD(get_iterator(global_object, items, IteratorHint::Sync, using_iterator));
|
2021-07-03 22:36:44 +00:00
|
|
|
auto& array_object = array.as_object();
|
2021-04-06 20:12:38 +00:00
|
|
|
|
2021-07-03 22:36:44 +00:00
|
|
|
size_t k = 0;
|
|
|
|
while (true) {
|
|
|
|
if (k >= MAX_ARRAY_LIKE_INDEX) {
|
2021-10-20 17:36:14 +00:00
|
|
|
auto error = vm.throw_completion<TypeError>(global_object, ErrorType::ArrayMaxSize);
|
|
|
|
return TRY_OR_DISCARD(iterator_close(*iterator, move(error)));
|
2021-07-03 22:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 12:56:01 +00:00
|
|
|
auto* next = TRY_OR_DISCARD(iterator_step(global_object, *iterator));
|
2021-07-03 22:36:44 +00:00
|
|
|
if (!next) {
|
2021-10-02 23:32:43 +00:00
|
|
|
TRY_OR_DISCARD(array_object.set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes));
|
2021-07-03 22:36:44 +00:00
|
|
|
return array;
|
2021-04-06 20:12:38 +00:00
|
|
|
}
|
2021-07-03 22:36:44 +00:00
|
|
|
|
2021-10-20 13:05:52 +00:00
|
|
|
auto next_value = TRY_OR_DISCARD(iterator_value(global_object, *next));
|
2021-04-06 20:12:38 +00:00
|
|
|
|
2021-07-03 22:36:44 +00:00
|
|
|
Value mapped_value;
|
2021-04-06 20:12:38 +00:00
|
|
|
if (map_fn) {
|
2021-09-23 22:41:54 +00:00
|
|
|
auto mapped_value_or_error = vm.call(*map_fn, this_arg, next_value, Value(k));
|
2021-10-20 17:36:14 +00:00
|
|
|
if (mapped_value_or_error.is_error())
|
|
|
|
return TRY_OR_DISCARD(iterator_close(*iterator, mapped_value_or_error.release_error()));
|
2021-09-23 22:41:54 +00:00
|
|
|
mapped_value = mapped_value_or_error.release_value();
|
2021-04-06 20:12:38 +00:00
|
|
|
} else {
|
2021-07-03 22:36:44 +00:00
|
|
|
mapped_value = next_value;
|
2021-04-06 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 00:18:46 +00:00
|
|
|
auto result_or_error = array_object.create_data_property_or_throw(k, mapped_value);
|
2021-10-20 17:36:14 +00:00
|
|
|
if (result_or_error.is_error())
|
|
|
|
return TRY_OR_DISCARD(iterator_close(*iterator, result_or_error.release_error()));
|
2021-07-03 22:36:44 +00:00
|
|
|
|
|
|
|
++k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 18:24:57 +00:00
|
|
|
auto* array_like = MUST(items.to_object(global_object));
|
2021-07-03 22:36:44 +00:00
|
|
|
|
2021-09-21 18:59:49 +00:00
|
|
|
auto length = TRY_OR_DISCARD(length_of_array_like(global_object, *array_like));
|
2021-07-03 22:36:44 +00:00
|
|
|
|
|
|
|
Value array;
|
|
|
|
if (constructor.is_constructor()) {
|
|
|
|
MarkedValueList arguments(vm.heap());
|
|
|
|
arguments.empend(length);
|
|
|
|
array = vm.construct(constructor.as_function(), constructor.as_function(), move(arguments));
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
} else {
|
2021-10-21 22:34:06 +00:00
|
|
|
array = TRY_OR_DISCARD(Array::create(global_object, length));
|
2020-08-17 13:52:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:36:44 +00:00
|
|
|
auto& array_object = array.as_object();
|
|
|
|
|
|
|
|
for (size_t k = 0; k < length; ++k) {
|
2021-10-02 22:52:27 +00:00
|
|
|
auto k_value = TRY_OR_DISCARD(array_like->get(k));
|
2021-07-03 22:36:44 +00:00
|
|
|
Value mapped_value;
|
2021-09-23 17:56:28 +00:00
|
|
|
if (map_fn)
|
|
|
|
mapped_value = TRY_OR_DISCARD(vm.call(*map_fn, this_arg, k_value, Value(k)));
|
|
|
|
else
|
2021-07-03 22:36:44 +00:00
|
|
|
mapped_value = k_value;
|
2021-10-03 00:18:46 +00:00
|
|
|
TRY_OR_DISCARD(array_object.create_data_property_or_throw(k, mapped_value));
|
2021-07-03 22:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 23:32:43 +00:00
|
|
|
TRY_OR_DISCARD(array_object.set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));
|
2021-07-03 22:36:44 +00:00
|
|
|
|
2020-08-17 13:52:24 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2021-06-12 23:22:35 +00:00
|
|
|
// 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
|
2021-10-19 17:18:01 +00:00
|
|
|
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::is_array)
|
2020-05-08 15:28:35 +00:00
|
|
|
{
|
2020-09-27 16:36:49 +00:00
|
|
|
auto value = vm.argument(0);
|
2021-09-23 17:05:34 +00:00
|
|
|
return Value(TRY_OR_DISCARD(value.is_array(global_object)));
|
2020-05-08 15:28:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:22:35 +00:00
|
|
|
// 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
|
2021-10-19 17:18:01 +00:00
|
|
|
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::of)
|
2020-05-08 15:32:56 +00:00
|
|
|
{
|
2021-06-29 12:06:43 +00:00
|
|
|
auto this_value = vm.this_value(global_object);
|
|
|
|
Value array;
|
|
|
|
if (this_value.is_constructor()) {
|
|
|
|
MarkedValueList arguments(vm.heap());
|
|
|
|
arguments.empend(vm.argument_count());
|
|
|
|
array = vm.construct(this_value.as_function(), this_value.as_function(), move(arguments));
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
} else {
|
2021-10-21 22:34:06 +00:00
|
|
|
array = TRY_OR_DISCARD(Array::create(global_object, vm.argument_count()));
|
2021-06-29 12:06:43 +00:00
|
|
|
}
|
|
|
|
auto& array_object = array.as_object();
|
2021-10-03 00:18:46 +00:00
|
|
|
for (size_t k = 0; k < vm.argument_count(); ++k)
|
|
|
|
TRY_OR_DISCARD(array_object.create_data_property_or_throw(k, vm.argument(k)));
|
2021-10-02 23:32:43 +00:00
|
|
|
TRY_OR_DISCARD(array_object.set(vm.names.length, Value(vm.argument_count()), Object::ShouldThrowExceptions::Yes));
|
2020-05-08 15:32:56 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2021-06-12 23:22:35 +00:00
|
|
|
// 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
|
2021-10-19 17:18:01 +00:00
|
|
|
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::symbol_species_getter)
|
2021-06-07 16:31:32 +00:00
|
|
|
{
|
|
|
|
return vm.this_value(global_object);
|
|
|
|
}
|
|
|
|
|
2020-04-04 20:28:21 +00:00
|
|
|
}
|