Skip to content

Commit

Permalink
Revert changes to Exportable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Jun 9, 2020
1 parent a8faa2e commit 7eb5495
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 112 deletions.
8 changes: 4 additions & 4 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ wasmer_compilers! {
},
};
let instance = Instance::new(&module, &import_object).unwrap();
let dyn_f: Function = instance.exports.get("add").unwrap();
let dyn_f: &Function = instance.exports.get("add").unwrap();
let f: NativeFunc<(i32, i32), i32> = dyn_f.native().unwrap();

c.bench_function(&format!("basic static func {}", COMPILER_NAME), |b| {
Expand All @@ -54,7 +54,7 @@ wasmer_compilers! {
})
});

let dyn_f_many: Function = instance.exports.get("add20").unwrap();
let dyn_f_many: &Function = instance.exports.get("add20").unwrap();
let f_many: NativeFunc<(i32, i32, i32, i32, i32,
i32, i32, i32, i32, i32,
i32, i32, i32, i32, i32,
Expand All @@ -77,15 +77,15 @@ wasmer_compilers! {
};
let instance = Instance::new(&module, &import_object).unwrap();

let dyn_f: Function = instance.exports.get("add").unwrap();
let dyn_f: &Function = instance.exports.get("add").unwrap();
c.bench_function(&format!("basic dynfunc {}", COMPILER_NAME), |b| {
b.iter(|| {
let dyn_result = black_box(dyn_f.call(&[Val::I32(4), Val::I32(6)]).unwrap());
assert_eq!(dyn_result[0], Val::I32(10));
})
});

let dyn_f_many: Function = instance.exports.get("add20").unwrap();
let dyn_f_many: &Function = instance.exports.get("add20").unwrap();
c.bench_function(&format!("basic dynfunc with many args {}", COMPILER_NAME), |b| {
b.iter(|| {
let dyn_result = black_box(dyn_f_many.call(
Expand Down
16 changes: 9 additions & 7 deletions lib/api/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,30 @@ impl Exports {
///
/// If you want to get an export dynamically handling manually
/// type checking manually, please use `get_extern`.
pub fn get<'a, T: Exportable<'a>>(&'a self, name: &str) -> Result<T, ExportError> {
pub fn get<'a, T: Exportable<'a>>(&'a self, name: &str) -> Result<&'a T, ExportError> {
match self.map.get(name) {
None => Err(ExportError::Missing(name.to_string())),
Some(extern_) => T::get_self_from_extern(extern_),
}
}

/// Get an export as a `Global`.
pub fn get_global(&self, name: &str) -> Result<Global, ExportError> {
pub fn get_global(&self, name: &str) -> Result<&Global, ExportError> {
self.get(name)
}

/// Get an export as a `Memory`.
pub fn get_memory(&self, name: &str) -> Result<Memory, ExportError> {
pub fn get_memory(&self, name: &str) -> Result<&Memory, ExportError> {
self.get(name)
}

/// Get an export as a `Table`.
pub fn get_table(&self, name: &str) -> Result<Table, ExportError> {
pub fn get_table(&self, name: &str) -> Result<&Table, ExportError> {
self.get(name)
}

/// Get an export as a `Func`.
pub fn get_function(&self, name: &str) -> Result<Function, ExportError> {
pub fn get_function(&self, name: &str) -> Result<&Function, ExportError> {
self.get(name)
}

Expand All @@ -127,7 +127,9 @@ impl Exports {
Args: WasmTypeList,
Rets: WasmTypeList,
{
self.get(name)
self.get_function(name)?
.native()
.ok_or(ExportError::IncompatibleType)
}

/// Get an export as an `Extern`.
Expand Down Expand Up @@ -256,5 +258,5 @@ pub trait Exportable<'a>: Sized {
/// from an [`Instance`] by name.
///
/// [`Instance`]: crate::Instance
fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError>;
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError>;
}
20 changes: 4 additions & 16 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Function {
}
}

pub fn native<'a, Args, Rets>(self) -> Option<NativeFunc<'a, Args, Rets>>
pub fn native<'a, Args, Rets>(&self) -> Option<NativeFunc<'a, Args, Rets>>
where
Args: WasmTypeList,
Rets: WasmTypeList,
Expand All @@ -304,11 +304,11 @@ impl Function {
}

Some(NativeFunc::new(
self.store,
self.store.clone(),
self.exported.address,
self.exported.vmctx,
self.exported.kind,
self.definition,
self.definition.clone(),
))
}
}
Expand All @@ -317,19 +317,7 @@ impl<'a> Exportable<'a> for Function {
fn to_export(&self) -> Export {
self.exported.clone().into()
}
fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
match _extern {
Extern::Function(func) => Ok(func.clone()),
_ => Err(ExportError::IncompatibleType),
}
}
}

impl<'a> Exportable<'a> for &'a Function {
fn to_export(&self) -> Export {
self.exported.clone().into()
}
fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Extern::Function(func) => Ok(func),
_ => Err(ExportError::IncompatibleType),
Expand Down
15 changes: 1 addition & 14 deletions lib/api/src/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,7 @@ impl<'a> Exportable<'a> for Global {
self.exported.clone().into()
}

fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
match _extern {
Extern::Global(global) => Ok(global.clone()),
_ => Err(ExportError::IncompatibleType),
}
}
}

impl<'a> Exportable<'a> for &'a Global {
fn to_export(&self) -> Export {
self.exported.clone().into()
}

fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Extern::Global(global) => Ok(global),
_ => Err(ExportError::IncompatibleType),
Expand Down
14 changes: 1 addition & 13 deletions lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,7 @@ impl<'a> Exportable<'a> for Memory {
fn to_export(&self) -> Export {
self.exported.clone().into()
}
fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
match _extern {
Extern::Memory(memory) => Ok(memory.clone()),
_ => Err(ExportError::IncompatibleType),
}
}
}

impl<'a> Exportable<'a> for &'a Memory {
fn to_export(&self) -> Export {
self.exported.clone().into()
}
fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Extern::Memory(memory) => Ok(memory),
_ => Err(ExportError::IncompatibleType),
Expand Down
17 changes: 1 addition & 16 deletions lib/api/src/externals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,7 @@ impl<'a> Exportable<'a> for Extern {
}
}

fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
Ok(_extern.clone())
}
}

impl<'a> Exportable<'a> for &'a Extern {
fn to_export(&self) -> Export {
match self {
Extern::Function(f) => f.to_export(),
Extern::Global(g) => g.to_export(),
Extern::Memory(m) => m.to_export(),
Extern::Table(t) => t.to_export(),
}
}

fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
// Since this is already an extern, we can just return it.
Ok(_extern)
}
Expand Down
14 changes: 1 addition & 13 deletions lib/api/src/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,7 @@ impl<'a> Exportable<'a> for Table {
fn to_export(&self) -> Export {
self.exported.clone().into()
}
fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
match _extern {
Extern::Table(table) => Ok(table.clone()),
_ => Err(ExportError::IncompatibleType),
}
}
}

impl<'a> Exportable<'a> for &'a Table {
fn to_export(&self) -> Export {
self.exported.clone().into()
}
fn get_self_from_extern(_extern: &'a Extern) -> Result<Self, ExportError> {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Extern::Table(table) => Ok(table),
_ => Err(ExportError::IncompatibleType),
Expand Down
23 changes: 0 additions & 23 deletions lib/api/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,6 @@ where
}
}

impl<'a, Args, Rets> Exportable<'a> for NativeFunc<'a, Args, Rets>
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
fn to_export(&self) -> Export {
let ef: ExportFunction = self.into();
ef.into()
}

// Cannot be implemented because of the return type `&Self` TODO:
fn get_self_from_extern(extern_: &'a Extern) -> Result<Self, ExportError> {
match extern_ {
// TODO: review error return type in failure of `f.native()`
Extern::Function(f) => f
.clone()
.native()
.ok_or_else(|| ExportError::IncompatibleType),
_ => Err(ExportError::IncompatibleType),
}
}
}

impl<'a, Args, Rets> From<&NativeFunc<'a, Args, Rets>> for ExportFunction
where
Args: WasmTypeList,
Expand Down
2 changes: 1 addition & 1 deletion lib/c-api/src/wasm_c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub unsafe extern "C" fn wasm_instance_exports(
.iter()
.map(|(name, r#extern)| {
let function = if let Extern::Function { .. } = r#extern {
instance.exports.get_function(&name).ok()
instance.exports.get_function(&name).ok().cloned()
} else {
None
};
Expand Down
6 changes: 3 additions & 3 deletions src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ impl Wasi {

let instance = Instance::new(&module, &import_object)?;

let memory: Memory = instance.exports.get("memory")?;
wasi_env.set_memory(&memory);
let memory: &Memory = instance.exports.get("memory")?;
wasi_env.set_memory(memory);

let start: Function = instance.exports.get("_start")?;
let start: &Function = instance.exports.get("_start")?;

start
.call(&[])
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/wast/src/wast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl Wast {
args: &[Val],
) -> Result<Vec<Val>> {
let instance = self.get_instance(instance_name.as_ref().map(|x| &**x))?;
let func: Function = instance.exports.get(field)?;
let func: &Function = instance.exports.get(field)?;
match func.call(args) {
Ok(result) => Ok(result.into()),
Err(e) => Err(e.into()),
Expand All @@ -351,7 +351,7 @@ impl Wast {
/// Get the value of an exported global from an instance.
fn get(&mut self, instance_name: Option<&str>, field: &str) -> Result<Vec<Val>> {
let instance = self.get_instance(instance_name.as_ref().map(|x| &**x))?;
let global: Global = instance.exports.get(field)?;
let global: &Global = instance.exports.get(field)?;
Ok(vec![global.get()])
}

Expand Down

0 comments on commit 7eb5495

Please sign in to comment.