Skip to content

Commit aa21bbc

Browse files
committed
Remove redundant __deps entries and autoAddDeps calls across JS libraries
Remove all `autoAddDeps` calls and redundant `__deps` entries across `src/lib/*.js` and the test suite. Fixes: #19098
1 parent d76a67c commit aa21bbc

114 files changed

Lines changed: 404 additions & 1974 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/jsifier.mjs‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
ATPOSTRUNS,
2121
defineI64Param,
2222
indentify,
23-
isSymbolNeeded,
2423
makeReturn64,
2524
modifyJSFunction,
2625
preprocess,
@@ -47,6 +46,8 @@ import {
4746
import {extraExports, LibraryManager, librarySymbols, nativeAliases} from './modules.mjs';
4847

4948
const addedLibraryItems = {};
49+
const emittedLibraryItems = new Set();
50+
const deferredAliases = {};
5051

5152
// Some JS-implemented library functions are proxied to be called on the main
5253
// browser thread, if the Emscripten runtime is executing in a Web Worker.
@@ -301,15 +302,17 @@ function calculateDeps(snippet, deps, symbol, symbolsOnly = false) {
301302
dep = name.slice(1);
302303
}
303304

304-
if (dep && dep !== symbol) {
305+
// $noExitRuntime cannot be referenced via __deps (it must be included via
306+
// DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or EXPORTED_RUNTIME_METHODS).
307+
if (dep && dep !== symbol && dep !== '$noExitRuntime') {
305308
found.add(dep);
306309
}
307310
}
308311

309312
const isUserSymbol = symbol && LibraryManager.library[symbol + '__user'];
310313
for (const dep of found) {
311314
if (deps.includes(dep)) {
312-
if (!symbolsOnly && symbol && isUserSymbol && STRICT && LibraryManager.library[symbol] !== dep) {
315+
if (!symbolsOnly && symbol && (STRICT || !isUserSymbol) && LibraryManager.library[symbol] !== dep) {
313316
warn(`${symbol}: redundant __deps entry: ${dep}`);
314317
}
315318
} else {
@@ -771,11 +774,12 @@ function(${args}) {
771774
}
772775
}
773776

777+
let aliasTarget;
774778
if (LibraryManager.isAlias(snippet)) {
775779
// Redirection for aliases. We include the parent, and at runtime
776780
// make ourselves equal to it. This avoid having duplicate
777781
// functions with identical content.
778-
const aliasTarget = snippet;
782+
aliasTarget = snippet;
779783
if (WASM_EXPORTS.has(aliasTarget)) {
780784
debugLog(`native alias: ${mangled} -> ${aliasTarget}`);
781785
nativeAliases[mangled] = aliasTarget;
@@ -818,8 +822,10 @@ function(${args}) {
818822

819823
const isUserSymbol = LibraryManager.library[symbol + '__user'];
820824
// Check for dependencies on `__internal` symbols from user libraries.
825+
// Exclude `implicitSnippetDeps` (such as `$getWasmTableEntry`, which is
826+
// injected by the `{{{ makeDynCall }}}` macro).
821827
for (const dep of deps) {
822-
if (isUserSymbol && LibraryManager.library[dep + '__internal']) {
828+
if (isUserSymbol && LibraryManager.library[dep + '__internal'] && !implicitSnippetDeps.has(dep.slice(1))) {
823829
warn(`user library symbol '${symbol}' depends on internal symbol '${dep}'`);
824830
}
825831
}
@@ -833,7 +839,7 @@ function(${args}) {
833839
// $noExitRuntime is special since there are conditional usages of it
834840
// in libcore.js and libpthread.js. These happen before deps are
835841
// processed so depending on it via `__deps` doesn't work.
836-
if (dep === '$noExitRuntime' && !isSymbolNeeded('$noExitRuntime')) {
842+
if (dep === '$noExitRuntime') {
837843
error('noExitRuntime cannot be referenced via __deps mechanism. Use DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or EXPORTED_RUNTIME_METHODS');
838844
}
839845
return addFromLibrary(dep, `${symbol}, referenced by ${dependent}`);
@@ -938,7 +944,23 @@ function(${args}) {
938944
.filter((x) => x != '')
939945
.join('\n') + '\n'
940946
: '';
941-
return depsText + commentText + contentText;
947+
if (aliasTarget && !isNativeAlias && !isFunction && !emittedLibraryItems.has(aliasTarget)) {
948+
// If `aliasTarget` is not yet emitted, we are in a dependency cycle
949+
// where `aliasTarget` is an ancestor on the recursion stack. Defer
950+
// emitting `var _alias = _target;` until immediately after `_target`
951+
// is defined so `_target` is not referenced before its initialization.
952+
(deferredAliases[aliasTarget] ??= []).push({symbol, text: commentText + contentText});
953+
return depsText.trim();
954+
}
955+
function flushDeferred(sym) {
956+
emittedLibraryItems.add(sym);
957+
let res = '';
958+
for (const item of deferredAliases[sym] ?? []) {
959+
res += '\n' + item.text + flushDeferred(item.symbol);
960+
}
961+
return res;
962+
}
963+
return depsText + commentText + contentText + flushDeferred(symbol);
942964
}
943965

944966
const JS = addFromLibrary(symbol, 'root reference (e.g. compiled C/C++ code)');

‎src/lib/libaddfunction.js‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ addToLibrary({
3535
}`,
3636

3737
$generateTypePack__internal: true,
38-
$generateTypePack__deps: ['$uleb128EncodeWithLen', '$wasmTypeCodes'],
3938
$generateTypePack: (types) => uleb128EncodeWithLen(Array.from(types, (type) => {
4039
var code = wasmTypeCodes[type];
4140
#if ASSERTIONS
@@ -46,10 +45,6 @@ addToLibrary({
4645

4746
#if !WASM2JS || WASM == 2
4847
// Wraps a JS function as a wasm function with a given signature.
49-
$convertJsFunctionToWasm__deps: [
50-
'$uleb128EncodeWithLen',
51-
'$generateTypePack'
52-
],
5348
$convertJsFunctionToWasm: (func, sig) => {
5449
#if ASSERTIONS && !WASM_BIGINT
5550
assert(!sig.includes('j'), 'i64 not permitted in function signatures when WASM_BIGINT is disabled');
@@ -93,7 +88,6 @@ addToLibrary({
9388
// Weak map of functions in the table to their indexes, created on first use.
9489
$functionsInTableMap: undefined,
9590

96-
$getEmptyTableSlot__deps: ['$freeTableIndexes', '$wasmTable'],
9791
$getEmptyTableSlot: () => {
9892
// Reuse a free index if there is one, otherwise grow.
9993
if (freeTableIndexes.length) {
@@ -114,7 +108,6 @@ addToLibrary({
114108
#endif
115109
},
116110

117-
$updateTableMap__deps: ['$getWasmTableEntry'],
118111
$updateTableMap: (offset, count) => {
119112
if (functionsInTableMap) {
120113
for (var i = offset; i < offset + count; i++) {
@@ -127,7 +120,6 @@ addToLibrary({
127120
}
128121
},
129122

130-
$getFunctionAddress__deps: ['$updateTableMap', '$functionsInTableMap', '$wasmTable'],
131123
$getFunctionAddress: (func) => {
132124
// First, create the map if this is the first use.
133125
if (!functionsInTableMap) {
@@ -142,16 +134,6 @@ addToLibrary({
142134
* 'sig' parameter is required if the function being added is a JS function.
143135
*/
144136
$addFunction__docs: '/** @param {string=} sig */',
145-
$addFunction__deps: ['$getFunctionAddress',
146-
'$functionsInTableMap', '$getEmptyTableSlot',
147-
'$setWasmTableEntry',
148-
#if !WASM2JS || WASM == 2
149-
'$convertJsFunctionToWasm',
150-
#endif
151-
#if ASSERTIONS >= 2
152-
'$getWasmTableEntry', '$wasmTable',
153-
#endif
154-
],
155137

156138
$addFunction: (func, sig) => {
157139
#if ASSERTIONS
@@ -201,8 +183,6 @@ addToLibrary({
201183
return ret;
202184
},
203185

204-
$removeFunction__deps: ['$functionsInTableMap', '$freeTableIndexes',
205-
'$getWasmTableEntry', '$setWasmTableEntry'],
206186
$removeFunction: (index) => {
207187
functionsInTableMap.delete(getWasmTableEntry(index));
208188
setWasmTableEntry(index, null);

‎src/lib/libasync.js‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,10 @@ addToLibrary({
2121

2222
#if ASYNCIFY
2323
$Asyncify__force: true,
24-
$Asyncify__deps: [
25-
#if useRuntimeKeepaliveStack()
26-
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
27-
#endif
2824
#if ASYNCIFY == 1
29-
'$runAndAbortIfError', '$callUserCallback',
30-
#if ASSERTIONS
31-
'$createNamedFunction',
32-
#endif
33-
// Needed by allocateData and handleSleep respectively
34-
'malloc', 'free',
25+
// Needed by allocateData and handleSleep respectively
26+
$Asyncify__deps: ['malloc', 'free'],
3527
#endif
36-
],
3728

3829
$Asyncify: {
3930
//
@@ -484,7 +475,7 @@ addToLibrary({
484475
emscripten_sleep__async: 'auto',
485476
emscripten_sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
486477

487-
emscripten_wget_data__deps: ['$asyncLoad', 'malloc'],
478+
emscripten_wget_data__deps: ['malloc'],
488479
emscripten_wget_data__async: 'auto',
489480
emscripten_wget_data: async (url, pbuffer, pnum, perror) => {
490481
/* no need for run dependency, this is async but will not do any prepare etc. step */
@@ -501,7 +492,6 @@ addToLibrary({
501492
}
502493
},
503494

504-
emscripten_scan_registers__deps: ['$safeSetTimeout'],
505495
emscripten_scan_registers__async: true,
506496
emscripten_scan_registers: (func) => {
507497
return Asyncify.handleSleep((wakeUp) => {
@@ -518,7 +508,7 @@ addToLibrary({
518508
});
519509
},
520510

521-
$Fibers__deps: ['$Asyncify', 'emscripten_stack_set_limits', '$stackRestore'],
511+
$Fibers__deps: ['emscripten_stack_set_limits'],
522512
$Fibers: {
523513
nextFiber: 0,
524514
trampolineRunning: false,
@@ -578,7 +568,6 @@ addToLibrary({
578568
},
579569
},
580570

581-
emscripten_fiber_swap__deps: ['$Asyncify', '$Fibers', '$stackSave'],
582571
emscripten_fiber_swap__async: true,
583572
emscripten_fiber_swap: (oldFiber, newFiber) => {
584573
if (ABORT) return;

‎src/lib/libatomic.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ addToLibrary({
6969
},
7070

7171
#if ASYNCIFY
72-
_emscripten_atomic_wait_promise__deps: ['$polyfillWaitAsync', '$atomicWaitStates', '$addPromise'],
72+
_emscripten_atomic_wait_promise__deps: ['$polyfillWaitAsync'],
7373
_emscripten_atomic_wait_promise: (addr, val, maxWaitMilliseconds) => {
7474
var wait = Atomics.waitAsync(HEAP32, {{{ getHeapOffset('addr', 'i32') }}}, val, maxWaitMilliseconds);
7575
if (wait.async) {
@@ -94,7 +94,7 @@ addToLibrary({
9494
$liveAtomicWaitAsyncCounter: 0,
9595
$liveAtomicWaitAsyncCounter__internal: true,
9696

97-
emscripten_atomic_wait_async__deps: ['$atomicWaitStates', '$liveAtomicWaitAsyncs', '$liveAtomicWaitAsyncCounter', '$polyfillWaitAsync', '$callUserCallback'],
97+
emscripten_atomic_wait_async__deps: ['$polyfillWaitAsync'],
9898
emscripten_atomic_wait_async: (addr, val, asyncWaitFinished, userData, maxWaitMilliseconds) => {
9999
let wait = Atomics.waitAsync(HEAP32, {{{ getHeapOffset('addr', 'i32') }}}, val, maxWaitMilliseconds);
100100
if (!wait.async) return atomicWaitStates.indexOf(wait.value);
@@ -116,7 +116,6 @@ addToLibrary({
116116
return -counter;
117117
},
118118

119-
emscripten_atomic_cancel_wait_async__deps: ['$liveAtomicWaitAsyncs'],
120119
emscripten_atomic_cancel_wait_async: (waitToken) => {
121120
#if ASSERTIONS
122121
if (waitToken == {{{ cDefs.ATOMICS_WAIT_NOT_EQUAL }}}) {
@@ -143,7 +142,6 @@ addToLibrary({
143142
return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_PARAM }}};
144143
},
145144

146-
emscripten_atomic_cancel_all_wait_asyncs__deps: ['$liveAtomicWaitAsyncs'],
147145
emscripten_atomic_cancel_all_wait_asyncs: () => {
148146
let waitAsyncs = Object.values(liveAtomicWaitAsyncs);
149147
for (var address of waitAsyncs) {
@@ -153,7 +151,6 @@ addToLibrary({
153151
return waitAsyncs.length;
154152
},
155153

156-
emscripten_atomic_cancel_all_wait_asyncs_at_address__deps: ['$liveAtomicWaitAsyncs'],
157154
emscripten_atomic_cancel_all_wait_asyncs_at_address: (address) => {
158155
let numCancelled = 0;
159156
for (var [waitToken, waitAddress] of Object.entries(liveAtomicWaitAsyncs)) {

‎src/lib/libautodebug.js‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const LibraryAutodebug = {
1515
dbg('get_i32 ' + [loc, index, value]);
1616
return value;
1717
},
18-
$get_i64__deps: ['setTempRet0'],
1918
$get_i64: (loc, index, low, high) => {
2019
dbg('get_i64 ' + [loc, index, low, high]);
2120
_setTempRet0(high);
@@ -49,7 +48,6 @@ const LibraryAutodebug = {
4948
dbg('set_i32 ' + [loc, index, value]);
5049
return value;
5150
},
52-
$set_i64__deps: ['setTempRet0'],
5351
$set_i64: (loc, index, low, high) => {
5452
dbg('set_i64 ' + [loc, index, low, high]);
5553
_setTempRet0(high);
@@ -87,7 +85,6 @@ const LibraryAutodebug = {
8785
dbg('load_val_i32 ' + [loc, value]);
8886
return value;
8987
},
90-
$load_val_i64__deps: ['setTempRet0'],
9188
$load_val_i64: (loc, low, high) => {
9289
dbg('load_val_i64 ' + [loc, low, high]);
9390
_setTempRet0(high);
@@ -109,7 +106,6 @@ const LibraryAutodebug = {
109106
dbg('store_val_i32 ' + [loc, value]);
110107
return value;
111108
},
112-
$store_val_i64__deps: ['setTempRet0'],
113109
$store_val_i64: (loc, low, high) => {
114110
dbg('store_val_i64 ' + [loc, low, high]);
115111
_setTempRet0(high);

‎src/lib/libbootstrap.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ addToLibrary({
3838
}
3939
},
4040

41-
$exitJS__deps: ['$ExitStatus'],
4241
$exitJS: (code) => quit_(code, new ExitStatus(code)),
4342

4443
$handleException: (e) => {

0 commit comments

Comments
 (0)