From 35d6c0229c1f85969053d695f89cbdc5586d6392 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Fri, 1 Oct 2021 16:08:47 +0100 Subject: [PATCH] Add ImportObject::get_namespace_exports This allows the contents of an existing namespace to be added to by extracting an `Exports` from it, adding to that `Exports` and then replacing the existing namespace with the modified `Exports`. --- lib/api/src/sys/exports.rs | 4 ++++ lib/api/src/sys/import_object.rs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/api/src/sys/exports.rs b/lib/api/src/sys/exports.rs index 6978a60428f..bee9c7bd4db 100644 --- a/lib/api/src/sys/exports.rs +++ b/lib/api/src/sys/exports.rs @@ -290,6 +290,10 @@ impl LikeNamespace for Exports { .map(|(k, v)| (k.clone(), v.to_export())) .collect() } + + fn as_exports(&self) -> Option { + Some(self.clone()) + } } /// This trait is used to mark types as gettable from an [`Instance`]. diff --git a/lib/api/src/sys/import_object.rs b/lib/api/src/sys/import_object.rs index e8491c684ce..cdeea93dc7c 100644 --- a/lib/api/src/sys/import_object.rs +++ b/lib/api/src/sys/import_object.rs @@ -1,6 +1,7 @@ //! The import module contains the implementation data structures and helper functions used to //! manipulate and access a wasm module's imports including memories, tables, globals, and //! functions. +use crate::Exports; use std::borrow::{Borrow, BorrowMut}; use std::collections::VecDeque; use std::collections::{hash_map::Entry, HashMap}; @@ -16,6 +17,12 @@ pub trait LikeNamespace { fn get_namespace_export(&self, name: &str) -> Option; /// Gets all exports in the namespace. fn get_namespace_exports(&self) -> Vec<(String, Export)>; + /// Returns the contents of this namespace as an `Exports`. + /// + /// This is used by `ImportObject::get_namespace_exports`. + fn as_exports(&self) -> Option { + None + } } /// All of the import data used when instantiating. @@ -101,6 +108,15 @@ impl ImportObject { } } + /// Returns the contents of a namespace as an `Exports`. + /// + /// Returns `None` if the namespace doesn't exist or doesn't implement the + /// `as_exports` method. + pub fn get_namespace_exports(&self, name: &str) -> Option { + let map = self.map.lock().unwrap(); + map.get(name).and_then(|ns| ns.as_exports()) + } + fn get_objects(&self) -> VecDeque<((String, String), Export)> { let mut out = VecDeque::new(); let guard = self.map.lock().unwrap();