Skip to content

Commit bc725a4

Browse files
committed
Refactor closure capture analysis and minor cleanups
Refactored analyzeCapturedVariablesWithDeclared to remove the unused outerFunc parameter and updated its call sites. Improved closure environment setup logic, fixed minor code style issues, and removed redundant code. Also made minor whitespace and formatting adjustments throughout the file. Simplify capture merging logic in Compiler Refactored the merging of captures to remove redundant check for undefined captureIndex, as the value is always present when setting in existingCaptures.
1 parent 80aa6ab commit bc725a4

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/compiler.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ export class Compiler extends DiagnosticEmitter {
15261526

15271527
private ensureEnumToString(enumElement: Enum, reportNode: Node): string | null {
15281528
if (enumElement.toStringFunctionName) return enumElement.toStringFunctionName;
1529-
1529+
15301530
if (!this.compileEnum(enumElement)) return null;
15311531
if (enumElement.is(CommonFlags.Const)) {
15321532
this.errorRelated(
@@ -2695,7 +2695,7 @@ export class Compiler extends DiagnosticEmitter {
26952695
// (then │ │ (body) │
26962696
// (?block $continue │ │ if loops: (incrementor) ─────┘
26972697
// (body) │ │ recompile body?
2698-
// ) ├◄┘
2698+
// ) ├◄┘
26992699
// (incrementor) ┌◄┘
27002700
// (br $loop)
27012701
// )
@@ -2984,17 +2984,17 @@ export class Compiler extends DiagnosticEmitter {
29842984
// Compile the condition (always executes)
29852985
let condExpr = this.compileExpression(statement.condition, Type.auto);
29862986
let condType = this.currentType;
2987-
2987+
29882988
// Shortcut if there are no cases
29892989
if (!numCases) return module.drop(condExpr);
2990-
2990+
29912991
// Assign the condition to a temporary local as we compare it multiple times
29922992
let outerFlow = this.currentFlow;
29932993
let tempLocal = outerFlow.getTempLocal(condType);
29942994
let tempLocalIndex = tempLocal.index;
29952995
let breaks = new Array<ExpressionRef>(1 + numCases);
29962996
breaks[0] = module.local_set(tempLocalIndex, condExpr, condType.isManaged);
2997-
2997+
29982998
// Make one br_if per labeled case and leave it to Binaryen to optimize the
29992999
// sequence of br_ifs to a br_table according to optimization levels
30003000
let breakIndex = 1;
@@ -3006,7 +3006,7 @@ export class Compiler extends DiagnosticEmitter {
30063006
defaultIndex = i;
30073007
continue;
30083008
}
3009-
3009+
30103010
// Compile the equality expression for this case
30113011
const left = statement.condition;
30123012
const leftExpr = module.local_get(tempLocalIndex, condType.toRef());
@@ -3021,7 +3021,7 @@ export class Compiler extends DiagnosticEmitter {
30213021
condType,
30223022
statement
30233023
);
3024-
3024+
30253025
// Add it to the list of breaks
30263026
breaks[breakIndex++] = module.br(`case${i}|${label}`, equalityExpr);
30273027
}
@@ -4012,7 +4012,7 @@ export class Compiler extends DiagnosticEmitter {
40124012
expression: BinaryExpression,
40134013
contextualType: Type,
40144014
): ExpressionRef {
4015-
4015+
40164016
const left = expression.left;
40174017
const leftExpr = this.compileExpression(left, contextualType);
40184018
const leftType = this.currentType;
@@ -4030,9 +4030,9 @@ export class Compiler extends DiagnosticEmitter {
40304030
);
40314031
}
40324032

4033-
/**
4033+
/**
40344034
* compile `==` `===` `!=` `!==` BinaryExpression, from previously compiled left and right expressions.
4035-
*
4035+
*
40364036
* This is split from `compileCommutativeCompareBinaryExpression` so that the logic can be reused
40374037
* for switch cases in `compileSwitchStatement`, where the left expression only should be compiled once.
40384038
*/
@@ -4050,15 +4050,15 @@ export class Compiler extends DiagnosticEmitter {
40504050

40514051
let module = this.module;
40524052
let operatorString = operatorTokenToString(operator);
4053-
4053+
40544054
// check operator overload
40554055
const operatorKind = OperatorKind.fromBinaryToken(operator);
40564056
const leftOverload = leftType.lookupOverload(operatorKind, this.program);
40574057
const rightOverload = rightType.lookupOverload(operatorKind, this.program);
40584058
if (leftOverload && rightOverload && leftOverload != rightOverload) {
40594059
this.error(
40604060
DiagnosticCode.Ambiguous_operator_overload_0_conflicting_overloads_1_and_2,
4061-
reportNode.range,
4061+
reportNode.range,
40624062
operatorString,
40634063
leftOverload.internalName,
40644064
rightOverload.internalName
@@ -4148,7 +4148,7 @@ export class Compiler extends DiagnosticEmitter {
41484148

41494149
leftExpr = this.compileExpression(left, contextualType);
41504150
leftType = this.currentType;
4151-
4151+
41524152
// check operator overload
41534153
const operatorKind = OperatorKind.fromBinaryToken(operator);
41544154
const leftOverload = leftType.lookupOverload(operatorKind, this.program);
@@ -4219,7 +4219,7 @@ export class Compiler extends DiagnosticEmitter {
42194219
return this.compileNonCommutativeCompareBinaryExpression(expression, contextualType);
42204220
}
42214221
case Token.Equals_Equals_Equals:
4222-
case Token.Equals_Equals:
4222+
case Token.Equals_Equals:
42234223
case Token.Exclamation_Equals_Equals:
42244224
case Token.Exclamation_Equals: {
42254225
return this.compileCommutativeCompareBinaryExpression(expression, contextualType);
@@ -6537,13 +6537,13 @@ export class Compiler extends DiagnosticEmitter {
65376537
if (numArguments < numParams) {
65386538
return argumentExpressions;
65396539
}
6540-
6540+
65416541
// make an array literal expression from the rest args
65426542
let elements = argumentExpressions.slice(numParams - 1);
65436543
let range = new Range(elements[0].range.start, elements[elements.length - 1].range.end);
65446544
range.source = reportNode.range.source;
65456545
let arrExpr = new ArrayLiteralExpression(elements, range);
6546-
6546+
65476547
// return the original args, but replace the rest args with the array
65486548
const exprs = argumentExpressions.slice(0, numParams - 1);
65496549
exprs.push(arrExpr);
@@ -8333,8 +8333,7 @@ export class Compiler extends DiagnosticEmitter {
83338333
case NodeKind.Function: {
83348334
// Found a function expression - analyze its captures
83358335
let funcExpr = <FunctionExpression>current;
8336-
let declaration = funcExpr.declaration;
8337-
let capturedNames = this.analyzeCapturedVariablesWithDeclared(declaration, flow, instance, declaredVars);
8336+
let capturedNames = this.analyzeCapturedVariablesWithDeclared(funcExpr.declaration, flow, declaredVars);
83388337
if (capturedNames.size > 0) {
83398338
// Check if closures feature is enabled
83408339
if (!this.options.hasFeature(Feature.Closures)) {
@@ -8584,7 +8583,6 @@ export class Compiler extends DiagnosticEmitter {
85848583
private analyzeCapturedVariablesWithDeclared(
85858584
declaration: FunctionDeclaration,
85868585
outerFlow: Flow,
8587-
outerFunc: Function,
85888586
declaredVars: Map<string, Type | null>
85898587
): Set<string> {
85908588
// For prescan, we just collect variable NAMES that are captured
@@ -8630,8 +8628,7 @@ export class Compiler extends DiagnosticEmitter {
86308628
if (endOfSlot > maxEnd) maxEnd = endOfSlot;
86318629
}
86328630
// Ensure total size is aligned to pointer size
8633-
let size = (maxEnd + usizeSize - 1) & ~(usizeSize - 1);
8634-
return size;
8631+
return (maxEnd + usizeSize - 1) & ~(usizeSize - 1);
86358632
}
86368633

86378634
/** Ensures a closure environment is set up for the outer function. */
@@ -8648,7 +8645,7 @@ export class Compiler extends DiagnosticEmitter {
86488645
for (let _keys = Map_keys(captures), i = 0, k = _keys.length; i < k; i++) {
86498646
let local = _keys[i];
86508647
if (!existingCaptures.has(local)) {
8651-
existingCaptures.set(local, captures.get(local)!);
8648+
existingCaptures.set(local, captures.get(local) as i32);
86528649
}
86538650
}
86548651
}
@@ -8657,8 +8654,7 @@ export class Compiler extends DiagnosticEmitter {
86578654

86588655
// Create a new environment local for the outer function
86598656
let usizeType = this.options.usizeType;
8660-
let envLocal = flow.addScopedLocal("$env", usizeType);
8661-
outerFunc.envLocal = envLocal;
8657+
outerFunc.envLocal = flow.addScopedLocal("$env", usizeType);;
86628658
outerFunc.capturedLocals = captures;
86638659

86648660
// Compute the environment size
@@ -9495,7 +9491,7 @@ export class Compiler extends DiagnosticEmitter {
94959491
stmts.length = 1;
94969492
stmts.push(
94979493
module.i32(1)
9498-
);
9494+
);
94999495
module.removeFunction(name);
95009496
module.addFunction(name, sizeType, TypeRef.I32, [ TypeRef.I32 ], module.block(null, stmts, TypeRef.I32));
95019497
}

0 commit comments

Comments
 (0)