Rewrite wasm-bindgen with ES6 modules in mind

This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent
discussions it's clear that the previous model wasn't quite going to cut it, and
this iteration is one which primarily embraces ES6 modules and the idea that
this is a polyfill for host bindings.

The overall interface and functionality hasn't changed much but the underlying
technology has now changed significantly. Previously `wasm-bindgen` would emit a
JS file that acted as an ES6 module but had a bit of a wonky interface. It
exposed an async function for instantiation of the wasm module, but that's the
bundler's job, not ours!

Instead this iteration views each input and output as a discrete ES6 module. The
input wasm file is interpreted as "this *should* be an ES6 module with rich
types" and the output is "well here's some ES6 modules that fulfill that
contract". Notably the tool now replaces the original wasm ES6 module with a JS
ES6 module that has the "rich interface". Additionally a second ES6 module is
emitted (the actual wasm file) which imports and exports to the original ES6
module.

This strategy is hoped to be much more amenable to bundlers and controlling how
the wasm itself is instantiated. The emitted files files purely assume ES6
modules and should be able to work as-is once ES6 module integration for wasm is
completed.

Note that there aren't a ton of tools to pretend a wasm module is an ES6 module
at the moment but those should be coming soon! In the meantime a local
`wasm2es6js` hack was added to help make *something* work today. The README has
also been updated with instructions for interacting with this model.
This commit is contained in:
Alex Crichton
2018-01-29 21:20:38 -08:00
parent f27e4a9e94
commit c51a342cb3
22 changed files with 1514 additions and 1946 deletions

View File

@ -5,10 +5,15 @@ authors = ["Alex Crichton <alex@alexcrichton.com>"]
[dependencies]
docopt = "0.8"
parity-wasm = "0.23"
serde = "1.0"
serde_derive = "1.0"
wasm-bindgen-cli-support = { path = "../wasm-bindgen-cli-support" }
[[bin]]
name = "wasm-bindgen"
path = "src/main.rs"
path = "src/bin/wasm-bindgen.rs"
[[bin]]
name = "wasm2es6js"
path = "src/bin/wasm2es6js.rs"

View File

@ -17,19 +17,17 @@ Usage:
Options:
-h --help Show this screen.
--output-js FILE Output Javascript file
--output-ts FILE Output TypeScript file
--output-wasm FILE Output WASM file
--out-dir DIR Output directory
--nodejs Generate output for node.js, not the browser
--typescript Output a TypeScript definition file
--debug Include otherwise-extraneous debug checks in output
";
#[derive(Debug, Deserialize)]
struct Args {
flag_output_js: Option<PathBuf>,
flag_output_ts: Option<PathBuf>,
flag_output_wasm: Option<PathBuf>,
flag_nodejs: bool,
flag_typescript: bool,
flag_out_dir: Option<PathBuf>,
flag_debug: bool,
arg_input: PathBuf,
}
@ -43,21 +41,12 @@ fn main() {
b.input_path(&args.arg_input)
.nodejs(args.flag_nodejs)
.debug(args.flag_debug)
.uglify_wasm_names(!args.flag_debug);
let ret = b.generate().expect("failed to generate bindings");
let mut written = false;
if let Some(ref ts) = args.flag_output_ts {
ret.write_ts_to(ts).expect("failed to write TypeScript output file");
written = true;
}
if let Some(ref js) = args.flag_output_js {
ret.write_js_to(js).expect("failed to write Javascript output file");
written = true;
}
if !written {
println!("{}", ret.generate_ts());
}
if let Some(ref wasm) = args.flag_output_wasm {
ret.write_wasm_to(wasm).expect("failed to write wasm output file");
}
.typescript(args.flag_typescript);
let out_dir = match args.flag_out_dir {
Some(ref p) => p,
None => panic!("the `--out-dir` argument is now required"),
};
b.generate(out_dir).expect("failed to generate bindings");
}

View File

@ -0,0 +1,63 @@
#[macro_use]
extern crate serde_derive;
extern crate docopt;
extern crate parity_wasm;
extern crate wasm_bindgen_cli_support;
use std::collections::HashSet;
use std::fs::File;
use std::io::{Write, Read};
use std::path::PathBuf;
use docopt::Docopt;
use parity_wasm::elements::*;
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
--base64 Inline the wasm module using base64 encoding
";
#[derive(Debug, Deserialize)]
struct Args {
flag_output: Option<PathBuf>,
flag_base64: bool,
arg_input: PathBuf,
}
fn main() {
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
if !args.flag_base64 {
panic!("unfortunately only works right now with base64");
}
let mut wasm = Vec::new();
File::open(&args.arg_input).expect("failed to open input")
.read_to_end(&mut wasm).expect("failed to read input");
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
.base64(args.flag_base64)
.generate(&wasm)
.expect("failed to parse wasm");
let js = object.js();
match args.flag_output {
Some(ref p) => {
File::create(p).expect("failed to create output")
.write_all(js.as_bytes()).expect("failed to write output");
}
None => {
println!("{}", js);
}
}
}