From d3bd4902fdcc588414aa2b659543179cdfeb0e84 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 9 Jun 2019 22:39:53 +0200 Subject: [PATCH] Write the functions also in text format in the registered order. --- .../jwebassembly/text/Function.java | 30 ++++++++++++++++ .../jwebassembly/text/TextModuleWriter.java | 35 +++++++++++++++---- 2 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 src/de/inetsoftware/jwebassembly/text/Function.java diff --git a/src/de/inetsoftware/jwebassembly/text/Function.java b/src/de/inetsoftware/jwebassembly/text/Function.java new file mode 100644 index 0000000..33f4fc2 --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/text/Function.java @@ -0,0 +1,30 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.inetsoftware.jwebassembly.text; + +/** + * A function in the wasm. + * + * @author Volker Berlin + */ +class Function { + + int id; + + int typeId; + + final StringBuilder output = new StringBuilder(); +} diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 574065a..34b4e31 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -63,9 +63,11 @@ public class TextModuleWriter extends ModuleWriter { private ArrayList types = new ArrayList<>(); - private StringBuilder methodOutput = new StringBuilder(); + private StringBuilder methodOutput; - private Map functions = new LinkedHashMap<>(); + private StringBuilder imports = new StringBuilder(); + + private Map functions = new LinkedHashMap<>(); private int inset; @@ -109,7 +111,11 @@ public class TextModuleWriter extends ModuleWriter { output.append( "(type $t" ).append( Integer.toString( i ) ).append( " (func" ).append( types.get( i ) ).append( "))" ); } - output.append( methodOutput ); + output.append( imports ); + + for( Function func : functions.values() ) { + output.append( func.output ); + } if( callIndirect ) { int count = functions.size(); @@ -151,7 +157,7 @@ public class TextModuleWriter extends ModuleWriter { protected int writeStructType( StructType type ) throws IOException { type.setVTable( dataStream.size() ); for( FunctionName funcName : type.getMethods() ) { - int functIdx = functions.get( funcName.signatureName ); + int functIdx = functions.get( funcName.signatureName ).id; // little-endian byte order dataStream.write( functIdx >>> 0 ); dataStream.write( functIdx >>> 8 ); @@ -211,6 +217,7 @@ public class TextModuleWriter extends ModuleWriter { @Override protected void prepareImport( FunctionName name, String importModule, String importName ) throws IOException { if( importName != null ) { + methodOutput = imports; newline( methodOutput ); methodOutput.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( normalizeName( name ) ); isImport = true; @@ -318,19 +325,32 @@ public class TextModuleWriter extends ModuleWriter { idx = types.size(); types.add( typeStr ); } - functions.put( name.signatureName, idx ); + getFunction( name ).typeId = idx; if( isImport ) { isImport = false; methodOutput.append( "))" ); + methodOutput = null; } } + private Function getFunction( FunctionName name ) { + Function func = functions.get( name.signatureName ); + if( func == null ) { + func = new Function(); + func.id = functions.size(); + functions.put( name.signatureName, func ); + } + return func; + } + /** * {@inheritDoc} */ @Override protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException { + methodOutput = getFunction( name ).output; + newline( methodOutput ); methodOutput.append( "(func $" ); methodOutput.append( normalizeName( name ) ); @@ -353,6 +373,7 @@ public class TextModuleWriter extends ModuleWriter { inset--; newline( methodOutput ); methodOutput.append( ')' ); + methodOutput = null; } /** @@ -593,9 +614,9 @@ public class TextModuleWriter extends ModuleWriter { methodOutput.append( "i32.load offset=" ).append( virtualFunctionIdx * 4 ); // use default alignment newline( methodOutput ); if(spiderMonkey) - methodOutput.append( "call_indirect $t" ).append( functions.get( name.signatureName ) ); // https://bugzilla.mozilla.org/show_bug.cgi?id=1556779 + methodOutput.append( "call_indirect $t" ).append( functions.get( name.signatureName ).typeId ); // https://bugzilla.mozilla.org/show_bug.cgi?id=1556779 else - methodOutput.append( "call_indirect (type $t" ).append( functions.get( name.signatureName ) ).append( ')' ); + methodOutput.append( "call_indirect (type $t" ).append( functions.get( name.signatureName ).typeId ).append( ')' ); } /**