From c6ca47000321542217a61406efd03aa1e3253958 Mon Sep 17 00:00:00 2001
From: Volker <Volker@Volker.fritz.box>
Date: Thu, 21 Jun 2018 18:09:19 +0200
Subject: [PATCH] Move the end handling of a method into writeCode() to hold
 all code operation together

---
 .../jwebassembly/module/ModuleGenerator.java  | 51 +++++++++----------
 1 file changed, 24 insertions(+), 27 deletions(-)

diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java
index 7830953..bb4b164 100644
--- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java
+++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java
@@ -165,31 +165,8 @@ public class ModuleGenerator {
                 branchManager.calculate();
                 localVariables.calculate();
 
-                boolean endWithReturn = false;
                 byteCode = code.getByteCode();
-                endWithReturn = writeCode( byteCode, method.getConstantPool() );
-                branchManager.handle( byteCode, writer ); // write the last end operators
-                if( !endWithReturn && returnType != null ) {
-                    // if a method ends with a loop without a break then code after the loop is no reachable
-                    // Java does not need a return byte code in this case
-                    // But WebAssembly need the dead code to validate
-                    switch( returnType ) {
-                        case i32:
-                            writer.writeConstInt( 0 );
-                            break;
-                        case i64:
-                            writer.writeConstLong( 0 );
-                            break;
-                        case f32:
-                            writer.writeConstFloat( 0 );
-                            break;
-                        case f64:
-                            writer.writeConstDouble( 0 );
-                            break;
-                        default:
-                    }
-                    writer.writeBlockCode( WasmBlockOperator.RETURN, null );
-                }
+                writeCode( byteCode, method.getConstantPool() );
                 writer.writeMethodFinish( localVariables.getLocalTypes( paramCount ) );
             }
         } catch( Exception ioex ) {
@@ -538,9 +515,8 @@ public class ModuleGenerator {
      *            the constant pool of the the current class
      * @throws WasmException
      *             if some Java code can't converted
-     * @return true, if the last operation was a return
      */
-    private boolean writeCode( CodeInputStream byteCode, ConstantPool constantPool  ) throws WasmException {
+    private void writeCode( CodeInputStream byteCode, ConstantPool constantPool  ) throws WasmException {
         boolean endWithReturn = false;
         try {
             while( byteCode.available() > 0 ) {
@@ -916,12 +892,33 @@ public class ModuleGenerator {
                         throw new WasmException( "Unimplemented Java byte code operation: " + op, sourceFile, byteCode.getLineNumber() );
                 }
             }
+            branchManager.handle( byteCode, writer ); // write the last end operators
+            if( !endWithReturn && returnType != null ) {
+                // if a method ends with a loop without a break then code after the loop is no reachable
+                // Java does not need a return byte code in this case
+                // But WebAssembly need the dead code to validate
+                switch( returnType ) {
+                    case i32:
+                        writer.writeConstInt( 0 );
+                        break;
+                    case i64:
+                        writer.writeConstLong( 0 );
+                        break;
+                    case f32:
+                        writer.writeConstFloat( 0 );
+                        break;
+                    case f64:
+                        writer.writeConstDouble( 0 );
+                        break;
+                    default:
+                }
+                writer.writeBlockCode( WasmBlockOperator.RETURN, null );
+            }
         } catch( WasmException ex ) {
             throw ex;
         } catch( Exception ex ) {
             throw WasmException.create( ex, sourceFile, byteCode.getLineNumber() );
         }
-        return endWithReturn;
     }
 
     /**