2020-03-20 19:29:57 +00:00
/*
2024-10-04 11:19:50 +00:00
* Copyright ( c ) 2020 , Andreas Kling < andreas @ ladybird . org >
2022-04-30 20:22:52 +00:00
* Copyright ( c ) 2020 - 2022 , 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>
2022-06-13 06:53:58 +00:00
# include <LibJS/Runtime/AbstractOperations.h>
2020-03-20 19:29:57 +00:00
# include <LibJS/Runtime/Array.h>
2022-06-13 06:55:25 +00:00
# include <LibJS/Runtime/ArrayPrototype.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>
2022-06-13 06:55:25 +00:00
# include <LibJS/Runtime/NativeFunction.h>
2023-10-06 15:54:21 +00:00
# include <LibJS/Runtime/ValueInlines.h>
2020-03-20 19:29:57 +00:00
namespace JS {
2024-11-14 15:01:23 +00:00
GC_DEFINE_ALLOCATOR ( Array ) ;
2023-11-19 08:45:05 +00:00
2021-06-12 23:22:35 +00:00
// 10.4.2.2 ArrayCreate ( length [ , proto ] ), https://tc39.es/ecma262/#sec-arraycreate
2024-11-14 15:01:23 +00:00
ThrowCompletionOr < GC : : Ref < Array > > Array : : create ( Realm & realm , u64 length , Object * prototype )
2020-03-20 19:29:57 +00:00
{
2022-08-15 23:20:49 +00:00
auto & vm = realm . vm ( ) ;
2022-07-03 14:21:35 +00:00
// 1. If length > 2^32 - 1, throw a RangeError exception.
2021-10-21 22:34:06 +00:00
if ( length > NumericLimits < u32 > : : max ( ) )
2022-08-16 19:33:17 +00:00
return vm . throw_completion < RangeError > ( ErrorType : : InvalidLength , " array " ) ;
2022-07-03 14:21:35 +00:00
// 2. If proto is not present, set proto to %Array.prototype%.
2021-07-03 22:36:44 +00:00
if ( ! prototype )
2022-08-26 23:54:55 +00:00
prototype = realm . intrinsics ( ) . array_prototype ( ) ;
2022-07-03 14:21:35 +00:00
// 3. Let A be MakeBasicObject(« [[Prototype]], [[Extensible]] »).
// 4. Set A.[[Prototype]] to proto.
// 5. Set A.[[DefineOwnProperty]] as specified in 10.4.2.1.
2024-11-13 16:50:17 +00:00
auto array = realm . create < Array > ( * prototype ) ;
2022-07-03 14:21:35 +00:00
// 6. Perform ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Value]]: 𝔽 (length), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
2021-10-03 14:57:26 +00:00
MUST ( array - > internal_define_own_property ( vm . names . length , { . value = Value ( length ) , . writable = true , . enumerable = false , . configurable = false } ) ) ;
2022-07-03 14:21:35 +00:00
// 7. Return A.
2022-12-14 17:40:33 +00:00
return array ;
2020-04-17 16:24:01 +00:00
}
2021-12-21 13:27:04 +00:00
// 7.3.18 CreateArrayFromList ( elements ), https://tc39.es/ecma262/#sec-createarrayfromlist
2024-11-14 15:01:23 +00:00
GC : : Ref < Array > Array : : create_from ( Realm & realm , ReadonlySpan < Value > elements )
2023-11-27 11:56:20 +00:00
{
// 1. Let array be ! ArrayCreate(0).
auto array = MUST ( Array : : create ( realm , 0 ) ) ;
// 2. Let n be 0.
// 3. For each element e of elements, do
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
for ( u32 n = 0 ; n < elements . size ( ) ; + + n ) {
// a. Perform ! CreateDataPropertyOrThrow(array, ! ToString(𝔽 (n)), e).
2021-10-03 00:18:46 +00:00
MUST ( array - > create_data_property_or_throw ( n , elements [ n ] ) ) ;
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
// b. Set n to n + 1.
}
2022-05-01 17:26:08 +00:00
// 4. Return array.
2021-04-06 19:39:17 +00:00
return array ;
}
2020-04-17 16:24:01 +00:00
Array : : Array ( Object & prototype )
2022-12-14 11:17:58 +00:00
: Object ( ConstructWithPrototypeTag : : Tag , prototype )
2021-02-24 08:48:29 +00:00
{
2023-11-12 14:04:23 +00:00
m_has_magical_length_property = true ;
2021-02-24 08:48:29 +00:00
}
2021-07-07 00:50:19 +00:00
// 10.4.2.4 ArraySetLength ( A, Desc ), https://tc39.es/ecma262/#sec-arraysetlength
2021-10-21 22:34:06 +00:00
ThrowCompletionOr < 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 & vm = this - > vm ( ) ;
2022-04-30 22:47:55 +00:00
// 1. If Desc does not have a [[Value]] field, then
2022-05-02 18:54:39 +00:00
// a. Return ! OrdinaryDefineOwnProperty(A, "length", Desc).
2021-07-07 00:50:19 +00:00
// 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]]).
2022-08-21 13:00:56 +00:00
new_length = TRY ( property_descriptor . value - > to_u32 ( vm ) ) ;
2021-07-07 00:50:19 +00:00
// 4. Let numberLen be ? ToNumber(Desc.[[Value]]).
2022-08-21 13:00:56 +00:00
auto number_length = TRY ( property_descriptor . value - > to_number ( vm ) ) ;
2021-07-07 00:50:19 +00:00
// 5. If newLen is not the same value as numberLen, throw a RangeError exception.
2021-10-21 22:34:06 +00:00
if ( new_length ! = number_length . as_double ( ) )
2022-08-16 19:33:17 +00:00
return vm . throw_completion < RangeError > ( ErrorType : : InvalidLength , " array " ) ;
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").
2022-05-02 18:54:39 +00:00
// 8. Assert: IsDataDescriptor(oldLenDesc) is true.
2021-07-07 00:50:19 +00:00
// 9. Assert: oldLenDesc.[[Configurable]] is false.
// 10. Let oldLen be oldLenDesc.[[Value]].
// 11. If newLen ≥ oldLen, then
2022-05-02 18:54:39 +00:00
// a. Return ! OrdinaryDefineOwnProperty(A, "length", newLenDesc).
2021-07-07 00:50:19 +00:00
// 12. If oldLenDesc.[[Writable]] is false, return false.
// NOTE: Handled by step 16
2022-04-30 22:47:55 +00:00
// 13. If newLenDesc does not have a [[Writable]] field or newLenDesc.[[Writable]] true, let newWritable be true.
2021-07-07 00:50:19 +00:00
// 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
2022-04-30 22:22:51 +00:00
// 5. If current.[[Configurable]] is false, then
2022-04-30 22:47:55 +00:00
// a. If Desc has a [[Configurable]] field and Desc.[[Configurable]] is true, return false.
2021-07-07 00:50:19 +00:00
if ( property_descriptor . configurable . has_value ( ) & & * property_descriptor . configurable )
return false ;
2022-05-02 18:54:39 +00:00
// b. If Desc has an [[Enumerable]] field and SameValue(Desc.[[Enumerable]], current.[[Enumerable]]) is false, return false.
2021-07-07 00:50:19 +00:00
if ( property_descriptor . enumerable . has_value ( ) & & * property_descriptor . enumerable )
return false ;
2022-05-02 18:54:39 +00:00
// c. If IsGenericDescriptor(Desc) is false and SameValue(IsAccessorDescriptor(Desc), IsAccessorDescriptor(current)) is false, return false.
2022-04-30 22:22:51 +00:00
if ( ! property_descriptor . is_generic_descriptor ( ) & & property_descriptor . is_accessor_descriptor ( ) )
2021-07-07 00:50:19 +00:00
return false ;
2022-04-30 22:22:51 +00:00
// NOTE: Step d. doesn't apply here.
// e. Else if current.[[Writable]] is false, then
2021-07-07 00:50:19 +00:00
if ( ! m_length_writable ) {
2022-04-30 22:47:55 +00:00
// i. If Desc has a [[Writable]] field and Desc.[[Writable]] is true, return false.
2021-07-07 00:50:19 +00:00
if ( property_descriptor . writable . has_value ( ) & & * property_descriptor . writable )
return false ;
2022-04-30 22:47:55 +00:00
// ii. If Desc has a [[Value]] field and SameValue(Desc.[[Value]], current.[[Value]]) is false, return false.
2021-07-07 00:50:19 +00:00
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
}
2023-03-20 16:24:48 +00:00
// 23.1.3.30.1 SortIndexedProperties ( obj, len, SortCompare, holes ), https://tc39.es/ecma262/#sec-sortindexedproperties
2024-11-14 15:01:23 +00:00
ThrowCompletionOr < GC : : MarkedVector < Value > > sort_indexed_properties ( VM & vm , Object const & object , size_t length , Function < ThrowCompletionOr < double > ( Value , Value ) > const & sort_compare , Holes holes )
2023-03-20 16:24:48 +00:00
{
// 1. Let items be a new empty List.
2024-11-14 15:01:23 +00:00
auto items = GC : : MarkedVector < Value > { vm . heap ( ) } ;
2023-03-20 16:24:48 +00:00
// 2. Let k be 0.
// 3. Repeat, while k < len,
for ( size_t k = 0 ; k < length ; + + k ) {
// a. Let Pk be ! ToString(𝔽 (k)).
auto property_key = PropertyKey { k } ;
bool k_read ;
// b. If holes is skip-holes, then
if ( holes = = Holes : : SkipHoles ) {
// i. Let kRead be ? HasProperty(obj, Pk).
k_read = TRY ( object . has_property ( property_key ) ) ;
}
// c. Else,
else {
// i. Assert: holes is read-through-holes.
VERIFY ( holes = = Holes : : ReadThroughHoles ) ;
// ii. Let kRead be true.
k_read = true ;
}
// d. If kRead is true, then
if ( k_read ) {
// i. Let kValue be ? Get(obj, Pk).
auto k_value = TRY ( object . get ( property_key ) ) ;
// ii. Append kValue to items.
items . append ( k_value ) ;
}
// e. Set k to k + 1.
}
// 4. Sort items using an implementation-defined sequence of calls to SortCompare. If any such call returns an abrupt completion, stop before performing any further calls to SortCompare or steps in this algorithm and return that Completion Record.
// Perform sorting by merge sort. This isn't as efficient compared to quick sort, but
// quicksort can't be used in all cases because the spec requires Array.prototype.sort()
// to be stable. FIXME: when initially scanning through the array, maintain a flag
// for if an unstable sort would be indistinguishable from a stable sort (such as just
// just strings or numbers), and in that case use quick sort instead for better performance.
TRY ( array_merge_sort ( vm , sort_compare , items ) ) ;
// 5. Return items.
return items ;
}
// 23.1.3.30.2 CompareArrayElements ( x, y, comparefn ), https://tc39.es/ecma262/#sec-comparearrayelements
2022-08-21 16:54:08 +00:00
ThrowCompletionOr < double > compare_array_elements ( VM & vm , Value x , Value y , FunctionObject * comparefn )
2022-06-13 06:53:58 +00:00
{
// 1. If x and y are both undefined, return +0𝔽 .
if ( x . is_undefined ( ) & & y . is_undefined ( ) )
return 0 ;
// 2. If x is undefined, return 1𝔽 .
if ( x . is_undefined ( ) )
return 1 ;
// 3. If y is undefined, return -1𝔽 .
if ( y . is_undefined ( ) )
return - 1 ;
// 4. If comparefn is not undefined, then
if ( comparefn ! = nullptr ) {
// a. Let v be ? ToNumber(? Call(comparefn, undefined, « x, y »)).
2022-08-21 18:24:32 +00:00
auto value = TRY ( call ( vm , comparefn , js_undefined ( ) , x , y ) ) ;
2022-08-21 13:00:56 +00:00
auto value_number = TRY ( value . to_number ( vm ) ) ;
2022-06-13 06:53:58 +00:00
// b. If v is NaN, return +0𝔽 .
if ( value_number . is_nan ( ) )
return 0 ;
// c. Return v.
return value_number . as_double ( ) ;
}
// 5. Let xString be ? ToString(x).
2023-12-16 14:19:34 +00:00
auto x_string = PrimitiveString : : create ( vm , TRY ( x . to_byte_string ( vm ) ) ) ;
2022-06-13 06:53:58 +00:00
// 6. Let yString be ? ToString(y).
2023-12-16 14:19:34 +00:00
auto y_string = PrimitiveString : : create ( vm , TRY ( y . to_byte_string ( vm ) ) ) ;
2022-06-13 06:53:58 +00:00
// 7. Let xSmaller be ! IsLessThan(xString, yString, true).
2022-08-21 13:00:56 +00:00
auto x_smaller = MUST ( is_less_than ( vm , x_string , y_string , true ) ) ;
2022-06-13 06:53:58 +00:00
// 8. If xSmaller is true, return -1𝔽 .
if ( x_smaller = = TriState : : True )
return - 1 ;
// 9. Let ySmaller be ! IsLessThan(yString, xString, true).
2022-08-21 13:00:56 +00:00
auto y_smaller = MUST ( is_less_than ( vm , y_string , x_string , true ) ) ;
2022-06-13 06:53:58 +00:00
// 10. If ySmaller is true, return 1𝔽 .
if ( y_smaller = = TriState : : True )
return 1 ;
// 11. Return +0𝔽 .
return 0 ;
}
2021-07-07 00:50:19 +00:00
// NON-STANDARD: Used to return the value of the ephemeral length property
2022-02-06 15:59:04 +00:00
ThrowCompletionOr < Optional < PropertyDescriptor > > Array : : internal_get_own_property ( PropertyKey const & property_key ) const
2020-04-22 23:33:13 +00:00
{
2021-07-07 00:50:19 +00:00
auto & vm = this - > vm ( ) ;
2022-02-06 15:59:04 +00:00
if ( property_key . is_string ( ) & & property_key . as_string ( ) = = vm . names . length . as_string ( ) )
2021-10-20 18:17:45 +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
2022-02-06 15:59:04 +00:00
return Object : : internal_get_own_property ( property_key ) ;
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
2024-11-01 20:03:18 +00:00
ThrowCompletionOr < bool > Array : : internal_define_own_property ( PropertyKey const & property_key , PropertyDescriptor const & property_descriptor , Optional < PropertyDescriptor > * precomputed_get_own_property )
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
2022-05-01 17:26:08 +00:00
// 1. If P is "length", then
2022-02-06 15:59:04 +00:00
if ( property_key . is_string ( ) & & property_key . as_string ( ) = = vm . names . length . as_string ( ) ) {
2021-07-07 00:50:19 +00:00
// a. Return ? ArraySetLength(A, Desc).
2021-10-21 22:34:06 +00:00
return set_length ( property_descriptor ) ;
2021-07-07 00:50:19 +00:00
}
2021-06-25 14:57:12 +00:00
2022-05-01 17:26:08 +00:00
// 2. Else if P is an array index, then
2022-02-06 15:59:04 +00:00
if ( property_key . is_number ( ) ) {
2021-07-07 00:50:19 +00:00
// a. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
2022-05-02 18:54:39 +00:00
// b. Assert: IsDataDescriptor(oldLenDesc) is true.
2021-07-07 00:50:19 +00:00
// 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.
2022-02-06 15:59:04 +00:00
if ( property_key . as_number ( ) > = indexed_properties ( ) . array_like_size ( ) & & ! m_length_writable )
2021-07-07 00:50:19 +00:00
return false ;
// h. Let succeeded be ! OrdinaryDefineOwnProperty(A, P, Desc).
2024-11-01 20:03:18 +00:00
auto succeeded = MUST ( Object : : internal_define_own_property ( property_key , property_descriptor , precomputed_get_own_property ) ) ;
2021-10-03 14:57:26 +00:00
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𝔽 .
2022-05-02 18:54:39 +00:00
// ii. Set succeeded to ! OrdinaryDefineOwnProperty(A, "length", oldLenDesc).
2021-07-07 00:50:19 +00:00
// 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
2022-05-02 18:54:39 +00:00
// 3. Return ? OrdinaryDefineOwnProperty(A, P, Desc).
2024-11-01 20:03:18 +00:00
return Object : : internal_define_own_property ( property_key , property_descriptor , precomputed_get_own_property ) ;
2021-07-07 00:50:19 +00:00
}
// NON-STANDARD: Used to reject deletes to ephemeral (non-configurable) length property
2022-02-06 15:59:04 +00:00
ThrowCompletionOr < bool > Array : : internal_delete ( PropertyKey const & property_key )
2021-07-07 00:50:19 +00:00
{
auto & vm = this - > vm ( ) ;
2022-02-06 15:59:04 +00:00
if ( property_key . is_string ( ) & & property_key . as_string ( ) = = vm . names . length . as_string ( ) )
2021-07-07 00:50:19 +00:00
return false ;
2022-02-06 15:59:04 +00:00
return Object : : internal_delete ( property_key ) ;
2021-07-07 00:50:19 +00:00
}
// NON-STANDARD: Used to inject the ephemeral length property's key
2024-11-14 15:01:23 +00:00
ThrowCompletionOr < GC : : MarkedVector < Value > > Array : : internal_own_property_keys ( ) const
2021-07-07 00:50:19 +00:00
{
auto & vm = this - > vm ( ) ;
2021-09-29 17:58:03 +00:00
auto keys = TRY ( Object : : internal_own_property_keys ( ) ) ;
2021-07-07 00:50:19 +00:00
// FIXME: This is pretty expensive, find a better way to do this
2022-12-06 22:17:27 +00:00
keys . insert ( indexed_properties ( ) . real_size ( ) , PrimitiveString : : create ( vm , vm . names . length . as_string ( ) ) ) ;
2021-09-29 17:58:03 +00:00
return { move ( keys ) } ;
2020-03-28 23:37:33 +00:00
}
2020-03-20 19:29:57 +00:00
}