Skip to content

Commit

Permalink
Write a source map if DebugNames is enabled. #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Mar 31, 2019
1 parent 82bf9f7 commit ce93ce5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
16 changes: 12 additions & 4 deletions src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
import de.inetsoftware.jwebassembly.module.WasmTarget;
import de.inetsoftware.jwebassembly.sourcemap.SourceMapWriter;
import de.inetsoftware.jwebassembly.sourcemap.SourceMapping;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
Expand Down Expand Up @@ -205,14 +206,21 @@ private void writeCodeSection() throws IOException {
if( size == 0 ) {
return;
}
SourceMapWriter sourceMap = new SourceMapWriter();
SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter() : null;

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.functionsStream.writeTo( stream );
}
wasm.writeSection( SectionType.Code, stream );
if( createSourceMap ) {
if( sourceMap != null ) {
sourceMap.generate( target.getSourceMapOutput() );
}
}
Expand Down Expand Up @@ -412,9 +420,9 @@ protected void writeMethodParamFinish() throws IOException {
* {@inheritDoc}
*/
@Override
protected void markCodePosition( int javaCodePosition ) {
protected void markSourceLine( int javaSourceLine ) {
if( createSourceMap ) {
function.markCodePosition( codeStream.size(), javaCodePosition, javaSourceFile );
function.markCodePosition( codeStream.size(), javaSourceLine, javaSourceFile );
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void scanLibraries( @Nonnull List<URL> libraries ) {
ClassFile classFile = new ClassFile( new BufferedInputStream( Files.newInputStream( path ) ) );
prepare( classFile );
}
};
}
}
} catch( Exception e ) {
e.printStackTrace();
Expand Down Expand Up @@ -370,7 +370,7 @@ private void writeMethodImpl( FunctionName name, boolean isStatic, LocalVariable
List<WasmInstruction> instructions = codeBuilder.getInstructions();
optimizer.optimze( instructions );

int lastCodePosition = -1;
int lastJavaSourceLine = -1;
for( WasmInstruction instruction : instructions ) {
switch( instruction.getType() ) {
case Block:
Expand All @@ -390,10 +390,10 @@ private void writeMethodImpl( FunctionName name, boolean isStatic, LocalVariable
break;
default:
}
int codePosition = instruction.getCodePosition();
if( codePosition >= 0 && codePosition != lastCodePosition ) {
writer.markCodePosition( codePosition );
lastCodePosition = codePosition;
int javaSourceLine = instruction.getLineNumber();
if( javaSourceLine >= 0 && javaSourceLine != lastJavaSourceLine ) {
writer.markSourceLine( javaSourceLine );
lastJavaSourceLine = javaSourceLine;
}
instruction.writeTo( writer );
}
Expand Down
6 changes: 3 additions & 3 deletions src/de/inetsoftware/jwebassembly/module/ModuleWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public void prepareFinish() {
/**
* Mark the current output position with Java code position for crating of a source map.
*
* @param javaCodePosition
* the position in the Java code
* @param javaSourceLine
* the line number in the Java code
*/
protected abstract void markCodePosition( int javaCodePosition );
protected abstract void markSourceLine( int javaSourceLine );

/**
* Complete the method
Expand Down
14 changes: 11 additions & 3 deletions src/de/inetsoftware/jwebassembly/sourcemap/SourceMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
* Mapping for Source Map.
*/
public class SourceMapping {
private final int generatedColumn;
private int generatedColumn;

private final int sourceLine;
private int sourceLine;

private final String sourceFileName;
private String sourceFileName;

/**
* Create a mapping between a Java code line and a WebAssembly code position
Expand Down Expand Up @@ -67,4 +67,12 @@ int getSourceLine() {
String getSourceFileName() {
return sourceFileName;
}

/**
* Ad an offset to the generated column
* @param offset the offset
*/
public void addOffset( int offset ) {
generatedColumn += offset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ protected void writeMethodParamFinish( ) throws IOException {
* {@inheritDoc}
*/
@Override
protected void markCodePosition( int javaCodePosition ) {
protected void markSourceLine( int javaSourceLine ) {
// nothing
}

Expand Down

0 comments on commit ce93ce5

Please sign in to comment.