experimenting with wasm global state

This commit is contained in:
milo 2024-02-23 14:47:38 -05:00
parent 8b5e562c3d
commit b8f8cf5de3
5 changed files with 107 additions and 11 deletions

View File

@ -8,6 +8,12 @@ version = "3.15.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -19,14 +25,28 @@ name = "fish-server-wasm"
version = "0.1.0"
dependencies = [
"wasm-bindgen",
"wasm-bindgen-macro",
"wee_alloc",
]
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "memory_units"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]]
name = "once_cell"
version = "1.19.0"
@ -74,7 +94,7 @@ version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"wasm-bindgen-macro",
]
@ -121,3 +141,37 @@ name = "wasm-bindgen-shared"
version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838"
[[package]]
name = "wee_alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
dependencies = [
"cfg-if 0.1.10",
"libc",
"memory_units",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -11,7 +11,7 @@ crate-type = ["cdylib"]
#fish = { path = "../fish" }
wasm-bindgen = "0.2"
#wasm-bindgen-macro = "0.2"
wasm-bindgen-macro = "0.2"
wee_alloc = "0.4"
# wee_alloc = "0.4"
# console_error_panic_hook = "0.1.7"

View File

@ -1,10 +1,20 @@
import loadFishLibrary from './build/fish_server_wasm.js'
import { default as init, incr } from './build/fish_server_wasm.js'
async function main() {
const { hello } = await loadFishLibrary('/fish_server_wasm_bg.wasm');
await init('/fish_server_wasm_bg.wasm');
document.getElementById("output")
.innerText = `${hello()}`;
let counter = document.getElementById("counter");
let button = document.getElementById("incr");
function incrAndUpdate() {
counter.innerText = `${incr()}`;
}
button.addEventListener('click', () => {
incrAndUpdate();
});
incrAndUpdate();
}
main()

View File

@ -5,7 +5,9 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "cargo build --release && wasm-bindgen target/wasm32-unknown-unknown/release/fish_server_wasm.wasm --out-dir build --target web --no-typescript && npx esbuild index.js --bundle --outdir=build"
"build-wasm": "cargo build --release && wasm-bindgen target/wasm32-unknown-unknown/release/fish_server_wasm.wasm --out-dir build --target web --no-typescript",
"build-js": "npx esbuild index.js --bundle --outdir=build",
"build": "npm run build-wasm && npm run build-js"
},
"author": "iitalics",
"license": "ISC",

View File

@ -1,6 +1,36 @@
use wasm_bindgen::prelude::*;
use core::cell::Cell;
use core::mem::MaybeUninit;
use wasm_bindgen_macro::wasm_bindgen;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
struct Server {
counter: Cell<u32>,
}
impl Server {
fn new() -> Self {
Self {
counter: Cell::new(0),
}
}
fn incr(&self) -> u32 {
self.counter.replace(self.counter.get() + 1)
}
}
static mut SERVER: MaybeUninit<Server> = MaybeUninit::uninit();
#[wasm_bindgen(start)]
fn start() {
let server = Server::new();
unsafe { SERVER.write(server) };
}
#[wasm_bindgen]
pub fn hello() -> i32 {
420
pub fn incr() -> u32 {
let server = unsafe { SERVER.assume_init_ref() };
server.incr()
}