Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/lib/libcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,110 @@ addToLibrary({
$noExitRuntime__postset: () => addAtModule(makeModuleReceive('noExitRuntime')),
$noExitRuntime: {{{ !EXIT_RUNTIME }}},

// A counter of dependencies for calling run(). If we need to
// do asynchronous work before running, increment this and
// decrement it. Incrementing must happen in a place like
// Module.preRun (used by emcc to add file preloading).
// Note that you can add dependencies in preRun, even though
// it happens right before run - run will be postponed until
// the dependencies are met.
$runDependencies__internal: true,
$runDependencies: 0,
// overridden to take different actions when all run dependencies are fulfilled
$dependenciesFulfilled__internal: true,
$dependenciesFulfilled: null,
#if ASSERTIONS
$runDependencyTracking__internal: true,
$runDependencyTracking: {},
$runDependencyWatcher__internal: true,
$runDependencyWatcher: null,
#endif

$addRunDependency__deps: ['$runDependencies',
#if ASSERTIONS
'$runDependencyTracking',
'$runDependencyWatcher',
#endif
],
$addRunDependency: (id) => {
runDependencies++;

#if expectToReceiveOnModule('monitorRunDependencies')
Module['monitorRunDependencies']?.(runDependencies);
#endif

#if ASSERTIONS
#if RUNTIME_DEBUG
dbg('addRunDependency', id);
#endif
assert(id, 'addRunDependency requires an ID')
assert(!runDependencyTracking[id]);
runDependencyTracking[id] = 1;
if (runDependencyWatcher === null && typeof setInterval != 'undefined') {
// Check for missing dependencies every few seconds
runDependencyWatcher = setInterval(() => {
if (ABORT) {
clearInterval(runDependencyWatcher);
runDependencyWatcher = null;
return;
}
var shown = false;
for (var dep in runDependencyTracking) {
if (!shown) {
shown = true;
err('still waiting on run dependencies:');
}
err(`dependency: ${dep}`);
}
if (shown) {
err('(end of list)');
}
}, 10000);
#if ENVIRONMENT_MAY_BE_NODE
// Prevent this timer from keeping the runtime alive if nothing
// else is.
runDependencyWatcher.unref?.()
#endif
}
#endif
},

$removeRunDependency__deps: ['$runDependencies', '$dependenciesFulfilled',
#if ASSERTIONS
'$runDependencyTracking',
'$runDependencyWatcher',
#endif
],
$removeRunDependency: (id) => {
runDependencies--;

#if expectToReceiveOnModule('monitorRunDependencies')
Module['monitorRunDependencies']?.(runDependencies);
#endif

#if ASSERTIONS
#if RUNTIME_DEBUG
dbg('removeRunDependency', id);
#endif
assert(id, 'removeRunDependency requires an ID');
assert(runDependencyTracking[id]);
delete runDependencyTracking[id];
#endif
if (runDependencies == 0) {
#if ASSERTIONS
if (runDependencyWatcher !== null) {
clearInterval(runDependencyWatcher);
runDependencyWatcher = null;
}
#endif
if (dependenciesFulfilled) {
var callback = dependenciesFulfilled;
dependenciesFulfilled = null;
callback(); // can add another dependenciesFulfilled
}
}
},

// The following addOn<X> functions are for adding runtime callbacks at
// various executions points. Each addOn<X> function has a corresponding
// compiled time version named addAt<X> that will instead inline during
Expand Down
2 changes: 0 additions & 2 deletions src/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,6 @@ function exportRuntimeSymbols() {
// All possible runtime elements that can be exported
let runtimeElements = [
'run',
'addRunDependency',
'removeRunDependency',
'out',
'err',
'callMain',
Expand Down
4 changes: 4 additions & 0 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,15 @@ function run(args = arguments_) {
function run() {
#endif

#if !BOOTSTRAPPING_STRUCT_INFO
if (runDependencies > 0) {
#if RUNTIME_DEBUG
dbg('run() called, but dependencies remain, so not running');
#endif
dependenciesFulfilled = run;
return;
}
#endif

#if PTHREADS || WASM_WORKERS
if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) {
Expand All @@ -185,6 +187,7 @@ function run() {

preRun();

#if !BOOTSTRAPPING_STRUCT_INFO
// a preRun added a dependency, run will be called later
if (runDependencies > 0) {
#if RUNTIME_DEBUG
Expand All @@ -193,6 +196,7 @@ function run() {
dependenciesFulfilled = run;
return;
}
#endif

{{{ asyncIf(ASYNCIFY == 2) }}}function doRun() {
// run may have just been called through dependencies being fulfilled just in this very frame,
Expand Down
87 changes: 0 additions & 87 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,93 +229,6 @@ function postRun() {
<<< ATPOSTRUNS >>>
}

// A counter of dependencies for calling run(). If we need to
// do asynchronous work before running, increment this and
// decrement it. Incrementing must happen in a place like
// Module.preRun (used by emcc to add file preloading).
// Note that you can add dependencies in preRun, even though
// it happens right before run - run will be postponed until
// the dependencies are met.
var runDependencies = 0;
var dependenciesFulfilled = null; // overridden to take different actions when all run dependencies are fulfilled
#if ASSERTIONS
var runDependencyTracking = {};
var runDependencyWatcher = null;
#endif

function addRunDependency(id) {
runDependencies++;

#if expectToReceiveOnModule('monitorRunDependencies')
Module['monitorRunDependencies']?.(runDependencies);
#endif

#if ASSERTIONS
#if RUNTIME_DEBUG
dbg('addRunDependency', id);
#endif
assert(id, 'addRunDependency requires an ID')
assert(!runDependencyTracking[id]);
runDependencyTracking[id] = 1;
if (runDependencyWatcher === null && typeof setInterval != 'undefined') {
// Check for missing dependencies every few seconds
runDependencyWatcher = setInterval(() => {
if (ABORT) {
clearInterval(runDependencyWatcher);
runDependencyWatcher = null;
return;
}
var shown = false;
for (var dep in runDependencyTracking) {
if (!shown) {
shown = true;
err('still waiting on run dependencies:');
}
err(`dependency: ${dep}`);
}
if (shown) {
err('(end of list)');
}
}, 10000);
#if ENVIRONMENT_MAY_BE_NODE
// Prevent this timer from keeping the runtime alive if nothing
// else is.
runDependencyWatcher.unref?.()
#endif
}
#endif
}

function removeRunDependency(id) {
runDependencies--;

#if expectToReceiveOnModule('monitorRunDependencies')
Module['monitorRunDependencies']?.(runDependencies);
#endif

#if ASSERTIONS
#if RUNTIME_DEBUG
dbg('removeRunDependency', id);
#endif
assert(id, 'removeRunDependency requires an ID');
assert(runDependencyTracking[id]);
delete runDependencyTracking[id];
#endif
if (runDependencies == 0) {
#if ASSERTIONS
if (runDependencyWatcher !== null) {
clearInterval(runDependencyWatcher);
runDependencyWatcher = null;
}
#endif
if (dependenciesFulfilled) {
var callback = dependenciesFulfilled;
dependenciesFulfilled = null;
callback(); // can add another dependenciesFulfilled
}
}
}

/** @param {string|number=} what */
function abort(what) {
#if expectToReceiveOnModule('onAbort')
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_cxx_ctors1.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19749,
"a.out.js.gz": 8155,
"a.out.js.gz": 8156,
"a.out.nodebug.wasm": 129507,
"a.out.nodebug.wasm.gz": 49234,
"total": 149256,
"total_gz": 57389,
"total_gz": 57390,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_cxx_ctors2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19727,
"a.out.js.gz": 8145,
"a.out.js.gz": 8144,
"a.out.nodebug.wasm": 128935,
"a.out.nodebug.wasm.gz": 48881,
"total": 148662,
"total_gz": 57026,
"total_gz": 57025,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_cxx_lto.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19077,
"a.out.js.gz": 7835,
"a.out.js.gz": 7834,
"a.out.nodebug.wasm": 106468,
"a.out.nodebug.wasm.gz": 42616,
"total": 125545,
"total_gz": 50451,
"total_gz": 50450,
"sent": [
"a (emscripten_resize_heap)",
"b (_setitimer_js)",
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_cxx_mangle.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 23464,
"a.out.js.gz": 9162,
"a.out.js.gz": 9161,
"a.out.nodebug.wasm": 235325,
"a.out.nodebug.wasm.gz": 78943,
"total": 258789,
"total_gz": 88105,
"total_gz": 88104,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_cxx_noexcept.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19749,
"a.out.js.gz": 8155,
"a.out.js.gz": 8156,
"a.out.nodebug.wasm": 131925,
"a.out.nodebug.wasm.gz": 50235,
"total": 151674,
"total_gz": 58390,
"total_gz": 58391,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_cxx_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"a.out.js": 7138,
"a.out.js": 7139,
"a.out.js.gz": 3337,
"a.out.nodebug.wasm": 169796,
"a.out.nodebug.wasm.gz": 63083,
"total": 176934,
"total": 176935,
"total_gz": 66420,
"sent": [
"__cxa_throw",
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_files_js_fs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 18352,
"a.out.js.gz": 7468,
"a.out.js.gz": 7467,
"a.out.nodebug.wasm": 381,
"a.out.nodebug.wasm.gz": 260,
"total": 18733,
"total_gz": 7728,
"total_gz": 7727,
"sent": [
"a (fd_write)",
"b (fd_read)",
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_files_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 5545,
"a.out.js.gz": 2590,
"a.out.js.gz": 2589,
"a.out.nodebug.wasm": 50232,
"a.out.nodebug.wasm.gz": 18078,
"total": 55777,
"total_gz": 20668,
"total_gz": 20667,
"sent": [
"a (emscripten_date_now)",
"b (emscripten_err)",
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/test_codesize_hello_O0.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 22503,
"a.out.js.gz": 8341,
"a.out.js": 22497,
"a.out.js.gz": 8331,
"a.out.nodebug.wasm": 15143,
"a.out.nodebug.wasm.gz": 7452,
"total": 37646,
"total_gz": 15793,
"total": 37640,
"total_gz": 15783,
"sent": [
"fd_write"
],
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_hello_O1.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 6407,
"a.out.js.gz": 2479,
"a.out.js.gz": 2478,
"a.out.nodebug.wasm": 2675,
"a.out.nodebug.wasm.gz": 1491,
"total": 9082,
"total_gz": 3970,
"total_gz": 3969,
"sent": [
"fd_write"
],
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_hello_O2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 4365,
"a.out.js.gz": 2141,
"a.out.js.gz": 2140,
"a.out.nodebug.wasm": 1979,
"a.out.nodebug.wasm.gz": 1157,
"total": 6344,
"total_gz": 3298,
"total_gz": 3297,
"sent": [
"fd_write"
],
Expand Down
Loading