Imported native functions now work!

This commit is contained in:
Syrus Akbary
2021-06-22 18:34:51 -07:00
parent 91b24274a9
commit b326903a35
2 changed files with 59 additions and 12 deletions

View File

@@ -116,7 +116,6 @@ impl wasmer_types::WasmValueType for Function {
impl WasmerEnv for WithoutEnv {}
#[wasm_bindgen]
pub extern "C" fn call_func_dynamic(arg: u32) -> u32 {
return arg + 1;
}
@@ -293,9 +292,17 @@ impl Function {
if std::mem::size_of::<F>() != 0 {
Self::closures_unsupported_panic();
}
unimplemented!();
// let function = inner::Function::<Args, Rets>::new(func);
// let address = function.address() as *const VMFunctionBody;
let function = inner::Function::<Args, Rets>::new(func);
let address = function.address() as usize as u32;
let ft = wasm_bindgen::function_table();
let as_table = ft.unchecked_ref::<js_sys::WebAssembly::Table>();
let func = as_table.get(address).unwrap();
Self {
store: store.clone(),
exported: func,
}
// let vmctx = VMFunctionEnvironment {
// host_env: std::ptr::null_mut() as *mut _,
// };
@@ -1311,7 +1318,7 @@ mod inner {
/// This is a function that wraps the real host
/// function. Its address will be used inside the
/// runtime.
extern fn func_wrapper<$( $x, )* Rets, RetsAsResult, Func>( _: usize, $( $x: $x::Native, )* ) -> Rets::CStruct
extern fn func_wrapper<$( $x, )* Rets, RetsAsResult, Func>( $( $x: $x::Native, )* ) -> Rets::CStruct
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
@@ -1322,12 +1329,13 @@ mod inner {
let result = panic::catch_unwind(AssertUnwindSafe(|| {
func( $( FromToNativeWasmType::from_native($x) ),* ).into_result()
}));
unimplemented!();
// match result {
// Ok(Ok(result)) => return result.into_c_struct(),
// Ok(Err(trap)) => unsafe { raise_user_trap(Box::new(trap)) },
// Err(panic) => unsafe { resume_panic(panic) },
// }
// unimplemented!();
match result {
Ok(Ok(result)) => return result.into_c_struct(),
_ => unimplemented!(),
// Ok(Err(trap)) => unsafe { raise_user_trap(Box::new(trap)) },
// Err(panic) => unsafe { resume_panic(panic) },
}
}
func_wrapper::< $( $x, )* Rets, RetsAsResult, Self > as *const VMFunctionBody

View File

@@ -120,7 +120,7 @@ fn test_exported_function() {
// }
#[wasm_bindgen_test]
fn test_imported_function() {
fn test_imported_function_dynamic() {
let store = Store::default();
let module = Module::new(
&store,
@@ -159,3 +159,42 @@ fn test_imported_function() {
let expected = vec![Val::F64(5.0)].into_boxed_slice();
assert_eq!(exported.call(&[Val::I32(4)]), Ok(expected));
}
#[wasm_bindgen_test]
fn test_imported_function_native() {
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();
assert_eq!(exported.call(&[Val::I32(4)]), Ok(expected));
}