2020-03-07 19:42:11 +01:00
/*
* Copyright ( c ) 2020 , Andreas Kling < kling @ serenityos . org >
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
*
* 1. Redistributions of source code must retain the above copyright notice , this
* list of conditions and the following disclaimer .
*
* 2. Redistributions in binary form must reproduce the above copyright notice ,
* this list of conditions and the following disclaimer in the documentation
* and / or other materials provided with the distribution .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS "
* AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL
* DAMAGES ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY ,
* OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
# include <AK/String.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Heap/Heap.h>
2020-03-15 15:25:43 +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>
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>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/Value.h>
2020-03-07 19:42:11 +01:00
namespace JS {
2020-06-03 14:34:52 -07:00
PropertyDescriptor PropertyDescriptor : : from_dictionary ( Interpreter & interpreter , const Object & object )
2020-06-03 09:40:17 -07:00
{
PropertyAttributes attributes ;
if ( object . has_property ( " configurable " ) ) {
attributes . set_has_configurable ( ) ;
if ( object . get ( " configurable " ) . value_or ( Value ( false ) ) . to_boolean ( ) )
attributes . set_configurable ( ) ;
if ( interpreter . exception ( ) )
return { } ;
}
if ( object . has_property ( " enumerable " ) ) {
attributes . set_has_enumerable ( ) ;
if ( object . get ( " enumerable " ) . value_or ( Value ( false ) ) . to_boolean ( ) )
attributes . set_enumerable ( ) ;
if ( interpreter . exception ( ) )
return { } ;
}
if ( object . has_property ( " writable " ) ) {
attributes . set_has_writable ( ) ;
if ( object . get ( " writable " ) . value_or ( Value ( false ) ) . to_boolean ( ) )
attributes . set_writable ( ) ;
if ( interpreter . exception ( ) )
return { } ;
}
PropertyDescriptor descriptor { attributes , object . get ( " value " ) , nullptr , nullptr } ;
2020-06-07 10:53:14 -07:00
if ( interpreter . exception ( ) )
return { } ;
2020-06-03 09:40:17 -07:00
auto getter = object . get ( " get " ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter . exception ( ) )
return { } ;
2020-06-03 09:40:17 -07:00
if ( getter . is_function ( ) )
descriptor . getter = & getter . as_function ( ) ;
auto setter = object . get ( " set " ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter . exception ( ) )
return { } ;
2020-06-03 09:40:17 -07:00
if ( setter . is_function ( ) )
descriptor . setter = & setter . as_function ( ) ;
return descriptor ;
}
2020-04-18 13:18:06 +02:00
Object * Object : : create_empty ( Interpreter & , GlobalObject & global_object )
2020-04-18 10:27:57 +02:00
{
2020-06-20 15:40:48 +02:00
return global_object . heap ( ) . allocate < Object > ( global_object , global_object . object_prototype ( ) ) ;
2020-04-18 10:27:57 +02:00
}
Object : : Object ( Object * prototype )
2020-03-08 19:23:58 +01:00
{
2020-04-18 13:56:13 +02:00
if ( prototype ) {
m_shape = interpreter ( ) . global_object ( ) . empty_object_shape ( ) ;
set_prototype ( prototype ) ;
} else {
2020-06-08 12:25:45 +02:00
// This is the global object
2020-06-20 15:40:48 +02:00
m_shape = interpreter ( ) . heap ( ) . allocate < Shape > ( static_cast < GlobalObject & > ( * this ) , static_cast < GlobalObject & > ( * this ) ) ;
2020-04-18 13:56:13 +02:00
}
2020-03-08 19:23:58 +01:00
}
2020-06-20 15:40:48 +02:00
void Object : : initialize ( Interpreter & , GlobalObject & )
{
}
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 ( ) ;
}
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 ;
2020-04-26 13:53:40 +02:00
if ( shape ( ) . is_unique ( ) ) {
shape ( ) . set_prototype_without_transition ( new_prototype ) ;
2020-06-02 12:32:54 +01:00
return true ;
2020-04-26 13:53:40 +02:00
}
2020-04-02 19:32:21 +02:00
m_shape = m_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-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
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 ;
}
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_own_property ( const Object & this_object , PropertyName property_name ) const
2020-03-21 14:37:34 +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
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 {
auto metadata = shape ( ) . lookup ( property_name . as_string ( ) ) ;
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
2020-04-06 20:24:45 +02:00
ASSERT ( ! value_here . is_empty ( ) ) ;
2020-05-21 11:14:23 -07:00
if ( value_here . is_accessor ( ) ) {
return value_here . as_accessor ( ) . call_getter ( Value ( const_cast < Object * > ( this ) ) ) ;
}
2020-05-29 20:10:21 -07:00
if ( value_here . is_object ( ) & & value_here . as_object ( ) . is_native_property ( ) )
return call_native_property_getter ( const_cast < Object * > ( & this_object ) , value_here ) ;
2020-03-21 14:37:34 +01:00
return value_here ;
}
2020-06-03 14:34:52 -07:00
Value Object : : get_own_properties ( const Object & this_object , GetOwnPropertyMode kind , bool only_enumerable_properties ) const
2020-04-29 18:59:23 -07:00
{
2020-06-08 12:25:45 +02:00
auto * properties_array = Array : : create ( global_object ( ) ) ;
2020-04-29 18:59:23 -07:00
// FIXME: Support generic iterables
if ( this_object . is_string_object ( ) ) {
auto str = static_cast < const StringObject & > ( this_object ) . primitive_string ( ) . string ( ) ;
for ( size_t i = 0 ; i < str . length ( ) ; + + i ) {
if ( kind = = GetOwnPropertyMode : : Key ) {
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
properties_array - > define_property ( i , js_string ( interpreter ( ) , String : : number ( i ) ) ) ;
2020-04-29 18:59:23 -07:00
} else if ( kind = = GetOwnPropertyMode : : 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
properties_array - > define_property ( i , js_string ( interpreter ( ) , String : : format ( " %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 ( ) ) ;
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
entry_array - > define_property ( 0 , js_string ( interpreter ( ) , String : : number ( i ) ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
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
entry_array - > define_property ( 1 , js_string ( interpreter ( ) , String : : format ( " %c " , str [ i ] ) ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
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
properties_array - > define_property ( i , entry_array ) ;
2020-04-29 18:59:23 -07:00
}
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-04-29 18:59:23 -07:00
}
return properties_array ;
}
size_t property_index = 0 ;
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 ) {
2020-06-03 14:34:52 -07:00
auto value_and_attributes = entry . value_and_attributes ( const_cast < Object * > ( & this_object ) ) ;
if ( only_enumerable_properties & & ! value_and_attributes . attributes . is_enumerable ( ) )
continue ;
2020-04-29 18:59:23 -07:00
if ( kind = = GetOwnPropertyMode : : Key ) {
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
properties_array - > define_property ( property_index , js_string ( interpreter ( ) , String : : number ( entry . index ( ) ) ) ) ;
2020-04-29 18:59:23 -07:00
} else if ( kind = = GetOwnPropertyMode : : Value ) {
2020-06-03 14:34:52 -07:00
properties_array - > define_property ( property_index , 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 ( ) ) ;
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
entry_array - > define_property ( 0 , js_string ( interpreter ( ) , String : : number ( entry . index ( ) ) ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-06-03 14:34:52 -07:00
entry_array - > define_property ( 1 , value_and_attributes . 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
if ( interpreter ( ) . exception ( ) )
return { } ;
properties_array - > define_property ( property_index , entry_array ) ;
2020-04-29 18:59:23 -07:00
}
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-04-29 18:59:23 -07:00
+ + property_index ;
}
for ( auto & it : this_object . shape ( ) . property_table_ordered ( ) ) {
2020-06-03 14:34:52 -07:00
if ( only_enumerable_properties & & ! it . value . attributes . is_enumerable ( ) )
continue ;
2020-04-29 18:59:23 -07:00
2020-06-03 14:34:52 -07:00
size_t offset = it . value . offset + property_index ;
if ( kind = = GetOwnPropertyMode : : Key ) {
properties_array - > define_property ( offset , js_string ( interpreter ( ) , it . key ) ) ;
} else if ( kind = = GetOwnPropertyMode : : Value ) {
properties_array - > define_property ( offset , this_object . get ( it . key ) ) ;
} else {
2020-06-08 12:25:45 +02:00
auto * entry_array = Array : : create ( global_object ( ) ) ;
2020-06-03 14:34:52 -07:00
entry_array - > define_property ( 0 , js_string ( interpreter ( ) , it . key ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-06-03 14:34:52 -07:00
entry_array - > define_property ( 1 , this_object . get ( it . key ) ) ;
if ( interpreter ( ) . exception ( ) )
return { } ;
properties_array - > define_property ( offset , entry_array ) ;
2020-04-29 18:59:23 -07:00
}
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-04-29 18:59:23 -07:00
}
return properties_array ;
}
2020-06-03 09:40:17 -07:00
Optional < PropertyDescriptor > Object : : get_own_property_descriptor ( PropertyName property_name ) const
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
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
attributes = default_attributes ;
} else {
auto metadata = shape ( ) . lookup ( property_name . as_string ( ) ) ;
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
if ( interpreter ( ) . exception ( ) )
return { } ;
attributes = metadata . value ( ) . attributes ;
}
2020-06-03 09:40:17 -07:00
PropertyDescriptor descriptor { attributes , { } , nullptr , nullptr } ;
2020-05-29 20:10:21 -07:00
if ( value . is_object ( ) & & value . as_object ( ) . is_native_property ( ) ) {
auto result = call_native_property_getter ( const_cast < Object * > ( this ) , value ) ;
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 ;
}
2020-06-03 09:40:17 -07:00
Value Object : : get_own_property_descriptor_object ( PropertyName property_name ) const
{
auto descriptor_opt = get_own_property_descriptor ( property_name ) ;
if ( ! descriptor_opt . has_value ( ) )
return js_undefined ( ) ;
auto descriptor = descriptor_opt . value ( ) ;
2020-06-08 12:25:45 +02:00
auto * descriptor_object = Object : : create_empty ( interpreter ( ) , global_object ( ) ) ;
2020-06-03 09:40:17 -07:00
descriptor_object - > define_property ( " enumerable " , Value ( descriptor . attributes . is_enumerable ( ) ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-06-03 09:40:17 -07:00
descriptor_object - > define_property ( " configurable " , Value ( descriptor . attributes . is_configurable ( ) ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-06-03 09:40:17 -07:00
if ( descriptor . is_data_descriptor ( ) ) {
descriptor_object - > define_property ( " value " , descriptor . value . value_or ( js_undefined ( ) ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-06-03 09:40:17 -07:00
descriptor_object - > define_property ( " writable " , Value ( descriptor . attributes . is_writable ( ) ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-06-03 09:40:17 -07:00
} else if ( descriptor . is_accessor_descriptor ( ) ) {
if ( descriptor . getter ) {
descriptor_object - > define_property ( " get " , Value ( descriptor . getter ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-06-03 09:40:17 -07:00
}
2020-06-07 10:53:14 -07:00
if ( descriptor . setter ) {
2020-06-03 09:40:17 -07:00
descriptor_object - > define_property ( " set " , Value ( descriptor . setter ) ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
}
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-05-01 11:06:27 +01:00
bool Object : : define_property ( const FlyString & property_name , const Object & descriptor , bool throw_exceptions )
{
2020-05-21 11:14:23 -07:00
bool is_accessor_property = descriptor . has_property ( " get " ) | | descriptor . has_property ( " set " ) ;
2020-06-02 17:13:09 -07:00
PropertyAttributes attributes ;
if ( descriptor . has_property ( " configurable " ) ) {
attributes . set_has_configurable ( ) ;
if ( descriptor . get ( " configurable " ) . value_or ( Value ( false ) ) . to_boolean ( ) )
attributes . set_configurable ( ) ;
if ( interpreter ( ) . exception ( ) )
return false ;
}
if ( descriptor . has_property ( " enumerable " ) ) {
attributes . set_has_enumerable ( ) ;
if ( descriptor . get ( " enumerable " ) . value_or ( Value ( false ) ) . to_boolean ( ) )
attributes . set_enumerable ( ) ;
if ( interpreter ( ) . exception ( ) )
return false ;
}
2020-05-21 11:14:23 -07:00
if ( is_accessor_property ) {
if ( descriptor . has_property ( " value " ) | | descriptor . has_property ( " writable " ) ) {
if ( throw_exceptions )
2020-06-09 22:48:01 -07:00
interpreter ( ) . throw_exception < TypeError > ( ErrorType : : AccessorValueOrWritable ) ;
2020-05-21 11:14:23 -07:00
return false ;
}
2020-05-23 23:27:10 +01:00
auto getter = descriptor . get ( " get " ) . value_or ( js_undefined ( ) ) ;
2020-05-21 11:14:23 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-05-23 23:27:10 +01:00
auto setter = descriptor . get ( " set " ) . value_or ( js_undefined ( ) ) ;
2020-05-21 11:14:23 -07:00
if ( interpreter ( ) . exception ( ) )
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-06-09 22:48:01 -07:00
interpreter ( ) . throw_exception < TypeError > ( 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-06-09 22:48:01 -07:00
interpreter ( ) . throw_exception < TypeError > ( ErrorType : : AccessorBadField , " set " ) ;
2020-05-21 11:14:23 -07:00
return false ;
}
2020-05-23 23:27:10 +01:00
dbg ( ) < < " Defining new property " < < property_name < < " with accessor descriptor { attributes= " < < attributes < < " , "
< < " getter= " < < getter . to_string_without_side_effects ( ) < < " , "
< < " setter= " < < setter . to_string_without_side_effects ( ) < < " } " ;
2020-05-21 11:14:23 -07:00
2020-06-20 16:11:17 +02:00
return define_property ( property_name , Accessor : : create ( interpreter ( ) , global_object ( ) , getter_function , setter_function ) , attributes , throw_exceptions ) ;
2020-05-21 11:14:23 -07:00
}
auto value = descriptor . get ( " value " ) ;
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-06-02 17:13:09 -07:00
if ( descriptor . has_property ( " writable " ) ) {
attributes . set_has_writable ( ) ;
if ( descriptor . get ( " writable " ) . value_or ( Value ( false ) ) . to_boolean ( ) )
attributes . set_writable ( ) ;
if ( interpreter ( ) . exception ( ) )
return false ;
}
2020-05-21 11:14:23 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-05-01 11:06:27 +01:00
2020-05-21 11:14:23 -07:00
dbg ( ) < < " Defining new property " < < property_name < < " with data descriptor { attributes= " < < attributes
< < " , value= " < < ( value . is_empty ( ) ? " <empty> " : value . to_string_without_side_effects ( ) ) < < " } " ;
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-06-02 17:13:09 -07:00
bool Object : : define_property ( 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
{
if ( property_name . is_number ( ) )
return put_own_property_by_index ( * this , property_name . as_number ( ) , value , attributes , PutOwnPropertyMode : : DefineProperty , throw_exceptions ) ;
2020-06-12 21:07:52 +02:00
i32 property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
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 put_own_property_by_index ( * this , property_index , value , attributes , PutOwnPropertyMode : : DefineProperty , throw_exceptions ) ;
return put_own_property ( * this , property_name . as_string ( ) , value , attributes , PutOwnPropertyMode : : DefineProperty , throw_exceptions ) ;
}
2020-06-02 17:13:09 -07:00
bool Object : : put_own_property ( Object & this_object , const FlyString & property_name , Value value , PropertyAttributes attributes , PutOwnPropertyMode mode , bool throw_exceptions )
2020-03-21 14:37:34 +01:00
{
2020-05-21 11:14:23 -07:00
ASSERT ( ! ( mode = = PutOwnPropertyMode : : Put & & value . is_accessor ( ) ) ) ;
2020-06-01 21:13:16 -07:00
if ( ! is_extensible ( ) ) {
dbg ( ) < < " Disallow define_property of non-extensible object " ;
if ( throw_exceptions & & interpreter ( ) . in_strict_mode ( ) )
2020-06-09 22:48:01 -07:00
interpreter ( ) . throw_exception < TypeError > ( ErrorType : : NonExtensibleDefine , property_name . characters ( ) ) ;
2020-06-01 21:13:16 -07:00
return false ;
}
2020-05-21 11:14:23 -07:00
if ( value . is_accessor ( ) ) {
auto & accessor = value . as_accessor ( ) ;
2020-05-23 23:27:10 +01:00
if ( accessor . getter ( ) )
2020-06-02 17:13:09 -07:00
attributes . set_has_getter ( ) ;
2020-05-23 23:27:10 +01:00
if ( accessor . setter ( ) )
2020-06-02 17:13:09 -07:00
attributes . set_has_setter ( ) ;
2020-05-21 11:14:23 -07:00
}
2020-04-02 19:32:21 +02:00
auto metadata = shape ( ) . lookup ( property_name ) ;
2020-04-27 23:05:02 -07:00
bool new_property = ! metadata . has_value ( ) ;
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-04-26 13:53:40 +02:00
} else {
set_shape ( * m_shape - > create_put_transition ( property_name , attributes ) ) ;
}
2020-04-02 19:32:21 +02:00
metadata = shape ( ) . lookup ( property_name ) ;
ASSERT ( 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 ) {
2020-04-09 22:15:26 +02:00
dbg ( ) < < " Disallow reconfig of non-configurable property " ;
2020-05-01 11:06:27 +01:00
if ( throw_exceptions )
2020-06-09 22:48:01 -07:00
interpreter ( ) . throw_exception < TypeError > ( ErrorType : : DescChangeNonConfigurable , property_name . characters ( ) ) ;
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 ) ;
dbg ( ) < < " Reconfigured property " < < property_name < < " , new shape says offset is " < < metadata . value ( ) . offset < < " and my storage capacity is " < < m_storage . size ( ) ;
}
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 ( ) ) {
2020-04-09 22:55:17 +02:00
dbg ( ) < < " Disallow write to non-writable property " ;
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-04-02 19:32:21 +02:00
if ( value_here . is_object ( ) & & value_here . as_object ( ) . is_native_property ( ) ) {
2020-05-29 20:10:21 -07:00
call_native_property_setter ( const_cast < Object * > ( & this_object ) , value_here , 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
}
2020-06-02 17:13:09 -07:00
bool Object : : put_own_property_by_index ( Object & this_object , 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
{
ASSERT ( ! ( mode = = PutOwnPropertyMode : : Put & & value . is_accessor ( ) ) ) ;
2020-06-01 21:13:16 -07:00
if ( ! is_extensible ( ) ) {
dbg ( ) < < " Disallow define_property of non-extensible object " ;
if ( throw_exceptions & & interpreter ( ) . in_strict_mode ( ) )
2020-06-09 22:48:01 -07:00
interpreter ( ) . throw_exception < TypeError > ( 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
}
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_index , false ) ;
auto new_property = ! existing_property . has_value ( ) ;
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 ) {
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
dbg ( ) < < " Disallow reconfig of non-configurable property " ;
if ( throw_exceptions )
2020-06-09 22:48:01 -07:00
interpreter ( ) . throw_exception < TypeError > ( 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 ( ) ) {
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
dbg ( ) < < " Disallow write to non-writable property " ;
return false ;
}
if ( value . is_empty ( ) )
return true ;
if ( value_here . is_object ( ) & & value_here . as_object ( ) . is_native_property ( ) ) {
2020-05-29 20:10:21 -07:00
call_native_property_setter ( const_cast < Object * > ( & this_object ) , value_here , 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 {
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
m_indexed_properties . put ( & this_object , 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 ;
}
2020-04-26 13:53:40 +02:00
Value Object : : delete_property ( PropertyName property_name )
{
ASSERT ( property_name . is_valid ( ) ) ;
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 ( ) )
return Value ( m_indexed_properties . remove ( property_name . as_number ( ) ) ) ;
2020-06-12 21:07:52 +02:00
int property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
2020-06-07 10:53:14 -07:00
return Value ( m_indexed_properties . remove ( property_name . as_number ( ) ) ) ;
2020-04-26 13:53:40 +02:00
auto metadata = shape ( ) . lookup ( property_name . as_string ( ) ) ;
if ( ! metadata . has_value ( ) )
return Value ( true ) ;
2020-06-02 17:13:09 -07:00
if ( ! metadata . value ( ) . attributes . is_configurable ( ) )
2020-04-26 13:53:40 +02:00
return Value ( false ) ;
size_t deleted_offset = metadata . value ( ) . offset ;
ensure_shape_is_unique ( ) ;
shape ( ) . remove_property_from_unique_shape ( property_name . as_string ( ) , deleted_offset ) ;
m_storage . remove ( deleted_offset ) ;
return Value ( true ) ;
}
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 ) {
2020-05-01 13:20:47 +01:00
if ( is_string_object ( ) ) {
auto & string = static_cast < const StringObject * > ( this ) - > 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 ) ) ;
return js_undefined ( ) ;
}
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 ( static_cast < size_t > ( property_index ) < object - > m_indexed_properties . array_like_size ( ) ) {
auto result = object - > m_indexed_properties . get ( const_cast < Object * > ( this ) , property_index ) ;
if ( interpreter ( ) . 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 ;
return { } ;
2020-04-06 20:24:45 +02:00
}
2020-04-06 16:53:02 +02:00
object = object - > prototype ( ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-04-06 16:53:02 +02: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
Value Object : : get ( PropertyName property_name ) const
2020-03-07 19:42:11 +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
if ( property_name . is_number ( ) )
return get_by_index ( property_name . as_number ( ) ) ;
auto property_string = property_name . to_string ( ) ;
2020-06-12 21:07:52 +02:00
i32 property_index = property_string . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
2020-04-06 16:53:02 +02:00
return get_by_index ( property_index ) ;
2020-03-15 15:01:10 +01:00
const Object * object = this ;
while ( object ) {
2020-03-24 14:46:05 +01:00
auto value = object - > get_own_property ( * this , property_name ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
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-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
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
}
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
{
2020-04-06 20:24:45 +02:00
ASSERT ( ! 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 ;
}
if ( value_here . value . is_object ( ) & & value_here . value . as_object ( ) . is_native_property ( ) ) {
2020-05-29 20:10:21 -07:00
call_native_property_setter ( const_cast < Object * > ( this ) , value_here . value , 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-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
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
}
return put_own_property_by_index ( * this , property_index , value , default_attributes , PutOwnPropertyMode : : Put ) ;
2020-04-06 16:53:02 +02: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
bool Object : : put ( PropertyName property_name , Value value )
2020-03-07 19:42:11 +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
if ( property_name . is_number ( ) )
return put_by_index ( property_name . as_number ( ) , value ) ;
2020-04-06 20:24:45 +02:00
ASSERT ( ! 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
auto property_string = property_name . to_string ( ) ;
2020-06-12 21:07:52 +02:00
i32 property_index = property_string . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
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 put_by_index ( property_index , value ) ;
2020-04-06 16:53:02 +02:00
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 ) {
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 metadata = object - > shape ( ) . lookup ( property_string ) ;
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 ( ) ) {
value_here . as_accessor ( ) . call_setter ( Value ( this ) , value ) ;
return true ;
}
2020-04-02 19:32:21 +02:00
if ( value_here . is_object ( ) & & value_here . as_object ( ) . is_native_property ( ) ) {
2020-05-29 20:10:21 -07:00
call_native_property_setter ( const_cast < Object * > ( this ) , value_here , value ) ;
2020-04-30 20:01:54 +01:00
return true ;
2020-03-19 17:39:13 +01:00
}
}
object = object - > prototype ( ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return { } ;
2020-03-19 17:39:13 +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 put_own_property ( * this , property_string , value , default_attributes , PutOwnPropertyMode : : Put ) ;
2020-04-06 17:08:23 +02:00
}
2020-06-20 13:55:34 +02:00
bool Object : : define_native_function ( const FlyString & property_name , AK : : Function < Value ( Interpreter & , GlobalObject & ) > native_function , i32 length , PropertyAttributes attribute )
2020-03-13 11:06:32 +01:00
{
2020-06-08 12:25:45 +02:00
auto * function = NativeFunction : : create ( interpreter ( ) , global_object ( ) , property_name , move ( native_function ) ) ;
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
function - > define_property ( " length " , Value ( length ) , Attribute : : Configurable ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
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
function - > define_property ( " name " , js_string ( heap ( ) , property_name ) , Attribute : : Configurable ) ;
2020-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
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-06-20 13:55:34 +02:00
bool Object : : define_native_property ( const FlyString & property_name , AK : : Function < Value ( Interpreter & , GlobalObject & ) > getter , AK : : Function < void ( Interpreter & , GlobalObject & , Value ) > setter , PropertyAttributes attribute )
2020-03-15 18:15:44 +01:00
{
2020-06-20 15:40:48 +02:00
return define_property ( property_name , heap ( ) . allocate < NativeProperty > ( global_object ( ) , move ( getter ) , move ( setter ) ) , attribute ) ;
2020-03-15 18:15:44 +01:00
}
2020-03-09 22:11:22 +01:00
void Object : : visit_children ( Cell : : Visitor & visitor )
2020-03-08 19:23:58 +01:00
{
2020-03-09 22:11:22 +01:00
Cell : : visit_children ( 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
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 & value : m_indexed_properties . values_unordered ( ) )
visitor . visit ( value . value ) ;
2020-03-08 19:23:58 +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
bool Object : : has_property ( 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-06-07 10:53:14 -07:00
if ( interpreter ( ) . exception ( ) )
return false ;
2020-04-30 20:03:40 +01: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
bool Object : : has_own_property ( PropertyName property_name ) const
2020-03-15 15:25:43 +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
auto has_indexed_property = [ & ] ( u32 index ) - > bool {
2020-05-01 13:20:47 +01:00
if ( is_string_object ( ) )
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-06-12 21:07:52 +02:00
i32 property_index = property_name . as_string ( ) . to_int ( ) . value_or ( - 1 ) ;
if ( property_index > = 0 )
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 has_indexed_property ( property_index ) ;
return shape ( ) . lookup ( property_name . as_string ( ) ) . has_value ( ) ;
2020-03-15 15:25:43 +01:00
}
2020-05-28 17:19:59 +02:00
Value Object : : to_primitive ( Value : : PreferredType preferred_type ) const
2020-03-16 00:19:41 +02:00
{
Value result = js_undefined ( ) ;
switch ( preferred_type ) {
2020-05-28 17:19:59 +02:00
case Value : : PreferredType : : Default :
case Value : : PreferredType : : Number : {
2020-03-16 00:19:41 +02:00
result = value_of ( ) ;
if ( result . is_object ( ) ) {
result = to_string ( ) ;
}
break ;
}
2020-05-28 17:19:59 +02:00
case Value : : PreferredType : : String : {
2020-03-16 00:19:41 +02:00
result = to_string ( ) ;
if ( result . is_object ( ) )
result = value_of ( ) ;
break ;
}
}
ASSERT ( ! result . is_object ( ) ) ;
return result ;
}
Value Object : : to_string ( ) const
{
2020-04-05 18:18:24 +02:00
auto to_string_property = get ( " toString " ) ;
2020-05-06 16:40:08 +02:00
if ( to_string_property . is_function ( ) ) {
2020-05-06 11:52:53 +01:00
auto & to_string_function = to_string_property . as_function ( ) ;
2020-04-29 16:29:26 +01:00
auto & interpreter = const_cast < Object * > ( this ) - > interpreter ( ) ;
2020-04-29 16:36:38 +01:00
auto to_string_result = interpreter . call ( to_string_function , const_cast < Object * > ( this ) ) ;
if ( to_string_result . is_object ( ) )
2020-06-09 22:48:01 -07:00
interpreter . throw_exception < TypeError > ( ErrorType : : Convert , " object " , " string " ) ;
2020-04-29 16:29:26 +01:00
if ( interpreter . exception ( ) )
return { } ;
2020-05-15 13:39:24 +02:00
auto * string = to_string_result . to_primitive_string ( interpreter ) ;
if ( interpreter . exception ( ) )
return { } ;
return string ;
2020-04-05 18:18:24 +02:00
}
2020-03-16 00:19:41 +02:00
return js_string ( heap ( ) , String : : format ( " [object %s] " , class_name ( ) ) ) ;
}
2020-04-05 18:18:24 +02:00
2020-05-29 12:20:14 +01:00
Value Object : : invoke ( const FlyString & property_name , Optional < MarkedValueList > arguments )
2020-05-28 19:12:34 +01:00
{
2020-05-29 12:20:14 +01:00
auto & interpreter = this - > interpreter ( ) ;
2020-05-28 19:12:34 +01:00
auto property = get ( property_name ) . value_or ( js_undefined ( ) ) ;
if ( interpreter . exception ( ) )
return { } ;
if ( ! property . is_function ( ) ) {
2020-06-09 22:48:01 -07:00
interpreter . throw_exception < TypeError > ( ErrorType : : NotAFunction , property . to_string_without_side_effects ( ) . characters ( ) ) ;
2020-05-28 19:12:34 +01:00
return { } ;
}
2020-05-29 12:20:14 +01:00
return interpreter . call ( property . as_function ( ) , this , move ( arguments ) ) ;
2020-05-28 19:12:34 +01:00
}
2020-05-29 20:10:21 -07:00
Value Object : : call_native_property_getter ( Object * this_object , Value property ) const
{
ASSERT ( property . is_object ( ) ) ;
ASSERT ( property . as_object ( ) . is_native_property ( ) ) ;
auto & native_property = static_cast < NativeProperty & > ( property . as_object ( ) ) ;
auto & call_frame = interpreter ( ) . push_call_frame ( ) ;
call_frame . this_value = this_object ;
2020-06-20 17:35:25 +02:00
auto result = native_property . get ( interpreter ( ) , global_object ( ) ) ;
2020-05-29 20:10:21 -07:00
interpreter ( ) . pop_call_frame ( ) ;
return result ;
}
void Object : : call_native_property_setter ( Object * this_object , Value property , Value value ) const
{
ASSERT ( property . is_object ( ) ) ;
ASSERT ( property . as_object ( ) . is_native_property ( ) ) ;
auto & native_property = static_cast < NativeProperty & > ( property . as_object ( ) ) ;
auto & call_frame = interpreter ( ) . push_call_frame ( ) ;
call_frame . this_value = this_object ;
2020-06-20 17:35:25 +02:00
native_property . set ( interpreter ( ) , global_object ( ) , value ) ;
2020-05-29 20:10:21 -07:00
interpreter ( ) . pop_call_frame ( ) ;
}
2020-03-07 19:42:11 +01:00
}