add wasm
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -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
11
wasm/Cargo.toml
Normal 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
16
wasm/README.md
Normal 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
10
wasm/site/index.html
Normal 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
4
wasm/site/index.js
Normal 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
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
13
wasm/site/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
9
wasm/site/webpack.config.js
Normal file
9
wasm/site/webpack.config.js
Normal 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
13
wasm/src/lib.rs
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user