Class ModuleGenerator


  • public class ModuleGenerator
    extends java.lang.Object
    Generate the WebAssembly output.
    • 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 writer
        target - the target for the module data
        libraries - 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 converted
        java.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 scan
        next - 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 classFile
        handler - 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 MethodInfo
        method - the method
        Throws:
        WasmException - if some Java code can't converted
        java.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 function
        codeBuilder - the code builder with instructions
        Throws:
        WasmException - if some Java code can't converted
        java.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 name
        method - 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 function
        codeBuilder - the calculated variables
        Throws:
        java.io.IOException - if any I/O error occur
        WasmException - if some Java code can't converted