Skip to content

Commit

Permalink
Make wasmtime::Export's fields private.
Browse files Browse the repository at this point in the history
This makes them more consistent with ExportType.
  • Loading branch information
sunfishcode committed Apr 20, 2020
1 parent e4b07c5 commit b53c7c1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
56 changes: 38 additions & 18 deletions crates/api/src/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ impl Extern {
}

pub(crate) fn from_wasmtime_export(
wasmtime_export: wasmtime_runtime::Export,
store: &Store,
instance_handle: InstanceHandle,
export: wasmtime_runtime::Export,
) -> Extern {
match export {
match wasmtime_export {
wasmtime_runtime::Export::Function(f) => {
Extern::Func(Func::from_wasmtime_function(f, store, instance_handle))
}
Expand Down Expand Up @@ -901,35 +901,55 @@ pub unsafe trait MemoryCreator: Send + Sync {
#[derive(Clone)]
pub struct Export<'instance> {
/// The name of the export.
pub name: &'instance str,
name: &'instance str,

/// The value of the export.
pub external: Extern,
/// The definition of the export.
definition: Extern,
}

impl<'instance> Export<'instance> {
/// Shorthand for `self.external.into_func()`.
/// Creates a new export which is exported with the given `name` and has the
/// given `definition`.
pub(crate) fn new(name: &'instance str, definition: Extern) -> Export<'instance> {
Export { name, definition }
}

/// Returns the name by which this export is known.
pub fn name(&self) -> &'instance str {
self.name
}

/// Return the `ExternType` of this export.
pub fn ty(&self) -> ExternType {
self.definition.ty()
}

/// Consume this `Export` and return the contained `Extern`.
pub fn into_extern(self) -> Extern {
self.definition
}

/// Consume this `Export` and return the contained `Func`, if it's a function,
/// or `None` otherwise.
pub fn into_func(self) -> Option<Func> {
self.external.into_func()
self.definition.into_func()
}

/// Shorthand for `self.external.into_table()`.
/// Consume this `Export` and return the contained `Table`, if it's a table,
/// or `None` otherwise.
pub fn into_table(self) -> Option<Table> {
self.external.into_table()
self.definition.into_table()
}

/// Shorthand for `self.external.into_memory()`.
/// Consume this `Export` and return the contained `Memory`, if it's a memory,
/// or `None` otherwise.
pub fn into_memory(self) -> Option<Memory> {
self.external.into_memory()
self.definition.into_memory()
}

/// Shorthand for `self.external.into_global()`.
/// Consume this `Export` and return the contained `Global`, if it's a global,
/// or `None` otherwise.
pub fn into_global(self) -> Option<Global> {
self.external.into_global()
}

/// Shorthand for `self.external.ty()`.
pub fn ty(&self) -> ExternType {
self.external.ty()
self.definition.into_global()
}
}
8 changes: 3 additions & 5 deletions crates/api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@ impl Instance {
.exports()
.map(move |(name, entity_index)| {
let export = instance_handle.lookup_by_declaration(entity_index);
Export {
name,
external: Extern::from_wasmtime_export(store, instance_handle.clone(), export),
}
let extern_ = Extern::from_wasmtime_export(export, store, instance_handle.clone());
Export::new(name, extern_)
})
}

Expand All @@ -183,9 +181,9 @@ impl Instance {
pub fn get_export(&self, name: &str) -> Option<Extern> {
let export = self.instance_handle.lookup(&name)?;
Some(Extern::from_wasmtime_export(
export,
self.module.store(),
self.instance_handle.clone(),
export,
))
}

Expand Down
8 changes: 4 additions & 4 deletions crates/api/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Linker {
if !item.comes_from_same_store(&self.store) {
bail!("all linker items must be from the same store");
}
self.insert(module, name, item.ty(), item)?;
self.insert(module, name, item)?;
Ok(self)
}

Expand Down Expand Up @@ -265,7 +265,7 @@ impl Linker {
bail!("all linker items must be from the same store");
}
for export in instance.exports() {
self.insert(module_name, export.name, export.ty(), export.external)?;
self.insert(module_name, export.name(), export.into_extern())?;
}
Ok(self)
}
Expand All @@ -291,8 +291,8 @@ impl Linker {
Ok(())
}

fn insert(&mut self, module: &str, name: &str, ty: ExternType, item: Extern) -> Result<()> {
let key = self.import_key(module, name, ty);
fn insert(&mut self, module: &str, name: &str, item: Extern) -> Result<()> {
let key = self.import_key(module, name, item.ty());
match self.map.entry(key) {
Entry::Occupied(o) if !self.allow_shadowing => bail!(
"import of `{}::{}` with kind {:?} defined twice",
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub extern "C" fn wasm_instance_exports(instance: &wasm_instance_t, out: &mut wa
let instance = &instance.instance.borrow();
instance
.exports()
.map(|e| match e.external {
.map(|e| match e.into_extern() {
Extern::Func(f) => ExternHost::Func(HostRef::new(f)),
Extern::Global(f) => ExternHost::Global(HostRef::new(f)),
Extern::Memory(f) => ExternHost::Memory(HostRef::new(f)),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl RunCommand {
// If a function to invoke was given, invoke it.
if let Some(name) = self.invoke.as_ref() {
self.invoke_export(instance, name)?;
} else if instance.exports().any(|export| export.name.is_empty()) {
} else if instance.exports().any(|export| export.name().is_empty()) {
// Launch the default command export.
self.invoke_export(instance, "")?;
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/all/custom_signal_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ mod tests {

// instance2 which calls 'instance1.read'
let module2 = Module::new(&store, WAT2)?;
let instance2 = Instance::new(&module2, &[instance1_read.external])?;
let instance2 = Instance::new(&module2, &[instance1_read.into_extern()])?;
// since 'instance2.run' calls 'instance1.read' we need to set up the signal handler to handle
// SIGSEGV originating from within the memory of instance1
unsafe {
Expand Down

0 comments on commit b53c7c1

Please sign in to comment.