Skip to content

Commit 71a934d

Browse files
committed
Keep generic handles on object index mapping
1 parent 1d25d92 commit 71a934d

3 files changed

Lines changed: 55 additions & 12 deletions

File tree

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ public static LuaExpr translate(ImFunctionCall e, LuaTranslator tr) {
113113
LuaExpr arg = e.getArguments().get(0).translateToLua(tr);
114114
ImType argumentType = e.getArguments().get(0).attrTyp();
115115
ImType resultType = e.attrTyp();
116-
if (tcFunc.equals("objectToIndex")
117-
|| (argumentType instanceof ImClassType && TypesHelper.isIntType(resultType))) {
116+
if (argumentType instanceof ImClassType && TypesHelper.isIntType(resultType)) {
118117
return LuaAst.LuaExprFunctionCall(tr.classToIndex, LuaAst.LuaExprlist(arg));
119-
} else if (tcFunc.equals("objectFromIndex")
120-
|| (TypesHelper.isIntType(argumentType) && resultType instanceof ImClassType)) {
118+
} else if (TypesHelper.isIntType(argumentType) && resultType instanceof ImClassType) {
121119
return LuaAst.LuaExprFunctionCall(tr.classFromIndex, LuaAst.LuaExprlist(arg));
120+
} else if (tcFunc.equals("objectToIndex")) {
121+
return LuaAst.LuaExprFunctionCall(tr.toIndexFunction, LuaAst.LuaExprlist(arg));
122+
} else if (tcFunc.equals("objectFromIndex")) {
123+
return LuaAst.LuaExprFunctionCall(tr.fromIndexFunction, LuaAst.LuaExprlist(arg));
122124
} else if (tcFunc.equals("stringToIndex")) {
123125
return LuaAst.LuaExprFunctionCall(tr.stringToIndexFunction, LuaAst.LuaExprlist(arg));
124126
} else if (tcFunc.equals("stringFromIndex")) {
@@ -485,14 +487,14 @@ public static LuaExpr translate(ImCast imCast, LuaTranslator tr) {
485487
if (TypesHelper.isStringType(imCast.getExpr().attrTyp())) {
486488
return LuaAst.LuaExprFunctionCall(tr.stringToIndexFunction, LuaAst.LuaExprlist(translated));
487489
}
488-
if (imCast.getExpr().attrTyp() instanceof ImClassType
489-
|| imCast.getExpr().attrTyp() instanceof ImAnyType) {
490+
if (imCast.getExpr().attrTyp() instanceof ImClassType) {
490491
return LuaAst.LuaExprFunctionCall(tr.classToIndex, LuaAst.LuaExprlist(translated));
491492
}
492493
return LuaAst.LuaExprFunctionCall(tr.toIndexFunction, LuaAst.LuaExprlist(translated));
493-
} else if (imCast.getToType() instanceof ImClassType
494-
|| imCast.getToType() instanceof ImAnyType) {
494+
} else if (imCast.getToType() instanceof ImClassType) {
495495
return LuaAst.LuaExprFunctionCall(tr.classFromIndex, LuaAst.LuaExprlist(translated));
496+
} else if (imCast.getToType() instanceof ImAnyType) {
497+
return LuaAst.LuaExprFunctionCall(tr.fromIndexFunction, LuaAst.LuaExprlist(translated));
496498
} else {
497499
return translated;
498500
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,7 @@ private boolean rewriteTypeCastingCompatFunction(ImFunction f, LuaFunction lf) {
741741
ImVar firstParam = f.getParameters().get(0);
742742
LuaExpr arg = LuaAst.LuaExprVarAccess(luaVar.getFor(firstParam));
743743

744-
if ("objectToIndex".equals(tcFunc)
745-
|| (firstParam.getType() instanceof ImClassType && TypesHelper.isIntType(f.getReturnType()))) {
744+
if (firstParam.getType() instanceof ImClassType && TypesHelper.isIntType(f.getReturnType())) {
746745
lf.getBody().clear();
747746
lf.getBody().add(LuaAst.LuaIf(
748747
LuaAst.LuaExprBinary(arg.copy(), LuaAst.LuaOpEquals(), LuaAst.LuaExprNull()),
@@ -751,8 +750,7 @@ private boolean rewriteTypeCastingCompatFunction(ImFunction f, LuaFunction lf) {
751750
lf.getBody().add(LuaAst.LuaReturn(arg));
752751
return true;
753752
}
754-
if ("objectFromIndex".equals(tcFunc)
755-
|| (TypesHelper.isIntType(firstParam.getType()) && f.getReturnType() instanceof ImClassType)) {
753+
if (TypesHelper.isIntType(firstParam.getType()) && f.getReturnType() instanceof ImClassType) {
756754
lf.getBody().clear();
757755
lf.getBody().add(LuaAst.LuaIf(
758756
LuaAst.LuaExprBinary(arg.copy(), LuaAst.LuaOpEquals(), LuaAst.LuaExprIntVal("0")),
@@ -762,6 +760,19 @@ private boolean rewriteTypeCastingCompatFunction(ImFunction f, LuaFunction lf) {
762760
return true;
763761
}
764762

763+
if ("objectToIndex".equals(tcFunc)) {
764+
lf.getBody().clear();
765+
lf.getBody().add(LuaAst.LuaReturn(
766+
LuaAst.LuaExprFunctionCall(toIndexFunction, LuaAst.LuaExprlist(arg))));
767+
return true;
768+
}
769+
if ("objectFromIndex".equals(tcFunc)) {
770+
lf.getBody().clear();
771+
lf.getBody().add(LuaAst.LuaReturn(
772+
LuaAst.LuaExprFunctionCall(fromIndexFunction, LuaAst.LuaExprlist(arg))));
773+
return true;
774+
}
775+
765776
if ("stringToIndex".equals(tcFunc)) {
766777
lf.getBody().clear();
767778
lf.getBody().add(LuaAst.LuaReturn(LuaAst.LuaExprFunctionCall(stringToIndexFunction, LuaAst.LuaExprlist(arg))));

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,36 @@ public void classInstancesUseRecycledIntegerIdsAndStaticFieldStorage() throws IO
110110
compiled.contains("Base_reference_storage[object] = nil"));
111111
}
112112

113+
@Test
114+
public void legacyGenericHandleCastsUseObjectIndexMap() throws IOException {
115+
test().testLua(true).executeProg().lines(
116+
"type timer extends handle",
117+
"package Test",
118+
"native testSuccess()",
119+
"native CreateTimer() returns timer",
120+
"function timerToIndex(timer value) returns int",
121+
" return 0",
122+
"function timerFromIndex(int value) returns timer",
123+
" return null",
124+
"function toIndex<T>(T value) returns int",
125+
" return value castTo int",
126+
"init",
127+
" let value = CreateTimer()",
128+
" let first = toIndex(value)",
129+
" let second = toIndex(value)",
130+
" if first > 0 and first == second",
131+
" testSuccess()"
132+
);
133+
134+
String compiled = compiledLua("legacyGenericHandleCastsUseObjectIndexMap");
135+
assertTrue(java.util.regex.Pattern.compile(
136+
"function toIndex\\((\\w+)\\)\\s*\\R\\s*return __wurst_objectToIndex\\(\\1\\)")
137+
.matcher(compiled).find());
138+
assertFalse(java.util.regex.Pattern.compile(
139+
"function toIndex\\((\\w+)\\)\\s*\\R\\s*return __wurst_classToIndex\\(\\1\\)")
140+
.matcher(compiled).find());
141+
}
142+
113143
@Test
114144
public void compiletimeGenericArrayReplayLeavesAreSplit() {
115145
String compiled = compileLuaWithRunArgs(

0 commit comments

Comments
 (0)