Skip to content

Commit 5a6cef8

Browse files
authored
Fix jass string null translation (#1110)
1 parent 5535ed8 commit 5a6cef8

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtojass/ExprTranslation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static JassExpr translate(ImNull e, ImToJassTranslator translator) {
6060
} else if (typename.equals("boolean") || typename.equals("bool")) {
6161
return JassExprBoolVal(false);
6262
} else if (typename.equals("string")) {
63-
return JassExprStringVal("");
63+
return JassExprNull();
6464
}
6565
}
6666

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Collections;
1313
import java.util.Map;
1414

15+
import static org.testng.AssertJUnit.assertFalse;
1516
import static org.testng.AssertJUnit.assertTrue;
1617

1718
public class JurstTests extends WurstScriptTest {
@@ -340,6 +341,45 @@ public void testKeepTRVEHooked() throws IOException {
340341
assertTrue(output.contains("real myVar"));
341342
}
342343

344+
@Test
345+
public void testStringNullCheckStaysNull() throws IOException {
346+
String jassCode = Utils.string(
347+
"globals",
348+
" string array s__SaveCodes",
349+
" integer PID",
350+
" integer unitSaveID",
351+
"endglobals",
352+
"",
353+
"function main takes nothing returns nothing",
354+
" if s__SaveCodes[PID * 19 + unitSaveID] != null then",
355+
" call BJDebugMsg(\"\")", // could be anything
356+
" endif",
357+
"endfunction"
358+
);
359+
360+
String jurstCode = Utils.string(
361+
"package test",
362+
"endpackage"
363+
);
364+
365+
testJurstWithJass(false, true, jassCode, jurstCode);
366+
367+
File out = new File("./test-output/JurstJassTest_inlopt.j");
368+
String output = com.google.common.io.Files.toString(out, Charsets.UTF_8);
369+
370+
// Ensure the null check survives as a null check
371+
assertTrue(
372+
"Expected string null check to stay != null",
373+
output.contains("s__SaveCodes[PID * 19 + unitSaveID] != null")
374+
);
375+
376+
// And make sure we did NOT silently change it to empty string
377+
assertFalse(
378+
"String null check must not become != \"\"",
379+
output.contains("s__SaveCodes[PID * 19 + unitSaveID] != \"\"")
380+
);
381+
}
382+
343383
private void testJurstWithJass(boolean executeProg, boolean withStdLib, String jass, String jurst) {
344384
Map<String, String> inputs = ImmutableMap.of(
345385
"example.j", jass,

0 commit comments

Comments
 (0)