LibWeb: Split JS wrapper constructors into construct/initialize

This commit is contained in:
Andreas Kling 2020-06-20 17:28:13 +02:00
parent 06e29fac57
commit 03da70c7d0
Notes: sideshowbarker 2024-07-19 05:31:37 +09:00
31 changed files with 107 additions and 55 deletions

View file

@ -47,9 +47,13 @@ CanvasRenderingContext2DWrapper* wrap(JS::Heap& heap, CanvasRenderingContext2D&
return static_cast<CanvasRenderingContext2DWrapper*>(wrap_impl(heap, impl));
}
CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRenderingContext2D& impl)
: Wrapper(*interpreter().global_object().object_prototype())
CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(JS::GlobalObject& global_object, CanvasRenderingContext2D& impl)
: Wrapper(*global_object.object_prototype())
, m_impl(impl)
{
}
void CanvasRenderingContext2DWrapper::initialize(JS::Interpreter&, JS::GlobalObject&)
{
define_native_function("fillRect", fill_rect, 4);
define_native_function("scale", scale, 2);

View file

@ -33,7 +33,8 @@ namespace Bindings {
class CanvasRenderingContext2DWrapper final : public Wrapper {
public:
explicit CanvasRenderingContext2DWrapper(CanvasRenderingContext2D&);
CanvasRenderingContext2DWrapper(JS::GlobalObject&, CanvasRenderingContext2D&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~CanvasRenderingContext2DWrapper() override;
CanvasRenderingContext2D& impl() { return m_impl; }

View file

@ -38,8 +38,12 @@
namespace Web {
namespace Bindings {
DocumentWrapper::DocumentWrapper(Document& document)
: NodeWrapper(document)
DocumentWrapper::DocumentWrapper(JS::GlobalObject& global_object, Document& document)
: NodeWrapper(global_object, document)
{
}
void DocumentWrapper::initialize(JS::Interpreter&, JS::GlobalObject&)
{
define_native_function("getElementById", get_element_by_id, 1);
define_native_function("querySelector", query_selector, 1);

View file

@ -33,7 +33,8 @@ namespace Bindings {
class DocumentWrapper : public NodeWrapper {
public:
explicit DocumentWrapper(Document&);
DocumentWrapper(JS::GlobalObject&, Document&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~DocumentWrapper() override;
Document& node();

View file

@ -38,8 +38,12 @@
namespace Web {
namespace Bindings {
ElementWrapper::ElementWrapper(Element& element)
: NodeWrapper(element)
ElementWrapper::ElementWrapper(JS::GlobalObject& global_object, Element& element)
: NodeWrapper(global_object, element)
{
}
void ElementWrapper::initialize(JS::Interpreter&, JS::GlobalObject&)
{
define_native_property("innerHTML", inner_html_getter, inner_html_setter);
define_native_property("id", id_getter, id_setter);

View file

@ -33,7 +33,8 @@ namespace Bindings {
class ElementWrapper : public NodeWrapper {
public:
explicit ElementWrapper(Element&);
ElementWrapper(JS::GlobalObject&, Element&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~ElementWrapper() override;
Element& node();

View file

@ -33,8 +33,8 @@
namespace Web {
namespace Bindings {
EventListenerWrapper::EventListenerWrapper(EventListener& impl)
: Wrapper(*interpreter().global_object().object_prototype())
EventListenerWrapper::EventListenerWrapper(JS::GlobalObject& global_object, EventListener& impl)
: Wrapper(*global_object.object_prototype())
, m_impl(impl)
{
}

View file

@ -33,7 +33,7 @@ namespace Bindings {
class EventListenerWrapper final : public Wrapper {
public:
explicit EventListenerWrapper(EventListener&);
EventListenerWrapper(JS::GlobalObject&, EventListener&);
virtual ~EventListenerWrapper() override;
EventListener& impl() { return *m_impl; }

View file

@ -38,9 +38,13 @@
namespace Web {
namespace Bindings {
EventTargetWrapper::EventTargetWrapper(EventTarget& impl)
: Wrapper(*interpreter().global_object().object_prototype())
EventTargetWrapper::EventTargetWrapper(JS::GlobalObject& global_object, EventTarget& impl)
: Wrapper(*global_object.object_prototype())
, m_impl(impl)
{
}
void EventTargetWrapper::initialize(JS::Interpreter&, JS::GlobalObject&)
{
define_native_function("addEventListener", add_event_listener, 2);
}

View file

@ -33,7 +33,8 @@ namespace Bindings {
class EventTargetWrapper : public Wrapper {
public:
explicit EventTargetWrapper(EventTarget&);
EventTargetWrapper(JS::GlobalObject&, EventTarget&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~EventTargetWrapper() override;
EventTarget& impl() { return *m_impl; }

View file

@ -40,8 +40,8 @@ EventWrapper* wrap(JS::Heap& heap, Event& event)
return static_cast<EventWrapper*>(wrap_impl(heap, event));
}
EventWrapper::EventWrapper(Event& event)
: Wrapper(*interpreter().global_object().object_prototype())
EventWrapper::EventWrapper(JS::GlobalObject& global_object, Event& event)
: Wrapper(*global_object.object_prototype())
, m_event(event)
{
}

View file

@ -33,7 +33,7 @@ namespace Bindings {
class EventWrapper : public Wrapper {
public:
explicit EventWrapper(Event&);
EventWrapper(JS::GlobalObject&, Event&);
virtual ~EventWrapper() override;
Event& event() { return m_event; }

View file

@ -38,8 +38,12 @@
namespace Web {
namespace Bindings {
HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element)
: ElementWrapper(element)
HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(JS::GlobalObject& global_object, HTMLCanvasElement& element)
: ElementWrapper(global_object, element)
{
}
void HTMLCanvasElementWrapper::initialize(JS::Interpreter&, JS::GlobalObject&)
{
define_native_function("getContext", get_context, 1);

View file

@ -33,7 +33,8 @@ namespace Bindings {
class HTMLCanvasElementWrapper : public ElementWrapper {
public:
explicit HTMLCanvasElementWrapper(HTMLCanvasElement&);
HTMLCanvasElementWrapper(JS::GlobalObject&, HTMLCanvasElement&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~HTMLCanvasElementWrapper() override;
HTMLCanvasElement& node();

View file

@ -35,8 +35,8 @@
namespace Web {
namespace Bindings {
HTMLImageElementWrapper::HTMLImageElementWrapper(HTMLImageElement& element)
: ElementWrapper(element)
HTMLImageElementWrapper::HTMLImageElementWrapper(JS::GlobalObject& global_object, HTMLImageElement& element)
: ElementWrapper(global_object, element)
{
}

View file

@ -33,7 +33,7 @@ namespace Bindings {
class HTMLImageElementWrapper : public ElementWrapper {
public:
explicit HTMLImageElementWrapper(HTMLImageElement&);
HTMLImageElementWrapper(JS::GlobalObject&, HTMLImageElement&);
virtual ~HTMLImageElementWrapper() override;
HTMLImageElement& node();

View file

@ -40,9 +40,13 @@ ImageDataWrapper* wrap(JS::Heap& heap, ImageData& event)
return static_cast<ImageDataWrapper*>(wrap_impl(heap, event));
}
ImageDataWrapper::ImageDataWrapper(ImageData& impl)
: Wrapper(*interpreter().global_object().object_prototype())
ImageDataWrapper::ImageDataWrapper(JS::GlobalObject& global_object, ImageData& impl)
: Wrapper(*global_object.object_prototype())
, m_impl(impl)
{
}
void ImageDataWrapper::initialize(JS::Interpreter&, JS::GlobalObject&)
{
define_native_property("width", width_getter, nullptr);
define_native_property("height", height_getter, nullptr);

View file

@ -33,7 +33,8 @@ namespace Bindings {
class ImageDataWrapper : public Wrapper {
public:
explicit ImageDataWrapper(ImageData&);
ImageDataWrapper(JS::GlobalObject&, ImageData&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~ImageDataWrapper() override;
ImageData& impl() { return m_impl; }

View file

@ -35,8 +35,12 @@
namespace Web {
namespace Bindings {
LocationObject::LocationObject()
: Object(interpreter().global_object().object_prototype())
LocationObject::LocationObject(JS::GlobalObject& global_object)
: Object(global_object.object_prototype())
{
}
void LocationObject::initialize(JS::Interpreter&, JS::GlobalObject&)
{
u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable;
define_native_property("href", href_getter, href_setter, attr);

View file

@ -34,7 +34,8 @@ namespace Bindings {
class LocationObject final : public JS::Object {
public:
LocationObject();
explicit LocationObject(JS::GlobalObject&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~LocationObject() override;
private:

View file

@ -35,8 +35,12 @@
namespace Web {
namespace Bindings {
MouseEventWrapper::MouseEventWrapper(MouseEvent& event)
: EventWrapper(event)
MouseEventWrapper::MouseEventWrapper(JS::GlobalObject& global_object, MouseEvent& event)
: EventWrapper(global_object, event)
{
}
void MouseEventWrapper::initialize(JS::Interpreter&, JS::GlobalObject&)
{
define_native_property("offsetX", offset_x_getter, nullptr);
define_native_property("offsetY", offset_y_getter, nullptr);

View file

@ -33,7 +33,8 @@ namespace Bindings {
class MouseEventWrapper : public EventWrapper {
public:
explicit MouseEventWrapper(MouseEvent&);
MouseEventWrapper(JS::GlobalObject&, MouseEvent&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~MouseEventWrapper() override;
MouseEvent& event();

View file

@ -34,19 +34,23 @@
namespace Web {
namespace Bindings {
NavigatorObject::NavigatorObject()
: Object(interpreter().global_object().object_prototype())
NavigatorObject::NavigatorObject(JS::GlobalObject& global_object)
: Object(global_object.object_prototype())
{
auto* languages = JS::Array::create(interpreter().global_object());
}
void NavigatorObject::initialize(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
{
auto* languages = JS::Array::create(global_object);
languages->indexed_properties().append(js_string(heap(), "en-US"));
define_property("appCodeName", js_string(heap(), "Mozilla"));
define_property("appName", js_string(heap(), "Netscape"));
define_property("appVersion", js_string(heap(), "4.0"));
define_property("appCodeName", js_string(interpreter.heap(), "Mozilla"));
define_property("appName", js_string(interpreter.heap(), "Netscape"));
define_property("appVersion", js_string(interpreter.heap(), "4.0"));
define_property("language", languages->get(0));
define_property("languages", languages);
define_property("platform", js_string(heap(), "SerenityOS"));
define_property("product", js_string(heap(), "Gecko"));
define_property("platform", js_string(interpreter.heap(), "SerenityOS"));
define_property("product", js_string(interpreter.heap(), "Gecko"));
define_native_property("userAgent", user_agent_getter, nullptr);
}

View file

@ -34,7 +34,8 @@ namespace Bindings {
class NavigatorObject final : public JS::Object {
public:
NavigatorObject();
NavigatorObject(JS::GlobalObject&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~NavigatorObject() override;
private:

View file

@ -52,10 +52,14 @@ NodeWrapper* wrap(JS::Heap& heap, Node& node)
return static_cast<NodeWrapper*>(wrap_impl(heap, node));
}
NodeWrapper::NodeWrapper(Node& node)
: EventTargetWrapper(node)
NodeWrapper::NodeWrapper(JS::GlobalObject& global_object, Node& node)
: EventTargetWrapper(global_object, node)
{
put("nodeName", JS::js_string(heap(), node.node_name()));
}
void NodeWrapper::initialize(JS::Interpreter& interpreter, JS::GlobalObject&)
{
put("nodeName", JS::js_string(interpreter.heap(), node().node_name()));
}
NodeWrapper::~NodeWrapper()

View file

@ -33,7 +33,8 @@ namespace Bindings {
class NodeWrapper : public EventTargetWrapper {
public:
explicit NodeWrapper(Node&);
NodeWrapper(JS::GlobalObject&, Node&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~NodeWrapper() override;
Node& node();

View file

@ -60,8 +60,8 @@ 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>(*this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_property("location", heap().allocate<LocationObject>(*this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(*this, *this);
m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>(*this, *this);

View file

@ -49,8 +49,10 @@ private:
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>(heap.interpreter().global_object(), 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));
}
return native_object.wrapper();
}

View file

@ -64,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>(window, XMLHttpRequest::create(window.impl()));
return interpreter.heap().allocate<XMLHttpRequestWrapper>(window, window, XMLHttpRequest::create(window.impl()));
}
}

View file

@ -41,10 +41,10 @@ XMLHttpRequestWrapper* wrap(JS::Heap& heap, XMLHttpRequest& impl)
return static_cast<XMLHttpRequestWrapper*>(wrap_impl(heap, impl));
}
XMLHttpRequestWrapper::XMLHttpRequestWrapper(XMLHttpRequest& impl)
: EventTargetWrapper(impl)
XMLHttpRequestWrapper::XMLHttpRequestWrapper(JS::GlobalObject& global_object, XMLHttpRequest& impl)
: EventTargetWrapper(global_object, impl)
{
set_prototype(static_cast<WindowObject&>(interpreter().global_object()).xhr_prototype());
set_prototype(static_cast<WindowObject&>(global_object).xhr_prototype());
}
XMLHttpRequestWrapper::~XMLHttpRequestWrapper()

View file

@ -33,7 +33,7 @@ namespace Bindings {
class XMLHttpRequestWrapper final : public EventTargetWrapper {
public:
explicit XMLHttpRequestWrapper(XMLHttpRequest&);
XMLHttpRequestWrapper(JS::GlobalObject&, XMLHttpRequest&);
virtual ~XMLHttpRequestWrapper() override;
XMLHttpRequest& impl();