mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
Add a function manager to add functions on demand.
This commit is contained in:
parent
1655310a0f
commit
51b58e2a79
@ -258,5 +258,6 @@ public class JWebAssembly {
|
|||||||
ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) );
|
ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) );
|
||||||
generator.write( classFile );
|
generator.write( classFile );
|
||||||
}
|
}
|
||||||
|
generator.finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
53
src/de/inetsoftware/jwebassembly/module/FunctionManager.java
Normal file
53
src/de/inetsoftware/jwebassembly/module/FunctionManager.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package de.inetsoftware.jwebassembly.module;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manage the required function/methods
|
||||||
|
*
|
||||||
|
* @author Volker Berlin
|
||||||
|
*/
|
||||||
|
public class FunctionManager {
|
||||||
|
|
||||||
|
private HashSet<String> writtenFunctions = new HashSet<>();
|
||||||
|
|
||||||
|
private HashSet<FunctionName> toWriteLater = new HashSet<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mark the a function as written to the wasm file.
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* the function name
|
||||||
|
*/
|
||||||
|
void writeFunction( FunctionName name ) {
|
||||||
|
toWriteLater.remove( name );
|
||||||
|
writtenFunctions.add( name.signatureName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mark a function as used/called.
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* the function name
|
||||||
|
*/
|
||||||
|
void functionCall( FunctionName name ) {
|
||||||
|
if( !writtenFunctions.contains( name.signatureName ) ) {
|
||||||
|
toWriteLater.add( name );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -53,6 +53,8 @@ public class ModuleGenerator {
|
|||||||
|
|
||||||
private String className;
|
private String className;
|
||||||
|
|
||||||
|
private FunctionManager functions = new FunctionManager();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new generator.
|
* Create a new generator.
|
||||||
*
|
*
|
||||||
@ -98,6 +100,23 @@ public class ModuleGenerator {
|
|||||||
iterateMethods( classFile, m -> writeMethod( m ) );
|
iterateMethods( classFile, m -> writeMethod( m ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finish the code generation.
|
||||||
|
*/
|
||||||
|
public void finish() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterate over all methods of the classFile and run the handler.
|
||||||
|
*
|
||||||
|
* @param classFile
|
||||||
|
* the classFile
|
||||||
|
* @param handler
|
||||||
|
* the handler
|
||||||
|
* @throws WasmException
|
||||||
|
* if some Java code can't converted
|
||||||
|
*/
|
||||||
private void iterateMethods( ClassFile classFile, Consumer<MethodInfo> handler ) throws WasmException {
|
private void iterateMethods( ClassFile classFile, Consumer<MethodInfo> handler ) throws WasmException {
|
||||||
sourceFile = null; // clear previous value for the case an IO exception occur
|
sourceFile = null; // clear previous value for the case an IO exception occur
|
||||||
className = null;
|
className = null;
|
||||||
@ -131,6 +150,7 @@ public class ModuleGenerator {
|
|||||||
FunctionName name = new FunctionName( method );
|
FunctionName name = new FunctionName( method );
|
||||||
Map<String,Object> annotationValues = method.getAnnotation( JWebAssembly.IMPORT_ANNOTATION );
|
Map<String,Object> annotationValues = method.getAnnotation( JWebAssembly.IMPORT_ANNOTATION );
|
||||||
if( annotationValues != null ) {
|
if( annotationValues != null ) {
|
||||||
|
functions.writeFunction( name );
|
||||||
String impoarModule = (String)annotationValues.get( "module" );
|
String impoarModule = (String)annotationValues.get( "module" );
|
||||||
String importName = (String)annotationValues.get( "name" );
|
String importName = (String)annotationValues.get( "name" );
|
||||||
writer.prepareImport( name, impoarModule, importName );
|
writer.prepareImport( name, impoarModule, importName );
|
||||||
@ -179,10 +199,13 @@ public class ModuleGenerator {
|
|||||||
FunctionName name = new FunctionName( method );
|
FunctionName name = new FunctionName( method );
|
||||||
writeExport( name, method );
|
writeExport( name, method );
|
||||||
writer.writeMethodStart( name );
|
writer.writeMethodStart( name );
|
||||||
|
functions.writeFunction( name );
|
||||||
writeMethodSignature( signature, code.getLocalVariableTable(), codeBuilder );
|
writeMethodSignature( signature, code.getLocalVariableTable(), codeBuilder );
|
||||||
|
|
||||||
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
|
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
|
||||||
|
if( instruction instanceof WasmCallInstruction ) {
|
||||||
|
functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() );
|
||||||
|
}
|
||||||
instruction.writeTo( writer );
|
instruction.writeTo( writer );
|
||||||
}
|
}
|
||||||
writer.writeMethodFinish();
|
writer.writeMethodFinish();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user