Move the code section parsing into its own function;

This commit is contained in:
Benjamin Bouvier
2018-07-19 14:56:58 +02:00
committed by Dan Gohman
parent 03159a9200
commit 7b290cd900
2 changed files with 25 additions and 16 deletions

View File

@@ -3,9 +3,9 @@
use cranelift_codegen::timing;
use environ::{ModuleEnvironment, WasmError, WasmResult};
use sections_translator::{
parse_data_section, parse_elements_section, parse_export_section, parse_function_section,
parse_function_signatures, parse_global_section, parse_import_section, parse_memory_section,
parse_start_section, parse_table_section,
parse_code_section, parse_data_section, parse_elements_section, parse_export_section,
parse_function_section, parse_function_signatures, parse_global_section, parse_import_section,
parse_memory_section, parse_start_section, parse_table_section,
};
use wasmparser::{Parser, ParserInput, ParserState, SectionCode, WasmDecoder};
@@ -94,19 +94,7 @@ pub fn translate_module<'data>(
ParserState::BeginSection {
code: SectionCode::Code,
..
} => loop {
match *parser.read() {
ParserState::BeginFunctionBody { .. } => {}
ParserState::EndSection => break,
ParserState::Error(e) => return Err(WasmError::from_binary_reader_error(e)),
ref s => panic!("wrong content in code section: {:?}", s),
}
let mut reader = parser.create_binary_reader();
let size = reader.bytes_remaining();
environ.define_function_body(reader
.read_bytes(size)
.map_err(WasmError::from_binary_reader_error)?)?;
},
} => parse_code_section(&mut parser, environ)?,
ParserState::EndSection => {
next_input = ParserInput::Default;
}

View File

@@ -397,3 +397,24 @@ pub fn parse_elements_section(
}
Ok(())
}
/// Parses every function body in the code section and defines the corresponding function.
pub fn parse_code_section<'data>(
parser: &mut Parser<'data>,
environ: &mut ModuleEnvironment<'data>,
) -> WasmResult<()> {
loop {
match *parser.read() {
ParserState::BeginFunctionBody { .. } => {}
ParserState::EndSection => break,
ParserState::Error(e) => return Err(WasmError::from_binary_reader_error(e)),
ref s => panic!("wrong content in code section: {:?}", s),
}
let mut reader = parser.create_binary_reader();
let size = reader.bytes_remaining();
environ.define_function_body(reader
.read_bytes(size)
.map_err(WasmError::from_binary_reader_error)?)?;
}
Ok(())
}