From 0f3833fecbec6fd832913e4ec25a5a5fba50ca1a Mon Sep 17 00:00:00 2001 From: Lachlan Sneff Date: Thu, 27 Dec 2018 00:07:48 -0500 Subject: [PATCH] Add support for the start function --- src/runtime/instance.rs | 56 ++++++++++++++++++++++++++--------------- src/runtime/module.rs | 2 +- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/runtime/instance.rs b/src/runtime/instance.rs index 14d5cc827..a1d9cb537 100644 --- a/src/runtime/instance.rs +++ b/src/runtime/instance.rs @@ -5,7 +5,7 @@ use crate::runtime::{ module::{Export, Module}, sig_registry::SigRegistry, table::TableBacking, - types::{FuncSig, Memory, Table, Type, Val}, + types::{FuncIndex, FuncSig, Memory, Table, Type, Val}, vm, }; use hashbrown::HashMap; @@ -27,31 +27,27 @@ impl Instance { let import_backing = ImportBacking::new(&*module, imports)?; let backing = LocalBacking::new(&*module, &import_backing, &sig_registry); - Ok(Box::new(Instance { + let start_func = module.start_func; + + let mut instance = Box::new(Instance { backing, import_backing, module, sig_registry, - })) + }); + + if let Some(start_index) = start_func { + instance.call_with_index(start_index, &[])?; + } + + Ok(instance) } - /// Call an exported webassembly function given the export name. - /// Pass arguments by wrapping each one in the `Val` enum. - /// The returned value is also returned in a `Val`. - /// - /// This will eventually return `Result>, String>` in - /// order to support multi-value returns. - pub fn call(&mut self, name: &str, args: &[Val]) -> Result, String> { - let func_index = *self - .module - .exports - .get(name) - .ok_or_else(|| "there is no export with that name".to_string()) - .and_then(|export| match export { - Export::Func(func_index) => Ok(func_index), - _ => Err("that export is not a function".to_string()), - })?; - + fn call_with_index( + &mut self, + func_index: FuncIndex, + args: &[Val], + ) -> Result, String> { // Check the function signature. let sig_index = *self .module @@ -115,6 +111,26 @@ impl Instance { }) }) } + + /// Call an exported webassembly function given the export name. + /// Pass arguments by wrapping each one in the `Val` enum. + /// The returned value is also returned in a `Val`. + /// + /// This will eventually return `Result>, String>` in + /// order to support multi-value returns. + pub fn call(&mut self, name: &str, args: &[Val]) -> Result, String> { + let func_index = *self + .module + .exports + .get(name) + .ok_or_else(|| "there is no export with that name".to_string()) + .and_then(|export| match export { + Export::Func(func_index) => Ok(func_index), + _ => Err("that export is not a function".to_string()), + })?; + + self.call_with_index(func_index, args) + } } #[derive(Debug)] diff --git a/src/runtime/module.rs b/src/runtime/module.rs index 3894e6142..707684682 100644 --- a/src/runtime/module.rs +++ b/src/runtime/module.rs @@ -24,7 +24,7 @@ pub struct Module { pub data_initializers: Vec, pub table_initializers: Vec, - pub start_func: FuncIndex, + pub start_func: Option, pub signature_assoc: Map, pub signatures: Map,