diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-24 18:41:38 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-24 18:41:38 +0100 |
| commit | 56a992c96740b647b7f6feb40ce0204aec0ff2c4 (patch) | |
| tree | 51fa456eb81a193eeb3bffaca019413a7ecad9ea | |
| parent | 4f8c81e479b5e83bc301f04f01a12466f9e1edf2 (diff) | |
WASM binary format: support memory imports
5 files changed, 41 insertions, 0 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithMemoryImports(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithMemoryImports(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..7217d7c4 --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithMemoryImports(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,11 @@ + +module.wasm: file format wasm 0x1 + +Section Details: + +Import[1]: + - memory[0] pages: initial=1 <- ModuleName1.EntityName1 +Custom: + - name: "name" + +Code Disassembly: 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 430e1ab4..714f1936 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 @@ -105,6 +105,10 @@ public class WasmBinaryFormat { writer.writeByte(0x01); writeTableType(table.type(), writer); } + case WasmImportDescriptor.Memory memory -> { + writer.writeByte(0x02); + writeMemoryType(memory.type(), writer); + } } } @@ -151,6 +155,10 @@ public class WasmBinaryFormat { writeValueTypeVector(type.results(), writer); } + private static void writeMemoryType(WasmMemoryType type, BinaryWriter writer) { + writeLimits(type.limits(), writer); + } + private static void writeReferenceType(WasmReferenceType type, BinaryWriter writer) { var encoding = switch (type) { case FUNC_REF -> 0x70; diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImportDescriptor.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImportDescriptor.java index db0ec936..e0bdc456 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImportDescriptor.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImportDescriptor.java @@ -1,9 +1,11 @@ package org.shedlang.compiler.backends.wasm.structure.ast.imports; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmMemoryType; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmTableType; public sealed interface WasmImportDescriptor { record Func(String identifier, WasmFuncType type) implements WasmImportDescriptor {} record Table(WasmTableType type) implements WasmImportDescriptor {} + record Memory(WasmMemoryType type) implements WasmImportDescriptor {} } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmMemoryType.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmMemoryType.java new file mode 100644 index 00000000..0517fd3c --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmMemoryType.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.types; + +public record WasmMemoryType(WasmLimits limits) { +} diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java index e8de81a4..b3652f98 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java @@ -87,6 +87,22 @@ public class WasmBinaryFormatModuleTests { snapshotter.assertSnapshot(result); } + @Test + public void canSerializeModuleWithMemoryImports(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addImport(new WasmImport( + "ModuleName1", + "EntityName1", + new WasmImportDescriptor.Memory(new WasmMemoryType( + WasmLimits.min(1) + )) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + private String serializeModule(WasmModule module) throws IOException, InterruptedException { Files.createDirectories(Path.of("tests-tmp")); var temporaryDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "WasmBinaryFormat"); |
