ladybird/Userland/Libraries/LibJS/Runtime/ThrowableStringBuilder.cpp
Timothy Flynn 4d10911f96 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.
2023-02-17 09:14:23 -05:00

46 lines
991 B
C++

/*
* 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)
{
TRY_OR_THROW_OOM(m_vm, try_append(ch));
return {};
}
ThrowCompletionOr<void> ThrowableStringBuilder::append(StringView string)
{
TRY_OR_THROW_OOM(m_vm, try_append(string));
return {};
}
ThrowCompletionOr<void> ThrowableStringBuilder::append(Utf16View const& string)
{
TRY_OR_THROW_OOM(m_vm, try_append(string));
return {};
}
ThrowCompletionOr<void> ThrowableStringBuilder::append_code_point(u32 value)
{
TRY_OR_THROW_OOM(m_vm, try_append_code_point(value));
return {};
}
ThrowCompletionOr<String> ThrowableStringBuilder::to_string() const
{
return TRY_OR_THROW_OOM(m_vm, StringBuilder::to_string());
}
}