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 extend to imports #286

Merged
merged 3 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
120 changes: 120 additions & 0 deletions lib/runtime-core/src/import.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::export::Export;
use hashbrown::{hash_map::Entry, HashMap};
use std::collections::VecDeque;
use std::{
cell::{Ref, RefCell},
rc::Rc,
};

pub trait LikeNamespace {
fn get_export(&self, name: &str) -> Option<Export>;
fn get_exports(&self) -> Vec<(String, Export)>;
fn maybe_insert(&mut self, name: &str, export: Export) -> Option<()>;
}

pub trait IsExport {
Expand Down Expand Up @@ -97,6 +100,56 @@ impl ImportObject {
map: Rc::clone(&self.map),
}
}

fn get_objects(&self) -> VecDeque<(String, String, Export)> {
let mut out = VecDeque::new();
for (name, ns) in self.map.borrow().iter() {
for (id, exp) in ns.get_exports() {
out.push_back((name.clone(), id, exp));
}
}
out
}
}

pub struct ImportObjectIterator {
elements: VecDeque<(String, String, Export)>,
}

impl Iterator for ImportObjectIterator {
type Item = (String, String, Export);
fn next(&mut self) -> Option<Self::Item> {
self.elements.pop_front()
}
}

impl IntoIterator for ImportObject {
type IntoIter = ImportObjectIterator;
type Item = (String, String, Export);

fn into_iter(self) -> Self::IntoIter {
ImportObjectIterator {
elements: self.get_objects(),
}
}
}

impl Extend<(String, String, Export)> for ImportObject {
fn extend<T: IntoIterator<Item = (String, String, Export)>>(&mut self, iter: T) {
let mut map = self.map.borrow_mut();
for (ns, id, exp) in iter.into_iter() {
if let Some(like_ns) = map.get_mut(&ns) {
like_ns.maybe_insert(&id, exp).expect(&format!(
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should panic if the imports are overwritten. The same way extend doesn't panic on extending a HashMap.

"Insert failed. Duplicate name {} found in namespace {}",
id, ns
));
} else {
let mut new_ns = Namespace::new();
new_ns.insert(id, exp);
map.insert(ns, Box::new(new_ns));
}
}
}
}

pub struct Namespace {
Expand All @@ -123,4 +176,71 @@ impl LikeNamespace for Namespace {
fn get_export(&self, name: &str) -> Option<Export> {
self.map.get(name).map(|is_export| is_export.to_export())
}

fn get_exports(&self) -> Vec<(String, Export)> {
self.map
.iter()
.map(|(k, v)| (k.clone(), v.to_export()))
.collect()
}

fn maybe_insert(&mut self, name: &str, export: Export) -> Option<()> {
if self.map.contains_key(name) {
None
} else {
self.map.insert(name.to_owned(), Box::new(export));
Some(())
}
}
}

#[cfg(test)]
mod test {
use crate::global::Global;
use crate::types::Value;

#[test]
fn extending_works() {
let mut imports1 = imports! {
"dog" => {
"happy" => Global::new(Value::I32(0)),
},
};

let imports2 = imports! {
"dog" => {
"small" => Global::new(Value::I32(2)),
},
"cat" => {
"small" => Global::new(Value::I32(3)),
},
};

imports1.extend(imports2);

let cat_ns = imports1.get_namespace("cat").unwrap();
assert!(cat_ns.get_export("small").is_some());

let dog_ns = imports1.get_namespace("dog").unwrap();
assert!(dog_ns.get_export("happy").is_some());
assert!(dog_ns.get_export("small").is_some());
}

#[test]
#[should_panic]
fn extending_conflict_panics() {
let mut imports1 = imports! {
"dog" => {
"happy" => Global::new(Value::I32(0)),
},
};

let imports2 = imports! {
"dog" => {
"happy" => Global::new(Value::I32(4)),
},
};

imports1.extend(imports2);
}
}
8 changes: 8 additions & 0 deletions lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ impl LikeNamespace for Instance {

Some(self.inner.get_export_from_index(&self.module, export_index))
}

fn get_exports(&self) -> Vec<(String, Export)> {
unimplemented!("Use the exports method instead");
}

fn maybe_insert(&mut self, _name: &str, _export: Export) -> Option<()> {
None
}
}

/// A representation of an exported WebAssembly function.
Expand Down