Skip to content

Commit

Permalink
codegen: hoist function decls pre-codegen
Browse files Browse the repository at this point in the history
test262: 50.96% (+0.01) | πŸ§ͺ 48381 | 🀠 24655 (+4) | ❌ 6924 (-4) | πŸ’€ 15257 (+1) | πŸ—οΈ 146 (-1) | πŸ’₯ 329 | ⏰ 134 | πŸ“ 936
  • Loading branch information
CanadaHonk committed Nov 25, 2024
1 parent b023571 commit f3f1b03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6432,6 +6432,25 @@ const generateFunc = (scope, decl, forceNoExpr = false) => {
};
}

// todo: enable for all block statement bodies, not just main
if (name === '#main') {
// hoist function declarations to the top of AST pre-codegen so
// we can optimize function calls so calls before decl are not indirect
// (without more post-codegen jank)
// plus, more spec-compliant hoisting!

let b = body.body, j = 0;

// append after directive if it exists
if (b[0]?.directive) j++;

for (let i = 0; i < b.length; i++) {
if (b[i].type === 'FunctionDeclaration') {
b.splice(j++, 0, b.splice(i, 1)[0]);
}
}
}

func.identFailEarly = true;
let localInd = args.length * 2;
for (let i = 0; i < args.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion test262/history.json

Large diffs are not rendered by default.

0 comments on commit f3f1b03

Please sign in to comment.