ladybird/Userland/Libraries/LibJS/Runtime/ThrowableStringBuilder.h
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

45 lines
1.2 KiB
C++

/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Format.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/ErrorTypes.h>
#include <LibJS/Runtime/VM.h>
namespace JS {
class ThrowableStringBuilder : private AK::StringBuilder {
public:
explicit ThrowableStringBuilder(VM&);
ThrowCompletionOr<void> append(char);
ThrowCompletionOr<void> append(StringView);
ThrowCompletionOr<void> append(Utf16View const&);
ThrowCompletionOr<void> append_code_point(u32 value);
ThrowCompletionOr<String> to_string() const;
template<typename... Parameters>
ThrowCompletionOr<void> appendff(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
{
AK::VariadicFormatParams<AK::AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... };
TRY_OR_THROW_OOM(m_vm, vformat(*this, fmtstr.view(), variadic_format_params));
return {};
}
using AK::StringBuilder::is_empty;
using AK::StringBuilder::string_view;
using AK::StringBuilder::trim;
private:
VM& m_vm;
};
}