Class ModuleGenerator
- java.lang.Object
-
- de.inetsoftware.jwebassembly.module.ModuleGenerator
-
public class ModuleGenerator extends java.lang.Object
Generate the WebAssembly output.
-
-
Field Summary
Fields Modifier and Type Field Description private ClassFileLoader
classFileLoader
private java.lang.String
className
private FunctionManager
functions
private JavaMethodWasmCodeBuilder
javaCodeBuilder
private JavaScriptWriter
javaScript
private java.lang.String
methodName
private CodeOptimizer
optimizer
private java.lang.String
sourceFile
private StaticCodeBuilder
staticCodeBuilder
private StringManager
strings
private TypeManager
types
private WatParser
watParser
private ModuleWriter
writer
-
Constructor Summary
Constructors Constructor Description ModuleGenerator(ModuleWriter writer, WasmTarget target, java.util.List<java.net.URL> libraries)
Create a new generator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private WasmCodeBuilder
createInstructions(MethodInfo method)
Create the instructions in a code buildervoid
finish()
Finish the code generation.private void
iterateMethods(ClassFile classFile, java.util.function.Consumer<MethodInfo> handler)
Iterate over all methods of the classFile and run the handler.void
prepare(ClassFile classFile)
Prepare the content of the class.void
prepareFinish()
Finish the prepare after all classes/methods are prepare.private void
prepareMethod(MethodInfo method)
Prepare the method.private void
prepareStartFunction()
Add a start method for the static class constructorsprivate void
scanForClinit()
Scan for needed static constructors.private boolean
scanFunctionInterfaces(ClassFile classFile, FunctionName next)
Search if there is a default implementation in an interface for the given method.private void
scanFunctions()
Scan all needed methods/functions in a loop.private void
scanLibraries(java.util.List<java.net.URL> libraries)
Scan the libraries for annotated methodsprivate void
writeExport(FunctionName name, MethodInfo method)
Look for a Export annotation and if there write an export directive.private void
writeMethod(FunctionName name, MethodInfo method)
Write the content of a method.private void
writeMethodImpl(FunctionName name, WasmCodeBuilder codeBuilder)
Write the method instruction to the Wasm writer.private void
writeMethodSignature(FunctionName name, FunctionType funcType, WasmCodeBuilder codeBuilder)
Write the parameter and return signatures
-
-
-
Field Detail
-
writer
private final ModuleWriter writer
-
javaScript
private final JavaScriptWriter javaScript
-
classFileLoader
private final ClassFileLoader classFileLoader
-
javaCodeBuilder
private final JavaMethodWasmCodeBuilder javaCodeBuilder
-
watParser
private final WatParser watParser
-
sourceFile
private java.lang.String sourceFile
-
className
private java.lang.String className
-
methodName
private java.lang.String methodName
-
functions
private final FunctionManager functions
-
types
private final TypeManager types
-
strings
private final StringManager strings
-
optimizer
private final CodeOptimizer optimizer
-
staticCodeBuilder
private final StaticCodeBuilder staticCodeBuilder
-
-
Constructor Detail
-
ModuleGenerator
public ModuleGenerator(@Nonnull ModuleWriter writer, WasmTarget target, @Nonnull java.util.List<java.net.URL> libraries)
Create a new generator.- Parameters:
writer
- the target writertarget
- the target for the module datalibraries
- libraries
-
-
Method Detail
-
scanLibraries
private void scanLibraries(@Nonnull java.util.List<java.net.URL> libraries)
Scan the libraries for annotated methods- Parameters:
libraries
- libraries
-
prepare
public void prepare(@Nonnull ClassFile classFile) throws java.io.IOException
Prepare the content of the class.- Parameters:
classFile
- the class file- Throws:
WasmException
- if some Java code can't convertedjava.io.IOException
- if any I/O error occur
-
scanFunctions
private void scanFunctions() throws java.io.IOException
Scan all needed methods/functions in a loop. If the scan find more needed content then the loop continue.- Throws:
java.io.IOException
- if any I/O error occur
-
scanFunctionInterfaces
private boolean scanFunctionInterfaces(ClassFile classFile, FunctionName next) throws java.io.IOException
Search if there is a default implementation in an interface for the given method.- Parameters:
classFile
- the class to scannext
- the method to scan- Returns:
- true, if method was found
- Throws:
java.io.IOException
- if any I/O error occur
-
prepareFinish
public void prepareFinish() throws java.io.IOException
Finish the prepare after all classes/methods are prepare. This must be call before we can start with write the first method.- Throws:
java.io.IOException
- if any I/O error occur
-
scanForClinit
private void scanForClinit() throws java.io.IOException
Scan for needed static constructors. The static code of all classes that used in any form must be executed.- Throws:
java.io.IOException
- if any I/O error occur
-
prepareStartFunction
private void prepareStartFunction() throws java.io.IOException
Add a start method for the static class constructors- Throws:
java.io.IOException
- if any I/O error occur
-
finish
public void finish() throws java.io.IOException
Finish the code generation.- Throws:
java.io.IOException
- if any I/O error occur
-
iterateMethods
private void iterateMethods(ClassFile classFile, java.util.function.Consumer<MethodInfo> handler) throws WasmException
Iterate over all methods of the classFile and run the handler.- Parameters:
classFile
- the classFilehandler
- the handler- Throws:
WasmException
- if some Java code can't converted
-
prepareMethod
private void prepareMethod(MethodInfo method) throws WasmException
Prepare the method.- Parameters:
method
- the method- Throws:
WasmException
- if some Java code can't converted
-
writeMethod
private void writeMethod(FunctionName name, MethodInfo method) throws WasmException, java.io.IOException
Write the content of a method.- Parameters:
name
- the function name that should be written. This can be differ from the value in the MethodInfomethod
- the method- Throws:
WasmException
- if some Java code can't convertedjava.io.IOException
- if any I/O error occur
-
createInstructions
@Nullable private WasmCodeBuilder createInstructions(MethodInfo method) throws java.io.IOException
Create the instructions in a code builder- Parameters:
method
- the method to parse- Returns:
- the CodeBuilder or null if it is an import function
- Throws:
java.io.IOException
- if any I/O error occur
-
writeMethodImpl
private void writeMethodImpl(FunctionName name, WasmCodeBuilder codeBuilder) throws WasmException, java.io.IOException
Write the method instruction to the Wasm writer.- Parameters:
name
- the name of the functioncodeBuilder
- the code builder with instructions- Throws:
WasmException
- if some Java code can't convertedjava.io.IOException
- if an i/O error occur
-
writeExport
private void writeExport(FunctionName name, MethodInfo method) throws java.io.IOException
Look for a Export annotation and if there write an export directive.- Parameters:
name
- the function namemethod
- the method- Throws:
java.io.IOException
- if any IOException occur
-
writeMethodSignature
private void writeMethodSignature(@Nonnull FunctionName name, @Nonnull FunctionType funcType, @Nullable WasmCodeBuilder codeBuilder) throws java.io.IOException, WasmException
Write the parameter and return signatures- Parameters:
name
- the Java signature, typical method.getType();funcType
- the type of functioncodeBuilder
- the calculated variables- Throws:
java.io.IOException
- if any I/O error occurWasmException
- if some Java code can't converted
-
-