From 1b7e1f872b27493895d44d038763cbb20e869d77 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 26 Nov 2023 15:47:13 +0000 Subject: Support Wasm try with no catch blocks --- .../binary/WasmBinaryFormatInstructionsTests.java | 45 ++++++++++++++++++++++ .../binary/WasmBinaryFormatModulesTests.java | 26 +------------ .../structure/binary/WasmBinaryFormatTesting.java | 37 ++++++++++++++++++ 3 files changed, 83 insertions(+), 25 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java create mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatTesting.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java new file mode 100644 index 00000000..54bc52a0 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java @@ -0,0 +1,45 @@ +package org.shedlang.compiler.backends.wasm.structure.binary; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.*; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFunction; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatTesting.serializeModule; + +@ExtendWith({SnapshotResolver.class}) +public class WasmBinaryFormatInstructionsTests { + // == Exception Instructions (v3 proposal) == + + @Test + public void canSerializeTryWithNoCatchBlocks(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addFunction(new WasmFunction( + "f", + List.of(), + List.of(), + List.of(), + new WasmExpression(List.of( + new WasmTry( + new WasmBlockType(List.of(WasmValueType.I32)), + List.of(), + List.of( + new WasmI32Const(42) + ) + ), + new WasmDrop() + )) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } +} diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java index afc44c79..02314d04 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java @@ -8,17 +8,13 @@ import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI64Con import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmInitFunc; import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; import org.shedlang.compiler.backends.wasm.structure.ast.types.*; -import org.shedlang.compiler.testing.TestProcessRunner; import org.shedlang.compiler.testing.snapshots.SnapshotResolver; import org.shedlang.compiler.testing.snapshots.Snapshotter; -import java.io.FileOutputStream; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.List; -import static org.shedlang.compiler.backends.wasm.structure.WasmObjdump.objdump; +import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatTesting.serializeModule; @ExtendWith({SnapshotResolver.class}) public class WasmBinaryFormatModulesTests { @@ -368,24 +364,4 @@ public class WasmBinaryFormatModulesTests { 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"); - var modulePath = temporaryDirectory.resolve("module.wasm"); - System.out.println(modulePath); - - try (var outputStream = new FileOutputStream(modulePath.toFile())) { - WasmBinaryFormatModules.serializeModule(module, outputStream); - } - - validate(modulePath); - - return objdump(modulePath); - } - - private void validate(Path path) throws IOException, InterruptedException { - TestProcessRunner.runCaptureOutput(List.of("wasm-validate", "--enable-exceptions", path.toString())) - .throwOnError(); - } } diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatTesting.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatTesting.java new file mode 100644 index 00000000..9d11dd18 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatTesting.java @@ -0,0 +1,37 @@ +package org.shedlang.compiler.backends.wasm.structure.binary; + +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; +import org.shedlang.compiler.testing.TestProcessRunner; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.shedlang.compiler.backends.wasm.structure.WasmObjdump.objdump; + +public class WasmBinaryFormatTesting { + private WasmBinaryFormatTesting() { + } + + static String serializeModule(WasmModule module) throws IOException, InterruptedException { + Files.createDirectories(Path.of("tests-tmp")); + var temporaryDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "WasmBinaryFormat"); + var modulePath = temporaryDirectory.resolve("module.wasm"); + System.out.println(modulePath); + + try (var outputStream = new FileOutputStream(modulePath.toFile())) { + WasmBinaryFormatModules.serializeModule(module, outputStream); + } + + validate(modulePath); + + return objdump(modulePath); + } + + private static void validate(Path path) throws IOException, InterruptedException { + TestProcessRunner.runCaptureOutput(List.of("wasm-validate", "--enable-exceptions", path.toString())) + .throwOnError(); + } +} -- cgit v1.2.3