bytecodealliance / ComponentizeJS

JS -> WebAssembly Component
Apache License 2.0
242 stars 32 forks source link

Getting `TypeError: this is undefined` #58

Closed jorge-vasquez-2301 closed 8 months ago

jorge-vasquez-2301 commented 1 year ago

I'm trying to componentize the following JS file generated by the Scala.js compiler (Scala 3.3.1):

https://gist.github.com/jorge-vasquez-2301/76ba147e39fda32aab07beae552a9dbb

The WIT file is like:

package golem:template

interface api {
  hello: func(name: string) -> ()
}

world hello {
  export api
}

When I try to run the component after transpiling it with jco, I get the following error:

source.js:1225:3 TypeError: this is undefined
Redirecting call to abort() to mozalloc_abort

wasm://wasm/01bbaa36:1

RuntimeError: unreachable
    at wasm://wasm/01bbaa36:wasm-function[6442]:0x4ad9bb
    at wasm://wasm/01bbaa36:wasm-function[7049]:0x4b0604
    at wasm://wasm/01bbaa36:wasm-function[516]:0x279137
    at golem:template/api#hello (wasm://wasm/01bbaa36:wasm-function[6384]:0x4ad19e)
    at Object.hello (file:///Users/jorgevasquez/dev/projects/scala-shopping-cart/cart/component.js:881:39)
    at [eval]:1:59

By the way, when I run the JS file without componentizing, everything works as expected

guybedford commented 1 year ago

@jorge-vasquez-2301 in JS modules, this is not defined. Whereas in JS scripts, this is the global object. You therefore need to replace the top-level this references in that file with an actual global reference or an empty object.

jorge-vasquez-2301 commented 1 year ago

ohh ok, that makes sense. Thank you @guybedford!