diff options
83 files changed, 295 insertions, 167 deletions
diff --git a/Libraries/LibJS/Heap/Heap.h b/Libraries/LibJS/Heap/Heap.h index 6544021b5a..c12805bfb9 100644 --- a/Libraries/LibJS/Heap/Heap.h +++ b/Libraries/LibJS/Heap/Heap.h @@ -46,13 +46,23 @@ public: ~Heap(); template<typename T, typename... Args> - T* allocate(Args&&... args) + T* allocate_without_global_object(Args&&... args) { auto* memory = allocate_cell(sizeof(T)); new (memory) T(forward<Args>(args)...); return static_cast<T*>(memory); } + template<typename T, typename... Args> + T* allocate(GlobalObject& global_object, Args&&... args) + { + auto* memory = allocate_cell(sizeof(T)); + new (memory) T(forward<Args>(args)...); + auto* cell = static_cast<T*>(memory); + cell->initialize(m_interpreter, global_object); + return cell; + } + enum class CollectionType { CollectGarbage, CollectEverything, diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 3c4eceeb57..abf18af759 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -59,7 +59,7 @@ Value Interpreter::run(GlobalObject& global_object, const Statement& statement, CallFrame global_call_frame; global_call_frame.this_value = &global_object; global_call_frame.function_name = "(global execution context)"; - global_call_frame.environment = heap().allocate<LexicalEnvironment>(); + global_call_frame.environment = heap().allocate<LexicalEnvironment>(global_object); m_call_stack.append(move(global_call_frame)); } } @@ -126,7 +126,7 @@ void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector argume bool pushed_lexical_environment = false; if (!scope_variables_with_declaration_kind.is_empty()) { - auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(move(scope_variables_with_declaration_kind), current_environment()); + auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_environment()); m_call_stack.last().environment = block_lexical_environment; pushed_lexical_environment = true; } diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index a4b6a4640f..6897b54018 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -78,7 +78,7 @@ public: static NonnullOwnPtr<Interpreter> create(Args&&... args) { auto interpreter = adopt_own(*new Interpreter); - interpreter->m_global_object = interpreter->heap().allocate<GlobalObjectType>(forward<Args>(args)...); + interpreter->m_global_object = interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...); static_cast<GlobalObjectType*>(interpreter->m_global_object)->initialize(); return interpreter; } @@ -173,7 +173,7 @@ public: Value throw_exception(Exception*); Value throw_exception(Value value) { - return throw_exception(heap().allocate<Exception>(value)); + return throw_exception(heap().allocate<Exception>(global_object(), value)); } template<typename T, typename... Args> diff --git a/Libraries/LibJS/Runtime/Accessor.h b/Libraries/LibJS/Runtime/Accessor.h index 82ade250a2..0bbf945914 100644 --- a/Libraries/LibJS/Runtime/Accessor.h +++ b/Libraries/LibJS/Runtime/Accessor.h @@ -37,7 +37,7 @@ class Accessor final : public Cell { public: static Accessor* create(Interpreter& interpreter, Function* getter, Function* setter) { - return interpreter.heap().allocate<Accessor>(getter, setter); + return interpreter.heap().allocate<Accessor>(interpreter.global_object(), getter, setter); } Accessor(Function* getter, Function* setter) diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index 1c58dacc05..950334b721 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -36,7 +36,7 @@ namespace JS { Array* Array::create(GlobalObject& global_object) { auto& interpreter = global_object.interpreter(); - return interpreter.heap().allocate<Array>(*global_object.array_prototype()); + return interpreter.heap().allocate<Array>(global_object, *global_object.array_prototype()); } Array::Array(Object& prototype) diff --git a/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Libraries/LibJS/Runtime/ArrayConstructor.cpp index c72a36299b..5758602e31 100644 --- a/Libraries/LibJS/Runtime/ArrayConstructor.cpp +++ b/Libraries/LibJS/Runtime/ArrayConstructor.cpp @@ -36,10 +36,20 @@ namespace JS { -ArrayConstructor::ArrayConstructor() - : NativeFunction("Array", *interpreter().global_object().function_prototype()) +ArrayConstructor::ArrayConstructor(GlobalObject& global_object) + : NativeFunction("Array", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().array_prototype(), 0); +} + +ArrayConstructor::~ArrayConstructor() +{ +} + +void ArrayConstructor::initialize(Interpreter& interpreter, GlobalObject& global_object) +{ + NativeFunction::initialize(interpreter, global_object); + + define_property("prototype", global_object.array_prototype(), 0); define_property("length", Value(1), Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; @@ -47,10 +57,6 @@ ArrayConstructor::ArrayConstructor() define_native_function("of", of, 0, attr); } -ArrayConstructor::~ArrayConstructor() -{ -} - Value ArrayConstructor::call(Interpreter& interpreter) { if (interpreter.argument_count() <= 0) diff --git a/Libraries/LibJS/Runtime/ArrayConstructor.h b/Libraries/LibJS/Runtime/ArrayConstructor.h index b2b666aa4b..182b2a3e2a 100644 --- a/Libraries/LibJS/Runtime/ArrayConstructor.h +++ b/Libraries/LibJS/Runtime/ArrayConstructor.h @@ -32,7 +32,8 @@ namespace JS { class ArrayConstructor final : public NativeFunction { public: - ArrayConstructor(); + explicit ArrayConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~ArrayConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index febf120773..b9bbe260ea 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -41,8 +41,12 @@ namespace JS { -ArrayPrototype::ArrayPrototype() - : Object(interpreter().global_object().object_prototype()) +ArrayPrototype::ArrayPrototype(GlobalObject& global_object) + : Object(global_object.object_prototype()) +{ +} + +void ArrayPrototype::initialize(Interpreter&, GlobalObject&) { u8 attr = Attribute::Writable | Attribute::Configurable; diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index 678db7ca89..2a1969ab08 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -33,7 +33,8 @@ namespace JS { class ArrayPrototype final : public Object { public: - ArrayPrototype(); + ArrayPrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~ArrayPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/BigInt.cpp b/Libraries/LibJS/Runtime/BigInt.cpp index 9b62e82d9e..3b7d97c4d7 100644 --- a/Libraries/LibJS/Runtime/BigInt.cpp +++ b/Libraries/LibJS/Runtime/BigInt.cpp @@ -42,7 +42,7 @@ BigInt::~BigInt() BigInt* js_bigint(Heap& heap, Crypto::SignedBigInteger big_integer) { - return heap.allocate<BigInt>(move(big_integer)); + return heap.allocate<BigInt>(heap.interpreter().global_object(), move(big_integer)); } BigInt* js_bigint(Interpreter& interpreter, Crypto::SignedBigInteger big_integer) diff --git a/Libraries/LibJS/Runtime/BigIntConstructor.cpp b/Libraries/LibJS/Runtime/BigIntConstructor.cpp index db6b7d9356..a8e888608d 100644 --- a/Libraries/LibJS/Runtime/BigIntConstructor.cpp +++ b/Libraries/LibJS/Runtime/BigIntConstructor.cpp @@ -34,10 +34,14 @@ namespace JS { -BigIntConstructor::BigIntConstructor() - : NativeFunction("BigInt", *interpreter().global_object().function_prototype()) +BigIntConstructor::BigIntConstructor(GlobalObject& global_object) + : NativeFunction("BigInt", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().bigint_prototype(), 0); +} + +void BigIntConstructor::initialize(Interpreter&, GlobalObject& global_object) +{ + define_property("prototype", global_object.bigint_prototype(), 0); define_property("length", Value(1), Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; diff --git a/Libraries/LibJS/Runtime/BigIntConstructor.h b/Libraries/LibJS/Runtime/BigIntConstructor.h index 13bbb987c4..4a9adcf6a1 100644 --- a/Libraries/LibJS/Runtime/BigIntConstructor.h +++ b/Libraries/LibJS/Runtime/BigIntConstructor.h @@ -32,7 +32,8 @@ namespace JS { class BigIntConstructor final : public NativeFunction { public: - BigIntConstructor(); + explicit BigIntConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~BigIntConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/BigIntObject.cpp b/Libraries/LibJS/Runtime/BigIntObject.cpp index 046761e1c2..6955ac93ad 100644 --- a/Libraries/LibJS/Runtime/BigIntObject.cpp +++ b/Libraries/LibJS/Runtime/BigIntObject.cpp @@ -33,7 +33,7 @@ namespace JS { BigIntObject* BigIntObject::create(GlobalObject& global_object, BigInt& bigint) { - return global_object.heap().allocate<BigIntObject>(bigint, *global_object.bigint_prototype()); + return global_object.heap().allocate<BigIntObject>(global_object, bigint, *global_object.bigint_prototype()); } BigIntObject::BigIntObject(BigInt& bigint, Object& prototype) diff --git a/Libraries/LibJS/Runtime/BigIntPrototype.cpp b/Libraries/LibJS/Runtime/BigIntPrototype.cpp index 4e624b4ebd..483d5a46ee 100644 --- a/Libraries/LibJS/Runtime/BigIntPrototype.cpp +++ b/Libraries/LibJS/Runtime/BigIntPrototype.cpp @@ -32,8 +32,12 @@ namespace JS { -BigIntPrototype::BigIntPrototype() - : Object(interpreter().global_object().object_prototype()) +BigIntPrototype::BigIntPrototype(GlobalObject& global_object) + : Object(global_object.object_prototype()) +{ +} + +void BigIntPrototype::initialize(Interpreter&, GlobalObject&) { u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function("toString", to_string, 0, attr); diff --git a/Libraries/LibJS/Runtime/BigIntPrototype.h b/Libraries/LibJS/Runtime/BigIntPrototype.h index ebd17a3936..b1aa29c81f 100644 --- a/Libraries/LibJS/Runtime/BigIntPrototype.h +++ b/Libraries/LibJS/Runtime/BigIntPrototype.h @@ -32,7 +32,8 @@ namespace JS { class BigIntPrototype final : public Object { public: - BigIntPrototype(); + explicit BigIntPrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~BigIntPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/BooleanConstructor.cpp b/Libraries/LibJS/Runtime/BooleanConstructor.cpp index e7b5eb8d7f..95a7b82615 100644 --- a/Libraries/LibJS/Runtime/BooleanConstructor.cpp +++ b/Libraries/LibJS/Runtime/BooleanConstructor.cpp @@ -33,10 +33,14 @@ namespace JS { -BooleanConstructor::BooleanConstructor() - : NativeFunction("Boolean", *interpreter().global_object().function_prototype()) +BooleanConstructor::BooleanConstructor(GlobalObject& global_object) + : NativeFunction("Boolean", *global_object.function_prototype()) { - define_property("prototype", Value(interpreter().global_object().boolean_prototype()), 0); +} + +void BooleanConstructor::initialize(Interpreter&, GlobalObject& global_object) +{ + define_property("prototype", Value(global_object.boolean_prototype()), 0); define_property("length", Value(1), Attribute::Configurable); } diff --git a/Libraries/LibJS/Runtime/BooleanConstructor.h b/Libraries/LibJS/Runtime/BooleanConstructor.h index ea98181b7c..22b559cd2b 100644 --- a/Libraries/LibJS/Runtime/BooleanConstructor.h +++ b/Libraries/LibJS/Runtime/BooleanConstructor.h @@ -32,7 +32,8 @@ namespace JS { class BooleanConstructor final : public NativeFunction { public: - BooleanConstructor(); + explicit BooleanConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~BooleanConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/BooleanObject.cpp b/Libraries/LibJS/Runtime/BooleanObject.cpp index 8537f5a1d8..2d2acb99de 100644 --- a/Libraries/LibJS/Runtime/BooleanObject.cpp +++ b/Libraries/LibJS/Runtime/BooleanObject.cpp @@ -33,7 +33,7 @@ namespace JS { BooleanObject* BooleanObject::create(GlobalObject& global_object, bool value) { auto& interpreter = global_object.interpreter(); - return interpreter.heap().allocate<BooleanObject>(value, *global_object.boolean_prototype()); + return interpreter.heap().allocate<BooleanObject>(global_object, value, *global_object.boolean_prototype()); } BooleanObject::BooleanObject(bool value, Object& prototype) diff --git a/Libraries/LibJS/Runtime/BooleanPrototype.cpp b/Libraries/LibJS/Runtime/BooleanPrototype.cpp index 572e8c3c7d..1942730e25 100644 --- a/Libraries/LibJS/Runtime/BooleanPrototype.cpp +++ b/Libraries/LibJS/Runtime/BooleanPrototype.cpp @@ -32,8 +32,12 @@ namespace JS { -BooleanPrototype::BooleanPrototype() - : BooleanObject(false, *interpreter().global_object().object_prototype()) +BooleanPrototype::BooleanPrototype(GlobalObject& global_object) + : BooleanObject(false, *global_object.object_prototype()) +{ +} + +void BooleanPrototype::initialize(Interpreter&, GlobalObject&) { define_native_function("toString", to_string, 0, Attribute::Writable | Attribute::Configurable); define_native_function("valueOf", value_of, 0, Attribute::Writable | Attribute::Configurable); diff --git a/Libraries/LibJS/Runtime/BooleanPrototype.h b/Libraries/LibJS/Runtime/BooleanPrototype.h index e587a91b04..1995b51129 100644 --- a/Libraries/LibJS/Runtime/BooleanPrototype.h +++ b/Libraries/LibJS/Runtime/BooleanPrototype.h @@ -32,7 +32,8 @@ namespace JS { class BooleanPrototype final : public BooleanObject { public: - BooleanPrototype(); + explicit BooleanPrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~BooleanPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/Cell.h b/Libraries/LibJS/Runtime/Cell.h index e08a31ac62..8bebdbf63c 100644 --- a/Libraries/LibJS/Runtime/Cell.h +++ b/Libraries/LibJS/Runtime/Cell.h @@ -37,7 +37,8 @@ class Cell { AK_MAKE_NONMOVABLE(Cell); public: - virtual ~Cell() {} + virtual void initialize(Interpreter&, GlobalObject&) { } + virtual ~Cell() { } bool is_marked() const { return m_mark; } void set_marked(bool b) { m_mark = b; } @@ -56,14 +57,14 @@ public: virtual void visit_impl(Cell*) = 0; }; - virtual void visit_children(Visitor&) {} + virtual void visit_children(Visitor&) { } Heap& heap() const; Interpreter& interpreter(); Interpreter& interpreter() const; protected: - Cell() {} + Cell() { } private: bool m_mark { false }; diff --git a/Libraries/LibJS/Runtime/Date.cpp b/Libraries/LibJS/Runtime/Date.cpp index 49ea57cb61..1ddee4e411 100644 --- a/Libraries/LibJS/Runtime/Date.cpp +++ b/Libraries/LibJS/Runtime/Date.cpp @@ -34,7 +34,7 @@ namespace JS { Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, u16 milliseconds) { - return global_object.heap().allocate<Date>(datetime, milliseconds, *global_object.date_prototype()); + return global_object.heap().allocate<Date>(global_object, datetime, milliseconds, *global_object.date_prototype()); } Date::Date(Core::DateTime datetime, u16 milliseconds, Object& prototype) diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp index d00b1e6323..41a02dd903 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -34,8 +34,12 @@ namespace JS { -DateConstructor::DateConstructor() - : NativeFunction("Date", *interpreter().global_object().function_prototype()) +DateConstructor::DateConstructor(GlobalObject& global_object) + : NativeFunction("Date", *global_object.function_prototype()) +{ +} + +void DateConstructor::initialize(Interpreter&, GlobalObject&) { define_property("prototype", interpreter().global_object().date_prototype(), 0); define_property("length", Value(7), Attribute::Configurable); diff --git a/Libraries/LibJS/Runtime/DateConstructor.h b/Libraries/LibJS/Runtime/DateConstructor.h index 9e70bca833..68b8dd6305 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.h +++ b/Libraries/LibJS/Runtime/DateConstructor.h @@ -32,7 +32,8 @@ namespace JS { class DateConstructor final : public NativeFunction { public: - DateConstructor(); + explicit DateConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~DateConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/DatePrototype.cpp b/Libraries/LibJS/Runtime/DatePrototype.cpp index 08bdf0e626..8aae092237 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -48,8 +48,12 @@ static Date* this_date_from_interpreter(Interpreter& interpreter) return static_cast<Date*>(this_object); } -DatePrototype::DatePrototype() - : Object(interpreter().global_object().object_prototype()) +DatePrototype::DatePrototype(GlobalObject& global_object) + : Object(global_object.object_prototype()) +{ +} + +void DatePrototype::initialize(Interpreter&, GlobalObject&) { u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function("getDate", get_date, 0, attr); diff --git a/Libraries/LibJS/Runtime/DatePrototype.h b/Libraries/LibJS/Runtime/DatePrototype.h index 212705e314..2e10ebb00a 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.h +++ b/Libraries/LibJS/Runtime/DatePrototype.h @@ -32,7 +32,8 @@ namespace JS { class DatePrototype final : public Object { public: - DatePrototype(); + explicit DatePrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~DatePrototype() override; private: diff --git a/Libraries/LibJS/Runtime/Error.cpp b/Libraries/LibJS/Runtime/Error.cpp index 1c6439ab19..3056fee031 100644 --- a/Libraries/LibJS/Runtime/Error.cpp +++ b/Libraries/LibJS/Runtime/Error.cpp @@ -33,7 +33,7 @@ namespace JS { Error* Error::create(GlobalObject& global_object, const FlyString& name, const String& message) { auto& interpreter = global_object.interpreter(); - return interpreter.heap().allocate<Error>(name, message, *global_object.error_prototype()); + return interpreter.heap().allocate<Error>(global_object, name, message, *global_object.error_prototype()); } Error::Error(const FlyString& name, const String& message, Object& prototype) @@ -47,16 +47,16 @@ Error::~Error() { } -#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ - ClassName* ClassName::create(GlobalObject& global_object, const String& message) \ - { \ - return global_object.heap().allocate<ClassName>(message, *global_object.snake_name##_prototype()); \ - } \ - ClassName::ClassName(const String& message, Object& prototype) \ - : Error(#ClassName, message, prototype) \ - { \ - } \ - ClassName::~ClassName() { } \ +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + ClassName* ClassName::create(GlobalObject& global_object, const String& message) \ + { \ + return global_object.heap().allocate<ClassName>(global_object, message, *global_object.snake_name##_prototype()); \ + } \ + ClassName::ClassName(const String& message, Object& prototype) \ + : Error(#ClassName, message, prototype) \ + { \ + } \ + ClassName::~ClassName() { } \ const char* ClassName::class_name() const { return #ClassName; } JS_ENUMERATE_ERROR_SUBCLASSES diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.cpp b/Libraries/LibJS/Runtime/ErrorConstructor.cpp index 0973ed37be..b3213798ef 100644 --- a/Libraries/LibJS/Runtime/ErrorConstructor.cpp +++ b/Libraries/LibJS/Runtime/ErrorConstructor.cpp @@ -31,10 +31,14 @@ namespace JS { -ErrorConstructor::ErrorConstructor() - : NativeFunction("Error", *interpreter().global_object().function_prototype()) +ErrorConstructor::ErrorConstructor(GlobalObject& global_object) + : NativeFunction("Error", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().error_prototype(), 0); +} + +void ErrorConstructor::initialize(Interpreter&, GlobalObject& global_object) +{ + define_property("prototype", global_object.error_prototype(), 0); define_property("length", Value(1), Attribute::Configurable); } @@ -59,10 +63,13 @@ Value ErrorConstructor::construct(Interpreter& interpreter) } #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ - ConstructorName::ConstructorName() \ - : NativeFunction(*interpreter().global_object().function_prototype()) \ + ConstructorName::ConstructorName(GlobalObject& global_object) \ + : NativeFunction(*global_object.function_prototype()) \ + { \ + } \ + void ConstructorName::initialize(Interpreter&, GlobalObject& global_object) \ { \ - define_property("prototype", interpreter().global_object().snake_name##_prototype(), 0); \ + define_property("prototype", global_object.snake_name##_prototype(), 0); \ define_property("length", Value(1), Attribute::Configurable); \ } \ ConstructorName::~ConstructorName() { } \ diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.h b/Libraries/LibJS/Runtime/ErrorConstructor.h index 2e6c4ac337..fcc610b08d 100644 --- a/Libraries/LibJS/Runtime/ErrorConstructor.h +++ b/Libraries/LibJS/Runtime/ErrorConstructor.h @@ -33,7 +33,8 @@ namespace JS { class ErrorConstructor final : public NativeFunction { public: - ErrorConstructor(); + explicit ErrorConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~ErrorConstructor() override; virtual Value call(Interpreter&) override; @@ -47,7 +48,8 @@ private: #define DECLARE_ERROR_SUBCLASS_CONSTRUCTOR(ClassName, snake_name, PrototypeName, ConstructorName) \ class ConstructorName final : public NativeFunction { \ public: \ - ConstructorName(); \ + explicit ConstructorName(GlobalObject&); \ + virtual void initialize(Interpreter&, GlobalObject&) override; \ virtual ~ConstructorName() override; \ virtual Value call(Interpreter&) override; \ virtual Value construct(Interpreter&) override; \ diff --git a/Libraries/LibJS/Runtime/ErrorPrototype.cpp b/Libraries/LibJS/Runtime/ErrorPrototype.cpp index d3b6dcafee..4e5098b046 100644 --- a/Libraries/LibJS/Runtime/ErrorPrototype.cpp +++ b/Libraries/LibJS/Runtime/ErrorPrototype.cpp @@ -35,8 +35,12 @@ namespace JS { -ErrorPrototype::ErrorPrototype() - : Object(interpreter().global_object().object_prototype()) +ErrorPrototype::ErrorPrototype(GlobalObject& global_object) + : Object(global_object.object_prototype()) +{ +} + +void ErrorPrototype::initialize(Interpreter&, GlobalObject&) { u8 attr = Attribute::Writable | Attribute::Configurable; define_native_property("name", name_getter, name_setter, attr); @@ -117,8 +121,8 @@ JS_DEFINE_NATIVE_FUNCTION(ErrorPrototype::to_string) } #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ - PrototypeName::PrototypeName() \ - : Object(interpreter().global_object().error_prototype()) \ + PrototypeName::PrototypeName(GlobalObject& global_object) \ + : Object(global_object.error_prototype()) \ { \ } \ PrototypeName::~PrototypeName() { } \ diff --git a/Libraries/LibJS/Runtime/ErrorPrototype.h b/Libraries/LibJS/Runtime/ErrorPrototype.h index 183bd26b14..a70fe4f9df 100644 --- a/Libraries/LibJS/Runtime/ErrorPrototype.h +++ b/Libraries/LibJS/Runtime/ErrorPrototype.h @@ -32,7 +32,8 @@ namespace JS { class ErrorPrototype final : public Object { public: - ErrorPrototype(); + explicit ErrorPrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~ErrorPrototype() override; private: @@ -49,7 +50,8 @@ private: #define DECLARE_ERROR_SUBCLASS_PROTOTYPE(ClassName, snake_name, PrototypeName, ConstructorName) \ class PrototypeName final : public Object { \ public: \ - PrototypeName(); \ + explicit PrototypeName(GlobalObject&); \ + virtual void initialize(Interpreter&, GlobalObject&) override { } \ virtual ~PrototypeName() override; \ \ private: \ diff --git a/Libraries/LibJS/Runtime/Function.cpp b/Libraries/LibJS/Runtime/Function.cpp index c6329cf2c3..7585f9a4a1 100644 --- a/Libraries/LibJS/Runtime/Function.cpp +++ b/Libraries/LibJS/Runtime/Function.cpp @@ -82,7 +82,7 @@ BoundFunction* Function::bind(Value bound_this_value, Vector<Value> arguments) auto all_bound_arguments = bound_arguments(); all_bound_arguments.append(move(arguments)); - return interpreter().heap().allocate<BoundFunction>(target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype); + return interpreter().heap().allocate<BoundFunction>(global_object(), target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype); } void Function::visit_children(Visitor& visitor) diff --git a/Libraries/LibJS/Runtime/Function.h b/Libraries/LibJS/Runtime/Function.h index e5964b3578..9e254f4bf6 100644 --- a/Libraries/LibJS/Runtime/Function.h +++ b/Libraries/LibJS/Runtime/Function.h @@ -34,6 +34,7 @@ namespace JS { class Function : public Object { public: virtual ~Function(); + virtual void initialize(Interpreter&, GlobalObject&) override { } virtual Value call(Interpreter&) = 0; virtual Value construct(Interpreter&) = 0; diff --git a/Libraries/LibJS/Runtime/FunctionConstructor.cpp b/Libraries/LibJS/Runtime/FunctionConstructor.cpp index 687db60748..69ae5bfecf 100644 --- a/Libraries/LibJS/Runtime/FunctionConstructor.cpp +++ b/Libraries/LibJS/Runtime/FunctionConstructor.cpp @@ -35,10 +35,14 @@ namespace JS { -FunctionConstructor::FunctionConstructor() - : NativeFunction("Function", *interpreter().global_object().function_prototype()) +FunctionConstructor::FunctionConstructor(GlobalObject& global_object) + : NativeFunction("Function", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().function_prototype(), 0); +} + +void FunctionConstructor::initialize(Interpreter&, GlobalObject& global_object) +{ + define_property("prototype", global_object.function_prototype(), 0); define_property("length", Value(1), Attribute::Configurable); } diff --git a/Libraries/LibJS/Runtime/FunctionConstructor.h b/Libraries/LibJS/Runtime/FunctionConstructor.h index 2a891be6ae..e84e7cddba 100644 --- a/Libraries/LibJS/Runtime/FunctionConstructor.h +++ b/Libraries/LibJS/Runtime/FunctionConstructor.h @@ -32,7 +32,8 @@ namespace JS { class FunctionConstructor final : public NativeFunction { public: - FunctionConstructor(); + explicit FunctionConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~FunctionConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Libraries/LibJS/Runtime/FunctionPrototype.cpp index 48e699f53e..f82236bcaa 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.cpp +++ b/Libraries/LibJS/Runtime/FunctionPrototype.cpp @@ -38,12 +38,12 @@ namespace JS { -FunctionPrototype::FunctionPrototype() - : Object(interpreter().global_object().object_prototype()) +FunctionPrototype::FunctionPrototype(GlobalObject& global_object) + : Object(global_object.object_prototype()) { } -void FunctionPrototype::initialize() +void FunctionPrototype::initialize(Interpreter&, GlobalObject&) { u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function("apply", apply, 2, attr); diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.h b/Libraries/LibJS/Runtime/FunctionPrototype.h index 495db7b317..bb5c3f6072 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.h +++ b/Libraries/LibJS/Runtime/FunctionPrototype.h @@ -32,9 +32,8 @@ namespace JS { class FunctionPrototype final : public Object { public: - FunctionPrototype(); - void initialize(); - + explicit FunctionPrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~FunctionPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index cab6083a69..a524db8407 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -71,16 +71,16 @@ GlobalObject::GlobalObject() void GlobalObject::initialize() { // These are done first since other prototypes depend on their presence. - m_empty_object_shape = heap().allocate<Shape>(*this); - m_object_prototype = heap().allocate<ObjectPrototype>(); - m_function_prototype = heap().allocate<FunctionPrototype>(); + m_empty_object_shape = heap().allocate<Shape>(*this, *this); + m_object_prototype = heap().allocate_without_global_object<ObjectPrototype>(*this); + m_function_prototype = heap().allocate_without_global_object<FunctionPrototype>(*this); - static_cast<FunctionPrototype*>(m_function_prototype)->initialize(); - static_cast<ObjectPrototype*>(m_object_prototype)->initialize(); + static_cast<FunctionPrototype*>(m_function_prototype)->initialize(heap().interpreter(), *this); + static_cast<ObjectPrototype*>(m_object_prototype)->initialize(heap().interpreter(), *this); #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ if (!m_##snake_name##_prototype) \ - m_##snake_name##_prototype = heap().allocate<PrototypeName>(); + m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this); JS_ENUMERATE_BUILTIN_TYPES #undef __JS_ENUMERATE @@ -95,10 +95,10 @@ void GlobalObject::initialize() define_property("undefined", js_undefined(), 0); define_property("globalThis", this, attr); - define_property("console", heap().allocate<ConsoleObject>(), attr); - define_property("Math", heap().allocate<MathObject>(), attr); - define_property("JSON", heap().allocate<JSONObject>(), attr); - define_property("Reflect", heap().allocate<ReflectObject>(), attr); + define_property("console", heap().allocate<ConsoleObject>(*this), attr); + define_property("Math", heap().allocate<MathObject>(*this), attr); + define_property("JSON", heap().allocate<JSONObject>(*this), attr); + define_property("Reflect", heap().allocate<ReflectObject>(*this), attr); add_constructor("Array", m_array_constructor, *m_array_prototype); add_constructor("BigInt", m_bigint_constructor, *m_bigint_prototype); diff --git a/Libraries/LibJS/Runtime/GlobalObject.h b/Libraries/LibJS/Runtime/GlobalObject.h index 4e67bcd485..f4ca8ce59c 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Libraries/LibJS/Runtime/GlobalObject.h @@ -72,7 +72,7 @@ private: template<typename ConstructorType> inline void GlobalObject::add_constructor(const FlyString& property_name, ConstructorType*& constructor, Object& prototype) { - constructor = heap().allocate<ConstructorType>(); + constructor = heap().allocate<ConstructorType>(*this, *this); constructor->define_property("name", js_string(heap(), property_name), Attribute::Configurable); if (interpreter().exception()) return; diff --git a/Libraries/LibJS/Runtime/NativeFunction.cpp b/Libraries/LibJS/Runtime/NativeFunction.cpp index 0c0972bcc6..070632bef0 100644 --- a/Libraries/LibJS/Runtime/NativeFunction.cpp +++ b/Libraries/LibJS/Runtime/NativeFunction.cpp @@ -33,7 +33,7 @@ namespace JS { NativeFunction* NativeFunction::create(Interpreter&, GlobalObject& global_object, const FlyString& name, AK::Function<Value(Interpreter&, GlobalObject&)> function) { - return global_object.heap().allocate<NativeFunction>(name, move(function), *global_object.function_prototype()); + return global_object.heap().allocate<NativeFunction>(global_object, name, move(function), *global_object.function_prototype()); } NativeFunction::NativeFunction(Object& prototype) diff --git a/Libraries/LibJS/Runtime/NativeFunction.h b/Libraries/LibJS/Runtime/NativeFunction.h index 34446eef6d..c8478171fc 100644 --- a/Libraries/LibJS/Runtime/NativeFunction.h +++ b/Libraries/LibJS/Runtime/NativeFunction.h @@ -36,6 +36,7 @@ public: static NativeFunction* create(Interpreter&, GlobalObject&, const FlyString& name, AK::Function<Value(Interpreter&, GlobalObject&)>); explicit NativeFunction(const FlyString& name, AK::Function<Value(Interpreter&, GlobalObject&)>, Object& prototype); + virtual void initialize(Interpreter&, GlobalObject&) override { } virtual ~NativeFunction() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/NumberConstructor.cpp b/Libraries/LibJS/Runtime/NumberConstructor.cpp index 14e6b4cb68..8e2df5a3f0 100644 --- a/Libraries/LibJS/Runtime/NumberConstructor.cpp +++ b/Libraries/LibJS/Runtime/NumberConstructor.cpp @@ -37,8 +37,12 @@ namespace JS { -NumberConstructor::NumberConstructor() - : NativeFunction("Number", *interpreter().global_object().function_prototype()) +NumberConstructor::NumberConstructor(GlobalObject& global_object) + : NativeFunction("Number", *global_object.function_prototype()) +{ +} + +void NumberConstructor::initialize(Interpreter&, GlobalObject&) { u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function("isFinite", is_finite, 1, attr); diff --git a/Libraries/LibJS/Runtime/NumberConstructor.h b/Libraries/LibJS/Runtime/NumberConstructor.h index f9e1546c6b..bcaff30162 100644 --- a/Libraries/LibJS/Runtime/NumberConstructor.h +++ b/Libraries/LibJS/Runtime/NumberConstructor.h @@ -32,7 +32,8 @@ namespace JS { class NumberConstructor final : public NativeFunction { public: - NumberConstructor(); + explicit NumberConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~NumberConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/NumberObject.cpp b/Libraries/LibJS/Runtime/NumberObject.cpp index 46fdf73052..b97806e53e 100644 --- a/Libraries/LibJS/Runtime/NumberObject.cpp +++ b/Libraries/LibJS/Runtime/NumberObject.cpp @@ -35,7 +35,7 @@ namespace JS { NumberObject* NumberObject::create(GlobalObject& global_object, double value) { - return global_object.heap().allocate<NumberObject>(value, *global_object.number_prototype()); + return global_object.heap().allocate<NumberObject>(global_object, value, *global_object.number_prototype()); } NumberObject::NumberObject(double value, Object& prototype) diff --git a/Libraries/LibJS/Runtime/NumberPrototype.cpp b/Libraries/LibJS/Runtime/NumberPrototype.cpp index 3d17abf394..85c874ab04 100644 --- a/Libraries/LibJS/Runtime/NumberPrototype.cpp +++ b/Libraries/LibJS/Runtime/NumberPrototype.cpp @@ -30,8 +30,8 @@ namespace JS { -NumberPrototype::NumberPrototype() - : NumberObject(0, *interpreter().global_object().object_prototype()) +NumberPrototype::NumberPrototype(GlobalObject& global_object) + : NumberObject(0, *global_object.object_prototype()) { } diff --git a/Libraries/LibJS/Runtime/NumberPrototype.h b/Libraries/LibJS/Runtime/NumberPrototype.h index d60823128d..3b1e224dac 100644 --- a/Libraries/LibJS/Runtime/NumberPrototype.h +++ b/Libraries/LibJS/Runtime/NumberPrototype.h @@ -32,7 +32,7 @@ namespace JS { class NumberPrototype final : public NumberObject { public: - NumberPrototype(); + explicit NumberPrototype(GlobalObject&); virtual ~NumberPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index 0f36ed527c..544de42add 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -82,7 +82,7 @@ PropertyDescriptor PropertyDescriptor::from_dictionary(Interpreter& interpreter, Object* Object::create_empty(Interpreter&, GlobalObject& global_object) { - return global_object.heap().allocate<Object>(global_object.object_prototype()); + return global_object.heap().allocate<Object>(global_object, global_object.object_prototype()); } Object::Object(Object* prototype) @@ -92,10 +92,14 @@ Object::Object(Object* prototype) set_prototype(prototype); } else { // This is the global object - m_shape = interpreter().heap().allocate<Shape>(static_cast<GlobalObject&>(*this)); + m_shape = interpreter().heap().allocate<Shape>(static_cast<GlobalObject&>(*this), static_cast<GlobalObject&>(*this)); } } +void Object::initialize(Interpreter&, GlobalObject&) +{ +} + Object::~Object() { } @@ -694,7 +698,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>(move(getter), move(setter)), attribute); + return define_property(property_name, heap().allocate<NativeProperty>(global_object(), move(getter), move(setter)), attribute); } void Object::visit_children(Cell::Visitor& visitor) diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index 105ed851b7..6bd5104127 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -57,6 +57,7 @@ public: static Object* create_empty(Interpreter&, GlobalObject&); explicit Object(Object* prototype); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~Object(); enum class GetOwnPropertyMode { diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Libraries/LibJS/Runtime/ObjectConstructor.cpp index 10fa061d90..1de7f1c01a 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -35,10 +35,14 @@ namespace JS { -ObjectConstructor::ObjectConstructor() - : NativeFunction("Object", *interpreter().global_object().function_prototype()) +ObjectConstructor::ObjectConstructor(GlobalObject& global_object) + : NativeFunction("Object", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().object_prototype(), 0); +} + +void ObjectConstructor::initialize(Interpreter&, GlobalObject& global_object) +{ + define_property("prototype", global_object.object_prototype(), 0); define_property("length", Value(1), Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.h b/Libraries/LibJS/Runtime/ObjectConstructor.h index 97d8e6b0a6..57eafb5282 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.h +++ b/Libraries/LibJS/Runtime/ObjectConstructor.h @@ -32,7 +32,8 @@ namespace JS { class ObjectConstructor final : public NativeFunction { public: - ObjectConstructor(); + explicit ObjectConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~ObjectConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Libraries/LibJS/Runtime/ObjectPrototype.cpp index a3ceb3ea63..03e5d68689 100644 --- a/Libraries/LibJS/Runtime/ObjectPrototype.cpp +++ b/Libraries/LibJS/Runtime/ObjectPrototype.cpp @@ -34,12 +34,12 @@ namespace JS { -ObjectPrototype::ObjectPrototype() +ObjectPrototype::ObjectPrototype(GlobalObject&) : Object(nullptr) { } -void ObjectPrototype::initialize() +void ObjectPrototype::initialize(Interpreter&, GlobalObject&) { // This must be called after the constructor has returned, so that the below code // can find the ObjectPrototype through normal paths. diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.h b/Libraries/LibJS/Runtime/ObjectPrototype.h index ceeaa0e092..4f0c6ca4bb 100644 --- a/Libraries/LibJS/Runtime/ObjectPrototype.h +++ b/Libraries/LibJS/Runtime/ObjectPrototype.h @@ -32,9 +32,8 @@ namespace JS { class ObjectPrototype final : public Object { public: - ObjectPrototype(); - void initialize(); - + explicit ObjectPrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~ObjectPrototype() override; // public to serve as intrinsic function %Object.prototype.toString% diff --git a/Libraries/LibJS/Runtime/PrimitiveString.cpp b/Libraries/LibJS/Runtime/PrimitiveString.cpp index 76c8024121..428295e861 100644 --- a/Libraries/LibJS/Runtime/PrimitiveString.cpp +++ b/Libraries/LibJS/Runtime/PrimitiveString.cpp @@ -41,7 +41,7 @@ PrimitiveString::~PrimitiveString() PrimitiveString* js_string(Heap& heap, String string) { - return heap.allocate<PrimitiveString>(move(string)); + return heap.allocate<PrimitiveString>(heap.interpreter().global_object(), move(string)); } PrimitiveString* js_string(Interpreter& interpreter, String string) diff --git a/Libraries/LibJS/Runtime/ProxyConstructor.cpp b/Libraries/LibJS/Runtime/ProxyConstructor.cpp index 3bd89190f8..5a715b7394 100644 --- a/Libraries/LibJS/Runtime/ProxyConstructor.cpp +++ b/Libraries/LibJS/Runtime/ProxyConstructor.cpp @@ -33,10 +33,14 @@ namespace JS { -ProxyConstructor::ProxyConstructor() - : NativeFunction("Proxy", *interpreter().global_object().function_prototype()) +ProxyConstructor::ProxyConstructor(GlobalObject& global_object) + : NativeFunction("Proxy", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().proxy_prototype(), 0); +} + +void ProxyConstructor::initialize(Interpreter&, GlobalObject& global_object) +{ + define_property("prototype", global_object.proxy_prototype(), 0); define_property("length", Value(2), Attribute::Configurable); } diff --git a/Libraries/LibJS/Runtime/ProxyConstructor.h b/Libraries/LibJS/Runtime/ProxyConstructor.h index 09cedaa10f..10d9277280 100644 --- a/Libraries/LibJS/Runtime/ProxyConstructor.h +++ b/Libraries/LibJS/Runtime/ProxyConstructor.h @@ -32,7 +32,8 @@ namespace JS { class ProxyConstructor final : public NativeFunction { public: - ProxyConstructor(); + explicit ProxyConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~ProxyConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/ProxyObject.cpp b/Libraries/LibJS/Runtime/ProxyObject.cpp index 2df848c4ee..fb8b62e1fc 100644 --- a/Libraries/LibJS/Runtime/ProxyObject.cpp +++ b/Libraries/LibJS/Runtime/ProxyObject.cpp @@ -59,7 +59,7 @@ bool static is_compatible_property_descriptor(Interpreter& interpreter, bool is_ ProxyObject* ProxyObject::create(GlobalObject& global_object, Object& target, Object& handler) { - return global_object.heap().allocate<ProxyObject>(target, handler, *global_object.proxy_prototype()); + return global_object.heap().allocate<ProxyObject>(global_object, target, handler, *global_object.proxy_prototype()); } ProxyObject::ProxyObject(Object& target, Object& handler, Object& prototype) diff --git a/Libraries/LibJS/Runtime/ProxyPrototype.cpp b/Libraries/LibJS/Runtime/ProxyPrototype.cpp index bc430b39ee..aea07ebd10 100644 --- a/Libraries/LibJS/Runtime/ProxyPrototype.cpp +++ b/Libraries/LibJS/Runtime/ProxyPrototype.cpp @@ -34,8 +34,8 @@ namespace JS { -ProxyPrototype::ProxyPrototype() - : Object(interpreter().global_object().object_prototype()) +ProxyPrototype::ProxyPrototype(GlobalObject& global_object) + : Object(global_object.object_prototype()) { } diff --git a/Libraries/LibJS/Runtime/ProxyPrototype.h b/Libraries/LibJS/Runtime/ProxyPrototype.h index cb278ed60b..e0ec6f23a1 100644 --- a/Libraries/LibJS/Runtime/ProxyPrototype.h +++ b/Libraries/LibJS/Runtime/ProxyPrototype.h @@ -32,7 +32,7 @@ namespace JS { class ProxyPrototype final : public Object { public: - ProxyPrototype(); + explicit ProxyPrototype(GlobalObject&); virtual ~ProxyPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/RegExpConstructor.cpp b/Libraries/LibJS/Runtime/RegExpConstructor.cpp index 78a81d8f90..ad70bd051f 100644 --- a/Libraries/LibJS/Runtime/RegExpConstructor.cpp +++ b/Libraries/LibJS/Runtime/RegExpConstructor.cpp @@ -32,10 +32,14 @@ namespace JS { -RegExpConstructor::RegExpConstructor() - : NativeFunction("RegExp", *interpreter().global_object().function_prototype()) +RegExpConstructor::RegExpConstructor(GlobalObject& global_object) + : NativeFunction("RegExp", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().regexp_prototype(), 0); +} + +void RegExpConstructor::initialize(Interpreter&, GlobalObject& global_object) +{ + define_property("prototype", global_object.regexp_prototype(), 0); define_property("length", Value(2), Attribute::Configurable); } diff --git a/Libraries/LibJS/Runtime/RegExpConstructor.h b/Libraries/LibJS/Runtime/RegExpConstructor.h index 76f4321052..7da95a71d0 100644 --- a/Libraries/LibJS/Runtime/RegExpConstructor.h +++ b/Libraries/LibJS/Runtime/RegExpConstructor.h @@ -32,7 +32,8 @@ namespace JS { class RegExpConstructor final : public NativeFunction { public: - RegExpConstructor(); + explicit RegExpConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~RegExpConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/RegExpObject.cpp b/Libraries/LibJS/Runtime/RegExpObject.cpp index d6ce164e98..34f5e424b8 100644 --- a/Libraries/LibJS/Runtime/RegExpObject.cpp +++ b/Libraries/LibJS/Runtime/RegExpObject.cpp @@ -37,7 +37,7 @@ namespace JS { RegExpObject* RegExpObject::create(GlobalObject& global_object, String content, String flags) { - return global_object.heap().allocate<RegExpObject>(content, flags, *global_object.regexp_prototype()); + return global_object.heap().allocate<RegExpObject>(global_object, content, flags, *global_object.regexp_prototype()); } RegExpObject::RegExpObject(String content, String flags, Object& prototype) diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 629d3eadc3..0b110ec32e 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -35,8 +35,8 @@ namespace JS { -RegExpPrototype::RegExpPrototype() - : RegExpObject({}, {}, *interpreter().global_object().object_prototype()) +RegExpPrototype::RegExpPrototype(GlobalObject& global_object) + : RegExpObject({}, {}, *global_object.object_prototype()) { } diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.h b/Libraries/LibJS/Runtime/RegExpPrototype.h index 07c5d51b3a..cab917e6e3 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.h +++ b/Libraries/LibJS/Runtime/RegExpPrototype.h @@ -32,7 +32,7 @@ namespace JS { class RegExpPrototype final : public RegExpObject { public: - RegExpPrototype(); + explicit RegExpPrototype(GlobalObject&); virtual ~RegExpPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index 17749e85c6..2962f60148 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -49,7 +49,7 @@ static ScriptFunction* script_function_from(Interpreter& interpreter) ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, bool is_arrow_function) { - return global_object.heap().allocate<ScriptFunction>(name, body, move(parameters), m_function_length, parent_environment, *global_object.function_prototype(), is_arrow_function); + return global_object.heap().allocate<ScriptFunction>(global_object, name, body, move(parameters), m_function_length, parent_environment, *global_object.function_prototype(), is_arrow_function); } ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_arrow_function) @@ -93,7 +93,7 @@ LexicalEnvironment* ScriptFunction::create_environment() } if (variables.is_empty()) return m_parent_environment; - return heap().allocate<LexicalEnvironment>(move(variables), m_parent_environment); + return heap().allocate<LexicalEnvironment>(global_object(), move(variables), m_parent_environment); } Value ScriptFunction::call(Interpreter& interpreter) diff --git a/Libraries/LibJS/Runtime/Shape.cpp b/Libraries/LibJS/Runtime/Shape.cpp index cf6821e04d..e6e70e58ab 100644 --- a/Libraries/LibJS/Runtime/Shape.cpp +++ b/Libraries/LibJS/Runtime/Shape.cpp @@ -32,7 +32,7 @@ namespace JS { Shape* Shape::create_unique_clone() const { - auto* new_shape = heap().allocate<Shape>(m_global_object); + auto* new_shape = heap().allocate<Shape>(m_global_object, m_global_object); new_shape->m_unique = true; new_shape->m_prototype = m_prototype; ensure_property_table(); @@ -46,7 +46,7 @@ Shape* Shape::create_put_transition(const FlyString& property_name, PropertyAttr TransitionKey key { property_name, attributes }; if (auto* existing_shape = m_forward_transitions.get(key).value_or(nullptr)) return existing_shape; - auto* new_shape = heap().allocate<Shape>(*this, property_name, attributes, TransitionType::Put); + auto* new_shape = heap().allocate<Shape>(m_global_object, *this, property_name, attributes, TransitionType::Put); m_forward_transitions.set(key, new_shape); return new_shape; } @@ -56,14 +56,14 @@ Shape* Shape::create_configure_transition(const FlyString& property_name, Proper TransitionKey key { property_name, attributes }; if (auto* existing_shape = m_forward_transitions.get(key).value_or(nullptr)) return existing_shape; - auto* new_shape = heap().allocate<Shape>(*this, property_name, attributes, TransitionType::Configure); + auto* new_shape = heap().allocate<Shape>(m_global_object, *this, property_name, attributes, TransitionType::Configure); m_forward_transitions.set(key, new_shape); return new_shape; } Shape* Shape::create_prototype_transition(Object* new_prototype) { - return heap().allocate<Shape>(*this, new_prototype); + return heap().allocate<Shape>(m_global_object, *this, new_prototype); } Shape::Shape(GlobalObject& global_object) diff --git a/Libraries/LibJS/Runtime/StringConstructor.cpp b/Libraries/LibJS/Runtime/StringConstructor.cpp index ba31d4acbb..5ea7f941d3 100644 --- a/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -35,10 +35,14 @@ namespace JS { -StringConstructor::StringConstructor() - : NativeFunction("String", *interpreter().global_object().function_prototype()) +StringConstructor::StringConstructor(GlobalObject& global_object) + : NativeFunction("String", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().string_prototype(), 0); +} + +void StringConstructor::initialize(Interpreter&, GlobalObject& global_object) +{ + define_property("prototype", global_object.string_prototype(), 0); define_property("length", Value(1), Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; diff --git a/Libraries/LibJS/Runtime/StringConstructor.h b/Libraries/LibJS/Runtime/StringConstructor.h index 4d6aaa8f18..b6c651bde5 100644 --- a/Libraries/LibJS/Runtime/StringConstructor.h +++ b/Libraries/LibJS/Runtime/StringConstructor.h @@ -32,7 +32,8 @@ namespace JS { class StringConstructor final : public NativeFunction { public: - StringConstructor(); + explicit StringConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~StringConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/StringObject.cpp b/Libraries/LibJS/Runtime/StringObject.cpp index db14c40773..4a86d7bc64 100644 --- a/Libraries/LibJS/Runtime/StringObject.cpp +++ b/Libraries/LibJS/Runtime/StringObject.cpp @@ -36,7 +36,7 @@ namespace JS { StringObject* StringObject::create(GlobalObject& global_object, PrimitiveString& primitive_string) { - return global_object.heap().allocate<StringObject>(primitive_string, *global_object.string_prototype()); + return global_object.heap().allocate<StringObject>(global_object, primitive_string, *global_object.string_prototype()); } StringObject::StringObject(PrimitiveString& string, Object& prototype) diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index f9d1a54827..9139e482d6 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -59,8 +59,12 @@ static String string_from(Interpreter& interpreter) return Value(this_object).to_string(interpreter); } -StringPrototype::StringPrototype() - : StringObject(*js_string(interpreter(), String::empty()), *interpreter().global_object().object_prototype()) +StringPrototype::StringPrototype(GlobalObject& global_object) + : StringObject(*js_string(interpreter(), String::empty()), *global_object.object_prototype()) +{ +} + +void StringPrototype::initialize(Interpreter&, GlobalObject&) { u8 attr = Attribute::Writable | Attribute::Configurable; diff --git a/Libraries/LibJS/Runtime/StringPrototype.h b/Libraries/LibJS/Runtime/StringPrototype.h index bef8d091b0..6df4c469de 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.h +++ b/Libraries/LibJS/Runtime/StringPrototype.h @@ -32,7 +32,8 @@ namespace JS { class StringPrototype final : public StringObject { public: - StringPrototype(); + explicit StringPrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~StringPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/Symbol.cpp b/Libraries/LibJS/Runtime/Symbol.cpp index 2a35aa8d99..25975b5c56 100644 --- a/Libraries/LibJS/Runtime/Symbol.cpp +++ b/Libraries/LibJS/Runtime/Symbol.cpp @@ -42,7 +42,7 @@ Symbol::~Symbol() Symbol* js_symbol(Heap& heap, String description, bool is_global) { - return heap.allocate<Symbol>(move(description), is_global); + return heap.allocate<Symbol>(heap.interpreter().global_object(), move(description), is_global); } Symbol* js_symbol(Interpreter& interpreter, String description, bool is_global) diff --git a/Libraries/LibJS/Runtime/SymbolConstructor.cpp b/Libraries/LibJS/Runtime/SymbolConstructor.cpp index a1a4661ae4..6eb52a93cf 100644 --- a/Libraries/LibJS/Runtime/SymbolConstructor.cpp +++ b/Libraries/LibJS/Runtime/SymbolConstructor.cpp @@ -32,16 +32,20 @@ namespace JS { -SymbolConstructor::SymbolConstructor() - : NativeFunction("Symbol", *interpreter().global_object().function_prototype()) +SymbolConstructor::SymbolConstructor(GlobalObject& global_object) + : NativeFunction("Symbol", *global_object.function_prototype()) { - define_property("prototype", interpreter().global_object().symbol_prototype(), 0); +} + +void SymbolConstructor::initialize(Interpreter& interpreter, GlobalObject& global_object) +{ + define_property("prototype", global_object.symbol_prototype(), 0); define_property("length", Value(0), Attribute::Configurable); define_native_function("for", for_, 1, Attribute::Writable | Attribute::Configurable); define_native_function("keyFor", key_for, 1, Attribute::Writable | Attribute::Configurable); - SymbolObject::initialize_well_known_symbols(interpreter()); + SymbolObject::initialize_well_known_symbols(interpreter); define_property("iterator", SymbolObject::well_known_iterator(), 0); define_property("asyncIterator", SymbolObject::well_known_async_terator(), 0); diff --git a/Libraries/LibJS/Runtime/SymbolConstructor.h b/Libraries/LibJS/Runtime/SymbolConstructor.h index 5c7ee56aef..c5e39774b6 100644 --- a/Libraries/LibJS/Runtime/SymbolConstructor.h +++ b/Libraries/LibJS/Runtime/SymbolConstructor.h @@ -32,7 +32,8 @@ namespace JS { class SymbolConstructor final : public NativeFunction { public: - SymbolConstructor(); + explicit SymbolConstructor(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~SymbolConstructor() override; virtual Value call(Interpreter&) override; diff --git a/Libraries/LibJS/Runtime/SymbolObject.cpp b/Libraries/LibJS/Runtime/SymbolObject.cpp index 63c4df93bb..e4d0ab3405 100644 --- a/Libraries/LibJS/Runtime/SymbolObject.cpp +++ b/Libraries/LibJS/Runtime/SymbolObject.cpp @@ -52,7 +52,7 @@ Value SymbolObject::s_well_known_to_string_tag; SymbolObject* SymbolObject::create(GlobalObject& global_object, Symbol& primitive_symbol) { - return global_object.heap().allocate<SymbolObject>(primitive_symbol, *global_object.symbol_prototype()); + return global_object.heap().allocate<SymbolObject>(global_object, primitive_symbol, *global_object.symbol_prototype()); } SymbolObject::SymbolObject(Symbol& symbol, Object& prototype) diff --git a/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Libraries/LibJS/Runtime/SymbolPrototype.cpp index 2d5bdd6a6c..7edf874009 100644 --- a/Libraries/LibJS/Runtime/SymbolPrototype.cpp +++ b/Libraries/LibJS/Runtime/SymbolPrototype.cpp @@ -39,8 +39,12 @@ namespace JS { -SymbolPrototype::SymbolPrototype() - : Object(interpreter().global_object().object_prototype()) +SymbolPrototype::SymbolPrototype(GlobalObject& global_object) + : Object(global_object.object_prototype()) +{ +} + +void SymbolPrototype::initialize(Interpreter&, GlobalObject&) { define_native_property("description", description_getter, nullptr, Attribute::Configurable); @@ -88,5 +92,4 @@ JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::value_of) return {}; return this_object->value_of(); } - } diff --git a/Libraries/LibJS/Runtime/SymbolPrototype.h b/Libraries/LibJS/Runtime/SymbolPrototype.h index 6c2c10b9e1..392c26a914 100644 --- a/Libraries/LibJS/Runtime/SymbolPrototype.h +++ b/Libraries/LibJS/Runtime/SymbolPrototype.h @@ -32,7 +32,8 @@ namespace JS { class SymbolPrototype final : public Object { public: - SymbolPrototype(); + explicit SymbolPrototype(GlobalObject&); + virtual void initialize(Interpreter&, GlobalObject&) override; virtual ~SymbolPrototype() override; private: diff --git a/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp b/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp index f9f8267718..f93290f54d 100644 --- a/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp +++ b/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp @@ -35,7 +35,7 @@ namespace JS { Uint8ClampedArray* Uint8ClampedArray::create(GlobalObject& global_object, u32 length) { auto& interpreter = global_object.interpreter(); - return interpreter.heap().allocate<Uint8ClampedArray>(length, *global_object.array_prototype()); + return interpreter.heap().allocate<Uint8ClampedArray>(global_object, length, *global_object.array_prototype()); } Uint8ClampedArray::Uint8ClampedArray(u32 length, Object& prototype) diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp index 8fc0e92dd4..dc61a06d5c 100644 --- a/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -60,11 +60,11 @@ void WindowObject::initialize() define_native_function("requestAnimationFrame", request_animation_frame, 1); define_native_function("cancelAnimationFrame", cancel_animation_frame, 1); - define_property("navigator", heap().allocate<NavigatorObject>(), JS::Attribute::Enumerable | JS::Attribute::Configurable); - define_property("location", heap().allocate<LocationObject>(), JS::Attribute::Enumerable | JS::Attribute::Configurable); + define_property("navigator", heap().allocate<NavigatorObject>(*this), JS::Attribute::Enumerable | JS::Attribute::Configurable); + define_property("location", heap().allocate<LocationObject>(*this), JS::Attribute::Enumerable | JS::Attribute::Configurable); - m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(); - m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>(); + m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(*this, *this); + m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>(*this, *this); m_xhr_constructor->define_property("prototype", m_xhr_prototype, 0); add_constructor("XMLHttpRequest", m_xhr_constructor, *m_xhr_prototype); } diff --git a/Libraries/LibWeb/Bindings/Wrappable.h b/Libraries/LibWeb/Bindings/Wrappable.h index fd14d908ff..33c49d4224 100644 --- a/Libraries/LibWeb/Bindings/Wrappable.h +++ b/Libraries/LibWeb/Bindings/Wrappable.h @@ -28,6 +28,7 @@ #include <AK/WeakPtr.h> #include <LibJS/Heap/Heap.h> +#include <LibJS/Interpreter.h> #include <LibWeb/Forward.h> namespace Web { @@ -49,7 +50,7 @@ template<class NativeObject> inline Wrapper* wrap_impl(JS::Heap& heap, NativeObject& native_object) { if (!native_object.wrapper()) - native_object.set_wrapper(*heap.allocate<typename NativeObject::WrapperType>(native_object)); + native_object.set_wrapper(*heap.allocate<typename NativeObject::WrapperType>(heap.interpreter().global_object(), native_object)); return native_object.wrapper(); } diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp index 413d5b94fd..9a205cc44a 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp @@ -36,8 +36,12 @@ namespace Web { namespace Bindings { -XMLHttpRequestConstructor::XMLHttpRequestConstructor() - : NativeFunction(*interpreter().global_object().function_prototype()) +XMLHttpRequestConstructor::XMLHttpRequestConstructor(JS::GlobalObject& global_object) + : NativeFunction(*global_object.function_prototype()) +{ +} + +void XMLHttpRequestConstructor::initialize(JS::Interpreter&, JS::GlobalObject&) { define_property("length", JS::Value(1), JS::Attribute::Configurable); @@ -60,7 +64,7 @@ JS::Value XMLHttpRequestConstructor::call(JS::Interpreter& interpreter) JS::Value XMLHttpRequestConstructor::construct(JS::Interpreter& interpreter) { auto& window = static_cast<WindowObject&>(global_object()); - return interpreter.heap().allocate<XMLHttpRequestWrapper>(XMLHttpRequest::create(window.impl())); + return interpreter.heap().allocate<XMLHttpRequestWrapper>(window, XMLHttpRequest::create(window.impl())); } } diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h index a0089b9049..ca5a8f8652 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h @@ -33,7 +33,8 @@ namespace Bindings { class XMLHttpRequestConstructor final : public JS::NativeFunction { public: - XMLHttpRequestConstructor(); + explicit XMLHttpRequestConstructor(JS::GlobalObject&); + virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override; virtual ~XMLHttpRequestConstructor() override; virtual JS::Value call(JS::Interpreter&) override; diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp index ea871c6663..98dc397b3a 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp @@ -35,8 +35,12 @@ namespace Web { namespace Bindings { -XMLHttpRequestPrototype::XMLHttpRequestPrototype() - : Object(interpreter().global_object().object_prototype()) +XMLHttpRequestPrototype::XMLHttpRequestPrototype(JS::GlobalObject& global_object) + : Object(global_object.object_prototype()) +{ +} + +void XMLHttpRequestPrototype::initialize(JS::Interpreter&, JS::GlobalObject&) { define_native_function("open", open, 2); define_native_function("send", send, 0); diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h index 50ba87d236..dcc2fdfe48 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h @@ -33,7 +33,8 @@ namespace Bindings { class XMLHttpRequestPrototype final : public JS::Object { public: - XMLHttpRequestPrototype(); + explicit XMLHttpRequestPrototype(JS::GlobalObject&); + virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override; virtual ~XMLHttpRequestPrototype() override; private: |