Skip to content

Commit 2095c7e

Browse files
authored
Merge pull request #15 from walfie/use-web-sys
Use web-sys and remove use_extern_macros
2 parents 7fd3629 + d667562 commit 2095c7e

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

crate/Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ crate-type = ["cdylib"]
1313

1414
[dependencies]
1515
cfg-if = "0.1.5"
16-
wasm-bindgen = "0.2.23"
16+
wasm-bindgen = "0.2.29"
1717

1818
# The `console_error_panic_hook` crate provides better debugging of panics by
1919
# logging them with `console.error`. This is great for development, but requires
@@ -26,5 +26,15 @@ console_error_panic_hook = { version = "0.1.5", optional = true }
2626
# allocator, however.
2727
wee_alloc = { version = "0.4.2", optional = true }
2828

29+
[dependencies.web-sys]
30+
version = "0.3.6"
31+
features = [
32+
'Document',
33+
'Element',
34+
'HtmlElement',
35+
'Node',
36+
'Window',
37+
]
38+
2939
[features]
3040
default-features = ["console_error_panic_hook", "wee_alloc"]

crate/src/lib.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#![feature(use_extern_macros)]
2-
31
#[macro_use]
42
extern crate cfg_if;
53

64
extern crate wasm_bindgen;
5+
extern crate web_sys;
76
use wasm_bindgen::prelude::*;
87

98
cfg_if! {
@@ -25,37 +24,21 @@ cfg_if! {
2524
}
2625
}
2726

28-
// Definitions of the functionality available in JS, which wasm-bindgen will
29-
// generate shims for today (and eventually these should be near-0 cost!)
30-
//
31-
// These definitions need to be hand-written today but the current vision is
32-
// that we'll use WebIDL to generate this `extern` block into a crate which you
33-
// can link and import. There's a tracking issue for this at
34-
// https://github.com/rustwasm/wasm-bindgen/issues/42
35-
//
36-
// In the meantime these are written out by hand and correspond to the names and
37-
// signatures documented on MDN, for example
38-
#[wasm_bindgen]
39-
extern "C" {
40-
type HTMLDocument;
41-
static document: HTMLDocument;
42-
#[wasm_bindgen(method)]
43-
fn createElement(this: &HTMLDocument, tagName: &str) -> Element;
44-
#[wasm_bindgen(method, getter)]
45-
fn body(this: &HTMLDocument) -> Element;
46-
47-
type Element;
48-
#[wasm_bindgen(method, setter = innerHTML)]
49-
fn set_inner_html(this: &Element, html: &str);
50-
#[wasm_bindgen(method, js_name = appendChild)]
51-
fn append_child(this: &Element, other: Element);
52-
}
53-
5427
// Called by our JS entry point to run the example
5528
#[wasm_bindgen]
56-
pub fn run() {
57-
let val = document.createElement("p");
29+
pub fn run() -> Result<(), JsValue> {
30+
// Use `web_sys`'s global `window` function to get a handle on the global
31+
// window object.
32+
let window = web_sys::window().expect("no global `window` exists");
33+
let document = window.document().expect("should have a document on window");
34+
let body = document.body().expect("document should have a body");
35+
36+
// Manufacture the element we're gonna append
37+
let val = document.create_element("p")?;
5838
val.set_inner_html("Hello from Rust, WebAssembly, and Parcel!");
59-
document.body().append_child(val);
39+
40+
body.append_child(&val)?;
41+
42+
Ok(())
6043
}
6144

0 commit comments

Comments
 (0)