From 2c9d03f9bd1b7faaecb7431b139ba51130cdc64d Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 9 Oct 2017 11:41:01 -0700 Subject: [PATCH] Let the runtime provide the number of imported functions. This obviates the need to keep a separate running total of the number of functions seen. --- lib/wasm/src/module_translator.rs | 6 +++--- lib/wasm/src/runtime/dummy.rs | 4 ++++ lib/wasm/src/runtime/spec.rs | 3 +++ lib/wasm/src/sections_translator.rs | 2 -- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/wasm/src/module_translator.rs b/lib/wasm/src/module_translator.rs index baab08b63b..7aee73c974 100644 --- a/lib/wasm/src/module_translator.rs +++ b/lib/wasm/src/module_translator.rs @@ -36,7 +36,6 @@ pub fn translate_module( } let mut exports: HashMap = HashMap::new(); let mut next_input = ParserInput::Default; - let mut function_index: FunctionIndex = 0; loop { match *parser.read_with_input(next_input) { ParserState::BeginSection { code: SectionCode::Type, .. } => { @@ -49,7 +48,7 @@ pub fn translate_module( next_input = ParserInput::Default; } ParserState::BeginSection { code: SectionCode::Import, .. } => { - match parse_import_section(&mut parser, runtime, &mut function_index) { + match parse_import_section(&mut parser, runtime) { Ok(()) => {} Err(SectionParsingError::WrongSectionContent(s)) => { return Err(format!("wrong content in the import section: {}", s)) @@ -139,6 +138,7 @@ pub fn translate_module( }; } // At this point we've entered the code section + let num_func_imports = runtime.get_num_func_imports(); let mut il_functions: Vec = Vec::new(); let mut trans = FuncTranslator::new(); runtime.begin_translation(); @@ -151,6 +151,7 @@ pub fn translate_module( runtime.next_function(); // First we build the Function object with its name and signature let mut func = Function::new(); + let function_index = num_func_imports + il_functions.len(); func.signature = runtime .get_signature(runtime.get_func_type(function_index)) .clone(); @@ -161,7 +162,6 @@ pub fn translate_module( .translate_from_reader(parser.create_binary_reader(), &mut func, runtime) .map_err(|e| String::from(e.description()))?; il_functions.push(func); - function_index += 1; } loop { match *parser.read() { diff --git a/lib/wasm/src/runtime/dummy.rs b/lib/wasm/src/runtime/dummy.rs index 810decce1e..6c7a12c88a 100644 --- a/lib/wasm/src/runtime/dummy.rs +++ b/lib/wasm/src/runtime/dummy.rs @@ -146,6 +146,10 @@ impl WasmRuntime for DummyRuntime { self.imported_funcs.push(ir::FunctionName::new(name)); } + fn get_num_func_imports(&self) -> usize { + self.imported_funcs.len() + } + fn declare_func_type(&mut self, sig_index: SignatureIndex) { self.func_types.push(sig_index); } diff --git a/lib/wasm/src/runtime/spec.rs b/lib/wasm/src/runtime/spec.rs index 8c11901f19..f914f642d4 100644 --- a/lib/wasm/src/runtime/spec.rs +++ b/lib/wasm/src/runtime/spec.rs @@ -159,6 +159,9 @@ pub trait WasmRuntime: FuncEnvironment { /// Declares a function import to the runtime. fn declare_func_import(&mut self, sig_index: SignatureIndex, module: &[u8], field: &[u8]); + /// Return the number of imported funcs. + fn get_num_func_imports(&self) -> usize; + /// Declares the type (signature) of a local function in the module. fn declare_func_type(&mut self, sig_index: SignatureIndex); diff --git a/lib/wasm/src/sections_translator.rs b/lib/wasm/src/sections_translator.rs index 04338af3e1..f0d5810253 100644 --- a/lib/wasm/src/sections_translator.rs +++ b/lib/wasm/src/sections_translator.rs @@ -60,7 +60,6 @@ pub fn parse_function_signatures( pub fn parse_import_section( parser: &mut Parser, runtime: &mut WasmRuntime, - function_index: &mut FunctionIndex, ) -> Result<(), SectionParsingError> { loop { match *parser.read() { @@ -70,7 +69,6 @@ pub fn parse_import_section( field, } => { runtime.declare_func_import(sig as SignatureIndex, module, field); - *function_index += 1; } ParserState::ImportSectionEntry { ty: ImportSectionEntryType::Memory(MemoryType { limits: ref memlimits }), ..