2020-03-28 17:23:54 +01:00
/*
2024-10-04 13:19:50 +02:00
* Copyright ( c ) 2020 , Andreas Kling < andreas @ ladybird . org >
2023-04-13 00:47:15 +02:00
* Copyright ( c ) 2020 - 2023 , Linus Groh < linusg @ serenityos . org >
2020-03-28 17:23:54 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-28 17:23:54 +01:00
*/
# include <AK/Function.h>
2021-06-19 21:45:00 +01:00
# include <LibJS/Runtime/AbstractOperations.h>
2020-03-29 01:08:25 +01:00
# include <LibJS/Runtime/Array.h>
2020-04-09 22:15:26 +02:00
# include <LibJS/Runtime/Error.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/GlobalObject.h>
2023-07-19 06:54:48 -04:00
# include <LibJS/Runtime/Iterator.h>
2020-03-28 17:23:54 +01:00
# include <LibJS/Runtime/ObjectConstructor.h>
2021-01-01 17:46:39 +01:00
# include <LibJS/Runtime/ProxyObject.h>
2020-04-02 19:32:21 +02:00
# include <LibJS/Runtime/Shape.h>
2025-09-13 20:50:09 +02:00
# include <LibJS/Runtime/ValueInlines.h>
2020-03-28 17:23:54 +01:00
namespace JS {
2024-11-15 04:01:23 +13:00
GC_DEFINE_ALLOCATOR ( ObjectConstructor ) ;
2023-11-19 09:45:05 +01:00
2022-08-16 00:20:49 +01:00
ObjectConstructor : : ObjectConstructor ( Realm & realm )
2023-04-13 00:47:15 +02:00
: NativeFunction ( realm . vm ( ) . names . Object . as_string ( ) , realm . intrinsics ( ) . function_prototype ( ) )
2020-03-28 17:23:54 +01:00
{
2020-06-20 15:40:48 +02:00
}
2023-08-07 08:41:28 +02:00
void ObjectConstructor : : initialize ( Realm & realm )
2020-06-20 15:40:48 +02:00
{
2020-10-13 23:49:19 +02:00
auto & vm = this - > vm ( ) ;
2023-08-07 08:41:28 +02:00
Base : : initialize ( realm ) ;
2021-06-13 00:22:35 +01:00
2023-12-26 08:55:15 -05:00
// 20.1.2.21 Object.prototype, https://tc39.es/ecma262/#sec-object.prototype
2022-08-27 00:54:55 +01:00
define_direct_property ( vm . names . prototype , realm . intrinsics ( ) . object_prototype ( ) , 0 ) ;
2021-06-13 00:22:35 +01:00
2020-04-27 23:05:02 -07:00
u8 attr = Attribute : : Writable | Attribute : : Configurable ;
2022-08-22 21:47:35 +01:00
define_native_function ( realm , vm . names . defineProperty , define_property , 3 , attr ) ;
define_native_function ( realm , vm . names . defineProperties , define_properties , 2 , attr ) ;
define_native_function ( realm , vm . names . is , is , 2 , attr ) ;
define_native_function ( realm , vm . names . getOwnPropertyDescriptor , get_own_property_descriptor , 2 , attr ) ;
define_native_function ( realm , vm . names . getOwnPropertyDescriptors , get_own_property_descriptors , 1 , attr ) ;
define_native_function ( realm , vm . names . getOwnPropertyNames , get_own_property_names , 1 , attr ) ;
define_native_function ( realm , vm . names . getOwnPropertySymbols , get_own_property_symbols , 1 , attr ) ;
define_native_function ( realm , vm . names . getPrototypeOf , get_prototype_of , 1 , attr ) ;
2023-07-11 16:23:00 +02:00
define_native_function ( realm , vm . names . groupBy , group_by , 2 , attr ) ;
2022-08-22 21:47:35 +01:00
define_native_function ( realm , vm . names . setPrototypeOf , set_prototype_of , 2 , attr ) ;
define_native_function ( realm , vm . names . isExtensible , is_extensible , 1 , attr ) ;
define_native_function ( realm , vm . names . isFrozen , is_frozen , 1 , attr ) ;
define_native_function ( realm , vm . names . isSealed , is_sealed , 1 , attr ) ;
define_native_function ( realm , vm . names . preventExtensions , prevent_extensions , 1 , attr ) ;
define_native_function ( realm , vm . names . freeze , freeze , 1 , attr ) ;
define_native_function ( realm , vm . names . fromEntries , from_entries , 1 , attr ) ;
define_native_function ( realm , vm . names . seal , seal , 1 , attr ) ;
define_native_function ( realm , vm . names . keys , keys , 1 , attr ) ;
define_native_function ( realm , vm . names . values , values , 1 , attr ) ;
define_native_function ( realm , vm . names . entries , entries , 1 , attr ) ;
define_native_function ( realm , vm . names . create , create , 2 , attr ) ;
define_native_function ( realm , vm . names . hasOwn , has_own , 2 , attr ) ;
define_native_function ( realm , vm . names . assign , assign , 2 , attr ) ;
2021-07-08 02:49:53 +03:00
define_direct_property ( vm . names . length , Value ( 1 ) , Attribute : : Configurable ) ;
2020-03-28 17:23:54 +01:00
}
2021-06-13 00:22:35 +01:00
// 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
2021-10-20 21:16:30 +01:00
ThrowCompletionOr < Value > ObjectConstructor : : call ( )
2021-06-20 01:09:39 +01:00
{
2021-10-20 21:16:30 +01:00
return TRY ( construct ( * this ) ) ;
2021-06-20 01:09:39 +01:00
}
// 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
2024-11-15 04:01:23 +13:00
ThrowCompletionOr < GC : : Ref < Object > > ObjectConstructor : : construct ( FunctionObject & new_target )
2020-03-28 17:23:54 +01:00
{
2021-06-16 20:52:30 +01:00
auto & vm = this - > vm ( ) ;
2022-08-22 19:00:49 +01:00
auto & realm = * vm . current_realm ( ) ;
2023-04-14 17:05:27 +02:00
auto value = vm . argument ( 0 ) ;
2021-06-16 20:52:30 +01:00
2023-04-14 17:05:27 +02:00
// 1. If NewTarget is neither undefined nor the active function object, then
if ( & new_target ! = this ) {
// a. Return ? OrdinaryCreateFromConstructor(NewTarget, "%Object.prototype%").
2022-12-14 19:18:10 +00:00
return TRY ( ordinary_create_from_constructor < Object > ( vm , new_target , & Intrinsics : : object_prototype , ConstructWithPrototypeTag : : Tag ) ) ;
2023-04-14 17:05:27 +02:00
}
// 2. If value is either undefined or null, return OrdinaryObjectCreate(%Object.prototype%).
2020-11-04 21:13:07 +00:00
if ( value . is_nullish ( ) )
2022-12-14 19:18:10 +00:00
return Object : : create ( realm , realm . intrinsics ( ) . object_prototype ( ) ) ;
2023-04-14 17:05:27 +02:00
// 3. Return ! ToObject(value).
return MUST ( value . to_object ( vm ) ) ;
2020-03-28 17:23:54 +01: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 18:14:16 +01:00
enum class GetOwnPropertyKeysType {
String ,
Symbol ,
} ;
// 20.1.2.11.1 GetOwnPropertyKeys ( O, type ), https://tc39.es/ecma262/#sec-getownpropertykeys
2024-12-26 14:32:52 +01:00
static ThrowCompletionOr < GC : : RootVector < Value > > get_own_property_keys ( VM & vm , Value value , GetOwnPropertyKeysType type )
2020-03-29 01:08:25 +01: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 18:14:16 +01:00
// 1. Let obj be ? ToObject(O).
2023-04-13 15:26:41 +02:00
auto object = TRY ( value . to_object ( vm ) ) ;
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 18:14:16 +01:00
// 2. Let keys be ? obj.[[OwnPropertyKeys]]().
2021-10-29 17:05:50 -04:00
auto keys = TRY ( object - > internal_own_property_keys ( ) ) ;
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 18:14:16 +01:00
// 3. Let nameList be a new empty List.
2024-12-26 14:32:52 +01:00
auto name_list = GC : : RootVector < Value > { vm . heap ( ) } ;
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 18:14:16 +01:00
// 4. For each element nextKey of keys, do
2025-05-16 04:14:12 +01:00
for ( auto & next_key : keys ) {
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 18:14:16 +01:00
// a. If Type(nextKey) is Symbol and type is symbol or Type(nextKey) is String and type is string, then
2025-05-16 04:14:12 +01:00
if ( ( next_key . is_symbol ( ) & & type = = GetOwnPropertyKeysType : : Symbol ) | | ( next_key . is_string ( ) & & type = = GetOwnPropertyKeysType : : String ) ) {
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 18:14:16 +01:00
// i. Append nextKey as the last element of nameList.
2025-05-16 04:14:12 +01:00
name_list . append ( next_key ) ;
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 18:14:16 +01:00
}
}
2022-05-02 20:54:39 +02:00
// 5. Return nameList.
return { move ( name_list ) } ;
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 18:14:16 +01:00
}
2023-04-14 17:05:27 +02:00
// 20.1.2.1 Object.assign ( target, ...sources ), https://tc39.es/ecma262/#sec-object.assign
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : assign )
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 18:14:16 +01:00
{
2023-04-14 17:05:27 +02:00
// 1. Let to be ? ToObject(target).
auto to = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2022-08-16 00:20:49 +01:00
2023-04-14 17:05:27 +02:00
// 2. If only one argument was passed, return to.
if ( vm . argument_count ( ) = = 1 )
return to ;
2020-03-29 01:08:25 +01:00
2023-04-14 17:05:27 +02:00
// 3. For each element nextSource of sources, do
for ( size_t i = 1 ; i < vm . argument_count ( ) ; + + i ) {
auto next_source = vm . argument ( i ) ;
2022-08-16 00:20:49 +01:00
2023-04-14 17:05:27 +02:00
// a. If nextSource is neither undefined nor null, then
if ( next_source . is_nullish ( ) )
continue ;
2021-06-12 17:41:13 +03:00
2023-04-14 17:05:27 +02:00
// i. Let from be ! ToObject(nextSource).
auto from = MUST ( next_source . to_object ( vm ) ) ;
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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
// ii. Let keys be ? from.[[OwnPropertyKeys]]().
auto keys = TRY ( from - > internal_own_property_keys ( ) ) ;
// iii. For each element nextKey of keys, do
2025-05-16 04:14:12 +01:00
for ( auto & next_key : keys ) {
auto property_key = MUST ( PropertyKey : : from_value ( vm , next_key ) ) ;
2023-04-14 17:05:27 +02:00
// 1. Let desc be ? from.[[GetOwnProperty]](nextKey).
2025-05-16 04:14:12 +01:00
auto desc = TRY ( from - > internal_get_own_property ( property_key ) ) ;
2023-04-14 17:05:27 +02:00
// 2. If desc is not undefined and desc.[[Enumerable]] is true, then
if ( ! desc . has_value ( ) | | ! * desc - > enumerable )
continue ;
// a. Let propValue be ? Get(from, nextKey).
2025-05-16 04:14:12 +01:00
auto prop_value = TRY ( from - > get ( property_key ) ) ;
2023-04-14 17:05:27 +02:00
// b. Perform ? Set(to, nextKey, propValue, true).
2025-05-16 04:14:12 +01:00
TRY ( to - > set ( property_key , prop_value , Object : : ShouldThrowExceptions : : Yes ) ) ;
2023-04-14 17:05:27 +02:00
}
}
// 4. Return to.
return to ;
2020-03-28 22:48:35 +01:00
}
2023-04-14 17:05:27 +02:00
// 20.1.2.2 Object.create ( O, Properties ), https://tc39.es/ecma262/#sec-object.create
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : create )
2020-03-28 22:48:35 +01:00
{
2023-04-14 17:05:27 +02:00
auto & realm = * vm . current_realm ( ) ;
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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
auto proto = vm . argument ( 0 ) ;
auto properties = vm . argument ( 1 ) ;
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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
// 1. If Type(O) is neither Object nor Null, throw a TypeError exception.
2021-10-29 17:08:28 -04:00
if ( ! proto . is_object ( ) & & ! proto . is_null ( ) )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : ObjectPrototypeWrongType ) ;
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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
// 2. Let obj be OrdinaryObjectCreate(O).
auto object = Object : : create ( realm , proto . is_null ( ) ? nullptr : & proto . as_object ( ) ) ;
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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
// 3. If Properties is not undefined, then
if ( ! properties . is_undefined ( ) ) {
// a. Return ? ObjectDefineProperties(obj, Properties).
return TRY ( object - > define_properties ( properties ) ) ;
2020-06-02 12:32:54 +01: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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
// 4. Return obj.
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 18:14:16 +01:00
return object ;
2020-03-28 22:48:35 +01:00
}
2023-04-14 17:05:27 +02:00
// 20.1.2.3 Object.defineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-object.defineproperties
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : define_properties )
2020-06-01 21:13:16 -07:00
{
2023-04-14 17:05:27 +02:00
auto object = vm . argument ( 0 ) ;
auto properties = vm . argument ( 1 ) ;
2020-06-01 21:13:16 -07:00
2023-04-14 17:05:27 +02:00
// 1. If Type(O) is not Object, throw a TypeError exception.
if ( ! object . is_object ( ) )
return vm . throw_completion < TypeError > ( ErrorType : : NotAnObject , " Object argument " ) ;
// 2. Return ? ObjectDefineProperties(O, Properties).
return TRY ( object . as_object ( ) . define_properties ( properties ) ) ;
2021-04-06 22:45:12 +02:00
}
2023-04-14 17:05:27 +02:00
// 20.1.2.4 Object.defineProperty ( O, P, Attributes ), https://tc39.es/ecma262/#sec-object.defineproperty
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : define_property )
2021-04-06 22:45:12 +02:00
{
2023-04-14 17:05:27 +02:00
// 1. If O is not an Object, throw a TypeError exception.
if ( ! vm . argument ( 0 ) . is_object ( ) )
2023-08-09 08:49:02 +02:00
return vm . throw_completion < TypeError > ( ErrorType : : NotAnObject , vm . argument ( 0 ) . to_string_without_side_effects ( ) ) ;
2023-04-14 17:05:27 +02:00
auto object = MUST ( vm . argument ( 0 ) . to_object ( vm ) ) ;
// 2. Let key be ? ToPropertyKey(P).
auto key = TRY ( vm . argument ( 1 ) . to_property_key ( vm ) ) ;
// 3. Let desc be ? ToPropertyDescriptor(Attributes).
auto descriptor = TRY ( to_property_descriptor ( vm , vm . argument ( 2 ) ) ) ;
// 4. Perform ? DefinePropertyOrThrow(O, key, desc).
TRY ( object - > define_property_or_throw ( key , descriptor ) ) ;
// 5. Return O.
return object ;
2021-04-06 22:45:12 +02:00
}
2023-04-14 17:05:27 +02:00
// 20.1.2.5 Object.entries ( O ), https://tc39.es/ecma262/#sec-object.entries
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : entries )
2020-06-01 21:13:16 -07:00
{
2023-04-14 17:05:27 +02:00
auto & realm = * vm . current_realm ( ) ;
// 1. Let obj be ? ToObject(O).
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
// 2. Let entryList be ? EnumerableOwnProperties(obj, key+value).
auto name_list = TRY ( object - > enumerable_own_property_names ( PropertyKind : : KeyAndValue ) ) ;
// 3. Return CreateArrayFromList(entryList).
return Array : : create_from ( realm , name_list ) ;
2021-04-06 22:06:11 +02:00
}
2021-06-13 00:22:35 +01:00
// 20.1.2.6 Object.freeze ( O ), https://tc39.es/ecma262/#sec-object.freeze
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : freeze )
2021-04-06 22:06:11 +02:00
{
auto argument = vm . argument ( 0 ) ;
2023-04-14 17:05:27 +02:00
// 1. If O is not an Object, return O.
2021-04-06 22:06:11 +02:00
if ( ! argument . is_object ( ) )
return argument ;
2023-04-14 17:05:27 +02:00
// 2. Let status be ? SetIntegrityLevel(O, frozen).
2021-10-29 17:08:28 -04:00
auto status = TRY ( argument . as_object ( ) . set_integrity_level ( Object : : IntegrityLevel : : Frozen ) ) ;
2023-04-14 17:05:27 +02:00
// 3. If status is false, throw a TypeError exception.
2021-10-29 17:08:28 -04:00
if ( ! status )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : ObjectFreezeFailed ) ;
2023-04-14 17:05:27 +02:00
// 4. Return O.
2021-04-06 22:06:11 +02:00
return argument ;
}
2021-06-15 12:46:14 +03:00
// 20.1.2.7 Object.fromEntries ( iterable ), https://tc39.es/ecma262/#sec-object.fromentries
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : from_entries )
2021-06-15 12:46:14 +03:00
{
2022-08-22 11:48:08 +01:00
auto & realm = * vm . current_realm ( ) ;
2023-04-14 17:05:27 +02:00
// 1. Perform ? RequireObjectCoercible(iterable).
2022-08-21 19:24:32 +01:00
auto iterable = TRY ( require_object_coercible ( vm , vm . argument ( 0 ) ) ) ;
2021-06-15 12:46:14 +03:00
2023-04-14 17:05:27 +02:00
// 2. Let obj be OrdinaryObjectCreate(%Object.prototype%).
2022-12-13 20:49:50 +00:00
auto object = Object : : create ( realm , realm . intrinsics ( ) . object_prototype ( ) ) ;
2021-06-15 12:46:14 +03:00
2023-04-14 17:05:27 +02:00
// 3. Assert: obj is an extensible ordinary object with no own properties.
// 4. Let closure be a new Abstract Closure with parameters (key, value) that captures obj and performs the following steps when called:
// 5. Let adder be CreateBuiltinFunction(closure, 2, "", « »).
// 6. Return ? AddEntriesFromIterable(obj, iterable, adder).
2022-08-21 15:56:27 +01:00
( void ) TRY ( get_iterator_values ( vm , iterable , [ & ] ( Value iterator_value ) - > Optional < Completion > {
2021-10-20 12:10:23 -04:00
if ( ! iterator_value . is_object ( ) )
2023-12-16 17:49:34 +03:30
return vm . throw_completion < TypeError > ( ErrorType : : NotAnObject , ByteString : : formatted ( " Iterator value {} " , iterator_value . to_string_without_side_effects ( ) ) ) ;
2021-10-20 12:10:23 -04:00
auto key = TRY ( iterator_value . as_object ( ) . get ( 0 ) ) ;
auto value = TRY ( iterator_value . as_object ( ) . get ( 1 ) ) ;
2023-04-14 17:05:27 +02:00
// a. Let propertyKey be ? ToPropertyKey(key).
2022-08-21 14:00:56 +01:00
auto property_key = TRY ( key . to_property_key ( vm ) ) ;
2023-04-14 17:05:27 +02:00
// b. Perform ! CreateDataPropertyOrThrow(obj, propertyKey, value).
2021-10-20 12:10:23 -04:00
MUST ( object - > create_data_property_or_throw ( property_key , value ) ) ;
2023-04-14 17:05:27 +02:00
// c. Return undefined.
2021-06-15 12:46:14 +03:00
return { } ;
2021-10-20 12:10:23 -04:00
} ) ) ;
2021-06-15 12:46:14 +03:00
return object ;
}
2021-06-13 00:22:35 +01:00
// 20.1.2.8 Object.getOwnPropertyDescriptor ( O, P ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptor
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : get_own_property_descriptor )
2020-04-09 22:15:26 +02:00
{
2023-04-14 17:05:27 +02:00
// 1. Let obj be ? ToObject(O).
2023-04-13 15:26:41 +02:00
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2023-04-14 17:05:27 +02:00
// 2. Let key be ? ToPropertyKey(P).
2022-08-21 14:00:56 +01:00
auto key = TRY ( vm . argument ( 1 ) . to_property_key ( vm ) ) ;
2023-04-14 17:05:27 +02:00
// 3. Let desc be ? obj.[[GetOwnProperty]](key).
2021-10-29 17:08:28 -04:00
auto descriptor = TRY ( object - > internal_get_own_property ( key ) ) ;
2023-04-14 17:05:27 +02:00
// 4. Return FromPropertyDescriptor(desc).
2022-08-21 20:38:35 +01:00
return from_property_descriptor ( vm , descriptor ) ;
2020-04-09 22:15:26 +02:00
}
2021-07-06 19:41:54 +03:00
// 20.1.2.9 Object.getOwnPropertyDescriptors ( O ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptors
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : get_own_property_descriptors )
2021-07-06 19:41:54 +03:00
{
2022-08-22 11:48:08 +01:00
auto & realm = * vm . current_realm ( ) ;
2022-08-16 00:20:49 +01:00
2021-07-06 19:41:54 +03:00
// 1. Let obj be ? ToObject(O).
2023-04-13 15:26:41 +02:00
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2021-09-29 18:58:03 +01:00
2021-07-06 19:41:54 +03:00
// 2. Let ownKeys be ? obj.[[OwnPropertyKeys]]().
2021-10-29 17:08:28 -04:00
auto own_keys = TRY ( object - > internal_own_property_keys ( ) ) ;
2021-09-29 18:58:03 +01:00
2022-05-02 20:54:39 +02:00
// 3. Let descriptors be OrdinaryObjectCreate(%Object.prototype%).
2022-12-13 20:49:50 +00:00
auto descriptors = Object : : create ( realm , realm . intrinsics ( ) . object_prototype ( ) ) ;
2021-09-29 18:58:03 +01:00
2021-07-06 19:41:54 +03:00
// 4. For each element key of ownKeys, do
2025-05-16 04:14:12 +01:00
for ( auto & key : own_keys ) {
auto property_key = MUST ( PropertyKey : : from_value ( vm , key ) ) ;
2021-07-06 19:41:54 +03:00
// a. Let desc be ? obj.[[GetOwnProperty]](key).
2022-02-06 15:59:04 +00:00
auto desc = TRY ( object - > internal_get_own_property ( property_key ) ) ;
2021-07-06 19:41:54 +03:00
2022-05-02 20:54:39 +02:00
// b. Let descriptor be FromPropertyDescriptor(desc).
2022-08-21 20:38:35 +01:00
auto descriptor = from_property_descriptor ( vm , desc ) ;
2021-07-06 19:41:54 +03:00
// c. If descriptor is not undefined, perform ! CreateDataPropertyOrThrow(descriptors, key, descriptor).
if ( ! descriptor . is_undefined ( ) )
2022-02-06 15:59:04 +00:00
MUST ( descriptors - > create_data_property_or_throw ( property_key , descriptor ) ) ;
2021-07-06 19:41:54 +03:00
}
// 5. Return descriptors.
return descriptors ;
}
2023-04-14 17:05:27 +02:00
// 20.1.2.10 Object.getOwnPropertyNames ( O ), https://tc39.es/ecma262/#sec-object.getownpropertynames
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : get_own_property_names )
2020-04-09 22:15:26 +02:00
{
2023-04-14 17:05:27 +02:00
auto & realm = * vm . current_realm ( ) ;
// 1. Return CreateArrayFromList(? GetOwnPropertyKeys(O, string)).
return Array : : create_from ( realm , TRY ( get_own_property_keys ( vm , vm . argument ( 0 ) , GetOwnPropertyKeysType : : String ) ) ) ;
2020-04-09 22:15:26 +02:00
}
2023-04-14 17:05:27 +02:00
// 20.1.2.11 Object.getOwnPropertySymbols ( O ), https://tc39.es/ecma262/#sec-object.getownpropertysymbols
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : get_own_property_symbols )
2021-04-10 18:39:11 +02:00
{
2023-04-14 17:05:27 +02:00
auto & realm = * vm . current_realm ( ) ;
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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
// 1. Return CreateArrayFromList(? GetOwnPropertyKeys(O, symbol)).
return Array : : create_from ( realm , TRY ( get_own_property_keys ( vm , vm . argument ( 0 ) , GetOwnPropertyKeysType : : Symbol ) ) ) ;
}
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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
// 20.1.2.12 Object.getPrototypeOf ( O ), https://tc39.es/ecma262/#sec-object.getprototypeof
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : get_prototype_of )
{
// 1. Let obj be ? ToObject(O).
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
// 2. Return ? obj.[[GetPrototypeOf]]().
return TRY ( object - > internal_get_prototype_of ( ) ) ;
}
2023-12-26 08:55:15 -05:00
// 20.1.2.13 Object.groupBy ( items, callbackfn ), https://tc39.es/ecma262/#sec-object.groupby
2023-07-11 16:23:00 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : group_by )
{
auto & realm = * vm . current_realm ( ) ;
auto items = vm . argument ( 0 ) ;
auto callback_function = vm . argument ( 1 ) ;
// 1. Let groups be ? GroupBy(items, callbackfn, property).
2024-12-26 14:32:52 +01:00
auto groups = TRY ( ( JS : : group_by < OrderedHashMap < PropertyKey , GC : : RootVector < Value > > , PropertyKey > ( vm , items , callback_function ) ) ) ;
2023-07-11 16:23:00 +02:00
// 2. Let obj be OrdinaryObjectCreate(null).
auto object = Object : : create ( realm , nullptr ) ;
// 3. For each Record { [[Key]], [[Elements]] } g of groups, do
for ( auto & group : groups ) {
// a. Let elements be CreateArrayFromList(g.[[Elements]]).
auto elements = Array : : create_from ( realm , group . value ) ;
// b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
MUST ( object - > create_data_property_or_throw ( group . key , elements ) ) ;
}
// 4. Return obj.
return object ;
}
2023-12-26 08:55:15 -05:00
// 20.1.2.14 Object.hasOwn ( O, P ), https://tc39.es/ecma262/#sec-object.hasown
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : has_own )
{
// 1. Let obj be ? ToObject(O).
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
// 2. Let key be ? ToPropertyKey(P).
auto key = TRY ( vm . argument ( 1 ) . to_property_key ( vm ) ) ;
// 3. Return ? HasOwnProperty(obj, key).
return Value ( TRY ( object - > has_own_property ( key ) ) ) ;
2021-04-10 18:39:11 +02:00
}
2023-12-26 08:55:15 -05:00
// 20.1.2.15 Object.is ( value1, value2 ), https://tc39.es/ecma262/#sec-object.is
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : is )
2020-04-26 00:27:54 +01:00
{
2023-04-14 17:05:27 +02:00
// 1. Return SameValue(value1, value2).
2020-09-27 18:36:49 +02:00
return Value ( same_value ( vm . argument ( 0 ) , vm . argument ( 1 ) ) ) ;
2020-04-26 00:27:54 +01:00
}
2023-12-26 08:55:15 -05:00
// 20.1.2.16 Object.isExtensible ( O ), https://tc39.es/ecma262/#sec-object.isextensible
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : is_extensible )
2020-04-29 18:59:23 -07:00
{
2023-04-14 17:05:27 +02:00
auto argument = vm . argument ( 0 ) ;
2022-08-16 00:20:49 +01:00
2023-04-14 17:05:27 +02:00
// 1. If O is not an Object, return false.
if ( ! argument . is_object ( ) )
return Value ( false ) ;
// 2. Return ? IsExtensible(O).
return Value ( TRY ( argument . as_object ( ) . is_extensible ( ) ) ) ;
2020-04-29 18:59:23 -07:00
}
2023-12-26 08:55:15 -05:00
// 20.1.2.17 Object.isFrozen ( O ), https://tc39.es/ecma262/#sec-object.isfrozen
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : is_frozen )
2020-04-29 18:59:23 -07:00
{
2023-04-14 17:05:27 +02:00
auto argument = vm . argument ( 0 ) ;
2022-08-16 00:20:49 +01:00
2023-04-14 17:05:27 +02:00
// 1. If O is not an Object, return true.
if ( ! argument . is_object ( ) )
return Value ( true ) ;
// 2. Return ? TestIntegrityLevel(O, frozen).
return Value ( TRY ( argument . as_object ( ) . test_integrity_level ( Object : : IntegrityLevel : : Frozen ) ) ) ;
2020-04-29 18:59:23 -07:00
}
2023-12-26 08:55:15 -05:00
// 20.1.2.18 Object.isSealed ( O ), https://tc39.es/ecma262/#sec-object.issealed
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : is_sealed )
{
auto argument = vm . argument ( 0 ) ;
// 1. If O is not an Object, return true.
if ( ! argument . is_object ( ) )
return Value ( true ) ;
// 2. Return ? TestIntegrityLevel(O, sealed).
return Value ( TRY ( argument . as_object ( ) . test_integrity_level ( Object : : IntegrityLevel : : Sealed ) ) ) ;
}
2023-12-26 08:55:15 -05:00
// 20.1.2.19 Object.keys ( O ), https://tc39.es/ecma262/#sec-object.keys
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : keys )
2020-04-29 18:59:23 -07:00
{
2022-08-22 11:48:08 +01:00
auto & realm = * vm . current_realm ( ) ;
2022-08-16 00:20:49 +01:00
2023-04-14 17:05:27 +02:00
// 1. Let obj be ? ToObject(O).
2023-04-13 15:26:41 +02:00
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2023-04-14 17:05:27 +02:00
// 2. Let keyList be ? EnumerableOwnProperties(obj, key).
auto name_list = TRY ( object - > enumerable_own_property_names ( PropertyKind : : Key ) ) ;
// 3. Return CreateArrayFromList(keyList).
2022-08-16 00:20:49 +01:00
return Array : : create_from ( realm , name_list ) ;
2020-04-29 18:59:23 -07:00
}
2023-12-26 08:55:15 -05:00
// 20.1.2.20 Object.preventExtensions ( O ), https://tc39.es/ecma262/#sec-object.preventextensions
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : prevent_extensions )
2021-04-10 18:40:12 +02:00
{
2023-04-14 17:05:27 +02:00
auto argument = vm . argument ( 0 ) ;
2021-04-10 18:40:12 +02:00
2023-04-14 17:05:27 +02:00
// 1. If O is not an Object, return O.
if ( ! argument . is_object ( ) )
return argument ;
2021-04-10 18:40:12 +02:00
2023-04-14 17:05:27 +02:00
// 2. Let status be ? O.[[PreventExtensions]]().
auto status = TRY ( argument . as_object ( ) . internal_prevent_extensions ( ) ) ;
2021-04-10 18:40:12 +02:00
2023-04-14 17:05:27 +02:00
// 3. If status is false, throw a TypeError exception.
if ( ! status ) {
// FIXME: Improve/contextualize error message
return vm . throw_completion < TypeError > ( ErrorType : : ObjectPreventExtensionsReturnedFalse ) ;
2021-04-10 18:40:12 +02: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 18:14:16 +01:00
2023-04-14 17:05:27 +02:00
// 4. Return O.
return argument ;
2021-04-10 18:40:12 +02:00
}
2023-12-26 08:55:15 -05:00
// 20.1.2.22 Object.seal ( O ), https://tc39.es/ecma262/#sec-object.seal
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : seal )
2021-05-18 11:15:49 +02:00
{
2023-04-14 17:05:27 +02:00
auto argument = vm . argument ( 0 ) ;
2021-07-05 17:19:46 +01:00
2023-04-14 17:05:27 +02:00
// 1. If O is not an Object, return O.
if ( ! argument . is_object ( ) )
return argument ;
2021-07-05 17:19:46 +01:00
2023-04-14 17:05:27 +02:00
// 2. Let status be ? SetIntegrityLevel(O, sealed).
auto status = TRY ( argument . as_object ( ) . set_integrity_level ( Object : : IntegrityLevel : : Sealed ) ) ;
// 3. If status is false, throw a TypeError exception.
if ( ! status )
return vm . throw_completion < TypeError > ( ErrorType : : ObjectSealFailed ) ;
// 4. Return O.
return argument ;
2021-05-18 11:15:49 +02:00
}
2023-12-26 08:55:15 -05:00
// 20.1.2.23 Object.setPrototypeOf ( O, proto ), https://tc39.es/ecma262/#sec-object.setprototypeof
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : set_prototype_of )
2021-06-12 11:35:53 +01:00
{
2023-04-14 17:05:27 +02:00
auto proto = vm . argument ( 1 ) ;
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 1. Set O to ? RequireObjectCoercible(O).
auto object = TRY ( require_object_coercible ( vm , vm . argument ( 0 ) ) ) ;
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 2. If Type(proto) is neither Object nor Null, throw a TypeError exception.
if ( ! proto . is_object ( ) & & ! proto . is_null ( ) )
return vm . throw_completion < TypeError > ( ErrorType : : ObjectPrototypeWrongType ) ;
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 3. If Type(O) is not Object, return O.
if ( ! object . is_object ( ) )
return object ;
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 4. Let status be ? O.[[SetPrototypeOf]](proto).
auto status = TRY ( object . as_object ( ) . internal_set_prototype_of ( proto . is_null ( ) ? nullptr : & proto . as_object ( ) ) ) ;
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 5. If status is false, throw a TypeError exception.
if ( ! status ) {
// FIXME: Improve/contextualize error message
return vm . throw_completion < TypeError > ( ErrorType : : ObjectSetPrototypeOfReturnedFalse ) ;
}
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 6. Return O.
return object ;
}
2021-07-05 17:20:12 +01:00
2023-12-26 08:55:15 -05:00
// 20.1.2.24 Object.values ( O ), https://tc39.es/ecma262/#sec-object.values
2023-04-14 17:05:27 +02:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : values )
{
auto & realm = * vm . current_realm ( ) ;
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 1. Let obj be ? ToObject(O).
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 2. Let valueList be ? EnumerableOwnProperties(obj, value).
auto name_list = TRY ( object - > enumerable_own_property_names ( PropertyKind : : Value ) ) ;
2021-07-05 17:20:12 +01:00
2023-04-14 17:05:27 +02:00
// 3. Return CreateArrayFromList(valueList).
return Array : : create_from ( realm , name_list ) ;
2021-06-12 11:35:53 +01:00
}
2020-03-28 17:23:54 +01:00
}