mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 12:31:22 +00:00
Reorganize and rewrite examples
This commit is a large-ish scale reorganization of our examples. The main goal here is to have a dedicated section of the guide for example, and all examples will be listed there. Each example's `README` is now just boilerplate pointing at the guide along with a blurb about how to run it. Some examples like `math` and `smorgasboard` have been deleted as they didn't really serve much purpose, and others like `closures` have been rewritten with `web-sys` instead of hand-bound bindings. Overall it's hoped that this puts us in a good and consistent state for our examples, with all of them being described in the guide, excerpts are in the guide, and they're all relatively idiomatically using `web-sys`.
This commit is contained in:
2
examples/wasm2js/.gitignore
vendored
Normal file
2
examples/wasm2js/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
package-lock.json
|
||||
wasm2js*
|
14
examples/wasm2js/Cargo.toml
Normal file
14
examples/wasm2js/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "wasm2js"
|
||||
version = "0.1.0"
|
||||
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
# Here we're using a path dependency to use what's already in this repository,
|
||||
# but you'd use the commented out version below if you're copying this into your
|
||||
# project.
|
||||
wasm-bindgen = { path = "../.." }
|
||||
#wasm-bindgen = "0.2"
|
15
examples/wasm2js/README.md
Normal file
15
examples/wasm2js/README.md
Normal file
@ -0,0 +1,15 @@
|
||||
# Converting WebAssembly to JS
|
||||
|
||||
[View documentation for this example online][dox]
|
||||
|
||||
[dox]: https://rustwasm.github.io/wasm-bindgen/examples/wasm2js.html
|
||||
|
||||
You can build the example locally with:
|
||||
|
||||
```
|
||||
$ ./build.sh
|
||||
```
|
||||
|
||||
(or running the commands on Windows manually)
|
||||
|
||||
and then visiting http://localhost:8080 in a browser should run the example!
|
18
examples/wasm2js/build.sh
Executable file
18
examples/wasm2js/build.sh
Executable file
@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -ex
|
||||
|
||||
# Compile our wasm module and run `wasm-bindgen`
|
||||
cargo +nightly build --target wasm32-unknown-unknown --release
|
||||
cargo +nightly run --manifest-path ../../crates/cli/Cargo.toml \
|
||||
--bin wasm-bindgen -- \
|
||||
../../target/wasm32-unknown-unknown/release/wasm2js.wasm --out-dir .
|
||||
|
||||
# Run the `wasm2js` tool from `binaryen`
|
||||
wasm2js wasm2js_bg.wasm -o wasm2js_bg.js
|
||||
|
||||
# Move our original wasm out of the way to avoid cofusing Webpack.
|
||||
mv wasm2js_bg.wasm wasm2js_bg.bak.wasm
|
||||
|
||||
npm install
|
||||
npm run serve
|
8
examples/wasm2js/index.html
Normal file
8
examples/wasm2js/index.html
Normal file
@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
|
||||
</head>
|
||||
<body>
|
||||
<p>Open up the developer console to see "Hello, World!"</p>
|
||||
</body>
|
||||
</html>
|
4
examples/wasm2js/index.js
Normal file
4
examples/wasm2js/index.js
Normal file
@ -0,0 +1,4 @@
|
||||
// Note that since we're not using `WebAssembly` we can do a synchronous import
|
||||
// here!
|
||||
import { run } from './wasm2js';
|
||||
run();
|
11
examples/wasm2js/package.json
Normal file
11
examples/wasm2js/package.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"scripts": {
|
||||
"serve": "webpack-dev-server"
|
||||
},
|
||||
"devDependencies": {
|
||||
"html-webpack-plugin": "^3.2.0",
|
||||
"webpack": "^4.11.1",
|
||||
"webpack-cli": "^2.0.10",
|
||||
"webpack-dev-server": "^3.1.0"
|
||||
}
|
||||
}
|
15
examples/wasm2js/src/lib.rs
Normal file
15
examples/wasm2js/src/lib.rs
Normal file
@ -0,0 +1,15 @@
|
||||
extern crate wasm_bindgen;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// lifted from the `console_log` example
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(s: &str);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn run() {
|
||||
log("Hello, World!");
|
||||
}
|
16
examples/wasm2js/webpack.config.js
Normal file
16
examples/wasm2js/webpack.config.js
Normal file
@ -0,0 +1,16 @@
|
||||
const path = require('path');
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||
|
||||
module.exports = {
|
||||
entry: './index.js',
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
filename: 'index.js',
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
template: "index.html"
|
||||
})
|
||||
],
|
||||
mode: 'development'
|
||||
};
|
Reference in New Issue
Block a user