From 97b7b9dafd3c9ab25c66c4d77e1a1cb41bfc5796 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Tue, 26 Mar 2019 18:21:20 +0100 Subject: [PATCH] passing code position for a source map. #6 --- .../jwebassembly/binary/BinaryModuleWriter.java | 8 ++++++++ .../inetsoftware/jwebassembly/binary/Function.java | 14 +++++++++++++- .../jwebassembly/module/ModuleGenerator.java | 7 +++++++ .../jwebassembly/module/ModuleWriter.java | 8 ++++++++ .../jwebassembly/text/TextModuleWriter.java | 8 ++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index ab8f1042..94b19498 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -394,6 +394,14 @@ protected void writeMethodParamFinish() throws IOException { function.typeId = typeId; } + /** + * {@inheritDoc} + */ + @Override + protected void markCodePosition( int javaCodePosition ) { + function.markCodePosition( codeStream.size(), javaCodePosition ); + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/binary/Function.java b/src/de/inetsoftware/jwebassembly/binary/Function.java index 0caa30b4..3078699e 100644 --- a/src/de/inetsoftware/jwebassembly/binary/Function.java +++ b/src/de/inetsoftware/jwebassembly/binary/Function.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2018 Volker Berlin (i-net software) + * Copyright 2017 - 2019 Volker Berlin (i-net software) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,4 +40,16 @@ class Function extends SectionEntry { void writeSectionEntry( WasmOutputStream stream ) throws IOException { stream.writeVaruint32( this.typeId ); } + + /** + * Add code position marker for a source map. + * + * @param streamPosition + * the position in the function stream + * @param javaCodePosition + * the position in the Java Source file + */ + void markCodePosition( int streamPosition, int javaCodePosition ) { + // TODO Auto-generated method stub + } } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 25b381bf..ba544ee9 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -369,6 +369,8 @@ private void writeMethodImpl( FunctionName name, boolean isStatic, LocalVariable List instructions = codeBuilder.getInstructions(); optimizer.optimze( instructions ); + + int lastCodePosition = -1; for( WasmInstruction instruction : instructions ) { switch( instruction.getType() ) { case Block: @@ -388,6 +390,11 @@ private void writeMethodImpl( FunctionName name, boolean isStatic, LocalVariable break; default: } + int codePosition = instruction.getCodePosition(); + if( codePosition >= 0 && codePosition != lastCodePosition ) { + writer.markCodePosition( codePosition ); + lastCodePosition = codePosition; + } instruction.writeTo( writer ); } writer.writeMethodFinish(); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index c3e05fea..c229f42e 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -126,6 +126,14 @@ public void prepareFinish() { */ protected abstract void writeMethodParamFinish() throws IOException; + /** + * Mark the current output position with Java code position for crating of a source map. + * + * @param javaCodePosition + * the position in the Java code + */ + protected abstract void markCodePosition( int javaCodePosition ); + /** * Complete the method * diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 125f6e54..456e8ff5 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -198,6 +198,14 @@ protected void writeMethodParamFinish( ) throws IOException { } } + /** + * {@inheritDoc} + */ + @Override + protected void markCodePosition( int javaCodePosition ) { + // nothing + } + /** * {@inheritDoc} */