Convert ValueTypeParser to an Iterator

This commit is contained in:
Volker Berlin 2019-01-18 17:48:12 +01:00
parent 1376af2f3f
commit b677355c0f
3 changed files with 21 additions and 9 deletions

View File

@ -20,6 +20,7 @@ import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -354,10 +355,10 @@ public class ModuleGenerator {
if( !isStatic ) { if( !isStatic ) {
writer.writeMethodParam( "param", ValueType.anyref, "this" ); writer.writeMethodParam( "param", ValueType.anyref, "this" );
} }
ValueTypeParser parser = new ValueTypeParser( signature ); Iterator<AnyType> parser = new ValueTypeParser( signature );
AnyType type; AnyType type;
for( String kind : new String[] {"param","result"}) { for( String kind : new String[] {"param","result"}) {
while( (type = parser.next()) != null ) { while( parser.hasNext() && (type = parser.next()) != null ) {
String paramName = null; String paramName = null;
if( kind == "param" ) { if( kind == "param" ) {
if( variables != null ) { if( variables != null ) {

View File

@ -17,10 +17,12 @@
package de.inetsoftware.jwebassembly.module; package de.inetsoftware.jwebassembly.module;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import de.inetsoftware.classparser.Member; import de.inetsoftware.classparser.Member;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
import de.inetsoftware.jwebassembly.wasm.ValueTypeParser; import de.inetsoftware.jwebassembly.wasm.ValueTypeParser;
@ -103,15 +105,14 @@ class WasmCallInstruction extends WasmInstruction {
if( paramCount >= 0 ) { if( paramCount >= 0 ) {
return; return;
} }
ValueTypeParser parser = new ValueTypeParser( method.getType() ); Iterator<AnyType> parser = new ValueTypeParser( method.getType() );
paramCount = 0; paramCount = 0;
while( parser.next() != null ) { while( parser.next() != null ) {
paramCount++; paramCount++;
} }
valueType = (ValueType)parser.next(); valueType = (ValueType)parser.next();
ValueType type; while( parser.hasNext() ) {
while( (type = (ValueType)parser.next()) != null ) { valueType = (ValueType)parser.next();
valueType = type;
paramCount--; paramCount--;
} }
} }

View File

@ -15,6 +15,9 @@
*/ */
package de.inetsoftware.jwebassembly.wasm; package de.inetsoftware.jwebassembly.wasm;
import java.util.Iterator;
import java.util.NoSuchElementException;
import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.TypeManager; import de.inetsoftware.jwebassembly.module.TypeManager;
@ -23,7 +26,7 @@ import de.inetsoftware.jwebassembly.module.TypeManager;
* *
* @author Volker Berlin * @author Volker Berlin
*/ */
public class ValueTypeParser { public class ValueTypeParser implements Iterator<AnyType> {
private final String sig; private final String sig;
private int idx; private int idx;
@ -56,14 +59,21 @@ public class ValueTypeParser {
} }
} }
/**
* {@inheritDoc}
*/
public boolean hasNext() {
return idx < sig.length();
}
/** /**
* Get the next value in the signature or null if the parameter are end or the signature is end. * Get the next value in the signature or null if the parameter are end or the signature is end.
* *
* @return next type or null * @return next type or null
*/ */
public AnyType next() { public AnyType next() {
if( idx >= sig.length() ) { if( !hasNext() ) {
return null; throw new NoSuchElementException();
} }
switch( sig.charAt( idx++ ) ) { switch( sig.charAt( idx++ ) ) {
case ')': case ')':