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

Added function to merge ImportObjects #190

Closed
wants to merge 9 commits into from

Conversation

tprk77
Copy link

@tprk77 tprk77 commented Feb 18, 2019

I thought I would try implementing the ImportObject merging I needed. The main use case is merging the Emscripten imports from generate_emscripten_env with the usual ones, i.e., print_str from the wasmer-rust-example.

Here's where I'm using it, in my attempt to update the wasmer-rust-example to run Emscripten stuff:

https://github.com/tprk77/wasmer-rust-example/blob/emscripten-wip/src/main.rs

Apologies in advance if my code is wacky, I'm still kind of learning Rust. I'd appreciate any feedback you might have.

@xmclark xmclark self-requested a review February 18, 2019 16:42
@xmclark
Copy link
Contributor

xmclark commented Feb 18, 2019

Don't be too hard on yourself. Many of us are still learning rust 😄.

This code looks great! Would you be willing to write some unit tests?

@@ -79,6 +80,46 @@ impl ImportObject {
pub fn get_namespace(&self, namespace: &str) -> Option<&(dyn LikeNamespace + 'static)> {
self.map.get(namespace).map(|namespace| &**namespace)
}

pub fn merged(mut imports_a: ImportObject, mut imports_b: ImportObject) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think merge may be a better name.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good to me.

@tprk77
Copy link
Author

tprk77 commented Feb 18, 2019

Yeah, I can write some tests, I just haven't done that before in Rust. Are the tests in emscripten/src/utils.rs a good example of how to structure those?

@xmclark
Copy link
Contributor

xmclark commented Feb 18, 2019

@tprk77 We do not have any conventions for this kind of testing. You can't do anything wrong by making tests. If we feel like reorganizing, we will, but the win with tests outweighs any refactoring costs in the future.

For now, putting the tests in the same file is fine. Cargo will find the tests no matter what when we run cargo test. To add some tests, append some code like this to the end of the file:

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_merge imports() {
        // assert!(truthy_thing, "message if fails")
    }
}

@@ -79,6 +80,46 @@ impl ImportObject {
pub fn get_namespace(&self, namespace: &str) -> Option<&(dyn LikeNamespace + 'static)> {
self.map.get(namespace).map(|namespace| &**namespace)
}

pub fn merged(mut imports_a: ImportObject, mut imports_b: ImportObject) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add some documentation here about the behavior of this method during edge cases (e.g. Two items with the same name)?

@tprk77 tprk77 force-pushed the import-object-merging branch 2 times, most recently from ce13820 to 9d81261 Compare February 20, 2019 03:29
@tprk77
Copy link
Author

tprk77 commented Feb 20, 2019

Ok, I think I've got everything in order. Let me know if I need to add or change anything else.

@tprk77 tprk77 force-pushed the import-object-merging branch from 9d81261 to 9065fe4 Compare February 20, 2019 03:45
@syrusakbary
Copy link
Member

@tprk77 we are thinking on adding an extend method to the ImportObject so it's much easier to do it via Extend, FromIterator and IntoIterator traits.

@tprk77
Copy link
Author

tprk77 commented Feb 23, 2019

@syrusakbary Sounds like a good idea. I wouldn't mind working on it.

I have a question about the details. I have mostly been thinking of Namespace as a set of exports, and ImportObject as a set of namespaces. So for a namespace, extend would add its exports to the set which makes sense. But for ImportObject what should extend actually do?

If it's analogous to a set, it should just add a namespace to the set. I think using extend for merge semantics on the underlying namespaces may be a bit confusing to the caller. But on the other hand, it's probably what people usually want to happen? Do you have an opinion on this either way?

@tprk77 tprk77 force-pushed the import-object-merging branch 3 times, most recently from 811fcc5 to eaa1a4f Compare March 1, 2019 03:36
@tprk77
Copy link
Author

tprk77 commented Mar 1, 2019

I implemented iterators for import objects and like-namespaces. I wrote tests for both.

Implementing the iterator for LikeNamespace was pretty tricky because of it being a trait-object. The iterator is boxed so different implementations can return different iterators.

I got rid of the get_all_exports, and replaced it with get_export_names and ExportIter. I thought you would all like that, it seems much cleaner to me.

I kept the merge function mostly the same for now, since I'm not sure what you want to do with that.

Please send your comments my way, I'm still happy to work on this.

@Hywan Hywan added 🎉 enhancement New feature! 📦 lib-deprecated About the deprecated crates labels Mar 6, 2019
@tprk77 tprk77 force-pushed the import-object-merging branch from eaa1a4f to 89445a5 Compare March 7, 2019 01:45
@tprk77
Copy link
Author

tprk77 commented Mar 9, 2019

This PR doesn't work anymore.

@tprk77 tprk77 closed this Mar 9, 2019
nlewycky pushed a commit that referenced this pull request Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature! 📦 lib-deprecated About the deprecated crates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants