From 2242b12c09562b0227c8ee875750930f42b06b50 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 21 Dec 2023 16:37:44 +0000 Subject: Check that we're not using placeholder unrelocatable values --- .../structure/binary/WasmBinaryWriterTests.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriterTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriterTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriterTests.java new file mode 100644 index 00000000..b7effd0e --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriterTests.java @@ -0,0 +1,50 @@ +package org.shedlang.compiler.backends.wasm.structure.binary; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class WasmBinaryWriterTests { + @Test + public void whenNotInRelocatableSectionThenWritingMemoryAddressConstThrowsError() throws IOException { + var writer = new WasmBinaryWriter(WasmIndexTable.fromModule(WasmModule.EMPTY)); + + var error = assertThrows( + IllegalStateException.class, + () -> writer.writeSection( + WasmSectionId.TYPE, + () -> writer.writeMemoryAddressConst(new WasmMemoryAddress("x")) + ) + ); + + assertThat( + error.getMessage(), + equalTo("cannot write memory address outside of relocatable section") + ); + } + + @Test + public void whenNotInRelocatableSectionThenWritingTableElementIndexConstThrowsError() throws IOException { + var writer = new WasmBinaryWriter(WasmIndexTable.fromModule(WasmModule.EMPTY)); + + var error = assertThrows( + IllegalStateException.class, + () -> writer.writeSection( + WasmSectionId.TYPE, + () -> writer.writeTableElementIndexConst(new WasmFuncName("f")) + ) + ); + + assertThat( + error.getMessage(), + equalTo("cannot write memory address outside of relocatable section") + ); + } +} -- cgit v1.2.3