write import function parameters

This commit is contained in:
Volker Berlin 2018-05-31 21:35:01 +02:00
parent ce78f9fe7a
commit 4a5518c69a
3 changed files with 42 additions and 20 deletions

View File

@ -26,6 +26,7 @@ import java.util.Map;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import de.inetsoftware.classparser.MethodInfo;
import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.FunctionName; import de.inetsoftware.jwebassembly.module.FunctionName;
import de.inetsoftware.jwebassembly.module.ModuleWriter; import de.inetsoftware.jwebassembly.module.ModuleWriter;
@ -208,15 +209,19 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void prepareFunction( FunctionName name, String importModule, String importName ) { protected void prepareImport( FunctionName name, String importModule, String importName ) {
if( importName != null ) {
ImportFunction importFunction; ImportFunction importFunction;
function = importFunction = new ImportFunction(importModule, importName); function = importFunction = new ImportFunction(importModule, importName);
imports.put( name.signatureName, importFunction ); imports.put( name.signatureName, importFunction );
functionType = new FunctionType(); functionType = new FunctionType();
} else {
functions.put( name.signatureName, new Function() );
} }
/**
* {@inheritDoc}
*/
@Override
protected void prepareFunction( FunctionName name ) {
functions.put( name.signatureName, new Function() );
} }
/** /**
@ -272,15 +277,22 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeMethodFinish( List<ValueType> locals ) throws IOException { protected void writeMethodSignature( MethodInfo method ) throws IOException, WasmException {
super.writeMethodSignature( method );
int typeId = functionTypes.indexOf( functionType ); int typeId = functionTypes.indexOf( functionType );
if( typeId < 0 ) { if( typeId < 0 ) {
typeId = functionTypes.size(); typeId = functionTypes.size();
functionTypes.add( functionType ); functionTypes.add( functionType );
} }
function.typeId = typeId; function.typeId = typeId;
}
/**
* {@inheritDoc}
*/
@Override
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
WasmOutputStream localsStream = new WasmOutputStream(); WasmOutputStream localsStream = new WasmOutputStream();
localsStream.writeVaruint32( locals.size() ); localsStream.writeVaruint32( locals.size() );
for( ValueType valueType : locals ) { for( ValueType valueType : locals ) {

View File

@ -116,21 +116,23 @@ public abstract class ModuleWriter implements Closeable {
*/ */
private void prepareMethod( MethodInfo method ) throws WasmException { private void prepareMethod( MethodInfo method ) throws WasmException {
try { try {
String module = null; FunctionName name = new FunctionName( method );
String name = null;
Map<String,Object> annotationValues = method.getAnnotation( "org.webassembly.annotation.Import" ); Map<String,Object> annotationValues = method.getAnnotation( "org.webassembly.annotation.Import" );
if( annotationValues != null ) { if( annotationValues != null ) {
module = (String)annotationValues.get( "module" ); String impoarModule = (String)annotationValues.get( "module" );
name = (String)annotationValues.get( "name" ); String importName = (String)annotationValues.get( "name" );
prepareImport( name, impoarModule, importName );
writeMethodSignature( method );
} else {
prepareFunction( name );
} }
prepareFunction( new FunctionName( method ), module, name );
} catch( IOException ioex ) { } catch( IOException ioex ) {
throw WasmException.create( ioex, sourceFile, -1 ); throw WasmException.create( ioex, sourceFile, -1 );
} }
} }
/** /**
* Prepare a single function in the prepare phase. * Prepare a imported single function in the prepare phase.
* *
* @param name * @param name
* the function name * the function name
@ -141,7 +143,15 @@ public abstract class ModuleWriter implements Closeable {
* @throws IOException * @throws IOException
* if any I/O error occur * if any I/O error occur
*/ */
protected abstract void prepareFunction( FunctionName name, String importModule, String importName ) throws IOException; 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 the content of a method. * Write the content of a method.
@ -260,7 +270,7 @@ public abstract class ModuleWriter implements Closeable {
* @throws WasmException * @throws WasmException
* if some Java code can't converted * if some Java code can't converted
*/ */
private void writeMethodSignature( MethodInfo method ) throws IOException, WasmException { protected void writeMethodSignature( MethodInfo method ) throws IOException, WasmException {
String signature = method.getDescription(); String signature = method.getDescription();
String kind = "param"; String kind = "param";
int paramCount = 0; int paramCount = 0;

View File

@ -70,7 +70,7 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void prepareFunction( FunctionName name, String importModule, String importName ) throws IOException { protected void prepareImport( FunctionName name, String importModule, String importName ) throws IOException {
if( importName != null ) { if( importName != null ) {
newline( output ); newline( output );
output.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( name.fullName ).append( "))" ); output.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( name.fullName ).append( "))" );