2020-03-28 17:23:54 +01:00
/*
* Copyright ( c ) 2020 , Andreas Kling < kling @ serenityos . org >
2022-05-02 20:54:39 +02:00
* Copyright ( c ) 2020 - 2022 , 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>
2021-06-15 12:46:14 +03:00
# include <LibJS/Runtime/IteratorOperations.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>
2020-03-28 17:23:54 +01:00
namespace JS {
2022-08-16 00:20:49 +01:00
ObjectConstructor : : ObjectConstructor ( Realm & realm )
2022-09-14 19:10:27 -04: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
}
2022-08-16 00:20:49 +01: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 ( ) ;
2022-08-16 00:20:49 +01:00
NativeFunction : : initialize ( realm ) ;
2021-06-13 00:22:35 +01:00
// 20.1.2.19 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 ) ;
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
2021-10-20 21:16:30 +01:00
ThrowCompletionOr < 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 ( ) ;
2021-06-16 20:52:30 +01:00
2021-06-20 01:09:39 +01:00
if ( & new_target ! = this )
2022-12-14 18:34:32 +00:00
return TRY ( ordinary_create_from_constructor < Object > ( vm , new_target , & Intrinsics : : object_prototype , ConstructWithPrototypeTag : : Tag ) ) . ptr ( ) ;
2021-06-16 20:52:30 +01:00
auto value = vm . argument ( 0 ) ;
2020-11-04 21:13:07 +00:00
if ( value . is_nullish ( ) )
2022-12-13 20:49:50 +00:00
return Object : : create ( realm , realm . intrinsics ( ) . object_prototype ( ) ) . ptr ( ) ;
2022-08-21 14:00:56 +01:00
return 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
2022-08-21 20:38:35 +01:00
static ThrowCompletionOr < MarkedVector < 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).
2022-08-21 14:00:56 +01: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.
2022-02-09 10:06:40 +00:00
auto name_list = MarkedVector < 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
for ( auto & next_key : keys ) {
// a. If Type(nextKey) is Symbol and type is symbol or Type(nextKey) is String and type is string, then
if ( ( next_key . is_symbol ( ) & & type = = GetOwnPropertyKeysType : : Symbol ) | | ( next_key . is_string ( ) & & type = = GetOwnPropertyKeysType : : String ) ) {
// i. Append nextKey as the last element of nameList.
name_list . append ( next_key ) ;
}
}
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
}
// 20.1.2.10 Object.getOwnPropertyNames ( O ), https://tc39.es/ecma262/#sec-object.getownpropertynames
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : get_own_property_names )
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-08-22 11:48:08 +01:00
auto & realm = * vm . current_realm ( ) ;
2022-08-16 00:20:49 +01:00
2022-05-02 20:54:39 +02:00
// 1. Return CreateArrayFromList(? GetOwnPropertyKeys(O, string)).
2022-08-21 20:38:35 +01:00
return Array : : create_from ( realm , TRY ( get_own_property_keys ( vm , vm . argument ( 0 ) , GetOwnPropertyKeysType : : String ) ) ) ;
2020-03-29 01:08:25 +01:00
}
2021-06-13 00:22:35 +01:00
// 20.1.2.11 Object.getOwnPropertySymbols ( O ), https://tc39.es/ecma262/#sec-object.getownpropertysymbols
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : get_own_property_symbols )
2021-06-12 17:41:13 +03:00
{
2022-08-22 11:48:08 +01:00
auto & realm = * vm . current_realm ( ) ;
2022-08-16 00:20:49 +01:00
2022-05-02 20:54:39 +02:00
// 1. Return CreateArrayFromList(? GetOwnPropertyKeys(O, symbol)).
2022-08-21 20:38:35 +01:00
return Array : : create_from ( realm , TRY ( get_own_property_keys ( vm , vm . argument ( 0 ) , GetOwnPropertyKeysType : : Symbol ) ) ) ;
2021-06-12 17:41:13 +03:00
}
2021-06-13 00:22:35 +01:00
// 20.1.2.12 Object.getPrototypeOf ( O ), https://tc39.es/ecma262/#sec-object.getprototypeof
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : get_prototype_of )
2020-03-28 22:48:35 +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).
2022-08-21 14:00:56 +01:00
auto * object = TRY ( vm . argument ( 0 ) . 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. Return ? obj.[[GetPrototypeOf]]().
2021-10-29 17:08:28 -04:00
return TRY ( object - > internal_get_prototype_of ( ) ) ;
2020-03-28 22:48:35 +01:00
}
2021-10-23 02:14:21 +01:00
// 20.1.2.22 Object.setPrototypeOf ( O, proto ), https://tc39.es/ecma262/#sec-object.setprototypeof
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : set_prototype_of )
2020-03-28 22:48:35 +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
auto proto = vm . argument ( 1 ) ;
// 1. Set O to ? RequireObjectCoercible(O).
2022-08-21 19:24:32 +01:00
auto object = TRY ( require_object_coercible ( vm , vm . argument ( 0 ) ) ) ;
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
// 2. If Type(proto) 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
// 3. If Type(O) is not Object, return O.
if ( ! object . is_object ( ) )
return object ;
// 4. Let status be ? O.[[SetPrototypeOf]](proto).
2021-10-29 17:08:28 -04:00
auto status = TRY ( object . as_object ( ) . internal_set_prototype_of ( 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
// 5. If status is false, throw a TypeError exception.
2021-06-22 18:59:24 +01:00
if ( ! status ) {
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
// FIXME: Improve/contextualize error message
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : ObjectSetPrototypeOfReturnedFalse ) ;
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
// 6. Return O.
return object ;
2020-03-28 22:48:35 +01:00
}
2021-10-23 02:14:21 +01:00
// 20.1.2.15 Object.isExtensible ( O ), https://tc39.es/ecma262/#sec-object.isextensible
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : is_extensible )
2020-06-01 21:13:16 -07:00
{
2020-09-27 18:36:49 +02:00
auto argument = vm . argument ( 0 ) ;
2020-06-01 21:13:16 -07:00
if ( ! argument . is_object ( ) )
return Value ( false ) ;
2021-10-29 17:08:28 -04:00
return Value ( TRY ( argument . as_object ( ) . is_extensible ( ) ) ) ;
2020-06-01 21:13:16 -07:00
}
2021-10-23 02:14:21 +01:00
// 20.1.2.16 Object.isFrozen ( O ), https://tc39.es/ecma262/#sec-object.isfrozen
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : is_frozen )
2021-04-06 22:45:12 +02:00
{
auto argument = vm . argument ( 0 ) ;
if ( ! argument . is_object ( ) )
return Value ( true ) ;
2021-10-29 17:08:28 -04:00
return Value ( TRY ( argument . as_object ( ) . test_integrity_level ( Object : : IntegrityLevel : : Frozen ) ) ) ;
2021-04-06 22:45:12 +02:00
}
2021-10-23 02:14:21 +01:00
// 20.1.2.17 Object.isSealed ( O ), https://tc39.es/ecma262/#sec-object.issealed
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : is_sealed )
2021-04-06 22:45:12 +02:00
{
auto argument = vm . argument ( 0 ) ;
if ( ! argument . is_object ( ) )
return Value ( true ) ;
2021-10-29 17:08:28 -04:00
return Value ( TRY ( argument . as_object ( ) . test_integrity_level ( Object : : IntegrityLevel : : Sealed ) ) ) ;
2021-04-06 22:45:12 +02:00
}
2021-10-23 02:14:21 +01:00
// 20.1.2.19 Object.preventExtensions ( O ), https://tc39.es/ecma262/#sec-object.preventextensions
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : prevent_extensions )
2020-06-01 21:13:16 -07:00
{
2020-09-27 18:36:49 +02:00
auto argument = vm . argument ( 0 ) ;
2020-06-01 21:13:16 -07:00
if ( ! argument . is_object ( ) )
return argument ;
2021-10-29 17:08:28 -04:00
auto status = TRY ( argument . as_object ( ) . internal_prevent_extensions ( ) ) ;
2021-04-06 22:06:11 +02:00
if ( ! status ) {
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
// FIXME: Improve/contextualize error message
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : ObjectPreventExtensionsReturnedFalse ) ;
2021-04-06 22:06:11 +02:00
}
return argument ;
}
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 ) ;
if ( ! argument . is_object ( ) )
return argument ;
2021-10-29 17:08:28 -04:00
auto status = TRY ( argument . as_object ( ) . set_integrity_level ( Object : : IntegrityLevel : : Frozen ) ) ;
if ( ! status )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : ObjectFreezeFailed ) ;
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 ( ) ;
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
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
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 ( ) )
2022-12-04 18:02:33 +00:00
return vm . throw_completion < TypeError > ( ErrorType : : NotAnObject , DeprecatedString : : 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 ) ) ;
2022-08-21 14:00:56 +01:00
auto property_key = TRY ( key . to_property_key ( vm ) ) ;
2021-10-20 12:10:23 -04:00
MUST ( object - > create_data_property_or_throw ( property_key , value ) ) ;
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-10-23 02:14:21 +01:00
// 20.1.2.21 Object.seal ( O ), https://tc39.es/ecma262/#sec-object.seal
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : seal )
2021-04-06 22:06:11 +02:00
{
auto argument = vm . argument ( 0 ) ;
if ( ! argument . is_object ( ) )
return argument ;
2021-10-29 17:08:28 -04:00
auto status = TRY ( argument . as_object ( ) . set_integrity_level ( Object : : IntegrityLevel : : Sealed ) ) ;
if ( ! status )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : ObjectSealFailed ) ;
2020-06-01 21:13:16 -07:00
return argument ;
}
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
{
2022-08-21 14:00:56 +01:00
auto * object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
auto key = TRY ( vm . argument ( 1 ) . to_property_key ( vm ) ) ;
2021-10-29 17:08:28 -04:00
auto descriptor = TRY ( object - > internal_get_own_property ( key ) ) ;
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).
2022-08-21 14:00:56 +01: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
for ( auto & key : own_keys ) {
2022-08-21 14:00:56 +01:00
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 ;
}
2021-06-13 00:22:35 +01:00
// 20.1.2.4 Object.defineProperty ( O, P, Attributes ), https://tc39.es/ecma262/#sec-object.defineproperty
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : define_property )
2020-04-09 22:15:26 +02:00
{
2021-10-29 17:08:28 -04:00
if ( ! vm . argument ( 0 ) . is_object ( ) )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : NotAnObject , vm . argument ( 0 ) . to_string_without_side_effects ( ) ) ;
2022-08-21 14:00:56 +01:00
auto key = TRY ( vm . argument ( 1 ) . to_property_key ( vm ) ) ;
2022-08-21 20:38:35 +01:00
auto descriptor = TRY ( to_property_descriptor ( vm , vm . argument ( 2 ) ) ) ;
2021-10-29 17:08:28 -04:00
TRY ( vm . argument ( 0 ) . as_object ( ) . define_property_or_throw ( key , descriptor ) ) ;
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 vm . argument ( 0 ) ;
2020-04-09 22:15:26 +02:00
}
2021-06-13 00:22:35 +01:00
// 20.1.2.3 Object.defineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-object.defineproperties
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : define_properties )
2021-04-10 18:39:11 +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
auto object = vm . argument ( 0 ) ;
auto properties = vm . argument ( 1 ) ;
// 1. If Type(O) is not Object, throw a TypeError exception.
2021-10-29 17:08:28 -04:00
if ( ! object . is_object ( ) )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : NotAnObject , " Object argument " ) ;
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. Return ? ObjectDefineProperties(O, Properties).
2021-10-29 17:08:28 -04:00
return TRY ( object . as_object ( ) . define_properties ( properties ) ) ;
2021-04-10 18:39:11 +02:00
}
2021-10-23 02:14:21 +01:00
// 20.1.2.14 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
{
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
}
2021-10-23 02:14:21 +01:00
// 20.1.2.18 Object.keys ( O ), https://tc39.es/ecma262/#sec-object.keys
2021-10-29 17:08:28 -04: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
2022-08-21 14:00:56 +01:00
auto * object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2021-10-29 17:08:28 -04:00
auto name_list = TRY ( object - > enumerable_own_property_names ( PropertyKind : : Key ) ) ;
2022-08-16 00:20:49 +01:00
return Array : : create_from ( realm , name_list ) ;
2020-04-29 18:59:23 -07:00
}
2021-10-23 02:14:21 +01:00
// 20.1.2.23 Object.values ( O ), https://tc39.es/ecma262/#sec-object.values
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : values )
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
2022-08-21 14:00:56 +01:00
auto * object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2021-10-29 17:08:28 -04:00
auto name_list = TRY ( object - > enumerable_own_property_names ( PropertyKind : : Value ) ) ;
2022-08-16 00:20:49 +01:00
return Array : : create_from ( realm , name_list ) ;
2020-04-29 18:59:23 -07:00
}
2021-06-13 00:22:35 +01:00
// 20.1.2.5 Object.entries ( O ), https://tc39.es/ecma262/#sec-object.entries
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : entries )
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
2022-08-21 14:00:56 +01:00
auto * object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2021-10-29 17:08:28 -04:00
auto name_list = TRY ( object - > enumerable_own_property_names ( PropertyKind : : KeyAndValue ) ) ;
2022-08-16 00:20:49 +01:00
return Array : : create_from ( realm , name_list ) ;
2020-04-29 18:59:23 -07:00
}
2021-06-13 00:22:35 +01:00
// 20.1.2.2 Object.create ( O, Properties ), https://tc39.es/ecma262/#sec-object.create
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : create )
2021-04-10 18:40:12 +02:00
{
2022-08-22 11:48:08 +01:00
auto & realm = * vm . current_realm ( ) ;
2022-08-16 00:20:49 +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
auto proto = vm . argument ( 0 ) ;
2021-04-10 18:40:12 +02:00
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
// 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 ) ;
2021-04-10 18:40:12 +02:00
2022-05-02 20:54:39 +02:00
// 2. Let obj be OrdinaryObjectCreate(O).
2022-12-13 20:49:50 +00:00
auto object = Object : : create ( realm , proto . is_null ( ) ? nullptr : & proto . as_object ( ) ) ;
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
// 3. If Properties is not undefined, then
2021-04-10 18:40:12 +02:00
if ( ! properties . is_undefined ( ) ) {
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. Return ? ObjectDefineProperties(obj, Properties).
2021-10-29 17:08:28 -04:00
return TRY ( object - > define_properties ( properties ) ) ;
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
// 4. Return obj.
2021-04-10 18:40:12 +02:00
return object ;
}
2021-10-23 02:14:21 +01:00
// 20.1.2.13 Object.hasOwn ( O, P ), https://tc39.es/ecma262/#sec-object.hasown
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : has_own )
2021-05-18 11:15:49 +02:00
{
2021-07-05 17:19:46 +01:00
// 1. Let obj be ? ToObject(O).
2022-08-21 14:00:56 +01:00
auto * object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2021-07-05 17:19:46 +01: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 ) ) ;
2021-07-05 17:19:46 +01:00
// 3. Return ? HasOwnProperty(obj, key).
2021-10-29 17:08:28 -04:00
return Value ( TRY ( object - > has_own_property ( key ) ) ) ;
2021-05-18 11:15:49 +02:00
}
2021-06-13 00:22:35 +01:00
// 20.1.2.1 Object.assign ( target, ...sources ), https://tc39.es/ecma262/#sec-object.assign
2021-10-29 17:08:28 -04:00
JS_DEFINE_NATIVE_FUNCTION ( ObjectConstructor : : assign )
2021-06-12 11:35:53 +01:00
{
2021-07-05 17:20:12 +01:00
// 1. Let to be ? ToObject(target).
2022-08-21 14:00:56 +01:00
auto * to = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
2021-07-05 17:20:12 +01:00
// 2. If only one argument was passed, return to.
2021-06-12 11:35:53 +01:00
if ( vm . argument_count ( ) = = 1 )
return to ;
2021-07-05 17:20:12 +01:00
// 3. For each element nextSource of sources, do
2021-06-12 11:35:53 +01:00
for ( size_t i = 1 ; i < vm . argument_count ( ) ; + + i ) {
auto next_source = vm . argument ( i ) ;
2021-07-05 17:20:12 +01:00
// a. If nextSource is neither undefined nor null, then
2021-06-12 11:35:53 +01:00
if ( next_source . is_nullish ( ) )
continue ;
2021-07-05 17:20:12 +01:00
// i. Let from be ! ToObject(nextSource).
2022-08-21 14:00:56 +01:00
auto * from = MUST ( next_source . to_object ( vm ) ) ;
2021-07-05 17:20:12 +01:00
// ii. Let keys be ? from.[[OwnPropertyKeys]]().
2021-10-29 17:08:28 -04:00
auto keys = TRY ( from - > internal_own_property_keys ( ) ) ;
2021-07-05 17:20:12 +01:00
// iii. For each element nextKey of keys, 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 18:14:16 +01:00
for ( auto & next_key : keys ) {
2022-08-21 14:00:56 +01:00
auto property_key = MUST ( PropertyKey : : from_value ( vm , next_key ) ) ;
2021-07-05 17:20:12 +01:00
// 1. Let desc be ? from.[[GetOwnProperty]](nextKey).
2022-02-06 15:59:04 +00:00
auto desc = TRY ( from - > internal_get_own_property ( property_key ) ) ;
2021-07-05 17:20:12 +01:00
// 2. If desc is not undefined and desc.[[Enumerable]] is true, then
if ( ! desc . has_value ( ) | | ! * desc - > enumerable )
2021-06-12 11:35:53 +01:00
continue ;
2021-07-05 17:20:12 +01:00
// a. Let propValue be ? Get(from, nextKey).
2022-02-06 15:59:04 +00:00
auto prop_value = TRY ( from - > get ( property_key ) ) ;
2021-07-05 17:20:12 +01:00
// b. Perform ? Set(to, nextKey, propValue, true).
2022-02-06 15:59:04 +00:00
TRY ( to - > set ( property_key , prop_value , Object : : ShouldThrowExceptions : : Yes ) ) ;
2021-06-12 11:35:53 +01:00
}
}
2021-07-05 17:20:12 +01:00
// 4. Return to.
2021-06-12 11:35:53 +01:00
return to ;
}
2020-03-28 17:23:54 +01:00
}