1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 09:34:57 +09:00

test-js: Add a helper method to convert a string to a UTF-8 byte array

In pure JS, we would have to implement a full UTF-8 encoder, so let's
add a helper method to do the work in C++.
This commit is contained in:
Timothy Flynn 2024-07-15 12:28:23 -04:00 committed by Andreas Kling
parent d265575269
commit dfad1a7329
Notes: github-actions[bot] 2024-09-03 15:47:10 +00:00

View file

@ -5,8 +5,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Enumerate.h>
#include <LibCore/Environment.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibTest/JavaScriptTestRunner.h>
#include <LibUnicode/TimeZone.h>
#include <stdlib.h>
@ -113,6 +115,19 @@ TESTJS_GLOBAL_FUNCTION(set_time_zone, setTimeZone)
return current_time_zone;
}
TESTJS_GLOBAL_FUNCTION(to_utf8_bytes, toUTF8Bytes)
{
auto& realm = *vm.current_realm();
auto string = TRY(vm.argument(0).to_string(vm));
auto typed_array = TRY(JS::Uint8Array::create(realm, string.bytes().size()));
for (auto [i, byte] : enumerate(string.bytes()))
typed_array->set_value_in_buffer(i, JS::Value { byte }, JS::ArrayBuffer::Order::SeqCst);
return typed_array;
}
TESTJS_RUN_FILE_FUNCTION(ByteString const& test_file, JS::Realm& realm, JS::ExecutionContext&)
{
if (!test262_parser_tests)