From c254e4cf10264cefe805578b14f2e4deaf286f56 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Wed, 30 Jun 2021 18:13:51 +0300 Subject: [PATCH] LibJS: Get the prototype of a new String from the constructor's realm --- .../LibJS/Runtime/StringConstructor.cpp | 17 +++++++++++------ .../Libraries/LibJS/Runtime/StringObject.cpp | 5 +++-- Userland/Libraries/LibJS/Runtime/StringObject.h | 2 +- Userland/Libraries/LibJS/Runtime/Value.cpp | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp index c89bdd22af..52c300121b 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 b23015ef12..cef42a85d6 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 3f74ec8769..d05d171c7e 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 0c8742b2f0..4c8f97a5bb 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: