663
664
const scopeTraversal = new ScopeBlockTraversal();
665
666
- for (const [blockId, block] of fn.body.blocks) {
667
- scopeTraversal.recordScopes(block);
668
- const scopeBlockInfo = scopeTraversal.blockInfos.get(blockId);
669
- if (scopeBlockInfo?.kind === 'begin') {
670
- context.enterScope(scopeBlockInfo.scope);
671
- } else if (scopeBlockInfo?.kind === 'end') {
672
- context.exitScope(scopeBlockInfo.scope, scopeBlockInfo?.pruned);
673
- }
674
-
675
- // Record referenced optional chains in phis
676
- for (const phi of block.phis) {
677
- for (const operand of phi.operands) {
678
- const maybeOptionalChain = temporaries.get(operand[1].identifier.id);
679
- if (maybeOptionalChain) {
680
- context.visitDependency(maybeOptionalChain);
666
+ const handleFunction = (fn: HIRFunction): void => {
667
+ for (const [blockId, block] of fn.body.blocks) {
668
+ scopeTraversal.recordScopes(block);
669
+ const scopeBlockInfo = scopeTraversal.blockInfos.get(blockId);
670
+ if (scopeBlockInfo?.kind === 'begin') {
671
+ context.enterScope(scopeBlockInfo.scope);
672
+ } else if (scopeBlockInfo?.kind === 'end') {
673
+ context.exitScope(scopeBlockInfo.scope, scopeBlockInfo.pruned);
674
+ }
675
+ // Record referenced optional chains in phis
676
+ for (const phi of block.phis) {
677
+ for (const operand of phi.operands) {
678
+ const maybeOptionalChain = temporaries.get(operand[1].identifier.id);
679
+ if (maybeOptionalChain) {
680
+ context.visitDependency(maybeOptionalChain);
681
+ }
682
}
683
}
683
- }
684
- for (const instr of block.instructions) {
685
- if (!processedInstrsInOptional.has(instr)) {
686
- handleInstruction(instr, context);
684
+ for (const instr of block.instructions) {
685
+ if (
686
+ fn.env.config.enableFunctionDependencyRewrite &&
687
+ (instr.value.kind === 'FunctionExpression' ||
688
+ instr.value.kind === 'ObjectMethod')
689
+ ) {
690
+ context.declare(instr.lvalue.identifier, {
691
+ id: instr.id,
692
+ scope: context.currentScope,
693
+ });
694
+ /**
695
+ * Recursively visit the inner function to extract dependencies there
696
+ */
697
+ const wasInInnerFn = context.inInnerFn;
698
+ context.inInnerFn = true;
699
+ handleFunction(instr.value.loweredFunc.func);
700
+ context.inInnerFn = wasInInnerFn;
701
+ } else if (!processedInstrsInOptional.has(instr)) {
702
+ handleInstruction(instr, context);
703
+ }
704
}
688
- }
705
690
- if (!processedInstrsInOptional.has(block.terminal)) {
691
- for (const place of eachTerminalOperand(block.terminal)) {
692
- context.visitOperand(place);
706
+ if (!processedInstrsInOptional.has(block.terminal)) {
707
+ for (const place of eachTerminalOperand(block.terminal)) {
708
+ context.visitOperand(place);
709
+ }
710
}
711
}
695
- }
712
+ };
713
+
714
+ handleFunction(fn);
715
return context.deps;
716
}