From d3f7069cc8a363f65595ed11df8bdc91e0516aa9 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Fri, 27 Mar 2020 21:10:02 +0100 Subject: [PATCH] pass the signature from synthetic functions to the wat parser. --- .../module/JavaMethodWasmCodeBuilder.java | 2 +- .../module/LocaleVariableManager.java | 17 ++++++++++------- .../jwebassembly/module/ModuleGenerator.java | 2 +- .../jwebassembly/module/WasmCodeBuilder.java | 9 ++++++--- .../module/WatCodeSyntheticFunctionName.java | 2 +- .../jwebassembly/watparser/WatParser.java | 11 +++++++---- .../jwebassembly/module/WatParserTest.java | 2 +- 7 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index 3b2c577..00c7e98 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -60,7 +60,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { void buildCode( @Nonnull Code code, MethodInfo method ) { CodeInputStream byteCode = null; try { - reset( code.getLocalVariableTable(), method ); + reset( code.getLocalVariableTable(), method, null ); branchManager.reset( code ); byteCode = code.getByteCode(); diff --git a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java index a8d0a46..e84b542 100644 --- a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java +++ b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java @@ -1,5 +1,5 @@ /* - Copyright 2018 - 2019 Volker Berlin (i-net software) + Copyright 2018 - 2020 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. @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import javax.annotation.Nullable; @@ -74,14 +75,16 @@ class LocaleVariableManager { } /** - * Reset the manager to an initial state + * Reset the manager to an initial state. * * @param variableTable * variable table of the Java method. * @param method - * the method with signature as fallback for a missing variable table + * the method with signature as fallback for a missing variable table. If null signature is used and the method must be static. + * @param signature + * alternative for method signature, can be null if method is set */ - void reset( LocalVariableTable variableTable, MethodInfo method ) { + void reset( LocalVariableTable variableTable, MethodInfo method, Iterator signature ) { size = 0; int maxLocals; @@ -157,9 +160,9 @@ class LocaleVariableManager { } // add missing slots from signature - if( (maxLocals > 0 || variableTable == null) && size == 0 && method != null ) { - ValueTypeParser parser = new ValueTypeParser( method.getType(), types ); - if( !method.isStatic() ) { + if( (maxLocals > 0 || variableTable == null) && size == 0 && (method != null || signature != null )) { + Iterator parser = signature == null ? new ValueTypeParser( method.getType(), types ) : signature; + if( method != null && !method.isStatic() ) { resetAddVar( ValueType.anyref, size ); } while( true ) { diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index acf475f..6bb8631 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -480,7 +480,7 @@ public class ModuleGenerator { if( signature == null ) { signature = method.getType(); } - watParser.parse( watCode, method, code == null ? -1 : code.getFirstLineNr() ); + watParser.parse( watCode, method, null, code == null ? -1 : code.getFirstLineNr() ); return watParser; } else if( code != null ) { // abstract methods and interface methods does not have code javaCodeBuilder.buildCode( code, method ); diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 9960011..d828c9f 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -17,6 +17,7 @@ package de.inetsoftware.jwebassembly.module; import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; @@ -247,11 +248,13 @@ public abstract class WasmCodeBuilder { * @param variableTable * variable table of the Java method. * @param method - * the method with signature as fallback for a missing variable table + * the method with signature as fallback for a missing variable table. If null signature is used and the method must be static. + * @param signature + * alternative for method signature, can be null if method is set */ - protected void reset( LocalVariableTable variableTable, MethodInfo method ) { + protected void reset( LocalVariableTable variableTable, MethodInfo method, Iterator signature ) { instructions.clear(); - localVariables.reset( variableTable, method ); + localVariables.reset( variableTable, method, signature ); } /** diff --git a/src/de/inetsoftware/jwebassembly/module/WatCodeSyntheticFunctionName.java b/src/de/inetsoftware/jwebassembly/module/WatCodeSyntheticFunctionName.java index 91bf793..258b888 100644 --- a/src/de/inetsoftware/jwebassembly/module/WatCodeSyntheticFunctionName.java +++ b/src/de/inetsoftware/jwebassembly/module/WatCodeSyntheticFunctionName.java @@ -87,7 +87,7 @@ class WatCodeSyntheticFunctionName extends ArraySyntheticFunctionName { */ @Override protected WasmCodeBuilder getCodeBuilder( WatParser watParser ) { - watParser.parse( getCode(), null, -1 ); + watParser.parse( getCode(), null, getSignature( null ), -1 ); return watParser; } } diff --git a/src/de/inetsoftware/jwebassembly/watparser/WatParser.java b/src/de/inetsoftware/jwebassembly/watparser/WatParser.java index 6fef01e..8bf652a 100644 --- a/src/de/inetsoftware/jwebassembly/watparser/WatParser.java +++ b/src/de/inetsoftware/jwebassembly/watparser/WatParser.java @@ -17,6 +17,7 @@ package de.inetsoftware.jwebassembly.watparser; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import javax.annotation.Nonnegative; @@ -50,14 +51,16 @@ public class WatParser extends WasmCodeBuilder { * * @param wat * the text format content of a function - * @param lineNumber - * the line number for an error message * @param method * the method with signature as fallback for a missing variable table + * @param signature + * alternative for method signature, can be null if method is set + * @param lineNumber + * the line number for an error message */ - public void parse( String wat, MethodInfo method, int lineNumber ) { + public void parse( String wat, MethodInfo method, Iterator signature, int lineNumber ) { try { - reset( null, method ); + reset( null, method, signature ); List tokens = splitTokens( wat ); for( int i = 0; i < tokens.size(); i++ ) { diff --git a/test/de/inetsoftware/jwebassembly/module/WatParserTest.java b/test/de/inetsoftware/jwebassembly/module/WatParserTest.java index c8c55cc..7bf324c 100644 --- a/test/de/inetsoftware/jwebassembly/module/WatParserTest.java +++ b/test/de/inetsoftware/jwebassembly/module/WatParserTest.java @@ -40,7 +40,7 @@ public class WatParserTest { WatParser parser = new WatParser(); WasmCodeBuilder codeBuilder = parser; codeBuilder.init( options, null ); - parser.parse( wat, null, 100 ); + parser.parse( wat, null, null, 100 ); StringBuilder builder = new StringBuilder(); ModuleWriter writer = new TextModuleWriter( new WasmTarget( builder ), options ); writer.writeMethodStart( new FunctionName( "A.a()V" ), null );