diff --git a/lib/wasm/src/module_translator.rs b/lib/wasm/src/module_translator.rs index 001a85bd69..2715aeb394 100644 --- a/lib/wasm/src/module_translator.rs +++ b/lib/wasm/src/module_translator.rs @@ -5,7 +5,7 @@ use sections_translator::{SectionParsingError, parse_function_signatures, parse_ parse_function_section, parse_export_section, parse_memory_section, parse_global_section, parse_table_section, parse_elements_section, parse_data_section}; -use translation_utils::{Import, SignatureIndex, FunctionIndex}; +use translation_utils::{Import, FunctionIndex}; use cretonne::ir::{Function, FunctionName}; use func_translator::FuncTranslator; use std::collections::HashMap; @@ -39,7 +39,6 @@ pub fn translate_module( } ref s => panic!("modules should begin properly: {:?}", s), } - let mut functions: Vec = Vec::new(); let mut globals = Vec::new(); let mut exports: HashMap = HashMap::new(); let mut next_input = ParserInput::Default; @@ -62,7 +61,6 @@ pub fn translate_module( for import in imps { match import { Import::Function { sig_index } => { - functions.push(sig_index as SignatureIndex); function_index += 1; } Import::Memory(mem) => { @@ -86,9 +84,7 @@ pub fn translate_module( } ParserState::BeginSection { code: SectionCode::Function, .. } => { match parse_function_section(&mut parser, runtime) { - Ok(funcs) => { - functions.extend(funcs); - } + Ok(()) => {} Err(SectionParsingError::WrongSectionContent(s)) => { return Err(format!("wrong content in the function section: {}", s)) } @@ -193,7 +189,9 @@ pub fn translate_module( runtime.next_function(); // First we build the Function object with its name and signature let mut func = Function::new(); - func.signature = runtime.get_signature(functions[function_index]).clone(); + func.signature = runtime + .get_signature(runtime.get_func_type(function_index)) + .clone(); if let Some(name) = exports.get(&function_index) { func.name = FunctionName::new(name.clone()); } diff --git a/lib/wasm/src/runtime/dummy.rs b/lib/wasm/src/runtime/dummy.rs index a2f58fb709..79921b7101 100644 --- a/lib/wasm/src/runtime/dummy.rs +++ b/lib/wasm/src/runtime/dummy.rs @@ -146,6 +146,10 @@ impl WasmRuntime for DummyRuntime { self.func_types.push(sig_index); } + fn get_func_type(&self, func_index: FunctionIndex) -> SignatureIndex { + self.func_types[func_index] + } + fn declare_global(&mut self, global: Global) { self.globals.push(global); } diff --git a/lib/wasm/src/runtime/spec.rs b/lib/wasm/src/runtime/spec.rs index 74bc27770a..5a67980d1b 100644 --- a/lib/wasm/src/runtime/spec.rs +++ b/lib/wasm/src/runtime/spec.rs @@ -162,6 +162,9 @@ pub trait WasmRuntime: FuncEnvironment { /// Declares the type (signature) of a local function in the module. fn declare_func_type(&mut self, sig_index: SignatureIndex); + /// Return the signature index for the given function index. + fn get_func_type(&self, func_index: FunctionIndex) -> SignatureIndex; + /// Declares a global to the runtime. fn declare_global(&mut self, global: Global); /// Declares a table to the runtime. diff --git a/lib/wasm/src/sections_translator.rs b/lib/wasm/src/sections_translator.rs index 8329220a21..ea3c0e97d7 100644 --- a/lib/wasm/src/sections_translator.rs +++ b/lib/wasm/src/sections_translator.rs @@ -113,19 +113,17 @@ pub fn parse_import_section( pub fn parse_function_section( parser: &mut Parser, runtime: &mut WasmRuntime, -) -> Result, SectionParsingError> { - let mut funcs = Vec::new(); +) -> Result<(), SectionParsingError> { loop { match *parser.read() { ParserState::FunctionSectionEntry(sigindex) => { runtime.declare_func_type(sigindex as SignatureIndex); - funcs.push(sigindex as SignatureIndex); } ParserState::EndSection => break, ref s => return Err(SectionParsingError::WrongSectionContent(format!("{:?}", s))), }; } - Ok(funcs) + Ok(()) } /// Retrieves the names of the functions from the export section