diff --git a/__wasm/deno_rust_wasm_import_functions/.gitignore b/__wasm/deno_rust_wasm_import_functions/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/.gitignore @@ -0,0 +1 @@ +/target diff --git a/__wasm/deno_rust_wasm_import_functions/.rustfmt.toml b/__wasm/deno_rust_wasm_import_functions/.rustfmt.toml new file mode 100644 index 0000000..9bb8d9d --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/.rustfmt.toml @@ -0,0 +1,3 @@ +max_width = 80 +tab_spaces = 2 +edition = "2021" diff --git a/__wasm/deno_rust_wasm_import_functions/Cargo.lock b/__wasm/deno_rust_wasm_import_functions/Cargo.lock new file mode 100644 index 0000000..1e925d7 --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/Cargo.lock @@ -0,0 +1,126 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bumpalo" +version = "3.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "proc-macro2" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rs_lib" +version = "0.0.0" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "syn" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" + +[[package]] +name = "wasm-bindgen" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c53b543413a17a202f4be280a7e5c62a1c69345f5de525ee64f8cfdbc954994" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5491a68ab4500fa6b4d726bd67408630c3dbe9c4fe7bda16d5c82a1fd8c7340a" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c441e177922bc58f1e12c022624b6216378e5febc2f0533e41ba443d505b80aa" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a89911bd99e5f3659ec4acf9c4d93b0a90fe4a2a11f15328472058edc5261be" diff --git a/__wasm/deno_rust_wasm_import_functions/Cargo.toml b/__wasm/deno_rust_wasm_import_functions/Cargo.toml new file mode 100644 index 0000000..3c579d8 --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +members = [ + "rs_lib", +] diff --git a/__wasm/deno_rust_wasm_import_functions/deno.json b/__wasm/deno_rust_wasm_import_functions/deno.json new file mode 100644 index 0000000..ea91683 --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/deno.json @@ -0,0 +1,5 @@ +{ + "tasks": { + "wasmbuild": "deno run -A https://deno.land/x/wasmbuild@0.8.2/main.ts" + } +} diff --git a/__wasm/deno_rust_wasm_import_functions/justfile b/__wasm/deno_rust_wasm_import_functions/justfile new file mode 100644 index 0000000..2c77c71 --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/justfile @@ -0,0 +1,9 @@ +_: + @just --list + +build_wasm: + deno task wasmbuild + +run *ARGS: + deno run --allow-read main.js {{ ARGS }} + diff --git a/__wasm/deno_rust_wasm_import_functions/lib/rs_lib.generated.js b/__wasm/deno_rust_wasm_import_functions/lib/rs_lib.generated.js new file mode 100644 index 0000000..5167395 --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/lib/rs_lib.generated.js @@ -0,0 +1,148 @@ +// @generated file from wasmbuild -- do not edit +// deno-lint-ignore-file +// deno-fmt-ignore-file +// source-hash: 509bffc30c487fb405bbe67e9580cb31f09f9858 +let wasm; +let cachedInt32Memory0; + +const cachedTextDecoder = new TextDecoder("utf-8", { + ignoreBOM: true, + fatal: true, +}); + +cachedTextDecoder.decode(); + +let cachedUint8Memory0; +function getUint8Memory0() { + if (cachedUint8Memory0.byteLength === 0) { + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8Memory0; +} + +function getStringFromWasm0(ptr, len) { + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); +} +/** + * @param {number} a + * @param {number} b + */ +export function alert_sum(a, b) { + wasm.alert_sum(a, b); +} + +const imports = { + __wbindgen_placeholder__: { + __wbg_alert_1d2369aa1e130bee: function (arg0, arg1) { + alert(getStringFromWasm0(arg0, arg1)); + }, + }, +}; + +const wasm_url = new URL("rs_lib_bg.wasm", import.meta.url); + +/** + * Decompression callback + * + * @callback decompressCallback + * @param {Uint8Array} compressed + * @return {Uint8Array} decompressed + */ + +/** Instantiates an instance of the Wasm module returning its functions. + * @remarks It is safe to call this multiple times and once successfully + * loaded it will always return a reference to the same object. + * @param {decompressCallback=} transform + */ +export async function instantiate(transform) { + return (await instantiateWithInstance(transform)).exports; +} + +let instanceWithExports; +let lastLoadPromise; + +/** Instantiates an instance of the Wasm module along with its exports. + * @remarks It is safe to call this multiple times and once successfully + * loaded it will always return a reference to the same object. + * @param {decompressCallback=} transform + * @returns {Promise<{ + * instance: WebAssembly.Instance; + * exports: { alert_sum: typeof alert_sum } + * }>} + */ +export function instantiateWithInstance(transform) { + if (instanceWithExports != null) { + return Promise.resolve(instanceWithExports); + } + if (lastLoadPromise == null) { + lastLoadPromise = (async () => { + try { + const instance = (await instantiateModule(transform)).instance; + wasm = instance.exports; + cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + instanceWithExports = { + instance, + exports: getWasmInstanceExports(), + }; + return instanceWithExports; + } finally { + lastLoadPromise = null; + } + })(); + } + return lastLoadPromise; +} + +function getWasmInstanceExports() { + return { alert_sum }; +} + +/** Gets if the Wasm module has been instantiated. */ +export function isInstantiated() { + return instanceWithExports != null; +} + +async function instantiateModule(transform) { + switch (wasm_url.protocol) { + case "file:": { + if (typeof Deno !== "object") { + throw new Error("file urls are not supported in this environment"); + } + + if ("permissions" in Deno) { + Deno.permissions.request({ name: "read", path: wasm_url }); + } + const wasmCode = await Deno.readFile(wasm_url); + return WebAssembly.instantiate( + !transform ? wasmCode : transform(wasmCode), + imports, + ); + } + case "https:": + case "http:": { + if (typeof Deno === "object" && "permissions" in Deno) { + Deno.permissions.request({ name: "net", host: wasm_url.host }); + } + const wasmResponse = await fetch(wasm_url); + if (transform) { + const wasmCode = new Uint8Array(await wasmResponse.arrayBuffer()); + return WebAssembly.instantiate(transform(wasmCode), imports); + } + if ( + wasmResponse.headers.get("content-type")?.toLowerCase().startsWith( + "application/wasm", + ) + ) { + return WebAssembly.instantiateStreaming(wasmResponse, imports); + } else { + return WebAssembly.instantiate( + await wasmResponse.arrayBuffer(), + imports, + ); + } + } + default: + throw new Error(`Unsupported protocol: ${wasm_url.protocol}`); + } +} diff --git a/__wasm/deno_rust_wasm_import_functions/lib/rs_lib_bg.wasm b/__wasm/deno_rust_wasm_import_functions/lib/rs_lib_bg.wasm new file mode 100644 index 0000000..b99af63 Binary files /dev/null and b/__wasm/deno_rust_wasm_import_functions/lib/rs_lib_bg.wasm differ diff --git a/__wasm/deno_rust_wasm_import_functions/main.js b/__wasm/deno_rust_wasm_import_functions/main.js new file mode 100644 index 0000000..bef5642 --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/main.js @@ -0,0 +1,8 @@ +import { instantiate } from "./lib/rs_lib.generated.js"; +const { alert_sum } = await instantiate(); + +async function main() { + alert_sum(100, 200); +} + +await main(); diff --git a/__wasm/deno_rust_wasm_import_functions/rs_lib/Cargo.toml b/__wasm/deno_rust_wasm_import_functions/rs_lib/Cargo.toml new file mode 100644 index 0000000..128f45f --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/rs_lib/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "rs_lib" +version = "0.0.0" +edition = "2021" + +[lib] +crate_type = ["cdylib"] + +[profile.release] +codegen-units = 1 +incremental = true +lto = true +opt-level = "z" + +[dependencies] +wasm-bindgen = "=0.2.81" diff --git a/__wasm/deno_rust_wasm_import_functions/rs_lib/src/lib.rs b/__wasm/deno_rust_wasm_import_functions/rs_lib/src/lib.rs new file mode 100644 index 0000000..6f2d9e3 --- /dev/null +++ b/__wasm/deno_rust_wasm_import_functions/rs_lib/src/lib.rs @@ -0,0 +1,12 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern { + fn alert(s: &str); +} + +#[wasm_bindgen] +pub fn alert_sum(a: i32, b: i32) { + alert(&format!("{} plus {} is {}", a, b, a + b)); +} +