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
15 changes: 13 additions & 2 deletions src/jsifier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ const PROXY_SYNC_ASYNC = 2;

export async function runJSify(outputFile, symbolsOnly) {
const libraryItems = [];
const libraryAliases = [];
const symbolDeps = {};
const asyncFuncs = [];
let postSets = [];
Expand Down Expand Up @@ -728,11 +729,12 @@ function(${args}) {
}
}

let aliasTarget;
if (LibraryManager.isAlias(snippet)) {
// Redirection for aliases. We include the parent, and at runtime
// make ourselves equal to it. This avoid having duplicate
// functions with identical content.
const aliasTarget = snippet;
aliasTarget = snippet;
if (WASM_EXPORTS.has(aliasTarget)) {
debugLog(`native alias: ${mangled} -> ${aliasTarget}`);
nativeAliases[mangled] = aliasTarget;
Expand Down Expand Up @@ -884,6 +886,15 @@ function(${args}) {
.filter((x) => x != '')
.join('\n') + '\n'
: '';
// Defer direct JS value aliases (`var _alias = _target;`) to `libraryAliases`
// so the target symbol is guaranteed to be defined first. Skip native
// aliases (which emit no code here) and signature-mismatch aliases (where
// `isFunction` is set because they are emitted as forwarding wrapper
// functions rather than immediate value assignments).
if (aliasTarget && !isNativeAlias && !isFunction) {
libraryAliases.push(commentText + contentText);
return depsText.trim();
}
return depsText + commentText + contentText;
}

Expand Down Expand Up @@ -928,7 +939,7 @@ function(${args}) {
includeSystemFile(preFile);

writeOutput('// Begin JS library code\n');
for (const item of libraryItems.concat(postSets)) {
for (const item of [...libraryItems, ...libraryAliases, ...postSets]) {
writeOutput(indentify(item ?? '', 2));
}
writeOutput('// End JS library code\n');
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_dylink_all.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"a.out.js": 272713,
"a.out.js": 272709,
"a.out.nodebug.wasm": 588152,
"total": 860865,
"total": 860861,
"sent": [
"IMG_Init",
"IMG_Load",
Expand Down
10 changes: 9 additions & 1 deletion test/test_jslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,15 @@ def test_jslib_include(self):
def test_jslib_aliases(self, args):
create_file('foo.js', '''
addToLibrary({
foo: () => 42,
// Create a dependency cycle (foo -> $call_alias -> foo_alias -> foo)
// to test that `var _foo_alias = _foo;` is still emitted after `_foo`.
$call_alias__deps: ['foo_alias'],
$call_alias: () => _foo_alias(),
foo__deps: ['$call_alias'],
foo: () => {
if (!call_alias) return 0;
return 42;
},
foo__sig: 'i',

foo_alias: 'foo',
Expand Down
Loading