diff options
Diffstat (limited to 'src/main/java/org')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java | 22 |
1 files changed, 15 insertions, 7 deletions
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<WasmFuncType, Integer>(); + var funcTypeToTypeIndex = new HashMap<WasmFuncType, Integer>(); 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<WasmFuncType, Integer> funcTypeToFuncIndex, + Map<WasmFuncType, Integer> 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); |
