1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:57:36 +00:00

LibJS: Pass prototype to BooleanObject constructor

This commit is contained in:
Andreas Kling 2020-04-17 19:03:52 +02:00
parent 298c606200
commit 2a15323029
5 changed files with 15 additions and 8 deletions

View file

@ -50,8 +50,7 @@ Value BooleanConstructor::call(Interpreter& interpreter)
Value BooleanConstructor::construct(Interpreter& interpreter) Value BooleanConstructor::construct(Interpreter& interpreter)
{ {
auto* bool_object = interpreter.heap().allocate<BooleanObject>(interpreter.argument(0).to_boolean()); return BooleanObject::create(interpreter.global_object(), interpreter.argument(0).to_boolean());
return Value(bool_object);
} }
} }

View file

@ -26,13 +26,20 @@
#include <LibJS/Interpreter.h> #include <LibJS/Interpreter.h>
#include <LibJS/Runtime/BooleanObject.h> #include <LibJS/Runtime/BooleanObject.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace JS { namespace JS {
BooleanObject::BooleanObject(bool value) BooleanObject* BooleanObject::create(GlobalObject& global_object, bool value)
{
auto& interpreter = global_object.interpreter();
return interpreter.heap().allocate<BooleanObject>(value, *interpreter.boolean_prototype());
}
BooleanObject::BooleanObject(bool value, Object& prototype)
: m_value(value) : m_value(value)
{ {
set_prototype(interpreter().boolean_prototype()); set_prototype(&prototype);
} }
BooleanObject::~BooleanObject() BooleanObject::~BooleanObject()

View file

@ -31,7 +31,9 @@
namespace JS { namespace JS {
class BooleanObject : public Object { class BooleanObject : public Object {
public: public:
explicit BooleanObject(bool); static BooleanObject* create(GlobalObject&, bool);
BooleanObject(bool, Object& prototype);
virtual ~BooleanObject() override; virtual ~BooleanObject() override;
virtual Value value_of() const override virtual Value value_of() const override

View file

@ -32,9 +32,8 @@
namespace JS { namespace JS {
BooleanPrototype::BooleanPrototype() BooleanPrototype::BooleanPrototype()
: BooleanObject(false) : BooleanObject(false, *interpreter().object_prototype())
{ {
set_prototype(interpreter().object_prototype());
put_native_function("toString", to_string); put_native_function("toString", to_string);
put_native_function("valueOf", value_of); put_native_function("valueOf", value_of);
} }

View file

@ -119,7 +119,7 @@ Object* Value::to_object(Heap& heap) const
return NumberObject::create(heap.interpreter().global_object(), m_value.as_double); return NumberObject::create(heap.interpreter().global_object(), m_value.as_double);
if (is_boolean()) if (is_boolean())
return heap.allocate<BooleanObject>(m_value.as_bool); return BooleanObject::create(heap.interpreter().global_object(), m_value.as_bool);
if (is_null() || is_undefined()) { if (is_null() || is_undefined()) {
heap.interpreter().throw_exception<TypeError>("ToObject on null or undefined."); heap.interpreter().throw_exception<TypeError>("ToObject on null or undefined.");