Skip to content

Commit 11b40db

Browse files
committed
LibWeb/WebIDL: Store SimpleException message as a String{,View} variant
1 parent 1032ac2 commit 11b40db

File tree

8 files changed

+28
-24
lines changed

8 files changed

+28
-24
lines changed

Userland/Libraries/LibWeb/Bindings/ExceptionOrUtils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2022, the SerenityOS developers.
2+
* Copyright (c) 2021-2023, the SerenityOS developers.
33
*
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
@@ -60,10 +60,11 @@ ALWAYS_INLINE JS::Completion dom_exception_to_throw_completion(JS::VM& vm, auto&
6060
{
6161
return exception.visit(
6262
[&](WebIDL::SimpleException const& exception) {
63+
auto message = exception.message.visit([](auto const& s) -> StringView { return s; });
6364
switch (exception.type) {
6465
#define E(x) \
6566
case WebIDL::SimpleExceptionType::x: \
66-
return vm.template throw_completion<JS::x>(exception.message);
67+
return vm.template throw_completion<JS::x>(message);
6768

6869
ENUMERATE_SIMPLE_WEBIDL_EXCEPTION_TYPES(E)
6970

Userland/Libraries/LibWeb/DOM/DOMTokenList.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,7 @@ WebIDL::ExceptionOr<bool> DOMTokenList::supports([[maybe_unused]] StringView tok
216216
// FIXME: Implement this fully when any use case defines supported tokens.
217217

218218
// 1. If the associated attribute’s local name does not define supported tokens, throw a TypeError.
219-
return WebIDL::SimpleException {
220-
WebIDL::SimpleExceptionType::TypeError,
221-
DeprecatedString::formatted("Attribute {} does not define any supported tokens", m_associated_attribute)
222-
};
219+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Attribute {} does not define any supported tokens", m_associated_attribute).release_value_but_fixme_should_propagate_errors() };
223220

224221
// 2. Let lowercase token be a copy of token, in ASCII lowercase.
225222
// 3. If lowercase token is present in supported tokens, return true.

Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ namespace Web::Encoding {
1414

1515
WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, DeprecatedFlyString encoding)
1616
{
17+
auto& vm = realm.vm();
18+
1719
auto decoder = TextCodec::decoder_for(encoding);
1820
if (!decoder.has_value())
19-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Invalid encoding {}", encoding) };
21+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY_OR_THROW_OOM(vm, String::formatted("Invalid encoding {}", encoding)) };
2022

2123
return MUST_OR_THROW_OOM(realm.heap().allocate<TextDecoder>(realm, realm, *decoder, move(encoding), false, false));
2224
}

Userland/Libraries/LibWeb/Geometry/DOMMatrix.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ namespace Web::Geometry {
1212

1313
WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::construct_impl(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
1414
{
15+
auto& vm = realm.vm();
16+
1517
// https://drafts.fxtf.org/geometry/#dom-dommatrix-dommatrix
1618
if (init.has_value()) {
1719
// -> Otherwise
1820
// Throw a TypeError exception.
1921
// The only condition where this can be met is with a sequence type which doesn't have exactly 6 or 16 elements.
2022
if (auto* double_sequence = init.value().get_pointer<Vector<double>>(); double_sequence && (double_sequence->size() != 6 && double_sequence->size() != 16))
21-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Sequence must contain exactly 6 or 16 elements, got {} element(s)", double_sequence->size()) };
23+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY_OR_THROW_OOM(vm, String::formatted("Sequence must contain exactly 6 or 16 elements, got {} element(s)", double_sequence->size())) };
2224
}
2325

2426
return realm.heap().allocate<DOMMatrix>(realm, realm, init).release_allocated_value_but_fixme_should_propagate_errors();

Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ namespace Web::Geometry {
1313

1414
WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::construct_impl(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
1515
{
16+
auto& vm = realm.vm();
17+
1618
// https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-dommatrixreadonly
1719
if (init.has_value()) {
1820
// -> Otherwise
1921
// Throw a TypeError exception.
2022
// The only condition where this can be met is with a sequence type which doesn't have exactly 6 or 16 elements.
2123
if (auto* double_sequence = init.value().get_pointer<Vector<double>>(); double_sequence && (double_sequence->size() != 6 && double_sequence->size() != 16))
22-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Sequence must contain exactly 6 or 16 elements, got {} element(s)", double_sequence->size()) };
24+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY_OR_THROW_OOM(vm, String::formatted("Sequence must contain exactly 6 or 16 elements, got {} element(s)", double_sequence->size())) };
2325
}
2426

2527
return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, init).release_allocated_value_but_fixme_should_propagate_errors();
@@ -394,27 +396,27 @@ WebIDL::ExceptionOr<void> validate_and_fixup_dom_matrix_2d_init(DOMMatrix2DInit&
394396
// 1. If at least one of the following conditions are true for dict, then throw a TypeError exception and abort these steps.
395397
// - a and m11 are both present and SameValueZero(a, m11) is false.
396398
if (init.a.has_value() && init.m11.has_value() && !JS::same_value_zero(JS::Value(init.a.value()), JS::Value(init.m11.value())))
397-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.a and DOMMatrix2DInit.m11 must have the same value if they are both present" };
399+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.a and DOMMatrix2DInit.m11 must have the same value if they are both present"sv };
398400

399401
// - b and m12 are both present and SameValueZero(b, m12) is false.
400402
if (init.b.has_value() && init.m12.has_value() && !JS::same_value_zero(JS::Value(init.b.value()), JS::Value(init.m12.value())))
401-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.b and DOMMatrix2DInit.m12 must have the same value if they are both present" };
403+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.b and DOMMatrix2DInit.m12 must have the same value if they are both present"sv };
402404

403405
// - c and m21 are both present and SameValueZero(c, m21) is false.
404406
if (init.c.has_value() && init.m21.has_value() && !JS::same_value_zero(JS::Value(init.c.value()), JS::Value(init.m21.value())))
405-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.c and DOMMatrix2DInit.m21 must have the same value if they are both present" };
407+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.c and DOMMatrix2DInit.m21 must have the same value if they are both present"sv };
406408

407409
// - d and m22 are both present and SameValueZero(d, m22) is false.
408410
if (init.d.has_value() && init.m22.has_value() && !JS::same_value_zero(JS::Value(init.d.value()), JS::Value(init.m22.value())))
409-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.d and DOMMatrix2DInit.m22 must have the same value if they are both present" };
411+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.d and DOMMatrix2DInit.m22 must have the same value if they are both present"sv };
410412

411413
// - e and m41 are both present and SameValueZero(e, m41) is false.
412414
if (init.e.has_value() && init.m41.has_value() && !JS::same_value_zero(JS::Value(init.e.value()), JS::Value(init.m41.value())))
413-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.e and DOMMatrix2DInit.m41 must have the same value if they are both present" };
415+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.e and DOMMatrix2DInit.m41 must have the same value if they are both present"sv };
414416

415417
// - f and m42 are both present and SameValueZero(f, m42) is false.
416418
if (init.f.has_value() && init.m42.has_value() && !JS::same_value_zero(JS::Value(init.f.value()), JS::Value(init.m42.value())))
417-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.f and DOMMatrix2DInit.m42 must have the same value if they are both present" };
419+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.f and DOMMatrix2DInit.m42 must have the same value if they are both present"sv };
418420

419421
// 2. If m11 is not present then set it to the value of member a, or value 1 if a is also not present.
420422
if (!init.m11.has_value())
@@ -462,7 +464,7 @@ WebIDL::ExceptionOr<void> validate_and_fixup_dom_matrix_init(DOMMatrixInit& init
462464
|| (init.m43 != 0.0 && init.m43 != -0.0)
463465
|| init.m33 != 1.0
464466
|| init.m44 != 1.0) {
465-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrixInit.is2D is true, but the given matrix is not a 2D matrix" };
467+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrixInit.is2D is true, but the given matrix is not a 2D matrix"sv };
466468
}
467469
}
468470

Userland/Libraries/LibWeb/HTML/Scripting/Fetching.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ WebIDL::ExceptionOr<AK::URL> resolve_module_specifier(Optional<Script&> referrin
113113
return as_url.release_value();
114114

115115
// 13. Throw a TypeError indicating that specifier was a bare specifier, but was not remapped to anything by importMap.
116-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Failed to resolve non relative module specifier '{}' from an import map.", specifier) };
116+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Failed to resolve non relative module specifier '{}' from an import map.", specifier).release_value_but_fixme_should_propagate_errors() };
117117
}
118118

119119
// https://html.spec.whatwg.org/multipage/webappapis.html#resolving-an-imports-match
@@ -125,7 +125,7 @@ WebIDL::ExceptionOr<Optional<AK::URL>> resolve_imports_match(DeprecatedString co
125125
if (specifier_key == normalized_specifier) {
126126
// 1. If resolutionResult is null, then throw a TypeError indicating that resolution of specifierKey was blocked by a null entry.
127127
if (!resolution_result.has_value())
128-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Import resolution of '{}' was blocked by a null entry.", specifier_key) };
128+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Import resolution of '{}' was blocked by a null entry.", specifier_key).release_value_but_fixme_should_propagate_errors() };
129129

130130
// 2. Assert: resolutionResult is a URL.
131131
VERIFY(resolution_result->is_valid());
@@ -146,7 +146,7 @@ WebIDL::ExceptionOr<Optional<AK::URL>> resolve_imports_match(DeprecatedString co
146146
) {
147147
// 1. If resolutionResult is null, then throw a TypeError indicating that the resolution of specifierKey was blocked by a null entry.
148148
if (!resolution_result.has_value())
149-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Import resolution of '{}' was blocked by a null entry.", specifier_key) };
149+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Import resolution of '{}' was blocked by a null entry.", specifier_key).release_value_but_fixme_should_propagate_errors() };
150150

151151
// 2. Assert: resolutionResult is a URL.
152152
VERIFY(resolution_result->is_valid());
@@ -164,15 +164,15 @@ WebIDL::ExceptionOr<Optional<AK::URL>> resolve_imports_match(DeprecatedString co
164164
// 6. If url is failure, then throw a TypeError indicating that resolution of normalizedSpecifier was blocked since the afterPrefix portion
165165
// could not be URL-parsed relative to the resolutionResult mapped to by the specifierKey prefix.
166166
if (!url.is_valid())
167-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Could not resolve '{}' as the after prefix portion could not be URL-parsed.", normalized_specifier) };
167+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Could not resolve '{}' as the after prefix portion could not be URL-parsed.", normalized_specifier).release_value_but_fixme_should_propagate_errors() };
168168

169169
// 7. Assert: url is a URL.
170170
VERIFY(url.is_valid());
171171

172172
// 8. If the serialization of resolutionResult is not a code unit prefix of the serialization of url, then throw a TypeError indicating
173173
// that the resolution of normalizedSpecifier was blocked due to it backtracking above its prefix specifierKey.
174174
if (!Infra::is_code_unit_prefix(resolution_result->serialize(), url.serialize()))
175-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Could not resolve '{}' as it backtracks above its prefix specifierKey.", normalized_specifier) };
175+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Could not resolve '{}' as it backtracks above its prefix specifierKey.", normalized_specifier).release_value_but_fixme_should_propagate_errors() };
176176

177177
// 9. Return url.
178178
return url;

Userland/Libraries/LibWeb/URL/URLSearchParams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<URLSearchParams>> URLSearchParams::construc
118118
for (auto const& pair : init_sequence) {
119119
// a. If pair does not contain exactly two items, then throw a TypeError.
120120
if (pair.size() != 2)
121-
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Expected only 2 items in pair, got {}", pair.size()) };
121+
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY_OR_THROW_OOM(vm, String::formatted("Expected only 2 items in pair, got {}", pair.size())) };
122122

123123
// b. Append a new name-value pair whose name is pair’s first item, and value is pair’s second item, to query’s list.
124124
list.append(QueryParam { .name = pair[0], .value = pair[1] });

Userland/Libraries/LibWeb/WebIDL/ExceptionOr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
2+
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
33
*
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
@@ -29,7 +29,7 @@ enum class SimpleExceptionType {
2929

3030
struct SimpleException {
3131
SimpleExceptionType type;
32-
DeprecatedString message;
32+
Variant<String, StringView> message;
3333
};
3434

3535
template<typename ValueType>

0 commit comments

Comments
 (0)