Skip to content

Commit dc766bc

Browse files
committed
Fix global redeclaration and update ExpressionId enum
Prevents duplicate global declarations in builtins by checking for existing globals before adding. Defers visit function compilation until after custom passes in the compiler. Updates the ExpressionId enum in module.ts to match new Binaryen IDs. Also updates build-web.js to support lockfileVersion 3 dependency resolution.
1 parent ae1b4af commit dc766bc

File tree

5 files changed

+87
-79
lines changed

5 files changed

+87
-79
lines changed

package-lock.json

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/build-web.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ export function buildWeb() {
88
const pkg = JSON.parse(fs.readFileSync(join(__dirname, "../package-lock.json")));
99

1010
const mainVersion = pkg.version;
11-
const binaryenVersion = pkg.dependencies.binaryen.version;
12-
const longVersion = pkg.dependencies.long.version;
11+
// lockfileVersion 3 stores deps in packages["node_modules/..."]
12+
const binaryenVersion = pkg.packages["node_modules/binaryen"].version;
13+
const longVersion = pkg.packages["node_modules/long"].version;
1314

1415
const distUrl = mainVersion === "0.0.0" ? `./` : `https://cdn.jsdelivr.net/npm/assemblyscript@${mainVersion}/dist/`;
1516
const binaryenUrl = `https://cdn.jsdelivr.net/npm/binaryen@${binaryenVersion}/index.js`;

src/builtins.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,10 @@ function builtin_heap_base_compile(ctx: BuiltinVariableContext): void {
23342334
let element = ctx.element;
23352335
let type = element.type;
23362336
compiler.runtimeFeatures |= RuntimeFeatures.Heap;
2337-
module.addGlobal(element.internalName, type.toRef(), true, compiler.makeZero(type)); // dummy
2337+
// Only add the dummy global if it doesn't already exist (may be called multiple times due to recompilation)
2338+
if (!module.getGlobal(element.internalName)) {
2339+
module.addGlobal(element.internalName, type.toRef(), true, compiler.makeZero(type)); // dummy
2340+
}
23382341
}
23392342
builtinVariables_onCompile.set(BuiltinNames.heap_base, builtin_heap_base_compile);
23402343

@@ -2357,7 +2360,10 @@ function builtin_data_end_compile(ctx: BuiltinVariableContext): void {
23572360
let element = ctx.element;
23582361
let type = element.type;
23592362
compiler.runtimeFeatures |= RuntimeFeatures.Data;
2360-
module.addGlobal(element.internalName, type.toRef(), true, compiler.makeZero(type)); // dummy
2363+
// Only add the dummy global if it doesn't already exist (may be called multiple times due to recompilation)
2364+
if (!module.getGlobal(element.internalName)) {
2365+
module.addGlobal(element.internalName, type.toRef(), true, compiler.makeZero(type)); // dummy
2366+
}
23612367
}
23622368
builtinVariables_onCompile.set(BuiltinNames.data_end, builtin_data_end_compile);
23632369

@@ -2380,7 +2386,10 @@ function builtin_stack_pointer_compile(ctx: BuiltinVariableContext): void {
23802386
let element = ctx.element;
23812387
let type = element.type;
23822388
compiler.runtimeFeatures |= RuntimeFeatures.Stack;
2383-
module.addGlobal(element.internalName, type.toRef(), true, compiler.makeZero(type)); // dummy
2389+
// Only add the dummy global if it doesn't already exist (may be called multiple times due to recompilation)
2390+
if (!module.getGlobal(element.internalName)) {
2391+
module.addGlobal(element.internalName, type.toRef(), true, compiler.makeZero(type)); // dummy
2392+
}
23842393
}
23852394
builtinVariables_onCompile.set(BuiltinNames.stack_pointer, builtin_stack_pointer_compile);
23862395

@@ -2403,7 +2412,10 @@ function builtin_rtti_base_compile(ctx: BuiltinVariableContext): void {
24032412
let element = ctx.element;
24042413
let type = element.type;
24052414
compiler.runtimeFeatures |= RuntimeFeatures.Rtti;
2406-
module.addGlobal(element.internalName, type.toRef(), true, compiler.makeZero(type)); // dummy
2415+
// Only add the dummy global if it doesn't already exist (may be called multiple times due to recompilation)
2416+
if (!module.getGlobal(element.internalName)) {
2417+
module.addGlobal(element.internalName, type.toRef(), true, compiler.makeZero(type)); // dummy
2418+
}
24072419
}
24082420
builtinVariables_onCompile.set(BuiltinNames.rtti_base, builtin_rtti_base_compile);
24092421

src/compiler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,11 @@ export class Compiler extends DiagnosticEmitter {
651651
}
652652
}
653653

654-
// finalize runtime features
654+
// finalize runtime features (RTTI must be compiled here as it affects memory layout)
655655
module.removeGlobal(BuiltinNames.rtti_base);
656656
if (this.runtimeFeatures & RuntimeFeatures.Rtti) compileRTTI(this);
657-
if (this.runtimeFeatures & RuntimeFeatures.visitGlobals) compileVisitGlobals(this);
658-
if (this.runtimeFeatures & RuntimeFeatures.visitMembers) compileVisitMembers(this);
657+
// NOTE: compileVisitGlobals and compileVisitMembers are deferred until after
658+
// custom passes (like shadow stack) that may trigger additional compilations
659659

660660
let memoryOffset = i64_align(this.memoryOffset, options.usizeType.byteSize);
661661

@@ -765,6 +765,10 @@ export class Compiler extends DiagnosticEmitter {
765765
new RtraceMemory(this).walkModule();
766766
}
767767

768+
// Finalize visit functions after custom passes that may trigger additional compilations
769+
if (this.runtimeFeatures & RuntimeFeatures.visitGlobals) compileVisitGlobals(this);
770+
if (this.runtimeFeatures & RuntimeFeatures.visitMembers) compileVisitMembers(this);
771+
768772
return module;
769773
}
770774

src/module.ts

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -251,74 +251,60 @@ export const enum ExpressionId {
251251
AtomicWait = 26 /* _BinaryenAtomicWaitId */,
252252
AtomicNotify = 27 /* _BinaryenAtomicNotifyId */,
253253
AtomicFence = 28 /* _BinaryenAtomicFenceId */,
254-
SIMDExtract = 29 /* _BinaryenSIMDExtractId */,
255-
SIMDReplace = 30 /* _BinaryenSIMDReplaceId */,
256-
SIMDShuffle = 31 /* _BinaryenSIMDShuffleId */,
257-
SIMDTernary = 32 /* _BinaryenSIMDTernaryId */,
258-
SIMDShift = 33 /* _BinaryenSIMDShiftId */,
259-
SIMDLoad = 34 /* _BinaryenSIMDLoadId */,
260-
SIMDLoadStoreLane = 35 /* _BinaryenSIMDLoadStoreLaneId */,
261-
MemoryInit = 36 /* _BinaryenMemoryInitId */,
262-
DataDrop = 37 /* _BinaryenDataDropId */,
263-
MemoryCopy = 38 /* _BinaryenMemoryCopyId */,
264-
MemoryFill = 39 /* _BinaryenMemoryFillId */,
265-
Pop = 40 /* _BinaryenPopId */,
266-
RefNull = 41 /* _BinaryenRefNullId */,
267-
RefIsNull = 42 /* _BinaryenRefIsNullId */,
268-
RefFunc = 43 /* _BinaryenRefFuncId */,
269-
RefEq = 44 /* _BinaryenRefEqId */,
270-
TableGet = 45 /* _BinaryenTableGetId */,
271-
TableSet = 46 /* _BinaryenTableSetId */,
272-
TableSize = 47 /* _BinaryenTableSizeId */,
273-
TableGrow = 48 /* _BinaryenTableGrowId */,
274-
TableFill = 49 /* _BinaryenTableFillId */,
275-
TableCopy = 50 /* _BinaryenTableCopyId */,
276-
TableInit = 51 /* _BinaryenTableInitId */,
277-
Try = 52 /* _BinaryenTryId */,
278-
TryTable = 53 /* _BinaryenTryTableId */,
279-
Throw = 54 /* _BinaryenThrowId */,
280-
Rethrow = 55 /* _BinaryenRethrowId */,
281-
ThrowRef = 56 /* _BinaryenThrowRefId */,
282-
TupleMake = 57 /* _BinaryenTupleMakeId */,
283-
TupleExtract = 58 /* _BinaryenTupleExtractId */,
284-
RefI31 = 59 /* _BinaryenRefI31Id */,
285-
I31Get = 60 /* _BinaryenI31GetId */,
286-
CallRef = 61 /* _BinaryenCallRefId */,
287-
RefTest = 62 /* _BinaryenRefTestId */,
288-
RefCast = 63 /* _BinaryenRefCastId */,
289-
RefGetDesc = 64 /* _BinaryenRefGetDescId */,
290-
BrOn = 65 /* _BinaryenBrOnId */,
291-
StructNew = 66 /* _BinaryenStructNewId */,
292-
StructGet = 67 /* _BinaryenStructGetId */,
293-
StructSet = 68 /* _BinaryenStructSetId */,
294-
StructRMW = 69 /* _BinaryenStructRMWId */,
295-
StructCmpxchg = 70 /* _BinaryenStructCmpxchgId */,
296-
ArrayNew = 71 /* _BinaryenArrayNewId */,
297-
ArrayNewData = 72 /* _BinaryenArrayNewDataId */,
298-
ArrayNewElem = 73 /* _BinaryenArrayNewElemId */,
299-
ArrayNewFixed = 74 /* _BinaryenArrayNewFixedId */,
300-
ArrayGet = 75 /* _BinaryenArrayGetId */,
301-
ArraySet = 76 /* _BinaryenArraySetId */,
302-
ArrayLen = 77 /* _BinaryenArrayLenId */,
303-
ArrayCopy = 78 /* _BinaryenArrayCopyId */,
304-
ArrayFill = 79 /* _BinaryenArrayFillId */,
305-
ArrayInitData = 80 /* _BinaryenArrayInitDataId */,
306-
ArrayInitElem = 81 /* _BinaryenArrayInitElemId */,
307-
RefAs = 82 /* _BinaryenRefAsId */,
308-
StringNew = 83 /* _BinaryenStringNewId */,
309-
StringConst = 84 /* _BinaryenStringConstId */,
310-
StringMeasure = 85 /* _BinaryenStringMeasureId */,
311-
StringEncode = 86 /* _BinaryenStringEncodeId */,
312-
StringConcat = 87 /* _BinaryenStringConcatId */,
313-
StringEq = 88 /* _BinaryenStringEqId */,
314-
StringWTF16Get = 89 /* _BinaryenStringWTF16GetId */,
315-
StringSliceWTF = 90 /* _BinaryenStringSliceWTFId */,
316-
ContNew = 91 /* _BinaryenContNewId */,
317-
ContBind = 92 /* _BinaryenContBindId */,
318-
Suspend = 93 /* _BinaryenSuspendId */,
319-
Resume = 94 /* _BinaryenResumeId */,
320-
ResumeThrow = 95 /* _BinaryenResumeThrowId */,
321-
StackSwitch = 96 /* _BinaryenStackSwitchId */
254+
SIMDExtract = 30 /* _BinaryenSIMDExtractId */,
255+
SIMDReplace = 31 /* _BinaryenSIMDReplaceId */,
256+
SIMDShuffle = 32 /* _BinaryenSIMDShuffleId */,
257+
SIMDTernary = 33 /* _BinaryenSIMDTernaryId */,
258+
SIMDShift = 34 /* _BinaryenSIMDShiftId */,
259+
SIMDLoad = 35 /* _BinaryenSIMDLoadId */,
260+
SIMDLoadStoreLane = 36 /* _BinaryenSIMDLoadStoreLaneId */,
261+
MemoryInit = 37 /* _BinaryenMemoryInitId */,
262+
DataDrop = 38 /* _BinaryenDataDropId */,
263+
MemoryCopy = 39 /* _BinaryenMemoryCopyId */,
264+
MemoryFill = 40 /* _BinaryenMemoryFillId */,
265+
Pop = 41 /* _BinaryenPopId */,
266+
RefNull = 42 /* _BinaryenRefNullId */,
267+
RefIsNull = 43 /* _BinaryenRefIsNullId */,
268+
RefFunc = 44 /* _BinaryenRefFuncId */,
269+
RefEq = 45 /* _BinaryenRefEqId */,
270+
TableGet = 46 /* _BinaryenTableGetId */,
271+
TableSet = 47 /* _BinaryenTableSetId */,
272+
TableSize = 48 /* _BinaryenTableSizeId */,
273+
TableGrow = 49 /* _BinaryenTableGrowId */,
274+
Try = 54 /* _BinaryenTryId */,
275+
Throw = 56 /* _BinaryenThrowId */,
276+
Rethrow = 57 /* _BinaryenRethrowId */,
277+
TupleMake = 59 /* _BinaryenTupleMakeId */,
278+
TupleExtract = 60 /* _BinaryenTupleExtractId */,
279+
RefI31 = 61 /* _BinaryenRefI31Id */,
280+
I31Get = 62 /* _BinaryenI31GetId */,
281+
CallRef = 63 /* _BinaryenCallRefId */,
282+
RefTest = 64 /* _BinaryenRefTestId */,
283+
RefCast = 65 /* _BinaryenRefCastId */,
284+
BrOn = 67 /* _BinaryenBrOnId */,
285+
StructNew = 68 /* _BinaryenStructNewId */,
286+
StructGet = 69 /* _BinaryenStructGetId */,
287+
StructSet = 70 /* _BinaryenStructSetId */,
288+
ArrayNew = 73 /* _BinaryenArrayNewId */,
289+
ArrayNewData = 74 /* _BinaryenArrayNewDataId */,
290+
ArrayNewElem = 75 /* _BinaryenArrayNewElemId */,
291+
ArrayNewFixed = 76 /* _BinaryenArrayNewFixedId */,
292+
ArrayGet = 77 /* _BinaryenArrayGetId */,
293+
ArraySet = 78 /* _BinaryenArraySetId */,
294+
ArrayLen = 79 /* _BinaryenArrayLenId */,
295+
ArrayCopy = 80 /* _BinaryenArrayCopyId */,
296+
ArrayFill = 81 /* _BinaryenArrayFillId */,
297+
ArrayInitData = 82 /* _BinaryenArrayInitDataId */,
298+
ArrayInitElem = 83 /* _BinaryenArrayInitElemId */,
299+
RefAs = 86 /* _BinaryenRefAsId */,
300+
StringNew = 87 /* _BinaryenStringNewId */,
301+
StringConst = 88 /* _BinaryenStringConstId */,
302+
StringMeasure = 89 /* _BinaryenStringMeasureId */,
303+
StringEncode = 90 /* _BinaryenStringEncodeId */,
304+
StringConcat = 91 /* _BinaryenStringConcatId */,
305+
StringEq = 92 /* _BinaryenStringEqId */,
306+
StringWTF16Get = 94 /* _BinaryenStringWTF16GetId */,
307+
StringSliceWTF = 95 /* _BinaryenStringSliceWTFId */
322308
}
323309

324310
/** Binaryen external kind constants. */

0 commit comments

Comments
 (0)