1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
use core:debug;
Int runFnPtr(fn(Int)->Int v) {
v.call(10);
}
Int runFnPtr(fn->Int v) {
v.call();
}
void consumeStr(fn(Str)->void v) {
v.call("22");
}
Int consumeDbg(fn(Dbg)->Int v) {
v.call(Dbg(23));
}
Int runStrFnPtr(fn(Str)->Str v) {
Str z = v.call("22");
z.toInt;
}
Int runFnPtr(fn(Int)->DbgVal v) {
v.call(23).get;
}
Int runFnPtr(fn(Int)->Dbg v) {
v.call(21).get;
}
Int addTen(Int v) {
v + 10;
}
fn(Int)->Int createIntFn() {
&addTen(Int);
}
DbgVal addTen(Dbg dbg) {
DbgVal(dbg.get + 10);
}
DbgVal addDbg(Dbg a, Dbg b) {
DbgVal(a.get + b.get);
}
fn(Dbg)->DbgVal createDbgFn(Int type) {
if (type == 0) {
&addTen(Dbg);
} else if (type == 1) {
Dbg z(20);
&z.addDbg(Dbg);
} else {
&addTen(Dbg);
}
}
DbgVal createDbgVal() {
DbgVal(22);
}
fn->DbgVal createVoidFn(Int type) {
if (type == 0) {
Dbg z(20);
&z.asDbgVal();
} else if (type == 1) {
DbgActor z(12);
&z.asDbgVal();
} else {
&createDbgVal();
}
}
DbgVal addDbgActor(DbgActor a, DbgActor b) {
DbgVal(a.get + b.get);
}
DbgVal incDbgActor(DbgActor a) {
DbgVal(a.get + 4);
}
fn(DbgActor)->DbgVal createActorFn(Int type) {
if (type == 0) {
DbgActor tmp(20);
&tmp.addDbgActor(DbgActor);
} else {
&incDbgActor(DbgActor);
}
}
// Counts how many copies have been made.
class CloneTrack extends Dbg {
init(Int v) {
init(v) {}
}
init(CloneTrack t) {
init(t) {}
set(get + 10000);
}
void deepCopy(CloneEnv env) {
super:deepCopy(env);
set(get + 100);
}
}
// Create the tracker.
Dbg dbgTrack(Int v) {
CloneTrack(v);
}
// Run a function pointer.
Int runPtr(fn(Dbg)->Int ptr, Dbg v) {
ptr.call(v);
}
// Run a function pointer on another thread.
Int runPtrOther(fn(Dbg)->Int ptr, Dbg value) {
runPtrOtherHelp(ptr, value);
}
Int runPtrOtherHelp(fn(Dbg)->Int ptr, Dbg value) on Other {
ptr.call(value);
}
// Create various function pointers.
fn(Dbg)->Int trackParam(Int t) {
if (t == 0) {
// Normal function - no clones.
&trackFree(Dbg);
} else if (t == 1) {
// Call to another thread - possibly clones.
&trackOnOther(Dbg);
} else if (t == 2) {
// Call to another thread - possibly clones.
&trackOnCompiler(Dbg);
} else if (t == 3) {
// Call to another thread - possibly clones.
InActor actor(Other);
&actor.trackInActor(Dbg);
} else {
// Call to another thread - possibly clones.
InActor actor(Compiler);
&actor.trackInActor(Dbg);
}
}
// Actor.
fn(InActor, Dbg)->Int trackActor() {
&InActor:trackInActor(InActor, Dbg);
}
InActor createInActor() {
InActor(Other);
}
// C++ does not know of TObject, so we use TObject instead. It is ugly, but it works for testing.
Int runPtrActor(fn(TObject, Dbg)->Int fn, Dbg dbg) {
fn.call(createInActor, dbg);
}
Int trackFree(Dbg v) {
v.get;
}
Int trackOnOther(Dbg v) on Other {
v.get;
}
Int trackOnCompiler(Dbg v) on Compiler {
v.get;
}
class InActor on ? {
init(Thread) {
init() {}
}
Int trackInActor(Dbg v) {
v.get;
}
}
fn->Dbg dbgFnPtr() {
&returnDbg();
}
Dbg returnDbg() on Other {
CloneTrack(1);
}
Int callDbgFnPtr(fn->Dbg fn) {
fn.call().get;
}
Bool returnBool1() {
true;
}
Bool returnBool2() {
false;
}
fn->Bool createBoolFn(Bool v) {
if (v) {
&returnBool1();
} else {
&returnBool2();
}
}
Int runBoolFn(fn->Bool fn) {
Bool z = false;
z |= fn.call();
if (z) {
1;
} else {
0;
}
}
Int autoFn() {
runFnPtr(&addTen);
}
class PtrTest {
Int z;
init(Int v) {
init() {
z = v;
}
}
Int doIt(Int v) {
v + z;
}
}
Int autoMemberFn() {
PtrTest x(5);
runFnPtr(&x.doIt);
}
Bool intPredicate(Int a, Int b) {
a > b;
}
Int copyIntPredicate() {
Fn<Bool, Int, Int> ptr = &intPredicate;
Array<Int> data;
data << 1 << 2 << 3;
// This should always work:
Int result = data.sorted(ptr)[0];
Fn<Bool, Int, Int> copy(ptr);
// This was broken previously:
result += data.sorted(copy)[0];
result;
}
fn()->DbgVal createCtorValuePtr() {
return &DbgVal;
}
fn(Int)->Dbg createCtorClassPtr() {
return &Dbg(Int);
}
fn()->Int createIntPtr() {
return ∬
}
void createOtherPtr() {
var ptr = &ThreadObj();
ptr.call();
}
void createDynActorPtr() {
var ptr = &DynActor(Thread);
ptr.call(Other);
}
|