tblock2.pp 721 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. { %target=darwin,iphonesim}
  2. {$mode objfpc}
  3. {$modeswitch blocks}
  4. type
  5. tblock = reference to procedure(j: longint); cdecl;
  6. tc = class
  7. i: longint;
  8. procedure callme(j: longint);
  9. end;
  10. var
  11. b: tblock;
  12. c: tc;
  13. procedure tc.callme(j: longint);
  14. const
  15. invocationcount: longint = 0;
  16. begin
  17. writeln('self: ',hexstr(pointer(self)),', i: ',i,', j: ',j);
  18. if self<>c then
  19. halt(1);
  20. if i<>12345 then
  21. halt(2);
  22. if invocationcount=0 then
  23. begin
  24. if j<>1 then
  25. halt(3)
  26. end
  27. else if j<>2 then
  28. halt(4);
  29. inc(invocationcount);
  30. end;
  31. procedure test(b: tblock);
  32. begin
  33. b(2);
  34. end;
  35. begin
  36. c:=tc.create;
  37. c.i:=12345;
  38. b:[email protected];
  39. b(1);
  40. test(@c.callme);
  41. test(b);
  42. end.