forked from rustwasm/wasm-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e894fc
commit dd054fa
Showing
11 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package-lock.json | ||
dom.js | ||
dom_bg.js | ||
dom_bg.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "dom" | ||
version = "0.1.0" | ||
authors = ["Alex Crichton <alex@alexcrichton.com>"] | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
wasm-bindgen = { path = "../.." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# DOM access | ||
|
||
This directory is an example of using the `#[wasm_bindgen]` macro to interact | ||
with the DOM, specifically the `document` object. You'll see here a few examples | ||
of defining access to the global `document` variable as well as appending a new | ||
HTML node to it. | ||
|
||
You can build the example with: | ||
|
||
``` | ||
$ ./build.sh | ||
``` | ||
|
||
(or running the commands on Windows manually) | ||
|
||
and then opening up `index.html` in a web browser should show a hello message on | ||
the web page generated by the wasm. | ||
|
||
For more information about this example be sure to check out | ||
[`hello_world`][hello] which also has more comments about caveats and such. | ||
|
||
[hello]: https://github.com/alexcrichton/wasm-bindgen/tree/master/examples/hello_world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/sh | ||
|
||
# For more coments about what's going on here, see the `hello_world` example | ||
|
||
set -ex | ||
|
||
cargo +nightly build --target wasm32-unknown-unknown | ||
cargo +nightly run --manifest-path ../../crates/wasm-bindgen-cli/Cargo.toml \ | ||
--bin wasm-bindgen -- \ | ||
../../target/wasm32-unknown-unknown/debug/dom.wasm --out-dir . | ||
npm install | ||
npm run serve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/> | ||
</head> | ||
<body> | ||
<script src='./index.js'></script> | ||
<p>A greeting from rust looks like...</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// For more comments about what's going on here, check out the `hello_world` | ||
// example | ||
const rust = import("./dom"); | ||
rust.then(m => m.run()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"scripts": { | ||
"serve": "webpack-dev-server" | ||
}, | ||
"devDependencies": { | ||
"webpack": "^4.0.1", | ||
"webpack-cli": "^2.0.10", | ||
"webpack-dev-server": "^3.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#![feature(proc_macro)] | ||
|
||
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)] | ||
fn set_innerHTML(this: &Element, html: &str); | ||
#[wasm_bindgen(method)] | ||
fn appendChild(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_innerHTML("Hello from Rust!"); | ||
document.body().appendChild(val); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: "./index.js", | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "index.js", | ||
}, | ||
mode: "development" | ||
}; |