mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-05-07 19:42:16 +00:00
Merge pull request #1044 from ibaryshnikov/dom-example-without-bundlers
Added web-sys dom example withot npm and webpack
This commit is contained in:
commit
6fd33a73fd
@ -8,3 +8,13 @@ crate-type = ["cdylib"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
wasm-bindgen = "0.2.28"
|
wasm-bindgen = "0.2.28"
|
||||||
|
|
||||||
|
[dependencies.web-sys]
|
||||||
|
version = "0.3.4"
|
||||||
|
features = [
|
||||||
|
'Document',
|
||||||
|
'Element',
|
||||||
|
'HtmlElement',
|
||||||
|
'Node',
|
||||||
|
'Window',
|
||||||
|
]
|
||||||
|
@ -20,19 +20,15 @@
|
|||||||
<script src='./no_modules.js'></script>
|
<script src='./no_modules.js'></script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// the `wasm_bindgen` global is set to the exports of the Rust module
|
window.addEventListener('load', async () => {
|
||||||
const { greet } = wasm_bindgen;
|
// the `wasm_bindgen` global is set to the exports of the Rust module
|
||||||
|
//
|
||||||
// we'll defer our execution until the wasm is ready to go
|
// here we tell bindgen the path to the wasm file so it can run
|
||||||
function run() {
|
// initialization and return to us a promise when it's done
|
||||||
greet('World');
|
// also, we can use 'await' on the returned promise
|
||||||
}
|
await wasm_bindgen('./no_modules_bg.wasm');
|
||||||
|
wasm_bindgen.run();
|
||||||
// here we tell bindgen the path to the wasm file so it can run
|
});
|
||||||
// initialization and return to us a promise when it's done
|
|
||||||
wasm_bindgen('./no_modules_bg.wasm')
|
|
||||||
.then(run)
|
|
||||||
.catch(console.error);
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
extern crate wasm_bindgen;
|
extern crate wasm_bindgen;
|
||||||
|
extern crate web_sys;
|
||||||
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
// Called by our JS entry point to run the example
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
pub fn run() -> Result<(), JsValue> {
|
||||||
fn alert(s: &str);
|
// 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]
|
// Manufacture the element we're gonna append
|
||||||
pub fn greet(name: &str) {
|
let val = document.create_element("p")?;
|
||||||
alert(&format!("Hello, {}!", name));
|
val.set_inner_html("Hello from Rust!");
|
||||||
|
|
||||||
|
body.append_child(&val)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user