WebAssemblyInstanceObject.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. namespace Web::Bindings {
  17. WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::Realm& realm, size_t index)
  18. : Object(Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssemblyInstancePrototype"))
  19. , m_index(index)
  20. {
  21. }
  22. void WebAssemblyInstanceObject::initialize(JS::Realm& realm)
  23. {
  24. Object::initialize(realm);
  25. auto& vm = this->vm();
  26. VERIFY(!m_exports_object);
  27. m_exports_object = create(realm, nullptr);
  28. auto& instance = this->instance();
  29. auto& cache = this->cache();
  30. for (auto& export_ : instance.exports()) {
  31. export_.value().visit(
  32. [&](Wasm::FunctionAddress const& address) {
  33. Optional<JS::FunctionObject*> object = cache.function_instances.get(address);
  34. if (!object.has_value()) {
  35. object = create_native_function(vm, address, export_.name());
  36. cache.function_instances.set(address, *object);
  37. }
  38. m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
  39. },
  40. [&](Wasm::MemoryAddress const& address) {
  41. Optional<WebAssemblyMemoryObject*> object = cache.memory_instances.get(address);
  42. if (!object.has_value()) {
  43. object = heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(realm, realm, address);
  44. cache.memory_instances.set(address, *object);
  45. }
  46. m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
  47. },
  48. [&](auto const&) {
  49. // FIXME: Implement other exports!
  50. });
  51. }
  52. MUST(m_exports_object->set_integrity_level(IntegrityLevel::Frozen));
  53. }
  54. void WebAssemblyInstanceObject::visit_edges(Visitor& visitor)
  55. {
  56. Base::visit_edges(visitor);
  57. visitor.visit(m_exports_object);
  58. }
  59. }