1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 06:47:34 +00:00

LibJS: Convert to_string() to ThrowCompletionOr

Also update get_function_name() to use ThrowCompletionOr, but this is
not a standard AO and should be refactored out of existence eventually.
This commit is contained in:
Linus Groh 2021-10-12 17:49:01 +01:00
parent 5d38cf4973
commit 4d8912a92b
48 changed files with 171 additions and 415 deletions

View file

@ -38,9 +38,7 @@ JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::escape)
return {};
}
auto identifier = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto identifier = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
return JS::js_string(vm, Web::CSS::serialize_an_identifier(identifier));
}
@ -55,17 +53,13 @@ JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::supports)
if (vm.argument_count() >= 2) {
// When the supports(property, value) method is invoked with two arguments property and value:
String property_name = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto property_name = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
// If property is an ASCII case-insensitive match for any defined CSS property that the UA supports,
// and value successfully parses according to that propertys grammar, return true.
auto property = CSS::property_id_from_string(property_name);
if (property != CSS::PropertyID::Invalid) {
auto value_string = vm.argument(1).to_string(global_object);
if (vm.exception())
return {};
auto value_string = TRY_OR_DISCARD(vm.argument(1).to_string(global_object));
if (parse_css_value({}, value_string, property))
return JS::Value(true);
}
@ -79,9 +73,7 @@ JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::supports)
return JS::Value(false);
} else {
// When the supports(conditionText) method is invoked with a single conditionText argument:
String supports_text = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto supports_text = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
// If conditionText, parsed and evaluated as a <supports-condition>, would return true, return true.
if (auto supports = parse_css_supports({}, supports_text); supports && supports->matches())

View file

@ -48,9 +48,7 @@ JS::ThrowCompletionOr<bool> CSSStyleDeclarationWrapper::internal_set(JS::Propert
if (property_id == CSS::PropertyID::Invalid)
return Base::internal_set(name, value, receiver);
auto css_text = value.to_string(global_object());
if (auto* exception = vm().exception())
return JS::throw_completion(exception->value());
auto css_text = TRY(value.to_string(global_object()));
impl().set_property(property_id, css_text);
return true;

View file

@ -53,9 +53,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_getter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_setter)
{
auto& window = static_cast<WindowObject&>(global_object);
auto new_href = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto new_href = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
auto href_url = window.impl().associated_document().parse_url(new_href);
if (!href_url.is_valid()) {
vm.throw_exception<JS::URIError>(global_object, String::formatted("Invalid URL '{}'", new_href));
@ -133,9 +131,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::replace)
{
auto& window = static_cast<WindowObject&>(global_object);
auto url = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto url = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
// FIXME: This needs spec compliance work.
window.impl().did_call_location_replace({}, move(url));
return JS::js_undefined();

View file

@ -190,11 +190,8 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
if (!impl)
return {};
String message = "";
if (vm.argument_count()) {
message = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
}
if (vm.argument_count())
message = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
impl->alert(message);
return JS::js_undefined();
}
@ -205,11 +202,8 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
if (!impl)
return {};
String message = "";
if (!vm.argument(0).is_undefined()) {
message = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
}
if (!vm.argument(0).is_undefined())
message = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
return JS::Value(impl->confirm(message));
}
@ -220,16 +214,10 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::prompt)
return {};
String message = "";
String default_ = "";
if (!vm.argument(0).is_undefined()) {
message = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
}
if (!vm.argument(1).is_undefined()) {
default_ = vm.argument(1).to_string(global_object);
if (vm.exception())
return {};
}
if (!vm.argument(0).is_undefined())
message = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
if (!vm.argument(1).is_undefined())
default_ = TRY_OR_DISCARD(vm.argument(1).to_string(global_object));
auto response = impl->prompt(message, default_);
if (response.is_null())
return JS::js_null();
@ -252,9 +240,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
if (vm.argument(0).is_function()) {
callback = &vm.argument(0).as_function();
} else {
auto script_source = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto script_source = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
// FIXME: This needs more work once we have a environment settings object.
// The spec wants us to use a task for the "run function or script string" part,
// using a NativeFunction for the latter is a workaround so that we can reuse the
@ -293,9 +279,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
if (vm.argument(0).is_function()) {
callback = &vm.argument(0).as_function();
} else {
auto script_source = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto script_source = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
// FIXME: This needs more work once we have a environment settings object.
// The spec wants us to use a task for the "run function or script string" part,
// using a NativeFunction for the latter is a workaround so that we can reuse the
@ -415,9 +399,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
return {};
}
auto string = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto string = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
auto decoded = decode_base64(StringView(string));
// decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
@ -435,9 +417,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
return {};
}
auto string = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto string = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
Vector<u8> byte_string;
byte_string.ensure_capacity(string.length());
@ -593,9 +573,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::match_media)
auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
auto media = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto media = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
return wrap(global_object, impl->match_media(move(media)));
}
@ -660,9 +638,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll)
auto behavior_string_value = TRY_OR_DISCARD(options->get("behavior"));
if (!behavior_string_value.is_undefined())
behavior_string = behavior_string_value.to_string(global_object);
if (vm.exception())
return {};
behavior_string = TRY_OR_DISCARD(behavior_string_value.to_string(global_object));
if (behavior_string != "smooth" && behavior_string != "auto") {
vm.throw_exception<JS::TypeError>(global_object, "Behavior is not one of 'smooth' or 'auto'");
return {};
@ -737,9 +713,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_by)
top = top + current_scroll_position.y();
auto behavior_string_value = TRY_OR_DISCARD(options->get("behavior"));
auto behavior_string = behavior_string_value.is_undefined() ? "auto" : behavior_string_value.to_string(global_object);
if (vm.exception())
return {};
auto behavior_string = behavior_string_value.is_undefined() ? "auto" : TRY_OR_DISCARD(behavior_string_value.to_string(global_object));
if (behavior_string != "smooth" && behavior_string != "auto") {
vm.throw_exception<JS::TypeError>(global_object, "Behavior is not one of 'smooth' or 'auto'");
return {};