mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-05-02 17:12:15 +00:00
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!
96 lines
2.9 KiB
Rust
96 lines
2.9 KiB
Rust
use docopt::Docopt;
|
|
use failure::{Error, ResultExt};
|
|
use serde::Deserialize;
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
use std::process;
|
|
|
|
// no need for jemalloc bloat in this binary (and we don't need speed)
|
|
#[global_allocator]
|
|
static ALLOC: std::alloc::System = std::alloc::System;
|
|
|
|
const USAGE: &'static str = "
|
|
Converts a wasm file to an ES6 JS module
|
|
|
|
Usage:
|
|
wasm2es6js [options] <input>
|
|
wasm2es6js -h | --help
|
|
|
|
Options:
|
|
-h --help Show this screen.
|
|
-o --output FILE File to place output in
|
|
--out-dir DIR Directory to place ouptut in
|
|
--typescript Output a `*.d.ts` file next to the JS output
|
|
--base64 Inline the wasm module using base64 encoding
|
|
--fetch PATH Load module by passing the PATH argument to `fetch()`
|
|
|
|
Note that this is not intended to produce a production-ready output module
|
|
but rather is intended purely as a temporary \"hack\" until it's standard in
|
|
bundlers for working with wasm. Use this program with care!
|
|
";
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct Args {
|
|
flag_output: Option<PathBuf>,
|
|
flag_out_dir: Option<PathBuf>,
|
|
flag_typescript: bool,
|
|
flag_base64: bool,
|
|
flag_fetch: Option<String>,
|
|
arg_input: PathBuf,
|
|
}
|
|
|
|
fn main() {
|
|
let args: Args = Docopt::new(USAGE)
|
|
.and_then(|d| d.deserialize())
|
|
.unwrap_or_else(|e| e.exit());
|
|
let err = match rmain(&args) {
|
|
Ok(()) => return,
|
|
Err(e) => e,
|
|
};
|
|
eprintln!("error: {}", err);
|
|
for cause in err.iter_causes() {
|
|
eprintln!("\tcaused by: {}", cause);
|
|
}
|
|
process::exit(1);
|
|
}
|
|
|
|
fn rmain(args: &Args) -> Result<(), Error> {
|
|
let wasm = fs::read(&args.arg_input)
|
|
.with_context(|_| format!("failed to read `{}`", args.arg_input.display()))?;
|
|
|
|
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
|
|
.base64(args.flag_base64)
|
|
.fetch(args.flag_fetch.clone())
|
|
.generate(&wasm)?;
|
|
|
|
if args.flag_typescript {
|
|
let ts = object.typescript()?;
|
|
write(&args, "d.ts", ts.as_bytes(), false)?;
|
|
}
|
|
|
|
let (js, wasm) = object.js_and_wasm()?;
|
|
|
|
write(args, "js", js.as_bytes(), false)?;
|
|
if let Some(wasm) = wasm {
|
|
write(args, "wasm", &wasm, false)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn write(args: &Args, extension: &str, contents: &[u8], print_fallback: bool) -> Result<(), Error> {
|
|
if let Some(p) = &args.flag_output {
|
|
let dst = p.with_extension(extension);
|
|
fs::write(&dst, contents)
|
|
.with_context(|_| format!("failed to write `{}`", dst.display()))?;
|
|
} else if let Some(p) = &args.flag_out_dir {
|
|
let filename = args.arg_input.file_name().unwrap();
|
|
let dst = p.join(filename).with_extension(extension);
|
|
fs::write(&dst, contents)
|
|
.with_context(|_| format!("failed to write `{}`", dst.display()))?;
|
|
} else if print_fallback {
|
|
println!("{}", String::from_utf8_lossy(contents))
|
|
}
|
|
|
|
Ok(())
|
|
}
|