From 60366ae03239b9987539e9359ed06276488f50ec Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 18 Oct 2021 19:49:36 +0100 Subject: [PATCH] LibJS: Convert this_string_value() to ThrowCompletionOr --- Userland/Libraries/LibJS/Runtime/StringObject.h | 4 +++- .../Libraries/LibJS/Runtime/StringPrototype.cpp | 14 +++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/StringObject.h b/Userland/Libraries/LibJS/Runtime/StringObject.h index 4b28d0c670..3d82be4ffa 100644 --- a/Userland/Libraries/LibJS/Runtime/StringObject.h +++ b/Userland/Libraries/LibJS/Runtime/StringObject.h @@ -20,7 +20,9 @@ public: virtual void initialize(GlobalObject&) override; virtual ~StringObject() override; - const PrimitiveString& primitive_string() const { return m_string; } + PrimitiveString const& primitive_string() const { return m_string; } + PrimitiveString& primitive_string() { return m_string; } + virtual Value value_of() const override { return Value(&m_string); diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index b95f2697cf..3541f95584 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -164,15 +165,14 @@ StringPrototype::~StringPrototype() } // thisStringValue ( value ), https://tc39.es/ecma262/#thisstringvalue -static Value this_string_value(GlobalObject& global_object, Value value) +static ThrowCompletionOr this_string_value(GlobalObject& global_object, Value value) { if (value.is_string()) - return value; + return &value.as_string(); if (value.is_object() && is(value.as_object())) - return static_cast(value.as_object()).value_of(); + return &static_cast(value.as_object()).primitive_string(); auto& vm = global_object.vm(); - vm.throw_exception(global_object, ErrorType::NotAnObjectOfType, "String"); - return {}; + return vm.throw_completion(global_object, ErrorType::NotAnObjectOfType, "String"); } // 22.1.3.1 String.prototype.charAt ( pos ), https://tc39.es/ecma262/#sec-string.prototype.charat @@ -431,13 +431,13 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_uppercase) // 22.1.3.27 String.prototype.toString ( ), https://tc39.es/ecma262/#sec-string.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_string) { - return this_string_value(global_object, vm.this_value(global_object)); + return TRY_OR_DISCARD(this_string_value(global_object, vm.this_value(global_object))); } // 22.1.3.32 String.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-string.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(StringPrototype::value_of) { - return this_string_value(global_object, vm.this_value(global_object)); + return TRY_OR_DISCARD(this_string_value(global_object, vm.this_value(global_object))); } enum class PadPlacement {