WebAssemblyInstanceObject.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, "WebAssemblyInstancePrototype"))
  20. , m_index(index)
  21. {
  22. }
  23. void WebAssemblyInstanceObject::initialize(JS::Realm& realm)
  24. {
  25. 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. export_.value().visit(
  33. [&](Wasm::FunctionAddress const& address) {
  34. Optional<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. },
  41. [&](Wasm::MemoryAddress const& address) {
  42. Optional<WebAssemblyMemoryObject*> object = cache.memory_instances.get(address);
  43. if (!object.has_value()) {
  44. object = heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(realm, realm, address);
  45. cache.memory_instances.set(address, *object);
  46. }
  47. m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
  48. },
  49. [&](Wasm::TableAddress const& address) {
  50. Optional<WebAssemblyTableObject*> object = cache.table_instances.get(address);
  51. if (!object.has_value()) {
  52. object = heap().allocate<Web::Bindings::WebAssemblyTableObject>(realm, realm, address);
  53. cache.table_instances.set(address, *object);
  54. }
  55. m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
  56. },
  57. [&](auto const&) {
  58. // FIXME: Implement other exports!
  59. });
  60. }
  61. MUST(m_exports_object->set_integrity_level(IntegrityLevel::Frozen));
  62. }
  63. void WebAssemblyInstanceObject::visit_edges(Visitor& visitor)
  64. {
  65. Base::visit_edges(visitor);
  66. visitor.visit(m_exports_object);
  67. }
  68. }