Memory.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <LibJS/Runtime/Realm.h>
  8. #include <LibJS/Runtime/VM.h>
  9. #include <LibWasm/Types.h>
  10. #include <LibWeb/Bindings/Intrinsics.h>
  11. #include <LibWeb/WebAssembly/Memory.h>
  12. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  13. namespace Web::WebAssembly {
  14. WebIDL::ExceptionOr<JS::NonnullGCPtr<Memory>> Memory::construct_impl(JS::Realm& realm, MemoryDescriptor& descriptor)
  15. {
  16. auto& vm = realm.vm();
  17. Wasm::Limits limits { descriptor.initial, move(descriptor.maximum) };
  18. Wasm::MemoryType memory_type { move(limits) };
  19. auto address = Bindings::WebAssemblyObject::s_abstract_machine.store().allocate(memory_type);
  20. if (!address.has_value())
  21. return vm.throw_completion<JS::TypeError>("Wasm Memory allocation failed"sv);
  22. return MUST_OR_THROW_OOM(vm.heap().allocate<Memory>(realm, realm, *address));
  23. }
  24. Memory::Memory(JS::Realm& realm, Wasm::MemoryAddress address)
  25. : Bindings::PlatformObject(realm)
  26. , m_address(address)
  27. {
  28. }
  29. JS::ThrowCompletionOr<void> Memory::initialize(JS::Realm& realm)
  30. {
  31. MUST_OR_THROW_OOM(Base::initialize(realm));
  32. set_prototype(&Bindings::ensure_web_prototype<Bindings::MemoryPrototype>(realm, "WebAssembly.Memory"sv));
  33. return {};
  34. }
  35. // https://webassembly.github.io/spec/js-api/#dom-memory-grow
  36. WebIDL::ExceptionOr<u32> Memory::grow(u32 delta)
  37. {
  38. auto& vm = this->vm();
  39. auto* memory = Bindings::WebAssemblyObject::s_abstract_machine.store().get(address());
  40. if (!memory)
  41. return vm.throw_completion<JS::RangeError>("Could not find the memory instance to grow"sv);
  42. auto previous_size = memory->size() / Wasm::Constants::page_size;
  43. if (!memory->grow(delta * Wasm::Constants::page_size))
  44. return vm.throw_completion<JS::RangeError>("Memory.grow() grows past the stated limit of the memory instance"sv);
  45. return previous_size;
  46. }
  47. // https://webassembly.github.io/spec/js-api/#dom-memory-buffer
  48. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> Memory::buffer() const
  49. {
  50. auto& vm = this->vm();
  51. auto& realm = *vm.current_realm();
  52. auto* memory = Bindings::WebAssemblyObject::s_abstract_machine.store().get(address());
  53. if (!memory)
  54. return vm.throw_completion<JS::RangeError>("Could not find the memory instance"sv);
  55. auto array_buffer = JS::ArrayBuffer::create(realm, &memory->data());
  56. array_buffer->set_detach_key(MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm, "WebAssembly.Memory"sv)));
  57. return array_buffer;
  58. }
  59. }