rustwasm / book

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

Hello World does not work as the book describes with current version of wasm-pack. #252

Open b4ux1t3 opened 3 years ago

b4ux1t3 commented 3 years ago

Describe the bug With updated tooling (read: no vulnerabilities in npm audit), wasm-pack produces non-runnable code when following the directions in the book for the hello world program (and as such, for the rest of the book).

To Reproduce Steps to reproduce the behavior:

  1. Follow instructions precisely for the Hello World section.
  2. Receive error in console when browsing to localhost:8080.

Expected behavior Application should display Hello, wasm-game-of-life! in an alert box.

Screenshots Error

Additional context The code generated by wasm-pack exports all members of the JavaScript file ./wasm_game_of_life_bg.js after importing all members of the file ./wasm_game_of_life_bg.wasm, but this doesn't appear to work.

import * as wasm from "./wasm_game_of_life_bg.wasm";
export * from "./wasm_game_of_life_bg.js";

I'm unsure at this time if the issue is with wasm-pack itself, or just a missing step in the book.

The basic functionality works when I directly export the greet function like so in wasm_game_of_life.js:

import * as wasm from "./wasm_game_of_life_bg.wasm";
export function greet() {
    return wasm.greet();
}

This mirrors what the book says should have been generated to begin with.

However, I do not get the ability to pass strings to the function.

wasm-pack version 0.9.1 rustc version 1.48.0

b4ux1t3 commented 3 years ago

Doing some reading and troubleshooting, I was able to produce a slightly different error when explicitly targeting the web in my call to wasm-pack. (wasm-pack build --target=web)

In this case, the wasm_game_of_life.js file gets substantially more content, but it appears that the wasm variable it tries to use never gets declared, leading to the following error:

firefox_JjFmsEhcvp

This doesn't really clear up the original issue, and doesn't lead to a fix or workaround as far as I can tell.

ptdecker commented 1 year ago

Hmm, I didn't have any issues myself. My only problem with the instructions as written is that code changes were not being automatically picked up. This was resolved by the suggestions from #261