This commit is contained in:
2020-01-30 21:11:12 +08:00
parent 66b3d096db
commit 9e8f93b667
9 changed files with 5600 additions and 0 deletions

3
.gitignore vendored
View File

@@ -7,6 +7,9 @@ target/
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
wasm/pkg/
node_modules/
# These are backup files generated by rustfmt
**/*.rs.bk

11
wasm/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "hello-wasm"
version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"

16
wasm/README.md Normal file
View File

@@ -0,0 +1,16 @@
from: https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm
build:
```
wasm-pack build
cd site/
npm install
npm run serve
```
Access: http://localhost:8080/

10
wasm/site/index.html Normal file
View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello-wasm example</title>
</head>
<body>
<script src="./index.js"></script>
</body>
</html>

4
wasm/site/index.js Normal file
View File

@@ -0,0 +1,4 @@
const js = import("./node_modules/hello-wasm/hello_wasm.js");
js.then(js => {
js.greet("WebAssembly");
});

5521
wasm/site/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

13
wasm/site/package.json Normal file
View File

@@ -0,0 +1,13 @@
{
"scripts": {
"serve": "webpack-dev-server"
},
"dependencies": {
"hello-wasm": "../pkg"
},
"devDependencies": {
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10"
}
}

View File

@@ -0,0 +1,9 @@
const path = require('path');
module.exports = {
entry: "./index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "index.js",
},
mode: "development"
};

13
wasm/src/lib.rs Normal file
View File

@@ -0,0 +1,13 @@
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
pub fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}