How to import WasmMemory without a bundler?

328 Views Asked by At

I was following the tutorial on making a WASM application with Rust. I decided on not using a bundler, but I stumbled upon a question. When using a bundler, to access WASM's memory, I can simply import { memory } from "<package>/<package>_bg", but without it, I can't make it work. What I'm doing:

import init , * as wasm from "./wasm/inner.js";

async function run() {
    await init();

    window.wasm = wasm;
    wasm.debug_mode();
}

run();

This code works, and I can call Rust's functions easily. The problem is accessing WASM's memory. How do I get a reference to it, or something? Is it even possible? I stumbled upon the possibility of doing let wasm = await init();, but the methods inside this object don't work, neither does the memory object inside of it.

Thanks in advance!

1

There are 1 best solutions below

0
On

You do not need to import memory. You can access memory through let wasm = await init();. E.g.

import init , * as my_inner from "./wasm/inner.js";

let wasm;
let memory;

async function run() {
    wasm = await init();
    memory = wasm.memory;
    window.wasm = my_inner;
    my_inner.debug_mode();
}

run();

Note: I am also learning wasm+rust without npm so my code style may be incorrect e.g. my_inner. I have been importing functions via import { function_name } from ....

I found this information from this tutorial covering using rust+wasm without npm: https://lionturkey.github.io/posts/rustwasm/rustwasm.html