diff --git a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp index c89bdd22af77f9..52c300121bd507 100644 --- a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -54,16 +54,21 @@ Value StringConstructor::call() } // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value -Value StringConstructor::construct(FunctionObject&) +Value StringConstructor::construct(FunctionObject& new_target) { - PrimitiveString* primitive_string = nullptr; - if (!vm().argument_count()) - primitive_string = js_string(vm(), ""); + auto& vm = global_object().vm(); + + PrimitiveString* primitive_string; + if (!vm.argument_count()) + primitive_string = js_string(vm, ""); else - primitive_string = vm().argument(0).to_primitive_string(global_object()); + primitive_string = vm.argument(0).to_primitive_string(global_object()); if (!primitive_string) return {}; - return StringObject::create(global_object(), *primitive_string); + auto* prototype = get_prototype_from_constructor(global_object(), new_target, &GlobalObject::string_prototype); + if (vm.exception()) + return {}; + return StringObject::create(global_object(), *primitive_string, *prototype); } // 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw diff --git a/Userland/Libraries/LibJS/Runtime/StringObject.cpp b/Userland/Libraries/LibJS/Runtime/StringObject.cpp index b23015ef124751..cef42a85d6ea86 100644 --- a/Userland/Libraries/LibJS/Runtime/StringObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringObject.cpp @@ -11,9 +11,10 @@ namespace JS { -StringObject* StringObject::create(GlobalObject& global_object, PrimitiveString& primitive_string) +// 10.4.3.4 StringCreate ( value, prototype ), https://tc39.es/ecma262/#sec-stringcreate +StringObject* StringObject::create(GlobalObject& global_object, PrimitiveString& primitive_string, Object& prototype) { - return global_object.heap().allocate(global_object, primitive_string, *global_object.string_prototype()); + return global_object.heap().allocate(global_object, primitive_string, prototype); } StringObject::StringObject(PrimitiveString& string, Object& prototype) diff --git a/Userland/Libraries/LibJS/Runtime/StringObject.h b/Userland/Libraries/LibJS/Runtime/StringObject.h index 3f74ec8769293b..d05d171c7eb6f1 100644 --- a/Userland/Libraries/LibJS/Runtime/StringObject.h +++ b/Userland/Libraries/LibJS/Runtime/StringObject.h @@ -14,7 +14,7 @@ class StringObject : public Object { JS_OBJECT(StringObject, Object); public: - static StringObject* create(GlobalObject&, PrimitiveString&); + static StringObject* create(GlobalObject&, PrimitiveString&, Object& prototype); StringObject(PrimitiveString&, Object& prototype); virtual void initialize(GlobalObject&) override; diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 0c8742b2f0792d..4c8f97a5bb856d 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -449,7 +449,7 @@ Object* Value::to_object(GlobalObject& global_object) const case Type::Double: return NumberObject::create(global_object, as_double()); case Type::String: - return StringObject::create(global_object, *m_value.as_string); + return StringObject::create(global_object, *m_value.as_string, *global_object.string_prototype()); case Type::Symbol: return SymbolObject::create(global_object, *m_value.as_symbol); case Type::BigInt: