From 8fb705a1ef43c33e595154ccdb6b6d2304b3b699 Mon Sep 17 00:00:00 2001
From: Alex Crichton <alex@alexcrichton.com>
Date: Tue, 19 Feb 2019 13:07:00 -0800
Subject: [PATCH] Upgrade to walrus 0.4

Also be sure to have an explicit GC pass!
---
 crates/cli-support/Cargo.toml          |  2 +-
 crates/cli-support/src/js/closures.rs  |  1 +
 crates/cli-support/src/js/mod.rs       | 14 +++++---------
 crates/cli-support/src/lib.rs          |  2 +-
 crates/cli/Cargo.toml                  |  2 +-
 crates/threads-xform/Cargo.toml        |  2 +-
 crates/wasm-interpreter/Cargo.toml     |  3 ++-
 crates/wasm-interpreter/src/lib.rs     | 17 ++++++++---------
 crates/wasm-interpreter/tests/smoke.rs |  2 +-
 9 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/crates/cli-support/Cargo.toml b/crates/cli-support/Cargo.toml
index 85c56055..7c84aa87 100644
--- a/crates/cli-support/Cargo.toml
+++ b/crates/cli-support/Cargo.toml
@@ -17,7 +17,7 @@ failure = "0.1.2"
 log = "0.4"
 rustc-demangle = "0.1.13"
 tempfile = "3.0"
-walrus = "0.2.1"
+walrus = "0.4.0"
 wasm-bindgen-shared = { path = "../shared", version = '=0.2.37' }
 wasm-bindgen-threads-xform = { path = '../threads-xform', version = '=0.2.37' }
 wasm-bindgen-wasm-interpreter = { path = "../wasm-interpreter", version = '=0.2.37' }
diff --git a/crates/cli-support/src/js/closures.rs b/crates/cli-support/src/js/closures.rs
index 92575307..24d5e932 100644
--- a/crates/cli-support/src/js/closures.rs
+++ b/crates/cli-support/src/js/closures.rs
@@ -142,6 +142,7 @@ impl ClosureDescriptors {
         let table = input.module.tables.get_mut(table_id);
         let table = match &mut table.kind {
             walrus::TableKind::Function(f) => f,
+            walrus::TableKind::Anyref(_) => unreachable!(),
         };
         for idx in self.element_removal_list.iter().cloned() {
             log::trace!("delete element {}", idx);
diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs
index fc0bea8b..f77e4100 100644
--- a/crates/cli-support/src/js/mod.rs
+++ b/crates/cli-support/src/js/mod.rs
@@ -536,6 +536,8 @@ impl<'a> Context<'a> {
 
         self.export_table()?;
 
+        walrus::passes::gc::run(self.module);
+
         // Note that it's important `throw` comes last *after* we gc. The
         // `__wbindgen_malloc` function may call this but we only want to
         // generate code for this if it's actually live (and __wbindgen_malloc
@@ -871,16 +873,10 @@ impl<'a> Context<'a> {
         if !self.function_table_needed {
             return Ok(());
         }
-        let mut tables = self.module.tables.iter().filter_map(|t| match t.kind {
-            walrus::TableKind::Function(_) => Some(t.id()),
-        });
-        let id = match tables.next() {
+        let id = match self.module.tables.main_function_table()? {
             Some(id) => id,
-            None => return Ok(()),
+            None => bail!("no function table found in module"),
         };
-        if tables.next().is_some() {
-            bail!("couldn't find function table to export");
-        }
         self.module.exports.add("__wbg_function_table", id);
         Ok(())
     }
@@ -976,7 +972,7 @@ impl<'a> Context<'a> {
             }
         }
         for id in to_remove {
-            self.module.exports.remove_root(id);
+            self.module.exports.delete(id);
         }
     }
 
diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs
index 49b04f0a..06c44c1d 100755
--- a/crates/cli-support/src/lib.rs
+++ b/crates/cli-support/src/lib.rs
@@ -202,7 +202,7 @@ impl Bindgen {
         // This means that whenever we encounter an import or export we'll
         // execute a shim function which informs us about its type so we can
         // then generate the appropriate bindings.
-        let mut instance = wasm_bindgen_wasm_interpreter::Interpreter::new(&module);
+        let mut instance = wasm_bindgen_wasm_interpreter::Interpreter::new(&module)?;
 
         let mut memories = module.memories.iter().map(|m| m.id());
         let memory = memories.next();
diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml
index 5335ac2e..f6a4794b 100644
--- a/crates/cli/Cargo.toml
+++ b/crates/cli/Cargo.toml
@@ -24,7 +24,7 @@ rouille = { version = "3.0.0", default-features = false }
 serde = { version = "1.0", features = ['derive'] }
 serde_derive = "1.0"
 serde_json = "1.0"
-walrus = "0.2"
+walrus = "0.4"
 wasm-bindgen-cli-support = { path = "../cli-support", version = "=0.2.37" }
 wasm-bindgen-shared = { path = "../shared", version = "=0.2.37" }
 
diff --git a/crates/threads-xform/Cargo.toml b/crates/threads-xform/Cargo.toml
index 39f20589..ff3a99d4 100644
--- a/crates/threads-xform/Cargo.toml
+++ b/crates/threads-xform/Cargo.toml
@@ -12,5 +12,5 @@ Support for threading-related transformations in wasm-bindgen
 edition = "2018"
 
 [dependencies]
-walrus = "0.2"
 failure = "0.1"
+walrus = "0.4"
diff --git a/crates/wasm-interpreter/Cargo.toml b/crates/wasm-interpreter/Cargo.toml
index 6ed5d8f4..4054b51a 100644
--- a/crates/wasm-interpreter/Cargo.toml
+++ b/crates/wasm-interpreter/Cargo.toml
@@ -12,8 +12,9 @@ Micro-interpreter optimized for wasm-bindgen's use case
 edition = '2018'
 
 [dependencies]
-walrus = "0.2"
+failure = "0.1"
 log = "0.4"
+walrus = "0.4"
 
 [dev-dependencies]
 tempfile = "3"
diff --git a/crates/wasm-interpreter/src/lib.rs b/crates/wasm-interpreter/src/lib.rs
index 645de6fb..723a93cd 100644
--- a/crates/wasm-interpreter/src/lib.rs
+++ b/crates/wasm-interpreter/src/lib.rs
@@ -64,7 +64,7 @@ impl Interpreter {
     ///
     /// Note that the `module` passed in to this function must be the same as
     /// the `module` passed to `interpret` below.
-    pub fn new(module: &Module) -> Interpreter {
+    pub fn new(module: &Module) -> Result<Interpreter, failure::Error> {
         let mut ret = Interpreter::default();
 
         // The descriptor functions shouldn't really use all that much memory
@@ -102,14 +102,9 @@ impl Interpreter {
             ret.name_map.insert(export.name.to_string(), id);
         }
 
-        for table in module.tables.iter() {
-            match table.kind {
-                walrus::TableKind::Function(_) => {}
-            }
-            ret.functions = Some(table.id());
-        }
+        ret.functions = module.tables.main_function_table()?;
 
-        return ret;
+        return Ok(ret);
     }
 
     /// Interprets the execution of the descriptor function `func`.
@@ -204,6 +199,7 @@ impl Interpreter {
         let functions = self.functions.expect("function table should be present");
         let functions = match &module.tables.get(functions).kind {
             walrus::TableKind::Function(f) => f,
+            _ => unreachable!(),
         };
         let descriptor_id = functions
             .elements
@@ -348,8 +344,11 @@ impl Frame<'_> {
 
             Expr::WithSideEffects(e) => {
                 log::debug!("side effects");
+                for x in e.before.iter() {
+                    self.eval(*x);
+                }
                 let ret = self.eval(e.value);
-                for x in e.side_effects.iter() {
+                for x in e.after.iter() {
                     self.eval(*x);
                 }
                 return ret;
diff --git a/crates/wasm-interpreter/tests/smoke.rs b/crates/wasm-interpreter/tests/smoke.rs
index 79af91fb..8bfbfa8c 100644
--- a/crates/wasm-interpreter/tests/smoke.rs
+++ b/crates/wasm-interpreter/tests/smoke.rs
@@ -16,7 +16,7 @@ fn interpret(wat: &str, name: &str, result: Option<&[u32]>) {
     println!("status: {}", status);
     assert!(status.success());
     let module = walrus::Module::from_file(output.path()).unwrap();
-    let mut i = Interpreter::new(&module);
+    let mut i = Interpreter::new(&module).unwrap();
     assert_eq!(i.interpret_descriptor(name, &module), result);
 }