1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:27:46 +00:00

LibJS: Move native objects towards two-pass construction

To make sure that everything is set up correctly in objects before we
start adding properties to them, we split cell allocation into 3 steps:

1. Allocate a cell of appropriate size from the Heap
2. Call the C++ constructor on the cell
3. Call initialize() on the constructed object

The job of initialize() is to define all the initial properties.
Doing it in a second pass guarantees that the Object has a valid Shape
and can find its own GlobalObject.
This commit is contained in:
Andreas Kling 2020-06-20 15:40:48 +02:00
parent e4add19915
commit 64513f3c23
83 changed files with 295 additions and 167 deletions

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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()));
}
}

View file

@ -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;

View file

@ -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);

View file

@ -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: