graaljs icon indicating copy to clipboard operation
graaljs copied to clipboard

ReferenceError: WebAssembly is not defined

Open oroppas opened this issue 3 years ago • 2 comments

It appears WebAssembly.Memory is supported

https://github.com/oracle/graaljs/blob/86f978c3bbf35a8fa0ab3289e01230a940448213/graal-nodejs/deps/v8/src/wasm/wasm-js.cc#L1126

but the following lines failed. Do I miss something obvious?

[ryuta@fedora cowasm]$ ~/packages/graalvm/bin/npx --experimental-options --js.webassembly --polyglot --jvm python-wasm@latest
OpenJDK VM warning: the use of signal() and sigset() for signal chaining was deprecated in version 16.0 and will be removed in a future release. Use sigaction() instead.
/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/@cowasm/kernel/dist/wasm/worker/import.js:57
    const memory = new WebAssembly.Memory({
                       ^

ReferenceError: WebAssembly is not defined
    at doWasmImport (/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/@cowasm/kernel/dist/wasm/worker/import.js:57:24)
    at debounced (/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/@cowasm/kernel/dist/wasm/reuseInFlight.js:35:37)
    at wasmImportNode (/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/@cowasm/kernel/dist/wasm/worker/node.js:96:18)
    at async createSyncKernel (/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/@cowasm/kernel/dist/kernel/kernel.js:17:27)
    at async syncKernel (/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/@cowasm/kernel/dist/kernel/node.js:48:26)
    at async createPython (/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/python-wasm/dist/node.js:50:17)
    at async syncPython (/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/python-wasm/dist/node.js:25:19)
    at async main (/home/ryuta/.npm/_npx/a6b69408c769c0a5/node_modules/python-wasm/dist/node-terminal.js:20:26)

oroppas avatar Dec 12 '22 02:12 oroppas

Try adding this to your environment variable:

NODE_OPTIONS=--polyglot
NODE_POLYGLOT_OPTIONS=--js.webassembly --experimental-options

clemorej avatar Dec 12 '22 10:12 clemorej

Thanks for the tip! It worked albeit it took about 5 min or so and node allocated about 53 GB of memory at peak.

[ryuta@fedora cowasm]$ NODE_OPTIONS='--polyglot' NODE_POLYGLOT_OPTIONS='--js.webassembly --experimental-options'  ~/packages/graalvm/bin/npx python-wasm@latest
[To redirect Truffle log output to a file use one of the following options:
* '--log.file=<path>' if the option is passed using a guest language launcher.
* '-Dpolyglot.log.file=<path>' if the option is passed using the host Java launcher.
* Configure logging using the polyglot embedding API.]
[java] WARNING: Disabling multi-threading since the context seems to contain single-threaded languages: [python, wasm, js]
[java] WARNING: Thread.start() called on java.lang.ref.Reference$ReferenceHandler / Reference Handler but thread support is disabled: context seems to contain single-threaded languages: [python, wasm, js]
[java] WARNING: Thread.start() called on java.lang.ref.Finalizer$FinalizerThread / Finalizer but thread support is disabled: context seems to contain single-threaded languages: [python, wasm, js]
[To redirect Truffle log output to a file use one of the following options:
* '--log.file=<path>' if the option is passed using a guest language launcher.
* '-Dpolyglot.log.file=<path>' if the option is passed using the host Java launcher.
* Configure logging using the polyglot embedding API.]
[java] WARNING: Disabling multi-threading since the context seems to contain single-threaded languages: [python, wasm, js]
[java] WARNING: Thread.start() called on java.lang.ref.Reference$ReferenceHandler / Reference Handler but thread support is disabled: context seems to contain single-threaded languages: [python, wasm, js]
[java] WARNING: Thread.start() called on java.lang.ref.Finalizer$FinalizerThread / Finalizer but thread support is disabled: context seems to contain single-threaded languages: [python, wasm, js]
OpenJDK VM warning: the use of signal() and sigset() for signal chaining was deprecated in version 16.0 and will be removed in a future release. Use sigaction() instead.
Python 3.11.0 (main, Nov 29 2022, 20:26:05) [Clang 15.0.3 ([email protected]:ziglang/zig-bootstrap.git 0ce789d0f7a4d89fdc4d9571 on wasi
Type "help", "copyright", "credits" or "license" for more information.
>>> 

oroppas avatar Dec 12 '22 12:12 oroppas

WebAssembly should work out-of-the-box in GraalNodeJS now, i.e. since 24.0.0. I can confirm that npx python-wasm@latest now works, albeit it's very slow to start as you've noticed. We should investigate why. Most likely this can be improved significantly. As for the original issue, I consider this fixed.

woess avatar Apr 17 '24 01:04 woess

I've looked into why it's so slow and found 2 performance issues in the JS↔WASM interface, fixed in https://github.com/oracle/graaljs/commit/edbfa8e109dddfe460e46bb0afe5497946b162ed. It should start up much faster now, within seconds not minutes.

woess avatar Apr 24 '24 14:04 woess