2020-03-07 19:42:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-04-22 22:51:19 +02:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2020-03-07 19:42:11 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-07 19:42:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2021-05-17 19:50:20 +02:00
|
|
|
#include <LibJS/Heap/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 {
|
|
|
|
|
2021-01-01 17:00:20 +01:00
|
|
|
#define JS_OBJECT(class_, base_class) \
|
|
|
|
public: \
|
|
|
|
using Base = base_class; \
|
|
|
|
virtual const char* class_name() const override { return #class_; }
|
2020-06-21 15:14:02 +02:00
|
|
|
|
2020-06-03 09:40:17 -07:00
|
|
|
struct PropertyDescriptor {
|
|
|
|
PropertyAttributes attributes;
|
|
|
|
Value value;
|
2021-06-27 21:48:34 +02:00
|
|
|
FunctionObject* getter { nullptr };
|
|
|
|
FunctionObject* 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:
|
2021-06-16 20:52:30 +01:00
|
|
|
static Object* create(GlobalObject&, Object* prototype);
|
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-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 {
|
2021-04-05 19:17:40 +02:00
|
|
|
All,
|
2020-07-07 21:38:46 -07:00
|
|
|
StringOnly,
|
|
|
|
SymbolOnly,
|
|
|
|
};
|
|
|
|
|
2020-05-01 11:06:27 +01:00
|
|
|
enum class PutOwnPropertyMode {
|
|
|
|
Put,
|
|
|
|
DefineProperty,
|
|
|
|
};
|
|
|
|
|
2021-04-06 22:06:11 +02:00
|
|
|
enum class IntegrityLevel {
|
|
|
|
Sealed,
|
|
|
|
Frozen,
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2021-06-17 03:12:41 +03:00
|
|
|
virtual Value get(const PropertyName&, Value receiver = {}, AllowSideEffects = AllowSideEffects::Yes) const;
|
2021-04-11 22:52:25 +02:00
|
|
|
Value get_without_side_effects(const PropertyName&) 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
|
|
|
|
2021-06-17 03:12:41 +03:00
|
|
|
Value get_own_property(const PropertyName&, Value receiver, AllowSideEffects = AllowSideEffects::Yes) const;
|
2021-04-06 21:39:17 +02:00
|
|
|
MarkedValueList get_own_properties(PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::All) const;
|
|
|
|
MarkedValueList get_enumerable_own_property_names(PropertyKind) 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);
|
2021-06-27 21:48:34 +02:00
|
|
|
bool define_accessor(const PropertyName&, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2021-06-27 22:38:42 +02:00
|
|
|
bool define_native_function(PropertyName const&, Function<Value(VM&, GlobalObject&)>, i32 length = 0, PropertyAttributes attributes = default_attributes);
|
|
|
|
bool define_native_property(PropertyName const&, Function<Value(VM&, GlobalObject&)> getter, Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attributes = default_attributes);
|
|
|
|
bool define_native_accessor(PropertyName const&, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> 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
|
|
|
|
2021-04-10 18:39:11 +02:00
|
|
|
void define_properties(Value properties);
|
|
|
|
|
2021-06-21 16:06:32 +02:00
|
|
|
virtual bool delete_property(PropertyName const&, bool force_throw_exception = false);
|
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-07 19:42:11 +01:00
|
|
|
virtual bool is_function() const { return false; }
|
2020-12-01 21:05:25 +01:00
|
|
|
virtual bool is_typed_array() const { return false; }
|
2021-03-19 11:28:44 +01:00
|
|
|
virtual bool is_string_object() const { return false; }
|
|
|
|
virtual bool is_global_object() const { return false; }
|
2021-06-13 11:23:23 +02:00
|
|
|
virtual bool is_proxy_object() const { return false; }
|
|
|
|
virtual bool is_native_function() const { return false; }
|
2021-06-28 13:26:01 +02:00
|
|
|
virtual bool is_ordinary_function_object() const { return false; }
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2021-06-20 17:08:29 +01:00
|
|
|
// B.3.7 The [[IsHTMLDDA]] Internal Slot, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot
|
|
|
|
virtual bool is_htmldda() const { return false; }
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
virtual const char* class_name() const override { return "Object"; }
|
2020-11-28 14:33:36 +01:00
|
|
|
virtual void visit_edges(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
|
|
|
|
2021-04-06 22:06:11 +02:00
|
|
|
bool set_integrity_level(IntegrityLevel);
|
2021-04-06 22:45:12 +02:00
|
|
|
bool test_integrity_level(IntegrityLevel);
|
2021-04-06 22:06:11 +02: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
|
|
|
|
2021-03-14 12:01:18 +01:00
|
|
|
[[nodiscard]] Value invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments);
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE Value invoke(const StringOrSymbol& property_name, Args... args)
|
|
|
|
{
|
|
|
|
if constexpr (sizeof...(Args) > 0) {
|
|
|
|
MarkedValueList arglist { heap() };
|
|
|
|
(..., arglist.append(move(args)));
|
|
|
|
return invoke(property_name, move(arglist));
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoke(property_name);
|
|
|
|
}
|
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; }
|
|
|
|
|
2021-03-19 11:28:44 +01:00
|
|
|
template<typename T>
|
|
|
|
bool fast_is() const = delete;
|
|
|
|
|
2020-06-23 17:21:53 +02:00
|
|
|
protected:
|
|
|
|
enum class GlobalObjectTag { Tag };
|
|
|
|
enum class ConstructWithoutPrototypeTag { Tag };
|
|
|
|
explicit Object(GlobalObjectTag);
|
|
|
|
Object(ConstructWithoutPrototypeTag, GlobalObject&);
|
|
|
|
|
2021-06-17 03:12:41 +03:00
|
|
|
virtual Value get_by_index(u32 property_index, AllowSideEffects = AllowSideEffects::Yes) const;
|
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 bool put_by_index(u32 property_index, Value);
|
2020-12-01 17:10:57 +01:00
|
|
|
|
|
|
|
private:
|
2021-04-05 18:04:55 +02:00
|
|
|
bool put_own_property(const StringOrSymbol& property_name, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
|
|
|
|
bool put_own_property_by_index(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
|
|
|
};
|
|
|
|
|
2021-03-14 12:01:18 +01:00
|
|
|
template<>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name, MarkedValueList arguments) { return invoke_internal(property_name, move(arguments)); }
|
|
|
|
|
|
|
|
template<>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments) { return invoke_internal(property_name, move(arguments)); }
|
|
|
|
|
|
|
|
template<>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name) { return invoke(property_name, Optional<MarkedValueList> {}); }
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|