diff options
30 files changed, 60 insertions, 28 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index a29229422f..93ae05e2f7 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -124,7 +124,7 @@ Value CallExpression::execute(Interpreter& interpreter) const Object* new_object = nullptr; Value result; if (is_new_expression()) { - new_object = interpreter.heap().allocate<Object>(); + new_object = Object::create_empty(interpreter, interpreter.global_object()); auto prototype = function.get("prototype"); if (prototype.has_value() && prototype.value().is_object()) new_object->set_prototype(&prototype.value().as_object()); @@ -901,7 +901,7 @@ void ExpressionStatement::dump(int indent) const Value ObjectExpression::execute(Interpreter& interpreter) const { - auto object = interpreter.heap().allocate<Object>(); + auto* object = Object::create_empty(interpreter, interpreter.global_object()); for (auto it : m_properties) { auto value = it.value->execute(interpreter); if (interpreter.exception()) diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index 2d526e1081..0c9ea12235 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -27,9 +27,9 @@ #include <AK/Function.h> #include <LibJS/Interpreter.h> #include <LibJS/Runtime/Array.h> -#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/ArrayPrototype.h> #include <LibJS/Runtime/Error.h> +#include <LibJS/Runtime/GlobalObject.h> namespace JS { @@ -40,8 +40,8 @@ Array* Array::create(GlobalObject& global_object) } Array::Array(Object& prototype) + : Object(&prototype) { - set_prototype(&prototype); put_native_property("length", length_getter, length_setter); } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 15de7ffdac..1fc08c8a36 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -38,6 +38,7 @@ namespace JS { ArrayPrototype::ArrayPrototype() + : Object(interpreter().object_prototype()) { put_native_function("filter", filter, 1); put_native_function("forEach", for_each, 1); diff --git a/Libraries/LibJS/Runtime/BooleanObject.cpp b/Libraries/LibJS/Runtime/BooleanObject.cpp index f823759582..c7d4a4c0f9 100644 --- a/Libraries/LibJS/Runtime/BooleanObject.cpp +++ b/Libraries/LibJS/Runtime/BooleanObject.cpp @@ -37,9 +37,9 @@ BooleanObject* BooleanObject::create(GlobalObject& global_object, bool value) } BooleanObject::BooleanObject(bool value, Object& prototype) - : m_value(value) + : Object(&prototype) + , m_value(value) { - set_prototype(&prototype); } BooleanObject::~BooleanObject() diff --git a/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Libraries/LibJS/Runtime/ConsoleObject.cpp index ac2cf89106..56464cde3c 100644 --- a/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -44,6 +44,7 @@ static void print_args(Interpreter& interpreter) } ConsoleObject::ConsoleObject() + : Object(interpreter().object_prototype()) { put_native_function("log", log); put_native_function("debug", debug); diff --git a/Libraries/LibJS/Runtime/Date.cpp b/Libraries/LibJS/Runtime/Date.cpp index 5ebf3a77a5..c9565d40ab 100644 --- a/Libraries/LibJS/Runtime/Date.cpp +++ b/Libraries/LibJS/Runtime/Date.cpp @@ -38,7 +38,8 @@ Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, u16 mil } Date::Date(Core::DateTime datetime, u16 milliseconds, Object& prototype) - : m_datetime(datetime) + : Object(&prototype) + , m_datetime(datetime) , m_milliseconds(milliseconds) { set_prototype(&prototype); diff --git a/Libraries/LibJS/Runtime/DatePrototype.cpp b/Libraries/LibJS/Runtime/DatePrototype.cpp index 9c827a0de3..0c79e4d099 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -48,6 +48,7 @@ static Date* this_date_from_interpreter(Interpreter& interpreter) } DatePrototype::DatePrototype() + : Object(interpreter().object_prototype()) { put_native_function("getDate", get_date); put_native_function("getDay", get_day); diff --git a/Libraries/LibJS/Runtime/Error.cpp b/Libraries/LibJS/Runtime/Error.cpp index 9fdf490dda..d6ea45cf4f 100644 --- a/Libraries/LibJS/Runtime/Error.cpp +++ b/Libraries/LibJS/Runtime/Error.cpp @@ -37,7 +37,8 @@ Error* Error::create(GlobalObject& global_object, const FlyString& name, const S } Error::Error(const FlyString& name, const String& message, Object& prototype) - : m_name(name) + : Object(&prototype) + , m_name(name) , m_message(message) { set_prototype(&prototype); diff --git a/Libraries/LibJS/Runtime/ErrorPrototype.cpp b/Libraries/LibJS/Runtime/ErrorPrototype.cpp index 0251fe779f..1082b8f06c 100644 --- a/Libraries/LibJS/Runtime/ErrorPrototype.cpp +++ b/Libraries/LibJS/Runtime/ErrorPrototype.cpp @@ -35,6 +35,7 @@ namespace JS { ErrorPrototype::ErrorPrototype() + : Object(interpreter().object_prototype()) { put_native_property("name", name_getter, name_setter); put_native_property("message", message_getter, nullptr); @@ -103,8 +104,8 @@ Value ErrorPrototype::to_string(Interpreter& interpreter) #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ PrototypeName::PrototypeName() \ + : Object(interpreter().error_prototype()) \ { \ - set_prototype(interpreter().error_prototype()); \ } \ PrototypeName::~PrototypeName() {} \ const char* PrototypeName::class_name() const { return #PrototypeName; } diff --git a/Libraries/LibJS/Runtime/Function.cpp b/Libraries/LibJS/Runtime/Function.cpp index 06c760eac1..18f3018e18 100644 --- a/Libraries/LibJS/Runtime/Function.cpp +++ b/Libraries/LibJS/Runtime/Function.cpp @@ -30,8 +30,8 @@ namespace JS { Function::Function(Object& prototype) + : Object(&prototype) { - set_prototype(&prototype); } Function::~Function() diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Libraries/LibJS/Runtime/FunctionPrototype.cpp index 1cbf9cf082..c7511bcb00 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.cpp +++ b/Libraries/LibJS/Runtime/FunctionPrototype.cpp @@ -36,6 +36,7 @@ namespace JS { FunctionPrototype::FunctionPrototype() + : Object(interpreter().object_prototype()) { } diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index 65a955fc38..78badbe6e6 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -53,6 +53,7 @@ void GlobalObject::add_constructor(const FlyString& property_name, ConstructorTy } GlobalObject::GlobalObject() + : Object(interpreter().object_prototype()) { put_native_function("gc", gc); put_native_function("isNaN", is_nan, 1); diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 36f075093e..fb3bd7fa99 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -33,6 +33,7 @@ namespace JS { MathObject::MathObject() + : Object(interpreter().object_prototype()) { put_native_function("abs", abs, 1); put_native_function("random", random); diff --git a/Libraries/LibJS/Runtime/NativeProperty.cpp b/Libraries/LibJS/Runtime/NativeProperty.cpp index c19a1f1d9d..9f9fd7b236 100644 --- a/Libraries/LibJS/Runtime/NativeProperty.cpp +++ b/Libraries/LibJS/Runtime/NativeProperty.cpp @@ -30,7 +30,8 @@ namespace JS { NativeProperty::NativeProperty(AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter) - : m_getter(move(getter)) + : Object(nullptr) + , m_getter(move(getter)) , m_setter(move(setter)) { } diff --git a/Libraries/LibJS/Runtime/NumberObject.cpp b/Libraries/LibJS/Runtime/NumberObject.cpp index d9beed48bd..c2432da33c 100644 --- a/Libraries/LibJS/Runtime/NumberObject.cpp +++ b/Libraries/LibJS/Runtime/NumberObject.cpp @@ -40,9 +40,9 @@ NumberObject* NumberObject::create(GlobalObject& global_object, double value) } NumberObject::NumberObject(double value, Object& prototype) - : m_value(value) + : Object(&prototype) + , m_value(value) { - set_prototype(&prototype); } NumberObject::~NumberObject() diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index 5d304cb2da..f7ecb0b4a1 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -38,10 +38,15 @@ namespace JS { -Object::Object() +Object* Object::create_empty(Interpreter& interpreter, GlobalObject&) +{ + return interpreter.heap().allocate<Object>(interpreter.object_prototype()); +} + +Object::Object(Object* prototype) { m_shape = interpreter().empty_object_shape(); - m_shape->set_prototype_without_transition(interpreter().object_prototype()); + set_prototype(prototype); } Object::~Object() @@ -60,6 +65,8 @@ const Object* Object::prototype() const void Object::set_prototype(Object* new_prototype) { + if (prototype() == new_prototype) + return; m_shape = m_shape->create_prototype_transition(new_prototype); } diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index 90fe25b852..ad3766a7d8 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -38,7 +38,9 @@ namespace JS { class Object : public Cell { public: - Object(); + static Object* create_empty(Interpreter&, GlobalObject&); + + explicit Object(Object* prototype); virtual ~Object(); Shape& shape() { return *m_shape; } diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Libraries/LibJS/Runtime/ObjectConstructor.cpp index e9f4a67551..d18ef48353 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -52,7 +52,7 @@ ObjectConstructor::~ObjectConstructor() Value ObjectConstructor::call(Interpreter& interpreter) { - return interpreter.heap().allocate<Object>(); + return Object::create_empty(interpreter, interpreter.global_object()); } Value ObjectConstructor::construct(Interpreter& interpreter) @@ -109,7 +109,7 @@ Value ObjectConstructor::get_own_property_descriptor(Interpreter& interpreter) auto metadata = object.shape().lookup(interpreter.argument(1).to_string()); if (!metadata.has_value()) return js_undefined(); - auto* descriptor = interpreter.heap().allocate<Object>(); + auto* descriptor = Object::create_empty(interpreter, interpreter.global_object()); descriptor->put("configurable", Value(!!(metadata.value().attributes & Attribute::Configurable))); descriptor->put("enumerable", Value(!!(metadata.value().attributes & Attribute::Enumerable))); descriptor->put("writable", Value(!!(metadata.value().attributes & Attribute::Writable))); diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Libraries/LibJS/Runtime/ObjectPrototype.cpp index 85332ca727..56f064be37 100644 --- a/Libraries/LibJS/Runtime/ObjectPrototype.cpp +++ b/Libraries/LibJS/Runtime/ObjectPrototype.cpp @@ -34,8 +34,8 @@ namespace JS { ObjectPrototype::ObjectPrototype() + : Object(nullptr) { - set_prototype(nullptr); } void ObjectPrototype::initialize() diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index 1348eb4d05..4ad613850c 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -47,7 +47,7 @@ ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vec , m_parameters(move(parameters)) , m_parent_environment(parent_environment) { - put("prototype", heap().allocate<Object>()); + put("prototype", Object::create_empty(interpreter(), interpreter().global_object())); put_native_property("length", length_getter, length_setter); } diff --git a/Libraries/LibJS/Runtime/StringObject.cpp b/Libraries/LibJS/Runtime/StringObject.cpp index cb67dbbc27..0be4afdfad 100644 --- a/Libraries/LibJS/Runtime/StringObject.cpp +++ b/Libraries/LibJS/Runtime/StringObject.cpp @@ -41,9 +41,9 @@ StringObject* StringObject::create(GlobalObject& global_object, PrimitiveString& } StringObject::StringObject(PrimitiveString& string, Object& prototype) - : m_string(string) + : Object(&prototype) + , m_string(string) { - set_prototype(&prototype); } StringObject::~StringObject() diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp index 255d266204..90d2e28b41 100644 --- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp +++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp @@ -44,7 +44,8 @@ CanvasRenderingContext2DWrapper* wrap(JS::Heap& heap, CanvasRenderingContext2D& } CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRenderingContext2D& impl) - : m_impl(impl) + : Wrapper(*interpreter().object_prototype()) + , m_impl(impl) { put_native_property("fillStyle", fill_style_getter, fill_style_setter); put_native_function("fillRect", fill_rect, 4); diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h index 5822dc77fb..ba5205714b 100644 --- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h +++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h @@ -31,7 +31,7 @@ namespace Web { namespace Bindings { -class CanvasRenderingContext2DWrapper : public Wrapper { +class CanvasRenderingContext2DWrapper final : public Wrapper { public: explicit CanvasRenderingContext2DWrapper(CanvasRenderingContext2D&); virtual ~CanvasRenderingContext2DWrapper() override; diff --git a/Libraries/LibWeb/Bindings/EventListenerWrapper.cpp b/Libraries/LibWeb/Bindings/EventListenerWrapper.cpp index 4e0be17d34..c48a61ee0e 100644 --- a/Libraries/LibWeb/Bindings/EventListenerWrapper.cpp +++ b/Libraries/LibWeb/Bindings/EventListenerWrapper.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <LibJS/Interpreter.h> #include <LibJS/Runtime/Function.h> #include <LibWeb/Bindings/EventListenerWrapper.h> #include <LibWeb/DOM/EventListener.h> @@ -32,7 +33,8 @@ namespace Web { namespace Bindings { EventListenerWrapper::EventListenerWrapper(EventListener& impl) - : m_impl(impl) + : Wrapper(*interpreter().object_prototype()) + , m_impl(impl) { } diff --git a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp index 46d24ca454..dd98284a25 100644 --- a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp +++ b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp @@ -37,7 +37,8 @@ namespace Web { namespace Bindings { EventTargetWrapper::EventTargetWrapper(EventTarget& impl) - : m_impl(impl) + : Wrapper(*interpreter().object_prototype()) + , m_impl(impl) { put_native_function("addEventListener", add_event_listener, 2); } diff --git a/Libraries/LibWeb/Bindings/EventWrapper.cpp b/Libraries/LibWeb/Bindings/EventWrapper.cpp index 86648a8061..8248ef62ce 100644 --- a/Libraries/LibWeb/Bindings/EventWrapper.cpp +++ b/Libraries/LibWeb/Bindings/EventWrapper.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <LibJS/Interpreter.h> #include <LibWeb/Bindings/EventWrapper.h> #include <LibWeb/Bindings/MouseEventWrapper.h> #include <LibWeb/DOM/MouseEvent.h> @@ -39,7 +40,8 @@ EventWrapper* wrap(JS::Heap& heap, Event& event) } EventWrapper::EventWrapper(Event& event) - : m_event(event) + : Wrapper(*interpreter().object_prototype()) + , m_event(event) { } diff --git a/Libraries/LibWeb/Bindings/NavigatorObject.cpp b/Libraries/LibWeb/Bindings/NavigatorObject.cpp index 0940510b71..1e36621100 100644 --- a/Libraries/LibWeb/Bindings/NavigatorObject.cpp +++ b/Libraries/LibWeb/Bindings/NavigatorObject.cpp @@ -25,12 +25,14 @@ */ #include <AK/FlyString.h> +#include <LibJS/Interpreter.h> #include <LibWeb/Bindings/NavigatorObject.h> namespace Web { namespace Bindings { NavigatorObject::NavigatorObject() + : Object(interpreter().object_prototype()) { put("appCodeName", js_string(heap(), "Mozilla")); put("appName", js_string(heap(), "Netscape")); diff --git a/Libraries/LibWeb/Bindings/Wrapper.h b/Libraries/LibWeb/Bindings/Wrapper.h index be7db5ace1..937385431f 100644 --- a/Libraries/LibWeb/Bindings/Wrapper.h +++ b/Libraries/LibWeb/Bindings/Wrapper.h @@ -38,7 +38,10 @@ class Wrapper : public JS::Object , public Weakable<Wrapper> { protected: - explicit Wrapper() {} + explicit Wrapper(Object& prototype) + : Object(&prototype) + { + } }; } diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp index 9f8bb9131f..52e10a34a2 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp @@ -35,6 +35,7 @@ namespace Web { namespace Bindings { XMLHttpRequestConstructor::XMLHttpRequestConstructor() + : NativeFunction(*interpreter().function_prototype()) { put("length", JS::Value(1)); } diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp index a0dd72bd4b..df537aa280 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp @@ -35,6 +35,7 @@ namespace Web { namespace Bindings { XMLHttpRequestPrototype::XMLHttpRequestPrototype() + : Object(interpreter().object_prototype()) { put_native_function("open", open, 2); put_native_function("send", send, 0); |