mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-01 14:54:25 +03:00
Facilitating high-level interactions between Wasm modules and JavaScript
cbeb301371
* Shard the `convert.rs` module into sub-modules Hopefully this'll make the organization a little nicer over time! * Start adding support for optional types This commit starts adding support for optional types to wasm-bindgen as arguments/return values to functions. The strategy here is to add two new traits, `OptionIntoWasmAbi` and `OptionFromWasmAbi`. These two traits are used as a blanket impl to implement `IntoWasmAbi` and `FromWasmAbi` for `Option<T>`. Some consequences of this design: * It should be possible to ensure `Option<SomeForeignType>` implements to/from wasm traits. This is because the option-based traits can be implemented for foreign types. * A specialized implementation is possible for all types, so there's no need for `Option<T>` to introduce unnecessary overhead. * Two new traits is a bit unforutnate but I can't currently think of an alternative design that works for the above two constraints, although it doesn't mean one doesn't exist! * The error messages for "can't use this type here" is actually halfway decent because it says these new traits need to be implemented, which provides a good place to document and talk about what's going on here! * Nested references like `Option<&T>` can't implement `FromWasmAbi`. This means that you can't define a function in Rust which takes `Option<&str>`. It may be possible to do this one day but it'll likely require more trait trickery than I'm capable of right now. * Add support for optional slices This commit adds support for optional slice types, things like strings and arrays. The null representation of these has a pointer value of 0, which should never happen in normal Rust. Otherwise the various plumbing is done throughout the tooling to enable these types in all locations. * Fix `takeObject` on global sentinels These don't have a reference count as they're always expected to work, so avoid actually dropping a reference on them. * Remove some no longer needed bindings * Add support for optional anyref types This commit adds support for optional imported class types. Each type imported with `#[wasm_bindgen]` automatically implements the relevant traits and now supports `Option<Foo>` in various argument/return positions. * Fix building without the `std` feature * Actually fix the build... * Add support for optional types to WebIDL Closes #502 |
||
---|---|---|
crates | ||
examples | ||
guide | ||
releases | ||
src | ||
tests/all | ||
.appveyor.yml | ||
.eslintignore | ||
.eslintrc | ||
.gitattributes | ||
.gitignore | ||
.travis.yml | ||
Cargo.toml | ||
CHANGELOG.md | ||
CONTRIBUTING.md | ||
LICENSE-APACHE | ||
LICENSE-MIT | ||
package-lock.json | ||
package.json | ||
README.md | ||
yarn.lock |
wasm-bindgen
Facilitating high-level interactions between wasm modules and JavaScript.
Introduction blog post: "JavaScript to Rust and Back Again: A wasm-bindgen
Tale"
Import JavaScript things into Rust and export Rust things to JavaScript.
src/lib.rs
:
#![feature(use_extern_macros, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
Use exported Rust things from JavaScript!
index.js
:
// Asynchronously load, compile, and import the Rust's WebAssembly
// and JavaScript interface.
import("./hello_world").then(module => {
// Alert "Hello, World!"
module.greet("World!");
});
Guide
📚 Read the wasm-bindgen
guide here! 📚
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
See the "Contributing" section of the guide for information on
hacking on wasm-bindgen
!
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.