Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ImportObject::get_namespace_exports #2592

Merged
merged 4 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines +23 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Option? Also, why a default impl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backward compatibility with any existing implementations of LikeNamespace outside wasmer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If backwards compatibility was not a concern then I would remove LikeNamespace entirely and use Exports directly in ImportObject.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

}

/// 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