Skip to content

Commit f578b88

Browse files
committed
Scope name preservation to top-level functions
1 parent c14989c commit f578b88

4 files changed

Lines changed: 1 addition & 74 deletions

File tree

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,6 @@ private void collectPredefinedNames() {
502502
}
503503
}
504504

505-
for (ImClass clazz : prog.getClasses()) {
506-
for (ImFunction function : clazz.getFunctions()) {
507-
if (NamePreservation.isPreserved(function)) {
508-
LuaFunction luaFunction = luaFunc.getFor(function);
509-
usedNames.add(luaFunction.getName());
510-
}
511-
}
512-
}
513-
514505
for (ImVar global : prog.getGlobals()) {
515506
if (global.getIsBJ()) {
516507
setNameFromTrace(global);
@@ -1061,9 +1052,7 @@ private void translateClass(ImClass c) {
10611052
// translate functions
10621053
for (ImFunction f : c.getFunctions()) {
10631054
translateFunc(f);
1064-
if (!NamePreservation.isPreserved(f)) {
1065-
luaFunc.getFor(f).setName(uniqueName(c.getName() + "_" + f.getName()));
1066-
}
1055+
luaFunc.getFor(f).setName(uniqueName(c.getName() + "_" + f.getName()));
10671056
}
10681057

10691058
createClassInitFunction(c, classVar, initMethod);

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,6 @@ private static void visitFunction(ImFunction f, Used used) {
222222
return;
223223
}
224224
used.addFunction(f);
225-
if (f.getParent() != null && f.getParent().getParent() instanceof ImClass owner) {
226-
// A preserved static method has no receiver type to retain its owner. Keep the class
227-
// because Lua emits class methods together with their class table.
228-
visitClass(owner, used, false);
229-
}
230-
231225
visitType(f.getReturnType(), used);
232226
f.accept(new Element.DefaultVisitor() {
233227
@Override

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ public void validate(Collection<CompilationUnit> toCheck) {
8484
visitedFunctions = 0;
8585
heavyFunctions.clear();
8686
heavyBlocks.clear();
87-
if (runtimeNameIndex != null) {
88-
runtimeNameIndex.clearSyntheticMarkers();
89-
}
9087
trveWrapperFuncs.clear();
9188
wrapperCalls.clear();
9289
NamePreservation.clearSyntheticMarkers(prog);

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -571,59 +571,6 @@ public void trvePreservesLoweredTupleComponent() throws IOException {
571571
"Expected TRVE to preserve the lowered tuple component.\n" + output);
572572
}
573573

574-
@Test
575-
public void preserveNameAnnotationKeepsClassFunctionNameInLua() throws IOException {
576-
test().testLua(true).luaOnly(true).executeProg(false).lines(
577-
"package test",
578-
" class ExternalApi",
579-
" @preserveName function callback()",
580-
" skip",
581-
"endpackage");
582-
583-
String output = Files.toString(
584-
new File("./test-output/lua/OptimizerTests_preserveNameAnnotationKeepsClassFunctionNameInLua.lua"),
585-
Charsets.UTF_8);
586-
assertTrue(output.contains("function ExternalApi_callback"),
587-
"Expected a preserved class function to keep its emitted name.\n" + output);
588-
}
589-
590-
@Test
591-
public void preserveNameAnnotationKeepsStaticClassFunctionReachableInLua() throws IOException {
592-
test().testLua(true).luaOnly(true).executeProg(false).lines(
593-
"package test",
594-
" class ExternalApi",
595-
" @preserveName static function callback()",
596-
" skip",
597-
"endpackage");
598-
599-
String output = Files.toString(
600-
new File("./test-output/lua/OptimizerTests_preserveNameAnnotationKeepsStaticClassFunctionReachableInLua.lua"),
601-
Charsets.UTF_8);
602-
assertTrue(output.contains("function callback("),
603-
"Expected a preserved static class function to keep its emitted name.\n" + output);
604-
}
605-
606-
@Test
607-
public void preservedClassFunctionReservesItsLuaNameBeforeClassVariables() throws IOException {
608-
test().testLua(true).luaOnly(true).executeProg(false).lines(
609-
"package test",
610-
" class Foo",
611-
" @preserveName function bar()",
612-
" skip",
613-
" class Foo_bar",
614-
" init",
615-
" new Foo_bar",
616-
"endpackage");
617-
618-
String output = Files.toString(
619-
new File("./test-output/lua/OptimizerTests_preservedClassFunctionReservesItsLuaNameBeforeClassVariables.lua"),
620-
Charsets.UTF_8);
621-
assertTrue(output.contains("function Foo_bar("),
622-
"Expected the preserved class function to keep its Lua name.\n" + output);
623-
assertTrue(output.contains("Foo_bar1 = ({})"),
624-
"Expected the colliding class variable to be uniqued around the preserved function.\n" + output);
625-
}
626-
627574
@Test
628575
public void test_tempVarRemover() throws IOException {
629576
test().lines(

0 commit comments

Comments
 (0)