Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rustwasm/wasm-bindgen
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.2.49
Choose a base ref
...
head repository: rustwasm/wasm-bindgen
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0.2.50
Choose a head ref
  • 6 commits
  • 46 files changed
  • 1 contributor

Commits on Aug 15, 2019

  1. Improve instantiateStreaming fallback

    This commit improves our `instantiateStreaming` fallback to only
    actually trigger the fallback if the headers look wrong. If the headers
    look right then we let through the original error which should help
    avoid accidentally papering over bugs with different bugs in
    misconfigured situations.
    
    Closes #1696
    alexcrichton committed Aug 15, 2019
    Copy the full SHA
    20be0a3 View commit details
  2. Merge pull request #1723 from alexcrichton/instntiate-streaming

    Improve instantiateStreaming fallback
    alexcrichton authored Aug 15, 2019
    Copy the full SHA
    bd7c907 View commit details

Commits on Aug 16, 2019

  1. Add support for emitting a Wasm Interface Types section

    This commit adds support to `wasm-bindgen` to emit a WebAssembly module
    that contains a WebAssembly Interface Types section. As of today there are no
    native consumers of these WebAssembly modules, and the actual binary format
    here is basically arbitrary (chosen by the `wasm-webidl-bindings` crate). The
    intention is that we'll be following the [WebAssembly Interface
    Types proposal][proposal] very closely and update here as necessary.
    
    The main feature added in this PR is that a new experimental environment
    variable, `WASM_INTERFACE_TYPES=1`, is recognized by the `wasm-bindgen`
    CLI tool. When present the CLI tool will act differently than it does
    today:
    
    * The `anyref` feature will be implicitly enabled
    * A WebAssembly interface types section will be emitted in the
      WebAssembly module
    * For now, the WebAssembly module is strictly validated to require zero
      JS glue. This means that `wasm-bindgen` is producing a fully
      standalone WebAssembly module.
    
    The last point here is one that will change before this functionality is
    stabilized in `wasm-bindgen`. For now it reflects the major use case of
    this feature which is to produce a standalone WebAssembly module with no
    support JS glue, and to do that we need to verify properties like it's
    not using JS global names, nonstandard binding expressions, etc. The
    error messages here aren't the best but they at least fail compilation
    at some point instead of silently producing weird wasm modules.
    
    Eventually it's envisioned that a WebAssembly module will contain an
    interface types section but *also* have JS glue so binding expressions
    can be used when available but otherwise we'd still generate JS glue for
    things like nonstandard expressions and accessing JS global values.
    
    It should be noted that a major feature not implemented in
    `wasm-bindgen` yet is the multi-value proposal for WebAssembly. This is
    coming soon (as soon as we can) in `walrus` and later for a pass here,
    but for now this means that returning multiple values (like a string
    which has a pointer/length) is a bit of a hack. To enable this use case
    a `wasm-bindgen`-specific-convention which will never be stabilized is
    invented here by using binding expression to indicate "this return value
    is actually returned through an out-ptr as the first argument list".
    This is a gross hack and is guaranteed to be removed. Eventually we will
    support multi-value and the wasm module emitted will simply use
    multi-value and contain internal polyfills for Rust's ABI which returns
    values through out-ptrs.
    
    Overall this should make `wasm-bindgen` usable for playing around with
    the WebIDL bindings proposal and helping us get a taste of what it looks
    like to have entirely standalone WebAssembly modules running in multiple
    environments, no extra fluff necessary!
    
    [proposal]: https://github.com/webassembly/webidl-bindings
    alexcrichton committed Aug 16, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c5dd572 View commit details

Commits on Aug 19, 2019

  1. Merge pull request #1725 from alexcrichton/real-webidl-section

    Add support for emitting a Wasm Interface Types section
    alexcrichton authored Aug 19, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    487289c View commit details
  2. Bump to 0.2.50

    alexcrichton committed Aug 19, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c2daa4f View commit details
  3. Merge pull request #1726 from alexcrichton/bump

    Bump to 0.2.50
    alexcrichton authored Aug 19, 2019
    Copy the full SHA
    5c56c02 View commit details
Showing with 741 additions and 159 deletions.
  1. +19 −0 CHANGELOG.md
  2. +4 −4 Cargo.toml
  3. +1 −1 crates/anyref-xform/Cargo.toml
  4. +2 −2 crates/backend/Cargo.toml
  5. +5 −5 crates/cli-support/Cargo.toml
  6. +19 −13 crates/cli-support/src/anyref.rs
  7. +9 −0 crates/cli-support/src/intrinsic.rs
  8. +14 −6 crates/cli-support/src/js/mod.rs
  9. +32 −11 crates/cli-support/src/lib.rs
  10. +76 −49 crates/cli-support/src/webidl/mod.rs
  11. +492 −0 crates/cli-support/src/webidl/standard.rs
  12. +3 −3 crates/cli/Cargo.toml
  13. +4 −4 crates/futures/Cargo.toml
  14. +4 −4 crates/js-sys/Cargo.toml
  15. +3 −3 crates/macro-support/Cargo.toml
  16. +3 −3 crates/macro/Cargo.toml
  17. +1 −1 crates/shared/Cargo.toml
  18. +1 −1 crates/test-macro/Cargo.toml
  19. +5 −5 crates/test/Cargo.toml
  20. +1 −1 crates/threads-xform/Cargo.toml
  21. +1 −1 crates/wasm-interpreter/Cargo.toml
  22. +6 −6 crates/web-sys/Cargo.toml
  23. +2 −2 crates/webidl/Cargo.toml
  24. +1 −1 examples/add/Cargo.toml
  25. +2 −2 examples/canvas/Cargo.toml
  26. +1 −1 examples/char/Cargo.toml
  27. +2 −2 examples/closures/Cargo.toml
  28. +2 −2 examples/console_log/Cargo.toml
  29. +1 −1 examples/dom/Cargo.toml
  30. +1 −1 examples/duck-typed-interfaces/Cargo.toml
  31. +3 −3 examples/fetch/Cargo.toml
  32. +1 −1 examples/guide-supported-types-examples/Cargo.toml
  33. +1 −1 examples/hello_world/Cargo.toml
  34. +1 −1 examples/import_js/crate/Cargo.toml
  35. +1 −1 examples/julia_set/Cargo.toml
  36. +2 −2 examples/paint/Cargo.toml
  37. +1 −1 examples/performance/Cargo.toml
  38. +3 −3 examples/raytrace-parallel/Cargo.toml
  39. +1 −1 examples/request-animation-frame/Cargo.toml
  40. +2 −2 examples/todomvc/Cargo.toml
  41. +2 −2 examples/wasm-in-wasm/Cargo.toml
  42. +1 −1 examples/wasm2js/Cargo.toml
  43. +1 −1 examples/webaudio/Cargo.toml
  44. +2 −2 examples/webgl/Cargo.toml
  45. +1 −1 examples/websockets/Cargo.toml
  46. +1 −1 examples/without-a-bundler/Cargo.toml
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,25 @@

--------------------------------------------------------------------------------

## 0.2.50

Released 2019-08-19.

### Added

* Experimental support with a `WASM_INTERFACE_TYPES=1` environment variable has
been added to emit a Wasm Interface Types custom section, making the output of
`wasm-bindgen` a single standalone WebAssembly file.
[#1725](https://github.com/rustwasm/wasm-bindgen/pull/1725)

### Fixed

* Unrelated errors are now no longer accidentally swallowed by the
`instantiateStreaming` fallback.
[#1723](https://github.com/rustwasm/wasm-bindgen/pull/1723)

--------------------------------------------------------------------------------

## 0.2.49

Released 2019-08-14.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm-bindgen"
version = "0.2.49"
version = "0.2.50"
authors = ["The wasm-bindgen Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
@@ -36,14 +36,14 @@ strict-macro = ["wasm-bindgen-macro/strict-macro"]
xxx_debug_only_print_generated_code = ["wasm-bindgen-macro/xxx_debug_only_print_generated_code"]

[dependencies]
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.49" }
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.50" }
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
cfg-if = "0.1.9"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
js-sys = { path = 'crates/js-sys', version = '0.3.26' }
wasm-bindgen-test = { path = 'crates/test', version = '=0.2.49' }
js-sys = { path = 'crates/js-sys', version = '0.3.27' }
wasm-bindgen-test = { path = 'crates/test', version = '=0.2.50' }
serde_derive = "1.0"
wasm-bindgen-test-crate-a = { path = 'tests/crates/a', version = '0.1' }
wasm-bindgen-test-crate-b = { path = 'tests/crates/b', version = '0.1' }
2 changes: 1 addition & 1 deletion crates/anyref-xform/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm-bindgen-anyref-xform"
version = "0.2.49"
version = "0.2.50"
authors = ["The wasm-bindgen Developers"]
license = "MIT/Apache-2.0"
repository = "https://github.com/rustwasm/wasm-bindgen/tree/master/crates/anyref-xform"
4 changes: 2 additions & 2 deletions crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm-bindgen-backend"
version = "0.2.49"
version = "0.2.50"
authors = ["The wasm-bindgen Developers"]
license = "MIT/Apache-2.0"
repository = "https://github.com/rustwasm/wasm-bindgen/tree/master/crates/backend"
@@ -22,4 +22,4 @@ log = "0.4"
proc-macro2 = "1.0"
quote = '1.0'
syn = { version = '1.0', features = ['full'] }
wasm-bindgen-shared = { path = "../shared", version = "=0.2.49" }
wasm-bindgen-shared = { path = "../shared", version = "=0.2.50" }
10 changes: 5 additions & 5 deletions crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm-bindgen-cli-support"
version = "0.2.49"
version = "0.2.50"
authors = ["The wasm-bindgen Developers"]
license = "MIT/Apache-2.0"
repository = "https://github.com/rustwasm/wasm-bindgen/tree/master/crates/cli-support"
@@ -19,8 +19,8 @@ rustc-demangle = "0.1.13"
serde_json = "1.0"
tempfile = "3.0"
walrus = "0.11.0"
wasm-bindgen-anyref-xform = { path = '../anyref-xform', version = '=0.2.49' }
wasm-bindgen-shared = { path = "../shared", version = '=0.2.49' }
wasm-bindgen-threads-xform = { path = '../threads-xform', version = '=0.2.49' }
wasm-bindgen-wasm-interpreter = { path = "../wasm-interpreter", version = '=0.2.49' }
wasm-bindgen-anyref-xform = { path = '../anyref-xform', version = '=0.2.50' }
wasm-bindgen-shared = { path = "../shared", version = '=0.2.50' }
wasm-bindgen-threads-xform = { path = '../threads-xform', version = '=0.2.50' }
wasm-bindgen-wasm-interpreter = { path = "../wasm-interpreter", version = '=0.2.50' }
wasm-webidl-bindings = "0.4.0"
32 changes: 19 additions & 13 deletions crates/cli-support/src/anyref.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ use walrus::Module;
use wasm_bindgen_anyref_xform::Context;
use wasm_webidl_bindings::ast;

pub fn process(module: &mut Module) -> Result<(), Error> {
pub fn process(module: &mut Module, wasm_interface_types: bool) -> Result<(), Error> {
let mut cfg = Context::default();
cfg.prepare(module)?;
let bindings = module
@@ -45,18 +45,24 @@ pub fn process(module: &mut Module) -> Result<(), Error> {

cfg.run(module)?;

// Make sure to export the `anyref` table for the JS bindings since it
// will need to be initialized. If it doesn't exist though then the
// module must not use it, so we skip it.
let table = module.tables.iter().find(|t| match t.kind {
walrus::TableKind::Anyref(_) => true,
_ => false,
});
let table = match table {
Some(t) => t.id(),
None => return Ok(()),
};
module.exports.add("__wbg_anyref_table", table);
// If our output is using WebAssembly interface types then our bindings will
// never use this table, so no need to export it. Otherwise it's highly
// likely in web/JS embeddings this will be used, so make sure we export it
// to avoid it getting gc'd accidentally.
if !wasm_interface_types {
// Make sure to export the `anyref` table for the JS bindings since it
// will need to be initialized. If it doesn't exist though then the
// module must not use it, so we skip it.
let table = module.tables.iter().find(|t| match t.kind {
walrus::TableKind::Anyref(_) => true,
_ => false,
});
let table = match table {
Some(t) => t.id(),
None => return Ok(()),
};
module.exports.add("__wbg_anyref_table", table);
}

// Clean up now-unused intrinsics and shims and such
walrus::passes::gc::run(module);
9 changes: 9 additions & 0 deletions crates/cli-support/src/intrinsic.rs
Original file line number Diff line number Diff line change
@@ -50,6 +50,15 @@ macro_rules! intrinsics {
)*
}
}

/// Returns the symbol name of this intrinsic
pub fn name(&self) -> &'static str {
match self {
$(
Intrinsic::$name => $sym,
)*
}
}
}
};
}
20 changes: 14 additions & 6 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
@@ -536,13 +536,19 @@ impl<'a> Context<'a> {
if (typeof WebAssembly.instantiateStreaming === 'function') {{
result = WebAssembly.instantiateStreaming(response, imports)
.catch(e => {{
console.warn(\"`WebAssembly.instantiateStreaming` failed. Assuming this is \
because your server does not serve wasm with \
`application/wasm` MIME type. Falling back to \
`WebAssembly.instantiate` which is slower. Original \
error:\\n\", e);
return response
.then(r => r.arrayBuffer())
.then(r => {{
if (r.headers.get('Content-Type') != 'application/wasm') {{
console.warn(\"`WebAssembly.instantiateStreaming` failed \
because your server does not serve wasm with \
`application/wasm` MIME type. Falling back to \
`WebAssembly.instantiate` which is slower. Original \
error:\\n\", e);
return r.arrayBuffer();
}} else {{
throw e;
}}
}})
.then(bytes => WebAssembly.instantiate(bytes, imports));
}});
}} else {{
@@ -2075,11 +2081,13 @@ impl<'a> Context<'a> {
&binding.outgoing,
wasm_ty.params(),
&webidl.params,
self.config.wasm_interface_types,
)
&& webidl::incoming_do_not_require_glue(
&binding.incoming,
&webidl.result.into_iter().collect::<Vec<_>>(),
wasm_ty.results(),
self.config.wasm_interface_types,
)
}

43 changes: 32 additions & 11 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ pub struct Bindgen {
// "ready to be instantiated on any thread"
threads: wasm_bindgen_threads_xform::Config,
anyref: bool,
wasm_interface_types: bool,
encode_into: EncodeInto,
}

@@ -49,6 +50,7 @@ pub struct Output {
snippets: HashMap<String, Vec<String>>,
local_modules: HashMap<String, String>,
npm_dependencies: HashMap<String, (PathBuf, String)>,
wasm_interface_types: bool,
}

#[derive(Clone)]
@@ -73,6 +75,8 @@ pub enum EncodeInto {

impl Bindgen {
pub fn new() -> Bindgen {
let anyref = env::var("WASM_BINDGEN_ANYREF").is_ok();
let wasm_interface_types = env::var("WASM_INTERFACE_TYPES").is_ok();
Bindgen {
input: Input::None,
out_name: None,
@@ -88,7 +92,8 @@ impl Bindgen {
emit_start: true,
weak_refs: env::var("WASM_BINDGEN_WEAKREF").is_ok(),
threads: threads_config(),
anyref: env::var("WASM_BINDGEN_ANYREF").is_ok(),
anyref: anyref || wasm_interface_types,
wasm_interface_types,
encode_into: EncodeInto::Test,
}
}
@@ -315,15 +320,15 @@ impl Bindgen {
// the webidl bindings proposal) as well as an auxiliary section for all
// sorts of miscellaneous information and features #[wasm_bindgen]
// supports that aren't covered by WebIDL bindings.
webidl::process(&mut module, self.anyref, self.emit_start)?;
webidl::process(&mut module, self.anyref, self.wasm_interface_types, self.emit_start)?;

// Now that we've got type information from the webidl processing pass,
// touch up the output of rustc to insert anyref shims where necessary.
// This is only done if the anyref pass is enabled, which it's
// currently off-by-default since `anyref` is still in development in
// engines.
if self.anyref {
anyref::process(&mut module)?;
anyref::process(&mut module, self.wasm_interface_types)?;
}

let aux = module
@@ -344,6 +349,11 @@ impl Bindgen {
(npm_dependencies, cx.finalize(stem)?)
};

if self.wasm_interface_types {
webidl::standard::add_section(&mut module, &aux, &bindings)
.with_context(|_| "failed to generate a standard wasm bindings custom section")?;
}

Ok(Output {
module,
stem: stem.to_string(),
@@ -354,6 +364,7 @@ impl Bindgen {
ts,
mode: self.mode.clone(),
typescript: self.typescript,
wasm_interface_types: self.wasm_interface_types,
})
}

@@ -506,6 +517,7 @@ fn unexported_unused_lld_things(module: &mut Module) {

impl Output {
pub fn js(&self) -> &str {
assert!(!self.wasm_interface_types);
&self.js
}

@@ -518,6 +530,23 @@ impl Output {
}

fn _emit(&self, out_dir: &Path) -> Result<(), Error> {
let wasm_name = if self.wasm_interface_types {
self.stem.clone()
} else {
format!("{}_bg", self.stem)
};
let wasm_path = out_dir
.join(wasm_name)
.with_extension("wasm");
fs::create_dir_all(out_dir)?;
let wasm_bytes = self.module.emit_wasm()?;
fs::write(&wasm_path, wasm_bytes)
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;

if self.wasm_interface_types {
return Ok(())
}

// Write out all local JS snippets to the final destination now that
// we've collected them from all the programs.
for (identifier, list) in self.snippets.iter() {
@@ -554,7 +583,6 @@ impl Output {
} else {
"js"
};
fs::create_dir_all(out_dir)?;
let js_path = out_dir.join(&self.stem).with_extension(extension);
fs::write(&js_path, reset_indentation(&self.js))
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
@@ -565,10 +593,6 @@ impl Output {
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
}

let wasm_path = out_dir
.join(format!("{}_bg", self.stem))
.with_extension("wasm");

if self.mode.nodejs() {
let js_path = wasm_path.with_extension(extension);
let shim = self.generate_node_wasm_import(&self.module, &wasm_path);
@@ -583,9 +607,6 @@ impl Output {
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
}

let wasm_bytes = self.module.emit_wasm()?;
fs::write(&wasm_path, wasm_bytes)
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;
Ok(())
}

Loading