Skip to content

Commit 4d8881a

Browse files
committed
cleanup
1 parent 63a1799 commit 4d8881a

2 files changed

Lines changed: 62 additions & 22 deletions

File tree

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.peeeq.wurstscript.translation.lua.translation;
22

33
import de.peeeq.datastructures.UnionFind;
4-
import de.peeeq.wurstscript.WLogger;
54
import de.peeeq.wurstscript.ast.Element;
65
import de.peeeq.wurstscript.ast.FuncDef;
76
import de.peeeq.wurstscript.ast.NameDef;
@@ -654,26 +653,17 @@ private boolean rewriteTypeCastingCompatFunction(ImFunction f, LuaFunction lf) {
654653
return false;
655654
}
656655
String tcFunc = f.getName();
657-
if (WLogger.isTraceEnabled()) {
658-
WLogger.trace("[LUA-TYPECAST] inspect function " + tcFunc + " params=" + f.getParameters().size());
659-
}
660656
ImVar p = f.getParameters().get(0);
661657
LuaExpr arg = LuaAst.LuaExprVarAccess(luaVar.getFor(p));
662658

663659
if ("stringToIndex".equals(tcFunc)) {
664660
lf.getBody().clear();
665661
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(stringToIndexFunction, LuaAst.LuaExprlist(arg))));
666-
if (WLogger.isTraceEnabled()) {
667-
WLogger.trace("[LUA-TYPECAST] rewrote body of " + tcFunc + " -> __wurst_stringToIndex");
668-
}
669662
return true;
670663
}
671664
if ("stringFromIndex".equals(tcFunc)) {
672665
lf.getBody().clear();
673666
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(stringFromIndexFunction, LuaAst.LuaExprlist(arg))));
674-
if (WLogger.isTraceEnabled()) {
675-
WLogger.trace("[LUA-TYPECAST] rewrote body of " + tcFunc + " -> __wurst_stringFromIndex");
676-
}
677667
return true;
678668
}
679669
// Keep semantic conversions for primitive/index-domain helpers intact.
@@ -685,34 +675,22 @@ private boolean rewriteTypeCastingCompatFunction(ImFunction f, LuaFunction lf) {
685675
if (LUA_HANDLE_TO_INDEX.contains(tcFunc)) {
686676
lf.getBody().clear();
687677
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(toIndexFunction, LuaAst.LuaExprlist(arg))));
688-
if (WLogger.isTraceEnabled()) {
689-
WLogger.trace("[LUA-TYPECAST] rewrote body of " + tcFunc + " -> __wurst_objectToIndex");
690-
}
691678
return true;
692679
}
693680
if (LUA_HANDLE_FROM_INDEX.contains(tcFunc)) {
694681
lf.getBody().clear();
695682
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(fromIndexFunction, LuaAst.LuaExprlist(arg))));
696-
if (WLogger.isTraceEnabled()) {
697-
WLogger.trace("[LUA-TYPECAST] rewrote body of " + tcFunc + " -> __wurst_objectFromIndex");
698-
}
699683
return true;
700684
}
701685
// Final fallback for transformed/copied function names that may lose trace info:
702686
if (tcFunc.endsWith("ToIndex")) {
703687
lf.getBody().clear();
704688
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(toIndexFunction, LuaAst.LuaExprlist(arg))));
705-
if (WLogger.isTraceEnabled()) {
706-
WLogger.trace("[LUA-TYPECAST] rewrote body of " + tcFunc + " via suffix -> __wurst_objectToIndex");
707-
}
708689
return true;
709690
}
710691
if (tcFunc.endsWith("FromIndex")) {
711692
lf.getBody().clear();
712693
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(fromIndexFunction, LuaAst.LuaExprlist(arg))));
713-
if (WLogger.isTraceEnabled()) {
714-
WLogger.trace("[LUA-TYPECAST] rewrote body of " + tcFunc + " via suffix -> __wurst_objectFromIndex");
715-
}
716694
return true;
717695
}
718696
return false;

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,66 @@ public void luaFramehandleFromIndexDoesNotUseFogstateHashtablePath() throws IOEx
262262
assertFalse(unitFromIndexSavesFog.matcher(compiled).find());
263263
}
264264

265+
@Test
266+
public void luaTypeCastingCompatWrappersUseLuaHelpers() throws IOException {
267+
test().testLua(true).withStdLib().lines(
268+
"package Test",
269+
"import TypeCasting",
270+
"init",
271+
" let u = unitFromIndex(1)",
272+
" let ui = unitToIndex(u)",
273+
" let w = widgetFromIndex(2)",
274+
" let wi = widgetToIndex(w)",
275+
" let fh = framehandleFromIndex(3)",
276+
" let fhi = framehandleToIndex(fh)",
277+
" let k = oskeytypeFromIndex(4)",
278+
" let ki = oskeytypeToIndex(k)",
279+
" let s = stringFromIndex(stringToIndex(\"abc\"))",
280+
" if ui + wi + fhi + ki >= 0 and s.length() >= 0",
281+
" skip"
282+
);
283+
String compiled = Files.toString(new File("test-output/lua/LuaTypecastingTests_luaTypeCastingCompatWrappersUseLuaHelpers.lua"), Charsets.UTF_8);
284+
285+
assertTrue(Pattern.compile(
286+
"function\\s+unitFromIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_objectFromIndex\\(",
287+
Pattern.MULTILINE
288+
).matcher(compiled).find());
289+
assertTrue(Pattern.compile(
290+
"function\\s+unitToIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_objectToIndex\\(",
291+
Pattern.MULTILINE
292+
).matcher(compiled).find());
293+
assertTrue(Pattern.compile(
294+
"function\\s+widgetFromIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_objectFromIndex\\(",
295+
Pattern.MULTILINE
296+
).matcher(compiled).find());
297+
assertTrue(Pattern.compile(
298+
"function\\s+widgetToIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_objectToIndex\\(",
299+
Pattern.MULTILINE
300+
).matcher(compiled).find());
301+
assertTrue(Pattern.compile(
302+
"function\\s+framehandleFromIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_objectFromIndex\\(",
303+
Pattern.MULTILINE
304+
).matcher(compiled).find());
305+
assertTrue(Pattern.compile(
306+
"function\\s+framehandleToIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_objectToIndex\\(",
307+
Pattern.MULTILINE
308+
).matcher(compiled).find());
309+
assertTrue(Pattern.compile(
310+
"function\\s+oskeytypeFromIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_objectFromIndex\\(",
311+
Pattern.MULTILINE
312+
).matcher(compiled).find());
313+
assertTrue(Pattern.compile(
314+
"function\\s+oskeytypeToIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_objectToIndex\\(",
315+
Pattern.MULTILINE
316+
).matcher(compiled).find());
317+
assertTrue(Pattern.compile(
318+
"function\\s+stringToIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_stringToIndex\\(",
319+
Pattern.MULTILINE
320+
).matcher(compiled).find());
321+
assertTrue(Pattern.compile(
322+
"function\\s+stringFromIndex\\([^)]*\\)[\\s\\S]*?return\\s+__wurst_stringFromIndex\\(",
323+
Pattern.MULTILINE
324+
).matcher(compiled).find());
325+
}
326+
265327
}

0 commit comments

Comments
 (0)