Instance.cpp 3.3 KB

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