From 873898e6c0326da6bab7adf6ccc7c5a71489580a Mon Sep 17 00:00:00 2001 From: ibaryshnikov Date: Tue, 27 Nov 2018 14:44:57 +0100 Subject: [PATCH] updated no_modules example to show web-sys usage --- examples/no_modules/Cargo.toml | 10 ++++++++++ examples/no_modules/index.html | 22 +++++++++------------- examples/no_modules/src/lib.rs | 21 +++++++++++++++------ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/examples/no_modules/Cargo.toml b/examples/no_modules/Cargo.toml index b9ca71062..f3cfd68a9 100644 --- a/examples/no_modules/Cargo.toml +++ b/examples/no_modules/Cargo.toml @@ -8,3 +8,13 @@ crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2.28" + +[dependencies.web-sys] +version = "0.3.4" +features = [ + 'Document', + 'Element', + 'HtmlElement', + 'Node', + 'Window', +] diff --git a/examples/no_modules/index.html b/examples/no_modules/index.html index edc583345..90147d23f 100644 --- a/examples/no_modules/index.html +++ b/examples/no_modules/index.html @@ -20,19 +20,15 @@ diff --git a/examples/no_modules/src/lib.rs b/examples/no_modules/src/lib.rs index 3a3634b96..a30af3bc8 100644 --- a/examples/no_modules/src/lib.rs +++ b/examples/no_modules/src/lib.rs @@ -1,13 +1,22 @@ extern crate wasm_bindgen; +extern crate web_sys; use wasm_bindgen::prelude::*; +// Called by our JS entry point to run the example #[wasm_bindgen] -extern "C" { - fn alert(s: &str); -} +pub fn run() -> Result<(), JsValue> { + // Use `web_sys`'s global `window` function to get a handle on the global + // window object. + let window = web_sys::window().expect("no global `window` exists"); + let document = window.document().expect("should have a document on window"); + let body = document.body().expect("document should have a body"); -#[wasm_bindgen] -pub fn greet(name: &str) { - alert(&format!("Hello, {}!", name)); + // Manufacture the element we're gonna append + let val = document.create_element("p")?; + val.set_inner_html("Hello from Rust!"); + + body.append_child(&val)?; + + Ok(()) }