diff options
-rw-r--r-- | Libraries/LibJS/Runtime/StringConstructor.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/StringObject.cpp | 13 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/StringObject.h | 13 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/StringPrototype.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 2 |
5 files changed, 23 insertions, 12 deletions
diff --git a/Libraries/LibJS/Runtime/StringConstructor.cpp b/Libraries/LibJS/Runtime/StringConstructor.cpp index f28c89b307..b92f0491c9 100644 --- a/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -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); } } diff --git a/Libraries/LibJS/Runtime/StringObject.cpp b/Libraries/LibJS/Runtime/StringObject.cpp index 2d6c76c580..cb67dbbc27 100644 --- a/Libraries/LibJS/Runtime/StringObject.cpp +++ b/Libraries/LibJS/Runtime/StringObject.cpp @@ -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); } } diff --git a/Libraries/LibJS/Runtime/StringObject.h b/Libraries/LibJS/Runtime/StringObject.h index 2f914e4246..b7efd82b7c 100644 --- a/Libraries/LibJS/Runtime/StringObject.h +++ b/Libraries/LibJS/Runtime/StringObject.h @@ -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; }; } diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index c75f9ab1ba..146e0890d9 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -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); diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index cd400082ff..a5a6adba49 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -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); |