emscripten-core/emscripten

preInit (and maybe onRuntimeInitialized?) should support Promises/async

Open

#14,520 opened on 2021年6月23日

GitHub で見る
 (8 comments) (1 reaction) (0 assignees)C++ (3,519 forks)batch import
help wanted

Repository metrics

Stars
 (27,361 stars)
PR merge metrics
 (平均マージ 19d 10h) (30d で 147 merged PRs)

説明

Because of how async works in JS (your function just starts returning a Promise) it's easy for someone to make a preInit or onRuntimeInitialized handler async and not notice that the behavior is subtly wrong, introducing a race condition. Handling this by hand using runtime dependencies is a little tricky because of how promise fulfillment works (next event loop turn) vs how runtime dependencies work (as soon as they're all gone, the callback is invoked synchronously), so you can accidentally introduce a new race when doing it. If preInit (and potentially onRuntimeInitialized) were updated to notice if the handler returned a thenable it would be possible for all this to just work and it would reduce the risk of people adding races by misusing runtime dependencies or forgetting about them entirely.

I think implementing this would just mean changing the loop at the bottom of generated JS that runs preInit functions so that it notices thenables and chains off .then if it encounters one, and then do the final run invoke in the last callback.

コントリビューターガイド