Skip to content

Commit cce2fed

Browse files
Run every script call in CppiaAdoptInstances on a worker
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.
1 parent 259ee26 commit cce2fed

2 files changed

Lines changed: 41 additions & 37 deletions

File tree

‎test/regression/CppiaAdoptInstances/Main.hx‎

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import cpp.cppia.Module;
88
// before stays on the old code and keeps the old module loaded until it is
99
// dropped. openfl/libhxcpp#265
1010
//
11-
// The modules are loaded and the instances held on joined native workers, so
12-
// stale values on the main thread's stack do not decide what is live.
11+
// Every call into script code runs on a joined native worker, so stale values
12+
// it leaves on a stack do not decide what keeps a module loaded. The main
13+
// thread only collects and prints.
1314
@:cppFileCode('
1415
#include <thread>
1516
#include <hx/Native.h>
@@ -27,9 +28,7 @@ class Main {
2728
static var doubler:Dynamic;
2829
static var reshaped:Dynamic;
2930
static var bound:Void->Int;
30-
static var report:Array<Dynamic>;
31-
static var keepsOld:Bool;
32-
static var staysOld:Bool;
31+
static var lines:Array<String>;
3332

3433
static function load(name:String):Module {
3534
var module = Module.fromData(sys.io.File.getBytes(haxe.io.Path.join([dir, name])).getData());
@@ -49,16 +48,39 @@ class Main {
4948

5049
static function adopt() {
5150
next = load("v2.cppia");
52-
report = untyped next.adoptInstances(previous);
51+
var report:Array<Dynamic> = untyped next.adoptInstances(previous);
5352
untyped previous.release();
5453
previous = null;
54+
var rows = [for (r in report) '${r.name} moved ${r.moved}' + (r.reason == null ? "" : ': ${r.reason}')];
55+
rows.sort(Reflect.compare);
56+
lines.push('report: ${rows.join(", ")}');
57+
}
58+
59+
static function useMoved() {
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+
var stepper:Stepper = counter;
64+
lines.push('the host interface reaches the new code: ${stepper.step() == 26}');
65+
var counterClass = 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")}');
5567
}
5668

57-
// The old code runs on a worker too, so nothing it leaves on the stack
58-
// keeps the old module
5969
static function useOld() {
60-
staysOld = reshaped.version() == "v1";
61-
keepsOld = bound() == 27 && counter.step() == 37;
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}');
73+
}
74+
75+
static function dropOld() {
76+
bound = null;
77+
reshaped = null;
78+
}
79+
80+
static function dropAll() {
81+
counter = doubler = null;
82+
untyped next.release();
83+
next = null;
6284
}
6385

6486
static function retained():String {
@@ -71,38 +93,19 @@ class Main {
7193

7294
static function main() {
7395
dir = haxe.io.Path.directory(Sys.programPath());
74-
var stepper:Stepper = null;
7596
for (jit in [false, true]) {
7697
Host.enableJit(jit);
98+
lines = [];
7799
untyped __cpp__("runJoined({0})", loadOld);
78100
untyped __cpp__("runJoined({0})", adopt);
79-
80-
var rows = [for (r in report) '${r.name} moved ${r.moved}' + (r.reason == null ? "" : ': ${r.reason}')];
81-
rows.sort(Reflect.compare);
82-
Sys.println('jit $jit: report: ${rows.join(", ")}');
83-
84-
// Moved, with their fields as they were
85-
Sys.println('jit $jit: counter runs the new code: ${counter.step() == 16 && counter.version() == "v2 counter"}');
86-
Sys.println('jit $jit: the subclass runs its new override: ${doubler.step() == 20 && doubler.version() == "v2 counter"}');
87-
stepper = counter;
88-
Sys.println('jit $jit: the host interface reaches the new code: ${stepper.step() == 26}');
89-
var counterClass = next.resolveClass("Counter");
90-
Sys.println('jit $jit: they are of the new classes: ${Type.getClass(counter) == counterClass && Std.isOfType(counter, counterClass) && Std.isOfType(doubler, counterClass) && Type.getClass(doubler) == next.resolveClass("Doubler")}');
91-
92-
// Stayed
101+
untyped __cpp__("runJoined({0})", useMoved);
93102
untyped __cpp__("runJoined({0})", useOld);
94-
Sys.println('jit $jit: the reshaped instance keeps its class: $staysOld');
95-
Sys.println('jit $jit: the bound method keeps the old code: $keepsOld');
96-
Sys.println('jit $jit: the old module keeps only what stayed: ${retained()}');
97-
98-
bound = null;
99-
reshaped = null;
100-
untyped __cpp__("runJoined({0})", () -> {});
101-
Sys.println('jit $jit: and is freed once they go: "${retained()}"');
102-
103-
counter = doubler = null;
104-
untyped next.release();
105-
next = null;
103+
lines.push('the old module keeps only what stayed: ${retained()}');
104+
untyped __cpp__("runJoined({0})", dropOld);
105+
lines.push('and is freed once they go: "${retained()}"');
106+
untyped __cpp__("runJoined({0})", dropAll);
107+
for (line in lines)
108+
Sys.println('jit $jit: $line');
106109
}
107110
}
108111
}

‎test/regression/emscripten-skip.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CffiNekoValueTags: runs neko, which loads an ndll the test builds
1212
CffiPrimeAfterCffi: loads an ndll it builds, and Emscripten links statically
1313
CffiPrimeUnicodeFallback: loads an ndll it builds, and Emscripten links statically
1414
CffiQuietMissingPrimitive: loads an ndll it builds, and Emscripten links statically
15+
CppiaAdoptInstances: threads (std::thread, which needs a pthreads build)
1516
CppiaArrayAccess: its host is the first --next section, which -D emscripten does not reach
1617
CppiaBreakpoints: threads
1718
CppiaHostArgs: processes

0 commit comments

Comments
 (0)