From 4a5518c69a41cd307bf33015d19baccbc47364aa Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Thu, 31 May 2018 21:35:01 +0200 Subject: [PATCH] write import function parameters --- .../binary/BinaryModuleWriter.java | 34 +++++++++++++------ .../jwebassembly/module/ModuleWriter.java | 26 +++++++++----- .../jwebassembly/text/TextModuleWriter.java | 2 +- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index ad1cb28..8de1a93 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -26,6 +26,7 @@ import java.util.Map; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import de.inetsoftware.classparser.MethodInfo; import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.module.FunctionName; import de.inetsoftware.jwebassembly.module.ModuleWriter; @@ -208,15 +209,19 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void prepareFunction( FunctionName name, String importModule, String importName ) { - if( importName != null ) { - ImportFunction importFunction; - function = importFunction = new ImportFunction(importModule, importName); - imports.put( name.signatureName, importFunction ); - functionType = new FunctionType(); - } else { - functions.put( name.signatureName, new Function() ); - } + protected void prepareImport( FunctionName name, String importModule, String importName ) { + ImportFunction importFunction; + function = importFunction = new ImportFunction(importModule, importName); + imports.put( name.signatureName, importFunction ); + functionType = new FunctionType(); + } + + /** + * {@inheritDoc} + */ + @Override + protected void prepareFunction( FunctionName name ) { + functions.put( name.signatureName, new Function() ); } /** @@ -272,15 +277,22 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeMethodFinish( List locals ) throws IOException { + protected void writeMethodSignature( MethodInfo method ) throws IOException, WasmException { + super.writeMethodSignature( method ); + int typeId = functionTypes.indexOf( functionType ); if( typeId < 0 ) { typeId = functionTypes.size(); functionTypes.add( functionType ); } function.typeId = typeId; + } - + /** + * {@inheritDoc} + */ + @Override + protected void writeMethodFinish( List locals ) throws IOException { WasmOutputStream localsStream = new WasmOutputStream(); localsStream.writeVaruint32( locals.size() ); for( ValueType valueType : locals ) { diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index 5f00ab4..c17fb1d 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -116,21 +116,23 @@ public abstract class ModuleWriter implements Closeable { */ private void prepareMethod( MethodInfo method ) throws WasmException { try { - String module = null; - String name = null; + FunctionName name = new FunctionName( method ); Map annotationValues = method.getAnnotation( "org.webassembly.annotation.Import" ); if( annotationValues != null ) { - module = (String)annotationValues.get( "module" ); - name = (String)annotationValues.get( "name" ); + String impoarModule = (String)annotationValues.get( "module" ); + String importName = (String)annotationValues.get( "name" ); + prepareImport( name, impoarModule, importName ); + writeMethodSignature( method ); + } else { + prepareFunction( name ); } - prepareFunction( new FunctionName( method ), module, name ); } catch( IOException ioex ) { throw WasmException.create( ioex, sourceFile, -1 ); } } /** - * Prepare a single function in the prepare phase. + * Prepare a imported single function in the prepare phase. * * @param name * the function name @@ -141,7 +143,15 @@ public abstract class ModuleWriter implements Closeable { * @throws IOException * if any I/O error occur */ - protected abstract void prepareFunction( FunctionName name, String importModule, String importName ) throws IOException; + protected abstract void prepareImport( FunctionName name, String importModule, String importName ) throws IOException; + + /** + * Prepare a single function in the prepare phase. + * + * @param name + * the function name + */ + protected void prepareFunction( FunctionName name ) {} /** * Write the content of a method. @@ -260,7 +270,7 @@ public abstract class ModuleWriter implements Closeable { * @throws WasmException * if some Java code can't converted */ - private void writeMethodSignature( MethodInfo method ) throws IOException, WasmException { + protected void writeMethodSignature( MethodInfo method ) throws IOException, WasmException { String signature = method.getDescription(); String kind = "param"; int paramCount = 0; diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 7618ecc..c918ca9 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -70,7 +70,7 @@ public class TextModuleWriter extends ModuleWriter { * {@inheritDoc} */ @Override - protected void prepareFunction( FunctionName name, String importModule, String importName ) throws IOException { + protected void prepareImport( FunctionName name, String importModule, String importName ) throws IOException { if( importName != null ) { newline( output ); output.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( name.fullName ).append( "))" );