plugins: plugin-wasm: Unexpected errors after compilation

  • Rollup Plugin Name: rollup/plugin-wasm
  • Rollup Plugin Version: 6.0.1
  • Rollup Version: 3.3.0
  • Operating System (or Browser): Windows
  • Node Version: 18.10.0
  • Link to reproduction (⚠️ read below):

https://github.com/humanwhocodes/momoa/tree/rust

Note: This requires the Rust toolchain to be installed. You can find the installation directions here: https://github.com/humanwhocodes/momoa/tree/rust#development

  • The main entrypoint is src/index.js
  • The main function exported from wasm is tokenize_js().
  • The output from wasm-bindgen ends up in the build directory.
  • This should build upon running npm install, but you can also manually build using npm run build.

Expected Behavior

Project should compile to api.js in the root of the project, and I should be able to load that into Node.js using require() and then execute tokenize_js("true");

Actual Behavior

I get a lot of warnings during the build (these are incorrect, the file does export all of these symbols):

Output log
memory is not exported by build/momoa_bg.wasm
40: function getUint8Memory0() {
41:     if (cachedUint8Memory0.byteLength === 0) {
42:         cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
                                                     ^
43:     }
44:     return cachedUint8Memory0;
build/momoa_bg.js
memory is not exported by build/momoa_bg.wasm
175: function getInt32Memory0() {
176:     if (cachedInt32Memory0.byteLength === 0) {
177:         cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
                                                      ^
178:     }
179:     return cachedInt32Memory0;
build/momoa_bg.js
__wbindgen_malloc is not exported by build/momoa_bg.wasm
184: */
185: export function tokenize_js(input) {
186:     const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                    ^
187:     const len0 = WASM_VECTOR_LEN;
188:     const ret = wasm.tokenize_js(ptr0, len0);
build/momoa_bg.js
__wbindgen_realloc is not exported by build/momoa_bg.wasm
184: */
185: export function tokenize_js(input) {
186:     const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                                            ^
187:     const len0 = WASM_VECTOR_LEN;
188:     const ret = wasm.tokenize_js(ptr0, len0);
build/momoa_bg.js
tokenize_js is not exported by build/momoa_bg.wasm
186:     const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
187:     const len0 = WASM_VECTOR_LEN;
188:     const ret = wasm.tokenize_js(ptr0, len0);
                          ^
189:     return takeObject(ret);
190: }
build/momoa_bg.js
__wbindgen_malloc is not exported by build/momoa_bg.wasm
243: export function __wbindgen_debug_string(arg0, arg1) {
244:     const ret = debugString(getObject(arg1));
245:     const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                  ^
246:     const len0 = WASM_VECTOR_LEN;
247:     getInt32Memory0()[arg0 / 4 + 1] = len0;
build/momoa_bg.js
__wbindgen_realloc is not exported by build/momoa_bg.wasm
243: export function __wbindgen_debug_string(arg0, arg1) {
244:     const ret = debugString(getObject(arg1));
245:     const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                                          ^
246:     const len0 = WASM_VECTOR_LEN;
247:     getInt32Memory0()[arg0 / 4 + 1] = len0;

Additionally, when I require('./api.js') with Node.js and run tokenize_js("true"), I get an error:

$ node
Welcome to Node.js v18.10.0.
Type ".help" for more information.
> const momoa = require("./api");
undefined
> momoa.tokenize_js("true")
Uncaught TypeError: malloc is not a function
    at passStringToWasm0 (C:\Users\nzaka\projects\humanwhocodes\momoa\api.js:1189:21)   
    at Object.tokenize_js (C:\Users\nzaka\projects\humanwhocodes\momoa\api.js:1227:18)  
>

So while the file api.js is built, it appears to be built in such a way that this function can’t actually be executed.

All of the other JavaScript-based functions work correctly, it’s just the wasm-based function that breaks.

Additional Information

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (16 by maintainers)

Commits related to this issue

Most upvoted comments

No worries. 😄 There are undoubtedly some APIs where async initialization isn’t that big of a deal, but in most cases, JS devs assume APIs are synchronous unless they deal with I/O in some way, and it’s truly rare to have async initialization of synchronous APIs. There’s a lot of muscle memory there to overcome, and I’m trying to avoid the cognitive dissonance.

I need to play around with this some more, though. My ultimate goal is to build a single package that will work equally well for browsers and Node.js (as the current package does). I was hoping to avoid distributing the wasm file but you’ve made some good points about not inlining it.