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;
|
2020-10-13 21:49:19 +00:00
|
|
|
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);
|
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-06-25 17:37:14 +00:00
|
|
|
define_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
|
2020-09-27 15:24:14 +00:00
|
|
|
Value ArrayConstructor::call()
|
2020-04-04 20:28:21 +00:00
|
|
|
{
|
2021-07-03 22:36:44 +00:00
|
|
|
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()) {
|
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 17:14:16 +00:00
|
|
|
array->create_data_property_or_throw(0, length);
|
2021-07-03 22:36:44 +00:00
|
|
|
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 {};
|
|
|
|
}
|
2020-05-08 15:23:56 +00:00
|
|
|
}
|
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 17:14:16 +00:00
|
|
|
array->set(vm.names.length, Value(int_length), true);
|
2020-04-04 20:28:21 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:36:44 +00:00
|
|
|
auto* array = Array::create(global_object(), vm.argument_count(), proto);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
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)
|
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 17:14:16 +00:00
|
|
|
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
|
2020-08-17 13:52:24 +00:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
|
|
|
|
{
|
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);
|
|
|
|
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 {};
|
2021-04-06 20:12:38 +00:00
|
|
|
|
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) {
|
|
|
|
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) {
|
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 17:14:16 +00:00
|
|
|
array_object.set(vm.names.length, Value(k), true);
|
2021-04-06 20:12:38 +00:00
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
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
|
|
|
|
|
|
|
auto next_value = iterator_value(global_object, *next);
|
2020-09-27 16:36:49 +00:00
|
|
|
if (vm.exception())
|
2021-07-03 22:36:44 +00:00
|
|
|
return {};
|
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-07-03 22:36:44 +00:00
|
|
|
mapped_value = vm.call(*map_fn, this_arg, next_value, Value(k));
|
|
|
|
if (vm.exception()) {
|
|
|
|
iterator_close(*iterator);
|
|
|
|
return {};
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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 17:14:16 +00:00
|
|
|
array_object.create_data_property_or_throw(k, mapped_value);
|
2021-07-03 22:36:44 +00:00
|
|
|
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);
|
2020-09-27 16:36:49 +00:00
|
|
|
if (vm.exception())
|
2020-08-17 13:52:24 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:36:44 +00:00
|
|
|
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;
|
|
|
|
}
|
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 17:14:16 +00:00
|
|
|
array_object.create_data_property_or_throw(k, mapped_value);
|
2021-07-03 22:36:44 +00:00
|
|
|
}
|
|
|
|
|
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 17:14:16 +00:00
|
|
|
array_object.set(vm.names.length, Value(length), true);
|
2021-07-03 22:36:44 +00:00
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
|
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
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_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-06-08 20:53:36 +00:00
|
|
|
return Value(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
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_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-07-03 22:36:44 +00:00
|
|
|
array = Array::create(global_object, vm.argument_count());
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
2021-06-29 12:06:43 +00:00
|
|
|
}
|
|
|
|
auto& array_object = array.as_object();
|
|
|
|
for (size_t k = 0; k < vm.argument_count(); ++k) {
|
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 17:14:16 +00:00
|
|
|
array_object.create_data_property_or_throw(k, vm.argument(k));
|
2021-06-29 12:06:43 +00:00
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
}
|
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 17:14:16 +00:00
|
|
|
array_object.set(vm.names.length, Value(vm.argument_count()), true);
|
2021-06-29 12:06:43 +00:00
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
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-06-07 16:31:32 +00:00
|
|
|
JS_DEFINE_NATIVE_GETTER(ArrayConstructor::symbol_species_getter)
|
|
|
|
{
|
|
|
|
return vm.this_value(global_object);
|
|
|
|
}
|
|
|
|
|
2020-04-04 20:28:21 +00:00
|
|
|
}
|