diff --git a/lib/wasm/src/module_translator.rs b/lib/wasm/src/module_translator.rs index 907a92660b..023d14dba1 100644 --- a/lib/wasm/src/module_translator.rs +++ b/lib/wasm/src/module_translator.rs @@ -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; } diff --git a/lib/wasm/src/sections_translator.rs b/lib/wasm/src/sections_translator.rs index c17b0e680b..6bfc55372b 100644 --- a/lib/wasm/src/sections_translator.rs +++ b/lib/wasm/src/sections_translator.rs @@ -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(()) +}