Tip: Starting from recent emcc / Emscripten Module() now returns a Promise

29 views
Skip to first unread message

Shlomi Fish

unread,
Aug 21, 2020, 8:24:50 AM8/21/20
to emscripte...@googlegroups.com
Hi all!

I'm documenting a recent change in the semantics of Emscripten and emcc
(starting from 1.39.x and also seems to affect 2.0.x): Module() now returns a
promise and one should for example do:

Module().then((result) => {
real_module = result;
})

E.g like in
https://github.com/shlomif/fc-solve/commit/dcdb924262860eb8d023402400fa1e8cd9dce6c2
:

diff --git a/fc-solve/site/wml/src/ts/web-fc-solve-tests.ts
b/fc-solve/site/wml/src/ts/web-fc-solve-tests.ts index f3082e52f..d510bc4fa
100644 --- a/fc-solve/site/wml/src/ts/web-fc-solve-tests.ts
+++ b/fc-solve/site/wml/src/ts/web-fc-solve-tests.ts
@@ -494,7 +494,12 @@ export function test_js_fc_solve_class(qunit: QUnit,
my_callback: () => void) { // var _my_mod = Module({});
const _my_mod = [null];
_my_mod[0] = Module()({
- onRuntimeInitialized: my_func(qunit, _my_mod, my_callback),
+ onRuntimeInitialized: () => {
+ _my_mod[0].then((result) => {
+ _my_mod[0] = result;
+ return my_func(qunit, _my_mod, my_callback)();
+ });
+ },
});
return;
}

(my code has a little weirdness of using "Module()()" instead of "Module()" -
you can ignore that).

For more about js promises, see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

--

Shlomi Fish https://www.shlomifish.org/
Perl Elems to Avoid - https://perl-begin.org/tutorials/bad-elements/

There was one Napoleon, one George Washington, and one me!
— Big Boy Caprice in
https://en.wikiquote.org/wiki/Dick_Tracy_%281990_film%29

Please reply to list if it's a mailing list post - https://shlom.in/reply .

Alon Zakai

unread,
Aug 21, 2020, 3:46:34 PM8/21/20
to emscripte...@googlegroups.com
I believe this is from 1.39.16, the changelog entry (https://github.com/emscripten-core/emscripten/blob/master/ChangeLog.md#13916-05152020) says

====
Change the factory function created by using the MODULARIZE build option to return a Promise instead of the module instance. That is, beforehand

  Module()

would return an instance (which was perhaps not ready yet if startup was async). In the new model, that returns a Promise which you can do .then or await on to get notified when the instance is ready, and the callback receives the instance. Note that both before and after this change doing Module() creates and runs an instance, so the only change is the return value from that call. This fixes some long-standing bugs with that option which have been reported multiple times, but is a breaking change - sorry about that. To reduce the risk of confusing breakage, in a build with ASSERTIONS we will show a clear warning on common errors. For more, see detailed examples for the current usage in src/settings.js on MODULARIZE. (#10697)
====

Thanks Shlomi, good to mention this here as it might help others (I don't think we mentioned it in the mailing list before).

- Alon


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/20200821152439.0cb14ca2%40telaviv1.shlomifish.org.

Shlomi Fish

unread,
Aug 22, 2020, 2:43:05 AM8/22/20
to Alon Zakai, emscripte...@googlegroups.com
Hi Alon and all!

On Fri, 21 Aug 2020 12:46:18 -0700
Alon Zakai <alon...@gmail.com> wrote:

> I believe this is from 1.39.16, the changelog entry (
> https://github.com/emscripten-core/emscripten/blob/master/ChangeLog.md#13916-05152020)
> says
>
> ====
> Change the factory function created by using the MODULARIZE build option to
> return a Promise instead of the module instance. That is, beforehand
>
> Module()
>
> would return an instance (which was perhaps not ready yet if startup was
> async). In the new model, that returns a Promise which you can do .then or
> await on to get notified when the instance is ready, and the callback
> receives the instance. Note that both before and after this change doing
> Module() creates and runs an instance, so the only change is the return
> value from that call. This fixes some long-standing bugs with that option
> which have been reported multiple times, but is a breaking change - sorry
> about that. To reduce the risk of confusing breakage, in a build with
> ASSERTIONS we will show a clear warning on common errors. For more, see
> detailed examples for the current usage in src/settings.js on MODULARIZE.
> (#10697)
> ====
>
> Thanks Shlomi, good to mention this here as it might help others (I don't
> think we mentioned it in the mailing list before).
>

Thanks for the extra info, and - you're welcome.

Regards,

Shlomi
https://www.shlomifish.org/open-source/resources/tech-tips/

Sometimes you don’t need to be familiar with a better alternative to know that
something sucks. Take Microsoft Word for example.
— Shlomi Fish’s friend.
Reply all
Reply to author
Forward
0 new messages