gen-host-js: Top-level await compatibility via $init promise#414
gen-host-js: Top-level await compatibility via $init promise#414guybedford merged 7 commits intobytecodealliance:mainfrom
Conversation
alexcrichton
left a comment
There was a problem hiding this comment.
This seems like a reasonable feature, but the internals feel pretty heavy-handed with lots of if/else strewn throughout code generation. Instead of this structure could something be generated along the lines of:
async function init() {
// current init, always using `await`
}
// if --tla-compat
init()
// if not-compat
await init()That way I think just the exports would get the let-vs-otherwise definition treatment?
|
@alexcrichton the problem is I don't know of a single JS optimizer short of closure that will unwrap |
|
I've managed to remove one branch with an update commit. The remaining 6 branches are for:
With the current output size constraints I'm not sure I can simplify more than this, but I'm open to refactoring suggestions. |
|
Ok, I've unified on just the single branch as discussed, leaving any unwrapping to rather be the job of an optimizer. |
This adds a new compatibility feature,
--tla-compatwhich is enabled under the--compatflag as well. Top-level await support is wide, but still gives some gap on browser compatibility (around 5% of browsers support modules but not TLA), so is still useful for maximum JS support.When used, all top-level Wasm loads and instantiations that rely on
awaitget wrapped in a single$initpromise, which is exported and must be awaited before any component functions can be called. This only applies to ESM instance mode and not--instantiation.If calling any exported component functions before initialization has been completed, an error is thrown informing that the
$initpromise must first be awaited. The$initexport name is used as it should be an invalid world export name I believe.