message: Port a metering test from the old repo.
diff: @@ -87,3 +87,82 @@ fn loop_twice() -> Result<()> {
  assert!(run_loop(18, 2).is_err());
  Ok(())
  }
  +
  +/// Ported from https://github.com/wasmerio/wasmer/blob/master/tests/middleware_common.rs
  +#[test]
  +fn complex_loop() -> Result<()> {
  + // Assemblyscript
  + // export function add_to(x: i32, y: i32): i32 {
  + // for(var i = 0; i < x; i++){
  + // if(i % 1 == 0){
  + // y += i;
  + // } else {
  + // y *= i
  + // }
  + // }
  + // return y;
  + // }
  + static WAT: &'static str = r#"
  + (module
  + (type $t0 (func (param i32 i32) (result i32)))
  + (type $t1 (func))
  + (func $add_to (export "add_to") (type $t0) (param $p0 i32) (param $p1 i32) (result i32)
  + (local $l0 i32)
  + block $B0
  + i32.const 0
  + set_local $l0
  + loop $L1
  + get_local $l0
  + get_local $p0
  + i32.lt_s
  + i32.eqz
  + br_if $B0
  + get_local $l0
  + i32.const 1
  + i32.rem_s
  + i32.const 0
  + i32.eq
  + if $I2
  + get_local $p1
  + get_local $l0
  + i32.add
  + set_local $p1
  + else
  + get_local $p1
  + get_local $l0
  + i32.mul
  + set_local $p1
  + end
  + get_local $l0
  + i32.const 1
  + i32.add
  + set_local $l0
  + br $L1
  + unreachable
  + end
  + unreachable
  + end
  + get_local $p1)
  + (func $f1 (type $t1))
  + (table $table (export "table") 1 anyfunc)
  + (memory $memory (export "memory") 0)
  + (global $g0 i32 (i32.const 8))
  + (elem (i32.const 0) $f1))
  + "#;
  + let store = get_store_with_middlewares(std::iter::once(Arc::new(Metering::new(
  + 100,
  + cost_always_one,
  + )) as Arc<dyn ModuleMiddleware>));
  + let module = Module::new(&store, WAT).unwrap();
  +
  + let import_object = imports! {};
  +
  + let instance = Instance::new(&module, &import_object)?;
  +
  + let f: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("add_to")?;
  +
  + // FIXME: Since now a metering error is signaled with an `unreachable`, it is impossible to verify
  + // the error type. Fix this later.
  + f.call(10_000_000, 4).unwrap_err();
  + Ok(())
  +}
  