tcoptimizations.pas 65 KB

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