Skip to content

Commit

Permalink
Merge #2592
Browse files Browse the repository at this point in the history
2592: Add ImportObject::get_namespace_exports r=syrusakbary a=Amanieu

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`.

Co-authored-by: Amanieu d'Antras <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2021
2 parents e84ba8f + f594722 commit 877ce1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/api/src/sys/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ impl LikeNamespace for Exports {
.map(|(k, v)| (k.clone(), v.to_export()))
.collect()
}

fn as_exports(&self) -> Option<Exports> {
Some(self.clone())
}
}

/// This trait is used to mark types as gettable from an [`Instance`].
Expand Down
16 changes: 16 additions & 0 deletions lib/api/src/sys/import_object.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -16,6 +17,12 @@ pub trait LikeNamespace {
fn get_namespace_export(&self, name: &str) -> Option<Export>;
/// 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<Exports> {
None
}
}

/// All of the import data used when instantiating.
Expand Down Expand Up @@ -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<Exports> {
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();
Expand Down

0 comments on commit 877ce1f

Please sign in to comment.