diff --git a/src/compiler.ts b/src/compiler.ts index a2b313603b..baa324e97c 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -9870,8 +9870,10 @@ export class Compiler extends DiagnosticEmitter { let targetFunction = this.currentFlow.targetFunction; let source = range.source; if (source.debugInfoIndex < 0) source.debugInfoIndex = this.module.addDebugInfoFile(source.normalizedPath); - range.debugInfoRef = expr; - targetFunction.debugLocations.push(range); + // It's possible that an `expr` is seen multiple times, for example when + // first adding debug information for an inner expression and later on for + // an expression supposedly wrapping it, where the wrapping became a noop. + targetFunction.debugLocations.set(expr, range); } /** Checks whether a particular function signature is supported. */ diff --git a/src/diagnostics.ts b/src/diagnostics.ts index 9ca1e3d4e1..963003023b 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -45,7 +45,6 @@ export const enum DiagnosticCategory { export class Range { source!: Source; - debugInfoRef: usize = 0; constructor(public start: i32, public end: i32) {} diff --git a/src/program.ts b/src/program.ts index 829cffee05..5bae6bd76b 100644 --- a/src/program.ts +++ b/src/program.ts @@ -116,6 +116,7 @@ import { import { Module, + ExpressionRef, FunctionRef, MemorySegment, getFunctionName @@ -3749,8 +3750,8 @@ export class Function extends TypedElement { contextualTypeArguments: Map | null; /** Default control flow. */ flow!: Flow; - /** Remembered debug locations. */ - debugLocations: Range[] = []; + /** Ordered debug locations by Binaryen expression reference. */ + debugLocations: Map = new Map(); /** Function reference, if compiled. */ ref: FunctionRef = 0; /** Varargs stub for calling with omitted arguments. */ @@ -3918,12 +3919,13 @@ export class Function extends TypedElement { addDebugInfo(module: Module, ref: FunctionRef): void { if (this.program.options.sourceMap) { let debugLocations = this.debugLocations; - for (let i = 0, k = debugLocations.length; i < k; ++i) { - let range = debugLocations[i]; + for (let _keys = Map_keys(debugLocations), i = 0, k = _keys.length; i < k; ++i) { + let expressionRef = _keys[i]; + let range = assert(debugLocations.get(expressionRef)); let source = range.source; module.setDebugLocation( ref, - range.debugInfoRef, + expressionRef, source.debugInfoIndex, source.lineAt(range.start), source.columnAt() - 1 // source maps are 0-based