diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-02-16 14:09:11 -0500 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2023-02-17 09:14:23 -0500 |
commit | 88814acbd39ba252167e48f48f5b938df142065b (patch) | |
tree | c00f3aba6c299cb6d131204274fa4b4a3e532488 /Userland/Libraries/LibWeb | |
parent | 153b7936381916aef185686d84e255380bb80f1d (diff) | |
download | serenity-88814acbd39ba252167e48f48f5b938df142065b.zip |
LibJS+Everywhere: Convert JS::Error to String
This includes an Error::create overload to create an Error from a UTF-8
StringView. If creating a String from that view fails, the factory will
return an OOM InternalError instead. VM::throw_completion can also make
use of this overload via its perfect forwarding.
Diffstat (limited to 'Userland/Libraries/LibWeb')
14 files changed, 31 insertions, 30 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp b/Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp index c533cdf488..1d1a1c7db6 100644 --- a/Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp +++ b/Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp @@ -219,7 +219,7 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm) } return record_union_type; } - return vm.throw_completion<JS::TypeError>("No union types matched"); + return vm.throw_completion<JS::TypeError>("No union types matched"sv); }; Optional<Variant<Vector<Vector<DeprecatedString>>, OrderedHashMap<DeprecatedString, DeprecatedString>>> headers_value; if (!headers_property_value.is_nullish()) diff --git a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp index ea2b368ac3..d6fd5a3e42 100644 --- a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp +++ b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp @@ -68,7 +68,7 @@ JS::VM& main_thread_vm() vm->host_ensure_can_add_private_element = [](JS::Object const& object) -> JS::ThrowCompletionOr<void> { // 1. If O is a WindowProxy object, or implements Location, then return Completion { [[Type]]: throw, [[Value]]: a new TypeError }. if (is<HTML::WindowProxy>(object) || is<HTML::Location>(object)) - return vm->throw_completion<JS::TypeError>("Cannot add private elements to window or location object"); + return vm->throw_completion<JS::TypeError>("Cannot add private elements to window or location object"sv); // 2. Return NormalCompletion(unused). return {}; diff --git a/Userland/Libraries/LibWeb/Fetch/Body.cpp b/Userland/Libraries/LibWeb/Fetch/Body.cpp index dff2ab6504..abe4dbc3b3 100644 --- a/Userland/Libraries/LibWeb/Fetch/Body.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Body.cpp @@ -148,7 +148,7 @@ JS::NonnullGCPtr<JS::Promise> consume_body(JS::Realm& realm, BodyMixin const& ob // 1. If object is unusable, then return a promise rejected with a TypeError. if (object.is_unusable()) { - auto promise_capability = WebIDL::create_rejected_promise(realm, JS::TypeError::create(realm, "Body is unusable"sv)); + auto promise_capability = WebIDL::create_rejected_promise(realm, JS::TypeError::create(realm, "Body is unusable"sv).release_allocated_value_but_fixme_should_propagate_errors()); return verify_cast<JS::Promise>(*promise_capability->promise().ptr()); } diff --git a/Userland/Libraries/LibWeb/Fetch/FetchMethod.cpp b/Userland/Libraries/LibWeb/Fetch/FetchMethod.cpp index 61e3bb0059..d42649cc07 100644 --- a/Userland/Libraries/LibWeb/Fetch/FetchMethod.cpp +++ b/Userland/Libraries/LibWeb/Fetch/FetchMethod.cpp @@ -100,7 +100,7 @@ JS::NonnullGCPtr<JS::Promise> fetch_impl(JS::VM& vm, RequestInfo const& input, R // 3. If response is a network error, then reject p with a TypeError and abort these steps. if (response->is_network_error()) { auto message = response->network_error_message().value_or("Response is a network error"sv); - WebIDL::reject_promise(vm, promise_capability, JS::TypeError::create(relevant_realm, message)); + WebIDL::reject_promise(vm, promise_capability, JS::TypeError::create(relevant_realm, message).release_allocated_value_but_fixme_should_propagate_errors()); return; } diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp index 15ce59f037..dee3e3f54d 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp @@ -52,7 +52,7 @@ JS::NonnullGCPtr<WebIDL::Promise> Body::fully_read_as_promise() const return WebIDL::create_resolved_promise(realm, JS::PrimitiveString::create(vm, move(result))); } // Empty, Blob, FormData - return WebIDL::create_rejected_promise(realm, JS::InternalError::create(realm, "Reading body isn't fully implemented"sv)); + return WebIDL::create_rejected_promise(realm, JS::InternalError::create(realm, "Reading body isn't fully implemented"sv).release_allocated_value_but_fixme_should_propagate_errors()); } // https://fetch.spec.whatwg.org/#byte-sequence-as-a-body diff --git a/Userland/Libraries/LibWeb/HTML/Location.cpp b/Userland/Libraries/LibWeb/HTML/Location.cpp index 727b3ee985..182b3067c7 100644 --- a/Userland/Libraries/LibWeb/HTML/Location.cpp +++ b/Userland/Libraries/LibWeb/HTML/Location.cpp @@ -89,7 +89,7 @@ JS::ThrowCompletionOr<void> Location::set_href(DeprecatedString const& new_href) // 2. Parse the given value relative to the entry settings object. If that failed, throw a TypeError exception. auto href_url = window.associated_document().parse_url(new_href); if (!href_url.is_valid()) - return vm.throw_completion<JS::URIError>(DeprecatedString::formatted("Invalid URL '{}'", new_href)); + return vm.throw_completion<JS::URIError>(TRY_OR_THROW_OOM(vm, String::formatted("Invalid URL '{}'", new_href))); // 3. Location-object navigate given the resulting URL record. window.did_set_location_href({}, href_url); diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp index 40c2452f22..0701593cc6 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp @@ -90,7 +90,7 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, JS::GCPtr<JS::En // 5. If script's error to rethrow is not null, then set evaluationStatus to Completion { [[Type]]: throw, [[Value]]: script's error to rethrow, [[Target]]: empty }. if (m_error_to_rethrow.has_value()) { - evaluation_status = vm.throw_completion<JS::SyntaxError>(m_error_to_rethrow.value().to_deprecated_string()); + evaluation_status = vm.throw_completion<JS::SyntaxError>(TRY_OR_THROW_OOM(vm, m_error_to_rethrow.value().to_string())); } else { auto timer = Core::ElapsedTimer::start_new(); diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp index db35df95eb..499377bc4c 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp @@ -44,7 +44,7 @@ void report_exception_to_console(JS::Value value, JS::Realm& realm, ErrorInPromi dbgln("\033[31;1mUnhandled JavaScript exception{}:\033[0m {}", error_in_promise == ErrorInPromise::Yes ? " (in promise)" : "", value); } - console.report_exception(*JS::Error::create(realm, value.to_string_without_side_effects().release_value_but_fixme_should_propagate_errors().to_deprecated_string()), error_in_promise == ErrorInPromise::Yes); + console.report_exception(*JS::Error::create(realm, value.to_string_without_side_effects().release_value_but_fixme_should_propagate_errors()), error_in_promise == ErrorInPromise::Yes); } // https://html.spec.whatwg.org/#report-the-exception diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 023bdaa8fc..4d8f297844 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -1725,7 +1725,7 @@ JS_DEFINE_NATIVE_FUNCTION(Window::scroll) if (!behavior_string_value.is_undefined()) behavior_string = TRY(behavior_string_value.to_deprecated_string(vm)); if (behavior_string != "smooth" && behavior_string != "auto") - return vm.throw_completion<JS::TypeError>("Behavior is not one of 'smooth' or 'auto'"); + return vm.throw_completion<JS::TypeError>("Behavior is not one of 'smooth' or 'auto'"sv); } else if (vm.argument_count() >= 2) { // We ignore arguments 2+ in line with behavior of Chrome and Firefox @@ -1788,7 +1788,7 @@ JS_DEFINE_NATIVE_FUNCTION(Window::scroll_by) auto behavior_string_value = TRY(options->get("behavior")); auto behavior_string = behavior_string_value.is_undefined() ? "auto" : TRY(behavior_string_value.to_deprecated_string(vm)); if (behavior_string != "smooth" && behavior_string != "auto") - return vm.throw_completion<JS::TypeError>("Behavior is not one of 'smooth' or 'auto'"); + return vm.throw_completion<JS::TypeError>("Behavior is not one of 'smooth' or 'auto'"sv); ScrollBehavior behavior = (behavior_string == "smooth") ? ScrollBehavior::Smooth : ScrollBehavior::Auto; // FIXME: Spec wants us to call scroll(options) here. diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryConstructor.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryConstructor.cpp index 21be84b718..bdf68ff714 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryConstructor.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryConstructor.cpp @@ -44,7 +44,7 @@ JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> WebAssemblyMemoryConstructor auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::MemoryType { Wasm::Limits { initial, maximum } }); if (!address.has_value()) - return vm.throw_completion<JS::TypeError>("Wasm Memory allocation failed"); + return vm.throw_completion<JS::TypeError>("Wasm Memory allocation failed"sv); return MUST_OR_THROW_OOM(vm.heap().allocate<WebAssemblyMemoryObject>(realm, realm, *address)); } diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryPrototype.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryPrototype.cpp index 41b24d3cc5..7dda6f568f 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryPrototype.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryPrototype.cpp @@ -34,7 +34,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow) auto previous_size = memory->size() / Wasm::Constants::page_size; if (!memory->grow(page_count * Wasm::Constants::page_size)) - return vm.throw_completion<JS::TypeError>("Memory.grow() grows past the stated limit of the memory instance"); + return vm.throw_completion<JS::TypeError>("Memory.grow() grows past the stated limit of the memory instance"sv); return JS::Value(static_cast<u32>(previous_size)); } diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp index 7d6ad22390..6073622e92 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp @@ -17,6 +17,7 @@ #include <LibJS/Runtime/ArrayBuffer.h> #include <LibJS/Runtime/BigInt.h> #include <LibJS/Runtime/DataView.h> +#include <LibJS/Runtime/ThrowableStringBuilder.h> #include <LibJS/Runtime/TypedArray.h> #include <LibWasm/AbstractMachine/Interpreter.h> #include <LibWasm/AbstractMachine/Validator.h> @@ -119,7 +120,7 @@ JS::ThrowCompletionOr<size_t> parse_module(JS::VM& vm, JS::Object* buffer_object auto& buffer = static_cast<JS::DataView&>(*buffer_object); data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), buffer.byte_length()); } else { - return vm.throw_completion<JS::TypeError>("Not a BufferSource"); + return vm.throw_completion<JS::TypeError>("Not a BufferSource"sv); } FixedMemoryStream stream { data }; auto module_result = Wasm::Module::parse(stream); @@ -233,11 +234,11 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(JS::VM& vm, if (import_.is_number() || import_.is_bigint()) { if (import_.is_number() && type.type().kind() == Wasm::ValueType::I64) { // FIXME: Throw a LinkError instead. - return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a Number to a BigInteger"); + return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a Number to a BigInteger"sv); } if (import_.is_bigint() && type.type().kind() != Wasm::ValueType::I64) { // FIXME: Throw a LinkError instead. - return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a BigInteger to a Number"); + return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a BigInteger to a Number"sv); } auto cast_value = TRY(to_webassembly_value(vm, import_, type.type())); address = s_abstract_machine.store().allocate({ type.type(), false }, cast_value); @@ -247,7 +248,7 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(JS::VM& vm, // let globaladdr be v.[[Global]] // FIXME: Throw a LinkError instead - return vm.throw_completion<JS::TypeError>("LinkError: Invalid value for global type"); + return vm.throw_completion<JS::TypeError>("LinkError: Invalid value for global type"sv); } resolved_imports.set(import_name, Wasm::ExternValue { *address }); @@ -256,7 +257,7 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(JS::VM& vm, [&](Wasm::MemoryType const&) -> JS::ThrowCompletionOr<void> { if (!import_.is_object() || !is<WebAssemblyMemoryObject>(import_.as_object())) { // FIXME: Throw a LinkError instead - return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Memory for a memory import"); + return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Memory for a memory import"sv); } auto address = static_cast<WebAssemblyMemoryObject const&>(import_.as_object()).address(); resolved_imports.set(import_name, Wasm::ExternValue { address }); @@ -265,7 +266,7 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(JS::VM& vm, [&](Wasm::TableType const&) -> JS::ThrowCompletionOr<void> { if (!import_.is_object() || !is<WebAssemblyTableObject>(import_.as_object())) { // FIXME: Throw a LinkError instead - return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Table for a table import"); + return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Table for a table import"sv); } auto address = static_cast<WebAssemblyTableObject const&>(import_.as_object()).address(); resolved_imports.set(import_name, Wasm::ExternValue { address }); @@ -274,7 +275,7 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(JS::VM& vm, [&](auto const&) -> JS::ThrowCompletionOr<void> { // FIXME: Implement these. dbgln("Unimplemented import of non-function attempted"); - return vm.throw_completion<JS::TypeError>("LinkError: Not Implemented"); + return vm.throw_completion<JS::TypeError>("LinkError: Not Implemented"sv); })); } } @@ -283,10 +284,10 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(JS::VM& vm, auto link_result = linker.finish(); if (link_result.is_error()) { // FIXME: Throw a LinkError. - StringBuilder builder; - builder.append("LinkError: Missing "sv); - builder.join(' ', link_result.error().missing_imports); - return vm.throw_completion<JS::TypeError>(builder.to_deprecated_string()); + JS::ThrowableStringBuilder builder(vm); + MUST_OR_THROW_OOM(builder.append("LinkError: Missing "sv)); + MUST_OR_THROW_OOM(builder.join(' ', link_result.error().missing_imports)); + return vm.throw_completion<JS::TypeError>(MUST_OR_THROW_OOM(builder.to_string())); } auto instance_result = s_abstract_machine.instantiate(module, link_result.release_value()); @@ -327,7 +328,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate) } else if (is<WebAssemblyModuleObject>(buffer)) { module = &static_cast<WebAssemblyModuleObject*>(buffer)->module(); } else { - auto error = JS::TypeError::create(realm, DeprecatedString::formatted("{} is not an ArrayBuffer or a Module", buffer->class_name())); + auto error = JS::TypeError::create(realm, TRY_OR_THROW_OOM(vm, String::formatted("{} is not an ArrayBuffer or a Module", buffer->class_name()))); promise->reject(error); return promise; } @@ -447,7 +448,7 @@ JS::NativeFunction* create_native_function(JS::VM& vm, Wasm::FunctionAddress add auto result = WebAssemblyObject::s_abstract_machine.invoke(address, move(values)); // FIXME: Use the convoluted mapping of errors defined in the spec. if (result.is_trap()) - return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("Wasm execution trapped (WIP): {}", result.trap().reason)); + return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("Wasm execution trapped (WIP): {}", result.trap().reason))); if (result.values().is_empty()) return JS::js_undefined(); diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp index faa157e564..af31ce34d2 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp @@ -57,7 +57,7 @@ JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> WebAssemblyTableConstructor: maximum = TRY(maximum_value.to_u32(vm)); if (maximum.has_value() && maximum.value() < initial) - return vm.throw_completion<JS::RangeError>("maximum should be larger than or equal to initial"); + return vm.throw_completion<JS::RangeError>("maximum should be larger than or equal to initial"sv); auto value_value = TRY(descriptor->get("value")); auto reference_value = TRY([&]() -> JS::ThrowCompletionOr<Wasm::Value> { @@ -71,7 +71,7 @@ JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> WebAssemblyTableConstructor: auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::TableType { *reference_type, Wasm::Limits { initial, maximum } }); if (!address.has_value()) - return vm.throw_completion<JS::TypeError>("Wasm Table allocation failed"); + return vm.throw_completion<JS::TypeError>("Wasm Table allocation failed"sv); auto& table = *WebAssemblyObject::s_abstract_machine.store().get(*address); for (auto& element : table.elements()) diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp index 3309a1f7f7..5301f60dcd 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp @@ -47,7 +47,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow) auto& reference = reference_value.value().get<Wasm::Reference>(); if (!table->grow(delta, reference)) - return vm.throw_completion<JS::RangeError>("Failed to grow table"); + return vm.throw_completion<JS::RangeError>("Failed to grow table"sv); return JS::Value(static_cast<u32>(initial_size)); } @@ -66,7 +66,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get) return JS::js_undefined(); if (table->elements().size() <= index) - return vm.throw_completion<JS::RangeError>("Table element index out of range"); + return vm.throw_completion<JS::RangeError>("Table element index out of range"sv); auto& ref = table->elements()[index]; if (!ref.has_value()) @@ -90,7 +90,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set) return JS::js_undefined(); if (table->elements().size() <= index) - return vm.throw_completion<JS::RangeError>("Table element index out of range"); + return vm.throw_completion<JS::RangeError>("Table element index out of range"sv); auto value_value = vm.argument(1); auto reference_value = TRY([&]() -> JS::ThrowCompletionOr<Wasm::Value> { |