Skip to content

Commit

Permalink
LibJS: Get the prototype of a new String from the constructor's realm
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanHo authored and linusg committed Jun 30, 2021
1 parent 5606332 commit c254e4c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
17 changes: 11 additions & 6 deletions Userland/Libraries/LibJS/Runtime/StringConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions Userland/Libraries/LibJS/Runtime/StringObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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)
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/StringObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c254e4c

Please sign in to comment.