Skip to content

Commit c9ae949

Browse files
committed
unchecked dispatch tests
1 parent c1917d1 commit c9ae949

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/GenericsWithTypeclassesTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,11 +2091,49 @@ public void fullArrayListTest() throws IOException {
20912091
assertEquals(count, 2);
20922092

20932093
assertFalse(compiled.contains("ArrayList_nextFreeIndex_"));
2094+
// In checked mode, virtual method calls should go through dispatch.
2095+
assertTrue(compiled.contains("dispatch_ArrayList"));
2096+
}
2097+
2098+
@Test
2099+
public void fullArrayListTestUncheckedDispatch() throws IOException {
2100+
test().withStdLib().uncheckedDispatch().executeProg().executeTests().file(new File(TEST_DIR + "arrayList.wurst"));
2101+
2102+
String compiled = Files.toString(
2103+
new File(TEST_OUTPUT_PATH + "GenericsWithTypeclassesTests_fullArrayListTestUncheckedDispatch_no_opts.jim"),
2104+
Charsets.UTF_8);
2105+
2106+
// In unchecked dispatch mode, monomorphic ArrayList<T>.get should be lowered directly.
2107+
assertTrue(compiled.contains("ArrayList_get"));
2108+
assertFalse(compiled.contains("dispatch_ArrayList"));
20942109
}
20952110

20962111
@Test
20972112
public void fullReactiveGenericDispatchTest() throws IOException {
20982113
test().withStdLib().executeProg().executeTests().file(new File(TEST_DIR + "reactiveGenericsDispatch.wurst"));
2114+
2115+
String compiled = Files.toString(
2116+
new File(TEST_OUTPUT_PATH + "GenericsWithTypeclassesTests_fullReactiveGenericDispatchTest_no_opts.jim"),
2117+
Charsets.UTF_8);
2118+
2119+
// In checked mode, polymorphic interface calls are dispatched and guarded.
2120+
assertTrue(compiled.contains("dispatch_ReactiveSource_ReactiveSource_subscribe"));
2121+
assertTrue(compiled.contains("Nullpointer exception when calling ReactiveSource.subscribe"));
2122+
assertTrue(compiled.contains("Called ReactiveSource.subscribe on invalid object."));
2123+
}
2124+
2125+
@Test
2126+
public void fullReactiveGenericDispatchTestUncheckedDispatch() throws IOException {
2127+
test().withStdLib().uncheckedDispatch().executeProg().executeTests().file(new File(TEST_DIR + "reactiveGenericsDispatch.wurst"));
2128+
2129+
String compiled = Files.toString(
2130+
new File(TEST_OUTPUT_PATH + "GenericsWithTypeclassesTests_fullReactiveGenericDispatchTestUncheckedDispatch_no_opts.jim"),
2131+
Charsets.UTF_8);
2132+
2133+
// ReactiveSource is polymorphic, so dispatch remains, but safety checks should be removed.
2134+
assertTrue(compiled.contains("dispatch_ReactiveSource_ReactiveSource_subscribe"));
2135+
assertFalse(compiled.contains("Nullpointer exception when calling ReactiveSource.subscribe"));
2136+
assertFalse(compiled.contains("Called ReactiveSource.subscribe on invalid object."));
20992137
}
21002138

21012139
@Test

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/WurstScriptTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TestConfig {
7070
private boolean stopOnFirstError = true;
7171
private boolean runCompiletimeFunctions;
7272
private boolean testLua = false;
73+
private boolean uncheckedDispatch = false;
7374

7475
TestConfig(String name) {
7576
this.name = name;
@@ -123,6 +124,15 @@ public TestConfig executeProgOnlyAfterTransforms(boolean b) {
123124
return this;
124125
}
125126

127+
public TestConfig uncheckedDispatch() {
128+
return uncheckedDispatch(true);
129+
}
130+
131+
public TestConfig uncheckedDispatch(boolean b) {
132+
this.uncheckedDispatch = b;
133+
return this;
134+
}
135+
126136
TestConfig expectError(String expectedError) {
127137
this.expectedError = expectedError;
128138
return this;
@@ -186,6 +196,9 @@ private CompilationResult testScript() {
186196
if (withStdLib) {
187197
runArgs = runArgs.with("-lib", StdLib.getLib());
188198
}
199+
if (uncheckedDispatch) {
200+
runArgs = runArgs.with("-uncheckedDispatch");
201+
}
189202
if (runCompiletimeFunctions) {
190203
runArgs = runArgs.with("-runcompiletimefunctions");
191204
}

0 commit comments

Comments
 (0)