From 41d4ba7f621b613a8c332addf2bd54f2c78e2d72 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 24 Jun 2023 20:43:59 +0100 Subject: WASM binary format: support functions --- .../structure/WasmBinaryFormatModuleTests.java | 21 ++++++++++++++++ .../wasm/structure/binary/Leb128EncodingTests.java | 29 +++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) (limited to 'src/test/java') 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 6d3684db..cb1e7a76 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 @@ -2,6 +2,8 @@ package org.shedlang.compiler.backends.wasm.structure; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI64Const; import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; import org.shedlang.compiler.backends.wasm.structure.ast.types.*; import org.shedlang.compiler.testing.ProcessRunner; @@ -39,6 +41,25 @@ public class WasmBinaryFormatModuleTests { snapshotter.assertSnapshot(result); } + @Test + public void canSerializeModuleWithFunctions(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addFunction(new WasmFunction( + new WasmFuncType(List.of(), List.of(WasmValueType.I32)), + List.of(WasmValueType.F32, WasmValueType.F64), + List.of(new WasmI32Const(0)) + )) + .addFunction(new WasmFunction( + new WasmFuncType(List.of(), List.of(WasmValueType.I64)), + List.of(), + List.of(new WasmI64Const(0)) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + @Test public void canSerializeModuleWithFunctionImports(Snapshotter snapshotter) throws IOException, InterruptedException { var module = WasmModule.EMPTY diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java index acb5ab9f..f9ef502d 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java @@ -10,7 +10,7 @@ import static org.zwobble.precisely.Matchers.equalTo; public class Leb128EncodingTests { @Test - public void unsignedEncoding() { + public void unsignedInt32Encoding() { assertUnsignedInt32Encoding(0x0, "00"); assertUnsignedInt32Encoding(0x1, "01"); assertUnsignedInt32Encoding(0x7F, "7f"); @@ -25,7 +25,7 @@ public class Leb128EncodingTests { } @Test - public void paddedUnsignedEncoding() { + public void paddedUnsignedInt32Encoding() { assertPaddedUnsignedInt32Encoding(0x0, "8080808000"); assertPaddedUnsignedInt32Encoding(0x1, "8180808000"); assertPaddedUnsignedInt32Encoding(0x7F, "ff80808000"); @@ -40,7 +40,7 @@ public class Leb128EncodingTests { } @Test - public void signedEncoding() { + public void signedInt32Encoding() { assertSignedInt32Encoding(0x0, "00"); assertSignedInt32Encoding(0x1, "01"); assertSignedInt32Encoding(0x3F, "3f"); @@ -59,7 +59,7 @@ public class Leb128EncodingTests { } @Test - public void paddedSignedEncoding() { + public void paddedSignedInt32Encoding() { assertPaddedSignedInt32Encoding(0x0, "8080808000"); assertPaddedSignedInt32Encoding(0x1, "8180808000"); assertPaddedSignedInt32Encoding(0x3F, "bf80808000"); @@ -77,6 +77,27 @@ public class Leb128EncodingTests { assertByteArray(writer.toBytes(), expectedHex); } + @Test + public void signedInt64Encoding() { + assertSignedInt64Encoding(0x0, "00"); + assertSignedInt64Encoding(0x1, "01"); + assertSignedInt64Encoding(0x3F, "3f"); + assertSignedInt64Encoding(0x40, "c000"); + assertSignedInt64Encoding(0x7F, "ff00"); + assertSignedInt64Encoding(0x80, "8001"); + assertSignedInt64Encoding(-0x1, "7f"); + assertSignedInt64Encoding(-0x40, "40"); + assertSignedInt64Encoding(-0x41, "bf7f"); + assertSignedInt64Encoding(Long.MAX_VALUE, "ffffffffffffffffff00"); + assertSignedInt64Encoding(Long.MIN_VALUE, "8080808080808080807f"); + } + + private void assertSignedInt64Encoding(long value, String expectedHex) { + var writer = new BinaryWriter(); + Leb128Encoding.encodeSignedInt64(value, writer); + assertByteArray(writer.toBytes(), expectedHex); + } + private void assertByteArray(byte[] actual, String expectedHex) { assertThat(HexFormat.of().formatHex(actual), equalTo(expectedHex)); } -- cgit v1.2.3