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.
This commit is contained in:
Dan Gohman
2017-10-09 11:41:01 -07:00
parent e74bc06380
commit 2c9d03f9bd
4 changed files with 10 additions and 5 deletions

View File

@@ -36,7 +36,6 @@ pub fn translate_module(
} }
let mut exports: HashMap<FunctionIndex, String> = HashMap::new(); let mut exports: HashMap<FunctionIndex, String> = HashMap::new();
let mut next_input = ParserInput::Default; let mut next_input = ParserInput::Default;
let mut function_index: FunctionIndex = 0;
loop { loop {
match *parser.read_with_input(next_input) { match *parser.read_with_input(next_input) {
ParserState::BeginSection { code: SectionCode::Type, .. } => { ParserState::BeginSection { code: SectionCode::Type, .. } => {
@@ -49,7 +48,7 @@ pub fn translate_module(
next_input = ParserInput::Default; next_input = ParserInput::Default;
} }
ParserState::BeginSection { code: SectionCode::Import, .. } => { ParserState::BeginSection { code: SectionCode::Import, .. } => {
match parse_import_section(&mut parser, runtime, &mut function_index) { match parse_import_section(&mut parser, runtime) {
Ok(()) => {} Ok(()) => {}
Err(SectionParsingError::WrongSectionContent(s)) => { Err(SectionParsingError::WrongSectionContent(s)) => {
return Err(format!("wrong content in the import section: {}", 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 // At this point we've entered the code section
let num_func_imports = runtime.get_num_func_imports();
let mut il_functions: Vec<Function> = Vec::new(); let mut il_functions: Vec<Function> = Vec::new();
let mut trans = FuncTranslator::new(); let mut trans = FuncTranslator::new();
runtime.begin_translation(); runtime.begin_translation();
@@ -151,6 +151,7 @@ pub fn translate_module(
runtime.next_function(); runtime.next_function();
// First we build the Function object with its name and signature // First we build the Function object with its name and signature
let mut func = Function::new(); let mut func = Function::new();
let function_index = num_func_imports + il_functions.len();
func.signature = runtime func.signature = runtime
.get_signature(runtime.get_func_type(function_index)) .get_signature(runtime.get_func_type(function_index))
.clone(); .clone();
@@ -161,7 +162,6 @@ pub fn translate_module(
.translate_from_reader(parser.create_binary_reader(), &mut func, runtime) .translate_from_reader(parser.create_binary_reader(), &mut func, runtime)
.map_err(|e| String::from(e.description()))?; .map_err(|e| String::from(e.description()))?;
il_functions.push(func); il_functions.push(func);
function_index += 1;
} }
loop { loop {
match *parser.read() { match *parser.read() {

View File

@@ -146,6 +146,10 @@ impl WasmRuntime for DummyRuntime {
self.imported_funcs.push(ir::FunctionName::new(name)); 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) { fn declare_func_type(&mut self, sig_index: SignatureIndex) {
self.func_types.push(sig_index); self.func_types.push(sig_index);
} }

View File

@@ -159,6 +159,9 @@ pub trait WasmRuntime: FuncEnvironment {
/// Declares a function import to the runtime. /// Declares a function import to the runtime.
fn declare_func_import(&mut self, sig_index: SignatureIndex, module: &[u8], field: &[u8]); 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. /// Declares the type (signature) of a local function in the module.
fn declare_func_type(&mut self, sig_index: SignatureIndex); fn declare_func_type(&mut self, sig_index: SignatureIndex);

View File

@@ -60,7 +60,6 @@ pub fn parse_function_signatures(
pub fn parse_import_section( pub fn parse_import_section(
parser: &mut Parser, parser: &mut Parser,
runtime: &mut WasmRuntime, runtime: &mut WasmRuntime,
function_index: &mut FunctionIndex,
) -> Result<(), SectionParsingError> { ) -> Result<(), SectionParsingError> {
loop { loop {
match *parser.read() { match *parser.read() {
@@ -70,7 +69,6 @@ pub fn parse_import_section(
field, field,
} => { } => {
runtime.declare_func_import(sig as SignatureIndex, module, field); runtime.declare_func_import(sig as SignatureIndex, module, field);
*function_index += 1;
} }
ParserState::ImportSectionEntry { ParserState::ImportSectionEntry {
ty: ImportSectionEntryType::Memory(MemoryType { limits: ref memlimits }), .. ty: ImportSectionEntryType::Memory(MemoryType { limits: ref memlimits }), ..