From 3b038d3e9058ea1683b19c58c397881e0b425088 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 21 Jun 2023 23:30:48 +0100 Subject: Extract functions to write indexes --- .../backends/wasm/structure/WasmBinaryFormat.java | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java index 1dc57d2e..14e31070 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java @@ -74,14 +74,14 @@ public class WasmBinaryFormat { ) throws IOException { if (module.imports().size() > 0) { writeSection(SectionType.IMPORT, writer, () -> { - var funcTypeToFuncIndex = new HashMap(); + var funcTypeToTypeIndex = new HashMap(); for (var funcIndex = 0; funcIndex < module.types().size(); funcIndex++) { - funcTypeToFuncIndex.put(module.types().get(funcIndex), funcIndex); + funcTypeToTypeIndex.put(module.types().get(funcIndex), funcIndex); } writeVec( module.imports(), - import_ -> writeImport(import_, funcTypeToFuncIndex, writer), + import_ -> writeImport(import_, funcTypeToTypeIndex, writer), writer ); }); @@ -90,7 +90,7 @@ public class WasmBinaryFormat { private static void writeImport( WasmImport import_, - Map funcTypeToFuncIndex, + Map funcTypeToTypeIndex, BinaryWriter writer ) { writeString(import_.moduleName(), writer); @@ -98,8 +98,8 @@ public class WasmBinaryFormat { switch (import_.descriptor()) { case WasmImportDescriptor.Func func -> { writer.writeByte(0x00); - var funcIndex = funcTypeToFuncIndex.get(func.type()); - Leb128Encoding.encodeUnsignedInt32(funcIndex, writer); + var typeIndex = funcTypeToTypeIndex.get(func.type()); + writeTypeIndex(typeIndex, writer); } } } @@ -118,7 +118,7 @@ public class WasmBinaryFormat { writeVec( module.imports(), (importIndex, import_) -> { - Leb128Encoding.encodeUnsignedInt32(importIndex, writer); + writeFuncIndex(importIndex, writer); writeString(module.imports().get(importIndex).identifier(), writer); }, writer @@ -126,6 +126,14 @@ public class WasmBinaryFormat { }); } + private static void writeFuncIndex(int funcIndex, BinaryWriter writer) { + Leb128Encoding.encodeUnsignedInt32(funcIndex, writer); + } + + private static void writeTypeIndex(int typeIndex, BinaryWriter writer) { + Leb128Encoding.encodeUnsignedInt32(typeIndex, writer); + } + private static void writeFuncType(WasmFuncType type, BinaryWriter writer) throws IOException { writer.writeByte((byte) 0x60); writeValueTypeVector(type.params(), writer); -- cgit v1.2.3