Menci/vite-plugin-wasm

wasm-bindgen memory optimization seems to have broken the plugin

Secretmapper opened this issue · 4 comments

Steps to reproduce:

  1. Do the wasm-pack tutorial
  2. Install Vite with this plugin
  3. Get error: Cannot access memory before initialization on the bg file. (Truncated below):
import * as wasm from '/-/hello-wasm/hello_wasm_bg.wasm?import';

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

cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); // Uncaught ReferenceError: Cannot access 'memory' before initialization

The memory checking seems to have been changed since this PR: rustwasm/wasm-bindgen#2886 and I'm guessing the initialization logic is different from what's now needed since the wasm file is referenced in the top level.

Menci commented

Could you please test this WASM binding with Webpack5's asyncWebAssembly and check if Webpack 5 could handle this correctly?

@Menci I've tried to look into it, and in the process it seems like I should have checked the related issues of the one I linked - the bug seems to have been fixed by this PR:

rustwasm/wasm-bindgen#2965

Closing as it's a wasm-bindgen issue.

Could you please test this WASM binding with Webpack5's asyncWebAssembly and check if Webpack 5 could handle this correctly?

I tested that everything was normal in webpack5

Menci commented

Yes, I also found that Webpack 5 could load buggy bindings successfully. But Node.js's native ESM WASM loader fails.