Instance.cpp 3.8 KB

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