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 @@ -311,8 +311,9 @@ private String makeNameSuffix() {

/**
* The scopes and calls which enclose the closure, innermost first. With {@code collapseRepeats},
* a name which directly follows an equal one is left out, so a closure passed to a call inside
* a closure passed to the same call is named after that call once rather than once per level.
* a name which directly follows an equal one is usually left out. Hot reload retains repeated
* call names so a nested closure and its enclosing closure cannot share an identity just because
* they were passed to the same builder.
* <p>
* A method call whose receiver holds the closure is not named: it comes later in a call chain such as
* {@code new Wave().modifier(x -> x + 1).hp(100)}, so adding or removing it must not rename the closure (which a
Expand All @@ -321,6 +322,8 @@ private String makeNameSuffix() {
private String makeNameSuffix(boolean collapseRepeats) {
StringBuilder sb = new StringBuilder();
String previous = null;
boolean previousCall = false;
boolean hotReload = tr.getRunArgs().isLuaHotReload();
Element child = null;
Element elem = this.e;
while (elem != null) {
Expand All @@ -333,16 +336,18 @@ private String makeNameSuffix(boolean collapseRepeats) {
name = ((AstElementWithFuncName) elem).getFuncNameId().getName();
}
if (name != null) {
if (!collapseRepeats || !name.equals(previous)) {
boolean call = elem instanceof FunctionCall;
if (!collapseRepeats || !name.equals(previous) || (hotReload && call && previousCall)) {
sb.append("_");
sb.append(name);
}
previous = name;
previousCall = call;
}
child = elem;
elem = elem.getParent();
}
if (tr.getRunArgs().isLuaHotReload()) sb.append(hotReloadCallSites());
if (hotReload) sb.append(hotReloadCallSites());
return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5000,6 +5000,32 @@ public void aClosureKeepsItsIdentityWhenALaterCallIsAddedToItsChain() {
.lines(chained(false, 1, 3));
}

@Test
public void editingABuilderClosureCanIntroduceAnotherCallToTheSameBuilderInsideIt() {
String[] baseline = {
"package Test", "native hotReload()", "native testSuccess()", "native testFail(string message)",
"interface Op", " function apply() returns int",
"function withParent(int parent, Op op) returns Op", " return op",
"Op saved", "Op inner",
"function build(int parent, int seed)",
" saved = withParent(parent) ->",
" return seed + parent",
"init",
" build(3, 4)",
" if saved.apply() != 7",
" testFail(\"baseline closure\")",
" hotReload()",
" if saved.apply() != 17",
" testFail(\"edited outer closure lost its live captures\")",
" testSuccess()"
};
String[] edited = baseline.clone();
edited[12] = " inner = withParent(parent) ->\n"
+ " return seed + parent + 10\n"
+ " return inner.apply()";
test().testLua(true).executeProg().luaHotReload(edited).lines(baseline);
}

/** Two closures of one chain stay apart: the edit of one reaches only that one. */
@Test
public void twoClosuresOfOneChainStayDistinct() {
Expand Down
Loading