From 8e8489ee7741fcc8bfb5d55a50ac2749074447e0 Mon Sep 17 00:00:00 2001
From: Volker Berlin <volker.berlin@googlemail.com>
Date: Sat, 13 Oct 2018 15:29:34 +0200
Subject: [PATCH] Write function names for debugging

---
 .../binary/BinaryModuleWriter.java            | 37 +++++++++++++++++--
 .../jwebassembly/binary/WasmOutputStream.java |  7 +---
 2 files changed, 35 insertions(+), 9 deletions(-)

diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java
index 2a16d57..b4e0b36 100644
--- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java
+++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java
@@ -24,6 +24,7 @@ import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -102,6 +103,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
         writeSection( SectionType.Global, globals.values() );
         writeExportSection();
         writeCodeSection();
+        writeDebugNames();
 
         wasm.close();
     }
@@ -124,7 +126,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
             for( SectionEntry entry : entries ) {
                 entry.writeSectionEntry( stream );
             }
-            wasm.writeSection( type, stream, null );
+            wasm.writeSection( type, stream );
         }
     }
 
@@ -148,7 +150,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
                 int id = functions.get( entry.getValue() ).id;
                 stream.writeVaruint32( id );
             }
-            wasm.writeSection( SectionType.Export, stream, null );
+            wasm.writeSection( SectionType.Export, stream );
         }
     }
 
@@ -166,7 +168,36 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
         WasmOutputStream stream = new WasmOutputStream();
         stream.writeVaruint32( size );
         functionsStream.writeTo( stream );
-        wasm.writeSection( SectionType.Code, stream, null );
+        wasm.writeSection( SectionType.Code, stream );
+    }
+
+    /**
+     * Write optional the debug names into the name section
+     * 
+     * @throws IOException
+     *             if any I/O error occur
+     */
+    private void writeDebugNames() throws IOException {
+        if( !debugNames ) {
+            return;
+        }
+        WasmOutputStream stream = new WasmOutputStream();
+        stream.writeString( "name" ); // Custom Section name "name", content is part of the section length
+
+        // write function names
+        stream.write( 1 ); // 1 - Function name
+        WasmOutputStream section = new WasmOutputStream();
+        section.writeVaruint32( functions.size() );
+        for( Entry<String, Function> entry : functions.entrySet() ) {
+            section.writeVaruint32( entry.getValue().id ); // function index
+            String functionName = entry.getKey();
+            functionName = functionName.substring( 0, functionName.indexOf( '(' ) );
+            section.writeString( functionName );
+        }
+        stream.writeVaruint32( section.size() );
+        section.writeTo( stream );
+
+        wasm.writeSection( SectionType.Custom, stream );
     }
 
     /**
diff --git a/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java b/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java
index 4699b8e..c644d38 100644
--- a/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java
+++ b/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java
@@ -204,12 +204,10 @@ class WasmOutputStream extends FilterOutputStream {
      *            the name of the section
      * @param data
      *            the data of the section
-     * @param name
-     *            the name, must be set if the id == 0
      * @throws IOException
      *             if any I/O error occur
      */
-    void writeSection( SectionType type, WasmOutputStream data, String name ) throws IOException {
+    void writeSection( SectionType type, WasmOutputStream data ) throws IOException {
         ByteArrayOutputStream baos = (ByteArrayOutputStream)data.out;
         int size = baos.size();
         if( size == 0 ) {
@@ -217,9 +215,6 @@ class WasmOutputStream extends FilterOutputStream {
         }
         writeVaruint32( type.ordinal() );
         writeVaruint32( size );
-        if( type == SectionType.Custom ) {
-            writeString( name );
-        }
         baos.writeTo( this );
     }