Migrate wasm-bindgen to using walrus

This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.

The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.

Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)

It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.

Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!

Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
This commit is contained in:
Alex Crichton
2019-01-31 09:54:23 -08:00
parent c30dbc3179
commit 894b479213
74 changed files with 1007 additions and 3522 deletions

View File

@@ -1,73 +0,0 @@
use std::mem;
type T = usize;
const BITS: usize = mem::size_of::<T>() * 8;
pub struct BitSet {
bits: Vec<T>,
}
impl BitSet {
pub fn new() -> BitSet {
BitSet { bits: Vec::new() }
}
pub fn insert(&mut self, i: u32) -> bool {
let i = i as usize;
let idx = i / BITS;
let bit = 1 << (i % BITS);
if self.bits.len() <= idx {
self.bits.resize(idx + 1, 0);
}
let slot = &mut self.bits[idx];
if *slot & bit != 0 {
false
} else {
*slot |= bit;
true
}
}
pub fn remove(&mut self, i: &u32) {
let i = *i as usize;
let idx = i / BITS;
let bit = 1 << (i % BITS);
if let Some(slot) = self.bits.get_mut(idx) {
*slot &= !bit;
}
}
pub fn contains(&self, i: &u32) -> bool {
let i = *i as usize;
let idx = i / BITS;
let bit = 1 << (i % BITS);
self.bits.get(idx).map(|x| *x & bit != 0).unwrap_or(false)
}
}
impl Default for BitSet {
fn default() -> BitSet {
BitSet::new()
}
}
#[cfg(test)]
mod tests {
use super::BitSet;
#[test]
fn simple() {
let mut x = BitSet::new();
assert!(!x.contains(&1));
assert!(!x.contains(&0));
assert!(!x.contains(&3));
assert!(x.insert(3));
assert!(x.contains(&3));
assert!(!x.insert(3));
assert!(x.contains(&3));
assert!(!x.contains(&1));
assert!(x.insert(2));
assert!(x.contains(&2));
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,146 +0,0 @@
extern crate parity_wasm;
extern crate rayon;
extern crate tempfile;
extern crate wasm_bindgen_gc;
use std::env;
use std::error::Error;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use parity_wasm::elements::Module;
use rayon::prelude::*;
use tempfile::NamedTempFile;
struct Test {
input: PathBuf,
}
fn main() {
let mut tests = Vec::new();
find_tests(&mut tests, "tests/wat".as_ref());
run_tests(&tests);
}
fn find_tests(tests: &mut Vec<Test>, path: &Path) {
for entry in path.read_dir().unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if entry.file_type().unwrap().is_dir() {
find_tests(tests, &path);
continue;
}
if path.extension().and_then(|s| s.to_str()) == Some("wat") {
tests.push(Test { input: path });
}
}
}
fn run_tests(tests: &[Test]) {
println!("");
let results = tests
.par_iter()
.map(|test| run_test(test).map_err(|e| (test, e.to_string())))
.collect::<Vec<_>>();
let mut bad = false;
for result in results {
let (test, err) = match result {
Ok(()) => continue,
Err(p) => p,
};
println!("fail: {} - {}", test.input.display(), err);
bad = true;
}
if bad {
std::process::exit(2);
}
println!("\nall good!");
}
fn run_test(test: &Test) -> Result<(), Box<Error>> {
println!("test {}", test.input.display());
let f = NamedTempFile::new()?;
let input = fs::read_to_string(&test.input)?;
let expected = extract_expected(&input);
let status = Command::new("wat2wasm")
.arg("--debug-names")
.arg("--enable-bulk-memory")
.arg(&test.input)
.arg("-o")
.arg(f.path())
.status()?;
if !status.success() {
return Err(io::Error::new(io::ErrorKind::Other, "failed to run wat2wasm").into());
}
let wasm = fs::read(f.path())?;
let mut module: Module = parity_wasm::deserialize_buffer(&wasm)?;
module = match module.parse_names() {
Ok(m) => m,
Err((_, m)) => m,
};
wasm_bindgen_gc::Config::new().run(&mut module);
let wasm = parity_wasm::serialize(module)?;
fs::write(f.path(), wasm)?;
let status = Command::new("wasm2wat")
.arg("--enable-bulk-memory")
.arg(&f.path())
.stderr(Stdio::inherit())
.output()?;
if !status.status.success() {
return Err(io::Error::new(io::ErrorKind::Other, "failed to run wasm2wat").into());
}
let actual = String::from_utf8(status.stdout)?;
let actual = actual.trim();
if env::var("BLESS_TESTS").is_ok() {
fs::write(&test.input, generate_blesssed(&input, &actual))?;
} else {
if actual != expected {
println!("{:?} {:?}", actual, expected);
return Err(io::Error::new(io::ErrorKind::Other, "test failed").into());
}
}
Ok(())
}
fn extract_expected(input: &str) -> String {
input
.lines()
.filter(|l| l.starts_with(";; "))
.skip_while(|l| !l.contains("STDOUT"))
.skip(1)
.take_while(|l| !l.contains("STDOUT"))
.map(|l| &l[3..])
.collect::<Vec<_>>()
.join("\n")
}
fn generate_blesssed(input: &str, actual: &str) -> String {
let mut input = input
.lines()
.filter(|l| !l.starts_with(";;"))
.collect::<Vec<_>>()
.join("\n")
.trim()
.to_string();
input.push_str("\n\n");
input.push_str(";; STDOUT (update this section with `BLESS_TESTS=1` while running tests)\n");
for line in actual.lines() {
input.push_str(";; ");
input.push_str(line);
input.push_str("\n");
}
input.push_str(";; STDOUT\n");
return input;
}

View File

@@ -1,11 +0,0 @@
(module
(func $foo)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0))
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,16 +0,0 @@
(module
(func $foo
call $bar
)
(func $bar)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0)
;; call $bar)
;; (func $bar (type 0))
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,21 +0,0 @@
(module
(import "" "" (func (param i32)))
(func $foo
i32.const 0
call 0
)
(start $foo)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func (param i32)))
;; (type (;1;) (func))
;; (import "" "" (func (;0;) (type 0)))
;; (func $foo (type 1)
;; i32.const 0
;; call 0)
;; (start 1))
;; STDOUT

View File

@@ -1,8 +0,0 @@
(module
(import "" "a" (memory 0))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (import "" "a" (memory (;0;) 0)))
;; STDOUT

View File

@@ -1,10 +0,0 @@
(module
(memory 0 1)
(data (i32.const 0) "foo")
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (memory (;0;) 0 1)
;; (data (;0;) (i32.const 0) "foo"))
;; STDOUT

View File

@@ -1,11 +0,0 @@
(module
(global i32 (i32.const 0))
(export "foo" (global 0))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (global (;0;) i32 (i32.const 0))
;; (export "foo" (global 0)))
;; STDOUT

View File

@@ -1,8 +0,0 @@
(module
(memory 0 17)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (memory (;0;) 0 17))
;; STDOUT

View File

@@ -1,27 +0,0 @@
(module
(memory 0 10)
(func $foo
i32.const 0
i32.const 0
i32.const 0
memory.init 0
)
(data passive "wut")
(start $foo)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0)
;; i32.const 0
;; i32.const 0
;; i32.const 0
;; memory.init 0)
;; (memory (;0;) 0 10)
;; (start 0)
;; (data (;0;) passive "wut"))
;; STDOUT

View File

@@ -1,30 +0,0 @@
(module
(import "" "" (table 0 1 anyfunc))
(func $foo
i32.const 0
i32.const 0
i32.const 0
table.init 0
)
(func $bar)
(elem passive $bar)
(start $foo)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (import "" "" (table (;0;) 0 1 anyfunc))
;; (func $foo (type 0)
;; i32.const 0
;; i32.const 0
;; i32.const 0
;; table.init 0)
;; (func $bar (type 0))
;; (start 0)
;; (elem (;0;) passive $bar))
;; STDOUT

View File

@@ -1,23 +0,0 @@
(module
(global (mut i32) (i32.const 0))
(start $foo)
(func $bar)
(func $foo
i32.const 1
set_global 0
)
(func $baz)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0)
;; i32.const 1
;; global.set 0)
;; (global (;0;) (mut i32) (i32.const 0))
;; (start 0))
;; STDOUT

View File

@@ -1,14 +0,0 @@
(module
(start $foo)
(func $bar)
(func $foo)
(func $baz)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0))
;; (start 0))
;; STDOUT

View File

@@ -1,10 +0,0 @@
(module
(table 0 17 anyfunc)
(export "foo" (table 0))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (table (;0;) 0 17 anyfunc)
;; (export "foo" (table 0)))
;; STDOUT

View File

@@ -1,17 +0,0 @@
(module
(table 0 17 anyfunc)
(func $foo
i32.const 0
call_indirect)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0)
;; i32.const 0
;; call_indirect (type 0))
;; (table (;0;) 0 17 anyfunc)
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,39 +0,0 @@
(module
(func $foo
(local i32 f32 i32 f64 i64 i32 f32 i64 i32 f32 f64)
get_local 0
get_local 1
get_local 2
get_local 3
get_local 4
get_local 5
get_local 6
get_local 7
get_local 8
get_local 9
get_local 10
unreachable
)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0)
;; (local i32 i32 i32 i32 f32 f32 f32 f64 f64 i64 i64)
;; local.get 0
;; local.get 4
;; local.get 1
;; local.get 7
;; local.get 9
;; local.get 2
;; local.get 5
;; local.get 10
;; local.get 3
;; local.get 6
;; local.get 8
;; unreachable)
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,16 +0,0 @@
(module
(func $foo (result i32)
(local i32)
get_local 0
)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func (result i32)))
;; (func $foo (type 0) (result i32)
;; (local i32)
;; local.get 0)
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,18 +0,0 @@
(module
(func $foo (param i32)
(local i32)
get_local 0
set_local 1
)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func (param i32)))
;; (func $foo (type 0) (param i32)
;; (local i32)
;; local.get 0
;; local.set 1)
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,18 +0,0 @@
(module
(func $foo (param i32) (result i32)
(local i32)
get_local 0
tee_local 1
)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func (param i32) (result i32)))
;; (func $foo (type 0) (param i32) (result i32)
;; (local i32)
;; local.get 0
;; local.tee 1)
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,7 +0,0 @@
(module
(func $foo)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module)
;; STDOUT

View File

@@ -1,9 +0,0 @@
(module
(global i32 (i32.const 0))
(export "__heap_base" (global 0))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module)
;; STDOUT

View File

@@ -1,26 +0,0 @@
(module
(import "" "a" (func $i1))
(import "" "b" (func $i2))
(import "" "c" (func $i3))
(func $bar)
(func $foo
call $i1
call $i3)
(func $baz)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (import "" "a" (func $i1 (type 0)))
;; (import "" "c" (func $i3 (type 0)))
;; (func $foo (type 0)
;; call $i1
;; call $i3)
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,7 +0,0 @@
(module
(import "" "" (global i32))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module)
;; STDOUT

View File

@@ -1,35 +0,0 @@
(module
(import "" "a" (global i32))
(import "" "b" (global i32))
(import "" "c" (global i32))
(global i32 (i32.const 1))
(global i32 (i32.const 2))
(func $foo
get_global 0
drop
get_global 2
drop
get_global 4
drop
)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (import "" "a" (global (;0;) i32))
;; (import "" "c" (global (;1;) i32))
;; (func $foo (type 0)
;; global.get 0
;; drop
;; global.get 1
;; drop
;; global.get 2
;; drop)
;; (global (;2;) i32 (i32.const 2))
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,11 +0,0 @@
(module
(import "" "" (table 0 1 anyfunc))
(func $foo)
(elem (i32.const 1) $foo)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module)
;; STDOUT

View File

@@ -1,13 +0,0 @@
(module
(func $foo
(local i32)
)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0))
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,7 +0,0 @@
(module
(table 0 17 anyfunc)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module)
;; STDOUT

View File

@@ -1,11 +0,0 @@
(module
(import "" "" (table 0 1 anyfunc))
(func $foo)
(elem passive $foo)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module)
;; STDOUT

View File

@@ -1,34 +0,0 @@
(module
(type (func))
(type (func (param i32)))
(type (func (param i32)))
(type (func (result i32)))
(func $f1 (type 0))
(func $f2 (type 1))
(func $f3 (type 2))
(func $f4 (type 3)
i32.const 0
)
(export "a" (func $f1))
(export "b" (func $f2))
(export "c" (func $f3))
(export "d" (func $f4))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (type (;1;) (func (param i32)))
;; (type (;2;) (func (result i32)))
;; (func $f1 (type 0))
;; (func $f2 (type 1) (param i32))
;; (func $f3 (type 1) (param i32))
;; (func $f4 (type 2) (result i32)
;; i32.const 0)
;; (export "a" (func $f1))
;; (export "b" (func $f2))
;; (export "c" (func $f3))
;; (export "d" (func $f4)))
;; STDOUT

View File

@@ -1,16 +0,0 @@
(module
(func
call 2)
(func)
(func)
(export "foo" (func 0))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func (;0;) (type 0)
;; call 1)
;; (func (;1;) (type 0))
;; (export "foo" (func 0)))
;; STDOUT

View File

@@ -1,16 +0,0 @@
(module
(global i32 (i32.const 0))
(global i32 (i32.const 0))
(func (result i32)
get_global 1)
(export "foo" (func 0))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func (result i32)))
;; (func (;0;) (type 0) (result i32)
;; global.get 0)
;; (global (;0;) i32 (i32.const 0))
;; (export "foo" (func 0)))
;; STDOUT

View File

@@ -1,16 +0,0 @@
(module
(func $foo (result i32)
(local i32 i32)
get_local 1
)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func (result i32)))
;; (func $foo (type 0) (result i32)
;; (local i32)
;; local.get 0)
;; (export "foo" (func $foo)))
;; STDOUT

View File

@@ -1,13 +0,0 @@
(module
(type (func (result i32)))
(type (func))
(func (type 1))
(export "foo" (func 0))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func (;0;) (type 0))
;; (export "foo" (func 0)))
;; STDOUT

View File

@@ -1,5 +0,0 @@
(module)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module)
;; STDOUT

View File

@@ -1,15 +0,0 @@
(module
(func $foo
i32.const 0
call_indirect
)
(func $bar)
(table 0 10 anyfunc)
(elem (i32.const 0) $bar)
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module)
;; STDOUT

View File

@@ -1,25 +0,0 @@
(module
(func $foo
i32.const 0
call_indirect
)
(func $bar)
(table 0 10 anyfunc)
(elem (i32.const 0) $bar)
(export "foo" (func $foo))
)
;; STDOUT (update this section with `BLESS_TESTS=1` while running tests)
;; (module
;; (type (;0;) (func))
;; (func $foo (type 0)
;; i32.const 0
;; call_indirect (type 0))
;; (func $bar (type 0))
;; (table (;0;) 0 10 anyfunc)
;; (export "foo" (func $foo))
;; (elem (;0;) (i32.const 0) $bar))
;; STDOUT