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>
2021-09-29 16:53:57 +00:00
# include <LibJS/Runtime/Completion.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-12 23:22:35 +00:00
// 10.4.2.2 ArrayCreate ( length [ , proto ] ), https://tc39.es/ecma262/#sec-arraycreate
2021-07-03 22:36:44 +00:00
Array * Array : : create ( GlobalObject & global_object , size_t length , Object * prototype )
2020-03-20 19:29:57 +00:00
{
2021-07-03 22:36:44 +00:00
auto & vm = global_object . vm ( ) ;
2021-06-06 22:25:33 +00:00
if ( length > NumericLimits < u32 > : : max ( ) ) {
vm . throw_exception < RangeError > ( global_object , ErrorType : : InvalidLength , " array " ) ;
return nullptr ;
}
2021-07-03 22:36:44 +00:00
if ( ! prototype )
prototype = global_object . array_prototype ( ) ;
auto * array = global_object . heap ( ) . allocate < Array > ( global_object , * prototype ) ;
2021-09-29 16:54:25 +00:00
( void ) array - > internal_define_own_property ( vm . names . length , { . value = Value ( length ) , . writable = true , . enumerable = false , . configurable = false } ) ;
2021-06-06 22:25:33 +00:00
return array ;
2020-04-17 16:24:01 +00:00
}
2021-06-12 23:22:35 +00:00
// 7.3.17 CreateArrayFromList ( elements ), https://tc39.es/ecma262/#sec-createarrayfromlist
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 * Array : : create_from ( GlobalObject & global_object , Vector < Value > const & elements )
2021-04-06 19:39:17 +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
// 1. Assert: elements is a List whose elements are all ECMAScript language values.
// 2. Let array be ! ArrayCreate(0).
2021-07-03 22:36:44 +00:00
auto * array = Array : : create ( global_object , 0 ) ;
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
// 3. Let n be 0.
// 4. For each element e of elements, do
for ( u32 n = 0 ; n < elements . size ( ) ; + + n ) {
// a. Perform ! CreateDataPropertyOrThrow(array, ! ToString(𝔽 (n)), e).
array - > create_data_property_or_throw ( n , elements [ n ] ) ;
// b. Set n to n + 1.
}
// 5. Return array.
2021-04-06 19:39:17 +00:00
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
{
}
2020-03-20 19:29:57 +00:00
Array : : ~ Array ( )
{
}
2021-07-07 00:50:19 +00:00
// 10.4.2.4 ArraySetLength ( A, Desc ), https://tc39.es/ecma262/#sec-arraysetlength
bool Array : : set_length ( PropertyDescriptor const & property_descriptor )
2020-03-28 23:37:33 +00:00
{
2021-07-07 00:50:19 +00:00
auto & global_object = this - > global_object ( ) ;
auto & vm = this - > vm ( ) ;
// 1. If Desc.[[Value]] is absent, then
// a. Return OrdinaryDefineOwnProperty(A, "length", Desc).
// 2. Let newLenDesc be a copy of Desc.
// NOTE: Handled by step 16
size_t new_length = indexed_properties ( ) . array_like_size ( ) ;
if ( property_descriptor . value . has_value ( ) ) {
// 3. Let newLen be ? ToUint32(Desc.[[Value]]).
new_length = property_descriptor . value - > to_u32 ( global_object ) ;
if ( vm . exception ( ) )
return { } ;
// 4. Let numberLen be ? ToNumber(Desc.[[Value]]).
auto number_length = property_descriptor . value - > to_number ( global_object ) ;
if ( vm . exception ( ) )
return { } ;
// 5. If newLen is not the same value as numberLen, throw a RangeError exception.
if ( new_length ! = number_length . as_double ( ) ) {
vm . throw_exception < RangeError > ( global_object , ErrorType : : InvalidLength , " array " ) ;
return { } ;
}
2020-04-22 23:33:13 +00:00
}
2021-07-07 00:50:19 +00:00
// 6. Set newLenDesc.[[Value]] to newLen.
// 7. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
// 8. Assert: ! IsDataDescriptor(oldLenDesc) is true.
// 9. Assert: oldLenDesc.[[Configurable]] is false.
// 10. Let oldLen be oldLenDesc.[[Value]].
// 11. If newLen ≥ oldLen, then
// a. Return OrdinaryDefineOwnProperty(A, "length", newLenDesc).
// 12. If oldLenDesc.[[Writable]] is false, return false.
// NOTE: Handled by step 16
// 13. If newLenDesc.[[Writable]] is absent or has the value true, let newWritable be true.
// 14. Else,
// a. NOTE: Setting the [[Writable]] attribute to false is deferred in case any elements cannot be deleted.
// b. Let newWritable be false.
auto new_writable = property_descriptor . writable . value_or ( true ) ;
// c. Set newLenDesc.[[Writable]] to true.
// 15. Let succeeded be ! OrdinaryDefineOwnProperty(A, "length", newLenDesc).
// 16. If succeeded is false, return false.
// NOTE: Because the length property does not actually exist calling OrdinaryDefineOwnProperty
2021-09-07 10:56:50 +00:00
// will result in unintended behavior, so instead we only implement here the small subset of
2021-07-07 00:50:19 +00:00
// checks performed inside of it that would have mattered to us:
// 10.1.6.3 ValidateAndApplyPropertyDescriptor ( O, P, extensible, Desc, current ), https://tc39.es/ecma262/#sec-validateandapplypropertydescriptor
// 4. If current.[[Configurable]] is false, then
// a. If Desc.[[Configurable]] is present and its value is true, return false.
if ( property_descriptor . configurable . has_value ( ) & & * property_descriptor . configurable )
return false ;
// b. If Desc.[[Enumerable]] is present and ! SameValue(Desc.[[Enumerable]], current.[[Enumerable]]) is false, return false.
if ( property_descriptor . enumerable . has_value ( ) & & * property_descriptor . enumerable )
return false ;
// 6. Else if ! SameValue(! IsDataDescriptor(current), ! IsDataDescriptor(Desc)) is false, then
if ( property_descriptor . is_accessor_descriptor ( ) ) {
// a. If current.[[Configurable]] is false, return false.
return false ;
}
// 7. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both true, then
// a. If current.[[Configurable]] is false and current.[[Writable]] is false, then
if ( ! m_length_writable ) {
// i. If Desc.[[Writable]] is present and Desc.[[Writable]] is true, return false.
if ( property_descriptor . writable . has_value ( ) & & * property_descriptor . writable )
return false ;
// ii. If Desc.[[Value]] is present and SameValue(Desc.[[Value]], current.[[Value]]) is false, return false.
if ( new_length ! = indexed_properties ( ) . array_like_size ( ) )
return false ;
}
// 17. For each own property key P of A that is an array index, whose numeric value is greater than or equal to newLen, in descending numeric index order, do
// a. Let deleteSucceeded be ! A.[[Delete]](P).
// b. If deleteSucceeded is false, then
// i. Set newLenDesc.[[Value]] to ! ToUint32(P) + 1𝔽 .
bool success = indexed_properties ( ) . set_array_like_size ( new_length ) ;
// ii. If newWritable is false, set newLenDesc.[[Writable]] to false.
// iii. Perform ! OrdinaryDefineOwnProperty(A, "length", newLenDesc).
// NOTE: Handled by step 18
// 18. If newWritable is false, then
// a. Set succeeded to ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Writable]]: false }).
// b. Assert: succeeded is true.
if ( ! new_writable )
m_length_writable = false ;
// NOTE: Continuation of step #17
// iv. Return false.
if ( ! success )
return false ;
// 19. Return true.
return true ;
2020-04-22 23:33:13 +00:00
}
2021-07-07 00:50:19 +00:00
// NON-STANDARD: Used to return the value of the ephemeral length property
2021-09-29 16:53:57 +00:00
ThrowCompletionOr < Optional < PropertyDescriptor > > Array : : internal_get_own_property ( PropertyName const & property_name ) const
2020-04-22 23:33:13 +00:00
{
2021-07-07 00:50:19 +00:00
auto & vm = this - > vm ( ) ;
if ( property_name . is_string ( ) & & property_name . as_string ( ) = = vm . names . length . as_string ( ) )
2021-09-29 16:53:57 +00:00
return { PropertyDescriptor { . value = Value ( indexed_properties ( ) . array_like_size ( ) ) , . writable = m_length_writable , . enumerable = false , . configurable = false } } ;
2021-06-25 14:57:12 +00:00
2021-07-07 00:50:19 +00:00
return Object : : internal_get_own_property ( property_name ) ;
2020-03-28 23:37:33 +00:00
}
2021-07-07 00:50:19 +00:00
// 10.4.2.1 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-array-exotic-objects-defineownproperty-p-desc
2021-09-29 16:54:25 +00:00
ThrowCompletionOr < bool > Array : : internal_define_own_property ( PropertyName const & property_name , PropertyDescriptor const & property_descriptor )
2020-03-28 23:37:33 +00:00
{
2021-07-07 00:50:19 +00:00
auto & vm = this - > vm ( ) ;
2021-06-25 14:57:12 +00:00
2021-07-07 00:50:19 +00:00
// 1. Assert: IsPropertyKey(P) is true.
VERIFY ( property_name . is_valid ( ) ) ;
2021-06-25 14:57:12 +00:00
2021-07-07 00:50:19 +00:00
// 2. If P is "length", then
if ( property_name . is_string ( ) & & property_name . as_string ( ) = = vm . names . length . as_string ( ) ) {
// a. Return ? ArraySetLength(A, Desc).
2021-09-29 16:54:25 +00:00
auto success = set_length ( property_descriptor ) ;
if ( auto * exception = vm . exception ( ) )
return throw_completion ( exception - > value ( ) ) ;
return success ;
2021-07-07 00:50:19 +00:00
}
2021-06-25 14:57:12 +00:00
2021-07-07 00:50:19 +00:00
// 3. Else if P is an array index, then
if ( property_name . is_number ( ) ) {
// a. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
// b. Assert: ! IsDataDescriptor(oldLenDesc) is true.
// c. Assert: oldLenDesc.[[Configurable]] is false.
// d. Let oldLen be oldLenDesc.[[Value]].
// e. Assert: oldLen is a non-negative integral Number.
// f. Let index be ! ToUint32(P).
// g. If index ≥ oldLen and oldLenDesc.[[Writable]] is false, return false.
if ( property_name . as_number ( ) > = indexed_properties ( ) . array_like_size ( ) & & ! m_length_writable )
return false ;
// h. Let succeeded be ! OrdinaryDefineOwnProperty(A, P, Desc).
2021-09-29 16:54:25 +00:00
auto succeeded = Object : : internal_define_own_property ( property_name , property_descriptor ) . release_value ( ) ;
2021-07-07 00:50:19 +00:00
// i. If succeeded is false, return false.
if ( ! succeeded )
return false ;
// j. If index ≥ oldLen, then
// i. Set oldLenDesc.[[Value]] to index + 1𝔽 .
// ii. Set succeeded to OrdinaryDefineOwnProperty(A, "length", oldLenDesc).
// iii. Assert: succeeded is true.
// k. Return true.
return true ;
2020-04-22 23:33:13 +00:00
}
2021-07-07 00:50:19 +00:00
// 4. Return OrdinaryDefineOwnProperty(A, P, Desc).
return Object : : internal_define_own_property ( property_name , property_descriptor ) ;
}
// NON-STANDARD: Used to reject deletes to ephemeral (non-configurable) length property
2021-09-29 17:45:33 +00:00
ThrowCompletionOr < bool > Array : : internal_delete ( PropertyName const & property_name )
2021-07-07 00:50:19 +00:00
{
auto & vm = this - > vm ( ) ;
if ( property_name . is_string ( ) & & property_name . as_string ( ) = = vm . names . length . as_string ( ) )
return false ;
return Object : : internal_delete ( property_name ) ;
}
// NON-STANDARD: Used to inject the ephemeral length property's key
MarkedValueList Array : : internal_own_property_keys ( ) const
{
auto & vm = this - > vm ( ) ;
auto keys = Object : : internal_own_property_keys ( ) ;
if ( vm . exception ( ) )
return MarkedValueList { vm . heap ( ) } ;
// FIXME: This is pretty expensive, find a better way to do this
keys . insert ( indexed_properties ( ) . real_size ( ) , js_string ( vm , vm . names . length . as_string ( ) ) ) ;
return keys ;
2020-03-28 23:37:33 +00:00
}
2020-03-20 19:29:57 +00:00
}