fermyon / spin-js-sdk

https://developer.fermyon.com/spin/javascript-components
Apache License 2.0
49 stars 17 forks source link

Can not build wasm file from webpacked javascript #243

Open gianghoang74 opened 2 months ago

gianghoang74 commented 2 months ago

Hi all, I'm having this problem when trying to convert my Javascript file to wasm Screenshot from 2024-05-31 12-35-26 In the error line, it already has semicolon at the end. Screenshot from 2024-05-31 13-25-00

My javascript file is webpacked with webpack version 5. It can run in node environment. Can anyone tell what is the problem? Or is there any way I can have more detail of the error? Thank you and have a nice day

bacongobbler commented 2 months ago

Hi @gianghoang74!

My javascript file is webpacked with webpack version 5. It can run in node environment.

From the documentation, it's noted that the Spin javascript SDK is not fully compatible with the browser or Node.js. It implements only a subset of the API.

Are you able to share the code you're attempting to compile?

gianghoang74 commented 2 months ago

test.zip Hi @bacongobbler, here is the testing file I'm using.

bacongobbler commented 2 months ago

Thanks for the source code. It looks like an app that makes API calls to bitfinex. Doesn't look too complex.

I see a number of calls to the fetch API, which isn't supported by the javascript SDK at this time. However there is development ongoing to add fetch call support to the SDK. fetch API support has been added to StarlingMonkey and fixes are in development; see https://github.com/bytecodealliance/StarlingMonkey/pull/49 for example. We will need to update ComponentizeJS to the latest version (https://github.com/bytecodealliance/ComponentizeJS/pull/111) and release a new version of the SDK pointing to that version of ComponentizeJS. cc @karthik2804

One other thing that stands out to me is the fact that this appears to run as a standalone app. That is, it has a main function that supposedly runs continuously. Is that correct? If so, hoow do you imagine this app would work with the Spin runtime?

EDIT: I was incorrect; the v1 SDK supports the fetch API.

bacongobbler commented 2 months ago

FWIW, I can confirm the same issue on my end:

><> spin js2wasm test.js

Starting to build Spin compatible module
Preinitiating using Wizer
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Uncaught SyntaxError: expecting ';'
    at <input>:17
[...]

If I compile with the v2 SDK (currently alpha, no fetch support atm), I see something different:

><> npm init
><> npm install -D @fermyon/spin-sdk
><> npx j2w -n spin-http -i test.js -o test.wasm
Error: the `componentize.wizer` function trapped

Caused by:
    0: error while executing at wasm backtrace:
           0: 0x730ad2 - <unknown>!<wasm function 8121>
           1: 0x3349b8 - <unknown>!<wasm function 567>
           2: 0xc5fd9 - <unknown>!<wasm function 156>
           3: 0x33217b - <unknown>!<wasm function 563>
           4: 0x334813 - <unknown>!<wasm function 567>
           5: 0x67d6f5 - <unknown>!<wasm function 4698>
           6: 0x4fcc8a - <unknown>!<wasm function 1809>
           7: 0x3349b8 - <unknown>!<wasm function 567>
           8: 0xc5fd9 - <unknown>!<wasm function 156>
           9: 0x33217b - <unknown>!<wasm function 563>
          10: 0x334813 - <unknown>!<wasm function 567>
          11: 0x67d6f5 - <unknown>!<wasm function 4698>
          12: 0x4fcc8a - <unknown>!<wasm function 1809>
          13: 0x3349b8 - <unknown>!<wasm function 567>
          14: 0xc5fd9 - <unknown>!<wasm function 156>
          15: 0x33217b - <unknown>!<wasm function 563>
          16: 0x334813 - <unknown>!<wasm function 567>
          17: 0x67d6f5 - <unknown>!<wasm function 4698>
          18: 0x4fcc8a - <unknown>!<wasm function 1809>
          19: 0x3349b8 - <unknown>!<wasm function 567>
          20: 0xc5fd9 - <unknown>!<wasm function 156>
          21: 0x33217b - <unknown>!<wasm function 563>
          22: 0x334813 - <unknown>!<wasm function 567>
          23: 0x67d6f5 - <unknown>!<wasm function 4698>
          24: 0x4fcc8a - <unknown>!<wasm function 1809>
          25: 0x3349b8 - <unknown>!<wasm function 567>
          26: 0xc5fd9 - <unknown>!<wasm function 156>
          27: 0x33217b - <unknown>!<wasm function 563>
          28: 0x334813 - <unknown>!<wasm function 567>
          29: 0x67d6f5 - <unknown>!<wasm function 4698>
          30: 0x4fcc8a - <unknown>!<wasm function 1809>
          31: 0x3349b8 - <unknown>!<wasm function 567>
          32: 0xc5fd9 - <unknown>!<wasm function 156>
          33: 0x33217b - <unknown>!<wasm function 563>
          34: 0x334813 - <unknown>!<wasm function 567>
          35: 0x67d6f5 - <unknown>!<wasm function 4698>
          36: 0x4fcc8a - <unknown>!<wasm function 1809>
          37: 0x3349b8 - <unknown>!<wasm function 567>
          38: 0xc5fd9 - <unknown>!<wasm function 156>
          39: 0x33217b - <unknown>!<wasm function 563>
          40: 0x334813 - <unknown>!<wasm function 567>
          41: 0x67d6f5 - <unknown>!<wasm function 4698>
          42: 0x4fcc8a - <unknown>!<wasm function 1809>
          43: 0x3349b8 - <unknown>!<wasm function 567>
          44: 0xc5fd9 - <unknown>!<wasm function 156>
          45: 0x33217b - <unknown>!<wasm function 563>
          46: 0x334813 - <unknown>!<wasm function 567>
          47: 0x67d6f5 - <unknown>!<wasm function 4698>
          48: 0x4fcc8a - <unknown>!<wasm function 1809>
          49: 0x3349b8 - <unknown>!<wasm function 567>
          50: 0xc5fd9 - <unknown>!<wasm function 156>
          51: 0x33217b - <unknown>!<wasm function 563>
          52: 0x334813 - <unknown>!<wasm function 567>
          53: 0x67d6f5 - <unknown>!<wasm function 4698>
          54: 0x4fcc8a - <unknown>!<wasm function 1809>
          55: 0x3349b8 - <unknown>!<wasm function 567>
          56: 0xc5fd9 - <unknown>!<wasm function 156>
          57: 0x33217b - <unknown>!<wasm function 563>
          58: 0x62eee0 - <unknown>!<wasm function 3823>
          59: 0x609fb5 - <unknown>!<wasm function 3478>
          60: 0x40348f - <unknown>!<wasm function 965>
          61: 0x4031d8 - <unknown>!<wasm function 965>
          62: 0x7dcb4 - <unknown>!<wasm function 151>
          63: 0x4f0d9d - <unknown>!<wasm function 1757>
          64: 0x32679c - <unknown>!<wasm function 547>
    1: Error: attempted to call an unknown imported function: 'wasi:random/random@0.2.0' 'get-random-u64'

       You cannot call arbitrary imported functions during Wizer initialization.
file:///home/bacongobbler/code/github.com/bacongobbler/testjs/node_modules/@bytecodealliance/componentize-js/src/componentize.js:217
    throw new Error(err);
          ^

Error: Failed to initialize the compiled Wasm binary with Wizer:
Wizering failed to complete
    at componentize (file:///home/bacongobbler/code/github.com/bacongobbler/testjs/node_modules/@bytecodealliance/componentize-js/src/componentize.js:217:11)
    at async file:///home/bacongobbler/code/github.com/bacongobbler/testjs/node_modules/@fermyon/spin-sdk/bin/j2w.mjs:48:23

Node.js v20.13.1
gianghoang74 commented 2 months ago

@bacongobbler Yes, I'm doing some research on how to convert my project to Webassembly. So I create a standalone project like this for testing purpose. For production implementation, I expect to run this in child process or worker instance and send the output through event emit if possible.