|
1 | 1 | package io.github.kawamuray.wasmtime;
|
2 | 2 |
|
| 3 | +import lombok.Data; |
| 4 | +import org.junit.Assert; |
3 | 5 | import org.junit.Test;
|
4 | 6 |
|
| 7 | +import java.util.function.Consumer; |
| 8 | + |
5 | 9 | public class ModuleTest {
|
6 | 10 | private static final byte[] WAT_BINARY = ("(module"
|
7 |
| - + " (func (export \"add\") (param $p1 i32) (param $p2 i32) (result i32)" |
8 |
| - + " local.get $p1\n" |
9 |
| - + " local.get $p2\n" |
10 |
| - + " i32.add)" |
11 |
| - + ")").getBytes(); |
| 11 | + + " (func (export \"add\") (param $p1 i32) (param $p2 i32) (result i32)" |
| 12 | + + " local.get $p1\n" |
| 13 | + + " local.get $p2\n" |
| 14 | + + " i32.add)" |
| 15 | + + ")").getBytes(); |
| 16 | + |
| 17 | + // Required: Only one memory may be defined |
| 18 | + private static final byte[] MEM2 = ("(module" + |
| 19 | + " (import \"mem\" \"two\" (memory $mem2 13 37))" + |
| 20 | + ")").getBytes(); |
| 21 | + |
| 22 | + private static final byte[] IMPORT_WAT_BINARY = ("(module" + |
| 23 | + " (global $m1 (import \"globals\" \"mutable\") (mut i32))\n" + |
| 24 | + " (global $c2 (import \"globalz\" \"const\") i64)\n" + |
| 25 | + " (func $hello (import \"first\" \"package\"))\n" + |
| 26 | + " (import \"tbl\" \"small\" (table 0 4 anyfunc))\n" + |
| 27 | + " (import \"tbl\" \"big\" (table 12 1995 anyfunc))\n" + |
| 28 | + " (import \"lua\" \"integration\" (table 1 anyfunc))\n" + |
| 29 | + " (import \"env\" \"memory\" (memory $mem 1))\n" + |
| 30 | + " (import \"\" \"package\" (func $world (param $p1 i32)))\n" + |
| 31 | + " (import \"xyz\" \"return\" (func (result i32)))\n" + |
| 32 | + " (import \"xyz\" \"return\" (func (param i32 i32 i32 i32 i32)))\n" + |
| 33 | + " (func (export \"run\") (call $hello))\n" + |
| 34 | + ")").getBytes(); |
| 35 | + |
12 | 36 | @Test
|
13 | 37 | public void testCreateDispose() {
|
14 | 38 | try (Engine engine = new Engine()) {
|
15 | 39 | Module module = new Module(engine, WAT_BINARY);
|
16 | 40 | module.close();
|
17 | 41 | }
|
18 | 42 | }
|
| 43 | + |
| 44 | + @Test |
| 45 | + public void testMem2() { |
| 46 | + try ( |
| 47 | + Engine engine = new Engine(); |
| 48 | + Module module = new Module(engine, MEM2) |
| 49 | + ) { |
| 50 | + runImportTest(module, new TestImportData[]{ |
| 51 | + TestImportData.memory("mem", "two", 13, 37) |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testAccessImports() { |
| 58 | + try ( |
| 59 | + Engine engine = new Engine(); |
| 60 | + Module module = new Module(engine, IMPORT_WAT_BINARY) |
| 61 | + ) { |
| 62 | + // TODO: Test other import typese |
| 63 | + runImportTest(module, new TestImportData[]{ |
| 64 | + TestImportData.global("globals", "mutable", Val.Type.I32, Mutability.VAR), |
| 65 | + TestImportData.global("globalz", "const", Val.Type.I64, Mutability.CONST), |
| 66 | + TestImportData.func("first", "package", new Val.Type[]{}, new Val.Type[]{}), |
| 67 | + TestImportData.table("tbl", "small", Val.Type.FUNC_REF, 0, 4), |
| 68 | + TestImportData.table("tbl", "big", Val.Type.FUNC_REF, 12, 1995), |
| 69 | + TestImportData.table("lua", "integration", Val.Type.FUNC_REF, 1, -1), |
| 70 | + TestImportData.memory("env", "memory", 1, -1), |
| 71 | + TestImportData.func("", "package", new Val.Type[]{Val.Type.I32}, new Val.Type[]{}), |
| 72 | + TestImportData.func("xyz", "return", new Val.Type[]{}, new Val.Type[]{Val.Type.I32}), |
| 73 | + TestImportData.func("xyz", "return", new Val.Type[]{Val.Type.I32, Val.Type.I32, Val.Type.I32, Val.Type.I32, Val.Type.I32}, new Val.Type[]{}) |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void runImportTest(Module module, TestImportData<?>[] testData) { |
| 79 | + int i = 0; |
| 80 | + for (ImportType imp : module.imports()) { |
| 81 | + Assert.assertTrue("Test Data not big enough", testData.length > i); |
| 82 | + TestImportData<?> data = testData[i]; |
| 83 | + Assert.assertEquals(data.getModule(), imp.module()); |
| 84 | + Assert.assertEquals(data.getName(), imp.name()); |
| 85 | + Assert.assertEquals(data.getType(), imp.type()); |
| 86 | + checkImportType(data, imp); |
| 87 | + i += 1; |
| 88 | + } |
| 89 | + Assert.assertEquals("Not Every Test Case was returned", testData.length, i); |
| 90 | + } |
| 91 | + |
| 92 | + private <T> void checkImportType(TestImportData<T> data, ImportType type) { |
| 93 | + Class<T> clazz = data.getClazz(); |
| 94 | + Object typeObj = type.typeObj(); |
| 95 | + Assert.assertNotNull("Type Object is null", typeObj); |
| 96 | + Class<?> typeObjClass = typeObj.getClass(); |
| 97 | + Assert.assertTrue(String.format("Expected Type is different. Expected %s but was %s", clazz, typeObjClass), clazz.isAssignableFrom(typeObjClass)); |
| 98 | + data.verify(type, typeObj); |
| 99 | + } |
| 100 | + |
| 101 | + @Data |
| 102 | + private static class TestImportData<T> { |
| 103 | + private final String module; |
| 104 | + private final String name; |
| 105 | + private final ImportType.Type type; |
| 106 | + private final Class<T> clazz; |
| 107 | + private final Consumer<ImportType> verifyImport; |
| 108 | + private final Consumer<T> consumer; |
| 109 | + |
| 110 | + static TestImportData<MemoryType> memory(String module, String name, int min, int max) { |
| 111 | + return new TestImportData<>( |
| 112 | + module, name, ImportType.Type.MEMORY, MemoryType.class, |
| 113 | + mod -> { |
| 114 | + Assert.assertThrows(RuntimeException.class, mod::func); |
| 115 | + Assert.assertThrows(RuntimeException.class, mod::global); |
| 116 | + Assert.assertEquals(mod.typeObj(), mod.memory()); |
| 117 | + Assert.assertThrows(RuntimeException.class, mod::table); |
| 118 | + }, |
| 119 | + mem -> { |
| 120 | + MemoryType.Limit limit = mem.limit(); |
| 121 | + Assert.assertEquals(min, limit.min()); |
| 122 | + Assert.assertEquals(max, limit.max()); |
| 123 | + } |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + static TestImportData<GlobalType> global(String module, String name, Val.Type content, Mutability mutability) { |
| 128 | + return new TestImportData<>( |
| 129 | + module, name, ImportType.Type.GLOBAL, GlobalType.class, |
| 130 | + mod -> { |
| 131 | + Assert.assertThrows(RuntimeException.class, mod::func); |
| 132 | + Assert.assertEquals(mod.typeObj(), mod.global()); |
| 133 | + Assert.assertThrows(RuntimeException.class, mod::memory); |
| 134 | + Assert.assertThrows(RuntimeException.class, mod::table); |
| 135 | + }, |
| 136 | + global -> { |
| 137 | + Assert.assertEquals(content, global.getContent()); |
| 138 | + Assert.assertEquals(mutability, global.getMutability()); |
| 139 | + } |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + static TestImportData<FuncType> func(String module, String name, Val.Type[] params, Val.Type[] results) { |
| 144 | + return new TestImportData<>( |
| 145 | + module, name, ImportType.Type.FUNC, FuncType.class, |
| 146 | + mod -> { |
| 147 | + Assert.assertEquals(mod.typeObj(), mod.func()); |
| 148 | + Assert.assertThrows(RuntimeException.class, mod::global); |
| 149 | + Assert.assertThrows(RuntimeException.class, mod::memory); |
| 150 | + Assert.assertThrows(RuntimeException.class, mod::table); |
| 151 | + }, |
| 152 | + func -> { |
| 153 | + Assert.assertArrayEquals(params, func.getParams()); |
| 154 | + Assert.assertArrayEquals(results, func.getResults()); |
| 155 | + } |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + public static TestImportData<TableType> table(String module, String name, Val.Type content, int min, int max) { |
| 160 | + return new TestImportData<>( |
| 161 | + module, name, ImportType.Type.TABLE, TableType.class, |
| 162 | + mod -> { |
| 163 | + Assert.assertThrows(RuntimeException.class, mod::func); |
| 164 | + Assert.assertThrows(RuntimeException.class, mod::global); |
| 165 | + Assert.assertThrows(RuntimeException.class, mod::memory); |
| 166 | + Assert.assertEquals(mod.typeObj(), mod.table()); |
| 167 | + }, |
| 168 | + table -> { |
| 169 | + Assert.assertEquals(content, table.element()); |
| 170 | + |
| 171 | + MemoryType.Limit limit = table.limit(); |
| 172 | + Assert.assertEquals(min, limit.min()); |
| 173 | + Assert.assertEquals(max, limit.max()); |
| 174 | + } |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + @SuppressWarnings("unchecked") |
| 179 | + public void verify(final ImportType imp, final Object typeObj) { |
| 180 | + this.verifyImport.accept(imp); |
| 181 | + this.consumer.accept((T) typeObj); |
| 182 | + } |
| 183 | + } |
19 | 184 | }
|
0 commit comments