Last active
December 26, 2023 00:29
-
-
Save saolsen/d273bb1baba5e912e4dc2b187511affa to your computer and use it in GitHub Desktop.
Rust Val
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [package] | |
| name = "rust_val" | |
| version = "0.1.0" | |
| edition = "2018" | |
| [lib] | |
| crate-type = ["cdylib", "rlib"] | |
| [features] | |
| default = ["console_error_panic_hook"] | |
| [dependencies] | |
| wasm-bindgen = "0.2.84" | |
| console_error_panic_hook = { version = "0.1.7", optional = true } | |
| [dev-dependencies] | |
| wasm-bindgen-test = "0.3.34" | |
| [profile.release] | |
| opt-level = "s" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { readFile, writeFile } from "node:fs/promises"; | |
| const cargoTomlContent = await readFile("./Cargo.toml", "utf8"); | |
| const cargoPackageName = /\[package\]\nname = "(.*?)"/.exec( | |
| cargoTomlContent | |
| )[1]; | |
| const name = cargoPackageName.replace(/-/g, "_"); | |
| const content = await readFile(`./dist/pkg/${name}.js`, "utf8"); | |
| const patched = content | |
| // use global TextDecoder TextEncoder | |
| .replace("require(`util`)", "globalThis") | |
| // attach to `imports` instead of module.exports | |
| .replace("= module.exports", "= imports") | |
| .replace(/\nmodule\.exports\.(.*?)\s+/g, "\nexport const $1 = imports.$1 ") | |
| .replace(/$/, "export default imports") | |
| // inline bytes Uint8Array | |
| .replace( | |
| /\nconst path.*\nconst bytes.*\n/, | |
| ` | |
| var __toBinary = /* @__PURE__ */ (() => { | |
| var table = new Uint8Array(128); | |
| for (var i = 0; i < 64; i++) | |
| table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i; | |
| return (base64) => { | |
| var n = base64.length, bytes = new Uint8Array((n - (base64[n - 1] == "=") - (base64[n - 2] == "=")) * 3 / 4 | 0); | |
| for (var i2 = 0, j = 0; i2 < n; ) { | |
| var c0 = table[base64.charCodeAt(i2++)], c1 = table[base64.charCodeAt(i2++)]; | |
| var c2 = table[base64.charCodeAt(i2++)], c3 = table[base64.charCodeAt(i2++)]; | |
| bytes[j++] = c0 << 2 | c1 >> 4; | |
| bytes[j++] = c1 << 4 | c2 >> 2; | |
| bytes[j++] = c2 << 6 | c3; | |
| } | |
| return bytes; | |
| }; | |
| })(); | |
| const bytes = __toBinary(${JSON.stringify( | |
| await readFile(`./dist/pkg/${name}_bg.wasm`, "base64") | |
| )}); | |
| ` | |
| ); | |
| await writeFile(`./dist/${name}.mjs`, patched); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use wasm_bindgen::prelude::*; | |
| #[wasm_bindgen] | |
| extern "C" { | |
| // Use `js_namespace` here to bind `console.log(..)` instead of just | |
| // `log(..)` | |
| #[wasm_bindgen(js_namespace = console)] | |
| fn log(s: &str); | |
| } | |
| #[wasm_bindgen] | |
| pub fn say_hello() { | |
| log("Hello from Rust!"); | |
| } | |
| #[wasm_bindgen] | |
| pub fn add(x: i32, y: i32) -> i32 { | |
| x + y | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment