mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-28 15:12:16 +00:00
This commit leverages two new attributes in the Rust compiler, `#[wasm_custom_section]` and `#[wasm_import_module]`. These two attributes allow removing a lot of hacks found in wasm-bindgen and also allows removing the requirement of `wasm-opt` to remove the unused data sections. This does require two new nightly features but we already required the `proc_macro` nightly feature and these will hopefully be stabilized before that feature!
40 lines
1.3 KiB
Rust
40 lines
1.3 KiB
Rust
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
// Definitions of the functionality available in JS, which wasm-bindgen will
|
|
// generate shims for today (and eventually these should be near-0 cost!)
|
|
//
|
|
// These definitions need to be hand-written today but the current vision is
|
|
// that we'll use WebIDL to generate this `extern` block into a crate which you
|
|
// can link and import. There's a tracking issue for this at
|
|
// https://github.com/alexcrichton/wasm-bindgen/issues/42
|
|
//
|
|
// In the meantime these are written out by hand and correspond to the names and
|
|
// signatures documented on MDN, for example
|
|
#[wasm_bindgen]
|
|
extern {
|
|
type HTMLDocument;
|
|
static document: HTMLDocument;
|
|
#[wasm_bindgen(method)]
|
|
fn createElement(this: &HTMLDocument, tagName: &str) -> Element;
|
|
#[wasm_bindgen(method, getter)]
|
|
fn body(this: &HTMLDocument) -> Element;
|
|
|
|
type Element;
|
|
#[wasm_bindgen(method, setter = innerHTML)]
|
|
fn set_inner_html(this: &Element, html: &str);
|
|
#[wasm_bindgen(method, js_name = appendChild)]
|
|
fn append_child(this: &Element, other: Element);
|
|
}
|
|
|
|
// Called by our JS entry point to run the example
|
|
#[wasm_bindgen]
|
|
pub fn run() {
|
|
let val = document.createElement("p");
|
|
val.set_inner_html("Hello from Rust!");
|
|
document.body().append_child(val);
|
|
}
|