From ce93ce517e103d2e35a1ac894ceb1a308f0b7cdb Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 31 Mar 2019 13:29:40 +0200 Subject: [PATCH] Write a source map if DebugNames is enabled. #6 --- .../jwebassembly/binary/BinaryModuleWriter.java | 16 ++++++++++++---- .../jwebassembly/module/ModuleGenerator.java | 12 ++++++------ .../jwebassembly/module/ModuleWriter.java | 6 +++--- .../jwebassembly/sourcemap/SourceMapping.java | 14 +++++++++++--- .../jwebassembly/text/TextModuleWriter.java | 2 +- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 6cd140e0..a0794053 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -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; @@ -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() ); } } @@ -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 ); } } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index c0c14bf5..9243ef7c 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -109,7 +109,7 @@ private void scanLibraries( @Nonnull List libraries ) { ClassFile classFile = new ClassFile( new BufferedInputStream( Files.newInputStream( path ) ) ); prepare( classFile ); } - }; + } } } catch( Exception e ) { e.printStackTrace(); @@ -370,7 +370,7 @@ private void writeMethodImpl( FunctionName name, boolean isStatic, LocalVariable List 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 @@ 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 ); } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index c70052ee..683cf5cc 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -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 diff --git a/src/de/inetsoftware/jwebassembly/sourcemap/SourceMapping.java b/src/de/inetsoftware/jwebassembly/sourcemap/SourceMapping.java index dd8548ef..1fcd0bb1 100644 --- a/src/de/inetsoftware/jwebassembly/sourcemap/SourceMapping.java +++ b/src/de/inetsoftware/jwebassembly/sourcemap/SourceMapping.java @@ -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 @@ -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; + } } diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index aba9af78..b2826d03 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -202,7 +202,7 @@ protected void writeMethodParamFinish( ) throws IOException { * {@inheritDoc} */ @Override - protected void markCodePosition( int javaCodePosition ) { + protected void markSourceLine( int javaSourceLine ) { // nothing }