mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:02:44 +00:00 
			
		
		
		
	 76b9d06b19
			
		
	
	
		76b9d06b19
		
	
	
	
	
		
			
			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.
		
			
				
	
	
		
			45 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			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)
 | |
| {
 | |
|     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 {};
 | |
| }
 | |
| 
 | |
| }
 |