From bf791efe2c623b1b20053e8e39c3aa4e8eb60a1c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 20 Aug 2018 16:18:09 -0700 Subject: [PATCH] Fix wasm-interpreter with mixed types of imports We counted all imports until the index of the descriptor function, now we only count imported functions --- crates/wasm-interpreter/src/lib.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/wasm-interpreter/src/lib.rs b/crates/wasm-interpreter/src/lib.rs index bac963908..da397f3d9 100644 --- a/crates/wasm-interpreter/src/lib.rs +++ b/crates/wasm-interpreter/src/lib.rs @@ -92,14 +92,19 @@ impl Interpreter { // function. if let Some(i) = module.import_section() { ret.imports = i.functions(); - for (i, entry) in i.entries().iter().enumerate() { + let mut idx = 0; + for entry in i.entries() { + match entry.external() { + External::Function(_) => idx += 1, + _ => continue, + } if entry.module() != "__wbindgen_placeholder__" { continue } if entry.field() != "__wbindgen_describe" { continue } - ret.describe_idx = Some(i as u32); + ret.describe_idx = Some(idx - 1 as u32); } } @@ -184,6 +189,13 @@ impl Interpreter { SetLocal(i) => locals[*i as usize] = self.stack.pop().unwrap(), GetLocal(i) => self.stack.push(locals[*i as usize]), Call(idx) => { + // If this function is calling the `__wbindgen_describe` + // function, which we've precomputed the index for, then + // it's telling us about the next `u32` element in the + // descriptor to return. We "call" the imported function + // here by directly inlining it. + // + // Otherwise this is a normal call so we recurse. if Some(*idx) == self.describe_idx { self.descriptor.push(self.stack.pop().unwrap() as u32); } else {