Skip to content

Commit b96bcd3

Browse files
authored
Remove redundant alias handling in JS compiler. NFC. (#17420)
This change removes the redundant handling of aliases, leaving the simpler and more efficient one in place. With this method the final output contains: ``` var foo = target; ``` Rather than: ``` function foo(arg1, arg2) { return target(arg1, arg1); } ```
1 parent fb8375f commit b96bcd3

File tree

2 files changed

+9
-76
lines changed

2 files changed

+9
-76
lines changed

src/jsifier.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ function ${name}(${args}) {
290290

291291
const original = LibraryManager.library[ident];
292292
let snippet = original;
293-
let redirectedIdent = null;
294293
const deps = LibraryManager.library[ident + '__deps'] || [];
295294
if (!Array.isArray(deps)) {
296295
error(`JS library directive ${ident}__deps=${deps.toString()} is of type ${typeof deps}, but it should be an array!`);
@@ -313,9 +312,9 @@ function ${name}(${args}) {
313312
if (target) {
314313
// Redirection for aliases. We include the parent, and at runtime make ourselves equal to it.
315314
// This avoid having duplicate functions with identical content.
316-
redirectedIdent = snippet;
317-
deps.push(snippet);
318-
snippet = mangleCSymbolName(snippet);
315+
const redirectedTarget = snippet;
316+
deps.push(redirectedTarget);
317+
snippet = mangleCSymbolName(redirectedTarget);
319318
}
320319
}
321320
} else if (typeof snippet == 'object') {
@@ -349,9 +348,6 @@ function ${name}(${args}) {
349348
}
350349
}
351350

352-
if (redirectedIdent) {
353-
deps = deps.concat(LibraryManager.library[redirectedIdent + '__deps'] || []);
354-
}
355351
if (VERBOSE) {
356352
printErr(`adding ${finalName} and deps ${deps} : ` + (snippet + '').substr(0, 40));
357353
}

src/modules.js

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -237,75 +237,12 @@ global.LibraryManager = {
237237
}
238238
}
239239

240-
// apply synonyms. these are typically not speed-sensitive, and doing it
241-
// this way makes it possible to not include hacks in the compiler
242-
// (and makes it simpler to switch between SDL versions, fastcomp and non-fastcomp, etc.).
243-
const lib = this.library;
244-
libloop: for (const x in lib) {
245-
if (!Object.prototype.hasOwnProperty.call(lib, x)) {
246-
continue;
247-
}
248-
if (isJsLibraryConfigIdentifier(x)) {
249-
const index = x.lastIndexOf('__');
250-
const basename = x.slice(0, index);
251-
if (!(basename in lib)) {
252-
error(`Missing library element '${basename}' for library config '${x}'`);
253-
}
254-
continue;
255-
}
256-
if (typeof lib[x] == 'string') {
257-
let target = x;
258-
while (typeof lib[target] == 'string') {
259-
// ignore code and variable assignments, aliases are just simple names
260-
if (lib[target].search(/[=({; ]/) >= 0) continue libloop;
261-
target = lib[target];
262-
}
263-
if (!isNaN(target)) continue; // This is a number, and so cannot be an alias target.
264-
if (typeof lib[target] == 'undefined' || typeof lib[target] == 'function') {
265-
// When functions are aliased, a signature for the function must be
266-
// provided so that an efficient form of forwarding can be
267-
// implemented.
268-
function testStringType(sig) {
269-
if (typeof lib[sig] != 'undefined' && typeof typeof lib[sig] != 'string') {
270-
error(`${sig} should be a string! (was ${typeof lib[sig]})`);
271-
}
272-
}
273-
const aliasSig = x + '__sig';
274-
const targetSig = target + '__sig';
275-
testStringType(aliasSig);
276-
testStringType(targetSig);
277-
if (typeof lib[aliasSig] == 'string' && typeof lib[targetSig] == 'string' && lib[aliasSig] != lib[targetSig]) {
278-
error(`${aliasSig} (${lib[aliasSig]}) differs from ${targetSig} (${lib[targetSig]})`);
279-
}
280-
281-
const sig = lib[aliasSig] || lib[targetSig];
282-
if (typeof sig != 'string') {
283-
error(`Function ${x} aliases to target function ${target}, but neither the alias or the target provide a signature. Please add a ${targetSig}: 'vifj...' annotation or a ${aliasSig}: 'vifj...' annotation to describe the type of function forwarding that is needed!`);
284-
}
285-
286-
// If only one of the target or the alias specifies a sig then copy
287-
// this signature to the other.
288-
if (!lib[aliasSig]) {
289-
lib[aliasSig] = lib[targetSig];
290-
} else if (!lib[targetSig]) {
291-
lib[targetSig] = lib[aliasSig];
292-
}
293-
294-
if (typeof lib[target] != 'function') {
295-
error(`no alias found for ${x}`);
296-
}
297-
298-
const argCount = sig.length - 1;
299-
if (argCount !== lib[target].length) {
300-
error(`incorrect number of arguments in signature of ${x} (declared: ${argCount}, expected: ${lib[target].length})`);
301-
}
302-
const ret = sig == 'v' ? '' : 'return ';
303-
const args = genArgSequence(argCount).join(',');
304-
const mangledName = mangleCSymbolName(target);
305-
lib[x] = new Function(args, `${ret}${mangledName}(${args});`);
306-
307-
if (!lib[x + '__deps']) lib[x + '__deps'] = [];
308-
lib[x + '__deps'].push(target);
240+
for (const ident in this.library) {
241+
if (isJsLibraryConfigIdentifier(ident)) {
242+
const index = ident.lastIndexOf('__');
243+
const basename = ident.slice(0, index);
244+
if (!(basename in this.library)) {
245+
error(`Missing library element '${basename}' for library config '${ident}'`);
309246
}
310247
}
311248
}

0 commit comments

Comments
 (0)