Fix the offset for the source map positions. #6

This commit is contained in:
Volker Berlin 2019-04-01 20:33:12 +02:00
parent dd9eb4b049
commit 47430b3bc0
2 changed files with 28 additions and 7 deletions

View File

@ -207,21 +207,27 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
if( size == 0 ) { if( size == 0 ) {
return; return;
} }
SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter() : null;
int start = wasm.size();
WasmOutputStream stream = new WasmOutputStream(); WasmOutputStream stream = new WasmOutputStream();
stream.writeVaruint32( size ); stream.writeVaruint32( size );
for( Function func : functions.values() ) { for( Function func : functions.values() ) {
if( sourceMap != null && func.sourceMappings != null ) { func.addCodeOffset( start + stream.size() );
for( SourceMapping mapping : func.sourceMappings ) {
mapping.addOffset( wasm.size() );
sourceMap.addMapping( mapping );
}
}
func.functionsStream.writeTo( stream ); func.functionsStream.writeTo( stream );
} }
wasm.writeSection( SectionType.Code, stream ); wasm.writeSection( SectionType.Code, stream );
SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter() : null;
if( sourceMap != 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() ); sourceMap.generate( target.getSourceMapOutput() );
} }
} }
@ -461,6 +467,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
WasmOutputStream functionsStream = function.functionsStream = new WasmOutputStream(); WasmOutputStream functionsStream = function.functionsStream = new WasmOutputStream();
functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 ); functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 );
localsStream.writeTo( functionsStream ); localsStream.writeTo( functionsStream );
function.addCodeOffset( functionsStream.size() );
codeStream.writeTo( functionsStream ); codeStream.writeTo( functionsStream );
functionsStream.write( END ); functionsStream.write( END );
} }

View File

@ -62,4 +62,18 @@ class Function extends SectionEntry {
} }
sourceMappings.add( new SourceMapping( streamPosition, javaSourceLine, sourceFileName ) ); 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 );
}
}
}
} }