diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 7296add..990686e 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -58,8 +58,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod private WasmOutputStream codeStream = new WasmOutputStream(); - private WasmOutputStream functionsStream = new WasmOutputStream(); - private List functionTypes = new ArrayList<>(); private Map functions = new LinkedHashMap<>(); @@ -175,7 +173,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod } WasmOutputStream stream = new WasmOutputStream(); stream.writeVaruint32( size ); - functionsStream.writeTo( stream ); + for( Function func : functions.values() ) { + func.functionsStream.writeTo( stream ); + } wasm.writeSection( SectionType.Code, stream ); } @@ -237,14 +237,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod functionType = new FunctionType(); } - /** - * {@inheritDoc} - */ - @Override - protected void prepareFunction( FunctionName name ) { - functions.put( name.signatureName, new Function() ); - } - /** * {@inheritDoc} */ @@ -274,7 +266,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod */ @Override protected void writeMethodStart( FunctionName name ) throws IOException { - function = functions.get( name.signatureName ); + function = getFunction( name ); functionType = new FunctionType(); codeStream.reset(); locals.clear(); @@ -328,6 +320,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod localsStream.writeVaruint32( 1 ); // TODO optimize, write the count of same types. localsStream.writeValueType( valueType ); } + WasmOutputStream functionsStream = function.functionsStream = new WasmOutputStream(); functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 ); localsStream.writeTo( functionsStream ); codeStream.writeTo( functionsStream ); @@ -702,23 +695,31 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod */ @Override protected void writeFunctionCall( FunctionName name ) throws IOException { + Function func = getFunction( name ); + codeStream.writeOpCode( CALL ); + codeStream.writeVaruint32( func.id ); + } + + /** + * Get the function object for the name. If not exists then it will be created. + * + * @param name + * the function name + * @return the function object + */ + @Nonnull + private Function getFunction( FunctionName name ) { String signatureName = name.signatureName; - int id; Function func = functions.get( signatureName ); - if( func != null ) { - id = func.id; - } else { - ImportFunction entry = imports.get( signatureName ); - if( entry != null ) { - id = entry.id; - } else { + if( func == null ) { + func = imports.get( signatureName ); + if( func == null ) { func = new Function(); - id = func.id = functions.size() + imports.size(); + func.id = functions.size() + imports.size(); functions.put( signatureName, func ); } } - codeStream.writeOpCode( CALL ); - codeStream.writeVaruint32( id ); + return func; } /** diff --git a/src/de/inetsoftware/jwebassembly/binary/Function.java b/src/de/inetsoftware/jwebassembly/binary/Function.java index db40c5c..0caa30b 100644 --- a/src/de/inetsoftware/jwebassembly/binary/Function.java +++ b/src/de/inetsoftware/jwebassembly/binary/Function.java @@ -31,6 +31,8 @@ class Function extends SectionEntry { List paramNames; + WasmOutputStream functionsStream; + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index b7ce0d0..973e839 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -192,8 +192,6 @@ public class ModuleGenerator { String importName = (String)annotationValues.get( "name" ); writer.prepareImport( name, impoarModule, importName ); writeMethodSignature( name, null, null ); - } else { - writer.prepareFunction( name ); } } catch( Exception ioex ) { throw WasmException.create( ioex, sourceFile, className, -1 ); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index 9a823f6..bc6e4dc 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -58,14 +58,6 @@ public abstract class ModuleWriter implements Closeable { */ 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 an export directive * @param name