Module.cpp 1.5 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. #include <LibJS/Runtime/Realm.h>
  8. #include <LibJS/Runtime/VM.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/Bindings/ModulePrototype.h>
  11. #include <LibWeb/WebAssembly/Module.h>
  12. #include <LibWeb/WebAssembly/WebAssembly.h>
  13. #include <LibWeb/WebIDL/AbstractOperations.h>
  14. #include <LibWeb/WebIDL/Buffers.h>
  15. namespace Web::WebAssembly {
  16. GC_DEFINE_ALLOCATOR(Module);
  17. WebIDL::ExceptionOr<GC::Ref<Module>> Module::construct_impl(JS::Realm& realm, GC::Root<WebIDL::BufferSource>& bytes)
  18. {
  19. auto& vm = realm.vm();
  20. auto stable_bytes_or_error = WebIDL::get_buffer_source_copy(bytes->raw_object());
  21. if (stable_bytes_or_error.is_error()) {
  22. VERIFY(stable_bytes_or_error.error().code() == ENOMEM);
  23. return vm.throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  24. }
  25. auto stable_bytes = stable_bytes_or_error.release_value();
  26. auto compiled_module = TRY(Detail::compile_a_webassembly_module(vm, move(stable_bytes)));
  27. return realm.create<Module>(realm, move(compiled_module));
  28. }
  29. Module::Module(JS::Realm& realm, NonnullRefPtr<Detail::CompiledWebAssemblyModule> compiled_module)
  30. : Bindings::PlatformObject(realm)
  31. , m_compiled_module(move(compiled_module))
  32. {
  33. }
  34. void Module::initialize(JS::Realm& realm)
  35. {
  36. Base::initialize(realm);
  37. WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(Module, WebAssembly.Module);
  38. }
  39. }