rtti.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (c) 2018 by Mattias Gaertner
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. unit RTTI;
  11. {$mode objfpc}
  12. interface
  13. uses
  14. SysUtils, Types, TypInfo, JS;
  15. resourcestring
  16. SErrInvokeInvalidCodeAddr = 'CodeAddress is not a function';
  17. type
  18. // will be changed to 'record' and improved as soon as the
  19. // operator overloading is implemented
  20. TValue = JSValue;
  21. EInvoke = EJS;
  22. TVirtualInterfaceInvokeEvent = function(const aMethodName: string;
  23. const Args: TJSValueDynArray): JSValue of object;
  24. { TVirtualInterface: A class that can implement any IInterface. Any method
  25. call is handled by the OnInvoke event. }
  26. TVirtualInterface = class(TInterfacedObject, IInterface)
  27. private
  28. FOnInvoke: TVirtualInterfaceInvokeEvent;
  29. public
  30. constructor Create(InterfaceTypeInfo: Pointer); overload; assembler;
  31. constructor Create(InterfaceTypeInfo: Pointer;
  32. const InvokeEvent: TVirtualInterfaceInvokeEvent); overload;
  33. property OnInvoke: TVirtualInterfaceInvokeEvent read FOnInvoke write FOnInvoke;
  34. end;
  35. procedure CreateVirtualCorbaInterface(InterfaceTypeInfo: Pointer;
  36. const MethodImplementation: TVirtualInterfaceInvokeEvent; out IntfVar); assembler;
  37. function Invoke(ACodeAddress: Pointer; const AArgs: TJSValueDynArray;
  38. ACallConv: TCallConv; AResultType: PTypeInfo; AIsStatic: Boolean;
  39. AIsConstructor: Boolean): TValue;
  40. implementation
  41. procedure CreateVirtualCorbaInterface(InterfaceTypeInfo: Pointer;
  42. const MethodImplementation: TVirtualInterfaceInvokeEvent; out IntfVar); assembler;
  43. asm
  44. var IntfType = InterfaceTypeInfo.interface;
  45. var i = Object.create(IntfType);
  46. var o = { $name: "virtual", $fullname: "virtual" };
  47. i.$o = o;
  48. do {
  49. var names = IntfType.$names;
  50. if (!names) break;
  51. for (var j=0; j<names.length; j++){
  52. let fnname = names[j];
  53. i[fnname] = function(){ return MethodImplementation(fnname,arguments); };
  54. }
  55. IntfType = Object.getPrototypeOf(IntfType);
  56. } while(IntfType!=null);
  57. IntfVar.set(i);
  58. end;
  59. { TVirtualInterface }
  60. constructor TVirtualInterface.Create(InterfaceTypeInfo: Pointer); assembler;
  61. asm
  62. var IntfType = InterfaceTypeInfo.interface;
  63. if (IntfType.$kind !== 'com') rtl.raiseE('EInvalidCast');
  64. var guid = IntfType.$guid;
  65. var i = Object.create(IntfType); // needed by IntfVar is IntfType
  66. i.$o = this;
  67. // copy IInterface methods: _AddRef, _Release, QueryInterface
  68. var iinterfaceguid = '{00000000-0000-0000-C000-000000000046}';
  69. var map = this.$intfmaps[iinterfaceguid];
  70. for (var key in map){
  71. var v = map[key];
  72. if (typeof(v)!=='function') continue;
  73. i[key] = map[key];
  74. }
  75. // all other methods call OnInvoke
  76. do {
  77. var names = IntfType.$names;
  78. if (!names) break;
  79. for (var j=0; j<names.length; j++){
  80. let fnname = names[j];
  81. if (i[fnname]) continue;
  82. i[fnname] = function(){ return this.$o.FOnInvoke(fnname,arguments); };
  83. }
  84. IntfType = Object.getPrototypeOf(IntfType);
  85. } while(IntfType!=null);
  86. // create a new list of interface map, supporting IInterface and IntfType
  87. this.$intfmaps = {};
  88. this.$intfmaps[iinterfaceguid] = map;
  89. this.$intfmaps[guid] = {};
  90. // store the implementation of IntfType (used by the as-operator)
  91. this.$interfaces = {};
  92. this.$interfaces[guid] = i;
  93. end;
  94. constructor TVirtualInterface.Create(InterfaceTypeInfo: Pointer;
  95. const InvokeEvent: TVirtualInterfaceInvokeEvent);
  96. begin
  97. Create(InterfaceTypeInfo);
  98. OnInvoke:=InvokeEvent;
  99. end;
  100. function Invoke(ACodeAddress: Pointer; const AArgs: TJSValueDynArray;
  101. ACallConv: TCallConv; AResultType: PTypeInfo; AIsStatic: Boolean;
  102. AIsConstructor: Boolean): TValue;
  103. begin
  104. if isFunction(ACodeAddress) then
  105. Result := TJSFunction(ACodeAddress).apply(nil, AArgs)
  106. else
  107. raise EInvoke.Create(SErrInvokeInvalidCodeAddr);
  108. end;
  109. end.