rustwasm / book

The Rust and WebAssembly Book
https://rustwasm.github.io/docs/book/
MIT License
1.75k stars 211 forks source link

Getting to memory.... #313

Closed ptdecker closed 7 months ago

ptdecker commented 7 months ago

Where in the docs did you come across this?

In section 4.4 Implementing Life, canvas rendering section

Describe what about it does not make sense

Receiving error when trying to run

Why does it not make sense?

It doesn't work

How could we improve it?

Suggestions on how to debug

Now that the template preliminaries are out of the way, has anyone been able to get this line to work:

import { memory } from "wasm-game-of-life/wasm_game_of_life_bg";

When I try it, webpack gives me the following error:

export 'memory' (imported as 'memory') was not found in 'wasm-game-of-life/wasm_game_of_life_bg' (possible exports: Cell, Universe, __wbg_set_wasm, __wbindgen_throw)

I inspected the generated wasm_game_of_life_bg.js file and can see nowhere that 'memory' is defined in it.

I do see:

let cachedUint8Memory0 = null;

function getUint8Memory0() {
    if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
        cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
    }
    return cachedUint8Memory0;
}

Do I need to be using getUnit8Memmory0() somehow instead? If so, how??

Thank you for your help in advance

ptdecker commented 7 months ago

Has anyone been able to get the canvas rendering working in 2024?

ptdecker commented 7 months ago

Got the above resolved:

'import { memory } from "wasm-game-of-life/wasm_game_of_life_bg";'

needs to be:

import { memory } from "wasm-game-of-life/wasm_game_of_life_bg.wasm";

No use submitting a PR to fix it given none are being accepted