rust-webwasm/index.js
2023-11-19 02:00:38 -08:00

27 lines
855 B
JavaScript

import wasmInit from "./pkg/gamarjoba.js"
const runWasm = async () => {
const rustWasm = await wasmInit("./pkg/gamarjoba_bg.wasm");
const result = rustWasm.de_iavascriptis(0, 1);
console.log("Should be undefined: ", rustWasm.add_integer_with_constant);
document.body.textContent = `Gamarjoba! result: ${result}`;
const NUMBER = 257;
console.log("Write in WASM, read in JS");
rustWasm.store_value_in_wasm_mem_idx_0(NUMBER);
let wasmMemory = new Uint8Array(rustWasm.memory.buffer);
let ptr = rustWasm.get_wasm_mem_buffer_pointer();
console.log("Pointer from rust: ", ptr);
console.log(wasmMemory[ptr+0]); // should be NUMBER, wrapped around 256
console.log("Write in JS, read in wasm");
wasmMemory[ptr+1] = 15;
console.log(rustWasm.read_wasm_mem_buffer_idx_1()); // should be 15
};
runWasm();