book icon indicating copy to clipboard operation
book copied to clipboard

Universe.new is not a function

Open jp3492 opened this issue 4 years ago • 8 comments

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" webpack:///./index.js?:6 js http://localhost:8080/0.bootstrap.js:34 webpack_require http://localhost:8080/bootstrap.js:81 bootstrap.js:5:23 webpack:///./bootstrap.js?:5

jp3492 avatar Mar 03 '20 13:03 jp3492

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?

mydnicq avatar Mar 06 '20 13:03 mydnicq

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.

mydnicq avatar Mar 06 '20 18:03 mydnicq

Awesome, just got me unstuck. To add on to this, #[wasm_bindgen] is only needed in blocks containing public methods. see #164.

collinco avatar Mar 17 '20 05:03 collinco

Thanks @tadejstanic This snagged me too!

JamieCrisman avatar Oct 20 '20 04:10 JamieCrisman

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?

YesSeri avatar Jan 02 '21 15:01 YesSeri

This is definitely the same issue as #265

Linking them to "link all the things"

Lazerbeak12345 avatar Nov 20 '21 01:11 Lazerbeak12345

As for the canvas part of the issue, it worked for me.

Lazerbeak12345 avatar Nov 20 '21 01:11 Lazerbeak12345

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.

nft2 avatar Apr 06 '22 12:04 nft2