Skip to content

Commit fd647d2

Browse files
authored
MODULARIZE followups after #10697 (#11116)
Get MINIMAL_RUNTIME + MODULARIZE code size back to where it was before that PR, when emitting HTML: the HTML in that mode will create a single instance of the app and run it automatically. It does so without needing the Promise, so we don't need to emit it. Diffing the output JS to before #10697 shows essentially no difference. Also move the changelog entry to the right place (we had a release meanwhile), expand it a little, and ifdef some logging in postamble.js (if we reject the Promise on an error, we log it out that way anyhow).
1 parent 9c8beb9 commit fd647d2

17 files changed

+83
-59
lines changed

ChangeLog.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ See docs/process.md for how version tagging works.
1717

1818
Current Trunk
1919
-------------
20+
- Change the factory function created by using the `MODULARIZE` build option to
21+
return a Promise instead of the module instance. If you use `MODULARIZE` you
22+
will need to wait on the returned Promise, using `await` or its `then`
23+
callback, to get the module instance (#10697). This fixes some long-standing
24+
bugs with that option which have been reported multiple times, but is a
25+
breaking change - sorry about that. See detailed examples for the
26+
current usage in `src/settings.js` on `MODULARIZE`.
2027
- A new `PRINTF_LONG_DOUBLE` option allows printf to print long doubles at full
2128
float128 precision. (#11130)
2229
- `emscripten_async_queue_on_thread` has been renamed to
@@ -43,10 +50,6 @@ Current Trunk
4350

4451
1.39.15: 05/06/2020
4552
-------------------
46-
- Change the factory function created by using the `MODULARIZE` build option to
47-
return a Promise instead of the module instance. If you use `MODULARIZE` you
48-
will need to wait on the returned Promise, using `await` or its `then`
49-
callback, to get the module instance (#10697).
5053
- Add `--extern-pre-js` and `--extern-post-js` emcc flags. Files provided there
5154
are prepended/appended to the final JavaScript output, *after* all other
5255
work has been done, including optimization, optional `MODULARIZE`-ation,

emcc.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,13 @@ def has_c_source(args):
13291329

13301330
if shared.Settings.MODULARIZE:
13311331
assert not options.proxy_to_worker, '-s MODULARIZE=1 is not compatible with --proxy-to-worker (if you want to run in a worker with -s MODULARIZE=1, you likely want to do the worker side setup manually)'
1332+
# in MINIMAL_RUNTIME we may not need to emit the Promise code, as the
1333+
# HTML output creates a singleton instance, and it does so without the
1334+
# Promise. However, in Pthreads mode the Promise is used for worker
1335+
# creation.
1336+
if shared.Settings.MINIMAL_RUNTIME and final_suffix == '.html' and \
1337+
not shared.Settings.USE_PTHREADS:
1338+
shared.Settings.EXPORT_READY_PROMISE = 0
13321339

13331340
if shared.Settings.EMULATE_FUNCTION_POINTER_CASTS:
13341341
shared.Settings.ALIASING_FUNCTION_POINTERS = 0
@@ -3423,17 +3430,22 @@ def modularize():
34233430
logger.debug('Modularizing, assigning to var ' + shared.Settings.EXPORT_NAME)
34243431
src = open(final).read()
34253432

3433+
return_value = shared.Settings.EXPORT_NAME + '.ready'
3434+
if not shared.Settings.EXPORT_READY_PROMISE:
3435+
return_value = '{}'
3436+
34263437
src = '''
34273438
function(%(EXPORT_NAME)s) {
34283439
%(EXPORT_NAME)s = %(EXPORT_NAME)s || {};
34293440
34303441
%(src)s
34313442
3432-
return %(EXPORT_NAME)s.ready;
3443+
return %(return_value)s
34333444
}
34343445
''' % {
34353446
'EXPORT_NAME': shared.Settings.EXPORT_NAME,
34363447
'src': src,
3448+
'return_value': return_value
34373449
}
34383450

34393451
if shared.Settings.MINIMAL_RUNTIME and not shared.Settings.USE_PTHREADS:

src/postamble.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,17 @@ function exit(status, implicit) {
408408
if (!implicit) {
409409
#if EXIT_RUNTIME == 0
410410
var msg = 'program exited (with status: ' + status + '), but EXIT_RUNTIME is not set, so halting execution but not exiting the runtime or preventing further async execution (build with EXIT_RUNTIME=1, if you want a true shutdown)';
411-
err(msg);
412411
#if MODULARIZE
413412
readyPromiseReject(msg);
413+
#else
414+
err(msg);
414415
#endif // MODULARIZE
415416
#else
416417
var msg = 'program exited (with status: ' + status + '), but noExitRuntime is set due to an async operation, so halting execution but not exiting the runtime or preventing further async execution (you can use emscripten_force_exit, if you want to force a true shutdown)';
417-
err(msg);
418418
#if MODULARIZE
419419
readyPromiseReject(msg);
420+
#else
421+
err(msg);
420422
#endif // MODULARIZE
421423
#endif // EXIT_RUNTIME
422424
}

src/settings.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,11 @@ var DETERMINISTIC = 0;
10641064
// for default values. Default values must be passed as a parameter to the
10651065
// factory function.
10661066
//
1067-
// The default .html shell file provided in MINIMAL_RUNTIME mode shows
1068-
// an example to how the module is instantiated from within the html file.
1067+
// The default .html shell file provided in MINIMAL_RUNTIME mode will create
1068+
// a singleton instance automatically, to run the application on the page.
1069+
// (Note that it does so without using the Promise API mentioned earlier, and
1070+
// so code for the Promise is not even emitted in the .js file if you tell
1071+
// emcc to emit an .html output.)
10691072
// The default .html shell file provided by traditional runtime mode is only
10701073
// compatible with MODULARIZE=0 mode, so when building with traditional
10711074
// runtime, you should provided your own html shell file to perform the

src/settings_internal.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,7 @@ var STACK_BASE = 0;
209209
// enabled, but if `--no-entry` is passed, or if `_main` is not part of
210210
// EXPORTED_FUNCTIONS then this gets set to 0.
211211
var EXPECT_MAIN = 1;
212+
213+
// Provide and export a .ready() Promise. This is currently used by default with
214+
// MODULARIZE, and returned from the factory function.
215+
var EXPORT_READY_PROMISE = 1;

src/shell_minimal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Module = {{{ EXPORT_NAME }}};
1515
#endif // USE_CLOSURE_COMPILER
1616
#endif // SIDE_MODULE
1717

18-
#if MODULARIZE
18+
#if MODULARIZE && EXPORT_READY_PROMISE
1919
// Set up the promise that indicates the Module is initialized
2020
var readyPromiseResolve, readyPromiseReject;
2121
Module['ready'] = new Promise(function(resolve, reject) {
@@ -140,7 +140,7 @@ function err(text) {
140140
// compilation is ready. In that callback, call the function run() to start
141141
// the program.
142142
function ready() {
143-
#if MODULARIZE
143+
#if MODULARIZE && EXPORT_READY_PROMISE
144144
readyPromiseResolve(Module);
145145
#endif // MODULARIZE
146146
#if INVOKE_RUN && hasExportedFunction('_main')
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"a.html": 580,
33
"a.html.gz": 382,
4-
"a.js": 5388,
5-
"a.js.gz": 2546,
4+
"a.js": 5326,
5+
"a.js.gz": 2510,
66
"a.mem": 321,
77
"a.mem.gz": 219,
8-
"a.asm.js": 9846,
9-
"a.asm.js.gz": 3535,
10-
"total": 16135,
11-
"total_gz": 6682
8+
"a.asm.js": 9814,
9+
"a.asm.js.gz": 3544,
10+
"total": 16042,
11+
"total_gz": 6655
1212
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 563,
33
"a.html.gz": 377,
4-
"a.js": 5353,
5-
"a.js.gz": 2570,
6-
"a.wasm": 8007,
7-
"a.wasm.gz": 4421,
8-
"total": 13923,
9-
"total_gz": 7368
4+
"a.js": 5293,
5+
"a.js.gz": 2537,
6+
"a.wasm": 8004,
7+
"a.wasm.gz": 4376,
8+
"total": 13861,
9+
"total_gz": 7290
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 563,
33
"a.html.gz": 377,
4-
"a.js": 5133,
5-
"a.js.gz": 2460,
4+
"a.js": 5073,
5+
"a.js.gz": 2423,
66
"a.wasm": 10912,
77
"a.wasm.gz": 6940,
8-
"total": 16608,
9-
"total_gz": 9777
8+
"total": 16549,
9+
"total_gz": 9740
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 588,
33
"a.html.gz": 386,
4-
"a.js": 23765,
5-
"a.js.gz": 8937,
4+
"a.js": 23705,
5+
"a.js.gz": 8895,
66
"a.mem": 3168,
77
"a.mem.gz": 2711,
8-
"total": 27521,
9-
"total_gz": 12034
8+
"total": 27462,
9+
"total_gz": 11992
1010
}

0 commit comments

Comments
 (0)