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!
2019-01-31 09:54:23 -08:00
|
|
|
use docopt::Docopt;
|
|
|
|
use failure::{bail, Error};
|
|
|
|
use serde::Deserialize;
|
2017-12-14 19:31:01 -08:00
|
|
|
use std::path::PathBuf;
|
2018-04-25 11:42:22 -07:00
|
|
|
use std::process;
|
2019-02-20 08:39:46 -08:00
|
|
|
use wasm_bindgen_cli_support::{Bindgen, EncodeInto};
|
2017-12-14 19:31:01 -08:00
|
|
|
|
2018-10-17 19:15:09 -07:00
|
|
|
// no need for jemalloc bloat in this binary (and we don't need speed)
|
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: std::alloc::System = std::alloc::System;
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
const USAGE: &'static str = "
|
|
|
|
Generating JS bindings for a wasm file
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
wasm-bindgen [options] <input>
|
2017-12-27 07:50:43 -08:00
|
|
|
wasm-bindgen -h | --help
|
2018-03-01 19:19:12 -08:00
|
|
|
wasm-bindgen -V | --version
|
2017-12-14 19:31:01 -08:00
|
|
|
|
|
|
|
Options:
|
2019-02-14 10:08:24 -08:00
|
|
|
-h --help Show this screen.
|
|
|
|
--out-dir DIR Output directory
|
|
|
|
--out-name VAR Set a custom output filename (Without extension. Defaults to crate name)
|
|
|
|
--nodejs Generate output that only works in node.js
|
2019-03-07 07:33:40 -08:00
|
|
|
--web Generate output that only works in a browser
|
|
|
|
--browser Hint that JS should only be compatible with a browser
|
2019-02-14 10:08:24 -08:00
|
|
|
--no-modules Generate output that only works in a browser (without modules)
|
|
|
|
--no-modules-global VAR Name of the global variable to initialize
|
|
|
|
--typescript Output a TypeScript definition file (on by default)
|
|
|
|
--no-typescript Don't emit a *.d.ts file
|
|
|
|
--debug Include otherwise-extraneous debug checks in output
|
|
|
|
--no-demangle Don't demangle Rust symbol names
|
|
|
|
--keep-debug Keep debug sections in wasm files
|
|
|
|
--remove-name-section Remove the debugging `name` section of the file
|
|
|
|
--remove-producers-section Remove the telemetry `producers` section
|
2019-02-20 08:39:46 -08:00
|
|
|
--encode-into MODE Whether or not to use TextEncoder#encodeInto,
|
|
|
|
valid values are [test, always, never]
|
2019-02-14 10:08:24 -08:00
|
|
|
-V --version Print the version number of wasm-bindgen
|
2017-12-14 19:31:01 -08:00
|
|
|
";
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct Args {
|
2017-12-14 21:55:21 -08:00
|
|
|
flag_nodejs: bool,
|
2018-03-28 07:37:56 -07:00
|
|
|
flag_browser: bool,
|
2019-03-07 07:33:40 -08:00
|
|
|
flag_web: bool,
|
2018-04-11 13:59:58 +05:45
|
|
|
flag_no_modules: bool,
|
2018-01-29 21:20:38 -08:00
|
|
|
flag_typescript: bool,
|
2018-04-27 07:14:46 -07:00
|
|
|
flag_no_typescript: bool,
|
2018-01-29 21:20:38 -08:00
|
|
|
flag_out_dir: Option<PathBuf>,
|
2018-12-04 21:35:05 -05:00
|
|
|
flag_out_name: Option<String>,
|
2017-12-20 12:50:10 -08:00
|
|
|
flag_debug: bool,
|
2018-03-01 19:19:12 -08:00
|
|
|
flag_version: bool,
|
2018-04-11 11:43:18 -07:00
|
|
|
flag_no_demangle: bool,
|
2018-04-19 13:33:58 -07:00
|
|
|
flag_no_modules_global: Option<String>,
|
2018-11-09 07:45:19 -08:00
|
|
|
flag_remove_name_section: bool,
|
2019-02-14 10:08:24 -08:00
|
|
|
flag_remove_producers_section: bool,
|
2018-07-13 08:10:51 -07:00
|
|
|
flag_keep_debug: bool,
|
2019-02-20 08:39:46 -08:00
|
|
|
flag_encode_into: Option<String>,
|
2018-03-01 19:19:12 -08:00
|
|
|
arg_input: Option<PathBuf>,
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-11-29 12:25:24 -08:00
|
|
|
env_logger::init();
|
2017-12-14 19:31:01 -08:00
|
|
|
let args: Args = Docopt::new(USAGE)
|
|
|
|
.and_then(|d| d.deserialize())
|
|
|
|
.unwrap_or_else(|e| e.exit());
|
|
|
|
|
2018-03-01 19:19:12 -08:00
|
|
|
if args.flag_version {
|
|
|
|
println!("wasm-bindgen {}", wasm_bindgen_shared::version());
|
2018-04-04 20:06:53 +05:45
|
|
|
return;
|
2018-03-01 19:19:12 -08:00
|
|
|
}
|
2018-04-25 11:42:22 -07:00
|
|
|
let err = match rmain(&args) {
|
|
|
|
Ok(()) => return,
|
|
|
|
Err(e) => e,
|
|
|
|
};
|
2018-04-30 13:24:29 -07:00
|
|
|
eprintln!("error: {}", err);
|
2018-08-01 16:15:09 -05:00
|
|
|
for cause in err.iter_causes() {
|
2018-04-30 13:24:29 -07:00
|
|
|
eprintln!("\tcaused by: {}", cause);
|
2018-04-25 11:42:22 -07:00
|
|
|
}
|
|
|
|
process::exit(1);
|
|
|
|
}
|
2018-03-01 19:19:12 -08:00
|
|
|
|
2018-04-25 11:42:22 -07:00
|
|
|
fn rmain(args: &Args) -> Result<(), Error> {
|
2018-03-01 19:19:12 -08:00
|
|
|
let input = match args.arg_input {
|
2018-04-25 11:42:22 -07:00
|
|
|
Some(ref s) => s,
|
|
|
|
None => bail!("input file expected"),
|
2018-03-01 19:19:12 -08:00
|
|
|
};
|
|
|
|
|
2018-04-27 07:14:46 -07:00
|
|
|
let typescript = args.flag_typescript || !args.flag_no_typescript;
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
let mut b = Bindgen::new();
|
2018-04-25 11:42:22 -07:00
|
|
|
b.input_path(input)
|
2019-02-25 11:11:30 -08:00
|
|
|
.nodejs(args.flag_nodejs)?
|
2019-03-07 07:33:40 -08:00
|
|
|
.web(args.flag_web)?
|
2019-02-25 11:11:30 -08:00
|
|
|
.browser(args.flag_browser)?
|
|
|
|
.no_modules(args.flag_no_modules)?
|
2018-04-04 20:06:53 +05:45
|
|
|
.debug(args.flag_debug)
|
2018-04-13 07:34:27 -07:00
|
|
|
.demangle(!args.flag_no_demangle)
|
2018-07-13 08:10:51 -07:00
|
|
|
.keep_debug(args.flag_keep_debug)
|
2018-11-09 07:45:19 -08:00
|
|
|
.remove_name_section(args.flag_remove_name_section)
|
2019-02-14 10:08:24 -08:00
|
|
|
.remove_producers_section(args.flag_remove_producers_section)
|
2018-04-27 07:14:46 -07:00
|
|
|
.typescript(typescript);
|
2018-04-19 14:28:30 -07:00
|
|
|
if let Some(ref name) = args.flag_no_modules_global {
|
2019-02-25 11:11:30 -08:00
|
|
|
b.no_modules_global(name)?;
|
2018-04-19 13:33:58 -07:00
|
|
|
}
|
2018-12-04 21:35:05 -05:00
|
|
|
if let Some(ref name) = args.flag_out_name {
|
|
|
|
b.out_name(name);
|
|
|
|
}
|
2019-02-20 08:39:46 -08:00
|
|
|
if let Some(mode) = &args.flag_encode_into {
|
|
|
|
match mode.as_str() {
|
|
|
|
"test" => b.encode_into(EncodeInto::Test),
|
|
|
|
"always" => b.encode_into(EncodeInto::Always),
|
|
|
|
"never" => b.encode_into(EncodeInto::Never),
|
|
|
|
s => bail!("invalid encode-into mode: `{}`", s),
|
|
|
|
};
|
|
|
|
}
|
2018-01-29 21:20:38 -08:00
|
|
|
|
|
|
|
let out_dir = match args.flag_out_dir {
|
|
|
|
Some(ref p) => p,
|
2018-04-25 11:42:22 -07:00
|
|
|
None => bail!("the `--out-dir` argument is now required"),
|
2018-01-29 21:20:38 -08:00
|
|
|
};
|
|
|
|
|
2018-04-25 11:42:22 -07:00
|
|
|
b.generate(out_dir)
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|