Skip to content

Move live cppia instances onto a reloaded module's classes (#265) - #442

Merged
dimensionscape merged 3 commits into
mainfrom
feature/cppia-adopt-instances
Nov 13, 2026
Merged

dimensionscape merged 3 commits into
mainfrom
feature/cppia-adopt-instances

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

Builds #265 as agreed there. It was first written on the cppia-bugs-1106 branch and is picked from there onto current main.

A loaded cppia module gets adoptInstances(previous). A host calls it after the new module boots and before the old one is released:

var next = cpp.cppia.Module.fromData(bytes.getData());
next.boot();
var report:Array<Dynamic> = untyped next.adoptInstances(previous);
untyped previous.release();

It runs a census collection and gives each live instance of an old class the class of the same name in the new module, so calls on objects made before the reload run the new code, through the instance, its interfaces and dynamic calls, and Type.getClass gives the new class. An instance finds everything about its class through slot -1 of its script vtable, so the move replaces that one pointer, and the instance stays where it is.

That makes the rule strict. Both classes must extend the same host class through super classes of the same names, implement the same interfaces, and have the same fields and dynamic functions, with the same names, storage and offsets. Methods can differ in any way, which covers the usual edit. The result has one row per old class with live instances: name, moved, and reason, which names the first difference when they couldn't move. Bound methods and closures made before keep the old code, and keep the old module loaded until they go. Statics aren't copied. A host that doesn't call it sees no change.

The census visitor is a single global, so the retained-module report and this now take turns at it.

CppiaAdoptInstances loads two versions of a script and checks the moved instances, a subclass, a host interface, Type.getClass, a class whose fields changed, a bound method, and that the old module is freed once what stayed goes, interpreted and with the JIT. Every call into script code runs on a joined worker, since on Linux the main thread's stack kept stale values that held the old module. It's on the Emscripten skip list, as it needs threads. docs/CppiaModuleRelease.md describes the call.

Tested here on Windows x64: CppiaAdoptInstances with Haxe 4.3.7, and with the Haxe nightly together with #441, which that build needs for Condition. CppiaModuleFree, CppiaModuleRelease and CppiaModuleRetained still pass.

Fixes #265.

Instances made before a reload kept their class and its code, so code
reloaded into a running process only reached objects made after it
(libhxcpp #265).

A loaded module's adoptInstances(previous), called after it boots and
before the previous module is released, runs a census collection and
gives each live instance of a previous class the class of the same name
in this module. An instance finds everything about its class through slot
-1 of its script vtable, so the move replaces that one pointer. The
instance stays where it is, at its size, so the classes must extend the
same host class through super classes of the same names, implement the
same interfaces, and have the same fields and dynamic functions with the
same names, storage and offsets. Methods can differ in any way.

It returns a row for each previous class with live instances: its name,
how many moved, and for one that could not, the first difference. Bound
methods and closures made before keep the old code and the old module.

The census visitor is a single global, so the retained-module report and
this take turns at it.

CppiaAdoptInstances loads two versions of a script and checks the moved
instances, a subclass, a host interface, Type.getClass, a class whose
fields changed, a bound method, and that the old module is freed once
what stayed goes, interpreted and with the JIT.
On Linux, musl and 32-bit Linux the JIT pass listed the first pass's new
module as still loaded, with its two instances and four closures: the main
thread had called its code through Dynamic, and the stale values those
calls left on its stack kept it. Windows happened to overwrite them. Every
call into script code now runs on a joined worker, and the main thread
only collects and prints.

CppiaAdoptInstances needs threads, which Emscripten does not have, so it
is on its skip list.
dimensionscape added a commit that referenced this pull request Nov 13, 2026
The input from #442's fuzz run. Replayed with run.sh --replay cppia, main's loader segfaults on it and this branch's loads all ten cppia inputs.
@dimensionscape
dimensionscape merged commit e96c80c into main Nov 13, 2026
37 checks passed
@dimensionscape
dimensionscape deleted the feature/cppia-adopt-instances branch November 13, 2026 19:55
@dimensionscape dimensionscape mentioned this pull request Nov 13, 2026
28 of 34 tasks
HomuHomu833 pushed a commit to ShadowEngineTeam/libhxcpp that referenced this pull request Nov 14, 2026
The fuzz job's cppia target stopped in CallStatic::link with "member call
on null pointer of type 'hx::HaxeNativeClass'", on openfl#442's run. A static
call whose type had a host class looked the function up in that class's
script glue, haxeBase, which a host interface does not have. Haxe gives
an interface no statics, so only a malformed file makes such a call. It
goes on to the field lookup the other host classes fall back to, and the
file fails to load as for any other static that is not there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cppia: move live instances of a replaced class onto its replacement

1 participant