Class ModuleGenerator
- java.lang.Object
-
- de.inetsoftware.jwebassembly.module.ModuleGenerator
-
public class ModuleGenerator extends java.lang.ObjectGenerate the WebAssembly output.
-
-
Field Summary
Fields Modifier and Type Field Description private ClassFileLoaderclassFileLoaderprivate java.lang.StringclassNameprivate FunctionManagerfunctionsprivate JavaMethodWasmCodeBuilderjavaCodeBuilderprivate JavaScriptWriterjavaScriptprivate java.lang.StringmethodNameprivate CodeOptimizeroptimizerprivate java.lang.StringsourceFileprivate StaticCodeBuilderstaticCodeBuilderprivate StringManagerstringsprivate TypeManagertypesprivate WatParserwatParserprivate ModuleWriterwriter
-
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 WasmCodeBuildercreateInstructions(MethodInfo method)Create the instructions in a code buildervoidfinish()Finish the code generation.private voiditerateMethods(ClassFile classFile, java.util.function.Consumer<MethodInfo> handler)Iterate over all methods of the classFile and run the handler.voidprepare(ClassFile classFile)Prepare the content of the class.voidprepareFinish()Finish the prepare after all classes/methods are prepare.private voidprepareMethod(MethodInfo method)Prepare the method.private voidprepareStartFunction()Add a start method for the static class constructorsprivate voidscanForClinit()Scan for needed static constructors.private booleanscanFunctionInterfaces(ClassFile classFile, FunctionName next)Search if there is a default implementation in an interface for the given method.private voidscanFunctions()Scan all needed methods/functions in a loop.private voidscanLibraries(java.util.List<java.net.URL> libraries)Scan the libraries for annotated methodsprivate voidwriteExport(FunctionName name, MethodInfo method)Look for a Export annotation and if there write an export directive.private voidwriteMethod(FunctionName name, MethodInfo method)Write the content of a method.private voidwriteMethodImpl(FunctionName name, WasmCodeBuilder codeBuilder)Write the method instruction to the Wasm writer.private voidwriteMethodSignature(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.IOExceptionPrepare 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.IOExceptionScan 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.IOExceptionFinish 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.IOExceptionScan 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.IOExceptionAdd 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.IOExceptionFinish 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, WasmExceptionWrite 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
-
-