Module.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/Buffers.h>
  14. namespace Web::WebAssembly {
  15. JS_DEFINE_ALLOCATOR(Module);
  16. WebIDL::ExceptionOr<JS::NonnullGCPtr<Module>> Module::construct_impl(JS::Realm& realm, JS::Handle<WebIDL::BufferSource>& bytes)
  17. {
  18. auto& vm = realm.vm();
  19. auto index = TRY(Detail::parse_module(vm, bytes->raw_object()));
  20. return vm.heap().allocate<Module>(realm, realm, index);
  21. }
  22. Module::Module(JS::Realm& realm, size_t index)
  23. : Bindings::PlatformObject(realm)
  24. , m_index(index)
  25. {
  26. }
  27. void Module::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(Module, WebAssembly.Module);
  31. }
  32. Wasm::Module const& Module::module() const
  33. {
  34. return Detail::s_compiled_modules.at(index())->module;
  35. }
  36. }