book
book copied to clipboard
Universe.new is not a function
Describe the bug
In the implementing life section I've ran into this error when trying to create a new Universe from the wasm-game-of-life package:
Error importing index.js
: TypeError: "wasm_game_of_life__WEBPACK_IMPORTED_MODULE_0__.Universe.new is not a function"
To Reproduce Follow the tutorial as described
Expected behavior It is supposed to show the Universe rendered into the html page but I receive the error as stated above
Screenshots If applicable, add screenshots to help explain your problem.
Additional context
Error importing index.js
: TypeError: "wasm_game_of_life__WEBPACK_IMPORTED_MODULE_0__.Universe.new is not a function"
I have the same error. Even the typescript definition file doesn't have all Universe's pub methods defined:
export enum Cell {
Dead,
Alive,
}
export class Universe {
free(): void;
}
Don't know from where free
method was taken from?
The root of this problem is in this block of code:
impl Universe {
fn get_index(&self, row: u32, column: u32) -> usize {
(row * self.width + column) as usize
}
// ...
}
What is missing here is #[wasm_bindgen]
annotation before the impl block. Further examples of this block have the required annotation so when you start using this tutorial you can easily overlook this detail.
Awesome, just got me unstuck. To add on to this, #[wasm_bindgen]
is only needed in blocks containing public methods. see #164.
Thanks @tadejstanic This snagged me too!
When I do the tutorial, it gives me an error if I add the #[wasm_bindgen] line. If I don't add it the program compiles, but it doesn't work. Nothing is displayed on screen. When I compile the program with #[wasm_bindgen], it does work in the first part of the tutorial with the <pre> but it doesn't work where it is rendered to the <canvas>.
What should I do?
This is definitely the same issue as #265
Linking them to "link all the things"
As for the canvas part of the issue, it worked for me.
One thing that can cause this problem is forgetting to run wasm-pack build
after you're done the Rust part of the code.
I did this tutorial over multiple days and forgot that was a thing. It produces the same error as OP is describing.