1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:57:45 +00:00

LibJS: Rewrite most of Object for spec compliance :^)

This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.

This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.

What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.

Key changes include:

- 1:1 matching function names and parameters of all object-related
  functions, to avoid ambiguity. Previously we had things like put(),
  which the spec doesn't have - as a result it wasn't always clear which
  need to be used.
- Better separation between object abstract operations and internal
  methods - the former are always the same, the latter can be overridden
  (and are therefore virtual). The internal methods (i.e. [[Foo]] in the
  spec) are now prefixed with 'internal_' for clarity - again, it was
  previously not always clear which AO a certain method represents,
  get() could've been both Get and [[Get]] (I don't know which one it
  was closer to right now).
  Note that some of the old names have been kept until all code relying
  on them is updated, but they are now simple wrappers around the
  closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
  storage are now prefixed with 'storage_' to make their purpose clear,
  and as they are not part of the spec they should not contain any steps
  specified by it. Much functionality is now covered by the layers above
  it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
  by PropertyDescriptor - a concept similar to the current
  implementation, but more aligned with the actual spec. See the commit
  message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
  introduced more inline comments with the exact steps from the spec -
  this makes it super easy to verify correctness.
- East-const all the things.

As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.

Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)

Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
This commit is contained in:
Linus Groh 2021-07-04 18:14:16 +01:00
parent 4e5362b7cb
commit 09bd5f8772
80 changed files with 4019 additions and 2239 deletions

View file

@ -14,6 +14,7 @@
#include <LibJS/Runtime/IndexedProperties.h>
#include <LibJS/Runtime/MarkedValueList.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/PropertyDescriptor.h>
#include <LibJS/Runtime/PropertyName.h>
#include <LibJS/Runtime/Shape.h>
#include <LibJS/Runtime/Value.h>
@ -25,19 +26,6 @@ public: \
using Base = base_class; \
virtual const char* class_name() const override { return #class_; }
struct PropertyDescriptor {
PropertyAttributes attributes;
Value value;
FunctionObject* getter { nullptr };
FunctionObject* setter { nullptr };
static PropertyDescriptor from_dictionary(VM&, const Object&);
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(); }
};
class Object : public Cell {
public:
static Object* create(GlobalObject&, Object* prototype);
@ -53,12 +41,6 @@ public:
KeyAndValue,
};
enum class GetOwnPropertyReturnType {
All,
StringOnly,
SymbolOnly,
};
enum class PutOwnPropertyMode {
Put,
DefineProperty,
@ -69,27 +51,84 @@ public:
Frozen,
};
Shape& shape() { return *m_shape; }
const Shape& shape() const { return *m_shape; }
// Please DO NOT make up your own non-standard methods unless you
// have a very good reason to do so. If any object abstract
// operation from the spec is missing, add it instead.
// Functionality for implementation details like shapes and
// property storage are obviously exempt from this rule :^)
//
// Methods named [[Foo]]() in the spec are named internal_foo()
// here, as they are "The [[Foo]] internal method of a ... object".
// They must be virtual and may be overridden. All other methods
// follow the the regular PascalCase name converted to camel_case
// naming convention and must not be virtual.
GlobalObject& global_object() const { return *shape().global_object(); }
// 7.1 Type Conversion, https://tc39.es/ecma262/#sec-type-conversion
Value ordinary_to_primitive(Value::PreferredType preferred_type) const;
// 7.2 Testing and Comparison Operations, https://tc39.es/ecma262/#sec-testing-and-comparison-operations
bool is_extensible() const;
// 7.3 Operations on Objects, https://tc39.es/ecma262/#sec-operations-on-objects
Value get(PropertyName const&) const;
bool set(PropertyName const&, Value, bool throw_exceptions);
bool create_data_property(PropertyName const&, Value);
bool create_method_property(PropertyName const&, Value);
bool create_data_property_or_throw(PropertyName const&, Value);
bool define_property_or_throw(PropertyName const&, PropertyDescriptor const&);
bool delete_property_or_throw(PropertyName const&);
bool has_property(PropertyName const&) const;
bool has_own_property(PropertyName const&) const;
bool set_integrity_level(IntegrityLevel);
bool test_integrity_level(IntegrityLevel) const;
MarkedValueList enumerable_own_property_names(PropertyKind kind) const;
// 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
virtual Object* internal_get_prototype_of() const;
virtual bool internal_set_prototype_of(Object* prototype);
virtual bool internal_is_extensible() const;
virtual bool internal_prevent_extensions();
virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const;
virtual bool internal_define_own_property(PropertyName const&, PropertyDescriptor const&);
virtual bool internal_has_property(PropertyName const&) const;
virtual Value internal_get(PropertyName const&, Value receiver) const;
virtual bool internal_set(PropertyName const&, Value value, Value receiver);
virtual bool internal_delete(PropertyName const&);
virtual MarkedValueList internal_own_property_keys() const;
// 20.1 Object Objects, https://tc39.es/ecma262/#sec-object-objects
Object* define_properties(Value properties);
// Implementation-specific storage abstractions
enum class CallNativeProperty {
Yes,
No,
};
Optional<ValueAndAttributes> storage_get(PropertyName const&, CallNativeProperty = CallNativeProperty::Yes) const;
bool storage_has(PropertyName const&) const;
void storage_set(PropertyName const&, ValueAndAttributes const&);
void storage_delete(PropertyName const&);
// Non-standard methods
// - Helpers using old, non-standard names but wrapping the standard methods.
// FIXME: Update all the code relying on these and remove them.
bool put(PropertyName const& property_name, Value value, Value receiver = {}) { return internal_set(property_name, value, receiver.value_or(this)); }
Optional<PropertyDescriptor> get_own_property_descriptor(PropertyName const& property_name) const { return internal_get_own_property(property_name); }
bool define_property(PropertyName const& property_name, Value value, PropertyAttributes attributes = default_attributes, bool = true)
{
return internal_define_own_property(property_name, { .value = value, .writable = attributes.is_writable(), .enumerable = attributes.is_enumerable(), .configurable = attributes.is_configurable() });
};
virtual Value get(const PropertyName&, Value receiver = {}, AllowSideEffects = AllowSideEffects::Yes) const;
Value get_without_side_effects(const PropertyName&) const;
virtual bool has_property(const PropertyName&) const;
bool has_own_property(const PropertyName&) const;
virtual bool put(const PropertyName&, Value, Value receiver = {});
Value get_own_property(const PropertyName&, Value receiver, AllowSideEffects = AllowSideEffects::Yes) const;
MarkedValueList get_own_properties(PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::All) const;
MarkedValueList get_enumerable_own_property_names(PropertyKind) const;
virtual Optional<PropertyDescriptor> get_own_property_descriptor(const PropertyName&) const;
Value get_own_property_descriptor_object(const PropertyName&) const;
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);
bool define_property_without_transition(const PropertyName&, Value value, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
bool define_accessor(const PropertyName&, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
@ -97,10 +136,6 @@ public:
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);
void define_properties(Value properties);
virtual bool delete_property(PropertyName const&, bool force_throw_exception = false);
virtual bool is_array() const { return false; }
virtual bool is_function() const { return false; }
virtual bool is_typed_array() const { return false; }
@ -115,20 +150,7 @@ public:
virtual const char* class_name() const override { return "Object"; }
virtual void visit_edges(Cell::Visitor&) override;
virtual Object* prototype();
virtual const Object* prototype() const;
virtual bool set_prototype(Object* prototype);
bool has_prototype(const Object* prototype) const;
virtual bool is_extensible() const { return m_is_extensible; }
virtual bool prevent_extensions();
bool set_integrity_level(IntegrityLevel);
bool test_integrity_level(IntegrityLevel);
virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
virtual Value ordinary_to_primitive(Value::PreferredType preferred_type) const;
Value get_direct(size_t index) const { return m_storage[index]; }
@ -150,6 +172,11 @@ public:
return invoke(property_name);
}
Shape& shape() { return *m_shape; }
Shape const& shape() const { return *m_shape; }
GlobalObject& global_object() const { return *shape().global_object(); }
void ensure_shape_is_unique();
void enable_transitions() { m_transitions_enabled = true; }
@ -164,19 +191,17 @@ protected:
explicit Object(GlobalObjectTag);
Object(ConstructWithoutPrototypeTag, GlobalObject&);
virtual Value get_by_index(u32 property_index, AllowSideEffects = AllowSideEffects::Yes) const;
virtual bool put_by_index(u32 property_index, Value);
bool m_is_extensible { true };
private:
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);
Value call_native_property_getter(NativeProperty& property, Value this_value) const;
void call_native_property_setter(NativeProperty& property, Value this_value, Value) const;
void set_shape(Shape&);
bool m_is_extensible { true };
Object* prototype() { return shape().prototype(); }
Object const* prototype() const { return shape().prototype(); }
bool m_transitions_enabled { true };
Shape* m_shape { nullptr };
Vector<Value> m_storage;