You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implements Promise-based API for modularized builds (#10697)
This removes the Module.then function and replaces it with a proper
Promise being returned from the initialization function.
That is, when in MODULARIZE mode, calling `Module()` does not
return an instance, but instead returns a Promise you can wait on
for when it is ready.
Fixes#5820
Copy file name to clipboardExpand all lines: emcc.py
+1-9Lines changed: 1 addition & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1320,9 +1320,6 @@ def has_c_source(args):
1320
1320
1321
1321
ifshared.Settings.MODULARIZE:
1322
1322
assertnotoptions.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)'
1323
-
# MODULARIZE's .then() method uses onRuntimeInitialized currently, so make sure
Copy file name to clipboardExpand all lines: site/source/docs/getting_started/FAQ.rst
+3-14Lines changed: 3 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -262,24 +262,13 @@ Here is an example of how to use it:
262
262
263
263
The crucial thing is that ``Module`` exists, and has the property ``onRuntimeInitialized``, before the script containing emscripten output (``my_project.js`` in this example) is loaded.
264
264
265
-
Another option is to use the ``MODULARIZE`` option, using ``-s MODULARIZE=1``. That will put all of the generated JavaScript in a function, which you can call to create an instance. The instance has a promise-like `.then()` method, so if you build with say ``-s MODULARIZE=1 -s 'EXPORT_NAME="MyCode"'`` (see details in settings.js), then you can do something like this:
265
+
Another option is to use the ``MODULARIZE`` option, using ``-s MODULARIZE=1``. That puts all of the generated JavaScript into a factory function, which you can call to create an instance of your module. The factory function returns a Promise that resolves with the module instance. The promise is resolved once it's safe to call the compiled code, i.e. after the compiled code has been downloaded and instantiated. For example, if you build with ``-s MODULARIZE=1 -s 'EXPORT_NAME="createMyModule"'`` (see details in settings.js), then you can do this:
266
266
267
267
::
268
268
269
-
MyCode().then(function(Module) {
270
-
// this is reached when everything is ready, and you can call methods on Module
269
+
createMyModule().then((myModule) => {
270
+
// this is reached when everything is ready, and you can call methods on myModule
271
271
});
272
-
273
-
.. note:: Be careful with using ``Module.then()`` inside ``Promise.resolve()`` or ``await`` statements. ``Module.then()`` resolves with ``Module`` which still has ``then``, so the ``await`` statement loops indefinitely. If you need to use ``Module.then()`` with ``await`` statement, you can use this workaround:
274
-
275
-
::
276
-
277
-
await Module.then(m => {
278
-
delete m['then'];
279
-
resolve(m);
280
-
});
281
-
282
-
For more information read `this issue <https://github.com/emscripten-core/emscripten/pull/10697>`_.
@@ -427,9 +407,17 @@ function exit(status, implicit) {
427
407
// if exit() was called, we may warn the user if the runtime isn't actually being shut down
428
408
if(!implicit){
429
409
#if EXIT_RUNTIME==0
430
-
err('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)');
410
+
varmsg='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);
412
+
#if MODULARIZE
413
+
readyPromiseReject(msg);
414
+
#endif // MODULARIZE
431
415
#else
432
-
err('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)');
416
+
varmsg='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);
418
+
#if MODULARIZE
419
+
readyPromiseReject(msg);
420
+
#endif // MODULARIZE
433
421
#endif // EXIT_RUNTIME
434
422
}
435
423
#endif // ASSERTIONS
@@ -482,15 +470,24 @@ if (!ENVIRONMENT_IS_PTHREAD) // EXIT_RUNTIME=0 only applies to default behavior
482
470
#endif
483
471
484
472
#if USE_PTHREADS
485
-
if(!ENVIRONMENT_IS_PTHREAD)run();
473
+
if(!ENVIRONMENT_IS_PTHREAD){
474
+
run();
475
+
}else{
486
476
#if EMBIND
487
-
else{// Embind must initialize itself on all threads, as it generates support JS.
477
+
// Embind must initialize itself on all threads, as it generates support JS.
0 commit comments