integration: instantiate the module in prepare

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus
2024-04-16 20:06:34 +02:00
parent 8a6d43bcd3
commit 796d0c19bb
3 changed files with 261 additions and 21 deletions
+7 -6
View File
@@ -5,8 +5,8 @@ use std::collections::HashMap;
use alloy_primitives::{Keccak256, U256};
use parity_scale_codec::Encode;
use polkavm::{
Caller, Config, Engine, ExportIndex, GasMeteringKind, InstancePre, Linker, Module,
ModuleConfig, ProgramBlob, Trap,
Caller, Config, Engine, ExportIndex, GasMeteringKind, Instance, Linker, Module, ModuleConfig,
ProgramBlob, Trap,
};
#[derive(Default, Clone, Debug)]
@@ -192,7 +192,7 @@ fn link_host_functions(engine: &Engine) -> Linker<State> {
linker
}
pub fn prepare(code: &[u8], config: Option<Config>) -> (InstancePre<State>, ExportIndex) {
pub fn prepare(code: &[u8], config: Option<Config>) -> (Instance<State>, ExportIndex) {
let blob = ProgramBlob::parse(code).unwrap();
let engine = Engine::new(&config.unwrap_or_default()).unwrap();
@@ -205,11 +205,12 @@ pub fn prepare(code: &[u8], config: Option<Config>) -> (InstancePre<State>, Expo
let func = link_host_functions(&engine)
.instantiate_pre(&module)
.unwrap();
let instance = func.instantiate().unwrap();
(func, export)
(instance, export)
}
pub fn call(mut state: State, on: &InstancePre<State>, export: ExportIndex) -> State {
pub fn call(mut state: State, on: &mut Instance<State>, export: ExportIndex) -> State {
state.reset_output();
let mut state_args = polkavm::StateArgs::default();
@@ -219,7 +220,7 @@ pub fn call(mut state: State, on: &InstancePre<State>, export: ExportIndex) -> S
init_logs();
match on.instantiate().unwrap().call(state_args, call_args) {
match on.call(state_args, call_args) {
Err(polkavm::ExecutionError::Trap(_)) => state,
Err(other) => panic!("unexpected error: {other}"),
Ok(_) => panic!("unexpected return"),