2020-03-07 19:42:11 +01:00
/*
* Copyright ( c ) 2020 , Andreas Kling < kling @ serenityos . org >
2021-04-22 22:51:19 +02:00
* Copyright ( c ) 2020 - 2021 , Linus Groh < linusg @ serenityos . org >
2020-03-07 19:42:11 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-07 19:42:11 +01:00
*/
2021-01-24 15:28:26 +01:00
# include <AK/Debug.h>
2020-03-07 19:42:11 +01:00
# include <AK/String.h>
2020-10-15 20:46:52 +02:00
# include <AK/TemporaryChange.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Heap/Heap.h>
2021-03-21 12:18:56 +01:00
# include <LibJS/Interpreter.h>
2020-05-21 11:14:23 -07:00
# include <LibJS/Runtime/Accessor.h>
2020-03-20 20:51:59 +01:00
# include <LibJS/Runtime/Array.h>
2020-04-09 22:15:26 +02:00
# include <LibJS/Runtime/Error.h>
2020-04-08 11:05:38 +02:00
# include <LibJS/Runtime/GlobalObject.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/NativeFunction.h>
# include <LibJS/Runtime/NativeProperty.h>
# include <LibJS/Runtime/Object.h>
2021-06-07 22:56:16 +01:00
# include <LibJS/Runtime/ProxyObject.h>
2020-04-02 19:32:21 +02:00
# include <LibJS/Runtime/Shape.h>
2020-04-28 19:19:31 -07:00
# include <LibJS/Runtime/StringObject.h>
2021-04-24 17:59:28 +02:00
# include <LibJS/Runtime/TemporaryClearException.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/Value.h>
2020-03-07 19:42:11 +01:00
namespace JS {
2020-09-21 16:46:45 +02:00
PropertyDescriptor PropertyDescriptor : : from_dictionary ( VM & vm , const Object & object )
2020-06-03 09:40:17 -07:00
{
PropertyAttributes attributes ;
2020-10-13 23:49:19 +02:00
if ( object . has_property ( vm . names . configurable ) ) {
2020-06-03 09:40:17 -07:00
attributes . set_has_configurable ( ) ;
2020-10-13 23:49:19 +02:00
if ( object . get ( vm . names . configurable ) . value_or ( Value ( false ) ) . to_boolean ( ) )
2020-06-03 09:40:17 -07:00
attributes . set_configurable ( ) ;
2020-09-21 16:46:45 +02:00
if ( vm . exception ( ) )
2020-06-03 09:40:17 -07:00
return { } ;
}
2020-10-13 23:49:19 +02:00
if ( object . has_property ( vm . names . enumerable ) ) {
2020-06-03 09:40:17 -07:00
attributes . set_has_enumerable ( ) ;
2020-10-13 23:49:19 +02:00
if ( object . get ( vm . names . enumerable ) . value_or ( Value ( false ) ) . to_boolean ( ) )
2020-06-03 09:40:17 -07:00
attributes . set_enumerable ( ) ;
2020-09-21 16:46:45 +02:00
if ( vm . exception ( ) )
2020-06-03 09:40:17 -07:00
return { } ;
}
2020-10-13 23:49:19 +02:00
if ( object . has_property ( vm . names . writable ) ) {
2020-06-03 09:40:17 -07:00
attributes . set_has_writable ( ) ;
2020-10-13 23:49:19 +02:00
if ( object . get ( vm . names . writable ) . value_or ( Value ( false ) ) . to_boolean ( ) )
2020-06-03 09:40:17 -07:00
attributes . set_writable ( ) ;
2020-09-21 16:46:45 +02:00
if ( vm . exception ( ) )
2020-06-03 09:40:17 -07:00
return { } ;
}
2020-10-13 23:49:19 +02:00
PropertyDescriptor descriptor { attributes , object . get ( vm . names . value ) , nullptr , nullptr } ;
2020-09-21 16:46:45 +02:00
if ( vm . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
2020-10-13 23:49:19 +02:00
auto getter = object . get ( vm . names . get ) ;
2020-09-21 16:46:45 +02:00
if ( vm . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
2020-06-03 09:40:17 -07:00
if ( getter . is_function ( ) )
descriptor . getter = & getter . as_function ( ) ;
2020-10-13 23:49:19 +02:00
auto setter = object . get ( vm . names . set ) ;
2020-09-21 16:46:45 +02:00
if ( vm . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
2020-06-03 09:40:17 -07:00
if ( setter . is_function ( ) )
descriptor . setter = & setter . as_function ( ) ;
return descriptor ;
}
2020-07-22 17:50:18 +02:00
Object * Object : : create_empty ( GlobalObject & global_object )
2020-04-18 10:27:57 +02:00
{
2020-10-17 23:13:37 +02:00
return global_object . heap ( ) . allocate < Object > ( global_object , * global_object . new_object_shape ( ) ) ;
2020-04-18 10:27:57 +02:00
}
2020-06-23 17:21:53 +02:00
Object : : Object ( GlobalObjectTag )
2020-03-08 19:23:58 +01:00
{
2020-06-23 17:21:53 +02:00
// This is the global object
2021-01-05 12:02:59 +01:00
m_shape = heap ( ) . allocate_without_global_object < Shape > ( * this ) ;
2020-06-23 17:21:53 +02:00
}
Object : : Object ( ConstructWithoutPrototypeTag , GlobalObject & global_object )
{
2020-10-17 23:47:07 +02:00
m_shape = heap ( ) . allocate_without_global_object < Shape > ( global_object ) ;
2020-06-23 17:21:53 +02:00
}
Object : : Object ( Object & prototype )
{
m_shape = prototype . global_object ( ) . empty_object_shape ( ) ;
set_prototype ( & prototype ) ;
2020-03-08 19:23:58 +01:00
}
2020-10-17 23:13:37 +02:00
Object : : Object ( Shape & shape )
: m_shape ( & shape )
{
m_storage . resize ( shape . property_count ( ) ) ;
}
2020-07-22 17:50:18 +02:00
void Object : : initialize ( GlobalObject & )
2020-06-20 15:40:48 +02:00
{
}
2020-03-08 19:23:58 +01:00
Object : : ~ Object ( )
{
}
2020-04-02 19:32:21 +02:00
Object * Object : : prototype ( )
{
return shape ( ) . prototype ( ) ;
}
const Object * Object : : prototype ( ) const
{
return shape ( ) . prototype ( ) ;
}
2021-06-07 22:56:16 +01:00
// 10.1.2.1 OrdinarySetPrototypeOf, https://tc39.es/ecma262/#sec-ordinarysetprototypeof
2020-06-02 12:32:54 +01:00
bool Object : : set_prototype ( Object * new_prototype )
2020-04-02 19:32:21 +02:00
{
2020-04-18 10:27:57 +02:00
if ( prototype ( ) = = new_prototype )
2020-06-02 12:32:54 +01:00
return true ;
if ( ! m_is_extensible )
return false ;
2021-06-07 22:56:16 +01:00
auto * prototype = new_prototype ;
while ( prototype ) {
if ( prototype = = this )
return false ;
// NOTE: This is a best-effort implementation of the following step:
// "If p.[[GetPrototypeOf]] is not the ordinary object internal method defined in 10.1.1,
// set done to true."
// We don't have a good way of detecting whether certain virtual Object methods have been
// overridden by a given object, but as ProxyObject is the only one doing that, this check
// does the trick.
if ( is < ProxyObject > ( prototype ) )
break ;
prototype = prototype - > prototype ( ) ;
2020-04-26 13:53:40 +02:00
}
2021-06-07 22:56:16 +01:00
auto & shape = this - > shape ( ) ;
if ( shape . is_unique ( ) )
shape . set_prototype_without_transition ( new_prototype ) ;
else
m_shape = shape . create_prototype_transition ( new_prototype ) ;
2020-06-02 12:32:54 +01:00
return true ;
2020-04-02 19:32:21 +02:00
}
2020-03-28 19:48:12 +01:00
bool Object : : has_prototype ( const Object * prototype ) const
{
2020-04-02 19:32:21 +02:00
for ( auto * object = this - > prototype ( ) ; object ; object = object - > prototype ( ) ) {
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-06-07 10:53:14 -07:00
return false ;
2020-03-28 19:48:12 +01:00
if ( object = = prototype )
return true ;
}
return false ;
}
2020-06-01 21:13:16 -07:00
bool Object : : prevent_extensions ( )
{
m_is_extensible = false ;
return true ;
}
2021-04-06 22:06:11 +02:00
// 7.3.15 SetIntegrityLevel, https://tc39.es/ecma262/#sec-setintegritylevel
bool Object : : set_integrity_level ( IntegrityLevel level )
{
// FIXME: This feels clunky and should get nicer abstractions.
2021-04-20 09:08:40 +02:00
auto update_property = [ this ] ( auto & property_name , auto new_attributes ) {
if ( property_name . is_number ( ) ) {
auto value_and_attributes = m_indexed_properties . get ( nullptr , property_name . as_number ( ) , false ) . value ( ) ;
auto value = value_and_attributes . value ;
auto attributes = value_and_attributes . attributes . bits ( ) & new_attributes ;
m_indexed_properties . put ( nullptr , property_name . as_number ( ) , value , attributes , false ) ;
} else {
auto metadata = shape ( ) . lookup ( property_name . to_string_or_symbol ( ) ) . value ( ) ;
auto attributes = metadata . attributes . bits ( ) & new_attributes ;
if ( m_shape - > is_unique ( ) )
m_shape - > reconfigure_property_in_unique_shape ( property_name . to_string_or_symbol ( ) , attributes ) ;
else
set_shape ( * m_shape - > create_configure_transition ( property_name . to_string_or_symbol ( ) , attributes ) ) ;
}
2021-04-06 22:06:11 +02:00
} ;
auto & vm = this - > vm ( ) ;
auto status = prevent_extensions ( ) ;
if ( vm . exception ( ) )
return false ;
if ( ! status )
return false ;
auto keys = get_own_properties ( PropertyKind : : Key ) ;
if ( vm . exception ( ) )
return false ;
switch ( level ) {
case IntegrityLevel : : Sealed :
for ( auto & key : keys ) {
2021-04-20 09:00:11 +02:00
auto property_name = PropertyName : : from_value ( global_object ( ) , key ) ;
2021-04-20 09:08:40 +02:00
if ( property_name . is_string ( ) ) {
i32 property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
property_name = property_index ;
}
2021-04-20 09:00:11 +02:00
update_property ( property_name , ~ Attribute : : Configurable ) ;
2021-04-06 22:06:11 +02:00
if ( vm . exception ( ) )
return { } ;
}
break ;
case IntegrityLevel : : Frozen :
for ( auto & key : keys ) {
auto property_name = PropertyName : : from_value ( global_object ( ) , key ) ;
2021-04-20 09:08:40 +02:00
if ( property_name . is_string ( ) ) {
i32 property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
property_name = property_index ;
}
2021-04-06 22:06:11 +02:00
auto property_descriptor = get_own_property_descriptor ( property_name ) ;
VERIFY ( property_descriptor . has_value ( ) ) ;
u8 attributes = property_descriptor - > is_accessor_descriptor ( )
? ~ Attribute : : Configurable
: ~ Attribute : : Configurable & ~ Attribute : : Writable ;
2021-04-20 09:00:11 +02:00
update_property ( property_name , attributes ) ;
2021-04-06 22:06:11 +02:00
if ( vm . exception ( ) )
return { } ;
}
break ;
default :
VERIFY_NOT_REACHED ( ) ;
}
return true ;
}
2021-04-06 22:45:12 +02:00
// 7.3.16 TestIntegrityLevel, https://tc39.es/ecma262/#sec-testintegritylevel
bool Object : : test_integrity_level ( IntegrityLevel level )
{
auto & vm = this - > vm ( ) ;
auto extensible = is_extensible ( ) ;
if ( vm . exception ( ) )
return false ;
if ( extensible )
return false ;
auto keys = get_own_properties ( PropertyKind : : Key ) ;
if ( vm . exception ( ) )
return false ;
for ( auto & key : keys ) {
auto property_name = PropertyName : : from_value ( global_object ( ) , key ) ;
auto property_descriptor = get_own_property_descriptor ( property_name ) ;
VERIFY ( property_descriptor . has_value ( ) ) ;
if ( property_descriptor - > attributes . is_configurable ( ) )
return false ;
if ( level = = IntegrityLevel : : Frozen & & property_descriptor - > is_data_descriptor ( ) ) {
if ( property_descriptor - > attributes . is_writable ( ) )
return false ;
}
}
return true ;
}
2021-04-11 22:52:25 +02:00
Value Object : : get_own_property ( const PropertyName & property_name , Value receiver , bool without_side_effects ) const
2020-03-21 14:37:34 +01:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
VERIFY ( ! receiver . is_empty ( ) ) ;
2020-11-04 23:29:45 +00:00
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
Value value_here ;
if ( property_name . is_number ( ) ) {
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
auto existing_property = m_indexed_properties . get ( nullptr , property_name . as_number ( ) , false ) ;
if ( ! existing_property . has_value ( ) )
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
return { } ;
2020-06-03 14:34:52 -07:00
value_here = existing_property . value ( ) . value . value_or ( js_undefined ( ) ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
} else {
2020-07-07 21:38:46 -07:00
auto metadata = shape ( ) . lookup ( property_name . to_string_or_symbol ( ) ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( ! metadata . has_value ( ) )
return { } ;
2020-06-03 14:34:52 -07:00
value_here = m_storage [ metadata . value ( ) . offset ] . value_or ( js_undefined ( ) ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
}
2020-04-02 19:32:21 +02:00
2021-02-23 20:42:32 +01:00
VERIFY ( ! value_here . is_empty ( ) ) ;
2021-04-11 22:52:25 +02:00
if ( ! without_side_effects ) {
if ( value_here . is_accessor ( ) )
return value_here . as_accessor ( ) . call_getter ( receiver ) ;
if ( value_here . is_native_property ( ) )
return call_native_property_getter ( value_here . as_native_property ( ) , receiver ) ;
}
2020-03-21 14:37:34 +01:00
return value_here ;
}
2021-04-06 21:39:17 +02:00
MarkedValueList Object : : get_own_properties ( PropertyKind kind , bool only_enumerable_properties , GetOwnPropertyReturnType return_type ) const
2020-04-29 18:59:23 -07:00
{
2021-04-06 21:39:17 +02:00
MarkedValueList properties ( heap ( ) ) ;
2020-04-29 18:59:23 -07:00
// FIXME: Support generic iterables
2021-04-05 18:04:55 +02:00
if ( is < StringObject > ( * this ) ) {
auto str = static_cast < const StringObject & > ( * this ) . primitive_string ( ) . string ( ) ;
2020-04-29 18:59:23 -07:00
for ( size_t i = 0 ; i < str . length ( ) ; + + i ) {
2020-07-09 14:42:30 -07:00
if ( kind = = PropertyKind : : Key ) {
2021-04-06 21:39:17 +02:00
properties . append ( js_string ( vm ( ) , String : : number ( i ) ) ) ;
2020-07-09 14:42:30 -07:00
} else if ( kind = = PropertyKind : : Value ) {
2021-04-06 21:39:17 +02:00
properties . append ( js_string ( vm ( ) , String : : formatted ( " {:c} " , str [ i ] ) ) ) ;
2020-04-29 18:59:23 -07:00
} else {
2020-06-08 12:25:45 +02:00
auto * entry_array = Array : : create ( global_object ( ) ) ;
2020-09-27 19:57:16 +02:00
entry_array - > define_property ( 0 , js_string ( vm ( ) , String : : number ( i ) ) ) ;
2020-12-06 16:55:19 +00:00
entry_array - > define_property ( 1 , js_string ( vm ( ) , String : : formatted ( " {:c} " , str [ i ] ) ) ) ;
2021-04-06 21:39:17 +02:00
properties . append ( entry_array ) ;
2020-04-29 18:59:23 -07:00
}
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2021-04-06 21:39:17 +02:00
return MarkedValueList { heap ( ) } ;
2020-04-29 18:59:23 -07:00
}
2021-04-06 21:39:17 +02:00
return properties ;
2020-04-29 18:59:23 -07:00
}
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
for ( auto & entry : m_indexed_properties ) {
2021-04-05 18:04:55 +02:00
auto value_and_attributes = entry . value_and_attributes ( const_cast < Object * > ( this ) ) ;
2020-06-03 14:34:52 -07:00
if ( only_enumerable_properties & & ! value_and_attributes . attributes . is_enumerable ( ) )
continue ;
2020-07-09 14:42:30 -07:00
if ( kind = = PropertyKind : : Key ) {
2021-04-06 21:39:17 +02:00
properties . append ( js_string ( vm ( ) , String : : number ( entry . index ( ) ) ) ) ;
2020-07-09 14:42:30 -07:00
} else if ( kind = = PropertyKind : : Value ) {
2021-04-06 21:39:17 +02:00
properties . append ( value_and_attributes . value ) ;
2020-04-29 18:59:23 -07:00
} else {
2020-06-08 12:25:45 +02:00
auto * entry_array = Array : : create ( global_object ( ) ) ;
2020-09-27 19:57:16 +02:00
entry_array - > define_property ( 0 , js_string ( vm ( ) , String : : number ( entry . index ( ) ) ) ) ;
2020-06-03 14:34:52 -07:00
entry_array - > define_property ( 1 , value_and_attributes . value ) ;
2021-04-06 21:39:17 +02:00
properties . append ( entry_array ) ;
2020-04-29 18:59:23 -07:00
}
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2021-04-06 21:39:17 +02:00
return MarkedValueList { heap ( ) } ;
2020-04-29 18:59:23 -07:00
}
2021-04-05 19:17:40 +02:00
auto add_property_to_results = [ & ] ( auto & property ) {
2020-07-09 14:42:30 -07:00
if ( kind = = PropertyKind : : Key ) {
2021-04-06 21:39:17 +02:00
properties . append ( property . key . to_value ( vm ( ) ) ) ;
2020-07-09 14:42:30 -07:00
} else if ( kind = = PropertyKind : : Value ) {
2021-04-06 21:39:17 +02:00
properties . append ( get ( property . key ) ) ;
2020-06-03 14:34:52 -07:00
} else {
2020-06-08 12:25:45 +02:00
auto * entry_array = Array : : create ( global_object ( ) ) ;
2021-04-05 19:17:40 +02:00
entry_array - > define_property ( 0 , property . key . to_value ( vm ( ) ) ) ;
entry_array - > define_property ( 1 , get ( property . key ) ) ;
2021-04-06 21:39:17 +02:00
properties . append ( entry_array ) ;
2020-04-29 18:59:23 -07:00
}
2021-04-05 19:17:40 +02:00
} ;
// NOTE: Most things including for..in/of and Object.{keys,values,entries}() use StringOnly, and in those
// cases we won't be iterating the ordered property table twice. We can certainly improve this though.
if ( return_type = = GetOwnPropertyReturnType : : All | | return_type = = GetOwnPropertyReturnType : : StringOnly ) {
for ( auto & it : shape ( ) . property_table_ordered ( ) ) {
if ( only_enumerable_properties & & ! it . value . attributes . is_enumerable ( ) )
continue ;
if ( it . key . is_symbol ( ) )
continue ;
add_property_to_results ( it ) ;
if ( vm ( ) . exception ( ) )
2021-04-06 21:39:17 +02:00
return MarkedValueList { heap ( ) } ;
2021-04-05 19:17:40 +02:00
}
}
if ( return_type = = GetOwnPropertyReturnType : : All | | return_type = = GetOwnPropertyReturnType : : SymbolOnly ) {
for ( auto & it : shape ( ) . property_table_ordered ( ) ) {
if ( only_enumerable_properties & & ! it . value . attributes . is_enumerable ( ) )
continue ;
if ( it . key . is_string ( ) )
continue ;
add_property_to_results ( it ) ;
if ( vm ( ) . exception ( ) )
2021-04-06 21:39:17 +02:00
return MarkedValueList { heap ( ) } ;
2021-04-05 19:17:40 +02:00
}
2020-04-29 18:59:23 -07:00
}
2021-04-06 21:39:17 +02:00
return properties ;
2020-04-29 18:59:23 -07:00
}
2021-04-05 19:05:15 +02:00
// 7.3.23 EnumerableOwnPropertyNames, https://tc39.es/ecma262/#sec-enumerableownpropertynames
2021-04-06 21:39:17 +02:00
MarkedValueList Object : : get_enumerable_own_property_names ( PropertyKind kind ) const
2021-04-05 19:05:15 +02:00
{
return get_own_properties ( kind , true , Object : : GetOwnPropertyReturnType : : StringOnly ) ;
}
2020-07-07 21:38:46 -07:00
Optional < PropertyDescriptor > Object : : get_own_property_descriptor ( const PropertyName & property_name ) const
2020-05-01 11:06:27 +01:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
2020-11-04 23:29:45 +00:00
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
Value value ;
2020-06-02 17:13:09 -07:00
PropertyAttributes attributes ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( property_name . is_number ( ) ) {
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
auto existing_value = m_indexed_properties . get ( nullptr , property_name . as_number ( ) , false ) ;
if ( ! existing_value . has_value ( ) )
2020-06-03 09:40:17 -07:00
return { } ;
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
value = existing_value . value ( ) . value ;
attributes = existing_value . value ( ) . attributes ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
} else {
2021-04-20 18:53:07 +02:00
if ( property_name . is_string ( ) ) {
i32 property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
return get_own_property_descriptor ( property_index ) ;
}
2020-07-07 21:38:46 -07:00
auto metadata = shape ( ) . lookup ( property_name . to_string_or_symbol ( ) ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( ! metadata . has_value ( ) )
2020-06-03 09:40:17 -07:00
return { } ;
2020-05-29 20:10:21 -07:00
value = m_storage [ metadata . value ( ) . offset ] ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
attributes = metadata . value ( ) . attributes ;
}
2020-06-03 09:40:17 -07:00
PropertyDescriptor descriptor { attributes , { } , nullptr , nullptr } ;
2020-06-23 17:56:57 +02:00
if ( value . is_native_property ( ) ) {
2020-11-22 17:29:25 +00:00
auto result = call_native_property_getter ( value . as_native_property ( ) , const_cast < Object * > ( this ) ) ;
2020-06-03 09:40:17 -07:00
descriptor . value = result . value_or ( js_undefined ( ) ) ;
2020-05-29 20:10:21 -07:00
} else if ( value . is_accessor ( ) ) {
2020-05-21 11:14:23 -07:00
auto & pair = value . as_accessor ( ) ;
2020-05-29 20:10:21 -07:00
if ( pair . getter ( ) )
2020-06-03 09:40:17 -07:00
descriptor . getter = pair . getter ( ) ;
2020-05-29 20:10:21 -07:00
if ( pair . setter ( ) )
2020-06-03 09:40:17 -07:00
descriptor . setter = pair . setter ( ) ;
2020-05-21 11:14:23 -07:00
} else {
2020-06-03 09:40:17 -07:00
descriptor . value = value . value_or ( js_undefined ( ) ) ;
2020-05-21 11:14:23 -07:00
}
2020-06-03 09:40:17 -07:00
2020-05-01 11:06:27 +01:00
return descriptor ;
}
2021-06-07 23:02:52 +01:00
// Equivalent to:
// 6.2.5.4 FromPropertyDescriptor, https://tc39.es/ecma262/#sec-frompropertydescriptor
2020-07-07 21:38:46 -07:00
Value Object : : get_own_property_descriptor_object ( const PropertyName & property_name ) const
2020-06-03 09:40:17 -07:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
2020-11-04 23:29:45 +00:00
2020-10-13 23:49:19 +02:00
auto & vm = this - > vm ( ) ;
2020-06-03 09:40:17 -07:00
auto descriptor_opt = get_own_property_descriptor ( property_name ) ;
if ( ! descriptor_opt . has_value ( ) )
return js_undefined ( ) ;
auto descriptor = descriptor_opt . value ( ) ;
2020-07-22 17:50:18 +02:00
auto * descriptor_object = Object : : create_empty ( global_object ( ) ) ;
2020-06-03 09:40:17 -07:00
if ( descriptor . is_data_descriptor ( ) ) {
2020-10-13 23:49:19 +02:00
descriptor_object - > define_property ( vm . names . value , descriptor . value . value_or ( js_undefined ( ) ) ) ;
descriptor_object - > define_property ( vm . names . writable , Value ( descriptor . attributes . is_writable ( ) ) ) ;
2021-06-07 23:07:13 +01:00
} else {
VERIFY ( descriptor . is_accessor_descriptor ( ) ) ;
descriptor_object - > define_property ( vm . names . get , descriptor . getter ? Value ( descriptor . getter ) : js_undefined ( ) ) ;
descriptor_object - > define_property ( vm . names . set , descriptor . setter ? Value ( descriptor . setter ) : js_undefined ( ) ) ;
2020-06-03 09:40:17 -07:00
}
2021-06-07 23:02:52 +01:00
descriptor_object - > define_property ( vm . names . enumerable , Value ( descriptor . attributes . is_enumerable ( ) ) ) ;
descriptor_object - > define_property ( vm . names . configurable , Value ( descriptor . attributes . is_configurable ( ) ) ) ;
2020-06-03 09:40:17 -07:00
return descriptor_object ;
}
2020-04-02 19:32:21 +02:00
void Object : : set_shape ( Shape & new_shape )
{
m_storage . resize ( new_shape . property_count ( ) ) ;
m_shape = & new_shape ;
}
2020-07-07 21:38:46 -07:00
bool Object : : define_property ( const StringOrSymbol & property_name , const Object & descriptor , bool throw_exceptions )
2020-05-01 11:06:27 +01:00
{
2020-10-13 23:49:19 +02:00
auto & vm = this - > vm ( ) ;
bool is_accessor_property = descriptor . has_property ( vm . names . get ) | | descriptor . has_property ( vm . names . set ) ;
2020-06-02 17:13:09 -07:00
PropertyAttributes attributes ;
2020-10-13 23:49:19 +02:00
if ( descriptor . has_property ( vm . names . configurable ) ) {
2020-06-02 17:13:09 -07:00
attributes . set_has_configurable ( ) ;
2020-10-13 23:49:19 +02:00
if ( descriptor . get ( vm . names . configurable ) . value_or ( Value ( false ) ) . to_boolean ( ) )
2020-06-02 17:13:09 -07:00
attributes . set_configurable ( ) ;
2020-10-13 23:49:19 +02:00
if ( vm . exception ( ) )
2020-06-02 17:13:09 -07:00
return false ;
}
2020-10-13 23:49:19 +02:00
if ( descriptor . has_property ( vm . names . enumerable ) ) {
2020-06-02 17:13:09 -07:00
attributes . set_has_enumerable ( ) ;
2020-10-13 23:49:19 +02:00
if ( descriptor . get ( vm . names . enumerable ) . value_or ( Value ( false ) ) . to_boolean ( ) )
2020-06-02 17:13:09 -07:00
attributes . set_enumerable ( ) ;
2020-10-13 23:49:19 +02:00
if ( vm . exception ( ) )
2020-06-02 17:13:09 -07:00
return false ;
}
2020-05-21 11:14:23 -07:00
if ( is_accessor_property ) {
2020-10-13 23:49:19 +02:00
if ( descriptor . has_property ( vm . names . value ) | | descriptor . has_property ( vm . names . writable ) ) {
2020-05-21 11:14:23 -07:00
if ( throw_exceptions )
2020-10-13 23:49:19 +02:00
vm . throw_exception < TypeError > ( global_object ( ) , ErrorType : : AccessorValueOrWritable ) ;
2020-05-21 11:14:23 -07:00
return false ;
}
2020-10-13 23:49:19 +02:00
auto getter = descriptor . get ( vm . names . get ) . value_or ( js_undefined ( ) ) ;
if ( vm . exception ( ) )
2020-05-21 11:14:23 -07:00
return { } ;
2020-10-13 23:49:19 +02:00
auto setter = descriptor . get ( vm . names . set ) . value_or ( js_undefined ( ) ) ;
if ( vm . exception ( ) )
2020-05-21 11:14:23 -07:00
return { } ;
2020-05-23 23:27:10 +01:00
Function * getter_function { nullptr } ;
Function * setter_function { nullptr } ;
if ( getter . is_function ( ) ) {
getter_function = & getter . as_function ( ) ;
} else if ( ! getter . is_undefined ( ) ) {
2020-10-13 23:49:19 +02:00
vm . throw_exception < TypeError > ( global_object ( ) , ErrorType : : AccessorBadField , " get " ) ;
2020-05-21 11:14:23 -07:00
return false ;
}
2020-05-23 23:27:10 +01:00
if ( setter . is_function ( ) ) {
setter_function = & setter . as_function ( ) ;
} else if ( ! setter . is_undefined ( ) ) {
2020-10-13 23:49:19 +02:00
vm . throw_exception < TypeError > ( global_object ( ) , ErrorType : : AccessorBadField , " set " ) ;
2020-05-21 11:14:23 -07:00
return false ;
}
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }} " , property_name . to_display_string ( ) , attributes , getter , setter ) ;
2020-05-21 11:14:23 -07:00
2020-10-13 23:49:19 +02:00
return define_property ( property_name , Accessor : : create ( vm , getter_function , setter_function ) , attributes , throw_exceptions ) ;
2020-05-21 11:14:23 -07:00
}
2020-10-13 23:49:19 +02:00
auto value = descriptor . get ( vm . names . value ) ;
if ( vm . exception ( ) )
2020-05-21 11:14:23 -07:00
return { } ;
2020-10-13 23:49:19 +02:00
if ( descriptor . has_property ( vm . names . writable ) ) {
2020-06-02 17:13:09 -07:00
attributes . set_has_writable ( ) ;
2020-10-13 23:49:19 +02:00
if ( descriptor . get ( vm . names . writable ) . value_or ( Value ( false ) ) . to_boolean ( ) )
2020-06-02 17:13:09 -07:00
attributes . set_writable ( ) ;
2020-10-13 23:49:19 +02:00
if ( vm . exception ( ) )
2020-06-02 17:13:09 -07:00
return false ;
}
2020-10-13 23:49:19 +02:00
if ( vm . exception ( ) )
2020-05-21 11:14:23 -07:00
return { } ;
2020-05-01 11:06:27 +01:00
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Defining new property {} with data descriptor {{ attributes={}, value={} }} " , property_name . to_display_string ( ) , attributes , value ) ;
2020-05-01 11:06:27 +01:00
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
return define_property ( property_name , value , attributes , throw_exceptions ) ;
2020-05-01 11:06:27 +01:00
}
2020-10-06 16:57:02 +02:00
bool Object : : define_property_without_transition ( const PropertyName & property_name , Value value , PropertyAttributes attributes , bool throw_exceptions )
{
TemporaryChange change ( m_transitions_enabled , false ) ;
return define_property ( property_name , value , attributes , throw_exceptions ) ;
}
2020-07-07 21:38:46 -07:00
bool Object : : define_property ( const PropertyName & property_name , Value value , PropertyAttributes attributes , bool throw_exceptions )
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
2020-11-04 23:29:45 +00:00
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( property_name . is_number ( ) )
2021-04-05 18:04:55 +02:00
return put_own_property_by_index ( property_name . as_number ( ) , value , attributes , PutOwnPropertyMode : : DefineProperty , throw_exceptions ) ;
2020-11-04 23:29:45 +00:00
2020-07-07 21:38:46 -07:00
if ( property_name . is_string ( ) ) {
i32 property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
2021-04-05 18:04:55 +02:00
return put_own_property_by_index ( property_index , value , attributes , PutOwnPropertyMode : : DefineProperty , throw_exceptions ) ;
2020-07-07 21:38:46 -07:00
}
2021-04-05 18:04:55 +02:00
return put_own_property ( property_name . to_string_or_symbol ( ) , value , attributes , PutOwnPropertyMode : : DefineProperty , throw_exceptions ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
}
LibJS: Update Object::define_accessor() to take both getter and setter
This replaces the current 'function plus boolean indicating the type'
API, which makes it easier to set both getter and setter at once.
This was already possible before but required two calls of this
function, which wasn't intuitive:
define_accessor(name, getter, true, ...);
define_accessor(name, setter, false, ...);
Which now becomes:
define_accessor(name, getter, setter, ...);
2021-04-10 18:35:29 +02:00
bool Object : : define_accessor ( const PropertyName & property_name , Function * getter , Function * setter , PropertyAttributes attributes , bool throw_exceptions )
2020-06-13 21:23:33 -05:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
2020-11-04 23:29:45 +00:00
2020-06-13 21:23:33 -05:00
Accessor * accessor { nullptr } ;
2020-07-07 21:38:46 -07:00
auto property_metadata = shape ( ) . lookup ( property_name . to_string_or_symbol ( ) ) ;
2020-06-13 21:23:33 -05:00
if ( property_metadata . has_value ( ) ) {
auto existing_property = get_direct ( property_metadata . value ( ) . offset ) ;
if ( existing_property . is_accessor ( ) )
accessor = & existing_property . as_accessor ( ) ;
}
if ( ! accessor ) {
LibJS: Update Object::define_accessor() to take both getter and setter
This replaces the current 'function plus boolean indicating the type'
API, which makes it easier to set both getter and setter at once.
This was already possible before but required two calls of this
function, which wasn't intuitive:
define_accessor(name, getter, true, ...);
define_accessor(name, setter, false, ...);
Which now becomes:
define_accessor(name, getter, setter, ...);
2021-04-10 18:35:29 +02:00
accessor = Accessor : : create ( vm ( ) , getter , setter ) ;
2020-06-13 21:23:33 -05:00
bool definition_success = define_property ( property_name , accessor , attributes , throw_exceptions ) ;
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-06-13 21:23:33 -05:00
return { } ;
if ( ! definition_success )
return false ;
LibJS: Update Object::define_accessor() to take both getter and setter
This replaces the current 'function plus boolean indicating the type'
API, which makes it easier to set both getter and setter at once.
This was already possible before but required two calls of this
function, which wasn't intuitive:
define_accessor(name, getter, true, ...);
define_accessor(name, setter, false, ...);
Which now becomes:
define_accessor(name, getter, setter, ...);
2021-04-10 18:35:29 +02:00
} else {
if ( getter )
accessor - > set_getter ( getter ) ;
if ( setter )
accessor - > set_setter ( setter ) ;
2020-06-13 21:23:33 -05:00
}
return true ;
}
2021-04-05 18:04:55 +02:00
bool Object : : put_own_property ( const StringOrSymbol & property_name , Value value , PropertyAttributes attributes , PutOwnPropertyMode mode , bool throw_exceptions )
2020-03-21 14:37:34 +01:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( ! ( mode = = PutOwnPropertyMode : : Put & & value . is_accessor ( ) ) ) ;
2020-05-21 11:14:23 -07:00
2020-10-13 23:49:19 +02:00
if ( value . is_accessor ( ) ) {
auto & accessor = value . as_accessor ( ) ;
if ( accessor . getter ( ) )
attributes . set_has_getter ( ) ;
if ( accessor . setter ( ) )
attributes . set_has_setter ( ) ;
}
2020-10-13 23:53:02 +02:00
// NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
// Transitions are primarily interesting when scripts add properties to objects.
if ( ! m_transitions_enabled & & ! m_shape - > is_unique ( ) ) {
m_shape - > add_property_without_transition ( property_name , attributes ) ;
m_storage . resize ( m_shape - > property_count ( ) ) ;
m_storage [ m_shape - > property_count ( ) - 1 ] = value ;
return true ;
}
2020-07-06 17:17:47 -07:00
auto metadata = shape ( ) . lookup ( property_name ) ;
bool new_property = ! metadata . has_value ( ) ;
if ( ! is_extensible ( ) & & new_property ) {
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Disallow define_property of non-extensible object " ) ;
2020-10-04 13:54:44 +02:00
if ( throw_exceptions & & vm ( ) . in_strict_mode ( ) )
2020-10-04 13:55:20 +01:00
vm ( ) . throw_exception < TypeError > ( global_object ( ) , ErrorType : : NonExtensibleDefine , property_name . to_display_string ( ) ) ;
2020-06-01 21:13:16 -07:00
return false ;
}
2020-04-27 23:05:02 -07:00
if ( new_property ) {
2020-05-05 18:48:30 +02:00
if ( ! m_shape - > is_unique ( ) & & shape ( ) . property_count ( ) > 100 ) {
// If you add more than 100 properties to an object, let's stop doing
// transitions to avoid filling up the heap with shapes.
ensure_shape_is_unique ( ) ;
}
2020-04-26 13:53:40 +02:00
if ( m_shape - > is_unique ( ) ) {
m_shape - > add_property_to_unique_shape ( property_name , attributes ) ;
2020-04-26 19:03:23 +02:00
m_storage . resize ( m_shape - > property_count ( ) ) ;
2020-10-05 20:08:14 +02:00
} else if ( m_transitions_enabled ) {
2020-04-26 13:53:40 +02:00
set_shape ( * m_shape - > create_put_transition ( property_name , attributes ) ) ;
2020-10-05 20:08:14 +02:00
} else {
m_shape - > add_property_without_transition ( property_name , attributes ) ;
m_storage . resize ( m_shape - > property_count ( ) ) ;
2020-04-26 13:53:40 +02:00
}
2020-04-02 19:32:21 +02:00
metadata = shape ( ) . lookup ( property_name ) ;
2021-02-23 20:42:32 +01:00
VERIFY ( metadata . has_value ( ) ) ;
2020-04-09 22:15:26 +02:00
}
2020-04-09 22:55:17 +02:00
2020-06-02 17:13:09 -07:00
if ( ! new_property & & mode = = PutOwnPropertyMode : : DefineProperty & & ! metadata . value ( ) . attributes . is_configurable ( ) & & attributes ! = metadata . value ( ) . attributes ) {
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Disallow reconfig of non-configurable property " ) ;
2020-05-01 11:06:27 +01:00
if ( throw_exceptions )
2020-10-04 13:55:20 +01:00
vm ( ) . throw_exception < TypeError > ( global_object ( ) , ErrorType : : DescChangeNonConfigurable , property_name . to_display_string ( ) ) ;
2020-04-30 20:01:54 +01:00
return false ;
2020-04-02 19:32:21 +02:00
}
2020-04-09 22:55:17 +02:00
if ( mode = = PutOwnPropertyMode : : DefineProperty & & attributes ! = metadata . value ( ) . attributes ) {
2020-04-26 13:53:40 +02:00
if ( m_shape - > is_unique ( ) ) {
m_shape - > reconfigure_property_in_unique_shape ( property_name , attributes ) ;
} else {
set_shape ( * m_shape - > create_configure_transition ( property_name , attributes ) ) ;
}
2020-04-09 22:55:17 +02:00
metadata = shape ( ) . lookup ( property_name ) ;
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Reconfigured property {}, new shape says offset is {} and my storage capacity is {} " , property_name . to_display_string ( ) , metadata . value ( ) . offset , m_storage . size ( ) ) ;
2020-04-09 22:55:17 +02:00
}
2020-05-21 11:14:23 -07:00
auto value_here = m_storage [ metadata . value ( ) . offset ] ;
2020-06-02 17:13:09 -07:00
if ( ! new_property & & mode = = PutOwnPropertyMode : : Put & & ! value_here . is_accessor ( ) & & ! metadata . value ( ) . attributes . is_writable ( ) ) {
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Disallow write to non-writable property " ) ;
2021-06-06 00:43:03 +03:00
if ( throw_exceptions & & vm ( ) . in_strict_mode ( ) )
vm ( ) . throw_exception < TypeError > ( global_object ( ) , ErrorType : : DescWriteNonWritable , property_name . to_display_string ( ) ) ;
2020-04-30 20:01:54 +01:00
return false ;
2020-04-09 22:55:17 +02:00
}
if ( value . is_empty ( ) )
2020-04-30 20:01:54 +01:00
return true ;
2020-04-09 22:55:17 +02:00
2020-06-23 17:56:57 +02:00
if ( value_here . is_native_property ( ) ) {
2021-04-05 18:04:55 +02:00
call_native_property_setter ( value_here . as_native_property ( ) , this , value ) ;
2020-03-21 14:37:34 +01:00
} else {
2020-04-02 19:32:21 +02:00
m_storage [ metadata . value ( ) . offset ] = value ;
2020-03-21 14:37:34 +01:00
}
2020-04-30 20:01:54 +01:00
return true ;
2020-03-21 14:37:34 +01:00
}
2021-04-05 18:04:55 +02:00
bool Object : : put_own_property_by_index ( u32 property_index , Value value , PropertyAttributes attributes , PutOwnPropertyMode mode , bool throw_exceptions )
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( ! ( mode = = PutOwnPropertyMode : : Put & & value . is_accessor ( ) ) ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
2020-07-06 17:17:47 -07:00
auto existing_property = m_indexed_properties . get ( nullptr , property_index , false ) ;
auto new_property = ! existing_property . has_value ( ) ;
if ( ! is_extensible ( ) & & new_property ) {
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Disallow define_property of non-extensible object " ) ;
2020-10-04 13:54:44 +02:00
if ( throw_exceptions & & vm ( ) . in_strict_mode ( ) )
2020-09-27 20:07:25 +02:00
vm ( ) . throw_exception < TypeError > ( global_object ( ) , ErrorType : : NonExtensibleDefine , property_index ) ;
2020-06-01 21:13:16 -07:00
return false ;
}
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( value . is_accessor ( ) ) {
auto & accessor = value . as_accessor ( ) ;
if ( accessor . getter ( ) )
2020-06-02 17:13:09 -07:00
attributes . set_has_getter ( ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( accessor . setter ( ) )
2020-06-02 17:13:09 -07:00
attributes . set_has_setter ( ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
}
2020-06-02 17:13:09 -07:00
PropertyAttributes existing_attributes = new_property ? 0 : existing_property . value ( ) . attributes ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
2020-06-02 17:13:09 -07:00
if ( ! new_property & & mode = = PutOwnPropertyMode : : DefineProperty & & ! existing_attributes . is_configurable ( ) & & attributes ! = existing_attributes ) {
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Disallow reconfig of non-configurable property " ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( throw_exceptions )
2020-09-27 20:07:25 +02:00
vm ( ) . throw_exception < TypeError > ( global_object ( ) , ErrorType : : DescChangeNonConfigurable , property_index ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
return false ;
}
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
auto value_here = new_property ? Value ( ) : existing_property . value ( ) . value ;
2020-06-02 17:13:09 -07:00
if ( ! new_property & & mode = = PutOwnPropertyMode : : Put & & ! value_here . is_accessor ( ) & & ! existing_attributes . is_writable ( ) ) {
2021-04-18 18:10:42 +02:00
dbgln_if ( OBJECT_DEBUG , " Disallow write to non-writable property " ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
return false ;
}
if ( value . is_empty ( ) )
return true ;
2020-06-23 17:56:57 +02:00
if ( value_here . is_native_property ( ) ) {
2021-04-05 18:04:55 +02:00
call_native_property_setter ( value_here . as_native_property ( ) , this , value ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
} else {
2021-04-05 18:04:55 +02:00
m_indexed_properties . put ( this , property_index , value , attributes , mode = = PutOwnPropertyMode : : Put ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
}
return true ;
}
2021-04-10 17:44:12 +02:00
bool Object : : delete_property ( const PropertyName & property_name )
2020-04-26 13:53:40 +02:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
2020-11-04 23:29:45 +00:00
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
if ( property_name . is_number ( ) )
2021-04-10 17:44:12 +02:00
return m_indexed_properties . remove ( property_name . as_number ( ) ) ;
2020-11-04 23:31:47 +00:00
if ( property_name . is_string ( ) ) {
i32 property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
2021-04-10 17:44:12 +02:00
return m_indexed_properties . remove ( property_index ) ;
2020-11-04 23:31:47 +00:00
}
2020-06-07 10:53:14 -07:00
2020-07-07 21:38:46 -07:00
auto metadata = shape ( ) . lookup ( property_name . to_string_or_symbol ( ) ) ;
2020-04-26 13:53:40 +02:00
if ( ! metadata . has_value ( ) )
2021-04-10 17:44:12 +02:00
return true ;
2020-06-02 17:13:09 -07:00
if ( ! metadata . value ( ) . attributes . is_configurable ( ) )
2021-04-10 17:44:12 +02:00
return false ;
2020-04-26 13:53:40 +02:00
size_t deleted_offset = metadata . value ( ) . offset ;
ensure_shape_is_unique ( ) ;
2020-07-07 21:38:46 -07:00
shape ( ) . remove_property_from_unique_shape ( property_name . to_string_or_symbol ( ) , deleted_offset ) ;
2020-04-26 13:53:40 +02:00
m_storage . remove ( deleted_offset ) ;
2021-04-10 17:44:12 +02:00
return true ;
2020-04-26 13:53:40 +02:00
}
void Object : : ensure_shape_is_unique ( )
{
if ( shape ( ) . is_unique ( ) )
return ;
m_shape = m_shape - > create_unique_clone ( ) ;
}
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
Value Object : : get_by_index ( u32 property_index ) const
2020-04-06 16:53:02 +02:00
{
const Object * object = this ;
while ( object ) {
2021-04-11 17:16:00 +02:00
if ( is < StringObject > ( * object ) ) {
auto & string = static_cast < const StringObject & > ( * object ) . primitive_string ( ) . string ( ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( property_index < string . length ( ) )
2020-05-01 13:20:47 +01:00
return js_string ( heap ( ) , string . substring ( property_index , 1 ) ) ;
2021-04-11 17:16:00 +02:00
} else if ( static_cast < size_t > ( property_index ) < object - > m_indexed_properties . array_like_size ( ) ) {
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
auto result = object - > m_indexed_properties . get ( const_cast < Object * > ( this ) , property_index ) ;
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-04-06 20:24:45 +02:00
return { } ;
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
if ( result . has_value ( ) & & ! result . value ( ) . value . is_empty ( ) )
return result . value ( ) . value ;
2020-04-06 20:24:45 +02:00
}
2020-04-06 16:53:02 +02:00
object = object - > prototype ( ) ;
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
2020-04-06 16:53:02 +02:00
}
return { } ;
}
2021-04-11 22:52:25 +02:00
Value Object : : get ( const PropertyName & property_name , Value receiver , bool without_side_effects ) const
2020-03-07 19:42:11 +01:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
2020-11-04 23:29:45 +00:00
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( property_name . is_number ( ) )
return get_by_index ( property_name . as_number ( ) ) ;
2020-07-07 21:38:46 -07:00
if ( property_name . is_string ( ) ) {
2021-06-05 12:57:29 +02:00
auto & property_string = property_name . as_string ( ) ;
2020-07-07 21:38:46 -07:00
i32 property_index = property_string . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
return get_by_index ( property_index ) ;
}
2020-04-06 16:53:02 +02:00
2021-03-16 21:59:49 +01:00
if ( receiver . is_empty ( ) )
receiver = Value ( this ) ;
2020-03-15 15:01:10 +01:00
const Object * object = this ;
while ( object ) {
2021-04-11 22:52:25 +02:00
auto value = object - > get_own_property ( property_name , receiver , without_side_effects ) ;
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
2020-04-25 18:43:34 +02:00
if ( ! value . is_empty ( ) )
return value ;
2020-03-15 15:01:10 +01:00
object = object - > prototype ( ) ;
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
2020-03-15 15:01:10 +01:00
}
2020-03-27 12:54:18 +01:00
return { } ;
2020-03-07 19:42:11 +01:00
}
2021-04-11 22:52:25 +02:00
Value Object : : get_without_side_effects ( const PropertyName & property_name ) const
{
2021-04-24 17:59:28 +02:00
TemporaryClearException clear_exception ( vm ( ) ) ;
2021-04-11 22:52:25 +02:00
return get ( property_name , { } , true ) ;
}
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
bool Object : : put_by_index ( u32 property_index , Value value )
2020-04-06 16:53:02 +02:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( ! value . is_empty ( ) ) ;
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
// If there's a setter in the prototype chain, we go to the setter.
// Otherwise, it goes in the own property storage.
Object * object = this ;
while ( object ) {
auto existing_value = object - > m_indexed_properties . get ( nullptr , property_index , false ) ;
if ( existing_value . has_value ( ) ) {
auto value_here = existing_value . value ( ) ;
if ( value_here . value . is_accessor ( ) ) {
value_here . value . as_accessor ( ) . call_setter ( object , value ) ;
return true ;
}
2020-06-23 17:56:57 +02:00
if ( value_here . value . is_native_property ( ) ) {
2020-11-22 17:29:25 +00:00
// FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
auto receiver = this ;
call_native_property_setter ( value_here . value . as_native_property ( ) , receiver , value ) ;
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
return true ;
}
}
object = object - > prototype ( ) ;
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
}
2021-04-05 18:04:55 +02:00
return put_own_property_by_index ( property_index , value , default_attributes , PutOwnPropertyMode : : Put ) ;
2020-04-06 16:53:02 +02:00
}
2020-07-07 21:38:46 -07:00
bool Object : : put ( const PropertyName & property_name , Value value , Value receiver )
2020-03-07 19:42:11 +01:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
2020-11-04 23:29:45 +00:00
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
if ( property_name . is_number ( ) )
return put_by_index ( property_name . as_number ( ) , value ) ;
2021-02-23 20:42:32 +01:00
VERIFY ( ! value . is_empty ( ) ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
2020-07-07 21:38:46 -07:00
if ( property_name . is_string ( ) ) {
auto & property_string = property_name . as_string ( ) ;
i32 property_index = property_string . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
return put_by_index ( property_index , value ) ;
}
auto string_or_symbol = property_name . to_string_or_symbol ( ) ;
2020-04-06 16:53:02 +02:00
2020-11-22 17:29:25 +00:00
if ( receiver . is_empty ( ) )
receiver = Value ( this ) ;
2020-04-02 21:34:31 +02:00
// If there's a setter in the prototype chain, we go to the setter.
// Otherwise, it goes in the own property storage.
2020-03-19 17:39:13 +01:00
Object * object = this ;
while ( object ) {
2020-07-07 21:38:46 -07:00
auto metadata = object - > shape ( ) . lookup ( string_or_symbol ) ;
2020-04-02 19:32:21 +02:00
if ( metadata . has_value ( ) ) {
auto value_here = object - > m_storage [ metadata . value ( ) . offset ] ;
2020-05-21 11:14:23 -07:00
if ( value_here . is_accessor ( ) ) {
2020-06-25 14:00:13 +01:00
value_here . as_accessor ( ) . call_setter ( receiver , value ) ;
2020-05-21 11:14:23 -07:00
return true ;
}
2020-06-23 17:56:57 +02:00
if ( value_here . is_native_property ( ) ) {
2020-11-22 17:29:25 +00:00
call_native_property_setter ( value_here . as_native_property ( ) , receiver , value ) ;
2020-04-30 20:01:54 +01:00
return true ;
2020-03-19 17:39:13 +01:00
}
}
object = object - > prototype ( ) ;
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-08-14 17:31:07 +02:00
return false ;
2020-03-19 17:39:13 +01:00
}
2021-04-05 18:04:55 +02:00
return put_own_property ( string_or_symbol , value , default_attributes , PutOwnPropertyMode : : Put ) ;
2020-04-06 17:08:23 +02:00
}
2020-09-27 18:36:49 +02:00
bool Object : : define_native_function ( const StringOrSymbol & property_name , AK : : Function < Value ( VM & , GlobalObject & ) > native_function , i32 length , PropertyAttributes attribute )
2020-03-13 11:06:32 +01:00
{
2020-10-13 23:49:19 +02:00
auto & vm = this - > vm ( ) ;
2020-07-07 21:38:46 -07:00
String function_name ;
if ( property_name . is_string ( ) ) {
function_name = property_name . as_string ( ) ;
} else {
2020-10-04 15:18:52 +01:00
function_name = String : : formatted ( " [{}] " , property_name . as_symbol ( ) - > description ( ) ) ;
2020-07-07 21:38:46 -07:00
}
2020-09-20 19:11:11 +02:00
auto * function = NativeFunction : : create ( global_object ( ) , function_name , move ( native_function ) ) ;
2020-10-13 23:49:19 +02:00
function - > define_property_without_transition ( vm . names . length , Value ( length ) , Attribute : : Configurable ) ;
if ( vm . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
2020-10-13 23:49:19 +02:00
function - > define_property_without_transition ( vm . names . name , js_string ( vm . heap ( ) , function_name ) , Attribute : : Configurable ) ;
if ( vm . exception ( ) )
2020-06-07 10:53:14 -07:00
return { } ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
return define_property ( property_name , function , attribute ) ;
2020-03-13 11:06:32 +01:00
}
2020-09-27 18:36:49 +02:00
bool Object : : define_native_property ( const StringOrSymbol & property_name , AK : : Function < Value ( VM & , GlobalObject & ) > getter , AK : : Function < void ( VM & , GlobalObject & , Value ) > setter , PropertyAttributes attribute )
2020-03-15 18:15:44 +01:00
{
2020-07-24 20:46:55 +02:00
return define_property ( property_name , heap ( ) . allocate_without_global_object < NativeProperty > ( move ( getter ) , move ( setter ) ) , attribute ) ;
2020-03-15 18:15:44 +01:00
}
2021-04-10 18:39:11 +02:00
// 20.1.2.3.1 ObjectDefineProperties, https://tc39.es/ecma262/#sec-objectdefineproperties
void Object : : define_properties ( Value properties )
{
auto & vm = this - > vm ( ) ;
auto * props = properties . to_object ( global_object ( ) ) ;
if ( ! props )
return ;
auto keys = props - > get_own_properties ( PropertyKind : : Key ) ;
if ( vm . exception ( ) )
return ;
struct NameAndDescriptor {
PropertyName name ;
PropertyDescriptor descriptor ;
} ;
Vector < NameAndDescriptor > descriptors ;
for ( auto & key : keys ) {
auto property_name = PropertyName : : from_value ( global_object ( ) , key ) ;
auto property_descriptor = props - > get_own_property_descriptor ( property_name ) ;
if ( property_descriptor . has_value ( ) & & property_descriptor - > attributes . is_enumerable ( ) ) {
auto descriptor_object = props - > get ( property_name ) ;
if ( vm . exception ( ) )
return ;
if ( ! descriptor_object . is_object ( ) ) {
vm . throw_exception < TypeError > ( global_object ( ) , ErrorType : : NotAnObject , descriptor_object . to_string_without_side_effects ( ) ) ;
return ;
}
auto descriptor = PropertyDescriptor : : from_dictionary ( vm , descriptor_object . as_object ( ) ) ;
if ( vm . exception ( ) )
return ;
descriptors . append ( { property_name , descriptor } ) ;
}
}
for ( auto & [ name , descriptor ] : descriptors ) {
// FIXME: The spec has both of this handled by DefinePropertyOrThrow(O, P, desc).
// We should invest some time in improving object property handling, it not being
// super close to the spec makes this and other things unnecessarily complicated.
if ( descriptor . is_accessor_descriptor ( ) )
define_accessor ( name , descriptor . getter , descriptor . setter , descriptor . attributes ) ;
else
define_property ( name , descriptor . value , descriptor . attributes ) ;
}
}
2020-11-28 14:33:36 +01:00
void Object : : visit_edges ( Cell : : Visitor & visitor )
2020-03-08 19:23:58 +01:00
{
2020-11-28 14:33:36 +01:00
Cell : : visit_edges ( visitor ) ;
2020-04-02 19:32:21 +02:00
visitor . visit ( m_shape ) ;
2020-04-06 12:33:27 +02:00
for ( auto & value : m_storage )
visitor . visit ( value ) ;
2020-04-06 16:53:02 +02:00
2020-12-08 13:33:01 +01:00
m_indexed_properties . for_each_value ( [ & visitor ] ( auto & value ) {
visitor . visit ( value ) ;
} ) ;
2020-03-08 19:23:58 +01:00
}
2020-07-07 21:38:46 -07:00
bool Object : : has_property ( const PropertyName & property_name ) const
2020-04-30 20:03:40 +01:00
{
const Object * object = this ;
while ( object ) {
if ( object - > has_own_property ( property_name ) )
return true ;
object = object - > prototype ( ) ;
2020-09-21 16:46:45 +02:00
if ( vm ( ) . exception ( ) )
2020-06-07 10:53:14 -07:00
return false ;
2020-04-30 20:03:40 +01:00
}
return false ;
}
2020-07-07 21:38:46 -07:00
bool Object : : has_own_property ( const PropertyName & property_name ) const
2020-03-15 15:25:43 +01:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( property_name . is_valid ( ) ) ;
2020-11-04 23:29:45 +00:00
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
auto has_indexed_property = [ & ] ( u32 index ) - > bool {
2021-01-01 17:46:39 +01:00
if ( is < StringObject > ( * this ) )
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
return index < static_cast < const StringObject * > ( this ) - > primitive_string ( ) . string ( ) . length ( ) ;
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
return m_indexed_properties . has_index ( index ) ;
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
} ;
if ( property_name . is_number ( ) )
return has_indexed_property ( property_name . as_number ( ) ) ;
2020-07-07 21:38:46 -07:00
if ( property_name . is_string ( ) ) {
i32 property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
return has_indexed_property ( property_index ) ;
}
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
2020-07-07 21:38:46 -07:00
return shape ( ) . lookup ( property_name . to_string_or_symbol ( ) ) . has_value ( ) ;
2020-03-15 15:25:43 +01:00
}
2020-11-03 19:52:21 +00:00
Value Object : : ordinary_to_primitive ( Value : : PreferredType preferred_type ) const
2020-03-16 00:19:41 +02:00
{
2021-02-23 20:42:32 +01:00
VERIFY ( preferred_type = = Value : : PreferredType : : String | | preferred_type = = Value : : PreferredType : : Number ) ;
2020-03-16 00:19:41 +02:00
2020-11-03 19:52:21 +00:00
auto & vm = this - > vm ( ) ;
2020-03-16 00:19:41 +02:00
2020-11-03 19:52:21 +00:00
Vector < FlyString , 2 > method_names ;
if ( preferred_type = = Value : : PreferredType : : String )
method_names = { vm . names . toString , vm . names . valueOf } ;
else
method_names = { vm . names . valueOf , vm . names . toString } ;
2020-03-16 00:19:41 +02:00
2020-11-03 19:52:21 +00:00
for ( auto & method_name : method_names ) {
auto method = get ( method_name ) ;
2020-09-27 19:57:16 +02:00
if ( vm . exception ( ) )
2020-05-15 13:39:24 +02:00
return { } ;
2020-11-03 19:52:21 +00:00
if ( method . is_function ( ) ) {
auto result = vm . call ( method . as_function ( ) , const_cast < Object * > ( this ) ) ;
if ( ! result . is_object ( ) )
return result ;
}
2020-04-05 18:18:24 +02:00
}
2020-11-03 19:52:21 +00:00
vm . throw_exception < TypeError > ( global_object ( ) , ErrorType : : Convert , " object " , preferred_type = = Value : : PreferredType : : String ? " string " : " number " ) ;
return { } ;
2020-03-16 00:19:41 +02:00
}
2020-04-05 18:18:24 +02:00
2021-03-14 12:01:18 +01:00
Value Object : : invoke_internal ( const StringOrSymbol & property_name , Optional < MarkedValueList > arguments )
2020-05-28 19:12:34 +01:00
{
2020-09-27 19:57:16 +02:00
auto & vm = this - > vm ( ) ;
2020-05-28 19:12:34 +01:00
auto property = get ( property_name ) . value_or ( js_undefined ( ) ) ;
2020-09-27 19:57:16 +02:00
if ( vm . exception ( ) )
2020-05-28 19:12:34 +01:00
return { } ;
if ( ! property . is_function ( ) ) {
2020-10-04 13:55:20 +01:00
vm . throw_exception < TypeError > ( global_object ( ) , ErrorType : : NotAFunction , property . to_string_without_side_effects ( ) ) ;
2020-05-28 19:12:34 +01:00
return { } ;
}
2020-09-27 19:57:16 +02:00
return vm . call ( property . as_function ( ) , this , move ( arguments ) ) ;
2020-05-28 19:12:34 +01:00
}
2020-11-22 17:29:25 +00:00
Value Object : : call_native_property_getter ( NativeProperty & property , Value this_value ) const
2020-05-29 20:10:21 -07:00
{
2020-11-08 12:54:52 +00:00
auto & vm = this - > vm ( ) ;
2020-11-07 11:07:17 +01:00
CallFrame call_frame ;
2021-03-21 12:18:56 +01:00
if ( auto * interpreter = vm . interpreter_if_exists ( ) )
call_frame . current_node = interpreter - > current_node ( ) ;
2020-11-08 12:54:52 +00:00
call_frame . is_strict_mode = vm . in_strict_mode ( ) ;
2020-11-22 17:29:25 +00:00
call_frame . this_value = this_value ;
2020-11-08 12:54:52 +00:00
vm . push_call_frame ( call_frame , global_object ( ) ) ;
if ( vm . exception ( ) )
return { } ;
2020-11-22 17:04:40 +00:00
auto result = property . get ( vm , global_object ( ) ) ;
2020-11-08 12:54:52 +00:00
vm . pop_call_frame ( ) ;
2020-05-29 20:10:21 -07:00
return result ;
}
2020-11-22 17:29:25 +00:00
void Object : : call_native_property_setter ( NativeProperty & property , Value this_value , Value setter_value ) const
2020-05-29 20:10:21 -07:00
{
2020-11-08 12:54:52 +00:00
auto & vm = this - > vm ( ) ;
2020-11-07 11:07:17 +01:00
CallFrame call_frame ;
2021-03-21 12:18:56 +01:00
if ( auto * interpreter = vm . interpreter_if_exists ( ) )
call_frame . current_node = interpreter - > current_node ( ) ;
2020-11-08 12:54:52 +00:00
call_frame . is_strict_mode = vm . in_strict_mode ( ) ;
2020-11-22 17:29:25 +00:00
call_frame . this_value = this_value ;
2020-11-08 12:54:52 +00:00
vm . push_call_frame ( call_frame , global_object ( ) ) ;
if ( vm . exception ( ) )
return ;
2020-11-22 17:29:25 +00:00
property . set ( vm , global_object ( ) , setter_value ) ;
2020-11-08 12:54:52 +00:00
vm . pop_call_frame ( ) ;
2020-05-29 20:10:21 -07:00
}
2020-03-07 19:42:11 +01:00
}