Instance.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 MUST_OR_THROW_OOM(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. JS::ThrowCompletionOr<void> Instance::initialize(JS::Realm& realm)
  35. {
  36. auto& vm = this->vm();
  37. MUST_OR_THROW_OOM(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. TRY(export_.value().visit(
  43. [&](Wasm::FunctionAddress const& address) -> JS::ThrowCompletionOr<void> {
  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. return {};
  51. },
  52. [&](Wasm::MemoryAddress const& address) -> JS::ThrowCompletionOr<void> {
  53. Optional<JS::GCPtr<Memory>> object = cache.memory_instances.get(address);
  54. if (!object.has_value()) {
  55. object = MUST_OR_THROW_OOM(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. return {};
  60. },
  61. [&](Wasm::TableAddress const& address) -> JS::ThrowCompletionOr<void> {
  62. Optional<JS::GCPtr<Table>> object = cache.table_instances.get(address);
  63. if (!object.has_value()) {
  64. object = MUST_OR_THROW_OOM(heap().allocate<Table>(realm, realm, address));
  65. cache.table_instances.set(address, *object);
  66. }
  67. m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
  68. return {};
  69. },
  70. [&](auto const&) -> JS::ThrowCompletionOr<void> {
  71. // FIXME: Implement other exports!
  72. return {};
  73. }));
  74. }
  75. MUST(m_exports->set_integrity_level(IntegrityLevel::Frozen));
  76. return {};
  77. }
  78. void Instance::visit_edges(Visitor& visitor)
  79. {
  80. Base::visit_edges(visitor);
  81. visitor.visit(m_exports);
  82. }
  83. }