Skip to content

Commit 80aa6ab

Browse files
committed
Refactor environment slot allocation logic
Simplifies and streamlines the allocation of environment slots for captured locals and 'this' references by removing unnecessary variable assignments and directly assigning results. This improves code readability and reduces redundancy in the closure environment setup.
1 parent 61ff8b0 commit 80aa6ab

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

src/compiler.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,8 +1782,7 @@ export class Compiler extends DiagnosticEmitter {
17821782
let preCapturedNames = instance.preCapturedNames;
17831783
if (preCapturedNames && preCapturedNames.size > 0) {
17841784
// Check if any parameters are captured
1785-
let parameterTypes = instance.signature.parameterTypes;
1786-
for (let i = 0, k = parameterTypes.length; i < k; i++) {
1785+
for (let i = 0, k = instance.signature.parameterTypes.length; i < k; i++) {
17871786
let paramName = instance.getParameterName(i);
17881787
if (preCapturedNames.has(paramName)) {
17891788
let local = flow.lookupLocal(paramName);
@@ -1798,24 +1797,21 @@ export class Compiler extends DiagnosticEmitter {
17981797
if (!capturedLocals.has(local)) {
17991798
// Calculate proper byte offset with alignment
18001799
// Reserve slot 0 for parent environment pointer (4 or 8 bytes depending on wasm32/64)
1801-
let ptrSize = this.options.usizeType.byteSize;
1802-
let currentOffset = ptrSize; // Start after parent pointer slot
1800+
let currentOffset = this.options.usizeType.byteSize; // Start after parent pointer slot
18031801
for (let _keys = Map_keys(capturedLocals), j = 0, m = _keys.length; j < m; ++j) {
18041802
let existingLocal = _keys[j];
18051803
let endOfSlot = existingLocal.envSlotIndex + existingLocal.type.byteSize;
18061804
if (endOfSlot > currentOffset) currentOffset = endOfSlot;
18071805
}
18081806
// Align to the type's natural alignment
1809-
let typeSize = local.type.byteSize;
1810-
let align = typeSize;
1807+
let align = local.type.byteSize;
18111808
currentOffset = (currentOffset + align - 1) & ~(align - 1);
18121809
local.envSlotIndex = currentOffset;
18131810
local.envOwner = instance; // Track which function owns this capture
18141811
capturedLocals.set(local, local.envSlotIndex);
18151812
}
18161813
if (!instance.envLocal) {
1817-
let envLocal = flow.addScopedLocal("$env", this.options.usizeType);
1818-
instance.envLocal = envLocal;
1814+
instance.envLocal = flow.addScopedLocal("$env", this.options.usizeType);
18191815
}
18201816
}
18211817
}
@@ -1832,23 +1828,20 @@ export class Compiler extends DiagnosticEmitter {
18321828
instance.capturedLocals = capturedLocals;
18331829
}
18341830
if (!capturedLocals.has(thisLocal)) {
1835-
let ptrSize = this.options.usizeType.byteSize;
1836-
let currentOffset = ptrSize;
1831+
let currentOffset = this.options.usizeType.byteSize;
18371832
for (let _keys = Map_keys(capturedLocals), j = 0, m = _keys.length; j < m; ++j) {
18381833
let existingLocal = _keys[j];
18391834
let endOfSlot = existingLocal.envSlotIndex + existingLocal.type.byteSize;
18401835
if (endOfSlot > currentOffset) currentOffset = endOfSlot;
18411836
}
1842-
let typeSize = thisLocal.type.byteSize;
1843-
let align = typeSize;
1837+
let align = thisLocal.type.byteSize;
18441838
currentOffset = (currentOffset + align - 1) & ~(align - 1);
18451839
thisLocal.envSlotIndex = currentOffset;
18461840
thisLocal.envOwner = instance;
18471841
capturedLocals.set(thisLocal, thisLocal.envSlotIndex);
18481842
}
18491843
if (!instance.envLocal) {
1850-
let envLocal = flow.addScopedLocal("$env", this.options.usizeType);
1851-
instance.envLocal = envLocal;
1844+
instance.envLocal = flow.addScopedLocal("$env", this.options.usizeType);
18521845
}
18531846
}
18541847
}
@@ -1858,8 +1851,7 @@ export class Compiler extends DiagnosticEmitter {
18581851
// the environment pointer. This is needed because indirect calls to other closures
18591852
// can overwrite the global $~lib/__closure_env.
18601853
if (instance.outerFunction && !instance.closureEnvLocal) {
1861-
let closureEnvLocal = flow.addScopedLocal("$closureEnv", this.options.usizeType);
1862-
instance.closureEnvLocal = closureEnvLocal;
1854+
instance.closureEnvLocal = flow.addScopedLocal("$closureEnv", this.options.usizeType);
18631855
}
18641856

18651857
// compile statements
@@ -2005,8 +1997,7 @@ export class Compiler extends DiagnosticEmitter {
20051997
valueTypeRef, property.memoryOffset
20061998
);
20071999
let flowBefore = this.currentFlow;
2008-
let flow = getterInstance.flow;
2009-
this.currentFlow = flow;
2000+
this.currentFlow = getterInstance.flow;
20102001
if (property.is(CommonFlags.DefinitelyAssigned) && valueType.isReference && !valueType.isNullableReference) {
20112002
body = this.makeRuntimeNonNullCheck(body, valueType, getterInstance.identifierNode);
20122003
}

0 commit comments

Comments
 (0)