Prechádzať zdrojové kódy

LibJS: Clarify Object (base class) construction somewhat

Divide the Object constructor into three variants:

- The regular one (takes an Object& prototype)
- One for use by GlobalObject
- One for use by objects without a prototype (e.g ObjectPrototype)
Andreas Kling 5 rokov pred
rodič
commit
ba641e97d9
33 zmenil súbory, kde vykonal 57 pridanie a 45 odobranie
  1. 1 1
      Libraries/LibJS/Runtime/Array.cpp
  2. 1 1
      Libraries/LibJS/Runtime/ArrayPrototype.cpp
  3. 1 1
      Libraries/LibJS/Runtime/BigIntObject.cpp
  4. 1 1
      Libraries/LibJS/Runtime/BigIntPrototype.cpp
  5. 1 1
      Libraries/LibJS/Runtime/BooleanObject.cpp
  6. 1 1
      Libraries/LibJS/Runtime/ConsoleObject.cpp
  7. 1 1
      Libraries/LibJS/Runtime/Date.cpp
  8. 1 1
      Libraries/LibJS/Runtime/DatePrototype.cpp
  9. 1 1
      Libraries/LibJS/Runtime/Error.cpp
  10. 2 2
      Libraries/LibJS/Runtime/ErrorPrototype.cpp
  11. 1 1
      Libraries/LibJS/Runtime/Function.cpp
  12. 1 1
      Libraries/LibJS/Runtime/FunctionPrototype.cpp
  13. 1 1
      Libraries/LibJS/Runtime/GlobalObject.cpp
  14. 1 1
      Libraries/LibJS/Runtime/JSONObject.cpp
  15. 1 1
      Libraries/LibJS/Runtime/MathObject.cpp
  16. 2 2
      Libraries/LibJS/Runtime/NativeProperty.cpp
  17. 1 1
      Libraries/LibJS/Runtime/NativeProperty.h
  18. 1 1
      Libraries/LibJS/Runtime/NumberObject.cpp
  19. 16 10
      Libraries/LibJS/Runtime/Object.cpp
  20. 7 1
      Libraries/LibJS/Runtime/Object.h
  21. 2 2
      Libraries/LibJS/Runtime/ObjectPrototype.cpp
  22. 1 1
      Libraries/LibJS/Runtime/ProxyObject.cpp
  23. 1 1
      Libraries/LibJS/Runtime/ProxyPrototype.cpp
  24. 1 1
      Libraries/LibJS/Runtime/ReflectObject.cpp
  25. 1 1
      Libraries/LibJS/Runtime/RegExpObject.cpp
  26. 1 1
      Libraries/LibJS/Runtime/StringObject.cpp
  27. 1 1
      Libraries/LibJS/Runtime/SymbolObject.cpp
  28. 1 1
      Libraries/LibJS/Runtime/SymbolPrototype.cpp
  29. 1 1
      Libraries/LibJS/Runtime/Uint8ClampedArray.cpp
  30. 1 1
      Libraries/LibWeb/Bindings/LocationObject.cpp
  31. 1 1
      Libraries/LibWeb/Bindings/NavigatorObject.cpp
  32. 1 1
      Libraries/LibWeb/Bindings/Wrapper.h
  33. 1 1
      Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp

+ 1 - 1
Libraries/LibJS/Runtime/Array.cpp

@@ -40,7 +40,7 @@ Array* Array::create(GlobalObject& global_object)
 }
 
 Array::Array(Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
 {
     define_native_property("length", length_getter, length_setter, Attribute::Writable);
 }

+ 1 - 1
Libraries/LibJS/Runtime/ArrayPrototype.cpp

@@ -42,7 +42,7 @@
 namespace JS {
 
 ArrayPrototype::ArrayPrototype(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/BigIntObject.cpp

@@ -37,7 +37,7 @@ BigIntObject* BigIntObject::create(GlobalObject& global_object, BigInt& bigint)
 }
 
 BigIntObject::BigIntObject(BigInt& bigint, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_bigint(bigint)
 {
 }

+ 1 - 1
Libraries/LibJS/Runtime/BigIntPrototype.cpp

@@ -33,7 +33,7 @@
 namespace JS {
 
 BigIntPrototype::BigIntPrototype(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/BooleanObject.cpp

@@ -37,7 +37,7 @@ BooleanObject* BooleanObject::create(GlobalObject& global_object, bool value)
 }
 
 BooleanObject::BooleanObject(bool value, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_value(value)
 {
 }

+ 1 - 1
Libraries/LibJS/Runtime/ConsoleObject.cpp

@@ -36,7 +36,7 @@
 namespace JS {
 
 ConsoleObject::ConsoleObject(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/Date.cpp

@@ -38,7 +38,7 @@ Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, u16 mil
 }
 
 Date::Date(Core::DateTime datetime, u16 milliseconds, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_datetime(datetime)
     , m_milliseconds(milliseconds)
 {

+ 1 - 1
Libraries/LibJS/Runtime/DatePrototype.cpp

@@ -49,7 +49,7 @@ static Date* typed_this(Interpreter& interpreter, GlobalObject& global_object)
 }
 
 DatePrototype::DatePrototype(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/Error.cpp

@@ -37,7 +37,7 @@ Error* Error::create(GlobalObject& global_object, const FlyString& name, const S
 }
 
 Error::Error(const FlyString& name, const String& message, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_name(name)
     , m_message(message)
 {

+ 2 - 2
Libraries/LibJS/Runtime/ErrorPrototype.cpp

@@ -36,7 +36,7 @@
 namespace JS {
 
 ErrorPrototype::ErrorPrototype(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 
@@ -123,7 +123,7 @@ JS_DEFINE_NATIVE_FUNCTION(ErrorPrototype::to_string)
 
 #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
     PrototypeName::PrototypeName(GlobalObject& global_object)                 \
-        : Object(global_object.error_prototype())                             \
+        : Object(*global_object.error_prototype())                             \
     {                                                                         \
     }                                                                         \
     PrototypeName::~PrototypeName() { }

+ 1 - 1
Libraries/LibJS/Runtime/Function.cpp

@@ -37,7 +37,7 @@ Function::Function(Object& prototype)
 }
 
 Function::Function(Object& prototype, Value bound_this, Vector<Value> bound_arguments)
-    : Object(&prototype)
+    : Object(prototype)
     , m_bound_this(bound_this)
     , m_bound_arguments(move(bound_arguments))
 {

+ 1 - 1
Libraries/LibJS/Runtime/FunctionPrototype.cpp

@@ -39,7 +39,7 @@
 namespace JS {
 
 FunctionPrototype::FunctionPrototype(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -64,7 +64,7 @@
 namespace JS {
 
 GlobalObject::GlobalObject()
-    : Object(nullptr)
+    : Object(GlobalObjectTag::Tag)
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/JSONObject.cpp

@@ -38,7 +38,7 @@
 namespace JS {
 
 JSONObject::JSONObject(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/MathObject.cpp

@@ -35,7 +35,7 @@
 namespace JS {
 
 MathObject::MathObject(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 2 - 2
Libraries/LibJS/Runtime/NativeProperty.cpp

@@ -29,8 +29,8 @@
 
 namespace JS {
 
-NativeProperty::NativeProperty(AK::Function<Value(Interpreter&, GlobalObject&)> getter, AK::Function<void(Interpreter&, GlobalObject&, Value)> setter)
-    : Object(nullptr)
+NativeProperty::NativeProperty(GlobalObject& global_object, AK::Function<Value(Interpreter&, GlobalObject&)> getter, AK::Function<void(Interpreter&, GlobalObject&, Value)> setter)
+    : Object(Object::ConstructWithoutPrototypeTag::Tag, global_object)
     , m_getter(move(getter))
     , m_setter(move(setter))
 {

+ 1 - 1
Libraries/LibJS/Runtime/NativeProperty.h

@@ -35,7 +35,7 @@ class NativeProperty final : public Object {
     JS_OBJECT(NativeProperty, Object);
 
 public:
-    NativeProperty(AK::Function<Value(Interpreter&, GlobalObject&)> getter, AK::Function<void(Interpreter&, GlobalObject&, Value)> setter);
+    NativeProperty(GlobalObject&, AK::Function<Value(Interpreter&, GlobalObject&)> getter, AK::Function<void(Interpreter&, GlobalObject&, Value)> setter);
     virtual ~NativeProperty() override;
 
     Value get(Interpreter&, GlobalObject&) const;

+ 1 - 1
Libraries/LibJS/Runtime/NumberObject.cpp

@@ -39,7 +39,7 @@ NumberObject* NumberObject::create(GlobalObject& global_object, double value)
 }
 
 NumberObject::NumberObject(double value, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_value(value)
 {
 }

+ 16 - 10
Libraries/LibJS/Runtime/Object.cpp

@@ -82,18 +82,24 @@ PropertyDescriptor PropertyDescriptor::from_dictionary(Interpreter& interpreter,
 
 Object* Object::create_empty(Interpreter&, GlobalObject& global_object)
 {
-    return global_object.heap().allocate<Object>(global_object, global_object.object_prototype());
+    return global_object.heap().allocate<Object>(global_object, *global_object.object_prototype());
 }
 
-Object::Object(Object* prototype)
+Object::Object(GlobalObjectTag)
 {
-    if (prototype) {
-        m_shape = interpreter().global_object().empty_object_shape();
-        set_prototype(prototype);
-    } else {
-        // This is the global object
-        m_shape = interpreter().heap().allocate<Shape>(static_cast<GlobalObject&>(*this), static_cast<GlobalObject&>(*this));
-    }
+    // This is the global object
+    m_shape = interpreter().heap().allocate<Shape>(static_cast<GlobalObject&>(*this), static_cast<GlobalObject&>(*this));
+}
+
+Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
+{
+    m_shape = interpreter().heap().allocate<Shape>(global_object, global_object);
+}
+
+Object::Object(Object& prototype)
+{
+    m_shape = prototype.global_object().empty_object_shape();
+    set_prototype(&prototype);
 }
 
 void Object::initialize(Interpreter&, GlobalObject&)
@@ -698,7 +704,7 @@ bool Object::define_native_function(const FlyString& property_name, AK::Function
 
 bool Object::define_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&, GlobalObject&)> getter, AK::Function<void(Interpreter&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
 {
-    return define_property(property_name, heap().allocate<NativeProperty>(global_object(), move(getter), move(setter)), attribute);
+    return define_property(property_name, heap().allocate<NativeProperty>(global_object(), global_object(), move(getter), move(setter)), attribute);
 }
 
 void Object::visit_children(Cell::Visitor& visitor)

+ 7 - 1
Libraries/LibJS/Runtime/Object.h

@@ -62,7 +62,7 @@ class Object : public Cell {
 public:
     static Object* create_empty(Interpreter&, GlobalObject&);
 
-    explicit Object(Object* prototype);
+    explicit Object(Object& prototype);
     virtual void initialize(Interpreter&, GlobalObject&) override;
     virtual ~Object();
 
@@ -142,6 +142,12 @@ public:
 
     Value invoke(const FlyString& property_name, Optional<MarkedValueList> arguments = {});
 
+protected:
+    enum class GlobalObjectTag { Tag };
+    enum class ConstructWithoutPrototypeTag { Tag };
+    explicit Object(GlobalObjectTag);
+    Object(ConstructWithoutPrototypeTag, GlobalObject&);
+
 private:
     virtual Value get_by_index(u32 property_index) const;
     virtual bool put_by_index(u32 property_index, Value);

+ 2 - 2
Libraries/LibJS/Runtime/ObjectPrototype.cpp

@@ -34,8 +34,8 @@
 
 namespace JS {
 
-ObjectPrototype::ObjectPrototype(GlobalObject&)
-    : Object(nullptr)
+ObjectPrototype::ObjectPrototype(GlobalObject& global_object)
+    : Object(Object::ConstructWithoutPrototypeTag::Tag, global_object)
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/ProxyObject.cpp

@@ -63,7 +63,7 @@ ProxyObject* ProxyObject::create(GlobalObject& global_object, Object& target, Ob
 }
 
 ProxyObject::ProxyObject(Object& target, Object& handler, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_target(target)
     , m_handler(handler)
 {

+ 1 - 1
Libraries/LibJS/Runtime/ProxyPrototype.cpp

@@ -35,7 +35,7 @@
 namespace JS {
 
 ProxyPrototype::ProxyPrototype(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/ReflectObject.cpp

@@ -76,7 +76,7 @@ static void prepare_arguments_list(Interpreter& interpreter, Value value, Marked
 }
 
 ReflectObject::ReflectObject(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/RegExpObject.cpp

@@ -41,7 +41,7 @@ RegExpObject* RegExpObject::create(GlobalObject& global_object, String content,
 }
 
 RegExpObject::RegExpObject(String content, String flags, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_content(content)
     , m_flags(flags)
 {

+ 1 - 1
Libraries/LibJS/Runtime/StringObject.cpp

@@ -40,7 +40,7 @@ StringObject* StringObject::create(GlobalObject& global_object, PrimitiveString&
 }
 
 StringObject::StringObject(PrimitiveString& string, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_string(string)
 {
 }

+ 1 - 1
Libraries/LibJS/Runtime/SymbolObject.cpp

@@ -56,7 +56,7 @@ SymbolObject* SymbolObject::create(GlobalObject& global_object, Symbol& primitiv
 }
 
 SymbolObject::SymbolObject(Symbol& symbol, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_symbol(symbol)
 {
 }

+ 1 - 1
Libraries/LibJS/Runtime/SymbolPrototype.cpp

@@ -40,7 +40,7 @@
 namespace JS {
 
 SymbolPrototype::SymbolPrototype(GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibJS/Runtime/Uint8ClampedArray.cpp

@@ -39,7 +39,7 @@ Uint8ClampedArray* Uint8ClampedArray::create(GlobalObject& global_object, u32 le
 }
 
 Uint8ClampedArray::Uint8ClampedArray(u32 length, Object& prototype)
-    : Object(&prototype)
+    : Object(prototype)
     , m_length(length)
 {
     define_native_property("length", length_getter, nullptr);

+ 1 - 1
Libraries/LibWeb/Bindings/LocationObject.cpp

@@ -36,7 +36,7 @@ namespace Web {
 namespace Bindings {
 
 LocationObject::LocationObject(JS::GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibWeb/Bindings/NavigatorObject.cpp

@@ -35,7 +35,7 @@ namespace Web {
 namespace Bindings {
 
 NavigatorObject::NavigatorObject(JS::GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }
 

+ 1 - 1
Libraries/LibWeb/Bindings/Wrapper.h

@@ -42,7 +42,7 @@ class Wrapper
 public:
 protected:
     explicit Wrapper(Object& prototype)
-        : Object(&prototype)
+        : Object(prototype)
     {
     }
 };

+ 1 - 1
Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp

@@ -36,7 +36,7 @@ namespace Web {
 namespace Bindings {
 
 XMLHttpRequestPrototype::XMLHttpRequestPrototype(JS::GlobalObject& global_object)
-    : Object(global_object.object_prototype())
+    : Object(*global_object.object_prototype())
 {
 }