WebAssemblyInstanceObject.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/ArrayBuffer.h>
  9. #include <LibJS/Runtime/BigInt.h>
  10. #include <LibJS/Runtime/TypedArray.h>
  11. #include <LibWasm/AbstractMachine/Interpreter.h>
  12. #include <LibWeb/Bindings/Intrinsics.h>
  13. #include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h>
  14. #include <LibWeb/WebAssembly/WebAssemblyMemoryPrototype.h>
  15. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  16. #include <LibWeb/WebAssembly/WebAssemblyTableObject.h>
  17. namespace Web::Bindings {
  18. WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::Realm& realm, size_t index)
  19. : Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"))
  20. , m_index(index)
  21. {
  22. }
  23. JS::ThrowCompletionOr<void> WebAssemblyInstanceObject::initialize(JS::Realm& realm)
  24. {
  25. MUST_OR_THROW_OOM(Object::initialize(realm));
  26. auto& vm = this->vm();
  27. VERIFY(!m_exports_object);
  28. m_exports_object = create(realm, nullptr);
  29. auto& instance = this->instance();
  30. auto& cache = this->cache();
  31. for (auto& export_ : instance.exports()) {
  32. TRY(export_.value().visit(
  33. [&](Wasm::FunctionAddress const& address) -> JS::ThrowCompletionOr<void> {
  34. Optional<JS::GCPtr<JS::FunctionObject>> object = cache.function_instances.get(address);
  35. if (!object.has_value()) {
  36. object = create_native_function(vm, address, export_.name());
  37. cache.function_instances.set(address, *object);
  38. }
  39. m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
  40. return {};
  41. },
  42. [&](Wasm::MemoryAddress const& address) -> JS::ThrowCompletionOr<void> {
  43. Optional<JS::GCPtr<WebAssemblyMemoryObject>> object = cache.memory_instances.get(address);
  44. if (!object.has_value()) {
  45. object = MUST_OR_THROW_OOM(heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(realm, realm, address));
  46. cache.memory_instances.set(address, *object);
  47. }
  48. m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
  49. return {};
  50. },
  51. [&](Wasm::TableAddress const& address) -> JS::ThrowCompletionOr<void> {
  52. Optional<JS::GCPtr<WebAssemblyTableObject>> object = cache.table_instances.get(address);
  53. if (!object.has_value()) {
  54. object = MUST_OR_THROW_OOM(heap().allocate<Web::Bindings::WebAssemblyTableObject>(realm, realm, address));
  55. cache.table_instances.set(address, *object);
  56. }
  57. m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
  58. return {};
  59. },
  60. [&](auto const&) -> JS::ThrowCompletionOr<void> {
  61. // FIXME: Implement other exports!
  62. return {};
  63. }));
  64. }
  65. MUST(m_exports_object->set_integrity_level(IntegrityLevel::Frozen));
  66. return {};
  67. }
  68. void WebAssemblyInstanceObject::visit_edges(Visitor& visitor)
  69. {
  70. Base::visit_edges(visitor);
  71. visitor.visit(m_exports_object);
  72. }
  73. }