extract writeMethodParamStart() from writeMethodStart() and prepareImport()

This commit is contained in:
Volker Berlin 2019-06-02 11:44:28 +02:00
parent b8b751f909
commit 557c348ed7
4 changed files with 59 additions and 38 deletions

View File

@ -472,7 +472,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
ImportFunction importFunction;
function = importFunction = new ImportFunction(importModule, importName);
imports.put( name.signatureName, importFunction );
functionType = new FunctionTypeEntry();
}
/**
@ -503,14 +502,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc}
*/
@Override
protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException {
protected void writeMethodParamStart( FunctionName name ) throws IOException {
function = getFunction( name );
if( createSourceMap ) {
int idx = name.className.lastIndexOf( '/' );
this.javaSourceFile = name.className.substring( 0, idx + 1 ) + sourceFile;
}
functionType = new FunctionTypeEntry();
codeStream.reset();
locals.clear();
}
@ -542,7 +536,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc}
*/
@Override
protected void writeMethodParamFinish() throws IOException {
protected void writeMethodParamFinish(FunctionName name) throws IOException {
int typeId = functionTypes.indexOf( functionType );
if( typeId < 0 ) {
typeId = functionTypes.size();
@ -551,6 +545,18 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
function.typeId = typeId;
}
/**
* {@inheritDoc}
*/
@Override
protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException {
if( createSourceMap ) {
int idx = name.className.lastIndexOf( '/' );
this.javaSourceFile = name.className.substring( 0, idx + 1 ) + sourceFile;
}
codeStream.reset();
}
/**
* {@inheritDoc}
*/

View File

@ -496,6 +496,7 @@ public class ModuleGenerator {
* if some Java code can't converted
*/
private void writeMethodSignature( FunctionName name, boolean isStatic, WasmCodeBuilder codeBuilder ) throws IOException, WasmException {
writer.writeMethodParamStart( name );
int paramCount = 0;
if( !isStatic ) {
StructType instanceType = types.valueOf( name.className );
@ -529,7 +530,7 @@ public class ModuleGenerator {
writer.writeMethodParam( "local", type, paramName );
}
}
writer.writeMethodParamFinish( );
writer.writeMethodParamFinish( name );
}
}

View File

@ -95,15 +95,14 @@ public abstract class ModuleWriter implements Closeable {
/**
* Write the method header.
*
* @param name
* the function name
* @param sourceFile
* the name of the source file
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeMethodStart( FunctionName name, String sourceFile ) throws IOException;
protected abstract void writeMethodParamStart( @Nonnull FunctionName name ) throws IOException;
/**
* Write a method parameter.
@ -122,10 +121,26 @@ public abstract class ModuleWriter implements Closeable {
/**
* Finish the function parameter.
*
* @param name
* the function name
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeMethodParamFinish() throws IOException;
protected abstract void writeMethodParamFinish( @Nonnull FunctionName name ) throws IOException;
/**
* Write the method header.
*
* @param name
* the function name
* @param sourceFile
* the name of the source file
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeMethodStart( FunctionName name, String sourceFile ) throws IOException;
/**
* Mark the current output position with Java code position for crating of a source map.

View File

@ -65,8 +65,6 @@ public class TextModuleWriter extends ModuleWriter {
private StringBuilder methodOutput = new StringBuilder();
private FunctionName function;
private Map<String, Integer> functions = new LinkedHashMap<>();
private int inset;
@ -77,8 +75,6 @@ public class TextModuleWriter extends ModuleWriter {
private boolean useExceptions;
private int importCount;
private int functionCount;
private boolean callIndirect;
@ -116,7 +112,7 @@ public class TextModuleWriter extends ModuleWriter {
output.append( methodOutput );
if( callIndirect ) {
int count = importCount + functionCount;
int count = functionCount;
String countStr = Integer.toString( count );
newline( output );
output.append( "(table " ).append( countStr ).append( ' ' ).append( countStr ).append( " anyfunc)" );
@ -200,8 +196,6 @@ public class TextModuleWriter extends ModuleWriter {
newline( methodOutput );
methodOutput.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( normalizeName( name ) );
isImport = true;
importCount++;
function = name;
}
}
@ -241,21 +235,6 @@ public class TextModuleWriter extends ModuleWriter {
output.append( "(export \"" ).append( exportName ).append( "\" (func $" ).append( normalizeName( name ) ).append( "))" );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException {
function = name;
typeOutput.setLength( 0 );
newline( methodOutput );
methodOutput.append( "(func $" );
methodOutput.append( normalizeName( name ) );
inset++;
methodParamNames.clear();
functionCount++;
}
/**
* Write the name of a type.
*
@ -274,6 +253,16 @@ public class TextModuleWriter extends ModuleWriter {
}
}
/**
* {@inheritDoc}
*/
@Override
protected void writeMethodParamStart( @Nonnull FunctionName name ) throws IOException {
typeOutput.setLength( 0 );
methodParamNames.clear();
functionCount++;
}
/**
* {@inheritDoc}
*/
@ -302,14 +291,14 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc}
*/
@Override
protected void writeMethodParamFinish( ) throws IOException {
protected void writeMethodParamFinish( @Nonnull FunctionName name ) throws IOException {
String typeStr = typeOutput.toString();
int idx = types.indexOf( typeStr );
if( idx < 0 ) {
idx = types.size();
types.add( typeStr );
}
functions.put( function.signatureName, idx );
functions.put( name.signatureName, idx );
if( isImport ) {
isImport = false;
@ -317,6 +306,17 @@ public class TextModuleWriter extends ModuleWriter {
}
}
/**
* {@inheritDoc}
*/
@Override
protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException {
newline( methodOutput );
methodOutput.append( "(func $" );
methodOutput.append( normalizeName( name ) );
inset++;
}
/**
* {@inheritDoc}
*/
@ -333,7 +333,6 @@ public class TextModuleWriter extends ModuleWriter {
inset--;
newline( methodOutput );
methodOutput.append( ')' );
function = null;
}
/**