aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-21 23:21:29 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-21 23:21:29 +0100
commit20ba12d3bf07b56cba79c0d606648cac2c9e7325 (patch)
treeed6b1713ef21aae533996c09de88757fc476a976 /src/main/java
parentadadbcb3481b85af3130d26da1f3f8f85808cb6e (diff)
Extract writeVec()
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java77
1 files changed, 56 insertions, 21 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 b472eecd..6acafeac 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
@@ -59,10 +59,11 @@ public class WasmBinaryFormat {
) throws IOException {
if (types.size() > 0) {
writeSection(SectionType.TYPE, writer, () -> {
- Leb128Encoding.encodeUnsignedInt32(types.size(), writer);
- for (var type : types) {
- writeFuncType(type, writer);
- }
+ writeVec(
+ types,
+ type -> writeFuncType(type, writer),
+ writer
+ );
});
}
}
@@ -73,16 +74,16 @@ public class WasmBinaryFormat {
) throws IOException {
if (module.imports().size() > 0) {
writeSection(SectionType.IMPORT, writer, () -> {
- Leb128Encoding.encodeUnsignedInt32(module.imports().size(), writer);
-
var funcTypeToFuncIndex = new HashMap<WasmFuncType, Integer>();
for (var funcIndex = 0; funcIndex < module.types().size(); funcIndex++) {
funcTypeToFuncIndex.put(module.types().get(funcIndex), funcIndex);
}
-
- for (var import_ : module.imports()) {
- writeImport(import_, funcTypeToFuncIndex, writer);
- }
+
+ writeVec(
+ module.imports(),
+ import_ -> writeImport(import_, funcTypeToFuncIndex, writer),
+ writer
+ );
});
}
}
@@ -108,27 +109,31 @@ public class WasmBinaryFormat {
writeSection(SectionType.CUSTOM, writer, () -> {
writeString("name", writer);
writeSection((byte) 1, writer, () -> {
- Leb128Encoding.encodeUnsignedInt32(module.imports().size(), writer);
- for (var importIndex = 0; importIndex < module.imports().size(); importIndex++) {
- Leb128Encoding.encodeUnsignedInt32(importIndex, writer);
- writeString(module.imports().get(importIndex).identifier(), writer);
- };
+ writeVec(
+ module.imports(),
+ (importIndex, import_) -> {
+ Leb128Encoding.encodeUnsignedInt32(importIndex, writer);
+ writeString(module.imports().get(importIndex).identifier(), writer);
+ },
+ writer
+ );
});
});
}
}
- private static void writeFuncType(WasmFuncType type, BinaryWriter writer) {
+ private static void writeFuncType(WasmFuncType type, BinaryWriter writer) throws IOException {
writer.writeByte((byte) 0x60);
writeValueTypeVector(type.params(), writer);
writeValueTypeVector(type.results(), writer);
}
- private static void writeValueTypeVector(List<WasmValueType> types, BinaryWriter writer) {
- Leb128Encoding.encodeUnsignedInt32(types.size(), writer);
- for (var type : types) {
- writeValueType(type, writer);
- }
+ private static void writeValueTypeVector(List<WasmValueType> types, BinaryWriter writer) throws IOException {
+ writeVec(
+ types,
+ type -> writeValueType(type, writer),
+ writer
+ );
}
private static void writeValueType(WasmValueType type, BinaryWriter writer) {
@@ -179,6 +184,36 @@ public class WasmBinaryFormat {
writer.seek(contentEndPosition);
}
+ interface WriteElement<T> {
+ void write(T element) throws IOException;
+ }
+
+ interface WriteIndexedElement<T> {
+ void write(int index, T element) throws IOException;
+ }
+
+ private static <T> void writeVec(
+ List<T> vec,
+ WriteElement<T> writeElement,
+ BinaryWriter writer
+ ) throws IOException {
+ Leb128Encoding.encodeUnsignedInt32(vec.size(), writer);
+ for (var element : vec) {
+ writeElement.write(element);
+ }
+ }
+
+ private static <T> void writeVec(
+ List<T> vec,
+ WriteIndexedElement<T> writeElement,
+ BinaryWriter writer
+ ) throws IOException {
+ Leb128Encoding.encodeUnsignedInt32(vec.size(), writer);
+ for (var elementIndex = 0; elementIndex < vec.size(); elementIndex++) {
+ writeElement.write(elementIndex, vec.get(elementIndex));
+ }
+ }
+
private static void writeString(String value, BinaryWriter writer) {
var bytes = value.getBytes(StandardCharsets.UTF_8);
Leb128Encoding.encodeUnsignedInt32(bytes.length, writer);