bytecodealliance / ComponentizeJS

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

Is there a standard convention for exporting a "main" function #42

Closed GordonSmith closed 8 months ago

GordonSmith commented 1 year ago

When creating a wasm file with c++ and wit, it is possible to export a "main" function, which is invoked by default - is there equivilant with JS?

guybedford commented 1 year ago

In WASI, this is called the "command" model, and we don't currently support this model in ComponentizeJS for creating JS components. We definitely should though, this would be interesting to explore further. It would be a flag like command: true which when set would allow either a special exported function like export function main(args) {} or otherwise.

guybedford commented 1 year ago

I've updated the test in https://github.com/bytecodealliance/componentize-js/blob/main/test/test.js#L134 to export a standard WASI CLI main "run" function, as defined in https://github.com/bytecodealliance/componentize-js/blob/main/test/wit/test1.wit.

This is about as standard as it gets.

The only part missing here is having the ability to define a "main" app as being the top-level execution of the JS, but the problem with that is we likely don't want that as there are benefits to the preinitialize process doing the top-level execution.

So I think that's probably as good as this gets? Further suggestions welcome!