Skip to content

Commit

Permalink
Fix the offset for the source map positions. #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Apr 1, 2019
1 parent dd9eb4b commit 47430b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
}
Expand Down Expand Up @@ -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 );
}
Expand Down
14 changes: 14 additions & 0 deletions src/de/inetsoftware/jwebassembly/binary/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}
}

0 comments on commit 47430b3

Please sign in to comment.