/* * Copyright (c) 2021, Ali Mohammad Pur * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include namespace Web::Bindings { WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::Realm& realm, size_t index) : Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype(realm, "WebAssemblyInstancePrototype")) , m_index(index) { } void WebAssemblyInstanceObject::initialize(JS::Realm& realm) { Object::initialize(realm); auto& vm = this->vm(); VERIFY(!m_exports_object); m_exports_object = create(realm, nullptr); auto& instance = this->instance(); auto& cache = this->cache(); for (auto& export_ : instance.exports()) { export_.value().visit( [&](Wasm::FunctionAddress const& address) { Optional object = cache.function_instances.get(address); if (!object.has_value()) { object = create_native_function(vm, address, export_.name()); cache.function_instances.set(address, *object); } m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes); }, [&](Wasm::MemoryAddress const& address) { Optional object = cache.memory_instances.get(address); if (!object.has_value()) { object = heap().allocate(realm, realm, address); cache.memory_instances.set(address, *object); } m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes); }, [&](Wasm::TableAddress const& address) { Optional object = cache.table_instances.get(address); if (!object.has_value()) { object = heap().allocate(realm, realm, address); cache.table_instances.set(address, *object); } m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes); }, [&](auto const&) { // FIXME: Implement other exports! }); } MUST(m_exports_object->set_integrity_level(IntegrityLevel::Frozen)); } void WebAssemblyInstanceObject::visit_edges(Visitor& visitor) { Base::visit_edges(visitor); visitor.visit(m_exports_object); } }