mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:47:45 +00:00
LibJS: Define Value::to_string_without_side_effects for String
This commit is contained in:
parent
dc4207323b
commit
3ffb6d9b5a
2 changed files with 28 additions and 11 deletions
|
@ -355,35 +355,51 @@ StringView Value::typeof() const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DeprecatedString Value::to_deprecated_string_without_side_effects() const
|
ErrorOr<String> Value::to_string_without_side_effects() const
|
||||||
{
|
{
|
||||||
if (is_double())
|
if (is_double())
|
||||||
return number_to_deprecated_string(m_value.as_double);
|
return number_to_string(m_value.as_double);
|
||||||
|
|
||||||
switch (m_value.tag) {
|
switch (m_value.tag) {
|
||||||
case UNDEFINED_TAG:
|
case UNDEFINED_TAG:
|
||||||
return "undefined";
|
return String::from_utf8("undefined"sv);
|
||||||
case NULL_TAG:
|
case NULL_TAG:
|
||||||
return "null";
|
return String::from_utf8("null"sv);
|
||||||
case BOOLEAN_TAG:
|
case BOOLEAN_TAG:
|
||||||
return as_bool() ? "true" : "false";
|
return String::from_utf8(as_bool() ? "true"sv : "false"sv);
|
||||||
case INT32_TAG:
|
case INT32_TAG:
|
||||||
return DeprecatedString::number(as_i32());
|
return String::number(as_i32());
|
||||||
case STRING_TAG:
|
case STRING_TAG:
|
||||||
return MUST(as_string().deprecated_string());
|
if (auto string = as_string().utf8_string(); string.is_throw_completion()) {
|
||||||
|
auto completion = string.release_error();
|
||||||
|
|
||||||
|
// We can't explicitly check for OOM because InternalError does not store the ErrorType
|
||||||
|
VERIFY(completion.value().has_value());
|
||||||
|
VERIFY(completion.value()->is_object());
|
||||||
|
VERIFY(is<InternalError>(completion.value()->as_object()));
|
||||||
|
|
||||||
|
return AK::Error::from_errno(ENOMEM);
|
||||||
|
} else {
|
||||||
|
return string.release_value();
|
||||||
|
}
|
||||||
case SYMBOL_TAG:
|
case SYMBOL_TAG:
|
||||||
return as_symbol().descriptive_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
|
return as_symbol().descriptive_string();
|
||||||
case BIGINT_TAG:
|
case BIGINT_TAG:
|
||||||
return as_bigint().to_deprecated_string();
|
return as_bigint().to_string();
|
||||||
case OBJECT_TAG:
|
case OBJECT_TAG:
|
||||||
return DeprecatedString::formatted("[object {}]", as_object().class_name());
|
return String::formatted("[object {}]", as_object().class_name());
|
||||||
case ACCESSOR_TAG:
|
case ACCESSOR_TAG:
|
||||||
return "<accessor>";
|
return String::from_utf8("<accessor>"sv);
|
||||||
default:
|
default:
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DeprecatedString Value::to_deprecated_string_without_side_effects() const
|
||||||
|
{
|
||||||
|
return MUST(to_string_without_side_effects()).to_deprecated_string();
|
||||||
|
}
|
||||||
|
|
||||||
ThrowCompletionOr<PrimitiveString*> Value::to_primitive_string(VM& vm)
|
ThrowCompletionOr<PrimitiveString*> Value::to_primitive_string(VM& vm)
|
||||||
{
|
{
|
||||||
if (is_string())
|
if (is_string())
|
||||||
|
|
|
@ -394,6 +394,7 @@ public:
|
||||||
ThrowCompletionOr<Value> get(VM&, PropertyKey const&) const;
|
ThrowCompletionOr<Value> get(VM&, PropertyKey const&) const;
|
||||||
ThrowCompletionOr<FunctionObject*> get_method(VM&, PropertyKey const&) const;
|
ThrowCompletionOr<FunctionObject*> get_method(VM&, PropertyKey const&) const;
|
||||||
|
|
||||||
|
ErrorOr<String> to_string_without_side_effects() const;
|
||||||
DeprecatedString to_deprecated_string_without_side_effects() const;
|
DeprecatedString to_deprecated_string_without_side_effects() const;
|
||||||
|
|
||||||
Value value_or(Value fallback) const
|
Value value_or(Value fallback) const
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue