You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e96c80c
Browse filesBrowse the repository at this point in the historyBrowse files
Move live cppia instances onto a reloaded module's classes (#442)
A loaded cppia module's adoptInstances(previous), called after it boots and before the previous module is released, gives each live instance of a previous class the class of the same name in this module, when the two lay out their fields the same way, so code reloaded into a running process reaches objects made before the reload. It returns a row per previous class with live instances: how many moved, and why not for one that could not. docs/CppiaModuleRelease.md describes it. Fixes#265.
# Conflicts:
# test/regression/emscripten-skip.txt
* Added adoptInstances to a loaded cppia module: called with the module it replaces, before that one is released, it moves the live instances of each of the old module's classes onto the new class of the same name when the two lay out their fields the same way, so code reloaded into a running process reaches objects made before the reload. Calls on them run the new code, through interfaces and dynamic calls too, and Type.getClass gives the new class. It returns a row for each old class with live instances: how many moved, and for a class whose fields changed, which field differs. Bound methods and closures made before keep the old code. See docs/CppiaModuleRelease.md (libhxcpp #265)
5
6
* Fixed the incremental collector (HXCPP_GC_INCREMENTAL) freeing a thread object's arrays while the thread was being created, as the gc stress test's verifier caught on Windows. A slice could find the new object on the stack while its first array was being allocated, and the cycle could end before the constructor's barrier, with the arrays stored after that unscanned. The arrays are now made in a mutation scope, which the stop that finishes a cycle scans again
6
7
* cpp.encoding.Utf8.decode replaces each malformed UTF-8 sequence with U+FFFD instead of throwing, with smart strings or without. Haxe 5's Bytes.toString and getString decode with it, so they threw for bytes that Haxe 4's, which go through String::create, decode with U+FFFD in the same places. Valid input still takes the decoder that counts first; only malformed input takes the replacing one. Utf8.codepoint still throws
7
8
* Fixed String::utf8_str into a view, at API level 500, writing the bytes and then throwing when the view had room for them but not the terminator. It returns false before writing anything. It also stopped a UTF-16 string at its first NUL, which is a character, and reported that length (libhxcpp #408)
Copy file name to clipboardExpand all lines: docs/CppiaModuleRelease.md
+27Lines changed: 27 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,3 +66,30 @@ for (row in (untyped __global__.__scriptable_cppia_retained() : Array<Dynamic>))
66
66
It works in release builds, like `__hxcpp_gc_census()`, on which it builds. Not counted, since the module holds them itself while it is loaded: its `Class` objects, its enum values, and its static functions used as values. A host holding one of those keeps the module loaded too. Code of the module still running on a thread, and stale pointers to it left on a stack (see Limitations), keep it loaded without anything being counted.
67
67
68
68
To find what holds the instances of a class, build the host with `-debug` and call `cpp.vm.Gc.trace` with that class, kept from before the release, since `Type.resolveClass` no longer finds it: it prints the path from a root to each instance.
69
+
70
+
## Moving live instances onto the new module's classes
71
+
72
+
Instances made before a reload keep their class and its code, so a reload only changes objects made afterwards. A host can move them instead, after the new module has booted and before the old one is released:
73
+
74
+
```haxe
75
+
var next = cpp.cppia.Module.fromData(bytes.getData());
76
+
next.boot();
77
+
var report:Array<Dynamic> = untyped next.adoptInstances(previous);
78
+
untyped previous.release();
79
+
```
80
+
81
+
`adoptInstances(previous)` runs a full collection, and in it gives each live instance of a previous module's class the class of the same name in this module, when the two lay out their fields the same way. Calls on a moved instance then run the new code, through the instance, its interfaces and dynamic calls alike, and `Type.getClass` gives the new class. It returns one row for each previous class that had live instances, in name order:
82
+
83
+
-`name`, the class's name.
84
+
-`moved`, how many of its instances moved.
85
+
-`reason`, null when they could move, otherwise why not, naming the first field that differs.
86
+
87
+
The rule is strict, as an instance stays where it is, at its size. Both classes must extend the same host class, through super classes of the same names, implement the same interfaces, and have the same fields, dynamic functions included, with the same names, storage types and offsets. Methods can differ in any way, so editing method bodies, the usual case, is covered. A class that adds, removes, retypes or reorders a field keeps its instances, which keep the old module loaded until they go.
88
+
89
+
What does not move:
90
+
91
+
- Bound methods and closures made from the old class's methods before the step keep calling the old code, and keep the old module loaded until they are dropped. The same goes for a dynamic function's value held in a field.
92
+
- Code running on any thread finishes in the old code. The fields are laid out the same, so it reads and writes them correctly.
93
+
- Statics: the new module has its own, and nothing is copied.
94
+
95
+
Calling it twice moves nothing the second time. A host that does not call it sees no change.
varrows= [for (rinreport) '${r.name} moved ${r.moved}'+ (r.reason==null?"":': ${r.reason}')];
55
+
rows.sort(Reflect.compare);
56
+
lines.push('report: ${rows.join(", ")}');
57
+
}
58
+
59
+
staticfunctionuseMoved() {
60
+
// Moved, with their fields as they were
61
+
lines.push('counter runs the new code: ${counter.step() ==16&&counter.version() =="v2 counter"}');
62
+
lines.push('the subclass runs its new override: ${doubler.step() ==20&&doubler.version() =="v2 counter"}');
63
+
varstepper:Stepper=counter;
64
+
lines.push('the host interface reaches the new code: ${stepper.step() ==26}');
65
+
varcounterClass=next.resolveClass("Counter");
66
+
lines.push('they are of the new classes: ${Type.getClass(counter) ==counterClass&&Std.isOfType(counter, counterClass) &&Std.isOfType(doubler, counterClass) &&Type.getClass(doubler) ==next.resolveClass("Doubler")}');
67
+
}
68
+
69
+
staticfunctionuseOld() {
70
+
// Stayed
71
+
lines.push('the reshaped instance keeps its class: ${reshaped.version() =="v1"}');
72
+
lines.push('the bound method keeps the old code: ${bound() ==27&&counter.step() ==37}');
0 commit comments