Skip to content

Commit c3e22dc

Browse files
committed
Emit JS library aliases after non-alias library items
When a JS library alias target (e.g. `foo`) is visited first and transitively depends on its own alias (e.g. `foo_alias: 'foo'`), `addedLibraryItems['foo']` is marked as visited before `var _foo = ...` is emitted. Emitting `var _foo_alias = _foo;` inline during dependency traversal causes it to appear before `var _foo = ...`, leaving `_foo_alias` initialized to `undefined` for arrow functions and non-hoisted values. Collect JS library alias assignments in `libraryAliases` and emit them after `libraryItems` (and before `postSets`) so all target symbols are defined before any alias assignment runs.
1 parent 97370e8 commit c3e22dc

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

‎src/jsifier.mjs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ const PROXY_SYNC_ASYNC = 2;
414414

415415
export async function runJSify(outputFile, symbolsOnly) {
416416
const libraryItems = [];
417+
const libraryAliases = [];
417418
const symbolDeps = {};
418419
const asyncFuncs = [];
419420
let postSets = [];
@@ -728,11 +729,12 @@ function(${args}) {
728729
}
729730
}
730731

732+
let aliasTarget;
731733
if (LibraryManager.isAlias(snippet)) {
732734
// Redirection for aliases. We include the parent, and at runtime
733735
// make ourselves equal to it. This avoid having duplicate
734736
// functions with identical content.
735-
const aliasTarget = snippet;
737+
aliasTarget = snippet;
736738
if (WASM_EXPORTS.has(aliasTarget)) {
737739
debugLog(`native alias: ${mangled} -> ${aliasTarget}`);
738740
nativeAliases[mangled] = aliasTarget;
@@ -884,6 +886,10 @@ function(${args}) {
884886
.filter((x) => x != '')
885887
.join('\n') + '\n'
886888
: '';
889+
if (aliasTarget && !isNativeAlias && !isFunction) {
890+
libraryAliases.push(commentText + contentText);
891+
return depsText.trim();
892+
}
887893
return depsText + commentText + contentText;
888894
}
889895

@@ -928,7 +934,7 @@ function(${args}) {
928934
includeSystemFile(preFile);
929935

930936
writeOutput('// Begin JS library code\n');
931-
for (const item of libraryItems.concat(postSets)) {
937+
for (const item of [...libraryItems, ...libraryAliases, ...postSets]) {
932938
writeOutput(indentify(item ?? '', 2));
933939
}
934940
writeOutput('// End JS library code\n');

‎test/codesize/test_codesize_hello_dylink_all.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 272709,
2+
"a.out.js": 272705,
33
"a.out.nodebug.wasm": 588152,
4-
"total": 860861,
4+
"total": 860857,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

‎test/test_jslib.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,15 @@ def test_jslib_include(self):
604604
def test_jslib_aliases(self, args):
605605
create_file('foo.js', '''
606606
addToLibrary({
607-
foo: () => 42,
607+
// Create a dependency cycle (foo -> $call_alias -> foo_alias -> foo)
608+
// to test that `var _foo_alias = _foo;` is still emitted after `_foo`.
609+
$call_alias__deps: ['foo_alias'],
610+
$call_alias: () => _foo_alias(),
611+
foo__deps: ['$call_alias'],
612+
foo: () => {
613+
if (!call_alias) return 0;
614+
return 42;
615+
},
608616
foo__sig: 'i',
609617
610618
foo_alias: 'foo',

0 commit comments

Comments
 (0)