/* * Copyright (c) 2021, Ali Mohammad Pur * Copyright (c) 2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include namespace Web::WebAssembly { WebIDL::ExceptionOr> Instance::construct_impl(JS::Realm& realm, Module& module, Optional>& import_object) { // FIXME: Implement the importObject parameter. (void)import_object; auto& vm = realm.vm(); auto index = TRY(Detail::instantiate_module(vm, module.module())); return MUST_OR_THROW_OOM(vm.heap().allocate(realm, realm, index)); } Instance::Instance(JS::Realm& realm, size_t index) : Bindings::PlatformObject(realm) , m_exports(Object::create(realm, nullptr)) , m_index(index) { } JS::ThrowCompletionOr Instance::initialize(JS::Realm& realm) { auto& vm = this->vm(); MUST_OR_THROW_OOM(Base::initialize(realm)); set_prototype(&Bindings::ensure_web_prototype(realm, "WebAssembly.Instance"sv)); auto& instance = *Detail::s_instantiated_modules[m_index]; auto& cache = Detail::s_module_caches.at(m_index); for (auto& export_ : instance.exports()) { TRY(export_.value().visit( [&](Wasm::FunctionAddress const& address) -> JS::ThrowCompletionOr { Optional> object = cache.function_instances.get(address); if (!object.has_value()) { object = Detail::create_native_function(vm, address, export_.name()); cache.function_instances.set(address, *object); } m_exports->define_direct_property(export_.name(), *object, JS::default_attributes); return {}; }, [&](Wasm::MemoryAddress const& address) -> JS::ThrowCompletionOr { Optional> object = cache.memory_instances.get(address); if (!object.has_value()) { object = MUST_OR_THROW_OOM(heap().allocate(realm, realm, address)); cache.memory_instances.set(address, *object); } m_exports->define_direct_property(export_.name(), *object, JS::default_attributes); return {}; }, [&](Wasm::TableAddress const& address) -> JS::ThrowCompletionOr { Optional> object = cache.table_instances.get(address); if (!object.has_value()) { object = MUST_OR_THROW_OOM(heap().allocate(realm, realm, address)); cache.table_instances.set(address, *object); } m_exports->define_direct_property(export_.name(), *object, JS::default_attributes); return {}; }, [&](auto const&) -> JS::ThrowCompletionOr { // FIXME: Implement other exports! return {}; })); } MUST(m_exports->set_integrity_level(IntegrityLevel::Frozen)); return {}; } void Instance::visit_edges(Visitor& visitor) { Base::visit_edges(visitor); visitor.visit(m_exports); } }