1
- #![ feature( use_extern_macros) ]
2
-
3
1
#[ macro_use]
4
2
extern crate cfg_if;
5
3
6
4
extern crate wasm_bindgen;
5
+ extern crate web_sys;
7
6
use wasm_bindgen:: prelude:: * ;
8
7
9
8
cfg_if ! {
@@ -25,37 +24,21 @@ cfg_if! {
25
24
}
26
25
}
27
26
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
-
54
27
// Called by our JS entry point to run the example
55
28
#[ 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" ) ?;
58
38
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 ( ( ) )
60
43
}
61
44
0 commit comments