From 50b76e4cc7baa2acbfa17c24b8311e619969ab6a Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 9 Dec 2020 00:14:43 +0000 Subject: [PATCH] LibWeb: Don't allocate XMLHttpRequestConstructor twice add_constructor() will already allocate an XMLHttpRequestConstructor and update m_xhr_constructor accordingly, we don't have to do it ourselves. This is now in line with how all the LibJS constructors work. Also make the XHR constructor responsible for setting its "prototype" property itself, for consistency and fail-proofing. Previously we would only set it on the constructor we allocated manually but which was then thrown away, leading to the property never being set properly. --- Libraries/LibWeb/Bindings/WindowObject.cpp | 2 -- Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp | 7 +++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp index ec740dfa794..5116b9c3084 100644 --- a/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -85,8 +85,6 @@ void WindowObject::initialize() define_property("location", heap().allocate(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable); m_xhr_prototype = heap().allocate(*this, *this); - m_xhr_constructor = heap().allocate(*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/XMLHttpRequestConstructor.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp index 636a7bd4618..16bb2a4b8bc 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp @@ -26,9 +26,9 @@ #include #include -#include #include #include +#include #include #include @@ -41,8 +41,11 @@ XMLHttpRequestConstructor::XMLHttpRequestConstructor(JS::GlobalObject& global_ob void XMLHttpRequestConstructor::initialize(JS::GlobalObject& global_object) { + auto& vm = this->vm(); NativeFunction::initialize(global_object); - define_property("length", JS::Value(1), JS::Attribute::Configurable); + auto& window = static_cast(global_object); + define_property(vm.names.prototype, window.xhr_prototype(), 0); + define_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable); define_property("UNSENT", JS::Value((i32)XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); define_property("OPENED", JS::Value((i32)XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable);