mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:18:13 +00:00
LibJS: Pre-allocate the out-of-memory error string on the VM
If we are out of memory, we can't try to allocate a string that could fail as well. When Error is converted to String, this would result in an endless OOM-throwing loop. Instead, pre-allocate the string on the VM, and use it to construct the Error. Note that as of this commit, the OOM string is still a DeprecatedString. This is just preporatory for Error's conversion to String.
This commit is contained in:
parent
93ad25fbe5
commit
4d10911f96
5 changed files with 42 additions and 31 deletions
|
@ -16,39 +16,31 @@ ThrowableStringBuilder::ThrowableStringBuilder(VM& vm)
|
|||
|
||||
ThrowCompletionOr<void> ThrowableStringBuilder::append(char ch)
|
||||
{
|
||||
if (try_append(ch).is_error())
|
||||
return m_vm.throw_completion<InternalError>(ErrorType::NotEnoughMemoryToAllocate, length() + 1);
|
||||
TRY_OR_THROW_OOM(m_vm, try_append(ch));
|
||||
return {};
|
||||
}
|
||||
|
||||
ThrowCompletionOr<void> ThrowableStringBuilder::append(StringView string)
|
||||
{
|
||||
if (try_append(string).is_error())
|
||||
return m_vm.throw_completion<InternalError>(ErrorType::NotEnoughMemoryToAllocate, length() + string.length());
|
||||
TRY_OR_THROW_OOM(m_vm, try_append(string));
|
||||
return {};
|
||||
}
|
||||
|
||||
ThrowCompletionOr<void> ThrowableStringBuilder::append(Utf16View const& string)
|
||||
{
|
||||
if (try_append(string).is_error())
|
||||
return m_vm.throw_completion<InternalError>(ErrorType::NotEnoughMemoryToAllocate, length() + (string.length_in_code_units() * 2));
|
||||
TRY_OR_THROW_OOM(m_vm, try_append(string));
|
||||
return {};
|
||||
}
|
||||
|
||||
ThrowCompletionOr<void> ThrowableStringBuilder::append_code_point(u32 value)
|
||||
{
|
||||
if (auto result = try_append_code_point(value); result.is_error())
|
||||
return m_vm.throw_completion<InternalError>(ErrorType::NotEnoughMemoryToAllocate, length() + sizeof(value));
|
||||
TRY_OR_THROW_OOM(m_vm, try_append_code_point(value));
|
||||
return {};
|
||||
}
|
||||
|
||||
ThrowCompletionOr<String> ThrowableStringBuilder::to_string() const
|
||||
{
|
||||
auto result = StringBuilder::to_string();
|
||||
if (result.is_error())
|
||||
return m_vm.throw_completion<InternalError>(ErrorType::NotEnoughMemoryToAllocate, length());
|
||||
|
||||
return result.release_value();
|
||||
return TRY_OR_THROW_OOM(m_vm, StringBuilder::to_string());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue