rust-webwasm/index.js

27 lines
855 B
JavaScript
Raw Normal View History

2023-11-19 01:45:09 -08:00
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}`;
2023-11-19 02:00:38 -08:00
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
2023-11-19 01:45:09 -08:00
};
runWasm();