tcoptimizations.pas 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2017 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  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. Examples:
  12. ./testpas2js --suite=TTestOptimizations
  13. ./testpas2js --suite=TTestOptimizations.TestOmitLocalVar
  14. }
  15. unit TCOptimizations;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, testregistry, fppas2js, pastree,
  20. PScanner, Pas2jsUseAnalyzer, PasResolver, PasResolveEval,
  21. TCModules;
  22. type
  23. { TCustomTestOptimizations }
  24. TCustomTestOptimizations = class(TCustomTestModule)
  25. private
  26. FAnalyzerModule: TPas2JSAnalyzer;
  27. FAnalyzerProgram: TPas2JSAnalyzer;
  28. FWholeProgramOptimization: boolean;
  29. function OnConverterIsElementUsed(Sender: TObject; El: TPasElement): boolean;
  30. function OnConverterIsTypeInfoUsed(Sender: TObject; El: TPasElement): boolean;
  31. protected
  32. procedure SetUp; override;
  33. procedure TearDown; override;
  34. procedure ParseModule; override;
  35. procedure ParseProgram; override;
  36. function CreateConverter: TPasToJSConverter; override;
  37. public
  38. property AnalyzerModule: TPas2JSAnalyzer read FAnalyzerModule;
  39. property AnalyzerProgram: TPas2JSAnalyzer read FAnalyzerProgram;
  40. property WholeProgramOptimization: boolean read FWholeProgramOptimization
  41. write FWholeProgramOptimization;
  42. end;
  43. { TTestOptimizations }
  44. TTestOptimizations = class(TCustomTestOptimizations)
  45. published
  46. // unit optimization: jsshortrefglobals
  47. procedure TestOptShortRefGlobals_Program;
  48. procedure TestOptShortRefGlobals_Unit_FromIntfImpl_ToIntfImpl;
  49. procedure TestOptShortRefGlobals_Enums;
  50. procedure TestOptShortRefGlobals_Property;
  51. procedure TestOptShortRefGlobals_ExternalAbstract;
  52. procedure TestOptShortRefGlobals_Class;
  53. procedure TestOptShortRefGlobals_GenericFunction;
  54. procedure TestOptShortRefGlobals_GenericMethod_Call;
  55. procedure TestOptShortRefGlobals_GenericStaticMethod_Call;
  56. // ToDo: GenericMethod_CallInherited ObjFPC+Delphi
  57. procedure TestOptShortRefGlobals_GenericClassHelperMethod;
  58. procedure TestOptShortRefGlobals_GenericMethod_ProcVar;
  59. procedure TestOptShortRefGlobals_GenericStaticMethod_ProcVar;
  60. procedure TestOptShortRefGlobals_SameUnit_EnumType;
  61. procedure TestOptShortRefGlobals_SameUnit_ClassType;
  62. procedure TestOptShortRefGlobals_SameUnit_RecordType;
  63. procedure TestOptShortRefGlobals_Unit_InitNoImpl;
  64. // Whole Program Optimization
  65. procedure TestWPO_OmitLocalVar;
  66. procedure TestWPO_OmitLocalProc;
  67. procedure TestWPO_OmitLocalProcForward;
  68. procedure TestWPO_OmitProcLocalVar;
  69. procedure TestWPO_OmitProcLocalConst;
  70. procedure TestWPO_OmitProcLocalType;
  71. procedure TestWPO_OmitProcLocalProc;
  72. procedure TestWPO_OmitProcLocalForwardProc;
  73. procedure TestWPO_OmitRecordMember;
  74. procedure TestWPO_OmitNotUsedTObject;
  75. procedure TestWPO_TObject;
  76. procedure TestWPO_Class_Property;
  77. procedure TestWPO_Class_OmitField;
  78. procedure TestWPO_Class_OmitMethod;
  79. procedure TestWPO_Class_OmitClassMethod;
  80. procedure TestWPO_Class_OmitPropertyGetter1;
  81. procedure TestWPO_Class_OmitPropertyGetter2;
  82. procedure TestWPO_Class_OmitPropertySetter1;
  83. procedure TestWPO_Class_OmitPropertySetter2;
  84. procedure TestWPO_Class_KeepNewInstance;
  85. procedure TestWPO_CallInherited;
  86. procedure TestWPO_UseUnit;
  87. procedure TestWPO_ArrayOfConst_Use;
  88. procedure TestWPO_ArrayOfConst_NotUsed;
  89. procedure TestWPO_Class_PropertyInOtherUnit;
  90. procedure TestWPO_ProgramPublicDeclaration;
  91. procedure TestWPO_ConstructorDefaultValueConst;
  92. procedure TestWPO_RTTI_PublishedField;
  93. procedure TestWPO_RTTI_TypeInfo;
  94. end;
  95. implementation
  96. { TCustomTestOptimizations }
  97. function TCustomTestOptimizations.OnConverterIsElementUsed(Sender: TObject;
  98. El: TPasElement): boolean;
  99. var
  100. A: TPas2JSAnalyzer;
  101. begin
  102. if WholeProgramOptimization then
  103. A:=AnalyzerProgram
  104. else if Sender=Converter then
  105. A:=AnalyzerModule
  106. else
  107. begin
  108. {$IF defined(VerbosePas2JS) or defined(VerbosePasAnalyzer)}
  109. writeln('TCustomTestOptimizations.OnConverterIsElementUsed El=',GetObjName(El),' WPO=',WholeProgramOptimization,' Sender=',GetObjName(Sender));
  110. {$ENDIF}
  111. Fail('converting other unit without WPO');
  112. end;
  113. Result:=A.IsUsed(El);
  114. {$IF defined(VerbosePas2JS) or defined(VerbosePasAnalyzer)}
  115. writeln('TCustomTestOptimizations.OnConverterIsElementUsed El=',GetObjName(El),' WPO=',WholeProgramOptimization,' Result=',Result);
  116. {$ENDIF}
  117. end;
  118. function TCustomTestOptimizations.OnConverterIsTypeInfoUsed(Sender: TObject;
  119. El: TPasElement): boolean;
  120. var
  121. A: TPas2JSAnalyzer;
  122. begin
  123. if WholeProgramOptimization then
  124. A:=AnalyzerProgram
  125. else if Sender=Converter then
  126. A:=AnalyzerModule
  127. else
  128. begin
  129. {$IF defined(VerbosePas2JS) or defined(VerbosePasAnalyzer)}
  130. writeln('TCustomTestOptimizations.OnConverterIsTypeInfoUsed El=',GetObjName(El),' WPO=',WholeProgramOptimization,' Sender=',GetObjName(Sender));
  131. {$ENDIF}
  132. Fail('converting other unit without WPO');
  133. end;
  134. Result:=A.IsTypeInfoUsed(El);
  135. {$IF defined(VerbosePas2JS) or defined(VerbosePasAnalyzer)}
  136. writeln('TCustomTestOptimizations.OnConverterIsTypeInfoUsed El=',GetObjName(El),' WPO=',WholeProgramOptimization,' Result=',Result);
  137. {$ENDIF}
  138. end;
  139. procedure TCustomTestOptimizations.SetUp;
  140. begin
  141. inherited SetUp;
  142. FWholeProgramOptimization:=false;
  143. FAnalyzerModule:=TPas2JSAnalyzer.Create;
  144. FAnalyzerModule.Resolver:=Engine;
  145. FAnalyzerProgram:=TPas2JSAnalyzer.Create;
  146. FAnalyzerProgram.Resolver:=Engine;
  147. end;
  148. procedure TCustomTestOptimizations.TearDown;
  149. begin
  150. FreeAndNil(FAnalyzerProgram);
  151. FreeAndNil(FAnalyzerModule);
  152. inherited TearDown;
  153. end;
  154. procedure TCustomTestOptimizations.ParseModule;
  155. begin
  156. inherited ParseModule;
  157. {$IF defined(VerbosePas2JS) or defined(VerbosePasAnalyzer)}
  158. writeln('TCustomTestOptimizations.ParseModule START');
  159. {$ENDIF}
  160. AnalyzerModule.AnalyzeModule(Module);
  161. {$IF defined(VerbosePas2JS) or defined(VerbosePasAnalyzer)}
  162. writeln('TCustomTestOptimizations.ParseModule END');
  163. {$ENDIF}
  164. end;
  165. procedure TCustomTestOptimizations.ParseProgram;
  166. begin
  167. WholeProgramOptimization:=true;
  168. inherited ParseProgram;
  169. {$IF defined(VerbosePas2JS) or defined(VerbosePasAnalyzer)}
  170. writeln('TCustomTestOptimizations.ParseProgram START');
  171. {$ENDIF}
  172. AnalyzerProgram.AnalyzeWholeProgram(Module as TPasProgram);
  173. {$IF defined(VerbosePas2JS) or defined(VerbosePasAnalyzer)}
  174. writeln('TCustomTestOptimizations.ParseProgram START');
  175. {$ENDIF}
  176. end;
  177. function TCustomTestOptimizations.CreateConverter: TPasToJSConverter;
  178. begin
  179. Result:=inherited CreateConverter;
  180. Result.OnIsElementUsed:=@OnConverterIsElementUsed;
  181. Result.OnIsTypeInfoUsed:=@OnConverterIsTypeInfoUsed;
  182. end;
  183. { TTestOptimizations }
  184. procedure TTestOptimizations.TestOptShortRefGlobals_Program;
  185. begin
  186. AddModuleWithIntfImplSrc('UnitA.pas',
  187. LinesToStr([
  188. 'type',
  189. ' TColor = (red,green,blue);',
  190. ' TColors = set of TColor;',
  191. 'const',
  192. ' cRedBlue = [red,blue];',
  193. 'type',
  194. ' TBird = class',
  195. ' public',
  196. ' class var c: word;',
  197. ' class function Run(w: word): word; virtual; abstract;',
  198. ' end;',
  199. ' TRec = record',
  200. ' x: word;',
  201. ' end;',
  202. 'var b: TBird;',
  203. '']),
  204. LinesToStr([
  205. '']));
  206. StartProgram(true,[supTObject]);
  207. Add([
  208. '{$optimization JSShortRefGlobals}',
  209. 'uses unita;',
  210. 'type',
  211. ' TEagle = class(TBird)',
  212. ' class function Run(w: word = 5): word; override;',
  213. ' end;',
  214. 'class function TEagle.Run(w: word): word;',
  215. 'begin',
  216. 'end;',
  217. 'var',
  218. ' e: TEagle;',
  219. ' r: TRec;',
  220. ' c: TColors;',
  221. 'begin',
  222. ' e:=TEagle.Create;',
  223. ' b:=TBird.Create;',
  224. ' e.c:=e.c+1;',
  225. ' r.x:=TBird.c;',
  226. ' r.x:=b.c;',
  227. ' r.x:=e.Run;',
  228. ' r.x:=e.Run();',
  229. ' r.x:=e.Run(4);',
  230. ' c:=cRedBlue;',
  231. '']);
  232. ConvertProgram;
  233. CheckSource('TestOptShortRefGlobals_Program',
  234. LinesToStr([
  235. 'var $lt = null;',
  236. 'var $lm = pas.UnitA;',
  237. 'var $lt1 = $lm.TBird;',
  238. 'var $lt2 = $lm.TRec;',
  239. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  240. ' $lt = this;',
  241. ' this.Run = function (w) {',
  242. ' var Result = 0;',
  243. ' return Result;',
  244. ' };',
  245. '});',
  246. 'this.e = null;',
  247. 'this.r = $lt2.$new();',
  248. 'this.c = {};',
  249. '']),
  250. LinesToStr([
  251. '$mod.e = $lt.$create("Create");',
  252. '$lm.b = $lt1.$create("Create");',
  253. '$lt1.c = $mod.e.c + 1;',
  254. '$mod.r.x = $lt1.c;',
  255. '$mod.r.x = $lm.b.c;',
  256. '$mod.r.x = $mod.e.$class.Run(5);',
  257. '$mod.r.x = $mod.e.$class.Run(5);',
  258. '$mod.r.x = $mod.e.$class.Run(4);',
  259. '$mod.c = rtl.refSet($lm.cRedBlue);',
  260. '']));
  261. end;
  262. procedure TTestOptimizations.TestOptShortRefGlobals_Unit_FromIntfImpl_ToIntfImpl;
  263. begin
  264. AddModuleWithIntfImplSrc('UnitA.pas',
  265. LinesToStr([
  266. 'type',
  267. ' TBird = class',
  268. ' public Speed: word;',
  269. ' end;',
  270. ' TRecA = record',
  271. ' x: word;',
  272. ' end;',
  273. 'var Bird: TBird;',
  274. '']),
  275. LinesToStr([
  276. '']));
  277. AddModuleWithIntfImplSrc('UnitB.pas',
  278. LinesToStr([
  279. 'type',
  280. ' TAnt = class',
  281. ' public Size: word;',
  282. ' end;',
  283. ' TRecB = record',
  284. ' y: word;',
  285. ' end;',
  286. ' TBear = class',
  287. ' end;',
  288. ' TFrog = class',
  289. ' end;',
  290. 'var Ant: TAnt;',
  291. '']),
  292. LinesToStr([
  293. '']));
  294. StartUnit(true,[supTObject]);
  295. Add([
  296. '{$optimization JSShortRefGlobals}',
  297. 'interface',
  298. 'uses unita;',
  299. 'type',
  300. ' TEagle = class(TBird)', // intf-JS to intf-uses
  301. ' procedure Fly;',
  302. ' end;',
  303. 'implementation',
  304. 'uses unitb;',
  305. 'type',
  306. ' TRedAnt = class(TAnt)', // impl-JS to impl-uses
  307. ' procedure Run;',
  308. ' end;',
  309. 'procedure TEagle.Fly;',
  310. 'begin',
  311. ' TRedAnt.Create;', // intf-JS to impl-JS
  312. ' TAnt.Create;', // intf-JS to impl-uses
  313. ' TBird.Create;', // intf-JS to intf-uses
  314. ' TEagle.Create;', // intf-JS to intf-JS
  315. 'end;',
  316. 'procedure TRedAnt.Run;',
  317. 'begin',
  318. ' TRedAnt.Create;', // impl-JS to impl-JS
  319. ' TAnt.Create;', // impl-JS to impl-uses
  320. ' TBird.Create;', // impl-JS to intf-uses
  321. ' TEagle.Create;', // impl-JS to intf-JS
  322. ' TBear.Create', // only in impl-JS to impl-uses
  323. 'end;',
  324. 'var',
  325. ' RedAnt: TRedAnt;',
  326. ' Ant: TAnt;',
  327. ' Bird: TBird;',
  328. ' Eagle: TEagle;',
  329. 'initialization',
  330. ' RedAnt:=TRedAnt.Create;', // init to impl-JS
  331. ' Ant:=TAnt.Create;', // init to impl-uses
  332. ' Bird:=TBird.Create;', // init to intf-uses
  333. ' Eagle:=TEagle.Create;', // init to intf-JS
  334. ' TFrog.Create;', // only in init to impl-uses
  335. ' Eagle.Fly;',
  336. ' RedAnt.Run;',
  337. '']);
  338. ConvertUnit;
  339. CheckSource('TestOptShortRefGlobals_Unit_FromIntfImpl_ToIntfImpl',
  340. LinesToStr([
  341. 'var $impl = $mod.$impl;',
  342. 'var $lt = null;',
  343. 'var $lt1 = null;',
  344. 'var $lm = pas.UnitA;',
  345. 'var $lt2 = $lm.TBird;',
  346. 'var $lm1 = null;',
  347. 'var $lt3 = null;',
  348. 'var $lt4 = null;',
  349. 'var $lt5 = null;',
  350. 'rtl.createClass(this, "TEagle", $lt2, function () {',
  351. ' $lt = this;',
  352. ' this.Fly = function () {',
  353. ' $lt1.$create("Create");',
  354. ' $lt3.$create("Create");',
  355. ' $lt2.$create("Create");',
  356. ' $lt.$create("Create");',
  357. ' };',
  358. '});',
  359. '']),
  360. LinesToStr([
  361. '$impl.RedAnt = $lt1.$create("Create");',
  362. '$impl.Ant = $lt3.$create("Create");',
  363. '$impl.Bird = $lt2.$create("Create");',
  364. '$impl.Eagle = $lt.$create("Create");',
  365. '$lt5.$create("Create");',
  366. '$impl.Eagle.Fly();',
  367. '$impl.RedAnt.Run();',
  368. '']),
  369. LinesToStr([
  370. '$lm1 = pas.UnitB;',
  371. '$lt3 = $lm1.TAnt;',
  372. '$lt4 = $lm1.TBear;',
  373. '$lt5 = $lm1.TFrog;',
  374. 'rtl.createClass($impl, "TRedAnt", $lt3, function () {',
  375. ' $lt1 = this;',
  376. ' this.Run = function () {',
  377. ' $lt1.$create("Create");',
  378. ' $lt3.$create("Create");',
  379. ' $lt2.$create("Create");',
  380. ' $lt.$create("Create");',
  381. ' $lt4.$create("Create");',
  382. ' };',
  383. '});',
  384. '$impl.RedAnt = null;',
  385. '$impl.Ant = null;',
  386. '$impl.Bird = null;',
  387. '$impl.Eagle = null;',
  388. '']));
  389. end;
  390. procedure TTestOptimizations.TestOptShortRefGlobals_Enums;
  391. begin
  392. AddModuleWithIntfImplSrc('UnitA.pas',
  393. LinesToStr([
  394. 'type',
  395. ' TColor = (red,green,blue);',
  396. '',
  397. '']),
  398. LinesToStr([
  399. '']));
  400. AddModuleWithIntfImplSrc('UnitB.pas',
  401. LinesToStr([
  402. 'type',
  403. ' TSize = (small,big);',
  404. '',
  405. '']),
  406. LinesToStr([
  407. '']));
  408. StartUnit(true,[supWriteln]);
  409. Add([
  410. '{$optimization JSShortRefGlobals}',
  411. 'interface',
  412. 'uses unita;',
  413. 'const',
  414. ' ColorRed = TColor.Red;',
  415. 'procedure Fly;',
  416. 'implementation',
  417. 'uses unitb;',
  418. 'const',
  419. ' SizeSmall = TSize.Small;',
  420. 'procedure Fly;',
  421. 'begin',
  422. ' writeln(ColorRed);',
  423. ' writeln(TColor.Blue);',
  424. ' writeln(SizeSmall);',
  425. ' writeln(TSize.Big);',
  426. ' writeln(unitb.TSize.Big);',
  427. 'end;',
  428. '']);
  429. ConvertUnit;
  430. CheckSource('TestOptShortRefGlobals_Enums',
  431. LinesToStr([
  432. 'var $impl = $mod.$impl;',
  433. 'var $lm = pas.UnitA;',
  434. 'var $lt = $lm.TColor;',
  435. 'var $lt1 = $lt.red;',
  436. 'var $lt2 = $lt.blue;',
  437. 'var $lm1 = null;',
  438. 'var $lt3 = null;',
  439. 'var $lt4 = null;',
  440. 'var $lt5 = null;',
  441. 'this.ColorRed = $lt1;',
  442. 'this.Fly = function () {',
  443. ' console.log($lt1);',
  444. ' console.log($lt2);',
  445. ' console.log($lt4);',
  446. ' console.log($lt5);',
  447. ' console.log($lt5);',
  448. '};',
  449. '']),
  450. LinesToStr([
  451. '']),
  452. LinesToStr([
  453. '$lm1 = pas.UnitB;',
  454. '$lt3 = $lm1.TSize;',
  455. '$lt4 = $lt3.small;',
  456. '$lt5 = $lt3.big;',
  457. '$impl.SizeSmall = $lt4;',
  458. '']));
  459. end;
  460. procedure TTestOptimizations.TestOptShortRefGlobals_Property;
  461. begin
  462. AddModuleWithIntfImplSrc('UnitA.pas',
  463. LinesToStr([
  464. 'type',
  465. ' TBird = class',
  466. ' FWing: TObject;',
  467. ' class var FLeg: TObject;',
  468. ' public',
  469. ' property Wing: TObject read FWing write FWing;',
  470. ' class property Leg: TObject read FLeg write FLeg;',
  471. ' end;',
  472. '']),
  473. LinesToStr([
  474. '']));
  475. StartUnit(true,[supTObject]);
  476. Add([
  477. '{$optimization JSShortRefGlobals}',
  478. 'interface',
  479. 'uses unita;',
  480. 'type',
  481. ' TEagle = class(TBird)', // intf-JS to intf-uses
  482. ' procedure Fly(o: TObject);',
  483. ' end;',
  484. 'implementation',
  485. 'procedure TEagle.Fly(o: TObject);',
  486. 'begin',
  487. ' Fly(Wing);',
  488. ' Fly(Leg);',
  489. 'end;',
  490. '']);
  491. ConvertUnit;
  492. CheckSource('TestOptShortRefGlobals_Property',
  493. LinesToStr([
  494. 'var $lt = null;',
  495. 'var $lm = pas.UnitA;',
  496. 'var $lt1 = $lm.TBird;',
  497. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  498. ' $lt = this;',
  499. ' this.Fly = function (o) {',
  500. ' this.Fly(this.FWing);',
  501. ' this.Fly(this.FLeg);',
  502. ' };',
  503. '});',
  504. '']),
  505. LinesToStr([
  506. '']),
  507. LinesToStr([
  508. '']));
  509. end;
  510. procedure TTestOptimizations.TestOptShortRefGlobals_ExternalAbstract;
  511. begin
  512. AddModuleWithIntfImplSrc('UnitA.pas',
  513. LinesToStr([
  514. 'type',
  515. ' TBird = class',
  516. ' generic function FlyExt<T>(a: word = 103): T; external name ''Flying'';',
  517. ' class procedure JumpVirtual(a: word = 104); virtual; abstract;',
  518. ' class procedure RunStaticExt(a: word = 105); static; external name ''Running'';',
  519. ' end;',
  520. 'procedure SayExt(a: word = 106); external name ''Saying'';',
  521. '']),
  522. LinesToStr([
  523. '']));
  524. StartUnit(true,[supTObject]);
  525. Add([
  526. '{$optimization JSShortRefGlobals}',
  527. 'interface',
  528. 'uses unita;',
  529. 'type',
  530. ' TEagle = class(TBird)',
  531. ' procedure Test;',
  532. ' end;',
  533. 'implementation',
  534. 'procedure TEagle.Test;',
  535. 'begin',
  536. ' specialize FlyExt<Word>;',
  537. ' specialize FlyExt<Word>(1);',
  538. ' specialize JumpVirtual;',
  539. ' specialize JumpVirtual(2);',
  540. ' specialize RunStaticExt;',
  541. ' specialize RunStaticExt(3);',
  542. ' specialize SayExt;',
  543. ' specialize SayExt(4);',
  544. ' Self.specialize FlyExt<Word>;',
  545. ' Self.specialize FlyExt<Word>(11);',
  546. ' Self.specialize JumpVirtual;',
  547. ' Self.specialize JumpVirtual(12);',
  548. ' Self.specialize RunStaticExt;',
  549. ' Self.specialize RunStaticExt(13);',
  550. ' with Self do begin',
  551. ' specialize FlyExt<Word>;',
  552. ' specialize FlyExt<Word>(21);',
  553. ' specialize JumpVirtual;',
  554. ' specialize JumpVirtual(22);',
  555. ' specialize RunStaticExt;',
  556. ' specialize RunStaticExt(23);',
  557. ' end;',
  558. 'end;',
  559. '']);
  560. ConvertUnit;
  561. CheckSource('TestOptShortRefGlobals_ExternalAbstract',
  562. LinesToStr([
  563. 'var $lt = null;',
  564. 'var $lm = pas.UnitA;',
  565. 'var $lt1 = $lm.TBird;',
  566. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  567. ' $lt = this;',
  568. ' this.Test = function () {',
  569. ' this.Flying(103);',
  570. ' this.Flying(1);',
  571. ' this.$class.JumpVirtual(104);',
  572. ' this.$class.JumpVirtual(2);',
  573. ' this.Running(105);',
  574. ' this.Running(3);',
  575. ' Saying(106);',
  576. ' Saying(4);',
  577. ' this.Flying(103);',
  578. ' this.Flying(11);',
  579. ' this.$class.JumpVirtual(104);',
  580. ' this.$class.JumpVirtual(12);',
  581. ' this.Running(105);',
  582. ' this.Running(13);',
  583. ' this.Flying(103);',
  584. ' this.Flying(21);',
  585. ' this.$class.JumpVirtual(104);',
  586. ' this.$class.JumpVirtual(22);',
  587. ' this.Running(105);',
  588. ' this.Running(23);',
  589. ' };',
  590. '});',
  591. '']),
  592. LinesToStr([
  593. '']),
  594. LinesToStr([
  595. '']));
  596. end;
  597. procedure TTestOptimizations.TestOptShortRefGlobals_Class;
  598. begin
  599. AddModuleWithIntfImplSrc('UnitA.pas',
  600. LinesToStr([
  601. 'type',
  602. ' TBird = class',
  603. ' end;',
  604. '']),
  605. LinesToStr([
  606. '']));
  607. StartUnit(true,[supTObject]);
  608. Add([
  609. '{$optimization JSShortRefGlobals}',
  610. 'interface',
  611. 'uses unita;',
  612. 'type',
  613. ' TEagle = class(TBird)',
  614. ' Size: TBird;',
  615. ' class var Color: TBird;',
  616. ' procedure Fly;',
  617. ' class procedure Run;',
  618. ' end;',
  619. 'implementation',
  620. 'procedure TEagle.Fly;',
  621. 'begin',
  622. ' Size:=Size;',
  623. ' Self.Size:=Self.Size;',
  624. ' Color:=Color;',
  625. ' Self.Color:=Self.Color;',
  626. 'end;',
  627. 'class procedure TEagle.Run;',
  628. 'begin',
  629. ' Color:=Color;',
  630. ' Self.Color:=Self.Color;',
  631. 'end;',
  632. '']);
  633. ConvertUnit;
  634. CheckSource('TestOptShortRefGlobals_Class',
  635. LinesToStr([
  636. 'var $lt = null;',
  637. 'var $lm = pas.UnitA;',
  638. 'var $lt1 = $lm.TBird;',
  639. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  640. ' $lt = this;',
  641. ' this.Color = null;',
  642. ' this.$init = function () {',
  643. ' $lt1.$init.call(this);',
  644. ' this.Size = null;',
  645. ' };',
  646. ' this.$final = function () {',
  647. ' this.Size = undefined;',
  648. ' $lt1.$final.call(this);',
  649. ' };',
  650. ' this.Fly = function () {',
  651. ' this.Size = this.Size;',
  652. ' this.Size = this.Size;',
  653. ' $lt.Color = this.Color;',
  654. ' $lt.Color = this.Color;',
  655. ' };',
  656. ' this.Run = function () {',
  657. ' $lt.Color = this.Color;',
  658. ' $lt.Color = this.Color;',
  659. ' };',
  660. '});',
  661. '']),
  662. LinesToStr([
  663. '']),
  664. LinesToStr([
  665. '']));
  666. end;
  667. procedure TTestOptimizations.TestOptShortRefGlobals_GenericFunction;
  668. begin
  669. AddModuleWithIntfImplSrc('UnitA.pas',
  670. LinesToStr([
  671. 'generic function Run<T>(a: T): T;',
  672. '']),
  673. LinesToStr([
  674. 'generic function Run<T>(a: T): T;',
  675. 'begin',
  676. 'end;',
  677. '']));
  678. StartUnit(true,[supTObject]);
  679. Add([
  680. '{$optimization JSShortRefGlobals}',
  681. 'interface',
  682. 'uses unita;',
  683. 'type',
  684. ' TEagle = class',
  685. ' end;',
  686. 'procedure Fly;',
  687. 'implementation',
  688. 'procedure Fly;',
  689. 'begin',
  690. ' specialize Run<TEagle>(nil);',
  691. 'end;',
  692. '']);
  693. ConvertUnit;
  694. CheckSource('TestOptShortRefGlobals_GenericFunction',
  695. LinesToStr([
  696. 'var $lt = null;',
  697. 'var $lm = pas.system;',
  698. 'var $lt1 = $lm.TObject;',
  699. 'var $lm1 = pas.UnitA;',
  700. 'var $lp = $lm1.Run$G1;',
  701. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  702. ' $lt = this;',
  703. '});',
  704. 'this.Fly = function () {',
  705. ' $lp(null);',
  706. '};',
  707. '']),
  708. LinesToStr([
  709. '']),
  710. LinesToStr([
  711. '']));
  712. end;
  713. procedure TTestOptimizations.TestOptShortRefGlobals_GenericMethod_Call;
  714. begin
  715. AddModuleWithIntfImplSrc('UnitA.pas',
  716. LinesToStr([
  717. 'type',
  718. ' TBird = class',
  719. ' generic function Fly<T>(a: word = 13): T;',
  720. ' generic class function Jump<T>(b: word = 14): T;',
  721. ' end;',
  722. '']),
  723. LinesToStr([
  724. 'generic function TBird.Fly<T>(a: word): T;',
  725. 'begin',
  726. 'end;',
  727. 'generic class function TBird.Jump<T>(b: word): T;',
  728. 'begin',
  729. 'end;',
  730. '']));
  731. StartUnit(true,[supTObject]);
  732. Add([
  733. '{$optimization JSShortRefGlobals}',
  734. 'interface',
  735. 'uses unita;',
  736. 'type',
  737. ' TEagle = class(TBird)',
  738. ' procedure Test;',
  739. ' generic function Run<T>(c: word = 25): T;',
  740. ' generic class function Sing<T>(d: word = 26): T;',
  741. ' end;',
  742. 'implementation',
  743. 'procedure TEagle.Test;',
  744. 'begin',
  745. ' specialize Run<Word>;',
  746. ' specialize Run<Word>(1);',
  747. ' specialize Sing<Word>;',
  748. ' specialize Sing<Word>(2);',
  749. ' specialize Fly<Word>;',
  750. ' specialize Fly<Word>(3);',
  751. ' specialize Jump<Word>;',
  752. ' specialize Jump<Word>(4);',
  753. ' Self.specialize Fly<Word>;',
  754. ' Self.specialize Fly<Word>(5);',
  755. ' Self.specialize Jump<Word>;',
  756. ' Self.specialize Jump<Word>(6);',
  757. ' with Self do begin',
  758. ' specialize Fly<Word>;',
  759. ' specialize Fly<Word>(7);',
  760. ' specialize Jump<Word>;',
  761. ' specialize Jump<Word>(8);',
  762. ' end;',
  763. 'end;',
  764. 'generic function TEagle.Run<T>(c: word): T;',
  765. 'begin',
  766. ' specialize Fly<T>;',
  767. ' specialize Fly<T>(7);',
  768. 'end;',
  769. 'generic class function TEagle.Sing<T>(d: word): T;',
  770. 'begin',
  771. ' specialize Jump<T>;',
  772. ' specialize Jump<T>(8);',
  773. 'end;',
  774. '']);
  775. ConvertUnit;
  776. CheckSource('TestOptShortRefGlobals_GenericMethod_Call',
  777. LinesToStr([
  778. 'var $lt = null;',
  779. 'var $lp = null;',
  780. 'var $lp1 = null;',
  781. 'var $lm = pas.UnitA;',
  782. 'var $lt1 = $lm.TBird;',
  783. 'var $lp2 = $lt1.Fly$G1;',
  784. 'var $lp3 = $lt1.Jump$G1;',
  785. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  786. ' $lt = this;',
  787. ' this.Test = function () {',
  788. ' $lp.apply(this, 25);',
  789. ' $lp.apply(this, 1);',
  790. ' $lp1.apply(this.$class, 26);',
  791. ' $lp1.apply(this.$class, 2);',
  792. ' $lp2.apply(this, 13);',
  793. ' $lp2.apply(this, 3);',
  794. ' $lp3.apply(this.$class, 14);',
  795. ' $lp3.apply(this.$class, 4);',
  796. ' $lp2.apply(this, 13);',
  797. ' $lp2.apply(this, 5);',
  798. ' $lp3.apply(this.$class, 14);',
  799. ' $lp3.apply(this, 6);',
  800. ' $lp2.apply(this, 13);',
  801. ' $lp2.apply(this, 7);',
  802. ' $lp3.apply(this.$class, 14);',
  803. ' $lp3.apply(this.$class, 8);',
  804. ' };',
  805. ' this.Run$G1 = $lp = function (c) {',
  806. ' var Result = 0;',
  807. ' $lp2.apply(this, 13);',
  808. ' $lp2.apply(this, 7);',
  809. ' return Result;',
  810. ' };',
  811. ' this.Sing$G1 = $lp1 = function (d) {',
  812. ' var Result = 0;',
  813. ' $lp3.apply(this, 14);',
  814. ' $lp3.apply(this, 8);',
  815. ' return Result;',
  816. ' };',
  817. '});',
  818. '']),
  819. LinesToStr([
  820. '']),
  821. LinesToStr([
  822. '']));
  823. end;
  824. procedure TTestOptimizations.TestOptShortRefGlobals_GenericStaticMethod_Call;
  825. begin
  826. AddModuleWithIntfImplSrc('UnitA.pas',
  827. LinesToStr([
  828. 'type',
  829. ' TBird = class',
  830. ' generic class function Fly<T>(a: word = 13): T; static;',
  831. ' class function Say(a: word = 13): word; static;',
  832. ' end;',
  833. '']),
  834. LinesToStr([
  835. 'generic class function TBird.Fly<T>(a: word): T;',
  836. 'begin',
  837. 'end;',
  838. 'class function TBird.Say(a: word): word;',
  839. 'begin',
  840. 'end;',
  841. '']));
  842. StartUnit(true,[supTObject]);
  843. Add([
  844. '{$optimization JSShortRefGlobals}',
  845. 'interface',
  846. 'uses unita;',
  847. 'type',
  848. ' TEagle = class(TBird)',
  849. ' procedure Test;',
  850. ' generic class function Run<T>(c: word = 25): T; static;',
  851. ' class function Lay(c: word = 25): word; static;',
  852. ' end;',
  853. 'implementation',
  854. 'procedure TEagle.Test;',
  855. 'begin',
  856. ' specialize Fly<Word>;',
  857. ' specialize Fly<Word>(31);',
  858. ' Say;',
  859. ' Say(32);',
  860. ' specialize Run<Word>;',
  861. ' specialize Run<Word>(33);',
  862. ' Lay;',
  863. ' Lay(34);',
  864. ' self.specialize Fly<Word>;',
  865. ' self.specialize Fly<Word>(41);',
  866. ' self.Say;',
  867. ' self.Say(42);',
  868. ' self.specialize Run<Word>;',
  869. ' self.specialize Run<Word>(43);',
  870. ' with Self do begin',
  871. ' specialize Fly<Word>;',
  872. ' specialize Fly<Word>(51);',
  873. ' Say;',
  874. ' Say(52);',
  875. ' specialize Run<Word>;',
  876. ' specialize Run<Word>(53);',
  877. ' end;',
  878. 'end;',
  879. 'generic class function TEagle.Run<T>(c: word): T;',
  880. 'begin',
  881. 'end;',
  882. 'class function TEagle.Lay(c: word): word;',
  883. 'begin',
  884. ' TEagle.specialize Fly<Word>;',
  885. ' TEagle.specialize Fly<Word>(61);',
  886. ' TEagle.Say;',
  887. ' TEagle.Say(62);',
  888. ' TEagle.specialize Run<Word>;',
  889. ' specialize Run<Word>(63);',
  890. ' Lay;',
  891. ' Lay(64);',
  892. 'end;',
  893. '']);
  894. ConvertUnit;
  895. CheckSource('TestOptShortRefGlobals_GenericStaticMethod_Call',
  896. LinesToStr([
  897. 'var $lt = null;',
  898. 'var $lp = null;',
  899. 'var $lm = pas.UnitA;',
  900. 'var $lt1 = $lm.TBird;',
  901. 'var $lp1 = $lt1.Fly$G1;',
  902. 'var $lp2 = $lt1.Say;',
  903. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  904. ' $lt = this;',
  905. ' this.Test = function () {',
  906. ' $lp1(13);',
  907. ' $lp1(31);',
  908. ' $lp2(13);',
  909. ' $lp2(32);',
  910. ' $lp(25);',
  911. ' $lp(33);',
  912. ' $lt.Lay(25);',
  913. ' $lt.Lay(34);',
  914. ' $lp1(13);',
  915. ' $lp1(41);',
  916. ' $lp2(13);',
  917. ' $lp2(42);',
  918. ' $lp(25);',
  919. ' $lp(43);',
  920. ' $lp1(13);',
  921. ' $lp1(51);',
  922. ' $lp2(13);',
  923. ' $lp2(52);',
  924. ' $lp(25);',
  925. ' $lp(53);',
  926. ' };',
  927. ' this.Lay = function (c) {',
  928. ' var Result = 0;',
  929. ' $lp1(13);',
  930. ' $lp1(61);',
  931. ' $lp2(13);',
  932. ' $lp2(62);',
  933. ' $lp(25);',
  934. ' $lp(63);',
  935. ' $lt.Lay(25);',
  936. ' $lt.Lay(64);',
  937. ' return Result;',
  938. ' };',
  939. ' this.Run$G1 = $lp = function (c) {',
  940. ' var Result = 0;',
  941. ' return Result;',
  942. ' };',
  943. '});',
  944. '']),
  945. LinesToStr([
  946. '']),
  947. LinesToStr([
  948. '']));
  949. end;
  950. procedure TTestOptimizations.TestOptShortRefGlobals_GenericClassHelperMethod;
  951. begin
  952. AddModuleWithIntfImplSrc('UnitA.pas',
  953. LinesToStr([
  954. 'type',
  955. ' TBird = class',
  956. ' end;',
  957. ' TBirdHelper = class helper for TBird',
  958. ' generic function Fly<T>(a: word = 13): T;',
  959. ' generic class function Say<T>(a: word = 13): T;',
  960. ' end;',
  961. '']),
  962. LinesToStr([
  963. 'generic function TBirdHelper.Fly<T>(a: word): T;',
  964. 'begin',
  965. 'end;',
  966. 'generic class function TBirdHelper.Say<T>(a: word): T;',
  967. 'begin',
  968. 'end;',
  969. '']));
  970. StartUnit(true,[supTObject]);
  971. Add([
  972. '{$optimization JSShortRefGlobals}',
  973. 'interface',
  974. 'uses unita;',
  975. 'type',
  976. ' TEagle = class(TBird)',
  977. ' procedure Test;',
  978. ' class procedure Lay;',
  979. ' end;',
  980. 'implementation',
  981. 'procedure TEagle.Test;',
  982. 'begin',
  983. ' specialize Fly<Word>;',
  984. ' specialize Fly<Word>(31);',
  985. ' specialize Say<word>;',
  986. ' specialize Say<Word>(32);',
  987. ' self.specialize Fly<Word>;',
  988. ' self.specialize Fly<Word>(41);',
  989. ' self.specialize Say<Word>;',
  990. ' self.specialize Say<Word>(42);',
  991. ' with Self do begin',
  992. ' specialize Fly<Word>;',
  993. ' specialize Fly<Word>(51);',
  994. ' specialize Say<Word>;',
  995. ' specialize Say<Word>(52);',
  996. ' end;',
  997. 'end;',
  998. 'class procedure TEagle.Lay;',
  999. 'begin',
  1000. ' specialize Say<Word>;',
  1001. ' specialize Say<Word>(32);',
  1002. ' self.specialize Say<Word>;',
  1003. ' self.specialize Say<Word>(42);',
  1004. ' with Self do begin',
  1005. ' specialize Say<Word>;',
  1006. ' specialize Say<Word>(52);',
  1007. ' end;',
  1008. 'end;',
  1009. '']);
  1010. ConvertUnit;
  1011. CheckSource('TestOptShortRefGlobals_GenericClassHelperMethod',
  1012. LinesToStr([
  1013. 'var $lt = null;',
  1014. 'var $lm = pas.UnitA;',
  1015. 'var $lt1 = $lm.TBird;',
  1016. 'var $lt2 = $lm.TBirdHelper;',
  1017. 'var $lp = $lt2.Fly$G1;',
  1018. 'var $lp1 = $lt2.Say$G1;',
  1019. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  1020. ' $lt = this;',
  1021. ' this.Test = function () {',
  1022. ' $lp.call(this, 13);',
  1023. ' $lp.call(this, 31);',
  1024. ' $lp1.call(this.$class, 13);',
  1025. ' $lp1.call(this.$class, 32);',
  1026. ' $lp.call(this, 13);',
  1027. ' $lp.call(this, 41);',
  1028. ' $lp1.call(this.$class, 13);',
  1029. ' $lp1.call(this.$class, 42);',
  1030. ' $lp.call(this, 13);',
  1031. ' $lp.call(this, 51);',
  1032. ' $lp1.call(this.$class, 13);',
  1033. ' $lp1.call(this.$class, 52);',
  1034. ' };',
  1035. ' this.Lay = function () {',
  1036. ' $lp1.call(this, 13);',
  1037. ' $lp1.call(this, 32);',
  1038. ' $lp1.call(this, 13);',
  1039. ' $lp1.call(this, 42);',
  1040. ' $lp1.call(this, 13);',
  1041. ' $lp1.call(this, 52);',
  1042. ' };',
  1043. '});',
  1044. '']),
  1045. LinesToStr([
  1046. '']),
  1047. LinesToStr([
  1048. '']));
  1049. end;
  1050. procedure TTestOptimizations.TestOptShortRefGlobals_GenericMethod_ProcVar;
  1051. begin
  1052. AddModuleWithIntfImplSrc('UnitA.pas',
  1053. LinesToStr([
  1054. '{$mode delphi}',
  1055. 'type',
  1056. ' TBird = class',
  1057. ' function Fly<T>(a: word = 13): T;',
  1058. ' class function Jump<T>(b: word = 14): T;',
  1059. ' end;',
  1060. '']),
  1061. LinesToStr([
  1062. 'function TBird.Fly<T>(a: word): T;',
  1063. 'begin',
  1064. 'end;',
  1065. 'class function TBird.Jump<T>(b: word): T;',
  1066. 'begin',
  1067. 'end;',
  1068. '']));
  1069. StartUnit(true,[supTObject]);
  1070. Add([
  1071. '{$mode delphi}',
  1072. '{$optimization JSShortRefGlobals}',
  1073. 'interface',
  1074. 'uses unita;',
  1075. 'type',
  1076. ' TFunc<T> = function(a: word): T of object;',
  1077. ' TEagle = class(TBird)',
  1078. ' procedure Test;',
  1079. ' function Run<T>(c: word = 25): T;',
  1080. ' class function Sing<T>(d: word = 26): T;',
  1081. ' end;',
  1082. 'implementation',
  1083. 'procedure TEagle.Test;',
  1084. 'var f: TFunc<word>;',
  1085. 'begin',
  1086. ' f:=@Run<Word>;',
  1087. ' f:=@Sing<Word>;',
  1088. ' f:=@Fly<Word>;',
  1089. ' f:=@Jump<Word>;',
  1090. ' f:[email protected]<Word>;',
  1091. ' f:[email protected]<Word>;',
  1092. ' with Self do begin',
  1093. ' f:=@Fly<Word>;',
  1094. ' f:=@Jump<Word>;',
  1095. ' end;',
  1096. 'end;',
  1097. 'function TEagle.Run<T>(c: word): T;',
  1098. 'begin',
  1099. 'end;',
  1100. 'class function TEagle.Sing<T>(d: word): T;',
  1101. 'var f: TFunc<T>;',
  1102. 'begin',
  1103. ' f:=@Jump<T>;',
  1104. 'end;',
  1105. '']);
  1106. ConvertUnit;
  1107. CheckSource('TestOptShortRefGlobals_GenericMethod_ProcVar',
  1108. LinesToStr([
  1109. 'var $lt = null;',
  1110. 'var $lp = null;',
  1111. 'var $lp1 = null;',
  1112. 'var $lm = pas.UnitA;',
  1113. 'var $lt1 = $lm.TBird;',
  1114. 'var $lp2 = $lt1.Fly$G1;',
  1115. 'var $lp3 = $lt1.Jump$G1;',
  1116. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  1117. ' $lt = this;',
  1118. ' this.Test = function () {',
  1119. ' var f = null;',
  1120. ' f = rtl.createCallback(this, $lp);',
  1121. ' f = rtl.createCallback(this.$class, $lp1);',
  1122. ' f = rtl.createCallback(this, $lp2);',
  1123. ' f = rtl.createCallback(this.$class, $lp3);',
  1124. ' f = rtl.createCallback(this, $lp2);',
  1125. ' f = rtl.createCallback(this.$class, $lp3);',
  1126. ' f = rtl.createCallback(this, $lp2);',
  1127. ' f = rtl.createCallback(this.$class, $lp3);',
  1128. ' };',
  1129. ' this.Run$G1 = $lp = function (c) {',
  1130. ' var Result = 0;',
  1131. ' return Result;',
  1132. ' };',
  1133. ' this.Sing$G1 = $lp1 = function (d) {',
  1134. ' var Result = 0;',
  1135. ' var f = null;',
  1136. ' f = rtl.createCallback(this, $lp3);',
  1137. ' return Result;',
  1138. ' };',
  1139. '});',
  1140. '']),
  1141. LinesToStr([
  1142. '']),
  1143. LinesToStr([
  1144. '']));
  1145. end;
  1146. procedure TTestOptimizations.TestOptShortRefGlobals_GenericStaticMethod_ProcVar;
  1147. begin
  1148. AddModuleWithIntfImplSrc('UnitA.pas',
  1149. LinesToStr([
  1150. 'type',
  1151. ' TBird = class',
  1152. ' generic class function Fly<T>(a: word = 13): T; static;',
  1153. ' class function Say(a: word = 13): word; static;',
  1154. ' end;',
  1155. '']),
  1156. LinesToStr([
  1157. 'generic class function TBird.Fly<T>(a: word): T;',
  1158. 'begin',
  1159. 'end;',
  1160. 'class function TBird.Say(a: word): word;',
  1161. 'begin',
  1162. 'end;',
  1163. '']));
  1164. StartUnit(true,[supTObject]);
  1165. Add([
  1166. '{$optimization JSShortRefGlobals}',
  1167. 'interface',
  1168. 'uses unita;',
  1169. 'type',
  1170. ' TFunc = function(a: word): word;',
  1171. ' TEagle = class(TBird)',
  1172. ' procedure Test;',
  1173. ' generic class function Run<T>(c: word = 25): T; static;',
  1174. ' class function Lay(c: word = 25): word; static;',
  1175. ' end;',
  1176. 'implementation',
  1177. 'procedure TEagle.Test;',
  1178. 'var f: TFunc;',
  1179. 'begin',
  1180. ' F:=@specialize Fly<Word>;',
  1181. ' F:=@Say;',
  1182. ' F:=@specialize Run<Word>;',
  1183. ' F:=@Lay;',
  1184. ' F:[email protected] Fly<Word>;',
  1185. ' F:[email protected];',
  1186. ' F:[email protected] Run<Word>;',
  1187. ' with Self do begin',
  1188. ' F:=@specialize Fly<Word>;',
  1189. ' F:=@Say;',
  1190. ' F:=@specialize Run<Word>;',
  1191. ' end;',
  1192. 'end;',
  1193. 'generic class function TEagle.Run<T>(c: word): T;',
  1194. 'begin',
  1195. 'end;',
  1196. 'class function TEagle.Lay(c: word): word;',
  1197. 'var f: TFunc;',
  1198. 'begin',
  1199. ' f:[email protected] Fly<Word>;',
  1200. ' f:[email protected];',
  1201. ' f:[email protected] Run<Word>;',
  1202. ' f:=@Lay;',
  1203. 'end;',
  1204. '']);
  1205. ConvertUnit;
  1206. CheckSource('TestOptShortRefGlobals_GenericStaticMethod_ProcVar',
  1207. LinesToStr([
  1208. 'var $lt = null;',
  1209. 'var $lp = null;',
  1210. 'var $lm = pas.UnitA;',
  1211. 'var $lt1 = $lm.TBird;',
  1212. 'var $lp1 = $lt1.Fly$G1;',
  1213. 'var $lp2 = $lt1.Say;',
  1214. 'rtl.createClass(this, "TEagle", $lt1, function () {',
  1215. ' $lt = this;',
  1216. ' this.Test = function () {',
  1217. ' var f = null;',
  1218. ' f = $lp1;',
  1219. ' f = $lp2;',
  1220. ' f = $lp;',
  1221. ' f = $lt.Lay;',
  1222. ' f = $lp1;',
  1223. ' f = $lp2;',
  1224. ' f = $lp;',
  1225. ' f = $lp1;',
  1226. ' f = $lp2;',
  1227. ' f = $lp;',
  1228. ' };',
  1229. ' this.Lay = function (c) {',
  1230. ' var Result = 0;',
  1231. ' var f = null;',
  1232. ' f = $lp1;',
  1233. ' f = $lp2;',
  1234. ' f = $lp;',
  1235. ' f = $lt.Lay;',
  1236. ' return Result;',
  1237. ' };',
  1238. ' this.Run$G1 = $lp = function (c) {',
  1239. ' var Result = 0;',
  1240. ' return Result;',
  1241. ' };',
  1242. '});',
  1243. '']),
  1244. LinesToStr([
  1245. '']),
  1246. LinesToStr([
  1247. '']));
  1248. end;
  1249. procedure TTestOptimizations.TestOptShortRefGlobals_SameUnit_EnumType;
  1250. begin
  1251. StartUnit(true,[supTObject]);
  1252. Add([
  1253. '{$optimization JSShortRefGlobals}',
  1254. 'interface',
  1255. 'type',
  1256. ' TBird = class',
  1257. ' type',
  1258. ' TFlag = (big,small);',
  1259. ' procedure Fly;',
  1260. ' end;',
  1261. ' TEnum = (red,blue);',
  1262. 'var',
  1263. ' e: TEnum;',
  1264. ' f: TBird.TFlag;',
  1265. 'procedure Run;',
  1266. 'implementation',
  1267. 'procedure TBird.Fly;',
  1268. 'begin',
  1269. ' e:=blue;',
  1270. ' f:=small;',
  1271. 'end;',
  1272. 'procedure Run;',
  1273. 'type TSub = (left,right);',
  1274. 'var s: TSub;',
  1275. 'begin',
  1276. ' e:=red;',
  1277. ' s:=right;',
  1278. ' f:=big;',
  1279. 'end;',
  1280. '']);
  1281. ConvertUnit;
  1282. CheckSource('TestOptShortRefGlobals_SameUnit_EnumType',
  1283. LinesToStr([
  1284. 'var $lt = null;',
  1285. 'var $lt1 = null;',
  1286. 'var $lt2 = null;',
  1287. 'var $lm = pas.system;',
  1288. 'var $lt3 = $lm.TObject;',
  1289. 'rtl.createClass(this, "TBird", $lt3, function () {',
  1290. ' $lt = this;',
  1291. ' $lt1 = this.TFlag = {',
  1292. ' "0": "big",',
  1293. ' big: 0,',
  1294. ' "1": "small",',
  1295. ' small: 1',
  1296. ' };',
  1297. ' this.Fly = function () {',
  1298. ' $mod.e = $lt2.blue;',
  1299. ' $mod.f = $lt1.small;',
  1300. ' };',
  1301. '});',
  1302. '$lt2 = this.TEnum = {',
  1303. ' "0": "red",',
  1304. ' red: 0,',
  1305. ' "1": "blue",',
  1306. ' blue: 1',
  1307. '};',
  1308. 'this.e = 0;',
  1309. 'this.f = 0;',
  1310. 'var TSub = {',
  1311. ' "0": "left",',
  1312. ' left: 0,',
  1313. ' "1": "right",',
  1314. ' right: 1',
  1315. '};',
  1316. 'this.Run = function () {',
  1317. ' var s = 0;',
  1318. ' $mod.e = $lt2.red;',
  1319. ' s = TSub.right;',
  1320. ' $mod.f = $lt1.big;',
  1321. '};',
  1322. '']),
  1323. LinesToStr([
  1324. '']),
  1325. LinesToStr([
  1326. '']));
  1327. end;
  1328. procedure TTestOptimizations.TestOptShortRefGlobals_SameUnit_ClassType;
  1329. begin
  1330. WithTypeInfo:=true;
  1331. StartUnit(true,[supTObject]);
  1332. Add([
  1333. '{$optimization JSShortRefGlobals}',
  1334. 'interface',
  1335. 'type',
  1336. ' TBird = class;',
  1337. ' TAnt = class',
  1338. ' type',
  1339. ' TLeg = class',
  1340. ' end;',
  1341. ' procedure Run;',
  1342. ' published',
  1343. ' Bird: TBird;',
  1344. ' end;',
  1345. ' TBird = class',
  1346. ' procedure Fly;',
  1347. ' end;',
  1348. 'implementation',
  1349. 'type',
  1350. ' TFrog = class',
  1351. ' end;',
  1352. 'procedure TAnt.Run;',
  1353. 'begin',
  1354. ' if typeinfo(TBird)=nil then;',
  1355. ' Bird:=TBird.Create;',
  1356. ' TLeg.Create;',
  1357. ' TFrog.Create;',
  1358. 'end;',
  1359. 'procedure TBird.Fly;',
  1360. 'begin',
  1361. ' if typeinfo(TAnt)=nil then;',
  1362. 'end;',
  1363. '']);
  1364. ConvertUnit;
  1365. CheckSource('TestOptShortRefGlobals_SameUnit_ClassType',
  1366. LinesToStr([
  1367. 'var $impl = $mod.$impl;',
  1368. 'var $lt = null;',
  1369. 'var $lt1 = null;',
  1370. 'var $lt2 = null;',
  1371. 'var $lt3 = null;',
  1372. 'var $lm = pas.system;',
  1373. 'var $lt4 = $lm.TObject;',
  1374. 'this.$rtti.$Class("TBird");',
  1375. 'rtl.createClass(this, "TAnt", $lt4, function () {',
  1376. ' $lt = this;',
  1377. ' rtl.createClass(this, "TLeg", $lt4, function () {',
  1378. ' $lt1 = this;',
  1379. ' }, "TAnt.TLeg");',
  1380. ' this.$init = function () {',
  1381. ' $lt4.$init.call(this);',
  1382. ' this.Bird = null;',
  1383. ' };',
  1384. ' this.$final = function () {',
  1385. ' this.Bird = undefined;',
  1386. ' $lt4.$final.call(this);',
  1387. ' };',
  1388. ' this.Run = function () {',
  1389. ' if ($mod.$rtti["TBird"] === null) ;',
  1390. ' this.Bird = $lt2.$create("Create");',
  1391. ' $lt1.$create("Create");',
  1392. ' $lt3.$create("Create");',
  1393. ' };',
  1394. ' var $r = this.$rtti;',
  1395. ' $r.addField("Bird", $mod.$rtti["TBird"]);',
  1396. '});',
  1397. 'rtl.createClass(this, "TBird", $lt4, function () {',
  1398. ' $lt2 = this;',
  1399. ' this.Fly = function () {',
  1400. ' if ($mod.$rtti["TAnt"] === null) ;',
  1401. ' };',
  1402. '});',
  1403. '']),
  1404. LinesToStr([
  1405. '']),
  1406. LinesToStr([
  1407. 'rtl.createClass($impl, "TFrog", $lt4, function () {',
  1408. ' $lt3 = this;',
  1409. '});',
  1410. '']));
  1411. end;
  1412. procedure TTestOptimizations.TestOptShortRefGlobals_SameUnit_RecordType;
  1413. begin
  1414. StartUnit(true,[supTObject]);
  1415. Add([
  1416. '{$optimization JSShortRefGlobals}',
  1417. '{$modeswitch advancedrecords}',
  1418. 'interface',
  1419. 'type',
  1420. ' TAnt = record',
  1421. ' type',
  1422. ' TLeg = record',
  1423. ' l: word;',
  1424. ' end;',
  1425. ' procedure Run;',
  1426. ' Leg: TLeg;',
  1427. ' end;',
  1428. 'implementation',
  1429. 'type',
  1430. ' TBird = record',
  1431. ' b: word;',
  1432. ' end;',
  1433. 'procedure TAnt.Run;',
  1434. 'type',
  1435. ' TFoot = record',
  1436. ' f: word;',
  1437. ' end;',
  1438. 'var',
  1439. ' b: TBird;',
  1440. ' l: TLeg;',
  1441. ' a: TAnt;',
  1442. ' f: TFoot;',
  1443. 'begin',
  1444. ' b.b:=1;',
  1445. ' l.l:=2;',
  1446. ' a.Leg.l:=3;',
  1447. ' f.f:=4;',
  1448. 'end;',
  1449. '']);
  1450. ConvertUnit;
  1451. CheckSource('TestOptShortRefGlobals_SameUnit_RecordType',
  1452. LinesToStr([
  1453. 'var $impl = $mod.$impl;',
  1454. 'var $lt = null;',
  1455. 'var $lt1 = null;',
  1456. 'var $lt2 = null;',
  1457. 'rtl.recNewT(this, "TAnt", function () {',
  1458. ' $lt = this;',
  1459. ' rtl.recNewT(this, "TLeg", function () {',
  1460. ' $lt1 = this;',
  1461. ' this.l = 0;',
  1462. ' this.$eq = function (b) {',
  1463. ' return this.l === b.l;',
  1464. ' };',
  1465. ' this.$assign = function (s) {',
  1466. ' this.l = s.l;',
  1467. ' return this;',
  1468. ' };',
  1469. ' });',
  1470. ' this.$new = function () {',
  1471. ' var r = Object.create(this);',
  1472. ' r.Leg = $lt1.$new();',
  1473. ' return r;',
  1474. ' };',
  1475. ' this.$eq = function (b) {',
  1476. ' return this.Leg.$eq(b.Leg);',
  1477. ' };',
  1478. ' this.$assign = function (s) {',
  1479. ' this.Leg.$assign(s.Leg);',
  1480. ' return this;',
  1481. ' };',
  1482. ' var TFoot = rtl.recNewT(null, "", function () {',
  1483. ' this.f = 0;',
  1484. ' this.$eq = function (b) {',
  1485. ' return this.f === b.f;',
  1486. ' };',
  1487. ' this.$assign = function (s) {',
  1488. ' this.f = s.f;',
  1489. ' return this;',
  1490. ' };',
  1491. ' });',
  1492. ' this.Run = function () {',
  1493. ' var b = $lt2.$new();',
  1494. ' var l = $lt1.$new();',
  1495. ' var a = $lt.$new();',
  1496. ' var f = TFoot.$new();',
  1497. ' b.b = 1;',
  1498. ' l.l = 2;',
  1499. ' a.Leg.l = 3;',
  1500. ' f.f = 4;',
  1501. ' };',
  1502. '}, true);',
  1503. '']),
  1504. LinesToStr([
  1505. '']),
  1506. LinesToStr([
  1507. 'rtl.recNewT($impl, "TBird", function () {',
  1508. ' $lt2 = this;',
  1509. ' this.b = 0;',
  1510. ' this.$eq = function (b) {',
  1511. ' return this.b === b.b;',
  1512. ' };',
  1513. ' this.$assign = function (s) {',
  1514. ' this.b = s.b;',
  1515. ' return this;',
  1516. ' };',
  1517. '});',
  1518. '']));
  1519. end;
  1520. procedure TTestOptimizations.TestOptShortRefGlobals_Unit_InitNoImpl;
  1521. begin
  1522. AddModuleWithIntfImplSrc('UnitA.pas',
  1523. LinesToStr([
  1524. 'var a: word;',
  1525. 'procedure Run(w: word);',
  1526. '']),
  1527. LinesToStr([
  1528. 'procedure Run(w: word);',
  1529. 'begin',
  1530. 'end;',
  1531. '']));
  1532. StartUnit(true,[supTObject]);
  1533. Add([
  1534. '{$optimization JSShortRefGlobals}',
  1535. 'interface',
  1536. 'implementation',
  1537. 'uses UnitA;', // empty implementation function
  1538. 'begin',
  1539. ' Run(a);',
  1540. '']);
  1541. ConvertUnit;
  1542. CheckSource('TestOptShortRefGlobals_Unit_InitNoImpl',
  1543. LinesToStr([
  1544. 'var $impl = $mod.$impl;',
  1545. 'var $lm = null;',
  1546. 'var $lp = null;',
  1547. '']),
  1548. LinesToStr([
  1549. '$lp($lm.a);',
  1550. '']),
  1551. LinesToStr([
  1552. '$lm = pas.UnitA;',
  1553. '$lp = $lm.Run;',
  1554. '']));
  1555. end;
  1556. procedure TTestOptimizations.TestWPO_OmitLocalVar;
  1557. begin
  1558. StartProgram(false);
  1559. Add('var');
  1560. Add(' a: longint;');
  1561. Add(' b: longint;');
  1562. Add('begin');
  1563. Add(' b:=3;');
  1564. ConvertProgram;
  1565. CheckSource('TestWPO_OmitLocalVar',
  1566. 'this.b = 0;',
  1567. '$mod.b = 3;');
  1568. end;
  1569. procedure TTestOptimizations.TestWPO_OmitLocalProc;
  1570. begin
  1571. StartProgram(false);
  1572. Add('procedure DoIt; begin end;');
  1573. Add('procedure NoIt; begin end;');
  1574. Add('begin');
  1575. Add(' DoIt;');
  1576. ConvertProgram;
  1577. CheckSource('TestWPO_OmitLocalProc',
  1578. LinesToStr([
  1579. 'this.DoIt = function () {',
  1580. '};',
  1581. '']),
  1582. LinesToStr([
  1583. '$mod.DoIt();',
  1584. '']));
  1585. end;
  1586. procedure TTestOptimizations.TestWPO_OmitLocalProcForward;
  1587. begin
  1588. StartProgram(false);
  1589. Add('procedure DoIt; forward;');
  1590. Add('procedure NoIt; forward;');
  1591. Add('procedure DoIt; begin end;');
  1592. Add('procedure NoIt; begin end;');
  1593. Add('begin');
  1594. Add(' DoIt;');
  1595. ConvertProgram;
  1596. CheckSource('TestWPO_OmitLocalProcForward',
  1597. LinesToStr([
  1598. 'this.DoIt = function () {',
  1599. '};',
  1600. '']),
  1601. LinesToStr([
  1602. '$mod.DoIt();',
  1603. '']));
  1604. end;
  1605. procedure TTestOptimizations.TestWPO_OmitProcLocalVar;
  1606. begin
  1607. StartProgram(false);
  1608. Add('function DoIt: longint;');
  1609. Add('var');
  1610. Add(' a: longint;');
  1611. Add(' b: longint;');
  1612. Add('begin');
  1613. Add(' b:=3;');
  1614. Add(' Result:=b;');
  1615. Add('end;');
  1616. Add('begin');
  1617. Add(' DoIt;');
  1618. ConvertProgram;
  1619. CheckSource('TestWPO_OmitProcLocalVar',
  1620. LinesToStr([
  1621. 'this.DoIt = function () {',
  1622. ' var Result = 0;',
  1623. ' var b = 0;',
  1624. ' b = 3;',
  1625. ' Result = b;',
  1626. ' return Result;',
  1627. '};',
  1628. '']),
  1629. LinesToStr([
  1630. '$mod.DoIt();',
  1631. '']));
  1632. end;
  1633. procedure TTestOptimizations.TestWPO_OmitProcLocalConst;
  1634. begin
  1635. StartProgram(false);
  1636. Add('function DoIt: longint;');
  1637. Add('const');
  1638. Add(' a = 3;');
  1639. Add(' b = 4;');
  1640. Add(' c: longint = 5;');
  1641. Add(' d: longint = 6;');
  1642. Add('begin');
  1643. Add(' Result:=b+d;');
  1644. Add('end;');
  1645. Add('begin');
  1646. Add(' DoIt;');
  1647. ConvertProgram;
  1648. CheckSource('TestWPO_OmitProcLocalConst',
  1649. LinesToStr([
  1650. 'var b = 4;',
  1651. 'var d = 6;',
  1652. 'this.DoIt = function () {',
  1653. ' var Result = 0;',
  1654. ' Result = 4 + d;',
  1655. ' return Result;',
  1656. '};',
  1657. '']),
  1658. LinesToStr([
  1659. '$mod.DoIt();',
  1660. '']));
  1661. end;
  1662. procedure TTestOptimizations.TestWPO_OmitProcLocalType;
  1663. begin
  1664. StartProgram(false);
  1665. Add('function DoIt: longint;');
  1666. Add('type');
  1667. Add(' TEnum = (red, green);');
  1668. Add(' TEnums = set of TEnum;');
  1669. Add('begin');
  1670. Add(' Result:=3;');
  1671. Add('end;');
  1672. Add('begin');
  1673. Add(' DoIt;');
  1674. ConvertProgram;
  1675. CheckSource('TestWPO_OmitProcLocalType',
  1676. LinesToStr([
  1677. 'this.DoIt = function () {',
  1678. ' var Result = 0;',
  1679. ' Result = 3;',
  1680. ' return Result;',
  1681. '};',
  1682. '']),
  1683. LinesToStr([
  1684. '$mod.DoIt();',
  1685. '']));
  1686. end;
  1687. procedure TTestOptimizations.TestWPO_OmitProcLocalProc;
  1688. begin
  1689. StartProgram(false);
  1690. Add('procedure DoIt;');
  1691. Add(' procedure SubProcA; begin end;');
  1692. Add(' procedure SubProcB; begin end;');
  1693. Add('begin');
  1694. Add(' SubProcB;');
  1695. Add('end;');
  1696. Add('begin');
  1697. Add(' DoIt;');
  1698. ConvertProgram;
  1699. CheckSource('TestWPO_OmitProcLocalProc',
  1700. LinesToStr([
  1701. 'this.DoIt = function () {',
  1702. ' function SubProcB() {',
  1703. ' };',
  1704. ' SubProcB();',
  1705. '};',
  1706. '']),
  1707. LinesToStr([
  1708. '$mod.DoIt();',
  1709. '']));
  1710. end;
  1711. procedure TTestOptimizations.TestWPO_OmitProcLocalForwardProc;
  1712. begin
  1713. StartProgram(false);
  1714. Add('procedure DoIt;');
  1715. Add(' procedure SubProcA; forward;');
  1716. Add(' procedure SubProcB; forward;');
  1717. Add(' procedure SubProcA; begin end;');
  1718. Add(' procedure SubProcB; begin end;');
  1719. Add('begin');
  1720. Add(' SubProcB;');
  1721. Add('end;');
  1722. Add('begin');
  1723. Add(' DoIt;');
  1724. ConvertProgram;
  1725. CheckSource('TestWPO_OmitProcLocalForwardProc',
  1726. LinesToStr([
  1727. 'this.DoIt = function () {',
  1728. ' function SubProcB() {',
  1729. ' };',
  1730. ' SubProcB();',
  1731. '};',
  1732. '']),
  1733. LinesToStr([
  1734. '$mod.DoIt();',
  1735. '']));
  1736. end;
  1737. procedure TTestOptimizations.TestWPO_OmitRecordMember;
  1738. begin
  1739. StartProgram(false);
  1740. Add('type');
  1741. Add(' TRec = record');
  1742. Add(' a: longint;');
  1743. Add(' b: longint;');
  1744. Add(' end;');
  1745. Add('var r: TRec;');
  1746. Add('begin');
  1747. Add(' r.a:=3;');
  1748. ConvertProgram;
  1749. CheckSource('TestWPO_OmitRecordMember',
  1750. LinesToStr([
  1751. 'rtl.recNewT(this, "TRec", function () {',
  1752. ' this.a = 0;',
  1753. ' this.$eq = function (b) {',
  1754. ' return this.a === b.a;',
  1755. ' };',
  1756. ' this.$assign = function (s) {',
  1757. ' this.a = s.a;',
  1758. ' return this;',
  1759. ' };',
  1760. '});',
  1761. 'this.r = this.TRec.$new();',
  1762. '']),
  1763. LinesToStr([
  1764. '$mod.r.a = 3;',
  1765. '']));
  1766. end;
  1767. procedure TTestOptimizations.TestWPO_OmitNotUsedTObject;
  1768. begin
  1769. StartProgram(false);
  1770. Add('type');
  1771. Add(' TObject = class end;');
  1772. Add('var o: TObject;');
  1773. Add('begin');
  1774. ConvertProgram;
  1775. CheckSource('TestWPO_OmitNotUsedTObject',
  1776. LinesToStr([
  1777. '']),
  1778. LinesToStr([
  1779. '']));
  1780. end;
  1781. procedure TTestOptimizations.TestWPO_TObject;
  1782. begin
  1783. StartProgram(false);
  1784. Add('type');
  1785. Add(' TObject = class');
  1786. Add(' procedure AfterConstruction; virtual;');
  1787. Add(' procedure BeforeDestruction; virtual;');
  1788. Add(' end;');
  1789. Add('procedure TObject.AfterConstruction; begin end;');
  1790. Add('procedure TObject.BeforeDestruction; begin end;');
  1791. Add('var o: TObject;');
  1792. Add('begin');
  1793. Add(' o:=nil;');
  1794. ConvertProgram;
  1795. CheckSource('TestWPO_TObject',
  1796. LinesToStr([
  1797. 'rtl.createClass(this, "TObject", null, function () {',
  1798. ' this.$init = function () {',
  1799. ' };',
  1800. ' this.$final = function () {',
  1801. ' };',
  1802. ' this.AfterConstruction = function () {',
  1803. ' };',
  1804. ' this.BeforeDestruction = function () {',
  1805. ' };',
  1806. '});',
  1807. 'this.o = null;',
  1808. '']),
  1809. LinesToStr([
  1810. '$mod.o = null;']));
  1811. end;
  1812. procedure TTestOptimizations.TestWPO_Class_Property;
  1813. begin
  1814. StartProgram(false);
  1815. Add([
  1816. 'type',
  1817. ' TObject = class',
  1818. ' private',
  1819. ' const CA = 3;',
  1820. ' private',
  1821. ' FA: longint;',
  1822. ' function GetA: longint;',
  1823. ' procedure SetA(Value: longint);',
  1824. ' function IsStoredA: boolean;',
  1825. ' property A: longint read GetA write SetA stored IsStoredA default CA;',
  1826. ' end;',
  1827. 'function tobject.geta: longint; begin end;',
  1828. 'procedure tobject.seta(value: longint); begin end;',
  1829. 'function tobject.isstoreda: boolean; begin end;',
  1830. 'var o: TObject;',
  1831. 'begin',
  1832. ' o.A:=o.A;']);
  1833. ConvertProgram;
  1834. CheckSource('TestWPO_Class_TObject',
  1835. LinesToStr([
  1836. 'rtl.createClass(this, "TObject", null, function () {',
  1837. ' this.$init = function () {',
  1838. ' };',
  1839. ' this.$final = function () {',
  1840. ' };',
  1841. ' this.GetA = function () {',
  1842. ' var Result = 0;',
  1843. ' return Result;',
  1844. ' };',
  1845. ' this.SetA = function (Value) {',
  1846. ' };',
  1847. '});',
  1848. 'this.o = null;',
  1849. '']),
  1850. LinesToStr([
  1851. '$mod.o.SetA($mod.o.GetA());']));
  1852. end;
  1853. procedure TTestOptimizations.TestWPO_Class_OmitField;
  1854. begin
  1855. StartProgram(false);
  1856. Add('type');
  1857. Add(' TObject = class');
  1858. Add(' a: longint;');
  1859. Add(' b: longint;');
  1860. Add(' end;');
  1861. Add('var o: TObject;');
  1862. Add('begin');
  1863. Add(' o.a:=3;');
  1864. ConvertProgram;
  1865. CheckSource('TestWPO_OmitClassField',
  1866. LinesToStr([
  1867. 'rtl.createClass(this, "TObject", null, function () {',
  1868. ' this.$init = function () {',
  1869. ' this.a = 0;',
  1870. ' };',
  1871. ' this.$final = function () {',
  1872. ' };',
  1873. '});',
  1874. 'this.o = null;',
  1875. '']),
  1876. LinesToStr([
  1877. '$mod.o.a = 3;']));
  1878. end;
  1879. procedure TTestOptimizations.TestWPO_Class_OmitMethod;
  1880. begin
  1881. StartProgram(false);
  1882. Add('type');
  1883. Add(' TObject = class');
  1884. Add(' procedure ProcA;');
  1885. Add(' procedure ProcB;');
  1886. Add(' end;');
  1887. Add('procedure TObject.ProcA; begin end;');
  1888. Add('procedure TObject.ProcB; begin end;');
  1889. Add('var o: TObject;');
  1890. Add('begin');
  1891. Add(' o.ProcB;');
  1892. ConvertProgram;
  1893. CheckSource('TestWPO_OmitClassMethod',
  1894. LinesToStr([
  1895. 'rtl.createClass(this, "TObject", null, function () {',
  1896. ' this.$init = function () {',
  1897. ' };',
  1898. ' this.$final = function () {',
  1899. ' };',
  1900. ' this.ProcB = function () {',
  1901. ' };',
  1902. '});',
  1903. 'this.o = null;',
  1904. '']),
  1905. LinesToStr([
  1906. '$mod.o.ProcB();']));
  1907. end;
  1908. procedure TTestOptimizations.TestWPO_Class_OmitClassMethod;
  1909. begin
  1910. StartProgram(false);
  1911. Add('type');
  1912. Add(' TObject = class');
  1913. Add(' class procedure ProcA;');
  1914. Add(' class procedure ProcB;');
  1915. Add(' end;');
  1916. Add('class procedure TObject.ProcA; begin end;');
  1917. Add('class procedure TObject.ProcB; begin end;');
  1918. Add('var o: TObject;');
  1919. Add('begin');
  1920. Add(' o.ProcB;');
  1921. ConvertProgram;
  1922. CheckSource('TestWPO_OmitClassMethod',
  1923. LinesToStr([
  1924. 'rtl.createClass(this, "TObject", null, function () {',
  1925. ' this.$init = function () {',
  1926. ' };',
  1927. ' this.$final = function () {',
  1928. ' };',
  1929. ' this.ProcB = function () {',
  1930. ' };',
  1931. '});',
  1932. 'this.o = null;',
  1933. '']),
  1934. LinesToStr([
  1935. '$mod.o.$class.ProcB();']));
  1936. end;
  1937. procedure TTestOptimizations.TestWPO_Class_OmitPropertyGetter1;
  1938. begin
  1939. StartProgram(false);
  1940. Add('type');
  1941. Add(' TObject = class');
  1942. Add(' FFoo: boolean;');
  1943. Add(' function GetFoo: boolean;');
  1944. Add(' property Foo: boolean read FFoo;');
  1945. Add(' property Foo2: boolean read GetFoo;');
  1946. Add(' FBar: boolean;');
  1947. Add(' function GetBar: boolean;');
  1948. Add(' property Bar: boolean read FBar;');
  1949. Add(' property Bar2: boolean read GetBar;');
  1950. Add(' end;');
  1951. Add('function TObject.GetFoo: boolean; begin Result:=FFoo; end;');
  1952. Add('function TObject.GetBar: boolean; begin Result:=FBar; end;');
  1953. Add('var o: TObject;');
  1954. Add('begin');
  1955. Add(' if o.Foo then;');
  1956. ConvertProgram;
  1957. CheckSource('TestWPO_OmitClassPropertyGetter1',
  1958. LinesToStr([
  1959. 'rtl.createClass(this, "TObject", null, function () {',
  1960. ' this.$init = function () {',
  1961. ' this.FFoo = false;',
  1962. ' };',
  1963. ' this.$final = function () {',
  1964. ' };',
  1965. '});',
  1966. 'this.o = null;',
  1967. '']),
  1968. LinesToStr([
  1969. 'if ($mod.o.FFoo);',
  1970. '']));
  1971. end;
  1972. procedure TTestOptimizations.TestWPO_Class_OmitPropertyGetter2;
  1973. begin
  1974. StartProgram(false);
  1975. Add('type');
  1976. Add(' TObject = class');
  1977. Add(' FFoo: boolean;');
  1978. Add(' function GetFoo: boolean;');
  1979. Add(' property Foo: boolean read FFoo;');
  1980. Add(' property Foo2: boolean read GetFoo;');
  1981. Add(' end;');
  1982. Add('function TObject.GetFoo: boolean; begin Result:=FFoo; end;');
  1983. Add('var o: TObject;');
  1984. Add('begin');
  1985. Add(' if o.Foo2 then;');
  1986. ConvertProgram;
  1987. CheckSource('TestWPO_OmitClassPropertyGetter2',
  1988. LinesToStr([
  1989. 'rtl.createClass(this, "TObject", null, function () {',
  1990. ' this.$init = function () {',
  1991. ' this.FFoo = false;',
  1992. ' };',
  1993. ' this.$final = function () {',
  1994. ' };',
  1995. ' this.GetFoo = function () {',
  1996. ' var Result = false;',
  1997. ' Result = this.FFoo;',
  1998. ' return Result;',
  1999. ' };',
  2000. '});',
  2001. 'this.o = null;',
  2002. '']),
  2003. LinesToStr([
  2004. 'if ($mod.o.GetFoo()) ;',
  2005. '']));
  2006. end;
  2007. procedure TTestOptimizations.TestWPO_Class_OmitPropertySetter1;
  2008. begin
  2009. StartProgram(false);
  2010. Add('type');
  2011. Add(' TObject = class');
  2012. Add(' FFoo: boolean;');
  2013. Add(' procedure SetFoo(Value: boolean);');
  2014. Add(' property Foo: boolean write FFoo;');
  2015. Add(' property Foo2: boolean write SetFoo;');
  2016. Add(' FBar: boolean;');
  2017. Add(' procedure SetBar(Value: boolean);');
  2018. Add(' property Bar: boolean write FBar;');
  2019. Add(' property Bar2: boolean write SetBar;');
  2020. Add(' end;');
  2021. Add('procedure TObject.SetFoo(Value: boolean); begin FFoo:=Value; end;');
  2022. Add('procedure TObject.SetBar(Value: boolean); begin FBar:=Value; end;');
  2023. Add('var o: TObject;');
  2024. Add('begin');
  2025. Add(' o.Foo:=true;');
  2026. ConvertProgram;
  2027. CheckSource('TestWPO_OmitClassPropertySetter1',
  2028. LinesToStr([
  2029. 'rtl.createClass(this, "TObject", null, function () {',
  2030. ' this.$init = function () {',
  2031. ' this.FFoo = false;',
  2032. ' };',
  2033. ' this.$final = function () {',
  2034. ' };',
  2035. '});',
  2036. 'this.o = null;',
  2037. '']),
  2038. LinesToStr([
  2039. '$mod.o.FFoo = true;',
  2040. '']));
  2041. end;
  2042. procedure TTestOptimizations.TestWPO_Class_OmitPropertySetter2;
  2043. begin
  2044. StartProgram(false);
  2045. Add('type');
  2046. Add(' TObject = class');
  2047. Add(' FFoo: boolean;');
  2048. Add(' procedure SetFoo(Value: boolean);');
  2049. Add(' property Foo: boolean write FFoo;');
  2050. Add(' property Foo2: boolean write SetFoo;');
  2051. Add(' end;');
  2052. Add('procedure TObject.SetFoo(Value: boolean); begin FFoo:=Value; end;');
  2053. Add('var o: TObject;');
  2054. Add('begin');
  2055. Add(' o.Foo2:=true;');
  2056. ConvertProgram;
  2057. CheckSource('TestWPO_OmitClassPropertySetter2',
  2058. LinesToStr([
  2059. 'rtl.createClass(this, "TObject", null, function () {',
  2060. ' this.$init = function () {',
  2061. ' this.FFoo = false;',
  2062. ' };',
  2063. ' this.$final = function () {',
  2064. ' };',
  2065. ' this.SetFoo = function (Value) {',
  2066. ' this.FFoo = Value;',
  2067. ' };',
  2068. '});',
  2069. 'this.o = null;',
  2070. '']),
  2071. LinesToStr([
  2072. '$mod.o.SetFoo(true);',
  2073. '']));
  2074. end;
  2075. procedure TTestOptimizations.TestWPO_Class_KeepNewInstance;
  2076. begin
  2077. StartProgram(false);
  2078. Add([
  2079. '{$modeswitch externalclass}',
  2080. 'type',
  2081. ' TExt = class external name ''Object''',
  2082. ' end;',
  2083. ' TBird = class(TExt)',
  2084. ' protected',
  2085. ' class function NewInstance(fnname: string; const paramarray): TBird; virtual;',
  2086. ' public',
  2087. ' constructor Create;',
  2088. ' end;',
  2089. 'class function TBird.NewInstance(fnname: string; const paramarray): TBird;',
  2090. 'begin',
  2091. ' asm',
  2092. ' Result = Object.create();',
  2093. ' end;',
  2094. 'end;',
  2095. 'constructor TBird.Create;',
  2096. 'begin',
  2097. ' inherited;',
  2098. 'end;',
  2099. 'begin',
  2100. ' TBird.Create;',
  2101. '']);
  2102. ConvertProgram;
  2103. CheckSource('TestWPO_Class_KeepNewInstance',
  2104. LinesToStr([
  2105. 'rtl.createClassExt(this, "TBird", Object, "NewInstance", function () {',
  2106. ' this.$init = function () {',
  2107. ' };',
  2108. ' this.$final = function () {',
  2109. ' };',
  2110. ' this.NewInstance = function (fnname, paramarray) {',
  2111. ' var Result = null;',
  2112. ' Result = Object.create();',
  2113. ' return Result;',
  2114. ' };',
  2115. ' this.Create = function () {',
  2116. ' return this;',
  2117. ' };',
  2118. '});',
  2119. '']),
  2120. LinesToStr([
  2121. '$mod.TBird.$create("Create");',
  2122. '']));
  2123. end;
  2124. procedure TTestOptimizations.TestWPO_CallInherited;
  2125. begin
  2126. StartProgram(false);
  2127. Add('type');
  2128. Add(' TObject = class');
  2129. Add(' procedure DoA;');
  2130. Add(' procedure DoB;');
  2131. Add(' end;');
  2132. Add(' TMobile = class');
  2133. Add(' procedure DoA;');
  2134. Add(' procedure DoC;');
  2135. Add(' end;');
  2136. Add('procedure TObject.DoA; begin end;');
  2137. Add('procedure TObject.DoB; begin end;');
  2138. Add('procedure TMobile.DoA;');
  2139. Add('begin');
  2140. Add(' inherited;');
  2141. Add('end;');
  2142. Add('procedure TMobile.DoC;');
  2143. Add('begin');
  2144. Add(' inherited DoB;');
  2145. Add('end;');
  2146. Add('var o: TMobile;');
  2147. Add('begin');
  2148. Add(' o.DoA;');
  2149. Add(' o.DoC;');
  2150. ConvertProgram;
  2151. CheckSource('TestWPO_CallInherited',
  2152. LinesToStr([
  2153. 'rtl.createClass(this, "TObject", null, function () {',
  2154. ' this.$init = function () {',
  2155. ' };',
  2156. ' this.$final = function () {',
  2157. ' };',
  2158. ' this.DoA = function () {',
  2159. ' };',
  2160. ' this.DoB = function () {',
  2161. ' };',
  2162. '});',
  2163. ' rtl.createClass(this, "TMobile", this.TObject, function () {',
  2164. ' this.DoA$1 = function () {',
  2165. ' $mod.TObject.DoA.call(this);',
  2166. ' };',
  2167. ' this.DoC = function () {',
  2168. ' $mod.TObject.DoB.call(this);',
  2169. ' };',
  2170. '});',
  2171. 'this.o = null;',
  2172. '']),
  2173. LinesToStr([
  2174. '$mod.o.DoA$1();',
  2175. '$mod.o.DoC();',
  2176. '']));
  2177. end;
  2178. procedure TTestOptimizations.TestWPO_UseUnit;
  2179. var
  2180. ActualSrc, ExpectedSrc: String;
  2181. begin
  2182. AddModuleWithIntfImplSrc('unit1.pp',
  2183. LinesToStr([
  2184. 'var i: longint;',
  2185. 'procedure DoIt;',
  2186. '']),
  2187. LinesToStr([
  2188. 'procedure DoIt; begin end;']));
  2189. AddModuleWithIntfImplSrc('unit2.pp',
  2190. LinesToStr([
  2191. 'var j: longint;',
  2192. 'procedure DoMore;',
  2193. '']),
  2194. LinesToStr([
  2195. 'procedure DoMore; begin end;']));
  2196. StartProgram(true);
  2197. Add('uses unit2;');
  2198. Add('begin');
  2199. Add(' j:=3;');
  2200. ConvertProgram;
  2201. ActualSrc:=ConvertJSModuleToString(JSModule);
  2202. ExpectedSrc:=LinesToStr([
  2203. 'rtl.module("program", ["system", "unit2"], function () {',
  2204. ' var $mod = this;',
  2205. ' $mod.$main = function () {',
  2206. ' pas.unit2.j = 3;',
  2207. ' };',
  2208. '});',
  2209. '']);
  2210. CheckDiff('TestWPO_UseUnit',ExpectedSrc,ActualSrc);
  2211. end;
  2212. procedure TTestOptimizations.TestWPO_ArrayOfConst_Use;
  2213. begin
  2214. StartProgram(true,[supTVarRec]);
  2215. Add([
  2216. 'procedure Say(arr: array of const);',
  2217. 'begin',
  2218. 'end;',
  2219. 'begin',
  2220. ' Say([true]);']);
  2221. ConvertProgram;
  2222. CheckUnit('system.pp',
  2223. LinesToStr([
  2224. 'rtl.module("system", [], function () {',
  2225. ' var $mod = this;',
  2226. ' rtl.recNewT(this, "TVarRec", function () {',
  2227. ' this.VType = 0;',
  2228. ' this.VJSValue = undefined;',
  2229. ' this.$eq = function (b) {',
  2230. ' return (this.VType === b.VType) && (this.VJSValue === b.VJSValue);',
  2231. ' };',
  2232. ' this.$assign = function (s) {',
  2233. ' this.VType = s.VType;',
  2234. ' this.VJSValue = s.VJSValue;',
  2235. ' return this;',
  2236. ' };',
  2237. ' });',
  2238. ' this.VarRecs = function () {',
  2239. ' var Result = [];',
  2240. ' var v = null;',
  2241. ' v.VType = 1;',
  2242. ' v.VJSValue = 2;',
  2243. ' return Result;',
  2244. ' };',
  2245. '});',
  2246. '']));
  2247. end;
  2248. procedure TTestOptimizations.TestWPO_ArrayOfConst_NotUsed;
  2249. begin
  2250. StartProgram(true,[supTVarRec]);
  2251. Add([
  2252. 'procedure Say(arr: array of const);',
  2253. 'begin',
  2254. 'end;',
  2255. 'begin']);
  2256. ConvertProgram;
  2257. CheckUnit('system.pp',
  2258. LinesToStr([
  2259. 'rtl.module("system", [], function () {',
  2260. ' var $mod = this;',
  2261. '});',
  2262. '']));
  2263. end;
  2264. procedure TTestOptimizations.TestWPO_Class_PropertyInOtherUnit;
  2265. begin
  2266. AddModuleWithIntfImplSrc('unit1.pp',
  2267. LinesToStr([
  2268. 'type',
  2269. ' TObject = class',
  2270. ' private',
  2271. ' const CA = 3;',
  2272. ' private',
  2273. ' FOther: string;',
  2274. ' FA: longint;',
  2275. ' function GetA: longint;',
  2276. ' procedure SetA(Value: longint);',
  2277. ' function IsStoredA: boolean;',
  2278. ' public',
  2279. ' property A: longint read GetA write SetA stored IsStoredA default CA;',
  2280. ' end;',
  2281. '']),
  2282. LinesToStr([
  2283. 'function TObject.geta: longint;',
  2284. 'begin',
  2285. 'end;',
  2286. 'procedure TObject.seta(value: longint);',
  2287. 'begin',
  2288. ' FA:=Value;',
  2289. 'end;',
  2290. 'function TObject.isstoreda: boolean; begin end;',
  2291. '']));
  2292. StartProgram(true);
  2293. Add([
  2294. 'uses unit1;',
  2295. 'var o: TObject;',
  2296. 'begin',
  2297. ' o.A:=o.A;']);
  2298. ConvertProgram;
  2299. CheckUnit('unit1.pp',
  2300. LinesToStr([
  2301. 'rtl.module("unit1", ["system"], function () {',
  2302. ' var $mod = this;',
  2303. ' rtl.createClass(this, "TObject", null, function () {',
  2304. ' this.$init = function () {',
  2305. ' this.FA = 0;',
  2306. ' };',
  2307. ' this.$final = function () {',
  2308. ' };',
  2309. ' this.GetA = function () {',
  2310. ' var Result = 0;',
  2311. ' return Result;',
  2312. ' };',
  2313. ' this.SetA = function (Value) {',
  2314. ' this.FA = Value;',
  2315. ' };',
  2316. ' });',
  2317. '});',
  2318. '']));
  2319. end;
  2320. procedure TTestOptimizations.TestWPO_ProgramPublicDeclaration;
  2321. var
  2322. ActualSrc, ExpectedSrc: String;
  2323. begin
  2324. StartProgram(true);
  2325. Add('var');
  2326. Add(' vPublic: longint; public;');
  2327. Add(' vPrivate: longint;');
  2328. Add('procedure DoPublic; public; begin end;');
  2329. Add('procedure DoPrivate; begin end;');
  2330. Add('begin');
  2331. ConvertProgram;
  2332. ActualSrc:=ConvertJSModuleToString(JSModule);
  2333. ExpectedSrc:=LinesToStr([
  2334. 'rtl.module("program", ["system"], function () {',
  2335. ' var $mod = this;',
  2336. ' this.vPublic = 0;',
  2337. ' this.DoPublic =function(){',
  2338. ' };',
  2339. ' $mod.$main = function () {',
  2340. ' };',
  2341. '});',
  2342. '']);
  2343. CheckDiff('TestWPO_ProgramPublicDeclaration',ExpectedSrc,ActualSrc);
  2344. end;
  2345. procedure TTestOptimizations.TestWPO_ConstructorDefaultValueConst;
  2346. var
  2347. ActualSrc, ExpectedSrc: String;
  2348. begin
  2349. WithTypeInfo:=true;
  2350. StartProgram(true);
  2351. Add([
  2352. 'const gcBlack = 0;',
  2353. 'type',
  2354. ' TColor = longint;',
  2355. ' TObject = class',
  2356. ' private',
  2357. ' FColor: TColor;',
  2358. ' public',
  2359. ' property Color: TColor read FColor write FColor;',
  2360. ' constructor Create(const AColor: TColor = gcBlack);',
  2361. ' end;',
  2362. 'constructor TObject.Create(const AColor: TColor = gcBlack);',
  2363. 'begin',
  2364. ' FColor := AColor;',
  2365. 'end;',
  2366. 'var T: TObject;',
  2367. 'begin',
  2368. ' T := TObject.Create;',
  2369. '']);
  2370. ConvertProgram;
  2371. ActualSrc:=ConvertJSModuleToString(JSModule);
  2372. ExpectedSrc:=LinesToStr([
  2373. 'rtl.module("program",["system"],function () {',
  2374. ' var $mod = this;',
  2375. ' this.gcBlack = 0;',
  2376. ' rtl.createClass(this,"TObject",null,function () {',
  2377. ' this.$init = function () {',
  2378. ' this.FColor = 0;',
  2379. ' };',
  2380. ' this.$final = function () {',
  2381. ' };',
  2382. ' this.Create = function (AColor) {',
  2383. ' this.FColor = AColor;',
  2384. ' return this;',
  2385. ' };',
  2386. ' });',
  2387. ' this.T = null;',
  2388. ' $mod.$main = function () {',
  2389. ' $mod.T = $mod.TObject.$create("Create",[0]);',
  2390. ' };',
  2391. '});',
  2392. '']);
  2393. CheckDiff('TestWPO_ConstructorDefaultValueConst',ExpectedSrc,ActualSrc);
  2394. end;
  2395. procedure TTestOptimizations.TestWPO_RTTI_PublishedField;
  2396. var
  2397. ActualSrc, ExpectedSrc: String;
  2398. begin
  2399. WithTypeInfo:=true;
  2400. StartProgram(true);
  2401. Add([
  2402. 'type',
  2403. ' TArrA = array of char;',
  2404. ' TArrB = array of string;',
  2405. ' TObject = class',
  2406. ' public',
  2407. ' PublicA: TArrA;',
  2408. ' published',
  2409. ' PublishedB: TArrB;',
  2410. ' end;',
  2411. 'var',
  2412. ' C: TObject;',
  2413. 'begin',
  2414. ' C.PublicA:=nil;',
  2415. ' if typeinfo(TObject)=nil then ;',
  2416. '']);
  2417. ConvertProgram;
  2418. ActualSrc:=ConvertJSModuleToString(JSModule);
  2419. ExpectedSrc:=LinesToStr([
  2420. 'rtl.module("program", ["system"], function () {',
  2421. ' var $mod = this;',
  2422. ' this.$rtti.$DynArray("TArrB", {',
  2423. ' eltype: rtl.string',
  2424. ' });',
  2425. ' rtl.createClass(this, "TObject", null, function () {',
  2426. ' this.$init = function () {',
  2427. ' this.PublicA = [];',
  2428. ' this.PublishedB = [];',
  2429. ' };',
  2430. ' this.$final = function () {',
  2431. ' this.PublicA = undefined;',
  2432. ' this.PublishedB = undefined;',
  2433. ' };',
  2434. ' var $r = this.$rtti;',
  2435. ' $r.addField("PublishedB", $mod.$rtti["TArrB"]);',
  2436. ' });',
  2437. ' this.C = null;',
  2438. ' $mod.$main = function () {',
  2439. ' $mod.C.PublicA = [];',
  2440. ' if ($mod.$rtti["TObject"] === null) ;',
  2441. ' };',
  2442. '});',
  2443. '']);
  2444. CheckDiff('TestWPO_RTTI_PublishedField',ExpectedSrc,ActualSrc);
  2445. end;
  2446. procedure TTestOptimizations.TestWPO_RTTI_TypeInfo;
  2447. var
  2448. ActualSrc, ExpectedSrc: String;
  2449. begin
  2450. WithTypeInfo:=true;
  2451. StartProgram(true);
  2452. Add('type');
  2453. Add(' TArrA = array of char;');
  2454. Add(' TArrB = array of string;');
  2455. Add('var');
  2456. Add(' A: TArrA;');
  2457. Add(' B: TArrB;');
  2458. Add(' p: pointer;');
  2459. Add('begin');
  2460. Add(' A:=nil;');
  2461. Add(' p:=typeinfo(B);');
  2462. ConvertProgram;
  2463. ActualSrc:=ConvertJSModuleToString(JSModule);
  2464. ExpectedSrc:=LinesToStr([
  2465. 'rtl.module("program", ["system"], function () {',
  2466. ' var $mod = this;',
  2467. ' this.$rtti.$DynArray("TArrB", {',
  2468. ' eltype: rtl.string',
  2469. ' });',
  2470. ' this.A = [];',
  2471. ' this.B = [];',
  2472. ' this.p = null;',
  2473. ' $mod.$main = function () {',
  2474. ' $mod.A = [];',
  2475. ' $mod.p = $mod.$rtti["TArrB"];',
  2476. ' };',
  2477. '});',
  2478. '']);
  2479. CheckDiff('TestWPO_RTTI_TypeInfo',ExpectedSrc,ActualSrc);
  2480. end;
  2481. Initialization
  2482. RegisterTests([TTestOptimizations]);
  2483. end.