Memory.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. JS_DECLARE_ALLOCATOR(Memory);
  23. public:
  24. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Memory>> construct_impl(JS::Realm&, MemoryDescriptor& descriptor);
  25. WebIDL::ExceptionOr<u32> grow(u32 delta);
  26. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> buffer() const;
  27. Wasm::MemoryAddress address() const { return m_address; }
  28. private:
  29. Memory(JS::Realm&, Wasm::MemoryAddress);
  30. virtual void initialize(JS::Realm&) override;
  31. WebIDL::ExceptionOr<void> reset_the_memory_buffer();
  32. static WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> create_a_memory_buffer(JS::VM&, JS::Realm&, Wasm::MemoryAddress);
  33. Wasm::MemoryAddress m_address;
  34. mutable JS::GCPtr<JS::ArrayBuffer> m_buffer;
  35. };
  36. }