ladybird/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp
Linus Groh 09bd5f8772 LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.

This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.

What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.

Key changes include:

- 1:1 matching function names and parameters of all object-related
  functions, to avoid ambiguity. Previously we had things like put(),
  which the spec doesn't have - as a result it wasn't always clear which
  need to be used.
- Better separation between object abstract operations and internal
  methods - the former are always the same, the latter can be overridden
  (and are therefore virtual). The internal methods (i.e. [[Foo]] in the
  spec) are now prefixed with 'internal_' for clarity - again, it was
  previously not always clear which AO a certain method represents,
  get() could've been both Get and [[Get]] (I don't know which one it
  was closer to right now).
  Note that some of the old names have been kept until all code relying
  on them is updated, but they are now simple wrappers around the
  closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
  storage are now prefixed with 'storage_' to make their purpose clear,
  and as they are not part of the spec they should not contain any steps
  specified by it. Much functionality is now covered by the layers above
  it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
  by PropertyDescriptor - a concept similar to the current
  implementation, but more aligned with the actual spec. See the commit
  message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
  introduced more inline comments with the exact steps from the spec -
  this makes it super easy to verify correctness.
- East-const all the things.

As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.

Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)

Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 22:07:36 +01:00

255 lines
7.9 KiB
C++

/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Function.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayConstructor.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Shape.h>
namespace JS {
ArrayConstructor::ArrayConstructor(GlobalObject& global_object)
: NativeFunction(vm().names.Array.as_string(), *global_object.function_prototype())
{
}
ArrayConstructor::~ArrayConstructor()
{
}
void ArrayConstructor::initialize(GlobalObject& global_object)
{
auto& vm = this->vm();
NativeFunction::initialize(global_object);
// 23.1.2.4 Array.prototype, https://tc39.es/ecma262/#sec-array.prototype
define_property(vm.names.prototype, global_object.array_prototype(), 0);
define_property(vm.names.length, Value(1), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.from, from, 1, attr);
define_native_function(vm.names.isArray, is_array, 1, attr);
define_native_function(vm.names.of, of, 0, attr);
// 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
}
// 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
Value ArrayConstructor::call()
{
return construct(*this);
}
// 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
Value ArrayConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
auto* proto = get_prototype_from_constructor(global_object(), new_target, &GlobalObject::array_prototype);
if (vm.exception())
return {};
if (vm.argument_count() == 0)
return Array::create(global_object(), 0, proto);
if (vm.argument_count() == 1) {
auto length = vm.argument(0);
auto* array = Array::create(global_object(), 0, proto);
size_t int_length;
if (!length.is_number()) {
array->create_data_property_or_throw(0, length);
int_length = 1;
} else {
int_length = length.to_u32(global_object());
if (int_length != length.as_double()) {
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
return {};
}
}
array->set(vm.names.length, Value(int_length), true);
return array;
}
auto* array = Array::create(global_object(), vm.argument_count(), proto);
if (vm.exception())
return {};
for (size_t k = 0; k < vm.argument_count(); ++k)
array->create_data_property_or_throw(k, vm.argument(k));
return array;
}
// 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
{
auto constructor = vm.this_value(global_object);
FunctionObject* map_fn = nullptr;
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();
}
auto this_arg = vm.argument(2);
auto items = vm.argument(0);
auto using_iterator = items.get_method(global_object, *vm.well_known_symbol_iterator());
if (vm.exception())
return {};
if (using_iterator) {
Value array;
if (constructor.is_constructor()) {
array = vm.construct(constructor.as_function(), constructor.as_function(), {});
if (vm.exception())
return {};
} else {
array = Array::create(global_object, 0);
}
auto iterator = get_iterator(global_object, items, IteratorHint::Sync, using_iterator);
if (vm.exception())
return {};
auto& array_object = array.as_object();
size_t k = 0;
while (true) {
if (k >= MAX_ARRAY_LIKE_INDEX) {
vm.throw_exception<TypeError>(global_object, ErrorType::ArrayMaxSize);
iterator_close(*iterator);
return {};
}
auto next = iterator_step(global_object, *iterator);
if (vm.exception())
return {};
if (!next) {
array_object.set(vm.names.length, Value(k), true);
if (vm.exception())
return {};
return array;
}
auto next_value = iterator_value(global_object, *next);
if (vm.exception())
return {};
Value mapped_value;
if (map_fn) {
mapped_value = vm.call(*map_fn, this_arg, next_value, Value(k));
if (vm.exception()) {
iterator_close(*iterator);
return {};
}
} else {
mapped_value = next_value;
}
array_object.create_data_property_or_throw(k, mapped_value);
if (vm.exception()) {
iterator_close(*iterator);
return {};
}
++k;
}
}
auto* array_like = items.to_object(global_object);
auto length = length_of_array_like(global_object, *array_like);
if (vm.exception())
return {};
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 {
array = Array::create(global_object, length);
if (vm.exception())
return {};
}
auto& array_object = array.as_object();
for (size_t k = 0; k < length; ++k) {
auto k_value = array_like->get(k);
Value mapped_value;
if (map_fn) {
mapped_value = vm.call(*map_fn, this_arg, k_value, Value(k));
if (vm.exception())
return {};
} else {
mapped_value = k_value;
}
array_object.create_data_property_or_throw(k, mapped_value);
}
array_object.set(vm.names.length, Value(length), true);
if (vm.exception())
return {};
return array;
}
// 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
{
auto value = vm.argument(0);
return Value(value.is_array(global_object));
}
// 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
{
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 {
array = Array::create(global_object, vm.argument_count());
if (vm.exception())
return {};
}
auto& array_object = array.as_object();
for (size_t k = 0; k < vm.argument_count(); ++k) {
array_object.create_data_property_or_throw(k, vm.argument(k));
if (vm.exception())
return {};
}
array_object.set(vm.names.length, Value(vm.argument_count()), true);
if (vm.exception())
return {};
return array;
}
// 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
JS_DEFINE_NATIVE_GETTER(ArrayConstructor::symbol_species_getter)
{
return vm.this_value(global_object);
}
}