From 8262a45343d529bbb4c518478304174298c0df76 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 5 Apr 2020 21:42:40 +0200 Subject: [PATCH] Write also the import function names into the debug section (custom section name). --- .../binary/BinaryModuleWriter.java | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 28882bc..b9e79f8 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -23,6 +23,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; @@ -358,7 +359,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod } /** - * Write optional the debug names into the name section + * Write optional the debug names into the custom "name" section. * * @throws IOException * if any I/O error occur @@ -373,21 +374,55 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod // write function names stream.write( 1 ); // 1 - Function name WasmOutputStream section = new WasmOutputStream(); - section.writeVaruint32( functions.size() ); - for( Entry entry : functions.entrySet() ) { - section.writeVaruint32( entry.getValue().id ); // function index - String functionName = entry.getKey(); - functionName = functionName.substring( 0, functionName.indexOf( '(' ) ); - section.writeString( functionName ); - } + section.writeVaruint32( imports.size() + functions.size() ); + writeDebugFunctionNames( imports.entrySet(), section ); + writeDebugFunctionNames( functions.entrySet(), section ); stream.writeVaruint32( section.size() ); section.writeTo( stream ); // write function parameter names stream.write( 2 ); // 2 - Local names section.reset(); - section.writeVaruint32( functions.size() ); - for( Entry entry : functions.entrySet() ) { + section.writeVaruint32( imports.size() + functions.size() ); + writeDebugParameternNames( imports.entrySet(), section ); + writeDebugParameternNames( functions.entrySet(), section ); + stream.writeVaruint32( section.size() ); + section.writeTo( stream ); + + wasm.writeSection( SectionType.Custom, stream ); + } + + /** + * Write function names to the custom "name" section. + * + * @param entries + * the functions + * @param section + * the target + * @throws IOException + * if any I/O error occur + */ + private void writeDebugFunctionNames( Set> entries, WasmOutputStream section ) throws IOException { + for( Entry entry : entries ) { + section.writeVaruint32( entry.getValue().id ); // function index + String functionName = entry.getKey(); + functionName = functionName.substring( 0, functionName.indexOf( '(' ) ); + section.writeString( functionName ); + } + } + + /** + * Write parameter names to the custom "name" section. + * + * @param entries + * the functions + * @param section + * the target + * @throws IOException + * if any I/O error occur + */ + private void writeDebugParameternNames( Set> entries, WasmOutputStream section ) throws IOException { + for( Entry entry : entries ) { Function func = entry.getValue(); section.writeVaruint32( func.id ); // function index List paramNames = func.paramNames; @@ -398,10 +433,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod section.writeString( paramNames.get( i ) ); } } - stream.writeVaruint32( section.size() ); - section.writeTo( stream ); - - wasm.writeSection( SectionType.Custom, stream ); } /**