1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-02 23:12:08 +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

@ -30,7 +30,9 @@ void StringObject::initialize(Realm& realm)
{
auto& vm = this->vm();
Object::initialize(realm);
define_direct_property(vm.names.length, Value(m_string.utf16_string_view().length_in_code_units()), 0);
// FIXME: Propagate this error.
define_direct_property(vm.names.length, Value(MUST(m_string.utf16_string_view()).length_in_code_units()), 0);
}
void StringObject::visit_edges(Cell::Visitor& visitor)
@ -40,7 +42,7 @@ void StringObject::visit_edges(Cell::Visitor& visitor)
}
// 10.4.3.5 StringGetOwnProperty ( S, P ), https://tc39.es/ecma262/#sec-stringgetownproperty
static Optional<PropertyDescriptor> string_get_own_property(StringObject const& string, PropertyKey const& property_key)
static ThrowCompletionOr<Optional<PropertyDescriptor>> string_get_own_property(StringObject const& string, PropertyKey const& property_key)
{
VERIFY(property_key.is_valid());
@ -48,7 +50,7 @@ static Optional<PropertyDescriptor> string_get_own_property(StringObject const&
// NOTE: The spec only uses string and symbol keys, and later coerces to numbers -
// this is not the case for PropertyKey, so '!property_key.is_string()' would be wrong.
if (property_key.is_symbol())
return {};
return Optional<PropertyDescriptor> {};
// 2. Let index be CanonicalNumericIndexString(P).
auto index = canonical_numeric_index_string(property_key, CanonicalIndexMode::IgnoreNumericRoundtrip);
@ -57,21 +59,21 @@ static Optional<PropertyDescriptor> string_get_own_property(StringObject const&
// 4. If IsIntegralNumber(index) is false, return undefined.
// 5. If index is -0𝔽, return undefined.
if (!index.is_index())
return {};
return Optional<PropertyDescriptor> {};
// 6. Let str be S.[[StringData]].
// 7. Assert: Type(str) is String.
auto str = string.primitive_string().utf16_string_view();
auto str = TRY(string.primitive_string().utf16_string_view());
// 8. Let len be the length of str.
auto length = str.length_in_code_units();
// 9. If (index) < 0 or len ≤ (index), return undefined.
if (length <= index.as_index())
return {};
return Optional<PropertyDescriptor> {};
// 10. Let resultStr be the String value of length 1, containing one code unit from str, specifically the code unit at index (index).
auto result_str = PrimitiveString::create(string.vm(), str.substring_view(index.as_index(), 1));
auto result_str = PrimitiveString::create(string.vm(), TRY(Utf16String::create(string.vm(), str.substring_view(index.as_index(), 1))));
// 11. Return the PropertyDescriptor { [[Value]]: resultStr, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false }.
return PropertyDescriptor {
@ -104,7 +106,7 @@ ThrowCompletionOr<bool> StringObject::internal_define_own_property(PropertyKey c
VERIFY(property_key.is_valid());
// 1. Let stringDesc be StringGetOwnProperty(S, P).
auto string_descriptor = string_get_own_property(*this, property_key);
auto string_descriptor = TRY(string_get_own_property(*this, property_key));
// 2. If stringDesc is not undefined, then
if (string_descriptor.has_value()) {
@ -128,7 +130,7 @@ ThrowCompletionOr<MarkedVector<Value>> StringObject::internal_own_property_keys(
auto keys = MarkedVector<Value> { heap() };
// 2. Let str be O.[[StringData]].
auto str = m_string.utf16_string_view();
auto str = TRY(m_string.utf16_string_view());
// 3. Assert: Type(str) is String.