Skip to content

Commit e80dcca

Browse files
authored
Inject alias dependencies early (#25921)
Split out from #25918
1 parent 2e02efc commit e80dcca

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

src/jsifier.mjs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,6 @@ function isDefined(symName) {
128128
return false;
129129
}
130130

131-
function isAlias(snippet) {
132-
return (typeof snippet == 'string' && snippet[0] != '=' && (LibraryManager.library.hasOwnProperty(snippet) || WASM_EXPORTS.has(snippet)));
133-
}
134-
135-
function resolveAlias(symbol) {
136-
while (true) {
137-
var value = LibraryManager.library[symbol];
138-
if (isAlias(value)) {
139-
symbol = value;
140-
} else {
141-
break;
142-
}
143-
}
144-
return symbol;
145-
}
146-
147131
function getTransitiveDeps(symbol) {
148132
// TODO(sbc): Use some kind of cache to avoid quadratic behaviour here.
149133
const transitiveDeps = new Set();
@@ -155,12 +139,11 @@ function getTransitiveDeps(symbol) {
155139
let directDeps = LibraryManager.library[sym + '__deps'] || [];
156140
directDeps = directDeps.filter((d) => typeof d === 'string');
157141
for (const dep of directDeps) {
158-
const resolved = resolveAlias(dep);
159142
if (!transitiveDeps.has(dep)) {
160143
debugLog(`adding dependency ${symbol} -> ${dep}`);
161144
}
162-
transitiveDeps.add(resolved);
163-
toVisit.push(resolved);
145+
transitiveDeps.add(dep);
146+
toVisit.push(dep);
164147
}
165148
seen.add(sym);
166149
}
@@ -558,9 +541,8 @@ function(${args}) {
558541
if (symbolsOnly) {
559542
if (LibraryManager.library.hasOwnProperty(symbol)) {
560543
// Resolve aliases before looking up deps
561-
var resolvedSymbol = resolveAlias(symbol);
562-
var transtiveDeps = getTransitiveDeps(resolvedSymbol);
563-
symbolDeps[symbol] = transtiveDeps.filter(
544+
var transitiveDeps = getTransitiveDeps(symbol);
545+
symbolDeps[symbol] = transitiveDeps.filter(
564546
(d) => !isJsOnlySymbol(d) && !(d in LibraryManager.library),
565547
);
566548
}
@@ -656,21 +638,19 @@ function(${args}) {
656638
}
657639
}
658640

659-
if (isAlias(snippet)) {
641+
if (LibraryManager.isAlias(snippet)) {
660642
// Redirection for aliases. We include the parent, and at runtime
661643
// make ourselves equal to it. This avoid having duplicate
662644
// functions with identical content.
663-
const aliasTarget = resolveAlias(snippet);
664-
if (WASM_EXPORTS.has(aliasTarget)) {
645+
if (WASM_EXPORTS.has(snippet)) {
665646
//printErr(`native alias: ${mangled} -> ${snippet}`);
666647
//console.error(WASM_EXPORTS);
667-
nativeAliases[mangled] = aliasTarget;
648+
nativeAliases[mangled] = snippet;
668649
snippet = undefined;
669650
isNativeAlias = true;
670651
} else {
671652
//printErr(`js alias: ${mangled} -> ${snippet}`);
672-
deps.push(aliasTarget);
673-
snippet = mangleCSymbolName(aliasTarget);
653+
snippet = mangleCSymbolName(snippet);
674654
}
675655
} else if (typeof snippet == 'object') {
676656
snippet = stringifyWithFunctions(snippet);

src/modules.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,30 @@ export const LibraryManager = {
280280
}
281281
timer.stop('executeJS')
282282

283+
this.addAliasDependencies();
284+
283285
timer.stop('load')
284286
},
285287

288+
isAlias(entry) {
289+
return (typeof entry == 'string' && entry[0] != '=' && (this.library.hasOwnProperty(entry) || WASM_EXPORTS.has(entry)));
290+
},
291+
292+
/**
293+
* Automatically add the target of an alias to it's dependency list.
294+
*/
295+
addAliasDependencies() {
296+
const aliases = {};
297+
for (const [key, value] of Object.entries(this.library)) {
298+
if (this.isAlias(value)) {
299+
aliases[key] = value;
300+
}
301+
}
302+
for (const [key, value] of Object.entries(aliases)) {
303+
(this.library[key + '__deps'] ??= []).push(value);
304+
}
305+
},
306+
286307
executeJSLibraryFile(filename, contents) {
287308
const userLibraryProxy = new Proxy(this.library, {
288309
set(target, prop, value) {

0 commit comments

Comments
 (0)