objpas.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. This unit makes Free Pascal as much as possible Delphi compatible
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  12. procedure DoDispCallByIDError(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
  13. begin
  14. handleerroraddrframeind(RuntimeErrorExitCodes[reVarDispatch],
  15. get_pc_addr,get_frame);
  16. end;
  17. procedure fpc_dispatch_by_id(Result: Pointer; const Dispatch: pointer;
  18. DispDesc: Pointer; Params: Pointer); compilerproc;
  19. type
  20. TDispProc = procedure(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
  21. begin
  22. TDispProc(DispCallByIDProc)(Result,IDispatch(Dispatch),DispDesc,Params);
  23. end;
  24. {$endif FPC_HAS_FEATURE_VARIANTS}
  25. {****************************************************************************
  26. Internal Routines called from the Compiler
  27. ****************************************************************************}
  28. { the reverse order of the parameters make code generation easier }
  29. function fpc_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS']; compilerproc;
  30. begin
  31. fpc_do_is:=assigned(aobject) and assigned(aclass) and
  32. aobject.inheritsfrom(aclass);
  33. end;
  34. { the reverse order of the parameters make code generation easier }
  35. function fpc_do_as(aclass : tclass;aobject : tobject): tobject;[public,alias: 'FPC_DO_AS']; compilerproc;
  36. begin
  37. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  38. handleerroraddrframeInd(219,get_pc_addr,get_frame);
  39. result := aobject;
  40. end;
  41. { interface helpers }
  42. procedure fpc_intf_decr_ref(var i: pointer);[public,alias: 'FPC_INTF_DECR_REF']; compilerproc;
  43. begin
  44. if assigned(i) then
  45. begin
  46. IUnknown(i)._Release;
  47. i:=nil;
  48. end;
  49. end;
  50. { local declaration for intf_decr_ref for local access }
  51. procedure intf_decr_ref(var i: pointer); [external name 'FPC_INTF_DECR_REF'];
  52. procedure fpc_intf_incr_ref(i: pointer);[public,alias: 'FPC_INTF_INCR_REF']; compilerproc;
  53. begin
  54. if assigned(i) then
  55. IUnknown(i)._AddRef;
  56. end;
  57. { local declaration of intf_incr_ref for local access }
  58. procedure intf_incr_ref(i: pointer); [external name 'FPC_INTF_INCR_REF'];
  59. procedure fpc_intf_assign(var D: pointer; const S: pointer);[public,alias: 'FPC_INTF_ASSIGN']; compilerproc;
  60. begin
  61. if assigned(S) then
  62. IUnknown(S)._AddRef;
  63. if assigned(D) then
  64. IUnknown(D)._Release;
  65. D:=S;
  66. end;
  67. procedure fpc_intf_assign(var D: pointer; const s: pointer); [external name 'FPC_INTF_ASSIGN'];
  68. {procedure fpc_intf_assign_by_iid(var D: pointer; const S: pointer; const iid: TGUID);[public,alias: 'FPC_INTF_ASSIGN2']; compilerproc;
  69. var
  70. tmp : pointer;
  71. begin
  72. if assigned(S) then
  73. begin
  74. tmp:=nil;
  75. if IUnknown(S).QueryInterface(iid,tmp)<>S_OK then
  76. handleerror(219);
  77. if assigned(D) then
  78. IUnknown(D)._Release;
  79. D:=tmp;
  80. end
  81. else
  82. begin
  83. if assigned(D) then
  84. IUnknown(D)._Release;
  85. D:=nil;
  86. end;
  87. end;}
  88. function fpc_intf_is(const S: pointer; const iid: TGUID): Boolean;[public,alias: 'FPC_INTF_IS']; compilerproc;
  89. var
  90. tmpi: pointer;
  91. begin
  92. tmpi:=nil;
  93. fpc_intf_is:=Assigned(S) and (IUnknown(S).QueryInterface(iid,tmpi)=S_OK);
  94. if Assigned(tmpi) then
  95. IUnknown(tmpi)._Release;
  96. end;
  97. function fpc_intf_is_class(const S: pointer; const aclass: tclass): Boolean;[public,alias: 'FPC_INTF_IS_CLASS']; compilerproc;
  98. var
  99. tmpo: tobject;
  100. begin
  101. fpc_intf_is_class:=Assigned(S) and (IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.InheritsFrom(aclass);
  102. end;
  103. function fpc_class_is_intf(const S: pointer; const iid: TGUID): Boolean;[public,alias: 'FPC_CLASS_IS_INTF']; compilerproc;
  104. var
  105. tmpi: pointer;
  106. tmpi2: pointer; // weak!
  107. begin
  108. tmpi:=nil;
  109. tmpi2:=nil;
  110. fpc_class_is_intf:=Assigned(S) and ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or
  111. TObject(S).GetInterface(IID,tmpi));
  112. if Assigned(tmpi) then
  113. IUnknown(tmpi)._Release;
  114. end;
  115. function fpc_class_is_corbaintf(const S: pointer; const iid: Shortstring): Boolean;[public,alias: 'FPC_CLASS_IS_CORBAINTF']; compilerproc;
  116. begin
  117. fpc_class_is_corbaintf:=Assigned(S) and Assigned(TObject(S).GetInterfaceEntryByStr(iid));
  118. end;
  119. function fpc_intf_cast(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_CAST']; compilerproc;
  120. var
  121. tmpi: pointer;
  122. begin
  123. tmpi:=nil;
  124. if Assigned(S) and (IUnknown(S).QueryInterface(iid,tmpi)=S_OK) then
  125. pointer(fpc_intf_cast):=tmpi
  126. else
  127. fpc_intf_cast:= nil;
  128. end;
  129. function fpc_intf_cast_class(const S: pointer; const aclass: tclass): pointer;[public,alias: 'FPC_INTF_CAST_CLASS']; compilerproc;
  130. var
  131. tmpo: tobject;
  132. begin
  133. if Assigned(S) and (IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.InheritsFrom(aclass) then
  134. fpc_intf_cast_class:=tmpo
  135. else
  136. fpc_intf_cast_class:=nil;
  137. end;
  138. function fpc_class_cast_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_CAST_INTF']; compilerproc;
  139. var
  140. tmpi: pointer;
  141. tmpi2: pointer; // weak!
  142. begin
  143. tmpi:=nil;
  144. tmpi2:=nil;
  145. if Assigned(S) and ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or
  146. TObject(S).GetInterface(IID,tmpi)) then
  147. begin
  148. // decrease reference count
  149. fpc_class_cast_intf:=nil;
  150. pointer(fpc_class_cast_intf):=tmpi
  151. end
  152. else
  153. fpc_class_cast_intf:=nil;
  154. end;
  155. function fpc_class_cast_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_CAST_CORBAINTF']; compilerproc;
  156. var
  157. tmpi: pointer;
  158. begin
  159. if Assigned(S) and TObject(S).GetInterface(iid,tmpi) then
  160. fpc_class_cast_corbaintf:=tmpi
  161. else
  162. fpc_class_cast_corbaintf:=nil;
  163. end;
  164. function fpc_intf_as(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_AS']; compilerproc;
  165. var
  166. tmpi: pointer; // _AddRef before _Release
  167. begin
  168. if assigned(S) then
  169. begin
  170. tmpi:=nil;
  171. if IUnknown(S).QueryInterface(iid,tmpi)<>S_OK then
  172. handleerror(219);
  173. // decrease reference count
  174. fpc_intf_as:=nil;
  175. pointer(fpc_intf_as):=tmpi;
  176. end
  177. else
  178. fpc_intf_as:=nil;
  179. end;
  180. function fpc_intf_as_class(const S: pointer; const aclass: tclass): pointer;[public,alias: 'FPC_INTF_AS_CLASS']; compilerproc;
  181. var
  182. tmpo: tobject;
  183. begin
  184. if assigned(S) then
  185. begin
  186. if not ((IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.inheritsfrom(aclass)) then
  187. handleerror(219);
  188. fpc_intf_as_class:=tmpo;
  189. end
  190. else
  191. fpc_intf_as_class:=nil;
  192. end;
  193. function fpc_class_as_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_AS_INTF']; compilerproc;
  194. var
  195. tmpi: pointer; // _AddRef before _Release
  196. tmpi2: pointer; // weak!
  197. begin
  198. if assigned(S) then
  199. begin
  200. tmpi:=nil;
  201. tmpi2:=nil;
  202. if not ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or TObject(S).GetInterface(IID,tmpi)) then
  203. handleerror(219);
  204. // decrease reference count
  205. fpc_class_as_intf:=nil;
  206. pointer(fpc_class_as_intf):=tmpi;
  207. end
  208. else
  209. fpc_class_as_intf:=nil;
  210. end;
  211. function fpc_class_as_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_AS_CORBAINTF']; compilerproc;
  212. var
  213. tmpi: pointer; // _AddRef before _Release
  214. begin
  215. if assigned(S) then
  216. begin
  217. tmpi:=nil;
  218. if not TObject(S).GetInterface(iid,tmpi) then
  219. handleerror(219);
  220. fpc_class_as_corbaintf:=tmpi;
  221. end
  222. else
  223. fpc_class_as_corbaintf:=nil;
  224. end;
  225. {****************************************************************************
  226. TVMT
  227. ****************************************************************************}
  228. function TVmt.GetvParent: PVmt;
  229. begin
  230. {$ifdef VER3_0}
  231. GetvParent:=vParentRef;
  232. {$else VER3_0}
  233. if Assigned(vParentRef) then
  234. GetvParent:=vParentRef^
  235. else
  236. GetvParent:=Nil;
  237. {$endif VER3_0}
  238. end;
  239. {****************************************************************************
  240. TINTERFACEENTRY
  241. ****************************************************************************}
  242. function tinterfaceentry.GetIID: pguid;
  243. begin
  244. {$ifdef VER3_0}
  245. GetIID:=IIDRef;
  246. {$else VER3_0}
  247. if Assigned(IIDRef) then
  248. GetIID:=IIDRef^
  249. else
  250. GetIID:=Nil;
  251. {$endif VER3_0}
  252. end;
  253. function tinterfaceentry.GetIIDStr: pshortstring;
  254. begin
  255. {$ifdef VER3_0}
  256. GetIIDStr:=IIDStrRef;
  257. {$else VER3_0}
  258. if Assigned(IIDStrRef) then
  259. GetIIDStr:=IIDStrRef^
  260. else
  261. GetIIDStr:=Nil;
  262. {$endif VER3_0}
  263. end;
  264. {****************************************************************************
  265. TOBJECT
  266. ****************************************************************************}
  267. constructor TObject.Create;
  268. begin
  269. end;
  270. destructor TObject.Destroy;
  271. begin
  272. end;
  273. procedure TObject.Free;
  274. begin
  275. // the call via self avoids a warning
  276. if self<>nil then
  277. self.destroy;
  278. end;
  279. class function TObject.InstanceSize : SizeInt;
  280. begin
  281. InstanceSize := PVmt(Self)^.vInstanceSize;
  282. end;
  283. {$ifdef VER3_0}
  284. var
  285. emptyintf: ptruint; public name 'FPC_EMPTYINTF';
  286. {$endif VER3_0}
  287. procedure InitInterfacePointers(objclass: tclass;instance : pointer);
  288. var
  289. ovmt: PVmt;
  290. i: longint;
  291. intftable: pinterfacetable;
  292. Res: pinterfaceentry;
  293. begin
  294. ovmt := PVmt(objclass);
  295. while assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}assigned(ovmt^.vIntfTable){$endif} do
  296. begin
  297. intftable:=ovmt^.vIntfTable;
  298. {$ifdef VER3_0}
  299. if assigned(intftable) then
  300. {$endif VER3_0}
  301. begin
  302. i:=intftable^.EntryCount;
  303. Res:=@intftable^.Entries[0];
  304. while i>0 do begin
  305. if Res^.IType = etStandard then
  306. ppointer(@(pbyte(instance)[Res^.IOffset]))^:=
  307. pointer(Res^.VTable);
  308. inc(Res);
  309. dec(i);
  310. end;
  311. end;
  312. ovmt:=ovmt^.vParent;
  313. end;
  314. end;
  315. class function TObject.InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$ENDIF}
  316. begin
  317. { the size is saved at offset 0 }
  318. fillchar(instance^, InstanceSize, 0);
  319. { insert VMT pointer into the new created memory area }
  320. { (in class methods self contains the VMT!) }
  321. ppointer(instance)^:=pointer(self);
  322. if {$ifdef VER3_0}PVmt(self)^.vIntfTable <> @emptyintf{$else}assigned(PVmt(self)^.vIntfTable){$endif} then
  323. InitInterfacePointers(self,instance);
  324. InitInstance:=TObject(Instance);
  325. end;
  326. class function TObject.ClassParent : tclass;
  327. begin
  328. { type of self is class of tobject => it points to the vmt }
  329. { the parent vmt is saved at offset vmtParent }
  330. classparent:=tclass(PVmt(Self)^.vParent);
  331. end;
  332. class function TObject.NewInstance : tobject;
  333. var
  334. p : pointer;
  335. begin
  336. getmem(p, InstanceSize);
  337. if p <> nil then
  338. InitInstance(p);
  339. NewInstance:=TObject(p);
  340. end;
  341. procedure TObject.FreeInstance;
  342. begin
  343. CleanupInstance;
  344. FreeMem(Pointer(Self));
  345. end;
  346. class function TObject.ClassType : TClass;
  347. begin
  348. ClassType:=TClass(Pointer(Self))
  349. end;
  350. type
  351. tmethodnamerec = packed record
  352. name : pshortstring;
  353. addr : codepointer;
  354. end;
  355. tmethodnametable = packed record
  356. count : dword;
  357. entries : packed array[0..0] of tmethodnamerec;
  358. end;
  359. pmethodnametable = ^tmethodnametable;
  360. class function TObject.MethodAddress(const name : shortstring) : codepointer;
  361. var
  362. methodtable : pmethodnametable;
  363. i : dword;
  364. ovmt : PVmt;
  365. begin
  366. ovmt:=PVmt(self);
  367. while assigned(ovmt) do
  368. begin
  369. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  370. if assigned(methodtable) then
  371. begin
  372. for i:=0 to methodtable^.count-1 do
  373. if ShortCompareText(methodtable^.entries[i].name^, name)=0 then
  374. begin
  375. MethodAddress:=methodtable^.entries[i].addr;
  376. exit;
  377. end;
  378. end;
  379. ovmt := ovmt^.vParent;
  380. end;
  381. MethodAddress:=nil;
  382. end;
  383. class function TObject.MethodName(address : codepointer) : shortstring;
  384. var
  385. methodtable : pmethodnametable;
  386. i : dword;
  387. ovmt : PVmt;
  388. begin
  389. ovmt:=PVmt(self);
  390. while assigned(ovmt) do
  391. begin
  392. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  393. if assigned(methodtable) then
  394. begin
  395. for i:=0 to methodtable^.count-1 do
  396. if methodtable^.entries[i].addr=address then
  397. begin
  398. MethodName:=methodtable^.entries[i].name^;
  399. exit;
  400. end;
  401. end;
  402. ovmt := ovmt^.vParent;
  403. end;
  404. MethodName:='';
  405. end;
  406. function TObject.FieldAddress(const name : shortstring) : pointer;
  407. type
  408. PFieldInfo = ^TFieldInfo;
  409. TFieldInfo =
  410. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  411. packed
  412. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  413. record
  414. FieldOffset: SizeUInt;
  415. ClassTypeIndex: Word;
  416. Name: ShortString;
  417. end;
  418. PFieldTable = ^TFieldTable;
  419. TFieldTable =
  420. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  421. packed
  422. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  423. record
  424. FieldCount: Word;
  425. ClassTable: Pointer;
  426. { should be array[Word] of TFieldInfo; but
  427. Elements have variant size! force at least proper alignment }
  428. Fields: array[0..0] of TFieldInfo
  429. end;
  430. var
  431. ovmt: PVmt;
  432. FieldTable: PFieldTable;
  433. FieldInfo: PFieldInfo;
  434. i: longint;
  435. begin
  436. if Length(name) > 0 then
  437. begin
  438. ovmt := PVmt(ClassType);
  439. while ovmt <> nil do
  440. begin
  441. FieldTable := PFieldTable(ovmt^.vFieldTable);
  442. if FieldTable <> nil then
  443. begin
  444. FieldInfo := @FieldTable^.Fields[0];
  445. for i := 0 to FieldTable^.FieldCount - 1 do
  446. begin
  447. if ShortCompareText(FieldInfo^.Name, name) = 0 then
  448. begin
  449. fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
  450. exit;
  451. end;
  452. FieldInfo := PFieldInfo(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
  453. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  454. { align to largest field of TFieldInfo }
  455. FieldInfo := Align(FieldInfo, SizeOf(PtrUInt));
  456. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  457. end;
  458. end;
  459. { Try again with the parent class type }
  460. ovmt:=ovmt^.vParent;
  461. end;
  462. end;
  463. fieldaddress:=nil;
  464. end;
  465. function TObject.SafeCallException(exceptobject : tobject;
  466. exceptaddr : codepointer) : HResult;
  467. begin
  468. safecallexception:=E_UNEXPECTED;
  469. end;
  470. class function TObject.ClassInfo : pointer;
  471. begin
  472. ClassInfo := PVmt(Self)^.vTypeInfo;
  473. end;
  474. class function TObject.ClassName : ShortString;
  475. begin
  476. ClassName := PVmt(Self)^.vClassName^;
  477. end;
  478. class function TObject.ClassNameIs(const name : string) : boolean;
  479. begin
  480. // call to ClassName inlined here, this eliminates stack and string copying.
  481. ClassNameIs:=ShortCompareText(PVmt(Self)^.vClassName^, name) = 0;
  482. end;
  483. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  484. var
  485. vmt: PVmt;
  486. begin
  487. if assigned(aclass) then
  488. begin
  489. vmt:=PVmt(self);
  490. while assigned(vmt) and (vmt <> PVmt(aclass)) do
  491. vmt := vmt^.vParent;
  492. InheritsFrom := (vmt = PVmt(aclass));
  493. end
  494. else
  495. inheritsFrom := False;
  496. end;
  497. class function TObject.stringmessagetable : pstringmessagetable;
  498. begin
  499. stringmessagetable:=PVmt(Self)^.vMsgStrPtr;
  500. end;
  501. type
  502. tmessagehandler = procedure(var msg) of object;
  503. procedure TObject.Dispatch(var message);
  504. type
  505. {$PUSH}
  506. {$PACKRECORDS NORMAL}
  507. PMsgIntTable = ^TMsgIntTable;
  508. TMsgIntTable = record
  509. index : dword;
  510. method : codepointer;
  511. end;
  512. PMsgInt = ^TMsgInt;
  513. TMsgInt = record
  514. count : longint;
  515. msgs : array[0..0] of TMsgIntTable;
  516. end;
  517. {$POP}
  518. var
  519. index : dword;
  520. count,i : longint;
  521. msgtable : PMsgIntTable;
  522. p : PMsgInt;
  523. ovmt : PVmt;
  524. msghandler : tmessagehandler;
  525. begin
  526. index:=dword(message);
  527. ovmt := PVmt(ClassType);
  528. while assigned(ovmt) do
  529. begin
  530. // See if we have messages at all in this class.
  531. p:=PMsgInt(ovmt^.vDynamicTable);
  532. If Assigned(p) then
  533. begin
  534. msgtable:=@p^.msgs;
  535. count:=p^.count;
  536. end
  537. else
  538. Count:=0;
  539. { later, we can implement a binary search here }
  540. for i:=0 to count-1 do
  541. begin
  542. if index=msgtable[i].index then
  543. begin
  544. TMethod(msghandler).Code:=msgtable[i].method;
  545. TMethod(msghandler).Data:=self;
  546. msghandler(message);
  547. exit;
  548. end;
  549. end;
  550. ovmt:=ovmt^.vParent;
  551. end;
  552. DefaultHandler(message);
  553. end;
  554. procedure TObject.DispatchStr(var message);
  555. var
  556. name : shortstring;
  557. count,i : longint;
  558. msgstrtable : pmsgstrtable;
  559. p: pstringmessagetable;
  560. ovmt : PVmt;
  561. msghandler : tmessagehandler;
  562. begin
  563. name:=pshortstring(@message)^;
  564. ovmt:=PVmt(ClassType);
  565. while assigned(ovmt) do
  566. begin
  567. p := ovmt^.vMsgStrPtr;
  568. if (P<>Nil) and (p^.count<>0) then
  569. begin
  570. count:=p^.count;
  571. msgstrtable:=@p^.msgstrtable;
  572. end
  573. else
  574. Count:=0;
  575. { later, we can implement a binary search here }
  576. for i:=0 to count-1 do
  577. begin
  578. if name=msgstrtable[i].name^ then
  579. begin
  580. TMethod(msghandler).Code:=msgstrtable[i].method;
  581. TMethod(msghandler).Data:=self;
  582. msghandler(message);
  583. exit;
  584. end;
  585. end;
  586. ovmt:=ovmt^.vParent;
  587. end;
  588. DefaultHandlerStr(message);
  589. end;
  590. procedure TObject.DefaultHandler(var message);
  591. begin
  592. end;
  593. procedure TObject.DefaultHandlerStr(var message);
  594. begin
  595. end;
  596. procedure TObject.CleanupInstance;
  597. var
  598. vmt : PVmt;
  599. temp : pointer;
  600. begin
  601. vmt := PVmt(ClassType);
  602. while vmt<>nil do
  603. begin
  604. Temp:= vmt^.vInitTable;
  605. { The RTTI format matches one for records, except the type is tkClass.
  606. Since RecordRTTI does not check the type, calling it yields the desired result. }
  607. if Assigned(Temp) then
  608. RecordRTTI(Self,Temp,@int_finalize);
  609. vmt:= vmt^.vParent;
  610. end;
  611. end;
  612. procedure TObject.AfterConstruction;
  613. begin
  614. end;
  615. procedure TObject.BeforeDestruction;
  616. begin
  617. end;
  618. function IsGUIDEqual(const guid1, guid2: tguid): boolean;
  619. begin
  620. IsGUIDEqual:=
  621. (guid1.D1=guid2.D1) and
  622. (PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
  623. (PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
  624. (PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
  625. end;
  626. // Use of managed types should be avoided here; implicit _Addref/_Release
  627. // will end up in unpredictable behaviour if called on CORBA interfaces.
  628. type
  629. TInterfaceGetter = procedure(out Obj) of object;
  630. TClassGetter = function: TObject of object;
  631. function GetInterfaceByEntry(Instance: pointer; IEntry: pinterfaceentry; out obj): boolean;
  632. var
  633. Getter: TMethod;
  634. begin
  635. Pointer(Obj) := nil;
  636. Getter.Data := Instance;
  637. if Assigned(IEntry) and Assigned(Instance) then
  638. begin
  639. case IEntry^.IType of
  640. etStandard:
  641. Pointer(Obj) := PByte(instance)+IEntry^.IOffset;
  642. etFieldValue, etFieldValueClass:
  643. Pointer(obj) := PPointer(PByte(Instance)+IEntry^.IOffset)^;
  644. etVirtualMethodResult:
  645. begin
  646. // IOffset is relative to the VMT, not to instance.
  647. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  648. TInterfaceGetter(Getter)(obj);
  649. end;
  650. etVirtualMethodClass:
  651. begin
  652. // IOffset is relative to the VMT, not to instance.
  653. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  654. TObject(obj) := TClassGetter(Getter)();
  655. end;
  656. etStaticMethodResult:
  657. begin
  658. Getter.code := IEntry^.IOffsetAsCodePtr;
  659. TInterfaceGetter(Getter)(obj);
  660. end;
  661. etStaticMethodClass:
  662. begin
  663. Getter.code := IEntry^.IOffsetAsCodePtr;
  664. TObject(obj) := TClassGetter(Getter)();
  665. end;
  666. end;
  667. end;
  668. result := assigned(pointer(obj));
  669. end;
  670. function TObject.GetInterface(const iid : tguid;out obj) : boolean;
  671. var
  672. IEntry: PInterfaceEntry;
  673. Instance: TObject;
  674. begin
  675. if IsGUIDEqual(IObjectInstance,iid) then
  676. begin
  677. TObject(Obj) := Self;
  678. Result := True;
  679. Exit;
  680. end;
  681. Instance := self;
  682. repeat
  683. IEntry := Instance.GetInterfaceEntry(iid);
  684. result := GetInterfaceByEntry(Instance, IEntry, obj);
  685. if (not result) or
  686. (IEntry^.IType in [etStandard, etFieldValue,
  687. etStaticMethodResult, etVirtualMethodResult]) then
  688. Break;
  689. { if interface is implemented by a class-type property or field,
  690. continue search }
  691. Instance := TObject(obj);
  692. until False;
  693. { Getter function will normally AddRef, so adding another reference here
  694. will cause memleak. }
  695. if result and (IEntry^.IType in [etStandard, etFieldValue]) then
  696. IInterface(obj)._AddRef;
  697. end;
  698. function TObject.GetInterfaceWeak(const iid : tguid; out obj) : boolean;
  699. var
  700. IEntry: PInterfaceEntry;
  701. Instance: TObject;
  702. begin
  703. if IsGUIDEqual(IObjectInstance,iid) then
  704. begin
  705. TObject(Obj) := Self;
  706. Result := True;
  707. Exit;
  708. end;
  709. Instance := self;
  710. repeat
  711. IEntry := Instance.GetInterfaceEntry(iid);
  712. result := GetInterfaceByEntry(Instance, IEntry, obj);
  713. if (not result) or
  714. (IEntry^.IType in [etStandard, etFieldValue,
  715. etStaticMethodResult, etVirtualMethodResult]) then
  716. Break;
  717. { if interface is implemented by a class-type property or field,
  718. continue search }
  719. Instance := TObject(obj);
  720. until False;
  721. { Getter function will normally AddRef, so we have to release it,
  722. else the ref is not weak. }
  723. if result and not (IEntry^.IType in [etStandard, etFieldValue]) then
  724. IInterface(obj)._Release;
  725. end;
  726. function TObject.GetInterfaceByStr(const iidstr : shortstring;out obj) : boolean;
  727. var
  728. IEntry: PInterfaceEntry;
  729. Instance: TObject;
  730. begin
  731. Instance := self;
  732. repeat
  733. IEntry := Instance.GetInterfaceEntryByStr(iidstr);
  734. result := GetInterfaceByEntry(Instance, IEntry, obj);
  735. if (not result) or
  736. (IEntry^.IType in [etStandard, etFieldValue,
  737. etStaticMethodResult, etVirtualMethodResult]) then
  738. Break;
  739. { if interface is implemented by a class-type property or field,
  740. continue search }
  741. Instance := TObject(obj);
  742. until False;
  743. { Getter function will normally AddRef, so adding another reference here
  744. will cause memleak. (com interfaces only!) }
  745. if result and Assigned(IEntry^.IID) and (IEntry^.IType in [etStandard, etFieldValue]) then
  746. IInterface(obj)._AddRef;
  747. end;
  748. function TObject.GetInterface(const iidstr : shortstring;out obj) : boolean;
  749. begin
  750. Result := GetInterfaceByStr(iidstr,obj);
  751. end;
  752. class function TObject.GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  753. var
  754. i: longint;
  755. intftable: pinterfacetable;
  756. ovmt: PVmt;
  757. begin
  758. ovmt := PVmt(Self);
  759. while Assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}Assigned(ovmt^.vIntftable){$endif} do
  760. begin
  761. intftable:=ovmt^.vIntfTable;
  762. {$ifdef VER3_0}
  763. if assigned(intftable) then
  764. {$endif VER3_0}
  765. begin
  766. for i:=0 to intftable^.EntryCount-1 do
  767. begin
  768. result:=@intftable^.Entries[i];
  769. if assigned(Result^.iid) and IsGUIDEqual(Result^.iid^,iid) then
  770. Exit;
  771. end;
  772. end;
  773. ovmt := ovmt^.vParent;
  774. end;
  775. result := nil;
  776. end;
  777. class function TObject.GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  778. var
  779. i: longint;
  780. intftable: pinterfacetable;
  781. ovmt: PVmt;
  782. begin
  783. ovmt := PVmt(Self);
  784. while Assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}Assigned(ovmt^.vIntfTable){$endif} do
  785. begin
  786. intftable:=ovmt^.vIntfTable;
  787. {$ifdef VER3_0}
  788. if assigned(intftable) then
  789. {$endif VER3_0}
  790. begin
  791. for i:=0 to intftable^.EntryCount-1 do
  792. begin
  793. result:=@intftable^.Entries[i];
  794. if assigned(result^.iidstr) and (result^.iidstr^ = iidstr) then
  795. Exit;
  796. end;
  797. end;
  798. ovmt := ovmt^.vParent;
  799. end;
  800. result:=nil;
  801. end;
  802. class function TObject.GetInterfaceTable : pinterfacetable;
  803. begin
  804. getinterfacetable:=PVmt(Self)^.vIntfTable;
  805. end;
  806. class function TObject.UnitName : {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  807. type
  808. // from the typinfo unit
  809. TClassTypeInfo = {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  810. ClassType: TClass;
  811. ParentInfo: Pointer;
  812. PropCount: SmallInt;
  813. UnitName: ShortString;
  814. end;
  815. PClassTypeInfo = ^TClassTypeInfo;
  816. var
  817. classtypeinfo: PClassTypeInfo;
  818. begin
  819. classtypeinfo:=ClassInfo;
  820. if Assigned(classtypeinfo) then
  821. begin
  822. // offset PTypeInfo by Length(Name) + 2 (ShortString length byte + SizeOf(Kind))
  823. inc(Pointer(classtypeinfo), PByte(Pointer(classtypeinfo)+1)^ + 2);
  824. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  825. classtypeinfo:=align(classtypeinfo,sizeof(classtypeinfo));
  826. {$endif}
  827. result:=classtypeinfo^.UnitName;
  828. end
  829. else
  830. result:='';
  831. end;
  832. class function TObject.QualifiedClassName: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  833. var
  834. uname: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  835. begin
  836. uname := UnitName; //TODO: change 'UnitName' to 'UnitScope' as soon as RTL implement it
  837. if uname='' then
  838. result:=ClassName
  839. else
  840. result:=Concat(uname, '.', ClassName);
  841. end;
  842. function TObject.Equals(Obj: TObject) : boolean;
  843. begin
  844. result:=Obj=Self;
  845. end;
  846. function TObject.GetHashCode: PtrInt;
  847. begin
  848. result:=PtrInt(Self);
  849. end;
  850. function TObject.ToString: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  851. begin
  852. result:=ClassName;
  853. end;
  854. {****************************************************************************
  855. TINTERFACEDOBJECT
  856. ****************************************************************************}
  857. function TInterfacedObject.QueryInterface(
  858. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  859. begin
  860. if getinterface(iid,obj) then
  861. result:=S_OK
  862. else
  863. result:=longint(E_NOINTERFACE);
  864. end;
  865. function TInterfacedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  866. begin
  867. _addref:=interlockedincrement(frefcount);
  868. end;
  869. function TInterfacedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  870. begin
  871. _Release:=interlockeddecrement(frefcount);
  872. if _Release=0 then
  873. self.destroy;
  874. end;
  875. procedure TInterfacedObject.AfterConstruction;
  876. begin
  877. { we need to fix the refcount we forced in newinstance }
  878. { further, it must be done in a thread safe way }
  879. declocked(frefcount);
  880. end;
  881. procedure TInterfacedObject.BeforeDestruction;
  882. begin
  883. if frefcount<>0 then
  884. HandleError(204);
  885. end;
  886. class function TInterfacedObject.NewInstance : TObject;
  887. begin
  888. NewInstance:=inherited NewInstance;
  889. if NewInstance<>nil then
  890. TInterfacedObject(NewInstance).frefcount:=1;
  891. end;
  892. {****************************************************************************
  893. TAGGREGATEDOBJECT
  894. ****************************************************************************}
  895. constructor TAggregatedObject.Create(const aController: IUnknown);
  896. begin
  897. inherited Create;
  898. { do not keep a counted reference to the controller! }
  899. fcontroller := Pointer(aController);
  900. end;
  901. function TAggregatedObject.QueryInterface(
  902. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  903. begin
  904. Result := IUnknown(fcontroller).QueryInterface(iid, obj);
  905. end;
  906. function TAggregatedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  907. begin
  908. Result := IUnknown(fcontroller)._AddRef;
  909. end;
  910. function TAggregatedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  911. begin
  912. Result := IUnknown(fcontroller)._Release;
  913. end;
  914. function TAggregatedObject.GetController : IUnknown;
  915. begin
  916. Result := IUnknown(fcontroller);
  917. end;
  918. {****************************************************************************
  919. TContainedOBJECT
  920. ****************************************************************************}
  921. function TContainedObject.QueryInterface(
  922. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  923. begin
  924. if getinterface(iid,obj) then
  925. result:=S_OK
  926. else
  927. result:=longint(E_NOINTERFACE);
  928. end;
  929. {****************************************************************************
  930. Exception Support
  931. ****************************************************************************}
  932. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  933. {$i except.inc}
  934. {$endif FPC_HAS_FEATURE_EXCEPTIONS}