mirror of
https://github.com/RGBCube/serenity
synced 2025-06-29 01:22:11 +00:00
LibJS: Add and begin using a completion-compatible string builder
ThrowableStringBuilder is a thin wrapper around StringBuilder to map results from the try_* methods to a throw completion. This will let us try to throw on OOM conditions rather than just blowing up.
This commit is contained in:
parent
fab8ef3dfc
commit
76b9d06b19
4 changed files with 107 additions and 30 deletions
45
Userland/Libraries/LibJS/Runtime/ThrowableStringBuilder.cpp
Normal file
45
Userland/Libraries/LibJS/Runtime/ThrowableStringBuilder.cpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Utf16View.h>
|
||||
#include <LibJS/Runtime/ThrowableStringBuilder.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
ThrowableStringBuilder::ThrowableStringBuilder(VM& vm)
|
||||
: m_vm(vm)
|
||||
{
|
||||
}
|
||||
|
||||
ThrowCompletionOr<void> ThrowableStringBuilder::append(char ch)
|
||||
{
|
||||
if (try_append(ch).is_error())
|
||||
return m_vm.throw_completion<InternalError>(ErrorType::NotEnoughMemoryToAllocate, length() + 1);
|
||||
return {};
|
||||
}
|
||||
|
||||
ThrowCompletionOr<void> ThrowableStringBuilder::append(StringView string)
|
||||
{
|
||||
if (try_append(string).is_error())
|
||||
return m_vm.throw_completion<InternalError>(ErrorType::NotEnoughMemoryToAllocate, length() + string.length());
|
||||
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));
|
||||
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));
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue