Skip to content

Commit d6b28a2

Browse files
committed
fix build
1 parent c367623 commit d6b28a2

9 files changed

Lines changed: 349 additions & 33 deletions

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ExprTranslation {
1212

1313
public static final String TYPE_ID = "__typeId__";
1414
public static final String WURST_SUPERTYPES = "__wurst_supertypes";
15+
private static final String WURST_ABORT_THREAD_SENTINEL = "__wurst_abort_thread";
1516

1617
public static LuaExpr translate(ImAlloc e, LuaTranslator tr) {
1718
ImClass c = e.getClazz().getClassDef();
@@ -51,7 +52,7 @@ public static LuaExpr translate(ImFuncRef e, LuaTranslator tr) {
5152
LuaAst.LuaExprFunctionCall(tr.luaFunc.getFor(e.getFunc()), LuaAst.LuaExprlist(LuaAst.LuaExprVarAccess(tempDots)))))
5253
),
5354
// LuaAst.LuaLiteral("function(err) " + errorFuncName(tr) + "(tostring(err)) end")
54-
LuaAst.LuaLiteral("function(err) xpcall(function() " + callErrorFunc(tr, "tostring(err)") + " end, function(err2) BJDebugMsg(\"error reporting error: \" .. tostring(err2)) BJDebugMsg(\"while reporting: \" .. tostring(err)) end) end")
55+
LuaAst.LuaLiteral("function(err) if err == \"" + WURST_ABORT_THREAD_SENTINEL + "\" then return end xpcall(function() " + callErrorFunc(tr, "tostring(err)") + " end, function(err2) if err2 == \"" + WURST_ABORT_THREAD_SENTINEL + "\" then return end BJDebugMsg(\"error reporting error: \" .. tostring(err2)) BJDebugMsg(\"while reporting: \" .. tostring(err)) end) end")
5556
// unfortunately BJDebugMsg(debug.traceback()) is not working
5657
)
5758
),
@@ -73,6 +74,12 @@ private static String callErrorFunc(LuaTranslator tr, String msg) {
7374

7475
public static LuaExpr translate(ImFunctionCall e, LuaTranslator tr) {
7576
LuaFunction f = tr.luaFunc.getFor(e.getFunc());
77+
if ("I2S".equals(f.getName()) && isIntentionalThreadAbortCall(e)) {
78+
return LuaAst.LuaExprFunctionCallByName("error", LuaAst.LuaExprlist(
79+
LuaAst.LuaExprStringVal(WURST_ABORT_THREAD_SENTINEL),
80+
LuaAst.LuaExprIntVal("0")
81+
));
82+
}
7683
if (f.getName().equals(ImTranslator.$DEBUG_PRINT)) {
7784
f.setName("BJDebugMsg");
7885
} else if (f.getName().equals("I2S")) {
@@ -81,6 +88,27 @@ public static LuaExpr translate(ImFunctionCall e, LuaTranslator tr) {
8188
return LuaAst.LuaExprFunctionCall(f, tr.translateExprList(e.getArguments()));
8289
}
8390

91+
private static boolean isIntentionalThreadAbortCall(ImFunctionCall e) {
92+
if (e.getArguments().size() != 1) {
93+
return false;
94+
}
95+
ImExpr arg = e.getArguments().get(0);
96+
if (!(arg instanceof ImOperatorCall)) {
97+
return false;
98+
}
99+
ImOperatorCall op = (ImOperatorCall) arg;
100+
if (op.getOp() != WurstOperator.DIV_INT) {
101+
return false;
102+
}
103+
if (op.getArguments().size() != 2) {
104+
return false;
105+
}
106+
ImExpr left = op.getArguments().get(0);
107+
ImExpr right = op.getArguments().get(1);
108+
return (left instanceof ImIntVal && ((ImIntVal) left).getValI() == 1)
109+
&& (right instanceof ImIntVal && ((ImIntVal) right).getValI() == 0);
110+
}
111+
84112
public static LuaExpr translate(ImInstanceof e, LuaTranslator tr) {
85113
return
86114
LuaAst.LuaExprFunctionCall(tr.instanceOfFunction, LuaAst.LuaExprlist(

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/lua/translation/LuaTranslator.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,6 @@ protected String uniqueName(String name) {
191191
public LuaCompilationUnit translate() {
192192
collectPredefinedNames();
193193

194-
RemoveGarbage.removeGarbage(prog);
195-
prog.flatten(imTr);
196-
197-
198194
normalizeMethodNames();
199195
normalizeFieldNames();
200196

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package tests.wurstscript.tests;
2+
3+
import org.testng.annotations.Test;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
import java.nio.charset.StandardCharsets;
10+
11+
import static org.testng.Assert.assertTrue;
12+
import static org.testng.Assert.fail;
13+
14+
public class LuaSyntaxCheckTests {
15+
16+
@Test
17+
public void luacSyntaxErrorsAreDetected() throws Exception {
18+
WurstScriptTest helper = new WurstScriptTest();
19+
String luacExecutable = invokeGetLuacExecutable(helper);
20+
21+
File invalidLua = File.createTempFile("wurst-invalid-lua-", ".lua");
22+
invalidLua.deleteOnExit();
23+
java.nio.file.Files.writeString(invalidLua.toPath(), "function broken(\n", StandardCharsets.UTF_8);
24+
25+
Method checkLuaSyntax = WurstScriptTest.class.getDeclaredMethod("checkLuaSyntax", String.class, File.class);
26+
checkLuaSyntax.setAccessible(true);
27+
try {
28+
checkLuaSyntax.invoke(helper, luacExecutable, invalidLua);
29+
fail("Expected syntax check to fail for invalid Lua source.");
30+
} catch (InvocationTargetException ite) {
31+
Throwable cause = ite.getCause();
32+
assertTrue(cause instanceof IOException,
33+
"Expected IOException but got: " + (cause == null ? "null" : cause.getClass().getName()));
34+
assertTrue(cause.getMessage() != null && cause.getMessage().contains("Lua syntax check failed"),
35+
"Expected syntax check failure message, but got: " + cause.getMessage());
36+
}
37+
}
38+
39+
private String invokeGetLuacExecutable(WurstScriptTest helper) throws Exception {
40+
Method getLuacExecutable = WurstScriptTest.class.getDeclaredMethod("getLuacExecutable");
41+
getLuacExecutable.setAccessible(true);
42+
Object result = getLuacExecutable.invoke(helper);
43+
return (String) result;
44+
}
45+
}
46+

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,34 @@ public void stdLibDoesNotEmitWar3HashtableNatives() throws IOException {
467467
assertDoesNotContainRegex(compiled, "\\bFlushChildHashtable\\(");
468468
}
469469

470+
@Test
471+
public void i2sDivisionByZeroCrashTrapUsesAbortSentinelInLua() throws IOException {
472+
test().testLua(true).withStdLib().lines(
473+
"package Test",
474+
"init",
475+
" skip"
476+
);
477+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_i2sDivisionByZeroCrashTrapUsesAbortSentinelInLua.lua"), Charsets.UTF_8);
478+
assertDoesNotContainRegex(compiled, "tostring\\s*\\(\\s*1\\s*//\\s*0\\s*\\)");
479+
assertDoesNotContainRegex(compiled, "tostring\\s*\\(\\s*1\\s*/\\s*0\\s*\\)");
480+
assertDoesNotContainRegex(compiled, "I2S\\s*\\(\\s*1\\s*/\\s*0\\s*\\)");
481+
assertDoesNotContainRegex(compiled, "I2S\\s*\\(\\s*1\\s*//\\s*0\\s*\\)");
482+
assertTrue(compiled.contains("__wurst_abort_thread"));
483+
assertDoesNotContainRegex(compiled, "error\\s*\\(\\s*\"[^\"]*divide by zero[^\"]*\"");
484+
}
485+
486+
@Test
487+
public void luaErrorWrapperIgnoresAbortSentinel() throws IOException {
488+
test().testLua(true).withStdLib().lines(
489+
"package Test",
490+
"function f()",
491+
" skip",
492+
"init",
493+
" f()"
494+
);
495+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_luaErrorWrapperIgnoresAbortSentinel.lua"), Charsets.UTF_8);
496+
assertTrue(compiled.contains("if err == \"__wurst_abort_thread\" then return end"));
497+
assertTrue(compiled.contains("if err2 == \"__wurst_abort_thread\" then return end"));
498+
}
499+
470500
}

0 commit comments

Comments
 (0)