diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index d4a81fd..7d8ea52 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -472,7 +472,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod ImportFunction importFunction; function = importFunction = new ImportFunction(importModule, importName); imports.put( name.signatureName, importFunction ); - functionType = new FunctionTypeEntry(); } /** @@ -503,14 +502,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException { + protected void writeMethodParamStart( FunctionName name ) throws IOException { function = getFunction( name ); - if( createSourceMap ) { - int idx = name.className.lastIndexOf( '/' ); - this.javaSourceFile = name.className.substring( 0, idx + 1 ) + sourceFile; - } functionType = new FunctionTypeEntry(); - codeStream.reset(); locals.clear(); } @@ -542,7 +536,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeMethodParamFinish() throws IOException { + protected void writeMethodParamFinish(FunctionName name) throws IOException { int typeId = functionTypes.indexOf( functionType ); if( typeId < 0 ) { typeId = functionTypes.size(); @@ -551,6 +545,18 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod function.typeId = typeId; } + /** + * {@inheritDoc} + */ + @Override + protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException { + if( createSourceMap ) { + int idx = name.className.lastIndexOf( '/' ); + this.javaSourceFile = name.className.substring( 0, idx + 1 ) + sourceFile; + } + codeStream.reset(); + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index c31ff91..de45092 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -496,6 +496,7 @@ public class ModuleGenerator { * if some Java code can't converted */ private void writeMethodSignature( FunctionName name, boolean isStatic, WasmCodeBuilder codeBuilder ) throws IOException, WasmException { + writer.writeMethodParamStart( name ); int paramCount = 0; if( !isStatic ) { StructType instanceType = types.valueOf( name.className ); @@ -529,7 +530,7 @@ public class ModuleGenerator { writer.writeMethodParam( "local", type, paramName ); } } - writer.writeMethodParamFinish( ); + writer.writeMethodParamFinish( name ); } } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index 4508a64..9fc29cc 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -95,15 +95,14 @@ public abstract class ModuleWriter implements Closeable { /** * Write the method header. + * * @param name * the function name - * @param sourceFile - * the name of the source file * * @throws IOException * if any I/O error occur */ - protected abstract void writeMethodStart( FunctionName name, String sourceFile ) throws IOException; + protected abstract void writeMethodParamStart( @Nonnull FunctionName name ) throws IOException; /** * Write a method parameter. @@ -122,10 +121,26 @@ public abstract class ModuleWriter implements Closeable { /** * Finish the function parameter. * + * @param name + * the function name + * * @throws IOException * if any I/O error occur */ - protected abstract void writeMethodParamFinish() throws IOException; + protected abstract void writeMethodParamFinish( @Nonnull FunctionName name ) throws IOException; + + /** + * Write the method header. + * + * @param name + * the function name + * @param sourceFile + * the name of the source file + * + * @throws IOException + * if any I/O error occur + */ + protected abstract void writeMethodStart( FunctionName name, String sourceFile ) throws IOException; /** * Mark the current output position with Java code position for crating of a source map. diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index cf49cfb..fc9c0bf 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -65,8 +65,6 @@ public class TextModuleWriter extends ModuleWriter { private StringBuilder methodOutput = new StringBuilder(); - private FunctionName function; - private Map functions = new LinkedHashMap<>(); private int inset; @@ -77,8 +75,6 @@ public class TextModuleWriter extends ModuleWriter { private boolean useExceptions; - private int importCount; - private int functionCount; private boolean callIndirect; @@ -116,7 +112,7 @@ public class TextModuleWriter extends ModuleWriter { output.append( methodOutput ); if( callIndirect ) { - int count = importCount + functionCount; + int count = functionCount; String countStr = Integer.toString( count ); newline( output ); output.append( "(table " ).append( countStr ).append( ' ' ).append( countStr ).append( " anyfunc)" ); @@ -200,8 +196,6 @@ public class TextModuleWriter extends ModuleWriter { newline( methodOutput ); methodOutput.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( normalizeName( name ) ); isImport = true; - importCount++; - function = name; } } @@ -241,21 +235,6 @@ public class TextModuleWriter extends ModuleWriter { output.append( "(export \"" ).append( exportName ).append( "\" (func $" ).append( normalizeName( name ) ).append( "))" ); } - /** - * {@inheritDoc} - */ - @Override - protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException { - function = name; - typeOutput.setLength( 0 ); - newline( methodOutput ); - methodOutput.append( "(func $" ); - methodOutput.append( normalizeName( name ) ); - inset++; - methodParamNames.clear(); - functionCount++; - } - /** * Write the name of a type. * @@ -274,6 +253,16 @@ public class TextModuleWriter extends ModuleWriter { } } + /** + * {@inheritDoc} + */ + @Override + protected void writeMethodParamStart( @Nonnull FunctionName name ) throws IOException { + typeOutput.setLength( 0 ); + methodParamNames.clear(); + functionCount++; + } + /** * {@inheritDoc} */ @@ -302,14 +291,14 @@ public class TextModuleWriter extends ModuleWriter { * {@inheritDoc} */ @Override - protected void writeMethodParamFinish( ) throws IOException { + protected void writeMethodParamFinish( @Nonnull FunctionName name ) throws IOException { String typeStr = typeOutput.toString(); int idx = types.indexOf( typeStr ); if( idx < 0 ) { idx = types.size(); types.add( typeStr ); } - functions.put( function.signatureName, idx ); + functions.put( name.signatureName, idx ); if( isImport ) { isImport = false; @@ -317,6 +306,17 @@ public class TextModuleWriter extends ModuleWriter { } } + /** + * {@inheritDoc} + */ + @Override + protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException { + newline( methodOutput ); + methodOutput.append( "(func $" ); + methodOutput.append( normalizeName( name ) ); + inset++; + } + /** * {@inheritDoc} */ @@ -333,7 +333,6 @@ public class TextModuleWriter extends ModuleWriter { inset--; newline( methodOutput ); methodOutput.append( ')' ); - function = null; } /**