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.ValueTypeConvertion;
import de.inetsoftware.jwebassembly.module.WasmTarget; import de.inetsoftware.jwebassembly.module.WasmTarget;
import de.inetsoftware.jwebassembly.sourcemap.SourceMapWriter; import de.inetsoftware.jwebassembly.sourcemap.SourceMapWriter;
import de.inetsoftware.jwebassembly.sourcemap.SourceMapping;
import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
@ -205,14 +206,21 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
if( size == 0 ) { if( size == 0 ) {
return; return;
} }
SourceMapWriter sourceMap = new SourceMapWriter(); SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter() : null;
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 ) {
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 );
if( createSourceMap ) { if( sourceMap != null ) {
sourceMap.generate( target.getSourceMapOutput() ); sourceMap.generate( target.getSourceMapOutput() );
} }
} }
@ -412,9 +420,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void markCodePosition( int javaCodePosition ) { protected void markSourceLine( int javaSourceLine ) {
if( createSourceMap ) { 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 ) ) ); ClassFile classFile = new ClassFile( new BufferedInputStream( Files.newInputStream( path ) ) );
prepare( classFile ); prepare( classFile );
} }
}; }
} }
} catch( Exception e ) { } catch( Exception e ) {
e.printStackTrace(); e.printStackTrace();
@ -370,7 +370,7 @@ public class ModuleGenerator {
List<WasmInstruction> instructions = codeBuilder.getInstructions(); List<WasmInstruction> instructions = codeBuilder.getInstructions();
optimizer.optimze( instructions ); optimizer.optimze( instructions );
int lastCodePosition = -1; int lastJavaSourceLine = -1;
for( WasmInstruction instruction : instructions ) { for( WasmInstruction instruction : instructions ) {
switch( instruction.getType() ) { switch( instruction.getType() ) {
case Block: case Block:
@ -390,10 +390,10 @@ public class ModuleGenerator {
break; break;
default: default:
} }
int codePosition = instruction.getCodePosition(); int javaSourceLine = instruction.getLineNumber();
if( codePosition >= 0 && codePosition != lastCodePosition ) { if( javaSourceLine >= 0 && javaSourceLine != lastJavaSourceLine ) {
writer.markCodePosition( codePosition ); writer.markSourceLine( javaSourceLine );
lastCodePosition = codePosition; lastJavaSourceLine = javaSourceLine;
} }
instruction.writeTo( writer ); 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. * Mark the current output position with Java code position for crating of a source map.
* *
* @param javaCodePosition * @param javaSourceLine
* the position in the Java code * the line number in the Java code
*/ */
protected abstract void markCodePosition( int javaCodePosition ); protected abstract void markSourceLine( int javaSourceLine );
/** /**
* Complete the method * Complete the method

View File

@ -19,11 +19,11 @@ package de.inetsoftware.jwebassembly.sourcemap;
* Mapping for Source Map. * Mapping for Source Map.
*/ */
public class SourceMapping { 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 * Create a mapping between a Java code line and a WebAssembly code position
@ -67,4 +67,12 @@ public class SourceMapping {
String getSourceFileName() { String getSourceFileName() {
return sourceFileName; 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} * {@inheritDoc}
*/ */
@Override @Override
protected void markCodePosition( int javaCodePosition ) { protected void markSourceLine( int javaSourceLine ) {
// nothing // nothing
} }