WebAssemblyInstanceObject.cpp 2.4 KB

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