diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 60e3a1ce..a5ce90b4 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -207,21 +207,27 @@ private void writeCodeSection() throws IOException { if( size == 0 ) { return; } - SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter() : null; + int start = wasm.size(); WasmOutputStream stream = new WasmOutputStream(); stream.writeVaruint32( size ); for( Function func : functions.values() ) { - if( sourceMap != null && func.sourceMappings != null ) { - for( SourceMapping mapping : func.sourceMappings ) { - mapping.addOffset( wasm.size() ); - sourceMap.addMapping( mapping ); - } - } + func.addCodeOffset( start + stream.size() ); func.functionsStream.writeTo( stream ); } wasm.writeSection( SectionType.Code, stream ); + + SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter() : null; if( sourceMap != null ) { + int offset = wasm.size() - start - stream.size(); + for( Function func : functions.values() ) { + if( func.sourceMappings != null ) { + func.addCodeOffset( offset ); + for( SourceMapping mapping : func.sourceMappings ) { + sourceMap.addMapping( mapping ); + } + } + } sourceMap.generate( target.getSourceMapOutput() ); } } @@ -461,6 +467,7 @@ protected void writeMethodFinish() throws IOException { WasmOutputStream functionsStream = function.functionsStream = new WasmOutputStream(); functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 ); localsStream.writeTo( functionsStream ); + function.addCodeOffset( functionsStream.size() ); codeStream.writeTo( functionsStream ); functionsStream.write( END ); } diff --git a/src/de/inetsoftware/jwebassembly/binary/Function.java b/src/de/inetsoftware/jwebassembly/binary/Function.java index 6952f8a2..56c56979 100644 --- a/src/de/inetsoftware/jwebassembly/binary/Function.java +++ b/src/de/inetsoftware/jwebassembly/binary/Function.java @@ -62,4 +62,18 @@ void markCodePosition( int streamPosition, int javaSourceLine, String sourceFile } sourceMappings.add( new SourceMapping( streamPosition, javaSourceLine, sourceFileName ) ); } + + /** + * Add an offset to the marked code position in the source map + * + * @param offset + * the offset + */ + void addCodeOffset( int offset ) { + if( sourceMappings != null ) { + for( SourceMapping mapping : sourceMappings ) { + mapping.addOffset( offset ); + } + } + } }