1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:18:12 +00:00

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)
This commit is contained in:
Andreas Kling 2020-06-23 17:21:53 +02:00
parent fc4ed8d444
commit ba641e97d9
33 changed files with 57 additions and 45 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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) Error::Error(const FlyString& name, const String& message, Object& prototype)
: Object(&prototype) : Object(prototype)
, m_name(name) , m_name(name)
, m_message(message) , m_message(message)
{ {

View file

@ -36,7 +36,7 @@
namespace JS { namespace JS {
ErrorPrototype::ErrorPrototype(GlobalObject& global_object) 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) \ #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
PrototypeName::PrototypeName(GlobalObject& global_object) \ PrototypeName::PrototypeName(GlobalObject& global_object) \
: Object(global_object.error_prototype()) \ : Object(*global_object.error_prototype()) \
{ \ { \
} \ } \
PrototypeName::~PrototypeName() { } PrototypeName::~PrototypeName() { }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -35,7 +35,7 @@ class NativeProperty final : public Object {
JS_OBJECT(NativeProperty, Object); JS_OBJECT(NativeProperty, Object);
public: 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; virtual ~NativeProperty() override;
Value get(Interpreter&, GlobalObject&) const; Value get(Interpreter&, GlobalObject&) const;

View file

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

View file

@ -82,18 +82,24 @@ PropertyDescriptor PropertyDescriptor::from_dictionary(Interpreter& interpreter,
Object* Object::create_empty(Interpreter&, GlobalObject& global_object) 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) { // This is the global object
m_shape = interpreter().global_object().empty_object_shape(); m_shape = interpreter().heap().allocate<Shape>(static_cast<GlobalObject&>(*this), static_cast<GlobalObject&>(*this));
set_prototype(prototype); }
} else {
// This is the global object Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
m_shape = interpreter().heap().allocate<Shape>(static_cast<GlobalObject&>(*this), static_cast<GlobalObject&>(*this)); {
} 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&) 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) 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) void Object::visit_children(Cell::Visitor& visitor)

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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