Write a source map if DebugNames is enabled. #6

This commit is contained in:
Volker Berlin 2019-03-31 13:29:40 +02:00
parent 82bf9f7eea
commit ce93ce517e
5 changed files with 33 additions and 17 deletions

View File

@ -35,6 +35,7 @@ import de.inetsoftware.jwebassembly.module.ModuleWriter;
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;
@ -205,14 +206,21 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
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() );
}
}
@ -412,9 +420,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@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 );
}
}

View File

@ -109,7 +109,7 @@ public class ModuleGenerator {
ClassFile classFile = new ClassFile( new BufferedInputStream( Files.newInputStream( path ) ) );
prepare( classFile );
}
};
}
}
} catch( Exception e ) {
e.printStackTrace();
@ -370,7 +370,7 @@ public class ModuleGenerator {
List<WasmInstruction> instructions = codeBuilder.getInstructions();
optimizer.optimze( instructions );
int lastCodePosition = -1;
int lastJavaSourceLine = -1;
for( WasmInstruction instruction : instructions ) {
switch( instruction.getType() ) {
case Block:
@ -390,10 +390,10 @@ public class ModuleGenerator {
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 );
}

View File

@ -131,10 +131,10 @@ public abstract class ModuleWriter implements Closeable {
/**
* 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

View File

@ -19,11 +19,11 @@ package de.inetsoftware.jwebassembly.sourcemap;
* 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
@ -67,4 +67,12 @@ public class SourceMapping {
String getSourceFileName() {
return sourceFileName;
}
/**
* Ad an offset to the generated column
* @param offset the offset
*/
public void addOffset( int offset ) {
generatedColumn += offset;
}
}

View File

@ -202,7 +202,7 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc}
*/
@Override
protected void markCodePosition( int javaCodePosition ) {
protected void markSourceLine( int javaSourceLine ) {
// nothing
}