Skip to content

Commit 8cb0c28

Browse files
Tutez64dimensionscape
authored andcommitted
Test values that an old cppia module creates, with a generational GC.
The cppia host now builds with HXCPP_GC_GENERATIONAL. After full collections make the module old, ClientModuleValues boxes a string literal and assigns a static array, which only the module references. Weak references then check that a generational collection keeps them. Without the fix, both are freed without the JIT, and the array with it (the JIT boxes the literal at load time).
1 parent 7861db8 commit 8cb0c28

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎test/cppia/Client.hx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ class ClientHolder {
4848
public function new() {}
4949
}
5050

51+
class ClientModuleValues {
52+
53+
static var kept:Array<String>;
54+
55+
// Read as Dynamic, the literal is boxed on first use, and the module keeps the box.
56+
public static function literal():Dynamic {
57+
var boxed:Dynamic = "a string literal";
58+
return boxed;
59+
}
60+
61+
// Assigned after load: the module holds the only reference.
62+
public static function assigned():Dynamic {
63+
if (kept == null)
64+
kept = ["assigned after load"];
65+
return kept;
66+
}
67+
}
68+
5169
class Client
5270
{
5371
public static var clientBool0 = true;

‎test/cppia/cases/TestCommon.hx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ class TestCommon extends Test {
101101
return new WeakRef(values);
102102
}
103103

104+
@:depends(testStatus)
105+
function testModuleValuesCreatedWhenOld() {
106+
final values = Type.resolveClass('ClientModuleValues');
107+
108+
// Full collections make the module old: a generational collection no longer traverses it.
109+
for (_ in 0...3) {
110+
Gc.run(true);
111+
}
112+
final created = createValues(values);
113+
Gc.run(false);
114+
115+
for (value in created) {
116+
Assert.notNull(value.get(), 'Freed a value that the old module holds');
117+
}
118+
}
119+
120+
// Only weak references to the values leave this frame, so that the stack does not keep them.
121+
static function createValues(values:Dynamic):Array<WeakRef<Dynamic>> {
122+
return [for (name in ['literal', 'assigned']) new WeakRef<Dynamic>(Reflect.callMethod(values, Reflect.field(values, name), []))];
123+
}
124+
104125
@:depends(testStatus)
105126
function testInterfaceCalling() {
106127
final obj : IFoo = Type.createInstance(Type.resolveClass('ClientFoo'), []);

0 commit comments

Comments
 (0)