mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 03:07:43 +00:00
LibJS: Pass prototype to StringObject constructor
This commit is contained in:
parent
cf702a13b9
commit
298c606200
5 changed files with 23 additions and 12 deletions
|
@ -57,7 +57,9 @@ Value StringConstructor::construct(Interpreter& interpreter)
|
||||||
primitive_string = js_string(interpreter, "");
|
primitive_string = js_string(interpreter, "");
|
||||||
else
|
else
|
||||||
primitive_string = js_string(interpreter, interpreter.argument(0).to_string());
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include <LibJS/Heap/Heap.h>
|
#include <LibJS/Heap/Heap.h>
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/PrimitiveString.h>
|
#include <LibJS/Runtime/PrimitiveString.h>
|
||||||
#include <LibJS/Runtime/StringObject.h>
|
#include <LibJS/Runtime/StringObject.h>
|
||||||
#include <LibJS/Runtime/StringPrototype.h>
|
#include <LibJS/Runtime/StringPrototype.h>
|
||||||
|
@ -33,10 +34,16 @@
|
||||||
|
|
||||||
namespace JS {
|
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)
|
: m_string(string)
|
||||||
{
|
{
|
||||||
set_prototype(interpreter().string_prototype());
|
set_prototype(&prototype);
|
||||||
}
|
}
|
||||||
|
|
||||||
StringObject::~StringObject()
|
StringObject::~StringObject()
|
||||||
|
@ -46,7 +53,7 @@ StringObject::~StringObject()
|
||||||
void StringObject::visit_children(Cell::Visitor& visitor)
|
void StringObject::visit_children(Cell::Visitor& visitor)
|
||||||
{
|
{
|
||||||
Object::visit_children(visitor);
|
Object::visit_children(visitor);
|
||||||
visitor.visit(m_string);
|
visitor.visit(&m_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,21 +32,24 @@ namespace JS {
|
||||||
|
|
||||||
class StringObject : public Object {
|
class StringObject : public Object {
|
||||||
public:
|
public:
|
||||||
explicit StringObject(PrimitiveString*);
|
static StringObject* create(GlobalObject&, PrimitiveString&);
|
||||||
|
|
||||||
|
StringObject(PrimitiveString&, Object& prototype);
|
||||||
virtual ~StringObject() override;
|
virtual ~StringObject() override;
|
||||||
|
|
||||||
virtual void visit_children(Visitor&) override;
|
// FIXME: Return const PrimitiveString&
|
||||||
const PrimitiveString* primitive_string() const { return m_string; }
|
const PrimitiveString* primitive_string() const { return &m_string; }
|
||||||
virtual Value value_of() const override
|
virtual Value value_of() const override
|
||||||
{
|
{
|
||||||
return Value(m_string);
|
return Value(&m_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
virtual void visit_children(Visitor&) override;
|
||||||
virtual const char* class_name() const override { return "StringObject"; }
|
virtual const char* class_name() const override { return "StringObject"; }
|
||||||
virtual bool is_string_object() const override { return true; }
|
virtual bool is_string_object() const override { return true; }
|
||||||
|
|
||||||
PrimitiveString* m_string { nullptr };
|
PrimitiveString& m_string;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,9 +38,8 @@
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
StringPrototype::StringPrototype()
|
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_property("length", length_getter, nullptr);
|
||||||
put_native_function("charAt", char_at, 1);
|
put_native_function("charAt", char_at, 1);
|
||||||
put_native_function("repeat", repeat, 1);
|
put_native_function("repeat", repeat, 1);
|
||||||
|
|
|
@ -113,7 +113,7 @@ Object* Value::to_object(Heap& heap) const
|
||||||
return &const_cast<Object&>(as_object());
|
return &const_cast<Object&>(as_object());
|
||||||
|
|
||||||
if (is_string())
|
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())
|
if (is_number())
|
||||||
return NumberObject::create(heap.interpreter().global_object(), m_value.as_double);
|
return NumberObject::create(heap.interpreter().global_object(), m_value.as_double);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue