mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-24 18:02:13 +00:00
Improved tests coverage
This commit is contained in:
parent
1879a14938
commit
daefac46bb
@ -27,8 +27,10 @@ region = "0.3.0"
|
||||
memmap = "0.6.2"
|
||||
spin = "0.4.9"
|
||||
log = "0.4.5"
|
||||
libffi = "0.6.4"
|
||||
target-lexicon = { version = "0.0.3", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
libffi = "0.6.4"
|
||||
|
||||
[features]
|
||||
debug = []
|
||||
|
@ -77,6 +77,8 @@ impl<'module> ScriptHandler for StoreCtrl<'module> {
|
||||
Some(&Export::Function(index)) => index,
|
||||
_ => panic!("Function not found"),
|
||||
};
|
||||
// We map the arguments provided into the raw Arguments provided
|
||||
// to libffi
|
||||
let call_args: Vec<Arg> = args
|
||||
.iter()
|
||||
.map(|a| match a {
|
||||
@ -86,8 +88,11 @@ impl<'module> ScriptHandler for StoreCtrl<'module> {
|
||||
Value::F64(v) => arg(v),
|
||||
})
|
||||
.collect();
|
||||
// We use libffi to call a function with a vector of arguments
|
||||
let call_func: fn() = instance.get_function(func_index);
|
||||
let result: i64 = unsafe { call(CodePtr(call_func as *mut _), &call_args) };
|
||||
|
||||
// We retrieve the return type of the function, and wrap the result with it
|
||||
let signature_index = module.info.functions[func_index].entity;
|
||||
let signature = &module.info.signatures[signature_index];
|
||||
let return_values = if signature.returns.len() > 0 {
|
||||
@ -104,64 +109,16 @@ impl<'module> ScriptHandler for StoreCtrl<'module> {
|
||||
};
|
||||
|
||||
println!(
|
||||
"Function {:?}(index: {:?}) => {:?}",
|
||||
"Function {:?}(index: {:?}) ({:?}) => {:?}",
|
||||
field.to_string(),
|
||||
func_index,
|
||||
call_args,
|
||||
return_values
|
||||
);
|
||||
|
||||
// let result = instance.invoke(func_index, vec![]);
|
||||
return InvokationResult::Vals(return_values);
|
||||
}
|
||||
panic!("module not found");
|
||||
// let x = modu.unwrap();
|
||||
// unimplemented!()
|
||||
// if let Some(m) = &mut self.last_module {
|
||||
// // let function = module.exports.get(field).expect("field not found");
|
||||
// // let mut m = &mut m;
|
||||
// let mut instance = &mut m.instance;
|
||||
// // println!("HEEY {:?}", module.instance);
|
||||
// let x = instance.execute_fn(
|
||||
// &m.module,
|
||||
// &m.compilation,
|
||||
// field,
|
||||
// ).unwrap();
|
||||
// println!("X value {:?}", x);
|
||||
// let res = match x {
|
||||
// InvokeResult::VOID => {
|
||||
// vec![]
|
||||
// },
|
||||
// InvokeResult::I32(v) => vec![Value::I32(v)],
|
||||
// InvokeResult::I64(v) => vec![Value::I64(v)],
|
||||
// InvokeResult::F32(v) => vec![Value::F32(v)],
|
||||
// InvokeResult::F64(v) => vec![Value::F64(v)],
|
||||
// };
|
||||
// InvokationResult::Vals(res)
|
||||
// // unimplemented!()
|
||||
// // InvokationResult::Vals(vec![Value::I32(*x)])
|
||||
// // unimplemented!();
|
||||
// // let result = Rc::try_unwrap(module);
|
||||
// // let mut mutinstance = Rc::make_mut(&module.instance);
|
||||
// // execute_fn(
|
||||
// // &module.module,
|
||||
// // &module.compilation,
|
||||
// // &mut (&mut module.instance),
|
||||
// // field,
|
||||
// // );
|
||||
// }
|
||||
// else {
|
||||
// panic!("module not found");
|
||||
// }
|
||||
// match module {
|
||||
// Some(m) => {
|
||||
// println!("HEEY {:?}", m);
|
||||
// },
|
||||
// _ => unimplemented!()
|
||||
// }
|
||||
// println!("action invoke {}", module.unwrap_or("as".to_string()));
|
||||
// let modul = &self.last_module;
|
||||
// modul.expect("a");
|
||||
//
|
||||
}
|
||||
fn action_get(&mut self, module: Option<String>, field: String) -> Value {
|
||||
// println!("action get");
|
||||
|
@ -1,49 +1,242 @@
|
||||
;; Test `call` operator
|
||||
|
||||
(module
|
||||
(table 0 anyfunc)
|
||||
(memory 0)
|
||||
;; (func $for_2 (; 0 ;) (param $0 i32) (result i32)
|
||||
;; (i32.shl
|
||||
;; (get_local $0)
|
||||
;; (i32.const 1)
|
||||
;; )
|
||||
;; )
|
||||
;; (func (export "main") (; 1 ;) (result i32)
|
||||
;; (call $for_2
|
||||
;; (i32.const 2)
|
||||
;; )
|
||||
;; )
|
||||
;; Auxiliary definitions
|
||||
(func $const-i32 (result i32) (i32.const 0x132))
|
||||
(func $const-i64 (result i64) (i64.const 0x164))
|
||||
(func $const-f32 (result f32) (f32.const 0xf32))
|
||||
(func $const-f64 (result f64) (f64.const 0xf64))
|
||||
|
||||
;; (func $multiply (; 1 ;) (param i32 i32) (result i32)
|
||||
;; (i32.mul
|
||||
;; (get_local 0)
|
||||
;; (get_local 1)
|
||||
;; )
|
||||
;; )
|
||||
(func $id-i32 (param i32) (result i32) (get_local 0))
|
||||
(func $id-i64 (param i64) (result i64) (get_local 0))
|
||||
(func $id-f32 (param f32) (result f32) (get_local 0))
|
||||
(func $id-f64 (param f64) (result f64) (get_local 0))
|
||||
|
||||
;; (func (export "multiply_direct") (; 1 ;) (param $0 i32) (result i32)
|
||||
;; (i32.mul
|
||||
;; (get_local 0)
|
||||
;; (i32.const 3)
|
||||
;; )
|
||||
;; )
|
||||
(func $f32-i32 (param f32 i32) (result i32) (get_local 1))
|
||||
(func $i32-i64 (param i32 i64) (result i64) (get_local 1))
|
||||
(func $f64-f32 (param f64 f32) (result f32) (get_local 1))
|
||||
(func $i64-f64 (param i64 f64) (result f64) (get_local 1))
|
||||
|
||||
;; (func (export "multiply_by_3") (; 1 ;) (param $0 i32) (result i32)
|
||||
;; (call $multiply
|
||||
;; (i32.const 3)
|
||||
;; (get_local $0)
|
||||
;; ;; (i32.const 2)
|
||||
;; )
|
||||
;; )
|
||||
;; Typing
|
||||
|
||||
(func (export "multiply_by_3_raw") (; 1 ;) (param $0 i32) (result i32)
|
||||
(i32.mul
|
||||
(get_local $0)
|
||||
(i32.const 3)
|
||||
(func (export "type-i32") (result i32) (call $const-i32))
|
||||
(func (export "type-i64") (result i64) (call $const-i64))
|
||||
(func (export "type-f32") (result f32) (call $const-f32))
|
||||
(func (export "type-f64") (result f64) (call $const-f64))
|
||||
|
||||
(func (export "type-first-i32") (result i32) (call $id-i32 (i32.const 32)))
|
||||
(func (export "type-first-i64") (result i64) (call $id-i64 (i64.const 64)))
|
||||
(func (export "type-first-f32") (result f32) (call $id-f32 (f32.const 1.32)))
|
||||
(func (export "type-first-f64") (result f64) (call $id-f64 (f64.const 1.64)))
|
||||
|
||||
(func (export "type-second-i32") (result i32)
|
||||
(call $f32-i32 (f32.const 32.1) (i32.const 32))
|
||||
)
|
||||
(func (export "type-second-i64") (result i64)
|
||||
(call $i32-i64 (i32.const 32) (i64.const 64))
|
||||
)
|
||||
(func (export "type-second-f32") (result f32)
|
||||
(call $f64-f32 (f64.const 64) (f32.const 32))
|
||||
)
|
||||
(func (export "type-second-f64") (result f64)
|
||||
(call $i64-f64 (i64.const 64) (f64.const 64.1))
|
||||
)
|
||||
)
|
||||
|
||||
;; Recursion
|
||||
|
||||
(func $fac (export "fac") (param i64) (result i64)
|
||||
(if (result i64) (i64.eqz (get_local 0))
|
||||
(then (i64.const 1))
|
||||
(else
|
||||
(i64.mul
|
||||
(get_local 0)
|
||||
(call $fac (i64.sub (get_local 0) (i64.const 1)))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(func $fac-acc (export "fac-acc") (param i64 i64) (result i64)
|
||||
(if (result i64) (i64.eqz (get_local 0))
|
||||
(then (get_local 1))
|
||||
(else
|
||||
(call $fac-acc
|
||||
(i64.sub (get_local 0) (i64.const 1))
|
||||
(i64.mul (get_local 0) (get_local 1))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(func $fib (export "fib") (param i64) (result i64)
|
||||
(if (result i64) (i64.le_u (get_local 0) (i64.const 1))
|
||||
(then (i64.const 1))
|
||||
(else
|
||||
(i64.add
|
||||
(call $fib (i64.sub (get_local 0) (i64.const 2)))
|
||||
(call $fib (i64.sub (get_local 0) (i64.const 1)))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(func $even (export "even") (param i64) (result i32)
|
||||
(if (result i32) (i64.eqz (get_local 0))
|
||||
(then (i32.const 44))
|
||||
(else (call $odd (i64.sub (get_local 0) (i64.const 1))))
|
||||
)
|
||||
)
|
||||
(func $odd (export "odd") (param i64) (result i32)
|
||||
(if (result i32) (i64.eqz (get_local 0))
|
||||
(then (i32.const 99))
|
||||
(else (call $even (i64.sub (get_local 0) (i64.const 1))))
|
||||
)
|
||||
)
|
||||
|
||||
;; Stack exhaustion
|
||||
|
||||
;; Implementations are required to have every call consume some abstract
|
||||
;; resource towards exhausting some abstract finite limit, such that
|
||||
;; infinitely recursive test cases reliably trap in finite time. This is
|
||||
;; because otherwise applications could come to depend on it on those
|
||||
;; implementations and be incompatible with implementations that don't do
|
||||
;; it (or don't do it under the same circumstances).
|
||||
|
||||
(func $runaway (export "runaway") (call $runaway))
|
||||
|
||||
(func $mutual-runaway1 (export "mutual-runaway") (call $mutual-runaway2))
|
||||
(func $mutual-runaway2 (call $mutual-runaway1))
|
||||
)
|
||||
|
||||
(assert_return (invoke "multiply_by_3_raw" (i32.const 2)) (i32.const 6))
|
||||
(assert_return (invoke "type-i32") (i32.const 0x132))
|
||||
(assert_return (invoke "type-i64") (i64.const 0x164))
|
||||
;; (assert_return (invoke "type-f32") (f32.const 0xf32))
|
||||
;; (assert_return (invoke "type-f64") (f64.const 0xf64))
|
||||
|
||||
;; (assert_return (invoke "multiply_by_3_raw" (i32.const 2)) (i32.const 6))
|
||||
(assert_return (invoke "type-first-i32") (i32.const 32))
|
||||
(assert_return (invoke "type-first-i64") (i64.const 64))
|
||||
;; (assert_return (invoke "type-first-f32") (f32.const 1.32))
|
||||
;; (assert_return (invoke "type-first-f64") (f64.const 1.64))
|
||||
|
||||
(assert_return (invoke "type-second-i32") (i32.const 32))
|
||||
(assert_return (invoke "type-second-i64") (i64.const 64))
|
||||
;; (assert_return (invoke "type-second-f32") (f32.const 32))
|
||||
;; (assert_return (invoke "type-second-f64") (f64.const 64.1))
|
||||
|
||||
(assert_return (invoke "fac" (i64.const 0)) (i64.const 1))
|
||||
(assert_return (invoke "fac" (i64.const 1)) (i64.const 1))
|
||||
(assert_return (invoke "fac" (i64.const 5)) (i64.const 120))
|
||||
(assert_return (invoke "fac" (i64.const 25)) (i64.const 7034535277573963776))
|
||||
(assert_return (invoke "fac-acc" (i64.const 0) (i64.const 1)) (i64.const 1))
|
||||
(assert_return (invoke "fac-acc" (i64.const 1) (i64.const 1)) (i64.const 1))
|
||||
(assert_return (invoke "fac-acc" (i64.const 5) (i64.const 1)) (i64.const 120))
|
||||
(assert_return
|
||||
(invoke "fac-acc" (i64.const 25) (i64.const 1))
|
||||
(i64.const 7034535277573963776)
|
||||
)
|
||||
|
||||
(assert_return (invoke "fib" (i64.const 0)) (i64.const 1))
|
||||
(assert_return (invoke "fib" (i64.const 1)) (i64.const 1))
|
||||
(assert_return (invoke "fib" (i64.const 2)) (i64.const 2))
|
||||
(assert_return (invoke "fib" (i64.const 5)) (i64.const 8))
|
||||
(assert_return (invoke "fib" (i64.const 20)) (i64.const 10946))
|
||||
|
||||
(assert_return (invoke "even" (i64.const 0)) (i32.const 44))
|
||||
(assert_return (invoke "even" (i64.const 1)) (i32.const 99))
|
||||
(assert_return (invoke "even" (i64.const 100)) (i32.const 44))
|
||||
(assert_return (invoke "even" (i64.const 77)) (i32.const 99))
|
||||
(assert_return (invoke "odd" (i64.const 0)) (i32.const 99))
|
||||
(assert_return (invoke "odd" (i64.const 1)) (i32.const 44))
|
||||
(assert_return (invoke "odd" (i64.const 200)) (i32.const 99))
|
||||
(assert_return (invoke "odd" (i64.const 77)) (i32.const 44))
|
||||
|
||||
(assert_exhaustion (invoke "runaway") "call stack exhausted")
|
||||
(assert_exhaustion (invoke "mutual-runaway") "call stack exhausted")
|
||||
|
||||
|
||||
;; Invalid typing
|
||||
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $type-void-vs-num (i32.eqz (call 1)))
|
||||
(func)
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $type-num-vs-num (i32.eqz (call 1)))
|
||||
(func (result i64) (i64.const 1))
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $arity-0-vs-1 (call 1))
|
||||
(func (param i32))
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $arity-0-vs-2 (call 1))
|
||||
(func (param f64 i32))
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $arity-1-vs-0 (call 1 (i32.const 1)))
|
||||
(func)
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $arity-2-vs-0 (call 1 (f64.const 2) (i32.const 1)))
|
||||
(func)
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $type-first-void-vs-num (call 1 (nop) (i32.const 1)))
|
||||
(func (param i32 i32))
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $type-second-void-vs-num (call 1 (i32.const 1) (nop)))
|
||||
(func (param i32 i32))
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $type-first-num-vs-num (call 1 (f64.const 1) (i32.const 1)))
|
||||
(func (param i32 f64))
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
(assert_invalid
|
||||
(module
|
||||
(func $type-second-num-vs-num (call 1 (i32.const 1) (f64.const 1)))
|
||||
(func (param f64 i32))
|
||||
)
|
||||
"type mismatch"
|
||||
)
|
||||
|
||||
|
||||
;; Unbound function
|
||||
|
||||
(assert_invalid
|
||||
(module (func $unbound-func (call 1)))
|
||||
"unknown function"
|
||||
)
|
||||
(assert_invalid
|
||||
(module (func $large-func (call 1012321300)))
|
||||
"unknown function"
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user