feat: allow WitnessCalculator construction from wasmer::Module (#24)

* Allow construction from Module

* Update src/witness/witness_calculator.rs

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
This commit is contained in:
Remco Bloemen 2022-03-19 11:21:03 -07:00 committed by GitHub
parent d0478d1538
commit a93c8b03d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -54,25 +54,34 @@ fn to_array32(s: &BigInt, size: usize) -> Vec<u32> {
impl WitnessCalculator { impl WitnessCalculator {
pub fn new(path: impl AsRef<std::path::Path>) -> Result<Self> { pub fn new(path: impl AsRef<std::path::Path>) -> Result<Self> {
Self::from_file(path)
}
pub fn from_file(path: impl AsRef<std::path::Path>) -> Result<Self> {
let store = Store::default(); let store = Store::default();
let module = Module::from_file(&store, path)?; let module = Module::from_file(&store, path)?;
Self::from_module(module)
}
pub fn from_module(module: Module) -> Result<Self> {
let store = module.store();
// Set up the memory // Set up the memory
let memory = Memory::new(&store, MemoryType::new(2000, None, false)).unwrap(); let memory = Memory::new(store, MemoryType::new(2000, None, false)).unwrap();
let import_object = imports! { let import_object = imports! {
"env" => { "env" => {
"memory" => memory.clone(), "memory" => memory.clone(),
}, },
// Host function callbacks from the WASM // Host function callbacks from the WASM
"runtime" => { "runtime" => {
"error" => runtime::error(&store), "error" => runtime::error(store),
"logSetSignal" => runtime::log_signal(&store), "logSetSignal" => runtime::log_signal(store),
"logGetSignal" => runtime::log_signal(&store), "logGetSignal" => runtime::log_signal(store),
"logFinishComponent" => runtime::log_component(&store), "logFinishComponent" => runtime::log_component(store),
"logStartComponent" => runtime::log_component(&store), "logStartComponent" => runtime::log_component(store),
"log" => runtime::log_component(&store), "log" => runtime::log_component(store),
"exceptionHandler" => runtime::exception_handler(&store), "exceptionHandler" => runtime::exception_handler(store),
"showSharedRWMemory" => runtime::show_memory(&store), "showSharedRWMemory" => runtime::show_memory(store),
} }
}; };
let instance = Wasm::new(Instance::new(&module, &import_object)?); let instance = Wasm::new(Instance::new(&module, &import_object)?);