Class ModuleGenerator

java.lang.Object
de.inetsoftware.jwebassembly.module.ModuleGenerator

public class ModuleGenerator extends Object
Generate the WebAssembly output.
  • Field Details

  • Constructor Details

    • ModuleGenerator

      public ModuleGenerator(@Nonnull ModuleWriter writer, WasmTarget target, @Nonnull List<URL> libraries)
      Create a new generator.
      Parameters:
      writer - the target writer
      target - the target for the module data
      libraries - libraries
  • Method Details

    • scanLibraries

      private void scanLibraries(@Nonnull List<URL> libraries)
      Scan the libraries for annotated methods
      Parameters:
      libraries - libraries
    • prepare

      public void prepare(@Nonnull ClassFile classFile) throws IOException
      Prepare the content of the class.
      Parameters:
      classFile - the class file
      Throws:
      WasmException - if some Java code can't converted
      IOException - if any I/O error occur
    • scanFunctions

      private void scanFunctions() throws IOException
      Scan all needed methods/functions in a loop. If the scan find more needed content then the loop continue.
      Throws:
      IOException - if any I/O error occur
    • scanFunctionInterfaces

      private boolean scanFunctionInterfaces(ClassFile classFile, FunctionName next) throws 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:
      IOException - if any I/O error occur
    • prepareFinish

      public void prepareFinish() throws IOException
      Finish the prepare after all classes/methods are prepare. This must be call before we can start with write the first method.
      Throws:
      IOException - if any I/O error occur
    • scanForClinit

      private void scanForClinit() throws IOException
      Scan for needed static constructors. The static code of all classes that used in any form must be executed.
      Throws:
      IOException - if any I/O error occur
    • prepareStartFunction

      private void prepareStartFunction() throws IOException
      Add a start method for the static class constructors
      Throws:
      IOException - if any I/O error occur
    • finish

      public void finish() throws IOException
      Finish the code generation.
      Throws:
      IOException - if any I/O error occur
    • iterateMethods

      private void iterateMethods(ClassFile classFile, 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, 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
      IOException - if any I/O error occur
    • createInstructions

      @Nullable private WasmCodeBuilder createInstructions(MethodInfo method) throws 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:
      IOException - if any I/O error occur
    • writeMethodImpl

      private void writeMethodImpl(FunctionName name, WasmCodeBuilder codeBuilder) throws WasmException, 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
      IOException - if an i/O error occur
    • writeExport

      private void writeExport(FunctionName name, MethodInfo method) throws IOException
      Look for a Export annotation and if there write an export directive.
      Parameters:
      name - the function name
      method - the method
      Throws:
      IOException - if any IOException occur
    • writeMethodSignature

      private void writeMethodSignature(@Nonnull FunctionName name, @Nonnull FunctionType funcType, @Nullable WasmCodeBuilder codeBuilder) throws 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:
      IOException - if any I/O error occur
      WasmException - if some Java code can't converted