Use the WasmRuntime's signature list rather than keeping a separate list.

This way, if the runtime modifies the signature, such as to add special
arguments, they are reflected in the resulting function defintions.
This commit is contained in:
Dan Gohman
2017-10-04 16:55:14 -07:00
parent 196795017b
commit 7410ddfe08
4 changed files with 11 additions and 8 deletions

View File

@@ -39,7 +39,6 @@ pub fn translate_module(
}
ref s => panic!("modules should begin properly: {:?}", s),
}
let mut signatures = None;
let mut functions: Option<Vec<SignatureIndex>> = None;
let mut globals = Vec::new();
let mut exports: Option<HashMap<FunctionIndex, String>> = None;
@@ -50,7 +49,7 @@ pub fn translate_module(
match *parser.read_with_input(next_input) {
ParserState::BeginSection { code: SectionCode::Type, .. } => {
match parse_function_signatures(&mut parser, runtime) {
Ok(sigs) => signatures = Some(sigs),
Ok(()) => (),
Err(SectionParsingError::WrongSectionContent(s)) => {
return Err(format!("wrong content in the type section: {}", s))
}
@@ -192,7 +191,6 @@ pub fn translate_module(
}
// At this point we've entered the code section
// First we check that we have all that is necessary to translate a function.
let signatures = signatures.unwrap_or_default();
let functions = match functions {
None => return Err(String::from("missing a function section")),
Some(functions) => functions,
@@ -209,7 +207,7 @@ 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 = signatures[functions[function_index]].clone();
func.signature = runtime.get_signature(functions[function_index]).clone();
if let Some(ref exports) = exports {
if let Some(name) = exports.get(&function_index) {
func.name = FunctionName::new(name.clone());

View File

@@ -123,6 +123,10 @@ impl WasmRuntime for DummyRuntime {
self.signatures.push(sig.clone());
}
fn get_signature(&self, sig_index: SignatureIndex) -> &ir::Signature {
&self.signatures[sig_index]
}
fn declare_func_import(&mut self, sig_index: SignatureIndex, module: &[u8], field: &[u8]) {
assert_eq!(
self.func_types.len(),

View File

@@ -153,6 +153,9 @@ pub trait WasmRuntime: FuncEnvironment {
/// Declares a function signature to the runtime.
fn declare_signature(&mut self, sig: &ir::Signature);
/// Return the signature with the given index.
fn get_signature(&self, sig_index: SignatureIndex) -> &ir::Signature;
/// Declares a function import to the runtime.
fn declare_func_import(&mut self, sig_index: SignatureIndex, module: &[u8], field: &[u8]);

View File

@@ -27,8 +27,7 @@ pub enum SectionParsingError {
pub fn parse_function_signatures(
parser: &mut Parser,
runtime: &mut WasmRuntime,
) -> Result<Vec<Signature>, SectionParsingError> {
let mut signatures: Vec<Signature> = Vec::new();
) -> Result<(), SectionParsingError> {
loop {
match *parser.read() {
ParserState::EndSection => break,
@@ -51,12 +50,11 @@ pub fn parse_function_signatures(
ArgumentType::new(cret_arg)
}));
runtime.declare_signature(&sig);
signatures.push(sig);
}
ref s => return Err(SectionParsingError::WrongSectionContent(format!("{:?}", s))),
}
}
Ok(signatures)
Ok(())
}
/// Retrieves the imports from the imports section of the binary.