LibJS: Pass prototype to StringObject constructor

This commit is contained in:
Andreas Kling 2020-04-17 19:01:31 +02:00
parent cf702a13b9
commit 298c606200
Notes: sideshowbarker 2024-07-19 07:31:41 +09:00
5 changed files with 23 additions and 12 deletions

View file

@ -57,7 +57,9 @@ Value StringConstructor::construct(Interpreter& interpreter)
primitive_string = js_string(interpreter, "");
else
primitive_string = js_string(interpreter, interpreter.argument(0).to_string());
return Value(interpreter.heap().allocate<StringObject>(primitive_string));
if (!primitive_string)
return {};
return StringObject::create(interpreter.global_object(), *primitive_string);
}
}

View file

@ -26,6 +26,7 @@
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/StringPrototype.h>
@ -33,10 +34,16 @@
namespace JS {
StringObject::StringObject(PrimitiveString* string)
StringObject* StringObject::create(GlobalObject& global_object, PrimitiveString& primitive_string)
{
auto& interpreter = global_object.interpreter();
return interpreter.heap().allocate<StringObject>(primitive_string, *interpreter.string_prototype());
}
StringObject::StringObject(PrimitiveString& string, Object& prototype)
: m_string(string)
{
set_prototype(interpreter().string_prototype());
set_prototype(&prototype);
}
StringObject::~StringObject()
@ -46,7 +53,7 @@ StringObject::~StringObject()
void StringObject::visit_children(Cell::Visitor& visitor)
{
Object::visit_children(visitor);
visitor.visit(m_string);
visitor.visit(&m_string);
}
}

View file

@ -32,21 +32,24 @@ namespace JS {
class StringObject : public Object {
public:
explicit StringObject(PrimitiveString*);
static StringObject* create(GlobalObject&, PrimitiveString&);
StringObject(PrimitiveString&, Object& prototype);
virtual ~StringObject() override;
virtual void visit_children(Visitor&) override;
const PrimitiveString* primitive_string() const { return m_string; }
// FIXME: Return const PrimitiveString&
const PrimitiveString* primitive_string() const { return &m_string; }
virtual Value value_of() const override
{
return Value(m_string);
return Value(&m_string);
}
private:
virtual void visit_children(Visitor&) override;
virtual const char* class_name() const override { return "StringObject"; }
virtual bool is_string_object() const override { return true; }
PrimitiveString* m_string { nullptr };
PrimitiveString& m_string;
};
}

View file

@ -38,9 +38,8 @@
namespace JS {
StringPrototype::StringPrototype()
: StringObject(js_string(interpreter(), String::empty()))
: StringObject(*js_string(interpreter(), String::empty()), *interpreter().object_prototype())
{
set_prototype(interpreter().object_prototype());
put_native_property("length", length_getter, nullptr);
put_native_function("charAt", char_at, 1);
put_native_function("repeat", repeat, 1);

View file

@ -113,7 +113,7 @@ Object* Value::to_object(Heap& heap) const
return &const_cast<Object&>(as_object());
if (is_string())
return heap.allocate<StringObject>(m_value.as_string);
return StringObject::create(heap.interpreter().global_object(), *m_value.as_string);
if (is_number())
return NumberObject::create(heap.interpreter().global_object(), m_value.as_double);