rtti.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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;
  15. type
  16. TVirtualInterfaceInvokeEvent = function(const aMethodName: string;
  17. const Args: TJSValueDynArray): JSValue of object;
  18. { TVirtualInterface: A class that can implement any IInterface. Any method
  19. call is handled by the OnInvoke event. }
  20. TVirtualInterface = class(TInterfacedObject, IInterface)
  21. private
  22. FOnInvoke: TVirtualInterfaceInvokeEvent;
  23. public
  24. constructor Create(InterfaceTypeInfo: Pointer); overload; assembler;
  25. constructor Create(InterfaceTypeInfo: Pointer;
  26. const InvokeEvent: TVirtualInterfaceInvokeEvent); overload;
  27. property OnInvoke: TVirtualInterfaceInvokeEvent read FOnInvoke write FOnInvoke;
  28. end;
  29. procedure CreateVirtualCorbaInterface(InterfaceTypeInfo: Pointer;
  30. const MethodImplementation: TVirtualInterfaceInvokeEvent; out IntfVar); assembler;
  31. implementation
  32. procedure CreateVirtualCorbaInterface(InterfaceTypeInfo: Pointer;
  33. const MethodImplementation: TVirtualInterfaceInvokeEvent; out IntfVar); assembler;
  34. asm
  35. var IntfType = InterfaceTypeInfo.interface;
  36. var i = Object.create(IntfType);
  37. var o = { $name: "virtual", $fullname: "virtual" };
  38. i.$o = o;
  39. do {
  40. var names = IntfType.$names;
  41. if (!names) break;
  42. for (var j=0; j<names.length; j++){
  43. let fnname = names[j];
  44. i[fnname] = function(){ return MethodImplementation(fnname,arguments); };
  45. }
  46. IntfType = Object.getPrototypeOf(IntfType);
  47. } while(IntfType!=null);
  48. IntfVar.set(i);
  49. end;
  50. { TVirtualInterface }
  51. constructor TVirtualInterface.Create(InterfaceTypeInfo: Pointer); assembler;
  52. asm
  53. var IntfType = InterfaceTypeInfo.interface;
  54. if (IntfType.$kind !== 'com') rtl.raiseE('EInvalidCast');
  55. var guid = IntfType.$guid;
  56. var i = Object.create(IntfType); // needed by IntfVar is IntfType
  57. i.$o = this;
  58. // copy IInterface methods: _AddRef, _Release, QueryInterface
  59. var iinterfaceguid = '{00000000-0000-0000-C000-000000000046}';
  60. var map = this.$intfmaps[iinterfaceguid];
  61. for (var key in map){
  62. var v = map[key];
  63. if (typeof(v)!=='function') continue;
  64. i[key] = map[key];
  65. }
  66. // all other methods call OnInvoke
  67. do {
  68. var names = IntfType.$names;
  69. if (!names) break;
  70. for (var j=0; j<names.length; j++){
  71. let fnname = names[j];
  72. if (i[fnname]) continue;
  73. i[fnname] = function(){ return this.$o.FOnInvoke(fnname,arguments); };
  74. }
  75. IntfType = Object.getPrototypeOf(IntfType);
  76. } while(IntfType!=null);
  77. // create a new list of interface map, supporting IInterface and IntfType
  78. this.$intfmaps = {};
  79. this.$intfmaps[iinterfaceguid] = map;
  80. this.$intfmaps[guid] = {};
  81. // store the implementation of IntfType (used by the as-operator)
  82. this.$interfaces = {};
  83. this.$interfaces[guid] = i;
  84. end;
  85. constructor TVirtualInterface.Create(InterfaceTypeInfo: Pointer;
  86. const InvokeEvent: TVirtualInterfaceInvokeEvent);
  87. begin
  88. Create(InterfaceTypeInfo);
  89. OnInvoke:=InvokeEvent;
  90. end;
  91. end.