Uint8Array.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Optional.h>
  9. #include <AK/StringView.h>
  10. #include <LibGC/Ptr.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/Runtime/Completion.h>
  13. #include <LibJS/Runtime/Value.h>
  14. namespace JS {
  15. class Uint8ArrayConstructorHelpers {
  16. public:
  17. static void initialize(Realm&, Object& constructor);
  18. private:
  19. JS_DECLARE_NATIVE_FUNCTION(from_base64);
  20. JS_DECLARE_NATIVE_FUNCTION(from_hex);
  21. };
  22. class Uint8ArrayPrototypeHelpers {
  23. public:
  24. static void initialize(Realm&, Object& prototype);
  25. private:
  26. JS_DECLARE_NATIVE_FUNCTION(to_base64);
  27. JS_DECLARE_NATIVE_FUNCTION(to_hex);
  28. JS_DECLARE_NATIVE_FUNCTION(set_from_base64);
  29. JS_DECLARE_NATIVE_FUNCTION(set_from_hex);
  30. };
  31. enum class Alphabet {
  32. Base64,
  33. Base64URL,
  34. };
  35. enum class LastChunkHandling {
  36. Loose,
  37. Strict,
  38. StopBeforePartial,
  39. };
  40. struct DecodeResult {
  41. size_t read { 0 }; // [[Read]]
  42. ByteBuffer bytes; // [[Bytes]]
  43. Optional<Completion> error; // [[Error]]
  44. };
  45. ThrowCompletionOr<GC::Ref<TypedArrayBase>> validate_uint8_array(VM&);
  46. ThrowCompletionOr<ByteBuffer> get_uint8_array_bytes(VM&, TypedArrayBase const&);
  47. void set_uint8_array_bytes(TypedArrayBase&, ReadonlyBytes);
  48. DecodeResult from_base64(VM&, StringView string, Alphabet alphabet, LastChunkHandling last_chunk_handling, Optional<size_t> max_length = {});
  49. DecodeResult from_hex(VM&, StringView string, Optional<size_t> max_length = {});
  50. }