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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
2020-03-16 00:19:41 +02:00
|
|
|
#include <AK/String.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <LibJS/Forward.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Cell.h>
|
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
|
|
|
#include <LibJS/Runtime/IndexedProperties.h>
|
2020-05-28 19:12:34 +01:00
|
|
|
#include <LibJS/Runtime/MarkedValueList.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
2020-04-06 17:08:23 +02:00
|
|
|
#include <LibJS/Runtime/PropertyName.h>
|
2020-04-27 23:05:02 -07:00
|
|
|
#include <LibJS/Runtime/Shape.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-21 15:14:02 +02:00
|
|
|
#define JS_OBJECT(class_, base_class) \
|
|
|
|
public: \
|
|
|
|
using Base = base_class; \
|
|
|
|
virtual const char* class_name() const override { return #class_; } \
|
|
|
|
virtual bool inherits(const StringView& class_name) const override { return class_name == #class_ || Base::inherits(class_name); }
|
|
|
|
|
2020-06-03 09:40:17 -07:00
|
|
|
struct PropertyDescriptor {
|
|
|
|
PropertyAttributes attributes;
|
|
|
|
Value value;
|
2020-06-03 14:34:52 -07:00
|
|
|
Function* getter { nullptr };
|
|
|
|
Function* setter { nullptr };
|
2020-06-03 09:40:17 -07:00
|
|
|
|
2020-09-21 16:46:45 +02:00
|
|
|
static PropertyDescriptor from_dictionary(VM&, const Object&);
|
2020-06-03 09:40:17 -07:00
|
|
|
|
|
|
|
bool is_accessor_descriptor() const { return getter || setter; }
|
|
|
|
bool is_data_descriptor() const { return !(value.is_empty() && !attributes.has_writable()); }
|
|
|
|
bool is_generic_descriptor() const { return !is_accessor_descriptor() && !is_data_descriptor(); }
|
|
|
|
};
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
class Object : public Cell {
|
2020-03-07 19:42:11 +01:00
|
|
|
public:
|
2020-07-22 17:50:18 +02:00
|
|
|
static Object* create_empty(GlobalObject&);
|
2020-04-18 10:27:57 +02:00
|
|
|
|
2020-06-23 17:21:53 +02:00
|
|
|
explicit Object(Object& prototype);
|
2020-10-17 23:13:37 +02:00
|
|
|
explicit Object(Shape&);
|
2020-07-22 17:50:18 +02:00
|
|
|
virtual void initialize(GlobalObject&) override;
|
2020-03-08 19:23:58 +01:00
|
|
|
virtual ~Object();
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-06-21 15:14:02 +02:00
|
|
|
virtual bool inherits(const StringView& class_name) const { return class_name == this->class_name(); }
|
|
|
|
|
2020-07-09 14:42:30 -07:00
|
|
|
enum class PropertyKind {
|
2020-05-01 11:06:27 +01:00
|
|
|
Key,
|
|
|
|
Value,
|
|
|
|
KeyAndValue,
|
|
|
|
};
|
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
enum class GetOwnPropertyReturnType {
|
|
|
|
StringOnly,
|
|
|
|
SymbolOnly,
|
|
|
|
};
|
|
|
|
|
2020-05-01 11:06:27 +01:00
|
|
|
enum class PutOwnPropertyMode {
|
|
|
|
Put,
|
|
|
|
DefineProperty,
|
|
|
|
};
|
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
Shape& shape() { return *m_shape; }
|
|
|
|
const Shape& shape() const { return *m_shape; }
|
|
|
|
|
2020-11-28 13:58:20 +01:00
|
|
|
GlobalObject& global_object() const { return *shape().global_object(); }
|
2020-06-08 12:15:58 +02:00
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
virtual Value get(const PropertyName&, Value receiver = {}) const;
|
2020-04-06 16:53:02 +02:00
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
virtual bool has_property(const PropertyName&) const;
|
|
|
|
bool has_own_property(const PropertyName&) const;
|
2020-03-21 14:37:34 +01:00
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
virtual bool put(const PropertyName&, Value, Value receiver = {});
|
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-11-22 17:29:25 +00:00
|
|
|
Value get_own_property(const PropertyName&, Value receiver) const;
|
2020-07-09 14:42:30 -07:00
|
|
|
Value get_own_properties(const Object& this_object, PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::StringOnly) const;
|
2020-07-07 21:38:46 -07:00
|
|
|
virtual Optional<PropertyDescriptor> get_own_property_descriptor(const PropertyName&) const;
|
|
|
|
Value get_own_property_descriptor_object(const PropertyName&) const;
|
2020-04-09 22:15:26 +02:00
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
virtual bool define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions = true);
|
|
|
|
bool define_property(const PropertyName&, Value value, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
|
2020-10-06 16:57:02 +02:00
|
|
|
bool define_property_without_transition(const PropertyName&, Value value, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
|
2020-07-07 21:38:46 -07:00
|
|
|
bool define_accessor(const PropertyName&, Function& getter_or_setter, bool is_getter, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-09-27 18:36:49 +02:00
|
|
|
bool define_native_function(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)>, i32 length = 0, PropertyAttributes attributes = default_attributes);
|
|
|
|
bool define_native_property(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attributes = default_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-07-07 21:38:46 -07:00
|
|
|
virtual Value delete_property(const PropertyName&);
|
2020-03-13 11:06:32 +01:00
|
|
|
|
2020-03-20 20:29:57 +01:00
|
|
|
virtual bool is_array() const { return false; }
|
2020-03-30 00:21:56 +01:00
|
|
|
virtual bool is_date() const { return false; }
|
|
|
|
virtual bool is_error() const { return false; }
|
2020-03-07 19:42:11 +01:00
|
|
|
virtual bool is_function() const { return false; }
|
2020-03-12 19:53:31 +01:00
|
|
|
virtual bool is_native_function() const { return false; }
|
2020-04-19 15:03:02 -05:00
|
|
|
virtual bool is_bound_function() const { return false; }
|
2020-06-03 14:34:52 -07:00
|
|
|
virtual bool is_proxy_object() const { return false; }
|
2020-06-03 16:05:49 -07:00
|
|
|
virtual bool is_regexp_object() const { return false; }
|
2020-06-10 11:01:00 -07:00
|
|
|
virtual bool is_boolean_object() const { return false; }
|
2020-03-30 00:21:56 +01:00
|
|
|
virtual bool is_string_object() const { return false; }
|
2020-06-10 11:01:00 -07:00
|
|
|
virtual bool is_number_object() const { return false; }
|
2020-04-29 23:25:21 -07:00
|
|
|
virtual bool is_symbol_object() const { return false; }
|
2020-06-06 01:14:10 +01:00
|
|
|
virtual bool is_bigint_object() const { return false; }
|
2020-07-11 20:23:01 -07:00
|
|
|
virtual bool is_string_iterator_object() const { return false; }
|
2020-07-09 14:58:20 -07:00
|
|
|
virtual bool is_array_iterator_object() const { return false; }
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
virtual const char* class_name() const override { return "Object"; }
|
2020-03-09 22:11:22 +01:00
|
|
|
virtual void visit_children(Cell::Visitor&) override;
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-06-03 14:34:52 -07:00
|
|
|
virtual Object* prototype();
|
|
|
|
virtual const Object* prototype() const;
|
|
|
|
virtual bool set_prototype(Object* prototype);
|
2020-03-28 19:48:12 +01:00
|
|
|
bool has_prototype(const Object* prototype) const;
|
2020-03-15 15:01:10 +01:00
|
|
|
|
2020-06-03 14:34:52 -07:00
|
|
|
virtual bool is_extensible() const { return m_is_extensible; }
|
|
|
|
virtual bool prevent_extensions();
|
2020-06-01 21:13:16 -07:00
|
|
|
|
2020-03-16 00:19:41 +02:00
|
|
|
virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
|
2020-11-03 19:52:21 +00:00
|
|
|
virtual Value ordinary_to_primitive(Value::PreferredType preferred_type) const;
|
2020-03-15 15:25:43 +01:00
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
Value get_direct(size_t index) const { return m_storage[index]; }
|
2020-03-26 12:19:01 +01: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
|
|
|
const IndexedProperties& indexed_properties() const { return m_indexed_properties; }
|
|
|
|
IndexedProperties& indexed_properties() { return m_indexed_properties; }
|
|
|
|
void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); }
|
2020-04-06 16:53:02 +02:00
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
Value invoke(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments = {});
|
2020-05-28 19:12:34 +01:00
|
|
|
|
2020-10-04 22:56:45 +02:00
|
|
|
void ensure_shape_is_unique();
|
|
|
|
|
2020-10-05 20:08:14 +02:00
|
|
|
void enable_transitions() { m_transitions_enabled = true; }
|
|
|
|
void disable_transitions() { m_transitions_enabled = false; }
|
|
|
|
|
2020-06-23 17:21:53 +02:00
|
|
|
protected:
|
|
|
|
enum class GlobalObjectTag { Tag };
|
|
|
|
enum class ConstructWithoutPrototypeTag { Tag };
|
|
|
|
explicit Object(GlobalObjectTag);
|
|
|
|
Object(ConstructWithoutPrototypeTag, GlobalObject&);
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
private:
|
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
|
|
|
virtual Value get_by_index(u32 property_index) const;
|
|
|
|
virtual bool put_by_index(u32 property_index, Value);
|
2020-07-07 21:38:46 -07:00
|
|
|
bool put_own_property(Object& this_object, const StringOrSymbol& property_name, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
|
2020-06-02 17:13:09 -07:00
|
|
|
bool put_own_property_by_index(Object& this_object, u32 property_index, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = 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
|
|
|
|
2020-11-22 17:29:25 +00:00
|
|
|
Value call_native_property_getter(NativeProperty& property, Value this_value) const;
|
|
|
|
void call_native_property_setter(NativeProperty& property, Value this_value, Value) const;
|
2020-05-29 20:10:21 -07:00
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
void set_shape(Shape&);
|
|
|
|
|
2020-06-01 21:13:16 -07:00
|
|
|
bool m_is_extensible { true };
|
2020-10-05 20:08:14 +02:00
|
|
|
bool m_transitions_enabled { true };
|
2020-04-02 19:32:21 +02:00
|
|
|
Shape* m_shape { nullptr };
|
|
|
|
Vector<Value> m_storage;
|
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
|
|
|
IndexedProperties m_indexed_properties;
|
2020-03-07 19:42:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|