aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-21 23:30:48 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-21 23:30:48 +0100
commit3b038d3e9058ea1683b19c58c397881e0b425088 (patch)
tree943b25d6f525cf7b357256f1f8096d56413aae21 /src/main/java
parent7d7cf0405e12e2f888097ed6f504082d8397c2b7 (diff)
Extract functions to write indexes
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java22
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);