mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
write import function parameters
This commit is contained in:
parent
ce78f9fe7a
commit
4a5518c69a
@ -26,6 +26,7 @@ import java.util.Map;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import de.inetsoftware.classparser.MethodInfo;
|
||||
import de.inetsoftware.jwebassembly.WasmException;
|
||||
import de.inetsoftware.jwebassembly.module.FunctionName;
|
||||
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
||||
@ -208,15 +209,19 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void prepareFunction( FunctionName name, String importModule, String importName ) {
|
||||
if( importName != null ) {
|
||||
ImportFunction importFunction;
|
||||
function = importFunction = new ImportFunction(importModule, importName);
|
||||
imports.put( name.signatureName, importFunction );
|
||||
functionType = new FunctionType();
|
||||
} else {
|
||||
functions.put( name.signatureName, new Function() );
|
||||
}
|
||||
protected void prepareImport( FunctionName name, String importModule, String importName ) {
|
||||
ImportFunction importFunction;
|
||||
function = importFunction = new ImportFunction(importModule, importName);
|
||||
imports.put( name.signatureName, importFunction );
|
||||
functionType = new FunctionType();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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}
|
||||
*/
|
||||
@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 );
|
||||
if( typeId < 0 ) {
|
||||
typeId = functionTypes.size();
|
||||
functionTypes.add( functionType );
|
||||
}
|
||||
function.typeId = typeId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
|
||||
WasmOutputStream localsStream = new WasmOutputStream();
|
||||
localsStream.writeVaruint32( locals.size() );
|
||||
for( ValueType valueType : locals ) {
|
||||
|
@ -116,21 +116,23 @@ public abstract class ModuleWriter implements Closeable {
|
||||
*/
|
||||
private void prepareMethod( MethodInfo method ) throws WasmException {
|
||||
try {
|
||||
String module = null;
|
||||
String name = null;
|
||||
FunctionName name = new FunctionName( method );
|
||||
Map<String,Object> annotationValues = method.getAnnotation( "org.webassembly.annotation.Import" );
|
||||
if( annotationValues != null ) {
|
||||
module = (String)annotationValues.get( "module" );
|
||||
name = (String)annotationValues.get( "name" );
|
||||
String impoarModule = (String)annotationValues.get( "module" );
|
||||
String importName = (String)annotationValues.get( "name" );
|
||||
prepareImport( name, impoarModule, importName );
|
||||
writeMethodSignature( method );
|
||||
} else {
|
||||
prepareFunction( name );
|
||||
}
|
||||
prepareFunction( new FunctionName( method ), module, name );
|
||||
} catch( IOException ioex ) {
|
||||
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
|
||||
* the function name
|
||||
@ -141,7 +143,15 @@ public abstract class ModuleWriter implements Closeable {
|
||||
* @throws IOException
|
||||
* 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.
|
||||
@ -260,7 +270,7 @@ public abstract class ModuleWriter implements Closeable {
|
||||
* @throws WasmException
|
||||
* 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 kind = "param";
|
||||
int paramCount = 0;
|
||||
|
@ -70,7 +70,7 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@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 ) {
|
||||
newline( output );
|
||||
output.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( name.fullName ).append( "))" );
|
||||
|
Loading…
x
Reference in New Issue
Block a user