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 @@ -276,6 +276,11 @@ private String hotReloadOwnerIdentity(TranslatedToImFunction callable) {
StringBuilder identity = new StringBuilder(callable instanceof ConstructorDef ? "constructor"
: callable instanceof OnDestroyDef ? "destructor" : callable instanceof ExtensionFuncDef ? "extension"
: callable instanceof InitBlock ? "init" : "function");
// The function's own name: a function is no named scope, so without it two functions of one signature in one
// package or class gave their closures one identity, told apart by their order across both functions.
if (callable instanceof FunctionDefinition function) {
identity.append("named").append(function.getName().length()).append(':').append(function.getName());
}
for (Element scope = callable; scope != null; scope = scope.getParent()) {
if (scope instanceof NamedScope named) {
String segment = named.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import de.peeeq.wurstscript.jassIm.ImTypeVarRef;
import de.peeeq.wurstscript.jassIm.ImTypeVars;
import de.peeeq.wurstscript.jassIm.ImVars;
import de.peeeq.wurstscript.translation.lua.translation.LuaHotReload;
import de.peeeq.wurstscript.translation.lua.translation.LuaIdentifiers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import org.eclipse.jdt.annotation.Nullable;

import java.util.LinkedHashMap;
Expand Down Expand Up @@ -132,13 +135,22 @@ private static void normalizeMethodNames(ImProg prog, List<ImMethod> allMethods,
group.sort(Comparator.comparing(LuaDispatchPreparation::methodSortKey));
// The name is about to become a Lua table key. Sanitising before uniquing means two
// names that only differed in characters Lua has no place for still get one slot each.
String name = uniqueName(LuaIdentifiers.toIdentifier(group.get(0).getName()), usedNames);
// Numbered in the order of the groups, a group added before others of its name (an interface with a
// `run` sorting before another's) renames their slots, which the objects a running map has were bound
// to; so does a member added which sorts first. A hot-reload build names a group after its root instead,
// and the root's key, as it names declarations.
ImMethod namedAfter = tr.getRunArgs().isLuaHotReload() ? rootOf(group) : group.get(0);
String base = LuaIdentifiers.toIdentifier(namedAfter.getName());
if (tr.getRunArgs().isLuaHotReload()) {
base = base + "_" + LuaHotReload.identityHash(rootKey(namedAfter));
}
String name = uniqueName(base, usedNames);
// The group is named after one member, whose name is that member's own class and then
// the method, so stripping the class here is the one place the boundary is known rather
// than guessed at. Every member shares the segment, including members of other classes
// whose own name appears nowhere in it - which is why no method can work this out for
// itself afterwards.
String segment = segmentOf(name, group.get(0));
String segment = segmentOf(name, namedAfter);
for (int j = 0; j < group.size(); j++) {
ImMethod method = group.get(j);
method.setName(name);
Expand Down Expand Up @@ -181,6 +193,35 @@ private static void collectPredefinedNames(ImProg prog, Set<String> usedNames) {
}
}

/**
* What a hot-reload build names a dispatch group after: its root, the first by key of the members no other member
* overrides (an interface's method, a base class's). An implementation added to the group leaves it as it is; the
* group's first member by name changes when the new one's class sorts before the others.
*/
private static ImMethod rootOf(List<ImMethod> group) {
Set<ImMethod> overriding = Collections.newSetFromMap(new IdentityHashMap<>());
for (ImMethod method : group) {
overriding.addAll(method.getSubMethods());
}
ImMethod root = null;
String rootKey = null;
for (ImMethod method : group) {
if (!overriding.contains(method)) {
String key = rootKey(method);
if (rootKey == null || key.compareTo(rootKey) < 0) {
root = method;
rootKey = key;
}
}
}
return root != null ? root : group.get(0);
}

private static String rootKey(ImMethod root) {
ImClass owner = root.attrClass();
return methodSortKey(root) + "|" + dispatchSignatureKey(root) + OWNERS + (owner == null ? "" : packageOf(owner.getTrace()));
}

private static String uniqueName(String name, Set<String> usedNames) {
int i = 0;
String result = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ static String methodId(ImMethod m) {
* program of 100,000 declarations) are numbered by {@link LuaTranslator#uniqueName}; a payload whose names then mean
* other declarations is rejected by the binding check.
*/
static String identityHash(String identity) {
public static String identityHash(String identity) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return HexFormat.of().formatHex(digest.digest(identity.getBytes(StandardCharsets.UTF_8)), 0, 6);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,63 @@ public void siblingClosureAppendedReloadsAndOneInsertedBeforeIsRefused() throws
assertTrue(rejected.getMessage(), rejected.getMessage().contains("told apart by their order"));
}

/**
* A dispatch slot is named after its group, not after the order the groups are named in. Interfaces with a method
* of one name (`run`) had their slots numbered in the order of their classes' names (`run`, `run1`, ...), so an
* interface added before another renumbered its slot, the closures bound to it lost their binding, and the edit
* was refused: in a large map's history it was most of the closure refusals.
*/
@Test
public void addedInterfaceWithAMethodOfTheSameNameKeepsTheOthersSlots() throws Exception {
String[] base = {"package Test", "interface Alpha", " function run() returns int",
"interface Zeta", " function run() returns int",
"function callA(Alpha a) returns int", " return a.run()",
"function callZ(Zeta z) returns int", " return z.run()",
// Two closures of each, so the calls dispatch through the interfaces' slots.
"function combine() returns int", " return callA(() -> 1) + callA(() -> 2) + callZ(() -> 3) + callZ(() -> 4)",
"init", " combine()"};
String[] edited = base.clone();
edited[10] = " return callA(() -> 1) + callA(() -> 2) + callZ(() -> 3) + callZ(() -> 4) + callB(() -> 5) + callB(() -> 6)";
edited = concat(edited, new String[] {"interface Beta", " function run() returns int",
"function callB(Beta b) returns int", " return b.run()"});
LuaHotReload.Payload payload = LuaHotReload.payload(manifest(base), manifest(edited), LuaHotReload.History.NONE, 1);
assertTrue(payload.warnings().toString(), payload.warnings().isEmpty());
}

/**
* A group is named after its roots, the methods none of its others override: a class added which implements an
* interface and sorts before it (`AaaImpl` before `Zeta`) leaves the interface's slot as it was.
*/
@Test
public void addedImplementationSortingFirstKeepsTheSlot() throws Exception {
String[] base = {"package Test", "interface Zeta", " function run() returns int",
"function callZ(Zeta z) returns int", " return z.run()",
"function combine() returns int", " return callZ(() -> 1) + callZ(() -> 2)", "init", " combine()"};
String[] edited = base.clone();
edited[6] = " return callZ(() -> 1) + callZ(() -> 2) + callZ(new AaaImpl())";
edited = concat(edited, new String[] {"class AaaImpl implements Zeta", " function run() returns int",
" return 3"});
LuaHotReload.Payload payload = LuaHotReload.payload(manifest(base), manifest(edited), LuaHotReload.History.NONE, 1);
assertTrue(payload.warnings().toString(), payload.warnings().isEmpty());
}

/**
* A closure's owner is its function, named: two functions of one signature in one package (`makeKept()` and
* `makeOther()`, both `returns Value`) made their closures one group told apart by order, so a closure added to the
* first moved the second's and the edit was refused.
*/
@Test
public void closuresOfTwoFunctionsOfOneSignatureAreTwoGroups() throws Exception {
String[] base = {"package Test", "interface Value", " function get() returns int",
"function makeKept(boolean first) returns Value", " return () -> 1",
"function makeOther(boolean first) returns Value", " return () -> 2",
"init", " makeKept(true).get()", " makeOther(true).get()"};
String[] edited = base.clone();
edited[4] = " if first\n return () -> 1\n return () -> 3";
LuaHotReload.Payload payload = LuaHotReload.payload(manifest(base), manifest(edited), LuaHotReload.History.NONE, 1);
assertTrue(payload.warnings().toString(), payload.warnings().isEmpty());
}

private static String[] consumers(String closures) {
return new String[] {"package Test", "interface Op", " function apply(int v) returns int",
"function consume(Op op) returns int", " let result = op.apply(1)", " destroy op", " return result",
Expand Down
10 changes: 9 additions & 1 deletion docs/LUA_HOT_RELOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ The payload compares the running script's manifest with the edited one:
functions, including packages and enclosing scopes of nested parameter types. A closure in a package's `init` block
is owned by the package, one in a constructor by the constructor's function. Removing or renaming a closure still requires a restart
when its old class-table bindings survive. Identities name the classes a nested class is declared in, so two inner
classes of one name in one package (`A.N`, `B.N`) are two.
classes of one name in one package (`A.N`, `B.N`) are two, and a closure's owner is its function by name as well as
signature: two functions of one signature in one package used to give their closures one identity, told apart by
their order across both, so a closure added to one moved the other's (`closuresOfTwoFunctionsOfOneSignatureAreTwoGroups`).
A dispatch slot (the class-table key a method call goes through) is named after its group's root, the method none of
the group's others override (an interface's `run`), and that root's key (`LuaDispatchPreparation.rootOf`). Groups of one
method name used to be numbered in the order of their classes' names (`run`, `run1`, ... `run13` in Castle Fight), so
an interface or an implementation added whose class sorted before others renamed their slots, and every closure bound
to them lost its binding and was refused (`addedInterfaceWithAMethodOfTheSameNameKeepsTheOthersSlots`,
`addedImplementationSortingFirstKeepsTheSlot`). Builds without `-luaHotReload` name groups as before.
Siblings still sharing an identity (anonymous closures, such as several `doAfter` callbacks in one function, or equal
binding names within one callable) are told apart by their order, which matches an object to the closure it was made
from. Editing their bodies keeps that order, and so does adding one after them: both reload. A closure whose source
Expand Down
Loading