Instance.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/FunctionObject.h>
  8. #include <LibJS/Runtime/NativeFunction.h>
  9. #include <LibJS/Runtime/Realm.h>
  10. #include <LibJS/Runtime/VM.h>
  11. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  12. #include <LibWeb/Bindings/InstancePrototype.h>
  13. #include <LibWeb/Bindings/Intrinsics.h>
  14. #include <LibWeb/WebAssembly/Instance.h>
  15. #include <LibWeb/WebAssembly/Memory.h>
  16. #include <LibWeb/WebAssembly/Module.h>
  17. #include <LibWeb/WebAssembly/Table.h>
  18. #include <LibWeb/WebAssembly/WebAssembly.h>
  19. namespace Web::WebAssembly {
  20. WebIDL::ExceptionOr<JS::NonnullGCPtr<Instance>> Instance::construct_impl(JS::Realm& realm, Module& module, Optional<JS::Handle<JS::Object>>& import_object)
  21. {
  22. // FIXME: Implement the importObject parameter.
  23. (void)import_object;
  24. auto& vm = realm.vm();
  25. auto index = TRY(Detail::instantiate_module(vm, module.module()));
  26. return vm.heap().allocate<Instance>(realm, realm, index);
  27. }
  28. Instance::Instance(JS::Realm& realm, size_t index)
  29. : Bindings::PlatformObject(realm)
  30. , m_exports(Object::create(realm, nullptr))
  31. , m_index(index)
  32. {
  33. }
  34. void Instance::initialize(JS::Realm& realm)
  35. {
  36. auto& vm = this->vm();
  37. Base::initialize(realm);
  38. set_prototype(&Bindings::ensure_web_prototype<Bindings::InstancePrototype>(realm, "WebAssembly.Instance"sv));
  39. auto& instance = *Detail::s_instantiated_modules[m_index];
  40. auto& cache = Detail::s_module_caches.at(m_index);
  41. for (auto& export_ : instance.exports()) {
  42. export_.value().visit(
  43. [&](Wasm::FunctionAddress const& address) {
  44. Optional<JS::GCPtr<JS::FunctionObject>> object = cache.function_instances.get(address);
  45. if (!object.has_value()) {
  46. object = Detail::create_native_function(vm, address, export_.name());
  47. cache.function_instances.set(address, *object);
  48. }
  49. m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
  50. },
  51. [&](Wasm::MemoryAddress const& address) {
  52. Optional<JS::GCPtr<Memory>> object = cache.memory_instances.get(address);
  53. if (!object.has_value()) {
  54. object = heap().allocate<Memory>(realm, realm, address);
  55. cache.memory_instances.set(address, *object);
  56. }
  57. m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
  58. },
  59. [&](Wasm::TableAddress const& address) {
  60. Optional<JS::GCPtr<Table>> object = cache.table_instances.get(address);
  61. if (!object.has_value()) {
  62. object = heap().allocate<Table>(realm, realm, address);
  63. cache.table_instances.set(address, *object);
  64. }
  65. m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
  66. },
  67. [&](auto const&) {
  68. // FIXME: Implement other exports!
  69. });
  70. }
  71. MUST(m_exports->set_integrity_level(IntegrityLevel::Frozen));
  72. }
  73. void Instance::visit_edges(Visitor& visitor)
  74. {
  75. Base::visit_edges(visitor);
  76. visitor.visit(m_exports);
  77. }
  78. }