Memory.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Optional.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibJS/Heap/GCPtr.h>
  11. #include <LibJS/Runtime/ArrayBuffer.h>
  12. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  13. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  14. #include <LibWeb/Bindings/PlatformObject.h>
  15. namespace Web::WebAssembly {
  16. struct MemoryDescriptor {
  17. u32 initial { 0 };
  18. Optional<u32> maximum;
  19. };
  20. class Memory : public Bindings::PlatformObject {
  21. WEB_PLATFORM_OBJECT(Memory, Bindings::PlatformObject);
  22. public:
  23. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Memory>> construct_impl(JS::Realm&, MemoryDescriptor& descriptor);
  24. WebIDL::ExceptionOr<u32> grow(u32 delta);
  25. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> buffer() const;
  26. Wasm::MemoryAddress address() const { return m_address; }
  27. private:
  28. Memory(JS::Realm&, Wasm::MemoryAddress);
  29. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  30. WebIDL::ExceptionOr<void> reset_the_memory_buffer();
  31. static WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> create_a_memory_buffer(JS::VM&, JS::Realm&, Wasm::MemoryAddress);
  32. Wasm::MemoryAddress m_address;
  33. mutable JS::GCPtr<JS::ArrayBuffer> m_buffer;
  34. };
  35. }