1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:47:35 +00:00

LibJS: Replace StringPrototype's typed_this() with this_string_value()

This brings the code close to the spec with no trade-offs, which is
always valuable.

No functionality change.
This commit is contained in:
Linus Groh 2021-06-06 16:38:05 +01:00 committed by Andreas Kling
parent 68d669443f
commit 6f96f01171

View file

@ -22,18 +22,6 @@
namespace JS { namespace JS {
static StringObject* typed_this(VM& vm, GlobalObject& global_object)
{
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return nullptr;
if (!is<StringObject>(this_object)) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "String");
return nullptr;
}
return static_cast<StringObject*>(this_object);
}
static String ak_string_from(VM& vm, GlobalObject& global_object) static String ak_string_from(VM& vm, GlobalObject& global_object)
{ {
auto this_value = require_object_coercible(global_object, vm.this_value(global_object)); auto this_value = require_object_coercible(global_object, vm.this_value(global_object));
@ -111,6 +99,18 @@ StringPrototype::~StringPrototype()
{ {
} }
// thisStringValue, https://tc39.es/ecma262/#thisstringvalue
static Value this_string_value(GlobalObject& global_object, Value value)
{
if (value.is_string())
return value;
if (value.is_object() && is<StringObject>(value.as_object()))
return static_cast<StringObject&>(value.as_object()).value_of();
auto& vm = global_object.vm();
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "String");
return {};
}
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_at) JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_at)
{ {
auto string = ak_string_from(vm, global_object); auto string = ak_string_from(vm, global_object);
@ -274,20 +274,17 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_uppercase)
return js_string(vm, string.to_uppercase()); return js_string(vm, string.to_uppercase());
} }
JS_DEFINE_NATIVE_GETTER(StringPrototype::length_getter) JS_DEFINE_NATIVE_FUNCTION(StringPrototype::length_getter)
{ {
auto* string_object = typed_this(vm, global_object); auto string_value = this_string_value(global_object, vm.this_value(global_object));
if (!string_object) if (vm.exception())
return {}; return {};
return Value((i32)string_object->primitive_string().string().length()); return Value((i32)string_value.as_string().string().length());
} }
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_string) JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_string)
{ {
auto* string_object = typed_this(vm, global_object); return this_string_value(global_object, vm.this_value(global_object));
if (!string_object)
return {};
return js_string(vm, string_object->primitive_string().string());
} }
enum class PadPlacement { enum class PadPlacement {