LibWeb: Make wrapper factory functions take JS::GlobalObject&

Instead of taking the JS::Heap&. This allows us to get rid of some
calls to JS::Interpreter::global_object(). We're getting closer and
closer to multiple global objects. :^)
This commit is contained in:
Andreas Kling 2020-06-23 16:57:39 +02:00
parent c24f5585b2
commit fc4ed8d444
12 changed files with 33 additions and 36 deletions

View file

@ -27,6 +27,7 @@
#pragma once
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Object.h>
namespace JS {

View file

@ -24,18 +24,17 @@
* 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>
namespace Web {
namespace Bindings {
EventWrapper* wrap(JS::Heap& heap, Event& event)
EventWrapper* wrap(JS::GlobalObject& global_object, Event& event)
{
if (event.is_mouse_event())
return static_cast<MouseEventWrapper*>(wrap_impl(heap, static_cast<MouseEvent&>(event)));
return static_cast<EventWrapper*>(wrap_impl(heap, event));
return static_cast<MouseEventWrapper*>(wrap_impl(global_object, static_cast<MouseEvent&>(event)));
return static_cast<EventWrapper*>(wrap_impl(global_object, event));
}
}

View file

@ -32,7 +32,7 @@
namespace Web {
namespace Bindings {
EventWrapper* wrap(JS::Heap&, Event&);
EventWrapper* wrap(JS::GlobalObject&, Event&);
}
}

View file

@ -24,7 +24,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibJS/Interpreter.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/Bindings/HTMLCanvasElementWrapper.h>
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
@ -38,19 +37,19 @@
namespace Web {
namespace Bindings {
NodeWrapper* wrap(JS::Heap& heap, Node& node)
NodeWrapper* wrap(JS::GlobalObject& global_object, Node& node)
{
if (is<Document>(node))
return static_cast<NodeWrapper*>(wrap_impl(heap, to<Document>(node)));
return static_cast<NodeWrapper*>(wrap_impl(global_object, to<Document>(node)));
if (is<HTMLCanvasElement>(node))
return static_cast<NodeWrapper*>(wrap_impl(heap, to<HTMLCanvasElement>(node)));
return static_cast<NodeWrapper*>(wrap_impl(global_object, to<HTMLCanvasElement>(node)));
if (is<HTMLImageElement>(node))
return static_cast<NodeWrapper*>(wrap_impl(heap, to<HTMLImageElement>(node)));
return static_cast<NodeWrapper*>(wrap_impl(global_object, to<HTMLImageElement>(node)));
if (is<HTMLElement>(node))
return static_cast<NodeWrapper*>(wrap_impl(heap, to<HTMLElement>(node)));
return static_cast<NodeWrapper*>(wrap_impl(global_object, to<HTMLElement>(node)));
if (is<Element>(node))
return static_cast<NodeWrapper*>(wrap_impl(heap, to<Element>(node)));
return static_cast<NodeWrapper*>(wrap_impl(heap, node));
return static_cast<NodeWrapper*>(wrap_impl(global_object, to<Element>(node)));
return static_cast<NodeWrapper*>(wrap_impl(global_object, node));
}
}

View file

@ -32,7 +32,7 @@
namespace Web {
namespace Bindings {
NodeWrapper* wrap(JS::Heap&, Node&);
NodeWrapper* wrap(JS::GlobalObject&, Node&);
}
}

View file

@ -244,7 +244,7 @@ JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)
auto* impl = impl_from(interpreter, global_object);
if (!impl)
return {};
return wrap(interpreter.heap(), impl->document());
return wrap(global_object, impl->document());
}
JS_DEFINE_NATIVE_SETTER(WindowObject::document_setter)

View file

@ -28,7 +28,7 @@
#include <AK/WeakPtr.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Forward.h>
namespace Web {
@ -47,11 +47,10 @@ private:
};
template<class NativeObject>
inline Wrapper* wrap_impl(JS::Heap& heap, NativeObject& native_object)
inline Wrapper* wrap_impl(JS::GlobalObject& global_object, NativeObject& native_object)
{
if (!native_object.wrapper()) {
auto& global_object = heap.interpreter().global_object();
native_object.set_wrapper(*heap.allocate<typename NativeObject::WrapperType>(global_object, global_object, native_object));
native_object.set_wrapper(*global_object.heap().allocate<typename NativeObject::WrapperType>(global_object, global_object, native_object));
}
return native_object.wrapper();
}

View file

@ -25,7 +25,6 @@
*/
#include <AK/FlyString.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/WindowObject.h>
@ -36,9 +35,9 @@
namespace Web {
namespace Bindings {
XMLHttpRequestWrapper* wrap(JS::Heap& heap, XMLHttpRequest& impl)
XMLHttpRequestWrapper* wrap(JS::GlobalObject& global_object, XMLHttpRequest& impl)
{
return static_cast<XMLHttpRequestWrapper*>(wrap_impl(heap, impl));
return static_cast<XMLHttpRequestWrapper*>(wrap_impl(global_object, impl));
}
XMLHttpRequestWrapper::XMLHttpRequestWrapper(JS::GlobalObject& global_object, XMLHttpRequest& impl)

View file

@ -43,7 +43,7 @@ private:
virtual const char* class_name() const override { return "XMLHttpRequestWrapper"; }
};
XMLHttpRequestWrapper* wrap(JS::Heap&, XMLHttpRequest&);
XMLHttpRequestWrapper* wrap(JS::GlobalObject&, XMLHttpRequest&);
}
}

View file

@ -398,7 +398,7 @@ static void generate_header(const IDL::Interface& interface)
out() << "};";
if (should_emit_wrapper_factory(interface)) {
out() << wrapper_class << "* wrap(JS::Heap&, " << interface.name << "&);";
out() << wrapper_class << "* wrap(JS::GlobalObject&, " << interface.name << "&);";
}
out() << "}";
@ -549,7 +549,7 @@ void generate_implementation(const IDL::Interface& interface)
// Basically once we have NodeList we can throw this out.
out() << " auto* new_array = JS::Array::create(global_object);";
out() << " for (auto& element : retval) {";
out() << " new_array->indexed_properties().append(wrap(interpreter.heap(), element));";
out() << " new_array->indexed_properties().append(wrap(global_object, element));";
out() << " }";
out() << " return new_array;";
} else if (return_type.name == "long" || return_type.name == "double") {
@ -557,7 +557,7 @@ void generate_implementation(const IDL::Interface& interface)
} else if (return_type.name == "Uint8ClampedArray") {
out() << " return retval;";
} else {
out() << " return wrap(interpreter.heap(), const_cast<" << return_type.name << "&>(*retval));";
out() << " return wrap(global_object, const_cast<" << return_type.name << "&>(*retval));";
}
};
@ -613,9 +613,9 @@ void generate_implementation(const IDL::Interface& interface)
// Implementation: Wrapper factory
if (should_emit_wrapper_factory(interface)) {
out() << wrapper_class << "* wrap(JS::Heap& heap, " << interface.name << "& impl)";
out() << wrapper_class << "* wrap(JS::GlobalObject& global_object, " << interface.name << "& impl)";
out() << "{";
out() << " return static_cast<" << wrapper_class << "*>(wrap_impl(heap, impl));";
out() << " return static_cast<" << wrapper_class << "*>(wrap_impl(global_object, impl));";
out() << "}";
}

View file

@ -137,13 +137,13 @@ void Node::dispatch_event(NonnullRefPtr<Event> event)
#ifdef EVENT_DEBUG
static_cast<const JS::ScriptFunction*>(function)->body().dump(0);
#endif
auto& heap = function.heap();
auto* this_value = wrap(heap, *this);
auto& global_object = function.global_object();
auto* this_value = wrap(global_object, *this);
#ifdef EVENT_DEBUG
dbg() << "calling event listener with this=" << this_value;
#endif
auto* event_wrapper = wrap(heap, *event);
JS::MarkedValueList arguments(heap);
auto* event_wrapper = wrap(global_object, *event);
JS::MarkedValueList arguments(global_object.heap());
arguments.append(event_wrapper);
document().interpreter().call(function, this_value, move(arguments));
}

View file

@ -94,10 +94,10 @@ void XMLHttpRequest::dispatch_event(NonnullRefPtr<Event> event)
for (auto& listener : listeners()) {
if (listener.event_name == event->type()) {
auto& function = const_cast<EventListener&>(*listener.listener).function();
auto& heap = function.heap();
auto* this_value = wrap(heap, *this);
JS::MarkedValueList arguments(heap);
arguments.append(wrap(heap, *event));
auto& global_object = function.global_object();
auto* this_value = wrap(global_object, *this);
JS::MarkedValueList arguments(global_object.heap());
arguments.append(wrap(global_object, *event));
function.interpreter().call(function, this_value, move(arguments));
}
}