Improved API further

This commit is contained in:
Syrus Akbary
2021-07-13 00:41:50 -07:00
parent 83d061d653
commit c6d25ffbfb
2 changed files with 61 additions and 14 deletions

View File

@@ -291,6 +291,7 @@ mod import_object;
mod instance;
mod iterators;
mod module;
#[cfg(feature = "wasm-types-polyfill")]
mod module_info_polyfill;
mod resolver;
// mod native;
@@ -340,3 +341,46 @@ pub use wat::parse_bytes as wat2wasm;
/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[no_mangle]
/// example doc
pub fn example() -> bool {
let store = Store::default();
let module = Module::new(
&store,
br#"
(module
(func $imported (import "env" "imported") (param i32) (result i32))
(func (export "exported") (param i32) (result i32)
(call $imported (local.get 0))
)
)
"#,
)
.unwrap();
fn imported_fn(arg: u32) -> u32 {
return arg + 1;
}
let imported = Function::new_native(&store, imported_fn);
let import_object = imports! {
"env" => {
"imported" => imported,
}
};
let instance = Instance::new(&module, &import_object).unwrap();
// let memory = instance.exports.get_memory("mem").unwrap();
// assert_eq!(memory.size(), Pages(1));
// assert_eq!(memory.data_size(), 65536);
let exported = instance.exports.get_function("exported").unwrap();
let expected = vec![Val::F64(5.0)].into_boxed_slice();
exported.call(&[Val::I32(4)]) == Ok(expected)
}

View File

@@ -162,21 +162,24 @@ impl Module {
let module = WebAssembly::Module::new(&js_bytes.into()).unwrap();
// The module is now validated, so we can safely parse it's types
let info = crate::module_info_polyfill::translate_module(binary).unwrap();
#[cfg(feature = "wasm-types-polyfill")]
let (type_hints, name) = (
Some(ModuleTypeHints {
imports: info
.imports()
.map(|import| import.ty().clone())
.collect::<Vec<_>>(),
exports: info
.exports()
.map(|export| export.ty().clone())
.collect::<Vec<_>>(),
}),
info.name,
);
let (type_hints, name) = {
let info = crate::module_info_polyfill::translate_module(binary).unwrap();
(
Some(ModuleTypeHints {
imports: info
.imports()
.map(|import| import.ty().clone())
.collect::<Vec<_>>(),
exports: info
.exports()
.map(|export| export.ty().clone())
.collect::<Vec<_>>(),
}),
info.name,
)
};
#[cfg(not(feature = "wasm-types-polyfill"))]
let (type_hints, name) = (None, None);