Skip to content

Commit c88e7e7

Browse files
committed
fixed #794 - could not use variablename 'main'
1 parent 40c3a70 commit c88e7e7

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imoptimizer/RestrictedCompressedNames.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public class RestrictedCompressedNames {
44
static String names[] = {"div", "val", "var", "mod", "use", "new", "for", "in", "it", "to",
55
"Sin", "Cos", "Tan", "Pow", "I2R", "R2I", "I2S", "R2S", "S2I", "S2R",
66
"And", "Or", "Not", "or", "and", "not", "if", "set", "loop", "endif",
7-
"endfunction", "endloop", "globals", "endglobals", "local", "call", "debug"};
7+
"endfunction", "endloop", "globals", "endglobals", "local", "call", "debug",
8+
"main", "config"};
89

910
public static boolean contains(String s) {
1011
for (String name : names) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@ public JassFunctionOrNative getJassFuncFor(ImFunction func) {
241241
prog.getNatives().add((JassNative) f);
242242
}
243243
} else {
244-
String name = getUniqueGlobalName(func.getName());
244+
String name = func.getName();
245+
// find a unique name, but keep special names 'main' and 'config'
246+
if (!name.equals("main") && !name.equals("config")) {
247+
name = getUniqueGlobalName(func.getName());
248+
}
245249
boolean isCompiletimeNative = func.hasFlag(FunctionFlagEnum.IS_COMPILETIME_NATIVE);
246250
f = JassFunction(name, JassSimpleVars(), "nothing", JassVars(), JassStatements(), isCompiletimeNative);
247251
if (!func.isBj() && !func.isExtern()) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,19 @@ public void testOverloadingConstructorClosureUse() {
810810
);
811811
}
812812

813+
@Test
814+
public void closureCaptureMain() {
815+
testAssertOkLines(true,
816+
"package test",
817+
"native testSuccess()",
818+
"interface SimpleFunc",
819+
" function apply(int x) returns int",
820+
"init",
821+
" int main = 4",
822+
" SimpleFunc f = (int x) -> x + main",
823+
" if f.apply(3) == 7",
824+
" testSuccess()"
825+
);
826+
}
827+
813828
}

0 commit comments

Comments
 (0)