ladybird/Libraries/LibJS/Runtime/Uint8Array.h
Andreas Kling 7025dd1fa7 Libraries: Parse JS strings from UTF-16
Thread UTF-16 string input through JSON, script parsing, Date parsing,
Intl option parsing, Temporal parsing, and the helper library boundaries
that feed those parsers. Preserve ASCII fast paths where the source data
is known to be ASCII.
2026-06-22 19:51:25 +02:00

59 lines
1.6 KiB
C++

/*
* Copyright (c) 2024-2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Base64.h>
#include <AK/ByteBuffer.h>
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <AK/Utf16View.h>
#include <LibGC/Ptr.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
class Uint8ArrayConstructorHelpers {
public:
static void initialize(Realm&, Object& constructor);
private:
JS_DECLARE_NATIVE_FUNCTION(from_base64);
JS_DECLARE_NATIVE_FUNCTION(from_hex);
};
class Uint8ArrayPrototypeHelpers {
public:
static void initialize(Realm&, Object& prototype);
private:
JS_DECLARE_NATIVE_FUNCTION(set_from_base64);
JS_DECLARE_NATIVE_FUNCTION(set_from_hex);
JS_DECLARE_NATIVE_FUNCTION(to_base64);
JS_DECLARE_NATIVE_FUNCTION(to_hex);
};
enum class Alphabet {
Base64,
Base64URL,
};
struct DecodeResult {
size_t read { 0 }; // [[Read]]
ByteBuffer bytes; // [[Bytes]]
Optional<Completion> error; // [[Error]]
};
ThrowCompletionOr<GC::Ref<TypedArrayBase>> validate_uint8_array(VM&);
ThrowCompletionOr<ByteBuffer> get_uint8_array_bytes(VM&, TypedArrayBase const&);
ThrowCompletionOr<ReadonlyBytes> get_uint8_array_bytes_view(VM&, TypedArrayBase const&);
void set_uint8_array_bytes(TypedArrayBase&, ReadonlyBytes);
DecodeResult from_base64(VM&, Utf16View string, Alphabet alphabet, AK::LastChunkHandling last_chunk_handling, Optional<size_t> max_length = {});
DecodeResult from_hex(VM&, Utf16View string, Optional<size_t> max_length = {});
}