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
2 changes: 2 additions & 0 deletions BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ Notes; finished-work narrative does not.
- **Use the Lua hot reload on a real map.** Hot run of a Lua project applies each save to the running
map: new and changed functions, globals, fields, classes, lambdas and packages, measured on the
3.0.1.24371 client in a test map over five payloads ([docs/LUA_HOT_RELOAD.md](docs/LUA_HOT_RELOAD.md)).
Tuned constants take their new values too. Every kind of edit, constants included, was run again in the game on
24 Oct, nine payloads in one game, driven from the compiler's command line.
Not yet measured: how often a real project's edits are rejected, and its payload sizes and reload
times. Later stages are in the design note: a library reload hook, and multiplayer only through a
synchronised channel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,49 @@ private static final class State {
Map<String, List<String>> slots = new LinkedHashMap<>();
Map<String, String> initializers = new LinkedHashMap<>();
List<String> packageInits = new ArrayList<>();
/** The initializers of globals a version the map ran declared as variables, which code may have written. */
List<String> variables = new ArrayList<>();
}

/**
* Builds the payload which takes a script with the first manifest to the program of the second and writes it as
* a preload file: {@code <baseline manifest.json> <current manifest.json> <output .pld>}. Each build with
* {@code -luaHotReload} leaves its manifest in {@code _build/luaHotReload/manifest.json}.
* A session without the editor, for a map built with {@code -build ... -luaHotReload}, which leaves its manifest in
* {@code <build dir>/luaHotReload/manifest.json}:
* <ul>
* <li>{@code start <build dir> <CustomMapData>}: the map of the last build is the one which runs; the payload files
* of an earlier session are deleted;</li>
* <li>{@code send <build dir> <CustomMapData>}: writes the next payload, which takes the running map to the program
* of the last build, where the map polls for it;</li>
* <li>{@code <baseline manifest.json> <current manifest.json> <output .pld>}: the first payload a map running the
* first manifest would get for the second, written as a preload file.</li>
* </ul>
* A payload the edit cannot carry exits with 1 and prints the reasons.
*/
public static void main(String[] args) throws IOException {
if (args.length == 3 && (args[0].equals("start") || args[0].equals("send"))) {
Path buildDir = Path.of(args[1]);
Path customMapData = Path.of(args[2]);
LuaHotReload.Manifest manifest = readManifest(buildDir.resolve(SESSION_FOLDER).resolve("manifest.json"));
if (args[0].equals("start")) {
setAsideDeliveries(customMapData);
discardDeliveries(customMapData);
start(buildDir, manifest);
System.out.println("session started");
return;
}
try {
Sent sent = send(buildDir, customMapData, manifest);
System.out.println("payload " + sent.payload().version() + " (" + String.join(", ", sent.payload().functions())
+ ") written to " + sent.file());
sent.payload().warnings().forEach(w -> System.out.println("warning: " + w));
} catch (LuaHotReload.Rejected e) {
System.out.println(e.getMessage());
System.exit(1);
}
return;
}
if (args.length != 3) {
System.err.println("usage: <baseline manifest.json> <current manifest.json> <output .pld>");
System.err.println("usage: start <build dir> <CustomMapData> | send <build dir> <CustomMapData>"
+ " | <baseline manifest.json> <current manifest.json> <output .pld>");
System.exit(2);
}
LuaHotReload.Manifest baseline = readManifest(Path.of(args[0]));
Expand Down Expand Up @@ -116,6 +149,7 @@ private static final class Pending {
Map<String, List<String>> slots = new LinkedHashMap<>();
Map<String, String> initializers = new LinkedHashMap<>();
List<String> packageInits = new ArrayList<>();
List<String> variables = new ArrayList<>();
}

/** A payload and the file it was written to. */
Expand All @@ -137,14 +171,16 @@ public static Sent send(Path buildDir, Path customMapData, LuaHotReload.Manifest
recoverPending(buildDir, customMapData, state);
LuaHotReload.Payload payload = LuaHotReload.payload(state.baseline, current, new LuaHotReload.History(
new LinkedHashSet<>(state.reloaded), orEmpty(state.capturedDefinitions), orEmpty(state.slots),
orEmpty(state.initializers), new LinkedHashSet<>(orEmpty(state.packageInits))), state.version + 1);
orEmpty(state.initializers), new LinkedHashSet<>(orEmpty(state.packageInits)),
new LinkedHashSet<>(orEmpty(state.variables))), state.version + 1);
Pending pending = new Pending();
pending.version = payload.version();
pending.functions = payload.functions();
pending.capturedDefinitions = new LinkedHashMap<>(payload.captured());
pending.slots = new LinkedHashMap<>(payload.slots());
pending.initializers = new LinkedHashMap<>(payload.initializers());
pending.packageInits = new ArrayList<>(payload.packageInits());
pending.variables = new ArrayList<>(payload.variables());
writeAtomically(pendingFile(buildDir), GSON.toJson(pending));
Path delivered = deliver(customMapData, payload);
record(state, pending);
Expand Down Expand Up @@ -193,6 +229,9 @@ private static void record(State state, Pending sent) {
Set<String> packageInits = new LinkedHashSet<>(orEmpty(state.packageInits));
packageInits.addAll(orEmpty(sent.packageInits));
state.packageInits = new ArrayList<>(packageInits);
Set<String> variables = new LinkedHashSet<>(orEmpty(state.variables));
variables.addAll(orEmpty(sent.variables));
state.variables = new ArrayList<>(variables);
state.version = sent.version;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,21 @@ private String makeClassName(ImClassType superClass) {

private String hotReloadOwnerIdentity(TranslatedToImFunction callable) {
StringBuilder identity = new StringBuilder(callable instanceof ConstructorDef ? "constructor"
: callable instanceof OnDestroyDef ? "destructor" : callable instanceof ExtensionFuncDef ? "extension" : "function");
: callable instanceof OnDestroyDef ? "destructor" : callable instanceof ExtensionFuncDef ? "extension"
: callable instanceof InitBlock ? "init" : "function");
for (Element scope = callable; scope != null; scope = scope.getParent()) {
if (scope instanceof NamedScope named) {
String segment = named.getName();
identity.append(segment.length()).append(':').append(segment);
}
}
ImFunction owner = tr.getFuncFor(callable);
if (callable instanceof InitBlock) {
// An init block is translated into its package's init function, which has no signature: the package is
// the whole identity.
return Hashing.sha256().hashString(identity, StandardCharsets.UTF_8).toString();
}
// A constructor's body is its construct function; getFuncFor would make another one from the same parameters.
ImFunction owner = callable instanceof ConstructorDef constructor ? tr.getConstructFunc(constructor) : tr.getFuncFor(callable);
identity.append('<').append(owner.getTypeVariables().size()).append('>')
.append(owner.hasFlag(FunctionFlagEnum.IS_VARARG) ? "vararg" : "fixed");
for (ImVar parameter : owner.getParameters()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public <T extends Element> T canonical(T copy) {
/** Complete source global initializers, kept as functions in hot-reload builds before flattening splits them. */
public final Map<ImFunction, ImVar> luaGlobalInitializers = new LinkedHashMap<>();

/** The stack and depth which -stacktraces instrumentation pushes and pops around every call; empty without it. */
public final Set<ImVar> stackTraceStorage = Collections.newSetFromMap(new IdentityHashMap<>());

private final Map<TranslatedToImFunction, ImVar> thisVarMap = new Object2ObjectLinkedOpenHashMap<>();

private final Set<WPackage> translatedPackages = new ObjectLinkedOpenHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public void visit(ImFuncRef imFuncRef) {
prog.getGlobals().add(stackSize);
stack = JassIm.ImVar(trace, TypesHelper.imStringArray(), "wurst_stack", false);
prog.getGlobals().add(stack);
tr.stackTraceStorage.add(stackSize);
tr.stackTraceStorage.add(stack);
prog.getGlobalInits().put(stackSize, Collections.singletonList(
JassIm.ImSet(trace, JassIm.ImVarAccess(stackSize), JassIm.ImIntVal(0))));

Expand Down Expand Up @@ -542,6 +544,11 @@ private ImFunction getEnclosingFunc(Element e) {
}

private boolean isStackTraceParam(ImVar p) {
return isStackPositionParameter(p);
}

/** Whether {@code p} is the parameter this injection adds, which each call passes its source position in. */
public static boolean isStackPositionParameter(ImVar p) {
return p.getName().equals(STACK_POS_PARAM);
}

Expand Down
Loading
Loading