Module.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. namespace Web::WebAssembly {
  14. WebIDL::ExceptionOr<JS::NonnullGCPtr<Module>> Module::construct_impl(JS::Realm& realm, JS::Handle<JS::Object>& bytes)
  15. {
  16. auto& vm = realm.vm();
  17. auto index = TRY(Detail::parse_module(vm, bytes.cell()));
  18. return MUST_OR_THROW_OOM(vm.heap().allocate<Module>(realm, realm, index));
  19. }
  20. Module::Module(JS::Realm& realm, size_t index)
  21. : Bindings::PlatformObject(realm)
  22. , m_index(index)
  23. {
  24. }
  25. JS::ThrowCompletionOr<void> Module::initialize(JS::Realm& realm)
  26. {
  27. MUST_OR_THROW_OOM(Base::initialize(realm));
  28. set_prototype(&Bindings::ensure_web_prototype<Bindings::ModulePrototype>(realm, "WebAssembly.Module"sv));
  29. return {};
  30. }
  31. Wasm::Module const& Module::module() const
  32. {
  33. return Detail::s_compiled_modules.at(index())->module;
  34. }
  35. }