Skip to content

Commit 49eb7e2

Browse files
Tutez64dimensionscape
authored andcommitted
Test a set by name on an old cppia instance, with a generational GC.
The cppia host now builds with HXCPP_GC_GENERATIONAL. ClientHolder grows old through full collections, then the host sets its array field with Reflect.setField, and a weak reference checks that a generational collection keeps the array. Without the write barrier, it is freed while the holder still points at it.
1 parent 2042f42 commit 49eb7e2

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎test/cppia/Client.hx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class ClientNursery {
4141
}
4242
}
4343

44+
class ClientHolder {
45+
46+
public var values:Array<Int>;
47+
48+
public function new() {}
49+
}
50+
4451
class Client
4552
{
4653
public static var clientBool0 = true;

‎test/cppia/cases/TestCommon.hx‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cases;
22

33
import cpp.cppia.Host;
44
import cpp.vm.Gc;
5+
import cpp.vm.WeakRef;
56
import utest.Test;
67
import utest.Assert;
78

@@ -79,6 +80,27 @@ class TestCommon extends Test {
7980
Assert.equals(42, kept[0]);
8081
}
8182

83+
@:depends(testStatus)
84+
function testSetFieldOfOldObject() {
85+
final holder = Type.createInstance(Type.resolveClass('ClientHolder'), []);
86+
87+
// Full collections make the holder old: a generational collection no longer traverses it.
88+
for (_ in 0...3) {
89+
Gc.run(true);
90+
}
91+
final values = setValues(holder);
92+
Gc.run(false);
93+
94+
Assert.notNull(values.get(), 'Freed a value set by name on an old object');
95+
}
96+
97+
// Only a weak reference to the value leaves this frame, so that the stack does not keep it.
98+
static function setValues(holder:Dynamic):WeakRef<Array<Int>> {
99+
final values = [42];
100+
Reflect.setField(holder, 'values', values);
101+
return new WeakRef(values);
102+
}
103+
82104
@:depends(testStatus)
83105
function testInterfaceCalling() {
84106
final obj : IFoo = Type.createInstance(Type.resolveClass('ClientFoo'), []);

0 commit comments

Comments
 (0)