Skip to content

Commit

Permalink
Merge pull request #373 from nervosnetwork/fix-wasm-example
Browse files Browse the repository at this point in the history
chore: fix wasm example
  • Loading branch information
quake authored Sep 11, 2024
2 parents cabee62 + f3d0aa2 commit 861c1f5
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 89 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ Cargo.lock
**/Cargo.lock
**/bin/
**/pkg/
**/**/www/node_modules/
**/**/www/package-lock.json
**/**/node_modules/
**/**/package-lock.json
tentacle-cov/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ $ cd tentacle && RUST_LOG=info cargo run --example simple --features ws -- serve

2. setup a browser client
```
$ cd simple_wasm/www && wasm-pack build
$ npm install && npm run start
$ cd simple_wasm && wasm-pack build
$ npm install && npm run serve
```

all wasm code generate from [book](https://rustwasm.github.io/docs/book/game-of-life/hello-world.html)
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.68.2
1.75.0
13 changes: 5 additions & 8 deletions simple_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ wasm-bindgen = "0.2.63"
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }

tentacle = { path = "../tentacle", default-features= false, features = ["wasm-timer"]}
# need nightly
talc = { version = "4.4.1", optional = true }
tentacle = { path = "../tentacle", default-features = false, features = [
"wasm-timer",
] }
wasm-bindgen-futures = "0.4"
futures = "0.3"
log = "0.4"
Expand Down
3 changes: 3 additions & 0 deletions simple_wasm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { greet } from './pkg';
debugger
greet();
13 changes: 13 additions & 0 deletions simple_wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"build": "webpack",
"serve": "webpack serve"
},
"devDependencies": {
"@wasm-tool/wasm-pack-plugin": "1.5.0",
"html-webpack-plugin": "^5.6.0",
"webpack": "^5.93.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
}
}
14 changes: 10 additions & 4 deletions simple_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ mod utils;

use wasm_bindgen::prelude::*;

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[cfg(feature = "talc")]
static mut ARENA: [u8; 10000] = [0; 1000000];

#[cfg(feature = "talc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
static ALLOCATOR: talc::Talck<spin::Mutex<()>, talc::ClaimOnOom> = talc::Talc::new(unsafe {
// if we're in a hosted environment, the Rust runtime may allocate before
// main() is called, so we need to initialize the arena automatically
talc::ClaimOnOom::new(talc::Span::from_const_array(core::ptr::addr_of!(ARENA)))
})
.lock();

#[wasm_bindgen]
extern "C" {
Expand Down
22 changes: 22 additions & 0 deletions simple_wasm/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");

module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
},
plugins: [
new HtmlWebpackPlugin(),
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, ".")
}),
],
mode: 'development',
experiments: {
asyncWebAssembly: true
}
};
5 changes: 0 additions & 5 deletions simple_wasm/www/bootstrap.js

This file was deleted.

11 changes: 0 additions & 11 deletions simple_wasm/www/index.html

This file was deleted.

3 changes: 0 additions & 3 deletions simple_wasm/www/index.js

This file was deleted.

39 changes: 0 additions & 39 deletions simple_wasm/www/package.json

This file was deleted.

14 changes: 0 additions & 14 deletions simple_wasm/www/webpack.config.js

This file was deleted.

0 comments on commit 861c1f5

Please sign in to comment.