Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utf8Encode failing on undefined #164

Closed
ctaggart opened this issue Nov 29, 2024 · 2 comments · Fixed by #167
Closed

utf8Encode failing on undefined #164

ctaggart opened this issue Nov 29, 2024 · 2 comments · Fixed by #167

Comments

@ctaggart
Copy link

ctaggart commented Nov 29, 2024

~/ms/StarlingMonkey15/cowsay> bunx jco componentize cowsay.js --wit ../wit -o cowsay.wasm
(jco componentize) TypeError: expected a string, not type: undefined
    at utf8Encode (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/componentize-js/lib/spidermonkey-embedding-splicer.js:118:11)
    at spliceBindings (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/componentize-js/lib/spidermonkey-embedding-splicer.js:3760:47)
    at componentize (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/componentize-js/src/componentize.js:87:64)
    at async componentize (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/jco/src/cmd/componentize.js:11:25)
    at async file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/jco/src/jco.js:200:9

The function that is throwing is:

function utf8Encode(s, realloc, memory) {
  if (typeof s !== 'string') throw new TypeError('expected a string');
  if (s.length === 0) {
    utf8EncodedLen = 0;
    return 1;
  }
  let buf = utf8Encoder.encode(s);
  let ptr = realloc(0, 0, 1, buf.length);
  new Uint8Array(memory.buffer).set(buf, ptr);
  utf8EncodedLen = buf.length;
  return ptr;
}

If I hack it to return if undefined with this code:

function utf8Encode(s, realloc, memory) {
  if (!s){
    utf8EncodedLen = 0;
    return 1;
  }
  if (typeof s !== 'string'){
    throw new TypeError(`expected a string, not type: ${typeof s}`);
  }
  if (s.length === 0) {
    utf8EncodedLen = 0;
    return 1;
  }
  let buf = utf8Encoder.encode(s);
  let ptr = realloc(0, 0, 1, buf.length);
  new Uint8Array(memory.buffer).set(buf, ptr);
  utf8EncodedLen = buf.length;
  return ptr;
}

Then I get this error:

~/ms/StarlingMonkey15/cowsay> bunx jco componentize cowsay.js --wit ../wit -o cowsay.wasm
Exception while evaluating top-level script
cowsay.js:28:47 SyntaxError: expected expression, got '.':
cowsay.js:28:47 var __require = /* @__PURE__ */ createRequire(./import.meta.js.url);
cowsay.js:28:47 ..............................................^
Error: the `componentize.wizer` function trapped

Caused by:
    0: error while executing at wasm backtrace:
           0: 0x78de33 - <unknown>!<wasm function 12690>
           1: 0x78e7a6 - <unknown>!<wasm function 12706>
           2: 0x20556 - <unknown>!<wasm function 103>
           3: 0x1f511 - <unknown>!<wasm function 101>
           4: 0xa692 - <unknown>!<wasm function 83>
           5: 0x225619 - <unknown>!<wasm function 5009>
    1: Exited with i32 exit status 1
(jco componentize) Error: Failed to initialize the compiled Wasm binary with Wizer:
Wizering failed to complete
    at componentize (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/componentize-js/src/componentize.js:271:13)
    at async componentize (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/jco/src/cmd/componentize.js:11:25)
    at async file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/jco/src/jco.js:200:9

The line it fails on does not have a .:

var __require = /* @__PURE__ */ createRequire(import.meta.url);
cataggar pushed a commit to cataggar/StarlingMonkey15 that referenced this issue Nov 29, 2024
@ctaggart
Copy link
Author

Steps to reproduce:

git clone [email protected]:cataggar/StarlingMonkey15.git --no-checkout
cd StarlingMonkey15
git checkout dd80430e13a3c032662341ea3f97a8c8ab33b3cc
cd cowsay
bun install
bunx jco componentize cowsay-node.js --wit ../wit/cowsay.wit -o cowsay.wasm

cowsay-node.js targets node instead of the browser, which I'm expecting to bundle the node implementation of @bytecodealliance/preview2-shim instead of the browser version. Using the bundle that target the browser does not have the problem:

~/tmp/StarlingMonkey15/cowsay> bunx jco componentize cowsay-browser.js --wit ../wit/cowsay.wit -o cowsay.wasm
OK Successfully written cowsay.wasm.

@guybedford
Copy link
Collaborator

Thanks, I've posted a fix in #164.

There's a remaining bug in this workflow due to the node:module dependency not being supported in ComponentizeJS. In future that would be resolved by bytecodealliance/StarlingMonkey#188, but for now you'll need to stub your own empty implementation internally before passing to ComponentizeJS.

We should definitely have better resolution errors here as well, and my apologies for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants