| 1 |
/* |
| 2 |
* tests of method context with dynamic functions |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <tads.h> |
| 6 |
#include <dynfunc.h> |
| 7 |
|
| 8 |
main(args) |
| 9 |
{ |
| 10 |
local i = 7; |
| 11 |
useval(i); |
| 12 |
|
| 13 |
run('test 1: function with no method context', |
| 14 |
'function(x) { return x*i; }', 2); |
| 15 |
|
| 16 |
objA.test2(); |
| 17 |
objB.test4(); |
| 18 |
} |
| 19 |
|
| 20 |
useval(n) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
objA: object |
| 25 |
name = 'objA' |
| 26 |
test2() |
| 27 |
{ |
| 28 |
run('test 2: function with a method context', |
| 29 |
'function(x) { return x*p1; }', 3); |
| 30 |
|
| 31 |
run('test 3: method with a method context', |
| 32 |
'method(x) { return x*p1; }', 4); |
| 33 |
} |
| 34 |
test4() { return 11; } |
| 35 |
testMethod() { return 41; } |
| 36 |
p1 = 13 |
| 37 |
; |
| 38 |
|
| 39 |
objB: objA |
| 40 |
name = 'objB' |
| 41 |
test4() |
| 42 |
{ |
| 43 |
run('test 4: function with a full method context', |
| 44 |
'function(x) { return x*p1*inherited(); }', 5); |
| 45 |
|
| 46 |
run('test 5: function with a full method context', |
| 47 |
'function() { return definingobj.p1; }'); |
| 48 |
|
| 49 |
run('test 6: method with a full method context', |
| 50 |
'method(x) { return x*p1*inherited(); }', 7); |
| 51 |
} |
| 52 |
p1 = 37 |
| 53 |
; |
| 54 |
|
| 55 |
objC: objA |
| 56 |
name = 'objc' |
| 57 |
p1 = 19 |
| 58 |
; |
| 59 |
|
| 60 |
run(desc, src, [args]) |
| 61 |
{ |
| 62 |
local frames = t3GetStackTrace(nil, T3GetStackDesc) |
| 63 |
.sublist(2) |
| 64 |
.mapAll({x: x.frameDesc_}); |
| 65 |
|
| 66 |
"<<desc>>\n<<src>>\n"; |
| 67 |
"self available: <<frames[1].getSelf() != nil ? 'yes' : 'no'>>\n"; |
| 68 |
try |
| 69 |
{ |
| 70 |
local func = Compiler.compile(src, frames); |
| 71 |
try |
| 72 |
{ |
| 73 |
"As function: <<func(args...)>>\n"; |
| 74 |
} |
| 75 |
catch (Exception e) |
| 76 |
{ |
| 77 |
"error: <<e.displayException()>>\n"; |
| 78 |
} |
| 79 |
|
| 80 |
objC.setMethod(&testMethod, func); |
| 81 |
try |
| 82 |
{ |
| 83 |
"As method: <<objC.testMethod(args...)>>\n"; |
| 84 |
} |
| 85 |
catch (Exception e) |
| 86 |
{ |
| 87 |
"error: <<e.displayException()>>\n"; |
| 88 |
} |
| 89 |
} |
| 90 |
catch (Exception e) |
| 91 |
{ |
| 92 |
"Compilation error: <<e.displayException()>>\n"; |
| 93 |
} |
| 94 |
"\b"; |
| 95 |
} |