瀏覽代碼

LibJS: Get the prototype of a new String from the constructor's realm

Idan Horowitz 4 年之前
父節點
當前提交
c254e4cf10

+ 11 - 6
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
 // 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
     else
-        primitive_string = vm().argument(0).to_primitive_string(global_object());
+        primitive_string = vm.argument(0).to_primitive_string(global_object());
     if (!primitive_string)
     if (!primitive_string)
         return {};
         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
 // 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw

+ 3 - 2
Userland/Libraries/LibJS/Runtime/StringObject.cpp

@@ -11,9 +11,10 @@
 
 
 namespace JS {
 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<StringObject>(global_object, primitive_string, *global_object.string_prototype());
+    return global_object.heap().allocate<StringObject>(global_object, primitive_string, prototype);
 }
 }
 
 
 StringObject::StringObject(PrimitiveString& string, Object& prototype)
 StringObject::StringObject(PrimitiveString& string, Object& prototype)

+ 1 - 1
Userland/Libraries/LibJS/Runtime/StringObject.h

@@ -14,7 +14,7 @@ class StringObject : public Object {
     JS_OBJECT(StringObject, Object);
     JS_OBJECT(StringObject, Object);
 
 
 public:
 public:
-    static StringObject* create(GlobalObject&, PrimitiveString&);
+    static StringObject* create(GlobalObject&, PrimitiveString&, Object& prototype);
 
 
     StringObject(PrimitiveString&, Object& prototype);
     StringObject(PrimitiveString&, Object& prototype);
     virtual void initialize(GlobalObject&) override;
     virtual void initialize(GlobalObject&) override;

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Value.cpp

@@ -449,7 +449,7 @@ Object* Value::to_object(GlobalObject& global_object) const
     case Type::Double:
     case Type::Double:
         return NumberObject::create(global_object, as_double());
         return NumberObject::create(global_object, as_double());
     case Type::String:
     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:
     case Type::Symbol:
         return SymbolObject::create(global_object, *m_value.as_symbol);
         return SymbolObject::create(global_object, *m_value.as_symbol);
     case Type::BigInt:
     case Type::BigInt: