()
| 675 | |
| 676 | #[test] |
| 677 | fn fuel() { |
| 678 | let code = "function %test() -> i8 { |
| 679 | block0: |
| 680 | v0 = iconst.i32 1 |
| 681 | v2 = iconst.i32 1 |
| 682 | v1 = iadd v0, v2 |
| 683 | return v1 |
| 684 | }"; |
| 685 | |
| 686 | let func = parse_functions(code).unwrap().into_iter().next().unwrap(); |
| 687 | let mut env = FunctionStore::default(); |
| 688 | env.add(func.name.to_string(), &func); |
| 689 | |
| 690 | // The default interpreter should not enable the fuel mechanism |
| 691 | let state = InterpreterState::default().with_function_store(env.clone()); |
| 692 | let result = Interpreter::new(state).call_by_name("%test", &[]).unwrap(); |
| 693 | |
| 694 | assert_eq!(result, ControlFlow::Return(smallvec![DataValue::I32(2)])); |
| 695 | |
| 696 | // With 3 fuel, we should execute the two iconsts and the iadd, but not the return thus |
| 697 | // giving a fuel exhausted error |
| 698 | let state = InterpreterState::default().with_function_store(env.clone()); |
| 699 | let result = Interpreter::new(state) |
| 700 | .with_fuel(Some(3)) |
| 701 | .call_by_name("%test", &[]); |
| 702 | match result { |
| 703 | Err(InterpreterError::FuelExhausted) => {} |
| 704 | _ => panic!("Expected Err(FuelExhausted), but got {result:?}"), |
| 705 | } |
| 706 | |
| 707 | // With 4 fuel, we should be able to execute the return instruction, and complete the test |
| 708 | let state = InterpreterState::default().with_function_store(env.clone()); |
| 709 | let result = Interpreter::new(state) |
| 710 | .with_fuel(Some(4)) |
| 711 | .call_by_name("%test", &[]) |
| 712 | .unwrap(); |
| 713 | |
| 714 | assert_eq!(result, ControlFlow::Return(smallvec![DataValue::I32(2)])); |
| 715 | } |
| 716 | |
| 717 | // Verifies that writing to the stack on a called function does not overwrite the parents |
| 718 | // stack slots. |
nothing calls this directly
no test coverage detected