Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public int optimize(ImTranslator trans) {
} else {
for (ImVarWrite write : v.attrWrites()) {
ImFunction func = write.getNearestFunc();
if (isInInitGlobals(func)) {
if (isInInitGlobals(trans, func)) {
right = write.getRight();
obs = write;
break;
Expand All @@ -96,7 +96,7 @@ public int optimize(ImTranslator trans) {
List<ImVarWrite> initWrites = new ArrayList<>();
for (ImVarWrite imVarWrite : v.attrWrites()) {
ImFunction nearestFunc = imVarWrite.getNearestFunc();
if (isInInitGlobals(nearestFunc)) {
if (isInInitGlobals(trans, nearestFunc)) {
initWrites.add(imVarWrite);
}
}
Expand Down Expand Up @@ -171,8 +171,9 @@ public String getName() {
}


private static boolean isInInitGlobals(ImFunction func) {
return func != null && func.getName().equals("initGlobals");
/** The globals initialiser of the compiler, not a function which happens to have its name. */
private static boolean isInInitGlobals(ImTranslator trans, ImFunction func) {
return func != null && func == trans.getGlobalInitFunc();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,48 @@ public void test_inline_globals() {
"endpackage");
}

/**
* A function of the user's which happens to be called initGlobals is not the globals initialiser: its write of
* the default value is a reset, which must stay.
*/
@Test
public void globalsInlinerDoesNotTakeAJassInitGlobalsForTheInitialiser() {
test().executeProg().compilationUnits(
compilationUnit("input.j",
"globals",
" integer mode",
"endglobals",
"function initGlobals takes nothing returns nothing",
" set mode = 0",
"endfunction"),
compilationUnit("test.wurst",
"package test",
"native testSuccess()",
"init",
" mode = 5",
" initGlobals()",
" if mode == 0",
" testSuccess()",
"endpackage"));
}

/** The same for a Wurst function of that name. */
@Test
public void globalsInlinerDoesNotTakeAWurstInitGlobalsForTheInitialiser() {
test().executeProg().lines(
"package test",
"native testSuccess()",
"int mode",
"function initGlobals()",
" mode = 0",
"init",
" mode = 5",
" initGlobals()",
" if mode == 0",
" testSuccess()",
"endpackage");
}

@Test
public void globalsInlinerDoesNotRemoveNonInitDefaultWrite() {
test().executeProg().lines(
Expand Down
Loading