mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 12:35:14 +00:00
LibJS: Convert to_primitive_string() to ThrowCompletionOr
This commit is contained in:
parent
da59c77fe3
commit
96ab116f0d
Notes:
sideshowbarker
2024-07-18 02:47:38 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/96ab116f0dd Pull-request: https://github.com/SerenityOS/serenity/pull/10452 Reviewed-by: https://github.com/IdanHo ✅
8 changed files with 18 additions and 30 deletions
|
@ -873,7 +873,7 @@ Value canonical_numeric_index_string(GlobalObject& global_object, PropertyName c
|
|||
auto n = argument.to_number(global_object);
|
||||
|
||||
// 4. If SameValue(! ToString(n), argument) is false, return undefined.
|
||||
if (!same_value(n.to_primitive_string(global_object), argument))
|
||||
if (!same_value(MUST(n.to_primitive_string(global_object)), argument))
|
||||
return js_undefined();
|
||||
|
||||
// 5. Return n.
|
||||
|
|
|
@ -1009,12 +1009,15 @@ static void array_merge_sort(VM& vm, GlobalObject& global_object, FunctionObject
|
|||
// the Abstract Relational Comparison in line once iterating over code points, rather
|
||||
// than calling it twice after creating two primitive strings.
|
||||
|
||||
auto x_string = x.to_primitive_string(global_object);
|
||||
if (vm.exception())
|
||||
auto x_string_or_error = x.to_primitive_string(global_object);
|
||||
if (x_string_or_error.is_error())
|
||||
return;
|
||||
auto y_string = y.to_primitive_string(global_object);
|
||||
if (vm.exception())
|
||||
auto x_string = x_string_or_error.release_value();
|
||||
|
||||
auto y_string_or_error = y.to_primitive_string(global_object);
|
||||
if (y_string_or_error.is_error())
|
||||
return;
|
||||
auto y_string = y_string_or_error.release_value();
|
||||
|
||||
auto x_string_value = Value(x_string);
|
||||
auto y_string_value = Value(y_string);
|
||||
|
|
|
@ -633,9 +633,7 @@ ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& o
|
|||
// 6. If type is "string", then
|
||||
else {
|
||||
// a. Let value be ? ToString(value).
|
||||
value = value.to_primitive_string(global_object);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
value = TRY(value.to_primitive_string(global_object));
|
||||
}
|
||||
|
||||
// 7. If values is not undefined and values does not contain an element equal to value, throw a RangeError exception.
|
||||
|
|
|
@ -87,9 +87,7 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu
|
|||
if (vm.exception())
|
||||
return {};
|
||||
} else if (is<StringObject>(space_object)) {
|
||||
space = space.to_primitive_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
space = TRY_OR_DISCARD(space.to_primitive_string(global_object));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -157,9 +155,7 @@ String JSONObject::serialize_json_property(GlobalObject& global_object, Stringif
|
|||
if (vm.exception())
|
||||
return {};
|
||||
} else if (is<StringObject>(value_object)) {
|
||||
value = value.to_primitive_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
value = TRY_OR_DISCARD(value.to_primitive_string(global_object));
|
||||
} else if (is<BooleanObject>(value_object)) {
|
||||
value = static_cast<BooleanObject&>(value_object).value_of();
|
||||
} else if (is<BigIntObject>(value_object)) {
|
||||
|
|
|
@ -49,10 +49,7 @@ Value StringConstructor::call()
|
|||
return js_string(heap(), "");
|
||||
if (vm().argument(0).is_symbol())
|
||||
return js_string(heap(), vm().argument(0).as_symbol().to_string());
|
||||
auto* string = vm().argument(0).to_primitive_string(global_object());
|
||||
if (vm().exception())
|
||||
return {};
|
||||
return string;
|
||||
return TRY_OR_DISCARD(vm().argument(0).to_primitive_string(global_object()));
|
||||
}
|
||||
|
||||
// 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
|
||||
|
@ -64,9 +61,7 @@ Value StringConstructor::construct(FunctionObject& new_target)
|
|||
if (!vm.argument_count())
|
||||
primitive_string = js_string(vm, "");
|
||||
else
|
||||
primitive_string = vm.argument(0).to_primitive_string(global_object());
|
||||
if (!primitive_string)
|
||||
return {};
|
||||
primitive_string = TRY_OR_DISCARD(vm.argument(0).to_primitive_string(global_object()));
|
||||
auto* prototype = TRY_OR_DISCARD(get_prototype_from_constructor(global_object(), new_target, &GlobalObject::string_prototype));
|
||||
return StringObject::create(global_object(), *primitive_string, *prototype);
|
||||
}
|
||||
|
|
|
@ -150,9 +150,7 @@ ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& o
|
|||
// 9. Else,
|
||||
else {
|
||||
// a. Set value to ? ToString(value).
|
||||
value = value.to_primitive_string(global_object);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
value = TRY(value.to_primitive_string(global_object));
|
||||
}
|
||||
|
||||
// 10. If values is not empty, then
|
||||
|
@ -1117,9 +1115,7 @@ ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject& global_object,
|
|||
} else if (property.is_one_of("month", "day")) {
|
||||
value = Value(TRY(to_positive_integer(global_object, value)));
|
||||
} else if (property.is_one_of("monthCode", "offset", "era")) {
|
||||
value = value.to_primitive_string(global_object);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
value = TRY(value.to_primitive_string(global_object));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -323,11 +323,11 @@ String Value::to_string_without_side_effects() const
|
|||
}
|
||||
}
|
||||
|
||||
PrimitiveString* Value::to_primitive_string(GlobalObject& global_object)
|
||||
ThrowCompletionOr<PrimitiveString*> Value::to_primitive_string(GlobalObject& global_object)
|
||||
{
|
||||
if (is_string())
|
||||
return &as_string();
|
||||
auto string = TRY_OR_DISCARD(to_string(global_object));
|
||||
auto string = TRY(to_string(global_object));
|
||||
return js_string(global_object.heap(), string);
|
||||
}
|
||||
|
||||
|
|
|
@ -305,7 +305,7 @@ public:
|
|||
|
||||
ThrowCompletionOr<String> to_string(GlobalObject&) const;
|
||||
ThrowCompletionOr<Utf16String> to_utf16_string(GlobalObject&) const;
|
||||
PrimitiveString* to_primitive_string(GlobalObject&);
|
||||
ThrowCompletionOr<PrimitiveString*> to_primitive_string(GlobalObject&);
|
||||
Value to_primitive(GlobalObject&, PreferredType preferred_type = PreferredType::Default) const;
|
||||
Object* to_object(GlobalObject&) const;
|
||||
Value to_numeric(GlobalObject&) const;
|
||||
|
|
Loading…
Add table
Reference in a new issue