1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:27:35 +00:00

LibJS+Everywhere: Make PrimitiveString and Utf16String fallible

This makes construction of Utf16String fallible in OOM conditions. The
immediate impact is that PrimitiveString must then be fallible as well,
as it may either transcode UTF-8 to UTF-16, or create a UTF-16 string
from ropes.

There are a couple of places where it is very non-trivial to propagate
the error further. A FIXME has been added to those locations.
This commit is contained in:
Timothy Flynn 2023-01-07 12:24:05 -05:00 committed by Linus Groh
parent d793262beb
commit 115baa7e32
57 changed files with 306 additions and 295 deletions

View file

@ -68,7 +68,7 @@ ThrowCompletionOr<void> set_legacy_regexp_static_property(VM& vm, RegExpConstruc
}
// UpdateLegacyRegExpStaticProperties ( C, S, startIndex, endIndex, capturedValues ), https://github.com/tc39/proposal-regexp-legacy-features#updatelegacyregexpstaticproperties--c-s-startindex-endindex-capturedvalues-
void update_legacy_regexp_static_properties(RegExpConstructor& constructor, Utf16String const& string, size_t start_index, size_t end_index, Vector<Utf16String> const& captured_values)
ThrowCompletionOr<void> update_legacy_regexp_static_properties(VM& vm, RegExpConstructor& constructor, Utf16String const& string, size_t start_index, size_t end_index, Vector<Utf16String> const& captured_values)
{
auto& legacy_static_properties = constructor.legacy_static_properties();
@ -92,7 +92,7 @@ void update_legacy_regexp_static_properties(RegExpConstructor& constructor, Utf1
// 8. Set the value of Cs [[RegExpLastMatch]] internal slot to a String whose length is endIndex - startIndex and containing the code units from S with indices startIndex through endIndex - 1, in ascending order.
auto last_match = string.view().substring_view(start_index, end_index - start_index);
legacy_static_properties.set_last_match(Utf16String(last_match));
legacy_static_properties.set_last_match(TRY(Utf16String::create(vm, last_match)));
// 9. If n > 0, set the value of Cs [[RegExpLastParen]] internal slot to the last element of capturedValues.
if (group_count > 0) {
@ -101,49 +101,45 @@ void update_legacy_regexp_static_properties(RegExpConstructor& constructor, Utf1
}
// 10. Else, set the value of Cs [[RegExpLastParen]] internal slot to the empty String.
else {
legacy_static_properties.set_last_paren(Utf16String(""sv));
legacy_static_properties.set_last_paren(TRY(Utf16String::create(vm)));
}
// 11. Set the value of Cs [[RegExpLeftContext]] internal slot to a String whose length is startIndex and containing the code units from S with indices 0 through startIndex - 1, in ascending order.
auto left_context = string.view().substring_view(0, start_index);
legacy_static_properties.set_left_context(Utf16String(left_context));
legacy_static_properties.set_left_context(TRY(Utf16String::create(vm, left_context)));
// 12. Set the value of Cs [[RegExpRightContext]] internal slot to a String whose length is len - endIndex and containing the code units from S with indices endIndex through len - 1, in ascending order.
auto right_context = string.view().substring_view(end_index, len - end_index);
legacy_static_properties.set_right_context(Utf16String(right_context));
legacy_static_properties.set_right_context(TRY(Utf16String::create(vm, right_context)));
// 13. For each integer i such that 1 ≤ i ≤ 9
for (size_t i = 1; i <= 9; i++) {
auto value = Utf16String(""sv);
// If i ≤ n, set the value of Cs [[RegExpPareni]] internal slot to the ith element of capturedValues.
if (i <= group_count) {
value = captured_values[i - 1];
}
// Else, set the value of Cs [[RegExpPareni]] internal slot to the empty String.
else {
// It's already an empty string
}
// i. If i ≤ n, set the value of Cs [[RegExpPareni]] internal slot to the ith element of capturedValues.
// ii. Else, set the value of Cs [[RegExpPareni]] internal slot to the empty String.
auto value = (i <= group_count) ? captured_values[i - 1] : TRY(Utf16String::create(vm));
if (i == 1) {
legacy_static_properties.set_$1(Utf16String(value));
legacy_static_properties.set_$1(move(value));
} else if (i == 2) {
legacy_static_properties.set_$2(Utf16String(value));
legacy_static_properties.set_$2(move(value));
} else if (i == 3) {
legacy_static_properties.set_$3(Utf16String(value));
legacy_static_properties.set_$3(move(value));
} else if (i == 4) {
legacy_static_properties.set_$4(Utf16String(value));
legacy_static_properties.set_$4(move(value));
} else if (i == 5) {
legacy_static_properties.set_$5(Utf16String(value));
legacy_static_properties.set_$5(move(value));
} else if (i == 6) {
legacy_static_properties.set_$6(Utf16String(value));
legacy_static_properties.set_$6(move(value));
} else if (i == 7) {
legacy_static_properties.set_$7(Utf16String(value));
legacy_static_properties.set_$7(move(value));
} else if (i == 8) {
legacy_static_properties.set_$8(Utf16String(value));
legacy_static_properties.set_$8(move(value));
} else if (i == 9) {
legacy_static_properties.set_$9(Utf16String(value));
legacy_static_properties.set_$9(move(value));
}
}
return {};
}
// InvalidateLegacyRegExpStaticProperties ( C ), https://github.com/tc39/proposal-regexp-legacy-features#invalidatelegacyregexpstaticproperties--c