Skip to content

Commit e031ed8

Browse files
committed
lua robustness
1 parent 81ae1e9 commit e031ed8

4 files changed

Lines changed: 48 additions & 22 deletions

File tree

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -775,9 +775,12 @@ private boolean rewriteTypeCastingCompatFunction(ImFunction f, LuaFunction lf) {
775775
if (f.getParameters().isEmpty()) {
776776
return false;
777777
}
778-
String tcFunc = f.getName();
779-
ImVar p = f.getParameters().get(0);
780-
LuaExpr arg = LuaAst.LuaExprVarAccess(luaVar.getFor(p));
778+
String tcFunc = getTypeCastingFunctionName(f);
779+
if (tcFunc == null) {
780+
return false;
781+
}
782+
ImVar firstParam = f.getParameters().get(0);
783+
LuaExpr arg = LuaAst.LuaExprVarAccess(luaVar.getFor(firstParam));
781784

782785
if ("stringToIndex".equals(tcFunc)) {
783786
lf.getBody().clear();
@@ -805,17 +808,6 @@ private boolean rewriteTypeCastingCompatFunction(ImFunction f, LuaFunction lf) {
805808
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(fromIndexFunction, LuaAst.LuaExprlist(arg))));
806809
return true;
807810
}
808-
// Final fallback for transformed/copied function names that may lose trace info:
809-
if (tcFunc.endsWith("ToIndex")) {
810-
lf.getBody().clear();
811-
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(toIndexFunction, LuaAst.LuaExprlist(arg))));
812-
return true;
813-
}
814-
if (tcFunc.endsWith("FromIndex")) {
815-
lf.getBody().clear();
816-
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(fromIndexFunction, LuaAst.LuaExprlist(arg))));
817-
return true;
818-
}
819811
return false;
820812
}
821813

@@ -1100,13 +1092,6 @@ public String getTypeCastingFunctionName(ImFunction f) {
11001092
return fd.getName();
11011093
}
11021094
}
1103-
// Fallback: transformed/copied IM functions may lose package trace metadata.
1104-
// Keep Lua behavior stable by routing canonical *ToIndex/*FromIndex names anyway.
1105-
String name = f.getName();
1106-
if ("stringToIndex".equals(name) || "stringFromIndex".equals(name)
1107-
|| name.endsWith("ToIndex") || name.endsWith("FromIndex")) {
1108-
return name;
1109-
}
11101095
return null;
11111096
}
11121097
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/WurstValidator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,10 @@ private boolean refersToSameVar(OptExpr a, OptExpr b) {
12291229
NameRef vb = (NameRef) b;
12301230
NameLink nla = va.attrNameLink();
12311231
NameLink nlb = vb.attrNameLink();
1232-
if (nla != null && nlb != null && nla.getDef() == nlb.getDef()
1232+
if (nla != null && nlb != null
1233+
&& !(nla instanceof OtherLink)
1234+
&& !(nlb instanceof OtherLink)
1235+
&& nla.getDef() == nlb.getDef()
12331236
&& refersToSameVar(va.attrImplicitParameter(), vb.attrImplicitParameter())) {
12341237
if (va instanceof AstElementWithIndexes && vb instanceof AstElementWithIndexes) {
12351238
AstElementWithIndexes vai = (AstElementWithIndexes) va;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ public void closure_selfReference_withIt_isReadOnly() {
124124
" end");
125125
}
126126

127+
@Test
128+
public void closure_selfReference_withIt_assignmentNoCrash() {
129+
testAssertOkLines(false,
130+
"package test",
131+
"interface IntFunc",
132+
" function apply(int x) returns int",
133+
"init",
134+
" IntFunc f = (int n) -> begin",
135+
" IntFunc x = it",
136+
" x = it",
137+
" return n",
138+
" end");
139+
}
140+
127141
@Test
128142
public void closure_begin_end1() {
129143
testAssertOkLines(true,

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,30 @@ public void objectIndexFunctionsDoNotCollideWithUserFunctions() throws IOExcepti
439439
assertFunctionBodyContains(compiled, "testClass", "__wurst_objectFromIndex", true);
440440
}
441441

442+
@Test
443+
public void tupleReturningMethodNamedFromIndexIsNotTypecastWrapper() throws IOException {
444+
test().testLua(true).lines(
445+
"package TupleFromIndexRepro",
446+
"public tuple vec2(real x, real y)",
447+
"public tuple searchResult(boolean found, vec2 pos)",
448+
"public constant ZERO2 = vec2(0., 0.)",
449+
"public class A",
450+
" function next() returns searchResult",
451+
" return nextFromIndex(1)",
452+
" function nextFromIndex(int startIndex) returns searchResult",
453+
" return searchResult(false, ZERO2)",
454+
"public class B extends A",
455+
"init",
456+
" let b = new B()",
457+
" let r = b.next()",
458+
" if r.found",
459+
" skip"
460+
);
461+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_tupleReturningMethodNamedFromIndexIsNotTypecastWrapper.lua"), Charsets.UTF_8);
462+
assertContainsRegex(compiled, "function\\s+[^\\n]*nextFromIndex[^\\n]*\\(");
463+
assertDoesNotContainRegex(compiled, "return\\s+__wurst_objectFromIndex\\s*\\(\\s*this\\s*\\)");
464+
}
465+
442466
@Test
443467
public void oldGenericsCastingDoesNotUseGetHandleId() throws IOException {
444468
test().testLua(true).withStdLib().lines(

0 commit comments

Comments
 (0)