tcmodules.pas 802 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 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=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [coNoTypeInfo];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec
  45. );
  46. TSystemUnitParts = set of TSystemUnitPart;
  47. { TTestHintMessage }
  48. TTestHintMessage = class
  49. public
  50. Id: int64;
  51. MsgType: TMessageType;
  52. MsgNumber: integer;
  53. Msg: string;
  54. SourcePos: TPasSourcePos;
  55. end;
  56. { TTestPasParser }
  57. TTestPasParser = Class(TPasParser)
  58. end;
  59. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  60. { TTestEnginePasResolver }
  61. TTestEnginePasResolver = class(TPas2JsResolver)
  62. private
  63. FFilename: string;
  64. FModule: TPasModule;
  65. FOnFindUnit: TOnFindUnit;
  66. FParser: TTestPasParser;
  67. FStreamResolver: TStreamResolver;
  68. FScanner: TPas2jsPasScanner;
  69. FSource: string;
  70. public
  71. destructor Destroy; override;
  72. function FindUnit(const AName, InFilename: String; NameExpr,
  73. InFileExpr: TPasExpr): TPasModule; override;
  74. procedure UsedInterfacesFinished(Section: TPasSection); override;
  75. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  76. property Filename: string read FFilename write FFilename;
  77. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  78. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  79. property Parser: TTestPasParser read FParser write FParser;
  80. property Source: string read FSource write FSource;
  81. property Module: TPasModule read FModule;
  82. end;
  83. { TCustomTestModule }
  84. TCustomTestModule = Class(TTestCase)
  85. private
  86. FConverter: TPasToJSConverter;
  87. FEngine: TTestEnginePasResolver;
  88. FExpectedErrorClass: ExceptClass;
  89. FExpectedErrorMsg: string;
  90. FExpectedErrorNumber: integer;
  91. FFilename: string;
  92. FFileResolver: TStreamResolver;
  93. FJSImplementationSrc: TJSSourceElements;
  94. FJSImplementationUses: TJSArrayLiteral;
  95. FJSInitBody: TJSFunctionBody;
  96. FJSImplentationUses: TJSArrayLiteral;
  97. FJSInterfaceUses: TJSArrayLiteral;
  98. FJSModule: TJSSourceElements;
  99. FJSModuleSrc: TJSSourceElements;
  100. FJSSource: TStringList;
  101. FModule: TPasModule;
  102. FJSModuleCallArgs: TJSArguments;
  103. FModules: TObjectList;// list of TTestEnginePasResolver
  104. FParser: TTestPasParser;
  105. FPasProgram: TPasProgram;
  106. FHintMsgs: TObjectList; // list of TTestHintMessage
  107. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  108. FJSRegModuleCall: TJSCallExpression;
  109. FScanner: TPas2jsPasScanner;
  110. FSkipTests: boolean;
  111. FSource: TStringList;
  112. FFirstPasStatement: TPasImplBlock;
  113. {$IFDEF EnablePasTreeGlobalRefCount}
  114. FElementRefCountAtSetup: int64;
  115. {$ENDIF}
  116. function GetMsgCount: integer;
  117. function GetMsgs(Index: integer): TTestHintMessage;
  118. function GetResolverCount: integer;
  119. function GetResolvers(Index: integer): TTestEnginePasResolver;
  120. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  121. procedure OnParserLog(Sender: TObject; const Msg: String);
  122. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  123. procedure OnScannerLog(Sender: TObject; const Msg: String);
  124. protected
  125. procedure SetUp; override;
  126. function CreateConverter: TPasToJSConverter; virtual;
  127. function LoadUnit(const aUnitName: String): TPasModule;
  128. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  129. procedure TearDown; override;
  130. Procedure Add(Line: string); virtual;
  131. Procedure Add(const Lines: array of string);
  132. Procedure StartParsing; virtual;
  133. procedure ParseModuleQueue; virtual;
  134. procedure ParseModule; virtual;
  135. procedure ParseProgram; virtual;
  136. procedure ParseUnit; virtual;
  137. protected
  138. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  139. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  140. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  141. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  142. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  143. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  144. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  145. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  146. procedure ConvertModule; virtual;
  147. procedure ConvertProgram; virtual;
  148. procedure ConvertUnit; virtual;
  149. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  150. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  151. function GetDottedIdentifier(El: TJSElement): string;
  152. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  153. ImplStatements: string = ''); virtual;
  154. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  155. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  156. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  157. Msg: string; Marker: PSrcMarker = nil); virtual;
  158. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  159. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  160. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  161. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  162. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  163. function IsErrorExpected(E: Exception): boolean;
  164. procedure HandleScannerError(E: EScannerError);
  165. procedure HandleParserError(E: EParserError);
  166. procedure HandlePasResolveError(E: EPasResolve);
  167. procedure HandlePas2JSError(E: EPas2JS);
  168. procedure HandleException(E: Exception);
  169. procedure FailException(E: Exception);
  170. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  171. function IndexOfResolver(const Filename: string): integer;
  172. function GetResolver(const Filename: string): TTestEnginePasResolver;
  173. function GetDefaultNamespace: string;
  174. property PasProgram: TPasProgram Read FPasProgram;
  175. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  176. property ResolverCount: integer read GetResolverCount;
  177. property Engine: TTestEnginePasResolver read FEngine;
  178. property Filename: string read FFilename;
  179. Property Module: TPasModule Read FModule;
  180. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  181. property Converter: TPasToJSConverter read FConverter;
  182. property JSSource: TStringList read FJSSource;
  183. property JSModule: TJSSourceElements read FJSModule;
  184. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  185. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  186. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  187. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  188. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  189. property JSInitBody: TJSFunctionBody read FJSInitBody;
  190. property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
  191. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  192. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  193. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  194. property SkipTests: boolean read FSkipTests write FSkipTests;
  195. public
  196. constructor Create; override;
  197. destructor Destroy; override;
  198. property Source: TStringList read FSource;
  199. property FileResolver: TStreamResolver read FFileResolver;
  200. property Scanner: TPas2jsPasScanner read FScanner;
  201. property Parser: TTestPasParser read FParser;
  202. property MsgCount: integer read GetMsgCount;
  203. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  204. end;
  205. { TTestModule }
  206. TTestModule = class(TCustomTestModule)
  207. Published
  208. Procedure TestReservedWords;
  209. // program/units
  210. Procedure TestEmptyProgram;
  211. Procedure TestEmptyProgramUseStrict;
  212. Procedure TestEmptyUnit;
  213. Procedure TestEmptyUnitUseStrict;
  214. Procedure TestDottedUnitNames;
  215. Procedure TestDottedUnitNameImpl;
  216. Procedure TestDottedUnitExpr;
  217. Procedure Test_ModeFPCFail;
  218. Procedure Test_ModeSwitchCBlocksFail;
  219. Procedure TestUnit_UseSystem;
  220. Procedure TestUnit_Intf1Impl2Intf1;
  221. Procedure TestIncludeVersion;
  222. // vars/const
  223. Procedure TestVarInt;
  224. Procedure TestVarBaseTypes;
  225. Procedure TestBaseTypeSingleFail;
  226. Procedure TestBaseTypeExtendedFail;
  227. Procedure TestConstBaseTypes;
  228. Procedure TestUnitImplVars;
  229. Procedure TestUnitImplConsts;
  230. Procedure TestUnitImplRecord;
  231. Procedure TestRenameJSNameConflict;
  232. Procedure TestLocalConst;
  233. Procedure TestVarExternal;
  234. Procedure TestVarExternalOtherUnit;
  235. Procedure TestVarAbsoluteFail;
  236. Procedure TestConstExternal;
  237. // numbers
  238. Procedure TestDouble;
  239. Procedure TestInteger;
  240. Procedure TestIntegerRange;
  241. Procedure TestIntegerTypecasts;
  242. Procedure TestInteger_BitwiseShrNativeInt;
  243. Procedure TestInteger_BitwiseShlNativeInt;
  244. Procedure TestInteger_SystemFunc;
  245. Procedure TestCurrency;
  246. Procedure TestForBoolDo;
  247. Procedure TestForIntDo;
  248. Procedure TestForIntInDo;
  249. // strings
  250. Procedure TestCharConst;
  251. Procedure TestChar_Compare;
  252. Procedure TestChar_BuiltInProcs;
  253. Procedure TestStringConst;
  254. Procedure TestStringConstSurrogate;
  255. Procedure TestString_Length;
  256. Procedure TestString_Compare;
  257. Procedure TestString_SetLength;
  258. Procedure TestString_CharAt;
  259. Procedure TestStringHMinusFail;
  260. Procedure TestStr;
  261. Procedure TestBaseType_AnsiStringFail;
  262. Procedure TestBaseType_WideStringFail;
  263. Procedure TestBaseType_ShortStringFail;
  264. Procedure TestBaseType_RawByteStringFail;
  265. Procedure TestTypeShortstring_Fail;
  266. Procedure TestCharSet_Custom;
  267. Procedure TestForCharDo;
  268. Procedure TestForCharInDo;
  269. // alias types
  270. Procedure TestAliasTypeRef;
  271. Procedure TestTypeCast_BaseTypes;
  272. Procedure TestTypeCast_AliasBaseTypes;
  273. // functions
  274. Procedure TestEmptyProc;
  275. Procedure TestProcOneParam;
  276. Procedure TestFunctionWithoutParams;
  277. Procedure TestProcedureWithoutParams;
  278. Procedure TestPrgProcVar;
  279. Procedure TestProcTwoArgs;
  280. Procedure TestProc_DefaultValue;
  281. Procedure TestUnitProcVar;
  282. Procedure TestImplProc;
  283. Procedure TestFunctionResult;
  284. Procedure TestNestedProc;
  285. Procedure TestNestedProc_ResultString;
  286. Procedure TestForwardProc;
  287. Procedure TestNestedForwardProc;
  288. Procedure TestAssignFunctionResult;
  289. Procedure TestFunctionResultInCondition;
  290. Procedure TestFunctionResultInForLoop;
  291. Procedure TestFunctionResultInTypeCast;
  292. Procedure TestExit;
  293. Procedure TestExit_ResultInFinally;
  294. Procedure TestBreak;
  295. Procedure TestBreakAsVar;
  296. Procedure TestContinue;
  297. Procedure TestProc_External;
  298. Procedure TestProc_ExternalOtherUnit;
  299. Procedure TestProc_Asm;
  300. Procedure TestProc_Assembler;
  301. Procedure TestProc_VarParam;
  302. Procedure TestProc_VarParamString;
  303. Procedure TestProc_VarParamV;
  304. Procedure TestProc_Overload;
  305. Procedure TestProc_OverloadForward;
  306. Procedure TestProc_OverloadIntfImpl;
  307. Procedure TestProc_OverloadNested;
  308. Procedure TestProc_OverloadUnitCycle;
  309. Procedure TestProc_Varargs;
  310. Procedure TestProc_ConstOrder;
  311. Procedure TestProc_DuplicateConst;
  312. Procedure TestProc_LocalVarAbsolute;
  313. Procedure TestProc_LocalVarInit;
  314. Procedure TestProc_ReservedWords;
  315. Procedure TestProc_ConstRefWord;
  316. // anonymous functions
  317. Procedure TestAnonymousProc_Assign_ObjFPC;
  318. Procedure TestAnonymousProc_Assign_Delphi;
  319. Procedure TestAnonymousProc_Arg;
  320. Procedure TestAnonymousProc_Typecast;
  321. Procedure TestAnonymousProc_With;
  322. Procedure TestAnonymousProc_ExceptOn;
  323. Procedure TestAnonymousProc_Nested;
  324. Procedure TestAnonymousProc_NestedAssignResult;
  325. Procedure TestAnonymousProc_Class;
  326. Procedure TestAnonymousProc_ForLoop;
  327. // enums, sets
  328. Procedure TestEnum_Name;
  329. Procedure TestEnum_Number;
  330. Procedure TestEnum_ConstFail;
  331. Procedure TestEnum_Functions;
  332. Procedure TestEnum_AsParams;
  333. Procedure TestEnumRange_Array;
  334. Procedure TestEnum_ForIn;
  335. Procedure TestEnum_ScopedNumber;
  336. Procedure TestEnum_InFunction;
  337. Procedure TestSet_Enum;
  338. Procedure TestSet_Operators;
  339. Procedure TestSet_Operator_In;
  340. Procedure TestSet_Functions;
  341. Procedure TestSet_PassAsArgClone;
  342. Procedure TestSet_AsParams;
  343. Procedure TestSet_Property;
  344. Procedure TestSet_EnumConst;
  345. Procedure TestSet_IntConst;
  346. Procedure TestSet_AnonymousEnumType;
  347. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  348. Procedure TestSet_ConstEnum;
  349. Procedure TestSet_ConstChar;
  350. Procedure TestSet_ConstInt;
  351. Procedure TestSet_InFunction;
  352. Procedure TestSet_ForIn;
  353. // statements
  354. Procedure TestNestBegin;
  355. Procedure TestIncDec;
  356. Procedure TestLoHiFpcMode;
  357. Procedure TestLoHiDelphiMode;
  358. Procedure TestAssignments;
  359. Procedure TestArithmeticOperators1;
  360. Procedure TestLogicalOperators;
  361. Procedure TestBitwiseOperators;
  362. Procedure TestBitwiseOperatorsLongword;
  363. Procedure TestFunctionInt;
  364. Procedure TestFunctionString;
  365. Procedure TestIfThen;
  366. Procedure TestForLoop;
  367. Procedure TestForLoopInsideFunction;
  368. Procedure TestForLoop_ReadVarAfter;
  369. Procedure TestForLoop_Nested;
  370. Procedure TestRepeatUntil;
  371. Procedure TestAsmBlock;
  372. Procedure TestAsmPas_Impl; // ToDo
  373. Procedure TestTryFinally;
  374. Procedure TestTryExcept;
  375. Procedure TestTryExcept_ReservedWords;
  376. Procedure TestIfThenRaiseElse;
  377. Procedure TestCaseOf;
  378. Procedure TestCaseOf_UseSwitch;
  379. Procedure TestCaseOfNoElse;
  380. Procedure TestCaseOfNoElse_UseSwitch;
  381. Procedure TestCaseOfRange;
  382. Procedure TestCaseOfString;
  383. Procedure TestCaseOfChar;
  384. Procedure TestCaseOfExternalClassConst;
  385. Procedure TestDebugger;
  386. // arrays
  387. Procedure TestArray_Dynamic;
  388. Procedure TestArray_Dynamic_Nil;
  389. Procedure TestArray_DynMultiDimensional;
  390. Procedure TestArray_StaticInt;
  391. Procedure TestArray_StaticBool;
  392. Procedure TestArray_StaticChar;
  393. Procedure TestArray_StaticMultiDim;
  394. Procedure TestArray_StaticInFunction;
  395. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  396. Procedure TestArrayOfRecord;
  397. Procedure TestArray_StaticRecord;
  398. Procedure TestArrayOfSet;
  399. Procedure TestArray_DynAsParam;
  400. Procedure TestArray_StaticAsParam;
  401. Procedure TestArrayElement_AsParams;
  402. Procedure TestArrayElementFromFuncResult_AsParams;
  403. Procedure TestArrayEnumTypeRange;
  404. Procedure TestArray_SetLengthOutArg;
  405. Procedure TestArray_SetLengthProperty;
  406. Procedure TestArray_SetLengthMultiDim;
  407. Procedure TestArray_SetLengthDynOfStatic;
  408. Procedure TestArray_OpenArrayOfString;
  409. Procedure TestArray_ConstRef;
  410. Procedure TestArray_Concat;
  411. Procedure TestArray_Copy;
  412. Procedure TestArray_InsertDelete;
  413. Procedure TestArray_DynArrayConstObjFPC;
  414. Procedure TestArray_DynArrayConstDelphi;
  415. Procedure TestArray_ArrayLitAsParam;
  416. Procedure TestArray_ArrayLitMultiDimAsParam;
  417. Procedure TestArray_ArrayLitStaticAsParam;
  418. Procedure TestArray_ForInArrOfString;
  419. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  420. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  421. Procedure TestArrayOfConst_TVarRec;
  422. Procedure TestArrayOfConst_PassBaseTypes;
  423. Procedure TestArrayOfConst_PassObj;
  424. // ToDo: tcfiler TPasModuleScope.SystemTVarRec TPas2JSModuleScope.SystemVarRecs
  425. // record
  426. Procedure TestRecord_Empty;
  427. Procedure TestRecord_Var;
  428. Procedure TestRecord_VarExternal;
  429. Procedure TestRecord_WithDo;
  430. Procedure TestRecord_Assign;
  431. Procedure TestRecord_AsParams;
  432. Procedure TestRecord_ConstRef;
  433. Procedure TestRecordElement_AsParams;
  434. Procedure TestRecordElementFromFuncResult_AsParams;
  435. Procedure TestRecordElementFromWith_AsParams;
  436. Procedure TestRecord_Equal;
  437. Procedure TestRecord_TypeCastJSValueToRecord;
  438. Procedure TestRecord_VariantFail;
  439. Procedure TestRecord_FieldArray;
  440. Procedure TestRecord_Const;
  441. Procedure TestRecord_TypecastFail;
  442. Procedure TestRecord_InFunction;
  443. Procedure TestRecord_AnonymousFail;
  444. // ToDo: RTTI of local record
  445. // ToDo: pcu local record, name clash and rtti
  446. // advanced record
  447. Procedure TestAdvRecord_Function;
  448. Procedure TestAdvRecord_Property;
  449. Procedure TestAdvRecord_PropertyDefault;
  450. Procedure TestAdvRecord_Property_ClassMethod;
  451. Procedure TestAdvRecord_Const;
  452. Procedure TestAdvRecord_ExternalField;
  453. Procedure TestAdvRecord_SubRecord;
  454. Procedure TestAdvRecord_SubClass;
  455. Procedure TestAdvRecord_SubInterfaceFail;
  456. Procedure TestAdvRecord_Constructor;
  457. // ToDo: class constructor
  458. // classes
  459. Procedure TestClass_TObjectDefaultConstructor;
  460. Procedure TestClass_TObjectConstructorWithParams;
  461. Procedure TestClass_TObjectConstructorWithDefaultParam;
  462. Procedure TestClass_Var;
  463. Procedure TestClass_Method;
  464. Procedure TestClass_Implementation;
  465. Procedure TestClass_Inheritance;
  466. Procedure TestClass_TypeAlias;
  467. Procedure TestClass_AbstractMethod;
  468. Procedure TestClass_CallInherited_ProcNoParams;
  469. Procedure TestClass_CallInherited_WithParams;
  470. Procedure TestClasS_CallInheritedConstructor;
  471. Procedure TestClass_ClassVar_Assign;
  472. Procedure TestClass_CallClassMethod;
  473. Procedure TestClass_Property;
  474. Procedure TestClass_Property_ClassMethod;
  475. Procedure TestClass_Property_Indexed;
  476. Procedure TestClass_Property_IndexSpec;
  477. Procedure TestClass_PropertyOfTypeArray;
  478. Procedure TestClass_PropertyDefault;
  479. Procedure TestClass_PropertyDefault2;
  480. Procedure TestClass_PropertyOverride;
  481. Procedure TestClass_PropertyIncVisibility;
  482. Procedure TestClass_Assigned;
  483. Procedure TestClass_WithClassDoCreate;
  484. Procedure TestClass_WithClassInstDoProperty;
  485. Procedure TestClass_WithClassInstDoPropertyWithParams;
  486. Procedure TestClass_WithClassInstDoFunc;
  487. Procedure TestClass_ProcVarDelphi;
  488. Procedure TestClass_TypeCast;
  489. Procedure TestClass_TypeCastUntypedParam;
  490. Procedure TestClass_Overloads;
  491. Procedure TestClass_OverloadsAncestor;
  492. Procedure TestClass_OverloadConstructor;
  493. Procedure TestClass_OverloadDelphiOverride;
  494. Procedure TestClass_ReintroduceVarDelphi;
  495. Procedure TestClass_ReintroducedVar;
  496. Procedure TestClass_RaiseDescendant;
  497. Procedure TestClass_ExternalMethod;
  498. Procedure TestClass_ExternalVirtualNameMismatchFail;
  499. Procedure TestClass_ExternalOverrideFail;
  500. Procedure TestClass_ExternalVar;
  501. Procedure TestClass_Const;
  502. Procedure TestClass_LocalVarSelfFail;
  503. Procedure TestClass_ArgSelfFail;
  504. Procedure TestClass_NestedProcSelf;
  505. Procedure TestClass_NestedProcSelf2;
  506. Procedure TestClass_NestedProcClassSelf;
  507. Procedure TestClass_NestedProcCallInherited;
  508. Procedure TestClass_TObjectFree;
  509. Procedure TestClass_TObjectFreeNewInstance;
  510. Procedure TestClass_TObjectFreeLowerCase;
  511. Procedure TestClass_TObjectFreeFunctionFail;
  512. Procedure TestClass_TObjectFreePropertyFail;
  513. Procedure TestClass_ForIn;
  514. // class of
  515. Procedure TestClassOf_Create;
  516. Procedure TestClassOf_Call;
  517. Procedure TestClassOf_Assign;
  518. Procedure TestClassOf_Is;
  519. Procedure TestClassOf_Compare;
  520. Procedure TestClassOf_ClassVar;
  521. Procedure TestClassOf_ClassMethod;
  522. Procedure TestClassOf_ClassProperty;
  523. Procedure TestClassOf_ClassMethodSelf;
  524. Procedure TestClassOf_TypeCast;
  525. Procedure TestClassOf_ImplicitFunctionCall;
  526. Procedure TestClassOf_Const;
  527. // nested class
  528. Procedure TestNestedClass_Alias;
  529. Procedure TestNestedClass_Record;
  530. Procedure TestNestedClass_Class;
  531. // external class
  532. Procedure TestExternalClass_Var;
  533. Procedure TestExternalClass_Const;
  534. Procedure TestExternalClass_Dollar;
  535. Procedure TestExternalClass_DuplicateVarFail;
  536. Procedure TestExternalClass_Method;
  537. Procedure TestExternalClass_ClassMethod;
  538. Procedure TestExternalClass_ClassMethodStatic;
  539. Procedure TestExternalClass_FunctionResultInTypeCast;
  540. Procedure TestExternalClass_NonExternalOverride;
  541. Procedure TestExternalClass_OverloadHint;
  542. Procedure TestExternalClass_SameNamePublishedProperty;
  543. Procedure TestExternalClass_Property;
  544. Procedure TestExternalClass_PropertyDate;
  545. Procedure TestExternalClass_ClassProperty;
  546. Procedure TestExternalClass_ClassOf;
  547. Procedure TestExternalClass_ClassOtherUnit;
  548. Procedure TestExternalClass_Is;
  549. Procedure TestExternalClass_As;
  550. Procedure TestExternalClass_DestructorFail;
  551. Procedure TestExternalClass_New;
  552. Procedure TestExternalClass_ClassOf_New;
  553. Procedure TestExternalClass_FuncClassOf_New;
  554. Procedure TestExternalClass_New_PasClassFail;
  555. Procedure TestExternalClass_New_PasClassBracketsFail;
  556. Procedure TestExternalClass_LocalConstSameName;
  557. Procedure TestExternalClass_ReintroduceOverload;
  558. Procedure TestExternalClass_Inherited;
  559. Procedure TestExternalClass_PascalAncestorFail;
  560. Procedure TestExternalClass_NewInstance;
  561. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  562. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  563. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  564. Procedure TestExternalClass_PascalProperty;
  565. Procedure TestExternalClass_TypeCastToRootClass;
  566. Procedure TestExternalClass_TypeCastToJSObject;
  567. Procedure TestExternalClass_TypeCastStringToExternalString;
  568. Procedure TestExternalClass_TypeCastToJSFunction;
  569. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  570. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  571. Procedure TestExternalClass_BracketAccessor;
  572. Procedure TestExternalClass_BracketAccessor_Call;
  573. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  574. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  575. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  576. Procedure TestExternalClass_BracketAccessor_MultiType;
  577. Procedure TestExternalClass_BracketAccessor_Index;
  578. Procedure TestExternalClass_ForInJSObject;
  579. Procedure TestExternalClass_ForInJSArray;
  580. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  581. // class interfaces
  582. Procedure TestClassInterface_Corba;
  583. Procedure TestClassInterface_ProcExternalFail;
  584. Procedure TestClassInterface_Overloads;
  585. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  586. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  587. Procedure TestClassInterface_AncestorImpl;
  588. Procedure TestClassInterface_ImplReintroduce;
  589. Procedure TestClassInterface_MethodResolution;
  590. Procedure TestClassInterface_AncestorMoreInterfaces;
  591. Procedure TestClassInterface_MethodOverride;
  592. Procedure TestClassInterface_Corba_Delegation;
  593. Procedure TestClassInterface_Corba_DelegationStatic;
  594. Procedure TestClassInterface_Corba_Operators;
  595. Procedure TestClassInterface_Corba_Args;
  596. Procedure TestClassInterface_Corba_ForIn;
  597. Procedure TestClassInterface_COM_AssignVar;
  598. Procedure TestClassInterface_COM_AssignArg;
  599. Procedure TestClassInterface_COM_FunctionResult;
  600. Procedure TestClassInterface_COM_InheritedFuncResult;
  601. Procedure TestClassInterface_COM_IsAsTypeCasts;
  602. Procedure TestClassInterface_COM_PassAsArg;
  603. Procedure TestClassInterface_COM_PassToUntypedParam;
  604. Procedure TestClassInterface_COM_FunctionInExpr;
  605. Procedure TestClassInterface_COM_Property;
  606. Procedure TestClassInterface_COM_IntfProperty;
  607. Procedure TestClassInterface_COM_Delegation;
  608. Procedure TestClassInterface_COM_With;
  609. Procedure TestClassInterface_COM_ForIn;
  610. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  611. Procedure TestClassInterface_COM_RecordIntfFail;
  612. Procedure TestClassInterface_COM_UnitInitialization;
  613. Procedure TestClassInterface_GUID;
  614. Procedure TestClassInterface_GUIDProperty;
  615. // helpers
  616. Procedure TestClassHelper_ClassVar;
  617. Procedure TestClassHelper_Method_AccessInstanceFields;
  618. Procedure TestClassHelper_Method_Call;
  619. Procedure TestClassHelper_Method_Nested_Call;
  620. Procedure TestClassHelper_ClassMethod_Call;
  621. Procedure TestClassHelper_ClassOf;
  622. Procedure TestClassHelper_MethodRefObjFPC;
  623. Procedure TestClassHelper_Constructor;
  624. Procedure TestClassHelper_InheritedObjFPC;
  625. Procedure TestClassHelper_Property;
  626. Procedure TestClassHelper_Property_Array;
  627. Procedure TestClassHelper_Property_Array_Default;
  628. Procedure TestClassHelper_Property_Array_DefaultDefault;
  629. Procedure TestClassHelper_ClassProperty;
  630. Procedure TestClassHelper_ClassPropertyStatic;
  631. Procedure TestClassHelper_ClassProperty_Array;
  632. Procedure TestClassHelper_ForIn;
  633. Procedure TestClassHelper_PassProperty;
  634. Procedure TestExtClassHelper_ClassVar;
  635. Procedure TestExtClassHelper_Method_Call;
  636. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  637. Procedure TestRecordHelper_ClassVar;
  638. Procedure TestRecordHelper_Method_Call;
  639. Procedure TestRecordHelper_Constructor;
  640. Procedure TestTypeHelper_ClassVar;
  641. Procedure TestTypeHelper_PassResultElement;
  642. Procedure TestTypeHelper_PassArgs;
  643. Procedure TestTypeHelper_PassVarConst;
  644. Procedure TestTypeHelper_PassFuncResult;
  645. Procedure TestTypeHelper_PassPropertyField;
  646. Procedure TestTypeHelper_PassPropertyGetter;
  647. Procedure TestTypeHelper_PassClassPropertyField;
  648. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  649. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  650. Procedure TestTypeHelper_Property;
  651. Procedure TestTypeHelper_Property_Array;
  652. Procedure TestTypeHelper_ClassProperty;
  653. Procedure TestTypeHelper_ClassProperty_Array;
  654. Procedure TestTypeHelper_ClassMethod;
  655. Procedure TestTypeHelper_ExtClassMethodFail;
  656. Procedure TestTypeHelper_Constructor;
  657. Procedure TestTypeHelper_Word;
  658. Procedure TestTypeHelper_Double;
  659. Procedure TestTypeHelper_NativeInt;
  660. Procedure TestTypeHelper_StringChar;
  661. Procedure TestTypeHelper_JSValue;
  662. Procedure TestTypeHelper_Array;
  663. Procedure TestTypeHelper_EnumType;
  664. Procedure TestTypeHelper_SetType;
  665. Procedure TestTypeHelper_NestedSelf;
  666. // proc types
  667. Procedure TestProcType;
  668. Procedure TestProcType_Arg;
  669. Procedure TestProcType_FunctionFPC;
  670. Procedure TestProcType_FunctionDelphi;
  671. Procedure TestProcType_ProcedureDelphi;
  672. Procedure TestProcType_AsParam;
  673. Procedure TestProcType_MethodFPC;
  674. Procedure TestProcType_MethodDelphi;
  675. Procedure TestProcType_PropertyFPC;
  676. Procedure TestProcType_PropertyDelphi;
  677. Procedure TestProcType_WithClassInstDoPropertyFPC;
  678. Procedure TestProcType_Nested;
  679. Procedure TestProcType_NestedOfObject;
  680. Procedure TestProcType_ReferenceToProc;
  681. Procedure TestProcType_ReferenceToMethod;
  682. Procedure TestProcType_Typecast;
  683. Procedure TestProcType_PassProcToUntyped;
  684. Procedure TestProcType_PassProcToArray;
  685. // pointer
  686. Procedure TestPointer;
  687. Procedure TestPointer_Proc;
  688. Procedure TestPointer_AssignRecordFail;
  689. Procedure TestPointer_AssignStaticArrayFail;
  690. Procedure TestPointer_TypeCastJSValueToPointer;
  691. Procedure TestPointer_NonRecordFail;
  692. Procedure TestPointer_AnonymousArgTypeFail;
  693. Procedure TestPointer_AnonymousVarTypeFail;
  694. Procedure TestPointer_AnonymousResultTypeFail;
  695. Procedure TestPointer_AddrOperatorFail;
  696. Procedure TestPointer_ArrayParamsFail;
  697. Procedure TestPointer_PointerAddFail;
  698. Procedure TestPointer_IncPointerFail;
  699. Procedure TestPointer_Record;
  700. Procedure TestPointer_RecordArg;
  701. // jsvalue
  702. Procedure TestJSValue_AssignToJSValue;
  703. Procedure TestJSValue_TypeCastToBaseType;
  704. Procedure TestJSValue_TypecastToJSValue;
  705. Procedure TestJSValue_Equal;
  706. Procedure TestJSValue_If;
  707. Procedure TestJSValue_Not;
  708. Procedure TestJSValue_Enum;
  709. Procedure TestJSValue_ClassInstance;
  710. Procedure TestJSValue_ClassOf;
  711. Procedure TestJSValue_ArrayOfJSValue;
  712. Procedure TestJSValue_ArrayLit;
  713. Procedure TestJSValue_Params;
  714. Procedure TestJSValue_UntypedParam;
  715. Procedure TestJSValue_FuncResultType;
  716. Procedure TestJSValue_ProcType_Assign;
  717. Procedure TestJSValue_ProcType_Equal;
  718. Procedure TestJSValue_ProcType_Param;
  719. Procedure TestJSValue_AssignToPointerFail;
  720. Procedure TestJSValue_OverloadDouble;
  721. Procedure TestJSValue_OverloadNativeInt;
  722. Procedure TestJSValue_OverloadWord;
  723. Procedure TestJSValue_OverloadString;
  724. Procedure TestJSValue_OverloadChar;
  725. Procedure TestJSValue_OverloadPointer;
  726. Procedure TestJSValue_ForIn;
  727. // RTTI
  728. Procedure TestRTTI_IntRange;
  729. Procedure TestRTTI_Double;
  730. Procedure TestRTTI_ProcType;
  731. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  732. Procedure TestRTTI_EnumAndSetType;
  733. Procedure TestRTTI_EnumRange;
  734. Procedure TestRTTI_AnonymousEnumType;
  735. Procedure TestRTTI_StaticArray;
  736. Procedure TestRTTI_DynArray;
  737. Procedure TestRTTI_ArrayNestedAnonymous;
  738. Procedure TestRTTI_PublishedMethodOverloadFail;
  739. Procedure TestRTTI_PublishedMethodExternalFail;
  740. Procedure TestRTTI_PublishedClassPropertyFail;
  741. Procedure TestRTTI_PublishedClassFieldFail;
  742. Procedure TestRTTI_PublishedFieldExternalFail;
  743. Procedure TestRTTI_Class_Field;
  744. Procedure TestRTTI_Class_Method;
  745. Procedure TestRTTI_Class_MethodArgFlags;
  746. Procedure TestRTTI_Class_Property;
  747. Procedure TestRTTI_Class_PropertyParams;
  748. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  749. Procedure TestRTTI_Class_OmitRTTI;
  750. Procedure TestRTTI_IndexModifier;
  751. Procedure TestRTTI_StoredModifier;
  752. Procedure TestRTTI_DefaultValue;
  753. Procedure TestRTTI_DefaultValueSet;
  754. Procedure TestRTTI_DefaultValueRangeType;
  755. Procedure TestRTTI_DefaultValueInherit;
  756. Procedure TestRTTI_OverrideMethod;
  757. Procedure TestRTTI_ReintroduceMethod;
  758. Procedure TestRTTI_OverloadProperty;
  759. // ToDo: array argument
  760. Procedure TestRTTI_ClassForward;
  761. Procedure TestRTTI_ClassOf;
  762. Procedure TestRTTI_Record;
  763. Procedure TestRTTI_RecordAnonymousArray;
  764. Procedure TestRTTI_LocalTypes;
  765. Procedure TestRTTI_TypeInfo_BaseTypes;
  766. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  767. Procedure TestRTTI_TypeInfo_LocalFail;
  768. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  769. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  770. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  771. Procedure TestRTTI_TypeInfo_FunctionClassType;
  772. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  773. Procedure TestRTTI_Interface_Corba;
  774. Procedure TestRTTI_Interface_COM;
  775. Procedure TestRTTI_ClassHelper;
  776. // Resourcestring
  777. Procedure TestResourcestringProgram;
  778. Procedure TestResourcestringUnit;
  779. Procedure TestResourcestringImplementation;
  780. // Attributes
  781. Procedure TestAtributes_Ignore;
  782. // Assertions, checks
  783. procedure TestAssert;
  784. procedure TestAssert_SysUtils;
  785. procedure TestObjectChecks;
  786. procedure TestRangeChecks_AssignInt;
  787. procedure TestRangeChecks_AssignIntRange;
  788. procedure TestRangeChecks_AssignEnum;
  789. procedure TestRangeChecks_AssignEnumRange;
  790. procedure TestRangeChecks_AssignChar;
  791. procedure TestRangeChecks_AssignCharRange;
  792. procedure TestRangeChecks_ArrayIndex;
  793. procedure TestRangeChecks_ArrayOfRecIndex;
  794. procedure TestRangeChecks_StringIndex;
  795. procedure TestRangeChecks_TypecastInt;
  796. end;
  797. function LinesToStr(Args: array of const): string;
  798. function ExtractFileUnitName(aFilename: string): string;
  799. function JSToStr(El: TJSElement): string;
  800. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  801. implementation
  802. function LinesToStr(Args: array of const): string;
  803. var
  804. s: String;
  805. i: Integer;
  806. begin
  807. s:='';
  808. for i:=Low(Args) to High(Args) do
  809. case Args[i].VType of
  810. vtChar: s += Args[i].VChar+LineEnding;
  811. vtString: s += Args[i].VString^+LineEnding;
  812. vtPChar: s += Args[i].VPChar+LineEnding;
  813. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  814. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  815. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  816. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  817. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  818. end;
  819. Result:=s;
  820. end;
  821. function ExtractFileUnitName(aFilename: string): string;
  822. var
  823. p: Integer;
  824. begin
  825. Result:=ExtractFileName(aFilename);
  826. if Result='' then exit;
  827. for p:=length(Result) downto 1 do
  828. case Result[p] of
  829. '/','\': exit;
  830. '.':
  831. begin
  832. Delete(Result,p,length(Result));
  833. exit;
  834. end;
  835. end;
  836. end;
  837. function JSToStr(El: TJSElement): string;
  838. var
  839. aWriter: TBufferWriter;
  840. aJSWriter: TJSWriter;
  841. begin
  842. aJSWriter:=nil;
  843. aWriter:=TBufferWriter.Create(1000);
  844. try
  845. aJSWriter:=TJSWriter.Create(aWriter);
  846. aJSWriter.IndentSize:=2;
  847. aJSWriter.WriteJS(El);
  848. Result:=aWriter.AsString;
  849. finally
  850. aJSWriter.Free;
  851. aWriter.Free;
  852. end;
  853. end;
  854. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  855. // search diff, ignore changes in spaces
  856. const
  857. SpaceChars = [#9,#10,#13,' '];
  858. var
  859. ExpectedP, ActualP: PChar;
  860. function FindLineEnd(p: PChar): PChar;
  861. begin
  862. Result:=p;
  863. while not (Result^ in [#0,#10,#13]) do inc(Result);
  864. end;
  865. function FindLineStart(p, MinP: PChar): PChar;
  866. begin
  867. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  868. Result:=p;
  869. end;
  870. procedure SkipLineEnd(var p: PChar);
  871. begin
  872. if p^ in [#10,#13] then
  873. begin
  874. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  875. inc(p,2)
  876. else
  877. inc(p);
  878. end;
  879. end;
  880. procedure DiffFound;
  881. var
  882. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  883. ExpLine, ActLine: String;
  884. i, LineNo, DiffLineNo: Integer;
  885. begin
  886. writeln('Diff found "',Msg,'". Lines:');
  887. // write correct lines
  888. p:=PChar(Expected);
  889. LineNo:=0;
  890. DiffLineNo:=0;
  891. repeat
  892. StartPos:=p;
  893. while not (p^ in [#0,#10,#13]) do inc(p);
  894. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  895. SkipLineEnd(p);
  896. inc(LineNo);
  897. if (p<=ExpectedP) and (p^<>#0) then
  898. begin
  899. writeln('= ',ExpLine);
  900. end else begin
  901. // diff line
  902. if DiffLineNo=0 then DiffLineNo:=LineNo;
  903. // write actual line
  904. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  905. ActLineEndP:=FindLineEnd(ActualP);
  906. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  907. writeln('- ',ActLine);
  908. // write expected line
  909. writeln('+ ',ExpLine);
  910. // write empty line with pointer ^
  911. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  912. writeln('^');
  913. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  914. CheckSrcDiff:=false;
  915. // write up to three following actual lines to get some context
  916. for i:=1 to 3 do begin
  917. ActLineStartP:=ActLineEndP;
  918. SkipLineEnd(ActLineStartP);
  919. if ActLineStartP^=#0 then break;
  920. ActLineEndP:=FindLineEnd(ActLineStartP);
  921. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  922. writeln('~ ',ActLine);
  923. end;
  924. exit;
  925. end;
  926. until p^=#0;
  927. writeln('DiffFound Actual:-----------------------');
  928. writeln(Actual);
  929. writeln('DiffFound Expected:---------------------');
  930. writeln(Expected);
  931. writeln('DiffFound ------------------------------');
  932. Msg:='diff found, but lines are the same, internal error';
  933. CheckSrcDiff:=false;
  934. end;
  935. var
  936. IsSpaceNeeded: Boolean;
  937. LastChar, Quote: Char;
  938. begin
  939. Result:=true;
  940. Msg:='';
  941. if Expected='' then Expected:=' ';
  942. if Actual='' then Actual:=' ';
  943. ExpectedP:=PChar(Expected);
  944. ActualP:=PChar(Actual);
  945. repeat
  946. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  947. case ExpectedP^ of
  948. #0:
  949. begin
  950. // check that rest of Actual has only spaces
  951. while ActualP^ in SpaceChars do inc(ActualP);
  952. if ActualP^<>#0 then
  953. begin
  954. DiffFound;
  955. exit;
  956. end;
  957. exit(true);
  958. end;
  959. ' ',#9,#10,#13:
  960. begin
  961. // skip space in Expected
  962. IsSpaceNeeded:=false;
  963. if ExpectedP>PChar(Expected) then
  964. LastChar:=ExpectedP[-1]
  965. else
  966. LastChar:=#0;
  967. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  968. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  969. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  970. IsSpaceNeeded:=true;
  971. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  972. begin
  973. DiffFound;
  974. exit;
  975. end;
  976. while ActualP^ in SpaceChars do inc(ActualP);
  977. end;
  978. '''','"':
  979. begin
  980. while ActualP^ in SpaceChars do inc(ActualP);
  981. if ExpectedP^<>ActualP^ then
  982. begin
  983. DiffFound;
  984. exit;
  985. end;
  986. Quote:=ExpectedP^;
  987. repeat
  988. inc(ExpectedP);
  989. inc(ActualP);
  990. if ExpectedP^<>ActualP^ then
  991. begin
  992. DiffFound;
  993. exit;
  994. end;
  995. if (ExpectedP^ in [#0,#10,#13]) then
  996. break
  997. else if (ExpectedP^=Quote) then
  998. begin
  999. inc(ExpectedP);
  1000. inc(ActualP);
  1001. break;
  1002. end;
  1003. until false;
  1004. end;
  1005. else
  1006. while ActualP^ in SpaceChars do inc(ActualP);
  1007. if ExpectedP^<>ActualP^ then
  1008. begin
  1009. DiffFound;
  1010. exit;
  1011. end;
  1012. inc(ExpectedP);
  1013. inc(ActualP);
  1014. end;
  1015. until false;
  1016. end;
  1017. { TTestEnginePasResolver }
  1018. destructor TTestEnginePasResolver.Destroy;
  1019. begin
  1020. FreeAndNil(FStreamResolver);
  1021. FreeAndNil(FParser);
  1022. FreeAndNil(FScanner);
  1023. FreeAndNil(FStreamResolver);
  1024. if Module<>nil then
  1025. begin
  1026. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1027. FModule:=nil;
  1028. end;
  1029. inherited Destroy;
  1030. end;
  1031. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1032. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1033. begin
  1034. Result:=nil;
  1035. if InFilename<>'' then
  1036. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1037. if Assigned(OnFindUnit) then
  1038. Result:=OnFindUnit(AName);
  1039. if NameExpr=nil then ;
  1040. end;
  1041. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1042. begin
  1043. // do not parse recursively
  1044. // parse via the queue
  1045. if Section=nil then ;
  1046. end;
  1047. { TCustomTestModule }
  1048. function TCustomTestModule.GetMsgCount: integer;
  1049. begin
  1050. Result:=FHintMsgs.Count;
  1051. end;
  1052. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1053. begin
  1054. Result:=TTestHintMessage(FHintMsgs[Index]);
  1055. end;
  1056. function TCustomTestModule.GetResolverCount: integer;
  1057. begin
  1058. Result:=FModules.Count;
  1059. end;
  1060. function TCustomTestModule.GetResolvers(Index: integer
  1061. ): TTestEnginePasResolver;
  1062. begin
  1063. Result:=TTestEnginePasResolver(FModules[Index]);
  1064. end;
  1065. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1066. ): TPasModule;
  1067. var
  1068. DefNamespace: String;
  1069. begin
  1070. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1071. if (Pos('.',aUnitName)<1) then
  1072. begin
  1073. DefNamespace:=GetDefaultNamespace;
  1074. if DefNamespace<>'' then
  1075. begin
  1076. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1077. if Result<>nil then exit;
  1078. end;
  1079. end;
  1080. Result:=LoadUnit(aUnitName);
  1081. if Result<>nil then exit;
  1082. {$IFDEF VerbosePas2JS}
  1083. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1084. {$ENDIF}
  1085. Fail('can''t find unit "'+aUnitName+'"');
  1086. end;
  1087. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1088. var
  1089. aParser: TPasParser;
  1090. Item: TTestHintMessage;
  1091. begin
  1092. aParser:=Sender as TPasParser;
  1093. Item:=TTestHintMessage.Create;
  1094. Item.Id:=aParser.LastMsgNumber;
  1095. Item.MsgType:=aParser.LastMsgType;
  1096. Item.MsgNumber:=aParser.LastMsgNumber;
  1097. Item.Msg:=Msg;
  1098. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1099. {$IFDEF VerbosePas2JS}
  1100. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1101. {$ENDIF}
  1102. FHintMsgs.Add(Item);
  1103. end;
  1104. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1105. );
  1106. var
  1107. aResolver: TTestEnginePasResolver;
  1108. Item: TTestHintMessage;
  1109. begin
  1110. aResolver:=Sender as TTestEnginePasResolver;
  1111. Item:=TTestHintMessage.Create;
  1112. Item.Id:=aResolver.LastMsgId;
  1113. Item.MsgType:=aResolver.LastMsgType;
  1114. Item.MsgNumber:=aResolver.LastMsgNumber;
  1115. Item.Msg:=Msg;
  1116. Item.SourcePos:=aResolver.LastSourcePos;
  1117. {$IFDEF VerbosePas2JS}
  1118. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1119. {$ENDIF}
  1120. FHintMsgs.Add(Item);
  1121. end;
  1122. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1123. var
  1124. Item: TTestHintMessage;
  1125. aScanner: TPas2jsPasScanner;
  1126. begin
  1127. aScanner:=Sender as TPas2jsPasScanner;
  1128. Item:=TTestHintMessage.Create;
  1129. Item.Id:=aScanner.LastMsgNumber;
  1130. Item.MsgType:=aScanner.LastMsgType;
  1131. Item.MsgNumber:=aScanner.LastMsgNumber;
  1132. Item.Msg:=Msg;
  1133. Item.SourcePos:=aScanner.CurSourcePos;
  1134. {$IFDEF VerbosePas2JS}
  1135. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1136. {$ENDIF}
  1137. FHintMsgs.Add(Item);
  1138. end;
  1139. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1140. var
  1141. i: Integer;
  1142. CurEngine: TTestEnginePasResolver;
  1143. CurUnitName: String;
  1144. begin
  1145. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1146. Result:=nil;
  1147. if (Module.ClassType=TPasModule)
  1148. and (CompareText(Module.Name,aUnitName)=0) then
  1149. exit(Module);
  1150. for i:=0 to ResolverCount-1 do
  1151. begin
  1152. CurEngine:=Resolvers[i];
  1153. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1154. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1155. if CompareText(aUnitName,CurUnitName)=0 then
  1156. begin
  1157. Result:=CurEngine.Module;
  1158. if Result<>nil then exit;
  1159. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1160. FileResolver.FindSourceFile(aUnitName);
  1161. CurEngine.StreamResolver:=TStreamResolver.Create;
  1162. CurEngine.StreamResolver.OwnsStreams:=True;
  1163. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1164. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1165. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1166. InitScanner(CurEngine.Scanner);
  1167. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1168. CurEngine.Parser.Options:=po_tcmodules;
  1169. if CompareText(CurUnitName,'System')=0 then
  1170. CurEngine.Parser.ImplicitUses.Clear;
  1171. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1172. try
  1173. CurEngine.Parser.NextToken;
  1174. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1175. except
  1176. on E: Exception do
  1177. HandleException(E);
  1178. end;
  1179. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1180. Result:=CurEngine.Module;
  1181. exit;
  1182. end;
  1183. end;
  1184. end;
  1185. procedure TCustomTestModule.SetUp;
  1186. begin
  1187. {$IFDEF EnablePasTreeGlobalRefCount}
  1188. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1189. {$ENDIF}
  1190. if FModules<>nil then
  1191. begin
  1192. writeln('TCustomTestModule.SetUp FModules<>nil');
  1193. Halt;
  1194. end;
  1195. inherited SetUp;
  1196. FSkipTests:=false;
  1197. FSource:=TStringList.Create;
  1198. FModules:=TObjectList.Create(true);
  1199. FFilename:='test1.pp';
  1200. FFileResolver:=TStreamResolver.Create;
  1201. FFileResolver.OwnsStreams:=True;
  1202. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1203. InitScanner(FScanner);
  1204. FEngine:=AddModule(Filename);
  1205. FEngine.Scanner:=FScanner;
  1206. FScanner.Resolver:=FEngine;
  1207. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1208. FParser.OnLog:=@OnParserLog;
  1209. FEngine.Parser:=FParser;
  1210. Parser.Options:=po_tcmodules;
  1211. FModule:=Nil;
  1212. FConverter:=CreateConverter;
  1213. FExpectedErrorClass:=nil;
  1214. end;
  1215. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1216. begin
  1217. Result:=TPasToJSConverter.Create;
  1218. Result.Options:=co_tcmodules;
  1219. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1220. end;
  1221. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1222. begin
  1223. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1224. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1225. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1226. aScanner.AllowedBoolSwitches:=msAllPas2jsBoolSwitches;
  1227. aScanner.ReadOnlyBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly;
  1228. aScanner.CurrentBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1229. aScanner.OnLog:=@OnScannerLog;
  1230. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1231. end;
  1232. procedure TCustomTestModule.TearDown;
  1233. {$IFDEF CheckPasTreeRefCount}
  1234. var
  1235. El: TPasElement;
  1236. {$ENDIF}
  1237. var
  1238. i: Integer;
  1239. CurModule: TPasModule;
  1240. begin
  1241. FHintMsgs.Clear;
  1242. FHintMsgsGood.Clear;
  1243. FSkipTests:=false;
  1244. FJSRegModuleCall:=nil;
  1245. FJSModuleCallArgs:=nil;
  1246. FJSImplentationUses:=nil;
  1247. FJSInterfaceUses:=nil;
  1248. FJSModuleSrc:=nil;
  1249. FJSInitBody:=nil;
  1250. FreeAndNil(FJSSource);
  1251. FreeAndNil(FJSModule);
  1252. FreeAndNil(FConverter);
  1253. Engine.Clear;
  1254. FreeAndNil(FSource);
  1255. FreeAndNil(FFileResolver);
  1256. if FModules<>nil then
  1257. begin
  1258. for i:=0 to FModules.Count-1 do
  1259. begin
  1260. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1261. if CurModule=nil then continue;
  1262. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1263. CurModule.ReleaseUsedUnits;
  1264. end;
  1265. if FModule<>nil then
  1266. FModule.ReleaseUsedUnits;
  1267. for i:=0 to FModules.Count-1 do
  1268. begin
  1269. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1270. if CurModule=nil then continue;
  1271. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1272. end;
  1273. FreeAndNil(FModules);
  1274. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1275. FEngine:=nil;
  1276. end;
  1277. inherited TearDown;
  1278. {$IFDEF EnablePasTreeGlobalRefCount}
  1279. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1280. begin
  1281. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1282. {$IFDEF CheckPasTreeRefCount}
  1283. El:=TPasElement.FirstRefEl;
  1284. while El<>nil do
  1285. begin
  1286. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1287. for i:=0 to El.RefIds.Count-1 do
  1288. writeln(' ',El.RefIds[i]);
  1289. El:=El.NextRefEl;
  1290. end;
  1291. {$ENDIF}
  1292. Halt;
  1293. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1294. end;
  1295. {$ENDIF}
  1296. end;
  1297. procedure TCustomTestModule.Add(Line: string);
  1298. begin
  1299. Source.Add(Line);
  1300. end;
  1301. procedure TCustomTestModule.Add(const Lines: array of string);
  1302. var
  1303. i: Integer;
  1304. begin
  1305. for i:=low(Lines) to high(Lines) do
  1306. Add(Lines[i]);
  1307. end;
  1308. procedure TCustomTestModule.StartParsing;
  1309. var
  1310. Src: String;
  1311. begin
  1312. Src:=Source.Text;
  1313. FEngine.Source:=Src;
  1314. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1315. Scanner.OpenFile(FileName);
  1316. Writeln('// Test : ',Self.TestName);
  1317. Writeln(Src);
  1318. end;
  1319. procedure TCustomTestModule.ParseModuleQueue;
  1320. var
  1321. i: Integer;
  1322. CurResolver: TTestEnginePasResolver;
  1323. Found: Boolean;
  1324. Section: TPasSection;
  1325. begin
  1326. // parse til exception or all modules finished
  1327. while not SkipTests do
  1328. begin
  1329. Found:=false;
  1330. for i:=0 to ResolverCount-1 do
  1331. begin
  1332. CurResolver:=Resolvers[i];
  1333. if CurResolver.CurrentParser=nil then continue;
  1334. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1335. continue;
  1336. CurResolver.Parser.ParseContinue;
  1337. Found:=true;
  1338. break;
  1339. end;
  1340. if not Found then break;
  1341. end;
  1342. for i:=0 to ResolverCount-1 do
  1343. begin
  1344. CurResolver:=Resolvers[i];
  1345. if CurResolver.Parser=nil then
  1346. begin
  1347. if CurResolver.CurrentParser<>nil then
  1348. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1349. continue;
  1350. end;
  1351. if CurResolver.Parser.CurModule<>nil then
  1352. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1353. end;
  1354. end;
  1355. procedure TCustomTestModule.ParseModule;
  1356. begin
  1357. if SkipTests then exit;
  1358. FFirstPasStatement:=nil;
  1359. try
  1360. StartParsing;
  1361. Parser.ParseMain(FModule);
  1362. ParseModuleQueue;
  1363. except
  1364. on E: Exception do
  1365. HandleException(E);
  1366. end;
  1367. if SkipTests then exit;
  1368. AssertNotNull('Module resulted in Module',Module);
  1369. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1370. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1371. end;
  1372. procedure TCustomTestModule.ParseProgram;
  1373. begin
  1374. if SkipTests then exit;
  1375. ParseModule;
  1376. if SkipTests then exit;
  1377. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1378. FPasProgram:=TPasProgram(Module);
  1379. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1380. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1381. if (PasProgram.InitializationSection.Elements.Count>0) then
  1382. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1383. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1384. end;
  1385. procedure TCustomTestModule.ParseUnit;
  1386. begin
  1387. if SkipTests then exit;
  1388. ParseModule;
  1389. if SkipTests then exit;
  1390. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1391. AssertNotNull('Has interface section',Module.InterfaceSection);
  1392. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1393. if (Module.InitializationSection<>nil)
  1394. and (Module.InitializationSection.Elements.Count>0)
  1395. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1396. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1397. end;
  1398. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1399. ): TTestEnginePasResolver;
  1400. var
  1401. i: Integer;
  1402. begin
  1403. for i:=0 to ResolverCount-1 do
  1404. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1405. exit(Resolvers[i]);
  1406. Result:=nil;
  1407. end;
  1408. function TCustomTestModule.AddModule(aFilename: string
  1409. ): TTestEnginePasResolver;
  1410. begin
  1411. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1412. if FindModuleWithFilename(aFilename)<>nil then
  1413. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1414. Result:=TTestEnginePasResolver.Create;
  1415. Result.Filename:=aFilename;
  1416. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1417. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1418. Result.OnLog:=@OnPasResolverLog;
  1419. FModules.Add(Result);
  1420. end;
  1421. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1422. ): TTestEnginePasResolver;
  1423. begin
  1424. Result:=AddModule(aFilename);
  1425. Result.Source:=Src;
  1426. end;
  1427. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1428. ImplementationSrc: string): TTestEnginePasResolver;
  1429. var
  1430. Src: String;
  1431. begin
  1432. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1433. Src+=LineEnding;
  1434. Src+='interface'+LineEnding;
  1435. Src+=LineEnding;
  1436. Src+=InterfaceSrc;
  1437. Src+='implementation'+LineEnding;
  1438. Src+=LineEnding;
  1439. Src+=ImplementationSrc;
  1440. Src+='end.'+LineEnding;
  1441. Result:=AddModuleWithSrc(aFilename,Src);
  1442. end;
  1443. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1444. var
  1445. Intf, Impl: TStringList;
  1446. begin
  1447. Intf:=TStringList.Create;
  1448. // interface
  1449. if supTVarRec in Parts then
  1450. Intf.Add('{$modeswitch externalclass}');
  1451. Intf.Add('type');
  1452. Intf.Add(' integer=longint;');
  1453. Intf.Add(' sizeint=nativeint;');
  1454. //'const',
  1455. //' LineEnding = #10;',
  1456. //' DirectorySeparator = ''/'';',
  1457. //' DriveSeparator = '''';',
  1458. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1459. //' AllowDriveSeparators : set of char = [];',
  1460. if supTObject in Parts then
  1461. Intf.AddStrings([
  1462. 'type',
  1463. ' TClass = class of TObject;',
  1464. ' TObject = class',
  1465. ' constructor Create;',
  1466. ' destructor Destroy; virtual;',
  1467. ' class function ClassType: TClass; assembler;',
  1468. ' class function ClassName: String; assembler;',
  1469. ' class function ClassNameIs(const Name: string): boolean;',
  1470. ' class function ClassParent: TClass; assembler;',
  1471. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1472. ' class function UnitName: String; assembler;',
  1473. ' procedure AfterConstruction; virtual;',
  1474. ' procedure BeforeDestruction;virtual;',
  1475. ' function Equals(Obj: TObject): boolean; virtual;',
  1476. ' function ToString: String; virtual;',
  1477. ' end;']);
  1478. if supTVarRec in Parts then
  1479. Intf.AddStrings([
  1480. 'const',
  1481. ' vtInteger = 0;',
  1482. ' vtBoolean = 1;',
  1483. ' vtJSValue = 19;',
  1484. 'type',
  1485. ' PVarRec = ^TVarRec;',
  1486. ' TVarRec = record',
  1487. ' VType : byte;',
  1488. ' VJSValue: JSValue;',
  1489. ' vInteger: longint external name ''VJSValue'';',
  1490. ' vBoolean: boolean external name ''VJSValue'';',
  1491. ' end;',
  1492. ' TVarRecArray = array of TVarRec;',
  1493. 'function VarRecs: TVarRecArray; varargs;',
  1494. '']);
  1495. Intf.Add('var');
  1496. Intf.Add(' ExitCode: Longint = 0;');
  1497. // implementation
  1498. Impl:=TStringList.Create;
  1499. if supTObject in Parts then
  1500. Impl.AddStrings([
  1501. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1502. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1503. 'asm',
  1504. 'end;',
  1505. 'constructor TObject.Create; begin end;',
  1506. 'destructor TObject.Destroy; begin end;',
  1507. 'class function TObject.ClassType: TClass; assembler;',
  1508. 'asm',
  1509. 'end;',
  1510. 'class function TObject.ClassName: String; assembler;',
  1511. 'asm',
  1512. 'end;',
  1513. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1514. 'begin',
  1515. ' Result:=SameText(Name,ClassName);',
  1516. 'end;',
  1517. 'class function TObject.ClassParent: TClass; assembler;',
  1518. 'asm',
  1519. 'end;',
  1520. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1521. 'asm',
  1522. 'end;',
  1523. 'class function TObject.UnitName: String; assembler;',
  1524. 'asm',
  1525. 'end;',
  1526. 'procedure TObject.AfterConstruction; begin end;',
  1527. 'procedure TObject.BeforeDestruction; begin end;',
  1528. 'function TObject.Equals(Obj: TObject): boolean;',
  1529. 'begin',
  1530. ' Result:=Obj=Self;',
  1531. 'end;',
  1532. 'function TObject.ToString: String;',
  1533. 'begin',
  1534. ' Result:=ClassName;',
  1535. 'end;'
  1536. ]);
  1537. if supTVarRec in Parts then
  1538. Impl.AddStrings([
  1539. 'function VarRecs: TVarRecArray; varargs;',
  1540. 'var',
  1541. ' v: PVarRec;',
  1542. 'begin',
  1543. ' v^.VType:=1;',
  1544. ' v^.VJSValue:=2;',
  1545. 'end;',
  1546. '']);
  1547. try
  1548. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1549. finally
  1550. Intf.Free;
  1551. Impl.Free;
  1552. end;
  1553. end;
  1554. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1555. SystemUnitParts: TSystemUnitParts);
  1556. begin
  1557. if NeedSystemUnit then
  1558. AddSystemUnit(SystemUnitParts)
  1559. else
  1560. Parser.ImplicitUses.Clear;
  1561. Add('program '+ExtractFileUnitName(Filename)+';');
  1562. Add('');
  1563. end;
  1564. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1565. SystemUnitParts: TSystemUnitParts);
  1566. begin
  1567. if NeedSystemUnit then
  1568. AddSystemUnit(SystemUnitParts)
  1569. else
  1570. Parser.ImplicitUses.Clear;
  1571. Add('unit Test1;');
  1572. Add('');
  1573. end;
  1574. procedure TCustomTestModule.ConvertModule;
  1575. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1576. out UsesLit: TJSArrayLiteral);
  1577. var
  1578. i: Integer;
  1579. Item: TJSElement;
  1580. Lit: TJSLiteral;
  1581. begin
  1582. UsesLit:=nil;
  1583. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1584. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1585. exit; // null is ok
  1586. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1587. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1588. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1589. begin
  1590. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1591. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1592. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1593. Lit:=TJSLiteral(Item);
  1594. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1595. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1596. end;
  1597. end;
  1598. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1599. out Src: TJSSourceElements);
  1600. var
  1601. FunDecl: TJSFunctionDeclarationStatement;
  1602. FunDef: TJSFuncDef;
  1603. FunBody: TJSFunctionBody;
  1604. begin
  1605. Src:=nil;
  1606. AssertNotNull(ParamName,Arg.Expr);
  1607. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1608. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1609. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1610. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1611. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1612. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1613. AssertNotNull(ParamName+' body',FunDef.Body);
  1614. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1615. FunBody:=FunDef.Body as TJSFunctionBody;
  1616. AssertNotNull(ParamName+' body.A',FunBody.A);
  1617. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1618. Src:=FunBody.A as TJSSourceElements;
  1619. end;
  1620. var
  1621. ModuleNameExpr: TJSLiteral;
  1622. InitFunction: TJSFunctionDeclarationStatement;
  1623. InitAssign: TJSSimpleAssignStatement;
  1624. InitName: String;
  1625. LastNode: TJSElement;
  1626. Arg: TJSArrayLiteralElement;
  1627. begin
  1628. if SkipTests then exit;
  1629. try
  1630. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1631. except
  1632. on E: Exception do
  1633. HandleException(E);
  1634. end;
  1635. if SkipTests then exit;
  1636. if ExpectedErrorClass<>nil then
  1637. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1638. FJSSource:=TStringList.Create;
  1639. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1640. {$IFDEF VerbosePas2JS}
  1641. writeln('TTestModule.ConvertModule JS:');
  1642. write(FJSSource.Text);
  1643. {$ENDIF}
  1644. // rtl.module(...
  1645. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1646. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1647. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1648. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1649. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1650. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1651. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1652. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1653. // parameter 'unitname'
  1654. if JSModuleCallArgs.Elements.Count<1 then
  1655. Fail('rtl.module first param unit missing');
  1656. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1657. AssertNotNull('module name param',Arg.Expr);
  1658. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1659. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1660. if Module is TPasProgram then
  1661. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1662. else
  1663. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1664. // main uses section
  1665. if JSModuleCallArgs.Elements.Count<2 then
  1666. Fail('rtl.module second param main uses missing');
  1667. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1668. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1669. // program/library/interface function()
  1670. if JSModuleCallArgs.Elements.Count<3 then
  1671. Fail('rtl.module third param intf-function missing');
  1672. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1673. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1674. // search for $mod.$init or $mod.$main - the last statement
  1675. if Module is TPasProgram then
  1676. begin
  1677. InitName:='$main';
  1678. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1679. end
  1680. else
  1681. InitName:='$init';
  1682. FJSInitBody:=nil;
  1683. if JSModuleSrc.Statements.Count>0 then
  1684. begin
  1685. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1686. if LastNode is TJSSimpleAssignStatement then
  1687. begin
  1688. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1689. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1690. begin
  1691. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1692. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1693. end
  1694. else if Module is TPasProgram then
  1695. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1696. end;
  1697. end;
  1698. // optional: implementation uses section
  1699. if JSModuleCallArgs.Elements.Count<4 then
  1700. exit;
  1701. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1702. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1703. // optional: implementation function()
  1704. if JSModuleCallArgs.Elements.Count<5 then
  1705. exit;
  1706. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1707. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1708. end;
  1709. procedure TCustomTestModule.ConvertProgram;
  1710. begin
  1711. Add('end.');
  1712. ParseProgram;
  1713. ConvertModule;
  1714. end;
  1715. procedure TCustomTestModule.ConvertUnit;
  1716. begin
  1717. Add('end.');
  1718. ParseUnit;
  1719. ConvertModule;
  1720. end;
  1721. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1722. begin
  1723. Result:=tcmodules.JSToStr(El);
  1724. end;
  1725. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1726. DottedName: string);
  1727. begin
  1728. if DottedName='' then
  1729. begin
  1730. AssertNull(Msg,El);
  1731. end
  1732. else
  1733. begin
  1734. AssertNotNull(Msg,El);
  1735. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1736. end;
  1737. end;
  1738. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1739. begin
  1740. if El=nil then
  1741. Result:=''
  1742. else if El is TJSPrimaryExpressionIdent then
  1743. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1744. else if El is TJSDotMemberExpression then
  1745. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1746. else
  1747. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1748. end;
  1749. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1750. InitStatements: string; ImplStatements: string);
  1751. var
  1752. ActualSrc, ExpectedSrc, InitName: String;
  1753. begin
  1754. ActualSrc:=JSToStr(JSModuleSrc);
  1755. ExpectedSrc:=
  1756. 'var $mod = this;'+LineEnding
  1757. +Statements;
  1758. if coUseStrict in Converter.Options then
  1759. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1760. if Module is TPasProgram then
  1761. InitName:='$main'
  1762. else
  1763. InitName:='$init';
  1764. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1765. ExpectedSrc:=ExpectedSrc+LineEnding
  1766. +'$mod.'+InitName+' = function () {'+LineEnding
  1767. +InitStatements
  1768. +'};'+LineEnding;
  1769. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1770. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1771. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1772. if (JSImplementationSrc<>nil) then
  1773. begin
  1774. ActualSrc:=JSToStr(JSImplementationSrc);
  1775. ExpectedSrc:=
  1776. 'var $mod = this;'+LineEnding
  1777. +'var $impl = $mod.$impl;'+LineEnding
  1778. +ImplStatements;
  1779. end
  1780. else
  1781. begin
  1782. ActualSrc:='';
  1783. ExpectedSrc:=ImplStatements;
  1784. end;
  1785. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1786. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1787. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1788. end;
  1789. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1790. // search diff, ignore changes in spaces
  1791. var
  1792. s: string;
  1793. begin
  1794. if CheckSrcDiff(Expected,Actual,s) then exit;
  1795. Fail(Msg+': '+s);
  1796. end;
  1797. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1798. var
  1799. aResolver: TTestEnginePasResolver;
  1800. aConverter: TPasToJSConverter;
  1801. aJSModule: TJSSourceElements;
  1802. ActualSrc: String;
  1803. begin
  1804. aResolver:=GetResolver(Filename);
  1805. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1806. {$IFDEF VerbosePas2JS}
  1807. writeln('CheckUnit '+Filename+' converting ...');
  1808. {$ENDIF}
  1809. aConverter:=CreateConverter;
  1810. aJSModule:=nil;
  1811. try
  1812. try
  1813. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1814. except
  1815. on E: Exception do
  1816. HandleException(E);
  1817. end;
  1818. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1819. {$IFDEF VerbosePas2JS}
  1820. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1821. write(aResolver.Source);
  1822. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1823. write(ActualSrc);
  1824. {$ENDIF}
  1825. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1826. finally
  1827. aJSModule.Free;
  1828. aConverter.Free;
  1829. end;
  1830. end;
  1831. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1832. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1833. var
  1834. i: Integer;
  1835. Item: TTestHintMessage;
  1836. Expected,Actual: string;
  1837. begin
  1838. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1839. for i:=0 to MsgCount-1 do
  1840. begin
  1841. Item:=Msgs[i];
  1842. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1843. if (Marker<>nil) then
  1844. begin
  1845. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1846. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1847. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1848. end;
  1849. // found
  1850. FHintMsgsGood.Add(Item);
  1851. str(Item.MsgType,Actual);
  1852. str(MsgType,Expected);
  1853. AssertEquals('MsgType',Expected,Actual);
  1854. exit;
  1855. end;
  1856. // needed message missing -> show emitted messages
  1857. WriteSources('',0,0);
  1858. for i:=0 to MsgCount-1 do
  1859. begin
  1860. Item:=Msgs[i];
  1861. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1862. ' ('+IntToStr(Item.MsgNumber),')');
  1863. if Marker<>nil then
  1864. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1865. writeln(' {',Item.Msg,'}');
  1866. end;
  1867. str(MsgType,Expected);
  1868. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1869. if Marker<>nil then
  1870. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1871. Actual:=Actual+' '+Msg;
  1872. Fail(Actual);
  1873. end;
  1874. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1875. );
  1876. var
  1877. i: Integer;
  1878. s, Txt: String;
  1879. Msg: TTestHintMessage;
  1880. begin
  1881. for i:=0 to MsgCount-1 do
  1882. begin
  1883. Msg:=Msgs[i];
  1884. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1885. s:='';
  1886. str(Msg.MsgType,s);
  1887. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1888. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1889. if WithSourcePos then
  1890. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1891. Txt:=Txt+' {'+Msg.Msg+'}';
  1892. Fail(Txt);
  1893. end;
  1894. end;
  1895. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1896. MsgNumber: integer);
  1897. begin
  1898. ExpectedErrorClass:=EScannerError;
  1899. ExpectedErrorMsg:=Msg;
  1900. ExpectedErrorNumber:=MsgNumber;
  1901. end;
  1902. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1903. MsgNumber: integer);
  1904. begin
  1905. ExpectedErrorClass:=EParserError;
  1906. ExpectedErrorMsg:=Msg;
  1907. ExpectedErrorNumber:=MsgNumber;
  1908. end;
  1909. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1910. MsgNumber: integer);
  1911. begin
  1912. ExpectedErrorClass:=EPasResolve;
  1913. ExpectedErrorMsg:=Msg;
  1914. ExpectedErrorNumber:=MsgNumber;
  1915. end;
  1916. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1917. MsgNumber: integer);
  1918. begin
  1919. ExpectedErrorClass:=EPas2JS;
  1920. ExpectedErrorMsg:=Msg;
  1921. ExpectedErrorNumber:=MsgNumber;
  1922. end;
  1923. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1924. var
  1925. MsgNumber: Integer;
  1926. Msg: String;
  1927. begin
  1928. Result:=false;
  1929. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1930. Msg:=E.Message;
  1931. if E is EPas2JS then
  1932. MsgNumber:=EPas2JS(E).MsgNumber
  1933. else if E is EPasResolve then
  1934. MsgNumber:=EPasResolve(E).MsgNumber
  1935. else if E is EParserError then
  1936. MsgNumber:=Parser.LastMsgNumber
  1937. else if E is EScannerError then
  1938. begin
  1939. MsgNumber:=Scanner.LastMsgNumber;
  1940. Msg:=Scanner.LastMsg;
  1941. end
  1942. else
  1943. MsgNumber:=0;
  1944. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1945. if Result then
  1946. SkipTests:=true;
  1947. end;
  1948. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1949. begin
  1950. if IsErrorExpected(E) then exit;
  1951. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1952. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1953. +' '+Scanner.CurFilename
  1954. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1955. FailException(E);
  1956. end;
  1957. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1958. begin
  1959. if IsErrorExpected(E) then exit;
  1960. WriteSources(E.Filename,E.Row,E.Column);
  1961. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1962. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1963. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1964. );
  1965. FailException(E);
  1966. end;
  1967. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1968. var
  1969. P: TPasSourcePos;
  1970. begin
  1971. if IsErrorExpected(E) then exit;
  1972. P:=E.SourcePos;
  1973. WriteSources(P.FileName,P.Row,P.Column);
  1974. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  1975. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  1976. FailException(E);
  1977. end;
  1978. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  1979. var
  1980. Row, Col: integer;
  1981. begin
  1982. if IsErrorExpected(E) then exit;
  1983. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  1984. WriteSources(E.PasElement.SourceFilename,Row,Col);
  1985. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  1986. +' '+E.PasElement.SourceFilename
  1987. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  1988. FailException(E);
  1989. end;
  1990. procedure TCustomTestModule.HandleException(E: Exception);
  1991. begin
  1992. if E is EScannerError then
  1993. HandleScannerError(EScannerError(E))
  1994. else if E is EParserError then
  1995. HandleParserError(EParserError(E))
  1996. else if E is EPasResolve then
  1997. HandlePasResolveError(EPasResolve(E))
  1998. else if E is EPas2JS then
  1999. HandlePas2JSError(EPas2JS(E))
  2000. else
  2001. begin
  2002. if IsErrorExpected(E) then exit;
  2003. if not (E is EAssertionFailedError) then
  2004. begin
  2005. WriteSources('',0,0);
  2006. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2007. end;
  2008. FailException(E);
  2009. end;
  2010. end;
  2011. procedure TCustomTestModule.FailException(E: Exception);
  2012. var
  2013. MsgNumber: Integer;
  2014. begin
  2015. if ExpectedErrorClass<>nil then
  2016. begin
  2017. if FExpectedErrorClass=E.ClassType then
  2018. begin
  2019. if E is EPas2JS then
  2020. MsgNumber:=EPas2JS(E).MsgNumber
  2021. else if E is EPasResolve then
  2022. MsgNumber:=EPasResolve(E).MsgNumber
  2023. else if E is EParserError then
  2024. MsgNumber:=Parser.LastMsgNumber
  2025. else if E is EScannerError then
  2026. MsgNumber:=Scanner.LastMsgNumber
  2027. else
  2028. MsgNumber:=0;
  2029. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2030. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2031. ExpectedErrorNumber,MsgNumber);
  2032. end else begin
  2033. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2034. end;
  2035. end;
  2036. Fail(E.Message);
  2037. end;
  2038. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2039. aCol: integer);
  2040. var
  2041. IsSrc: Boolean;
  2042. i, j: Integer;
  2043. SrcLines: TStringList;
  2044. Line: string;
  2045. aModule: TTestEnginePasResolver;
  2046. begin
  2047. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2048. for i:=0 to ResolverCount-1 do
  2049. begin
  2050. aModule:=Resolvers[i];
  2051. SrcLines:=TStringList.Create;
  2052. try
  2053. SrcLines.Text:=aModule.Source;
  2054. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2055. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2056. for j:=1 to SrcLines.Count do
  2057. begin
  2058. Line:=SrcLines[j-1];
  2059. if IsSrc and (j=aRow) then
  2060. begin
  2061. write('*');
  2062. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2063. end;
  2064. writeln(Format('%:4d: ',[j]),Line);
  2065. end;
  2066. finally
  2067. SrcLines.Free;
  2068. end;
  2069. end;
  2070. end;
  2071. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2072. var
  2073. i: Integer;
  2074. begin
  2075. for i:=0 to ResolverCount-1 do
  2076. if Filename=Resolvers[i].Filename then exit(i);
  2077. Result:=-1;
  2078. end;
  2079. function TCustomTestModule.GetResolver(const Filename: string
  2080. ): TTestEnginePasResolver;
  2081. var
  2082. i: Integer;
  2083. begin
  2084. i:=IndexOfResolver(Filename);
  2085. if i<0 then exit(nil);
  2086. Result:=Resolvers[i];
  2087. end;
  2088. function TCustomTestModule.GetDefaultNamespace: string;
  2089. var
  2090. C: TClass;
  2091. begin
  2092. Result:='';
  2093. if FModule=nil then exit;
  2094. C:=FModule.ClassType;
  2095. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2096. Result:=Engine.DefaultNameSpace;
  2097. end;
  2098. constructor TCustomTestModule.Create;
  2099. begin
  2100. inherited Create;
  2101. FHintMsgs:=TObjectList.Create(true);
  2102. FHintMsgsGood:=TFPList.Create;
  2103. end;
  2104. destructor TCustomTestModule.Destroy;
  2105. begin
  2106. FreeAndNil(FHintMsgs);
  2107. FreeAndNil(FHintMsgsGood);
  2108. inherited Destroy;
  2109. end;
  2110. { TTestModule }
  2111. procedure TTestModule.TestReservedWords;
  2112. var
  2113. i: integer;
  2114. begin
  2115. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2116. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2117. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2118. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2119. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2120. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2121. end;
  2122. procedure TTestModule.TestEmptyProgram;
  2123. begin
  2124. StartProgram(false);
  2125. Add('begin');
  2126. ConvertProgram;
  2127. CheckSource('TestEmptyProgram','','');
  2128. end;
  2129. procedure TTestModule.TestEmptyProgramUseStrict;
  2130. begin
  2131. Converter.Options:=Converter.Options+[coUseStrict];
  2132. StartProgram(false);
  2133. Add('begin');
  2134. ConvertProgram;
  2135. CheckSource('TestEmptyProgramUseStrict','','');
  2136. end;
  2137. procedure TTestModule.TestEmptyUnit;
  2138. begin
  2139. StartUnit(false);
  2140. Add('interface');
  2141. Add('implementation');
  2142. ConvertUnit;
  2143. CheckSource('TestEmptyUnit',
  2144. LinesToStr([
  2145. ]),
  2146. '');
  2147. end;
  2148. procedure TTestModule.TestEmptyUnitUseStrict;
  2149. begin
  2150. Converter.Options:=Converter.Options+[coUseStrict];
  2151. StartUnit(false);
  2152. Add('interface');
  2153. Add('implementation');
  2154. ConvertUnit;
  2155. CheckSource('TestEmptyUnitUseStrict',
  2156. LinesToStr([
  2157. ''
  2158. ]),
  2159. '');
  2160. end;
  2161. procedure TTestModule.TestDottedUnitNames;
  2162. begin
  2163. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2164. LinesToStr([
  2165. 'var iV: longint;'
  2166. ]),
  2167. '');
  2168. FFilename:='ns1.test1.pp';
  2169. StartProgram(true);
  2170. Add('uses unIt2;');
  2171. Add('implementation');
  2172. Add('var');
  2173. Add(' i: longint;');
  2174. Add('begin');
  2175. Add(' i:=iv;');
  2176. Add(' i:=uNit2.iv;');
  2177. Add(' i:=Ns1.TEst1.i;');
  2178. ConvertProgram;
  2179. CheckSource('TestDottedUnitNames',
  2180. LinesToStr([
  2181. 'this.i = 0;',
  2182. '']),
  2183. LinesToStr([ // this.$init
  2184. '$mod.i = pas["NS1.Unit2"].iV;',
  2185. '$mod.i = pas["NS1.Unit2"].iV;',
  2186. '$mod.i = $mod.i;',
  2187. '']) );
  2188. end;
  2189. procedure TTestModule.TestDottedUnitNameImpl;
  2190. begin
  2191. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2192. LinesToStr([
  2193. 'type',
  2194. ' TObject = class end;',
  2195. ' TTestA = class',
  2196. ' end;'
  2197. ]),
  2198. LinesToStr(['uses TEST.UnitB;'])
  2199. );
  2200. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2201. LinesToStr([
  2202. 'uses TEST.UnitA;',
  2203. 'type TTestB = class(TTestA);'
  2204. ]),
  2205. ''
  2206. );
  2207. StartProgram(true);
  2208. Add('uses TEST.UnitA;');
  2209. Add('begin');
  2210. ConvertProgram;
  2211. CheckSource('TestDottedUnitNameImpl',
  2212. LinesToStr([
  2213. '']),
  2214. LinesToStr([ // this.$init
  2215. '']) );
  2216. CheckUnit('TEST.UnitA.pas',
  2217. LinesToStr([
  2218. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2219. ' var $mod = this;',
  2220. ' rtl.createClass($mod, "TObject", null, function () {',
  2221. ' this.$init = function () {',
  2222. ' };',
  2223. ' this.$final = function () {',
  2224. ' };',
  2225. ' });',
  2226. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2227. ' });',
  2228. '}, ["TEST.UnitB"]);'
  2229. ]));
  2230. CheckUnit('TEST.UnitB.pas',
  2231. LinesToStr([
  2232. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2233. ' var $mod = this;',
  2234. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2235. ' });',
  2236. '});'
  2237. ]));
  2238. end;
  2239. procedure TTestModule.TestDottedUnitExpr;
  2240. begin
  2241. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2242. LinesToStr([
  2243. 'procedure DoIt;'
  2244. ]),
  2245. 'procedure DoIt; begin end;');
  2246. FFilename:='Ns1.SubNs1.Test1.pp';
  2247. StartProgram(true);
  2248. Add('uses Ns2.sUbnS2.unIt2;');
  2249. Add('implementation');
  2250. Add('var');
  2251. Add(' i: longint;');
  2252. Add('begin');
  2253. Add(' ns2.subns2.unit2.doit;');
  2254. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2255. ConvertProgram;
  2256. CheckSource('TestDottedUnitExpr',
  2257. LinesToStr([
  2258. 'this.i = 0;',
  2259. '']),
  2260. LinesToStr([ // this.$init
  2261. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2262. '$mod.i = $mod.i;',
  2263. '']) );
  2264. end;
  2265. procedure TTestModule.Test_ModeFPCFail;
  2266. begin
  2267. StartProgram(false);
  2268. Add('{$mode FPC}');
  2269. Add('begin');
  2270. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2271. ConvertProgram;
  2272. end;
  2273. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2274. begin
  2275. StartProgram(false);
  2276. Add('{$modeswitch cblocks-}');
  2277. Add('begin');
  2278. SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
  2279. ConvertProgram;
  2280. end;
  2281. procedure TTestModule.TestUnit_UseSystem;
  2282. begin
  2283. StartUnit(true);
  2284. Add([
  2285. 'interface',
  2286. 'var i: integer;',
  2287. 'implementation']);
  2288. ConvertUnit;
  2289. CheckSource('TestUnit_UseSystem',
  2290. LinesToStr([
  2291. 'this.i = 0;',
  2292. '']),
  2293. LinesToStr([
  2294. '']) );
  2295. end;
  2296. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2297. begin
  2298. AddModuleWithIntfImplSrc('unit1.pp',
  2299. LinesToStr([
  2300. 'type number = longint;']),
  2301. LinesToStr([
  2302. 'uses test1;',
  2303. 'procedure DoIt;',
  2304. 'begin',
  2305. ' i:=3;',
  2306. 'end;']));
  2307. StartUnit(true);
  2308. Add([
  2309. 'interface',
  2310. 'uses unit1;',
  2311. 'var i: number;',
  2312. 'implementation']);
  2313. ConvertUnit;
  2314. CheckSource('TestUnit_Intf1Impl2Intf1',
  2315. LinesToStr([
  2316. 'this.i = 0;',
  2317. '']),
  2318. LinesToStr([
  2319. '']) );
  2320. end;
  2321. procedure TTestModule.TestIncludeVersion;
  2322. begin
  2323. StartProgram(false);
  2324. Add([
  2325. 'var',
  2326. ' s: string;',
  2327. ' i: word;',
  2328. 'begin',
  2329. ' s:={$I %line%};',
  2330. ' i:={$I %linenum%};',
  2331. ' s:={$I %currentroutine%};',
  2332. ' s:={$I %pas2jsversion%};',
  2333. ' s:={$I %pas2jstarget%};',
  2334. ' s:={$I %pas2jstargetos%};',
  2335. ' s:={$I %pas2jstargetcpu%};',
  2336. ' s:={$I %file%};',
  2337. '']);
  2338. ConvertProgram;
  2339. CheckSource('TestIncludeVersion',
  2340. LinesToStr([
  2341. 'this.s="";',
  2342. 'this.i = 0;']),
  2343. LinesToStr([
  2344. '$mod.s = "7";',
  2345. '$mod.i = 8;',
  2346. '$mod.s = "<anonymous>";',
  2347. '$mod.s = "Comp.Ver.tcmodules";',
  2348. '$mod.s = "Browser";',
  2349. '$mod.s = "Browser";',
  2350. '$mod.s = "ECMAScript5";',
  2351. '$mod.s = "test1.pp";',
  2352. '']));
  2353. end;
  2354. procedure TTestModule.TestVarInt;
  2355. begin
  2356. StartProgram(false);
  2357. Add('var MyI: longint;');
  2358. Add('begin');
  2359. ConvertProgram;
  2360. CheckSource('TestVarInt','this.MyI=0;','');
  2361. end;
  2362. procedure TTestModule.TestVarBaseTypes;
  2363. begin
  2364. StartProgram(false);
  2365. Add('var');
  2366. Add(' i: longint;');
  2367. Add(' s: string;');
  2368. Add(' c: char;');
  2369. Add(' b: boolean;');
  2370. Add(' d: double;');
  2371. Add(' i2: longint = 3;');
  2372. Add(' s2: string = ''foo'';');
  2373. Add(' c2: char = ''4'';');
  2374. Add(' b2: boolean = true;');
  2375. Add(' d2: double = 5.6;');
  2376. Add(' i3: longint = $707;');
  2377. Add(' i4: nativeint = 4503599627370495;');
  2378. Add(' i5: nativeint = -4503599627370495-1;');
  2379. Add(' i6: nativeint = $fffffffffffff;');
  2380. Add(' i7: nativeint = -$fffffffffffff-1;');
  2381. Add(' i8: byte = 00;');
  2382. Add(' u8: nativeuint = $fffffffffffff;');
  2383. Add(' u9: nativeuint = $0000000000000;');
  2384. Add(' u10: nativeuint = $00ff00;');
  2385. Add('begin');
  2386. ConvertProgram;
  2387. CheckSource('TestVarBaseTypes',
  2388. LinesToStr([
  2389. 'this.i = 0;',
  2390. 'this.s = "";',
  2391. 'this.c = "";',
  2392. 'this.b = false;',
  2393. 'this.d = 0.0;',
  2394. 'this.i2 = 3;',
  2395. 'this.s2 = "foo";',
  2396. 'this.c2 = "4";',
  2397. 'this.b2 = true;',
  2398. 'this.d2 = 5.6;',
  2399. 'this.i3 = 0x707;',
  2400. 'this.i4 = 4503599627370495;',
  2401. 'this.i5 = -4503599627370495-1;',
  2402. 'this.i6 = 0xfffffffffffff;',
  2403. 'this.i7 =-0xfffffffffffff-1;',
  2404. 'this.i8 = 0;',
  2405. 'this.u8 = 0xfffffffffffff;',
  2406. 'this.u9 = 0x0;',
  2407. 'this.u10 = 0xff00;'
  2408. ]),
  2409. '');
  2410. end;
  2411. procedure TTestModule.TestBaseTypeSingleFail;
  2412. begin
  2413. StartProgram(false);
  2414. Add('var s: single;');
  2415. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2416. ConvertProgram;
  2417. end;
  2418. procedure TTestModule.TestBaseTypeExtendedFail;
  2419. begin
  2420. StartProgram(false);
  2421. Add('var e: extended;');
  2422. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2423. ConvertProgram;
  2424. end;
  2425. procedure TTestModule.TestConstBaseTypes;
  2426. begin
  2427. StartProgram(false);
  2428. Add('const');
  2429. Add(' i: longint = 3;');
  2430. Add(' s: string = ''foo'';');
  2431. Add(' c: char = ''4'';');
  2432. Add(' b: boolean = true;');
  2433. Add(' d: double = 5.6;');
  2434. Add(' e = low(word);');
  2435. Add(' f = high(word);');
  2436. Add('begin');
  2437. ConvertProgram;
  2438. CheckSource('TestVarBaseTypes',
  2439. LinesToStr([
  2440. 'this.i=3;',
  2441. 'this.s="foo";',
  2442. 'this.c="4";',
  2443. 'this.b=true;',
  2444. 'this.d=5.6;',
  2445. 'this.e = 0;',
  2446. 'this.f = 65535;'
  2447. ]),
  2448. '');
  2449. end;
  2450. procedure TTestModule.TestAliasTypeRef;
  2451. begin
  2452. StartProgram(false);
  2453. Add('type');
  2454. Add(' a=longint;');
  2455. Add(' b=a;');
  2456. Add('var');
  2457. Add(' c: A;');
  2458. Add(' d: B;');
  2459. Add('begin');
  2460. ConvertProgram;
  2461. CheckSource('TestAliasTypeRef',
  2462. LinesToStr([ // statements
  2463. 'this.c = 0;',
  2464. 'this.d = 0;'
  2465. ]),
  2466. LinesToStr([ // this.$main
  2467. ''
  2468. ]));
  2469. end;
  2470. procedure TTestModule.TestTypeCast_BaseTypes;
  2471. begin
  2472. StartProgram(false);
  2473. Add([
  2474. 'var',
  2475. ' i: longint;',
  2476. ' b: boolean;',
  2477. ' d: double;',
  2478. ' s: string;',
  2479. ' c: char;',
  2480. 'begin',
  2481. ' i:=longint(i);',
  2482. ' i:=longint(b);',
  2483. ' b:=boolean(b);',
  2484. ' b:=boolean(i);',
  2485. ' d:=double(d);',
  2486. ' d:=double(i);',
  2487. ' s:=string(s);',
  2488. ' s:=string(c);',
  2489. ' c:=char(c);',
  2490. ' c:=char(i);',
  2491. ' c:=char(65);',
  2492. ' c:=char(#10);',
  2493. ' c:=char(#$E000);',
  2494. '']);
  2495. ConvertProgram;
  2496. CheckSource('TestAliasTypeRef',
  2497. LinesToStr([ // statements
  2498. 'this.i = 0;',
  2499. 'this.b = false;',
  2500. 'this.d = 0.0;',
  2501. 'this.s = "";',
  2502. 'this.c = "";',
  2503. '']),
  2504. LinesToStr([ // this.$main
  2505. '$mod.i = $mod.i;',
  2506. '$mod.i = ($mod.b ? 1 : 0);',
  2507. '$mod.b = $mod.b;',
  2508. '$mod.b = $mod.i != 0;',
  2509. '$mod.d = $mod.d;',
  2510. '$mod.d = $mod.i;',
  2511. '$mod.s = $mod.s;',
  2512. '$mod.s = $mod.c;',
  2513. '$mod.c = $mod.c;',
  2514. '$mod.c = String.fromCharCode($mod.i);',
  2515. '$mod.c = "A";',
  2516. '$mod.c = "\n";',
  2517. '$mod.c = "";',
  2518. '']));
  2519. end;
  2520. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2521. begin
  2522. StartProgram(false);
  2523. Add('type');
  2524. Add(' integer = longint;');
  2525. Add(' TYesNo = boolean;');
  2526. Add(' TFloat = double;');
  2527. Add(' TCaption = string;');
  2528. Add(' TChar = char;');
  2529. Add('var');
  2530. Add(' i: integer;');
  2531. Add(' b: TYesNo;');
  2532. Add(' d: TFloat;');
  2533. Add(' s: TCaption;');
  2534. Add(' c: TChar;');
  2535. Add('begin');
  2536. Add(' i:=integer(i);');
  2537. Add(' i:=integer(b);');
  2538. Add(' b:=TYesNo(b);');
  2539. Add(' b:=TYesNo(i);');
  2540. Add(' d:=TFloat(d);');
  2541. Add(' d:=TFloat(i);');
  2542. Add(' s:=TCaption(s);');
  2543. Add(' s:=TCaption(c);');
  2544. Add(' c:=TChar(c);');
  2545. ConvertProgram;
  2546. CheckSource('TestAliasTypeRef',
  2547. LinesToStr([ // statements
  2548. 'this.i = 0;',
  2549. 'this.b = false;',
  2550. 'this.d = 0.0;',
  2551. 'this.s = "";',
  2552. 'this.c = "";',
  2553. '']),
  2554. LinesToStr([ // this.$main
  2555. '$mod.i = $mod.i;',
  2556. '$mod.i = ($mod.b ? 1 : 0);',
  2557. '$mod.b = $mod.b;',
  2558. '$mod.b = $mod.i != 0;',
  2559. '$mod.d = $mod.d;',
  2560. '$mod.d = $mod.i;',
  2561. '$mod.s = $mod.s;',
  2562. '$mod.s = $mod.c;',
  2563. '$mod.c = $mod.c;',
  2564. '']));
  2565. end;
  2566. procedure TTestModule.TestEmptyProc;
  2567. begin
  2568. StartProgram(false);
  2569. Add('procedure Test;');
  2570. Add('begin');
  2571. Add('end;');
  2572. Add('begin');
  2573. ConvertProgram;
  2574. CheckSource('TestEmptyProc',
  2575. LinesToStr([ // statements
  2576. 'this.Test = function () {',
  2577. '};'
  2578. ]),
  2579. LinesToStr([ // this.$main
  2580. ''
  2581. ]));
  2582. end;
  2583. procedure TTestModule.TestProcOneParam;
  2584. begin
  2585. StartProgram(false);
  2586. Add('procedure ProcA(i: longint);');
  2587. Add('begin');
  2588. Add('end;');
  2589. Add('begin');
  2590. Add(' PROCA(3);');
  2591. ConvertProgram;
  2592. CheckSource('TestProcOneParam',
  2593. LinesToStr([ // statements
  2594. 'this.ProcA = function (i) {',
  2595. '};'
  2596. ]),
  2597. LinesToStr([ // this.$main
  2598. '$mod.ProcA(3);'
  2599. ]));
  2600. end;
  2601. procedure TTestModule.TestFunctionWithoutParams;
  2602. begin
  2603. StartProgram(false);
  2604. Add('function FuncA: longint;');
  2605. Add('begin');
  2606. Add('end;');
  2607. Add('var i: longint;');
  2608. Add('begin');
  2609. Add(' I:=FUNCA();');
  2610. Add(' I:=FUNCA;');
  2611. Add(' FUNCA();');
  2612. Add(' FUNCA;');
  2613. ConvertProgram;
  2614. CheckSource('TestProcWithoutParams',
  2615. LinesToStr([ // statements
  2616. 'this.FuncA = function () {',
  2617. ' var Result = 0;',
  2618. ' return Result;',
  2619. '};',
  2620. 'this.i=0;'
  2621. ]),
  2622. LinesToStr([ // this.$main
  2623. '$mod.i=$mod.FuncA();',
  2624. '$mod.i=$mod.FuncA();',
  2625. '$mod.FuncA();',
  2626. '$mod.FuncA();'
  2627. ]));
  2628. end;
  2629. procedure TTestModule.TestProcedureWithoutParams;
  2630. begin
  2631. StartProgram(false);
  2632. Add('procedure ProcA;');
  2633. Add('begin');
  2634. Add('end;');
  2635. Add('begin');
  2636. Add(' PROCA();');
  2637. Add(' PROCA;');
  2638. ConvertProgram;
  2639. CheckSource('TestProcWithoutParams',
  2640. LinesToStr([ // statements
  2641. 'this.ProcA = function () {',
  2642. '};'
  2643. ]),
  2644. LinesToStr([ // this.$main
  2645. '$mod.ProcA();',
  2646. '$mod.ProcA();'
  2647. ]));
  2648. end;
  2649. procedure TTestModule.TestIncDec;
  2650. begin
  2651. StartProgram(false);
  2652. Add([
  2653. 'procedure DoIt(var i: longint);',
  2654. 'begin',
  2655. ' inc(i);',
  2656. ' inc(i,2);',
  2657. 'end;',
  2658. 'var',
  2659. ' Bar: longint;',
  2660. 'begin',
  2661. ' inc(bar);',
  2662. ' inc(bar,2);',
  2663. ' dec(bar);',
  2664. ' dec(bar,3);',
  2665. '']);
  2666. ConvertProgram;
  2667. CheckSource('TestIncDec',
  2668. LinesToStr([ // statements
  2669. 'this.DoIt = function (i) {',
  2670. ' i.set(i.get()+1);',
  2671. ' i.set(i.get()+2);',
  2672. '};',
  2673. 'this.Bar = 0;'
  2674. ]),
  2675. LinesToStr([ // this.$main
  2676. '$mod.Bar+=1;',
  2677. '$mod.Bar+=2;',
  2678. '$mod.Bar-=1;',
  2679. '$mod.Bar-=3;'
  2680. ]));
  2681. end;
  2682. procedure TTestModule.TestLoHiFpcMode;
  2683. begin
  2684. StartProgram(false);
  2685. Add([
  2686. '{$mode objfpc}',
  2687. 'const',
  2688. ' LoByte1 = Lo(Word($1234));',
  2689. ' HiByte1 = Hi(Word($1234));',
  2690. ' LoByte2 = Lo(SmallInt($1234));',
  2691. ' HiByte2 = Hi(SmallInt($1234));',
  2692. ' LoWord1 = Lo($1234CDEF);',
  2693. ' HiWord1 = Hi($1234CDEF);',
  2694. ' LoWord2 = Lo(-$1234CDEF);',
  2695. ' HiWord2 = Hi(-$1234CDEF);',
  2696. ' lo4:byte=lo(byte($34));',
  2697. ' hi4:byte=hi(byte($34));',
  2698. ' lo5:byte=lo(shortint(-$34));',
  2699. ' hi5:byte=hi(shortint(-$34));',
  2700. ' lo6:longword=lo($123456789ABCD);',
  2701. ' hi6:longword=hi($123456789ABCD);',
  2702. ' lo7:longword=lo(-$123456789ABCD);',
  2703. ' hi7:longword=hi(-$123456789ABCD);',
  2704. 'var',
  2705. ' b: Byte;',
  2706. ' ss: shortint;',
  2707. ' w: Word;',
  2708. ' si: SmallInt;',
  2709. ' lw: LongWord;',
  2710. ' li: LongInt;',
  2711. ' b2: Byte;',
  2712. ' ni: nativeint;',
  2713. 'begin',
  2714. ' w := $1234;',
  2715. ' ss := -$12;',
  2716. ' b := lo(ss);',
  2717. ' b := HI(ss);',
  2718. ' b := lo(w);',
  2719. ' b := HI(w);',
  2720. ' b2 := lo(b);',
  2721. ' b2 := hi(b);',
  2722. ' lw := $1234CDEF;',
  2723. ' w := lo(lw);',
  2724. ' w := hi(lw);',
  2725. ' ni := $123456789ABCD;',
  2726. ' lw := lo(ni);',
  2727. ' lw := hi(ni);',
  2728. '']);
  2729. ConvertProgram;
  2730. CheckSource('TestLoHiFpcMode',
  2731. LinesToStr([ // statements
  2732. 'this.LoByte1 = 0x1234 & 0xFF;',
  2733. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2734. 'this.LoByte2 = 0x1234 & 0xFF;',
  2735. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2736. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2737. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2738. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2739. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2740. 'this.lo4 = 0x34 & 0xF;',
  2741. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2742. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2743. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2744. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2745. 'this.hi6 = 74565 >>> 0;',
  2746. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2747. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2748. 'this.b = 0;',
  2749. 'this.ss = 0;',
  2750. 'this.w = 0;',
  2751. 'this.si = 0;',
  2752. 'this.lw = 0;',
  2753. 'this.li = 0;',
  2754. 'this.b2 = 0;',
  2755. 'this.ni = 0;',
  2756. '']),
  2757. LinesToStr([ // this.$main
  2758. '$mod.w = 0x1234;',
  2759. '$mod.ss = -0x12;',
  2760. '$mod.b = $mod.ss & 0xFF;',
  2761. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2762. '$mod.b = $mod.w & 0xFF;',
  2763. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2764. '$mod.b2 = $mod.b & 0xF;',
  2765. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2766. '$mod.lw = 0x1234CDEF;',
  2767. '$mod.w = $mod.lw & 0xFFFF;',
  2768. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2769. '$mod.ni = 0x123456789ABCD;',
  2770. '$mod.lw = $mod.ni >>> 0;',
  2771. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2772. '']));
  2773. end;
  2774. procedure TTestModule.TestLoHiDelphiMode;
  2775. begin
  2776. StartProgram(false);
  2777. Add([
  2778. '{$mode delphi}',
  2779. 'const',
  2780. ' LoByte1 = Lo(Word($1234));',
  2781. ' HiByte1 = Hi(Word($1234));',
  2782. ' LoByte2 = Lo(SmallInt($1234));',
  2783. ' HiByte2 = Hi(SmallInt($1234));',
  2784. ' LoByte3 = Lo($1234CDEF);',
  2785. ' HiByte3 = Hi($1234CDEF);',
  2786. ' LoByte4 = Lo(-$1234CDEF);',
  2787. ' HiByte4 = Hi(-$1234CDEF);',
  2788. 'var',
  2789. ' b: Byte;',
  2790. ' w: Word;',
  2791. ' si: SmallInt;',
  2792. ' lw: LongWord;',
  2793. ' li: LongInt;',
  2794. 'begin',
  2795. ' w := $1234;',
  2796. ' b := lo(w);',
  2797. ' b := HI(w);',
  2798. ' lw := $1234CDEF;',
  2799. ' b := lo(lw);',
  2800. ' b := hi(lw);',
  2801. '']);
  2802. ConvertProgram;
  2803. CheckSource('TestLoHiDelphiMode',
  2804. LinesToStr([ // statements
  2805. 'this.LoByte1 = 0x1234 & 0xFF;',
  2806. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2807. 'this.LoByte2 = 0x1234 & 0xFF;',
  2808. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2809. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2810. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2811. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2812. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2813. 'this.b = 0;',
  2814. 'this.w = 0;',
  2815. 'this.si = 0;',
  2816. 'this.lw = 0;',
  2817. 'this.li = 0;'
  2818. ]),
  2819. LinesToStr([ // this.$main
  2820. '$mod.w = 0x1234;',
  2821. '$mod.b = $mod.w & 0xFF;',
  2822. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2823. '$mod.lw = 0x1234CDEF;',
  2824. '$mod.b = $mod.lw & 0xFF;',
  2825. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2826. ]));
  2827. end;
  2828. procedure TTestModule.TestAssignments;
  2829. begin
  2830. StartProgram(false);
  2831. Parser.Options:=Parser.Options+[po_cassignments];
  2832. Add('var');
  2833. Add(' Bar:longint;');
  2834. Add('begin');
  2835. Add(' bar:=3;');
  2836. Add(' bar+=4;');
  2837. Add(' bar-=5;');
  2838. Add(' bar*=6;');
  2839. ConvertProgram;
  2840. CheckSource('TestAssignments',
  2841. LinesToStr([ // statements
  2842. 'this.Bar = 0;'
  2843. ]),
  2844. LinesToStr([ // this.$main
  2845. '$mod.Bar=3;',
  2846. '$mod.Bar+=4;',
  2847. '$mod.Bar-=5;',
  2848. '$mod.Bar*=6;'
  2849. ]));
  2850. end;
  2851. procedure TTestModule.TestArithmeticOperators1;
  2852. begin
  2853. StartProgram(false);
  2854. Add('var');
  2855. Add(' vA,vB,vC:longint;');
  2856. Add('begin');
  2857. Add(' va:=1;');
  2858. Add(' vb:=va+va;');
  2859. Add(' vb:=va div vb;');
  2860. Add(' vb:=va mod vb;');
  2861. Add(' vb:=va+va*vb+va div vb;');
  2862. Add(' vc:=-va;');
  2863. Add(' va:=va-vb;');
  2864. Add(' vb:=va;');
  2865. Add(' if va<vb then vc:=va else vc:=vb;');
  2866. ConvertProgram;
  2867. CheckSource('TestArithmeticOperators1',
  2868. LinesToStr([ // statements
  2869. 'this.vA = 0;',
  2870. 'this.vB = 0;',
  2871. 'this.vC = 0;'
  2872. ]),
  2873. LinesToStr([ // this.$main
  2874. '$mod.vA = 1;',
  2875. '$mod.vB = $mod.vA + $mod.vA;',
  2876. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  2877. '$mod.vB = $mod.vA % $mod.vB;',
  2878. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  2879. '$mod.vC = -$mod.vA;',
  2880. '$mod.vA = $mod.vA - $mod.vB;',
  2881. '$mod.vB = $mod.vA;',
  2882. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2883. ]));
  2884. end;
  2885. procedure TTestModule.TestLogicalOperators;
  2886. begin
  2887. StartProgram(false);
  2888. Add('var');
  2889. Add(' vA,vB,vC:boolean;');
  2890. Add('begin');
  2891. Add(' va:=vb and vc;');
  2892. Add(' va:=vb or vc;');
  2893. Add(' va:=vb xor vc;');
  2894. Add(' va:=true and vc;');
  2895. Add(' va:=(vb and vc) or (va and vb);');
  2896. Add(' va:=not vb;');
  2897. ConvertProgram;
  2898. CheckSource('TestLogicalOperators',
  2899. LinesToStr([ // statements
  2900. 'this.vA = false;',
  2901. 'this.vB = false;',
  2902. 'this.vC = false;'
  2903. ]),
  2904. LinesToStr([ // this.$main
  2905. '$mod.vA = $mod.vB && $mod.vC;',
  2906. '$mod.vA = $mod.vB || $mod.vC;',
  2907. '$mod.vA = $mod.vB ^ $mod.vC;',
  2908. '$mod.vA = true && $mod.vC;',
  2909. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2910. '$mod.vA = !$mod.vB;'
  2911. ]));
  2912. end;
  2913. procedure TTestModule.TestBitwiseOperators;
  2914. begin
  2915. StartProgram(false);
  2916. Add([
  2917. 'var',
  2918. ' vA,vB,vC:longint;',
  2919. ' X,Y,Z: nativeint;',
  2920. 'begin',
  2921. ' va:=vb and vc;',
  2922. ' va:=vb or vc;',
  2923. ' va:=vb xor vc;',
  2924. ' va:=vb shl vc;',
  2925. ' va:=vb shr vc;',
  2926. ' va:=3 and vc;',
  2927. ' va:=(vb and vc) or (va and vb);',
  2928. ' va:=not vb;',
  2929. ' X:=Y and Z;',
  2930. ' X:=Y and va;',
  2931. ' X:=Y or Z;',
  2932. ' X:=Y or va;',
  2933. ' X:=Y xor Z;',
  2934. ' X:=Y xor va;',
  2935. '']);
  2936. ConvertProgram;
  2937. CheckSource('TestBitwiseOperators',
  2938. LinesToStr([ // statements
  2939. 'this.vA = 0;',
  2940. 'this.vB = 0;',
  2941. 'this.vC = 0;',
  2942. 'this.X = 0;',
  2943. 'this.Y = 0;',
  2944. 'this.Z = 0;',
  2945. '']),
  2946. LinesToStr([ // this.$main
  2947. '$mod.vA = $mod.vB & $mod.vC;',
  2948. '$mod.vA = $mod.vB | $mod.vC;',
  2949. '$mod.vA = $mod.vB ^ $mod.vC;',
  2950. '$mod.vA = $mod.vB << $mod.vC;',
  2951. '$mod.vA = $mod.vB >>> $mod.vC;',
  2952. '$mod.vA = 3 & $mod.vC;',
  2953. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2954. '$mod.vA = ~$mod.vB;',
  2955. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  2956. '$mod.X = $mod.Y & $mod.vA;',
  2957. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  2958. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  2959. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  2960. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  2961. '']));
  2962. end;
  2963. procedure TTestModule.TestBitwiseOperatorsLongword;
  2964. begin
  2965. StartProgram(false);
  2966. Add([
  2967. 'var',
  2968. ' a,b,c:longword;',
  2969. ' i: longint;',
  2970. 'begin',
  2971. ' a:=$12345678;',
  2972. ' b:=$EDCBA987;',
  2973. ' c:=not a;',
  2974. ' c:=a and b;',
  2975. ' c:=a and $ffff0000;',
  2976. ' c:=a or b;',
  2977. ' c:=a or $ff00ff00;',
  2978. ' c:=a xor b;',
  2979. ' c:=a xor $f0f0f0f0;',
  2980. ' c:=a shl 1;',
  2981. ' c:=a shl 16;',
  2982. ' c:=a shl 24;',
  2983. ' c:=a shl b;',
  2984. ' c:=a shr 1;',
  2985. ' c:=a shr 16;',
  2986. ' c:=a shr 24;',
  2987. ' c:=a shr b;',
  2988. ' c:=(b and c) or (a and b);',
  2989. ' c:=i and a;',
  2990. ' c:=i or a;',
  2991. ' c:=i xor a;',
  2992. '']);
  2993. ConvertProgram;
  2994. CheckSource('TestBitwiseOperatorsLongword',
  2995. LinesToStr([ // statements
  2996. 'this.a = 0;',
  2997. 'this.b = 0;',
  2998. 'this.c = 0;',
  2999. 'this.i = 0;',
  3000. '']),
  3001. LinesToStr([ // this.$main
  3002. '$mod.a = 0x12345678;',
  3003. '$mod.b = 0xEDCBA987;',
  3004. '$mod.c = rtl.lw(~$mod.a);',
  3005. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3006. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3007. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3008. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3009. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3010. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3011. '$mod.c = rtl.lw($mod.a << 1);',
  3012. '$mod.c = rtl.lw($mod.a << 16);',
  3013. '$mod.c = rtl.lw($mod.a << 24);',
  3014. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3015. '$mod.c = rtl.lw($mod.a >>> 1);',
  3016. '$mod.c = rtl.lw($mod.a >>> 16);',
  3017. '$mod.c = rtl.lw($mod.a >>> 24);',
  3018. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3019. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3020. '$mod.c = $mod.i & $mod.a;',
  3021. '$mod.c = $mod.i | $mod.a;',
  3022. '$mod.c = $mod.i ^ $mod.a;',
  3023. '']));
  3024. end;
  3025. procedure TTestModule.TestPrgProcVar;
  3026. begin
  3027. StartProgram(false);
  3028. Add('procedure Proc1;');
  3029. Add('type');
  3030. Add(' t1=longint;');
  3031. Add('var');
  3032. Add(' vA:t1;');
  3033. Add('begin');
  3034. Add('end;');
  3035. Add('begin');
  3036. ConvertProgram;
  3037. CheckSource('TestPrgProcVar',
  3038. LinesToStr([ // statements
  3039. 'this.Proc1 = function () {',
  3040. ' var vA=0;',
  3041. '};'
  3042. ]),
  3043. LinesToStr([ // this.$main
  3044. ''
  3045. ]));
  3046. end;
  3047. procedure TTestModule.TestUnitProcVar;
  3048. begin
  3049. StartUnit(false);
  3050. Add('interface');
  3051. Add('');
  3052. Add('type tA=string; // unit scope');
  3053. Add('procedure Proc1;');
  3054. Add('');
  3055. Add('implementation');
  3056. Add('');
  3057. Add('procedure Proc1;');
  3058. Add('type tA=longint; // local proc scope');
  3059. Add('var v1:tA; // using local tA');
  3060. Add('begin');
  3061. Add('end;');
  3062. Add('var v2:tA; // using interface tA');
  3063. ConvertUnit;
  3064. CheckSource('TestUnitProcVar',
  3065. LinesToStr([ // statements
  3066. 'var $impl = $mod.$impl;',
  3067. 'this.Proc1 = function () {',
  3068. ' var v1 = 0;',
  3069. '};',
  3070. '']),
  3071. // this.$init
  3072. '',
  3073. // implementation
  3074. LinesToStr([
  3075. '$impl.v2 = "";',
  3076. '']));
  3077. end;
  3078. procedure TTestModule.TestImplProc;
  3079. begin
  3080. StartUnit(false);
  3081. Add('interface');
  3082. Add('');
  3083. Add('procedure Proc1;');
  3084. Add('');
  3085. Add('implementation');
  3086. Add('');
  3087. Add('procedure Proc1; begin end;');
  3088. Add('procedure Proc2; begin end;');
  3089. Add('initialization');
  3090. Add(' Proc1;');
  3091. Add(' Proc2;');
  3092. ConvertUnit;
  3093. CheckSource('TestImplProc',
  3094. LinesToStr([ // statements
  3095. 'var $impl = $mod.$impl;',
  3096. 'this.Proc1 = function () {',
  3097. '};',
  3098. '']),
  3099. LinesToStr([ // this.$init
  3100. '$mod.Proc1();',
  3101. '$impl.Proc2();',
  3102. '']),
  3103. LinesToStr([ // implementation
  3104. '$impl.Proc2 = function () {',
  3105. '};',
  3106. ''])
  3107. );
  3108. end;
  3109. procedure TTestModule.TestFunctionResult;
  3110. begin
  3111. StartProgram(false);
  3112. Add('function Func1: longint;');
  3113. Add('begin');
  3114. Add(' Result:=3;');
  3115. Add(' Func1:=4;');
  3116. Add('end;');
  3117. Add('begin');
  3118. ConvertProgram;
  3119. CheckSource('TestFunctionResult',
  3120. LinesToStr([ // statements
  3121. 'this.Func1 = function () {',
  3122. ' var Result = 0;',
  3123. ' Result = 3;',
  3124. ' Result = 4;',
  3125. ' return Result;',
  3126. '};'
  3127. ]),
  3128. '');
  3129. end;
  3130. procedure TTestModule.TestNestedProc;
  3131. begin
  3132. StartProgram(false);
  3133. Add([
  3134. 'var vInUnit: longint;',
  3135. 'function DoIt(pA,pD: longint): longint;',
  3136. 'var',
  3137. ' vB: longint;',
  3138. ' vC: longint;',
  3139. ' function Nesty(pA: longint): longint; ',
  3140. ' var vB: longint;',
  3141. ' begin',
  3142. ' Result:=pa+vb+vc+pd+vInUnit;',
  3143. ' nesty:=3;',
  3144. ' doit:=4;',
  3145. ' exit;',
  3146. ' end;',
  3147. 'begin',
  3148. ' Result:=pa+vb+vc;',
  3149. ' doit:=6;',
  3150. ' exit;',
  3151. 'end;',
  3152. 'begin']);
  3153. ConvertProgram;
  3154. CheckSource('TestNestedProc',
  3155. LinesToStr([ // statements
  3156. 'this.vInUnit = 0;',
  3157. 'this.DoIt = function (pA, pD) {',
  3158. ' var Result = 0;',
  3159. ' var vB = 0;',
  3160. ' var vC = 0;',
  3161. ' function Nesty(pA) {',
  3162. ' var Result$1 = 0;',
  3163. ' var vB = 0;',
  3164. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3165. ' Result$1 = 3;',
  3166. ' Result = 4;',
  3167. ' return Result$1;',
  3168. ' return Result$1;',
  3169. ' };',
  3170. ' Result = pA + vB + vC;',
  3171. ' Result = 6;',
  3172. ' return Result;',
  3173. ' return Result;',
  3174. '};'
  3175. ]),
  3176. '');
  3177. end;
  3178. procedure TTestModule.TestNestedProc_ResultString;
  3179. begin
  3180. StartProgram(false);
  3181. Add([
  3182. 'function DoIt: string;',
  3183. ' function Nesty: string; ',
  3184. ' begin',
  3185. ' nesty:=#65#66;',
  3186. ' nesty[1]:=#67;',
  3187. ' doit:=#68;',
  3188. ' doit[2]:=#69;',
  3189. ' end;',
  3190. 'begin',
  3191. ' doit:=#70;',
  3192. ' doit[3]:=#71;',
  3193. 'end;',
  3194. 'begin']);
  3195. ConvertProgram;
  3196. CheckSource('TestNestedProc_ResultString',
  3197. LinesToStr([ // statements
  3198. 'this.DoIt = function () {',
  3199. ' var Result = "";',
  3200. ' function Nesty() {',
  3201. ' var Result$1 = "";',
  3202. ' Result$1 = "AB";',
  3203. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3204. ' Result = "D";',
  3205. ' Result = rtl.setCharAt(Result, 1, "E");',
  3206. ' return Result$1;',
  3207. ' };',
  3208. ' Result = "F";',
  3209. ' Result = rtl.setCharAt(Result, 2, "G");',
  3210. ' return Result;',
  3211. '};'
  3212. ]),
  3213. '');
  3214. end;
  3215. procedure TTestModule.TestForwardProc;
  3216. begin
  3217. StartProgram(false);
  3218. Add('procedure FuncA(Bar: longint); forward;');
  3219. Add('procedure FuncB(Bar: longint);');
  3220. Add('begin');
  3221. Add(' funca(bar);');
  3222. Add('end;');
  3223. Add('procedure funca(bar: longint);');
  3224. Add('begin');
  3225. Add(' if bar=3 then ;');
  3226. Add('end;');
  3227. Add('begin');
  3228. Add(' funca(4);');
  3229. Add(' funcb(5);');
  3230. ConvertProgram;
  3231. CheckSource('TestForwardProc',
  3232. LinesToStr([ // statements'
  3233. 'this.FuncB = function (Bar) {',
  3234. ' $mod.FuncA(Bar);',
  3235. '};',
  3236. 'this.FuncA = function (Bar) {',
  3237. ' if (Bar === 3);',
  3238. '};'
  3239. ]),
  3240. LinesToStr([
  3241. '$mod.FuncA(4);',
  3242. '$mod.FuncB(5);'
  3243. ])
  3244. );
  3245. end;
  3246. procedure TTestModule.TestNestedForwardProc;
  3247. begin
  3248. StartProgram(false);
  3249. Add('procedure FuncA;');
  3250. Add(' procedure FuncB(i: longint); forward;');
  3251. Add(' procedure FuncC(i: longint);');
  3252. Add(' begin');
  3253. Add(' funcb(i);');
  3254. Add(' end;');
  3255. Add(' procedure FuncB(i: longint);');
  3256. Add(' begin');
  3257. Add(' if i=3 then ;');
  3258. Add(' end;');
  3259. Add('begin');
  3260. Add(' funcc(4)');
  3261. Add('end;');
  3262. Add('begin');
  3263. Add(' funca;');
  3264. ConvertProgram;
  3265. CheckSource('TestNestedForwardProc',
  3266. LinesToStr([ // statements'
  3267. 'this.FuncA = function () {',
  3268. ' function FuncC(i) {',
  3269. ' FuncB(i);',
  3270. ' };',
  3271. ' function FuncB(i) {',
  3272. ' if (i === 3);',
  3273. ' };',
  3274. ' FuncC(4);',
  3275. '};'
  3276. ]),
  3277. LinesToStr([
  3278. '$mod.FuncA();'
  3279. ])
  3280. );
  3281. end;
  3282. procedure TTestModule.TestAssignFunctionResult;
  3283. begin
  3284. StartProgram(false);
  3285. Add('function Func1: longint;');
  3286. Add('begin');
  3287. Add('end;');
  3288. Add('var i: longint;');
  3289. Add('begin');
  3290. Add(' i:=func1();');
  3291. Add(' i:=func1()+func1();');
  3292. ConvertProgram;
  3293. CheckSource('TestAssignFunctionResult',
  3294. LinesToStr([ // statements
  3295. 'this.Func1 = function () {',
  3296. ' var Result = 0;',
  3297. ' return Result;',
  3298. '};',
  3299. 'this.i = 0;'
  3300. ]),
  3301. LinesToStr([
  3302. '$mod.i = $mod.Func1();',
  3303. '$mod.i = $mod.Func1() + $mod.Func1();'
  3304. ]));
  3305. end;
  3306. procedure TTestModule.TestFunctionResultInCondition;
  3307. begin
  3308. StartProgram(false);
  3309. Add('function Func1: longint;');
  3310. Add('begin');
  3311. Add('end;');
  3312. Add('function Func2: boolean;');
  3313. Add('begin');
  3314. Add('end;');
  3315. Add('var i: longint;');
  3316. Add('begin');
  3317. Add(' if func2 then ;');
  3318. Add(' if i=func1() then ;');
  3319. Add(' if i=func1 then ;');
  3320. ConvertProgram;
  3321. CheckSource('TestFunctionResultInCondition',
  3322. LinesToStr([ // statements
  3323. 'this.Func1 = function () {',
  3324. ' var Result = 0;',
  3325. ' return Result;',
  3326. '};',
  3327. 'this.Func2 = function () {',
  3328. ' var Result = false;',
  3329. ' return Result;',
  3330. '};',
  3331. 'this.i = 0;'
  3332. ]),
  3333. LinesToStr([
  3334. 'if ($mod.Func2());',
  3335. 'if ($mod.i === $mod.Func1());',
  3336. 'if ($mod.i === $mod.Func1());'
  3337. ]));
  3338. end;
  3339. procedure TTestModule.TestFunctionResultInForLoop;
  3340. begin
  3341. StartProgram(false);
  3342. Add([
  3343. 'function Func1(a: array of longint): longint;',
  3344. 'begin',
  3345. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3346. ' for Result in a do if a[Result]=0 then exit;',
  3347. 'end;',
  3348. 'begin',
  3349. ' Func1([1,2,3])']);
  3350. ConvertProgram;
  3351. CheckSource('TestFunctionResultInForLoop',
  3352. LinesToStr([ // statements
  3353. 'this.Func1 = function (a) {',
  3354. ' var Result = 0;',
  3355. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3356. ' Result = $l1;',
  3357. ' if (a[Result] === 0) return Result;',
  3358. ' };',
  3359. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3360. ' Result = $in2[$l3];',
  3361. ' if (a[Result] === 0) return Result;',
  3362. ' };',
  3363. ' return Result;',
  3364. '};',
  3365. '']),
  3366. LinesToStr([
  3367. '$mod.Func1([1, 2, 3]);'
  3368. ]));
  3369. end;
  3370. procedure TTestModule.TestFunctionResultInTypeCast;
  3371. begin
  3372. StartProgram(false);
  3373. Add([
  3374. 'function GetInt: longint;',
  3375. 'begin',
  3376. 'end;',
  3377. 'begin',
  3378. ' if Byte(GetInt)=0 then ;',
  3379. '']);
  3380. ConvertProgram;
  3381. CheckSource('TestFunctionResultInTypeCast',
  3382. LinesToStr([ // statements
  3383. 'this.GetInt = function () {',
  3384. ' var Result = 0;',
  3385. ' return Result;',
  3386. '};',
  3387. '']),
  3388. LinesToStr([
  3389. 'if (($mod.GetInt() & 255) === 0) ;'
  3390. ]));
  3391. end;
  3392. procedure TTestModule.TestExit;
  3393. begin
  3394. StartProgram(false);
  3395. Add('procedure ProcA;');
  3396. Add('begin');
  3397. Add(' exit;');
  3398. Add('end;');
  3399. Add('function FuncB: longint;');
  3400. Add('begin');
  3401. Add(' exit;');
  3402. Add(' exit(3);');
  3403. Add('end;');
  3404. Add('function FuncC: string;');
  3405. Add('begin');
  3406. Add(' exit;');
  3407. Add(' exit(''a'');');
  3408. Add(' exit(''abc'');');
  3409. Add('end;');
  3410. Add('begin');
  3411. Add(' exit;');
  3412. Add(' exit(1);');
  3413. ConvertProgram;
  3414. CheckSource('TestExit',
  3415. LinesToStr([ // statements
  3416. 'this.ProcA = function () {',
  3417. ' return;',
  3418. '};',
  3419. 'this.FuncB = function () {',
  3420. ' var Result = 0;',
  3421. ' return Result;',
  3422. ' return 3;',
  3423. ' return Result;',
  3424. '};',
  3425. 'this.FuncC = function () {',
  3426. ' var Result = "";',
  3427. ' return Result;',
  3428. ' return "a";',
  3429. ' return "abc";',
  3430. ' return Result;',
  3431. '};'
  3432. ]),
  3433. LinesToStr([
  3434. 'return;',
  3435. 'return 1;',
  3436. '']));
  3437. end;
  3438. procedure TTestModule.TestExit_ResultInFinally;
  3439. begin
  3440. StartProgram(false);
  3441. Add([
  3442. 'function Run: word;',
  3443. 'begin',
  3444. ' try',
  3445. ' exit(3);', // no Result in finally -> use return 3
  3446. ' finally',
  3447. ' end;',
  3448. 'end;',
  3449. 'function Fly: word;',
  3450. 'begin',
  3451. ' try',
  3452. ' exit(3);',
  3453. ' finally',
  3454. ' if Result>0 then ;',
  3455. ' end;',
  3456. 'end;',
  3457. 'function Jump: word;',
  3458. 'begin',
  3459. ' try',
  3460. ' try',
  3461. ' exit(4);',
  3462. ' finally',
  3463. ' end;',
  3464. ' finally',
  3465. ' if Result>0 then ;',
  3466. ' end;',
  3467. 'end;',
  3468. 'begin',
  3469. '']);
  3470. ConvertProgram;
  3471. CheckSource('TestExit_ResultInFinally',
  3472. LinesToStr([ // statements
  3473. 'this.Run = function () {',
  3474. ' var Result = 0;',
  3475. ' try {',
  3476. ' return 3;',
  3477. ' } finally {',
  3478. ' };',
  3479. ' return Result;',
  3480. '};',
  3481. 'this.Fly = function () {',
  3482. ' var Result = 0;',
  3483. ' try {',
  3484. ' Result = 3;',
  3485. ' return Result;',
  3486. ' } finally {',
  3487. ' if (Result > 0) ;',
  3488. ' };',
  3489. ' return Result;',
  3490. '};',
  3491. 'this.Jump = function () {',
  3492. ' var Result = 0;',
  3493. ' try {',
  3494. ' try {',
  3495. ' Result = 4;',
  3496. ' return Result;',
  3497. ' } finally {',
  3498. ' };',
  3499. ' } finally {',
  3500. ' if (Result > 0) ;',
  3501. ' };',
  3502. ' return Result;',
  3503. '};',
  3504. '']),
  3505. LinesToStr([
  3506. '']));
  3507. end;
  3508. procedure TTestModule.TestBreak;
  3509. begin
  3510. StartProgram(false);
  3511. Add([
  3512. 'var',
  3513. ' i: longint;',
  3514. 'begin',
  3515. ' repeat',
  3516. ' break;',
  3517. ' until true;',
  3518. ' while true do',
  3519. ' break;',
  3520. ' for i:=1 to 2 do',
  3521. ' break;']);
  3522. ConvertProgram;
  3523. CheckSource('TestBreak',
  3524. LinesToStr([ // statements
  3525. 'this.i = 0;'
  3526. ]),
  3527. LinesToStr([
  3528. 'do {',
  3529. ' break;',
  3530. '} while (!true);',
  3531. 'while (true) break;',
  3532. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3533. '']));
  3534. end;
  3535. procedure TTestModule.TestBreakAsVar;
  3536. begin
  3537. StartProgram(false);
  3538. Add([
  3539. 'procedure DoIt(break: boolean);',
  3540. 'begin',
  3541. ' if break then ;',
  3542. 'end;',
  3543. 'var',
  3544. ' break: boolean;',
  3545. 'begin',
  3546. ' if break then ;']);
  3547. ConvertProgram;
  3548. CheckSource('TestBreakAsVar',
  3549. LinesToStr([ // statements
  3550. 'this.DoIt = function (Break) {',
  3551. ' if (Break) ;',
  3552. '};',
  3553. 'this.Break = false;',
  3554. '']),
  3555. LinesToStr([
  3556. 'if($mod.Break) ;',
  3557. '']));
  3558. end;
  3559. procedure TTestModule.TestContinue;
  3560. begin
  3561. StartProgram(false);
  3562. Add('var i: longint;');
  3563. Add('begin');
  3564. Add(' repeat');
  3565. Add(' continue;');
  3566. Add(' until true;');
  3567. Add(' while true do');
  3568. Add(' continue;');
  3569. Add(' for i:=1 to 2 do');
  3570. Add(' continue;');
  3571. ConvertProgram;
  3572. CheckSource('TestContinue',
  3573. LinesToStr([ // statements
  3574. 'this.i = 0;'
  3575. ]),
  3576. LinesToStr([
  3577. 'do {',
  3578. ' continue;',
  3579. '} while (!true);',
  3580. 'while (true) continue;',
  3581. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3582. '']));
  3583. end;
  3584. procedure TTestModule.TestProc_External;
  3585. begin
  3586. StartProgram(false);
  3587. Add('procedure Foo; external name ''console.log'';');
  3588. Add('function Bar: longint; external name ''get.item'';');
  3589. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3590. Add('var');
  3591. Add(' i: longint;');
  3592. Add('begin');
  3593. Add(' Foo;');
  3594. Add(' i:=Bar;');
  3595. Add(' i:=Bla(''abc'');');
  3596. ConvertProgram;
  3597. CheckSource('TestProc_External',
  3598. LinesToStr([ // statements
  3599. 'this.i = 0;'
  3600. ]),
  3601. LinesToStr([
  3602. 'console.log();',
  3603. '$mod.i = get.item();',
  3604. '$mod.i = apply.something("abc");'
  3605. ]));
  3606. end;
  3607. procedure TTestModule.TestProc_ExternalOtherUnit;
  3608. begin
  3609. AddModuleWithIntfImplSrc('unit2.pas',
  3610. LinesToStr([
  3611. 'procedure Now; external name ''Date.now'';',
  3612. 'procedure DoIt;'
  3613. ]),
  3614. 'procedure doit; begin end;');
  3615. StartUnit(true);
  3616. Add('interface');
  3617. Add('uses unit2;');
  3618. Add('implementation');
  3619. Add('begin');
  3620. Add(' now;');
  3621. Add(' now();');
  3622. Add(' uNit2.now;');
  3623. Add(' uNit2.now();');
  3624. Add(' doit;');
  3625. Add(' uNit2.doit;');
  3626. ConvertUnit;
  3627. CheckSource('TestProc_ExternalOtherUnit',
  3628. LinesToStr([
  3629. '']),
  3630. LinesToStr([
  3631. 'Date.now();',
  3632. 'Date.now();',
  3633. 'Date.now();',
  3634. 'Date.now();',
  3635. 'pas.unit2.DoIt();',
  3636. 'pas.unit2.DoIt();',
  3637. '']));
  3638. end;
  3639. procedure TTestModule.TestProc_Asm;
  3640. begin
  3641. StartProgram(false);
  3642. Add([
  3643. '{$mode delphi}',
  3644. 'function DoIt: longint;',
  3645. 'begin;',
  3646. ' asm',
  3647. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3648. ' end;',
  3649. ' asm console.log(); end;',
  3650. ' asm',
  3651. ' s = "'' ";',
  3652. ' s = ''" '';',
  3653. ' s = s + "world" + "''";',
  3654. ' // end',
  3655. ' s = ''end'';',
  3656. ' s = "end";',
  3657. ' s = "foo\"bar";',
  3658. ' s = ''a\''b'';',
  3659. ' s = `${expr}\`-"-''-`;',
  3660. ' s = `multi',
  3661. 'line`;',
  3662. ' end;',
  3663. 'end;',
  3664. 'procedure Fly;',
  3665. 'asm',
  3666. ' return;',
  3667. 'end;',
  3668. 'begin']);
  3669. ConvertProgram;
  3670. CheckSource('TestProc_Asm',
  3671. LinesToStr([ // statements
  3672. 'this.DoIt = function () {',
  3673. ' var Result = 0;',
  3674. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3675. ' console.log();',
  3676. ' s = "'' ";',
  3677. ' s = ''" '';',
  3678. ' s = s + "world" + "''";',
  3679. ' // end',
  3680. ' s = ''end'';',
  3681. ' s = "end";',
  3682. ' s = "foo\"bar";',
  3683. ' s = ''a\''b'';',
  3684. ' s = `${expr}\`-"-''-`;',
  3685. ' s = `multi',
  3686. 'line`;',
  3687. ' return Result;',
  3688. '};',
  3689. 'this.Fly = function () {',
  3690. ' return;',
  3691. '};',
  3692. '']),
  3693. LinesToStr([
  3694. ''
  3695. ]));
  3696. end;
  3697. procedure TTestModule.TestProc_Assembler;
  3698. begin
  3699. StartProgram(false);
  3700. Add('function DoIt: longint; assembler;');
  3701. Add('asm');
  3702. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3703. Add('end;');
  3704. Add('begin');
  3705. ConvertProgram;
  3706. CheckSource('TestProc_Assembler',
  3707. LinesToStr([ // statements
  3708. 'this.DoIt = function () {',
  3709. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3710. '};'
  3711. ]),
  3712. LinesToStr([
  3713. ''
  3714. ]));
  3715. end;
  3716. procedure TTestModule.TestProc_VarParam;
  3717. begin
  3718. StartProgram(false);
  3719. Add('type integer = longint;');
  3720. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3721. Add('var vJ: integer;');
  3722. Add('begin');
  3723. Add(' vg:=vg+1;');
  3724. Add(' vj:=vh+2;');
  3725. Add(' vi:=vi+3;');
  3726. Add(' doit(vg,vg,vg);');
  3727. Add(' doit(vh,vh,vj);');
  3728. Add(' doit(vi,vi,vi);');
  3729. Add(' doit(vj,vj,vj);');
  3730. Add('end;');
  3731. Add('var i: integer;');
  3732. Add('begin');
  3733. Add(' doit(i,i,i);');
  3734. ConvertProgram;
  3735. CheckSource('TestProc_VarParam',
  3736. LinesToStr([ // statements
  3737. 'this.DoIt = function (vG,vH,vI) {',
  3738. ' var vJ = 0;',
  3739. ' vG = vG + 1;',
  3740. ' vJ = vH + 2;',
  3741. ' vI.set(vI.get()+3);',
  3742. ' $mod.DoIt(vG, vG, {',
  3743. ' get: function () {',
  3744. ' return vG;',
  3745. ' },',
  3746. ' set: function (v) {',
  3747. ' vG = v;',
  3748. ' }',
  3749. ' });',
  3750. ' $mod.DoIt(vH, vH, {',
  3751. ' get: function () {',
  3752. ' return vJ;',
  3753. ' },',
  3754. ' set: function (v) {',
  3755. ' vJ = v;',
  3756. ' }',
  3757. ' });',
  3758. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3759. ' $mod.DoIt(vJ, vJ, {',
  3760. ' get: function () {',
  3761. ' return vJ;',
  3762. ' },',
  3763. ' set: function (v) {',
  3764. ' vJ = v;',
  3765. ' }',
  3766. ' });',
  3767. '};',
  3768. 'this.i = 0;'
  3769. ]),
  3770. LinesToStr([
  3771. '$mod.DoIt($mod.i,$mod.i,{',
  3772. ' p: $mod,',
  3773. ' get: function () {',
  3774. ' return this.p.i;',
  3775. ' },',
  3776. ' set: function (v) {',
  3777. ' this.p.i = v;',
  3778. ' }',
  3779. '});'
  3780. ]));
  3781. end;
  3782. procedure TTestModule.TestProc_VarParamString;
  3783. begin
  3784. StartProgram(false);
  3785. Add(['type TCaption = string;',
  3786. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3787. 'var c: char;',
  3788. 'begin',
  3789. ' va[1]:=c;',
  3790. ' vb[2]:=c;',
  3791. ' vc[3]:=c;',
  3792. 'end;',
  3793. 'begin']);
  3794. ConvertProgram;
  3795. CheckSource('TestProc_VarParamString',
  3796. LinesToStr([ // statements
  3797. 'this.DoIt = function (vA,vB,vC) {',
  3798. ' var c = "";',
  3799. ' vA = rtl.setCharAt(vA, 0, c);',
  3800. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3801. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3802. '};',
  3803. '']),
  3804. LinesToStr([
  3805. ]));
  3806. end;
  3807. procedure TTestModule.TestProc_VarParamV;
  3808. begin
  3809. StartProgram(false);
  3810. Add([
  3811. 'procedure Inc2(var i: longint);',
  3812. 'begin',
  3813. ' i:=i+2;',
  3814. 'end;',
  3815. 'procedure DoIt(v: longint);',
  3816. 'var p: array of longint;',
  3817. 'begin',
  3818. ' Inc2(v);',
  3819. ' Inc2(p[v]);',
  3820. 'end;',
  3821. 'begin']);
  3822. ConvertProgram;
  3823. CheckSource('TestProc_VarParamV',
  3824. LinesToStr([ // statements
  3825. 'this.Inc2 = function (i) {',
  3826. ' i.set(i.get()+2);',
  3827. '};',
  3828. 'this.DoIt = function (v) {',
  3829. ' var p = [];',
  3830. ' $mod.Inc2({get: function () {',
  3831. ' return v;',
  3832. ' }, set: function (w) {',
  3833. ' v = w;',
  3834. ' }});',
  3835. ' $mod.Inc2({',
  3836. ' a: v,',
  3837. ' p: p,',
  3838. ' get: function () {',
  3839. ' return this.p[this.a];',
  3840. ' },',
  3841. ' set: function (v) {',
  3842. ' this.p[this.a] = v;',
  3843. ' }',
  3844. ' });',
  3845. '};',
  3846. '']),
  3847. LinesToStr([
  3848. '']));
  3849. end;
  3850. procedure TTestModule.TestProc_Overload;
  3851. begin
  3852. StartProgram(false);
  3853. Add('procedure DoIt(vI: longint); begin end;');
  3854. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3855. Add('procedure DoIt(vD: double); begin end;');
  3856. Add('begin');
  3857. Add(' DoIt(1);');
  3858. Add(' DoIt(2,3);');
  3859. Add(' DoIt(4.5);');
  3860. ConvertProgram;
  3861. CheckSource('TestProcedureOverload',
  3862. LinesToStr([ // statements
  3863. 'this.DoIt = function (vI) {',
  3864. '};',
  3865. 'this.DoIt$1 = function (vI, vJ) {',
  3866. '};',
  3867. 'this.DoIt$2 = function (vD) {',
  3868. '};',
  3869. '']),
  3870. LinesToStr([
  3871. '$mod.DoIt(1);',
  3872. '$mod.DoIt$1(2, 3);',
  3873. '$mod.DoIt$2(4.5);',
  3874. '']));
  3875. end;
  3876. procedure TTestModule.TestProc_OverloadForward;
  3877. begin
  3878. StartProgram(false);
  3879. Add('procedure DoIt(vI: longint); forward;');
  3880. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3881. Add('procedure doit(vi: longint); begin end;');
  3882. Add('begin');
  3883. Add(' doit(1);');
  3884. Add(' doit(2,3);');
  3885. ConvertProgram;
  3886. CheckSource('TestProcedureOverloadForward',
  3887. LinesToStr([ // statements
  3888. 'this.DoIt$1 = function (vI, vJ) {',
  3889. '};',
  3890. 'this.DoIt = function (vI) {',
  3891. '};',
  3892. '']),
  3893. LinesToStr([
  3894. '$mod.DoIt(1);',
  3895. '$mod.DoIt$1(2, 3);',
  3896. '']));
  3897. end;
  3898. procedure TTestModule.TestProc_OverloadIntfImpl;
  3899. begin
  3900. StartUnit(false);
  3901. Add('interface');
  3902. Add('procedure DoIt(vI: longint);');
  3903. Add('procedure DoIt(vI, vJ: longint);');
  3904. Add('implementation');
  3905. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3906. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3907. Add('procedure DoIt(vi: longint); begin end;');
  3908. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3909. Add('procedure DoIt(vi, vj: longint); begin end;');
  3910. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3911. Add('begin');
  3912. Add(' doit(1);');
  3913. Add(' doit(2,3);');
  3914. Add(' doit(4,5,6);');
  3915. Add(' doit(7,8,9,10);');
  3916. Add(' doit(11,12,13,14,15);');
  3917. ConvertUnit;
  3918. CheckSource('TestProcedureOverloadUnit',
  3919. LinesToStr([ // statements
  3920. 'var $impl = $mod.$impl;',
  3921. 'this.DoIt = function (vI) {',
  3922. '};',
  3923. 'this.DoIt$1 = function (vI, vJ) {',
  3924. '};',
  3925. '']),
  3926. LinesToStr([ // this.$init
  3927. '$mod.DoIt(1);',
  3928. '$mod.DoIt$1(2, 3);',
  3929. '$impl.DoIt$3(4,5,6);',
  3930. '$impl.DoIt$4(7,8,9,10);',
  3931. '$impl.DoIt$2(11,12,13,14,15);',
  3932. '']),
  3933. LinesToStr([ // implementation
  3934. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3935. '};',
  3936. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3937. '};',
  3938. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3939. '};',
  3940. '']));
  3941. end;
  3942. procedure TTestModule.TestProc_OverloadNested;
  3943. begin
  3944. StartProgram(false);
  3945. Add([
  3946. 'procedure DoIt(vA: longint); overload; forward;',
  3947. 'procedure DoIt(vB, vC: longint); overload;',
  3948. 'begin // 2 param overload',
  3949. ' doit(1);',
  3950. ' doit(1,2);',
  3951. 'end;',
  3952. 'procedure doit(vA: longint);',
  3953. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3954. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3955. ' begin // 4 param overload',
  3956. ' doit(1);',
  3957. ' doit(1,2);',
  3958. ' doit(1,2,3);',
  3959. ' doit(1,2,3,4);',
  3960. ' end;',
  3961. ' procedure doit(vA, vB, vC: longint);',
  3962. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3963. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3964. ' begin // 6 param overload',
  3965. ' doit(1);',
  3966. ' doit(1,2);',
  3967. ' doit(1,2,3);',
  3968. ' doit(1,2,3,4);',
  3969. ' doit(1,2,3,4,5);',
  3970. ' doit(1,2,3,4,5,6);',
  3971. ' end;',
  3972. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3973. ' begin // 5 param overload',
  3974. ' doit(1);',
  3975. ' doit(1,2);',
  3976. ' doit(1,2,3);',
  3977. ' doit(1,2,3,4);',
  3978. ' doit(1,2,3,4,5);',
  3979. ' doit(1,2,3,4,5,6);',
  3980. ' end;',
  3981. ' begin // 3 param overload',
  3982. ' doit(1);',
  3983. ' doit(1,2);',
  3984. ' doit(1,2,3);',
  3985. ' doit(1,2,3,4);',
  3986. ' doit(1,2,3,4,5);',
  3987. ' doit(1,2,3,4,5,6);',
  3988. ' end;',
  3989. 'begin // 1 param overload',
  3990. ' doit(1);',
  3991. ' doit(1,2);',
  3992. ' doit(1,2,3);',
  3993. ' doit(1,2,3,4);',
  3994. 'end;',
  3995. 'begin // main',
  3996. ' doit(1);',
  3997. ' doit(1,2);']);
  3998. ConvertProgram;
  3999. CheckSource('TestProcedureOverloadNested',
  4000. LinesToStr([ // statements
  4001. 'this.DoIt$1 = function (vB, vC) {',
  4002. ' $mod.DoIt(1);',
  4003. ' $mod.DoIt$1(1, 2);',
  4004. '};',
  4005. 'this.DoIt = function (vA) {',
  4006. ' function DoIt$3(vA, vB, vC, vD) {',
  4007. ' $mod.DoIt(1);',
  4008. ' $mod.DoIt$1(1, 2);',
  4009. ' DoIt$2(1, 2, 3);',
  4010. ' DoIt$3(1, 2, 3, 4);',
  4011. ' };',
  4012. ' function DoIt$2(vA, vB, vC) {',
  4013. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4014. ' $mod.DoIt(1);',
  4015. ' $mod.DoIt$1(1, 2);',
  4016. ' DoIt$2(1, 2, 3);',
  4017. ' DoIt$3(1, 2, 3, 4);',
  4018. ' DoIt$4(1, 2, 3, 4, 5);',
  4019. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4020. ' };',
  4021. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4022. ' $mod.DoIt(1);',
  4023. ' $mod.DoIt$1(1, 2);',
  4024. ' DoIt$2(1, 2, 3);',
  4025. ' DoIt$3(1, 2, 3, 4);',
  4026. ' DoIt$4(1, 2, 3, 4, 5);',
  4027. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4028. ' };',
  4029. ' $mod.DoIt(1);',
  4030. ' $mod.DoIt$1(1, 2);',
  4031. ' DoIt$2(1, 2, 3);',
  4032. ' DoIt$3(1, 2, 3, 4);',
  4033. ' DoIt$4(1, 2, 3, 4, 5);',
  4034. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4035. ' };',
  4036. ' $mod.DoIt(1);',
  4037. ' $mod.DoIt$1(1, 2);',
  4038. ' DoIt$2(1, 2, 3);',
  4039. ' DoIt$3(1, 2, 3, 4);',
  4040. '};',
  4041. '']),
  4042. LinesToStr([
  4043. '$mod.DoIt(1);',
  4044. '$mod.DoIt$1(1, 2);',
  4045. '']));
  4046. end;
  4047. procedure TTestModule.TestProc_OverloadUnitCycle;
  4048. begin
  4049. AddModuleWithIntfImplSrc('Unit2.pas',
  4050. LinesToStr([
  4051. 'type',
  4052. ' TObject = class',
  4053. ' procedure DoIt(b: boolean); virtual; abstract;',
  4054. ' procedure DoIt(i: longint); virtual; abstract;',
  4055. ' end;',
  4056. '']),
  4057. 'uses test1;');
  4058. StartUnit(true);
  4059. Add([
  4060. 'interface',
  4061. 'uses unit2;',
  4062. 'type',
  4063. ' TEagle = class(TObject)',
  4064. ' procedure DoIt(b: boolean); override;',
  4065. ' procedure DoIt(i: longint); override;',
  4066. ' end;',
  4067. 'implementation',
  4068. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4069. 'procedure TEagle.DoIt(i: longint); begin end;',
  4070. '']);
  4071. ConvertUnit;
  4072. CheckSource('TestProc_OverloadUnitCycle',
  4073. LinesToStr([ // statements
  4074. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  4075. ' this.DoIt = function (b) {',
  4076. ' };',
  4077. ' this.DoIt$1 = function (i) {',
  4078. ' };',
  4079. '});',
  4080. '']),
  4081. '',
  4082. LinesToStr([
  4083. '']));
  4084. end;
  4085. procedure TTestModule.TestProc_Varargs;
  4086. begin
  4087. StartProgram(false);
  4088. Add([
  4089. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4090. 'procedure ProcB; varargs; external name ''ProcB'';',
  4091. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4092. 'function GetIt: longint; begin end;',
  4093. 'begin',
  4094. ' ProcA(1);',
  4095. ' ProcA(1,2);',
  4096. ' ProcA(1,2.0);',
  4097. ' ProcA(1,2,3);',
  4098. ' ProcA(1,''2'');',
  4099. ' ProcA(2,'''');',
  4100. ' ProcA(3,false);',
  4101. ' ProcB;',
  4102. ' ProcB();',
  4103. ' ProcB(4);',
  4104. ' ProcB(''foo'');',
  4105. ' ProcC;',
  4106. ' ProcC();',
  4107. ' ProcC(4);',
  4108. ' ProcC(5,''foo'');',
  4109. ' ProcB(GetIt);',
  4110. ' ProcB(GetIt());',
  4111. ' ProcB(GetIt,GetIt());']);
  4112. ConvertProgram;
  4113. CheckSource('TestProc_Varargs',
  4114. LinesToStr([ // statements
  4115. 'this.GetIt = function () {',
  4116. ' var Result = 0;',
  4117. ' return Result;',
  4118. '};',
  4119. '']),
  4120. LinesToStr([
  4121. 'ProcA(1);',
  4122. 'ProcA(1, 2);',
  4123. 'ProcA(1, 2.0);',
  4124. 'ProcA(1, 2, 3);',
  4125. 'ProcA(1, "2");',
  4126. 'ProcA(2, "");',
  4127. 'ProcA(3, false);',
  4128. 'ProcB();',
  4129. 'ProcB();',
  4130. 'ProcB(4);',
  4131. 'ProcB("foo");',
  4132. 'ProcC(17);',
  4133. 'ProcC(17);',
  4134. 'ProcC(4);',
  4135. 'ProcC(5, "foo");',
  4136. 'ProcB($mod.GetIt());',
  4137. 'ProcB($mod.GetIt());',
  4138. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4139. '']));
  4140. end;
  4141. procedure TTestModule.TestProc_ConstOrder;
  4142. begin
  4143. StartProgram(false);
  4144. Add([
  4145. 'const A = 3;',
  4146. 'const B = A+1;',
  4147. 'procedure DoIt;',
  4148. 'const C = A+1;',
  4149. 'const D = B+1;',
  4150. 'const E = D+C+B+A;',
  4151. 'begin',
  4152. 'end;',
  4153. 'begin'
  4154. ]);
  4155. ConvertProgram;
  4156. CheckSource('TestProc_ConstOrder',
  4157. LinesToStr([ // statements
  4158. 'this.A = 3;',
  4159. 'this.B = 3 + 1;',
  4160. 'var C = 3 + 1;',
  4161. 'var D = 4 + 1;',
  4162. 'var E = 5 + 4 + 4 + 3;',
  4163. 'this.DoIt = function () {',
  4164. '};',
  4165. '']),
  4166. LinesToStr([
  4167. ''
  4168. ]));
  4169. end;
  4170. procedure TTestModule.TestProc_DuplicateConst;
  4171. begin
  4172. StartProgram(false);
  4173. Add([
  4174. 'const A = 1;',
  4175. 'procedure DoIt;',
  4176. 'const A = 2;',
  4177. ' procedure SubIt;',
  4178. ' const A = 21;',
  4179. ' begin',
  4180. ' end;',
  4181. 'begin',
  4182. 'end;',
  4183. 'procedure DoSome;',
  4184. 'const A = 3;',
  4185. 'begin',
  4186. 'end;',
  4187. 'begin'
  4188. ]);
  4189. ConvertProgram;
  4190. CheckSource('TestProc_DuplicateConst',
  4191. LinesToStr([ // statements
  4192. 'this.A = 1;',
  4193. 'var A$1 = 2;',
  4194. 'var A$2 = 21;',
  4195. 'this.DoIt = function () {',
  4196. ' function SubIt() {',
  4197. ' };',
  4198. '};',
  4199. 'var A$3 = 3;',
  4200. 'this.DoSome = function () {',
  4201. '};',
  4202. '']),
  4203. LinesToStr([
  4204. ''
  4205. ]));
  4206. end;
  4207. procedure TTestModule.TestProc_LocalVarAbsolute;
  4208. begin
  4209. StartProgram(false);
  4210. Add([
  4211. 'type',
  4212. ' TObject = class',
  4213. ' Index: longint;',
  4214. ' procedure DoAbs(Item: pointer);',
  4215. ' end;',
  4216. 'procedure TObject.DoAbs(Item: pointer);',
  4217. 'var',
  4218. ' o: TObject absolute Item;',
  4219. 'begin',
  4220. ' if o.Index<o.Index then o.Index:=o.Index;',
  4221. 'end;',
  4222. 'procedure DoIt(i: longint; p: pointer);',
  4223. 'var',
  4224. ' d: double absolute i;',
  4225. ' s: string absolute d;',
  4226. ' oi: TObject absolute i;',
  4227. ' op: TObject absolute p;',
  4228. 'begin',
  4229. ' if d=d then d:=d;',
  4230. ' if s=s then s:=s;',
  4231. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4232. ' if op.Index=op.Index then op.Index:=op.Index;',
  4233. 'end;',
  4234. 'begin']);
  4235. ConvertProgram;
  4236. CheckSource('TestProc_LocalVarAbsolute',
  4237. LinesToStr([ // statements
  4238. 'rtl.createClass($mod, "TObject", null, function () {',
  4239. ' this.$init = function () {',
  4240. ' this.Index = 0;',
  4241. ' };',
  4242. ' this.$final = function () {',
  4243. ' };',
  4244. ' this.DoAbs = function (Item) {',
  4245. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4246. ' };',
  4247. '});',
  4248. 'this.DoIt = function (i, p) {',
  4249. ' if (i === i) i = i;',
  4250. ' if (i === i) i = i;',
  4251. ' if (i.Index < i.Index) i.Index = i.Index;',
  4252. ' if (p.Index === p.Index) p.Index = p.Index;',
  4253. '};'
  4254. ]),
  4255. LinesToStr([
  4256. ]));
  4257. end;
  4258. procedure TTestModule.TestProc_LocalVarInit;
  4259. begin
  4260. StartProgram(false);
  4261. Add([
  4262. 'type TBytes = array of byte;',
  4263. 'procedure DoIt;',
  4264. 'const c = 4;',
  4265. 'var',
  4266. ' b: byte = 1;',
  4267. ' w: word = 2+c;',
  4268. ' p: pointer = nil;',
  4269. ' Buffer: TBytes = nil;',
  4270. 'begin',
  4271. 'end;',
  4272. 'begin']);
  4273. ConvertProgram;
  4274. CheckSource('TestProc_LocalVarInit',
  4275. LinesToStr([ // statements
  4276. 'var c = 4;',
  4277. 'this.DoIt = function () {',
  4278. ' var b = 1;',
  4279. ' var w = 2 + 4;',
  4280. ' var p = null;',
  4281. ' var Buffer = [];',
  4282. '};',
  4283. '']),
  4284. LinesToStr([
  4285. ]));
  4286. end;
  4287. procedure TTestModule.TestProc_ReservedWords;
  4288. begin
  4289. StartProgram(false);
  4290. Add([
  4291. 'procedure Date(ArrayBuffer: longint);',
  4292. 'const',
  4293. ' NaN: longint = 3;',
  4294. 'var',
  4295. ' &Boolean: longint;',
  4296. ' procedure Error(ArrayBuffer: longint);',
  4297. ' begin',
  4298. ' end;',
  4299. 'begin',
  4300. ' Nan:=&bOolean;',
  4301. 'end;',
  4302. 'begin',
  4303. ' Date(1);']);
  4304. ConvertProgram;
  4305. CheckSource('TestProc_ReservedWords',
  4306. LinesToStr([ // statements
  4307. 'var naN = 3;',
  4308. 'this.Date = function (arrayBuffer) {',
  4309. ' var boolean = 0;',
  4310. ' function error(arrayBuffer) {',
  4311. ' };',
  4312. ' naN = boolean;',
  4313. '};',
  4314. '']),
  4315. LinesToStr([
  4316. ' $mod.Date(1);'
  4317. ]));
  4318. end;
  4319. procedure TTestModule.TestProc_ConstRefWord;
  4320. begin
  4321. StartProgram(false);
  4322. Add([
  4323. 'procedure Run(constref w: word);',
  4324. 'var l: word;',
  4325. 'begin',
  4326. ' l:=w;',
  4327. ' Run(w);',
  4328. ' Run(l);',
  4329. 'end;',
  4330. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4331. 'begin',
  4332. ' Run(a);',
  4333. ' Run(b);',
  4334. ' Run(c);',
  4335. ' Run(d);',
  4336. ' Run(e);',
  4337. 'end;',
  4338. 'begin',
  4339. ' Run(1);']);
  4340. ConvertProgram;
  4341. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4342. CheckSource('TestProc_ConstRefWord',
  4343. LinesToStr([ // statements
  4344. 'this.Run = function (w) {',
  4345. ' var l = 0;',
  4346. ' l = w;',
  4347. ' $mod.Run(w);',
  4348. ' $mod.Run(l);',
  4349. '};',
  4350. 'this.Fly = function (a, b, c, d, e) {',
  4351. ' $mod.Run(a);',
  4352. ' $mod.Run(b.get());',
  4353. ' $mod.Run(c.get());',
  4354. ' $mod.Run(d);',
  4355. ' $mod.Run(e);',
  4356. '};',
  4357. '']),
  4358. LinesToStr([
  4359. '$mod.Run(1);'
  4360. ]));
  4361. end;
  4362. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4363. begin
  4364. StartProgram(false);
  4365. Add([
  4366. '{$mode objfpc}',
  4367. 'type',
  4368. ' TFunc = reference to function(x: word): word;',
  4369. 'var Func: TFunc;',
  4370. 'procedure DoIt(a: word);',
  4371. 'begin',
  4372. ' Func:=function(b:word): word',
  4373. ' begin',
  4374. ' Result:=a+b;',
  4375. ' exit(b);',
  4376. ' exit(Result);',
  4377. ' end;',// test semicolon
  4378. ' a:=3;',
  4379. 'end;',
  4380. 'begin',
  4381. ' Func:=function(c:word):word begin',
  4382. ' Result:=3+c;',
  4383. ' exit(c);',
  4384. ' exit(Result);',
  4385. ' end;']);
  4386. ConvertProgram;
  4387. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4388. LinesToStr([ // statements
  4389. 'this.Func = null;',
  4390. 'this.DoIt = function (a) {',
  4391. ' $mod.Func = function (b) {',
  4392. ' var Result = 0;',
  4393. ' Result = a + b;',
  4394. ' return b;',
  4395. ' return Result;',
  4396. ' return Result;',
  4397. ' };',
  4398. ' a = 3;',
  4399. '};',
  4400. '']),
  4401. LinesToStr([
  4402. '$mod.Func = function (c) {',
  4403. ' var Result = 0;',
  4404. ' Result = 3 + c;',
  4405. ' return c;',
  4406. ' return Result;',
  4407. ' return Result;',
  4408. '};',
  4409. '']));
  4410. end;
  4411. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4412. begin
  4413. StartProgram(false);
  4414. Add([
  4415. '{$mode delphi}',
  4416. 'type',
  4417. ' TProc = reference to procedure(x: word);',
  4418. 'procedure DoIt(a: word);',
  4419. 'var Proc: TProc;',
  4420. 'begin',
  4421. ' Proc:=procedure(b:word) begin end;',
  4422. 'end;',
  4423. 'var Proc: TProc;',
  4424. 'begin',
  4425. ' Proc:=procedure(c:word) begin end;',
  4426. '']);
  4427. ConvertProgram;
  4428. CheckSource('TestAnonymousProc_Assign_Delphi',
  4429. LinesToStr([ // statements
  4430. 'this.DoIt = function (a) {',
  4431. ' var Proc = null;',
  4432. ' Proc = function (b) {',
  4433. ' };',
  4434. '};',
  4435. 'this.Proc = null;',
  4436. '']),
  4437. LinesToStr([
  4438. '$mod.Proc = function (c) {',
  4439. '};',
  4440. '']));
  4441. end;
  4442. procedure TTestModule.TestAnonymousProc_Arg;
  4443. begin
  4444. StartProgram(false);
  4445. Add([
  4446. 'type',
  4447. ' TProc = reference to procedure;',
  4448. ' TFunc = reference to function(x: word): word;',
  4449. 'procedure DoMore(f,g: TProc);',
  4450. 'begin',
  4451. 'end;',
  4452. 'procedure DoOdd(v: jsvalue);',
  4453. 'begin',
  4454. 'end;',
  4455. 'procedure DoIt(f: TFunc);',
  4456. 'begin',
  4457. ' DoIt(function(b:word): word',
  4458. ' begin',
  4459. ' Result:=1+b;',
  4460. ' end);',
  4461. ' DoMore(procedure begin end, procedure begin end);',
  4462. ' DoOdd(procedure begin end);',
  4463. 'end;',
  4464. 'begin',
  4465. ' DoMore(procedure begin end,',
  4466. ' procedure assembler asm',
  4467. ' console.log("c");',
  4468. ' end);',
  4469. '']);
  4470. ConvertProgram;
  4471. CheckSource('TestAnonymousProc_Arg',
  4472. LinesToStr([ // statements
  4473. 'this.DoMore = function (f, g) {',
  4474. '};',
  4475. 'this.DoOdd = function (v) {',
  4476. '};',
  4477. 'this.DoIt = function (f) {',
  4478. ' $mod.DoIt(function (b) {',
  4479. ' var Result = 0;',
  4480. ' Result = 1 + b;',
  4481. ' return Result;',
  4482. ' });',
  4483. ' $mod.DoMore(function () {',
  4484. ' }, function () {',
  4485. ' });',
  4486. ' $mod.DoOdd(function () {',
  4487. ' });',
  4488. '};',
  4489. '']),
  4490. LinesToStr([
  4491. '$mod.DoMore(function () {',
  4492. '}, function () {',
  4493. ' console.log("c");',
  4494. '});',
  4495. '']));
  4496. end;
  4497. procedure TTestModule.TestAnonymousProc_Typecast;
  4498. begin
  4499. StartProgram(false);
  4500. Add([
  4501. 'type',
  4502. ' TProc = reference to procedure(w: word);',
  4503. ' TArr = array of word;',
  4504. ' TFuncArr = reference to function: TArr;',
  4505. 'procedure DoIt(p: TProc);',
  4506. 'var',
  4507. ' w: word;',
  4508. ' a: TArr;',
  4509. 'begin',
  4510. ' p:=TProc(procedure(b: smallint) begin end);',
  4511. ' a:=TFuncArr(function: TArr begin end)();',
  4512. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4513. 'end;',
  4514. 'begin']);
  4515. ConvertProgram;
  4516. CheckSource('TestAnonymousProc_Typecast',
  4517. LinesToStr([ // statements
  4518. 'this.DoIt = function (p) {',
  4519. ' var w = 0;',
  4520. ' var a = [];',
  4521. ' p = function (b) {',
  4522. ' };',
  4523. ' a = function () {',
  4524. ' var Result = [];',
  4525. ' return Result;',
  4526. ' }();',
  4527. ' w = function () {',
  4528. ' var Result = [];',
  4529. ' return Result;',
  4530. ' }()[3];',
  4531. '};',
  4532. '']),
  4533. LinesToStr([
  4534. '']));
  4535. end;
  4536. procedure TTestModule.TestAnonymousProc_With;
  4537. begin
  4538. StartProgram(false);
  4539. Add([
  4540. 'type',
  4541. ' TProc = reference to procedure(w: word);',
  4542. ' TObject = class',
  4543. ' b: boolean;',
  4544. ' end;',
  4545. 'var',
  4546. ' p: TProc;',
  4547. ' bird: TObject;',
  4548. 'begin',
  4549. ' with bird do',
  4550. ' p:=procedure(w: word)',
  4551. ' begin',
  4552. ' b:=w>2;',
  4553. ' end;',
  4554. '']);
  4555. ConvertProgram;
  4556. CheckSource('TestAnonymousProc_With',
  4557. LinesToStr([ // statements
  4558. 'rtl.createClass($mod, "TObject", null, function () {',
  4559. ' this.$init = function () {',
  4560. ' this.b = false;',
  4561. ' };',
  4562. ' this.$final = function () {',
  4563. ' };',
  4564. '});',
  4565. 'this.p = null;',
  4566. 'this.bird = null;',
  4567. '']),
  4568. LinesToStr([
  4569. 'var $with1 = $mod.bird;',
  4570. '$mod.p = function (w) {',
  4571. ' $with1.b = w > 2;',
  4572. '};',
  4573. '']));
  4574. end;
  4575. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4576. begin
  4577. StartProgram(false);
  4578. Add([
  4579. 'type',
  4580. ' TProc = reference to procedure;',
  4581. ' TObject = class',
  4582. ' b: boolean;',
  4583. ' end;',
  4584. 'procedure DoIt;',
  4585. 'var',
  4586. ' p: TProc;',
  4587. 'begin',
  4588. ' try',
  4589. ' except',
  4590. ' on E: TObject do',
  4591. ' p:=procedure',
  4592. ' begin',
  4593. ' E.b:=true;',
  4594. ' end;',
  4595. ' end;',
  4596. 'end;',
  4597. 'begin']);
  4598. ConvertProgram;
  4599. CheckSource('TestAnonymousProc_ExceptOn',
  4600. LinesToStr([ // statements
  4601. 'rtl.createClass($mod, "TObject", null, function () {',
  4602. ' this.$init = function () {',
  4603. ' this.b = false;',
  4604. ' };',
  4605. ' this.$final = function () {',
  4606. ' };',
  4607. '});',
  4608. 'this.DoIt = function () {',
  4609. ' var p = null;',
  4610. ' try {} catch ($e) {',
  4611. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4612. ' var E = $e;',
  4613. ' p = function () {',
  4614. ' E.b = true;',
  4615. ' };',
  4616. ' } else throw $e',
  4617. ' };',
  4618. '};',
  4619. '']),
  4620. LinesToStr([
  4621. '']));
  4622. end;
  4623. procedure TTestModule.TestAnonymousProc_Nested;
  4624. begin
  4625. StartProgram(false);
  4626. Add([
  4627. 'type',
  4628. ' TProc = reference to procedure;',
  4629. ' TObject = class',
  4630. ' i: byte;',
  4631. ' procedure DoIt;',
  4632. ' end;',
  4633. 'procedure TObject.DoIt;',
  4634. 'var',
  4635. ' p: TProc;',
  4636. ' procedure Sub;',
  4637. ' begin',
  4638. ' p:=procedure',
  4639. ' begin',
  4640. ' i:=3;',
  4641. ' Self.i:=4;',
  4642. ' p:=procedure',
  4643. ' procedure SubSub;',
  4644. ' begin',
  4645. ' i:=13;',
  4646. ' Self.i:=14;',
  4647. ' end;',
  4648. ' begin',
  4649. ' i:=13;',
  4650. ' Self.i:=14;',
  4651. ' end;',
  4652. ' end;',
  4653. ' end;',
  4654. 'begin',
  4655. 'end;',
  4656. 'begin']);
  4657. ConvertProgram;
  4658. CheckSource('TestAnonymousProc_Nested',
  4659. LinesToStr([ // statements
  4660. 'rtl.createClass($mod, "TObject", null, function () {',
  4661. ' this.$init = function () {',
  4662. ' this.i = 0;',
  4663. ' };',
  4664. ' this.$final = function () {',
  4665. ' };',
  4666. ' this.DoIt = function () {',
  4667. ' var $Self = this;',
  4668. ' var p = null;',
  4669. ' function Sub() {',
  4670. ' p = function () {',
  4671. ' $Self.i = 3;',
  4672. ' $Self.i = 4;',
  4673. ' p = function () {',
  4674. ' function SubSub() {',
  4675. ' $Self.i = 13;',
  4676. ' $Self.i = 14;',
  4677. ' };',
  4678. ' $Self.i = 13;',
  4679. ' $Self.i = 14;',
  4680. ' };',
  4681. ' };',
  4682. ' };',
  4683. ' };',
  4684. '});',
  4685. '']),
  4686. LinesToStr([
  4687. '']));
  4688. end;
  4689. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4690. begin
  4691. StartProgram(false);
  4692. Add([
  4693. 'type',
  4694. ' TProc = reference to procedure;',
  4695. 'function DoIt: TProc;',
  4696. ' function Sub: TProc;',
  4697. ' begin',
  4698. ' Result:=procedure',
  4699. ' begin',
  4700. ' Sub:=procedure',
  4701. ' procedure SubSub;',
  4702. ' begin',
  4703. ' Result:=nil;',
  4704. ' Sub:=nil;',
  4705. ' DoIt:=nil;',
  4706. ' end;',
  4707. ' begin',
  4708. ' Result:=nil;',
  4709. ' Sub:=nil;',
  4710. ' DoIt:=nil;',
  4711. ' end;',
  4712. ' end;',
  4713. ' end;',
  4714. 'begin',
  4715. 'end;',
  4716. 'begin']);
  4717. ConvertProgram;
  4718. CheckSource('TestAnonymousProc_NestedAssignResult',
  4719. LinesToStr([ // statements
  4720. 'this.DoIt = function () {',
  4721. ' var Result = null;',
  4722. ' function Sub() {',
  4723. ' var Result$1 = null;',
  4724. ' Result$1 = function () {',
  4725. ' Result$1 = function () {',
  4726. ' function SubSub() {',
  4727. ' Result$1 = null;',
  4728. ' Result$1 = null;',
  4729. ' Result = null;',
  4730. ' };',
  4731. ' Result$1 = null;',
  4732. ' Result$1 = null;',
  4733. ' Result = null;',
  4734. ' };',
  4735. ' };',
  4736. ' return Result$1;',
  4737. ' };',
  4738. ' return Result;',
  4739. '};',
  4740. '']),
  4741. LinesToStr([
  4742. '']));
  4743. end;
  4744. procedure TTestModule.TestAnonymousProc_Class;
  4745. begin
  4746. StartProgram(false);
  4747. Add([
  4748. 'type',
  4749. ' TProc = reference to procedure;',
  4750. ' TEvent = procedure of object;',
  4751. ' TObject = class',
  4752. ' Size: word;',
  4753. ' function GetIt: TProc;',
  4754. ' procedure DoIt; virtual; abstract;',
  4755. ' end;',
  4756. 'function TObject.GetIt: TProc;',
  4757. 'begin',
  4758. ' Result:=procedure',
  4759. ' var p: TEvent;',
  4760. ' begin',
  4761. ' Size:=Size;',
  4762. ' Size:=Self.Size;',
  4763. ' p:=@DoIt;',
  4764. ' p:[email protected];',
  4765. ' end;',
  4766. 'end;',
  4767. 'begin']);
  4768. ConvertProgram;
  4769. CheckSource('TestAnonymousProc_Class',
  4770. LinesToStr([ // statements
  4771. 'rtl.createClass($mod, "TObject", null, function () {',
  4772. ' this.$init = function () {',
  4773. ' this.Size = 0;',
  4774. ' };',
  4775. ' this.$final = function () {',
  4776. ' };',
  4777. ' this.GetIt = function () {',
  4778. ' var $Self = this;',
  4779. ' var Result = null;',
  4780. ' Result = function () {',
  4781. ' var p = null;',
  4782. ' $Self.Size = $Self.Size;',
  4783. ' $Self.Size = $Self.Size;',
  4784. ' p = rtl.createCallback($Self, "DoIt");',
  4785. ' p = rtl.createCallback($Self, "DoIt");',
  4786. ' };',
  4787. ' return Result;',
  4788. ' };',
  4789. '});',
  4790. '']),
  4791. LinesToStr([
  4792. '']));
  4793. end;
  4794. procedure TTestModule.TestAnonymousProc_ForLoop;
  4795. begin
  4796. StartProgram(false);
  4797. Add([
  4798. 'type TProc = reference to procedure;',
  4799. 'procedure Foo(p: TProc);',
  4800. 'begin',
  4801. 'end;',
  4802. 'procedure DoIt;',
  4803. 'var i: word;',
  4804. ' a: word;',
  4805. 'begin',
  4806. ' for i:=1 to 10 do begin',
  4807. ' Foo(procedure begin a:=3; end);',
  4808. ' end;',
  4809. 'end;',
  4810. 'begin',
  4811. ' DoIt;']);
  4812. ConvertProgram;
  4813. CheckSource('TestAnonymousProc_ForLoop',
  4814. LinesToStr([ // statements
  4815. 'this.Foo = function (p) {',
  4816. '};',
  4817. 'this.DoIt = function () {',
  4818. ' var i = 0;',
  4819. ' var a = 0;',
  4820. ' for (i = 1; i <= 10; i++) {',
  4821. ' $mod.Foo(function () {',
  4822. ' a = 3;',
  4823. ' });',
  4824. ' };',
  4825. '};',
  4826. '']),
  4827. LinesToStr([
  4828. '$mod.DoIt();'
  4829. ]));
  4830. end;
  4831. procedure TTestModule.TestEnum_Name;
  4832. begin
  4833. StartProgram(false);
  4834. Add('type TMyEnum = (Red, Green, Blue);');
  4835. Add('var e: TMyEnum;');
  4836. Add('var f: TMyEnum = Blue;');
  4837. Add('begin');
  4838. Add(' e:=green;');
  4839. Add(' e:=default(TMyEnum);');
  4840. ConvertProgram;
  4841. CheckSource('TestEnumName',
  4842. LinesToStr([ // statements
  4843. 'this.TMyEnum = {',
  4844. ' "0":"Red",',
  4845. ' Red:0,',
  4846. ' "1":"Green",',
  4847. ' Green:1,',
  4848. ' "2":"Blue",',
  4849. ' Blue:2',
  4850. ' };',
  4851. 'this.e = 0;',
  4852. 'this.f = $mod.TMyEnum.Blue;'
  4853. ]),
  4854. LinesToStr([
  4855. '$mod.e=$mod.TMyEnum.Green;',
  4856. '$mod.e=$mod.TMyEnum.Red;'
  4857. ]));
  4858. end;
  4859. procedure TTestModule.TestEnum_Number;
  4860. begin
  4861. Converter.Options:=Converter.Options+[coEnumNumbers];
  4862. StartProgram(false);
  4863. Add('type TMyEnum = (Red, Green);');
  4864. Add('var');
  4865. Add(' e: TMyEnum;');
  4866. Add(' f: TMyEnum = Green;');
  4867. Add(' i: longint;');
  4868. Add('begin');
  4869. Add(' e:=green;');
  4870. Add(' i:=longint(e);');
  4871. ConvertProgram;
  4872. CheckSource('TestEnumNumber',
  4873. LinesToStr([ // statements
  4874. 'this.TMyEnum = {',
  4875. ' "0":"Red",',
  4876. ' Red:0,',
  4877. ' "1":"Green",',
  4878. ' Green:1',
  4879. ' };',
  4880. 'this.e = 0;',
  4881. 'this.f = 1;',
  4882. 'this.i = 0;'
  4883. ]),
  4884. LinesToStr([
  4885. '$mod.e=1;',
  4886. '$mod.i=$mod.e;'
  4887. ]));
  4888. end;
  4889. procedure TTestModule.TestEnum_ConstFail;
  4890. begin
  4891. StartProgram(false);
  4892. Add([
  4893. 'type TMyEnum = (Red = 100, Green = 101);',
  4894. 'var',
  4895. ' e: TMyEnum;',
  4896. ' f: TMyEnum = Green;',
  4897. 'begin',
  4898. ' e:=green;']);
  4899. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] enum const',3002);
  4900. ConvertProgram;
  4901. end;
  4902. procedure TTestModule.TestEnum_Functions;
  4903. begin
  4904. StartProgram(false);
  4905. Add([
  4906. 'type TMyEnum = (Red, Green);',
  4907. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4908. 'var',
  4909. ' v: longint;',
  4910. ' s: string;',
  4911. 'begin',
  4912. ' val(s,e,v);',
  4913. ' val(s,e,i);',
  4914. 'end;',
  4915. 'var',
  4916. ' e: TMyEnum;',
  4917. ' i: longint;',
  4918. ' s: string;',
  4919. ' b: boolean;',
  4920. 'begin',
  4921. ' i:=ord(red);',
  4922. ' i:=ord(green);',
  4923. ' i:=ord(e);',
  4924. ' i:=ord(b);',
  4925. ' e:=low(tmyenum);',
  4926. ' e:=low(e);',
  4927. ' b:=low(boolean);',
  4928. ' e:=high(tmyenum);',
  4929. ' e:=high(e);',
  4930. ' b:=high(boolean);',
  4931. ' e:=pred(green);',
  4932. ' e:=pred(e);',
  4933. ' b:=pred(b);',
  4934. ' e:=succ(red);',
  4935. ' e:=succ(e);',
  4936. ' b:=succ(b);',
  4937. ' e:=tmyenum(1);',
  4938. ' e:=tmyenum(i);',
  4939. ' s:=str(e);',
  4940. ' str(e,s);',
  4941. ' str(red,s);',
  4942. ' s:=str(e:3);',
  4943. ' writestr(s,e:3,red);',
  4944. ' val(s,e,i);',
  4945. ' e:=TMyEnum(i);',
  4946. ' i:=longint(e);']);
  4947. ConvertProgram;
  4948. CheckSource('TestEnum_Functions',
  4949. LinesToStr([ // statements
  4950. 'this.TMyEnum = {',
  4951. ' "0":"Red",',
  4952. ' Red:0,',
  4953. ' "1":"Green",',
  4954. ' Green:1',
  4955. ' };',
  4956. 'this.DoIt = function (e, i) {',
  4957. ' var v = 0;',
  4958. ' var s = "";',
  4959. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4960. ' v = w;',
  4961. ' }));',
  4962. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4963. '};',
  4964. 'this.e = 0;',
  4965. 'this.i = 0;',
  4966. 'this.s = "";',
  4967. 'this.b = false;',
  4968. '']),
  4969. LinesToStr([
  4970. '$mod.i=$mod.TMyEnum.Red;',
  4971. '$mod.i=$mod.TMyEnum.Green;',
  4972. '$mod.i=$mod.e;',
  4973. '$mod.i=$mod.b+0;',
  4974. '$mod.e=$mod.TMyEnum.Red;',
  4975. '$mod.e=$mod.TMyEnum.Red;',
  4976. '$mod.b=false;',
  4977. '$mod.e=$mod.TMyEnum.Green;',
  4978. '$mod.e=$mod.TMyEnum.Green;',
  4979. '$mod.b=true;',
  4980. '$mod.e=$mod.TMyEnum.Green-1;',
  4981. '$mod.e=$mod.e-1;',
  4982. '$mod.b=false;',
  4983. '$mod.e=$mod.TMyEnum.Red+1;',
  4984. '$mod.e=$mod.e+1;',
  4985. '$mod.b=true;',
  4986. '$mod.e=1;',
  4987. '$mod.e=$mod.i;',
  4988. '$mod.s = $mod.TMyEnum[$mod.e];',
  4989. '$mod.s = $mod.TMyEnum[$mod.e];',
  4990. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  4991. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  4992. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  4993. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  4994. ' $mod.i = v;',
  4995. '});',
  4996. '$mod.e=$mod.i;',
  4997. '$mod.i=$mod.e;',
  4998. '']));
  4999. end;
  5000. procedure TTestModule.TestEnum_AsParams;
  5001. begin
  5002. StartProgram(false);
  5003. Add('type TEnum = (Red,Blue);');
  5004. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5005. Add('var vJ: TEnum;');
  5006. Add('begin');
  5007. Add(' vg:=vg;');
  5008. Add(' vj:=vh;');
  5009. Add(' vi:=vi;');
  5010. Add(' doit(vg,vg,vg);');
  5011. Add(' doit(vh,vh,vj);');
  5012. Add(' doit(vi,vi,vi);');
  5013. Add(' doit(vj,vj,vj);');
  5014. Add('end;');
  5015. Add('var i: TEnum;');
  5016. Add('begin');
  5017. Add(' doit(i,i,i);');
  5018. ConvertProgram;
  5019. CheckSource('TestEnum_AsParams',
  5020. LinesToStr([ // statements
  5021. 'this.TEnum = {',
  5022. ' "0": "Red",',
  5023. ' Red: 0,',
  5024. ' "1": "Blue",',
  5025. ' Blue: 1',
  5026. '};',
  5027. 'this.DoIt = function (vG,vH,vI) {',
  5028. ' var vJ = 0;',
  5029. ' vG = vG;',
  5030. ' vJ = vH;',
  5031. ' vI.set(vI.get());',
  5032. ' $mod.DoIt(vG, vG, {',
  5033. ' get: function () {',
  5034. ' return vG;',
  5035. ' },',
  5036. ' set: function (v) {',
  5037. ' vG = v;',
  5038. ' }',
  5039. ' });',
  5040. ' $mod.DoIt(vH, vH, {',
  5041. ' get: function () {',
  5042. ' return vJ;',
  5043. ' },',
  5044. ' set: function (v) {',
  5045. ' vJ = v;',
  5046. ' }',
  5047. ' });',
  5048. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5049. ' $mod.DoIt(vJ, vJ, {',
  5050. ' get: function () {',
  5051. ' return vJ;',
  5052. ' },',
  5053. ' set: function (v) {',
  5054. ' vJ = v;',
  5055. ' }',
  5056. ' });',
  5057. '};',
  5058. 'this.i = 0;'
  5059. ]),
  5060. LinesToStr([
  5061. '$mod.DoIt($mod.i,$mod.i,{',
  5062. ' p: $mod,',
  5063. ' get: function () {',
  5064. ' return this.p.i;',
  5065. ' },',
  5066. ' set: function (v) {',
  5067. ' this.p.i = v;',
  5068. ' }',
  5069. '});'
  5070. ]));
  5071. end;
  5072. procedure TTestModule.TestEnumRange_Array;
  5073. begin
  5074. StartProgram(false);
  5075. Add([
  5076. 'type',
  5077. ' TEnum = (Red, Green, Blue);',
  5078. ' TEnumRg = green..blue;',
  5079. ' TArr = array[TEnumRg] of byte;',
  5080. ' TArr2 = array[green..blue] of byte;',
  5081. 'var',
  5082. ' a: TArr;',
  5083. ' b: TArr = (3,4);',
  5084. ' c: TArr2 = (5,6);',
  5085. 'begin',
  5086. ' a[green] := b[blue];',
  5087. ' c[green] := c[blue];',
  5088. '']);
  5089. ConvertProgram;
  5090. CheckSource('TestEnumRange_Array',
  5091. LinesToStr([ // statements
  5092. 'this.TEnum = {',
  5093. ' "0": "Red",',
  5094. ' Red: 0,',
  5095. ' "1": "Green",',
  5096. ' Green: 1,',
  5097. ' "2": "Blue",',
  5098. ' Blue: 2',
  5099. '};',
  5100. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5101. 'this.b = [3, 4];',
  5102. 'this.c = [5, 6];',
  5103. '']),
  5104. LinesToStr([
  5105. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5106. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5107. '']));
  5108. end;
  5109. procedure TTestModule.TestEnum_ForIn;
  5110. begin
  5111. StartProgram(false);
  5112. Add([
  5113. 'type',
  5114. ' TEnum = (Red, Green, Blue);',
  5115. ' TEnumRg = green..blue;',
  5116. ' TArr = array[TEnum] of byte;',
  5117. ' TArrRg = array[TEnumRg] of byte;',
  5118. 'var',
  5119. ' e: TEnum;',
  5120. ' a1: TArr = (3,4,5);',
  5121. ' a2: TArrRg = (11,12);',
  5122. ' b: byte;',
  5123. 'begin',
  5124. ' for e in TEnum do ;',
  5125. ' for e in TEnumRg do ;',
  5126. ' for e in TArr do ;',
  5127. ' for e in TArrRg do ;',
  5128. ' for b in a1 do ;',
  5129. ' for b in a2 do ;',
  5130. '']);
  5131. ConvertProgram;
  5132. CheckSource('TestEnum_ForIn',
  5133. LinesToStr([ // statements
  5134. 'this.TEnum = {',
  5135. ' "0": "Red",',
  5136. ' Red: 0,',
  5137. ' "1": "Green",',
  5138. ' Green: 1,',
  5139. ' "2": "Blue",',
  5140. ' Blue: 2',
  5141. '};',
  5142. 'this.e = 0;',
  5143. 'this.a1 = [3, 4, 5];',
  5144. 'this.a2 = [11, 12];',
  5145. 'this.b = 0;',
  5146. '']),
  5147. LinesToStr([
  5148. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5149. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5150. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5151. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5152. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  5153. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  5154. '']));
  5155. end;
  5156. procedure TTestModule.TestEnum_ScopedNumber;
  5157. begin
  5158. Converter.Options:=Converter.Options+[coEnumNumbers];
  5159. StartProgram(false);
  5160. Add([
  5161. 'type',
  5162. ' TEnum = (Red, Green);',
  5163. 'var',
  5164. ' e: TEnum;',
  5165. 'begin',
  5166. ' e:=TEnum.Green;',
  5167. '']);
  5168. ConvertProgram;
  5169. CheckSource('TestEnum_ScopedNumber',
  5170. LinesToStr([ // statements
  5171. 'this.TEnum = {',
  5172. ' "0": "Red",',
  5173. ' Red: 0,',
  5174. ' "1": "Green",',
  5175. ' Green: 1',
  5176. '};',
  5177. 'this.e = 0;',
  5178. '']),
  5179. LinesToStr([
  5180. '$mod.e = 1;']));
  5181. end;
  5182. procedure TTestModule.TestEnum_InFunction;
  5183. begin
  5184. StartProgram(false);
  5185. Add([
  5186. 'const TEnum = 3;',
  5187. 'procedure DoIt;',
  5188. 'type',
  5189. ' TEnum = (Red, Green, Blue);',
  5190. ' procedure Sub;',
  5191. ' type',
  5192. ' TEnumSub = (Left, Right);',
  5193. ' var',
  5194. ' es: TEnumSub;',
  5195. ' begin',
  5196. ' es:=Left;',
  5197. ' end;',
  5198. 'var',
  5199. ' e, e2: TEnum;',
  5200. 'begin',
  5201. ' if e in [red,blue] then e2:=e;',
  5202. 'end;',
  5203. 'begin']);
  5204. ConvertProgram;
  5205. CheckSource('TestEnum_InFunction',
  5206. LinesToStr([ // statements
  5207. 'this.TEnum = 3;',
  5208. 'var TEnum$1 = {',
  5209. ' "0":"Red",',
  5210. ' Red:0,',
  5211. ' "1":"Green",',
  5212. ' Green:1,',
  5213. ' "2":"Blue",',
  5214. ' Blue:2',
  5215. ' };',
  5216. 'var TEnumSub = {',
  5217. ' "0": "Left",',
  5218. ' Left: 0,',
  5219. ' "1": "Right",',
  5220. ' Right: 1',
  5221. '};',
  5222. 'this.DoIt = function () {',
  5223. ' function Sub() {',
  5224. ' var es = 0;',
  5225. ' es = TEnumSub.Left;',
  5226. ' };',
  5227. ' var e = 0;',
  5228. ' var e2 = 0;',
  5229. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5230. '};',
  5231. '']),
  5232. LinesToStr([
  5233. '']));
  5234. end;
  5235. procedure TTestModule.TestSet_Enum;
  5236. begin
  5237. StartProgram(false);
  5238. Add([
  5239. 'type',
  5240. ' TColor = (Red, Green, Blue);',
  5241. ' TColors = set of TColor;',
  5242. 'var',
  5243. ' c: TColor;',
  5244. ' s: TColors;',
  5245. ' t: TColors = [];',
  5246. ' u: TColors = [Red];',
  5247. 'begin',
  5248. ' s:=[];',
  5249. ' s:=[Green];',
  5250. ' s:=[Green,Blue];',
  5251. ' s:=[Red..Blue];',
  5252. ' s:=[Red,Green..Blue];',
  5253. ' s:=[Red,c];',
  5254. ' s:=t;',
  5255. ' s:=default(TColors);',
  5256. '']);
  5257. ConvertProgram;
  5258. CheckSource('TestSet',
  5259. LinesToStr([ // statements
  5260. 'this.TColor = {',
  5261. ' "0":"Red",',
  5262. ' Red:0,',
  5263. ' "1":"Green",',
  5264. ' Green:1,',
  5265. ' "2":"Blue",',
  5266. ' Blue:2',
  5267. ' };',
  5268. 'this.c = 0;',
  5269. 'this.s = {};',
  5270. 'this.t = {};',
  5271. 'this.u = rtl.createSet($mod.TColor.Red);'
  5272. ]),
  5273. LinesToStr([
  5274. '$mod.s={};',
  5275. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5276. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5277. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5278. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5279. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5280. '$mod.s=rtl.refSet($mod.t);',
  5281. '$mod.s={};',
  5282. '']));
  5283. end;
  5284. procedure TTestModule.TestSet_Operators;
  5285. begin
  5286. StartProgram(false);
  5287. Add('type');
  5288. Add(' TColor = (Red, Green, Blue);');
  5289. Add(' TColors = set of tcolor;');
  5290. Add('var');
  5291. Add(' vC: TColor;');
  5292. Add(' vS: TColors;');
  5293. Add(' vT: TColors;');
  5294. Add(' vU: TColors;');
  5295. Add(' B: boolean;');
  5296. Add('begin');
  5297. Add(' include(vs,green);');
  5298. Add(' exclude(vs,vc);');
  5299. Add(' vs:=vt+vu;');
  5300. Add(' vs:=vt+[red];');
  5301. Add(' vs:=[red]+vt;');
  5302. Add(' vs:=[red]+[green];');
  5303. Add(' vs:=vt-vu;');
  5304. Add(' vs:=vt-[red];');
  5305. Add(' vs:=[red]-vt;');
  5306. Add(' vs:=[red]-[green];');
  5307. Add(' vs:=vt*vu;');
  5308. Add(' vs:=vt*[red];');
  5309. Add(' vs:=[red]*vt;');
  5310. Add(' vs:=[red]*[green];');
  5311. Add(' vs:=vt><vu;');
  5312. Add(' vs:=vt><[red];');
  5313. Add(' vs:=[red]><vt;');
  5314. Add(' vs:=[red]><[green];');
  5315. Add(' b:=vt=vu;');
  5316. Add(' b:=vt=[red];');
  5317. Add(' b:=[red]=vt;');
  5318. Add(' b:=[red]=[green];');
  5319. Add(' b:=vt<>vu;');
  5320. Add(' b:=vt<>[red];');
  5321. Add(' b:=[red]<>vt;');
  5322. Add(' b:=[red]<>[green];');
  5323. Add(' b:=vt<=vu;');
  5324. Add(' b:=vt<=[red];');
  5325. Add(' b:=[red]<=vt;');
  5326. Add(' b:=[red]<=[green];');
  5327. Add(' b:=vt>=vu;');
  5328. Add(' b:=vt>=[red];');
  5329. Add(' b:=[red]>=vt;');
  5330. Add(' b:=[red]>=[green];');
  5331. ConvertProgram;
  5332. CheckSource('TestSet_Operators',
  5333. LinesToStr([ // statements
  5334. 'this.TColor = {',
  5335. ' "0":"Red",',
  5336. ' Red:0,',
  5337. ' "1":"Green",',
  5338. ' Green:1,',
  5339. ' "2":"Blue",',
  5340. ' Blue:2',
  5341. ' };',
  5342. 'this.vC = 0;',
  5343. 'this.vS = {};',
  5344. 'this.vT = {};',
  5345. 'this.vU = {};',
  5346. 'this.B = false;'
  5347. ]),
  5348. LinesToStr([
  5349. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5350. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5351. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5352. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5353. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5354. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5355. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5356. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5357. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5358. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5359. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5360. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5361. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5362. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5363. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5364. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5365. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5366. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5367. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5368. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5369. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5370. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5371. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5372. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5373. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5374. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5375. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5376. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5377. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5378. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5379. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5380. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5381. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5382. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5383. '']));
  5384. end;
  5385. procedure TTestModule.TestSet_Operator_In;
  5386. begin
  5387. StartProgram(false);
  5388. Add('type');
  5389. Add(' TColor = (Red, Green, Blue);');
  5390. Add(' TColors = set of tcolor;');
  5391. Add('var');
  5392. Add(' vC: tcolor;');
  5393. Add(' vT: tcolors;');
  5394. Add(' B: boolean;');
  5395. Add('begin');
  5396. Add(' b:=red in vt;');
  5397. Add(' b:=vc in vt;');
  5398. Add(' b:=green in [red..blue];');
  5399. Add(' b:=vc in [red..blue];');
  5400. Add(' ');
  5401. Add(' if red in vt then ;');
  5402. Add(' while vC in vt do ;');
  5403. Add(' repeat');
  5404. Add(' until vC in vt;');
  5405. ConvertProgram;
  5406. CheckSource('TestSet_Operator_In',
  5407. LinesToStr([ // statements
  5408. 'this.TColor = {',
  5409. ' "0":"Red",',
  5410. ' Red:0,',
  5411. ' "1":"Green",',
  5412. ' Green:1,',
  5413. ' "2":"Blue",',
  5414. ' Blue:2',
  5415. ' };',
  5416. 'this.vC = 0;',
  5417. 'this.vT = {};',
  5418. 'this.B = false;'
  5419. ]),
  5420. LinesToStr([
  5421. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5422. '$mod.B = $mod.vC in $mod.vT;',
  5423. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5424. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5425. 'if ($mod.TColor.Red in $mod.vT) ;',
  5426. 'while ($mod.vC in $mod.vT) {',
  5427. '};',
  5428. 'do {',
  5429. '} while (!($mod.vC in $mod.vT));',
  5430. '']));
  5431. end;
  5432. procedure TTestModule.TestSet_Functions;
  5433. begin
  5434. StartProgram(false);
  5435. Add('type');
  5436. Add(' TMyEnum = (Red, Green);');
  5437. Add(' TMyEnums = set of TMyEnum;');
  5438. Add('var');
  5439. Add(' e: TMyEnum;');
  5440. Add(' s: TMyEnums;');
  5441. Add('begin');
  5442. Add(' e:=Low(TMyEnums);');
  5443. Add(' e:=Low(s);');
  5444. Add(' e:=High(TMyEnums);');
  5445. Add(' e:=High(s);');
  5446. ConvertProgram;
  5447. CheckSource('TestSetFunctions',
  5448. LinesToStr([ // statements
  5449. 'this.TMyEnum = {',
  5450. ' "0":"Red",',
  5451. ' Red:0,',
  5452. ' "1":"Green",',
  5453. ' Green:1',
  5454. ' };',
  5455. 'this.e = 0;',
  5456. 'this.s = {};'
  5457. ]),
  5458. LinesToStr([
  5459. '$mod.e=$mod.TMyEnum.Red;',
  5460. '$mod.e=$mod.TMyEnum.Red;',
  5461. '$mod.e=$mod.TMyEnum.Green;',
  5462. '$mod.e=$mod.TMyEnum.Green;',
  5463. '']));
  5464. end;
  5465. procedure TTestModule.TestSet_PassAsArgClone;
  5466. begin
  5467. StartProgram(false);
  5468. Add('type');
  5469. Add(' TMyEnum = (Red, Green);');
  5470. Add(' TMyEnums = set of TMyEnum;');
  5471. Add('procedure DoDefault(s: tmyenums); begin end;');
  5472. Add('procedure DoConst(const s: tmyenums); begin end;');
  5473. Add('var');
  5474. Add(' aSet: tmyenums;');
  5475. Add('begin');
  5476. Add(' dodefault(aset);');
  5477. Add(' doconst(aset);');
  5478. ConvertProgram;
  5479. CheckSource('TestSetFunctions',
  5480. LinesToStr([ // statements
  5481. 'this.TMyEnum = {',
  5482. ' "0":"Red",',
  5483. ' Red:0,',
  5484. ' "1":"Green",',
  5485. ' Green:1',
  5486. ' };',
  5487. 'this.DoDefault = function (s) {',
  5488. '};',
  5489. 'this.DoConst = function (s) {',
  5490. '};',
  5491. 'this.aSet = {};'
  5492. ]),
  5493. LinesToStr([
  5494. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5495. '$mod.DoConst($mod.aSet);',
  5496. '']));
  5497. end;
  5498. procedure TTestModule.TestSet_AsParams;
  5499. begin
  5500. StartProgram(false);
  5501. Add([
  5502. 'type TEnum = (Red,Blue);',
  5503. 'type TEnums = set of TEnum;',
  5504. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5505. 'var vJ: TEnums;',
  5506. 'begin',
  5507. ' Include(vg,red);',
  5508. ' Include(result,blue);',
  5509. ' vg:=vg;',
  5510. ' vj:=vh;',
  5511. ' vi:=vi;',
  5512. ' doit(vg,vg,vg);',
  5513. ' doit(vh,vh,vj);',
  5514. ' doit(vi,vi,vi);',
  5515. ' doit(vj,vj,vj);',
  5516. 'end;',
  5517. 'var i: TEnums;',
  5518. 'begin',
  5519. ' doit(i,i,i);']);
  5520. ConvertProgram;
  5521. CheckSource('TestSet_AsParams',
  5522. LinesToStr([ // statements
  5523. 'this.TEnum = {',
  5524. ' "0": "Red",',
  5525. ' Red: 0,',
  5526. ' "1": "Blue",',
  5527. ' Blue: 1',
  5528. '};',
  5529. 'this.DoIt = function (vG,vH,vI) {',
  5530. ' var Result = {};',
  5531. ' var vJ = {};',
  5532. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5533. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5534. ' vG = rtl.refSet(vG);',
  5535. ' vJ = rtl.refSet(vH);',
  5536. ' vI.set(rtl.refSet(vI.get()));',
  5537. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5538. ' get: function () {',
  5539. ' return vG;',
  5540. ' },',
  5541. ' set: function (v) {',
  5542. ' vG = v;',
  5543. ' }',
  5544. ' });',
  5545. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5546. ' get: function () {',
  5547. ' return vJ;',
  5548. ' },',
  5549. ' set: function (v) {',
  5550. ' vJ = v;',
  5551. ' }',
  5552. ' });',
  5553. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5554. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5555. ' get: function () {',
  5556. ' return vJ;',
  5557. ' },',
  5558. ' set: function (v) {',
  5559. ' vJ = v;',
  5560. ' }',
  5561. ' });',
  5562. ' return Result;',
  5563. '};',
  5564. 'this.i = {};'
  5565. ]),
  5566. LinesToStr([
  5567. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5568. ' p: $mod,',
  5569. ' get: function () {',
  5570. ' return this.p.i;',
  5571. ' },',
  5572. ' set: function (v) {',
  5573. ' this.p.i = v;',
  5574. ' }',
  5575. '});'
  5576. ]));
  5577. end;
  5578. procedure TTestModule.TestSet_Property;
  5579. begin
  5580. StartProgram(false);
  5581. Add('type');
  5582. Add(' TEnum = (Red,Blue);');
  5583. Add(' TEnums = set of TEnum;');
  5584. Add(' TObject = class');
  5585. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5586. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5587. Add(' property Colors: TEnums read GetColors write SetColors;');
  5588. Add(' end;');
  5589. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5590. Add('begin end;');
  5591. Add('var Obj: TObject;');
  5592. Add('begin');
  5593. Add(' Include(Obj.Colors,Red);');
  5594. Add(' Exclude(Obj.Colors,Red);');
  5595. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5596. ConvertProgram;
  5597. CheckSource('TestSet_Property',
  5598. LinesToStr([ // statements
  5599. 'this.TEnum = {',
  5600. ' "0": "Red",',
  5601. ' Red: 0,',
  5602. ' "1": "Blue",',
  5603. ' Blue: 1',
  5604. '};',
  5605. 'rtl.createClass($mod, "TObject", null, function () {',
  5606. ' this.$init = function () {',
  5607. ' };',
  5608. ' this.$final = function () {',
  5609. ' };',
  5610. '});',
  5611. 'this.DoIt = function (i, j, k, l) {',
  5612. '};',
  5613. 'this.Obj = null;',
  5614. '']),
  5615. LinesToStr([
  5616. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5617. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5618. '']));
  5619. end;
  5620. procedure TTestModule.TestSet_EnumConst;
  5621. begin
  5622. StartProgram(false);
  5623. Add([
  5624. 'type',
  5625. ' TEnum = (Red,Blue);',
  5626. ' TEnums = set of TEnum;',
  5627. 'const',
  5628. ' Orange = red;',
  5629. 'var',
  5630. ' Enum: tenum;',
  5631. ' Enums: tenums;',
  5632. 'begin',
  5633. ' Include(enums,orange);',
  5634. ' Exclude(enums,orange);',
  5635. ' if orange in enums then;',
  5636. ' if orange in [orange,red] then;']);
  5637. ConvertProgram;
  5638. CheckSource('TestSet_EnumConst',
  5639. LinesToStr([ // statements
  5640. 'this.TEnum = {',
  5641. ' "0": "Red",',
  5642. ' Red: 0,',
  5643. ' "1": "Blue",',
  5644. ' Blue: 1',
  5645. '};',
  5646. 'this.Orange = $mod.TEnum.Red;',
  5647. 'this.Enum = 0;',
  5648. 'this.Enums = {};',
  5649. '']),
  5650. LinesToStr([
  5651. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5652. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5653. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5654. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5655. '']));
  5656. end;
  5657. procedure TTestModule.TestSet_IntConst;
  5658. begin
  5659. StartProgram(false);
  5660. Add([
  5661. 'type',
  5662. ' TEnums = set of Byte;',
  5663. 'const',
  5664. ' Orange = 0;',
  5665. 'var',
  5666. ' Enum: byte;',
  5667. ' Enums: tenums;',
  5668. 'begin',
  5669. ' Enums:=[];',
  5670. ' Enums:=[0];',
  5671. ' Enums:=[1..2];',
  5672. //' Include(enums,orange);',
  5673. //' Exclude(enums,orange);',
  5674. ' if orange in enums then;',
  5675. ' if orange in [orange,1] then;']);
  5676. ConvertProgram;
  5677. CheckSource('TestSet_IntConst',
  5678. LinesToStr([ // statements
  5679. 'this.Orange = 0;',
  5680. 'this.Enum = 0;',
  5681. 'this.Enums = {};',
  5682. '']),
  5683. LinesToStr([
  5684. '$mod.Enums = {};',
  5685. '$mod.Enums = rtl.createSet(0);',
  5686. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5687. 'if (0 in $mod.Enums) ;',
  5688. 'if (0 in rtl.createSet(0, 1)) ;',
  5689. '']));
  5690. end;
  5691. procedure TTestModule.TestSet_AnonymousEnumType;
  5692. begin
  5693. StartProgram(false);
  5694. Add('type');
  5695. Add(' TFlags = set of (red, green);');
  5696. Add('const');
  5697. Add(' favorite = red;');
  5698. Add('var');
  5699. Add(' f: TFlags;');
  5700. Add(' i: longint;');
  5701. Add('begin');
  5702. Add(' Include(f,red);');
  5703. Add(' Include(f,favorite);');
  5704. Add(' i:=ord(red);');
  5705. Add(' i:=ord(favorite);');
  5706. Add(' i:=ord(low(TFlags));');
  5707. Add(' i:=ord(low(f));');
  5708. Add(' i:=ord(low(favorite));');
  5709. Add(' i:=ord(high(TFlags));');
  5710. Add(' i:=ord(high(f));');
  5711. Add(' i:=ord(high(favorite));');
  5712. Add(' f:=[green,favorite];');
  5713. ConvertProgram;
  5714. CheckSource('TestSet_AnonymousEnumType',
  5715. LinesToStr([ // statements
  5716. 'this.TFlags$a = {',
  5717. ' "0": "red",',
  5718. ' red: 0,',
  5719. ' "1": "green",',
  5720. ' green: 1',
  5721. '};',
  5722. 'this.favorite = $mod.TFlags$a.red;',
  5723. 'this.f = {};',
  5724. 'this.i = 0;',
  5725. '']),
  5726. LinesToStr([
  5727. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5728. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5729. '$mod.i = $mod.TFlags$a.red;',
  5730. '$mod.i = $mod.TFlags$a.red;',
  5731. '$mod.i = $mod.TFlags$a.red;',
  5732. '$mod.i = $mod.TFlags$a.red;',
  5733. '$mod.i = $mod.TFlags$a.red;',
  5734. '$mod.i = $mod.TFlags$a.green;',
  5735. '$mod.i = $mod.TFlags$a.green;',
  5736. '$mod.i = $mod.TFlags$a.green;',
  5737. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5738. '']));
  5739. end;
  5740. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5741. begin
  5742. exit;
  5743. StartProgram(false);
  5744. Add([
  5745. 'type',
  5746. ' TAtoZ = ''A''..''Z'';',
  5747. ' TSetOfAZ = set of TAtoZ;',
  5748. 'var',
  5749. ' c: char;',
  5750. ' a: TAtoZ;',
  5751. ' s: TSetOfAZ = [''P'',''A''];',
  5752. ' i: longint;',
  5753. 'begin',
  5754. ' Include(s,''S'');',
  5755. ' Include(s,c);',
  5756. ' Include(s,a);',
  5757. ' c:=low(TAtoZ);',
  5758. ' i:=ord(low(TAtoZ));',
  5759. ' a:=high(TAtoZ);',
  5760. ' a:=high(TSetOfAtoZ);',
  5761. ' s:=[a,c,''M''];',
  5762. '']);
  5763. ConvertProgram;
  5764. CheckSource('TestSet_AnonymousEnumTypeChar',
  5765. LinesToStr([ // statements
  5766. '']),
  5767. LinesToStr([
  5768. '']));
  5769. end;
  5770. procedure TTestModule.TestSet_ConstEnum;
  5771. begin
  5772. StartProgram(false);
  5773. Add([
  5774. 'type',
  5775. ' TEnum = (red,blue,green);',
  5776. ' TEnums = set of TEnum;',
  5777. 'const',
  5778. ' teAny = [low(TEnum)..high(TEnum)];',
  5779. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5780. 'var',
  5781. ' e: TEnum;',
  5782. ' s: TEnums;',
  5783. 'begin',
  5784. ' if blue in teAny then;',
  5785. ' if blue in teAny+[e] then;',
  5786. ' if blue in teAny+teRedBlue then;',
  5787. ' if e in [red,blue] then;',
  5788. ' s:=teAny;',
  5789. ' s:=teAny+[e];',
  5790. ' s:=[e]+teAny;',
  5791. ' s:=teAny+teRedBlue;',
  5792. ' s:=teAny+teRedBlue+[e];',
  5793. '']);
  5794. ConvertProgram;
  5795. CheckSource('TestSet_ConstEnum',
  5796. LinesToStr([ // statements
  5797. 'this.TEnum = {',
  5798. ' "0": "red",',
  5799. ' red: 0,',
  5800. ' "1": "blue",',
  5801. ' blue: 1,',
  5802. ' "2": "green",',
  5803. ' green: 2',
  5804. '};',
  5805. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5806. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5807. 'this.e = 0;',
  5808. 'this.s = {};',
  5809. '']),
  5810. LinesToStr([
  5811. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5812. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5813. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5814. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5815. '$mod.s = rtl.refSet($mod.teAny);',
  5816. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5817. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5818. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5819. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5820. '']));
  5821. end;
  5822. procedure TTestModule.TestSet_ConstChar;
  5823. begin
  5824. StartProgram(false);
  5825. Add([
  5826. 'const',
  5827. ' LowChars = [''a''..''z''];',
  5828. ' Chars = LowChars+[''A''..''Z''];',
  5829. ' sc = [''А'', ''Я''];',
  5830. 'var',
  5831. ' c: char;',
  5832. ' s: string;',
  5833. 'begin',
  5834. ' if c in lowchars then ;',
  5835. ' if ''a'' in lowchars then ;',
  5836. ' if s[1] in lowchars then ;',
  5837. ' if c in chars then ;',
  5838. ' if c in [''a''..''z'',''_''] then ;',
  5839. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5840. ' if ''Я'' in sc then ;',
  5841. '']);
  5842. ConvertProgram;
  5843. CheckSource('TestSet_ConstChar',
  5844. LinesToStr([ // statements
  5845. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5846. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5847. 'this.sc = rtl.createSet(1040, 1071);',
  5848. 'this.c = "";',
  5849. 'this.s = "";',
  5850. '']),
  5851. LinesToStr([
  5852. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5853. 'if (97 in $mod.LowChars) ;',
  5854. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5855. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5856. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5857. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5858. 'if (1071 in $mod.sc) ;',
  5859. '']));
  5860. end;
  5861. procedure TTestModule.TestSet_ConstInt;
  5862. begin
  5863. StartProgram(false);
  5864. Add([
  5865. 'const',
  5866. ' Months = [1..12];',
  5867. ' Mirror = [-12..-1]+Months;',
  5868. 'var',
  5869. ' i: smallint;',
  5870. 'begin',
  5871. ' if 3 in Months then;',
  5872. ' if i in Months+[i] then;',
  5873. ' if i in Months+Mirror then;',
  5874. ' if i in [4..6,8] then;',
  5875. '']);
  5876. ConvertProgram;
  5877. CheckSource('TestSet_ConstInt',
  5878. LinesToStr([ // statements
  5879. 'this.Months = rtl.createSet(null, 1, 12);',
  5880. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5881. 'this.i = 0;',
  5882. '']),
  5883. LinesToStr([
  5884. 'if (3 in $mod.Months) ;',
  5885. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5886. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5887. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5888. '']));
  5889. end;
  5890. procedure TTestModule.TestSet_InFunction;
  5891. begin
  5892. StartProgram(false);
  5893. Add([
  5894. 'const',
  5895. ' TEnum = 3;',
  5896. ' TSetOfEnum = 4;',
  5897. ' TSetOfAno = 5;',
  5898. 'procedure DoIt;',
  5899. 'type',
  5900. ' TEnum = (red, blue);',
  5901. ' TSetOfEnum = set of TEnum;',
  5902. ' TSetOfAno = set of (up,down);',
  5903. 'var',
  5904. ' e: TEnum;',
  5905. ' se: TSetOfEnum;',
  5906. ' sa: TSetOfAno;',
  5907. 'begin',
  5908. ' se:=[e];',
  5909. ' sa:=[up];',
  5910. 'end;',
  5911. 'begin',
  5912. '']);
  5913. ConvertProgram;
  5914. CheckSource('TestSet_InFunction',
  5915. LinesToStr([ // statements
  5916. 'this.TEnum = 3;',
  5917. 'this.TSetOfEnum = 4;',
  5918. 'this.TSetOfAno = 5;',
  5919. 'var TEnum$1 = {',
  5920. ' "0": "red",',
  5921. ' red: 0,',
  5922. ' "1": "blue",',
  5923. ' blue: 1',
  5924. '};',
  5925. 'var TSetOfAno$a = {',
  5926. ' "0": "up",',
  5927. ' up: 0,',
  5928. ' "1": "down",',
  5929. ' down: 1',
  5930. '};',
  5931. 'this.DoIt = function () {',
  5932. ' var e = 0;',
  5933. ' var se = {};',
  5934. ' var sa = {};',
  5935. ' se = rtl.createSet(e);',
  5936. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5937. '};',
  5938. '']),
  5939. LinesToStr([
  5940. '']));
  5941. end;
  5942. procedure TTestModule.TestSet_ForIn;
  5943. begin
  5944. StartProgram(false);
  5945. Add([
  5946. 'type',
  5947. ' TEnum = (Red, Green, Blue);',
  5948. ' TEnumRg = green..blue;',
  5949. ' TSetOfEnum = set of TEnum;',
  5950. ' TSetOfEnumRg = set of TEnumRg;',
  5951. 'var',
  5952. ' e, e2: TEnum;',
  5953. ' er: TEnum;',
  5954. ' s: TSetOfEnum;',
  5955. 'begin',
  5956. ' for e in TSetOfEnum do ;',
  5957. ' for e in TSetOfEnumRg do ;',
  5958. ' for e in [] do e2:=e;',
  5959. ' for e in [red..green] do e2:=e;',
  5960. ' for e in [green,blue] do e2:=e;',
  5961. ' for e in [red,blue] do e2:=e;',
  5962. ' for e in s do e2:=e;',
  5963. ' for er in TSetOfEnumRg do ;',
  5964. '']);
  5965. ConvertProgram;
  5966. CheckSource('TestSet_ForIn',
  5967. LinesToStr([ // statements
  5968. 'this.TEnum = {',
  5969. ' "0":"Red",',
  5970. ' Red:0,',
  5971. ' "1":"Green",',
  5972. ' Green:1,',
  5973. ' "2":"Blue",',
  5974. ' Blue:2',
  5975. ' };',
  5976. 'this.e = 0;',
  5977. 'this.e2 = 0;',
  5978. 'this.er = 0;',
  5979. 'this.s = {};',
  5980. '']),
  5981. LinesToStr([
  5982. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5983. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5984. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5985. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5986. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5987. 'for (var $l1 in $mod.s){',
  5988. ' $mod.e = +$l1;',
  5989. ' $mod.e2 = $mod.e;',
  5990. '};',
  5991. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  5992. '']));
  5993. end;
  5994. procedure TTestModule.TestNestBegin;
  5995. begin
  5996. StartProgram(false);
  5997. Add('begin');
  5998. Add(' begin');
  5999. Add(' begin');
  6000. Add(' end;');
  6001. Add(' begin');
  6002. Add(' if true then ;');
  6003. Add(' end;');
  6004. Add(' end;');
  6005. ConvertProgram;
  6006. CheckSource('TestNestBegin',
  6007. '',
  6008. 'if (true) ;');
  6009. end;
  6010. procedure TTestModule.TestUnitImplVars;
  6011. begin
  6012. StartUnit(false);
  6013. Add('interface');
  6014. Add('implementation');
  6015. Add('var');
  6016. Add(' V1:longint;');
  6017. Add(' V2:longint = 3;');
  6018. Add(' V3:string = ''abc'';');
  6019. ConvertUnit;
  6020. CheckSource('TestUnitImplVars',
  6021. LinesToStr([ // statements
  6022. 'var $impl = $mod.$impl;',
  6023. '']),
  6024. '', // this.$init
  6025. LinesToStr([ // implementation
  6026. '$impl.V1 = 0;',
  6027. '$impl.V2 = 3;',
  6028. '$impl.V3 = "abc";',
  6029. '']) );
  6030. end;
  6031. procedure TTestModule.TestUnitImplConsts;
  6032. begin
  6033. StartUnit(false);
  6034. Add('interface');
  6035. Add('implementation');
  6036. Add('const');
  6037. Add(' v1 = 3;');
  6038. Add(' v2:longint = 4;');
  6039. Add(' v3:string = ''abc'';');
  6040. ConvertUnit;
  6041. CheckSource('TestUnitImplConsts',
  6042. LinesToStr([ // statements
  6043. 'var $impl = $mod.$impl;',
  6044. '']),
  6045. '', // this.$init
  6046. LinesToStr([ // implementation
  6047. '$impl.v1 = 3;',
  6048. '$impl.v2 = 4;',
  6049. '$impl.v3 = "abc";',
  6050. '']) );
  6051. end;
  6052. procedure TTestModule.TestUnitImplRecord;
  6053. begin
  6054. StartUnit(false);
  6055. Add('interface');
  6056. Add('implementation');
  6057. Add('type');
  6058. Add(' TMyRecord = record');
  6059. Add(' i: longint;');
  6060. Add(' end;');
  6061. Add('var aRec: TMyRecord;');
  6062. Add('initialization');
  6063. Add(' arec.i:=3;');
  6064. ConvertUnit;
  6065. CheckSource('TestUnitImplRecord',
  6066. LinesToStr([ // statements
  6067. 'var $impl = $mod.$impl;',
  6068. '']),
  6069. // this.$init
  6070. '$impl.aRec.i = 3;',
  6071. LinesToStr([ // implementation
  6072. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6073. ' this.i = 0;',
  6074. ' this.$eq = function (b) {',
  6075. ' return this.i === b.i;',
  6076. ' };',
  6077. ' this.$assign = function (s) {',
  6078. ' this.i = s.i;',
  6079. ' return this;',
  6080. ' };',
  6081. '});',
  6082. '$impl.aRec = $impl.TMyRecord.$new();',
  6083. '']) );
  6084. end;
  6085. procedure TTestModule.TestRenameJSNameConflict;
  6086. begin
  6087. StartProgram(false);
  6088. Add('var apply: longint;');
  6089. Add('var bind: longint;');
  6090. Add('var call: longint;');
  6091. Add('begin');
  6092. ConvertProgram;
  6093. CheckSource('TestRenameJSNameConflict',
  6094. LinesToStr([ // statements
  6095. 'this.Apply = 0;',
  6096. 'this.Bind = 0;',
  6097. 'this.Call = 0;'
  6098. ]),
  6099. LinesToStr([ // this.$main
  6100. ''
  6101. ]));
  6102. end;
  6103. procedure TTestModule.TestLocalConst;
  6104. begin
  6105. StartProgram(false);
  6106. Add('procedure DoIt;');
  6107. Add('const');
  6108. Add(' cA: longint = 1;');
  6109. Add(' cB = 2;');
  6110. Add(' procedure Sub;');
  6111. Add(' const');
  6112. Add(' csA = 3;');
  6113. Add(' cB: double = 4;');
  6114. Add(' begin');
  6115. Add(' cb:=cb+csa;');
  6116. Add(' ca:=ca+csa+5;');
  6117. Add(' end;');
  6118. Add('begin');
  6119. Add(' ca:=ca+cb+6;');
  6120. Add('end;');
  6121. Add('begin');
  6122. ConvertProgram;
  6123. CheckSource('TestLocalConst',
  6124. LinesToStr([
  6125. 'var cA = 1;',
  6126. 'var cB = 2;',
  6127. 'var csA = 3;',
  6128. 'var cB$1 = 4;',
  6129. 'this.DoIt = function () {',
  6130. ' function Sub() {',
  6131. ' cB$1 = cB$1 + 3;',
  6132. ' cA = cA + 3 + 5;',
  6133. ' };',
  6134. ' cA = cA + 2 + 6;',
  6135. '};'
  6136. ]),
  6137. LinesToStr([
  6138. ]));
  6139. end;
  6140. procedure TTestModule.TestVarExternal;
  6141. begin
  6142. StartProgram(false);
  6143. Add('var');
  6144. Add(' NaN: double; external name ''Global.NaN'';');
  6145. Add(' d: double;');
  6146. Add('begin');
  6147. Add(' d:=NaN;');
  6148. ConvertProgram;
  6149. CheckSource('TestVarExternal',
  6150. LinesToStr([
  6151. 'this.d = 0.0;'
  6152. ]),
  6153. LinesToStr([
  6154. '$mod.d = Global.NaN;'
  6155. ]));
  6156. end;
  6157. procedure TTestModule.TestVarExternalOtherUnit;
  6158. begin
  6159. AddModuleWithIntfImplSrc('unit2.pas',
  6160. LinesToStr([
  6161. 'var NaN: double; external name ''Global.NaN'';',
  6162. 'var iV: longint;'
  6163. ]),
  6164. '');
  6165. StartUnit(true);
  6166. Add('interface');
  6167. Add('uses unit2;');
  6168. Add('implementation');
  6169. Add('var');
  6170. Add(' d: double;');
  6171. Add(' i: longint; external name ''$i'';');
  6172. Add('begin');
  6173. Add(' d:=nan;');
  6174. Add(' d:=uNit2.nan;');
  6175. Add(' d:=test1.d;');
  6176. Add(' i:=iv;');
  6177. Add(' i:=uNit2.iv;');
  6178. Add(' i:=test1.i;');
  6179. ConvertUnit;
  6180. CheckSource('TestVarExternalOtherUnit',
  6181. LinesToStr([
  6182. 'var $impl = $mod.$impl;',
  6183. '']),
  6184. LinesToStr([ // this.$init
  6185. '$impl.d = Global.NaN;',
  6186. '$impl.d = Global.NaN;',
  6187. '$impl.d = $impl.d;',
  6188. '$i = pas.unit2.iV;',
  6189. '$i = pas.unit2.iV;',
  6190. '$i = $i;',
  6191. '']),
  6192. LinesToStr([ // implementation
  6193. '$impl.d = 0.0;',
  6194. '']) );
  6195. end;
  6196. procedure TTestModule.TestVarAbsoluteFail;
  6197. begin
  6198. StartProgram(false);
  6199. Add([
  6200. 'var',
  6201. ' a: longint;',
  6202. ' b: longword absolute a;',
  6203. 'begin']);
  6204. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6205. ConvertProgram;
  6206. end;
  6207. procedure TTestModule.TestConstExternal;
  6208. begin
  6209. StartProgram(false);
  6210. Add([
  6211. 'const',
  6212. ' PI: double; external name ''Global.PI'';',
  6213. ' Tau = 2*pi;',
  6214. 'var d: double;',
  6215. 'begin',
  6216. ' d:=pi;',
  6217. ' d:=tau+pi;']);
  6218. ConvertProgram;
  6219. CheckSource('TestConstExternal',
  6220. LinesToStr([
  6221. 'this.Tau = 2*Global.PI;',
  6222. 'this.d = 0.0;'
  6223. ]),
  6224. LinesToStr([
  6225. '$mod.d = Global.PI;',
  6226. '$mod.d = $mod.Tau + Global.PI;'
  6227. ]));
  6228. end;
  6229. procedure TTestModule.TestDouble;
  6230. begin
  6231. StartProgram(false);
  6232. Add([
  6233. 'type',
  6234. ' TDateTime = double;',
  6235. 'const',
  6236. ' a = TDateTime(2.7);',
  6237. ' b = a + TDateTime(1.7);',
  6238. ' c = 0.9 + 0.1;',
  6239. ' f0_1 = 0.1;',
  6240. ' f0_3 = 0.3;',
  6241. ' fn0_1 = -0.1;',
  6242. ' fn0_3 = -0.3;',
  6243. ' fn0_003 = -0.003;',
  6244. ' fn0_123456789 = -0.123456789;',
  6245. ' fn300_0 = -300.0;',
  6246. ' fn123456_0 = -123456.0;',
  6247. ' fn1234567_8 = -1234567.8;',
  6248. ' fn12345678_9 = -12345678.9;',
  6249. ' f1_0En12 = 1E-12;',
  6250. ' fn1_0En12 = -1E-12;',
  6251. ' maxdouble = 1.7e+308;',
  6252. ' mindouble = -1.7e+308;',
  6253. ' MinSafeIntDouble = -$10000000000000;',
  6254. ' MinSafeIntDouble2 = -$fffffffffffff-1;',
  6255. ' MaxSafeIntDouble = $fffffffffffff;',
  6256. ' DZeroResolution = 1E-12;',
  6257. ' Minus1 = -1E-12;',
  6258. ' EPS = 1E-9;',
  6259. ' DELTA = 0.001;',
  6260. ' Big = 129.789E+100;',
  6261. ' Test0_15 = 0.15;',
  6262. ' Test999 = 2.9999999999999;',
  6263. ' Test111999 = 211199999999999000.0;',
  6264. ' TestMinus111999 = -211199999999999000.0;',
  6265. 'var',
  6266. ' d: double = b;',
  6267. 'begin',
  6268. ' d:=1.0;',
  6269. ' d:=1.0/3.0;',
  6270. ' d:=1/3;',
  6271. ' d:=5.0E-324;',
  6272. ' d:=1.7E308;',
  6273. ' d:=001.00E00;',
  6274. ' d:=002.00E001;',
  6275. ' d:=003.000E000;',
  6276. ' d:=-004.00E-00;',
  6277. ' d:=-005.00E-001;',
  6278. ' d:=10**3;',
  6279. ' d:=10 mod 3;',
  6280. ' d:=10 div 3;',
  6281. ' d:=c;',
  6282. ' d:=f0_1;',
  6283. ' d:=f0_3;',
  6284. ' d:=fn0_1;',
  6285. ' d:=fn0_3;',
  6286. ' d:=fn0_003;',
  6287. ' d:=fn0_123456789;',
  6288. ' d:=fn300_0;',
  6289. ' d:=fn123456_0;',
  6290. ' d:=fn1234567_8;',
  6291. ' d:=fn12345678_9;',
  6292. ' d:=f1_0En12;',
  6293. ' d:=fn1_0En12;',
  6294. ' d:=maxdouble;',
  6295. ' d:=mindouble;',
  6296. ' d:=MinSafeIntDouble;',
  6297. ' d:=double(MinSafeIntDouble);',
  6298. ' d:=MinSafeIntDouble2;',
  6299. ' d:=double(MinSafeIntDouble2);',
  6300. ' d:=MaxSafeIntDouble;',
  6301. ' d:=default(double);',
  6302. '']);
  6303. ConvertProgram;
  6304. CheckSource('TestDouble',
  6305. LinesToStr([
  6306. 'this.a = 2.7;',
  6307. 'this.b = 2.7 + 1.7;',
  6308. 'this.c = 0.9 + 0.1;',
  6309. 'this.f0_1 = 0.1;',
  6310. 'this.f0_3 = 0.3;',
  6311. 'this.fn0_1 = -0.1;',
  6312. 'this.fn0_3 = -0.3;',
  6313. 'this.fn0_003 = -0.003;',
  6314. 'this.fn0_123456789 = -0.123456789;',
  6315. 'this.fn300_0 = -300.0;',
  6316. 'this.fn123456_0 = -123456.0;',
  6317. 'this.fn1234567_8 = -1234567.8;',
  6318. 'this.fn12345678_9 = -12345678.9;',
  6319. 'this.f1_0En12 = 1E-12;',
  6320. 'this.fn1_0En12 = -1E-12;',
  6321. 'this.maxdouble = 1.7e+308;',
  6322. 'this.mindouble = -1.7e+308;',
  6323. 'this.MinSafeIntDouble = -0x10000000000000;',
  6324. 'this.MinSafeIntDouble2 = -0xfffffffffffff - 1;',
  6325. 'this.MaxSafeIntDouble = 0xfffffffffffff;',
  6326. 'this.DZeroResolution = 1E-12;',
  6327. 'this.Minus1 = -1E-12;',
  6328. 'this.EPS = 1E-9;',
  6329. 'this.DELTA = 0.001;',
  6330. 'this.Big = 129.789E+100;',
  6331. 'this.Test0_15 = 0.15;',
  6332. 'this.Test999 = 2.9999999999999;',
  6333. 'this.Test111999 = 211199999999999000.0;',
  6334. 'this.TestMinus111999 = -211199999999999000.0;',
  6335. 'this.d = 4.4;'
  6336. ]),
  6337. LinesToStr([
  6338. '$mod.d = 1.0;',
  6339. '$mod.d = 1.0 / 3.0;',
  6340. '$mod.d = 1 / 3;',
  6341. '$mod.d = 5.0E-324;',
  6342. '$mod.d = 1.7E308;',
  6343. '$mod.d = 1.00E0;',
  6344. '$mod.d = 2.00E1;',
  6345. '$mod.d = 3.000E0;',
  6346. '$mod.d = -4.00E-0;',
  6347. '$mod.d = -5.00E-1;',
  6348. '$mod.d = Math.pow(10, 3);',
  6349. '$mod.d = 10 % 3;',
  6350. '$mod.d = rtl.trunc(10 / 3);',
  6351. '$mod.d = 1;',
  6352. '$mod.d = 0.1;',
  6353. '$mod.d = 0.3;',
  6354. '$mod.d = -0.1;',
  6355. '$mod.d = -0.3;',
  6356. '$mod.d = -0.003;',
  6357. '$mod.d = -0.123456789;',
  6358. '$mod.d = -300;',
  6359. '$mod.d = -123456;',
  6360. '$mod.d = -1234567.8;',
  6361. '$mod.d = -1.23456789E7;',
  6362. '$mod.d = 1E-12;',
  6363. '$mod.d = -1E-12;',
  6364. '$mod.d = 1.7E308;',
  6365. '$mod.d = -1.7E308;',
  6366. '$mod.d = -4503599627370496;',
  6367. '$mod.d = -4503599627370496;',
  6368. '$mod.d = -4503599627370496;',
  6369. '$mod.d = -4503599627370496;',
  6370. '$mod.d = 4503599627370495;',
  6371. '$mod.d = 0.0;',
  6372. '']));
  6373. end;
  6374. procedure TTestModule.TestInteger;
  6375. begin
  6376. StartProgram(false);
  6377. Add([
  6378. 'const',
  6379. ' MinInt = low(NativeInt);',
  6380. ' MaxInt = high(NativeInt);',
  6381. 'type',
  6382. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6383. 'const',
  6384. ' a = low(TMyInt)+High(TMyInt);',
  6385. 'var',
  6386. ' i: TMyInt;',
  6387. 'begin',
  6388. ' i:=-MinInt;',
  6389. ' i:=default(TMyInt);',
  6390. ' i:=low(i)+high(i);',
  6391. '']);
  6392. ConvertProgram;
  6393. CheckSource('TestIntegerRange',
  6394. LinesToStr([
  6395. 'this.MinInt = -4503599627370496;',
  6396. 'this.MaxInt = 4503599627370495;',
  6397. 'this.a = -4503599627370496 + 4503599627370495;',
  6398. 'this.i = 0;',
  6399. '']),
  6400. LinesToStr([
  6401. '$mod.i = - -4503599627370496;',
  6402. '$mod.i = -4503599627370496;',
  6403. '$mod.i = -4503599627370496 + 4503599627370495;',
  6404. '']));
  6405. end;
  6406. procedure TTestModule.TestIntegerRange;
  6407. begin
  6408. StartProgram(false);
  6409. Add([
  6410. 'const',
  6411. ' MinInt = -1;',
  6412. ' MaxInt = +1;',
  6413. 'type',
  6414. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6415. ' TInt2 = 1..3;',
  6416. 'const',
  6417. ' a = low(TMyInt)+High(TMyInt);',
  6418. ' b = low(TInt2)+High(TInt2);',
  6419. ' s1 = [1];',
  6420. ' s2 = [1,2];',
  6421. ' s3 = [1..3];',
  6422. ' s4 = [low(shortint)..high(shortint)];',
  6423. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6424. ' s6 = 1 in s2;',
  6425. 'var',
  6426. ' i: TMyInt;',
  6427. ' i2: TInt2;',
  6428. 'begin',
  6429. ' i:=i2;',
  6430. ' i:=default(TMyInt);',
  6431. ' if i=i2 then ;']);
  6432. ConvertProgram;
  6433. CheckSource('TestIntegerRange',
  6434. LinesToStr([
  6435. 'this.MinInt = -1;',
  6436. 'this.MaxInt = +1;',
  6437. 'this.a = -1 + 1;',
  6438. 'this.b = 1 + 3;',
  6439. 'this.s1 = rtl.createSet(1);',
  6440. 'this.s2 = rtl.createSet(1, 2);',
  6441. 'this.s3 = rtl.createSet(null, 1, 3);',
  6442. 'this.s4 = rtl.createSet(null, -128, 127);',
  6443. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6444. 'this.s6 = 1 in $mod.s2;',
  6445. 'this.i = 0;',
  6446. 'this.i2 = 0;',
  6447. '']),
  6448. LinesToStr([
  6449. '$mod.i = $mod.i2;',
  6450. '$mod.i = -1;',
  6451. 'if ($mod.i === $mod.i2) ;',
  6452. '']));
  6453. end;
  6454. procedure TTestModule.TestIntegerTypecasts;
  6455. begin
  6456. StartProgram(false);
  6457. Add([
  6458. 'var',
  6459. ' i: nativeint;',
  6460. ' b: byte;',
  6461. ' sh: shortint;',
  6462. ' w: word;',
  6463. ' sm: smallint;',
  6464. ' lw: longword;',
  6465. ' li: longint;',
  6466. 'begin',
  6467. ' b:=byte(i);',
  6468. ' sh:=shortint(i);',
  6469. ' w:=word(i);',
  6470. ' sm:=smallint(i);',
  6471. ' lw:=longword(i);',
  6472. ' li:=longint(i);',
  6473. '']);
  6474. ConvertProgram;
  6475. CheckSource('TestIntegerTypecasts',
  6476. LinesToStr([
  6477. 'this.i = 0;',
  6478. 'this.b = 0;',
  6479. 'this.sh = 0;',
  6480. 'this.w = 0;',
  6481. 'this.sm = 0;',
  6482. 'this.lw = 0;',
  6483. 'this.li = 0;',
  6484. '']),
  6485. LinesToStr([
  6486. '$mod.b = $mod.i & 255;',
  6487. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6488. '$mod.w = $mod.i & 65535;',
  6489. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6490. '$mod.lw = $mod.i >>> 0;',
  6491. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6492. '']));
  6493. end;
  6494. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6495. begin
  6496. StartProgram(false);
  6497. Add([
  6498. 'var',
  6499. ' i,j: nativeint;',
  6500. 'begin',
  6501. ' i:=i shr 0;',
  6502. ' i:=i shr 1;',
  6503. ' i:=i shr 3;',
  6504. ' i:=i shr 54;',
  6505. ' i:=j shr i;',
  6506. '']);
  6507. ConvertProgram;
  6508. CheckResolverUnexpectedHints;
  6509. CheckSource('TestInteger_BitwiseShrNativeInt',
  6510. LinesToStr([
  6511. 'this.i = 0;',
  6512. 'this.j = 0;',
  6513. '']),
  6514. LinesToStr([
  6515. '$mod.i = $mod.i;',
  6516. '$mod.i = Math.floor($mod.i / 2);',
  6517. '$mod.i = Math.floor($mod.i / 8);',
  6518. '$mod.i = 0;',
  6519. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6520. '']));
  6521. end;
  6522. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6523. begin
  6524. StartProgram(false);
  6525. Add([
  6526. 'var',
  6527. ' i: nativeint;',
  6528. 'begin',
  6529. ' i:=i shl 0;',
  6530. ' i:=i shl 54;',
  6531. ' i:=123456789012 shl 1;',
  6532. ' i:=i shl 1;',
  6533. '']);
  6534. ConvertProgram;
  6535. CheckResolverUnexpectedHints;
  6536. CheckSource('TestInteger_BitwiseShrNativeInt',
  6537. LinesToStr([
  6538. 'this.i = 0;',
  6539. '']),
  6540. LinesToStr([
  6541. '$mod.i = $mod.i;',
  6542. '$mod.i = 0;',
  6543. '$mod.i = 246913578024;',
  6544. '$mod.i = rtl.shl($mod.i, 1);',
  6545. '']));
  6546. end;
  6547. procedure TTestModule.TestInteger_SystemFunc;
  6548. begin
  6549. StartProgram(true);
  6550. Add([
  6551. 'var',
  6552. ' i: byte;',
  6553. ' s: string;',
  6554. 'begin',
  6555. ' system.inc(i);',
  6556. ' system.str(i,s);',
  6557. ' s:=system.str(i);',
  6558. ' i:=system.low(i);',
  6559. ' i:=system.high(i);',
  6560. ' i:=system.pred(i);',
  6561. ' i:=system.succ(i);',
  6562. '']);
  6563. ConvertProgram;
  6564. CheckResolverUnexpectedHints;
  6565. CheckSource('TestInteger_SystemFunc',
  6566. LinesToStr([
  6567. 'this.i = 0;',
  6568. 'this.s = "";',
  6569. '']),
  6570. LinesToStr([
  6571. '$mod.i += 1;',
  6572. '$mod.s = "" + $mod.i;',
  6573. '$mod.s = "" + $mod.i;',
  6574. '$mod.i = 0;',
  6575. '$mod.i = 255;',
  6576. '$mod.i = $mod.i - 1;',
  6577. '$mod.i = $mod.i + 1;',
  6578. '']));
  6579. end;
  6580. procedure TTestModule.TestCurrency;
  6581. begin
  6582. StartProgram(false);
  6583. Add([
  6584. 'type',
  6585. ' TCoin = currency;',
  6586. 'const',
  6587. ' a = TCoin(2.7);',
  6588. ' b = a + TCoin(1.7);',
  6589. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6590. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6591. 'var',
  6592. ' c: TCoin = b;',
  6593. ' i: nativeint;',
  6594. ' d: double;',
  6595. ' j: jsvalue;',
  6596. 'function DoIt(c: currency): currency; begin end;',
  6597. 'function GetIt(d: double): double; begin end;',
  6598. 'procedure Write(v: jsvalue); begin end;',
  6599. 'begin',
  6600. ' c:=1.0;',
  6601. ' c:=0.1;',
  6602. ' c:=1.0/3.0;',
  6603. ' c:=1/3;',
  6604. ' c:=a;',
  6605. ' d:=c;',
  6606. ' c:=d;',
  6607. ' c:=currency(c);',
  6608. ' c:=currency(d);',
  6609. ' d:=double(c);',
  6610. ' c:=i;',
  6611. ' c:=currency(i);',
  6612. //' i:=c;', not allowed
  6613. ' i:=nativeint(c);',
  6614. ' c:=c+a;',
  6615. ' c:=-c-a;',
  6616. ' c:=d+c;',
  6617. ' c:=c+d;',
  6618. ' c:=d-c;',
  6619. ' c:=c-d;',
  6620. ' c:=c*a;',
  6621. ' c:=a*c;',
  6622. ' c:=d*c;',
  6623. ' c:=c*d;',
  6624. ' c:=c/a;',
  6625. ' c:=a/c;',
  6626. ' c:=d/c;',
  6627. ' c:=c/d;',
  6628. ' c:=c**a;',
  6629. ' c:=a**c;',
  6630. ' c:=d**c;',
  6631. ' c:=c**d;',
  6632. ' if c=c then ;',
  6633. ' if c=a then ;',
  6634. ' if a=c then ;',
  6635. ' if d=c then ;',
  6636. ' if c=d then ;',
  6637. ' c:=DoIt(c);',
  6638. ' c:=DoIt(i);',
  6639. ' c:=DoIt(d);',
  6640. ' c:=GetIt(c);',
  6641. ' j:=c;',
  6642. ' Write(c);',
  6643. ' c:=default(currency);',
  6644. ' j:=str(c);',
  6645. ' j:=str(c:0:3);',
  6646. '']);
  6647. ConvertProgram;
  6648. CheckSource('TestCurrency',
  6649. LinesToStr([
  6650. 'this.a = 27000;',
  6651. 'this.b = $mod.a + 17000;',
  6652. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6653. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6654. 'this.c = $mod.b;',
  6655. 'this.i = 0;',
  6656. 'this.d = 0.0;',
  6657. 'this.j = undefined;',
  6658. 'this.DoIt = function (c) {',
  6659. ' var Result = 0;',
  6660. ' return Result;',
  6661. '};',
  6662. 'this.GetIt = function (d) {',
  6663. ' var Result = 0.0;',
  6664. ' return Result;',
  6665. '};',
  6666. 'this.Write = function (v) {',
  6667. '};',
  6668. '']),
  6669. LinesToStr([
  6670. '$mod.c = 10000;',
  6671. '$mod.c = 1000;',
  6672. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  6673. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  6674. '$mod.c = $mod.a;',
  6675. '$mod.d = $mod.c / 10000;',
  6676. '$mod.c = rtl.trunc($mod.d * 10000);',
  6677. '$mod.c = $mod.c;',
  6678. '$mod.c = $mod.d * 10000;',
  6679. '$mod.d = $mod.c / 10000;',
  6680. '$mod.c = $mod.i * 10000;',
  6681. '$mod.c = $mod.i * 10000;',
  6682. '$mod.i = rtl.trunc($mod.c / 10000);',
  6683. '$mod.c = $mod.c + $mod.a;',
  6684. '$mod.c = -$mod.c - $mod.a;',
  6685. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6686. '$mod.c = $mod.c + ($mod.d * 10000);',
  6687. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6688. '$mod.c = $mod.c - ($mod.d * 10000);',
  6689. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6690. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6691. '$mod.c = $mod.d * $mod.c;',
  6692. '$mod.c = $mod.c * $mod.d;',
  6693. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  6694. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  6695. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  6696. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  6697. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6698. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6699. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6700. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6701. 'if ($mod.c === $mod.c) ;',
  6702. 'if ($mod.c === $mod.a) ;',
  6703. 'if ($mod.a === $mod.c) ;',
  6704. 'if (($mod.d * 10000) === $mod.c) ;',
  6705. 'if ($mod.c === ($mod.d * 10000)) ;',
  6706. '$mod.c = $mod.DoIt($mod.c);',
  6707. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6708. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6709. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  6710. '$mod.j = $mod.c / 10000;',
  6711. '$mod.Write($mod.c / 10000);',
  6712. '$mod.c = 0;',
  6713. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6714. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6715. '']));
  6716. end;
  6717. procedure TTestModule.TestForBoolDo;
  6718. begin
  6719. StartProgram(false);
  6720. Add([
  6721. 'var b: boolean;',
  6722. 'begin',
  6723. ' for b:=false to true do ;',
  6724. ' for b:=b downto false do ;',
  6725. ' for b in boolean do ;',
  6726. '']);
  6727. ConvertProgram;
  6728. CheckSource('TestForBoolDo',
  6729. LinesToStr([ // statements
  6730. 'this.b = false;']),
  6731. LinesToStr([ // this.$main
  6732. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6733. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6734. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6735. '']));
  6736. end;
  6737. procedure TTestModule.TestForIntDo;
  6738. begin
  6739. StartProgram(false);
  6740. Add([
  6741. 'var i: longint;',
  6742. 'begin',
  6743. ' for i:=3 to 5 do ;',
  6744. ' for i:=i downto 2 do ;',
  6745. ' for i in byte do ;',
  6746. '']);
  6747. ConvertProgram;
  6748. CheckSource('TestForIntDo',
  6749. LinesToStr([ // statements
  6750. 'this.i = 0;']),
  6751. LinesToStr([ // this.$main
  6752. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6753. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6754. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6755. '']));
  6756. end;
  6757. procedure TTestModule.TestForIntInDo;
  6758. begin
  6759. StartProgram(false);
  6760. Add([
  6761. 'type',
  6762. ' TSetOfInt = set of byte;',
  6763. ' TIntRg = 3..7;',
  6764. ' TSetOfIntRg = set of TIntRg;',
  6765. 'var',
  6766. ' i,i2: longint;',
  6767. ' a1: array of byte;',
  6768. ' a2: array[1..3] of byte;',
  6769. ' soi: TSetOfInt;',
  6770. ' soir: TSetOfIntRg;',
  6771. ' ir: TIntRg;',
  6772. 'begin',
  6773. ' for i in byte do ;',
  6774. ' for i in a1 do ;',
  6775. ' for i in a2 do ;',
  6776. ' for i in [11..13] do ;',
  6777. ' for i in TSetOfInt do ;',
  6778. ' for i in TIntRg do ;',
  6779. ' for i in soi do i2:=i;',
  6780. ' for i in TSetOfIntRg do ;',
  6781. ' for i in soir do ;',
  6782. ' for ir in TIntRg do ;',
  6783. ' for ir in TSetOfIntRg do ;',
  6784. ' for ir in soir do ;',
  6785. '']);
  6786. ConvertProgram;
  6787. CheckSource('TestForIntInDo',
  6788. LinesToStr([ // statements
  6789. 'this.i = 0;',
  6790. 'this.i2 = 0;',
  6791. 'this.a1 = [];',
  6792. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6793. 'this.soi = {};',
  6794. 'this.soir = {};',
  6795. 'this.ir = 0;',
  6796. '']),
  6797. LinesToStr([ // this.$main
  6798. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6799. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6800. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6801. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6802. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6803. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6804. 'for (var $l11 in $mod.soi) {',
  6805. ' $mod.i = +$l11;',
  6806. ' $mod.i2 = $mod.i;',
  6807. '};',
  6808. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6809. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6810. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6811. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6812. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6813. '']));
  6814. end;
  6815. procedure TTestModule.TestCharConst;
  6816. begin
  6817. StartProgram(false);
  6818. Add([
  6819. 'const',
  6820. ' a = #$00F3;',
  6821. ' c: char = ''1'';',
  6822. 'begin',
  6823. ' c:=#0;',
  6824. ' c:=#1;',
  6825. ' c:=#9;',
  6826. ' c:=#10;',
  6827. ' c:=#13;',
  6828. ' c:=#31;',
  6829. ' c:=#32;',
  6830. ' c:=#$A;',
  6831. ' c:=#$0A;',
  6832. ' c:=#$b;',
  6833. ' c:=#$0b;',
  6834. ' c:=^A;',
  6835. ' c:=''"'';',
  6836. ' c:=default(char);',
  6837. ' c:=#$00E4;', // ä
  6838. ' c:=''ä'';',
  6839. ' c:=#$E4;', // ä
  6840. ' c:=#$D800;', // invalid UTF-16
  6841. ' c:=#$DFFF;', // invalid UTF-16
  6842. ' c:=#$FFFF;', // last UCS-2
  6843. ' c:=high(c);', // last UCS-2
  6844. '']);
  6845. ConvertProgram;
  6846. CheckSource('TestCharConst',
  6847. LinesToStr([
  6848. 'this.a="ó";',
  6849. 'this.c="1";'
  6850. ]),
  6851. LinesToStr([
  6852. '$mod.c="\x00";',
  6853. '$mod.c="\x01";',
  6854. '$mod.c="\t";',
  6855. '$mod.c="\n";',
  6856. '$mod.c="\r";',
  6857. '$mod.c="\x1F";',
  6858. '$mod.c=" ";',
  6859. '$mod.c="\n";',
  6860. '$mod.c="\n";',
  6861. '$mod.c="\x0B";',
  6862. '$mod.c="\x0B";',
  6863. '$mod.c="\x01";',
  6864. '$mod.c=''"'';',
  6865. '$mod.c="\x00";',
  6866. '$mod.c = "ä";',
  6867. '$mod.c = "ä";',
  6868. '$mod.c = "ä";',
  6869. '$mod.c="\uD800";',
  6870. '$mod.c="\uDFFF";',
  6871. '$mod.c="\uFFFF";',
  6872. '$mod.c="\uFFFF";',
  6873. '']));
  6874. end;
  6875. procedure TTestModule.TestChar_Compare;
  6876. begin
  6877. StartProgram(false);
  6878. Add('var');
  6879. Add(' c: char;');
  6880. Add(' b: boolean;');
  6881. Add('begin');
  6882. Add(' b:=c=''1'';');
  6883. Add(' b:=''2''=c;');
  6884. Add(' b:=''3''=''4'';');
  6885. Add(' b:=c<>''5'';');
  6886. Add(' b:=''6''<>c;');
  6887. Add(' b:=c>''7'';');
  6888. Add(' b:=''8''>c;');
  6889. Add(' b:=c>=''9'';');
  6890. Add(' b:=''A''>=c;');
  6891. Add(' b:=c<''B'';');
  6892. Add(' b:=''C''<c;');
  6893. Add(' b:=c<=''D'';');
  6894. Add(' b:=''E''<=c;');
  6895. ConvertProgram;
  6896. CheckSource('TestChar_Compare',
  6897. LinesToStr([
  6898. 'this.c="";',
  6899. 'this.b = false;'
  6900. ]),
  6901. LinesToStr([
  6902. '$mod.b = $mod.c === "1";',
  6903. '$mod.b = "2" === $mod.c;',
  6904. '$mod.b = "3" === "4";',
  6905. '$mod.b = $mod.c !== "5";',
  6906. '$mod.b = "6" !== $mod.c;',
  6907. '$mod.b = $mod.c > "7";',
  6908. '$mod.b = "8" > $mod.c;',
  6909. '$mod.b = $mod.c >= "9";',
  6910. '$mod.b = "A" >= $mod.c;',
  6911. '$mod.b = $mod.c < "B";',
  6912. '$mod.b = "C" < $mod.c;',
  6913. '$mod.b = $mod.c <= "D";',
  6914. '$mod.b = "E" <= $mod.c;',
  6915. '']));
  6916. end;
  6917. procedure TTestModule.TestChar_BuiltInProcs;
  6918. begin
  6919. StartProgram(false);
  6920. Add([
  6921. 'var',
  6922. ' c: char;',
  6923. ' i: longint;',
  6924. ' s: string;',
  6925. 'begin',
  6926. ' i:=ord(c);',
  6927. ' i:=ord(s[i]);',
  6928. ' c:=chr(i);',
  6929. ' c:=pred(c);',
  6930. ' c:=succ(c);',
  6931. ' c:=low(c);',
  6932. ' c:=high(c);',
  6933. ' i:=byte(c);',
  6934. ' i:=word(c);',
  6935. ' i:=longint(c);',
  6936. '']);
  6937. ConvertProgram;
  6938. CheckSource('TestChar_BuiltInProcs',
  6939. LinesToStr([
  6940. 'this.c = "";',
  6941. 'this.i = 0;',
  6942. 'this.s = "";'
  6943. ]),
  6944. LinesToStr([
  6945. '$mod.i = $mod.c.charCodeAt();',
  6946. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6947. '$mod.c = String.fromCharCode($mod.i);',
  6948. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6949. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6950. '$mod.c = "\x00";',
  6951. '$mod.c = "\uFFFF";',
  6952. '$mod.i = $mod.c.charCodeAt() & 255;',
  6953. '$mod.i = $mod.c.charCodeAt();',
  6954. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  6955. '']));
  6956. end;
  6957. procedure TTestModule.TestStringConst;
  6958. begin
  6959. StartProgram(false);
  6960. Add([
  6961. '{$H+}',
  6962. 'const',
  6963. ' a = #$00F3#$017C;', // first <256, then >=256
  6964. ' b = string(''a'');',
  6965. ' c = string(''ä'');',
  6966. ' d = UnicodeString(''b'');',
  6967. ' e = UnicodeString(''ö'');',
  6968. 'var',
  6969. ' s: string = ''abc'';',
  6970. 'begin',
  6971. ' s:='''';',
  6972. ' s:=#13#10;',
  6973. ' s:=#9''foo'';',
  6974. ' s:=#$A9;',
  6975. ' s:=''foo''#13''bar'';',
  6976. ' s:=''"'';',
  6977. ' s:=''"''''"'';',
  6978. ' s:=#$20AC;', // euro
  6979. ' s:=#$10437;', // outside BMP
  6980. ' s:=default(string);',
  6981. ' s:=concat(s);',
  6982. ' s:=concat(s,''a'',s)',
  6983. '']);
  6984. ConvertProgram;
  6985. CheckSource('TestStringConst',
  6986. LinesToStr([
  6987. 'this.a = "óż";',
  6988. 'this.b = "a";',
  6989. 'this.c = "ä";',
  6990. 'this.d = "b";',
  6991. 'this.e = "ö";',
  6992. 'this.s="abc";',
  6993. '']),
  6994. LinesToStr([
  6995. '$mod.s="";',
  6996. '$mod.s="\r\n";',
  6997. '$mod.s="\tfoo";',
  6998. '$mod.s="©";',
  6999. '$mod.s="foo\rbar";',
  7000. '$mod.s=''"'';',
  7001. '$mod.s=''"\''"'';',
  7002. '$mod.s="€";',
  7003. '$mod.s="'#$F0#$90#$90#$B7'";',
  7004. '$mod.s="";',
  7005. '$mod.s = $mod.s;',
  7006. '$mod.s = $mod.s.concat("a", $mod.s);',
  7007. '']));
  7008. end;
  7009. procedure TTestModule.TestStringConstSurrogate;
  7010. begin
  7011. StartProgram(false);
  7012. Add([
  7013. 'var',
  7014. ' s: string;',
  7015. 'begin',
  7016. ' s:=''😊'';', // 1F60A
  7017. '']);
  7018. ConvertProgram;
  7019. CheckSource('TestStringConstSurrogate',
  7020. LinesToStr([
  7021. 'this.s="";'
  7022. ]),
  7023. LinesToStr([
  7024. '$mod.s="😊";'
  7025. ]));
  7026. end;
  7027. procedure TTestModule.TestString_Length;
  7028. begin
  7029. StartProgram(false);
  7030. Add('const c = ''foo'';');
  7031. Add('var');
  7032. Add(' s: string;');
  7033. Add(' i: longint;');
  7034. Add('begin');
  7035. Add(' i:=length(s);');
  7036. Add(' i:=length(s+s);');
  7037. Add(' i:=length(''abc'');');
  7038. Add(' i:=length(c);');
  7039. ConvertProgram;
  7040. CheckSource('TestString_Length',
  7041. LinesToStr([
  7042. 'this.c = "foo";',
  7043. 'this.s = "";',
  7044. 'this.i = 0;',
  7045. '']),
  7046. LinesToStr([
  7047. '$mod.i = $mod.s.length;',
  7048. '$mod.i = ($mod.s+$mod.s).length;',
  7049. '$mod.i = "abc".length;',
  7050. '$mod.i = $mod.c.length;',
  7051. '']));
  7052. end;
  7053. procedure TTestModule.TestString_Compare;
  7054. begin
  7055. StartProgram(false);
  7056. Add('var');
  7057. Add(' s, t: string;');
  7058. Add(' b: boolean;');
  7059. Add('begin');
  7060. Add(' b:=s=t;');
  7061. Add(' b:=s<>t;');
  7062. Add(' b:=s>t;');
  7063. Add(' b:=s>=t;');
  7064. Add(' b:=s<t;');
  7065. Add(' b:=s<=t;');
  7066. ConvertProgram;
  7067. CheckSource('TestString_Compare',
  7068. LinesToStr([ // statements
  7069. 'this.s = "";',
  7070. 'this.t = "";',
  7071. 'this.b =false;'
  7072. ]),
  7073. LinesToStr([ // this.$main
  7074. '$mod.b = $mod.s === $mod.t;',
  7075. '$mod.b = $mod.s !== $mod.t;',
  7076. '$mod.b = $mod.s > $mod.t;',
  7077. '$mod.b = $mod.s >= $mod.t;',
  7078. '$mod.b = $mod.s < $mod.t;',
  7079. '$mod.b = $mod.s <= $mod.t;',
  7080. '']));
  7081. end;
  7082. procedure TTestModule.TestString_SetLength;
  7083. begin
  7084. StartProgram(false);
  7085. Add([
  7086. 'procedure DoIt(var s: string);',
  7087. 'begin',
  7088. ' SetLength(s,2);',
  7089. 'end;',
  7090. 'var s: string;',
  7091. 'begin',
  7092. ' SetLength(s,3);',
  7093. '']);
  7094. ConvertProgram;
  7095. CheckSource('TestString_SetLength',
  7096. LinesToStr([ // statements
  7097. 'this.DoIt = function (s) {',
  7098. ' s.set(rtl.strSetLength(s.get(), 2));',
  7099. '};',
  7100. 'this.s = "";',
  7101. '']),
  7102. LinesToStr([ // this.$main
  7103. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7104. ]));
  7105. end;
  7106. procedure TTestModule.TestString_CharAt;
  7107. begin
  7108. StartProgram(false);
  7109. Add([
  7110. 'var',
  7111. ' s: string;',
  7112. ' c: char;',
  7113. ' b: boolean;',
  7114. 'begin',
  7115. ' b:= s[1] = c;',
  7116. ' b:= c = s[1];',
  7117. ' b:= c <> s[1];',
  7118. ' b:= c > s[1];',
  7119. ' b:= c >= s[1];',
  7120. ' b:= c < s[2];',
  7121. ' b:= c <= s[1];',
  7122. ' s[1] := c;',
  7123. ' s[2+3] := c;']);
  7124. ConvertProgram;
  7125. CheckSource('TestString_CharAt',
  7126. LinesToStr([ // statements
  7127. 'this.s = "";',
  7128. 'this.c = "";',
  7129. 'this.b = false;'
  7130. ]),
  7131. LinesToStr([ // this.$main
  7132. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7133. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7134. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7135. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7136. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7137. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7138. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7139. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7140. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7141. '']));
  7142. end;
  7143. procedure TTestModule.TestStringHMinusFail;
  7144. begin
  7145. StartProgram(false);
  7146. Add([
  7147. '{$H-}',
  7148. 'var s: string;',
  7149. 'begin']);
  7150. ConvertProgram;
  7151. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7152. end;
  7153. procedure TTestModule.TestStr;
  7154. begin
  7155. StartProgram(false);
  7156. Add('var');
  7157. Add(' b: boolean;');
  7158. Add(' i: longint;');
  7159. Add(' d: double;');
  7160. Add(' s: string;');
  7161. Add('begin');
  7162. Add(' str(b,s);');
  7163. Add(' str(i,s);');
  7164. Add(' str(d,s);');
  7165. Add(' str(i:3,s);');
  7166. Add(' str(d:3:2,s);');
  7167. Add(' Str(12.456:12:1,s);');
  7168. Add(' Str(12.456:12,s);');
  7169. Add(' s:=str(b);');
  7170. Add(' s:=str(i);');
  7171. Add(' s:=str(d);');
  7172. Add(' s:=str(i,i);');
  7173. Add(' s:=str(i:3);');
  7174. Add(' s:=str(d:3:2);');
  7175. Add(' s:=str(i:4,i);');
  7176. Add(' s:=str(i,i:5);');
  7177. Add(' s:=str(i:4,i:5);');
  7178. Add(' s:=str(s,s);');
  7179. Add(' s:=str(s,''foo'');');
  7180. ConvertProgram;
  7181. CheckSource('TestStr',
  7182. LinesToStr([ // statements
  7183. 'this.b = false;',
  7184. 'this.i = 0;',
  7185. 'this.d = 0.0;',
  7186. 'this.s = "";',
  7187. '']),
  7188. LinesToStr([ // this.$main
  7189. '$mod.s = ""+$mod.b;',
  7190. '$mod.s = ""+$mod.i;',
  7191. '$mod.s = rtl.floatToStr($mod.d);',
  7192. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7193. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7194. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7195. '$mod.s = rtl.floatToStr(12.456,12);',
  7196. '$mod.s = ""+$mod.b;',
  7197. '$mod.s = ""+$mod.i;',
  7198. '$mod.s = rtl.floatToStr($mod.d);',
  7199. '$mod.s = ""+$mod.i+$mod.i;',
  7200. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7201. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7202. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7203. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7204. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7205. '$mod.s = $mod.s + $mod.s;',
  7206. '$mod.s = $mod.s + "foo";',
  7207. '']));
  7208. end;
  7209. procedure TTestModule.TestBaseType_AnsiStringFail;
  7210. begin
  7211. StartProgram(false);
  7212. Add('var s: AnsiString');
  7213. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7214. ConvertProgram;
  7215. end;
  7216. procedure TTestModule.TestBaseType_WideStringFail;
  7217. begin
  7218. StartProgram(false);
  7219. Add('var s: WideString');
  7220. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7221. ConvertProgram;
  7222. end;
  7223. procedure TTestModule.TestBaseType_ShortStringFail;
  7224. begin
  7225. StartProgram(false);
  7226. Add('var s: ShortString');
  7227. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7228. ConvertProgram;
  7229. end;
  7230. procedure TTestModule.TestBaseType_RawByteStringFail;
  7231. begin
  7232. StartProgram(false);
  7233. Add('var s: RawByteString');
  7234. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7235. ConvertProgram;
  7236. end;
  7237. procedure TTestModule.TestTypeShortstring_Fail;
  7238. begin
  7239. StartProgram(false);
  7240. Add('type t = string[12];');
  7241. Add('var s: t;');
  7242. Add('begin');
  7243. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7244. ConvertProgram;
  7245. end;
  7246. procedure TTestModule.TestCharSet_Custom;
  7247. begin
  7248. StartProgram(false);
  7249. Add([
  7250. 'type',
  7251. ' TCharRg = ''a''..''z'';',
  7252. ' TSetOfCharRg = set of TCharRg;',
  7253. ' TCharRg2 = ''m''..''p'';',
  7254. 'const',
  7255. ' crg: TCharRg = ''b'';',
  7256. 'var',
  7257. ' c: char;',
  7258. ' crg2: TCharRg2;',
  7259. ' s: TSetOfCharRg;',
  7260. 'begin',
  7261. ' c:=crg;',
  7262. ' crg:=c;',
  7263. ' crg2:=crg;',
  7264. ' if c=crg then ;',
  7265. ' if crg=c then ;',
  7266. ' if crg=crg2 then ;',
  7267. ' if c in s then ;',
  7268. ' if crg2 in s then ;',
  7269. ' c:=default(TCharRg);',
  7270. '']);
  7271. ConvertProgram;
  7272. CheckSource('TestCharSet_Custom',
  7273. LinesToStr([ // statements
  7274. 'this.crg = "b";',
  7275. 'this.c = "";',
  7276. 'this.crg2 = "m";',
  7277. 'this.s = {};',
  7278. '']),
  7279. LinesToStr([ // this.$main
  7280. '$mod.c = $mod.crg;',
  7281. '$mod.crg = $mod.c;',
  7282. '$mod.crg2 = $mod.crg;',
  7283. 'if ($mod.c === $mod.crg) ;',
  7284. 'if ($mod.crg === $mod.c) ;',
  7285. 'if ($mod.crg === $mod.crg2) ;',
  7286. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7287. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7288. '$mod.c = "a";',
  7289. '']));
  7290. end;
  7291. procedure TTestModule.TestForCharDo;
  7292. begin
  7293. StartProgram(false);
  7294. Add([
  7295. 'var c: char;',
  7296. 'begin',
  7297. ' for c:=''a'' to ''c'' do ;',
  7298. ' for c:=c downto ''a'' do ;',
  7299. ' for c:=''Б'' to ''Я'' do ;',
  7300. '']);
  7301. ConvertProgram;
  7302. CheckSource('TestForCharDo',
  7303. LinesToStr([ // statements
  7304. 'this.c = "";']),
  7305. LinesToStr([ // this.$main
  7306. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  7307. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  7308. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  7309. '']));
  7310. end;
  7311. procedure TTestModule.TestForCharInDo;
  7312. begin
  7313. StartProgram(false);
  7314. Add([
  7315. 'type',
  7316. ' TSetOfChar = set of char;',
  7317. ' TCharRg = ''a''..''z'';',
  7318. ' TSetOfCharRg = set of TCharRg;',
  7319. 'const Foo = ''foo'';',
  7320. 'var',
  7321. ' c,c2: char;',
  7322. ' s: string;',
  7323. ' a1: array of char;',
  7324. ' a2: array[1..3] of char;',
  7325. ' soc: TSetOfChar;',
  7326. ' socr: TSetOfCharRg;',
  7327. ' cr: TCharRg;',
  7328. 'begin',
  7329. ' for c in foo do ;',
  7330. ' for c in s do ;',
  7331. ' for c in char do ;',
  7332. ' for c in a1 do ;',
  7333. ' for c in a2 do ;',
  7334. ' for c in [''1''..''3''] do ;',
  7335. ' for c in TSetOfChar do ;',
  7336. ' for c in TCharRg do ;',
  7337. ' for c in soc do c2:=c;',
  7338. ' for c in TSetOfCharRg do ;',
  7339. ' for c in socr do ;',
  7340. ' for cr in TCharRg do ;',
  7341. ' for cr in TSetOfCharRg do ;',
  7342. ' for cr in socr do ;',
  7343. '']);
  7344. ConvertProgram;
  7345. CheckSource('TestForCharInDo',
  7346. LinesToStr([ // statements
  7347. 'this.Foo = "foo";',
  7348. 'this.c = "";',
  7349. 'this.c2 = "";',
  7350. 'this.s = "";',
  7351. 'this.a1 = [];',
  7352. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7353. 'this.soc = {};',
  7354. 'this.socr = {};',
  7355. 'this.cr = "a";',
  7356. '']),
  7357. LinesToStr([ // this.$main
  7358. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  7359. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  7360. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  7361. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  7362. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  7363. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  7364. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  7365. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  7366. 'for (var $l17 in $mod.soc) {',
  7367. ' $mod.c = String.fromCharCode($l17);',
  7368. ' $mod.c2 = $mod.c;',
  7369. '};',
  7370. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  7371. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  7372. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  7373. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  7374. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  7375. '']));
  7376. end;
  7377. procedure TTestModule.TestProcTwoArgs;
  7378. begin
  7379. StartProgram(false);
  7380. Add('procedure Test(a,b: longint);');
  7381. Add('begin');
  7382. Add('end;');
  7383. Add('begin');
  7384. ConvertProgram;
  7385. CheckSource('TestProcTwoArgs',
  7386. LinesToStr([ // statements
  7387. 'this.Test = function (a,b) {',
  7388. '};'
  7389. ]),
  7390. LinesToStr([ // this.$main
  7391. ''
  7392. ]));
  7393. end;
  7394. procedure TTestModule.TestProc_DefaultValue;
  7395. begin
  7396. StartProgram(false);
  7397. Add('procedure p1(i: longint = 1);');
  7398. Add('begin');
  7399. Add('end;');
  7400. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7401. Add('begin');
  7402. Add('end;');
  7403. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7404. Add('begin');
  7405. Add('end;');
  7406. Add('begin');
  7407. Add(' p1;');
  7408. Add(' p1();');
  7409. Add(' p1(11);');
  7410. Add(' p2;');
  7411. Add(' p2();');
  7412. Add(' p2(12);');
  7413. Add(' p2(13,''b'');');
  7414. Add(' p3();');
  7415. ConvertProgram;
  7416. CheckSource('TestProc_DefaultValue',
  7417. LinesToStr([ // statements
  7418. 'this.p1 = function (i) {',
  7419. '};',
  7420. 'this.p2 = function (i,c) {',
  7421. '};',
  7422. 'this.p3 = function (d,b,s) {',
  7423. '};'
  7424. ]),
  7425. LinesToStr([ // this.$main
  7426. ' $mod.p1(1);',
  7427. ' $mod.p1(1);',
  7428. ' $mod.p1(11);',
  7429. ' $mod.p2(1,"a");',
  7430. ' $mod.p2(1,"a");',
  7431. ' $mod.p2(12,"a");',
  7432. ' $mod.p2(13,"b");',
  7433. ' $mod.p3(1.0,false,"abc");'
  7434. ]));
  7435. end;
  7436. procedure TTestModule.TestFunctionInt;
  7437. begin
  7438. StartProgram(false);
  7439. Add('function MyTest(Bar: longint): longint;');
  7440. Add('begin');
  7441. Add(' Result:=2*bar');
  7442. Add('end;');
  7443. Add('begin');
  7444. ConvertProgram;
  7445. CheckSource('TestFunctionInt',
  7446. LinesToStr([ // statements
  7447. 'this.MyTest = function (Bar) {',
  7448. ' var Result = 0;',
  7449. ' Result = 2*Bar;',
  7450. ' return Result;',
  7451. '};'
  7452. ]),
  7453. LinesToStr([ // this.$main
  7454. ''
  7455. ]));
  7456. end;
  7457. procedure TTestModule.TestFunctionString;
  7458. begin
  7459. StartProgram(false);
  7460. Add('function Test(Bar: string): string;');
  7461. Add('begin');
  7462. Add(' Result:=bar+BAR');
  7463. Add('end;');
  7464. Add('begin');
  7465. ConvertProgram;
  7466. CheckSource('TestFunctionString',
  7467. LinesToStr([ // statements
  7468. 'this.Test = function (Bar) {',
  7469. ' var Result = "";',
  7470. ' Result = Bar+Bar;',
  7471. ' return Result;',
  7472. '};'
  7473. ]),
  7474. LinesToStr([ // this.$main
  7475. ''
  7476. ]));
  7477. end;
  7478. procedure TTestModule.TestIfThen;
  7479. begin
  7480. StartProgram(false);
  7481. Add([
  7482. 'var b: boolean;',
  7483. 'begin',
  7484. ' if b then ;',
  7485. ' if b then else ;']);
  7486. ConvertProgram;
  7487. CheckSource('TestIfThen',
  7488. LinesToStr([ // statements
  7489. 'this.b = false;',
  7490. '']),
  7491. LinesToStr([ // this.$main
  7492. 'if ($mod.b) ;',
  7493. 'if ($mod.b) ;',
  7494. '']));
  7495. end;
  7496. procedure TTestModule.TestForLoop;
  7497. begin
  7498. StartProgram(false);
  7499. Add('var');
  7500. Add(' vI, vJ, vN: longint;');
  7501. Add('begin');
  7502. Add(' VJ:=0;');
  7503. Add(' VN:=3;');
  7504. Add(' for VI:=1 to VN do');
  7505. Add(' begin');
  7506. Add(' VJ:=VJ+VI;');
  7507. Add(' end;');
  7508. ConvertProgram;
  7509. CheckSource('TestForLoop',
  7510. LinesToStr([ // statements
  7511. 'this.vI = 0;',
  7512. 'this.vJ = 0;',
  7513. 'this.vN = 0;'
  7514. ]),
  7515. LinesToStr([ // this.$main
  7516. ' $mod.vJ = 0;',
  7517. ' $mod.vN = 3;',
  7518. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  7519. ' $mod.vI = $l1;',
  7520. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7521. ' };',
  7522. '']));
  7523. end;
  7524. procedure TTestModule.TestForLoopInsideFunction;
  7525. begin
  7526. StartProgram(false);
  7527. Add('function SumNumbers(Count: longint): longint;');
  7528. Add('var');
  7529. Add(' vI, vJ: longint;');
  7530. Add('begin');
  7531. Add(' vj:=0;');
  7532. Add(' for vi:=1 to count do');
  7533. Add(' begin');
  7534. Add(' vj:=vj+vi;');
  7535. Add(' end;');
  7536. Add('end;');
  7537. Add('begin');
  7538. Add(' sumnumbers(3);');
  7539. ConvertProgram;
  7540. CheckSource('TestForLoopInsideFunction',
  7541. LinesToStr([ // statements
  7542. 'this.SumNumbers = function (Count) {',
  7543. ' var Result = 0;',
  7544. ' var vI = 0;',
  7545. ' var vJ = 0;',
  7546. ' vJ = 0;',
  7547. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7548. ' vI = $l1;',
  7549. ' vJ = vJ + vI;',
  7550. ' };',
  7551. ' return Result;',
  7552. '};'
  7553. ]),
  7554. LinesToStr([ // $mod.$main
  7555. ' $mod.SumNumbers(3);'
  7556. ]));
  7557. end;
  7558. procedure TTestModule.TestForLoop_ReadVarAfter;
  7559. begin
  7560. StartProgram(false);
  7561. Add('var');
  7562. Add(' vI: longint;');
  7563. Add('begin');
  7564. Add(' for vi:=1 to 2 do ;');
  7565. Add(' if vi=3 then ;');
  7566. ConvertProgram;
  7567. CheckSource('TestForLoop',
  7568. LinesToStr([ // statements
  7569. 'this.vI = 0;'
  7570. ]),
  7571. LinesToStr([ // this.$main
  7572. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7573. ' if ($mod.vI===3) ;'
  7574. ]));
  7575. end;
  7576. procedure TTestModule.TestForLoop_Nested;
  7577. begin
  7578. StartProgram(false);
  7579. Add('function SumNumbers(Count: longint): longint;');
  7580. Add('var');
  7581. Add(' vI, vJ, vK: longint;');
  7582. Add('begin');
  7583. Add(' VK:=0;');
  7584. Add(' for VI:=1 to count do');
  7585. Add(' begin');
  7586. Add(' for vj:=1 to vi do');
  7587. Add(' begin');
  7588. Add(' vk:=VK+VI;');
  7589. Add(' end;');
  7590. Add(' end;');
  7591. Add('end;');
  7592. Add('begin');
  7593. Add(' sumnumbers(3);');
  7594. ConvertProgram;
  7595. CheckSource('TestForLoopInFunction',
  7596. LinesToStr([ // statements
  7597. 'this.SumNumbers = function (Count) {',
  7598. ' var Result = 0;',
  7599. ' var vI = 0;',
  7600. ' var vJ = 0;',
  7601. ' var vK = 0;',
  7602. ' vK = 0;',
  7603. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7604. ' vI = $l1;',
  7605. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7606. ' vJ = $l3;',
  7607. ' vK = vK + vI;',
  7608. ' };',
  7609. ' };',
  7610. ' return Result;',
  7611. '};'
  7612. ]),
  7613. LinesToStr([ // $mod.$main
  7614. ' $mod.SumNumbers(3);'
  7615. ]));
  7616. end;
  7617. procedure TTestModule.TestRepeatUntil;
  7618. begin
  7619. StartProgram(false);
  7620. Add('var');
  7621. Add(' vI, vJ, vN: longint;');
  7622. Add('begin');
  7623. Add(' vn:=3;');
  7624. Add(' vj:=0;');
  7625. Add(' VI:=0;');
  7626. Add(' repeat');
  7627. Add(' VI:=vi+1;');
  7628. Add(' vj:=VJ+vI;');
  7629. Add(' until vi>=vn');
  7630. ConvertProgram;
  7631. CheckSource('TestRepeatUntil',
  7632. LinesToStr([ // statements
  7633. 'this.vI = 0;',
  7634. 'this.vJ = 0;',
  7635. 'this.vN = 0;'
  7636. ]),
  7637. LinesToStr([ // $mod.$main
  7638. ' $mod.vN = 3;',
  7639. ' $mod.vJ = 0;',
  7640. ' $mod.vI = 0;',
  7641. ' do{',
  7642. ' $mod.vI = $mod.vI + 1;',
  7643. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7644. ' }while(!($mod.vI>=$mod.vN));'
  7645. ]));
  7646. end;
  7647. procedure TTestModule.TestAsmBlock;
  7648. begin
  7649. StartProgram(false);
  7650. Add([
  7651. 'var',
  7652. ' vI: longint;',
  7653. 'begin',
  7654. ' vi:=1;',
  7655. ' asm',
  7656. ' if (vI===1) {',
  7657. ' vI=2;',
  7658. //' console.log(''end;'');', ToDo
  7659. ' }',
  7660. ' if (vI===2){ vI=3; }',
  7661. ' end;',
  7662. ' VI:=4;']);
  7663. ConvertProgram;
  7664. CheckSource('TestAsmBlock',
  7665. LinesToStr([ // statements
  7666. 'this.vI = 0;'
  7667. ]),
  7668. LinesToStr([ // $mod.$main
  7669. '$mod.vI = 1;',
  7670. 'if (vI===1) {',
  7671. ' vI=2;',
  7672. '}',
  7673. 'if (vI===2){ vI=3; }',
  7674. ';',
  7675. '$mod.vI = 4;'
  7676. ]));
  7677. end;
  7678. procedure TTestModule.TestAsmPas_Impl;
  7679. begin
  7680. StartUnit(false);
  7681. Add('interface');
  7682. Add('const cIntf: longint = 1;');
  7683. Add('var vIntf: longint;');
  7684. Add('implementation');
  7685. Add('const cImpl: longint = 2;');
  7686. Add('var vImpl: longint;');
  7687. Add('procedure DoIt;');
  7688. Add('const cLoc: longint = 3;');
  7689. Add('var vLoc: longint;');
  7690. Add('begin;');
  7691. Add(' asm');
  7692. //Add(' pas(vIntf)=pas(cIntf);');
  7693. //Add(' pas(vImpl)=pas(cImpl);');
  7694. //Add(' pas(vLoc)=pas(cLoc);');
  7695. Add(' end;');
  7696. Add('end;');
  7697. ConvertUnit;
  7698. CheckSource('TestAsmPas_Impl',
  7699. LinesToStr([
  7700. 'var $impl = $mod.$impl;',
  7701. 'this.cIntf = 1;',
  7702. 'this.vIntf = 0;',
  7703. '']),
  7704. '', // this.$init
  7705. LinesToStr([ // implementation
  7706. '$impl.cImpl = 2;',
  7707. '$impl.vImpl = 0;',
  7708. 'var cLoc = 3;',
  7709. '$impl.DoIt = function () {',
  7710. ' var vLoc = 0;',
  7711. '};',
  7712. '']) );
  7713. end;
  7714. procedure TTestModule.TestTryFinally;
  7715. begin
  7716. StartProgram(false);
  7717. Add('var i: longint;');
  7718. Add('begin');
  7719. Add(' try');
  7720. Add(' i:=0; i:=2 div i;');
  7721. Add(' finally');
  7722. Add(' i:=3');
  7723. Add(' end;');
  7724. ConvertProgram;
  7725. CheckSource('TestTryFinally',
  7726. LinesToStr([ // statements
  7727. 'this.i = 0;'
  7728. ]),
  7729. LinesToStr([ // $mod.$main
  7730. 'try {',
  7731. ' $mod.i = 0;',
  7732. ' $mod.i = rtl.trunc(2 / $mod.i);',
  7733. '} finally {',
  7734. ' $mod.i = 3;',
  7735. '};'
  7736. ]));
  7737. end;
  7738. procedure TTestModule.TestTryExcept;
  7739. begin
  7740. StartProgram(false);
  7741. Add('type');
  7742. Add(' TObject = class end;');
  7743. Add(' Exception = class Msg: string; end;');
  7744. Add(' EInvalidCast = class(Exception) end;');
  7745. Add('var vI: longint;');
  7746. Add('begin');
  7747. Add(' try');
  7748. Add(' vi:=1;');
  7749. Add(' except');
  7750. Add(' vi:=2');
  7751. Add(' end;');
  7752. Add(' try');
  7753. Add(' vi:=3;');
  7754. Add(' except');
  7755. Add(' raise;');
  7756. Add(' end;');
  7757. Add(' try');
  7758. Add(' VI:=4;');
  7759. Add(' except');
  7760. Add(' on einvalidcast do');
  7761. Add(' raise;');
  7762. Add(' on E: exception do');
  7763. Add(' if e.msg='''' then');
  7764. Add(' raise e;');
  7765. Add(' else');
  7766. Add(' vi:=5');
  7767. Add(' end;');
  7768. Add(' try');
  7769. Add(' VI:=6;');
  7770. Add(' except');
  7771. Add(' on einvalidcast do ;');
  7772. Add(' end;');
  7773. ConvertProgram;
  7774. CheckSource('TestTryExcept',
  7775. LinesToStr([ // statements
  7776. 'rtl.createClass($mod, "TObject", null, function () {',
  7777. ' this.$init = function () {',
  7778. ' };',
  7779. ' this.$final = function () {',
  7780. ' };',
  7781. '});',
  7782. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7783. ' this.$init = function () {',
  7784. ' $mod.TObject.$init.call(this);',
  7785. ' this.Msg = "";',
  7786. ' };',
  7787. '});',
  7788. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7789. '});',
  7790. 'this.vI = 0;'
  7791. ]),
  7792. LinesToStr([ // $mod.$main
  7793. 'try {',
  7794. ' $mod.vI = 1;',
  7795. '} catch ($e) {',
  7796. ' $mod.vI = 2;',
  7797. '};',
  7798. 'try {',
  7799. ' $mod.vI = 3;',
  7800. '} catch ($e) {',
  7801. ' throw $e;',
  7802. '};',
  7803. 'try {',
  7804. ' $mod.vI = 4;',
  7805. '} catch ($e) {',
  7806. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7807. ' throw $e',
  7808. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7809. ' var E = $e;',
  7810. ' if (E.Msg === "") throw E;',
  7811. ' } else {',
  7812. ' $mod.vI = 5;',
  7813. ' }',
  7814. '};',
  7815. 'try {',
  7816. ' $mod.vI = 6;',
  7817. '} catch ($e) {',
  7818. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7819. ' } else throw $e',
  7820. '};',
  7821. '']));
  7822. end;
  7823. procedure TTestModule.TestTryExcept_ReservedWords;
  7824. begin
  7825. StartProgram(false);
  7826. Add([
  7827. 'type',
  7828. ' TObject = class end;',
  7829. ' Exception = class',
  7830. ' Symbol: string;',
  7831. ' end;',
  7832. 'var &try: longint;',
  7833. 'begin',
  7834. ' try',
  7835. ' &try:=4;',
  7836. ' except',
  7837. ' on Error: exception do',
  7838. ' if errOR.symBol='''' then',
  7839. ' raise ERRor;',
  7840. ' end;',
  7841. '']);
  7842. ConvertProgram;
  7843. CheckSource('TestTryExcept_ReservedWords',
  7844. LinesToStr([ // statements
  7845. 'rtl.createClass($mod, "TObject", null, function () {',
  7846. ' this.$init = function () {',
  7847. ' };',
  7848. ' this.$final = function () {',
  7849. ' };',
  7850. '});',
  7851. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7852. ' this.$init = function () {',
  7853. ' $mod.TObject.$init.call(this);',
  7854. ' this.Symbol = "";',
  7855. ' };',
  7856. '});',
  7857. 'this.Try = 0;',
  7858. '']),
  7859. LinesToStr([ // $mod.$main
  7860. 'try {',
  7861. ' $mod.Try = 4;',
  7862. '} catch ($e) {',
  7863. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7864. ' var error = $e;',
  7865. ' if (error.Symbol === "") throw error;',
  7866. ' } else throw $e',
  7867. '};',
  7868. '']));
  7869. end;
  7870. procedure TTestModule.TestIfThenRaiseElse;
  7871. begin
  7872. StartProgram(false);
  7873. Add([
  7874. 'type',
  7875. ' TObject = class',
  7876. ' constructor Create;',
  7877. ' end;',
  7878. 'constructor TObject.Create;',
  7879. 'begin',
  7880. 'end;',
  7881. 'var b: boolean;',
  7882. 'begin',
  7883. ' if b then',
  7884. ' raise TObject.Create',
  7885. ' else',
  7886. ' b:=false;',
  7887. '']);
  7888. ConvertProgram;
  7889. CheckSource('TestIfThenRaiseElse',
  7890. LinesToStr([ // statements
  7891. 'rtl.createClass($mod, "TObject", null, function () {',
  7892. ' this.$init = function () {',
  7893. ' };',
  7894. ' this.$final = function () {',
  7895. ' };',
  7896. ' this.Create = function () {',
  7897. ' return this;',
  7898. ' };',
  7899. '});',
  7900. 'this.b = false;',
  7901. '']),
  7902. LinesToStr([ // $mod.$main
  7903. 'if ($mod.b) {',
  7904. ' throw $mod.TObject.$create("Create")}',
  7905. ' else $mod.b = false;',
  7906. '']));
  7907. end;
  7908. procedure TTestModule.TestCaseOf;
  7909. begin
  7910. StartProgram(false);
  7911. Add([
  7912. 'const e: longint; external name ''$e'';',
  7913. 'var vI: longint;',
  7914. 'begin',
  7915. ' case vi of',
  7916. ' 1: ;',
  7917. ' 2: vi:=3;',
  7918. ' e: ;',
  7919. ' else',
  7920. ' VI:=4',
  7921. ' end;']);
  7922. ConvertProgram;
  7923. CheckSource('TestCaseOf',
  7924. LinesToStr([ // statements
  7925. 'this.vI = 0;'
  7926. ]),
  7927. LinesToStr([ // $mod.$main
  7928. 'var $tmp1 = $mod.vI;',
  7929. 'if ($tmp1 === 1) {}',
  7930. 'else if ($tmp1 === 2) {',
  7931. ' $mod.vI = 3}',
  7932. ' else if ($tmp1 === $e) {}',
  7933. 'else {',
  7934. ' $mod.vI = 4;',
  7935. '};'
  7936. ]));
  7937. end;
  7938. procedure TTestModule.TestCaseOf_UseSwitch;
  7939. begin
  7940. StartProgram(false);
  7941. Converter.UseSwitchStatement:=true;
  7942. Add('var Vi: longint;');
  7943. Add('begin');
  7944. Add(' case vi of');
  7945. Add(' 1: ;');
  7946. Add(' 2: VI:=3;');
  7947. Add(' else');
  7948. Add(' vi:=4');
  7949. Add(' end;');
  7950. ConvertProgram;
  7951. CheckSource('TestCaseOf_UseSwitch',
  7952. LinesToStr([ // statements
  7953. 'this.Vi = 0;'
  7954. ]),
  7955. LinesToStr([ // $mod.$main
  7956. 'switch ($mod.Vi) {',
  7957. 'case 1:',
  7958. ' break;',
  7959. 'case 2:',
  7960. ' $mod.Vi = 3;',
  7961. ' break;',
  7962. 'default:',
  7963. ' $mod.Vi = 4;',
  7964. '};'
  7965. ]));
  7966. end;
  7967. procedure TTestModule.TestCaseOfNoElse;
  7968. begin
  7969. StartProgram(false);
  7970. Add('var Vi: longint;');
  7971. Add('begin');
  7972. Add(' case vi of');
  7973. Add(' 1: begin vi:=2; VI:=3; end;');
  7974. Add(' end;');
  7975. ConvertProgram;
  7976. CheckSource('TestCaseOfNoElse',
  7977. LinesToStr([ // statements
  7978. 'this.Vi = 0;'
  7979. ]),
  7980. LinesToStr([ // $mod.$main
  7981. 'var $tmp1 = $mod.Vi;',
  7982. 'if ($tmp1 === 1) {',
  7983. ' $mod.Vi = 2;',
  7984. ' $mod.Vi = 3;',
  7985. '};'
  7986. ]));
  7987. end;
  7988. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7989. begin
  7990. StartProgram(false);
  7991. Converter.UseSwitchStatement:=true;
  7992. Add('var vI: longint;');
  7993. Add('begin');
  7994. Add(' case vi of');
  7995. Add(' 1: begin VI:=2; vi:=3; end;');
  7996. Add(' end;');
  7997. ConvertProgram;
  7998. CheckSource('TestCaseOfNoElse_UseSwitch',
  7999. LinesToStr([ // statements
  8000. 'this.vI = 0;'
  8001. ]),
  8002. LinesToStr([ // $mod.$main
  8003. 'switch ($mod.vI) {',
  8004. 'case 1:',
  8005. ' $mod.vI = 2;',
  8006. ' $mod.vI = 3;',
  8007. ' break;',
  8008. '};'
  8009. ]));
  8010. end;
  8011. procedure TTestModule.TestCaseOfRange;
  8012. begin
  8013. StartProgram(false);
  8014. Add('var vI: longint;');
  8015. Add('begin');
  8016. Add(' case vi of');
  8017. Add(' 1..3: vi:=14;');
  8018. Add(' 4,5: vi:=16;');
  8019. Add(' 6..7,9..10: ;');
  8020. Add(' else ;');
  8021. Add(' end;');
  8022. ConvertProgram;
  8023. CheckSource('TestCaseOfRange',
  8024. LinesToStr([ // statements
  8025. 'this.vI = 0;'
  8026. ]),
  8027. LinesToStr([ // $mod.$main
  8028. 'var $tmp1 = $mod.vI;',
  8029. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  8030. ' $mod.vI = 14',
  8031. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  8032. ' $mod.vI = 16',
  8033. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  8034. ]));
  8035. end;
  8036. procedure TTestModule.TestCaseOfString;
  8037. begin
  8038. StartProgram(false);
  8039. Add([
  8040. 'var s,h: string;',
  8041. 'begin',
  8042. ' case s of',
  8043. ' ''foo'': s:=h;',
  8044. ' ''a''..''z'': h:=s;',
  8045. ' ''ў'', ''ё'': ;',
  8046. ' ''Б''..''Я'': ;',
  8047. ' end;',
  8048. '']);
  8049. ConvertProgram;
  8050. CheckSource('TestCaseOfString',
  8051. LinesToStr([ // statements
  8052. 'this.s = "";',
  8053. 'this.h = "";',
  8054. '']),
  8055. LinesToStr([ // $mod.$main
  8056. 'var $tmp1 = $mod.s;',
  8057. 'if ($tmp1 === "foo") {',
  8058. ' $mod.s = $mod.h}',
  8059. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  8060. ' $mod.h = $mod.s}',
  8061. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  8062. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  8063. '']));
  8064. end;
  8065. procedure TTestModule.TestCaseOfChar;
  8066. begin
  8067. StartProgram(false);
  8068. Add([
  8069. 'var s,h: char;',
  8070. 'begin',
  8071. ' case s of',
  8072. ' ''a''..''z'': h:=s;',
  8073. ' ''ä'': ;',
  8074. ' ''ў'', ''ё'': ;',
  8075. ' ''Б''..''Я'': ;',
  8076. ' end;',
  8077. '']);
  8078. ConvertProgram;
  8079. CheckSource('TestCaseOfString',
  8080. LinesToStr([ // statements
  8081. 'this.s = "";',
  8082. 'this.h = "";',
  8083. '']),
  8084. LinesToStr([ // $mod.$main
  8085. 'var $tmp1 = $mod.s;',
  8086. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  8087. ' $mod.h = $mod.s}',
  8088. ' else if ($tmp1 === "ä") {}',
  8089. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  8090. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  8091. '']));
  8092. end;
  8093. procedure TTestModule.TestCaseOfExternalClassConst;
  8094. begin
  8095. StartProgram(false);
  8096. Add([
  8097. '{$modeswitch externalclass}',
  8098. 'type',
  8099. ' TBird = class external name ''Bird''',
  8100. ' const e: longint;',
  8101. ' end;',
  8102. 'var vI: longint;',
  8103. 'begin',
  8104. ' case vi of',
  8105. ' 1: vi:=3;',
  8106. ' TBird.e: ;',
  8107. ' end;']);
  8108. ConvertProgram;
  8109. CheckSource('TestCaseOfExternalClassConst',
  8110. LinesToStr([ // statements
  8111. 'this.vI = 0;'
  8112. ]),
  8113. LinesToStr([ // $mod.$main
  8114. 'var $tmp1 = $mod.vI;',
  8115. 'if ($tmp1 === 1) {',
  8116. ' $mod.vI = 3}',
  8117. ' else if ($tmp1 === Bird.e) ;'
  8118. ]));
  8119. end;
  8120. procedure TTestModule.TestDebugger;
  8121. begin
  8122. StartProgram(false);
  8123. Add([
  8124. 'procedure DoIt;',
  8125. 'begin',
  8126. ' deBugger;',
  8127. ' DeBugger();',
  8128. 'end;',
  8129. 'begin',
  8130. ' Debugger;']);
  8131. ConvertProgram;
  8132. CheckSource('TestDebugger',
  8133. LinesToStr([ // statements
  8134. 'this.DoIt = function () {',
  8135. ' debugger;',
  8136. ' debugger;',
  8137. '};',
  8138. '']),
  8139. LinesToStr([ // $mod.$main
  8140. 'debugger;',
  8141. '']));
  8142. end;
  8143. procedure TTestModule.TestArray_Dynamic;
  8144. begin
  8145. StartProgram(false);
  8146. Add([
  8147. 'type',
  8148. ' TArrayInt = array of longint;',
  8149. 'var',
  8150. ' Arr: TArrayInt;',
  8151. ' i: longint;',
  8152. ' b: boolean;',
  8153. 'begin',
  8154. ' SetLength(arr,3);',
  8155. ' arr[0]:=4;',
  8156. ' arr[1]:=length(arr)+arr[0];',
  8157. ' arr[i]:=5;',
  8158. ' arr[arr[i]]:=arr[6];',
  8159. ' i:=low(arr);',
  8160. ' i:=high(arr);',
  8161. ' b:=Assigned(arr);',
  8162. ' Arr:=default(TArrayInt);']);
  8163. ConvertProgram;
  8164. CheckSource('TestArray_Dynamic',
  8165. LinesToStr([ // statements
  8166. 'this.Arr = [];',
  8167. 'this.i = 0;',
  8168. 'this.b = false;'
  8169. ]),
  8170. LinesToStr([ // $mod.$main
  8171. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8172. '$mod.Arr[0] = 4;',
  8173. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8174. '$mod.Arr[$mod.i] = 5;',
  8175. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8176. '$mod.i = 0;',
  8177. '$mod.i = rtl.length($mod.Arr) - 1;',
  8178. '$mod.b = rtl.length($mod.Arr) > 0;',
  8179. '$mod.Arr = [];',
  8180. '']));
  8181. end;
  8182. procedure TTestModule.TestArray_Dynamic_Nil;
  8183. begin
  8184. StartProgram(false);
  8185. Add('type');
  8186. Add(' TArrayInt = array of longint;');
  8187. Add('var');
  8188. Add(' Arr: TArrayInt;');
  8189. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8190. Add('begin');
  8191. Add(' arr:=nil;');
  8192. Add(' if arr=nil then;');
  8193. Add(' if nil=arr then;');
  8194. Add(' if arr<>nil then;');
  8195. Add(' if nil<>arr then;');
  8196. Add(' DoIt(nil,nil);');
  8197. ConvertProgram;
  8198. CheckSource('TestArray_Dynamic',
  8199. LinesToStr([ // statements
  8200. 'this.Arr = [];',
  8201. 'this.DoIt = function(i,j){',
  8202. '};'
  8203. ]),
  8204. LinesToStr([ // $mod.$main
  8205. '$mod.Arr = [];',
  8206. 'if (rtl.length($mod.Arr) === 0) ;',
  8207. 'if (rtl.length($mod.Arr) === 0) ;',
  8208. 'if (rtl.length($mod.Arr) > 0) ;',
  8209. 'if (rtl.length($mod.Arr) > 0) ;',
  8210. '$mod.DoIt([],[]);',
  8211. '']));
  8212. end;
  8213. procedure TTestModule.TestArray_DynMultiDimensional;
  8214. begin
  8215. StartProgram(false);
  8216. Add('type');
  8217. Add(' TArrayInt = array of longint;');
  8218. Add(' TArrayArrayInt = array of TArrayInt;');
  8219. Add('var');
  8220. Add(' Arr: TArrayInt;');
  8221. Add(' Arr2: TArrayArrayInt;');
  8222. Add(' i: longint;');
  8223. Add('begin');
  8224. Add(' arr2:=nil;');
  8225. Add(' if arr2=nil then;');
  8226. Add(' if nil=arr2 then;');
  8227. Add(' i:=low(arr2);');
  8228. Add(' i:=low(arr2[1]);');
  8229. Add(' i:=high(arr2);');
  8230. Add(' i:=high(arr2[2]);');
  8231. Add(' arr2[3]:=arr;');
  8232. Add(' arr2[4][5]:=i;');
  8233. Add(' i:=arr2[6][7];');
  8234. Add(' arr2[8,9]:=i;');
  8235. Add(' i:=arr2[10,11];');
  8236. Add(' SetLength(arr2,14);');
  8237. Add(' SetLength(arr2[15],16);');
  8238. ConvertProgram;
  8239. CheckSource('TestArray_Dynamic',
  8240. LinesToStr([ // statements
  8241. 'this.Arr = [];',
  8242. 'this.Arr2 = [];',
  8243. 'this.i = 0;'
  8244. ]),
  8245. LinesToStr([ // $mod.$main
  8246. '$mod.Arr2 = [];',
  8247. 'if (rtl.length($mod.Arr2) === 0) ;',
  8248. 'if (rtl.length($mod.Arr2) === 0) ;',
  8249. '$mod.i = 0;',
  8250. '$mod.i = 0;',
  8251. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8252. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8253. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8254. '$mod.Arr2[4][5] = $mod.i;',
  8255. '$mod.i = $mod.Arr2[6][7];',
  8256. '$mod.Arr2[8][9] = $mod.i;',
  8257. '$mod.i = $mod.Arr2[10][11];',
  8258. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8259. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8260. '']));
  8261. end;
  8262. procedure TTestModule.TestArray_StaticInt;
  8263. begin
  8264. StartProgram(false);
  8265. Add('type');
  8266. Add(' TArrayInt = array[2..4] of longint;');
  8267. Add('var');
  8268. Add(' Arr: TArrayInt;');
  8269. Add(' Arr2: TArrayInt = (5,6,7);');
  8270. Add(' i: longint;');
  8271. Add(' b: boolean;');
  8272. Add('begin');
  8273. Add(' arr[2]:=4;');
  8274. Add(' arr[3]:=arr[2]+arr[3];');
  8275. Add(' arr[i]:=5;');
  8276. Add(' arr[arr[i]]:=arr[high(arr)];');
  8277. Add(' i:=low(arr);');
  8278. Add(' i:=high(arr);');
  8279. Add(' b:=arr[2]=arr[3];');
  8280. Add(' arr:=default(TArrayInt);');
  8281. ConvertProgram;
  8282. CheckSource('TestArray_StaticInt',
  8283. LinesToStr([ // statements
  8284. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8285. 'this.Arr2 = [5, 6, 7];',
  8286. 'this.i = 0;',
  8287. 'this.b = false;'
  8288. ]),
  8289. LinesToStr([ // $mod.$main
  8290. '$mod.Arr[0] = 4;',
  8291. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8292. '$mod.Arr[$mod.i-2] = 5;',
  8293. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8294. '$mod.i = 2;',
  8295. '$mod.i = 4;',
  8296. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8297. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8298. '']));
  8299. end;
  8300. procedure TTestModule.TestArray_StaticBool;
  8301. begin
  8302. StartProgram(false);
  8303. Add('type');
  8304. Add(' TBools = array[boolean] of boolean;');
  8305. Add(' TBool2 = array[true..true] of boolean;');
  8306. Add('var');
  8307. Add(' Arr: TBools;');
  8308. Add(' Arr2: TBool2;');
  8309. Add(' Arr3: TBools = (true,false);');
  8310. Add(' b: boolean;');
  8311. Add('begin');
  8312. Add(' b:=low(arr);');
  8313. Add(' b:=high(arr);');
  8314. Add(' arr[true]:=false;');
  8315. Add(' arr[false]:=arr[b] or arr[true];');
  8316. Add(' arr[b]:=true;');
  8317. Add(' arr[arr[b]]:=arr[high(arr)];');
  8318. Add(' b:=arr[false]=arr[true];');
  8319. Add(' b:=low(arr2);');
  8320. Add(' b:=high(arr2);');
  8321. Add(' arr2[true]:=true;');
  8322. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8323. Add(' arr2[b]:=false;');
  8324. ConvertProgram;
  8325. CheckSource('TestArray_StaticBool',
  8326. LinesToStr([ // statements
  8327. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8328. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8329. 'this.Arr3 = [true, false];',
  8330. 'this.b = false;'
  8331. ]),
  8332. LinesToStr([ // $mod.$main
  8333. '$mod.b = false;',
  8334. '$mod.b = true;',
  8335. '$mod.Arr[1] = false;',
  8336. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8337. '$mod.Arr[+$mod.b] = true;',
  8338. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8339. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8340. '$mod.b = true;',
  8341. '$mod.b = true;',
  8342. '$mod.Arr2[0] = true;',
  8343. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8344. '$mod.Arr2[1-$mod.b] = false;',
  8345. '']));
  8346. end;
  8347. procedure TTestModule.TestArray_StaticChar;
  8348. begin
  8349. StartProgram(false);
  8350. Add([
  8351. 'type',
  8352. ' TChars = array[char] of char;',
  8353. ' TChars2 = array[''a''..''z''] of char;',
  8354. 'var',
  8355. ' Arr: TChars;',
  8356. ' Arr2: TChars2;',
  8357. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8358. ' Arr4: array[11..13] of char = ''pas'';',
  8359. ' Arr5: array[21..22] of char = ''äö'';',
  8360. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8361. ' c: char;',
  8362. ' b: boolean;',
  8363. 'begin',
  8364. ' c:=low(arr);',
  8365. ' c:=high(arr);',
  8366. ' arr[''B'']:=''a'';',
  8367. ' arr[''D'']:=arr[c];',
  8368. ' arr[c]:=arr[''d''];',
  8369. ' arr[arr[c]]:=arr[high(arr)];',
  8370. ' b:=arr[low(arr)]=arr[''e''];',
  8371. ' c:=low(arr2);',
  8372. ' c:=high(arr2);',
  8373. ' arr2[''b'']:=''f'';',
  8374. ' arr2[''a'']:=arr2[c];',
  8375. ' arr2[c]:=arr2[''g''];']);
  8376. ConvertProgram;
  8377. CheckSource('TestArray_StaticChar',
  8378. LinesToStr([ // statements
  8379. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8380. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8381. 'this.Arr3 = ["p", "a", "s"];',
  8382. 'this.Arr4 = ["p", "a", "s"];',
  8383. 'this.Arr5 = ["ä", "ö"];',
  8384. 'this.Arr6 = ["ä", "ö"];',
  8385. 'this.c = "";',
  8386. 'this.b = false;',
  8387. '']),
  8388. LinesToStr([ // $mod.$main
  8389. '$mod.c = "\x00";',
  8390. '$mod.c = "\uFFFF";',
  8391. '$mod.Arr[66] = "a";',
  8392. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8393. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8394. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8395. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8396. '$mod.c = "a";',
  8397. '$mod.c = "z";',
  8398. '$mod.Arr2[1] = "f";',
  8399. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8400. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8401. '']));
  8402. end;
  8403. procedure TTestModule.TestArray_StaticMultiDim;
  8404. begin
  8405. StartProgram(false);
  8406. Add([
  8407. 'type',
  8408. ' TArrayInt = array[1..3] of longint;',
  8409. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8410. 'var',
  8411. ' Arr: TArrayInt;',
  8412. ' Arr2: TArrayArrayInt;',
  8413. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8414. ' i: longint;',
  8415. 'begin',
  8416. ' i:=low(arr);',
  8417. ' i:=low(arr2);',
  8418. ' i:=low(arr2[5]);',
  8419. ' i:=high(arr);',
  8420. ' i:=high(arr2);',
  8421. ' i:=high(arr2[6]);',
  8422. ' arr2[5]:=arr;',
  8423. ' arr2[6][2]:=i;',
  8424. ' i:=arr2[6][3];',
  8425. ' arr2[6,3]:=i;',
  8426. ' i:=arr2[5,2];',
  8427. ' arr2:=arr2;',// clone multi dim static array
  8428. //' arr3:=arr3;',// clone anonymous multi dim static array
  8429. '']);
  8430. ConvertProgram;
  8431. CheckSource('TestArray_StaticMultiDim',
  8432. LinesToStr([ // statements
  8433. 'this.TArrayArrayInt$clone = function (a) {',
  8434. ' var r = [];',
  8435. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8436. ' return r;',
  8437. '};',
  8438. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8439. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8440. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8441. 'this.i = 0;'
  8442. ]),
  8443. LinesToStr([ // $mod.$main
  8444. '$mod.i = 1;',
  8445. '$mod.i = 5;',
  8446. '$mod.i = 1;',
  8447. '$mod.i = 3;',
  8448. '$mod.i = 6;',
  8449. '$mod.i = 3;',
  8450. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8451. '$mod.Arr2[1][1] = $mod.i;',
  8452. '$mod.i = $mod.Arr2[1][2];',
  8453. '$mod.Arr2[1][2] = $mod.i;',
  8454. '$mod.i = $mod.Arr2[0][1];',
  8455. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8456. '']));
  8457. end;
  8458. procedure TTestModule.TestArray_StaticInFunction;
  8459. begin
  8460. StartProgram(false);
  8461. Add([
  8462. 'const TArrayInt = 3;',
  8463. 'const TArrayArrayInt = 4;',
  8464. 'procedure DoIt;',
  8465. 'type',
  8466. ' TArrayInt = array[1..3] of longint;',
  8467. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8468. 'var',
  8469. ' Arr: TArrayInt;',
  8470. ' Arr2: TArrayArrayInt;',
  8471. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8472. ' i: longint;',
  8473. 'begin',
  8474. ' arr2[5]:=arr;',
  8475. ' arr2:=arr2;',// clone multi dim static array
  8476. 'end;',
  8477. 'begin',
  8478. '']);
  8479. ConvertProgram;
  8480. CheckSource('TestArray_StaticInFunction',
  8481. LinesToStr([ // statements
  8482. 'this.TArrayInt = 3;',
  8483. 'this.TArrayArrayInt = 4;',
  8484. 'var TArrayArrayInt$1$clone = function (a) {',
  8485. ' var r = [];',
  8486. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8487. ' return r;',
  8488. '};',
  8489. 'this.DoIt = function () {',
  8490. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8491. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8492. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8493. ' var i = 0;',
  8494. ' Arr2[0] = Arr.slice(0);',
  8495. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8496. '};',
  8497. '']),
  8498. LinesToStr([ // $mod.$main
  8499. '']));
  8500. end;
  8501. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  8502. begin
  8503. StartProgram(false);
  8504. Add([
  8505. 'type',
  8506. ' TArrayInt = array[1..3,1..2] of longint;',
  8507. 'var',
  8508. ' a,b: TArrayInt;',
  8509. 'begin',
  8510. ' if a=b then ;',
  8511. '']);
  8512. SetExpectedPasResolverError('compare static array is not supported',
  8513. nXIsNotSupported);
  8514. ConvertProgram;
  8515. end;
  8516. procedure TTestModule.TestArrayOfRecord;
  8517. begin
  8518. StartProgram(false);
  8519. Add([
  8520. 'type',
  8521. ' TRec = record',
  8522. ' Int: longint;',
  8523. ' end;',
  8524. ' TArrayRec = array of TRec;',
  8525. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8526. 'begin',
  8527. 'end;',
  8528. 'var',
  8529. ' Arr: TArrayRec;',
  8530. ' r: TRec;',
  8531. ' i: longint;',
  8532. 'begin',
  8533. ' SetLength(arr,3);',
  8534. ' arr[0].int:=4;',
  8535. ' arr[1].int:=length(arr)+arr[2].int;',
  8536. ' arr[arr[i].int].int:=arr[5].int;',
  8537. ' arr[7]:=r;',
  8538. ' r:=arr[8];',
  8539. ' i:=low(arr);',
  8540. ' i:=high(arr);',
  8541. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8542. ConvertProgram;
  8543. CheckSource('TestArrayOfRecord',
  8544. LinesToStr([ // statements
  8545. 'rtl.recNewT($mod, "TRec", function () {',
  8546. ' this.Int = 0;',
  8547. ' this.$eq = function (b) {',
  8548. ' return this.Int === b.Int;',
  8549. ' };',
  8550. ' this.$assign = function (s) {',
  8551. ' this.Int = s.Int;',
  8552. ' return this;',
  8553. ' };',
  8554. '});',
  8555. 'this.DoIt = function (vd, vc, vv) {',
  8556. '};',
  8557. 'this.Arr = [];',
  8558. 'this.r = $mod.TRec.$new();',
  8559. 'this.i = 0;'
  8560. ]),
  8561. LinesToStr([ // $mod.$main
  8562. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8563. '$mod.Arr[0].Int = 4;',
  8564. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8565. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8566. '$mod.Arr[7].$assign($mod.r);',
  8567. '$mod.r.$assign($mod.Arr[8]);',
  8568. '$mod.i = 0;',
  8569. '$mod.i = rtl.length($mod.Arr)-1;',
  8570. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8571. '']));
  8572. end;
  8573. procedure TTestModule.TestArray_StaticRecord;
  8574. begin
  8575. StartProgram(false);
  8576. Add([
  8577. 'type',
  8578. ' TRec = record',
  8579. ' Int: longint;',
  8580. ' end;',
  8581. ' TArrayRec = array[1..2] of TRec;',
  8582. 'var',
  8583. ' Arr: TArrayRec;',
  8584. 'begin',
  8585. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8586. '']);
  8587. ConvertProgram;
  8588. CheckSource('TestArray_StaticRecord',
  8589. LinesToStr([ // statements
  8590. 'rtl.recNewT($mod, "TRec", function () {',
  8591. ' this.Int = 0;',
  8592. ' this.$eq = function (b) {',
  8593. ' return this.Int === b.Int;',
  8594. ' };',
  8595. ' this.$assign = function (s) {',
  8596. ' this.Int = s.Int;',
  8597. ' return this;',
  8598. ' };',
  8599. '});',
  8600. 'this.TArrayRec$clone = function (a) {',
  8601. ' var r = [];',
  8602. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8603. ' return r;',
  8604. '};',
  8605. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8606. '']),
  8607. LinesToStr([ // $mod.$main
  8608. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8609. end;
  8610. procedure TTestModule.TestArrayOfSet;
  8611. begin
  8612. StartProgram(false);
  8613. Add([
  8614. 'type',
  8615. ' TFlag = (big,small);',
  8616. ' TSetOfFlag = set of tflag;',
  8617. ' TArrayFlag = array of TSetOfFlag;',
  8618. 'procedure DoIt(const a: Tarrayflag);',
  8619. 'begin',
  8620. 'end;',
  8621. 'var',
  8622. ' f: TFlag;',
  8623. ' s: TSetOfFlag;',
  8624. ' Arr: TArrayFlag;',
  8625. ' i: longint;',
  8626. 'begin',
  8627. ' SetLength(arr,3);',
  8628. ' arr[0]:=s;',
  8629. ' arr[1]:=[big];',
  8630. ' arr[2]:=[big]+s;',
  8631. ' arr[3]:=s+[big];',
  8632. ' arr[4]:=arr[5];',
  8633. ' s:=arr[6];',
  8634. ' i:=low(arr);',
  8635. ' i:=high(arr);',
  8636. ' DoIt(arr);',
  8637. ' DoIt([s]);',
  8638. ' DoIt([[],s]);',
  8639. ' DoIt([s,[]]);',
  8640. '']);
  8641. ConvertProgram;
  8642. CheckSource('TestArrayOfSet',
  8643. LinesToStr([ // statements
  8644. 'this.TFlag = {',
  8645. ' "0": "big",',
  8646. ' big: 0,',
  8647. ' "1": "small",',
  8648. ' small: 1',
  8649. '};',
  8650. 'this.DoIt = function (a) {',
  8651. '};',
  8652. 'this.f = 0;',
  8653. 'this.s = {};',
  8654. 'this.Arr = [];',
  8655. 'this.i = 0;',
  8656. '']),
  8657. LinesToStr([ // $mod.$main
  8658. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8659. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8660. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8661. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8662. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8663. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8664. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8665. '$mod.i = 0;',
  8666. '$mod.i = rtl.length($mod.Arr) - 1;',
  8667. '$mod.DoIt($mod.Arr);',
  8668. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8669. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8670. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8671. '']));
  8672. end;
  8673. procedure TTestModule.TestArray_DynAsParam;
  8674. begin
  8675. StartProgram(false);
  8676. Add([
  8677. 'type integer = longint;',
  8678. 'type TArrInt = array of integer;',
  8679. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8680. 'var vJ: TArrInt;',
  8681. 'begin',
  8682. ' vg:=vg;',
  8683. ' vj:=vh;',
  8684. ' vi:=vi;',
  8685. ' doit(vg,vg,vg);',
  8686. ' doit(vh,vh,vj);',
  8687. ' doit(vi,vi,vi);',
  8688. ' doit(vj,vj,vj);',
  8689. 'end;',
  8690. 'var i: TArrInt;',
  8691. 'begin',
  8692. ' doit(i,i,i);']);
  8693. ConvertProgram;
  8694. CheckSource('TestArray_DynAsParams',
  8695. LinesToStr([ // statements
  8696. 'this.DoIt = function (vG,vH,vI) {',
  8697. ' var vJ = [];',
  8698. ' vG = rtl.arrayRef(vG);',
  8699. ' vJ = rtl.arrayRef(vH);',
  8700. ' vI.set(rtl.arrayRef(vI.get()));',
  8701. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  8702. ' get: function () {',
  8703. ' return vG;',
  8704. ' },',
  8705. ' set: function (v) {',
  8706. ' vG = v;',
  8707. ' }',
  8708. ' });',
  8709. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  8710. ' get: function () {',
  8711. ' return vJ;',
  8712. ' },',
  8713. ' set: function (v) {',
  8714. ' vJ = v;',
  8715. ' }',
  8716. ' });',
  8717. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  8718. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  8719. ' get: function () {',
  8720. ' return vJ;',
  8721. ' },',
  8722. ' set: function (v) {',
  8723. ' vJ = v;',
  8724. ' }',
  8725. ' });',
  8726. '};',
  8727. 'this.i = [];'
  8728. ]),
  8729. LinesToStr([
  8730. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  8731. ' p: $mod,',
  8732. ' get: function () {',
  8733. ' return this.p.i;',
  8734. ' },',
  8735. ' set: function (v) {',
  8736. ' this.p.i = v;',
  8737. ' }',
  8738. '});'
  8739. ]));
  8740. end;
  8741. procedure TTestModule.TestArray_StaticAsParam;
  8742. begin
  8743. StartProgram(false);
  8744. Add([
  8745. 'type integer = longint;',
  8746. 'type TArrInt = array[1..2] of integer;',
  8747. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8748. 'var vJ: TArrInt;',
  8749. 'begin',
  8750. ' vg:=vg;',
  8751. ' vj:=vh;',
  8752. ' vi:=vi;',
  8753. ' doit(vg,vg,vg);',
  8754. ' doit(vh,vh,vj);',
  8755. ' doit(vi,vi,vi);',
  8756. ' doit(vj,vj,vj);',
  8757. 'end;',
  8758. 'var i: TArrInt;',
  8759. 'begin',
  8760. ' doit(i,i,i);']);
  8761. ConvertProgram;
  8762. CheckSource('TestArray_StaticAsParams',
  8763. LinesToStr([ // statements
  8764. 'this.DoIt = function (vG,vH,vI) {',
  8765. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8766. ' vG = vG.slice(0);',
  8767. ' vJ = vH.slice(0);',
  8768. ' vI.set(vI.get().slice(0));',
  8769. ' $mod.DoIt(vG.slice(0), vG, {',
  8770. ' get: function () {',
  8771. ' return vG;',
  8772. ' },',
  8773. ' set: function (v) {',
  8774. ' vG = v;',
  8775. ' }',
  8776. ' });',
  8777. ' $mod.DoIt(vH.slice(0), vH, {',
  8778. ' get: function () {',
  8779. ' return vJ;',
  8780. ' },',
  8781. ' set: function (v) {',
  8782. ' vJ = v;',
  8783. ' }',
  8784. ' });',
  8785. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8786. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8787. ' get: function () {',
  8788. ' return vJ;',
  8789. ' },',
  8790. ' set: function (v) {',
  8791. ' vJ = v;',
  8792. ' }',
  8793. ' });',
  8794. '};',
  8795. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8796. ]),
  8797. LinesToStr([
  8798. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  8799. ' p: $mod,',
  8800. ' get: function () {',
  8801. ' return this.p.i;',
  8802. ' },',
  8803. ' set: function (v) {',
  8804. ' this.p.i = v;',
  8805. ' }',
  8806. '});'
  8807. ]));
  8808. end;
  8809. procedure TTestModule.TestArrayElement_AsParams;
  8810. begin
  8811. StartProgram(false);
  8812. Add('type integer = longint;');
  8813. Add('type TArrayInt = array of integer;');
  8814. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  8815. Add('var vJ: tarrayint;');
  8816. Add('begin');
  8817. Add(' vi:=vi;');
  8818. Add(' doit(vi,vi,vi);');
  8819. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  8820. Add('end;');
  8821. Add('var a: TArrayInt;');
  8822. Add('begin');
  8823. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  8824. ConvertProgram;
  8825. CheckSource('TestArrayElement_AsParams',
  8826. LinesToStr([ // statements
  8827. 'this.DoIt = function (vG,vH,vI) {',
  8828. ' var vJ = [];',
  8829. ' vI.set(vI.get());',
  8830. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8831. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  8832. ' a:1+3,',
  8833. ' p:vJ,',
  8834. ' get: function () {',
  8835. ' return this.p[this.a];',
  8836. ' },',
  8837. ' set: function (v) {',
  8838. ' this.p[this.a] = v;',
  8839. ' }',
  8840. ' });',
  8841. '};',
  8842. 'this.a = [];'
  8843. ]),
  8844. LinesToStr([
  8845. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8846. ' a: 1+6,',
  8847. ' p: $mod.a,',
  8848. ' get: function () {',
  8849. ' return this.p[this.a];',
  8850. ' },',
  8851. ' set: function (v) {',
  8852. ' this.p[this.a] = v;',
  8853. ' }',
  8854. '});'
  8855. ]));
  8856. end;
  8857. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8858. begin
  8859. StartProgram(false);
  8860. Add('type Integer = longint;');
  8861. Add('type TArrayInt = array of integer;');
  8862. Add('function GetArr(vB: integer = 0): tarrayint;');
  8863. Add('begin');
  8864. Add('end;');
  8865. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8866. Add('begin');
  8867. Add('end;');
  8868. Add('begin');
  8869. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8870. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8871. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8872. ConvertProgram;
  8873. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8874. LinesToStr([ // statements
  8875. 'this.GetArr = function (vB) {',
  8876. ' var Result = [];',
  8877. ' return Result;',
  8878. '};',
  8879. 'this.DoIt = function (vG,vH,vI) {',
  8880. '};'
  8881. ]),
  8882. LinesToStr([
  8883. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8884. ' a: 1+3,',
  8885. ' p: $mod.GetArr(0),',
  8886. ' get: function () {',
  8887. ' return this.p[this.a];',
  8888. ' },',
  8889. ' set: function (v) {',
  8890. ' this.p[this.a] = v;',
  8891. ' }',
  8892. '});',
  8893. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8894. ' a: 2+3,',
  8895. ' p: $mod.GetArr(0),',
  8896. ' get: function () {',
  8897. ' return this.p[this.a];',
  8898. ' },',
  8899. ' set: function (v) {',
  8900. ' this.p[this.a] = v;',
  8901. ' }',
  8902. '});',
  8903. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8904. ' a: 3+3,',
  8905. ' p: $mod.GetArr(9),',
  8906. ' get: function () {',
  8907. ' return this.p[this.a];',
  8908. ' },',
  8909. ' set: function (v) {',
  8910. ' this.p[this.a] = v;',
  8911. ' }',
  8912. '});',
  8913. '']));
  8914. end;
  8915. procedure TTestModule.TestArrayEnumTypeRange;
  8916. begin
  8917. StartProgram(false);
  8918. Add([
  8919. 'type',
  8920. ' TEnum = (red,blue);',
  8921. ' TEnumArray = array[TEnum] of longint;',
  8922. 'var',
  8923. ' e: TEnum;',
  8924. ' i: longint;',
  8925. ' a: TEnumArray;',
  8926. ' numbers: TEnumArray = (1,2);',
  8927. ' names: array[TEnum] of string = (''red'',''blue'');',
  8928. 'begin',
  8929. ' e:=low(a);',
  8930. ' e:=high(a);',
  8931. ' i:=a[red];',
  8932. ' a[e]:=a[e];']);
  8933. ConvertProgram;
  8934. CheckSource('TestArrayEnumTypeRange',
  8935. LinesToStr([ // statements
  8936. ' this.TEnum = {',
  8937. ' "0": "red",',
  8938. ' red: 0,',
  8939. ' "1": "blue",',
  8940. ' blue: 1',
  8941. '};',
  8942. 'this.e = 0;',
  8943. 'this.i = 0;',
  8944. 'this.a = rtl.arraySetLength(null,0,2);',
  8945. 'this.numbers = [1, 2];',
  8946. 'this.names = ["red", "blue"];',
  8947. '']),
  8948. LinesToStr([ // $mod.$main
  8949. '$mod.e = $mod.TEnum.red;',
  8950. '$mod.e = $mod.TEnum.blue;',
  8951. '$mod.i = $mod.a[$mod.TEnum.red];',
  8952. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8953. '']));
  8954. end;
  8955. procedure TTestModule.TestArray_SetLengthOutArg;
  8956. begin
  8957. StartProgram(false);
  8958. Add([
  8959. 'type TArrInt = array of longint;',
  8960. 'procedure DoIt(out a: TArrInt);',
  8961. 'begin',
  8962. ' SetLength(a,2);',
  8963. 'end;',
  8964. 'begin',
  8965. '']);
  8966. ConvertProgram;
  8967. CheckSource('TestArray_SetLengthOutArg',
  8968. LinesToStr([ // statements
  8969. 'this.DoIt = function (a) {',
  8970. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8971. '};',
  8972. '']),
  8973. LinesToStr([
  8974. '']));
  8975. end;
  8976. procedure TTestModule.TestArray_SetLengthProperty;
  8977. begin
  8978. StartProgram(false);
  8979. Add('type');
  8980. Add(' TArrInt = array of longint;');
  8981. Add(' TObject = class');
  8982. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8983. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8984. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8985. Add(' end;');
  8986. Add('var Obj: TObject;');
  8987. Add('begin');
  8988. Add(' SetLength(Obj.Colors,2);');
  8989. ConvertProgram;
  8990. CheckSource('TestArray_SetLengthProperty',
  8991. LinesToStr([ // statements
  8992. 'rtl.createClass($mod, "TObject", null, function () {',
  8993. ' this.$init = function () {',
  8994. ' };',
  8995. ' this.$final = function () {',
  8996. ' };',
  8997. '});',
  8998. 'this.Obj = null;',
  8999. '']),
  9000. LinesToStr([
  9001. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9002. '']));
  9003. end;
  9004. procedure TTestModule.TestArray_SetLengthMultiDim;
  9005. begin
  9006. StartProgram(false);
  9007. Add([
  9008. 'type',
  9009. ' TArrArrInt = array of array of longint;',
  9010. ' TArrStaInt = array of array[1..2] of longint;',
  9011. 'var',
  9012. ' a: TArrArrInt;',
  9013. ' b: TArrStaInt;',
  9014. 'begin',
  9015. ' SetLength(a,2);',
  9016. ' SetLength(a,3,4);',
  9017. ' SetLength(b,5);',
  9018. '']);
  9019. ConvertProgram;
  9020. CheckSource('TestArray_SetLengthMultiDim',
  9021. LinesToStr([ // statements
  9022. 'this.a = [];',
  9023. 'this.b = [];',
  9024. '']),
  9025. LinesToStr([
  9026. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9027. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9028. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9029. '']));
  9030. end;
  9031. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9032. begin
  9033. StartProgram(false);
  9034. Add([
  9035. 'type',
  9036. ' TStaArr1 = array[1..3] of boolean;',
  9037. //' TStaArr2 = array[5..6] of TStaArr1;',
  9038. ' TDynArr1StaArr1 = array of TStaArr1;',
  9039. //' TDynArr1StaArr2 = array of TStaArr2;',
  9040. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9041. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9042. 'var',
  9043. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9044. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9045. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9046. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9047. 'begin',
  9048. ' SetLength(DynArr1StaArr1,11);',
  9049. ' SetLength(DynArr2StaArr1,12);',
  9050. ' SetLength(DynArr2StaArr1[13],14);',
  9051. ' SetLength(DynArr2StaArr1,15,16);',
  9052. //' SetLength(DynArr1StaArr2,21);',
  9053. //' SetLength(DynArr2StaArr2,22);',
  9054. //' SetLength(DynArr2StaArr2[23],24);',
  9055. //' SetLength(DynArr2StaArr2,25,26);',
  9056. '']);
  9057. ConvertProgram;
  9058. CheckSource('TestArray_DynOfStatic',
  9059. LinesToStr([ // statements
  9060. 'this.DynArr1StaArr1 = [];',
  9061. 'this.DynArr2StaArr1 = [];',
  9062. '']),
  9063. LinesToStr([ // $mod.$main
  9064. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9065. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9066. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9067. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9068. ' $mod.DynArr2StaArr1,',
  9069. ' false,',
  9070. ' 15,',
  9071. ' 16,',
  9072. ' "s",',
  9073. ' 3',
  9074. ');',
  9075. '']));
  9076. end;
  9077. procedure TTestModule.TestArray_OpenArrayOfString;
  9078. begin
  9079. StartProgram(false);
  9080. Add('procedure DoIt(const a: array of String);');
  9081. Add('var');
  9082. Add(' i: longint;');
  9083. Add(' s: string;');
  9084. Add('begin');
  9085. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9086. Add('end;');
  9087. Add('var s: string;');
  9088. Add('begin');
  9089. Add(' DoIt([]);');
  9090. Add(' DoIt([s,''foo'','''',s+s]);');
  9091. ConvertProgram;
  9092. CheckSource('TestArray_OpenArrayOfString',
  9093. LinesToStr([ // statements
  9094. 'this.DoIt = function (a) {',
  9095. ' var i = 0;',
  9096. ' var s = "";',
  9097. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  9098. ' i = $l1;',
  9099. ' s = a[rtl.length(a) - i - 1];',
  9100. ' };',
  9101. '};',
  9102. 'this.s = "";',
  9103. '']),
  9104. LinesToStr([
  9105. '$mod.DoIt([]);',
  9106. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9107. '']));
  9108. end;
  9109. procedure TTestModule.TestArray_ConstRef;
  9110. begin
  9111. StartProgram(false);
  9112. Add([
  9113. 'type TArr = array of word;',
  9114. 'procedure Run(constref a: TArr);',
  9115. 'begin',
  9116. 'end;',
  9117. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9118. 'var l: TArr;',
  9119. 'begin',
  9120. ' Run(l);',
  9121. ' Run(a);',
  9122. ' Run(b);',
  9123. ' Run(c);',
  9124. ' Run(d);',
  9125. ' Run(e);',
  9126. 'end;',
  9127. 'begin',
  9128. '']);
  9129. ConvertProgram;
  9130. CheckResolverUnexpectedHints();
  9131. CheckSource('TestArray_ConstRef',
  9132. LinesToStr([ // statements
  9133. 'this.Run = function (a) {',
  9134. '};',
  9135. 'this.Fly = function (a, b, c, d, e) {',
  9136. ' var l = [];',
  9137. ' $mod.Run(l);',
  9138. ' $mod.Run(a);',
  9139. ' $mod.Run(b.get());',
  9140. ' $mod.Run(c.get());',
  9141. ' $mod.Run(d);',
  9142. ' $mod.Run(e);',
  9143. '};',
  9144. '']),
  9145. LinesToStr([
  9146. '']));
  9147. end;
  9148. procedure TTestModule.TestArray_Concat;
  9149. begin
  9150. StartProgram(false);
  9151. Add([
  9152. 'type',
  9153. ' integer = longint;',
  9154. ' TFlag = (big,small);',
  9155. ' TFlags = set of TFlag;',
  9156. ' TRec = record',
  9157. ' i: integer;',
  9158. ' end;',
  9159. ' TArrInt = array of integer;',
  9160. ' TArrRec = array of TRec;',
  9161. ' TArrFlag = array of TFlag;',
  9162. ' TArrSet = array of TFlags;',
  9163. ' TArrJSValue = array of jsvalue;',
  9164. 'var',
  9165. ' ArrInt: tarrint;',
  9166. ' ArrRec: tarrrec;',
  9167. ' ArrFlag: tarrflag;',
  9168. ' ArrSet: tarrset;',
  9169. ' ArrJSValue: tarrjsvalue;',
  9170. 'begin',
  9171. ' arrint:=concat(arrint);',
  9172. ' arrint:=concat(arrint,arrint);',
  9173. ' arrint:=concat(arrint,arrint,arrint);',
  9174. ' arrrec:=concat(arrrec);',
  9175. ' arrrec:=concat(arrrec,arrrec);',
  9176. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9177. ' arrset:=concat(arrset);',
  9178. ' arrset:=concat(arrset,arrset);',
  9179. ' arrset:=concat(arrset,arrset,arrset);',
  9180. ' arrjsvalue:=concat(arrjsvalue);',
  9181. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9182. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9183. ' arrint:=concat([1],arrint);',
  9184. ' arrflag:=concat([big]);',
  9185. ' arrflag:=concat([big],arrflag);',
  9186. ' arrflag:=concat(arrflag,[small]);',
  9187. '']);
  9188. ConvertProgram;
  9189. CheckSource('TestArray_Concat',
  9190. LinesToStr([ // statements
  9191. 'this.TFlag = {',
  9192. ' "0": "big",',
  9193. ' big: 0,',
  9194. ' "1": "small",',
  9195. ' small: 1',
  9196. '};',
  9197. 'rtl.recNewT($mod, "TRec", function () {',
  9198. ' this.i = 0;',
  9199. ' this.$eq = function (b) {',
  9200. ' return this.i === b.i;',
  9201. ' };',
  9202. ' this.$assign = function (s) {',
  9203. ' this.i = s.i;',
  9204. ' return this;',
  9205. ' };',
  9206. '});',
  9207. 'this.ArrInt = [];',
  9208. 'this.ArrRec = [];',
  9209. 'this.ArrFlag = [];',
  9210. 'this.ArrSet = [];',
  9211. 'this.ArrJSValue = [];',
  9212. '']),
  9213. LinesToStr([ // $mod.$main
  9214. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9215. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9216. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9217. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9218. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9219. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9220. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9221. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9222. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9223. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9224. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9225. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9226. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9227. '$mod.ArrFlag = [$mod.TFlag.big];',
  9228. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9229. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9230. '']));
  9231. end;
  9232. procedure TTestModule.TestArray_Copy;
  9233. begin
  9234. StartProgram(false);
  9235. Add([
  9236. 'type',
  9237. ' integer = longint;',
  9238. ' TFlag = (big,small);',
  9239. ' TFlags = set of TFlag;',
  9240. ' TRec = record',
  9241. ' i: integer;',
  9242. ' end;',
  9243. ' TArrInt = array of integer;',
  9244. ' TArrRec = array of TRec;',
  9245. ' TArrSet = array of TFlags;',
  9246. ' TArrJSValue = array of jsvalue;',
  9247. 'var',
  9248. ' ArrInt: tarrint;',
  9249. ' ArrRec: tarrrec;',
  9250. ' ArrSet: tarrset;',
  9251. ' ArrJSValue: tarrjsvalue;',
  9252. 'begin',
  9253. ' arrint:=copy(arrint);',
  9254. ' arrint:=copy(arrint,2);',
  9255. ' arrint:=copy(arrint,3,4);',
  9256. ' arrint:=copy([1,1],1,2);',
  9257. ' arrrec:=copy(arrrec);',
  9258. ' arrrec:=copy(arrrec,5);',
  9259. ' arrrec:=copy(arrrec,6,7);',
  9260. ' arrset:=copy(arrset);',
  9261. ' arrset:=copy(arrset,8);',
  9262. ' arrset:=copy(arrset,9,10);',
  9263. ' arrjsvalue:=copy(arrjsvalue);',
  9264. ' arrjsvalue:=copy(arrjsvalue,11);',
  9265. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9266. ' ']);
  9267. ConvertProgram;
  9268. CheckSource('TestArray_Copy',
  9269. LinesToStr([ // statements
  9270. 'this.TFlag = {',
  9271. ' "0": "big",',
  9272. ' big: 0,',
  9273. ' "1": "small",',
  9274. ' small: 1',
  9275. '};',
  9276. 'rtl.recNewT($mod, "TRec", function () {',
  9277. ' this.i = 0;',
  9278. ' this.$eq = function (b) {',
  9279. ' return this.i === b.i;',
  9280. ' };',
  9281. ' this.$assign = function (s) {',
  9282. ' this.i = s.i;',
  9283. ' return this;',
  9284. ' };',
  9285. '});',
  9286. 'this.ArrInt = [];',
  9287. 'this.ArrRec = [];',
  9288. 'this.ArrSet = [];',
  9289. 'this.ArrJSValue = [];',
  9290. '']),
  9291. LinesToStr([ // $mod.$main
  9292. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9293. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9294. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9295. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9296. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9297. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9298. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9299. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9300. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9301. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9302. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9303. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9304. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9305. '']));
  9306. end;
  9307. procedure TTestModule.TestArray_InsertDelete;
  9308. begin
  9309. StartProgram(false);
  9310. Add([
  9311. 'type',
  9312. ' integer = longint;',
  9313. ' TFlag = (big,small);',
  9314. ' TFlags = set of TFlag;',
  9315. ' TRec = record',
  9316. ' i: integer;',
  9317. ' end;',
  9318. ' TArrInt = array of integer;',
  9319. ' TArrRec = array of TRec;',
  9320. ' TArrSet = array of TFlags;',
  9321. ' TArrJSValue = array of jsvalue;',
  9322. ' TArrArrInt = array of TArrInt;',
  9323. 'var',
  9324. ' ArrInt: tarrint;',
  9325. ' ArrRec: tarrrec;',
  9326. ' ArrSet: tarrset;',
  9327. ' ArrJSValue: tarrjsvalue;',
  9328. ' ArrArrInt: TArrArrInt;',
  9329. 'begin',
  9330. ' Insert(1,arrint,2);',
  9331. ' Insert(arrint[3],arrint,4);',
  9332. ' Insert(arrrec[5],arrrec,6);',
  9333. ' Insert(arrset[7],arrset,7);',
  9334. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9335. ' Insert(10,arrjsvalue,11);',
  9336. ' Insert([23],arrarrint,22);',
  9337. ' Delete(arrint,12,13);',
  9338. ' Delete(arrrec,14,15);',
  9339. ' Delete(arrset,17,18);',
  9340. ' Delete(arrjsvalue,19,10);']);
  9341. ConvertProgram;
  9342. CheckSource('TestArray_InsertDelete',
  9343. LinesToStr([ // statements
  9344. 'this.TFlag = {',
  9345. ' "0": "big",',
  9346. ' big: 0,',
  9347. ' "1": "small",',
  9348. ' small: 1',
  9349. '};',
  9350. 'rtl.recNewT($mod, "TRec", function () {',
  9351. ' this.i = 0;',
  9352. ' this.$eq = function (b) {',
  9353. ' return this.i === b.i;',
  9354. ' };',
  9355. ' this.$assign = function (s) {',
  9356. ' this.i = s.i;',
  9357. ' return this;',
  9358. ' };',
  9359. '});',
  9360. 'this.ArrInt = [];',
  9361. 'this.ArrRec = [];',
  9362. 'this.ArrSet = [];',
  9363. 'this.ArrJSValue = [];',
  9364. 'this.ArrArrInt = [];',
  9365. '']),
  9366. LinesToStr([ // $mod.$main
  9367. '$mod.ArrInt.splice(2, 0, 1);',
  9368. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9369. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9370. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9371. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9372. '$mod.ArrJSValue.splice(11, 0, 10);',
  9373. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9374. '$mod.ArrInt.splice(12, 13);',
  9375. '$mod.ArrRec.splice(14, 15);',
  9376. '$mod.ArrSet.splice(17, 18);',
  9377. '$mod.ArrJSValue.splice(19, 10);',
  9378. '']));
  9379. end;
  9380. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9381. begin
  9382. StartProgram(false);
  9383. Add([
  9384. '{$modeswitch arrayoperators}',
  9385. 'type',
  9386. ' integer = longint;',
  9387. ' TArrInt = array of integer;',
  9388. ' TArrStr = array of string;',
  9389. 'const',
  9390. ' Ints: TArrInt = (1,2,3);',
  9391. ' Aliases: TarrStr = (''foo'',''b'');',
  9392. ' OneInt: TArrInt = (7);',
  9393. ' OneStr: array of integer = (7);',
  9394. ' Chars: array of char = ''aoc'';',
  9395. ' Names: array of string = (''a'',''foo'');',
  9396. ' NameCount = low(Names)+high(Names)+length(Names);',
  9397. 'var i: integer;',
  9398. 'begin',
  9399. ' Ints:=[];',
  9400. ' Ints:=[1,1];',
  9401. ' Ints:=[1]+[2];',
  9402. ' Ints:=[2];',
  9403. ' Ints:=[]+ints;',
  9404. ' Ints:=Ints+[];',
  9405. ' Ints:=Ints+OneInt;',
  9406. ' Ints:=Ints+[1,1];',
  9407. ' Ints:=[i,i]+Ints;',
  9408. ' Ints:=[1]+[i]+[3];',
  9409. '']);
  9410. ConvertProgram;
  9411. CheckSource('TestArray_DynArrayConstObjFPC',
  9412. LinesToStr([ // statements
  9413. 'this.Ints = [1, 2, 3];',
  9414. 'this.Aliases = ["foo", "b"];',
  9415. 'this.OneInt = [7];',
  9416. 'this.OneStr = [7];',
  9417. 'this.Chars = ["a", "o", "c"];',
  9418. 'this.Names = ["a", "foo"];',
  9419. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9420. 'this.i = 0;',
  9421. '']),
  9422. LinesToStr([ // $mod.$main
  9423. '$mod.Ints = [];',
  9424. '$mod.Ints = [1, 1];',
  9425. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9426. '$mod.Ints = [2];',
  9427. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9428. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9429. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9430. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9431. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9432. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9433. '']));
  9434. end;
  9435. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9436. begin
  9437. StartProgram(false);
  9438. // Note: const c = [1,1]; defines a set!
  9439. Add([
  9440. '{$mode delphi}',
  9441. 'type',
  9442. ' integer = longint;',
  9443. ' TArrInt = array of integer;',
  9444. ' TArrStr = array of string;',
  9445. 'const',
  9446. ' Ints: TArrInt = [1,1,2];',
  9447. ' Aliases: TarrStr = [''foo'',''b''];',
  9448. ' OneInt: TArrInt = [7];',
  9449. ' OneStr: array of integer = [7]+[8];',
  9450. ' Chars: array of char = ''aoc'';',
  9451. ' Names: array of string = [''a'',''a''];',
  9452. ' NameCount = low(Names)+high(Names)+length(Names);',
  9453. 'begin',
  9454. '']);
  9455. ConvertProgram;
  9456. CheckSource('TestArray_DynArrayConstDelphi',
  9457. LinesToStr([ // statements
  9458. 'this.Ints = [1, 1, 2];',
  9459. 'this.Aliases = ["foo", "b"];',
  9460. 'this.OneInt = [7];',
  9461. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9462. 'this.Chars = ["a", "o", "c"];',
  9463. 'this.Names = ["a", "a"];',
  9464. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9465. '']),
  9466. LinesToStr([ // $mod.$main
  9467. '']));
  9468. end;
  9469. procedure TTestModule.TestArray_ArrayLitAsParam;
  9470. begin
  9471. StartProgram(false);
  9472. Add([
  9473. '{$modeswitch arrayoperators}',
  9474. 'type',
  9475. ' integer = longint;',
  9476. ' TArrInt = array of integer;',
  9477. ' TArrSet = array of (red,green,blue);',
  9478. 'procedure DoOpenInt(a: array of integer); forward;',
  9479. 'procedure DoInt(a: TArrInt);',
  9480. 'begin',
  9481. ' DoInt(a+[1]);',
  9482. ' DoInt([1]+a);',
  9483. ' DoOpenInt(a);',
  9484. ' DoOpenInt(a+[1]);',
  9485. ' DoOpenInt([1]+a);',
  9486. 'end;',
  9487. 'procedure DoOpenInt(a: array of integer);',
  9488. 'begin',
  9489. ' DoOpenInt(a+[1]);',
  9490. ' DoOpenInt([1]+a);',
  9491. ' DoInt(a);',
  9492. ' DoInt(a+[1]);',
  9493. ' DoInt([1]+a);',
  9494. 'end;',
  9495. 'procedure DoSet(a: TArrSet);',
  9496. 'begin',
  9497. ' DoSet(a+[red]);',
  9498. ' DoSet([blue]+a);',
  9499. 'end;',
  9500. 'var',
  9501. ' i: TArrInt;',
  9502. ' s: TArrSet;',
  9503. 'begin',
  9504. ' DoInt([1]);',
  9505. ' DoInt([1]+[2]);',
  9506. ' DoInt(i+[1]);',
  9507. ' DoInt([1]+i);',
  9508. ' DoOpenInt([1]);',
  9509. ' DoOpenInt([1]+[2]);',
  9510. ' DoOpenInt(i+[1]);',
  9511. ' DoOpenInt([1]+i);',
  9512. ' DoSet([red]);',
  9513. ' DoSet([blue]+[green]);',
  9514. ' DoSet(s+[blue]);',
  9515. ' DoSet([red]+s);',
  9516. '']);
  9517. ConvertProgram;
  9518. CheckSource('TestArray_ArrayLitAsParam',
  9519. LinesToStr([ // statements
  9520. 'this.TArrSet$a = {',
  9521. ' "0": "red",',
  9522. ' red: 0,',
  9523. ' "1": "green",',
  9524. ' green: 1,',
  9525. ' "2": "blue",',
  9526. ' blue: 2',
  9527. '};',
  9528. 'this.DoInt = function (a) {',
  9529. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9530. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9531. ' $mod.DoOpenInt(a);',
  9532. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9533. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9534. '};',
  9535. 'this.DoOpenInt = function (a) {',
  9536. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9537. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9538. ' $mod.DoInt(rtl.arrayRef(a));',
  9539. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9540. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9541. '};',
  9542. 'this.DoSet = function (a) {',
  9543. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9544. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9545. '};',
  9546. 'this.i = [];',
  9547. 'this.s = [];',
  9548. '']),
  9549. LinesToStr([ // $mod.$main
  9550. '$mod.DoInt([1]);',
  9551. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9552. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9553. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9554. '$mod.DoOpenInt([1]);',
  9555. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9556. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9557. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9558. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9559. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9560. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9561. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9562. '']));
  9563. end;
  9564. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9565. begin
  9566. StartProgram(false);
  9567. Add([
  9568. '{$modeswitch arrayoperators}',
  9569. 'type',
  9570. ' integer = longint;',
  9571. ' TArrInt = array of integer;',
  9572. ' TArrArrInt = array of TArrInt;',
  9573. 'procedure DoInt(const a: TArrArrInt);',
  9574. 'begin',
  9575. ' DoInt(a+[[1]]);',
  9576. ' DoInt([[1]]+a);',
  9577. ' DoInt(a);',
  9578. 'end;',
  9579. 'var',
  9580. ' i: TArrInt;',
  9581. ' a: TArrArrInt;',
  9582. 'begin',
  9583. ' a:=[[1]];',
  9584. ' a:=[i];',
  9585. ' a:=a+[i];',
  9586. ' a:=[i]+a;',
  9587. ' a:=[[1]+i];',
  9588. ' a:=[[1]+[2]];',
  9589. ' a:=[i+[2]];',
  9590. ' DoInt([[1]]);',
  9591. ' DoInt([[1]+[2],[3,4],[5]]);',
  9592. ' DoInt([i+[1]]+a);',
  9593. ' DoInt([i]+a);',
  9594. '']);
  9595. ConvertProgram;
  9596. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9597. LinesToStr([ // statements
  9598. 'this.DoInt = function (a) {',
  9599. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9600. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9601. ' $mod.DoInt(a);',
  9602. '};',
  9603. 'this.i = [];',
  9604. 'this.a = [];',
  9605. '']),
  9606. LinesToStr([ // $mod.$main
  9607. '$mod.a = [[1]];',
  9608. '$mod.a = [$mod.i];',
  9609. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9610. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9611. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9612. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9613. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9614. '$mod.DoInt([[1]]);',
  9615. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9616. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9617. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9618. '']));
  9619. end;
  9620. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9621. begin
  9622. StartProgram(false);
  9623. Add([
  9624. '{$modeswitch arrayoperators}',
  9625. 'type',
  9626. ' integer = longint;',
  9627. ' TArrInt = array[1..2] of integer;',
  9628. ' TArrArrInt = array of TArrInt;',
  9629. 'procedure DoInt(const a: TArrArrInt);',
  9630. 'begin',
  9631. ' DoInt(a+[[1,2]]);',
  9632. ' DoInt([[1,2]]+a);',
  9633. ' DoInt(a);',
  9634. 'end;',
  9635. 'var',
  9636. ' i: TArrInt;',
  9637. ' a: TArrArrInt;',
  9638. 'begin',
  9639. ' a:=[[1,1]];',
  9640. ' a:=[i];',
  9641. ' a:=a+[i];',
  9642. ' a:=[i]+a;',
  9643. ' DoInt([[1,1]]);',
  9644. ' DoInt([[1,2],[3,4]]);',
  9645. '']);
  9646. ConvertProgram;
  9647. CheckSource('TestArray_ArrayLitStaticAsParam',
  9648. LinesToStr([ // statements
  9649. 'this.DoInt = function (a) {',
  9650. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9651. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9652. ' $mod.DoInt(a);',
  9653. '};',
  9654. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9655. 'this.a = [];',
  9656. '']),
  9657. LinesToStr([ // $mod.$main
  9658. '$mod.a = [[1, 1]];',
  9659. '$mod.a = [$mod.i.slice(0)];',
  9660. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9661. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9662. '$mod.DoInt([[1, 1]]);',
  9663. '$mod.DoInt([[1, 2], [3, 4]]);',
  9664. '']));
  9665. end;
  9666. procedure TTestModule.TestArray_ForInArrOfString;
  9667. begin
  9668. StartProgram(false);
  9669. Add([
  9670. 'type',
  9671. 'type',
  9672. ' TMonthNameArray = array [1..12] of string;',
  9673. ' TMonthNames = TMonthNameArray;',
  9674. ' TObject = class',
  9675. ' private',
  9676. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9677. ' public',
  9678. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9679. ' end;',
  9680. 'var',
  9681. ' f: TObject;',
  9682. ' Month: string;',
  9683. ' Names: array of string = (''a'',''foo'',''bar'');',
  9684. ' i: longint;',
  9685. 'begin',
  9686. ' for Month in f.LongMonthNames do ;',
  9687. ' for Month in Names do ;',
  9688. ' for i:=low(Names) to high(Names) do ;',
  9689. '']);
  9690. ConvertProgram;
  9691. CheckSource('TestArray_ForInArrOfString',
  9692. LinesToStr([ // statements
  9693. 'rtl.createClass($mod, "TObject", null, function () {',
  9694. ' this.$init = function () {',
  9695. ' };',
  9696. ' this.$final = function () {',
  9697. ' };',
  9698. '});',
  9699. 'this.f = null;',
  9700. 'this.Month = "";',
  9701. 'this.Names = ["a", "foo", "bar"];',
  9702. 'this.i = 0;',
  9703. '']),
  9704. LinesToStr([ // $mod.$main
  9705. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9706. 'for (var $in4 = $mod.Names, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.Month = $in4[$l5];',
  9707. 'for (var $l7 = 0, $end8 = rtl.length($mod.Names) - 1; $l7 <= $end8; $l7++) $mod.i = $l7;',
  9708. '']));
  9709. end;
  9710. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9711. begin
  9712. StartProgram(false);
  9713. Add([
  9714. '{$modeswitch externalclass}',
  9715. 'type',
  9716. ' TJSObject = class external name ''Object''',
  9717. ' end;',
  9718. ' TJSArray = class external name ''Array''',
  9719. ' class function isArray(Value: JSValue) : boolean;',
  9720. ' function concat() : TJSArray; varargs;',
  9721. ' end;',
  9722. 'var',
  9723. ' aObj: TJSArray;',
  9724. ' a: array of longint;',
  9725. ' o: TJSObject;',
  9726. 'begin',
  9727. ' if TJSArray.isArray(65) then ;',
  9728. ' aObj:=TJSArray(a).concat(a);',
  9729. ' o:=TJSObject(a);']);
  9730. ConvertProgram;
  9731. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9732. LinesToStr([ // statements
  9733. 'this.aObj = null;',
  9734. 'this.a = [];',
  9735. 'this.o = null;',
  9736. '']),
  9737. LinesToStr([ // $mod.$main
  9738. 'if (Array.isArray(65)) ;',
  9739. '$mod.aObj = $mod.a.concat($mod.a);',
  9740. '$mod.o = $mod.a;',
  9741. '']));
  9742. end;
  9743. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9744. begin
  9745. StartProgram(false);
  9746. Add([
  9747. '{$modeswitch externalclass}',
  9748. 'type',
  9749. ' TArrStr = array of string;',
  9750. ' TJSArray = class external name ''Array''',
  9751. ' end;',
  9752. ' TJSObject = class external name ''Object''',
  9753. ' end;',
  9754. 'var',
  9755. ' aObj: TJSArray;',
  9756. ' a: TArrStr;',
  9757. ' jo: TJSObject;',
  9758. 'begin',
  9759. ' a:=TArrStr(aObj);',
  9760. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  9761. ' a:=TarrStr(jo);',
  9762. '']);
  9763. ConvertProgram;
  9764. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  9765. LinesToStr([ // statements
  9766. 'this.aObj = null;',
  9767. 'this.a = [];',
  9768. 'this.jo = null;',
  9769. '']),
  9770. LinesToStr([ // $mod.$main
  9771. '$mod.a = $mod.aObj;',
  9772. '$mod.aObj[1] = $mod.aObj[2];',
  9773. '$mod.a = $mod.jo;',
  9774. '']));
  9775. end;
  9776. procedure TTestModule.TestArrayOfConst_TVarRec;
  9777. begin
  9778. StartProgram(true,[supTVarRec]);
  9779. Add([
  9780. 'procedure Say(args: array of const);',
  9781. 'var',
  9782. ' i: longint;',
  9783. ' v: TVarRec;',
  9784. 'begin',
  9785. ' for i:=low(args) to high(args) do begin',
  9786. ' v:=args[i];',
  9787. ' case v.vtype of',
  9788. ' vtInteger: if length(args)=args[i].vInteger then ;',
  9789. ' end;',
  9790. ' end;',
  9791. ' for v in args do ;',
  9792. ' args:=nil;',
  9793. ' SetLength(args,2);',
  9794. 'end;',
  9795. 'begin']);
  9796. ConvertProgram;
  9797. CheckSource('TestArrayOfConst_TVarRec',
  9798. LinesToStr([ // statements
  9799. 'this.Say = function (args) {',
  9800. ' var i = 0;',
  9801. ' var v = pas.system.TVarRec.$new();',
  9802. ' for (var $l1 = 0, $end2 = rtl.length(args) - 1; $l1 <= $end2; $l1++) {',
  9803. ' i = $l1;',
  9804. ' v.$assign(args[i]);',
  9805. ' var $tmp3 = v.VType;',
  9806. ' if ($tmp3 === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  9807. ' };',
  9808. ' for (var $in4 = args, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) v = $in4[$l5];',
  9809. ' args = [];',
  9810. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  9811. '};',
  9812. '']),
  9813. LinesToStr([ // $mod.$main
  9814. ]));
  9815. end;
  9816. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  9817. begin
  9818. StartProgram(true,[supTVarRec]);
  9819. Add([
  9820. 'procedure Say(args: array of const);',
  9821. 'begin',
  9822. ' Say(args);',
  9823. 'end;',
  9824. 'var',
  9825. ' p: Pointer;',
  9826. ' j: jsvalue;',
  9827. ' c: currency;',
  9828. 'begin',
  9829. ' Say([]);',
  9830. ' Say([1]);',
  9831. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  9832. '']);
  9833. ConvertProgram;
  9834. CheckSource('TestArrayOfConst_PassBaseTypes',
  9835. LinesToStr([ // statements
  9836. 'this.Say = function (args) {',
  9837. ' $mod.Say(args);',
  9838. '};',
  9839. 'this.p = null;',
  9840. 'this.j = undefined;',
  9841. 'this.c = 0;',
  9842. '']),
  9843. LinesToStr([ // $mod.$main
  9844. '$mod.Say([]);',
  9845. '$mod.Say(pas.system.VarRecs(0, 1));',
  9846. '$mod.Say(pas.system.VarRecs(',
  9847. ' 9,',
  9848. ' "c",',
  9849. ' 18,',
  9850. ' "foo",',
  9851. ' 5,',
  9852. ' null,',
  9853. ' 1,',
  9854. ' true,',
  9855. ' 3,',
  9856. ' 1.3,',
  9857. ' 5,',
  9858. ' $mod.p,',
  9859. ' 20,',
  9860. ' $mod.j,',
  9861. ' 12,',
  9862. ' $mod.c',
  9863. ' ));',
  9864. '']));
  9865. end;
  9866. procedure TTestModule.TestArrayOfConst_PassObj;
  9867. begin
  9868. StartProgram(true,[supTVarRec]);
  9869. Add([
  9870. '{$interfaces corba}',
  9871. 'type',
  9872. ' TObject = class',
  9873. ' end;',
  9874. ' TClass = class of TObject;',
  9875. ' IUnknown = interface',
  9876. ' end;',
  9877. 'procedure Say(args: array of const);',
  9878. 'begin',
  9879. 'end;',
  9880. 'var',
  9881. ' o: TObject;',
  9882. ' c: TClass;',
  9883. ' i: IUnknown;',
  9884. 'begin',
  9885. ' Say([o,c,TObject]);',
  9886. ' Say([nil,i]);',
  9887. '']);
  9888. ConvertProgram;
  9889. CheckSource('TestArrayOfConst_PassObj',
  9890. LinesToStr([ // statements
  9891. 'rtl.createClass($mod, "TObject", null, function () {',
  9892. ' this.$init = function () {',
  9893. ' };',
  9894. ' this.$final = function () {',
  9895. ' };',
  9896. '});',
  9897. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  9898. 'this.Say = function (args) {',
  9899. '};',
  9900. 'this.o = null;',
  9901. 'this.c = null;',
  9902. 'this.i = null;',
  9903. '']),
  9904. LinesToStr([ // $mod.$main
  9905. '$mod.Say(pas.system.VarRecs(',
  9906. ' 7,',
  9907. ' $mod.o,',
  9908. ' 8,',
  9909. ' $mod.c,',
  9910. ' 8,',
  9911. ' $mod.TObject',
  9912. '));',
  9913. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  9914. '']));
  9915. end;
  9916. procedure TTestModule.TestRecord_Empty;
  9917. begin
  9918. StartProgram(false);
  9919. Add([
  9920. 'type',
  9921. ' TRecA = record',
  9922. ' end;',
  9923. 'var a,b: TRecA;',
  9924. 'begin',
  9925. ' if a=b then ;']);
  9926. ConvertProgram;
  9927. CheckSource('TestRecord_Empty',
  9928. LinesToStr([ // statements
  9929. 'rtl.recNewT($mod, "TRecA", function () {',
  9930. ' this.$eq = function (b) {',
  9931. ' return true;',
  9932. ' };',
  9933. ' this.$assign = function (s) {',
  9934. ' return this;',
  9935. ' };',
  9936. '});',
  9937. 'this.a = $mod.TRecA.$new();',
  9938. 'this.b = $mod.TRecA.$new();',
  9939. '']),
  9940. LinesToStr([ // $mod.$main
  9941. 'if ($mod.a.$eq($mod.b)) ;'
  9942. ]));
  9943. end;
  9944. procedure TTestModule.TestRecord_Var;
  9945. begin
  9946. StartProgram(false);
  9947. Add('type');
  9948. Add(' TRecA = record');
  9949. Add(' Bold: longint;');
  9950. Add(' end;');
  9951. Add('var Rec: TRecA;');
  9952. Add('begin');
  9953. Add(' rec.bold:=123');
  9954. ConvertProgram;
  9955. CheckSource('TestRecord_Var',
  9956. LinesToStr([ // statements
  9957. 'rtl.recNewT($mod, "TRecA", function () {',
  9958. ' this.Bold = 0;',
  9959. ' this.$eq = function (b) {',
  9960. ' return this.Bold === b.Bold;',
  9961. ' };',
  9962. ' this.$assign = function (s) {',
  9963. ' this.Bold = s.Bold;',
  9964. ' return this;',
  9965. ' };',
  9966. '});',
  9967. 'this.Rec = $mod.TRecA.$new();',
  9968. '']),
  9969. LinesToStr([ // $mod.$main
  9970. '$mod.Rec.Bold = 123;'
  9971. ]));
  9972. end;
  9973. procedure TTestModule.TestRecord_VarExternal;
  9974. begin
  9975. StartProgram(false);
  9976. Add([
  9977. '{$modeswitch externalclass}',
  9978. 'type',
  9979. ' TRecA = record',
  9980. ' i: byte;',
  9981. ' length_: longint external name ''length'';',
  9982. ' end;',
  9983. 'var Rec: TRecA;',
  9984. 'begin',
  9985. ' rec.length_ := rec.length_',
  9986. '']);
  9987. ConvertProgram;
  9988. CheckSource('TestRecord_VarExternal',
  9989. LinesToStr([ // statements
  9990. 'rtl.recNewT($mod, "TRecA", function () {',
  9991. ' this.i = 0;',
  9992. ' this.$eq = function (b) {',
  9993. ' return (this.i === b.i) && (this.length === b.length);',
  9994. ' };',
  9995. ' this.$assign = function (s) {',
  9996. ' this.i = s.i;',
  9997. ' this.length = s.length;',
  9998. ' return this;',
  9999. ' };',
  10000. '});',
  10001. 'this.Rec = $mod.TRecA.$new();',
  10002. '']),
  10003. LinesToStr([ // $mod.$main
  10004. '$mod.Rec.length = $mod.Rec.length;'
  10005. ]));
  10006. end;
  10007. procedure TTestModule.TestRecord_WithDo;
  10008. begin
  10009. StartProgram(false);
  10010. Add('type');
  10011. Add(' TRec = record');
  10012. Add(' vI: longint;');
  10013. Add(' end;');
  10014. Add('var');
  10015. Add(' Int: longint;');
  10016. Add(' r: TRec;');
  10017. Add('begin');
  10018. Add(' with r do');
  10019. Add(' int:=vi;');
  10020. Add(' with r do begin');
  10021. Add(' int:=vi;');
  10022. Add(' vi:=int;');
  10023. Add(' end;');
  10024. ConvertProgram;
  10025. CheckSource('TestWithRecordDo',
  10026. LinesToStr([ // statements
  10027. 'rtl.recNewT($mod, "TRec", function () {',
  10028. ' this.vI = 0;',
  10029. ' this.$eq = function (b) {',
  10030. ' return this.vI === b.vI;',
  10031. ' };',
  10032. ' this.$assign = function (s) {',
  10033. ' this.vI = s.vI;',
  10034. ' return this;',
  10035. ' };',
  10036. '});',
  10037. 'this.Int = 0;',
  10038. 'this.r = $mod.TRec.$new();',
  10039. '']),
  10040. LinesToStr([ // $mod.$main
  10041. 'var $with1 = $mod.r;',
  10042. '$mod.Int = $with1.vI;',
  10043. 'var $with2 = $mod.r;',
  10044. '$mod.Int = $with2.vI;',
  10045. '$with2.vI = $mod.Int;'
  10046. ]));
  10047. end;
  10048. procedure TTestModule.TestRecord_Assign;
  10049. begin
  10050. StartProgram(false);
  10051. Add('type');
  10052. Add(' TEnum = (red,green);');
  10053. Add(' TEnums = set of TEnum;');
  10054. Add(' TSmallRec = record');
  10055. Add(' N: longint;');
  10056. Add(' end;');
  10057. Add(' TBigRec = record');
  10058. Add(' Int: longint;');
  10059. Add(' D: double;');
  10060. Add(' Arr: array of longint;');
  10061. Add(' Arr2: array[1..2] of longint;');
  10062. Add(' Small: TSmallRec;');
  10063. Add(' Enums: TEnums;');
  10064. Add(' end;');
  10065. Add('var');
  10066. Add(' r, s: TBigRec;');
  10067. Add('begin');
  10068. Add(' r:=s;');
  10069. Add(' r:=default(TBigRec);');
  10070. Add(' r:=default(s);');
  10071. ConvertProgram;
  10072. CheckSource('TestRecord_Assign',
  10073. LinesToStr([ // statements
  10074. 'this.TEnum = {',
  10075. ' "0": "red",',
  10076. ' red: 0,',
  10077. ' "1": "green",',
  10078. ' green: 1',
  10079. '};',
  10080. 'rtl.recNewT($mod, "TSmallRec", function () {',
  10081. ' this.N = 0;',
  10082. ' this.$eq = function (b) {',
  10083. ' return this.N === b.N;',
  10084. ' };',
  10085. ' this.$assign = function (s) {',
  10086. ' this.N = s.N;',
  10087. ' return this;',
  10088. ' };',
  10089. '});',
  10090. 'rtl.recNewT($mod, "TBigRec", function () {',
  10091. ' this.Int = 0;',
  10092. ' this.D = 0.0;',
  10093. ' this.$new = function () {',
  10094. ' var r = Object.create(this);',
  10095. ' r.Arr = [];',
  10096. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10097. ' r.Small = $mod.TSmallRec.$new();',
  10098. ' r.Enums = {};',
  10099. ' return r;',
  10100. ' };',
  10101. ' this.$eq = function (b) {',
  10102. ' return (this.Int === b.Int) && (this.D === b.D) && (this.Arr === b.Arr) && rtl.arrayEq(this.Arr2, b.Arr2) && this.Small.$eq(b.Small) && rtl.eqSet(this.Enums, b.Enums);',
  10103. ' };',
  10104. ' this.$assign = function (s) {',
  10105. ' this.Int = s.Int;',
  10106. ' this.D = s.D;',
  10107. ' this.Arr = rtl.arrayRef(s.Arr);',
  10108. ' this.Arr2 = s.Arr2.slice(0);',
  10109. ' this.Small.$assign(s.Small);',
  10110. ' this.Enums = rtl.refSet(s.Enums);',
  10111. ' return this;',
  10112. ' };',
  10113. '});',
  10114. 'this.r = $mod.TBigRec.$new();',
  10115. 'this.s = $mod.TBigRec.$new();',
  10116. '']),
  10117. LinesToStr([ // $mod.$main
  10118. '$mod.r.$assign($mod.s);',
  10119. '$mod.r.$assign($mod.TBigRec.$new());',
  10120. '$mod.r.$assign($mod.TBigRec.$new());',
  10121. '']));
  10122. end;
  10123. procedure TTestModule.TestRecord_AsParams;
  10124. begin
  10125. StartProgram(false);
  10126. Add([
  10127. 'type',
  10128. ' integer = longint;',
  10129. ' TRecord = record',
  10130. ' i: integer;',
  10131. ' end;',
  10132. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10133. 'var vL: TRecord;',
  10134. 'begin',
  10135. ' vd:=vd;',
  10136. ' vd.i:=vd.i;',
  10137. ' vl:=vc;',
  10138. ' vv:=vv;',
  10139. ' vv.i:=vv.i;',
  10140. ' U:=vl;',
  10141. ' U:=vd;',
  10142. ' U:=vc;',
  10143. ' U:=vv;',
  10144. ' doit(vd,vd,vd,vd);',
  10145. ' doit(vc,vc,vl,vl);',
  10146. ' doit(vv,vv,vv,vv);',
  10147. ' doit(vl,vl,vl,vl);',
  10148. 'end;',
  10149. 'var i: TRecord;',
  10150. 'begin',
  10151. ' doit(i,i,i,i);']);
  10152. ConvertProgram;
  10153. CheckSource('TestRecord_AsParams',
  10154. LinesToStr([ // statements
  10155. 'rtl.recNewT($mod, "TRecord", function () {',
  10156. ' this.i = 0;',
  10157. ' this.$eq = function (b) {',
  10158. ' return this.i === b.i;',
  10159. ' };',
  10160. ' this.$assign = function (s) {',
  10161. ' this.i = s.i;',
  10162. ' return this;',
  10163. ' };',
  10164. '});',
  10165. 'this.DoIt = function (vD, vC, vV, U) {',
  10166. ' var vL = $mod.TRecord.$new();',
  10167. ' vD.$assign(vD);',
  10168. ' vD.i = vD.i;',
  10169. ' vL.$assign(vC);',
  10170. ' vV.$assign(vV);',
  10171. ' vV.i = vV.i;',
  10172. ' U.set(vL);',
  10173. ' U.set(vD);',
  10174. ' U.set(vC);',
  10175. ' U.set(vV);',
  10176. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, {',
  10177. ' get: function () {',
  10178. ' return vD;',
  10179. ' },',
  10180. ' set: function (v) {',
  10181. ' vD.$assign(v);',
  10182. ' }',
  10183. ' });',
  10184. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, {',
  10185. ' get: function () {',
  10186. ' return vL;',
  10187. ' },',
  10188. ' set: function (v) {',
  10189. ' vL.$assign(v);',
  10190. ' }',
  10191. ' });',
  10192. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, {',
  10193. ' get: function () {',
  10194. ' return vV;',
  10195. ' },',
  10196. ' set: function (v) {',
  10197. ' vV.$assign(v);',
  10198. ' }',
  10199. ' });',
  10200. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, {',
  10201. ' get: function () {',
  10202. ' return vL;',
  10203. ' },',
  10204. ' set: function (v) {',
  10205. ' vL.$assign(v);',
  10206. ' }',
  10207. ' });',
  10208. '};',
  10209. 'this.i = $mod.TRecord.$new();'
  10210. ]),
  10211. LinesToStr([
  10212. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, {',
  10213. ' p: $mod,',
  10214. ' get: function () {',
  10215. ' return this.p.i;',
  10216. ' },',
  10217. ' set: function (v) {',
  10218. ' this.p.i.$assign(v);',
  10219. ' }',
  10220. '});',
  10221. '']));
  10222. end;
  10223. procedure TTestModule.TestRecord_ConstRef;
  10224. begin
  10225. StartProgram(false);
  10226. Add([
  10227. 'type TRec = record i: word; end;',
  10228. 'procedure Run(constref a: TRec);',
  10229. 'begin',
  10230. 'end;',
  10231. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10232. 'var l: TRec;',
  10233. 'begin',
  10234. ' Run(l);',
  10235. ' Run(a);',
  10236. ' Run(b);',
  10237. ' Run(c);',
  10238. ' Run(d);',
  10239. ' Run(e);',
  10240. 'end;',
  10241. 'begin',
  10242. '']);
  10243. ConvertProgram;
  10244. CheckResolverUnexpectedHints();
  10245. CheckSource('TestRecord_ConstRef',
  10246. LinesToStr([ // statements
  10247. 'rtl.recNewT($mod, "TRec", function () {',
  10248. ' this.i = 0;',
  10249. ' this.$eq = function (b) {',
  10250. ' return this.i === b.i;',
  10251. ' };',
  10252. ' this.$assign = function (s) {',
  10253. ' this.i = s.i;',
  10254. ' return this;',
  10255. ' };',
  10256. '});',
  10257. 'this.Run = function (a) {',
  10258. '};',
  10259. 'this.Fly = function (a, b, c, d, e) {',
  10260. ' var l = $mod.TRec.$new();',
  10261. ' $mod.Run(l);',
  10262. ' $mod.Run(a);',
  10263. ' $mod.Run(b);',
  10264. ' $mod.Run(c);',
  10265. ' $mod.Run(d);',
  10266. ' $mod.Run(e);',
  10267. '};',
  10268. '']),
  10269. LinesToStr([
  10270. '']));
  10271. end;
  10272. procedure TTestModule.TestRecordElement_AsParams;
  10273. begin
  10274. StartProgram(false);
  10275. Add('type');
  10276. Add(' integer = longint;');
  10277. Add(' TRecord = record');
  10278. Add(' i: integer;');
  10279. Add(' end;');
  10280. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10281. Add('var vJ: TRecord;');
  10282. Add('begin');
  10283. Add(' doit(vj.i,vj.i,vj.i);');
  10284. Add('end;');
  10285. Add('var r: TRecord;');
  10286. Add('begin');
  10287. Add(' doit(r.i,r.i,r.i);');
  10288. ConvertProgram;
  10289. CheckSource('TestRecordElement_AsParams',
  10290. LinesToStr([ // statements
  10291. 'rtl.recNewT($mod, "TRecord", function () {',
  10292. ' this.i = 0;',
  10293. ' this.$eq = function (b) {',
  10294. ' return this.i === b.i;',
  10295. ' };',
  10296. ' this.$assign = function (s) {',
  10297. ' this.i = s.i;',
  10298. ' return this;',
  10299. ' };',
  10300. '});',
  10301. 'this.DoIt = function (vG,vH,vI) {',
  10302. ' var vJ = $mod.TRecord.$new();',
  10303. ' $mod.DoIt(vJ.i, vJ.i, {',
  10304. ' p: vJ,',
  10305. ' get: function () {',
  10306. ' return this.p.i;',
  10307. ' },',
  10308. ' set: function (v) {',
  10309. ' this.p.i = v;',
  10310. ' }',
  10311. ' });',
  10312. '};',
  10313. 'this.r = $mod.TRecord.$new();'
  10314. ]),
  10315. LinesToStr([
  10316. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10317. ' p: $mod.r,',
  10318. ' get: function () {',
  10319. ' return this.p.i;',
  10320. ' },',
  10321. ' set: function (v) {',
  10322. ' this.p.i = v;',
  10323. ' }',
  10324. '});'
  10325. ]));
  10326. end;
  10327. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10328. begin
  10329. StartProgram(false);
  10330. Add('type');
  10331. Add(' integer = longint;');
  10332. Add(' TRecord = record');
  10333. Add(' i: integer;');
  10334. Add(' end;');
  10335. Add('function GetRec(vB: integer = 0): TRecord;');
  10336. Add('begin');
  10337. Add('end;');
  10338. Add('procedure DoIt(vG: integer; const vH: integer);');
  10339. Add('begin');
  10340. Add('end;');
  10341. Add('begin');
  10342. Add(' doit(getrec.i,getrec.i);');
  10343. Add(' doit(getrec().i,getrec().i);');
  10344. Add(' doit(getrec(1).i,getrec(2).i);');
  10345. ConvertProgram;
  10346. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10347. LinesToStr([ // statements
  10348. 'rtl.recNewT($mod, "TRecord", function () {',
  10349. ' this.i = 0;',
  10350. ' this.$eq = function (b) {',
  10351. ' return this.i === b.i;',
  10352. ' };',
  10353. ' this.$assign = function (s) {',
  10354. ' this.i = s.i;',
  10355. ' return this;',
  10356. ' };',
  10357. '});',
  10358. 'this.GetRec = function (vB) {',
  10359. ' var Result = $mod.TRecord.$new();',
  10360. ' return Result;',
  10361. '};',
  10362. 'this.DoIt = function (vG, vH) {',
  10363. '};',
  10364. '']),
  10365. LinesToStr([
  10366. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10367. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10368. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10369. '']));
  10370. end;
  10371. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10372. begin
  10373. StartProgram(false);
  10374. Add('type');
  10375. Add(' integer = longint;');
  10376. Add(' TRecord = record');
  10377. Add(' i: integer;');
  10378. Add(' end;');
  10379. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10380. Add('begin');
  10381. Add('end;');
  10382. Add('var r: trecord;');
  10383. Add('begin');
  10384. Add(' with r do ');
  10385. Add(' doit(i,i,i);');
  10386. ConvertProgram;
  10387. CheckSource('TestRecordElementFromWith_AsParams',
  10388. LinesToStr([ // statements
  10389. 'rtl.recNewT($mod, "TRecord", function () {',
  10390. ' this.i = 0;',
  10391. ' this.$eq = function (b) {',
  10392. ' return this.i === b.i;',
  10393. ' };',
  10394. ' this.$assign = function (s) {',
  10395. ' this.i = s.i;',
  10396. ' return this;',
  10397. ' };',
  10398. '});',
  10399. 'this.DoIt = function (vG,vH,vI) {',
  10400. '};',
  10401. 'this.r = $mod.TRecord.$new();'
  10402. ]),
  10403. LinesToStr([
  10404. 'var $with1 = $mod.r;',
  10405. '$mod.DoIt($with1.i,$with1.i,{',
  10406. ' p: $with1,',
  10407. ' get: function () {',
  10408. ' return this.p.i;',
  10409. ' },',
  10410. ' set: function (v) {',
  10411. ' this.p.i = v;',
  10412. ' }',
  10413. '});',
  10414. '']));
  10415. end;
  10416. procedure TTestModule.TestRecord_Equal;
  10417. begin
  10418. StartProgram(false);
  10419. Add('type');
  10420. Add(' integer = longint;');
  10421. Add(' TFlag = (red,blue);');
  10422. Add(' TFlags = set of TFlag;');
  10423. Add(' TProc = procedure;');
  10424. Add(' TRecord = record');
  10425. Add(' i: integer;');
  10426. Add(' Event: TProc;');
  10427. Add(' f: TFlags;');
  10428. Add(' end;');
  10429. Add(' TNested = record');
  10430. Add(' r: TRecord;');
  10431. Add(' end;');
  10432. Add('var');
  10433. Add(' b: boolean;');
  10434. Add(' r,s: trecord;');
  10435. Add('begin');
  10436. Add(' b:=r=s;');
  10437. Add(' b:=r<>s;');
  10438. ConvertProgram;
  10439. CheckSource('TestRecord_Equal',
  10440. LinesToStr([ // statements
  10441. 'this.TFlag = {',
  10442. ' "0": "red",',
  10443. ' red: 0,',
  10444. ' "1": "blue",',
  10445. ' blue: 1',
  10446. '};',
  10447. 'rtl.recNewT($mod, "TRecord", function () {',
  10448. ' this.i = 0;',
  10449. ' this.Event = null;',
  10450. ' this.$new = function () {',
  10451. ' var r = Object.create(this);',
  10452. ' r.f = {};',
  10453. ' return r;',
  10454. ' };',
  10455. ' this.$eq = function (b) {',
  10456. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10457. ' };',
  10458. ' this.$assign = function (s) {',
  10459. ' this.i = s.i;',
  10460. ' this.Event = s.Event;',
  10461. ' this.f = rtl.refSet(s.f);',
  10462. ' return this;',
  10463. ' };',
  10464. '});',
  10465. 'rtl.recNewT($mod, "TNested", function () {',
  10466. ' this.$new = function () {',
  10467. ' var r = Object.create(this);',
  10468. ' r.r = $mod.TRecord.$new();',
  10469. ' return r;',
  10470. ' };',
  10471. ' this.$eq = function (b) {',
  10472. ' return this.r.$eq(b.r);',
  10473. ' };',
  10474. ' this.$assign = function (s) {',
  10475. ' this.r.$assign(s.r);',
  10476. ' return this;',
  10477. ' };',
  10478. '});',
  10479. 'this.b = false;',
  10480. 'this.r = $mod.TRecord.$new();',
  10481. 'this.s = $mod.TRecord.$new();',
  10482. '']),
  10483. LinesToStr([
  10484. '$mod.b = $mod.r.$eq($mod.s);',
  10485. '$mod.b = !$mod.r.$eq($mod.s);',
  10486. '']));
  10487. end;
  10488. procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
  10489. begin
  10490. StartProgram(false);
  10491. Add('type');
  10492. Add(' TRecord = record');
  10493. Add(' i: longint;');
  10494. Add(' end;');
  10495. Add('var');
  10496. Add(' Jv: jsvalue;');
  10497. Add(' Rec: trecord;');
  10498. Add('begin');
  10499. Add(' rec:=trecord(jv);');
  10500. ConvertProgram;
  10501. CheckSource('TestRecord_TypeCastJSValueToRecord',
  10502. LinesToStr([ // statements
  10503. 'rtl.recNewT($mod, "TRecord", function () {',
  10504. ' this.i = 0;',
  10505. ' this.$eq = function (b) {',
  10506. ' return this.i === b.i;',
  10507. ' };',
  10508. ' this.$assign = function (s) {',
  10509. ' this.i = s.i;',
  10510. ' return this;',
  10511. ' };',
  10512. '});',
  10513. 'this.Jv = undefined;',
  10514. 'this.Rec = $mod.TRecord.$new();',
  10515. '']),
  10516. LinesToStr([
  10517. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10518. '']));
  10519. end;
  10520. procedure TTestModule.TestRecord_VariantFail;
  10521. begin
  10522. StartProgram(false);
  10523. Add([
  10524. 'type',
  10525. ' TRec = record',
  10526. ' case word of',
  10527. ' 0: (b0, b1: Byte);',
  10528. ' 1: (i: word);',
  10529. ' end;',
  10530. 'begin']);
  10531. SetExpectedPasResolverError('variant record is not supported',
  10532. nXIsNotSupported);
  10533. ConvertProgram;
  10534. end;
  10535. procedure TTestModule.TestRecord_FieldArray;
  10536. begin
  10537. StartProgram(false);
  10538. Add([
  10539. 'type',
  10540. ' TArrInt = array[3..4] of longint;',
  10541. ' TArrArrInt = array[3..4] of longint;',
  10542. ' TRec = record',
  10543. ' a: array of longint;',
  10544. ' s: array[1..2] of longint;',
  10545. ' m: array[1..2,3..4] of longint;',
  10546. ' o: TArrArrInt;',
  10547. ' end;',
  10548. 'begin']);
  10549. ConvertProgram;
  10550. CheckSource('TestRecord_FieldArray',
  10551. LinesToStr([ // statements
  10552. 'rtl.recNewT($mod, "TRec", function () {',
  10553. ' this.$new = function () {',
  10554. ' var r = Object.create(this);',
  10555. ' r.a = [];',
  10556. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10557. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10558. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10559. ' return r;',
  10560. ' };',
  10561. ' this.$eq = function (b) {',
  10562. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10563. ' };',
  10564. ' this.$assign = function (s) {',
  10565. ' this.a = rtl.arrayRef(s.a);',
  10566. ' this.s = s.s.slice(0);',
  10567. ' this.m = s.m.slice(0);',
  10568. ' this.o = s.o.slice(0);',
  10569. ' return this;',
  10570. ' };',
  10571. '});',
  10572. '']),
  10573. LinesToStr([ // $mod.$main
  10574. '']));
  10575. end;
  10576. procedure TTestModule.TestRecord_Const;
  10577. begin
  10578. StartProgram(false);
  10579. Add([
  10580. 'type',
  10581. ' TArrInt = array[3..4] of longint;',
  10582. ' TPoint = record x,y: longint; end;',
  10583. ' TRec = record',
  10584. ' i: longint;',
  10585. ' a: array of longint;',
  10586. ' s: array[1..2] of longint;',
  10587. ' m: array[1..2,3..4] of longint;',
  10588. ' p: TPoint;',
  10589. ' end;',
  10590. ' TPoints = array of TPoint;',
  10591. 'const',
  10592. ' r: TRec = (',
  10593. ' i:1;',
  10594. ' a:(2,3);',
  10595. ' s:(4,5);',
  10596. ' m:( (11,12), (13,14) );',
  10597. ' p: (x:21; y:22)',
  10598. ' );',
  10599. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10600. 'begin']);
  10601. ConvertProgram;
  10602. CheckSource('TestRecord_Const',
  10603. LinesToStr([ // statements
  10604. 'rtl.recNewT($mod, "TPoint", function () {',
  10605. ' this.x = 0;',
  10606. ' this.y = 0;',
  10607. ' this.$eq = function (b) {',
  10608. ' return (this.x === b.x) && (this.y === b.y);',
  10609. ' };',
  10610. ' this.$assign = function (s) {',
  10611. ' this.x = s.x;',
  10612. ' this.y = s.y;',
  10613. ' return this;',
  10614. ' };',
  10615. '});',
  10616. 'rtl.recNewT($mod, "TRec", function () {',
  10617. ' this.i = 0;',
  10618. ' this.$new = function () {',
  10619. ' var r = Object.create(this);',
  10620. ' r.a = [];',
  10621. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10622. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10623. ' r.p = $mod.TPoint.$new();',
  10624. ' return r;',
  10625. ' };',
  10626. ' this.$eq = function (b) {',
  10627. ' return (this.i === b.i) && (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && this.p.$eq(b.p);',
  10628. ' };',
  10629. ' this.$assign = function (s) {',
  10630. ' this.i = s.i;',
  10631. ' this.a = rtl.arrayRef(s.a);',
  10632. ' this.s = s.s.slice(0);',
  10633. ' this.m = s.m.slice(0);',
  10634. ' this.p.$assign(s.p);',
  10635. ' return this;',
  10636. ' };',
  10637. '});',
  10638. 'this.r = $mod.TRec.$clone({',
  10639. ' i: 1,',
  10640. ' a: [2, 3],',
  10641. ' s: [4, 5],',
  10642. ' m: [[11, 12], [13, 14]],',
  10643. ' p: $mod.TPoint.$clone({',
  10644. ' x: 21,',
  10645. ' y: 22',
  10646. ' })',
  10647. '});',
  10648. 'this.p = [$mod.TPoint.$clone({',
  10649. ' x: 1,',
  10650. ' y: 2',
  10651. '}), $mod.TPoint.$clone({',
  10652. ' x: 3,',
  10653. ' y: 4',
  10654. '})];',
  10655. '']),
  10656. LinesToStr([ // $mod.$main
  10657. '']));
  10658. end;
  10659. procedure TTestModule.TestRecord_TypecastFail;
  10660. begin
  10661. StartProgram(false);
  10662. Add([
  10663. 'type',
  10664. ' TPoint = record x,y: longint; end;',
  10665. ' TRec = record l: longint end;',
  10666. 'var p: TPoint;',
  10667. 'begin',
  10668. ' if TRec(p).l=2 then ;']);
  10669. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10670. nIllegalTypeConversionTo);
  10671. ConvertProgram;
  10672. end;
  10673. procedure TTestModule.TestRecord_InFunction;
  10674. begin
  10675. StartProgram(false);
  10676. Add([
  10677. 'var TPoint: longint = 3;',
  10678. 'procedure DoIt;',
  10679. 'type',
  10680. ' TPoint = record x,y: longint; end;',
  10681. ' TPoints = array of TPoint;',
  10682. 'var',
  10683. ' r: TPoint;',
  10684. ' p: TPoints;',
  10685. 'begin',
  10686. ' SetLength(p,2);',
  10687. 'end;',
  10688. 'begin']);
  10689. ConvertProgram;
  10690. CheckSource('TestRecord_InFunction',
  10691. LinesToStr([ // statements
  10692. 'this.TPoint = 3;',
  10693. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10694. ' this.x = 0;',
  10695. ' this.y = 0;',
  10696. ' this.$eq = function (b) {',
  10697. ' return (this.x === b.x) && (this.y === b.y);',
  10698. ' };',
  10699. ' this.$assign = function (s) {',
  10700. ' this.x = s.x;',
  10701. ' this.y = s.y;',
  10702. ' return this;',
  10703. ' };',
  10704. '});',
  10705. 'this.DoIt = function () {',
  10706. ' var r = TPoint$1.$new();',
  10707. ' var p = [];',
  10708. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10709. '};',
  10710. '']),
  10711. LinesToStr([ // $mod.$main
  10712. '']));
  10713. end;
  10714. procedure TTestModule.TestRecord_AnonymousFail;
  10715. begin
  10716. StartProgram(false);
  10717. Add([
  10718. 'var',
  10719. ' r: record x: word end;',
  10720. 'begin']);
  10721. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] anonymous record type',
  10722. nNotYetImplemented);
  10723. ConvertProgram;
  10724. end;
  10725. procedure TTestModule.TestAdvRecord_Function;
  10726. begin
  10727. StartProgram(false);
  10728. Parser.Options:=Parser.Options+[po_cassignments];
  10729. Add([
  10730. '{$modeswitch AdvancedRecords}',
  10731. 'type',
  10732. ' TPoint = record',
  10733. ' x,y: word;',
  10734. ' function Add(const apt: TPoint): TPoint;',
  10735. ' end;',
  10736. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10737. 'begin',
  10738. ' Result:=Self;',
  10739. ' Result.x+=apt.x;',
  10740. ' Result.y:=Result.y+apt.y;',
  10741. ' Self:=apt;',
  10742. 'end;',
  10743. 'var p,q: TPoint;',
  10744. 'begin',
  10745. ' p.add(q);',
  10746. ' p:=default(TPoint);',
  10747. ' p:=q;',
  10748. '']);
  10749. ConvertProgram;
  10750. CheckSource('TestAdvRecord_Function',
  10751. LinesToStr([ // statements
  10752. 'rtl.recNewT($mod, "TPoint", function () {',
  10753. ' this.x = 0;',
  10754. ' this.y = 0;',
  10755. ' this.$eq = function (b) {',
  10756. ' return (this.x === b.x) && (this.y === b.y);',
  10757. ' };',
  10758. ' this.$assign = function (s) {',
  10759. ' this.x = s.x;',
  10760. ' this.y = s.y;',
  10761. ' return this;',
  10762. ' };',
  10763. ' this.Add = function (apt) {',
  10764. ' var Result = $mod.TPoint.$new();',
  10765. ' Result.$assign(this);',
  10766. ' Result.x += apt.x;',
  10767. ' Result.y = Result.y + apt.y;',
  10768. ' this.$assign(apt);',
  10769. ' return Result;',
  10770. ' };',
  10771. '});',
  10772. 'this.p = $mod.TPoint.$new();',
  10773. 'this.q = $mod.TPoint.$new();',
  10774. '']),
  10775. LinesToStr([ // $mod.$main
  10776. '$mod.p.Add($mod.q);',
  10777. '$mod.p.$assign($mod.TPoint.$new());',
  10778. '$mod.p.$assign($mod.q);',
  10779. '']));
  10780. end;
  10781. procedure TTestModule.TestAdvRecord_Property;
  10782. begin
  10783. StartProgram(false);
  10784. Add([
  10785. '{$modeswitch AdvancedRecords}',
  10786. 'type',
  10787. ' TPoint = record',
  10788. ' x,y: word;',
  10789. ' strict private',
  10790. ' function GetSize: longword;',
  10791. ' procedure SetSize(Value: longword);',
  10792. ' public',
  10793. ' property Size: longword read GetSize write SetSize;',
  10794. ' property Left: word read x write y;',
  10795. ' end;',
  10796. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  10797. 'function TPoint.GetSize: longword;',
  10798. 'begin',
  10799. ' x:=y;',
  10800. ' Size:=Size;',
  10801. ' Left:=Left;',
  10802. 'end;',
  10803. 'procedure TPoint.SetSize(Value: longword);',
  10804. 'begin',
  10805. 'end;',
  10806. 'var p,q: TPoint;',
  10807. 'begin',
  10808. ' p.Size:=q.Size;',
  10809. ' p.Left:=q.Left;',
  10810. '']);
  10811. ConvertProgram;
  10812. CheckSource('TestAdvRecord_Property',
  10813. LinesToStr([ // statements
  10814. 'rtl.recNewT($mod, "TPoint", function () {',
  10815. ' this.x = 0;',
  10816. ' this.y = 0;',
  10817. ' this.$eq = function (b) {',
  10818. ' return (this.x === b.x) && (this.y === b.y);',
  10819. ' };',
  10820. ' this.$assign = function (s) {',
  10821. ' this.x = s.x;',
  10822. ' this.y = s.y;',
  10823. ' return this;',
  10824. ' };',
  10825. ' this.GetSize = function () {',
  10826. ' var Result = 0;',
  10827. ' this.x = this.y;',
  10828. ' this.SetSize(this.GetSize());',
  10829. ' this.y = this.x;',
  10830. ' return Result;',
  10831. ' };',
  10832. ' this.SetSize = function (Value) {',
  10833. ' };',
  10834. '});',
  10835. 'this.SetSize = function (Value) {',
  10836. '};',
  10837. 'this.p = $mod.TPoint.$new();',
  10838. 'this.q = $mod.TPoint.$new();',
  10839. '']),
  10840. LinesToStr([ // $mod.$main
  10841. '$mod.p.SetSize($mod.q.GetSize());',
  10842. '$mod.p.y = $mod.q.x;',
  10843. '']));
  10844. end;
  10845. procedure TTestModule.TestAdvRecord_PropertyDefault;
  10846. begin
  10847. StartProgram(false);
  10848. Add([
  10849. '{$modeswitch AdvancedRecords}',
  10850. 'type',
  10851. ' TPoint = record',
  10852. ' strict private',
  10853. ' function GetItems(Index: word): word;',
  10854. ' procedure SetItems(Index: word; Value: word);',
  10855. ' public',
  10856. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  10857. ' end;',
  10858. 'function TPoint.GetItems(Index: word): word;',
  10859. 'begin',
  10860. ' Items[index]:=Items[index];',
  10861. ' self.Items[index]:=self.Items[index];',
  10862. 'end;',
  10863. 'procedure TPoint.SetItems(Index: word; Value: word);',
  10864. 'begin',
  10865. 'end;',
  10866. 'var p: TPoint;',
  10867. 'begin',
  10868. ' p[1]:=p[2];',
  10869. ' p.Items[3]:=p.Items[4];',
  10870. '']);
  10871. ConvertProgram;
  10872. CheckSource('TestAdvRecord_PropertyDefault',
  10873. LinesToStr([ // statements
  10874. 'rtl.recNewT($mod, "TPoint", function () {',
  10875. ' this.$eq = function (b) {',
  10876. ' return true;',
  10877. ' };',
  10878. ' this.$assign = function (s) {',
  10879. ' return this;',
  10880. ' };',
  10881. ' this.GetItems = function (Index) {',
  10882. ' var Result = 0;',
  10883. ' this.SetItems(Index, this.GetItems(Index));',
  10884. ' this.SetItems(Index, this.GetItems(Index));',
  10885. ' return Result;',
  10886. ' };',
  10887. ' this.SetItems = function (Index, Value) {',
  10888. ' };',
  10889. '});',
  10890. 'this.p = $mod.TPoint.$new();',
  10891. '']),
  10892. LinesToStr([ // $mod.$main
  10893. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  10894. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  10895. '']));
  10896. end;
  10897. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  10898. begin
  10899. StartProgram(false);
  10900. Add([
  10901. '{$modeswitch AdvancedRecords}',
  10902. 'type',
  10903. ' TRec = record',
  10904. ' class var',
  10905. ' Fx: longint;',
  10906. ' Fy: longint;',
  10907. ' class function GetInt: longint; static;',
  10908. ' class procedure SetInt(Value: longint); static;',
  10909. ' class procedure DoIt; static;',
  10910. ' class property IntA: longint read Fx write Fy;',
  10911. ' class property IntB: longint read GetInt write SetInt;',
  10912. ' end;',
  10913. 'class function trec.getint: longint;',
  10914. 'begin',
  10915. ' result:=fx;',
  10916. 'end;',
  10917. 'class procedure trec.setint(value: longint);',
  10918. 'begin',
  10919. 'end;',
  10920. 'class procedure trec.doit;',
  10921. 'begin',
  10922. ' IntA:=IntA+1;',
  10923. ' IntB:=IntB+1;',
  10924. 'end;',
  10925. 'var r: trec;',
  10926. 'begin',
  10927. ' trec.inta:=trec.inta+1;',
  10928. ' if trec.intb=2 then;',
  10929. ' trec.intb:=trec.intb+2;',
  10930. ' trec.setint(trec.inta);',
  10931. ' r.inta:=r.inta+1;',
  10932. ' if r.intb=2 then;',
  10933. ' r.intb:=r.intb+2;',
  10934. ' r.setint(r.inta);']);
  10935. ConvertProgram;
  10936. CheckSource('TestAdvRecord_Property_ClassMethod',
  10937. LinesToStr([ // statements
  10938. 'rtl.recNewT($mod, "TRec", function () {',
  10939. ' this.Fx = 0;',
  10940. ' this.Fy = 0;',
  10941. ' this.$eq = function (b) {',
  10942. ' return true;',
  10943. ' };',
  10944. ' this.$assign = function (s) {',
  10945. ' return this;',
  10946. ' };',
  10947. ' this.GetInt = function () {',
  10948. ' var Result = 0;',
  10949. ' Result = this.Fx;',
  10950. ' return Result;',
  10951. ' };',
  10952. ' this.SetInt = function (Value) {',
  10953. ' };',
  10954. ' this.DoIt = function () {',
  10955. ' $mod.TRec.Fy = this.Fx + 1;',
  10956. ' this.SetInt(this.GetInt() + 1);',
  10957. ' };',
  10958. '}, true);',
  10959. 'this.r = $mod.TRec.$new();',
  10960. '']),
  10961. LinesToStr([ // $mod.$main
  10962. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10963. 'if ($mod.TRec.GetInt() === 2) ;',
  10964. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  10965. '$mod.TRec.SetInt($mod.TRec.Fx);',
  10966. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  10967. 'if ($mod.r.GetInt() === 2) ;',
  10968. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  10969. '$mod.r.SetInt($mod.r.Fx);',
  10970. '']));
  10971. end;
  10972. procedure TTestModule.TestAdvRecord_Const;
  10973. begin
  10974. StartProgram(false);
  10975. Add([
  10976. '{$modeswitch AdvancedRecords}',
  10977. 'type',
  10978. ' TArrInt = array[3..4] of longint;',
  10979. ' TPoint = record',
  10980. ' x,y: longint;',
  10981. ' class var Count: nativeint;',
  10982. ' end;',
  10983. ' TRec = record',
  10984. ' i: longint;',
  10985. ' a: array of longint;',
  10986. ' s: array[1..2] of longint;',
  10987. ' m: array[1..2,3..4] of longint;',
  10988. ' p: TPoint;',
  10989. ' end;',
  10990. ' TPoints = array of TPoint;',
  10991. 'const',
  10992. ' r: TRec = (',
  10993. ' i:1;',
  10994. ' a:(2,3);',
  10995. ' s:(4,5);',
  10996. ' m:( (11,12), (13,14) );',
  10997. ' p: (x:21)',
  10998. ' );',
  10999. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11000. 'begin']);
  11001. ConvertProgram;
  11002. CheckSource('TestAdvRecord_Const',
  11003. LinesToStr([ // statements
  11004. 'rtl.recNewT($mod, "TPoint", function () {',
  11005. ' this.x = 0;',
  11006. ' this.y = 0;',
  11007. ' this.Count = 0;',
  11008. ' this.$eq = function (b) {',
  11009. ' return (this.x === b.x) && (this.y === b.y);',
  11010. ' };',
  11011. ' this.$assign = function (s) {',
  11012. ' this.x = s.x;',
  11013. ' this.y = s.y;',
  11014. ' return this;',
  11015. ' };',
  11016. '}, true);',
  11017. 'rtl.recNewT($mod, "TRec", function () {',
  11018. ' this.i = 0;',
  11019. ' this.$new = function () {',
  11020. ' var r = Object.create(this);',
  11021. ' r.a = [];',
  11022. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11023. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11024. ' r.p = $mod.TPoint.$new();',
  11025. ' return r;',
  11026. ' };',
  11027. ' this.$eq = function (b) {',
  11028. ' return (this.i === b.i) && (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && this.p.$eq(b.p);',
  11029. ' };',
  11030. ' this.$assign = function (s) {',
  11031. ' this.i = s.i;',
  11032. ' this.a = rtl.arrayRef(s.a);',
  11033. ' this.s = s.s.slice(0);',
  11034. ' this.m = s.m.slice(0);',
  11035. ' this.p.$assign(s.p);',
  11036. ' return this;',
  11037. ' };',
  11038. '});',
  11039. 'this.r = $mod.TRec.$clone({',
  11040. ' i: 1,',
  11041. ' a: [2, 3],',
  11042. ' s: [4, 5],',
  11043. ' m: [[11, 12], [13, 14]],',
  11044. ' p: $mod.TPoint.$clone({',
  11045. ' x: 21,',
  11046. ' y: 0',
  11047. ' })',
  11048. '});',
  11049. 'this.p = [$mod.TPoint.$clone({',
  11050. ' x: 1,',
  11051. ' y: 2',
  11052. '}), $mod.TPoint.$clone({',
  11053. ' x: 3,',
  11054. ' y: 4',
  11055. '})];',
  11056. '']),
  11057. LinesToStr([ // $mod.$main
  11058. '']));
  11059. end;
  11060. procedure TTestModule.TestAdvRecord_ExternalField;
  11061. begin
  11062. StartProgram(false);
  11063. Add([
  11064. '{$modeswitch AdvancedRecords}',
  11065. '{$modeswitch externalclass}',
  11066. 'type',
  11067. ' TCar = record',
  11068. ' public',
  11069. ' Intern: longint external name ''$Intern'';',
  11070. ' Intern2: longint external name ''$Intern2'';',
  11071. ' Bracket: longint external name ''["A B"]'';',
  11072. ' procedure DoIt;',
  11073. ' end;',
  11074. 'implementation',
  11075. 'procedure tcar.doit;',
  11076. 'begin',
  11077. ' Intern:=Intern+1;',
  11078. ' Intern2:=Intern2+2;',
  11079. ' Bracket:=Bracket+3;',
  11080. 'end;',
  11081. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11082. 'begin',
  11083. ' Rec.intern:=Rec.intern+1;',
  11084. ' Rec.intern2:=Rec.intern2+2;',
  11085. ' Rec.Bracket:=Rec.Bracket+3;',
  11086. ' with Rec do begin',
  11087. ' intern:=intern+1;',
  11088. ' intern2:=intern2+2;',
  11089. ' Bracket:=Bracket+3;',
  11090. ' end;']);
  11091. ConvertProgram;
  11092. CheckSource('TestAdvRecord_ExternalField',
  11093. LinesToStr([ // statements
  11094. 'rtl.recNewT($mod, "TCar", function () {',
  11095. ' this.$eq = function (b) {',
  11096. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11097. ' };',
  11098. ' this.$assign = function (s) {',
  11099. ' this.$Intern = s.$Intern;',
  11100. ' this.$Intern2 = s.$Intern2;',
  11101. ' this["A B"] = s["A B"];',
  11102. ' return this;',
  11103. ' };',
  11104. ' this.DoIt = function () {',
  11105. ' this.$Intern = this.$Intern + 1;',
  11106. ' this.$Intern2 = this.$Intern2 + 2;',
  11107. ' this["A B"] = this["A B"] + 3;',
  11108. ' };',
  11109. '});',
  11110. 'this.Rec = $mod.TCar.$clone({',
  11111. ' $Intern: 11,',
  11112. ' $Intern2: 12,',
  11113. ' "A B": 13',
  11114. '});',
  11115. '']),
  11116. LinesToStr([ // $mod.$main
  11117. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11118. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11119. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11120. 'var $with1 = $mod.Rec;',
  11121. '$with1.$Intern = $with1.$Intern + 1;',
  11122. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  11123. '$with1["A B"] = $with1["A B"] + 3;',
  11124. '']));
  11125. end;
  11126. procedure TTestModule.TestAdvRecord_SubRecord;
  11127. begin
  11128. StartProgram(false);
  11129. Add([
  11130. '{$modeswitch AdvancedRecords}',
  11131. 'type',
  11132. ' TRec = record',
  11133. ' type',
  11134. ' TPoint = record',
  11135. ' x,y: longint;',
  11136. ' class var Count: nativeint;',
  11137. ' procedure DoIt;',
  11138. ' class procedure DoThat; static;',
  11139. ' end;',
  11140. ' var',
  11141. ' i: longint;',
  11142. ' p: TPoint;',
  11143. ' procedure DoSome;',
  11144. ' end;',
  11145. 'const',
  11146. ' r: TRec = (',
  11147. ' i:1;',
  11148. ' p: (x:21;y:22)',
  11149. ' );',
  11150. 'procedure TRec.DoSome;',
  11151. 'begin',
  11152. ' p.x:=p.y+1;',
  11153. ' p.Count:=p.Count+2;',
  11154. 'end;',
  11155. 'procedure TRec.TPoint.DoIt;',
  11156. 'begin',
  11157. ' Count:=Count+3;',
  11158. 'end;',
  11159. 'class procedure TRec.TPoint.DoThat;',
  11160. 'begin',
  11161. ' Count:=Count+4;',
  11162. 'end;',
  11163. 'begin']);
  11164. ConvertProgram;
  11165. CheckSource('TestAdvRecord_SubRecord',
  11166. LinesToStr([ // statements
  11167. 'rtl.recNewT($mod, "TRec", function () {',
  11168. ' rtl.recNewT(this, "TPoint", function () {',
  11169. ' this.x = 0;',
  11170. ' this.y = 0;',
  11171. ' this.Count = 0;',
  11172. ' this.$eq = function (b) {',
  11173. ' return (this.x === b.x) && (this.y === b.y);',
  11174. ' };',
  11175. ' this.$assign = function (s) {',
  11176. ' this.x = s.x;',
  11177. ' this.y = s.y;',
  11178. ' return this;',
  11179. ' };',
  11180. ' this.DoIt = function () {',
  11181. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11182. ' };',
  11183. ' this.DoThat = function () {',
  11184. ' $mod.TRec.TPoint.Count = this.Count + 4;',
  11185. ' };',
  11186. ' }, true);',
  11187. ' this.i = 0;',
  11188. ' this.$new = function () {',
  11189. ' var r = Object.create(this);',
  11190. ' r.p = this.TPoint.$new();',
  11191. ' return r;',
  11192. ' };',
  11193. ' this.$eq = function (b) {',
  11194. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11195. ' };',
  11196. ' this.$assign = function (s) {',
  11197. ' this.i = s.i;',
  11198. ' this.p.$assign(s.p);',
  11199. ' return this;',
  11200. ' };',
  11201. ' this.DoSome = function () {',
  11202. ' this.p.x = this.p.y + 1;',
  11203. ' this.TPoint.Count = this.p.Count + 2;',
  11204. ' };',
  11205. '}, true);',
  11206. 'this.r = $mod.TRec.$clone({',
  11207. ' i: 1,',
  11208. ' p: $mod.TRec.TPoint.$clone({',
  11209. ' x: 21,',
  11210. ' y: 22',
  11211. ' })',
  11212. '});',
  11213. '']),
  11214. LinesToStr([ // $mod.$main
  11215. '']));
  11216. end;
  11217. procedure TTestModule.TestAdvRecord_SubClass;
  11218. begin
  11219. StartProgram(false);
  11220. Add([
  11221. '{$modeswitch AdvancedRecords}',
  11222. 'type',
  11223. ' TObject = class end;',
  11224. ' TPoint = record',
  11225. ' type',
  11226. ' TBird = class',
  11227. ' procedure DoIt;',
  11228. ' class procedure Glob;',
  11229. ' end;',
  11230. ' procedure DoIt(b: TBird);',
  11231. ' end;',
  11232. 'procedure TPoint.TBird.DoIt;',
  11233. 'begin',
  11234. ' doit;',
  11235. ' self.doit;',
  11236. ' glob;',
  11237. ' self.glob;',
  11238. 'end;',
  11239. 'class procedure TPoint.TBird.Glob;',
  11240. 'begin',
  11241. ' glob;',
  11242. ' self.glob;',
  11243. 'end;',
  11244. 'procedure TPoint.DoIt(b: TBird);',
  11245. 'begin',
  11246. ' b.doit;',
  11247. ' b.glob;',
  11248. ' TBird.glob;',
  11249. 'end;',
  11250. 'begin',
  11251. '']);
  11252. ConvertProgram;
  11253. CheckSource('TestAdvRecord_SubClass',
  11254. LinesToStr([ // statements
  11255. 'rtl.createClass($mod, "TObject", null, function () {',
  11256. ' this.$init = function () {',
  11257. ' };',
  11258. ' this.$final = function () {',
  11259. ' };',
  11260. '});',
  11261. 'rtl.recNewT($mod, "TPoint", function () {',
  11262. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11263. ' this.DoIt = function () {',
  11264. ' this.DoIt();',
  11265. ' this.DoIt();',
  11266. ' this.$class.Glob();',
  11267. ' this.$class.Glob();',
  11268. ' };',
  11269. ' this.Glob = function () {',
  11270. ' this.Glob();',
  11271. ' this.Glob();',
  11272. ' };',
  11273. ' });',
  11274. ' this.$eq = function (b) {',
  11275. ' return true;',
  11276. ' };',
  11277. ' this.$assign = function (s) {',
  11278. ' return this;',
  11279. ' };',
  11280. ' this.DoIt = function (b) {',
  11281. ' b.DoIt();',
  11282. ' b.$class.Glob();',
  11283. ' this.TBird.Glob();',
  11284. ' };',
  11285. '}, true);',
  11286. '']),
  11287. LinesToStr([ // $mod.$main
  11288. '']));
  11289. end;
  11290. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11291. begin
  11292. StartProgram(false);
  11293. Add([
  11294. '{$modeswitch AdvancedRecords}',
  11295. 'type',
  11296. ' IUnknown = interface end;',
  11297. ' TPoint = record',
  11298. ' type IBird = interface end;',
  11299. ' end;',
  11300. 'begin',
  11301. '']);
  11302. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] interface inside record',
  11303. nNotYetImplemented);
  11304. ParseProgram;
  11305. end;
  11306. procedure TTestModule.TestAdvRecord_Constructor;
  11307. begin
  11308. StartProgram(false);
  11309. Add([
  11310. '{$modeswitch AdvancedRecords}',
  11311. 'type',
  11312. ' TPoint = record',
  11313. ' x,y: longint;',
  11314. ' constructor Create(ax: longint; ay: longint = -1);',
  11315. ' end;',
  11316. 'constructor tpoint.create(ax,ay: longint);',
  11317. 'begin',
  11318. ' x:=ax;',
  11319. ' self.y:=ay;',
  11320. 'end;',
  11321. 'var r: TPoint;',
  11322. 'begin',
  11323. ' r:=TPoint.Create(1,2);',
  11324. ' with TPoint do r:=Create(1,2);',
  11325. ' r.Create(3);',
  11326. ' r:=r.Create(4);',
  11327. '']);
  11328. ConvertProgram;
  11329. CheckSource('TestAdvRecord_Constructor',
  11330. LinesToStr([ // statements
  11331. 'rtl.recNewT($mod, "TPoint", function () {',
  11332. ' this.x = 0;',
  11333. ' this.y = 0;',
  11334. ' this.$eq = function (b) {',
  11335. ' return (this.x === b.x) && (this.y === b.y);',
  11336. ' };',
  11337. ' this.$assign = function (s) {',
  11338. ' this.x = s.x;',
  11339. ' this.y = s.y;',
  11340. ' return this;',
  11341. ' };',
  11342. ' this.Create = function (ax, ay) {',
  11343. ' this.x = ax;',
  11344. ' this.y = ay;',
  11345. ' return this;',
  11346. ' };',
  11347. '}, true);',
  11348. 'this.r = $mod.TPoint.$new();',
  11349. '']),
  11350. LinesToStr([ // $mod.$main
  11351. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11352. 'var $with1 = $mod.TPoint;',
  11353. '$mod.r.$assign($with1.$new().Create(1, 2));',
  11354. '$mod.r.Create(3, -1);',
  11355. '$mod.r.$assign($mod.r.Create(4, -1));',
  11356. '']));
  11357. end;
  11358. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11359. begin
  11360. StartProgram(false);
  11361. Add(['type',
  11362. ' TObject = class',
  11363. ' public',
  11364. ' constructor Create;',
  11365. ' destructor Destroy;',
  11366. ' end;',
  11367. ' TBird = TObject;',
  11368. 'constructor tobject.create;',
  11369. 'begin end;',
  11370. 'destructor tobject.destroy;',
  11371. 'begin end;',
  11372. 'var Obj: tobject;',
  11373. 'begin',
  11374. ' obj:=tobject.create;',
  11375. ' obj:=tobject.create();',
  11376. ' obj:=tbird.create;',
  11377. ' obj:=tbird.create();',
  11378. ' obj:=obj.create();',
  11379. ' obj.destroy;',
  11380. '']);
  11381. ConvertProgram;
  11382. CheckSource('TestClass_TObjectDefaultConstructor',
  11383. LinesToStr([ // statements
  11384. 'rtl.createClass($mod,"TObject",null,function(){',
  11385. ' this.$init = function () {',
  11386. ' };',
  11387. ' this.$final = function () {',
  11388. ' };',
  11389. ' this.Create = function(){',
  11390. ' return this;',
  11391. ' };',
  11392. ' this.Destroy = function(){',
  11393. ' };',
  11394. '});',
  11395. 'this.Obj = null;'
  11396. ]),
  11397. LinesToStr([ // $mod.$main
  11398. '$mod.Obj = $mod.TObject.$create("Create");',
  11399. '$mod.Obj = $mod.TObject.$create("Create");',
  11400. '$mod.Obj = $mod.TObject.$create("Create");',
  11401. '$mod.Obj = $mod.TObject.$create("Create");',
  11402. '$mod.Obj = $mod.Obj.Create();',
  11403. '$mod.Obj.$destroy("Destroy");',
  11404. '']));
  11405. end;
  11406. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11407. begin
  11408. StartProgram(false);
  11409. Add('type');
  11410. Add(' TObject = class');
  11411. Add(' public');
  11412. Add(' constructor Create(Par: longint);');
  11413. Add(' end;');
  11414. Add('constructor tobject.create(par: longint);');
  11415. Add('begin end;');
  11416. Add('var Obj: tobject;');
  11417. Add('begin');
  11418. Add(' obj:=tobject.create(3);');
  11419. ConvertProgram;
  11420. CheckSource('TestClass_TObjectConstructorWithParams',
  11421. LinesToStr([ // statements
  11422. 'rtl.createClass($mod,"TObject",null,function(){',
  11423. ' this.$init = function () {',
  11424. ' };',
  11425. ' this.$final = function () {',
  11426. ' };',
  11427. ' this.Create = function(Par){',
  11428. ' return this;',
  11429. ' };',
  11430. '});',
  11431. 'this.Obj = null;'
  11432. ]),
  11433. LinesToStr([ // $mod.$main
  11434. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11435. ]));
  11436. end;
  11437. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11438. begin
  11439. StartProgram(false);
  11440. Add('type');
  11441. Add(' TObject = class');
  11442. Add(' public');
  11443. Add(' constructor Create;');
  11444. Add(' end;');
  11445. Add(' TTest = class(TObject)');
  11446. Add(' public');
  11447. Add(' constructor Create(const Par: longint = 1);');
  11448. Add(' end;');
  11449. Add('constructor tobject.create;');
  11450. Add('begin end;');
  11451. Add('constructor ttest.create(const par: longint);');
  11452. Add('begin end;');
  11453. Add('var t: ttest;');
  11454. Add('begin');
  11455. Add(' t:=ttest.create;');
  11456. Add(' t:=ttest.create(2);');
  11457. ConvertProgram;
  11458. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11459. LinesToStr([ // statements
  11460. 'rtl.createClass($mod,"TObject",null,function(){',
  11461. ' this.$init = function () {',
  11462. ' };',
  11463. ' this.$final = function () {',
  11464. ' };',
  11465. ' this.Create = function(){',
  11466. ' return this;',
  11467. ' };',
  11468. '});',
  11469. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11470. ' this.Create$1 = function (Par) {',
  11471. ' return this;',
  11472. ' };',
  11473. '});',
  11474. 'this.t = null;'
  11475. ]),
  11476. LinesToStr([ // $mod.$main
  11477. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11478. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11479. ]));
  11480. end;
  11481. procedure TTestModule.TestClass_Var;
  11482. begin
  11483. StartProgram(false);
  11484. Add([
  11485. 'type',
  11486. ' TObject = class',
  11487. ' public',
  11488. ' vI: longint;',
  11489. ' constructor Create(Par: longint);',
  11490. ' end;',
  11491. 'constructor tobject.create(par: longint);',
  11492. 'begin',
  11493. ' vi:=par+3',
  11494. 'end;',
  11495. 'var Obj: tobject;',
  11496. 'begin',
  11497. ' obj:=tobject.create(4);',
  11498. ' obj.vi:=obj.VI+5;']);
  11499. ConvertProgram;
  11500. CheckSource('TestClass_Var',
  11501. LinesToStr([ // statements
  11502. 'rtl.createClass($mod,"TObject",null,function(){',
  11503. ' this.$init = function () {',
  11504. ' this.vI = 0;',
  11505. ' };',
  11506. ' this.$final = function () {',
  11507. ' };',
  11508. ' this.Create = function(Par){',
  11509. ' this.vI = Par+3;',
  11510. ' return this;',
  11511. ' };',
  11512. '});',
  11513. 'this.Obj = null;'
  11514. ]),
  11515. LinesToStr([ // $mod.$main
  11516. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11517. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11518. ]));
  11519. end;
  11520. procedure TTestModule.TestClass_Method;
  11521. begin
  11522. StartProgram(false);
  11523. Add('type');
  11524. Add(' TObject = class');
  11525. Add(' public');
  11526. Add(' vI: longint;');
  11527. Add(' Sub: TObject;');
  11528. Add(' constructor Create;');
  11529. Add(' function GetIt(Par: longint): tobject;');
  11530. Add(' end;');
  11531. Add('constructor tobject.create; begin end;');
  11532. Add('function tobject.getit(par: longint): tobject;');
  11533. Add('begin');
  11534. Add(' Self.vi:=par+3;');
  11535. Add(' Result:=self.sub;');
  11536. Add('end;');
  11537. Add('var Obj: tobject;');
  11538. Add('begin');
  11539. Add(' obj:=tobject.create;');
  11540. Add(' obj.getit(4);');
  11541. Add(' obj.sub.sub:=nil;');
  11542. Add(' obj.sub.getit(5);');
  11543. Add(' obj.sub.getit(6).SUB:=nil;');
  11544. Add(' obj.sub.getit(7).GETIT(8);');
  11545. Add(' obj.sub.getit(9).SuB.getit(10);');
  11546. ConvertProgram;
  11547. CheckSource('TestClass_Method',
  11548. LinesToStr([ // statements
  11549. 'rtl.createClass($mod,"TObject",null,function(){',
  11550. ' this.$init = function () {',
  11551. ' this.vI = 0;',
  11552. ' this.Sub = null;',
  11553. ' };',
  11554. ' this.$final = function () {',
  11555. ' this.Sub = undefined;',
  11556. ' };',
  11557. ' this.Create = function(){',
  11558. ' return this;',
  11559. ' };',
  11560. ' this.GetIt = function(Par){',
  11561. ' var Result = null;',
  11562. ' this.vI = Par + 3;',
  11563. ' Result = this.Sub;',
  11564. ' return Result;',
  11565. ' };',
  11566. '});',
  11567. 'this.Obj = null;'
  11568. ]),
  11569. LinesToStr([ // $mod.$main
  11570. '$mod.Obj = $mod.TObject.$create("Create");',
  11571. '$mod.Obj.GetIt(4);',
  11572. '$mod.Obj.Sub.Sub=null;',
  11573. '$mod.Obj.Sub.GetIt(5);',
  11574. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11575. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11576. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11577. ]));
  11578. end;
  11579. procedure TTestModule.TestClass_Implementation;
  11580. begin
  11581. StartUnit(false);
  11582. Add([
  11583. 'interface',
  11584. 'type',
  11585. ' TObject = class',
  11586. ' constructor Create;',
  11587. ' end;',
  11588. 'implementation',
  11589. 'type',
  11590. ' TIntClass = class',
  11591. ' constructor Create; reintroduce;',
  11592. ' class procedure DoGlob;',
  11593. ' end;',
  11594. 'constructor tintclass.create;',
  11595. 'begin',
  11596. ' inherited;',
  11597. ' inherited create;',
  11598. ' doglob;',
  11599. 'end;',
  11600. 'class procedure tintclass.doglob;',
  11601. 'begin',
  11602. 'end;',
  11603. 'constructor tobject.create;',
  11604. 'var',
  11605. ' iC: tintclass;',
  11606. 'begin',
  11607. ' ic:=tintclass.create;',
  11608. ' tintclass.doglob;',
  11609. ' ic.doglob;',
  11610. 'end;',
  11611. 'initialization',
  11612. ' tintclass.doglob;',
  11613. '']);
  11614. ConvertUnit;
  11615. CheckSource('TestClass_Implementation',
  11616. LinesToStr([ // statements
  11617. 'var $impl = $mod.$impl;',
  11618. 'rtl.createClass($mod, "TObject", null, function () {',
  11619. ' this.$init = function () {',
  11620. ' };',
  11621. ' this.$final = function () {',
  11622. ' };',
  11623. ' this.Create = function () {',
  11624. ' var iC = null;',
  11625. ' iC = $impl.TIntClass.$create("Create$1");',
  11626. ' $impl.TIntClass.DoGlob();',
  11627. ' iC.$class.DoGlob();',
  11628. ' return this;',
  11629. ' };',
  11630. '});',
  11631. '']),
  11632. LinesToStr([ // $mod.$main
  11633. '$impl.TIntClass.DoGlob();',
  11634. '']),
  11635. LinesToStr([
  11636. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  11637. ' this.Create$1 = function () {',
  11638. ' $mod.TObject.Create.call(this);',
  11639. ' $mod.TObject.Create.call(this);',
  11640. ' this.$class.DoGlob();',
  11641. ' return this;',
  11642. ' };',
  11643. ' this.DoGlob = function () {',
  11644. ' };',
  11645. '});',
  11646. '']));
  11647. end;
  11648. procedure TTestModule.TestClass_Inheritance;
  11649. begin
  11650. StartProgram(false);
  11651. Add('type');
  11652. Add(' TObject = class');
  11653. Add(' public');
  11654. Add(' constructor Create;');
  11655. Add(' end;');
  11656. Add(' TClassA = class');
  11657. Add(' end;');
  11658. Add(' TClassB = class(TObject)');
  11659. Add(' procedure ProcB;');
  11660. Add(' end;');
  11661. Add('constructor tobject.create; begin end;');
  11662. Add('procedure tclassb.procb; begin end;');
  11663. Add('var');
  11664. Add(' oO: TObject;');
  11665. Add(' oA: TClassA;');
  11666. Add(' oB: TClassB;');
  11667. Add('begin');
  11668. Add(' oO:=tobject.Create;');
  11669. Add(' oA:=tclassa.Create;');
  11670. Add(' ob:=tclassb.Create;');
  11671. Add(' if oo is tclassa then ;');
  11672. Add(' ob:=oo as tclassb;');
  11673. Add(' (oo as tclassb).procb;');
  11674. ConvertProgram;
  11675. CheckSource('TestClass_Inheritance',
  11676. LinesToStr([ // statements
  11677. 'rtl.createClass($mod,"TObject",null,function(){',
  11678. ' this.$init = function () {',
  11679. ' };',
  11680. ' this.$final = function () {',
  11681. ' };',
  11682. ' this.Create = function () {',
  11683. ' return this;',
  11684. ' };',
  11685. '});',
  11686. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  11687. '});',
  11688. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  11689. ' this.ProcB = function () {',
  11690. ' };',
  11691. '});',
  11692. 'this.oO = null;',
  11693. 'this.oA = null;',
  11694. 'this.oB = null;'
  11695. ]),
  11696. LinesToStr([ // $mod.$main
  11697. '$mod.oO = $mod.TObject.$create("Create");',
  11698. '$mod.oA = $mod.TClassA.$create("Create");',
  11699. '$mod.oB = $mod.TClassB.$create("Create");',
  11700. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  11701. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  11702. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  11703. ]));
  11704. end;
  11705. procedure TTestModule.TestClass_TypeAlias;
  11706. begin
  11707. StartProgram(false);
  11708. Add([
  11709. '{$interfaces corba}',
  11710. 'type',
  11711. ' IObject = interface',
  11712. ' end;',
  11713. ' IBird = type IObject;',
  11714. ' TObject = class',
  11715. ' end;',
  11716. ' TBird = type TObject;',
  11717. 'var',
  11718. ' oObj: TObject;',
  11719. ' oBird: TBird;',
  11720. ' IntfObj: IObject;',
  11721. ' IntfBird: IBird;',
  11722. 'begin',
  11723. ' oObj:=oBird;',
  11724. '']);
  11725. ConvertProgram;
  11726. CheckSource('TestClass_TypeAlias',
  11727. LinesToStr([ // statements
  11728. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  11729. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  11730. 'rtl.createClass($mod, "TObject", null, function () {',
  11731. ' this.$init = function () {',
  11732. ' };',
  11733. ' this.$final = function () {',
  11734. ' };',
  11735. '});',
  11736. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11737. '});',
  11738. 'this.oObj = null;',
  11739. 'this.oBird = null;',
  11740. 'this.IntfObj = null;',
  11741. 'this.IntfBird = null;',
  11742. '']),
  11743. LinesToStr([ // $mod.$main
  11744. '$mod.oObj = $mod.oBird;',
  11745. '']));
  11746. end;
  11747. procedure TTestModule.TestClass_AbstractMethod;
  11748. begin
  11749. StartProgram(false);
  11750. Add('type');
  11751. Add(' TObject = class');
  11752. Add(' public');
  11753. Add(' procedure DoIt; virtual; abstract;');
  11754. Add(' end;');
  11755. Add('begin');
  11756. ConvertProgram;
  11757. CheckSource('TestClass_AbstractMethod',
  11758. LinesToStr([ // statements
  11759. 'rtl.createClass($mod,"TObject",null,function(){',
  11760. ' this.$init = function () {',
  11761. ' };',
  11762. ' this.$final = function () {',
  11763. ' };',
  11764. '});'
  11765. ]),
  11766. LinesToStr([ // this.$main
  11767. ''
  11768. ]));
  11769. end;
  11770. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  11771. begin
  11772. StartProgram(false);
  11773. Add([
  11774. 'type',
  11775. ' TObject = class',
  11776. ' procedure DoAbstract; virtual; abstract;',
  11777. ' procedure DoVirtual; virtual;',
  11778. ' procedure DoIt;',
  11779. ' end;',
  11780. ' TA = class',
  11781. ' procedure doabstract; override;',
  11782. ' procedure dovirtual; override;',
  11783. ' procedure DoSome;',
  11784. ' end;',
  11785. 'procedure tobject.dovirtual;',
  11786. 'begin',
  11787. ' inherited; // call non existing ancestor -> ignore silently',
  11788. 'end;',
  11789. 'procedure tobject.doit;',
  11790. 'begin',
  11791. 'end;',
  11792. 'procedure ta.doabstract;',
  11793. 'begin',
  11794. ' inherited dovirtual; // call TObject.DoVirtual',
  11795. 'end;',
  11796. 'procedure ta.dovirtual;',
  11797. 'begin',
  11798. ' inherited; // call TObject.DoVirtual',
  11799. ' inherited dovirtual; // call TObject.DoVirtual',
  11800. ' inherited dovirtual(); // call TObject.DoVirtual',
  11801. ' doit;',
  11802. ' doit();',
  11803. 'end;',
  11804. 'procedure ta.dosome;',
  11805. 'begin',
  11806. ' inherited; // call non existing ancestor method -> silently ignore',
  11807. 'end;',
  11808. 'begin']);
  11809. ConvertProgram;
  11810. CheckSource('TestClass_CallInherited_ProcNoParams',
  11811. LinesToStr([ // statements
  11812. 'rtl.createClass($mod,"TObject",null,function(){',
  11813. ' this.$init = function () {',
  11814. ' };',
  11815. ' this.$final = function () {',
  11816. ' };',
  11817. ' this.DoVirtual = function () {',
  11818. ' };',
  11819. ' this.DoIt = function () {',
  11820. ' };',
  11821. '});',
  11822. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11823. ' this.DoAbstract = function () {',
  11824. ' $mod.TObject.DoVirtual.call(this);',
  11825. ' };',
  11826. ' this.DoVirtual = function () {',
  11827. ' $mod.TObject.DoVirtual.call(this);',
  11828. ' $mod.TObject.DoVirtual.call(this);',
  11829. ' $mod.TObject.DoVirtual.call(this);',
  11830. ' this.DoIt();',
  11831. ' this.DoIt();',
  11832. ' };',
  11833. ' this.DoSome = function () {',
  11834. ' };',
  11835. '});'
  11836. ]),
  11837. LinesToStr([ // this.$main
  11838. ''
  11839. ]));
  11840. end;
  11841. procedure TTestModule.TestClass_CallInherited_WithParams;
  11842. begin
  11843. StartProgram(false);
  11844. Add([
  11845. 'type',
  11846. ' TObject = class',
  11847. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  11848. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  11849. ' procedure DoIt(pA: longint; pB: longint = 0);',
  11850. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  11851. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11852. ' end;',
  11853. ' TClassA = class',
  11854. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  11855. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  11856. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11857. ' end;',
  11858. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  11859. 'begin',
  11860. 'end;',
  11861. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  11862. 'begin',
  11863. 'end;',
  11864. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  11865. 'begin',
  11866. 'end;',
  11867. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  11868. 'begin',
  11869. 'end;',
  11870. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  11871. 'begin',
  11872. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11873. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11874. 'end;',
  11875. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  11876. 'begin',
  11877. ' inherited; // call TObject.DoVirtual(pA,pB)',
  11878. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11879. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11880. ' doit(pa,pb);',
  11881. ' doit(pa);',
  11882. ' doit2(pa);',
  11883. ' doit2;',
  11884. 'end;',
  11885. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  11886. 'begin',
  11887. ' pa:=inherited;',
  11888. 'end;',
  11889. 'begin']);
  11890. ConvertProgram;
  11891. CheckSource('TestClass_CallInherited_WithParams',
  11892. LinesToStr([ // statements
  11893. 'rtl.createClass($mod,"TObject",null,function(){',
  11894. ' this.$init = function () {',
  11895. ' };',
  11896. ' this.$final = function () {',
  11897. ' };',
  11898. ' this.DoVirtual = function (pA,pB) {',
  11899. ' };',
  11900. ' this.DoIt = function (pA,pB) {',
  11901. ' };',
  11902. ' this.DoIt2 = function (pA,pB) {',
  11903. ' };',
  11904. ' this.GetIt = function (pA, pB) {',
  11905. ' var Result = 0;',
  11906. ' return Result;',
  11907. ' };',
  11908. '});',
  11909. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  11910. ' this.DoAbstract = function (pA,pB) {',
  11911. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11912. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11913. ' };',
  11914. ' this.DoVirtual = function (pA,pB) {',
  11915. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  11916. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11917. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11918. ' this.DoIt(pA,pB);',
  11919. ' this.DoIt(pA,0);',
  11920. ' this.DoIt2(pA,2);',
  11921. ' this.DoIt2(1,2);',
  11922. ' };',
  11923. ' this.GetIt$1 = function (pA, pB) {',
  11924. ' var Result = 0;',
  11925. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  11926. ' return Result;',
  11927. ' };',
  11928. '});'
  11929. ]),
  11930. LinesToStr([ // this.$main
  11931. ''
  11932. ]));
  11933. end;
  11934. procedure TTestModule.TestClasS_CallInheritedConstructor;
  11935. begin
  11936. StartProgram(false);
  11937. Add('type');
  11938. Add(' TObject = class');
  11939. Add(' constructor Create; virtual;');
  11940. Add(' constructor CreateWithB(b: boolean);');
  11941. Add(' end;');
  11942. Add(' TA = class');
  11943. Add(' constructor Create; override;');
  11944. Add(' constructor CreateWithC(c: char);');
  11945. Add(' procedure DoIt;');
  11946. Add(' class function DoSome: TObject;');
  11947. Add(' end;');
  11948. Add('constructor tobject.create;');
  11949. Add('begin');
  11950. Add(' inherited; // call non existing ancestor -> ignore silently');
  11951. Add('end;');
  11952. Add('constructor tobject.createwithb(b: boolean);');
  11953. Add('begin');
  11954. Add(' inherited; // call non existing ancestor -> ignore silently');
  11955. Add(' create; // normal call');
  11956. Add('end;');
  11957. Add('constructor ta.create;');
  11958. Add('begin');
  11959. Add(' inherited; // normal call TObject.Create');
  11960. Add(' inherited create; // normal call TObject.Create');
  11961. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  11962. Add('end;');
  11963. Add('constructor ta.createwithc(c: char);');
  11964. Add('begin');
  11965. Add(' inherited create; // call TObject.Create');
  11966. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  11967. Add(' doit;');
  11968. Add(' doit();');
  11969. Add(' dosome;');
  11970. Add('end;');
  11971. Add('procedure ta.doit;');
  11972. Add('begin');
  11973. Add(' create; // normal call');
  11974. Add(' createwithb(false); // normal call');
  11975. Add(' createwithc(''c''); // normal call');
  11976. Add('end;');
  11977. Add('class function ta.dosome: TObject;');
  11978. Add('begin');
  11979. Add(' Result:=create; // constructor');
  11980. Add(' Result:=createwithb(true); // constructor');
  11981. Add(' Result:=createwithc(''c''); // constructor');
  11982. Add('end;');
  11983. Add('begin');
  11984. ConvertProgram;
  11985. CheckSource('TestClass_CallInheritedConstructor',
  11986. LinesToStr([ // statements
  11987. 'rtl.createClass($mod,"TObject",null,function(){',
  11988. ' this.$init = function () {',
  11989. ' };',
  11990. ' this.$final = function () {',
  11991. ' };',
  11992. ' this.Create = function () {',
  11993. ' return this;',
  11994. ' };',
  11995. ' this.CreateWithB = function (b) {',
  11996. ' this.Create();',
  11997. ' return this;',
  11998. ' };',
  11999. '});',
  12000. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  12001. ' this.Create = function () {',
  12002. ' $mod.TObject.Create.call(this);',
  12003. ' $mod.TObject.Create.call(this);',
  12004. ' $mod.TObject.CreateWithB.call(this, false);',
  12005. ' return this;',
  12006. ' };',
  12007. ' this.CreateWithC = function (c) {',
  12008. ' $mod.TObject.Create.call(this);',
  12009. ' $mod.TObject.CreateWithB.call(this, true);',
  12010. ' this.DoIt();',
  12011. ' this.DoIt();',
  12012. ' this.$class.DoSome();',
  12013. ' return this;',
  12014. ' };',
  12015. ' this.DoIt = function () {',
  12016. ' this.Create();',
  12017. ' this.CreateWithB(false);',
  12018. ' this.CreateWithC("c");',
  12019. ' };',
  12020. ' this.DoSome = function () {',
  12021. ' var Result = null;',
  12022. ' Result = this.$create("Create");',
  12023. ' Result = this.$create("CreateWithB", [true]);',
  12024. ' Result = this.$create("CreateWithC", ["c"]);',
  12025. ' return Result;',
  12026. ' };',
  12027. '});'
  12028. ]),
  12029. LinesToStr([ // this.$main
  12030. ''
  12031. ]));
  12032. end;
  12033. procedure TTestModule.TestClass_ClassVar_Assign;
  12034. begin
  12035. StartProgram(false);
  12036. Add([
  12037. 'type',
  12038. ' TObject = class',
  12039. ' public',
  12040. ' class var vI: longint;',
  12041. ' class var Sub: TObject;',
  12042. ' constructor Create;',
  12043. ' class function GetIt(var Par: longint): tobject;',
  12044. ' end;',
  12045. 'constructor tobject.create;',
  12046. 'begin',
  12047. ' vi:=vi+1;',
  12048. ' Self.vi:=Self.vi+1;',
  12049. ' inc(vi);',
  12050. 'end;',
  12051. 'class function tobject.getit(var par: longint): tobject;',
  12052. 'begin',
  12053. ' vi:=vi+3;',
  12054. ' Self.vi:=Self.vi+4;',
  12055. ' inc(vi);',
  12056. ' Result:=self.sub;',
  12057. ' GetIt(vi);',
  12058. 'end;',
  12059. 'var Obj: tobject;',
  12060. 'begin',
  12061. ' obj:=tobject.create;',
  12062. ' tobject.vi:=3;',
  12063. ' if tobject.vi=4 then ;',
  12064. ' tobject.sub:=nil;',
  12065. ' obj.sub:=nil;',
  12066. ' obj.sub.sub:=nil;']);
  12067. ConvertProgram;
  12068. CheckSource('TestClass_ClassVar_Assign',
  12069. LinesToStr([ // statements
  12070. 'rtl.createClass($mod,"TObject",null,function(){',
  12071. ' this.vI = 0;',
  12072. ' this.Sub = null;',
  12073. ' this.$init = function () {',
  12074. ' };',
  12075. ' this.$final = function () {',
  12076. ' };',
  12077. ' this.Create = function(){',
  12078. ' $mod.TObject.vI = this.vI+1;',
  12079. ' $mod.TObject.vI = this.vI+1;',
  12080. ' $mod.TObject.vI += 1;',
  12081. ' return this;',
  12082. ' };',
  12083. ' this.GetIt = function(Par){',
  12084. ' var Result = null;',
  12085. ' $mod.TObject.vI = this.vI + 3;',
  12086. ' $mod.TObject.vI = this.vI + 4;',
  12087. ' $mod.TObject.vI += 1;',
  12088. ' Result = this.Sub;',
  12089. ' this.GetIt({',
  12090. ' p: $mod.TObject,',
  12091. ' get: function () {',
  12092. ' return this.p.vI;',
  12093. ' },',
  12094. ' set: function (v) {',
  12095. ' this.p.vI = v;',
  12096. ' }',
  12097. ' });',
  12098. ' return Result;',
  12099. ' };',
  12100. '});',
  12101. 'this.Obj = null;'
  12102. ]),
  12103. LinesToStr([ // $mod.$main
  12104. '$mod.Obj = $mod.TObject.$create("Create");',
  12105. '$mod.TObject.vI = 3;',
  12106. 'if ($mod.TObject.vI === 4);',
  12107. '$mod.TObject.Sub=null;',
  12108. '$mod.TObject.Sub=null;',
  12109. '$mod.TObject.Sub=null;',
  12110. '']));
  12111. end;
  12112. procedure TTestModule.TestClass_CallClassMethod;
  12113. begin
  12114. StartProgram(false);
  12115. Add('type');
  12116. Add(' TObject = class');
  12117. Add(' public');
  12118. Add(' class var vI: longint;');
  12119. Add(' class var Sub: TObject;');
  12120. Add(' constructor Create;');
  12121. Add(' function GetMore(Par: longint): longint;');
  12122. Add(' class function GetIt(Par: longint): tobject;');
  12123. Add(' end;');
  12124. Add('constructor tobject.create;');
  12125. Add('begin');
  12126. Add(' sub:=getit(3);');
  12127. Add(' vi:=getmore(4);');
  12128. Add(' sub:=Self.getit(5);');
  12129. Add(' vi:=Self.getmore(6);');
  12130. Add('end;');
  12131. Add('function tobject.getmore(par: longint): longint;');
  12132. Add('begin');
  12133. Add(' sub:=getit(11);');
  12134. Add(' vi:=getmore(12);');
  12135. Add(' sub:=self.getit(13);');
  12136. Add(' vi:=self.getmore(14);');
  12137. Add('end;');
  12138. Add('class function tobject.getit(par: longint): tobject;');
  12139. Add('begin');
  12140. Add(' sub:=getit(21);');
  12141. Add(' vi:=sub.getmore(22);');
  12142. Add(' sub:=self.getit(23);');
  12143. Add(' vi:=self.sub.getmore(24);');
  12144. Add('end;');
  12145. Add('var Obj: tobject;');
  12146. Add('begin');
  12147. Add(' obj:=tobject.create;');
  12148. Add(' tobject.getit(5);');
  12149. Add(' obj.getit(6);');
  12150. Add(' obj.sub.getit(7);');
  12151. Add(' obj.sub.getit(8).SUB:=nil;');
  12152. Add(' obj.sub.getit(9).GETIT(10);');
  12153. Add(' obj.sub.getit(11).SuB.getit(12);');
  12154. ConvertProgram;
  12155. CheckSource('TestClass_CallClassMethod',
  12156. LinesToStr([ // statements
  12157. 'rtl.createClass($mod,"TObject",null,function(){',
  12158. ' this.vI = 0;',
  12159. ' this.Sub = null;',
  12160. ' this.$init = function () {',
  12161. ' };',
  12162. ' this.$final = function () {',
  12163. ' };',
  12164. ' this.Create = function(){',
  12165. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12166. ' $mod.TObject.vI = this.GetMore(4);',
  12167. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12168. ' $mod.TObject.vI = this.GetMore(6);',
  12169. ' return this;',
  12170. ' };',
  12171. ' this.GetMore = function(Par){',
  12172. ' var Result = 0;',
  12173. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12174. ' $mod.TObject.vI = this.GetMore(12);',
  12175. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12176. ' $mod.TObject.vI = this.GetMore(14);',
  12177. ' return Result;',
  12178. ' };',
  12179. ' this.GetIt = function(Par){',
  12180. ' var Result = null;',
  12181. ' $mod.TObject.Sub = this.GetIt(21);',
  12182. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12183. ' $mod.TObject.Sub = this.GetIt(23);',
  12184. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12185. ' return Result;',
  12186. ' };',
  12187. '});',
  12188. 'this.Obj = null;'
  12189. ]),
  12190. LinesToStr([ // $mod.$main
  12191. '$mod.Obj = $mod.TObject.$create("Create");',
  12192. '$mod.TObject.GetIt(5);',
  12193. '$mod.Obj.$class.GetIt(6);',
  12194. '$mod.Obj.Sub.$class.GetIt(7);',
  12195. '$mod.TObject.Sub=null;',
  12196. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12197. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12198. '']));
  12199. end;
  12200. procedure TTestModule.TestClass_Property;
  12201. begin
  12202. StartProgram(false);
  12203. Add('type');
  12204. Add(' TObject = class');
  12205. Add(' Fx: longint;');
  12206. Add(' Fy: longint;');
  12207. Add(' function GetInt: longint;');
  12208. Add(' procedure SetInt(Value: longint);');
  12209. Add(' procedure DoIt;');
  12210. Add(' property IntA: longint read Fx write Fy;');
  12211. Add(' property IntB: longint read GetInt write SetInt;');
  12212. Add(' end;');
  12213. Add('function tobject.getint: longint;');
  12214. Add('begin');
  12215. Add(' result:=fx;');
  12216. Add('end;');
  12217. Add('procedure tobject.setint(value: longint);');
  12218. Add('begin');
  12219. Add(' if value=fy then exit;');
  12220. Add(' fy:=value;');
  12221. Add('end;');
  12222. Add('procedure tobject.doit;');
  12223. Add('begin');
  12224. Add(' IntA:=IntA+1;');
  12225. Add(' Self.IntA:=Self.IntA+1;');
  12226. Add(' IntB:=IntB+1;');
  12227. Add(' Self.IntB:=Self.IntB+1;');
  12228. Add('end;');
  12229. Add('var Obj: tobject;');
  12230. Add('begin');
  12231. Add(' obj.inta:=obj.inta+1;');
  12232. Add(' if obj.intb=2 then;');
  12233. Add(' obj.intb:=obj.intb+2;');
  12234. Add(' obj.setint(obj.inta);');
  12235. ConvertProgram;
  12236. CheckSource('TestClass_Property',
  12237. LinesToStr([ // statements
  12238. 'rtl.createClass($mod, "TObject", null, function () {',
  12239. ' this.$init = function () {',
  12240. ' this.Fx = 0;',
  12241. ' this.Fy = 0;',
  12242. ' };',
  12243. ' this.$final = function () {',
  12244. ' };',
  12245. ' this.GetInt = function () {',
  12246. ' var Result = 0;',
  12247. ' Result = this.Fx;',
  12248. ' return Result;',
  12249. ' };',
  12250. ' this.SetInt = function (Value) {',
  12251. ' if (Value === this.Fy) return;',
  12252. ' this.Fy = Value;',
  12253. ' };',
  12254. ' this.DoIt = function () {',
  12255. ' this.Fy = this.Fx + 1;',
  12256. ' this.Fy = this.Fx + 1;',
  12257. ' this.SetInt(this.GetInt() + 1);',
  12258. ' this.SetInt(this.GetInt() + 1);',
  12259. ' };',
  12260. '});',
  12261. 'this.Obj = null;'
  12262. ]),
  12263. LinesToStr([ // $mod.$main
  12264. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12265. 'if ($mod.Obj.GetInt() === 2);',
  12266. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12267. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12268. ]));
  12269. end;
  12270. procedure TTestModule.TestClass_Property_ClassMethod;
  12271. begin
  12272. StartProgram(false);
  12273. Add([
  12274. 'type',
  12275. ' TObject = class',
  12276. ' class var Fx: longint;',
  12277. ' class var Fy: longint;',
  12278. ' class function GetInt: longint;',
  12279. ' class procedure SetInt(Value: longint);',
  12280. ' end;',
  12281. ' TBird = class',
  12282. ' class procedure DoIt;',
  12283. ' class property IntA: longint read Fx write Fy;',
  12284. ' class property IntB: longint read GetInt write SetInt;',
  12285. ' end;',
  12286. 'class function tobject.getint: longint;',
  12287. 'begin',
  12288. ' result:=fx;',
  12289. 'end;',
  12290. 'class procedure tobject.setint(value: longint);',
  12291. 'begin',
  12292. 'end;',
  12293. 'class procedure tbird.doit;',
  12294. 'begin',
  12295. ' FX:=3;',
  12296. ' IntA:=IntA+1;',
  12297. ' Self.IntA:=Self.IntA+1;',
  12298. ' IntB:=IntB+1;',
  12299. ' Self.IntB:=Self.IntB+1;',
  12300. ' with Self do begin',
  12301. ' FX:=11;',
  12302. ' IntA:=IntA+12;',
  12303. ' IntB:=IntB+13;',
  12304. ' end;',
  12305. 'end;',
  12306. 'var Obj: tbird;',
  12307. 'begin',
  12308. ' tbird.fx:=tbird.fx+1;',
  12309. ' tbird.inta:=tbird.inta+1;',
  12310. ' if tbird.intb=2 then;',
  12311. ' tbird.intb:=tbird.intb+2;',
  12312. ' tbird.setint(tbird.inta);',
  12313. ' obj.inta:=obj.inta+1;',
  12314. ' if obj.intb=2 then;',
  12315. ' obj.intb:=obj.intb+2;',
  12316. ' obj.setint(obj.inta);',
  12317. ' with Tbird do begin',
  12318. ' FX:=FY+1;',
  12319. ' inta:=inta+2;',
  12320. ' intb:=intb+3;',
  12321. ' end;',
  12322. ' with Obj do begin',
  12323. ' FX:=FY+1;',
  12324. ' inta:=inta+2;',
  12325. ' intb:=intb+3;',
  12326. ' end;',
  12327. '']);
  12328. ConvertProgram;
  12329. CheckSource('TestClass_Property_ClassMethod',
  12330. LinesToStr([ // statements
  12331. 'rtl.createClass($mod, "TObject", null, function () {',
  12332. ' this.Fx = 0;',
  12333. ' this.Fy = 0;',
  12334. ' this.$init = function () {',
  12335. ' };',
  12336. ' this.$final = function () {',
  12337. ' };',
  12338. ' this.GetInt = function () {',
  12339. ' var Result = 0;',
  12340. ' Result = this.Fx;',
  12341. ' return Result;',
  12342. ' };',
  12343. ' this.SetInt = function (Value) {',
  12344. ' };',
  12345. '});',
  12346. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12347. ' this.DoIt = function () {',
  12348. ' $mod.TObject.Fx = 3;',
  12349. ' $mod.TObject.Fy = this.Fx + 1;',
  12350. ' $mod.TObject.Fy = this.Fx + 1;',
  12351. ' this.SetInt(this.GetInt() + 1);',
  12352. ' this.SetInt(this.GetInt() + 1);',
  12353. ' $mod.TObject.Fx = 11;',
  12354. ' $mod.TObject.Fy = this.Fx + 12;',
  12355. ' this.SetInt(this.GetInt() + 13);',
  12356. ' };',
  12357. '});',
  12358. 'this.Obj = null;'
  12359. ]),
  12360. LinesToStr([ // $mod.$main
  12361. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12362. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12363. 'if ($mod.TBird.GetInt() === 2);',
  12364. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12365. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12366. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12367. 'if ($mod.Obj.$class.GetInt() === 2);',
  12368. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12369. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12370. 'var $with1 = $mod.TBird;',
  12371. '$mod.TObject.Fx = $with1.Fy + 1;',
  12372. '$mod.TObject.Fy = $with1.Fx + 2;',
  12373. '$with1.SetInt($with1.GetInt() + 3);',
  12374. 'var $with2 = $mod.Obj;',
  12375. '$mod.TObject.Fx = $with2.Fy + 1;',
  12376. '$mod.TObject.Fy = $with2.Fx + 2;',
  12377. '$with2.$class.SetInt($with2.$class.GetInt() + 3);',
  12378. '']));
  12379. end;
  12380. procedure TTestModule.TestClass_Property_Indexed;
  12381. begin
  12382. StartProgram(false);
  12383. Add('type');
  12384. Add(' TObject = class');
  12385. Add(' FItems: array of longint;');
  12386. Add(' function GetItems(Index: longint): longint;');
  12387. Add(' procedure SetItems(Index: longint; Value: longint);');
  12388. Add(' procedure DoIt;');
  12389. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  12390. Add(' end;');
  12391. Add('function tobject.getitems(index: longint): longint;');
  12392. Add('begin');
  12393. Add(' Result:=fitems[index];');
  12394. Add('end;');
  12395. Add('procedure tobject.setitems(index: longint; value: longint);');
  12396. Add('begin');
  12397. Add(' fitems[index]:=value;');
  12398. Add('end;');
  12399. Add('procedure tobject.doit;');
  12400. Add('begin');
  12401. Add(' items[1]:=2;');
  12402. Add(' items[3]:=items[4];');
  12403. Add(' self.items[5]:=self.items[6];');
  12404. Add(' items[items[7]]:=items[items[8]];');
  12405. Add('end;');
  12406. Add('var Obj: tobject;');
  12407. Add('begin');
  12408. Add(' obj.Items[11]:=obj.Items[12];');
  12409. ConvertProgram;
  12410. CheckSource('TestClass_Property_Indexed',
  12411. LinesToStr([ // statements
  12412. 'rtl.createClass($mod, "TObject", null, function () {',
  12413. ' this.$init = function () {',
  12414. ' this.FItems = [];',
  12415. ' };',
  12416. ' this.$final = function () {',
  12417. ' this.FItems = undefined;',
  12418. ' };',
  12419. ' this.GetItems = function (Index) {',
  12420. ' var Result = 0;',
  12421. ' Result = this.FItems[Index];',
  12422. ' return Result;',
  12423. ' };',
  12424. ' this.SetItems = function (Index, Value) {',
  12425. ' this.FItems[Index] = Value;',
  12426. ' };',
  12427. ' this.DoIt = function () {',
  12428. ' this.SetItems(1, 2);',
  12429. ' this.SetItems(3,this.GetItems(4));',
  12430. ' this.SetItems(5,this.GetItems(6));',
  12431. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12432. ' };',
  12433. '});',
  12434. 'this.Obj = null;'
  12435. ]),
  12436. LinesToStr([ // $mod.$main
  12437. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12438. ]));
  12439. end;
  12440. procedure TTestModule.TestClass_Property_IndexSpec;
  12441. begin
  12442. StartProgram(false);
  12443. Add([
  12444. 'type',
  12445. ' TEnum = (red, blue);',
  12446. ' TObject = class',
  12447. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12448. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12449. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12450. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12451. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12452. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12453. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12454. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12455. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12456. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12457. ' end;',
  12458. 'procedure DoIt(b: boolean); begin end;',
  12459. 'var',
  12460. ' o: TObject;',
  12461. 'begin',
  12462. ' o.B1:=o.B1;',
  12463. ' o.B2:=o.B2;',
  12464. ' o.B3:=o.B3;',
  12465. ' o.I1[''a'']:=o.I1[''b''];',
  12466. ' doit(o.b1);',
  12467. ' doit(o.b2);',
  12468. ' doit(o.i1[''c'']);',
  12469. '']);
  12470. ConvertProgram;
  12471. CheckSource('TestClass_Property_IndexSpec',
  12472. LinesToStr([ // statements
  12473. 'this.TEnum = {',
  12474. ' "0": "red",',
  12475. ' red: 0,',
  12476. ' "1": "blue",',
  12477. ' blue: 1',
  12478. '};',
  12479. 'rtl.createClass($mod, "TObject", null, function () {',
  12480. ' this.$init = function () {',
  12481. ' };',
  12482. ' this.$final = function () {',
  12483. ' };',
  12484. '});',
  12485. 'this.DoIt = function (b) {',
  12486. '};',
  12487. 'this.o = null;',
  12488. '']),
  12489. LinesToStr([ // $mod.$main
  12490. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12491. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12492. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12493. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12494. '$mod.DoIt($mod.o.GetIntBool(1));',
  12495. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12496. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12497. '']));
  12498. end;
  12499. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12500. begin
  12501. StartProgram(false);
  12502. Add('type');
  12503. Add(' TArray = array of longint;');
  12504. Add(' TObject = class');
  12505. Add(' FItems: TArray;');
  12506. Add(' function GetItems: tarray;');
  12507. Add(' procedure SetItems(Value: tarray);');
  12508. Add(' property Items: tarray read getitems write setitems;');
  12509. Add(' procedure SetNumbers(const Value: tarray);');
  12510. Add(' property Numbers: tarray write setnumbers;');
  12511. Add(' end;');
  12512. Add('function tobject.getitems: tarray;');
  12513. Add('begin');
  12514. Add(' Result:=fitems;');
  12515. Add('end;');
  12516. Add('procedure tobject.setitems(value: tarray);');
  12517. Add('begin');
  12518. Add(' fitems:=value;');
  12519. Add(' fitems:=nil;');
  12520. Add(' Items:=nil;');
  12521. Add(' Items:=Items;');
  12522. Add(' Items[1]:=2;');
  12523. Add(' fitems[3]:=Items[4];');
  12524. Add(' Items[5]:=Items[6];');
  12525. Add(' Self.Items[7]:=8;');
  12526. Add(' Self.Items[9]:=Self.Items[10];');
  12527. Add(' Items[Items[11]]:=Items[Items[12]];');
  12528. Add('end;');
  12529. Add('procedure tobject.SetNumbers(const Value: tarray);');
  12530. Add('begin;');
  12531. Add(' Numbers:=nil;');
  12532. Add(' Numbers:=Value;');
  12533. Add(' Self.Numbers:=Value;');
  12534. Add('end;');
  12535. Add('var Obj: tobject;');
  12536. Add('begin');
  12537. Add(' obj.items:=nil;');
  12538. Add(' obj.items:=obj.items;');
  12539. Add(' obj.items[11]:=obj.items[12];');
  12540. ConvertProgram;
  12541. CheckSource('TestClass_PropertyOfTypeArray',
  12542. LinesToStr([ // statements
  12543. 'rtl.createClass($mod, "TObject", null, function () {',
  12544. ' this.$init = function () {',
  12545. ' this.FItems = [];',
  12546. ' };',
  12547. ' this.$final = function () {',
  12548. ' this.FItems = undefined;',
  12549. ' };',
  12550. ' this.GetItems = function () {',
  12551. ' var Result = [];',
  12552. ' Result = rtl.arrayRef(this.FItems);',
  12553. ' return Result;',
  12554. ' };',
  12555. ' this.SetItems = function (Value) {',
  12556. ' this.FItems = rtl.arrayRef(Value);',
  12557. ' this.FItems = [];',
  12558. ' this.SetItems([]);',
  12559. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  12560. ' this.GetItems()[1] = 2;',
  12561. ' this.FItems[3] = this.GetItems()[4];',
  12562. ' this.GetItems()[5] = this.GetItems()[6];',
  12563. ' this.GetItems()[7] = 8;',
  12564. ' this.GetItems()[9] = this.GetItems()[10];',
  12565. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12566. ' };',
  12567. ' this.SetNumbers = function (Value) {',
  12568. ' this.SetNumbers([]);',
  12569. ' this.SetNumbers(Value);',
  12570. ' this.SetNumbers(Value);',
  12571. ' };',
  12572. '});',
  12573. 'this.Obj = null;'
  12574. ]),
  12575. LinesToStr([ // $mod.$main
  12576. '$mod.Obj.SetItems([]);',
  12577. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12578. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12579. ]));
  12580. end;
  12581. procedure TTestModule.TestClass_PropertyDefault;
  12582. begin
  12583. StartProgram(false);
  12584. Add([
  12585. 'type',
  12586. ' TArray = array of longint;',
  12587. ' TObject = class',
  12588. ' FItems: TArray;',
  12589. ' function GetItems(Index: longint): longint;',
  12590. ' procedure SetItems(Index, Value: longint);',
  12591. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12592. ' end;',
  12593. 'function tobject.getitems(index: longint): longint;',
  12594. 'begin',
  12595. 'end;',
  12596. 'procedure tobject.setitems(index, value: longint);',
  12597. 'begin',
  12598. ' Self[1]:=2;',
  12599. ' Self[3]:=Self[index];',
  12600. ' Self[index]:=Self[Self[value]];',
  12601. ' Self[Self[4]]:=value;',
  12602. 'end;',
  12603. 'var Obj: tobject;',
  12604. 'begin',
  12605. ' obj[11]:=12;',
  12606. ' obj[13]:=obj[14];',
  12607. ' obj[obj[15]]:=obj[obj[15]];',
  12608. ' TObject(obj)[16]:=TObject(obj)[17];']);
  12609. ConvertProgram;
  12610. CheckSource('TestClass_PropertyDefault',
  12611. LinesToStr([ // statements
  12612. 'rtl.createClass($mod, "TObject", null, function () {',
  12613. ' this.$init = function () {',
  12614. ' this.FItems = [];',
  12615. ' };',
  12616. ' this.$final = function () {',
  12617. ' this.FItems = undefined;',
  12618. ' };',
  12619. ' this.GetItems = function (Index) {',
  12620. ' var Result = 0;',
  12621. ' return Result;',
  12622. ' };',
  12623. ' this.SetItems = function (Index, Value) {',
  12624. ' this.SetItems(1, 2);',
  12625. ' this.SetItems(3, this.GetItems(Index));',
  12626. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  12627. ' this.SetItems(this.GetItems(4), Value);',
  12628. ' };',
  12629. '});',
  12630. 'this.Obj = null;'
  12631. ]),
  12632. LinesToStr([ // $mod.$main
  12633. '$mod.Obj.SetItems(11, 12);',
  12634. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  12635. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  12636. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  12637. '']));
  12638. end;
  12639. procedure TTestModule.TestClass_PropertyDefault2;
  12640. begin
  12641. StartProgram(false);
  12642. Add([
  12643. 'type',
  12644. ' TObject = class end;',
  12645. ' TAlphaList = class',
  12646. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  12647. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  12648. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  12649. ' end;',
  12650. ' TBetaList = class',
  12651. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  12652. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  12653. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  12654. ' end;',
  12655. ' TBird = class',
  12656. ' procedure DoIt;',
  12657. ' end;',
  12658. 'procedure TBird.DoIt;',
  12659. 'var',
  12660. ' List: TAlphaList;',
  12661. 'begin',
  12662. ' if TBetaList(List[true])[3]=nil then ;',
  12663. ' TBetaList(List[false])[5]:=nil;',
  12664. 'end;',
  12665. 'var',
  12666. ' List: TAlphaList;',
  12667. 'begin',
  12668. ' if TBetaList(List[true])[3]=nil then ;',
  12669. ' TBetaList(List[false])[5]:=nil;',
  12670. '']);
  12671. ConvertProgram;
  12672. CheckSource('TestClass_PropertyDefault2',
  12673. LinesToStr([ // statements
  12674. 'rtl.createClass($mod, "TObject", null, function () {',
  12675. ' this.$init = function () {',
  12676. ' };',
  12677. ' this.$final = function () {',
  12678. ' };',
  12679. '});',
  12680. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  12681. '});',
  12682. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  12683. '});',
  12684. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12685. ' this.DoIt = function () {',
  12686. ' var List = null;',
  12687. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  12688. ' List.GetAlphas(false).SetBetas(5, null);',
  12689. ' };',
  12690. '});',
  12691. 'this.List = null;',
  12692. '']),
  12693. LinesToStr([ // $mod.$main
  12694. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  12695. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  12696. '']));
  12697. end;
  12698. procedure TTestModule.TestClass_PropertyOverride;
  12699. begin
  12700. StartProgram(false);
  12701. Add('type');
  12702. Add(' integer = longint;');
  12703. Add(' TObject = class');
  12704. Add(' FItem: integer;');
  12705. Add(' function GetItem: integer; external name ''GetItem'';');
  12706. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  12707. Add(' property Item: integer read getitem write setitem;');
  12708. Add(' end;');
  12709. Add(' TCar = class');
  12710. Add(' FBag: integer;');
  12711. Add(' function GetBag: integer; external name ''GetBag'';');
  12712. Add(' property Item read getbag;');
  12713. Add(' end;');
  12714. Add('var');
  12715. Add(' Obj: tobject;');
  12716. Add(' Car: tcar;');
  12717. Add('begin');
  12718. Add(' Obj.Item:=Obj.Item;');
  12719. Add(' Car.Item:=Car.Item;');
  12720. ConvertProgram;
  12721. CheckSource('TestClass_PropertyOverride',
  12722. LinesToStr([ // statements
  12723. 'rtl.createClass($mod, "TObject", null, function () {',
  12724. ' this.$init = function () {',
  12725. ' this.FItem = 0;',
  12726. ' };',
  12727. ' this.$final = function () {',
  12728. ' };',
  12729. '});',
  12730. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12731. ' this.$init = function () {',
  12732. ' $mod.TObject.$init.call(this);',
  12733. ' this.FBag = 0;',
  12734. ' };',
  12735. '});',
  12736. 'this.Obj = null;',
  12737. 'this.Car = null;',
  12738. '']),
  12739. LinesToStr([ // $mod.$main
  12740. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  12741. '$mod.Car.SetItem($mod.Car.GetBag());',
  12742. '']));
  12743. end;
  12744. procedure TTestModule.TestClass_PropertyIncVisibility;
  12745. begin
  12746. AddModuleWithIntfImplSrc('unit1.pp',
  12747. LinesToStr([
  12748. 'type',
  12749. ' TNumber = longint;',
  12750. ' TInteger = longint;',
  12751. ' TObject = class',
  12752. ' private',
  12753. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  12754. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  12755. ' protected',
  12756. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  12757. ' end;']),
  12758. LinesToStr([
  12759. '']));
  12760. StartProgram(true);
  12761. Add([
  12762. 'uses unit1;',
  12763. 'type',
  12764. ' TBird = class',
  12765. ' public',
  12766. ' property Items;',
  12767. ' end;',
  12768. 'procedure DoIt(i: TInteger);',
  12769. 'begin',
  12770. 'end;',
  12771. 'var b: TBird;',
  12772. 'begin',
  12773. ' b.Items[1]:=2;',
  12774. ' b.Items[3]:=b.Items[4];',
  12775. ' DoIt(b.Items[5]);',
  12776. '']);
  12777. ConvertProgram;
  12778. CheckSource('TestClass_PropertyIncVisibility',
  12779. LinesToStr([ // statements
  12780. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  12781. '});',
  12782. 'this.DoIt = function (i) {',
  12783. '};',
  12784. 'this.b = null;'
  12785. ]),
  12786. LinesToStr([ // $mod.$main
  12787. '$mod.b.SetItems(1, 2);',
  12788. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  12789. '$mod.DoIt($mod.b.GetItems(5));'
  12790. ]));
  12791. end;
  12792. procedure TTestModule.TestClass_Assigned;
  12793. begin
  12794. StartProgram(false);
  12795. Add('type');
  12796. Add(' TObject = class');
  12797. Add(' end;');
  12798. Add('var');
  12799. Add(' Obj: tobject;');
  12800. Add(' b: boolean;');
  12801. Add('begin');
  12802. Add(' if Assigned(obj) then ;');
  12803. Add(' b:=Assigned(obj) or false;');
  12804. ConvertProgram;
  12805. CheckSource('TestClass_Assigned',
  12806. LinesToStr([ // statements
  12807. 'rtl.createClass($mod, "TObject", null, function () {',
  12808. ' this.$init = function () {',
  12809. ' };',
  12810. ' this.$final = function () {',
  12811. ' };',
  12812. '});',
  12813. 'this.Obj = null;',
  12814. 'this.b = false;'
  12815. ]),
  12816. LinesToStr([ // $mod.$main
  12817. 'if ($mod.Obj != null);',
  12818. '$mod.b = ($mod.Obj != null) || false;'
  12819. ]));
  12820. end;
  12821. procedure TTestModule.TestClass_WithClassDoCreate;
  12822. begin
  12823. StartProgram(false);
  12824. Add('type');
  12825. Add(' TObject = class');
  12826. Add(' aBool: boolean;');
  12827. Add(' Arr: array of boolean;');
  12828. Add(' constructor Create;');
  12829. Add(' end;');
  12830. Add('constructor TObject.Create; begin end;');
  12831. Add('var');
  12832. Add(' Obj: tobject;');
  12833. Add(' b: boolean;');
  12834. Add('begin');
  12835. Add(' with tobject.create do begin');
  12836. Add(' b:=abool;');
  12837. Add(' abool:=b;');
  12838. Add(' b:=arr[1];');
  12839. Add(' arr[2]:=b;');
  12840. Add(' end;');
  12841. Add(' with tobject do');
  12842. Add(' obj:=create;');
  12843. Add(' with obj do begin');
  12844. Add(' create;');
  12845. Add(' b:=abool;');
  12846. Add(' abool:=b;');
  12847. Add(' b:=arr[3];');
  12848. Add(' arr[4]:=b;');
  12849. Add(' end;');
  12850. ConvertProgram;
  12851. CheckSource('TestClass_WithClassDoCreate',
  12852. LinesToStr([ // statements
  12853. 'rtl.createClass($mod, "TObject", null, function () {',
  12854. ' this.$init = function () {',
  12855. ' this.aBool = false;',
  12856. ' this.Arr = [];',
  12857. ' };',
  12858. ' this.$final = function () {',
  12859. ' this.Arr = undefined;',
  12860. ' };',
  12861. ' this.Create = function () {',
  12862. ' return this;',
  12863. ' };',
  12864. '});',
  12865. 'this.Obj = null;',
  12866. 'this.b = false;'
  12867. ]),
  12868. LinesToStr([ // $mod.$main
  12869. 'var $with1 = $mod.TObject.$create("Create");',
  12870. '$mod.b = $with1.aBool;',
  12871. '$with1.aBool = $mod.b;',
  12872. '$mod.b = $with1.Arr[1];',
  12873. '$with1.Arr[2] = $mod.b;',
  12874. 'var $with2 = $mod.TObject;',
  12875. '$mod.Obj = $with2.$create("Create");',
  12876. 'var $with3 = $mod.Obj;',
  12877. '$with3.Create();',
  12878. '$mod.b = $with3.aBool;',
  12879. '$with3.aBool = $mod.b;',
  12880. '$mod.b = $with3.Arr[3];',
  12881. '$with3.Arr[4] = $mod.b;',
  12882. '']));
  12883. end;
  12884. procedure TTestModule.TestClass_WithClassInstDoProperty;
  12885. begin
  12886. StartProgram(false);
  12887. Add('type');
  12888. Add(' TObject = class');
  12889. Add(' FInt: longint;');
  12890. Add(' constructor Create;');
  12891. Add(' function GetSize: longint;');
  12892. Add(' procedure SetSize(Value: longint);');
  12893. Add(' property Int: longint read FInt write FInt;');
  12894. Add(' property Size: longint read GetSize write SetSize;');
  12895. Add(' end;');
  12896. Add('constructor TObject.Create; begin end;');
  12897. Add('function TObject.GetSize: longint; begin; end;');
  12898. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12899. Add('var');
  12900. Add(' Obj: tobject;');
  12901. Add(' i: longint;');
  12902. Add('begin');
  12903. Add(' with TObject.Create do begin');
  12904. Add(' i:=int;');
  12905. Add(' int:=i;');
  12906. Add(' i:=size;');
  12907. Add(' size:=i;');
  12908. Add(' end;');
  12909. Add(' with obj do begin');
  12910. Add(' i:=int;');
  12911. Add(' int:=i;');
  12912. Add(' i:=size;');
  12913. Add(' size:=i;');
  12914. Add(' end;');
  12915. ConvertProgram;
  12916. CheckSource('TestClass_WithClassInstDoProperty',
  12917. LinesToStr([ // statements
  12918. 'rtl.createClass($mod, "TObject", null, function () {',
  12919. ' this.$init = function () {',
  12920. ' this.FInt = 0;',
  12921. ' };',
  12922. ' this.$final = function () {',
  12923. ' };',
  12924. ' this.Create = function () {',
  12925. ' return this;',
  12926. ' };',
  12927. ' this.GetSize = function () {',
  12928. ' var Result = 0;',
  12929. ' return Result;',
  12930. ' };',
  12931. ' this.SetSize = function (Value) {',
  12932. ' };',
  12933. '});',
  12934. 'this.Obj = null;',
  12935. 'this.i = 0;'
  12936. ]),
  12937. LinesToStr([ // $mod.$main
  12938. 'var $with1 = $mod.TObject.$create("Create");',
  12939. '$mod.i = $with1.FInt;',
  12940. '$with1.FInt = $mod.i;',
  12941. '$mod.i = $with1.GetSize();',
  12942. '$with1.SetSize($mod.i);',
  12943. 'var $with2 = $mod.Obj;',
  12944. '$mod.i = $with2.FInt;',
  12945. '$with2.FInt = $mod.i;',
  12946. '$mod.i = $with2.GetSize();',
  12947. '$with2.SetSize($mod.i);',
  12948. '']));
  12949. end;
  12950. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  12951. begin
  12952. StartProgram(false);
  12953. Add('type');
  12954. Add(' TObject = class');
  12955. Add(' constructor Create;');
  12956. Add(' function GetItems(Index: longint): longint;');
  12957. Add(' procedure SetItems(Index, Value: longint);');
  12958. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  12959. Add(' end;');
  12960. Add('constructor TObject.Create; begin end;');
  12961. Add('function tobject.getitems(index: longint): longint; begin; end;');
  12962. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  12963. Add('var');
  12964. Add(' Obj: tobject;');
  12965. Add(' i: longint;');
  12966. Add('begin');
  12967. Add(' with TObject.Create do begin');
  12968. Add(' i:=Items[1];');
  12969. Add(' Items[2]:=i;');
  12970. Add(' end;');
  12971. Add(' with obj do begin');
  12972. Add(' i:=Items[3];');
  12973. Add(' Items[4]:=i;');
  12974. Add(' end;');
  12975. ConvertProgram;
  12976. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  12977. LinesToStr([ // statements
  12978. 'rtl.createClass($mod, "TObject", null, function () {',
  12979. ' this.$init = function () {',
  12980. ' };',
  12981. ' this.$final = function () {',
  12982. ' };',
  12983. ' this.Create = function () {',
  12984. ' return this;',
  12985. ' };',
  12986. ' this.GetItems = function (Index) {',
  12987. ' var Result = 0;',
  12988. ' return Result;',
  12989. ' };',
  12990. ' this.SetItems = function (Index, Value) {',
  12991. ' };',
  12992. '});',
  12993. 'this.Obj = null;',
  12994. 'this.i = 0;'
  12995. ]),
  12996. LinesToStr([ // $mod.$main
  12997. 'var $with1 = $mod.TObject.$create("Create");',
  12998. '$mod.i = $with1.GetItems(1);',
  12999. '$with1.SetItems(2, $mod.i);',
  13000. 'var $with2 = $mod.Obj;',
  13001. '$mod.i = $with2.GetItems(3);',
  13002. '$with2.SetItems(4, $mod.i);',
  13003. '']));
  13004. end;
  13005. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13006. begin
  13007. StartProgram(false);
  13008. Add('type');
  13009. Add(' TObject = class');
  13010. Add(' constructor Create;');
  13011. Add(' function GetSize: longint;');
  13012. Add(' procedure SetSize(Value: longint);');
  13013. Add(' end;');
  13014. Add('constructor TObject.Create; begin end;');
  13015. Add('function TObject.GetSize: longint; begin; end;');
  13016. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13017. Add('var');
  13018. Add(' Obj: tobject;');
  13019. Add(' i: longint;');
  13020. Add('begin');
  13021. Add(' with TObject.Create do begin');
  13022. Add(' i:=GetSize;');
  13023. Add(' i:=GetSize();');
  13024. Add(' SetSize(i);');
  13025. Add(' end;');
  13026. Add(' with obj do begin');
  13027. Add(' i:=GetSize;');
  13028. Add(' i:=GetSize();');
  13029. Add(' SetSize(i);');
  13030. Add(' end;');
  13031. ConvertProgram;
  13032. CheckSource('TestClass_WithClassInstDoFunc',
  13033. LinesToStr([ // statements
  13034. 'rtl.createClass($mod, "TObject", null, function () {',
  13035. ' this.$init = function () {',
  13036. ' };',
  13037. ' this.$final = function () {',
  13038. ' };',
  13039. ' this.Create = function () {',
  13040. ' return this;',
  13041. ' };',
  13042. ' this.GetSize = function () {',
  13043. ' var Result = 0;',
  13044. ' return Result;',
  13045. ' };',
  13046. ' this.SetSize = function (Value) {',
  13047. ' };',
  13048. '});',
  13049. 'this.Obj = null;',
  13050. 'this.i = 0;'
  13051. ]),
  13052. LinesToStr([ // $mod.$main
  13053. 'var $with1 = $mod.TObject.$create("Create");',
  13054. '$mod.i = $with1.GetSize();',
  13055. '$mod.i = $with1.GetSize();',
  13056. '$with1.SetSize($mod.i);',
  13057. 'var $with2 = $mod.Obj;',
  13058. '$mod.i = $with2.GetSize();',
  13059. '$mod.i = $with2.GetSize();',
  13060. '$with2.SetSize($mod.i);',
  13061. '']));
  13062. end;
  13063. procedure TTestModule.TestClass_ProcVarDelphi;
  13064. begin
  13065. StartProgram(false);
  13066. Add([
  13067. '{$mode delphi}',
  13068. 'type',
  13069. ' TProc = procedure of object;',
  13070. ' TObject = class',
  13071. ' procedure Run;',
  13072. ' procedure Fly(const p: TProc);',
  13073. ' end;',
  13074. 'procedure TObject.Run;',
  13075. 'var o: TObject;',
  13076. 'begin',
  13077. ' Fly(Run);',
  13078. ' Fly(Self.Run);',
  13079. ' with Self do Fly(Run);',
  13080. ' with o do Fly(Run);',
  13081. 'end;',
  13082. 'procedure TObject.Fly(const p: TProc);',
  13083. 'begin',
  13084. 'end;',
  13085. 'begin',
  13086. '']);
  13087. ConvertProgram;
  13088. CheckSource('TestClass_ProcVarDelphi',
  13089. LinesToStr([ // statements
  13090. 'rtl.createClass($mod, "TObject", null, function () {',
  13091. ' this.$init = function () {',
  13092. ' };',
  13093. ' this.$final = function () {',
  13094. ' };',
  13095. ' this.Run = function () {',
  13096. ' var o = null;',
  13097. ' this.Fly(rtl.createCallback(this, "Run"));',
  13098. ' this.Fly(rtl.createCallback(this, "Run"));',
  13099. ' this.Fly(rtl.createCallback(this, "Run"));',
  13100. ' o.Fly(rtl.createCallback(o, "Run"));',
  13101. ' };',
  13102. ' this.Fly = function (p) {',
  13103. ' };',
  13104. '});',
  13105. '']),
  13106. LinesToStr([ // $mod.$main
  13107. '']));
  13108. end;
  13109. procedure TTestModule.TestClass_TypeCast;
  13110. begin
  13111. StartProgram(false);
  13112. Add('type');
  13113. Add(' TObject = class');
  13114. Add(' Next: TObject;');
  13115. Add(' constructor Create;');
  13116. Add(' end;');
  13117. Add(' TControl = class(TObject)');
  13118. Add(' Arr: array of TObject;');
  13119. Add(' function GetIt(vI: longint = 0): TObject;');
  13120. Add(' end;');
  13121. Add('constructor tobject.create; begin end;');
  13122. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13123. Add('var');
  13124. Add(' Obj: tobject;');
  13125. Add('begin');
  13126. Add(' obj:=tcontrol(obj).next;');
  13127. Add(' tcontrol(obj):=nil;');
  13128. Add(' obj:=tcontrol(obj);');
  13129. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13130. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13131. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13132. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13133. Add(' obj:=tcontrol(nil);');
  13134. ConvertProgram;
  13135. CheckSource('TestClass_TypeCast',
  13136. LinesToStr([ // statements
  13137. 'rtl.createClass($mod, "TObject", null, function () {',
  13138. ' this.$init = function () {',
  13139. ' this.Next = null;',
  13140. ' };',
  13141. ' this.$final = function () {',
  13142. ' this.Next = undefined;',
  13143. ' };',
  13144. ' this.Create = function () {',
  13145. ' return this;',
  13146. ' };',
  13147. '});',
  13148. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  13149. ' this.$init = function () {',
  13150. ' $mod.TObject.$init.call(this);',
  13151. ' this.Arr = [];',
  13152. ' };',
  13153. ' this.$final = function () {',
  13154. ' this.Arr = undefined;',
  13155. ' $mod.TObject.$final.call(this);',
  13156. ' };',
  13157. ' this.GetIt = function (vI) {',
  13158. ' var Result = null;',
  13159. ' return Result;',
  13160. ' };',
  13161. '});',
  13162. 'this.Obj = null;'
  13163. ]),
  13164. LinesToStr([ // $mod.$main
  13165. '$mod.Obj = $mod.Obj.Next;',
  13166. '$mod.Obj = null;',
  13167. '$mod.Obj = $mod.Obj;',
  13168. '$mod.Obj = $mod.Obj.GetIt(0);',
  13169. '$mod.Obj = $mod.Obj.GetIt(0);',
  13170. '$mod.Obj = $mod.Obj.GetIt(1);',
  13171. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13172. '$mod.Obj = null;',
  13173. '']));
  13174. end;
  13175. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13176. begin
  13177. StartProgram(false);
  13178. Add('type');
  13179. Add(' TObject = class end;');
  13180. Add('procedure ProcA(var A);');
  13181. Add('begin');
  13182. Add(' TObject(A):=nil;');
  13183. Add(' TObject(A):=TObject(A);');
  13184. Add(' if TObject(A)=nil then ;');
  13185. Add(' if nil=TObject(A) then ;');
  13186. Add('end;');
  13187. Add('procedure ProcB(out A);');
  13188. Add('begin');
  13189. Add(' TObject(A):=nil;');
  13190. Add(' TObject(A):=TObject(A);');
  13191. Add(' if TObject(A)=nil then ;');
  13192. Add(' if nil=TObject(A) then ;');
  13193. Add('end;');
  13194. Add('procedure ProcC(const A);');
  13195. Add('begin');
  13196. Add(' if TObject(A)=nil then ;');
  13197. Add(' if nil=TObject(A) then ;');
  13198. Add('end;');
  13199. Add('var o: TObject;');
  13200. Add('begin');
  13201. Add(' ProcA(o);');
  13202. Add(' ProcB(o);');
  13203. Add(' ProcC(o);');
  13204. ConvertProgram;
  13205. CheckSource('TestClass_TypeCastUntypedParam',
  13206. LinesToStr([ // statements
  13207. 'rtl.createClass($mod, "TObject", null, function () {',
  13208. ' this.$init = function () {',
  13209. ' };',
  13210. ' this.$final = function () {',
  13211. ' };',
  13212. '});',
  13213. 'this.ProcA = function (A) {',
  13214. ' A.set(null);',
  13215. ' A.set(A.get());',
  13216. ' if (A.get() === null);',
  13217. ' if (null === A.get());',
  13218. '};',
  13219. 'this.ProcB = function (A) {',
  13220. ' A.set(null);',
  13221. ' A.set(A.get());',
  13222. ' if (A.get() === null);',
  13223. ' if (null === A.get());',
  13224. '};',
  13225. 'this.ProcC = function (A) {',
  13226. ' if (A === null);',
  13227. ' if (null === A);',
  13228. '};',
  13229. 'this.o = null;',
  13230. '']),
  13231. LinesToStr([ // $mod.$main
  13232. '$mod.ProcA({',
  13233. ' p: $mod,',
  13234. ' get: function () {',
  13235. ' return this.p.o;',
  13236. ' },',
  13237. ' set: function (v) {',
  13238. ' this.p.o = v;',
  13239. ' }',
  13240. '});',
  13241. '$mod.ProcB({',
  13242. ' p: $mod,',
  13243. ' get: function () {',
  13244. ' return this.p.o;',
  13245. ' },',
  13246. ' set: function (v) {',
  13247. ' this.p.o = v;',
  13248. ' }',
  13249. '});',
  13250. '$mod.ProcC($mod.o);',
  13251. '']));
  13252. end;
  13253. procedure TTestModule.TestClass_Overloads;
  13254. begin
  13255. StartProgram(false);
  13256. Add('type');
  13257. Add(' TObject = class');
  13258. Add(' procedure DoIt;');
  13259. Add(' procedure DoIt(vI: longint);');
  13260. Add(' end;');
  13261. Add('procedure TObject.DoIt;');
  13262. Add('begin');
  13263. Add(' DoIt;');
  13264. Add(' DoIt(1);');
  13265. Add('end;');
  13266. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13267. Add('begin');
  13268. ConvertProgram;
  13269. CheckSource('TestClass_Overloads',
  13270. LinesToStr([ // statements
  13271. 'rtl.createClass($mod, "TObject", null, function () {',
  13272. ' this.$init = function () {',
  13273. ' };',
  13274. ' this.$final = function () {',
  13275. ' };',
  13276. ' this.DoIt = function () {',
  13277. ' this.DoIt();',
  13278. ' this.DoIt$1(1);',
  13279. ' };',
  13280. ' this.DoIt$1 = function (vI) {',
  13281. ' };',
  13282. '});',
  13283. '']),
  13284. LinesToStr([ // $mod.$main
  13285. '']));
  13286. end;
  13287. procedure TTestModule.TestClass_OverloadsAncestor;
  13288. begin
  13289. StartProgram(false);
  13290. Add('type');
  13291. Add(' TObject = class;');
  13292. Add(' TObject = class');
  13293. Add(' procedure DoIt(vA: longint);');
  13294. Add(' procedure DoIt(vA, vB: longint);');
  13295. Add(' end;');
  13296. Add(' TCar = class;');
  13297. Add(' TCar = class');
  13298. Add(' procedure DoIt(vA: longint);');
  13299. Add(' procedure DoIt(vA, vB: longint);');
  13300. Add(' end;');
  13301. Add('procedure tobject.doit(va: longint);');
  13302. Add('begin');
  13303. Add(' doit(1);');
  13304. Add(' doit(1,2);');
  13305. Add('end;');
  13306. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13307. Add('procedure tcar.doit(va: longint);');
  13308. Add('begin');
  13309. Add(' doit(1);');
  13310. Add(' doit(1,2);');
  13311. Add(' inherited doit(1);');
  13312. Add(' inherited doit(1,2);');
  13313. Add('end;');
  13314. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13315. Add('begin');
  13316. ConvertProgram;
  13317. CheckSource('TestClass_OverloadsAncestor',
  13318. LinesToStr([ // statements
  13319. 'rtl.createClass($mod, "TObject", null, function () {',
  13320. ' this.$init = function () {',
  13321. ' };',
  13322. ' this.$final = function () {',
  13323. ' };',
  13324. ' this.DoIt = function (vA) {',
  13325. ' this.DoIt(1);',
  13326. ' this.DoIt$1(1,2);',
  13327. ' };',
  13328. ' this.DoIt$1 = function (vA, vB) {',
  13329. ' };',
  13330. '});',
  13331. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13332. ' this.DoIt$2 = function (vA) {',
  13333. ' this.DoIt$2(1);',
  13334. ' this.DoIt$3(1, 2);',
  13335. ' $mod.TObject.DoIt.call(this, 1);',
  13336. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13337. ' };',
  13338. ' this.DoIt$3 = function (vA, vB) {',
  13339. ' };',
  13340. '});',
  13341. '']),
  13342. LinesToStr([ // $mod.$main
  13343. '']));
  13344. end;
  13345. procedure TTestModule.TestClass_OverloadConstructor;
  13346. begin
  13347. StartProgram(false);
  13348. Add('type');
  13349. Add(' TObject = class');
  13350. Add(' constructor Create(vA: longint);');
  13351. Add(' constructor Create(vA, vB: longint);');
  13352. Add(' end;');
  13353. Add(' TCar = class');
  13354. Add(' constructor Create(vA: longint);');
  13355. Add(' constructor Create(vA, vB: longint);');
  13356. Add(' end;');
  13357. Add('constructor tobject.create(va: longint);');
  13358. Add('begin');
  13359. Add(' create(1);');
  13360. Add(' create(1,2);');
  13361. Add('end;');
  13362. Add('constructor tobject.create(va, vb: longint); begin end;');
  13363. Add('constructor tcar.create(va: longint);');
  13364. Add('begin');
  13365. Add(' create(1);');
  13366. Add(' create(1,2);');
  13367. Add(' inherited create(1);');
  13368. Add(' inherited create(1,2);');
  13369. Add('end;');
  13370. Add('constructor tcar.create(va, vb: longint); begin end;');
  13371. Add('begin');
  13372. Add(' tobject.create(1);');
  13373. Add(' tobject.create(1,2);');
  13374. Add(' tcar.create(1);');
  13375. Add(' tcar.create(1,2);');
  13376. ConvertProgram;
  13377. CheckSource('TestClass_OverloadConstructor',
  13378. LinesToStr([ // statements
  13379. 'rtl.createClass($mod, "TObject", null, function () {',
  13380. ' this.$init = function () {',
  13381. ' };',
  13382. ' this.$final = function () {',
  13383. ' };',
  13384. ' this.Create = function (vA) {',
  13385. ' this.Create(1);',
  13386. ' this.Create$1(1,2);',
  13387. ' return this;',
  13388. ' };',
  13389. ' this.Create$1 = function (vA, vB) {',
  13390. ' return this;',
  13391. ' };',
  13392. '});',
  13393. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13394. ' this.Create$2 = function (vA) {',
  13395. ' this.Create$2(1);',
  13396. ' this.Create$3(1, 2);',
  13397. ' $mod.TObject.Create.call(this, 1);',
  13398. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13399. ' return this;',
  13400. ' };',
  13401. ' this.Create$3 = function (vA, vB) {',
  13402. ' return this;',
  13403. ' };',
  13404. '});',
  13405. '']),
  13406. LinesToStr([ // $mod.$main
  13407. '$mod.TObject.$create("Create", [1]);',
  13408. '$mod.TObject.$create("Create$1", [1, 2]);',
  13409. '$mod.TCar.$create("Create$2", [1]);',
  13410. '$mod.TCar.$create("Create$3", [1, 2]);',
  13411. '']));
  13412. end;
  13413. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13414. begin
  13415. StartProgram(false);
  13416. Add([
  13417. '{$mode delphi}',
  13418. 'type',
  13419. ' TObject = class end;',
  13420. ' TBird = class',
  13421. ' function {#a}GetValue: longint; overload; virtual;',
  13422. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  13423. ' end;',
  13424. ' TEagle = class(TBird)',
  13425. ' function {#c}GetValue: longint; overload; override;',
  13426. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  13427. ' end;',
  13428. 'function TBird.GetValue: longint;',
  13429. 'begin',
  13430. ' if 3={@a}GetValue then ;',
  13431. ' if 4={@b}GetValue(5) then ;',
  13432. 'end;',
  13433. 'function TBird.GetValue(AValue: longint): longint;',
  13434. 'begin',
  13435. 'end;',
  13436. 'function TEagle.GetValue: longint;',
  13437. 'begin',
  13438. ' if 13={@c}GetValue then ;',
  13439. ' if 14={@d}GetValue(15) then ;',
  13440. ' if 15=inherited {@a}GetValue then ;',
  13441. ' if 16=inherited {@b}GetValue(17) then ;',
  13442. 'end;',
  13443. 'function TEagle.GetValue(AValue: longint): longint;',
  13444. 'begin',
  13445. 'end;',
  13446. 'var',
  13447. ' e: TEagle;',
  13448. 'begin',
  13449. ' if 23=e.{@c}GetValue then ;',
  13450. ' if 24=e.{@d}GetValue(25) then ;']);
  13451. ConvertProgram;
  13452. CheckSource('TestClass_OverloadDelphiOverride',
  13453. LinesToStr([ // statements
  13454. 'rtl.createClass($mod, "TObject", null, function () {',
  13455. ' this.$init = function () {',
  13456. ' };',
  13457. ' this.$final = function () {',
  13458. ' };',
  13459. '});',
  13460. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13461. ' this.GetValue = function () {',
  13462. ' var Result = 0;',
  13463. ' if (3 === this.GetValue()) ;',
  13464. ' if (4 === this.GetValue$1(5)) ;',
  13465. ' return Result;',
  13466. ' };',
  13467. ' this.GetValue$1 = function (AValue) {',
  13468. ' var Result = 0;',
  13469. ' return Result;',
  13470. ' };',
  13471. '});',
  13472. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13473. ' this.GetValue = function () {',
  13474. ' var Result = 0;',
  13475. ' if (13 === this.GetValue()) ;',
  13476. ' if (14 === this.GetValue$1(15)) ;',
  13477. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13478. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13479. ' return Result;',
  13480. ' };',
  13481. ' this.GetValue$1 = function (AValue) {',
  13482. ' var Result = 0;',
  13483. ' return Result;',
  13484. ' };',
  13485. '});',
  13486. 'this.e = null;',
  13487. '']),
  13488. LinesToStr([ // $mod.$main
  13489. 'if (23 === $mod.e.GetValue()) ;',
  13490. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13491. '']));
  13492. end;
  13493. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  13494. begin
  13495. StartProgram(false);
  13496. Add([
  13497. '{$mode delphi}',
  13498. 'type',
  13499. ' TObject = class end;',
  13500. ' TAnimal = class',
  13501. ' public',
  13502. ' {#animal_a}A: longint;',
  13503. ' function {#animal_b}B: longint;',
  13504. ' end;',
  13505. ' TBird = class(TAnimal)',
  13506. ' public',
  13507. ' {#bird_a}A: double;',
  13508. ' {#bird_b}B: boolean;',
  13509. ' end;',
  13510. ' TEagle = class(TBird)',
  13511. ' public',
  13512. ' function {#eagle_a}A: boolean;',
  13513. ' {#eagle_b}B: double;',
  13514. ' end;',
  13515. 'function TAnimal.B: longint;',
  13516. 'begin',
  13517. 'end;',
  13518. 'function TEagle.A: boolean;',
  13519. 'begin',
  13520. ' {@eagle_b}B:=3.3;',
  13521. ' {@eagle_a}A();',
  13522. ' TBird(Self).{@bird_b}B:=true;',
  13523. ' TAnimal(Self).{@animal_a}A:=17;',
  13524. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  13525. 'end;',
  13526. 'var',
  13527. ' e: TEagle;',
  13528. 'begin',
  13529. ' e.{@eagle_b}B:=5.3;',
  13530. ' if e.{@eagle_a}A then ;',
  13531. '']);
  13532. ConvertProgram;
  13533. CheckSource('TestClass_ReintroduceVarDelphi',
  13534. LinesToStr([ // statements
  13535. 'rtl.createClass($mod, "TObject", null, function () {',
  13536. ' this.$init = function () {',
  13537. ' };',
  13538. ' this.$final = function () {',
  13539. ' };',
  13540. '});',
  13541. 'rtl.createClass($mod, "TAnimal", $mod.TObject, function () {',
  13542. ' this.$init = function () {',
  13543. ' $mod.TObject.$init.call(this);',
  13544. ' this.A = 0;',
  13545. ' };',
  13546. ' this.B = function () {',
  13547. ' var Result = 0;',
  13548. ' return Result;',
  13549. ' };',
  13550. '});',
  13551. 'rtl.createClass($mod, "TBird", $mod.TAnimal, function () {',
  13552. ' this.$init = function () {',
  13553. ' $mod.TAnimal.$init.call(this);',
  13554. ' this.A$1 = 0.0;',
  13555. ' this.B$1 = false;',
  13556. ' };',
  13557. '});',
  13558. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13559. ' this.$init = function () {',
  13560. ' $mod.TBird.$init.call(this);',
  13561. ' this.B$2 = 0.0;',
  13562. ' };',
  13563. ' this.A$2 = function () {',
  13564. ' var Result = false;',
  13565. ' this.B$2 = 3.3;',
  13566. ' this.A$2();',
  13567. ' this.B$1 = true;',
  13568. ' this.A = 17;',
  13569. ' this.B$1 = this.A$1 > 1;',
  13570. ' return Result;',
  13571. ' };',
  13572. '});',
  13573. 'this.e = null;',
  13574. '']),
  13575. LinesToStr([ // $mod.$main
  13576. '$mod.e.B$2 = 5.3;',
  13577. 'if ($mod.e.A$2()) ;',
  13578. '']));
  13579. end;
  13580. procedure TTestModule.TestClass_ReintroducedVar;
  13581. begin
  13582. StartProgram(false);
  13583. Add('type');
  13584. Add(' TObject = class');
  13585. Add(' strict private');
  13586. Add(' Some: longint;');
  13587. Add(' end;');
  13588. Add(' TMobile = class');
  13589. Add(' strict private');
  13590. Add(' Some: string;');
  13591. Add(' end;');
  13592. Add(' TCar = class(tmobile)');
  13593. Add(' procedure Some;');
  13594. Add(' procedure Some(vA: longint);');
  13595. Add(' end;');
  13596. Add('procedure tcar.some;');
  13597. Add('begin');
  13598. Add(' Some;');
  13599. Add(' Some(1);');
  13600. Add('end;');
  13601. Add('procedure tcar.some(va: longint); begin end;');
  13602. Add('begin');
  13603. ConvertProgram;
  13604. CheckSource('TestClass_ReintroducedVar',
  13605. LinesToStr([ // statements
  13606. 'rtl.createClass($mod, "TObject", null, function () {',
  13607. ' this.$init = function () {',
  13608. ' this.Some = 0;',
  13609. ' };',
  13610. ' this.$final = function () {',
  13611. ' };',
  13612. '});',
  13613. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13614. ' this.$init = function () {',
  13615. ' $mod.TObject.$init.call(this);',
  13616. ' this.Some$1 = "";',
  13617. ' };',
  13618. '});',
  13619. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13620. ' this.Some$2 = function () {',
  13621. ' this.Some$2();',
  13622. ' this.Some$3(1);',
  13623. ' };',
  13624. ' this.Some$3 = function (vA) {',
  13625. ' };',
  13626. '});',
  13627. '']),
  13628. LinesToStr([ // $mod.$main
  13629. '']));
  13630. end;
  13631. procedure TTestModule.TestClass_RaiseDescendant;
  13632. begin
  13633. StartProgram(false);
  13634. Add([
  13635. 'type',
  13636. ' TObject = class',
  13637. ' constructor Create(Msg: string);',
  13638. ' end;',
  13639. ' Exception = class',
  13640. ' end;',
  13641. ' EConvertError = class(Exception)',
  13642. ' end;',
  13643. 'constructor TObject.Create(Msg: string); begin end;',
  13644. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  13645. 'begin',
  13646. ' raise Exception.Create(''Bar1'');',
  13647. ' raise EConvertError.Create(''Bar2'');',
  13648. ' raise AssertConv(''Bar2'');',
  13649. ' raise AssertConv;',
  13650. '']);
  13651. ConvertProgram;
  13652. CheckSource('TestClass_RaiseDescendant',
  13653. LinesToStr([ // statements
  13654. 'rtl.createClass($mod, "TObject", null, function () {',
  13655. ' this.$init = function () {',
  13656. ' };',
  13657. ' this.$final = function () {',
  13658. ' };',
  13659. ' this.Create = function (Msg) {',
  13660. ' return this;',
  13661. ' };',
  13662. '});',
  13663. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  13664. '});',
  13665. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  13666. '});',
  13667. 'this.AssertConv = function (Msg) {',
  13668. ' var Result = null;',
  13669. ' return Result;',
  13670. '};',
  13671. '']),
  13672. LinesToStr([ // $mod.$main
  13673. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  13674. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  13675. 'throw $mod.AssertConv("Bar2");',
  13676. 'throw $mod.AssertConv("def");',
  13677. '']));
  13678. end;
  13679. procedure TTestModule.TestClass_ExternalMethod;
  13680. begin
  13681. AddModuleWithIntfImplSrc('unit2.pas',
  13682. LinesToStr([
  13683. 'type',
  13684. ' TObject = class',
  13685. ' public',
  13686. ' procedure Intern; external name ''$DoIntern'';',
  13687. ' end;',
  13688. '']),
  13689. LinesToStr([
  13690. '']));
  13691. StartUnit(true);
  13692. Add('interface');
  13693. Add('uses unit2;');
  13694. Add('type');
  13695. Add(' TCar = class(TObject)');
  13696. Add(' public');
  13697. Add(' procedure Intern2; external name ''$DoIntern2'';');
  13698. Add(' procedure DoIt;');
  13699. Add(' end;');
  13700. Add('implementation');
  13701. Add('procedure tcar.doit;');
  13702. Add('begin');
  13703. Add(' Intern;');
  13704. Add(' Intern();');
  13705. Add(' Intern2;');
  13706. Add(' Intern2();');
  13707. Add('end;');
  13708. Add('var Obj: TCar;');
  13709. Add('begin');
  13710. Add(' obj.intern;');
  13711. Add(' obj.intern();');
  13712. Add(' obj.intern2;');
  13713. Add(' obj.intern2();');
  13714. Add(' obj.doit;');
  13715. Add(' obj.doit();');
  13716. Add(' with obj do begin');
  13717. Add(' Intern;');
  13718. Add(' Intern();');
  13719. Add(' Intern2;');
  13720. Add(' Intern2();');
  13721. Add(' end;');
  13722. ConvertUnit;
  13723. CheckSource('TestClass_ExternalMethod',
  13724. LinesToStr([
  13725. 'var $impl = $mod.$impl;',
  13726. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13727. ' this.DoIt = function () {',
  13728. ' this.$DoIntern();',
  13729. ' this.$DoIntern();',
  13730. ' this.$DoIntern2();',
  13731. ' this.$DoIntern2();',
  13732. ' };',
  13733. ' });',
  13734. '']),
  13735. LinesToStr([ // this.$init
  13736. '$impl.Obj.$DoIntern();',
  13737. '$impl.Obj.$DoIntern();',
  13738. '$impl.Obj.$DoIntern2();',
  13739. '$impl.Obj.$DoIntern2();',
  13740. '$impl.Obj.DoIt();',
  13741. '$impl.Obj.DoIt();',
  13742. 'var $with1 = $impl.Obj;',
  13743. '$with1.$DoIntern();',
  13744. '$with1.$DoIntern();',
  13745. '$with1.$DoIntern2();',
  13746. '$with1.$DoIntern2();',
  13747. '']),
  13748. LinesToStr([ // implementation
  13749. '$impl.Obj = null;',
  13750. '']) );
  13751. end;
  13752. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  13753. begin
  13754. StartProgram(false);
  13755. Add('type');
  13756. Add(' TObject = class');
  13757. Add(' procedure DoIt; virtual; external name ''Foo'';');
  13758. Add(' end;');
  13759. Add('begin');
  13760. SetExpectedPasResolverError('Virtual method name must match external',
  13761. nVirtualMethodNameMustMatchExternal);
  13762. ConvertProgram;
  13763. end;
  13764. procedure TTestModule.TestClass_ExternalOverrideFail;
  13765. begin
  13766. StartProgram(false);
  13767. Add('type');
  13768. Add(' TObject = class');
  13769. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  13770. Add(' end;');
  13771. Add(' TCar = class');
  13772. Add(' procedure DoIt; override; external name ''DoIt'';');
  13773. Add(' end;');
  13774. Add('begin');
  13775. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  13776. nInvalidXModifierY);
  13777. ConvertProgram;
  13778. end;
  13779. procedure TTestModule.TestClass_ExternalVar;
  13780. begin
  13781. AddModuleWithIntfImplSrc('unit2.pas',
  13782. LinesToStr([
  13783. '{$modeswitch externalclass}',
  13784. 'type',
  13785. ' TObject = class',
  13786. ' public',
  13787. ' Intern: longint external name ''$Intern'';',
  13788. ' Bracket: longint external name ''["A B"]'';',
  13789. ' end;',
  13790. '']),
  13791. LinesToStr([
  13792. '']));
  13793. StartUnit(true);
  13794. Add([
  13795. 'interface',
  13796. 'uses unit2;',
  13797. '{$modeswitch externalclass}',
  13798. 'type',
  13799. ' TCar = class(tobject)',
  13800. ' public',
  13801. ' Intern2: longint external name ''$Intern2'';',
  13802. ' procedure DoIt;',
  13803. ' end;',
  13804. 'implementation',
  13805. 'procedure tcar.doit;',
  13806. 'begin',
  13807. ' Intern:=Intern+1;',
  13808. ' Intern2:=Intern2+2;',
  13809. ' Bracket:=Bracket+3;',
  13810. 'end;',
  13811. 'var Obj: TCar;',
  13812. 'begin',
  13813. ' obj.intern:=obj.intern+1;',
  13814. ' obj.intern2:=obj.intern2+2;',
  13815. ' obj.Bracket:=obj.Bracket+3;',
  13816. ' with obj do begin',
  13817. ' intern:=intern+1;',
  13818. ' intern2:=intern2+2;',
  13819. ' Bracket:=Bracket+3;',
  13820. ' end;']);
  13821. ConvertUnit;
  13822. CheckSource('TestClass_ExternalVar',
  13823. LinesToStr([
  13824. 'var $impl = $mod.$impl;',
  13825. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13826. ' this.DoIt = function () {',
  13827. ' this.$Intern = this.$Intern + 1;',
  13828. ' this.$Intern2 = this.$Intern2 + 2;',
  13829. ' this["A B"] = this["A B"] + 3;',
  13830. ' };',
  13831. ' });',
  13832. '']),
  13833. LinesToStr([
  13834. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  13835. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  13836. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  13837. 'var $with1 = $impl.Obj;',
  13838. '$with1.$Intern = $with1.$Intern + 1;',
  13839. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  13840. '$with1["A B"] = $with1["A B"] + 3;',
  13841. '']),
  13842. LinesToStr([ // implementation
  13843. '$impl.Obj = null;',
  13844. '']));
  13845. end;
  13846. procedure TTestModule.TestClass_Const;
  13847. begin
  13848. StartProgram(false);
  13849. Add('type');
  13850. Add(' integer = longint;');
  13851. Add(' TClass = class of TObject;');
  13852. Add(' TObject = class');
  13853. Add(' public');
  13854. Add(' const cI: integer = 3;');
  13855. Add(' procedure DoIt;');
  13856. Add(' class procedure DoMore;');
  13857. Add(' end;');
  13858. Add('implementation');
  13859. Add('procedure tobject.doit;');
  13860. Add('begin');
  13861. Add(' if cI=4 then;');
  13862. Add(' if 5=cI then;');
  13863. Add(' if Self.cI=6 then;');
  13864. Add(' if 7=Self.cI then;');
  13865. Add(' with Self do begin');
  13866. Add(' if cI=11 then;');
  13867. Add(' if 12=cI then;');
  13868. Add(' end;');
  13869. Add('end;');
  13870. Add('class procedure tobject.domore;');
  13871. Add('begin');
  13872. Add(' if cI=8 then;');
  13873. Add(' if Self.cI=9 then;');
  13874. Add(' if 10=cI then;');
  13875. Add(' if 11=Self.cI then;');
  13876. Add(' with Self do begin');
  13877. Add(' if cI=13 then;');
  13878. Add(' if 14=cI then;');
  13879. Add(' end;');
  13880. Add('end;');
  13881. Add('var');
  13882. Add(' Obj: TObject;');
  13883. Add(' Cla: TClass;');
  13884. Add('begin');
  13885. Add(' if TObject.cI=21 then ;');
  13886. Add(' if Obj.cI=22 then ;');
  13887. Add(' if Cla.cI=23 then ;');
  13888. Add(' with obj do if ci=24 then;');
  13889. Add(' with TObject do if ci=25 then;');
  13890. Add(' with Cla do if ci=26 then;');
  13891. ConvertProgram;
  13892. CheckSource('TestClass_Const',
  13893. LinesToStr([
  13894. 'rtl.createClass($mod, "TObject", null, function () {',
  13895. ' this.cI = 3;',
  13896. ' this.$init = function () {',
  13897. ' };',
  13898. ' this.$final = function () {',
  13899. ' };',
  13900. ' this.DoIt = function () {',
  13901. ' if (this.cI === 4) ;',
  13902. ' if (5 === this.cI) ;',
  13903. ' if (this.cI === 6) ;',
  13904. ' if (7 === this.cI) ;',
  13905. ' if (this.cI === 11) ;',
  13906. ' if (12 === this.cI) ;',
  13907. ' };',
  13908. ' this.DoMore = function () {',
  13909. ' if (this.cI === 8) ;',
  13910. ' if (this.cI === 9) ;',
  13911. ' if (10 === this.cI) ;',
  13912. ' if (11 === this.cI) ;',
  13913. ' if (this.cI === 13) ;',
  13914. ' if (14 === this.cI) ;',
  13915. ' };',
  13916. '});',
  13917. 'this.Obj = null;',
  13918. 'this.Cla = null;',
  13919. '']),
  13920. LinesToStr([
  13921. 'if ($mod.TObject.cI === 21) ;',
  13922. 'if ($mod.Obj.cI === 22) ;',
  13923. 'if ($mod.Cla.cI === 23) ;',
  13924. 'var $with1 = $mod.Obj;',
  13925. 'if ($with1.cI === 24) ;',
  13926. 'var $with2 = $mod.TObject;',
  13927. 'if ($with2.cI === 25) ;',
  13928. 'var $with3 = $mod.Cla;',
  13929. 'if ($with3.cI === 26) ;',
  13930. '']));
  13931. end;
  13932. procedure TTestModule.TestClass_LocalVarSelfFail;
  13933. begin
  13934. StartProgram(false);
  13935. Add([
  13936. 'type',
  13937. ' TObject = class',
  13938. ' constructor Create;',
  13939. ' end;',
  13940. 'constructor tobject.create;',
  13941. 'var self: longint;',
  13942. 'begin',
  13943. 'end',
  13944. 'begin',
  13945. '']);
  13946. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  13947. ConvertProgram;
  13948. end;
  13949. procedure TTestModule.TestClass_ArgSelfFail;
  13950. begin
  13951. StartProgram(false);
  13952. Add([
  13953. 'type',
  13954. ' TObject = class',
  13955. ' procedure DoIt(Self: longint);',
  13956. ' end;',
  13957. 'procedure tobject.doit(self: longint);',
  13958. 'begin',
  13959. 'end',
  13960. 'begin',
  13961. '']);
  13962. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  13963. ConvertProgram;
  13964. end;
  13965. procedure TTestModule.TestClass_NestedProcSelf;
  13966. begin
  13967. StartProgram(false);
  13968. Add([
  13969. 'type',
  13970. ' TObject = class',
  13971. ' Key: longint;',
  13972. ' class var State: longint;',
  13973. ' procedure DoIt;',
  13974. ' function GetSize: longint; virtual; abstract;',
  13975. ' procedure SetSize(Value: longint); virtual; abstract;',
  13976. ' property Size: longint read GetSize write SetSize;',
  13977. ' end;',
  13978. 'procedure tobject.doit;',
  13979. ' procedure Sub;',
  13980. ' begin',
  13981. ' key:=key+2;',
  13982. ' self.key:=self.key+3;',
  13983. ' state:=state+4;',
  13984. ' self.state:=self.state+5;',
  13985. ' tobject.state:=tobject.state+6;',
  13986. ' size:=size+7;',
  13987. ' self.size:=self.size+8;',
  13988. ' end;',
  13989. 'begin',
  13990. ' sub;',
  13991. ' key:=key+12;',
  13992. ' self.key:=self.key+13;',
  13993. ' state:=state+14;',
  13994. ' self.state:=self.state+15;',
  13995. ' tobject.state:=tobject.state+16;',
  13996. ' size:=size+17;',
  13997. ' self.size:=self.size+18;',
  13998. 'end;',
  13999. 'begin',
  14000. '']);
  14001. ConvertProgram;
  14002. CheckSource('TestClass_NestedProcSelf',
  14003. LinesToStr([ // statements
  14004. 'rtl.createClass($mod, "TObject", null, function () {',
  14005. ' this.State = 0;',
  14006. ' this.$init = function () {',
  14007. ' this.Key = 0;',
  14008. ' };',
  14009. ' this.$final = function () {',
  14010. ' };',
  14011. ' this.DoIt = function () {',
  14012. ' var $Self = this;',
  14013. ' function Sub() {',
  14014. ' $Self.Key = $Self.Key + 2;',
  14015. ' $Self.Key = $Self.Key + 3;',
  14016. ' $mod.TObject.State = $Self.State + 4;',
  14017. ' $mod.TObject.State = $Self.State + 5;',
  14018. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14019. ' $Self.SetSize($Self.GetSize() + 7);',
  14020. ' $Self.SetSize($Self.GetSize() + 8);',
  14021. ' };',
  14022. ' Sub();',
  14023. ' $Self.Key = $Self.Key + 12;',
  14024. ' $Self.Key = $Self.Key + 13;',
  14025. ' $mod.TObject.State = $Self.State + 14;',
  14026. ' $mod.TObject.State = $Self.State + 15;',
  14027. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14028. ' $Self.SetSize($Self.GetSize() + 17);',
  14029. ' $Self.SetSize($Self.GetSize() + 18);',
  14030. ' };',
  14031. '});',
  14032. '']),
  14033. LinesToStr([ // $mod.$main
  14034. '']));
  14035. end;
  14036. procedure TTestModule.TestClass_NestedProcSelf2;
  14037. begin
  14038. StartProgram(false);
  14039. Add([
  14040. 'type',
  14041. ' TObject = class',
  14042. ' Key: longint;',
  14043. ' class var State: longint;',
  14044. ' function GetSize: longint; virtual; abstract;',
  14045. ' procedure SetSize(Value: longint); virtual; abstract;',
  14046. ' property Size: longint read GetSize write SetSize;',
  14047. ' end;',
  14048. ' TBird = class',
  14049. ' procedure DoIt;',
  14050. ' end;',
  14051. 'procedure tbird.doit;',
  14052. ' procedure Sub;',
  14053. ' begin',
  14054. ' key:=key+2;',
  14055. ' self.key:=self.key+3;',
  14056. ' state:=state+4;',
  14057. ' self.state:=self.state+5;',
  14058. ' tobject.state:=tobject.state+6;',
  14059. ' size:=size+7;',
  14060. ' self.size:=self.size+8;',
  14061. ' end;',
  14062. 'begin',
  14063. ' sub;',
  14064. ' key:=key+12;',
  14065. ' self.key:=self.key+13;',
  14066. ' state:=state+14;',
  14067. ' self.state:=self.state+15;',
  14068. ' tobject.state:=tobject.state+16;',
  14069. ' size:=size+17;',
  14070. ' self.size:=self.size+18;',
  14071. 'end;',
  14072. 'begin',
  14073. '']);
  14074. ConvertProgram;
  14075. CheckSource('TestClass_NestedProcSelf2',
  14076. LinesToStr([ // statements
  14077. 'rtl.createClass($mod, "TObject", null, function () {',
  14078. ' this.State = 0;',
  14079. ' this.$init = function () {',
  14080. ' this.Key = 0;',
  14081. ' };',
  14082. ' this.$final = function () {',
  14083. ' };',
  14084. '});',
  14085. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14086. ' this.DoIt = function () {',
  14087. ' var $Self = this;',
  14088. ' function Sub() {',
  14089. ' $Self.Key = $Self.Key + 2;',
  14090. ' $Self.Key = $Self.Key + 3;',
  14091. ' $mod.TObject.State = $Self.State + 4;',
  14092. ' $mod.TObject.State = $Self.State + 5;',
  14093. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14094. ' $Self.SetSize($Self.GetSize() + 7);',
  14095. ' $Self.SetSize($Self.GetSize() + 8);',
  14096. ' };',
  14097. ' Sub();',
  14098. ' $Self.Key = $Self.Key + 12;',
  14099. ' $Self.Key = $Self.Key + 13;',
  14100. ' $mod.TObject.State = $Self.State + 14;',
  14101. ' $mod.TObject.State = $Self.State + 15;',
  14102. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14103. ' $Self.SetSize($Self.GetSize() + 17);',
  14104. ' $Self.SetSize($Self.GetSize() + 18);',
  14105. ' };',
  14106. '});',
  14107. '']),
  14108. LinesToStr([ // $mod.$main
  14109. '']));
  14110. end;
  14111. procedure TTestModule.TestClass_NestedProcClassSelf;
  14112. begin
  14113. StartProgram(false);
  14114. Add([
  14115. 'type',
  14116. ' TObject = class',
  14117. ' class var State: longint;',
  14118. ' class procedure DoIt;',
  14119. ' class function GetSize: longint; virtual; abstract;',
  14120. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14121. ' class property Size: longint read GetSize write SetSize;',
  14122. ' end;',
  14123. 'class procedure tobject.doit;',
  14124. ' procedure Sub;',
  14125. ' begin',
  14126. ' state:=state+2;',
  14127. ' self.state:=self.state+3;',
  14128. ' tobject.state:=tobject.state+4;',
  14129. ' size:=size+5;',
  14130. ' self.size:=self.size+6;',
  14131. ' tobject.size:=tobject.size+7;',
  14132. ' end;',
  14133. 'begin',
  14134. ' sub;',
  14135. ' state:=state+12;',
  14136. ' self.state:=self.state+13;',
  14137. ' tobject.state:=tobject.state+14;',
  14138. ' size:=size+15;',
  14139. ' self.size:=self.size+16;',
  14140. ' tobject.size:=tobject.size+17;',
  14141. 'end;',
  14142. 'begin',
  14143. '']);
  14144. ConvertProgram;
  14145. CheckSource('TestClass_NestedProcClassSelf',
  14146. LinesToStr([ // statements
  14147. 'rtl.createClass($mod, "TObject", null, function () {',
  14148. ' this.State = 0;',
  14149. ' this.$init = function () {',
  14150. ' };',
  14151. ' this.$final = function () {',
  14152. ' };',
  14153. ' this.DoIt = function () {',
  14154. ' var $Self = this;',
  14155. ' function Sub() {',
  14156. ' $mod.TObject.State = $Self.State + 2;',
  14157. ' $mod.TObject.State = $Self.State + 3;',
  14158. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14159. ' $Self.SetSize($Self.GetSize() + 5);',
  14160. ' $Self.SetSize($Self.GetSize() + 6);',
  14161. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  14162. ' };',
  14163. ' Sub();',
  14164. ' $mod.TObject.State = $Self.State + 12;',
  14165. ' $mod.TObject.State = $Self.State + 13;',
  14166. ' $mod.TObject.State = $mod.TObject.State + 14;',
  14167. ' $Self.SetSize($Self.GetSize() + 15);',
  14168. ' $Self.SetSize($Self.GetSize() + 16);',
  14169. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  14170. ' };',
  14171. '});',
  14172. '']),
  14173. LinesToStr([ // $mod.$main
  14174. '']));
  14175. end;
  14176. procedure TTestModule.TestClass_NestedProcCallInherited;
  14177. begin
  14178. StartProgram(false);
  14179. Add([
  14180. 'type',
  14181. ' TObject = class',
  14182. ' function DoIt(k: boolean): longint; virtual;',
  14183. ' end;',
  14184. ' TBird = class',
  14185. ' function DoIt(k: boolean): longint; override;',
  14186. ' end;',
  14187. 'function tobject.doit(k: boolean): longint;',
  14188. 'begin',
  14189. 'end;',
  14190. 'function tbird.doit(k: boolean): longint;',
  14191. ' procedure Sub;',
  14192. ' begin',
  14193. ' inherited DoIt(true);',
  14194. //' if inherited DoIt(false)=4 then ;',
  14195. ' end;',
  14196. 'begin',
  14197. ' Sub;',
  14198. ' inherited;',
  14199. ' inherited DoIt(true);',
  14200. //' if inherited DoIt(false)=14 then ;',
  14201. 'end;',
  14202. 'begin',
  14203. '']);
  14204. ConvertProgram;
  14205. CheckSource('TestClass_NestedProcCallInherited',
  14206. LinesToStr([ // statements
  14207. 'rtl.createClass($mod, "TObject", null, function () {',
  14208. ' this.$init = function () {',
  14209. ' };',
  14210. ' this.$final = function () {',
  14211. ' };',
  14212. ' this.DoIt = function (k) {',
  14213. ' var Result = 0;',
  14214. ' return Result;',
  14215. ' };',
  14216. '});',
  14217. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14218. ' this.DoIt = function (k) {',
  14219. ' var $Self = this;',
  14220. ' var Result = 0;',
  14221. ' function Sub() {',
  14222. ' $mod.TObject.DoIt.call($Self, true);',
  14223. ' };',
  14224. ' Sub();',
  14225. ' $mod.TObject.DoIt.apply($Self, arguments);',
  14226. ' $mod.TObject.DoIt.call($Self, true);',
  14227. ' return Result;',
  14228. ' };',
  14229. '});',
  14230. '']),
  14231. LinesToStr([ // $mod.$main
  14232. '']));
  14233. end;
  14234. procedure TTestModule.TestClass_TObjectFree;
  14235. begin
  14236. StartProgram(false);
  14237. Add([
  14238. 'type',
  14239. ' TObject = class',
  14240. ' Obj: tobject;',
  14241. ' procedure Free;',
  14242. ' procedure Release;',
  14243. ' end;',
  14244. 'procedure tobject.free;',
  14245. 'begin',
  14246. 'end;',
  14247. 'procedure tobject.release;',
  14248. 'begin',
  14249. ' free;',
  14250. ' if true then free;',
  14251. 'end;',
  14252. 'function DoIt(o: tobject): tobject;',
  14253. 'var l: tobject;',
  14254. 'begin',
  14255. ' o.free;',
  14256. ' o.free();',
  14257. ' l.free;',
  14258. ' l.free();',
  14259. ' o.obj.free;',
  14260. ' o.obj.free();',
  14261. ' with o do obj.free;',
  14262. ' with o do obj.free();',
  14263. ' result.Free;',
  14264. ' result.Free();',
  14265. 'end;',
  14266. 'var o: tobject;',
  14267. ' a: array of tobject;',
  14268. 'begin',
  14269. ' o.free;',
  14270. ' o.obj.free;',
  14271. ' a[1+2].free;',
  14272. '']);
  14273. ConvertProgram;
  14274. CheckSource('TestClass_TObjectFree',
  14275. LinesToStr([ // statements
  14276. 'rtl.createClass($mod, "TObject", null, function () {',
  14277. ' this.$init = function () {',
  14278. ' this.Obj = null;',
  14279. ' };',
  14280. ' this.$final = function () {',
  14281. ' this.Obj = undefined;',
  14282. ' };',
  14283. ' this.Free = function () {',
  14284. ' };',
  14285. ' this.Release = function () {',
  14286. ' this.Free();',
  14287. ' if (true) this.Free();',
  14288. ' };',
  14289. '});',
  14290. 'this.DoIt = function (o) {',
  14291. ' var Result = null;',
  14292. ' var l = null;',
  14293. ' o = rtl.freeLoc(o);',
  14294. ' o = rtl.freeLoc(o);',
  14295. ' l = rtl.freeLoc(l);',
  14296. ' l = rtl.freeLoc(l);',
  14297. ' rtl.free(o, "Obj");',
  14298. ' rtl.free(o, "Obj");',
  14299. ' rtl.free(o, "Obj");',
  14300. ' rtl.free(o, "Obj");',
  14301. ' Result = rtl.freeLoc(Result);',
  14302. ' Result = rtl.freeLoc(Result);',
  14303. ' return Result;',
  14304. '};',
  14305. 'this.o = null;',
  14306. 'this.a = [];',
  14307. '']),
  14308. LinesToStr([ // $mod.$main
  14309. 'rtl.free($mod, "o");',
  14310. 'rtl.free($mod.o, "Obj");',
  14311. 'rtl.free($mod.a, 1 + 2);',
  14312. '']));
  14313. end;
  14314. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  14315. begin
  14316. StartProgram(false);
  14317. Add([
  14318. 'type',
  14319. ' TObject = class',
  14320. ' constructor Create;',
  14321. ' procedure Free;',
  14322. ' end;',
  14323. 'constructor TObject.Create; begin end;',
  14324. 'procedure tobject.free; begin end;',
  14325. 'begin',
  14326. ' with tobject.create do free;',
  14327. '']);
  14328. ConvertProgram;
  14329. CheckSource('TestClass_TObjectFreeNewInstance',
  14330. LinesToStr([ // statements
  14331. 'rtl.createClass($mod, "TObject", null, function () {',
  14332. ' this.$init = function () {',
  14333. ' };',
  14334. ' this.$final = function () {',
  14335. ' };',
  14336. ' this.Create = function () {',
  14337. ' return this;',
  14338. ' };',
  14339. ' this.Free = function () {',
  14340. ' };',
  14341. '});',
  14342. '']),
  14343. LinesToStr([ // $mod.$main
  14344. 'var $with1 = $mod.TObject.$create("Create");',
  14345. '$with1=rtl.freeLoc($with1);',
  14346. '']));
  14347. end;
  14348. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  14349. begin
  14350. StartProgram(false);
  14351. Add([
  14352. 'type',
  14353. ' TObject = class',
  14354. ' destructor Destroy;',
  14355. ' procedure Free;',
  14356. ' end;',
  14357. 'destructor TObject.Destroy; begin end;',
  14358. 'procedure tobject.free; begin end;',
  14359. 'var o: tobject;',
  14360. 'begin',
  14361. ' o.free;',
  14362. '']);
  14363. Converter.UseLowerCase:=true;
  14364. ConvertProgram;
  14365. CheckSource('TestClass_TObjectFreeLowerCase',
  14366. LinesToStr([ // statements
  14367. 'rtl.createClass($mod, "tobject", null, function () {',
  14368. ' this.$init = function () {',
  14369. ' };',
  14370. ' this.$final = function () {',
  14371. ' };',
  14372. ' rtl.tObjectDestroy = "destroy";',
  14373. ' this.destroy = function () {',
  14374. ' };',
  14375. ' this.free = function () {',
  14376. ' };',
  14377. '});',
  14378. 'this.o = null;',
  14379. '']),
  14380. LinesToStr([ // $mod.$main
  14381. 'rtl.free($mod, "o");',
  14382. '']));
  14383. end;
  14384. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  14385. begin
  14386. StartProgram(false);
  14387. Add([
  14388. 'type',
  14389. ' TObject = class',
  14390. ' procedure Free;',
  14391. ' function GetObj: tobject; virtual; abstract;',
  14392. ' end;',
  14393. 'procedure tobject.free;',
  14394. 'begin',
  14395. 'end;',
  14396. 'var o: tobject;',
  14397. 'begin',
  14398. ' o.getobj.free;',
  14399. '']);
  14400. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14401. ConvertProgram;
  14402. end;
  14403. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  14404. begin
  14405. StartProgram(false);
  14406. Add([
  14407. 'type',
  14408. ' TObject = class',
  14409. ' procedure Free;',
  14410. ' FObj: TObject;',
  14411. ' property Obj: tobject read FObj write FObj;',
  14412. ' end;',
  14413. 'procedure tobject.free;',
  14414. 'begin',
  14415. 'end;',
  14416. 'var o: tobject;',
  14417. 'begin',
  14418. ' o.obj.free;',
  14419. '']);
  14420. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14421. ConvertProgram;
  14422. end;
  14423. procedure TTestModule.TestClass_ForIn;
  14424. begin
  14425. StartProgram(false);
  14426. Add([
  14427. 'type',
  14428. ' TObject = class end;',
  14429. ' TItem = TObject;',
  14430. ' TEnumerator = class',
  14431. ' FCurrent: TItem;',
  14432. ' property Current: TItem read FCurrent;',
  14433. ' function MoveNext: boolean;',
  14434. ' end;',
  14435. ' TBird = class',
  14436. ' function GetEnumerator: TEnumerator;',
  14437. ' end;',
  14438. 'function TEnumerator.MoveNext: boolean;',
  14439. 'begin',
  14440. 'end;',
  14441. 'function TBird.GetEnumerator: TEnumerator;',
  14442. 'begin',
  14443. 'end;',
  14444. 'var',
  14445. ' b: TBird;',
  14446. ' i, i2: TItem;',
  14447. 'begin',
  14448. ' for i in b do i2:=i;']);
  14449. ConvertProgram;
  14450. CheckSource('TestClass_ForIn',
  14451. LinesToStr([ // statements
  14452. 'rtl.createClass($mod, "TObject", null, function () {',
  14453. ' this.$init = function () {',
  14454. ' };',
  14455. ' this.$final = function () {',
  14456. ' };',
  14457. '});',
  14458. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  14459. ' this.$init = function () {',
  14460. ' $mod.TObject.$init.call(this);',
  14461. ' this.FCurrent = null;',
  14462. ' };',
  14463. ' this.$final = function () {',
  14464. ' this.FCurrent = undefined;',
  14465. ' $mod.TObject.$final.call(this);',
  14466. ' };',
  14467. ' this.MoveNext = function () {',
  14468. ' var Result = false;',
  14469. ' return Result;',
  14470. ' };',
  14471. '});',
  14472. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14473. ' this.GetEnumerator = function () {',
  14474. ' var Result = null;',
  14475. ' return Result;',
  14476. ' };',
  14477. '});',
  14478. 'this.b = null;',
  14479. 'this.i = null;',
  14480. 'this.i2 = null;'
  14481. ]),
  14482. LinesToStr([ // $mod.$main
  14483. 'var $in1 = $mod.b.GetEnumerator();',
  14484. 'try {',
  14485. ' while ($in1.MoveNext()){',
  14486. ' $mod.i = $in1.FCurrent;',
  14487. ' $mod.i2 = $mod.i;',
  14488. ' }',
  14489. '} finally {',
  14490. ' $in1 = rtl.freeLoc($in1)',
  14491. '};',
  14492. '']));
  14493. end;
  14494. procedure TTestModule.TestClassOf_Create;
  14495. begin
  14496. StartProgram(false);
  14497. Add('type');
  14498. Add(' TObject = class');
  14499. Add(' constructor Create;');
  14500. Add(' end;');
  14501. Add(' TClass = class of TObject;');
  14502. Add('constructor tobject.create; begin end;');
  14503. Add('var');
  14504. Add(' Obj: tobject;');
  14505. Add(' C: tclass;');
  14506. Add('begin');
  14507. Add(' obj:=C.create;');
  14508. Add(' with c do obj:=create;');
  14509. ConvertProgram;
  14510. CheckSource('TestClassOf_Create',
  14511. LinesToStr([ // statements
  14512. 'rtl.createClass($mod, "TObject", null, function () {',
  14513. ' this.$init = function () {',
  14514. ' };',
  14515. ' this.$final = function () {',
  14516. ' };',
  14517. ' this.Create = function () {',
  14518. ' return this;',
  14519. ' };',
  14520. '});',
  14521. 'this.Obj = null;',
  14522. 'this.C = null;'
  14523. ]),
  14524. LinesToStr([ // $mod.$main
  14525. '$mod.Obj = $mod.C.$create("Create");',
  14526. 'var $with1 = $mod.C;',
  14527. '$mod.Obj = $with1.$create("Create");',
  14528. '']));
  14529. end;
  14530. procedure TTestModule.TestClassOf_Call;
  14531. begin
  14532. StartProgram(false);
  14533. Add('type');
  14534. Add(' TObject = class');
  14535. Add(' class procedure DoIt;');
  14536. Add(' end;');
  14537. Add(' TClass = class of TObject;');
  14538. Add('class procedure tobject.doit; begin end;');
  14539. Add('var');
  14540. Add(' C: tclass;');
  14541. Add('begin');
  14542. Add(' c.doit;');
  14543. Add(' with c do doit;');
  14544. ConvertProgram;
  14545. CheckSource('TestClassOf_Call',
  14546. LinesToStr([ // statements
  14547. 'rtl.createClass($mod, "TObject", null, function () {',
  14548. ' this.$init = function () {',
  14549. ' };',
  14550. ' this.$final = function () {',
  14551. ' };',
  14552. ' this.DoIt = function () {',
  14553. ' };',
  14554. '});',
  14555. 'this.C = null;'
  14556. ]),
  14557. LinesToStr([ // $mod.$main
  14558. '$mod.C.DoIt();',
  14559. 'var $with1 = $mod.C;',
  14560. '$with1.DoIt();',
  14561. '']));
  14562. end;
  14563. procedure TTestModule.TestClassOf_Assign;
  14564. begin
  14565. StartProgram(false);
  14566. Add('type');
  14567. Add(' TClass = class of TObject;');
  14568. Add(' TObject = class');
  14569. Add(' ClassType: TClass; ');
  14570. Add(' end;');
  14571. Add('var');
  14572. Add(' Obj: tobject;');
  14573. Add(' C: tclass;');
  14574. Add('begin');
  14575. Add(' c:=nil;');
  14576. Add(' c:=obj.classtype;');
  14577. ConvertProgram;
  14578. CheckSource('TestClassOf_Assign',
  14579. LinesToStr([ // statements
  14580. 'rtl.createClass($mod, "TObject", null, function () {',
  14581. ' this.$init = function () {',
  14582. ' this.ClassType = null;',
  14583. ' };',
  14584. ' this.$final = function () {',
  14585. ' this.ClassType = undefined;',
  14586. ' };',
  14587. '});',
  14588. 'this.Obj = null;',
  14589. 'this.C = null;'
  14590. ]),
  14591. LinesToStr([ // $mod.$main
  14592. '$mod.C = null;',
  14593. '$mod.C = $mod.Obj.ClassType;',
  14594. '']));
  14595. end;
  14596. procedure TTestModule.TestClassOf_Is;
  14597. begin
  14598. StartProgram(false);
  14599. Add('type');
  14600. Add(' TClass = class of TObject;');
  14601. Add(' TObject = class');
  14602. Add(' end;');
  14603. Add(' TCar = class');
  14604. Add(' end;');
  14605. Add(' TCars = class of TCar;');
  14606. Add('var');
  14607. Add(' Obj: tobject;');
  14608. Add(' C: tclass;');
  14609. Add(' Cars: tcars;');
  14610. Add('begin');
  14611. Add(' if c is tcar then ;');
  14612. Add(' if c is tcars then ;');
  14613. ConvertProgram;
  14614. CheckSource('TestClassOf_Is',
  14615. LinesToStr([ // statements
  14616. 'rtl.createClass($mod, "TObject", null, function () {',
  14617. ' this.$init = function () {',
  14618. ' };',
  14619. ' this.$final = function () {',
  14620. ' };',
  14621. '});',
  14622. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  14623. '});',
  14624. 'this.Obj = null;',
  14625. 'this.C = null;',
  14626. 'this.Cars = null;'
  14627. ]),
  14628. LinesToStr([ // $mod.$main
  14629. 'if(rtl.is($mod.C,$mod.TCar));',
  14630. 'if(rtl.is($mod.C,$mod.TCar));',
  14631. '']));
  14632. end;
  14633. procedure TTestModule.TestClassOf_Compare;
  14634. begin
  14635. StartProgram(false);
  14636. Add('type');
  14637. Add(' TClass = class of TObject;');
  14638. Add(' TObject = class');
  14639. Add(' ClassType: TClass; ');
  14640. Add(' end;');
  14641. Add('var');
  14642. Add(' b: boolean;');
  14643. Add(' Obj: tobject;');
  14644. Add(' C: tclass;');
  14645. Add('begin');
  14646. Add(' b:=c=nil;');
  14647. Add(' b:=nil=c;');
  14648. Add(' b:=c=obj.classtype;');
  14649. Add(' b:=obj.classtype=c;');
  14650. Add(' b:=c=TObject;');
  14651. Add(' b:=TObject=c;');
  14652. Add(' b:=c<>nil;');
  14653. Add(' b:=nil<>c;');
  14654. Add(' b:=c<>obj.classtype;');
  14655. Add(' b:=obj.classtype<>c;');
  14656. Add(' b:=c<>TObject;');
  14657. Add(' b:=TObject<>c;');
  14658. ConvertProgram;
  14659. CheckSource('TestClassOf_Compare',
  14660. LinesToStr([ // statements
  14661. 'rtl.createClass($mod, "TObject", null, function () {',
  14662. ' this.$init = function () {',
  14663. ' this.ClassType = null;',
  14664. ' };',
  14665. ' this.$final = function () {',
  14666. ' this.ClassType = undefined;',
  14667. ' };',
  14668. '});',
  14669. 'this.b = false;',
  14670. 'this.Obj = null;',
  14671. 'this.C = null;'
  14672. ]),
  14673. LinesToStr([ // $mod.$main
  14674. '$mod.b = $mod.C === null;',
  14675. '$mod.b = null === $mod.C;',
  14676. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  14677. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  14678. '$mod.b = $mod.C === $mod.TObject;',
  14679. '$mod.b = $mod.TObject === $mod.C;',
  14680. '$mod.b = $mod.C !== null;',
  14681. '$mod.b = null !== $mod.C;',
  14682. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  14683. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  14684. '$mod.b = $mod.C !== $mod.TObject;',
  14685. '$mod.b = $mod.TObject !== $mod.C;',
  14686. '']));
  14687. end;
  14688. procedure TTestModule.TestClassOf_ClassVar;
  14689. begin
  14690. StartProgram(false);
  14691. Add('type');
  14692. Add(' TObject = class');
  14693. Add(' class var id: longint;');
  14694. Add(' end;');
  14695. Add(' TClass = class of TObject;');
  14696. Add('var');
  14697. Add(' C: tclass;');
  14698. Add('begin');
  14699. Add(' C.id:=C.id;');
  14700. ConvertProgram;
  14701. CheckSource('TestClassOf_ClassVar',
  14702. LinesToStr([ // statements
  14703. 'rtl.createClass($mod, "TObject", null, function () {',
  14704. ' this.id = 0;',
  14705. ' this.$init = function () {',
  14706. ' };',
  14707. ' this.$final = function () {',
  14708. ' };',
  14709. '});',
  14710. 'this.C = null;'
  14711. ]),
  14712. LinesToStr([ // $mod.$main
  14713. '$mod.TObject.id = $mod.C.id;',
  14714. '']));
  14715. end;
  14716. procedure TTestModule.TestClassOf_ClassMethod;
  14717. begin
  14718. StartProgram(false);
  14719. Add('type');
  14720. Add(' TObject = class');
  14721. Add(' class function DoIt(i: longint = 0): longint;');
  14722. Add(' end;');
  14723. Add(' TClass = class of TObject;');
  14724. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  14725. Add('var');
  14726. Add(' i: longint;');
  14727. Add(' C: tclass;');
  14728. Add('begin');
  14729. Add(' C.DoIt;');
  14730. Add(' C.DoIt();');
  14731. Add(' i:=C.DoIt;');
  14732. Add(' i:=C.DoIt();');
  14733. ConvertProgram;
  14734. CheckSource('TestClassOf_ClassMethod',
  14735. LinesToStr([ // statements
  14736. 'rtl.createClass($mod, "TObject", null, function () {',
  14737. ' this.$init = function () {',
  14738. ' };',
  14739. ' this.$final = function () {',
  14740. ' };',
  14741. ' this.DoIt = function (i) {',
  14742. ' var Result = 0;',
  14743. ' return Result;',
  14744. ' };',
  14745. '});',
  14746. 'this.i = 0;',
  14747. 'this.C = null;'
  14748. ]),
  14749. LinesToStr([ // $mod.$main
  14750. '$mod.C.DoIt(0);',
  14751. '$mod.C.DoIt(0);',
  14752. '$mod.i = $mod.C.DoIt(0);',
  14753. '$mod.i = $mod.C.DoIt(0);',
  14754. '']));
  14755. end;
  14756. procedure TTestModule.TestClassOf_ClassProperty;
  14757. begin
  14758. StartProgram(false);
  14759. Add([
  14760. 'type',
  14761. ' TObject = class',
  14762. ' class var FA: longint;',
  14763. ' class function GetA: longint;',
  14764. ' class procedure SetA(Value: longint);',
  14765. ' class property pA: longint read fa write fa;',
  14766. ' class property pB: longint read geta write seta;',
  14767. ' end;',
  14768. ' TObjectClass = class of tobject;',
  14769. 'class function tobject.geta: longint; begin end;',
  14770. 'class procedure tobject.seta(value: longint); begin end;',
  14771. 'var',
  14772. ' b: boolean;',
  14773. ' Obj: tobject;',
  14774. ' Cla: tobjectclass;',
  14775. 'begin',
  14776. ' obj.pa:=obj.pa;',
  14777. ' obj.pb:=obj.pb;',
  14778. ' b:=obj.pa=4;',
  14779. ' b:=obj.pb=obj.pb;',
  14780. ' b:=5=obj.pa;',
  14781. ' cla.pa:=6;',
  14782. ' cla.pa:=cla.pa;',
  14783. ' cla.pb:=cla.pb;',
  14784. ' b:=cla.pa=7;',
  14785. ' b:=cla.pb=cla.pb;',
  14786. ' b:=8=cla.pa;',
  14787. ' tobject.pa:=9;',
  14788. ' tobject.pb:=tobject.pb;',
  14789. ' b:=tobject.pa=10;',
  14790. ' b:=11=tobject.pa;',
  14791. '']);
  14792. ConvertProgram;
  14793. CheckSource('TestClassOf_ClassProperty',
  14794. LinesToStr([ // statements
  14795. 'rtl.createClass($mod, "TObject", null, function () {',
  14796. ' this.FA = 0;',
  14797. ' this.$init = function () {',
  14798. ' };',
  14799. ' this.$final = function () {',
  14800. ' };',
  14801. ' this.GetA = function () {',
  14802. ' var Result = 0;',
  14803. ' return Result;',
  14804. ' };',
  14805. ' this.SetA = function (Value) {',
  14806. ' };',
  14807. '});',
  14808. 'this.b = false;',
  14809. 'this.Obj = null;',
  14810. 'this.Cla = null;'
  14811. ]),
  14812. LinesToStr([ // $mod.$main
  14813. '$mod.TObject.FA = $mod.Obj.FA;',
  14814. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  14815. '$mod.b = $mod.Obj.FA === 4;',
  14816. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  14817. '$mod.b = 5 === $mod.Obj.FA;',
  14818. '$mod.TObject.FA = 6;',
  14819. '$mod.TObject.FA = $mod.Cla.FA;',
  14820. '$mod.Cla.SetA($mod.Cla.GetA());',
  14821. '$mod.b = $mod.Cla.FA === 7;',
  14822. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  14823. '$mod.b = 8 === $mod.Cla.FA;',
  14824. '$mod.TObject.FA = 9;',
  14825. '$mod.TObject.SetA($mod.TObject.GetA());',
  14826. '$mod.b = $mod.TObject.FA === 10;',
  14827. '$mod.b = 11 === $mod.TObject.FA;',
  14828. '']));
  14829. end;
  14830. procedure TTestModule.TestClassOf_ClassMethodSelf;
  14831. begin
  14832. StartProgram(false);
  14833. Add('type');
  14834. Add(' TObject = class');
  14835. Add(' class var GlobalId: longint;');
  14836. Add(' class procedure ProcA;');
  14837. Add(' end;');
  14838. Add('class procedure tobject.proca;');
  14839. Add('var b: boolean;');
  14840. Add('begin');
  14841. Add(' b:=self=nil;');
  14842. Add(' b:=self.globalid=3;');
  14843. Add(' b:=4=self.globalid;');
  14844. Add(' self.globalid:=5;');
  14845. Add(' self.proca;');
  14846. Add('end;');
  14847. Add('begin');
  14848. ConvertProgram;
  14849. CheckSource('TestClassOf_ClassMethodSelf',
  14850. LinesToStr([ // statements
  14851. 'rtl.createClass($mod, "TObject", null, function () {',
  14852. ' this.GlobalId = 0;',
  14853. ' this.$init = function () {',
  14854. ' };',
  14855. ' this.$final = function () {',
  14856. ' };',
  14857. ' this.ProcA = function () {',
  14858. ' var b = false;',
  14859. ' b = this === null;',
  14860. ' b = this.GlobalId === 3;',
  14861. ' b = 4 === this.GlobalId;',
  14862. ' $mod.TObject.GlobalId = 5;',
  14863. ' this.ProcA();',
  14864. ' };',
  14865. '});'
  14866. ]),
  14867. LinesToStr([ // $mod.$main
  14868. '']));
  14869. end;
  14870. procedure TTestModule.TestClassOf_TypeCast;
  14871. begin
  14872. StartProgram(false);
  14873. Add('type');
  14874. Add(' TObject = class');
  14875. Add(' class procedure {#TObject_DoIt}DoIt;');
  14876. Add(' end;');
  14877. Add(' TClass = class of TObject;');
  14878. Add(' TMobile = class');
  14879. Add(' class procedure {#TMobile_DoIt}DoIt;');
  14880. Add(' end;');
  14881. Add(' TMobileClass = class of TMobile;');
  14882. Add(' TCar = class(TMobile)');
  14883. Add(' class procedure {#TCar_DoIt}DoIt;');
  14884. Add(' end;');
  14885. Add(' TCarClass = class of TCar;');
  14886. Add('class procedure TObject.DoIt;');
  14887. Add('begin');
  14888. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14889. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14890. Add('end;');
  14891. Add('class procedure TMobile.DoIt;');
  14892. Add('begin');
  14893. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14894. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14895. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  14896. Add('end;');
  14897. Add('class procedure TCar.DoIt; begin end;');
  14898. Add('var');
  14899. Add(' ObjC: TClass;');
  14900. Add(' MobileC: TMobileClass;');
  14901. Add(' CarC: TCarClass;');
  14902. Add('begin');
  14903. Add(' ObjC.{@TObject_DoIt}DoIt;');
  14904. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  14905. Add(' CarC.{@TCar_DoIt}DoIt;');
  14906. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  14907. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  14908. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  14909. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  14910. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  14911. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  14912. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  14913. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  14914. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  14915. ConvertProgram;
  14916. CheckSource('TestClassOf_TypeCast',
  14917. LinesToStr([ // statements
  14918. 'rtl.createClass($mod, "TObject", null, function () {',
  14919. ' this.$init = function () {',
  14920. ' };',
  14921. ' this.$final = function () {',
  14922. ' };',
  14923. ' this.DoIt = function () {',
  14924. ' this.DoIt();',
  14925. ' this.DoIt$1();',
  14926. ' };',
  14927. '});',
  14928. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  14929. ' this.DoIt$1 = function () {',
  14930. ' this.DoIt();',
  14931. ' this.DoIt$1();',
  14932. ' this.DoIt$2();',
  14933. ' };',
  14934. '});',
  14935. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  14936. ' this.DoIt$2 = function () {',
  14937. ' };',
  14938. '});',
  14939. 'this.ObjC = null;',
  14940. 'this.MobileC = null;',
  14941. 'this.CarC = null;',
  14942. '']),
  14943. LinesToStr([ // $mod.$main
  14944. '$mod.ObjC.DoIt();',
  14945. '$mod.MobileC.DoIt$1();',
  14946. '$mod.CarC.DoIt$2();',
  14947. '$mod.ObjC.DoIt();',
  14948. '$mod.ObjC.DoIt$1();',
  14949. '$mod.ObjC.DoIt$2();',
  14950. '$mod.MobileC.DoIt();',
  14951. '$mod.MobileC.DoIt$1();',
  14952. '$mod.MobileC.DoIt$2();',
  14953. '$mod.CarC.DoIt();',
  14954. '$mod.CarC.DoIt$1();',
  14955. '$mod.CarC.DoIt$2();',
  14956. '']));
  14957. end;
  14958. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  14959. begin
  14960. StartProgram(false);
  14961. Add('type');
  14962. Add(' TObject = class');
  14963. Add(' function CurNow: longint; ');
  14964. Add(' class function Now: longint; ');
  14965. Add(' end;');
  14966. Add('function TObject.CurNow: longint; begin end;');
  14967. Add('class function TObject.Now: longint; begin end;');
  14968. Add('var');
  14969. Add(' Obj: tobject;');
  14970. Add(' vI: longint;');
  14971. Add('begin');
  14972. Add(' obj.curnow;');
  14973. Add(' vi:=obj.curnow;');
  14974. Add(' tobject.now;');
  14975. Add(' vi:=tobject.now;');
  14976. ConvertProgram;
  14977. CheckSource('TestClassOf_ImplicitFunctionCall',
  14978. LinesToStr([ // statements
  14979. 'rtl.createClass($mod, "TObject", null, function () {',
  14980. ' this.$init = function () {',
  14981. ' };',
  14982. ' this.$final = function () {',
  14983. ' };',
  14984. ' this.CurNow = function () {',
  14985. ' var Result = 0;',
  14986. ' return Result;',
  14987. ' };',
  14988. ' this.Now = function () {',
  14989. ' var Result = 0;',
  14990. ' return Result;',
  14991. ' };',
  14992. '});',
  14993. 'this.Obj = null;',
  14994. 'this.vI = 0;',
  14995. '']),
  14996. LinesToStr([ // $mod.$main
  14997. '$mod.Obj.CurNow();',
  14998. '$mod.vI = $mod.Obj.CurNow();',
  14999. '$mod.TObject.Now();',
  15000. '$mod.vI = $mod.TObject.Now();',
  15001. '']));
  15002. end;
  15003. procedure TTestModule.TestClassOf_Const;
  15004. begin
  15005. StartProgram(false);
  15006. Add([
  15007. 'type',
  15008. ' TObject = class',
  15009. ' end;',
  15010. ' TBird = TObject;',
  15011. ' TBirds = class of TBird;',
  15012. ' TEagles = TBirds;',
  15013. ' THawk = class(TBird);',
  15014. 'const',
  15015. ' Hawk: TEagles = THawk;',
  15016. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  15017. ' TBird,',
  15018. ' THawk',
  15019. ' );',
  15020. 'begin']);
  15021. ConvertProgram;
  15022. CheckSource('TestClassOf_Const',
  15023. LinesToStr([ // statements
  15024. 'rtl.createClass($mod, "TObject", null, function () {',
  15025. ' this.$init = function () {',
  15026. ' };',
  15027. ' this.$final = function () {',
  15028. ' };',
  15029. '});',
  15030. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  15031. '});',
  15032. 'this.Hawk = $mod.THawk;',
  15033. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  15034. '']),
  15035. LinesToStr([ // $mod.$main
  15036. '']));
  15037. end;
  15038. procedure TTestModule.TestNestedClass_Alias;
  15039. begin
  15040. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15041. StartProgram(false);
  15042. Add([
  15043. 'type',
  15044. ' TObject = class',
  15045. ' type TNested = type longint;',
  15046. ' end;',
  15047. 'type TAlias = type tobject.tnested;',
  15048. 'var i: tobject.tnested = 3;',
  15049. 'var j: TAlias = 4;',
  15050. 'begin',
  15051. ' if typeinfo(TAlias)=nil then ;',
  15052. ' if typeinfo(tobject.tnested)=nil then ;',
  15053. '']);
  15054. ConvertProgram;
  15055. CheckSource('TestNestedClass_Alias',
  15056. LinesToStr([ // statements
  15057. 'rtl.createClass($mod, "TObject", null, function () {',
  15058. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  15059. ' this.$init = function () {',
  15060. ' };',
  15061. ' this.$final = function () {',
  15062. ' };',
  15063. '});',
  15064. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  15065. 'this.i = 3;',
  15066. 'this.j = 4;',
  15067. '']),
  15068. LinesToStr([ // $mod.$main
  15069. 'if ($mod.$rtti["TAlias"] === null) ;',
  15070. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  15071. '']));
  15072. end;
  15073. procedure TTestModule.TestNestedClass_Record;
  15074. begin
  15075. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15076. StartProgram(false);
  15077. Add([
  15078. 'type',
  15079. ' TObject = class',
  15080. ' type TPoint = record',
  15081. ' x,y: byte;',
  15082. ' end;',
  15083. ' procedure DoIt(t: TPoint);',
  15084. ' end;',
  15085. 'procedure tobject.DoIt(t: TPoint);',
  15086. 'var p: TPoint;',
  15087. 'begin',
  15088. ' t.x:=t.y;',
  15089. ' p:=t;',
  15090. 'end;',
  15091. 'var',
  15092. ' p: tobject.tpoint = (x:2; y:4);',
  15093. ' o: TObject;',
  15094. 'begin',
  15095. ' p:=p;',
  15096. ' o.doit(p);',
  15097. '']);
  15098. ConvertProgram;
  15099. CheckSource('TestNestedClass_Record',
  15100. LinesToStr([ // statements
  15101. 'rtl.createClass($mod, "TObject", null, function () {',
  15102. ' rtl.recNewT(this, "TPoint", function () {',
  15103. ' this.x = 0;',
  15104. ' this.y = 0;',
  15105. ' this.$eq = function (b) {',
  15106. ' return (this.x === b.x) && (this.y === b.y);',
  15107. ' };',
  15108. ' this.$assign = function (s) {',
  15109. ' this.x = s.x;',
  15110. ' this.y = s.y;',
  15111. ' return this;',
  15112. ' };',
  15113. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  15114. ' $r.addField("x", rtl.byte);',
  15115. ' $r.addField("y", rtl.byte);',
  15116. ' });',
  15117. ' this.$init = function () {',
  15118. ' };',
  15119. ' this.$final = function () {',
  15120. ' };',
  15121. ' this.DoIt = function (t) {',
  15122. ' var p = this.TPoint.$new();',
  15123. ' t.x = t.y;',
  15124. ' p.$assign(t);',
  15125. ' };',
  15126. '});',
  15127. 'this.p = $mod.TObject.TPoint.$clone({',
  15128. ' x: 2,',
  15129. ' y: 4',
  15130. '});',
  15131. 'this.o = null;',
  15132. '']),
  15133. LinesToStr([ // $mod.$main
  15134. '$mod.p.$assign($mod.p);',
  15135. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  15136. '']));
  15137. end;
  15138. procedure TTestModule.TestNestedClass_Class;
  15139. begin
  15140. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15141. StartProgram(false);
  15142. Add([
  15143. 'type',
  15144. ' TObject = class end;',
  15145. ' TBird = class',
  15146. ' type TLeg = class',
  15147. ' FId: longint;',
  15148. ' constructor Create;',
  15149. ' function Create(i: longint): TLeg;',
  15150. ' end;',
  15151. ' function DoIt(b: TBird): Tleg;',
  15152. ' end;',
  15153. 'constructor tbird.tleg.create;',
  15154. 'begin',
  15155. ' FId:=3;',
  15156. 'end;',
  15157. 'function tbird.tleg.Create(i: longint): TLeg;',
  15158. 'begin',
  15159. ' Create;',
  15160. ' Result:=TLeg.Create;',
  15161. ' Result:=TBird.TLeg.Create;',
  15162. ' Result:=Create(3);',
  15163. ' FId:=i;',
  15164. 'end;',
  15165. 'function tbird.DoIt(b: tbird): tleg;',
  15166. 'begin',
  15167. ' Result.Create;',
  15168. ' Result:=TLeg.Create;',
  15169. ' Result:=TBird.TLeg.Create;',
  15170. ' Result:=Result.Create(3);',
  15171. 'end;',
  15172. 'var',
  15173. ' b: Tbird.tleg;',
  15174. 'begin',
  15175. ' b.Create;',
  15176. ' b:=TBird.TLeg.Create;',
  15177. ' b:=b.Create(3);',
  15178. '']);
  15179. ConvertProgram;
  15180. CheckSource('TestNestedClass_Class',
  15181. LinesToStr([ // statements
  15182. 'rtl.createClass($mod, "TObject", null, function () {',
  15183. ' this.$init = function () {',
  15184. ' };',
  15185. ' this.$final = function () {',
  15186. ' };',
  15187. '});',
  15188. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15189. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  15190. ' this.$init = function () {',
  15191. ' $mod.TObject.$init.call(this);',
  15192. ' this.FId = 0;',
  15193. ' };',
  15194. ' this.Create = function () {',
  15195. ' this.FId = 3;',
  15196. ' return this;',
  15197. ' };',
  15198. ' this.Create$1 = function (i) {',
  15199. ' var Result = null;',
  15200. ' this.Create();',
  15201. ' Result = $mod.TBird.TLeg.$create("Create");',
  15202. ' Result = $mod.TBird.TLeg.$create("Create");',
  15203. ' Result = this.Create$1(3);',
  15204. ' this.FId = i;',
  15205. ' return Result;',
  15206. ' };',
  15207. ' });',
  15208. ' this.DoIt = function (b) {',
  15209. ' var Result = null;',
  15210. ' Result.Create();',
  15211. ' Result = this.TLeg.$create("Create");',
  15212. ' Result = $mod.TBird.TLeg.$create("Create");',
  15213. ' Result = Result.Create$1(3);',
  15214. ' return Result;',
  15215. ' };',
  15216. '});',
  15217. 'this.b = null;',
  15218. '']),
  15219. LinesToStr([ // $mod.$main
  15220. '$mod.b.Create();',
  15221. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  15222. '$mod.b = $mod.b.Create$1(3);',
  15223. '']));
  15224. end;
  15225. procedure TTestModule.TestExternalClass_Var;
  15226. begin
  15227. StartProgram(false);
  15228. Add([
  15229. '{$modeswitch externalclass}',
  15230. 'type',
  15231. ' TExtA = class external name ''ExtObj''',
  15232. ' Id: longint external name ''$Id'';',
  15233. ' B: longint;',
  15234. ' end;',
  15235. 'var Obj: TExtA;',
  15236. 'begin',
  15237. ' obj.id:=obj.id+1;',
  15238. ' obj.B:=obj.B+1;']);
  15239. ConvertProgram;
  15240. CheckSource('TestExternalClass_Var',
  15241. LinesToStr([ // statements
  15242. 'this.Obj = null;',
  15243. '']),
  15244. LinesToStr([ // $mod.$main
  15245. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  15246. '$mod.Obj.B = $mod.Obj.B + 1;',
  15247. '']));
  15248. end;
  15249. procedure TTestModule.TestExternalClass_Const;
  15250. begin
  15251. StartProgram(false);
  15252. Add([
  15253. '{$modeswitch externalclass}',
  15254. 'type',
  15255. ' TExtA = class external name ''ExtObj''',
  15256. ' const Two: longint = 2;',
  15257. ' const Three = 3;',
  15258. ' const Id: longint;',
  15259. ' end;',
  15260. ' TExtB = class external name ''ExtB''',
  15261. ' A: TExtA;',
  15262. ' end;',
  15263. 'var',
  15264. ' A: texta;',
  15265. ' B: textb;',
  15266. ' i: longint;',
  15267. 'begin',
  15268. ' i:=a.two;',
  15269. ' i:=texta.two;',
  15270. ' i:=a.three;',
  15271. ' i:=texta.three;',
  15272. ' i:=a.id;',
  15273. ' i:=texta.id;',
  15274. '']);
  15275. ConvertProgram;
  15276. CheckSource('TestExternalClass_Const',
  15277. LinesToStr([ // statements
  15278. 'this.A = null;',
  15279. 'this.B = null;',
  15280. 'this.i = 0;',
  15281. '']),
  15282. LinesToStr([ // $mod.$main
  15283. '$mod.i = 2;',
  15284. '$mod.i = 2;',
  15285. '$mod.i = 3;',
  15286. '$mod.i = 3;',
  15287. '$mod.i = $mod.A.Id;',
  15288. '$mod.i = ExtObj.Id;',
  15289. '']));
  15290. end;
  15291. procedure TTestModule.TestExternalClass_Dollar;
  15292. begin
  15293. StartProgram(false);
  15294. Add([
  15295. '{$modeswitch externalclass}',
  15296. 'type',
  15297. ' TExtA = class external name ''$''',
  15298. ' Id: longint external name ''$'';',
  15299. ' function Bla(i: longint): longint; external name ''$'';',
  15300. ' end;',
  15301. 'function dollar(k: longint): longint; external name ''$'';',
  15302. 'var Obj: TExtA;',
  15303. 'begin',
  15304. ' dollar(1);',
  15305. ' obj.id:=obj.id+2;',
  15306. ' obj.Bla(3);',
  15307. '']);
  15308. ConvertProgram;
  15309. CheckSource('TestExternalClass_Dollar',
  15310. LinesToStr([ // statements
  15311. 'this.Obj = null;',
  15312. '']),
  15313. LinesToStr([ // $mod.$main
  15314. '$(1);',
  15315. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  15316. '$mod.Obj.$(3);',
  15317. '']));
  15318. end;
  15319. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  15320. begin
  15321. StartProgram(false);
  15322. Add('{$modeswitch externalclass}');
  15323. Add('type');
  15324. Add(' TExtA = class external name ''ExtA''');
  15325. Add(' Id: longint external name ''$Id'';');
  15326. Add(' end;');
  15327. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  15328. Add(' Id: longint;');
  15329. Add(' end;');
  15330. Add('begin');
  15331. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  15332. ConvertProgram;
  15333. end;
  15334. procedure TTestModule.TestExternalClass_Method;
  15335. begin
  15336. StartProgram(false);
  15337. Add('{$modeswitch externalclass}');
  15338. Add('type');
  15339. Add(' TExtA = class external name ''ExtObj''');
  15340. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  15341. Add(' procedure DoSome(Id: longint = 1);');
  15342. Add(' end;');
  15343. Add('var Obj: texta;');
  15344. Add('begin');
  15345. Add(' obj.doit;');
  15346. Add(' obj.doit();');
  15347. Add(' obj.doit(2);');
  15348. Add(' with obj do begin');
  15349. Add(' doit;');
  15350. Add(' doit();');
  15351. Add(' doit(3);');
  15352. Add(' end;');
  15353. ConvertProgram;
  15354. CheckSource('TestExternalClass_Method',
  15355. LinesToStr([ // statements
  15356. 'this.Obj = null;',
  15357. '']),
  15358. LinesToStr([ // $mod.$main
  15359. '$mod.Obj.$Execute(1);',
  15360. '$mod.Obj.$Execute(1);',
  15361. '$mod.Obj.$Execute(2);',
  15362. 'var $with1 = $mod.Obj;',
  15363. '$with1.$Execute(1);',
  15364. '$with1.$Execute(1);',
  15365. '$with1.$Execute(3);',
  15366. '']));
  15367. end;
  15368. procedure TTestModule.TestExternalClass_ClassMethod;
  15369. begin
  15370. StartProgram(false);
  15371. Add([
  15372. '{$modeswitch externalclass}',
  15373. 'type',
  15374. ' TExtA = class external name ''ExtObj''',
  15375. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15376. ' end;',
  15377. ' TExtB = TExtA;',
  15378. 'var p: Pointer;',
  15379. 'begin',
  15380. ' texta.doit;',
  15381. ' texta.doit();',
  15382. ' texta.doit(2);',
  15383. ' p:[email protected];',
  15384. ' with texta do begin',
  15385. ' doit;',
  15386. ' doit();',
  15387. ' doit(3);',
  15388. ' p:=@DoIt;',
  15389. ' end;',
  15390. ' textb.doit;',
  15391. ' textb.doit();',
  15392. ' textb.doit(4);',
  15393. ' with textb do begin',
  15394. ' doit;',
  15395. ' doit();',
  15396. ' doit(5);',
  15397. ' end;',
  15398. '']);
  15399. ConvertProgram;
  15400. CheckSource('TestExternalClass_ClassMethod',
  15401. LinesToStr([ // statements
  15402. 'this.p = null;',
  15403. '']),
  15404. LinesToStr([ // $mod.$main
  15405. 'ExtObj.$Execute(1);',
  15406. 'ExtObj.$Execute(1);',
  15407. 'ExtObj.$Execute(2);',
  15408. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15409. 'ExtObj.$Execute(1);',
  15410. 'ExtObj.$Execute(1);',
  15411. 'ExtObj.$Execute(3);',
  15412. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15413. 'ExtObj.$Execute(1);',
  15414. 'ExtObj.$Execute(1);',
  15415. 'ExtObj.$Execute(4);',
  15416. 'ExtObj.$Execute(1);',
  15417. 'ExtObj.$Execute(1);',
  15418. 'ExtObj.$Execute(5);',
  15419. '']));
  15420. end;
  15421. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  15422. begin
  15423. StartProgram(false);
  15424. Add([
  15425. '{$modeswitch externalclass}',
  15426. 'type',
  15427. ' TExtA = class external name ''ExtObj''',
  15428. ' class procedure DoIt(Id: longint = 1); static;',
  15429. ' end;',
  15430. 'var p: Pointer;',
  15431. 'begin',
  15432. ' texta.doit;',
  15433. ' texta.doit();',
  15434. ' texta.doit(2);',
  15435. ' p:[email protected];',
  15436. ' with texta do begin',
  15437. ' doit;',
  15438. ' doit();',
  15439. ' doit(3);',
  15440. ' p:=@DoIt;',
  15441. ' end;',
  15442. '']);
  15443. ConvertProgram;
  15444. CheckSource('TestExternalClass_ClassMethodStatic',
  15445. LinesToStr([ // statements
  15446. 'this.p = null;',
  15447. '']),
  15448. LinesToStr([ // $mod.$main
  15449. 'ExtObj.DoIt(1);',
  15450. 'ExtObj.DoIt(1);',
  15451. 'ExtObj.DoIt(2);',
  15452. '$mod.p = ExtObj.DoIt;',
  15453. 'ExtObj.DoIt(1);',
  15454. 'ExtObj.DoIt(1);',
  15455. 'ExtObj.DoIt(3);',
  15456. '$mod.p = ExtObj.DoIt;',
  15457. '']));
  15458. end;
  15459. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  15460. begin
  15461. StartProgram(false);
  15462. Add([
  15463. '{$modeswitch externalclass}',
  15464. 'type',
  15465. ' TBird = class external name ''Array''',
  15466. ' end;',
  15467. 'function GetPtr: Pointer;',
  15468. 'begin',
  15469. 'end;',
  15470. 'procedure Write(const p);',
  15471. 'begin',
  15472. 'end;',
  15473. 'procedure WriteLn; varargs;',
  15474. 'begin',
  15475. 'end;',
  15476. 'begin',
  15477. ' if TBird(GetPtr)=nil then ;',
  15478. ' Write(GetPtr);',
  15479. ' WriteLn(GetPtr);',
  15480. ' Write(TBird(GetPtr));',
  15481. ' WriteLn(TBird(GetPtr));',
  15482. '']);
  15483. ConvertProgram;
  15484. CheckSource('TestFunctionResultInTypeCast',
  15485. LinesToStr([ // statements
  15486. 'this.GetPtr = function () {',
  15487. ' var Result = null;',
  15488. ' return Result;',
  15489. '};',
  15490. 'this.Write = function (p) {',
  15491. '};',
  15492. 'this.WriteLn = function () {',
  15493. '};',
  15494. '']),
  15495. LinesToStr([
  15496. 'if ($mod.GetPtr() === null) ;',
  15497. '$mod.Write($mod.GetPtr());',
  15498. '$mod.WriteLn($mod.GetPtr());',
  15499. '$mod.Write($mod.GetPtr());',
  15500. '$mod.WriteLn($mod.GetPtr());',
  15501. '']));
  15502. end;
  15503. procedure TTestModule.TestExternalClass_NonExternalOverride;
  15504. begin
  15505. StartProgram(false);
  15506. Add([
  15507. '{$modeswitch externalclass}',
  15508. 'type',
  15509. ' TExtA = class external name ''ExtObjA''',
  15510. ' procedure ProcA; virtual;',
  15511. ' procedure ProcB; virtual;',
  15512. ' end;',
  15513. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  15514. ' end;',
  15515. ' TExtC = class (TExtB)',
  15516. ' procedure ProcA; override;',
  15517. ' end;',
  15518. 'procedure TExtC.ProcA;',
  15519. 'begin',
  15520. ' ProcA;',
  15521. ' Self.ProcA;',
  15522. ' ProcB;',
  15523. ' Self.ProcB;',
  15524. 'end;',
  15525. 'var',
  15526. ' A: texta;',
  15527. ' B: textb;',
  15528. ' C: textc;',
  15529. 'begin',
  15530. ' a.proca;',
  15531. ' b.proca;',
  15532. ' c.proca;']);
  15533. ConvertProgram;
  15534. CheckSource('TestExternalClass_NonExternalOverride',
  15535. LinesToStr([ // statements
  15536. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  15537. ' this.$init = function () {',
  15538. ' };',
  15539. ' this.$final = function () {',
  15540. ' };',
  15541. ' this.ProcA = function () {',
  15542. ' this.ProcA();',
  15543. ' this.ProcA();',
  15544. ' this.ProcB();',
  15545. ' this.ProcB();',
  15546. ' };',
  15547. '});',
  15548. 'this.A = null;',
  15549. 'this.B = null;',
  15550. 'this.C = null;',
  15551. '']),
  15552. LinesToStr([ // $mod.$main
  15553. '$mod.A.ProcA();',
  15554. '$mod.B.ProcA();',
  15555. '$mod.C.ProcA();',
  15556. '']));
  15557. end;
  15558. procedure TTestModule.TestExternalClass_OverloadHint;
  15559. begin
  15560. StartProgram(false);
  15561. Add([
  15562. '{$modeswitch externalclass}',
  15563. 'type',
  15564. ' TExtA = class external name ''ExtObjA''',
  15565. ' procedure DoIt;',
  15566. ' procedure DoIt(i: longint);',
  15567. ' end;',
  15568. 'begin',
  15569. '']);
  15570. ConvertProgram;
  15571. CheckResolverUnexpectedHints(true);
  15572. CheckSource('TestExternalClass_OverloadHint',
  15573. LinesToStr([ // statements
  15574. '']),
  15575. LinesToStr([ // $mod.$main
  15576. '']));
  15577. end;
  15578. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  15579. begin
  15580. StartProgram(false);
  15581. Add([
  15582. '{$modeswitch externalclass}',
  15583. 'type',
  15584. ' JSwiper = class external name ''Swiper''',
  15585. ' constructor New;',
  15586. ' end;',
  15587. ' TObject = class',
  15588. ' private',
  15589. ' FSwiper: JSwiper;',
  15590. ' published',
  15591. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  15592. ' end;',
  15593. 'begin',
  15594. ' JSwiper.new;',
  15595. '']);
  15596. ConvertProgram;
  15597. CheckSource('TestExternalClass_SameNamePublishedProperty',
  15598. LinesToStr([ // statements
  15599. 'rtl.createClass($mod, "TObject", null, function () {',
  15600. ' this.$init = function () {',
  15601. ' this.FSwiper = null;',
  15602. ' };',
  15603. ' this.$final = function () {',
  15604. ' this.FSwiper = undefined;',
  15605. ' };',
  15606. ' var $r = this.$rtti;',
  15607. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  15608. '});',
  15609. '']),
  15610. LinesToStr([ // $mod.$main
  15611. 'new Swiper();',
  15612. '']));
  15613. end;
  15614. procedure TTestModule.TestExternalClass_Property;
  15615. begin
  15616. StartProgram(false);
  15617. Add([
  15618. '{$modeswitch externalclass}',
  15619. 'type',
  15620. ' TExtA = class external name ''ExtA''',
  15621. ' function getYear: longint;',
  15622. ' procedure setYear(Value: longint);',
  15623. ' property Year: longint read getyear write setyear;',
  15624. ' end;',
  15625. ' TExtB = class (TExtA)',
  15626. ' procedure OtherSetYear(Value: longint);',
  15627. ' property year write othersetyear;',
  15628. ' end;',
  15629. 'procedure textb.othersetyear(value: longint);',
  15630. 'begin',
  15631. ' setYear(Value+4);',
  15632. 'end;',
  15633. 'var',
  15634. ' A: texta;',
  15635. ' B: textb;',
  15636. 'begin',
  15637. ' a.year:=a.year+1;',
  15638. ' b.year:=b.year+2;']);
  15639. ConvertProgram;
  15640. CheckSource('TestExternalClass_NonExternalOverride',
  15641. LinesToStr([ // statements
  15642. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15643. ' this.$init = function () {',
  15644. ' };',
  15645. ' this.$final = function () {',
  15646. ' };',
  15647. ' this.OtherSetYear = function (Value) {',
  15648. ' this.setYear(Value+4);',
  15649. ' };',
  15650. '});',
  15651. 'this.A = null;',
  15652. 'this.B = null;',
  15653. '']),
  15654. LinesToStr([ // $mod.$main
  15655. '$mod.A.setYear($mod.A.getYear()+1);',
  15656. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  15657. '']));
  15658. end;
  15659. procedure TTestModule.TestExternalClass_PropertyDate;
  15660. begin
  15661. StartProgram(false);
  15662. Add([
  15663. '{$modeswitch externalclass}',
  15664. 'type',
  15665. ' TExtA = class external name ''ExtA''',
  15666. ' end;',
  15667. ' TExtB = class (TExtA)',
  15668. ' FDate: string;',
  15669. ' property Date: string read FDate write FDate;',
  15670. ' property ExtA: string read FDate write FDate;',
  15671. ' end;',
  15672. ' {$M+}',
  15673. ' TObject = class',
  15674. ' FDate: string;',
  15675. ' published',
  15676. ' property Date: string read FDate write FDate;',
  15677. ' property ExtA: string read FDate write FDate;',
  15678. ' end;',
  15679. 'var',
  15680. ' B: textb;',
  15681. ' o: TObject;',
  15682. 'begin',
  15683. ' b.date:=b.exta;',
  15684. ' o.date:=o.exta;']);
  15685. ConvertProgram;
  15686. CheckSource('TestExternalClass_PropertyDate',
  15687. LinesToStr([ // statements
  15688. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15689. ' this.$init = function () {',
  15690. ' this.FDate = "";',
  15691. ' };',
  15692. ' this.$final = function () {',
  15693. ' };',
  15694. '});',
  15695. 'rtl.createClass($mod, "TObject", null, function () {',
  15696. ' this.$init = function () {',
  15697. ' this.FDate = "";',
  15698. ' };',
  15699. ' this.$final = function () {',
  15700. ' };',
  15701. ' var $r = this.$rtti;',
  15702. ' $r.addField("FDate", rtl.string);',
  15703. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  15704. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  15705. '});',
  15706. 'this.B = null;',
  15707. 'this.o = null;',
  15708. '']),
  15709. LinesToStr([ // $mod.$main
  15710. '$mod.B.FDate = $mod.B.FDate;',
  15711. '$mod.o.FDate = $mod.o.FDate;',
  15712. '']));
  15713. end;
  15714. procedure TTestModule.TestExternalClass_ClassProperty;
  15715. begin
  15716. StartProgram(false);
  15717. Add('{$modeswitch externalclass}');
  15718. Add('type');
  15719. Add(' TExtA = class external name ''ExtA''');
  15720. Add(' class function getYear: longint;');
  15721. Add(' class procedure setYear(Value: longint);');
  15722. Add(' class property Year: longint read getyear write setyear;');
  15723. Add(' end;');
  15724. Add(' TExtB = class (TExtA)');
  15725. Add(' class function GetCentury: longint;');
  15726. Add(' class procedure SetCentury(Value: longint);');
  15727. Add(' class property Century: longint read getcentury write setcentury;');
  15728. Add(' end;');
  15729. Add('class function textb.getcentury: longint;');
  15730. Add('begin');
  15731. Add('end;');
  15732. Add('class procedure textb.setcentury(value: longint);');
  15733. Add('begin');
  15734. Add(' setyear(value+11);');
  15735. Add(' texta.year:=texta.year+12;');
  15736. Add(' year:=year+13;');
  15737. Add(' textb.century:=textb.century+14;');
  15738. Add(' century:=century+15;');
  15739. Add('end;');
  15740. Add('var');
  15741. Add(' A: texta;');
  15742. Add(' B: textb;');
  15743. Add('begin');
  15744. Add(' texta.year:=texta.year+1;');
  15745. Add(' textb.year:=textb.year+2;');
  15746. Add(' TextA.year:=TextA.year+3;');
  15747. Add(' b.year:=b.year+4;');
  15748. Add(' textb.century:=textb.century+5;');
  15749. Add(' b.century:=b.century+6;');
  15750. ConvertProgram;
  15751. CheckSource('TestExternalClass_ClassProperty',
  15752. LinesToStr([ // statements
  15753. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15754. ' this.$init = function () {',
  15755. ' };',
  15756. ' this.$final = function () {',
  15757. ' };',
  15758. ' this.GetCentury = function () {',
  15759. ' var Result = 0;',
  15760. ' return Result;',
  15761. ' };',
  15762. ' this.SetCentury = function (Value) {',
  15763. ' this.setYear(Value + 11);',
  15764. ' ExtA.setYear(ExtA.getYear() + 12);',
  15765. ' this.setYear(this.getYear() + 13);',
  15766. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  15767. ' this.SetCentury(this.GetCentury() + 15);',
  15768. ' };',
  15769. '});',
  15770. 'this.A = null;',
  15771. 'this.B = null;',
  15772. '']),
  15773. LinesToStr([ // $mod.$main
  15774. 'ExtA.setYear(ExtA.getYear() + 1);',
  15775. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  15776. 'ExtA.setYear(ExtA.getYear() + 3);',
  15777. '$mod.B.setYear($mod.B.getYear() + 4);',
  15778. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  15779. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  15780. '']));
  15781. end;
  15782. procedure TTestModule.TestExternalClass_ClassOf;
  15783. begin
  15784. StartProgram(false);
  15785. Add('{$modeswitch externalclass}');
  15786. Add('type');
  15787. Add(' TExtA = class external name ''ExtA''');
  15788. Add(' procedure ProcA; virtual;');
  15789. Add(' procedure ProcB; virtual;');
  15790. Add(' end;');
  15791. Add(' TExtAClass = class of TExtA;');
  15792. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15793. Add(' end;');
  15794. Add(' TExtBClass = class of TExtB;');
  15795. Add(' TExtC = class (TExtB)');
  15796. Add(' procedure ProcA; override;');
  15797. Add(' end;');
  15798. Add(' TExtCClass = class of TExtC;');
  15799. Add('procedure TExtC.ProcA; begin end;');
  15800. Add('var');
  15801. Add(' A: texta; ClA: TExtAClass;');
  15802. Add(' B: textb; ClB: TExtBClass;');
  15803. Add(' C: textc; ClC: TExtCClass;');
  15804. Add('begin');
  15805. Add(' ClA:=texta;');
  15806. Add(' ClA:=textb;');
  15807. Add(' ClA:=textc;');
  15808. Add(' ClB:=textb;');
  15809. Add(' ClB:=textc;');
  15810. Add(' ClC:=textc;');
  15811. ConvertProgram;
  15812. CheckSource('TestExternalClass_ClassOf',
  15813. LinesToStr([ // statements
  15814. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15815. ' this.$init = function () {',
  15816. ' };',
  15817. ' this.$final = function () {',
  15818. ' };',
  15819. ' this.ProcA = function () {',
  15820. ' };',
  15821. '});',
  15822. 'this.A = null;',
  15823. 'this.ClA = null;',
  15824. 'this.B = null;',
  15825. 'this.ClB = null;',
  15826. 'this.C = null;',
  15827. 'this.ClC = null;',
  15828. '']),
  15829. LinesToStr([ // $mod.$main
  15830. '$mod.ClA = ExtA;',
  15831. '$mod.ClA = ExtB;',
  15832. '$mod.ClA = $mod.TExtC;',
  15833. '$mod.ClB = ExtB;',
  15834. '$mod.ClB = $mod.TExtC;',
  15835. '$mod.ClC = $mod.TExtC;',
  15836. '']));
  15837. end;
  15838. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  15839. begin
  15840. AddModuleWithIntfImplSrc('unit2.pas',
  15841. LinesToStr([
  15842. '{$modeswitch externalclass}',
  15843. 'type',
  15844. ' TExtA = class external name ''ExtA''',
  15845. ' class var Id: longint;',
  15846. ' end;',
  15847. '']),
  15848. '');
  15849. StartUnit(true);
  15850. Add('interface');
  15851. Add('uses unit2;');
  15852. Add('implementation');
  15853. Add('begin');
  15854. Add(' unit2.texta.id:=unit2.texta.id+1;');
  15855. ConvertUnit;
  15856. CheckSource('TestExternalClass_ClassOtherUnit',
  15857. LinesToStr([
  15858. '']),
  15859. LinesToStr([
  15860. 'ExtA.Id = ExtA.Id + 1;',
  15861. '']));
  15862. end;
  15863. procedure TTestModule.TestExternalClass_Is;
  15864. begin
  15865. StartProgram(false);
  15866. Add([
  15867. '{$modeswitch externalclass}',
  15868. 'type',
  15869. ' TExtA = class external name ''ExtA''',
  15870. ' end;',
  15871. ' TExtAClass = class of TExtA;',
  15872. ' TExtB = class external name ''ExtB'' (TExtA)',
  15873. ' end;',
  15874. ' TExtBClass = class of TExtB;',
  15875. ' TExtC = class (TExtB)',
  15876. ' end;',
  15877. ' TExtCClass = class of TExtC;',
  15878. 'var',
  15879. ' A: texta; ClA: TExtAClass;',
  15880. ' B: textb; ClB: TExtBClass;',
  15881. ' C: textc; ClC: TExtCClass;',
  15882. 'begin',
  15883. ' if a is textb then ;',
  15884. ' if a is textc then ;',
  15885. ' if b is textc then ;',
  15886. ' if cla is textb then ;',
  15887. ' if cla is textc then ;',
  15888. ' if clb is textc then ;',
  15889. ' try',
  15890. ' except',
  15891. ' on TExtA do ;',
  15892. ' on e: TExtB do ;',
  15893. ' end;',
  15894. '']);
  15895. ConvertProgram;
  15896. CheckSource('TestExternalClass_Is',
  15897. LinesToStr([ // statements
  15898. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15899. ' this.$init = function () {',
  15900. ' };',
  15901. ' this.$final = function () {',
  15902. ' };',
  15903. '});',
  15904. 'this.A = null;',
  15905. 'this.ClA = null;',
  15906. 'this.B = null;',
  15907. 'this.ClB = null;',
  15908. 'this.C = null;',
  15909. 'this.ClC = null;',
  15910. '']),
  15911. LinesToStr([ // $mod.$main
  15912. 'if (rtl.isExt($mod.A, ExtB)) ;',
  15913. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  15914. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  15915. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  15916. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  15917. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  15918. 'try {} catch ($e) {',
  15919. ' if (rtl.isExt($e,ExtA)) {}',
  15920. ' else if (rtl.isExt($e,ExtB)) {',
  15921. ' var e = $e;',
  15922. ' } else throw $e',
  15923. '};',
  15924. '']));
  15925. end;
  15926. procedure TTestModule.TestExternalClass_As;
  15927. begin
  15928. StartProgram(false);
  15929. Add('{$modeswitch externalclass}');
  15930. Add('type');
  15931. Add(' TExtA = class external name ''ExtA''');
  15932. Add(' end;');
  15933. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15934. Add(' end;');
  15935. Add(' TExtC = class (TExtB)');
  15936. Add(' end;');
  15937. Add('var');
  15938. Add(' A: texta;');
  15939. Add(' B: textb;');
  15940. Add(' C: textc;');
  15941. Add('begin');
  15942. Add(' b:=a as textb;');
  15943. Add(' c:=a as textc;');
  15944. Add(' c:=b as textc;');
  15945. ConvertProgram;
  15946. CheckSource('TestExternalClass_Is',
  15947. LinesToStr([ // statements
  15948. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15949. ' this.$init = function () {',
  15950. ' };',
  15951. ' this.$final = function () {',
  15952. ' };',
  15953. '});',
  15954. 'this.A = null;',
  15955. 'this.B = null;',
  15956. 'this.C = null;',
  15957. '']),
  15958. LinesToStr([ // $mod.$main
  15959. '$mod.B = rtl.asExt($mod.A, ExtB);',
  15960. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  15961. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  15962. '']));
  15963. end;
  15964. procedure TTestModule.TestExternalClass_DestructorFail;
  15965. begin
  15966. StartProgram(false);
  15967. Add('{$modeswitch externalclass}');
  15968. Add('type');
  15969. Add(' TExtA = class external name ''ExtA''');
  15970. Add(' destructor Free;');
  15971. Add(' end;');
  15972. SetExpectedPasResolverError('Pascal element not supported: destructor',
  15973. nPasElementNotSupported);
  15974. ConvertProgram;
  15975. end;
  15976. procedure TTestModule.TestExternalClass_New;
  15977. begin
  15978. StartProgram(false);
  15979. Add('{$modeswitch externalclass}');
  15980. Add('type');
  15981. Add(' TExtA = class external name ''ExtA''');
  15982. Add(' constructor New;');
  15983. Add(' constructor New(i: longint; j: longint = 2);');
  15984. Add(' end;');
  15985. Add('var');
  15986. Add(' A: texta;');
  15987. Add('begin');
  15988. Add(' a:=texta.new;');
  15989. Add(' a:=texta(texta.new);');
  15990. Add(' a:=texta.new();');
  15991. Add(' a:=texta.new(1);');
  15992. Add(' with texta do begin');
  15993. Add(' a:=new;');
  15994. Add(' a:=new();');
  15995. Add(' a:=new(2);');
  15996. Add(' end;');
  15997. Add(' a:=test1.texta.new;');
  15998. Add(' a:=test1.texta.new();');
  15999. Add(' a:=test1.texta.new(3);');
  16000. ConvertProgram;
  16001. CheckSource('TestExternalClass_New',
  16002. LinesToStr([ // statements
  16003. 'this.A = null;',
  16004. '']),
  16005. LinesToStr([ // $mod.$main
  16006. '$mod.A = new ExtA();',
  16007. '$mod.A = new ExtA();',
  16008. '$mod.A = new ExtA();',
  16009. '$mod.A = new ExtA(1,2);',
  16010. '$mod.A = new ExtA();',
  16011. '$mod.A = new ExtA();',
  16012. '$mod.A = new ExtA(2,2);',
  16013. '$mod.A = new ExtA();',
  16014. '$mod.A = new ExtA();',
  16015. '$mod.A = new ExtA(3,2);',
  16016. '']));
  16017. end;
  16018. procedure TTestModule.TestExternalClass_ClassOf_New;
  16019. begin
  16020. StartProgram(false);
  16021. Add('{$modeswitch externalclass}');
  16022. Add('type');
  16023. Add(' TExtAClass = class of TExtA;');
  16024. Add(' TExtA = class external name ''ExtA''');
  16025. Add(' C: TExtAClass;');
  16026. Add(' constructor New;');
  16027. Add(' end;');
  16028. Add('var');
  16029. Add(' A: texta;');
  16030. Add(' C: textaclass;');
  16031. Add('begin');
  16032. Add(' a:=c.new;');
  16033. Add(' a:=c.new();');
  16034. Add(' with C do begin');
  16035. Add(' a:=new;');
  16036. Add(' a:=new();');
  16037. Add(' end;');
  16038. Add(' a:=test1.c.new;');
  16039. Add(' a:=test1.c.new();');
  16040. Add(' a:=A.c.new();');
  16041. ConvertProgram;
  16042. CheckSource('TestExternalClass_ClassOf_New',
  16043. LinesToStr([ // statements
  16044. 'this.A = null;',
  16045. 'this.C = null;',
  16046. '']),
  16047. LinesToStr([ // $mod.$main
  16048. '$mod.A = new $mod.C();',
  16049. '$mod.A = new $mod.C();',
  16050. 'var $with1 = $mod.C;',
  16051. '$mod.A = new $with1();',
  16052. '$mod.A = new $with1();',
  16053. '$mod.A = new $mod.C();',
  16054. '$mod.A = new $mod.C();',
  16055. '$mod.A = new $mod.A.C();',
  16056. '']));
  16057. end;
  16058. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  16059. begin
  16060. StartProgram(false);
  16061. Add([
  16062. '{$modeswitch externalclass}',
  16063. 'type',
  16064. ' TExtAClass = class of TExtA;',
  16065. ' TExtA = class external name ''ExtA''',
  16066. ' constructor New;',
  16067. ' end;',
  16068. 'function GetCreator: TExtAClass;',
  16069. 'begin',
  16070. ' Result:=TExtA;',
  16071. 'end;',
  16072. 'var',
  16073. ' A: texta;',
  16074. 'begin',
  16075. ' a:=getcreator.new;',
  16076. ' a:=getcreator().new;',
  16077. ' a:=getcreator().new();',
  16078. ' a:=getcreator.new();',
  16079. ' with getcreator do begin',
  16080. ' a:=new;',
  16081. ' a:=new();',
  16082. ' end;']);
  16083. ConvertProgram;
  16084. CheckSource('TestExternalClass_FuncClassOf_New',
  16085. LinesToStr([ // statements
  16086. 'this.GetCreator = function () {',
  16087. ' var Result = null;',
  16088. ' Result = ExtA;',
  16089. ' return Result;',
  16090. '};',
  16091. 'this.A = null;',
  16092. '']),
  16093. LinesToStr([ // $mod.$main
  16094. '$mod.A = new ($mod.GetCreator())();',
  16095. '$mod.A = new ($mod.GetCreator())();',
  16096. '$mod.A = new ($mod.GetCreator())();',
  16097. '$mod.A = new ($mod.GetCreator())();',
  16098. 'var $with1 = $mod.GetCreator();',
  16099. '$mod.A = new $with1();',
  16100. '$mod.A = new $with1();',
  16101. '']));
  16102. end;
  16103. procedure TTestModule.TestExternalClass_New_PasClassFail;
  16104. begin
  16105. StartProgram(false);
  16106. Add([
  16107. '{$modeswitch externalclass}',
  16108. 'type',
  16109. ' TExtA = class external name ''ExtA''',
  16110. ' constructor New;',
  16111. ' end;',
  16112. ' TBird = class(TExtA)',
  16113. ' end;',
  16114. 'begin',
  16115. ' TBird.new;',
  16116. '']);
  16117. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16118. ConvertProgram;
  16119. end;
  16120. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  16121. begin
  16122. StartProgram(false);
  16123. Add([
  16124. '{$modeswitch externalclass}',
  16125. 'type',
  16126. ' TExtA = class external name ''ExtA''',
  16127. ' constructor New;',
  16128. ' end;',
  16129. ' TBird = class(TExtA)',
  16130. ' end;',
  16131. 'begin',
  16132. ' TBird.new();',
  16133. '']);
  16134. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16135. ConvertProgram;
  16136. end;
  16137. procedure TTestModule.TestExternalClass_LocalConstSameName;
  16138. begin
  16139. StartProgram(false);
  16140. Add('{$modeswitch externalclass}');
  16141. Add('type');
  16142. Add(' TExtA = class external name ''ExtA''');
  16143. Add(' constructor New;');
  16144. Add(' end;');
  16145. Add('function DoIt: longint;');
  16146. Add('const ExtA: longint = 3;');
  16147. Add('begin');
  16148. Add(' Result:=ExtA;');
  16149. Add('end;');
  16150. Add('var');
  16151. Add(' A: texta;');
  16152. Add('begin');
  16153. Add(' a:=texta.new;');
  16154. ConvertProgram;
  16155. CheckSource('TestExternalClass_LocalConstSameName',
  16156. LinesToStr([ // statements
  16157. 'var ExtA$1 = 3;',
  16158. 'this.DoIt = function () {',
  16159. ' var Result = 0;',
  16160. ' Result = ExtA$1;',
  16161. ' return Result;',
  16162. '};',
  16163. 'this.A = null;',
  16164. '']),
  16165. LinesToStr([ // $mod.$main
  16166. '$mod.A = new ExtA();',
  16167. '']));
  16168. end;
  16169. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  16170. begin
  16171. StartProgram(false);
  16172. Add('{$modeswitch externalclass}');
  16173. Add('type');
  16174. Add(' TExtA = class external name ''ExtA''');
  16175. Add(' procedure DoIt;');
  16176. Add(' end;');
  16177. Add(' TMyA = class(TExtA)');
  16178. Add(' procedure DoIt;');
  16179. Add(' end;');
  16180. Add('procedure TMyA.DoIt; begin end;');
  16181. Add('begin');
  16182. ConvertProgram;
  16183. CheckSource('TestExternalClass_ReintroduceOverload',
  16184. LinesToStr([ // statements
  16185. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  16186. ' this.$init = function () {',
  16187. ' };',
  16188. ' this.$final = function () {',
  16189. ' };',
  16190. ' this.DoIt$1 = function () {',
  16191. ' };',
  16192. '});',
  16193. '']),
  16194. LinesToStr([ // $mod.$main
  16195. '']));
  16196. end;
  16197. procedure TTestModule.TestExternalClass_Inherited;
  16198. begin
  16199. StartProgram(false);
  16200. Add('{$modeswitch externalclass}');
  16201. Add('type');
  16202. Add(' TExtA = class external name ''ExtA''');
  16203. Add(' procedure DoIt(i: longint = 1); virtual;');
  16204. Add(' procedure DoSome(j: longint = 2);');
  16205. Add(' end;');
  16206. Add(' TExtB = class external name ''ExtB''(TExtA)');
  16207. Add(' end;');
  16208. Add(' TMyC = class(TExtB)');
  16209. Add(' procedure DoIt(i: longint = 1); override;');
  16210. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  16211. Add(' end;');
  16212. Add('procedure TMyC.DoIt(i: longint);');
  16213. Add('begin');
  16214. Add(' inherited;');
  16215. Add(' inherited DoIt;');
  16216. Add(' inherited DoIt();');
  16217. Add(' inherited DoIt(3);');
  16218. Add(' inherited DoSome;');
  16219. Add(' inherited DoSome();');
  16220. Add(' inherited DoSome(4);');
  16221. Add('end;');
  16222. Add('procedure TMyC.DoSome(j: longint);');
  16223. Add('begin');
  16224. Add(' inherited;');
  16225. Add('end;');
  16226. Add('begin');
  16227. ConvertProgram;
  16228. CheckSource('TestExternalClass_ReintroduceOverload',
  16229. LinesToStr([ // statements
  16230. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  16231. ' this.$init = function () {',
  16232. ' };',
  16233. ' this.$final = function () {',
  16234. ' };',
  16235. ' this.DoIt = function (i) {',
  16236. ' ExtB.DoIt.apply(this, arguments);',
  16237. ' ExtB.DoIt.call(this, 1);',
  16238. ' ExtB.DoIt.call(this, 1);',
  16239. ' ExtB.DoIt.call(this, 3);',
  16240. ' ExtB.DoSome.call(this, 2);',
  16241. ' ExtB.DoSome.call(this, 2);',
  16242. ' ExtB.DoSome.call(this, 4);',
  16243. ' };',
  16244. ' this.DoSome$1 = function (j) {',
  16245. ' ExtB.DoSome.apply(this, arguments);',
  16246. ' };',
  16247. '});',
  16248. '']),
  16249. LinesToStr([ // $mod.$main
  16250. '']));
  16251. end;
  16252. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  16253. begin
  16254. StartProgram(false);
  16255. Add('{$modeswitch externalclass}');
  16256. Add('type');
  16257. Add(' TObject = class');
  16258. Add(' end;');
  16259. Add(' TExtA = class external name ''ExtA''(TObject)');
  16260. Add(' end;');
  16261. Add('begin');
  16262. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  16263. ConvertProgram;
  16264. end;
  16265. procedure TTestModule.TestExternalClass_NewInstance;
  16266. begin
  16267. StartProgram(false);
  16268. Add('{$modeswitch externalclass}');
  16269. Add('type');
  16270. Add(' TExtA = class external name ''ExtA''');
  16271. Add(' end;');
  16272. Add(' TMyB = class(TExtA)');
  16273. Add(' protected');
  16274. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  16275. Add(' end;');
  16276. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16277. Add('begin end;');
  16278. Add('begin');
  16279. ConvertProgram;
  16280. CheckSource('TestExternalClass_NewInstance',
  16281. LinesToStr([ // statements
  16282. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  16283. ' this.$init = function () {',
  16284. ' };',
  16285. ' this.$final = function () {',
  16286. ' };',
  16287. ' this.NewInstance = function (fnname, paramarray) {',
  16288. ' var Result = null;',
  16289. ' return Result;',
  16290. ' };',
  16291. '});',
  16292. '']),
  16293. LinesToStr([ // $mod.$main
  16294. '']));
  16295. end;
  16296. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  16297. begin
  16298. StartProgram(false);
  16299. Add('{$modeswitch externalclass}');
  16300. Add('type');
  16301. Add(' TExtA = class external name ''ExtA''');
  16302. Add(' end;');
  16303. Add(' TMyB = class(TExtA)');
  16304. Add(' protected');
  16305. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  16306. Add(' end;');
  16307. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16308. Add('begin end;');
  16309. Add('begin');
  16310. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  16311. ConvertProgram;
  16312. end;
  16313. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  16314. begin
  16315. StartProgram(false);
  16316. Add('{$modeswitch externalclass}');
  16317. Add('type');
  16318. Add(' TExtA = class external name ''ExtA''');
  16319. Add(' end;');
  16320. Add(' TMyB = class(TExtA)');
  16321. Add(' protected');
  16322. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  16323. Add(' end;');
  16324. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  16325. Add('begin end;');
  16326. Add('begin');
  16327. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  16328. nIncompatibleTypeArgNo);
  16329. ConvertProgram;
  16330. end;
  16331. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  16332. begin
  16333. StartProgram(false);
  16334. Add('{$modeswitch externalclass}');
  16335. Add('type');
  16336. Add(' TExtA = class external name ''ExtA''');
  16337. Add(' end;');
  16338. Add(' TMyB = class(TExtA)');
  16339. Add(' protected');
  16340. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  16341. Add(' end;');
  16342. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  16343. Add('begin end;');
  16344. Add('begin');
  16345. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  16346. nIncompatibleTypeArgNo);
  16347. ConvertProgram;
  16348. end;
  16349. procedure TTestModule.TestExternalClass_PascalProperty;
  16350. begin
  16351. StartProgram(false);
  16352. Add('{$modeswitch externalclass}');
  16353. Add('type');
  16354. Add(' TJSElement = class;');
  16355. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  16356. Add(' TJSElement = class external name ''ExtA''');
  16357. Add(' end;');
  16358. Add(' TControl = class(TJSElement)');
  16359. Add(' private');
  16360. Add(' FOnClick: TJSNotifyEvent;');
  16361. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  16362. Add(' procedure Click(Sender: TJSElement);');
  16363. Add(' end;');
  16364. Add('procedure TControl.Click(Sender: TJSElement);');
  16365. Add('begin');
  16366. Add(' OnClick(Self);');
  16367. Add('end;');
  16368. Add('var');
  16369. Add(' Ctrl: TControl;');
  16370. Add('begin');
  16371. Add(' Ctrl.OnClick:[email protected];');
  16372. Add(' Ctrl.OnClick(Ctrl);');
  16373. ConvertProgram;
  16374. CheckSource('TestExternalClass_PascalProperty',
  16375. LinesToStr([ // statements
  16376. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  16377. ' this.$init = function () {',
  16378. ' this.FOnClick = null;',
  16379. ' };',
  16380. ' this.$final = function () {',
  16381. ' this.FOnClick = undefined;',
  16382. ' };',
  16383. ' this.Click = function (Sender) {',
  16384. ' this.FOnClick(this);',
  16385. ' };',
  16386. '});',
  16387. 'this.Ctrl = null;',
  16388. '']),
  16389. LinesToStr([ // $mod.$main
  16390. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  16391. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  16392. '']));
  16393. end;
  16394. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  16395. begin
  16396. StartProgram(false);
  16397. Add([
  16398. '{$modeswitch externalclass}',
  16399. 'type',
  16400. ' IUnknown = interface end;',
  16401. ' TObject = class',
  16402. ' end;',
  16403. ' TChild = class',
  16404. ' end;',
  16405. ' TExtRootA = class external name ''ExtRootA''',
  16406. ' end;',
  16407. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  16408. ' end;',
  16409. ' TExtRootB = class external name ''ExtRootB''',
  16410. ' end;',
  16411. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  16412. ' end;',
  16413. 'var',
  16414. ' Obj: TObject;',
  16415. ' Child: TChild;',
  16416. ' RootA: TExtRootA;',
  16417. ' ChildA: TExtChildA;',
  16418. ' RootB: TExtRootB;',
  16419. ' ChildB: TExtChildB;',
  16420. ' i: IUnknown;',
  16421. 'begin',
  16422. ' obj:=tobject(roota);',
  16423. ' obj:=tobject(childa);',
  16424. ' child:=tchild(tobject(roota));',
  16425. ' roota:=textroota(obj);',
  16426. ' roota:=textroota(child);',
  16427. ' roota:=textroota(rootb);',
  16428. ' roota:=textroota(childb);',
  16429. ' childa:=textchilda(textroota(obj));',
  16430. ' roota:=TExtRootA(i)',
  16431. '']);
  16432. ConvertProgram;
  16433. CheckSource('TestExternalClass_TypeCastToRootClass',
  16434. LinesToStr([ // statements
  16435. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16436. 'rtl.createClass($mod, "TObject", null, function () {',
  16437. ' this.$init = function () {',
  16438. ' };',
  16439. ' this.$final = function () {',
  16440. ' };',
  16441. '});',
  16442. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16443. '});',
  16444. 'this.Obj = null;',
  16445. 'this.Child = null;',
  16446. 'this.RootA = null;',
  16447. 'this.ChildA = null;',
  16448. 'this.RootB = null;',
  16449. 'this.ChildB = null;',
  16450. 'this.i = null;',
  16451. '']),
  16452. LinesToStr([ // $mod.$main
  16453. '$mod.Obj = $mod.RootA;',
  16454. '$mod.Obj = $mod.ChildA;',
  16455. '$mod.Child = $mod.RootA;',
  16456. '$mod.RootA = $mod.Obj;',
  16457. '$mod.RootA = $mod.Child;',
  16458. '$mod.RootA = $mod.RootB;',
  16459. '$mod.RootA = $mod.ChildB;',
  16460. '$mod.ChildA = $mod.Obj;',
  16461. '$mod.RootA = $mod.i;',
  16462. '']));
  16463. end;
  16464. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  16465. begin
  16466. StartProgram(false);
  16467. Add([
  16468. '{$modeswitch externalclass}',
  16469. 'type',
  16470. ' IUnknown = interface end;',
  16471. ' IBird = interface(IUnknown) end;',
  16472. ' TClass = class of TObject;',
  16473. ' TObject = class',
  16474. ' end;',
  16475. ' TChild = class',
  16476. ' end;',
  16477. ' TJSObject = class external name ''Object''',
  16478. ' end;',
  16479. ' TRec = record end;',
  16480. 'var',
  16481. ' Obj: TObject;',
  16482. ' Child: TChild;',
  16483. ' i: IUnknown;',
  16484. ' Bird: IBird;',
  16485. ' j: TJSObject;',
  16486. ' r: TRec;',
  16487. ' c: TClass;',
  16488. 'begin',
  16489. ' j:=tjsobject(IUnknown);',
  16490. ' j:=tjsobject(IBird);',
  16491. ' j:=tjsobject(TObject);',
  16492. ' j:=tjsobject(TChild);',
  16493. ' j:=tjsobject(TRec);',
  16494. ' j:=tjsobject(Obj);',
  16495. ' j:=tjsobject(Child);',
  16496. ' j:=tjsobject(i);',
  16497. ' j:=tjsobject(Bird);',
  16498. ' j:=tjsobject(r);',
  16499. ' j:=tjsobject(c);',
  16500. '']);
  16501. ConvertProgram;
  16502. CheckSource('TestExternalClass_TypeCastToJSObject',
  16503. LinesToStr([ // statements
  16504. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16505. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  16506. 'rtl.createClass($mod, "TObject", null, function () {',
  16507. ' this.$init = function () {',
  16508. ' };',
  16509. ' this.$final = function () {',
  16510. ' };',
  16511. '});',
  16512. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16513. '});',
  16514. 'rtl.recNewT($mod, "TRec", function () {',
  16515. ' this.$eq = function (b) {',
  16516. ' return true;',
  16517. ' };',
  16518. ' this.$assign = function (s) {',
  16519. ' return this;',
  16520. ' };',
  16521. '});',
  16522. 'this.Obj = null;',
  16523. 'this.Child = null;',
  16524. 'this.i = null;',
  16525. 'this.Bird = null;',
  16526. 'this.j = null;',
  16527. 'this.r = $mod.TRec.$new();',
  16528. 'this.c = null;',
  16529. '']),
  16530. LinesToStr([ // $mod.$main
  16531. '$mod.j = $mod.IUnknown;',
  16532. '$mod.j = $mod.IBird;',
  16533. '$mod.j = $mod.TObject;',
  16534. '$mod.j = $mod.TChild;',
  16535. '$mod.j = $mod.TRec;',
  16536. '$mod.j = $mod.Obj;',
  16537. '$mod.j = $mod.Child;',
  16538. '$mod.j = $mod.i;',
  16539. '$mod.j = $mod.Bird;',
  16540. '$mod.j = $mod.r;',
  16541. '$mod.j = $mod.c;',
  16542. '']));
  16543. end;
  16544. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  16545. begin
  16546. StartProgram(false);
  16547. Add('{$modeswitch externalclass}');
  16548. Add('type');
  16549. Add(' TJSString = class external name ''String''');
  16550. Add(' class function fromCharCode() : string; varargs;');
  16551. Add(' function anchor(const aName : string) : string;');
  16552. Add(' end;');
  16553. Add('var');
  16554. Add(' s: string;');
  16555. Add('begin');
  16556. Add(' s:=TJSString.fromCharCode(65,66);');
  16557. Add(' s:=TJSString(s).anchor(s);');
  16558. Add(' s:=TJSString(''foo'').anchor(s);');
  16559. ConvertProgram;
  16560. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  16561. LinesToStr([ // statements
  16562. 'this.s = "";',
  16563. '']),
  16564. LinesToStr([ // $mod.$main
  16565. '$mod.s = String.fromCharCode(65, 66);',
  16566. '$mod.s = $mod.s.anchor($mod.s);',
  16567. '$mod.s = "foo".anchor($mod.s);',
  16568. '']));
  16569. end;
  16570. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  16571. begin
  16572. StartProgram(false);
  16573. Add([
  16574. '{$modeswitch externalclass}',
  16575. 'type',
  16576. ' TJSObject = class external name ''Object'' end;',
  16577. ' TJSFunction = class external name ''Function''',
  16578. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  16579. ' function call(thisArg: TJSObject): JSValue; varargs;',
  16580. ' end;',
  16581. ' TObject = class',
  16582. ' procedure DoIt(i: longint);',
  16583. ' end;',
  16584. ' TFuncInt = function(o: TObject): longint;',
  16585. 'function GetIt(o: TObject): longint;',
  16586. ' procedure Sub; begin end;',
  16587. 'var',
  16588. ' f: TJSFunction;',
  16589. ' fi: TFuncInt;',
  16590. 'begin',
  16591. ' fi:=TFuncInt(f);',
  16592. ' f:=TJSFunction(fi);',
  16593. ' f:=TJSFunction(@GetIt);',
  16594. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  16595. ' f:=TJSFunction(@Sub);',
  16596. ' f:=TJSFunction(@o.doit);',
  16597. ' f:=TJSFunction(fi).bind(nil,4)',
  16598. 'end;',
  16599. 'procedure TObject.DoIt(i: longint);',
  16600. ' procedure Sub; begin end;',
  16601. 'var f: TJSFunction;',
  16602. 'begin',
  16603. ' f:=TJSFunction(@DoIt);',
  16604. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  16605. ' f:=TJSFunction(@Sub);',
  16606. ' f:=TJSFunction(@GetIt);',
  16607. 'end;',
  16608. 'begin']);
  16609. ConvertProgram;
  16610. CheckSource('TestExternalClass_TypeCastToJSFunction',
  16611. LinesToStr([ // statements
  16612. 'rtl.createClass($mod, "TObject", null, function () {',
  16613. ' this.$init = function () {',
  16614. ' };',
  16615. ' this.$final = function () {',
  16616. ' };',
  16617. ' this.DoIt = function (i) {',
  16618. ' var $Self = this;',
  16619. ' function Sub() {',
  16620. ' };',
  16621. ' var f = null;',
  16622. ' f = rtl.createCallback($Self, "DoIt");',
  16623. ' f = rtl.createCallback($Self, "DoIt").bind(null, 13);',
  16624. ' f = Sub;',
  16625. ' f = $mod.GetIt;',
  16626. ' };',
  16627. '});',
  16628. 'this.GetIt = function (o) {',
  16629. ' var Result = 0;',
  16630. ' function Sub() {',
  16631. ' };',
  16632. ' var f = null;',
  16633. ' var fi = null;',
  16634. ' fi = f;',
  16635. ' f = fi;',
  16636. ' f = $mod.GetIt;',
  16637. ' f = $mod.GetIt.bind(null, 3);',
  16638. ' f = Sub;',
  16639. ' f = rtl.createCallback(o, "DoIt");',
  16640. ' f = fi.bind(null, 4);',
  16641. ' return Result;',
  16642. '};',
  16643. '']),
  16644. LinesToStr([ // $mod.$main
  16645. '']));
  16646. end;
  16647. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  16648. begin
  16649. StartProgram(false);
  16650. Add([
  16651. '{$mode delphi}',
  16652. '{$modeswitch externalclass}',
  16653. 'type',
  16654. ' TJSObject = class external name ''Object'' end;',
  16655. ' TJSWindow = class external name ''Window''(TJSObject)',
  16656. ' procedure Open;',
  16657. ' end;',
  16658. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  16659. ' procedure Execute;',
  16660. ' end;',
  16661. 'procedure Fly;',
  16662. 'var',
  16663. ' w: TJSWindow;',
  16664. ' e: TJSEventTarget;',
  16665. 'begin',
  16666. ' w:=TJSWindow(e);',
  16667. ' e:=TJSEventTarget(w);',
  16668. 'end;',
  16669. 'begin']);
  16670. ConvertProgram;
  16671. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  16672. LinesToStr([ // statements
  16673. 'this.Fly = function () {',
  16674. ' var w = null;',
  16675. ' var e = null;',
  16676. ' w = e;',
  16677. ' e = w;',
  16678. '};',
  16679. '']),
  16680. LinesToStr([ // $mod.$main
  16681. '']));
  16682. end;
  16683. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  16684. begin
  16685. StartProgram(false);
  16686. Add('{$modeswitch externalclass}');
  16687. Add('type');
  16688. Add(' TJSString = class external name ''String''');
  16689. Add(' class function fromCharCode() : string; varargs;');
  16690. Add(' end;');
  16691. Add('var');
  16692. Add(' s: string;');
  16693. Add(' sObj: TJSString;');
  16694. Add('begin');
  16695. Add(' s:=sObj.fromCharCode(65,66);');
  16696. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  16697. nExternalClassInstanceCannotAccessStaticX);
  16698. ConvertProgram;
  16699. end;
  16700. procedure TTestModule.TestExternalClass_BracketAccessor;
  16701. begin
  16702. StartProgram(false);
  16703. Add([
  16704. '{$modeswitch externalclass}',
  16705. 'type',
  16706. ' TJSArray = class external name ''Array2''',
  16707. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16708. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16709. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16710. ' end;',
  16711. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  16712. 'begin end;',
  16713. 'var',
  16714. ' Arr: tjsarray;',
  16715. ' s: string;',
  16716. ' i: longint;',
  16717. ' v: jsvalue;',
  16718. 'begin',
  16719. ' v:=arr[0];',
  16720. ' v:=arr.items[1];',
  16721. ' arr[2]:=s;',
  16722. ' arr.items[3]:=s;',
  16723. ' arr[4]:=i;',
  16724. ' arr[5]:=arr[6];',
  16725. ' arr.items[7]:=arr.items[8];',
  16726. ' with arr do items[9]:=items[10];',
  16727. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  16728. ' with arr do begin',
  16729. ' v:=GetItems(14);',
  16730. ' setitems(15,16);',
  16731. ' end;',
  16732. ' v:=test1.arr.items[17];',
  16733. ' test1.arr.items[18]:=v;',
  16734. '']);
  16735. ConvertProgram;
  16736. CheckSource('TestExternalClass_BracketAccessor',
  16737. LinesToStr([ // statements
  16738. 'this.DoIt = function (vI, vJ, vK, vL) {',
  16739. '};',
  16740. 'this.Arr = null;',
  16741. 'this.s = "";',
  16742. 'this.i = 0;',
  16743. 'this.v = undefined;',
  16744. '']),
  16745. LinesToStr([ // $mod.$main
  16746. '$mod.v = $mod.Arr[0];',
  16747. '$mod.v = $mod.Arr[1];',
  16748. '$mod.Arr[2] = $mod.s;',
  16749. '$mod.Arr[3] = $mod.s;',
  16750. '$mod.Arr[4] = $mod.i;',
  16751. '$mod.Arr[5] = $mod.Arr[6];',
  16752. '$mod.Arr[7] = $mod.Arr[8];',
  16753. 'var $with1 = $mod.Arr;',
  16754. '$with1[9] = $with1[10];',
  16755. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  16756. ' a: 9,',
  16757. ' p: $mod.Arr,',
  16758. ' get: function () {',
  16759. ' return this.p[this.a];',
  16760. ' },',
  16761. ' set: function (v) {',
  16762. ' this.p[this.a] = v;',
  16763. ' }',
  16764. '}, {',
  16765. ' a: 10,',
  16766. ' p: $mod.Arr,',
  16767. ' get: function () {',
  16768. ' return this.p[this.a];',
  16769. ' },',
  16770. ' set: function (v) {',
  16771. ' this.p[this.a] = v;',
  16772. ' }',
  16773. '});',
  16774. 'var $with2 = $mod.Arr;',
  16775. '$mod.v = $with2[14];',
  16776. '$with2[15] = 16;',
  16777. '$mod.v = $mod.Arr[17];',
  16778. '$mod.Arr[18] = $mod.v;',
  16779. '']));
  16780. end;
  16781. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  16782. begin
  16783. StartProgram(false);
  16784. Add([
  16785. '{$modeswitch externalclass}',
  16786. 'type',
  16787. ' TJSArray = class external name ''Array2''',
  16788. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16789. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16790. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16791. ' end;',
  16792. ' TMyArr = class(TJSArray)',
  16793. ' procedure DoIt;',
  16794. ' end;',
  16795. 'procedure tmyarr.DoIt;',
  16796. 'begin',
  16797. ' Items[1]:=Items[2];',
  16798. ' SetItems(3,getItems(4));',
  16799. 'end;',
  16800. 'var',
  16801. ' Arr: tmyarr;',
  16802. ' s: string;',
  16803. ' i: longint;',
  16804. ' v: jsvalue;',
  16805. 'begin',
  16806. ' v:=arr[0];',
  16807. ' v:=arr.items[1];',
  16808. ' arr[2]:=s;',
  16809. ' arr.items[3]:=s;',
  16810. ' arr[4]:=i;',
  16811. ' arr[5]:=arr[6];',
  16812. ' arr.items[7]:=arr.items[8];',
  16813. ' with arr do items[9]:=items[10];',
  16814. ' with arr do begin',
  16815. ' v:=GetItems(14);',
  16816. ' setitems(15,16);',
  16817. ' end;',
  16818. '']);
  16819. ConvertProgram;
  16820. CheckSource('TestExternalClass_BracketAccessor_Call',
  16821. LinesToStr([ // statements
  16822. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  16823. ' this.$init = function () {',
  16824. ' };',
  16825. ' this.$final = function () {',
  16826. ' };',
  16827. ' this.DoIt = function () {',
  16828. ' this[1] = this[2];',
  16829. ' this[3] = this[4];',
  16830. ' };',
  16831. '});',
  16832. 'this.Arr = null;',
  16833. 'this.s = "";',
  16834. 'this.i = 0;',
  16835. 'this.v = undefined;',
  16836. '']),
  16837. LinesToStr([ // $mod.$main
  16838. '$mod.v = $mod.Arr[0];',
  16839. '$mod.v = $mod.Arr[1];',
  16840. '$mod.Arr[2] = $mod.s;',
  16841. '$mod.Arr[3] = $mod.s;',
  16842. '$mod.Arr[4] = $mod.i;',
  16843. '$mod.Arr[5] = $mod.Arr[6];',
  16844. '$mod.Arr[7] = $mod.Arr[8];',
  16845. 'var $with1 = $mod.Arr;',
  16846. '$with1[9] = $with1[10];',
  16847. 'var $with2 = $mod.Arr;',
  16848. '$mod.v = $with2[14];',
  16849. '$with2[15] = 16;',
  16850. '']));
  16851. end;
  16852. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  16853. begin
  16854. StartProgram(false);
  16855. Add('{$modeswitch externalclass}');
  16856. Add('type');
  16857. Add(' TJSArray = class external name ''Array2''');
  16858. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  16859. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  16860. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  16861. Add(' end;');
  16862. Add('begin');
  16863. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  16864. nBracketAccessorOfExternalClassMustHaveOneParameter);
  16865. ConvertProgram;
  16866. end;
  16867. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  16868. begin
  16869. StartProgram(false);
  16870. Add('{$modeswitch externalclass}');
  16871. Add('type');
  16872. Add(' TJSArray = class external name ''Array2''');
  16873. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16874. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  16875. Add(' end;');
  16876. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  16877. Add('begin end;');
  16878. Add('var');
  16879. Add(' Arr: tjsarray;');
  16880. Add(' v: jsvalue;');
  16881. Add('begin');
  16882. Add(' v:=arr[0];');
  16883. Add(' v:=arr.items[1];');
  16884. Add(' with arr do v:=items[2];');
  16885. Add(' doit(arr[3],arr[4]);');
  16886. ConvertProgram;
  16887. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  16888. LinesToStr([ // statements
  16889. 'this.DoIt = function (vI, vJ) {',
  16890. '};',
  16891. 'this.Arr = null;',
  16892. 'this.v = undefined;',
  16893. '']),
  16894. LinesToStr([ // $mod.$main
  16895. '$mod.v = $mod.Arr[0];',
  16896. '$mod.v = $mod.Arr[1];',
  16897. 'var $with1 = $mod.Arr;',
  16898. '$mod.v = $with1[2];',
  16899. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  16900. '']));
  16901. end;
  16902. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  16903. begin
  16904. StartProgram(false);
  16905. Add('{$modeswitch externalclass}');
  16906. Add('type');
  16907. Add(' TJSArray = class external name ''Array2''');
  16908. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16909. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16910. Add(' end;');
  16911. Add('var');
  16912. Add(' Arr: tjsarray;');
  16913. Add(' s: string;');
  16914. Add(' i: longint;');
  16915. Add(' v: jsvalue;');
  16916. Add('begin');
  16917. Add(' arr[2]:=s;');
  16918. Add(' arr.items[3]:=s;');
  16919. Add(' arr[4]:=i;');
  16920. Add(' with arr do items[5]:=i;');
  16921. ConvertProgram;
  16922. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  16923. LinesToStr([ // statements
  16924. 'this.Arr = null;',
  16925. 'this.s = "";',
  16926. 'this.i = 0;',
  16927. 'this.v = undefined;',
  16928. '']),
  16929. LinesToStr([ // $mod.$main
  16930. '$mod.Arr[2] = $mod.s;',
  16931. '$mod.Arr[3] = $mod.s;',
  16932. '$mod.Arr[4] = $mod.i;',
  16933. 'var $with1 = $mod.Arr;',
  16934. '$with1[5] = $mod.i;',
  16935. '']));
  16936. end;
  16937. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  16938. begin
  16939. StartProgram(false);
  16940. Add('{$modeswitch externalclass}');
  16941. Add('type');
  16942. Add(' TJSArray = class external name ''Array2''');
  16943. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16944. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16945. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  16946. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  16947. Add(' end;');
  16948. Add('var');
  16949. Add(' Arr: tjsarray;');
  16950. Add(' s: string;');
  16951. Add(' i: longint;');
  16952. Add(' v: jsvalue;');
  16953. Add('begin');
  16954. Add(' arr[2]:=s;');
  16955. Add(' arr.items[3]:=s;');
  16956. Add(' arr.numbers[4]:=i;');
  16957. Add(' with arr do items[5]:=i;');
  16958. Add(' with arr do numbers[6]:=i;');
  16959. ConvertProgram;
  16960. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  16961. LinesToStr([ // statements
  16962. 'this.Arr = null;',
  16963. 'this.s = "";',
  16964. 'this.i = 0;',
  16965. 'this.v = undefined;',
  16966. '']),
  16967. LinesToStr([ // $mod.$main
  16968. '$mod.Arr[2] = $mod.s;',
  16969. '$mod.Arr[3] = $mod.s;',
  16970. '$mod.Arr[4] = $mod.i;',
  16971. 'var $with1 = $mod.Arr;',
  16972. '$with1[5] = $mod.i;',
  16973. 'var $with2 = $mod.Arr;',
  16974. '$with2[6] = $mod.i;',
  16975. '']));
  16976. end;
  16977. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  16978. begin
  16979. StartProgram(false);
  16980. Add('{$modeswitch externalclass}');
  16981. Add('type');
  16982. Add(' TJSArray = class external name ''Array2''');
  16983. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16984. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16985. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  16986. Add(' end;');
  16987. Add('var');
  16988. Add(' Arr: tjsarray;');
  16989. Add(' i: longint;');
  16990. Add(' IntArr: array of longint;');
  16991. Add(' v: jsvalue;');
  16992. Add('begin');
  16993. Add(' v:=arr.items[i];');
  16994. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  16995. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  16996. ConvertProgram;
  16997. CheckSource('TestExternalClass_BracketAccessor_Index',
  16998. LinesToStr([ // statements
  16999. 'this.Arr = null;',
  17000. 'this.i = 0;',
  17001. 'this.IntArr = [];',
  17002. 'this.v = undefined;',
  17003. '']),
  17004. LinesToStr([ // $mod.$main
  17005. '$mod.v = $mod.Arr[$mod.i];',
  17006. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  17007. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  17008. '']));
  17009. end;
  17010. procedure TTestModule.TestExternalClass_ForInJSObject;
  17011. begin
  17012. StartProgram(false);
  17013. Add([
  17014. '{$modeswitch externalclass}',
  17015. 'type',
  17016. ' TJSObject = class external name ''Object''',
  17017. ' end;',
  17018. 'var',
  17019. ' o: TJSObject;',
  17020. ' key: string;',
  17021. 'begin',
  17022. ' for key in o do',
  17023. ' if key=''abc'' then ;',
  17024. '']);
  17025. ConvertProgram;
  17026. CheckSource('TestExternalClass_ForInJSObject',
  17027. LinesToStr([ // statements
  17028. 'this.o = null;',
  17029. 'this.key = "";',
  17030. '']),
  17031. LinesToStr([ // $mod.$main
  17032. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  17033. '']));
  17034. end;
  17035. procedure TTestModule.TestExternalClass_ForInJSArray;
  17036. begin
  17037. StartProgram(false);
  17038. Add([
  17039. '{$modeswitch externalclass}',
  17040. 'type',
  17041. ' TJSInt8Array = class external name ''Int8Array''',
  17042. ' private',
  17043. ' flength: NativeInt external name ''length'';',
  17044. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  17045. ' public',
  17046. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  17047. ' property Length: NativeInt read flength;',
  17048. ' end;',
  17049. 'var',
  17050. ' a: TJSInt8Array;',
  17051. ' value: shortint;',
  17052. 'begin',
  17053. ' for value in a do',
  17054. ' if value=3 then ;',
  17055. '']);
  17056. ConvertProgram;
  17057. CheckSource('TestExternalClass_ForInJSArray',
  17058. LinesToStr([ // statements
  17059. 'this.a = null;',
  17060. 'this.value = 0;',
  17061. '']),
  17062. LinesToStr([ // $mod.$main
  17063. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  17064. ' $mod.value = $in1[$l2];',
  17065. ' if ($mod.value === 3) ;',
  17066. '};',
  17067. '']));
  17068. end;
  17069. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  17070. begin
  17071. AddModuleWithIntfImplSrc('unit2.pas',
  17072. LinesToStr([
  17073. '{$modeswitch externalclass}',
  17074. 'type',
  17075. ' TJSBufferSource = class external name ''BufferSource''',
  17076. ' end;',
  17077. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  17078. '']),
  17079. '');
  17080. AddModuleWithIntfImplSrc('unit3.pas',
  17081. LinesToStr([
  17082. '{$modeswitch externalclass}',
  17083. 'type',
  17084. ' TJSBufferSource = class external name ''BufferSource''',
  17085. ' end;',
  17086. '']),
  17087. '');
  17088. StartUnit(true);
  17089. Add([
  17090. 'interface',
  17091. 'uses unit2, unit3;',
  17092. 'procedure DoSome(s: TJSBufferSource);',
  17093. 'implementation',
  17094. 'procedure DoSome(s: TJSBufferSource);',
  17095. 'begin',
  17096. ' DoIt(s);',
  17097. 'end;',
  17098. '']);
  17099. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  17100. nIncompatibleTypeArgNo);
  17101. ConvertUnit;
  17102. end;
  17103. procedure TTestModule.TestClassInterface_Corba;
  17104. begin
  17105. StartProgram(false);
  17106. Add([
  17107. '{$interfaces corba}',
  17108. 'type',
  17109. ' IUnknown = interface;',
  17110. ' IUnknown = interface',
  17111. ' [''{00000000-0000-0000-C000-000000000046}'']',
  17112. ' end;',
  17113. ' IInterface = IUnknown;',
  17114. ' IBird = interface(IInterface)',
  17115. ' function GetSize: longint;',
  17116. ' procedure SetSize(i: longint);',
  17117. ' property Size: longint read GetSize write SetSize;',
  17118. ' procedure DoIt(i: longint);',
  17119. ' end;',
  17120. ' TObject = class',
  17121. ' end;',
  17122. ' TBird = class(TObject,IBird)',
  17123. ' function GetSize: longint; virtual; abstract;',
  17124. ' procedure SetSize(i: longint); virtual; abstract;',
  17125. ' procedure DoIt(i: longint); virtual; abstract;',
  17126. ' end;',
  17127. 'var',
  17128. ' BirdIntf: IBird;',
  17129. 'begin',
  17130. ' BirdIntf.Size:=BirdIntf.Size;',
  17131. '']);
  17132. ConvertProgram;
  17133. CheckSource('TestClassInterface_Corba',
  17134. LinesToStr([ // statements
  17135. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  17136. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  17137. 'rtl.createClass($mod, "TObject", null, function () {',
  17138. ' this.$init = function () {',
  17139. ' };',
  17140. ' this.$final = function () {',
  17141. ' };',
  17142. '});',
  17143. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17144. ' rtl.addIntf(this, $mod.IBird);',
  17145. '});',
  17146. 'this.BirdIntf = null;',
  17147. '']),
  17148. LinesToStr([ // $mod.$main
  17149. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  17150. '']));
  17151. end;
  17152. procedure TTestModule.TestClassInterface_ProcExternalFail;
  17153. begin
  17154. StartProgram(false);
  17155. Add([
  17156. '{$interfaces corba}',
  17157. 'type',
  17158. ' IUnknown = interface',
  17159. ' procedure DoIt; external name ''foo'';',
  17160. ' end;',
  17161. 'begin']);
  17162. SetExpectedParserError(
  17163. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  17164. nParserNoFieldsAllowed);
  17165. ConvertProgram;
  17166. end;
  17167. procedure TTestModule.TestClassInterface_Overloads;
  17168. begin
  17169. StartProgram(false);
  17170. Add([
  17171. '{$interfaces corba}',
  17172. 'type',
  17173. ' integer = longint;',
  17174. ' IUnknown = interface',
  17175. ' procedure DoIt(i: integer);',
  17176. ' procedure DoIt(s: string);',
  17177. ' end;',
  17178. ' IBird = interface(IUnknown)',
  17179. ' procedure DoIt(b: boolean); overload;',
  17180. ' end;',
  17181. ' TObject = class',
  17182. ' end;',
  17183. ' TBird = class(TObject,IBird)',
  17184. ' procedure DoIt(o: TObject);',
  17185. ' procedure DoIt(s: string);',
  17186. ' procedure DoIt(i: integer);',
  17187. ' procedure DoIt(b: boolean);',
  17188. ' end;',
  17189. 'procedure TBird.DoIt(o: TObject); begin end;',
  17190. 'procedure TBird.DoIt(s: string); begin end;',
  17191. 'procedure TBird.DoIt(i: integer); begin end;',
  17192. 'procedure TBird.DoIt(b: boolean); begin end;',
  17193. 'var',
  17194. ' BirdIntf: IBird;',
  17195. 'begin',
  17196. ' BirdIntf.DoIt(3);',
  17197. ' BirdIntf.DoIt(''abc'');',
  17198. ' BirdIntf.DoIt(true);',
  17199. '']);
  17200. ConvertProgram;
  17201. CheckSource('TestClassInterface_Overloads',
  17202. LinesToStr([ // statements
  17203. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  17204. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  17205. 'rtl.createClass($mod, "TObject", null, function () {',
  17206. ' this.$init = function () {',
  17207. ' };',
  17208. ' this.$final = function () {',
  17209. ' };',
  17210. '});',
  17211. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17212. ' this.DoIt = function (o) {',
  17213. ' };',
  17214. ' this.DoIt$1 = function (s) {',
  17215. ' };',
  17216. ' this.DoIt$2 = function (i) {',
  17217. ' };',
  17218. ' this.DoIt$3 = function (b) {',
  17219. ' };',
  17220. ' rtl.addIntf(this, $mod.IBird, {',
  17221. ' DoIt$2: "DoIt$3",',
  17222. ' DoIt: "DoIt$2"',
  17223. ' });',
  17224. '});',
  17225. 'this.BirdIntf = null;',
  17226. '']),
  17227. LinesToStr([ // $mod.$main
  17228. '$mod.BirdIntf.DoIt(3);',
  17229. '$mod.BirdIntf.DoIt$1("abc");',
  17230. '$mod.BirdIntf.DoIt$2(true);',
  17231. '']));
  17232. end;
  17233. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  17234. begin
  17235. StartProgram(false);
  17236. Add([
  17237. '{$interfaces corba}',
  17238. 'type',
  17239. ' IBird = interface',
  17240. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17241. ' end;',
  17242. ' IDog = interface',
  17243. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17244. ' end;',
  17245. ' TObject = class(IBird,IDog)',
  17246. ' end;',
  17247. 'begin']);
  17248. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  17249. nDuplicateGUIDXInYZ);
  17250. ConvertProgram;
  17251. end;
  17252. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  17253. begin
  17254. StartProgram(false);
  17255. Add([
  17256. '{$interfaces corba}',
  17257. 'type',
  17258. ' IAnimal = interface',
  17259. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17260. ' end;',
  17261. ' IBird = interface(IAnimal)',
  17262. ' end;',
  17263. ' IHawk = interface(IBird)',
  17264. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17265. ' end;',
  17266. 'begin']);
  17267. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  17268. nDuplicateGUIDXInYZ);
  17269. ConvertProgram;
  17270. end;
  17271. procedure TTestModule.TestClassInterface_AncestorImpl;
  17272. begin
  17273. StartProgram(false);
  17274. Add([
  17275. '{$interfaces corba}',
  17276. 'type',
  17277. ' integer = longint;',
  17278. ' IUnknown = interface',
  17279. ' procedure DoIt(i: integer);',
  17280. ' end;',
  17281. ' IBird = interface',
  17282. ' procedure Fly(i: integer);',
  17283. ' end;',
  17284. ' TObject = class(IUnknown)',
  17285. ' procedure DoIt(i: integer);',
  17286. ' end;',
  17287. ' TBird = class(IBird)',
  17288. ' procedure Fly(i: integer);',
  17289. ' end;',
  17290. 'procedure TObject.DoIt(i: integer); begin end;',
  17291. 'procedure TBird.Fly(i: integer); begin end;',
  17292. 'begin',
  17293. '']);
  17294. ConvertProgram;
  17295. CheckSource('TestClassInterface_AncestorIntf',
  17296. LinesToStr([ // statements
  17297. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  17298. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  17299. 'rtl.createClass($mod, "TObject", null, function () {',
  17300. ' this.$init = function () {',
  17301. ' };',
  17302. ' this.$final = function () {',
  17303. ' };',
  17304. ' this.DoIt = function (i) {',
  17305. ' };',
  17306. ' rtl.addIntf(this, $mod.IUnknown);',
  17307. '});',
  17308. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17309. ' this.Fly = function (i) {',
  17310. ' };',
  17311. ' rtl.addIntf(this, $mod.IBird);',
  17312. ' rtl.addIntf(this, $mod.IUnknown);',
  17313. '});',
  17314. '']),
  17315. LinesToStr([ // $mod.$main
  17316. '']));
  17317. end;
  17318. procedure TTestModule.TestClassInterface_ImplReintroduce;
  17319. begin
  17320. StartProgram(false);
  17321. Add([
  17322. '{$interfaces corba}',
  17323. 'type',
  17324. ' integer = longint;',
  17325. ' IBird = interface',
  17326. ' procedure DoIt(i: integer);',
  17327. ' end;',
  17328. ' TObject = class',
  17329. ' procedure DoIt(i: integer);',
  17330. ' end;',
  17331. ' TBird = class(IBird)',
  17332. ' procedure DoIt(i: integer); virtual; reintroduce;',
  17333. ' end;',
  17334. 'procedure TObject.DoIt(i: integer); begin end;',
  17335. 'procedure TBird.DoIt(i: integer); begin end;',
  17336. 'begin',
  17337. '']);
  17338. ConvertProgram;
  17339. CheckSource('TestClassInterface_ImplReintroduce',
  17340. LinesToStr([ // statements
  17341. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  17342. 'rtl.createClass($mod, "TObject", null, function () {',
  17343. ' this.$init = function () {',
  17344. ' };',
  17345. ' this.$final = function () {',
  17346. ' };',
  17347. ' this.DoIt = function (i) {',
  17348. ' };',
  17349. '});',
  17350. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17351. ' this.DoIt$1 = function (i) {',
  17352. ' };',
  17353. ' rtl.addIntf(this, $mod.IBird, {',
  17354. ' DoIt: "DoIt$1"',
  17355. ' });',
  17356. '});',
  17357. '']),
  17358. LinesToStr([ // $mod.$main
  17359. '']));
  17360. end;
  17361. procedure TTestModule.TestClassInterface_MethodResolution;
  17362. begin
  17363. StartProgram(false);
  17364. Add([
  17365. '{$interfaces corba}',
  17366. 'type',
  17367. ' IUnknown = interface',
  17368. ' procedure Walk(i: longint);',
  17369. ' end;',
  17370. ' IBird = interface(IUnknown)',
  17371. ' procedure Walk(b: boolean); overload;',
  17372. ' procedure Fly(s: string);',
  17373. ' end;',
  17374. ' TObject = class',
  17375. ' end;',
  17376. ' TBird = class(TObject,IBird)',
  17377. ' procedure IBird.Fly = Move;',
  17378. ' procedure IBird.Walk = Hop;',
  17379. ' procedure Hop(i: longint);',
  17380. ' procedure Move(s: string);',
  17381. ' procedure Hop(b: boolean);',
  17382. ' end;',
  17383. 'procedure TBird.Move(s: string); begin end;',
  17384. 'procedure TBird.Hop(i: longint); begin end;',
  17385. 'procedure TBird.Hop(b: boolean); begin end;',
  17386. 'var',
  17387. ' BirdIntf: IBird;',
  17388. 'begin',
  17389. ' BirdIntf.Walk(3);',
  17390. ' BirdIntf.Walk(true);',
  17391. ' BirdIntf.Fly(''abc'');',
  17392. '']);
  17393. ConvertProgram;
  17394. CheckSource('TestClassInterface_MethodResolution',
  17395. LinesToStr([ // statements
  17396. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  17397. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  17398. 'rtl.createClass($mod, "TObject", null, function () {',
  17399. ' this.$init = function () {',
  17400. ' };',
  17401. ' this.$final = function () {',
  17402. ' };',
  17403. '});',
  17404. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17405. ' this.Hop = function (i) {',
  17406. ' };',
  17407. ' this.Move = function (s) {',
  17408. ' };',
  17409. ' this.Hop$1 = function (b) {',
  17410. ' };',
  17411. ' rtl.addIntf(this, $mod.IBird, {',
  17412. ' Walk$1: "Hop$1",',
  17413. ' Fly: "Move",',
  17414. ' Walk: "Hop"',
  17415. ' });',
  17416. '});',
  17417. 'this.BirdIntf = null;',
  17418. '']),
  17419. LinesToStr([ // $mod.$main
  17420. '$mod.BirdIntf.Walk(3);',
  17421. '$mod.BirdIntf.Walk$1(true);',
  17422. '$mod.BirdIntf.Fly("abc");',
  17423. '']));
  17424. end;
  17425. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  17426. begin
  17427. StartProgram(false);
  17428. Add([
  17429. '{$interfaces com}',
  17430. 'type',
  17431. ' IUnknown = interface',
  17432. ' function _AddRef: longint;',
  17433. ' procedure Walk;',
  17434. ' end;',
  17435. ' IBird = interface end;',
  17436. ' IDog = interface end;',
  17437. ' TObject = class(IBird,IDog)',
  17438. ' function _AddRef: longint; virtual; abstract;',
  17439. ' procedure Walk; virtual; abstract;',
  17440. ' end;',
  17441. ' TBird = class(IUnknown)',
  17442. ' end;',
  17443. 'begin',
  17444. '']);
  17445. ConvertProgram;
  17446. CheckSource('TestClassInterface_COM_AncestorLess',
  17447. LinesToStr([ // statements
  17448. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  17449. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  17450. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  17451. 'rtl.createClass($mod, "TObject", null, function () {',
  17452. ' this.$init = function () {',
  17453. ' };',
  17454. ' this.$final = function () {',
  17455. ' };',
  17456. ' rtl.addIntf(this, $mod.IBird);',
  17457. ' rtl.addIntf(this, $mod.IDog);',
  17458. '});',
  17459. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17460. ' rtl.addIntf(this, $mod.IUnknown);',
  17461. ' rtl.addIntf(this, $mod.IBird);',
  17462. ' rtl.addIntf(this, $mod.IDog);',
  17463. '});',
  17464. '']),
  17465. LinesToStr([ // $mod.$main
  17466. '']));
  17467. end;
  17468. procedure TTestModule.TestClassInterface_MethodOverride;
  17469. begin
  17470. StartProgram(false);
  17471. Add([
  17472. '{$interfaces corba}',
  17473. 'type',
  17474. ' IUnknown = interface',
  17475. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  17476. ' procedure Go;',
  17477. ' end;',
  17478. ' TObject = class(IUnknown)',
  17479. ' procedure Go; virtual; abstract;',
  17480. ' end;',
  17481. ' TBird = class',
  17482. ' procedure Go; override;',
  17483. ' end;',
  17484. ' TCat = class(TObject)',
  17485. ' procedure Go; override;',
  17486. ' end;',
  17487. ' TDog = class(TObject, IUnknown)',
  17488. ' procedure Go; override;',
  17489. ' end;',
  17490. 'procedure TBird.Go; begin end;',
  17491. 'procedure TCat.Go; begin end;',
  17492. 'procedure TDog.Go; begin end;',
  17493. 'begin',
  17494. '']);
  17495. ConvertProgram;
  17496. CheckSource('TestClassInterface_MethodOverride',
  17497. LinesToStr([ // statements
  17498. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  17499. 'rtl.createClass($mod, "TObject", null, function () {',
  17500. ' this.$init = function () {',
  17501. ' };',
  17502. ' this.$final = function () {',
  17503. ' };',
  17504. ' rtl.addIntf(this, $mod.IUnknown);',
  17505. '});',
  17506. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17507. ' this.Go = function () {',
  17508. ' };',
  17509. ' rtl.addIntf(this, $mod.IUnknown);',
  17510. '});',
  17511. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  17512. ' this.Go = function () {',
  17513. ' };',
  17514. ' rtl.addIntf(this, $mod.IUnknown);',
  17515. '});',
  17516. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  17517. ' this.Go = function () {',
  17518. ' };',
  17519. ' rtl.addIntf(this, $mod.IUnknown);',
  17520. '});',
  17521. '']),
  17522. LinesToStr([ // $mod.$main
  17523. '']));
  17524. end;
  17525. procedure TTestModule.TestClassInterface_Corba_Delegation;
  17526. begin
  17527. StartProgram(false);
  17528. Add([
  17529. '{$interfaces corba}',
  17530. 'type',
  17531. ' IUnknown = interface',
  17532. ' end;',
  17533. ' IBird = interface(IUnknown)',
  17534. ' procedure Fly(s: string);',
  17535. ' end;',
  17536. ' IEagle = interface(IBird)',
  17537. ' end;',
  17538. ' IDove = interface(IBird)',
  17539. ' end;',
  17540. ' ISwallow = interface(IBird)',
  17541. ' end;',
  17542. ' TObject = class',
  17543. ' end;',
  17544. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17545. ' procedure Fly(s: string); virtual; abstract;',
  17546. ' end;',
  17547. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17548. ' FBirdIntf: IBird;',
  17549. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  17550. ' function GetEagleIntf: IEagle; virtual; abstract;',
  17551. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17552. ' FDoveObj: TBird;',
  17553. ' property DoveObj: TBird read FDoveObj implements IDove;',
  17554. ' function GetSwallowObj: TBird; virtual; abstract;',
  17555. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17556. ' end;',
  17557. 'begin',
  17558. '']);
  17559. ConvertProgram;
  17560. CheckSource('TestClassInterface_Delegation',
  17561. LinesToStr([ // statements
  17562. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17563. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17564. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17565. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17566. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17567. 'rtl.createClass($mod, "TObject", null, function () {',
  17568. ' this.$init = function () {',
  17569. ' };',
  17570. ' this.$final = function () {',
  17571. ' };',
  17572. '});',
  17573. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17574. ' rtl.addIntf(this, $mod.IBird);',
  17575. ' rtl.addIntf(this, $mod.IEagle);',
  17576. ' rtl.addIntf(this, $mod.IDove);',
  17577. ' rtl.addIntf(this, $mod.ISwallow);',
  17578. '});',
  17579. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17580. ' this.$init = function () {',
  17581. ' $mod.TObject.$init.call(this);',
  17582. ' this.FBirdIntf = null;',
  17583. ' this.FDoveObj = null;',
  17584. ' };',
  17585. ' this.$final = function () {',
  17586. ' this.FBirdIntf = undefined;',
  17587. ' this.FDoveObj = undefined;',
  17588. ' $mod.TObject.$final.call(this);',
  17589. ' };',
  17590. ' this.$intfmaps = {',
  17591. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17592. ' return this.FBirdIntf;',
  17593. ' },',
  17594. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17595. ' return this.GetEagleIntf();',
  17596. ' },',
  17597. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17598. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17599. ' },',
  17600. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17601. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17602. ' }',
  17603. ' };',
  17604. '});',
  17605. '']),
  17606. LinesToStr([ // $mod.$main
  17607. '']));
  17608. end;
  17609. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  17610. begin
  17611. StartProgram(false);
  17612. Add([
  17613. '{$interfaces corba}',
  17614. 'type',
  17615. ' IUnknown = interface',
  17616. ' end;',
  17617. ' IBird = interface(IUnknown)',
  17618. ' procedure Fly(s: string);',
  17619. ' end;',
  17620. ' IEagle = interface(IBird)',
  17621. ' end;',
  17622. ' IDove = interface(IBird)',
  17623. ' end;',
  17624. ' ISwallow = interface(IBird)',
  17625. ' end;',
  17626. ' TObject = class',
  17627. ' end;',
  17628. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17629. ' procedure Fly(s: string); virtual; abstract;',
  17630. ' end;',
  17631. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17632. ' private',
  17633. ' class var FBirdIntf: IBird;',
  17634. ' class var FDoveObj: TBird;',
  17635. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  17636. ' class function GetSwallowObj: TBird; virtual; abstract;',
  17637. ' protected',
  17638. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  17639. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17640. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  17641. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17642. ' end;',
  17643. 'begin',
  17644. '']);
  17645. ConvertProgram;
  17646. CheckSource('TestClassInterface_DelegationStatic',
  17647. LinesToStr([ // statements
  17648. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17649. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17650. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17651. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17652. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17653. 'rtl.createClass($mod, "TObject", null, function () {',
  17654. ' this.$init = function () {',
  17655. ' };',
  17656. ' this.$final = function () {',
  17657. ' };',
  17658. '});',
  17659. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17660. ' rtl.addIntf(this, $mod.IBird);',
  17661. ' rtl.addIntf(this, $mod.IEagle);',
  17662. ' rtl.addIntf(this, $mod.IDove);',
  17663. ' rtl.addIntf(this, $mod.ISwallow);',
  17664. '});',
  17665. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17666. ' this.FBirdIntf = null;',
  17667. ' this.FDoveObj = null;',
  17668. ' this.$intfmaps = {',
  17669. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17670. ' return this.FBirdIntf;',
  17671. ' },',
  17672. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17673. ' return this.$class.GetEagleIntf();',
  17674. ' },',
  17675. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17676. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17677. ' },',
  17678. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17679. ' return rtl.getIntfT(this.$class.GetSwallowObj(), $mod.ISwallow);',
  17680. ' }',
  17681. ' };',
  17682. '});',
  17683. '']),
  17684. LinesToStr([ // $mod.$main
  17685. '']));
  17686. end;
  17687. procedure TTestModule.TestClassInterface_Corba_Operators;
  17688. begin
  17689. StartProgram(false);
  17690. Add([
  17691. '{$interfaces corba}',
  17692. 'type',
  17693. ' IUnknown = interface',
  17694. ' end;',
  17695. ' IBird = interface(IUnknown)',
  17696. ' function GetItems(Index: longint): longint;',
  17697. ' procedure SetItems(Index: longint; Value: longint);',
  17698. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  17699. ' end;',
  17700. ' TObject = class',
  17701. ' end;',
  17702. ' TBird = class(TObject,IBird)',
  17703. ' function GetItems(Index: longint): longint; virtual; abstract;',
  17704. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  17705. ' end;',
  17706. 'var',
  17707. ' IntfVar: IBird = nil;',
  17708. ' IntfVar2: IBird;',
  17709. ' ObjVar: TBird;',
  17710. ' v: JSValue;',
  17711. 'begin',
  17712. ' IntfVar:=nil;',
  17713. ' IntfVar[3]:=IntfVar[4];',
  17714. ' if Assigned(IntfVar) then ;',
  17715. ' IntfVar:=IntfVar2;',
  17716. ' IntfVar:=ObjVar;',
  17717. ' if IntfVar=IntfVar2 then ;',
  17718. ' if IntfVar<>IntfVar2 then ;',
  17719. ' if IntfVar is IBird then ;',
  17720. ' if IntfVar is TBird then ;',
  17721. ' if ObjVar is IBird then ;',
  17722. ' IntfVar:=IntfVar2 as IBird;',
  17723. ' ObjVar:=IntfVar2 as TBird;',
  17724. ' IntfVar:=ObjVar as IBird;',
  17725. ' IntfVar:=IBird(IntfVar2);',
  17726. ' ObjVar:=TBird(IntfVar);',
  17727. ' IntfVar:=IBird(ObjVar);',
  17728. ' v:=IntfVar;',
  17729. ' IntfVar:=IBird(v);',
  17730. ' if v is IBird then ;',
  17731. ' v:=JSValue(IntfVar);',
  17732. ' v:=IBird;',
  17733. '']);
  17734. ConvertProgram;
  17735. CheckSource('TestClassInterface_Corba_Operators',
  17736. LinesToStr([ // statements
  17737. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17738. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  17739. 'rtl.createClass($mod, "TObject", null, function () {',
  17740. ' this.$init = function () {',
  17741. ' };',
  17742. ' this.$final = function () {',
  17743. ' };',
  17744. '});',
  17745. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17746. ' rtl.addIntf(this, $mod.IBird);',
  17747. '});',
  17748. 'this.IntfVar = null;',
  17749. 'this.IntfVar2 = null;',
  17750. 'this.ObjVar = null;',
  17751. 'this.v = undefined;',
  17752. '']),
  17753. LinesToStr([ // $mod.$main
  17754. '$mod.IntfVar = null;',
  17755. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  17756. 'if ($mod.IntfVar != null) ;',
  17757. '$mod.IntfVar = $mod.IntfVar2;',
  17758. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  17759. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  17760. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  17761. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  17762. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  17763. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  17764. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  17765. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  17766. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17767. '$mod.IntfVar = $mod.IntfVar2;',
  17768. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  17769. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17770. '$mod.v = $mod.IntfVar;',
  17771. '$mod.IntfVar = rtl.getObject($mod.v);',
  17772. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  17773. '$mod.v = $mod.IntfVar;',
  17774. '$mod.v = $mod.IBird;',
  17775. '']));
  17776. end;
  17777. procedure TTestModule.TestClassInterface_Corba_Args;
  17778. begin
  17779. StartProgram(false);
  17780. Add([
  17781. '{$interfaces corba}',
  17782. 'type',
  17783. ' IUnknown = interface',
  17784. ' end;',
  17785. ' IBird = interface(IUnknown)',
  17786. ' end;',
  17787. ' TObject = class',
  17788. ' end;',
  17789. ' TBird = class(TObject,IBird)',
  17790. ' end;',
  17791. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  17792. 'begin',
  17793. ' DoIt(i,i,i);',
  17794. 'end;',
  17795. 'procedure Change(var i: IBird; out j: IBird);',
  17796. 'begin',
  17797. ' DoIt(i,i,i);',
  17798. ' Change(i,i);',
  17799. 'end;',
  17800. 'var',
  17801. ' i: IBird;',
  17802. ' o: TBird;',
  17803. 'begin',
  17804. ' DoIt(i,i,i);',
  17805. ' Change(i,i);',
  17806. ' DoIt(o,o,o);',
  17807. '']);
  17808. ConvertProgram;
  17809. CheckSource('TestClassInterface_Corba_Args',
  17810. LinesToStr([ // statements
  17811. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17812. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  17813. 'rtl.createClass($mod, "TObject", null, function () {',
  17814. ' this.$init = function () {',
  17815. ' };',
  17816. ' this.$final = function () {',
  17817. ' };',
  17818. '});',
  17819. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17820. ' rtl.addIntf(this, $mod.IBird);',
  17821. '});',
  17822. 'this.DoIt = function (u, i, j) {',
  17823. ' $mod.DoIt({',
  17824. ' get: function () {',
  17825. ' return i;',
  17826. ' },',
  17827. ' set: function (v) {',
  17828. ' i = v;',
  17829. ' }',
  17830. ' }, i, i);',
  17831. '};',
  17832. 'this.Change = function (i, j) {',
  17833. ' $mod.DoIt(i, i.get(), i.get());',
  17834. ' $mod.Change(i, i);',
  17835. '};',
  17836. 'this.i = null;',
  17837. 'this.o = null;',
  17838. '']),
  17839. LinesToStr([ // $mod.$main
  17840. '$mod.DoIt({',
  17841. ' p: $mod,',
  17842. ' get: function () {',
  17843. ' return this.p.i;',
  17844. ' },',
  17845. ' set: function (v) {',
  17846. ' this.p.i = v;',
  17847. ' }',
  17848. '}, $mod.i, $mod.i);',
  17849. '$mod.Change({',
  17850. ' p: $mod,',
  17851. ' get: function () {',
  17852. ' return this.p.i;',
  17853. ' },',
  17854. ' set: function (v) {',
  17855. ' this.p.i = v;',
  17856. ' }',
  17857. '}, {',
  17858. ' p: $mod,',
  17859. ' get: function () {',
  17860. ' return this.p.i;',
  17861. ' },',
  17862. ' set: function (v) {',
  17863. ' this.p.i = v;',
  17864. ' }',
  17865. '});',
  17866. '$mod.DoIt({',
  17867. ' p: $mod,',
  17868. ' get: function () {',
  17869. ' return this.p.o;',
  17870. ' },',
  17871. ' set: function (v) {',
  17872. ' this.p.o = v;',
  17873. ' }',
  17874. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  17875. '']));
  17876. end;
  17877. procedure TTestModule.TestClassInterface_Corba_ForIn;
  17878. begin
  17879. StartProgram(false);
  17880. Add([
  17881. '{$interfaces corba}',
  17882. 'type',
  17883. ' IUnknown = interface end;',
  17884. ' TObject = class',
  17885. ' Id: longint;',
  17886. ' end;',
  17887. ' IEnumerator = interface(IUnknown)',
  17888. ' function GetCurrent: TObject;',
  17889. ' function MoveNext: Boolean;',
  17890. ' property Current: TObject read GetCurrent;',
  17891. ' end;',
  17892. ' IEnumerable = interface(IUnknown)',
  17893. ' function GetEnumerator: IEnumerator;',
  17894. ' end;',
  17895. 'var',
  17896. ' o: TObject;',
  17897. ' i: IEnumerable;',
  17898. 'begin',
  17899. ' for o in i do o.Id:=3;',
  17900. '']);
  17901. ConvertProgram;
  17902. CheckSource('TestClassInterface_Corba_ForIn',
  17903. LinesToStr([ // statements
  17904. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17905. 'rtl.createClass($mod, "TObject", null, function () {',
  17906. ' this.$init = function () {',
  17907. ' this.Id = 0;',
  17908. ' };',
  17909. ' this.$final = function () {',
  17910. ' };',
  17911. '});',
  17912. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17913. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17914. 'this.o = null;',
  17915. 'this.i = null;',
  17916. '']),
  17917. LinesToStr([ // $mod.$main
  17918. 'var $in1 = $mod.i.GetEnumerator();',
  17919. 'while ($in1.MoveNext()) {',
  17920. ' $mod.o = $in1.GetCurrent();',
  17921. ' $mod.o.Id = 3;',
  17922. '};',
  17923. '']));
  17924. end;
  17925. procedure TTestModule.TestClassInterface_COM_AssignVar;
  17926. begin
  17927. StartProgram(false);
  17928. Add([
  17929. '{$interfaces com}',
  17930. 'type',
  17931. ' IUnknown = interface',
  17932. ' function _AddRef: longint;',
  17933. ' function _Release: longint;',
  17934. ' end;',
  17935. ' TObject = class(IUnknown)',
  17936. ' function _AddRef: longint; virtual; abstract;',
  17937. ' function _Release: longint; virtual; abstract;',
  17938. ' end;',
  17939. 'var',
  17940. ' i: IUnknown;',
  17941. 'procedure DoGlobal(o: TObject);',
  17942. 'begin',
  17943. ' i:=nil;',
  17944. ' i:=o;',
  17945. ' i:=i;',
  17946. 'end;',
  17947. 'procedure DoLocal(o: TObject);',
  17948. 'const k: IUnknown = nil;',
  17949. 'var j: IUnknown;',
  17950. 'begin',
  17951. ' k:=o;',
  17952. ' k:=i;',
  17953. ' j:=o;',
  17954. ' j:=i;',
  17955. 'end;',
  17956. 'var o: TObject;',
  17957. 'begin',
  17958. ' i:=nil;',
  17959. ' i:=o;',
  17960. '']);
  17961. ConvertProgram;
  17962. CheckSource('TestClassInterface_COM_AssignVar',
  17963. LinesToStr([ // statements
  17964. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17965. 'rtl.createClass($mod, "TObject", null, function () {',
  17966. ' this.$init = function () {',
  17967. ' };',
  17968. ' this.$final = function () {',
  17969. ' };',
  17970. ' rtl.addIntf(this, $mod.IUnknown);',
  17971. '});',
  17972. 'this.i = null;',
  17973. 'this.DoGlobal = function (o) {',
  17974. ' rtl.setIntfP($mod, "i", null);',
  17975. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  17976. ' rtl.setIntfP($mod, "i", $mod.i);',
  17977. '};',
  17978. 'var k = null;',
  17979. 'this.DoLocal = function (o) {',
  17980. ' var j = null;',
  17981. ' try{',
  17982. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17983. ' k = rtl.setIntfL(k, $mod.i);',
  17984. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17985. ' j = rtl.setIntfL(j, $mod.i);',
  17986. ' }finally{',
  17987. ' rtl._Release(j);',
  17988. ' };',
  17989. '};',
  17990. 'this.o = null;',
  17991. '']),
  17992. LinesToStr([ // $mod.$main
  17993. 'rtl.setIntfP($mod, "i", null);',
  17994. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  17995. '']));
  17996. end;
  17997. procedure TTestModule.TestClassInterface_COM_AssignArg;
  17998. begin
  17999. StartProgram(false);
  18000. Add([
  18001. '{$interfaces com}',
  18002. 'type',
  18003. ' IUnknown = interface',
  18004. ' function _AddRef: longint;',
  18005. ' function _Release: longint;',
  18006. ' end;',
  18007. ' TObject = class(IUnknown)',
  18008. ' function _AddRef: longint; virtual; abstract;',
  18009. ' function _Release: longint; virtual; abstract;',
  18010. ' end;',
  18011. 'procedure DoDefault(i, j: IUnknown);',
  18012. 'begin',
  18013. ' i:=nil;',
  18014. ' i:=j;',
  18015. 'end;',
  18016. 'begin',
  18017. '']);
  18018. ConvertProgram;
  18019. CheckSource('TestClassInterface_COM_AssignArg',
  18020. LinesToStr([ // statements
  18021. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18022. 'rtl.createClass($mod, "TObject", null, function () {',
  18023. ' this.$init = function () {',
  18024. ' };',
  18025. ' this.$final = function () {',
  18026. ' };',
  18027. ' rtl.addIntf(this, $mod.IUnknown);',
  18028. '});',
  18029. 'this.DoDefault = function (i, j) {',
  18030. ' rtl._AddRef(i);',
  18031. ' try {',
  18032. ' i = rtl.setIntfL(i, null);',
  18033. ' i = rtl.setIntfL(i, j);',
  18034. ' } finally {',
  18035. ' rtl._Release(i);',
  18036. ' };',
  18037. '};',
  18038. '']),
  18039. LinesToStr([ // $mod.$main
  18040. '']));
  18041. end;
  18042. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  18043. begin
  18044. StartProgram(false);
  18045. Add([
  18046. '{$interfaces com}',
  18047. 'type',
  18048. ' IUnknown = interface',
  18049. ' function _AddRef: longint;',
  18050. ' function _Release: longint;',
  18051. ' end;',
  18052. ' TObject = class(IUnknown)',
  18053. ' function _AddRef: longint; virtual; abstract;',
  18054. ' function _Release: longint; virtual; abstract;',
  18055. ' end;',
  18056. 'function DoDefault(i: IUnknown): IUnknown;',
  18057. 'begin',
  18058. ' Result:=i;',
  18059. ' if Result<>nil then exit;',
  18060. 'end;',
  18061. 'begin',
  18062. '']);
  18063. ConvertProgram;
  18064. CheckSource('TestClassInterface_COM_FunctionResult',
  18065. LinesToStr([ // statements
  18066. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18067. 'rtl.createClass($mod, "TObject", null, function () {',
  18068. ' this.$init = function () {',
  18069. ' };',
  18070. ' this.$final = function () {',
  18071. ' };',
  18072. ' rtl.addIntf(this, $mod.IUnknown);',
  18073. '});',
  18074. 'this.DoDefault = function (i) {',
  18075. ' var Result = null;',
  18076. ' var $ok = false;',
  18077. ' try {',
  18078. ' Result = rtl.setIntfL(Result, i);',
  18079. ' if(Result !== null){',
  18080. ' $ok = true;',
  18081. ' return Result;',
  18082. ' };',
  18083. ' $ok = true;',
  18084. ' } finally {',
  18085. ' if(!$ok) rtl._Release(Result);',
  18086. ' };',
  18087. ' return Result;',
  18088. '};',
  18089. '']),
  18090. LinesToStr([ // $mod.$main
  18091. '']));
  18092. end;
  18093. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  18094. begin
  18095. StartProgram(false);
  18096. Add([
  18097. '{$interfaces com}',
  18098. 'type',
  18099. ' IUnknown = interface',
  18100. ' function _AddRef: longint;',
  18101. ' function _Release: longint;',
  18102. ' end;',
  18103. ' TObject = class(IUnknown)',
  18104. ' function _AddRef: longint; virtual; abstract;',
  18105. ' function _Release: longint; virtual; abstract;',
  18106. ' function GetIntf: IUnknown; virtual;',
  18107. ' end;',
  18108. ' TMouse = class',
  18109. ' function GetIntf: IUnknown; override;',
  18110. ' end;',
  18111. 'function TObject.GetIntf: IUnknown; begin end;',
  18112. 'function TMouse.GetIntf: IUnknown;',
  18113. 'var i: IUnknown;',
  18114. 'begin',
  18115. ' inherited;',
  18116. ' inherited GetIntf;',
  18117. ' inherited GetIntf();',
  18118. ' Result:=inherited GetIntf;',
  18119. ' Result:=inherited GetIntf();',
  18120. ' i:=inherited GetIntf;',
  18121. ' i:=inherited GetIntf();',
  18122. 'end;',
  18123. 'begin',
  18124. '']);
  18125. ConvertProgram;
  18126. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  18127. LinesToStr([ // statements
  18128. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18129. 'rtl.createClass($mod, "TObject", null, function () {',
  18130. ' this.$init = function () {',
  18131. ' };',
  18132. ' this.$final = function () {',
  18133. ' };',
  18134. ' this.GetIntf = function () {',
  18135. ' var Result = null;',
  18136. ' return Result;',
  18137. ' };',
  18138. ' rtl.addIntf(this, $mod.IUnknown);',
  18139. '});',
  18140. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  18141. ' this.GetIntf = function () {',
  18142. ' var Result = null;',
  18143. ' var i = null;',
  18144. ' var $ir = rtl.createIntfRefs();',
  18145. ' var $ok = false;',
  18146. ' try {',
  18147. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  18148. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  18149. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  18150. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18151. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18152. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18153. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18154. ' $ok = true;',
  18155. ' } finally {',
  18156. ' $ir.free();',
  18157. ' rtl._Release(i);',
  18158. ' if (!$ok) rtl._Release(Result);',
  18159. ' };',
  18160. ' return Result;',
  18161. ' };',
  18162. ' rtl.addIntf(this, $mod.IUnknown);',
  18163. '});',
  18164. '']),
  18165. LinesToStr([ // $mod.$main
  18166. '']));
  18167. end;
  18168. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  18169. begin
  18170. StartProgram(false);
  18171. Add([
  18172. '{$interfaces com}',
  18173. 'type',
  18174. ' IUnknown = interface',
  18175. ' function _AddRef: longint;',
  18176. ' function _Release: longint;',
  18177. ' end;',
  18178. ' TObject = class(IUnknown)',
  18179. ' function _AddRef: longint; virtual; abstract;',
  18180. ' function _Release: longint; virtual; abstract;',
  18181. ' end;',
  18182. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  18183. 'begin',
  18184. ' if i is IUnknown then ;',
  18185. ' if o is IUnknown then ;',
  18186. ' if i is TObject then ;',
  18187. ' i:=j as IUnknown;',
  18188. ' i:=o as IUnknown;',
  18189. ' o:=j as TObject;',
  18190. ' i:=IUnknown(j);',
  18191. ' i:=IUnknown(o);',
  18192. ' o:=TObject(i);',
  18193. 'end;',
  18194. 'begin',
  18195. '']);
  18196. ConvertProgram;
  18197. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  18198. LinesToStr([ // statements
  18199. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18200. 'rtl.createClass($mod, "TObject", null, function () {',
  18201. ' this.$init = function () {',
  18202. ' };',
  18203. ' this.$final = function () {',
  18204. ' };',
  18205. ' rtl.addIntf(this, $mod.IUnknown);',
  18206. '});',
  18207. 'this.DoDefault = function (i, j, o) {',
  18208. ' rtl._AddRef(i);',
  18209. ' try {',
  18210. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  18211. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  18212. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  18213. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  18214. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18215. ' o = rtl.intfAsClass(j, $mod.TObject);',
  18216. ' i = rtl.setIntfL(i, j);',
  18217. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18218. ' o = rtl.intfToClass(i, $mod.TObject);',
  18219. ' } finally {',
  18220. ' rtl._Release(i);',
  18221. ' };',
  18222. '};',
  18223. '']),
  18224. LinesToStr([ // $mod.$main
  18225. '']));
  18226. end;
  18227. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  18228. begin
  18229. StartProgram(false);
  18230. Add([
  18231. '{$interfaces com}',
  18232. 'type',
  18233. ' IUnknown = interface',
  18234. ' function _AddRef: longint;',
  18235. ' function _Release: longint;',
  18236. ' end;',
  18237. ' TObject = class(IUnknown)',
  18238. ' function _AddRef: longint; virtual; abstract;',
  18239. ' function _Release: longint; virtual; abstract;',
  18240. ' end;',
  18241. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  18242. 'var o: TObject;',
  18243. 'begin',
  18244. ' DoIt(v,v,v,v);',
  18245. ' DoIt(o,o,k,k);',
  18246. 'end;',
  18247. 'procedure DoSome;',
  18248. 'var v: IUnknown;',
  18249. 'begin',
  18250. ' DoIt(v,v,v,v);',
  18251. 'end;',
  18252. 'var i: IUnknown;',
  18253. 'begin',
  18254. ' DoIt(i,i,i,i);',
  18255. '']);
  18256. ConvertProgram;
  18257. CheckSource('TestClassInterface_COM_PassAsArg',
  18258. LinesToStr([ // statements
  18259. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18260. 'rtl.createClass($mod, "TObject", null, function () {',
  18261. ' this.$init = function () {',
  18262. ' };',
  18263. ' this.$final = function () {',
  18264. ' };',
  18265. ' rtl.addIntf(this, $mod.IUnknown);',
  18266. '});',
  18267. 'this.DoIt = function (v, j, k, l) {',
  18268. ' var o = null;',
  18269. ' var $ir = rtl.createIntfRefs();',
  18270. ' rtl._AddRef(v);',
  18271. ' try {',
  18272. ' $mod.DoIt(v, v, {',
  18273. ' get: function () {',
  18274. ' return v;',
  18275. ' },',
  18276. ' set: function (w) {',
  18277. ' v = rtl.setIntfL(v, w);',
  18278. ' }',
  18279. ' }, {',
  18280. ' get: function () {',
  18281. ' return v;',
  18282. ' },',
  18283. ' set: function (w) {',
  18284. ' v = rtl.setIntfL(v, w);',
  18285. ' }',
  18286. ' });',
  18287. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  18288. ' } finally {',
  18289. ' $ir.free();',
  18290. ' rtl._Release(v);',
  18291. ' };',
  18292. '};',
  18293. 'this.DoSome = function () {',
  18294. ' var v = null;',
  18295. ' try {',
  18296. ' $mod.DoIt(v, v, {',
  18297. ' get: function () {',
  18298. ' return v;',
  18299. ' },',
  18300. ' set: function (w) {',
  18301. ' v = rtl.setIntfL(v, w);',
  18302. ' }',
  18303. ' }, {',
  18304. ' get: function () {',
  18305. ' return v;',
  18306. ' },',
  18307. ' set: function (w) {',
  18308. ' v = rtl.setIntfL(v, w);',
  18309. ' }',
  18310. ' });',
  18311. ' } finally {',
  18312. ' rtl._Release(v);',
  18313. ' };',
  18314. '};',
  18315. 'this.i = null;',
  18316. '']),
  18317. LinesToStr([ // $mod.$main
  18318. '$mod.DoIt($mod.i, $mod.i, {',
  18319. ' p: $mod,',
  18320. ' get: function () {',
  18321. ' return this.p.i;',
  18322. ' },',
  18323. ' set: function (v) {',
  18324. ' rtl.setIntfP(this.p, "i", v);',
  18325. ' }',
  18326. '}, {',
  18327. ' p: $mod,',
  18328. ' get: function () {',
  18329. ' return this.p.i;',
  18330. ' },',
  18331. ' set: function (v) {',
  18332. ' rtl.setIntfP(this.p, "i", v);',
  18333. ' }',
  18334. '});',
  18335. '']));
  18336. end;
  18337. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  18338. begin
  18339. StartProgram(false);
  18340. Add([
  18341. '{$interfaces com}',
  18342. 'type',
  18343. ' IUnknown = interface',
  18344. ' function _AddRef: longint;',
  18345. ' function _Release: longint;',
  18346. ' end;',
  18347. ' TObject = class(IUnknown)',
  18348. ' function _AddRef: longint; virtual; abstract;',
  18349. ' function _Release: longint; virtual; abstract;',
  18350. ' end;',
  18351. 'procedure DoIt(out i);',
  18352. 'begin end;',
  18353. 'procedure DoSome;',
  18354. 'var v: IUnknown;',
  18355. 'begin',
  18356. ' DoIt(v);',
  18357. 'end;',
  18358. 'function GetIt: IUnknown;',
  18359. 'begin',
  18360. ' DoIt(Result);',
  18361. 'end;',
  18362. 'var i: IUnknown;',
  18363. 'begin',
  18364. ' DoIt(i);',
  18365. '']);
  18366. ConvertProgram;
  18367. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  18368. LinesToStr([ // statements
  18369. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18370. 'rtl.createClass($mod, "TObject", null, function () {',
  18371. ' this.$init = function () {',
  18372. ' };',
  18373. ' this.$final = function () {',
  18374. ' };',
  18375. ' rtl.addIntf(this, $mod.IUnknown);',
  18376. '});',
  18377. 'this.DoIt = function (i) {',
  18378. '};',
  18379. 'this.DoSome = function () {',
  18380. ' var v = null;',
  18381. ' try {',
  18382. ' $mod.DoIt({',
  18383. ' get: function () {',
  18384. ' return v;',
  18385. ' },',
  18386. ' set: function (w) {',
  18387. ' v = w;',
  18388. ' }',
  18389. ' });',
  18390. ' } finally {',
  18391. ' rtl._Release(v);',
  18392. ' };',
  18393. '};',
  18394. 'this.GetIt = function () {',
  18395. ' var Result = null;',
  18396. ' var $ok = false;',
  18397. ' try {',
  18398. ' $mod.DoIt({',
  18399. ' get: function () {',
  18400. ' return Result;',
  18401. ' },',
  18402. ' set: function (v) {',
  18403. ' Result = v;',
  18404. ' }',
  18405. ' });',
  18406. ' $ok = true;',
  18407. ' } finally {',
  18408. ' if (!$ok) rtl._Release(Result);',
  18409. ' };',
  18410. ' return Result;',
  18411. '};',
  18412. 'this.i = null;',
  18413. '']),
  18414. LinesToStr([ // $mod.$main
  18415. 'try {',
  18416. ' $mod.DoIt({',
  18417. ' p: $mod,',
  18418. ' get: function () {',
  18419. ' return this.p.i;',
  18420. ' },',
  18421. ' set: function (v) {',
  18422. ' this.p.i = v;',
  18423. ' }',
  18424. ' });',
  18425. '} finally {',
  18426. ' rtl._Release($mod.i);',
  18427. '};',
  18428. '']));
  18429. end;
  18430. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  18431. begin
  18432. StartProgram(false);
  18433. Add([
  18434. '{$interfaces com}',
  18435. 'type',
  18436. ' IUnknown = interface',
  18437. ' function _AddRef: longint;',
  18438. ' function _Release: longint;',
  18439. ' end;',
  18440. ' TObject = class(IUnknown)',
  18441. ' function _AddRef: longint; virtual; abstract;',
  18442. ' function _Release: longint; virtual; abstract;',
  18443. ' end;',
  18444. 'function GetIt: IUnknown;',
  18445. 'begin',
  18446. 'end;',
  18447. 'procedure DoSome;',
  18448. 'var v: IUnknown;',
  18449. ' i: longint;',
  18450. 'begin',
  18451. ' v:=GetIt;',
  18452. ' v:=GetIt();',
  18453. ' GetIt()._AddRef;',
  18454. ' i:=GetIt()._AddRef;',
  18455. 'end;',
  18456. 'var v: IUnknown;',
  18457. ' i: longint;',
  18458. 'begin',
  18459. ' v:=GetIt;',
  18460. ' v:=GetIt();',
  18461. ' GetIt()._AddRef;',
  18462. ' i:=GetIt()._AddRef;',
  18463. '']);
  18464. ConvertProgram;
  18465. CheckSource('TestClassInterface_COM_FunctionInExpr',
  18466. LinesToStr([ // statements
  18467. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18468. 'rtl.createClass($mod, "TObject", null, function () {',
  18469. ' this.$init = function () {',
  18470. ' };',
  18471. ' this.$final = function () {',
  18472. ' };',
  18473. ' rtl.addIntf(this, $mod.IUnknown);',
  18474. '});',
  18475. 'this.GetIt = function () {',
  18476. ' var Result = null;',
  18477. ' return Result;',
  18478. '};',
  18479. 'this.DoSome = function () {',
  18480. ' var v = null;',
  18481. ' var i = 0;',
  18482. ' var $ir = rtl.createIntfRefs();',
  18483. ' try {',
  18484. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  18485. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  18486. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18487. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18488. ' } finally {',
  18489. ' $ir.free();',
  18490. ' rtl._Release(v);',
  18491. ' };',
  18492. '};',
  18493. 'this.v = null;',
  18494. 'this.i = 0;',
  18495. '']),
  18496. LinesToStr([ // $mod.$main
  18497. 'var $ir = rtl.createIntfRefs();',
  18498. 'try {',
  18499. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18500. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18501. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18502. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18503. '} finally {',
  18504. ' $ir.free();',
  18505. '};',
  18506. '']));
  18507. end;
  18508. procedure TTestModule.TestClassInterface_COM_Property;
  18509. begin
  18510. StartProgram(false);
  18511. Add([
  18512. '{$interfaces com}',
  18513. 'type',
  18514. ' IUnknown = interface',
  18515. ' function _AddRef: longint;',
  18516. ' function _Release: longint;',
  18517. ' end;',
  18518. ' TObject = class(IUnknown)',
  18519. ' FAnt: IUnknown;',
  18520. ' function _AddRef: longint; virtual; abstract;',
  18521. ' function _Release: longint; virtual; abstract;',
  18522. ' function GetBird: IUnknown; virtual; abstract;',
  18523. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18524. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18525. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18526. ' property Ant: IUnknown read FAnt write FAnt;',
  18527. ' property Bird: IUnknown read GetBird write SetBird;',
  18528. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18529. ' end;',
  18530. 'procedure DoIt;',
  18531. 'var',
  18532. ' o: TObject;',
  18533. ' v: IUnknown;',
  18534. 'begin',
  18535. ' v:=o.Ant;',
  18536. ' o.Ant:=v;',
  18537. ' o.Ant:=o.Ant;',
  18538. ' v:=o.Bird;',
  18539. ' o.Bird:=v;',
  18540. ' o.Bird:=o.Bird;',
  18541. ' v:=o.Items[1];',
  18542. ' o.Items[2]:=v;',
  18543. ' o.Items[3]:=o.Items[4];',
  18544. ' v:=o[5];',
  18545. ' o[6]:=v;',
  18546. ' o[7]:=o[8];',
  18547. 'end;',
  18548. 'begin',
  18549. '']);
  18550. ConvertProgram;
  18551. CheckSource('TestClassInterface_COM_Property',
  18552. LinesToStr([ // statements
  18553. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18554. 'rtl.createClass($mod, "TObject", null, function () {',
  18555. ' this.$init = function () {',
  18556. ' this.FAnt = null;',
  18557. ' };',
  18558. ' this.$final = function () {',
  18559. ' this.FAnt = undefined;',
  18560. ' };',
  18561. ' rtl.addIntf(this, $mod.IUnknown);',
  18562. '});',
  18563. 'this.DoIt = function () {',
  18564. ' var o = null;',
  18565. ' var v = null;',
  18566. ' var $ir = rtl.createIntfRefs();',
  18567. ' try {',
  18568. ' v = rtl.setIntfL(v, o.FAnt);',
  18569. ' rtl.setIntfP(o, "FAnt", v);',
  18570. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  18571. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  18572. ' o.SetBird(v);',
  18573. ' o.SetBird($ir.ref(1, o.GetBird()));',
  18574. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  18575. ' o.SetItems(2, v);',
  18576. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  18577. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  18578. ' o.SetItems(6, v);',
  18579. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  18580. ' } finally {',
  18581. ' $ir.free();',
  18582. ' rtl._Release(v);',
  18583. ' };',
  18584. '};',
  18585. '']),
  18586. LinesToStr([ // $mod.$main
  18587. '']));
  18588. end;
  18589. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  18590. begin
  18591. StartProgram(false);
  18592. Add([
  18593. '{$interfaces com}',
  18594. 'type',
  18595. ' IUnknown = interface',
  18596. ' function _AddRef: longint;',
  18597. ' function _Release: longint;',
  18598. ' function GetBird: IUnknown;',
  18599. ' procedure SetBird(Value: IUnknown);',
  18600. ' function GetItems(Index: longint): IUnknown;',
  18601. ' procedure SetItems(Index: longint; Value: IUnknown);',
  18602. ' property Bird: IUnknown read GetBird write SetBird;',
  18603. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18604. ' end;',
  18605. ' TObject = class(IUnknown)',
  18606. ' function _AddRef: longint; virtual; abstract;',
  18607. ' function _Release: longint; virtual; abstract;',
  18608. ' function GetBird: IUnknown; virtual; abstract;',
  18609. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18610. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18611. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18612. ' end;',
  18613. 'procedure DoIt;',
  18614. 'var',
  18615. ' o: TObject;',
  18616. ' v: IUnknown;',
  18617. 'begin',
  18618. ' v:=v.Items[1];',
  18619. ' v.Items[2]:=v;',
  18620. ' v.Items[3]:=v.Items[4];',
  18621. ' v:=v[5];',
  18622. ' v[6]:=v;',
  18623. ' v[7]:=v[8];',
  18624. ' v[9].Bird.Bird:=v;',
  18625. ' v:=v.Bird[10].Bird',
  18626. 'end;',
  18627. 'begin',
  18628. '']);
  18629. ConvertProgram;
  18630. CheckSource('TestClassInterface_COM_IntfProperty',
  18631. LinesToStr([ // statements
  18632. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  18633. ' "_AddRef",',
  18634. ' "_Release",',
  18635. ' "GetBird",',
  18636. ' "SetBird",',
  18637. ' "GetItems",',
  18638. ' "SetItems"',
  18639. '], null);',
  18640. 'rtl.createClass($mod, "TObject", null, function () {',
  18641. ' this.$init = function () {',
  18642. ' };',
  18643. ' this.$final = function () {',
  18644. ' };',
  18645. ' rtl.addIntf(this, $mod.IUnknown);',
  18646. '});',
  18647. 'this.DoIt = function () {',
  18648. ' var o = null;',
  18649. ' var v = null;',
  18650. ' var $ir = rtl.createIntfRefs();',
  18651. ' try {',
  18652. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  18653. ' v.SetItems(2, v);',
  18654. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  18655. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  18656. ' v.SetItems(6, v);',
  18657. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  18658. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  18659. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  18660. ' } finally {',
  18661. ' $ir.free();',
  18662. ' rtl._Release(v);',
  18663. ' };',
  18664. '};',
  18665. '']),
  18666. LinesToStr([ // $mod.$main
  18667. '']));
  18668. end;
  18669. procedure TTestModule.TestClassInterface_COM_Delegation;
  18670. begin
  18671. StartProgram(false);
  18672. Add([
  18673. '{$interfaces com}',
  18674. 'type',
  18675. ' IUnknown = interface',
  18676. ' function _AddRef: longint;',
  18677. ' function _Release: longint;',
  18678. ' end;',
  18679. ' IBird = interface(IUnknown)',
  18680. ' procedure Fly(s: string);',
  18681. ' end;',
  18682. ' IEagle = interface(IBird) end;',
  18683. ' IDove = interface(IBird) end;',
  18684. ' ISwallow = interface(IBird) end;',
  18685. ' TObject = class',
  18686. ' end;',
  18687. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18688. ' function _AddRef: longint; virtual; abstract;',
  18689. ' function _Release: longint; virtual; abstract;',
  18690. ' procedure Fly(s: string); virtual; abstract;',
  18691. ' end;',
  18692. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18693. ' function _AddRef: longint; virtual; abstract;',
  18694. ' function _Release: longint; virtual; abstract;',
  18695. ' FBirdIntf: IBird;',
  18696. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18697. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18698. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18699. ' FDoveObj: TBird;',
  18700. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18701. ' function GetSwallowObj: TBird; virtual; abstract;',
  18702. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18703. ' end;',
  18704. 'begin',
  18705. '']);
  18706. ConvertProgram;
  18707. CheckSource('TestClassInterface_COM_Delegation',
  18708. LinesToStr([ // statements
  18709. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18710. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  18711. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  18712. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  18713. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  18714. 'rtl.createClass($mod, "TObject", null, function () {',
  18715. ' this.$init = function () {',
  18716. ' };',
  18717. ' this.$final = function () {',
  18718. ' };',
  18719. '});',
  18720. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18721. ' rtl.addIntf(this, $mod.IBird);',
  18722. ' rtl.addIntf(this, $mod.IEagle);',
  18723. ' rtl.addIntf(this, $mod.IDove);',
  18724. ' rtl.addIntf(this, $mod.ISwallow);',
  18725. '});',
  18726. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18727. ' this.$init = function () {',
  18728. ' $mod.TObject.$init.call(this);',
  18729. ' this.FBirdIntf = null;',
  18730. ' this.FDoveObj = null;',
  18731. ' };',
  18732. ' this.$final = function () {',
  18733. ' this.FBirdIntf = undefined;',
  18734. ' this.FDoveObj = undefined;',
  18735. ' $mod.TObject.$final.call(this);',
  18736. ' };',
  18737. ' this.$intfmaps = {',
  18738. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  18739. ' return rtl._AddRef(this.FBirdIntf);',
  18740. ' },',
  18741. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  18742. ' return this.GetEagleIntf();',
  18743. ' },',
  18744. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  18745. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  18746. ' },',
  18747. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  18748. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18749. ' }',
  18750. ' };',
  18751. '});',
  18752. '']),
  18753. LinesToStr([ // $mod.$main
  18754. '']));
  18755. end;
  18756. procedure TTestModule.TestClassInterface_COM_With;
  18757. begin
  18758. StartProgram(false);
  18759. Add([
  18760. '{$interfaces com}',
  18761. 'type',
  18762. ' IUnknown = interface',
  18763. ' function _AddRef: longint;',
  18764. ' function _Release: longint;',
  18765. ' function GetAnt: IUnknown;',
  18766. ' property Ant: IUnknown read GetAnt;',
  18767. ' end;',
  18768. ' TObject = class(IUnknown)',
  18769. ' function _AddRef: longint; virtual; abstract;',
  18770. ' function _Release: longint; virtual; abstract;',
  18771. ' function GetAnt: IUnknown; virtual; abstract;',
  18772. ' property Ant: IUnknown read GetAnt;',
  18773. ' end;',
  18774. 'procedure DoIt;',
  18775. 'var',
  18776. ' i: IUnknown;',
  18777. 'begin',
  18778. ' with i do ',
  18779. ' GetAnt;',
  18780. ' with i.Ant, Ant do ',
  18781. ' GetAnt;',
  18782. 'end;',
  18783. 'begin',
  18784. '']);
  18785. ConvertProgram;
  18786. CheckSource('TestClassInterface_COM_With',
  18787. LinesToStr([ // statements
  18788. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  18789. 'rtl.createClass($mod, "TObject", null, function () {',
  18790. ' this.$init = function () {',
  18791. ' };',
  18792. ' this.$final = function () {',
  18793. ' };',
  18794. ' rtl.addIntf(this, $mod.IUnknown);',
  18795. '});',
  18796. 'this.DoIt = function () {',
  18797. ' var i = null;',
  18798. ' var $ir = rtl.createIntfRefs();',
  18799. ' try {',
  18800. ' $ir.ref(1, i.GetAnt());',
  18801. ' var $with1 = $ir.ref(2, i.GetAnt());',
  18802. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  18803. ' $ir.ref(4, $with2.GetAnt());',
  18804. ' } finally {',
  18805. ' $ir.free();',
  18806. ' };',
  18807. '};',
  18808. '']),
  18809. LinesToStr([ // $mod.$main
  18810. '']));
  18811. end;
  18812. procedure TTestModule.TestClassInterface_COM_ForIn;
  18813. begin
  18814. StartProgram(false);
  18815. Add([
  18816. '{$interfaces com}',
  18817. 'type',
  18818. ' IUnknown = interface end;',
  18819. ' TObject = class',
  18820. ' Id: longint;',
  18821. ' end;',
  18822. ' IEnumerator = interface(IUnknown)',
  18823. ' function GetCurrent: TObject;',
  18824. ' function MoveNext: Boolean;',
  18825. ' property Current: TObject read GetCurrent;',
  18826. ' end;',
  18827. ' IEnumerable = interface(IUnknown)',
  18828. ' function GetEnumerator: IEnumerator;',
  18829. ' end;',
  18830. 'var',
  18831. ' o: TObject;',
  18832. ' i: IEnumerable;',
  18833. 'begin',
  18834. ' for o in i do o.Id:=3;',
  18835. '']);
  18836. ConvertProgram;
  18837. CheckSource('TestClassInterface_COM_ForIn',
  18838. LinesToStr([ // statements
  18839. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18840. 'rtl.createClass($mod, "TObject", null, function () {',
  18841. ' this.$init = function () {',
  18842. ' this.Id = 0;',
  18843. ' };',
  18844. ' this.$final = function () {',
  18845. ' };',
  18846. '});',
  18847. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18848. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18849. 'this.o = null;',
  18850. 'this.i = null;',
  18851. '']),
  18852. LinesToStr([ // $mod.$main
  18853. 'var $in1 = $mod.i.GetEnumerator();',
  18854. 'try {',
  18855. ' while ($in1.MoveNext()) {',
  18856. ' $mod.o = $in1.GetCurrent();',
  18857. ' $mod.o.Id = 3;',
  18858. ' }',
  18859. '} finally {',
  18860. ' rtl._Release($in1)',
  18861. '};',
  18862. '']));
  18863. end;
  18864. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  18865. begin
  18866. StartProgram(false);
  18867. Add([
  18868. '{$interfaces com}',
  18869. 'type',
  18870. ' IUnknown = interface',
  18871. ' function _AddRef: longint;',
  18872. ' function _Release: longint;',
  18873. ' end;',
  18874. ' TObject = class',
  18875. ' end;',
  18876. ' TArrOfIntf = array of IUnknown;',
  18877. 'begin',
  18878. '']);
  18879. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  18880. ConvertProgram;
  18881. end;
  18882. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  18883. begin
  18884. StartProgram(false);
  18885. Add([
  18886. '{$interfaces com}',
  18887. 'type',
  18888. ' IUnknown = interface',
  18889. ' function _AddRef: longint;',
  18890. ' function _Release: longint;',
  18891. ' end;',
  18892. ' TRec = record',
  18893. ' i: IUnknown;',
  18894. ' end;',
  18895. 'begin',
  18896. '']);
  18897. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  18898. ConvertProgram;
  18899. end;
  18900. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  18901. begin
  18902. StartUnit(false);
  18903. Add([
  18904. '{$interfaces com}',
  18905. 'interface',
  18906. 'implementation',
  18907. 'type',
  18908. ' IUnknown = interface',
  18909. ' function _AddRef: longint;',
  18910. ' end;',
  18911. ' TObject = class(IUnknown)',
  18912. ' function _AddRef: longint;',
  18913. ' end;',
  18914. 'function TObject._AddRef: longint; begin end;',
  18915. 'var i: IUnknown;',
  18916. ' o: TObject;',
  18917. 'initialization',
  18918. ' i:=nil;',
  18919. ' i:=i;',
  18920. ' i:=o;',
  18921. ' if (o as IUnknown)=nil then ;',
  18922. '']);
  18923. ConvertUnit;
  18924. CheckSource('TestClassInterface_COM_UnitInitialization',
  18925. LinesToStr([ // statements
  18926. 'var $impl = $mod.$impl;',
  18927. '']),
  18928. LinesToStr([ // this.$init
  18929. 'var $ir = rtl.createIntfRefs();',
  18930. 'try {',
  18931. ' rtl.setIntfP($impl, "i", null);',
  18932. ' rtl.setIntfP($impl, "i", $impl.i);',
  18933. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  18934. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  18935. '} finally {',
  18936. ' $ir.free();',
  18937. '};',
  18938. '']),
  18939. LinesToStr([ // implementation
  18940. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  18941. 'rtl.createClass($impl, "TObject", null, function () {',
  18942. ' this.$init = function () {',
  18943. ' };',
  18944. ' this.$final = function () {',
  18945. ' };',
  18946. ' this._AddRef = function () {',
  18947. ' var Result = 0;',
  18948. ' return Result;',
  18949. ' };',
  18950. ' rtl.addIntf(this, $impl.IUnknown);',
  18951. '});',
  18952. '$impl.i = null;',
  18953. '$impl.o = null;',
  18954. ''])
  18955. );
  18956. end;
  18957. procedure TTestModule.TestClassInterface_GUID;
  18958. begin
  18959. StartProgram(false);
  18960. Add([
  18961. '{$interfaces corba}',
  18962. 'type',
  18963. ' IUnknown = interface',
  18964. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18965. ' end;',
  18966. ' TObject = class end;',
  18967. ' TGUID = record D1, D2, D3, D4: word; end;',
  18968. ' TAliasGUID = TGUID;',
  18969. ' TGUIDString = type string;',
  18970. ' TAliasGUIDString = TGUIDString;',
  18971. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  18972. 'begin end;',
  18973. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  18974. 'begin end;',
  18975. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  18976. 'begin end;',
  18977. 'var',
  18978. ' i: IUnknown;',
  18979. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  18980. ' s: TAliasGUIDString;',
  18981. 'begin',
  18982. ' DoConstGUIDIt(IUnknown);',
  18983. ' DoDefGUID(IUnknown);',
  18984. ' DoStr(IUnknown);',
  18985. ' DoConstGUIDIt(i);',
  18986. ' DoDefGUID(i);',
  18987. ' DoStr(i);',
  18988. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18989. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18990. ' DoStr(g);',
  18991. ' g:=i;',
  18992. ' g:=IUnknown;',
  18993. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18994. ' s:=i;',
  18995. ' s:=IUnknown;',
  18996. ' s:=g;',
  18997. ' if g=i then ;',
  18998. ' if i=g then ;',
  18999. ' if g=IUnknown then ;',
  19000. ' if IUnknown=g then ;',
  19001. ' if s=i then ;',
  19002. ' if i=s then ;',
  19003. ' if s=IUnknown then ;',
  19004. ' if IUnknown=s then ;',
  19005. ' if s=g then ;',
  19006. ' if g=s then ;',
  19007. '']);
  19008. ConvertProgram;
  19009. CheckSource('TestClassInterface_GUID',
  19010. LinesToStr([ // statements
  19011. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19012. 'rtl.createClass($mod, "TObject", null, function () {',
  19013. ' this.$init = function () {',
  19014. ' };',
  19015. ' this.$final = function () {',
  19016. ' };',
  19017. '});',
  19018. 'rtl.recNewT($mod, "TGUID", function () {',
  19019. ' this.D1 = 0;',
  19020. ' this.D2 = 0;',
  19021. ' this.D3 = 0;',
  19022. ' this.D4 = 0;',
  19023. ' this.$eq = function (b) {',
  19024. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19025. ' };',
  19026. ' this.$assign = function (s) {',
  19027. ' this.D1 = s.D1;',
  19028. ' this.D2 = s.D2;',
  19029. ' this.D3 = s.D3;',
  19030. ' this.D4 = s.D4;',
  19031. ' return this;',
  19032. ' };',
  19033. '});',
  19034. 'this.DoConstGUIDIt = function (g) {',
  19035. '};',
  19036. 'this.DoDefGUID = function (g) {',
  19037. '};',
  19038. 'this.DoStr = function (s) {',
  19039. '};',
  19040. 'this.i = null;',
  19041. 'this.g = $mod.TGUID.$clone({',
  19042. ' D1: 0xD91C9AF4,',
  19043. ' D2: 0x3C93,',
  19044. ' D3: 0x420F,',
  19045. ' D4: [',
  19046. ' 0xA3,',
  19047. ' 0x03,',
  19048. ' 0xBF,',
  19049. ' 0x5B,',
  19050. ' 0xA8,',
  19051. ' 0x2B,',
  19052. ' 0xFD,',
  19053. ' 0x23',
  19054. ' ]',
  19055. '});',
  19056. 'this.s = "";',
  19057. '']),
  19058. LinesToStr([ // $mod.$main
  19059. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  19060. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  19061. '$mod.DoStr($mod.IUnknown.$guid);',
  19062. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  19063. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  19064. '$mod.DoStr($mod.i.$guid);',
  19065. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19066. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19067. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  19068. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  19069. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  19070. '$mod.g.$assign({',
  19071. ' D1: 0xD91C9AF4,',
  19072. ' D2: 0x3C93,',
  19073. ' D3: 0x420F,',
  19074. ' D4: [',
  19075. ' 0xA3,',
  19076. ' 0x03,',
  19077. ' 0xBF,',
  19078. ' 0x5B,',
  19079. ' 0xA8,',
  19080. ' 0x2B,',
  19081. ' 0xFD,',
  19082. ' 0x23',
  19083. ' ]',
  19084. '});',
  19085. '$mod.s = $mod.i.$guid;',
  19086. '$mod.s = $mod.IUnknown.$guid;',
  19087. '$mod.s = rtl.guidrToStr($mod.g);',
  19088. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19089. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19090. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19091. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19092. 'if ($mod.s === $mod.i.$guid) ;',
  19093. 'if ($mod.i.$guid === $mod.s) ;',
  19094. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  19095. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  19096. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19097. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19098. '']));
  19099. end;
  19100. procedure TTestModule.TestClassInterface_GUIDProperty;
  19101. begin
  19102. StartProgram(false);
  19103. Add([
  19104. '{$interfaces corba}',
  19105. 'type',
  19106. ' IUnknown = interface',
  19107. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19108. ' end;',
  19109. ' TGUID = record D1, D2, D3, D4: word; end;',
  19110. ' TAliasGUID = TGUID;',
  19111. ' TGUIDString = type string;',
  19112. ' TAliasGUIDString = TGUIDString;',
  19113. ' TObject = class',
  19114. ' function GetG: TAliasGUID; virtual; abstract;',
  19115. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  19116. ' function GetS: TAliasGUIDString; virtual; abstract;',
  19117. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  19118. ' property g: TAliasGUID read GetG write SetG;',
  19119. ' property s: TAliasGUIDString read GetS write SetS;',
  19120. ' end;',
  19121. 'var o: TObject;',
  19122. 'begin',
  19123. ' o.g:=IUnknown;',
  19124. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19125. ' o.s:=IUnknown;',
  19126. ' o.s:=o.g;',
  19127. '']);
  19128. ConvertProgram;
  19129. CheckSource('TestClassInterface_GUIDProperty',
  19130. LinesToStr([ // statements
  19131. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19132. 'rtl.recNewT($mod, "TGUID", function () {',
  19133. ' this.D1 = 0;',
  19134. ' this.D2 = 0;',
  19135. ' this.D3 = 0;',
  19136. ' this.D4 = 0;',
  19137. ' this.$eq = function (b) {',
  19138. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19139. ' };',
  19140. ' this.$assign = function (s) {',
  19141. ' this.D1 = s.D1;',
  19142. ' this.D2 = s.D2;',
  19143. ' this.D3 = s.D3;',
  19144. ' this.D4 = s.D4;',
  19145. ' return this;',
  19146. ' };',
  19147. '});',
  19148. 'rtl.createClass($mod, "TObject", null, function () {',
  19149. ' this.$init = function () {',
  19150. ' };',
  19151. ' this.$final = function () {',
  19152. ' };',
  19153. '});',
  19154. 'this.o = null;',
  19155. '']),
  19156. LinesToStr([ // $mod.$main
  19157. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  19158. '$mod.o.SetG({',
  19159. ' D1: 0xD91C9AF4,',
  19160. ' D2: 0x3C93,',
  19161. ' D3: 0x420F,',
  19162. ' D4: [',
  19163. ' 0xA3,',
  19164. ' 0x03,',
  19165. ' 0xBF,',
  19166. ' 0x5B,',
  19167. ' 0xA8,',
  19168. ' 0x2B,',
  19169. ' 0xFD,',
  19170. ' 0x23',
  19171. ' ]',
  19172. '});',
  19173. '$mod.o.SetS($mod.IUnknown.$guid);',
  19174. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  19175. '']));
  19176. end;
  19177. procedure TTestModule.TestClassHelper_ClassVar;
  19178. begin
  19179. StartProgram(false);
  19180. Add([
  19181. 'type',
  19182. ' TObject = class',
  19183. ' end;',
  19184. ' THelper = class helper for TObject',
  19185. ' const',
  19186. ' One = 1;',
  19187. ' Two: word = 2;',
  19188. ' class var',
  19189. ' Glob: word;',
  19190. ' function Foo(w: word): word;',
  19191. ' class function Bar(w: word): word;',
  19192. ' end;',
  19193. 'function THelper.foo(w: word): word;',
  19194. 'begin',
  19195. ' Result:=w;',
  19196. ' Two:=One+w;',
  19197. ' Glob:=Glob;',
  19198. ' Result:=Self.Glob;',
  19199. ' Self.Glob:=Self.Glob;',
  19200. ' with Self do Glob:=Glob;',
  19201. 'end;',
  19202. 'class function THelper.bar(w: word): word;',
  19203. 'begin',
  19204. ' Result:=w;',
  19205. ' Two:=One;',
  19206. ' Glob:=Glob;',
  19207. ' Self.Glob:=Self.Glob;',
  19208. ' with Self do Glob:=Glob;',
  19209. 'end;',
  19210. 'var o: TObject;',
  19211. 'begin',
  19212. ' tobject.two:=tobject.one;',
  19213. ' tobject.Glob:=tobject.Glob;',
  19214. ' with tobject do begin',
  19215. ' two:=one;',
  19216. ' Glob:=Glob;',
  19217. ' end;',
  19218. ' o.two:=o.one;',
  19219. ' o.Glob:=o.Glob;',
  19220. ' with o do begin',
  19221. ' two:=one;',
  19222. ' Glob:=Glob;',
  19223. ' end;',
  19224. '']);
  19225. ConvertProgram;
  19226. CheckSource('TestClassHelper_ClassVar',
  19227. LinesToStr([ // statements
  19228. 'rtl.createClass($mod, "TObject", null, function () {',
  19229. ' this.$init = function () {',
  19230. ' };',
  19231. ' this.$final = function () {',
  19232. ' };',
  19233. '});',
  19234. 'rtl.createHelper($mod, "THelper", null, function () {',
  19235. ' this.One = 1;',
  19236. ' this.Two = 2;',
  19237. ' this.Glob = 0;',
  19238. ' this.Foo = function (w) {',
  19239. ' var Result = 0;',
  19240. ' Result = w;',
  19241. ' $mod.THelper.Two = 1 + w;',
  19242. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19243. ' Result = $mod.THelper.Glob;',
  19244. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19245. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19246. ' return Result;',
  19247. ' };',
  19248. ' this.Bar = function (w) {',
  19249. ' var Result = 0;',
  19250. ' Result = w;',
  19251. ' $mod.THelper.Two = 1;',
  19252. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19253. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19254. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19255. ' return Result;',
  19256. ' };',
  19257. '});',
  19258. 'this.o = null;',
  19259. '']),
  19260. LinesToStr([ // $mod.$main
  19261. '$mod.THelper.Two = 1;',
  19262. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19263. 'var $with1 = $mod.TObject;',
  19264. '$mod.THelper.Two = 1;',
  19265. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19266. '$mod.THelper.Two = 1;',
  19267. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19268. 'var $with2 = $mod.o;',
  19269. '$mod.THelper.Two = 1;',
  19270. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19271. '']));
  19272. end;
  19273. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  19274. begin
  19275. StartProgram(false);
  19276. Add([
  19277. 'type',
  19278. ' TObject = class',
  19279. ' FSize: word;',
  19280. ' property Size: word read FSize write FSize;',
  19281. ' end;',
  19282. ' THelper = class helper for TObject',
  19283. ' function Foo(w: word = 1): word;',
  19284. ' end;',
  19285. 'function THelper.foo(w: word): word;',
  19286. 'begin',
  19287. ' Result:=Size;',
  19288. ' Size:=Size+2;',
  19289. ' Self.Size:=Self.Size+3;',
  19290. ' FSize:=FSize+4;',
  19291. ' Self.FSize:=Self.FSize+5;',
  19292. ' with Self do begin',
  19293. ' Size:=Size+6;',
  19294. ' FSize:=FSize+7;',
  19295. ' FSize:=FSize+8;',
  19296. ' end;',
  19297. 'end;',
  19298. 'begin',
  19299. '']);
  19300. ConvertProgram;
  19301. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  19302. LinesToStr([ // statements
  19303. 'rtl.createClass($mod, "TObject", null, function () {',
  19304. ' this.$init = function () {',
  19305. ' this.FSize = 0;',
  19306. ' };',
  19307. ' this.$final = function () {',
  19308. ' };',
  19309. '});',
  19310. 'rtl.createHelper($mod, "THelper", null, function () {',
  19311. ' this.Foo = function (w) {',
  19312. ' var Result = 0;',
  19313. ' Result = this.FSize;',
  19314. ' this.FSize = this.FSize + 2;',
  19315. ' this.FSize = this.FSize + 3;',
  19316. ' this.FSize = this.FSize + 4;',
  19317. ' this.FSize = this.FSize + 5;',
  19318. ' this.FSize = this.FSize + 6;',
  19319. ' this.FSize = this.FSize + 7;',
  19320. ' this.FSize = this.FSize + 8;',
  19321. ' return Result;',
  19322. ' };',
  19323. '});',
  19324. '']),
  19325. LinesToStr([ // $mod.$main
  19326. '']));
  19327. end;
  19328. procedure TTestModule.TestClassHelper_Method_Call;
  19329. begin
  19330. StartProgram(false);
  19331. Add([
  19332. 'type',
  19333. ' TObject = class',
  19334. ' procedure Run(w: word = 10);',
  19335. ' end;',
  19336. ' THelper = class helper for TObject',
  19337. ' function Foo(w: word = 1): word;',
  19338. ' end;',
  19339. 'procedure TObject.Run(w: word);',
  19340. 'var o: TObject;',
  19341. 'begin',
  19342. ' Foo;',
  19343. ' Foo();',
  19344. ' Foo(2);',
  19345. ' Self.Foo;',
  19346. ' Self.Foo();',
  19347. ' Self.Foo(3);',
  19348. ' with Self do begin',
  19349. ' Foo;',
  19350. ' Foo();',
  19351. ' Foo(4);',
  19352. ' end;',
  19353. ' with o do Foo(5);',
  19354. 'end;',
  19355. 'function THelper.foo(w: word): word;',
  19356. 'begin',
  19357. ' Run;',
  19358. ' Run();',
  19359. ' Run(11);',
  19360. ' Foo;',
  19361. ' Foo();',
  19362. ' Foo(12);',
  19363. ' Self.Foo;',
  19364. ' Self.Foo();',
  19365. ' Self.Foo(13);',
  19366. ' with Self do begin',
  19367. ' Foo;',
  19368. ' Foo();',
  19369. ' Foo(14);',
  19370. ' end;',
  19371. 'end;',
  19372. 'var Obj: TObject;',
  19373. 'begin',
  19374. ' obj.Foo;',
  19375. ' obj.Foo();',
  19376. ' obj.Foo(21);',
  19377. ' with obj do begin',
  19378. ' Foo;',
  19379. ' Foo();',
  19380. ' Foo(22);',
  19381. ' end;',
  19382. '']);
  19383. ConvertProgram;
  19384. CheckSource('TestClassHelper_Method_Call',
  19385. LinesToStr([ // statements
  19386. 'rtl.createClass($mod, "TObject", null, function () {',
  19387. ' this.$init = function () {',
  19388. ' };',
  19389. ' this.$final = function () {',
  19390. ' };',
  19391. ' this.Run = function (w) {',
  19392. ' var o = null;',
  19393. ' $mod.THelper.Foo.call(this, 1);',
  19394. ' $mod.THelper.Foo.call(this, 1);',
  19395. ' $mod.THelper.Foo.call(this, 2);',
  19396. ' $mod.THelper.Foo.call(this, 1);',
  19397. ' $mod.THelper.Foo.call(this, 1);',
  19398. ' $mod.THelper.Foo.call(this, 3);',
  19399. ' $mod.THelper.Foo.call(this, 1);',
  19400. ' $mod.THelper.Foo.call(this, 1);',
  19401. ' $mod.THelper.Foo.call(this, 4);',
  19402. ' $mod.THelper.Foo.call(o, 5);',
  19403. ' };',
  19404. '});',
  19405. 'rtl.createHelper($mod, "THelper", null, function () {',
  19406. ' this.Foo = function (w) {',
  19407. ' var Result = 0;',
  19408. ' this.Run(10);',
  19409. ' this.Run(10);',
  19410. ' this.Run(11);',
  19411. ' $mod.THelper.Foo.call(this, 1);',
  19412. ' $mod.THelper.Foo.call(this, 1);',
  19413. ' $mod.THelper.Foo.call(this, 12);',
  19414. ' $mod.THelper.Foo.call(this, 1);',
  19415. ' $mod.THelper.Foo.call(this, 1);',
  19416. ' $mod.THelper.Foo.call(this, 13);',
  19417. ' $mod.THelper.Foo.call(this, 1);',
  19418. ' $mod.THelper.Foo.call(this, 1);',
  19419. ' $mod.THelper.Foo.call(this, 14);',
  19420. ' return Result;',
  19421. ' };',
  19422. '});',
  19423. 'this.Obj = null;',
  19424. '']),
  19425. LinesToStr([ // $mod.$main
  19426. '$mod.THelper.Foo.call($mod.Obj, 1);',
  19427. '$mod.THelper.Foo.call($mod.Obj, 1);',
  19428. '$mod.THelper.Foo.call($mod.Obj, 21);',
  19429. 'var $with1 = $mod.Obj;',
  19430. '$mod.THelper.Foo.call($with1, 1);',
  19431. '$mod.THelper.Foo.call($with1, 1);',
  19432. '$mod.THelper.Foo.call($with1, 22);',
  19433. '']));
  19434. end;
  19435. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  19436. begin
  19437. StartProgram(false);
  19438. Add([
  19439. 'type',
  19440. ' TObject = class',
  19441. ' procedure Run(w: word = 10);',
  19442. ' end;',
  19443. ' THelper = class helper for TObject',
  19444. ' function Foo(w: word = 1): word;',
  19445. ' end;',
  19446. 'procedure TObject.Run(w: word);',
  19447. ' procedure Sub(Self: TObject);',
  19448. ' begin',
  19449. ' Foo;',
  19450. ' Foo();',
  19451. ' Self.Foo;',
  19452. ' Self.Foo();',
  19453. ' with Self do begin',
  19454. ' Foo;',
  19455. ' Foo();',
  19456. ' end;',
  19457. ' end;',
  19458. 'begin',
  19459. 'end;',
  19460. 'function THelper.foo(w: word): word;',
  19461. ' procedure Sub(Self: TObject);',
  19462. ' begin',
  19463. ' Run;',
  19464. ' Run();',
  19465. ' Foo;',
  19466. ' Foo();',
  19467. ' Self.Foo;',
  19468. ' Self.Foo();',
  19469. ' with Self do begin',
  19470. ' Foo;',
  19471. ' Foo();',
  19472. ' end;',
  19473. ' end;',
  19474. 'begin',
  19475. 'end;',
  19476. 'begin',
  19477. '']);
  19478. ConvertProgram;
  19479. CheckSource('TestClassHelper_Method_Nested_Call',
  19480. LinesToStr([ // statements
  19481. 'rtl.createClass($mod, "TObject", null, function () {',
  19482. ' this.$init = function () {',
  19483. ' };',
  19484. ' this.$final = function () {',
  19485. ' };',
  19486. ' this.Run = function (w) {',
  19487. ' var $Self = this;',
  19488. ' function Sub(Self) {',
  19489. ' $mod.THelper.Foo.call($Self, 1);',
  19490. ' $mod.THelper.Foo.call($Self, 1);',
  19491. ' $mod.THelper.Foo.call(Self, 1);',
  19492. ' $mod.THelper.Foo.call(Self, 1);',
  19493. ' $mod.THelper.Foo.call(Self, 1);',
  19494. ' $mod.THelper.Foo.call(Self, 1);',
  19495. ' };',
  19496. ' };',
  19497. '});',
  19498. 'rtl.createHelper($mod, "THelper", null, function () {',
  19499. ' this.Foo = function (w) {',
  19500. ' var $Self = this;',
  19501. ' var Result = 0;',
  19502. ' function Sub(Self) {',
  19503. ' $Self.Run(10);',
  19504. ' $Self.Run(10);',
  19505. ' $mod.THelper.Foo.call($Self, 1);',
  19506. ' $mod.THelper.Foo.call($Self, 1);',
  19507. ' $mod.THelper.Foo.call(Self, 1);',
  19508. ' $mod.THelper.Foo.call(Self, 1);',
  19509. ' $mod.THelper.Foo.call(Self, 1);',
  19510. ' $mod.THelper.Foo.call(Self, 1);',
  19511. ' };',
  19512. ' return Result;',
  19513. ' };',
  19514. '});',
  19515. '']),
  19516. LinesToStr([ // $mod.$main
  19517. '']));
  19518. end;
  19519. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  19520. begin
  19521. StartProgram(false);
  19522. Add([
  19523. 'type',
  19524. ' TObject = class',
  19525. ' class procedure Run(w: word = 10);',
  19526. ' end;',
  19527. ' THelper = class helper for TObject',
  19528. ' class function Foo(w: word = 1): word;',
  19529. ' end;',
  19530. 'class procedure TObject.Run(w: word);',
  19531. 'begin',
  19532. ' Foo;',
  19533. ' Foo();',
  19534. ' Self.Foo;',
  19535. ' Self.Foo();',
  19536. ' with Self do begin',
  19537. ' Foo;',
  19538. ' Foo();',
  19539. ' end;',
  19540. 'end;',
  19541. 'class function THelper.foo(w: word): word;',
  19542. 'begin',
  19543. ' Run;',
  19544. ' Run();',
  19545. ' Foo;',
  19546. ' Foo();',
  19547. ' Self.Foo;',
  19548. ' Self.Foo();',
  19549. ' with Self do begin',
  19550. ' Foo;',
  19551. ' Foo();',
  19552. ' end;',
  19553. 'end;',
  19554. 'var',
  19555. ' Obj: TObject;',
  19556. 'begin',
  19557. ' obj.Foo;',
  19558. ' obj.Foo();',
  19559. ' with obj do begin',
  19560. ' Foo;',
  19561. ' Foo();',
  19562. ' end;',
  19563. ' tobject.Foo;',
  19564. ' tobject.Foo();',
  19565. ' with tobject do begin',
  19566. ' Foo;',
  19567. ' Foo();',
  19568. ' end;',
  19569. '']);
  19570. ConvertProgram;
  19571. CheckSource('TestClassHelper_ClassMethod_Call',
  19572. LinesToStr([ // statements
  19573. 'rtl.createClass($mod, "TObject", null, function () {',
  19574. ' this.$init = function () {',
  19575. ' };',
  19576. ' this.$final = function () {',
  19577. ' };',
  19578. ' this.Run = function (w) {',
  19579. ' $mod.THelper.Foo.call(this, 1);',
  19580. ' $mod.THelper.Foo.call(this, 1);',
  19581. ' $mod.THelper.Foo.call(this, 1);',
  19582. ' $mod.THelper.Foo.call(this, 1);',
  19583. ' $mod.THelper.Foo.call(this, 1);',
  19584. ' $mod.THelper.Foo.call(this, 1);',
  19585. ' };',
  19586. '});',
  19587. 'rtl.createHelper($mod, "THelper", null, function () {',
  19588. ' this.Foo = function (w) {',
  19589. ' var Result = 0;',
  19590. ' this.Run(10);',
  19591. ' this.Run(10);',
  19592. ' $mod.THelper.Foo.call(this, 1);',
  19593. ' $mod.THelper.Foo.call(this, 1);',
  19594. ' $mod.THelper.Foo.call(this, 1);',
  19595. ' $mod.THelper.Foo.call(this, 1);',
  19596. ' $mod.THelper.Foo.call(this, 1);',
  19597. ' $mod.THelper.Foo.call(this, 1);',
  19598. ' return Result;',
  19599. ' };',
  19600. '});',
  19601. 'this.Obj = null;',
  19602. '']),
  19603. LinesToStr([ // $mod.$main
  19604. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19605. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19606. 'var $with1 = $mod.Obj;',
  19607. '$mod.THelper.Foo.call($with1.$class, 1);',
  19608. '$mod.THelper.Foo.call($with1.$class, 1);',
  19609. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19610. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19611. 'var $with2 = $mod.TObject;',
  19612. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19613. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19614. '']));
  19615. end;
  19616. procedure TTestModule.TestClassHelper_ClassOf;
  19617. begin
  19618. StartProgram(false);
  19619. Add([
  19620. 'type',
  19621. ' TObject = class',
  19622. ' end;',
  19623. ' TClass = class of TObject;',
  19624. ' THelper = class helper for TObject',
  19625. ' class function Foo(w: word = 1): word;',
  19626. ' end;',
  19627. 'class function THelper.foo(w: word): word;',
  19628. 'begin',
  19629. 'end;',
  19630. 'var',
  19631. ' c: TClass;',
  19632. 'begin',
  19633. ' c.Foo;',
  19634. ' c.Foo();',
  19635. ' with c do begin',
  19636. ' Foo;',
  19637. ' Foo();',
  19638. ' end;',
  19639. '']);
  19640. ConvertProgram;
  19641. CheckSource('TestClassHelper_ClassOf',
  19642. LinesToStr([ // statements
  19643. 'rtl.createClass($mod, "TObject", null, function () {',
  19644. ' this.$init = function () {',
  19645. ' };',
  19646. ' this.$final = function () {',
  19647. ' };',
  19648. '});',
  19649. 'rtl.createHelper($mod, "THelper", null, function () {',
  19650. ' this.Foo = function (w) {',
  19651. ' var Result = 0;',
  19652. ' return Result;',
  19653. ' };',
  19654. '});',
  19655. 'this.c = null;',
  19656. '']),
  19657. LinesToStr([ // $mod.$main
  19658. '$mod.THelper.Foo.call($mod.c, 1);',
  19659. '$mod.THelper.Foo.call($mod.c, 1);',
  19660. 'var $with1 = $mod.c;',
  19661. '$mod.THelper.Foo.call($with1, 1);',
  19662. '$mod.THelper.Foo.call($with1, 1);',
  19663. '']));
  19664. end;
  19665. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  19666. begin
  19667. StartProgram(false);
  19668. Add([
  19669. '{$mode objfpc}',
  19670. 'type',
  19671. ' TObject = class',
  19672. ' procedure DoIt;',
  19673. ' end;',
  19674. ' THelper = class helper for TObject',
  19675. ' procedure Fly(w: word = 1);',
  19676. ' class procedure Glide(w: word = 1);',
  19677. ' class procedure Run(w: word = 1); static;',
  19678. ' end;',
  19679. ' TFly = procedure(w: word) of object;',
  19680. ' TGlide = TFly;',
  19681. ' TRun = procedure(w: word);',
  19682. 'var',
  19683. ' f: TFly;',
  19684. ' g: TGlide;',
  19685. ' r: TRun;',
  19686. 'procedure TObject.DoIt;',
  19687. 'begin',
  19688. ' f:=@fly;',
  19689. ' g:=@glide;',
  19690. ' r:=@run;',
  19691. ' f:[email protected];',
  19692. ' g:[email protected];',
  19693. ' r:[email protected];',
  19694. ' with self do begin',
  19695. ' f:=@fly;',
  19696. ' g:=@glide;',
  19697. ' r:=@run;',
  19698. ' end;',
  19699. 'end;',
  19700. 'procedure THelper.fly(w: word);',
  19701. 'begin',
  19702. ' f:=@fly;',
  19703. ' g:=@glide;',
  19704. ' r:=@run;',
  19705. 'end;',
  19706. 'class procedure THelper.glide(w: word);',
  19707. 'begin',
  19708. ' g:=@glide;',
  19709. ' r:=@run;',
  19710. 'end;',
  19711. 'class procedure THelper.run(w: word);',
  19712. 'begin',
  19713. ' g:=@glide;',
  19714. ' r:=@run;',
  19715. 'end;',
  19716. 'var',
  19717. ' Obj: TObject;',
  19718. 'begin',
  19719. ' f:[email protected];',
  19720. ' g:[email protected];',
  19721. ' r:[email protected];',
  19722. ' with obj do begin',
  19723. ' f:=@fly;',
  19724. ' g:=@glide;',
  19725. ' r:=@run;',
  19726. ' end;',
  19727. ' g:[email protected];',
  19728. ' r:[email protected];',
  19729. ' with tobject do begin',
  19730. ' g:=@glide;',
  19731. ' r:=@run;',
  19732. ' end;',
  19733. '']);
  19734. ConvertProgram;
  19735. CheckSource('TestClassHelper_MethodRefObjFPC',
  19736. LinesToStr([ // statements
  19737. 'rtl.createClass($mod, "TObject", null, function () {',
  19738. ' this.$init = function () {',
  19739. ' };',
  19740. ' this.$final = function () {',
  19741. ' };',
  19742. ' this.DoIt = function () {',
  19743. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19744. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19745. ' $mod.r = $mod.THelper.Run;',
  19746. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19747. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19748. ' $mod.r = $mod.THelper.Run;',
  19749. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19750. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19751. ' $mod.r = $mod.THelper.Run;',
  19752. ' };',
  19753. '});',
  19754. 'rtl.createHelper($mod, "THelper", null, function () {',
  19755. ' this.Fly = function (w) {',
  19756. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19757. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19758. ' $mod.r = $mod.THelper.Run;',
  19759. ' };',
  19760. ' this.Glide = function (w) {',
  19761. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  19762. ' $mod.r = $mod.THelper.Run;',
  19763. ' };',
  19764. ' this.Run = function (w) {',
  19765. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  19766. ' $mod.r = $mod.THelper.Run;',
  19767. ' };',
  19768. '});',
  19769. 'this.f = null;',
  19770. 'this.g = null;',
  19771. 'this.r = null;',
  19772. 'this.Obj = null;',
  19773. '']),
  19774. LinesToStr([ // $mod.$main
  19775. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  19776. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  19777. '$mod.r = $mod.THelper.Run;',
  19778. 'var $with1 = $mod.Obj;',
  19779. '$mod.f = rtl.createCallback($with1, $mod.THelper.Fly);',
  19780. '$mod.g = rtl.createCallback($with1.$class, $mod.THelper.Glide);',
  19781. '$mod.r = $mod.THelper.Run;',
  19782. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  19783. '$mod.r = $mod.THelper.Run;',
  19784. 'var $with2 = $mod.TObject;',
  19785. '$mod.g = rtl.createCallback($with2, $mod.THelper.Glide);',
  19786. '$mod.r = $mod.THelper.Run;',
  19787. '']));
  19788. end;
  19789. procedure TTestModule.TestClassHelper_Constructor;
  19790. begin
  19791. StartProgram(false);
  19792. Add([
  19793. 'type',
  19794. ' TObject = class',
  19795. ' constructor Create;',
  19796. ' end;',
  19797. ' TClass = class of TObject;',
  19798. ' THelper = class helper for TObject',
  19799. ' constructor NewHlp(w: word);',
  19800. ' end;',
  19801. 'var',
  19802. ' obj: TObject;',
  19803. ' c: TClass;',
  19804. 'constructor TObject.Create;',
  19805. 'begin',
  19806. ' NewHlp(2);', // normal call
  19807. ' tobject.NewHlp(3);', // new instance
  19808. ' c.newhlp(4);', // new instance
  19809. 'end;',
  19810. 'constructor THelper.NewHlp(w: word);',
  19811. 'begin',
  19812. ' create;', // normal call
  19813. ' tobject.create;', // new instance
  19814. ' NewHlp(2);', // normal call
  19815. ' tobject.NewHlp(3);', // new instance
  19816. ' c.newhlp(4);', // new instance
  19817. 'end;',
  19818. 'begin',
  19819. ' obj.newhlp(2);', // normal call
  19820. ' with Obj do newhlp(12);', // normal call
  19821. ' tobject.newhlp(3);', // new instance
  19822. ' with tobject do newhlp(13);', // new instance
  19823. ' c.newhlp(4);', // new instance
  19824. ' with c do newhlp(14);', // new instance
  19825. '']);
  19826. ConvertProgram;
  19827. CheckSource('TestClassHelper_Constructor',
  19828. LinesToStr([ // statements
  19829. 'rtl.createClass($mod, "TObject", null, function () {',
  19830. ' this.$init = function () {',
  19831. ' };',
  19832. ' this.$final = function () {',
  19833. ' };',
  19834. ' this.Create = function () {',
  19835. ' $mod.THelper.NewHlp.call(this, 2);',
  19836. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19837. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19838. ' return this;',
  19839. ' };',
  19840. '});',
  19841. 'rtl.createHelper($mod, "THelper", null, function () {',
  19842. ' this.NewHlp = function (w) {',
  19843. ' this.Create();',
  19844. ' $mod.TObject.$create("Create");',
  19845. ' $mod.THelper.NewHlp.call(this, 2);',
  19846. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19847. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19848. ' return this;',
  19849. ' };',
  19850. '});',
  19851. 'this.obj = null;',
  19852. 'this.c = null;',
  19853. '']),
  19854. LinesToStr([ // $mod.$main
  19855. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  19856. 'var $with1 = $mod.obj;',
  19857. '$mod.THelper.NewHlp.call($with1, 12);',
  19858. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19859. 'var $with2 = $mod.TObject;',
  19860. '$with2.$create($mod.THelper.NewHlp, [13]);',
  19861. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  19862. 'var $with3 = $mod.c;',
  19863. '$with3.$create($mod.THelper.NewHlp, [14]);',
  19864. '']));
  19865. end;
  19866. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  19867. begin
  19868. StartProgram(false);
  19869. Add([
  19870. 'type',
  19871. ' TObject = class',
  19872. ' procedure Fly;',
  19873. ' end;',
  19874. ' TObjHelper = class helper for TObject',
  19875. ' procedure Fly;',
  19876. ' end;',
  19877. ' TBird = class',
  19878. ' procedure Fly;',
  19879. ' end;',
  19880. ' TBirdHelper = class helper for TBird',
  19881. ' procedure Fly;',
  19882. ' procedure Walk(w: word);',
  19883. ' end;',
  19884. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  19885. ' procedure Fly;',
  19886. ' procedure Walk(w: word);',
  19887. ' end;',
  19888. 'procedure Tobject.fly;',
  19889. 'begin',
  19890. ' inherited;', // ignore
  19891. 'end;',
  19892. 'procedure Tobjhelper.fly;',
  19893. 'begin',
  19894. ' {@TObject_Fly}inherited;',
  19895. ' inherited {@TObject_Fly}Fly;',
  19896. 'end;',
  19897. 'procedure Tbird.fly;',
  19898. 'begin',
  19899. ' {@TObjHelper_Fly}inherited;',
  19900. ' inherited {@TObjHelper_Fly}Fly;',
  19901. 'end;',
  19902. 'procedure Tbirdhelper.fly;',
  19903. 'begin',
  19904. ' {@TBird_Fly}inherited;',
  19905. ' inherited {@TBird_Fly}Fly;',
  19906. 'end;',
  19907. 'procedure Tbirdhelper.walk(w: word);',
  19908. 'begin',
  19909. 'end;',
  19910. 'procedure teagleHelper.fly;',
  19911. 'begin',
  19912. ' {@TBird_Fly}inherited;',
  19913. ' inherited {@TBird_Fly}Fly;',
  19914. 'end;',
  19915. 'procedure teagleHelper.walk(w: word);',
  19916. 'begin',
  19917. ' {@TBirdHelper_Walk}inherited;',
  19918. ' inherited {@TBirdHelper_Walk}Walk(3);',
  19919. 'end;',
  19920. 'begin',
  19921. '']);
  19922. ConvertProgram;
  19923. CheckSource('TestClassHelper_InheritedObjFPC',
  19924. LinesToStr([ // statements
  19925. 'rtl.createClass($mod, "TObject", null, function () {',
  19926. ' this.$init = function () {',
  19927. ' };',
  19928. ' this.$final = function () {',
  19929. ' };',
  19930. ' this.Fly = function () {',
  19931. ' };',
  19932. '});',
  19933. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19934. ' this.Fly = function () {',
  19935. ' $mod.TObject.Fly.call(this);',
  19936. ' $mod.TObject.Fly.call(this);',
  19937. ' };',
  19938. '});',
  19939. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19940. ' this.Fly$1 = function () {',
  19941. ' $mod.TObjHelper.Fly.call(this);',
  19942. ' $mod.TObjHelper.Fly.call(this);',
  19943. ' };',
  19944. '});',
  19945. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  19946. ' this.Fly = function () {',
  19947. ' $mod.TBird.Fly$1.call(this);',
  19948. ' $mod.TBird.Fly$1.call(this);',
  19949. ' };',
  19950. ' this.Walk = function (w) {',
  19951. ' };',
  19952. '});',
  19953. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  19954. ' this.Fly$1 = function () {',
  19955. ' $mod.TBird.Fly$1.call(this);',
  19956. ' $mod.TBird.Fly$1.call(this);',
  19957. ' };',
  19958. ' this.Walk$1 = function (w) {',
  19959. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  19960. ' $mod.TBirdHelper.Walk.call(this, 3);',
  19961. ' };',
  19962. '});',
  19963. '']),
  19964. LinesToStr([ // $mod.$main
  19965. '']));
  19966. end;
  19967. procedure TTestModule.TestClassHelper_Property;
  19968. begin
  19969. StartProgram(false);
  19970. Add([
  19971. 'type',
  19972. ' TObject = class',
  19973. ' FSize: word;',
  19974. ' function GetSpeed: word;',
  19975. ' procedure SetSpeed(Value: word);',
  19976. ' end;',
  19977. ' TObjHelper = class helper for TObject',
  19978. ' function GetLeft: word;',
  19979. ' procedure SetLeft(Value: word);',
  19980. ' property Size: word read FSize write FSize;',
  19981. ' property Speed: word read GetSpeed write SetSpeed;',
  19982. ' property Left: word read GetLeft write SetLeft;',
  19983. ' end;',
  19984. ' TBird = class',
  19985. ' property NotRight: word read GetLeft write SetLeft;',
  19986. ' procedure DoIt;',
  19987. ' end;',
  19988. 'var',
  19989. ' b: TBird;',
  19990. 'function Tobject.GetSpeed: word;',
  19991. 'begin',
  19992. ' Size:=Size+11;',
  19993. ' Speed:=Speed+12;',
  19994. ' Result:=Left+13;',
  19995. ' Left:=13;',
  19996. ' Left:=Left+13;',
  19997. ' Self.Size:=Self.Size+21;',
  19998. ' Self.Speed:=Self.Speed+22;',
  19999. ' Self.Left:=Self.Left+23;',
  20000. ' with Self do begin',
  20001. ' Size:=Size+31;',
  20002. ' Speed:=Speed+32;',
  20003. ' Left:=Left+33;',
  20004. ' end;',
  20005. 'end;',
  20006. 'procedure Tobject.SetSpeed(Value: word);',
  20007. 'begin',
  20008. 'end;',
  20009. 'function TObjHelper.GetLeft: word;',
  20010. 'begin',
  20011. ' Size:=Size+11;',
  20012. ' Speed:=Speed+12;',
  20013. ' Left:=Left+13;',
  20014. ' Self.Size:=Self.Size+21;',
  20015. ' Self.Speed:=Self.Speed+22;',
  20016. ' Self.Left:=Self.Left+23;',
  20017. ' with Self do begin',
  20018. ' Size:=Size+31;',
  20019. ' Speed:=Speed+32;',
  20020. ' Left:=Left+33;',
  20021. ' end;',
  20022. 'end;',
  20023. 'procedure TObjHelper.SetLeft(Value: word);',
  20024. 'begin',
  20025. 'end;',
  20026. 'procedure TBird.DoIt;',
  20027. 'begin',
  20028. ' NotRight:=NotRight+11;',
  20029. ' Self.NotRight:=Self.NotRight+21;',
  20030. ' with Self do begin',
  20031. ' NotRight:=NotRight+31;',
  20032. ' end;',
  20033. 'end;',
  20034. 'begin',
  20035. ' b.Size:=b.Size+11;',
  20036. ' b.Speed:=b.Speed+12;',
  20037. ' b.Left:=b.Left+13;',
  20038. ' b.NotRight:=b.NotRight+14;',
  20039. ' with b do begin',
  20040. ' Size:=Size+31;',
  20041. ' Speed:=Speed+32;',
  20042. ' Left:=Left+33;',
  20043. ' NotRight:=NotRight+34;',
  20044. ' end;',
  20045. '']);
  20046. ConvertProgram;
  20047. CheckSource('TestClassHelper_Property',
  20048. LinesToStr([ // statements
  20049. 'rtl.createClass($mod, "TObject", null, function () {',
  20050. ' this.$init = function () {',
  20051. ' this.FSize = 0;',
  20052. ' };',
  20053. ' this.$final = function () {',
  20054. ' };',
  20055. ' this.GetSpeed = function () {',
  20056. ' var Result = 0;',
  20057. ' this.FSize = this.FSize + 11;',
  20058. ' this.SetSpeed(this.GetSpeed() + 12);',
  20059. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  20060. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  20061. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20062. ' this.FSize = this.FSize + 21;',
  20063. ' this.SetSpeed(this.GetSpeed() + 22);',
  20064. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20065. ' this.FSize = this.FSize + 31;',
  20066. ' this.SetSpeed(this.GetSpeed() + 32);',
  20067. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20068. ' return Result;',
  20069. ' };',
  20070. ' this.SetSpeed = function (Value) {',
  20071. ' };',
  20072. '});',
  20073. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20074. ' this.GetLeft = function () {',
  20075. ' var Result = 0;',
  20076. ' this.FSize = this.FSize + 11;',
  20077. ' this.SetSpeed(this.GetSpeed() + 12);',
  20078. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20079. ' this.FSize = this.FSize + 21;',
  20080. ' this.SetSpeed(this.GetSpeed() + 22);',
  20081. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20082. ' this.FSize = this.FSize + 31;',
  20083. ' this.SetSpeed(this.GetSpeed() + 32);',
  20084. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20085. ' return Result;',
  20086. ' };',
  20087. ' this.SetLeft = function (Value) {',
  20088. ' };',
  20089. '});',
  20090. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20091. ' this.DoIt = function () {',
  20092. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20093. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20094. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20095. ' };',
  20096. '});',
  20097. 'this.b = null;',
  20098. '']),
  20099. LinesToStr([ // $mod.$main
  20100. '$mod.b.FSize = $mod.b.FSize + 11;',
  20101. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20102. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  20103. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  20104. 'var $with1 = $mod.b;',
  20105. '$with1.FSize = $with1.FSize + 31;',
  20106. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20107. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  20108. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  20109. '']));
  20110. end;
  20111. procedure TTestModule.TestClassHelper_Property_Array;
  20112. begin
  20113. StartProgram(false);
  20114. Add([
  20115. 'type',
  20116. ' TObject = class',
  20117. ' function GetSpeed(Index: boolean): word;',
  20118. ' procedure SetSpeed(Index: boolean; Value: word);',
  20119. ' end;',
  20120. ' TObjHelper = class helper for TObject',
  20121. ' function GetSize(Index: boolean): word;',
  20122. ' procedure SetSize(Index: boolean; Value: word);',
  20123. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  20124. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20125. ' end;',
  20126. ' TBird = class',
  20127. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  20128. ' procedure DoIt;',
  20129. ' end;',
  20130. 'var',
  20131. ' b: TBird;',
  20132. 'function Tobject.GetSpeed(Index: boolean): word;',
  20133. 'begin',
  20134. ' Result:=Size[false];',
  20135. ' Size[true]:=Size[false]+11;',
  20136. ' Speed[true]:=Speed[false]+12;',
  20137. ' Self.Size[true]:=Self.Size[false]+21;',
  20138. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20139. ' with Self do begin',
  20140. ' Size[true]:=Size[false]+31;',
  20141. ' Speed[true]:=Speed[false]+32;',
  20142. ' end;',
  20143. 'end;',
  20144. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20145. 'begin',
  20146. 'end;',
  20147. 'function TObjHelper.GetSize(Index: boolean): word;',
  20148. 'begin',
  20149. ' Size[true]:=Size[false]+11;',
  20150. ' Speed[true]:=Speed[false]+12;',
  20151. ' Self.Size[true]:=Self.Size[false]+21;',
  20152. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20153. ' with Self do begin',
  20154. ' Size[true]:=Size[false]+31;',
  20155. ' Speed[true]:=Speed[false]+32;',
  20156. ' end;',
  20157. 'end;',
  20158. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20159. 'begin',
  20160. 'end;',
  20161. 'procedure TBird.DoIt;',
  20162. 'begin',
  20163. ' Items[true]:=Items[false]+11;',
  20164. ' Self.Items[true]:=Self.Items[false]+21;',
  20165. ' with Self do Items[true]:=Items[false]+31;',
  20166. 'end;',
  20167. 'begin',
  20168. ' b.Size[true]:=b.Size[false]+11;',
  20169. ' b.Speed[true]:=b.Speed[false]+12;',
  20170. ' b.Items[true]:=b.Items[false]+13;',
  20171. ' with b do begin',
  20172. ' Size[true]:=Size[false]+21;',
  20173. ' Speed[true]:=Speed[false]+22;',
  20174. ' Items[true]:=Items[false]+23;',
  20175. ' end;',
  20176. '']);
  20177. ConvertProgram;
  20178. CheckSource('TestClassHelper_Property_Array',
  20179. LinesToStr([ // statements
  20180. 'rtl.createClass($mod, "TObject", null, function () {',
  20181. ' this.$init = function () {',
  20182. ' };',
  20183. ' this.$final = function () {',
  20184. ' };',
  20185. ' this.GetSpeed = function (Index) {',
  20186. ' var Result = 0;',
  20187. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  20188. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20189. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20190. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20191. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20192. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20193. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20194. ' return Result;',
  20195. ' };',
  20196. ' this.SetSpeed = function (Index, Value) {',
  20197. ' };',
  20198. '});',
  20199. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20200. ' this.GetSize = function (Index) {',
  20201. ' var Result = 0;',
  20202. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20203. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20204. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20205. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20206. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20207. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20208. ' return Result;',
  20209. ' };',
  20210. ' this.SetSize = function (Index, Value) {',
  20211. ' };',
  20212. '});',
  20213. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20214. ' this.DoIt = function () {',
  20215. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20216. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20217. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20218. ' };',
  20219. '});',
  20220. 'this.b = null;',
  20221. '']),
  20222. LinesToStr([ // $mod.$main
  20223. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  20224. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  20225. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  20226. 'var $with1 = $mod.b;',
  20227. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  20228. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  20229. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  20230. '']));
  20231. end;
  20232. procedure TTestModule.TestClassHelper_Property_Array_Default;
  20233. begin
  20234. StartProgram(false);
  20235. Add([
  20236. 'type',
  20237. ' TObject = class',
  20238. ' function GetSpeed(Index: boolean): word;',
  20239. ' procedure SetSpeed(Index: boolean; Value: word);',
  20240. ' end;',
  20241. ' TObjHelper = class helper for TObject',
  20242. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  20243. ' end;',
  20244. ' TBird = class',
  20245. ' end;',
  20246. ' TBirdHelper = class helper for TBird',
  20247. ' function GetSize(Index: word): boolean;',
  20248. ' procedure SetSize(Index: word; Value: boolean);',
  20249. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  20250. ' end;',
  20251. 'function Tobject.GetSpeed(Index: boolean): word;',
  20252. 'begin',
  20253. ' Self[true]:=Self[false]+1;',
  20254. 'end;',
  20255. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20256. 'begin',
  20257. 'end;',
  20258. 'function TBirdHelper.GetSize(Index: word): boolean;',
  20259. 'begin',
  20260. ' Self[1]:=not Self[2];',
  20261. 'end;',
  20262. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  20263. 'begin',
  20264. 'end;',
  20265. 'var',
  20266. ' o: TObject;',
  20267. ' b: TBird;',
  20268. 'begin',
  20269. ' o[true]:=o[false]+1;',
  20270. ' b[3]:=not b[4];',
  20271. '']);
  20272. ConvertProgram;
  20273. CheckSource('TestClassHelper_Property_Array_Default',
  20274. LinesToStr([ // statements
  20275. 'rtl.createClass($mod, "TObject", null, function () {',
  20276. ' this.$init = function () {',
  20277. ' };',
  20278. ' this.$final = function () {',
  20279. ' };',
  20280. ' this.GetSpeed = function (Index) {',
  20281. ' var Result = 0;',
  20282. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  20283. ' return Result;',
  20284. ' };',
  20285. ' this.SetSpeed = function (Index, Value) {',
  20286. ' };',
  20287. '});',
  20288. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20289. '});',
  20290. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20291. '});',
  20292. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20293. ' this.GetSize = function (Index) {',
  20294. ' var Result = false;',
  20295. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  20296. ' return Result;',
  20297. ' };',
  20298. ' this.SetSize = function (Index, Value) {',
  20299. ' };',
  20300. '});',
  20301. 'this.o = null;',
  20302. 'this.b = null;',
  20303. '']),
  20304. LinesToStr([ // $mod.$main
  20305. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  20306. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  20307. '']));
  20308. end;
  20309. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  20310. begin
  20311. StartProgram(false);
  20312. Add([
  20313. 'type',
  20314. ' TObject = class',
  20315. ' end;',
  20316. ' TObjHelper = class helper for TObject',
  20317. ' function GetItems(Index: word): TObject;',
  20318. ' procedure SetItems(Index: word; Value: TObject);',
  20319. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  20320. ' end;',
  20321. 'function Tobjhelper.GetItems(Index: word): TObject;',
  20322. 'begin',
  20323. ' Self[1][2]:=Self[3][4];',
  20324. 'end;',
  20325. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  20326. 'begin',
  20327. 'end;',
  20328. 'var',
  20329. ' o: TObject;',
  20330. 'begin',
  20331. ' o[1][2]:=o[3][4];',
  20332. '']);
  20333. ConvertProgram;
  20334. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  20335. LinesToStr([ // statements
  20336. 'rtl.createClass($mod, "TObject", null, function () {',
  20337. ' this.$init = function () {',
  20338. ' };',
  20339. ' this.$final = function () {',
  20340. ' };',
  20341. '});',
  20342. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20343. ' this.GetItems = function (Index) {',
  20344. ' var Result = null;',
  20345. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  20346. ' return Result;',
  20347. ' };',
  20348. ' this.SetItems = function (Index, Value) {',
  20349. ' };',
  20350. '});',
  20351. 'this.o = null;',
  20352. '']),
  20353. LinesToStr([ // $mod.$main
  20354. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  20355. '']));
  20356. end;
  20357. procedure TTestModule.TestClassHelper_ClassProperty;
  20358. begin
  20359. StartProgram(false);
  20360. Add([
  20361. 'type',
  20362. ' TObject = class',
  20363. ' class var FSize: word;',
  20364. ' class function GetSpeed: word;',
  20365. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  20366. ' end;',
  20367. ' TObjHelper = class helper for TObject',
  20368. ' class function GetLeft: word;',
  20369. ' class procedure SetLeft(Value: word);',
  20370. ' class property Size: word read FSize write FSize;',
  20371. ' class property Speed: word read GetSpeed write SetSpeed;',
  20372. ' class property Left: word read GetLeft write SetLeft;',
  20373. ' end;',
  20374. ' TBird = class',
  20375. ' class property NotRight: word read GetLeft write SetLeft;',
  20376. ' class procedure DoIt;',
  20377. ' end;',
  20378. ' TBirdClass = class of TBird;',
  20379. 'class function Tobject.GetSpeed: word;',
  20380. 'begin',
  20381. ' Size:=Size+11;',
  20382. ' Speed:=Speed+12;',
  20383. ' Left:=Left+13;',
  20384. ' Self.Size:=Self.Size+21;',
  20385. ' Self.Speed:=Self.Speed+22;',
  20386. ' Self.Left:=Self.Left+23;',
  20387. ' with Self do begin',
  20388. ' Size:=Size+31;',
  20389. ' Speed:=Speed+32;',
  20390. ' Left:=Left+33;',
  20391. ' end;',
  20392. 'end;',
  20393. 'class function TObjHelper.GetLeft: word;',
  20394. 'begin',
  20395. ' Size:=Size+11;',
  20396. ' Speed:=Speed+12;',
  20397. ' Left:=Left+13;',
  20398. ' Self.Size:=Self.Size+21;',
  20399. ' Self.Speed:=Self.Speed+22;',
  20400. ' Self.Left:=Self.Left+23;',
  20401. ' with Self do begin',
  20402. ' Size:=Size+31;',
  20403. ' Speed:=Speed+32;',
  20404. ' Left:=Left+33;',
  20405. ' end;',
  20406. 'end;',
  20407. 'class procedure TObjHelper.SetLeft(Value: word);',
  20408. 'begin',
  20409. 'end;',
  20410. 'class procedure TBird.DoIt;',
  20411. 'begin',
  20412. ' NotRight:=NotRight+11;',
  20413. ' Self.NotRight:=Self.NotRight+21;',
  20414. ' with Self do NotRight:=NotRight+31;',
  20415. 'end;',
  20416. 'var',
  20417. ' b: TBird;',
  20418. ' c: TBirdClass;',
  20419. 'begin',
  20420. ' b.Size:=b.Size+11;',
  20421. ' b.Speed:=b.Speed+12;',
  20422. ' b.Left:=b.Left+13;',
  20423. ' b.NotRight:=b.NotRight+14;',
  20424. ' with b do begin',
  20425. ' Size:=Size+31;',
  20426. ' Speed:=Speed+32;',
  20427. ' Left:=Left+33;',
  20428. ' NotRight:=NotRight+34;',
  20429. ' end;',
  20430. ' c.Size:=c.Size+11;',
  20431. ' c.Speed:=c.Speed+12;',
  20432. ' c.Left:=c.Left+13;',
  20433. ' c.NotRight:=c.NotRight+14;',
  20434. ' with c do begin',
  20435. ' Size:=Size+31;',
  20436. ' Speed:=Speed+32;',
  20437. ' Left:=Left+33;',
  20438. ' NotRight:=NotRight+34;',
  20439. ' end;',
  20440. ' tbird.Size:=tbird.Size+11;',
  20441. ' tbird.Speed:=tbird.Speed+12;',
  20442. ' tbird.Left:=tbird.Left+13;',
  20443. ' tbird.NotRight:=tbird.NotRight+14;',
  20444. ' with tbird do begin',
  20445. ' Size:=Size+31;',
  20446. ' Speed:=Speed+32;',
  20447. ' Left:=Left+33;',
  20448. ' NotRight:=NotRight+34;',
  20449. ' end;',
  20450. '']);
  20451. ConvertProgram;
  20452. CheckSource('TestClassHelper_ClassProperty',
  20453. LinesToStr([ // statements
  20454. 'rtl.createClass($mod, "TObject", null, function () {',
  20455. ' this.FSize = 0;',
  20456. ' this.$init = function () {',
  20457. ' };',
  20458. ' this.$final = function () {',
  20459. ' };',
  20460. ' this.GetSpeed = function () {',
  20461. ' var Result = 0;',
  20462. ' $mod.TObject.FSize = this.FSize + 11;',
  20463. ' this.SetSpeed(this.GetSpeed() + 12);',
  20464. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20465. ' $mod.TObject.FSize = this.FSize + 21;',
  20466. ' this.SetSpeed(this.GetSpeed() + 22);',
  20467. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20468. ' $mod.TObject.FSize = this.FSize + 31;',
  20469. ' this.SetSpeed(this.GetSpeed() + 32);',
  20470. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20471. ' return Result;',
  20472. ' };',
  20473. '});',
  20474. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20475. ' this.GetLeft = function () {',
  20476. ' var Result = 0;',
  20477. ' $mod.TObject.FSize = this.FSize + 11;',
  20478. ' this.SetSpeed(this.GetSpeed() + 12);',
  20479. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20480. ' $mod.TObject.FSize = this.FSize + 21;',
  20481. ' this.SetSpeed(this.GetSpeed() + 22);',
  20482. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20483. ' $mod.TObject.FSize = this.FSize + 31;',
  20484. ' this.SetSpeed(this.GetSpeed() + 32);',
  20485. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20486. ' return Result;',
  20487. ' };',
  20488. ' this.SetLeft = function (Value) {',
  20489. ' };',
  20490. '});',
  20491. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20492. ' this.DoIt = function () {',
  20493. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20494. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20495. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20496. ' };',
  20497. '});',
  20498. 'this.b = null;',
  20499. 'this.c = null;',
  20500. '']),
  20501. LinesToStr([ // $mod.$main
  20502. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  20503. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  20504. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  20505. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  20506. 'var $with1 = $mod.b;',
  20507. '$mod.TObject.FSize = $with1.FSize + 31;',
  20508. '$with1.$class.SetSpeed($with1.$class.GetSpeed() + 32);',
  20509. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 33);',
  20510. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 34);',
  20511. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  20512. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20513. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  20514. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  20515. 'var $with2 = $mod.c;',
  20516. '$mod.TObject.FSize = $with2.FSize + 31;',
  20517. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20518. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 33);',
  20519. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 34);',
  20520. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  20521. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20522. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  20523. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  20524. 'var $with3 = $mod.TBird;',
  20525. '$mod.TObject.FSize = $with3.FSize + 31;',
  20526. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20527. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  20528. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  20529. '']));
  20530. end;
  20531. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  20532. begin
  20533. StartProgram(false);
  20534. Add([
  20535. 'type',
  20536. ' TObject = class',
  20537. ' class function GetSpeed: word; static;',
  20538. ' class procedure SetSpeed(Value: word); static;',
  20539. ' end;',
  20540. ' TObjHelper = class helper for TObject',
  20541. ' class function GetLeft: word; static;',
  20542. ' class procedure SetLeft(Value: word); static;',
  20543. ' class property Speed: word read GetSpeed write SetSpeed;',
  20544. ' class property Left: word read GetLeft write SetLeft;',
  20545. ' end;',
  20546. ' TBird = class',
  20547. ' class property NotRight: word read GetLeft write SetLeft;',
  20548. ' class procedure DoIt; static;',
  20549. ' class procedure DoSome;',
  20550. ' end;',
  20551. ' TBirdClass = class of TBird;',
  20552. 'class function Tobject.GetSpeed: word;',
  20553. 'begin',
  20554. ' Speed:=Speed+12;',
  20555. ' Left:=Left+13;',
  20556. 'end;',
  20557. 'class procedure TObject.SetSpeed(Value: word);',
  20558. 'begin',
  20559. 'end;',
  20560. 'class function TObjHelper.GetLeft: word;',
  20561. 'begin',
  20562. ' Speed:=Speed+12;',
  20563. ' Left:=Left+13;',
  20564. 'end;',
  20565. 'class procedure TObjHelper.SetLeft(Value: word);',
  20566. 'begin',
  20567. 'end;',
  20568. 'class procedure TBird.DoIt;',
  20569. 'begin',
  20570. ' NotRight:=NotRight+11;',
  20571. 'end;',
  20572. 'class procedure TBird.DoSome;',
  20573. 'begin',
  20574. ' Speed:=Speed+12;',
  20575. ' Left:=Left+13;',
  20576. ' Self.Speed:=Self.Speed+22;',
  20577. ' Self.Left:=Self.Left+23;',
  20578. ' with Self do begin',
  20579. ' Speed:=Speed+32;',
  20580. ' Left:=Left+33;',
  20581. ' end;',
  20582. ' NotRight:=NotRight+11;',
  20583. ' Self.NotRight:=Self.NotRight+21;',
  20584. ' with Self do NotRight:=NotRight+31;',
  20585. 'end;',
  20586. 'var',
  20587. ' b: TBird;',
  20588. ' c: TBirdClass;',
  20589. 'begin',
  20590. ' b.Speed:=b.Speed+12;',
  20591. ' b.Left:=b.Left+13;',
  20592. ' b.NotRight:=b.NotRight+14;',
  20593. ' with b do begin',
  20594. ' Speed:=Speed+32;',
  20595. ' Left:=Left+33;',
  20596. ' NotRight:=NotRight+34;',
  20597. ' end;',
  20598. ' c.Speed:=c.Speed+12;',
  20599. ' c.Left:=c.Left+13;',
  20600. ' c.NotRight:=c.NotRight+14;',
  20601. ' with c do begin',
  20602. ' Speed:=Speed+32;',
  20603. ' Left:=Left+33;',
  20604. ' NotRight:=NotRight+34;',
  20605. ' end;',
  20606. ' tbird.Speed:=tbird.Speed+12;',
  20607. ' tbird.Left:=tbird.Left+13;',
  20608. ' tbird.NotRight:=tbird.NotRight+14;',
  20609. ' with tbird do begin',
  20610. ' Speed:=Speed+32;',
  20611. ' Left:=Left+33;',
  20612. ' NotRight:=NotRight+34;',
  20613. ' end;',
  20614. '']);
  20615. ConvertProgram;
  20616. CheckSource('TestClassHelper_ClassPropertyStatic',
  20617. LinesToStr([ // statements
  20618. 'rtl.createClass($mod, "TObject", null, function () {',
  20619. ' this.$init = function () {',
  20620. ' };',
  20621. ' this.$final = function () {',
  20622. ' };',
  20623. ' this.GetSpeed = function () {',
  20624. ' var Result = 0;',
  20625. ' this.SetSpeed(this.GetSpeed() + 12);',
  20626. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20627. ' return Result;',
  20628. ' };',
  20629. ' this.SetSpeed = function (Value) {',
  20630. ' };',
  20631. '});',
  20632. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20633. ' this.GetLeft = function () {',
  20634. ' var Result = 0;',
  20635. ' this.SetSpeed(this.GetSpeed() + 12);',
  20636. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20637. ' return Result;',
  20638. ' };',
  20639. ' this.SetLeft = function (Value) {',
  20640. ' };',
  20641. '});',
  20642. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20643. ' this.DoIt = function () {',
  20644. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20645. ' };',
  20646. ' this.DoSome = function () {',
  20647. ' this.SetSpeed(this.GetSpeed() + 12);',
  20648. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20649. ' this.SetSpeed(this.GetSpeed() + 22);',
  20650. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  20651. ' this.SetSpeed(this.GetSpeed() + 32);',
  20652. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20653. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20654. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  20655. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  20656. ' };',
  20657. '});',
  20658. 'this.b = null;',
  20659. 'this.c = null;',
  20660. '']),
  20661. LinesToStr([ // $mod.$main
  20662. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20663. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20664. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20665. 'var $with1 = $mod.b;',
  20666. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20667. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20668. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20669. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20670. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20671. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20672. 'var $with2 = $mod.c;',
  20673. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20674. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20675. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20676. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20677. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20678. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20679. 'var $with3 = $mod.TBird;',
  20680. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20681. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20682. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20683. '']));
  20684. end;
  20685. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  20686. begin
  20687. StartProgram(false);
  20688. Add([
  20689. 'type',
  20690. ' TObject = class',
  20691. ' class function GetSpeed(Index: boolean): word;',
  20692. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  20693. ' end;',
  20694. ' TObjHelper = class helper for TObject',
  20695. ' class function GetSize(Index: boolean): word;',
  20696. ' class procedure SetSize(Index: boolean; Value: word);',
  20697. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  20698. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20699. ' end;',
  20700. ' TBird = class',
  20701. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  20702. ' class procedure DoIt;',
  20703. ' end;',
  20704. ' TBirdClass = class of TBird;',
  20705. 'class function Tobject.GetSpeed(Index: boolean): word;',
  20706. 'begin',
  20707. ' Size[true]:=Size[false]+11;',
  20708. ' Speed[true]:=Speed[false]+12;',
  20709. ' Self.Size[true]:=Self.Size[false]+21;',
  20710. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20711. ' with Self do begin',
  20712. ' Size[true]:=Size[false]+31;',
  20713. ' Speed[true]:=Speed[false]+32;',
  20714. ' end;',
  20715. 'end;',
  20716. 'class function TObjHelper.GetSize(Index: boolean): word;',
  20717. 'begin',
  20718. ' Size[true]:=Size[false]+11;',
  20719. ' Speed[true]:=Speed[false]+12;',
  20720. ' Self.Size[true]:=Self.Size[false]+21;',
  20721. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20722. ' with Self do begin',
  20723. ' Size[true]:=Size[false]+31;',
  20724. ' Speed[true]:=Speed[false]+32;',
  20725. ' end;',
  20726. 'end;',
  20727. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20728. 'begin',
  20729. 'end;',
  20730. 'class procedure TBird.DoIt;',
  20731. 'begin',
  20732. ' Items[true]:=Items[false]+11;',
  20733. ' Self.Items[true]:=Self.Items[false]+21;',
  20734. ' with Self do Items[true]:=Items[false]+31;',
  20735. 'end;',
  20736. 'var',
  20737. ' b: TBird;',
  20738. ' c: TBirdClass;',
  20739. 'begin',
  20740. ' b.Size[true]:=b.Size[false]+11;',
  20741. ' b.Speed[true]:=b.Speed[false]+12;',
  20742. ' b.Items[true]:=b.Items[false]+13;',
  20743. ' with b do begin',
  20744. ' Size[true]:=Size[false]+21;',
  20745. ' Speed[true]:=Speed[false]+22;',
  20746. ' Items[true]:=Items[false]+23;',
  20747. ' end;',
  20748. ' c.Size[true]:=c.Size[false]+11;',
  20749. ' c.Speed[true]:=c.Speed[false]+12;',
  20750. ' c.Items[true]:=c.Items[false]+13;',
  20751. ' with c do begin',
  20752. ' Size[true]:=Size[false]+21;',
  20753. ' Speed[true]:=Speed[false]+22;',
  20754. ' Items[true]:=Items[false]+23;',
  20755. ' end;',
  20756. ' TBird.Size[true]:=TBird.Size[false]+11;',
  20757. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  20758. ' TBird.Items[true]:=TBird.Items[false]+13;',
  20759. ' with TBird do begin',
  20760. ' Size[true]:=Size[false]+21;',
  20761. ' Speed[true]:=Speed[false]+22;',
  20762. ' Items[true]:=Items[false]+23;',
  20763. ' end;',
  20764. '']);
  20765. ConvertProgram;
  20766. CheckSource('TestClassHelper_ClassProperty_Array',
  20767. LinesToStr([ // statements
  20768. 'rtl.createClass($mod, "TObject", null, function () {',
  20769. ' this.$init = function () {',
  20770. ' };',
  20771. ' this.$final = function () {',
  20772. ' };',
  20773. ' this.GetSpeed = function (Index) {',
  20774. ' var Result = 0;',
  20775. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20776. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20777. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20778. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20779. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20780. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20781. ' return Result;',
  20782. ' };',
  20783. '});',
  20784. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20785. ' this.GetSize = function (Index) {',
  20786. ' var Result = 0;',
  20787. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20788. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20789. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20790. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20791. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20792. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20793. ' return Result;',
  20794. ' };',
  20795. ' this.SetSize = function (Index, Value) {',
  20796. ' };',
  20797. '});',
  20798. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20799. ' this.DoIt = function () {',
  20800. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20801. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20802. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20803. ' };',
  20804. '});',
  20805. 'this.b = null;',
  20806. 'this.c = null;',
  20807. '']),
  20808. LinesToStr([ // $mod.$main
  20809. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  20810. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  20811. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  20812. 'var $with1 = $mod.b;',
  20813. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 21);',
  20814. '$with1.$class.SetSpeed(true, $with1.$class.GetSpeed(false) + 22);',
  20815. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 23);',
  20816. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  20817. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  20818. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  20819. 'var $with2 = $mod.c;',
  20820. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 21);',
  20821. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  20822. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 23);',
  20823. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  20824. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  20825. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  20826. 'var $with3 = $mod.TBird;',
  20827. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  20828. '$with3.SetSpeed(true, $with3.GetSpeed(false) + 22);',
  20829. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  20830. '']));
  20831. end;
  20832. procedure TTestModule.TestClassHelper_ForIn;
  20833. begin
  20834. StartProgram(false);
  20835. Add([
  20836. 'type',
  20837. ' TObject = class end;',
  20838. ' TItem = TObject;',
  20839. ' TEnumerator = class',
  20840. ' FCurrent: TItem;',
  20841. ' property Current: TItem read FCurrent;',
  20842. ' function MoveNext: boolean;',
  20843. ' end;',
  20844. ' TBird = class',
  20845. ' end;',
  20846. ' TBirdHelper = class helper for TBird',
  20847. ' function GetEnumerator: TEnumerator;',
  20848. ' end;',
  20849. 'function TEnumerator.MoveNext: boolean;',
  20850. 'begin',
  20851. 'end;',
  20852. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  20853. 'begin',
  20854. 'end;',
  20855. 'var',
  20856. ' b: TBird;',
  20857. ' i, i2: TItem;',
  20858. 'begin',
  20859. ' for i in b do i2:=i;']);
  20860. ConvertProgram;
  20861. CheckSource('TestClassHelper_ForIn',
  20862. LinesToStr([ // statements
  20863. 'rtl.createClass($mod, "TObject", null, function () {',
  20864. ' this.$init = function () {',
  20865. ' };',
  20866. ' this.$final = function () {',
  20867. ' };',
  20868. '});',
  20869. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  20870. ' this.$init = function () {',
  20871. ' $mod.TObject.$init.call(this);',
  20872. ' this.FCurrent = null;',
  20873. ' };',
  20874. ' this.$final = function () {',
  20875. ' this.FCurrent = undefined;',
  20876. ' $mod.TObject.$final.call(this);',
  20877. ' };',
  20878. ' this.MoveNext = function () {',
  20879. ' var Result = false;',
  20880. ' return Result;',
  20881. ' };',
  20882. '});',
  20883. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20884. '});',
  20885. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20886. ' this.GetEnumerator = function () {',
  20887. ' var Result = null;',
  20888. ' return Result;',
  20889. ' };',
  20890. '});',
  20891. 'this.b = null;',
  20892. 'this.i = null;',
  20893. 'this.i2 = null;'
  20894. ]),
  20895. LinesToStr([ // $mod.$main
  20896. 'var $in1 = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  20897. 'try {',
  20898. ' while ($in1.MoveNext()){',
  20899. ' $mod.i = $in1.FCurrent;',
  20900. ' $mod.i2 = $mod.i;',
  20901. ' }',
  20902. '} finally {',
  20903. ' $in1 = rtl.freeLoc($in1)',
  20904. '};',
  20905. '']));
  20906. end;
  20907. procedure TTestModule.TestClassHelper_PassProperty;
  20908. begin
  20909. StartProgram(false);
  20910. Add([
  20911. 'type',
  20912. ' TObject = class',
  20913. ' FField: TObject;',
  20914. ' property Field: TObject read FField write FField;',
  20915. ' end;',
  20916. ' THelper = class helper for TObject',
  20917. ' procedure Fly;',
  20918. ' class procedure Run;',
  20919. ' class procedure Jump; static;',
  20920. ' end;',
  20921. 'procedure THelper.Fly;',
  20922. 'begin',
  20923. ' Field.Fly;',
  20924. ' Field.Run;',
  20925. ' Field.Jump;',
  20926. ' with Field do begin',
  20927. ' Fly;',
  20928. ' Run;',
  20929. ' Jump;',
  20930. ' end;',
  20931. 'end;',
  20932. 'class procedure THelper.Run;',
  20933. 'begin',
  20934. 'end;',
  20935. 'class procedure THelper.Jump;',
  20936. 'begin',
  20937. 'end;',
  20938. 'var',
  20939. ' b: TObject;',
  20940. 'begin',
  20941. ' b.Field.Fly;',
  20942. ' b.Field.Run;',
  20943. ' b.Field.Jump;',
  20944. ' with b do begin',
  20945. ' Field.Run;',
  20946. ' Field.Fly;',
  20947. ' Field.Jump;',
  20948. ' end;',
  20949. ' with b.Field do begin',
  20950. ' Run;',
  20951. ' Fly;',
  20952. ' Jump;',
  20953. ' end;',
  20954. '']);
  20955. ConvertProgram;
  20956. CheckSource('TestClassHelper_PassProperty',
  20957. LinesToStr([ // statements
  20958. 'rtl.createClass($mod, "TObject", null, function () {',
  20959. ' this.$init = function () {',
  20960. ' this.FField = null;',
  20961. ' };',
  20962. ' this.$final = function () {',
  20963. ' this.FField = undefined;',
  20964. ' };',
  20965. '});',
  20966. 'rtl.createHelper($mod, "THelper", null, function () {',
  20967. ' this.Fly = function () {',
  20968. ' $mod.THelper.Fly.call(this.FField);',
  20969. ' $mod.THelper.Run.call(this.FField.$class);',
  20970. ' $mod.THelper.Jump();',
  20971. ' var $with1 = this.FField;',
  20972. ' $mod.THelper.Fly.call($with1);',
  20973. ' $mod.THelper.Run.call($with1.$class);',
  20974. ' $mod.THelper.Jump();',
  20975. ' };',
  20976. ' this.Run = function () {',
  20977. ' };',
  20978. ' this.Jump = function () {',
  20979. ' };',
  20980. '});',
  20981. 'this.b = null;',
  20982. '']),
  20983. LinesToStr([ // $mod.$main
  20984. '$mod.THelper.Fly.call($mod.b.FField);',
  20985. '$mod.THelper.Run.call($mod.b.FField.$class);',
  20986. '$mod.THelper.Jump();',
  20987. 'var $with1 = $mod.b;',
  20988. '$mod.THelper.Run.call($with1.FField.$class);',
  20989. '$mod.THelper.Fly.call($with1.FField);',
  20990. '$mod.THelper.Jump();',
  20991. 'var $with2 = $mod.b.FField;',
  20992. '$mod.THelper.Run.call($with2.$class);',
  20993. '$mod.THelper.Fly.call($with2);',
  20994. '$mod.THelper.Jump();',
  20995. '']));
  20996. end;
  20997. procedure TTestModule.TestExtClassHelper_ClassVar;
  20998. begin
  20999. StartProgram(false);
  21000. Add([
  21001. '{$modeswitch externalclass}',
  21002. 'type',
  21003. ' TExtA = class external name ''ExtObj''',
  21004. ' end;',
  21005. ' THelper = class helper for TExtA',
  21006. ' const',
  21007. ' One = 1;',
  21008. ' Two: word = 2;',
  21009. ' class var',
  21010. ' Glob: word;',
  21011. ' function Foo(w: word): word;',
  21012. ' class function Bar(w: word): word; static;',
  21013. ' end;',
  21014. 'function THelper.foo(w: word): word;',
  21015. 'begin',
  21016. ' Result:=w;',
  21017. ' Two:=One+w;',
  21018. ' Glob:=Glob;',
  21019. ' Result:=Self.Glob;',
  21020. ' Self.Glob:=Self.Glob;',
  21021. ' with Self do Glob:=Glob;',
  21022. 'end;',
  21023. 'class function THelper.bar(w: word): word;',
  21024. 'begin',
  21025. ' Result:=w;',
  21026. ' Two:=One;',
  21027. ' Glob:=Glob;',
  21028. 'end;',
  21029. 'var o: TExtA;',
  21030. 'begin',
  21031. ' texta.two:=texta.one;',
  21032. ' texta.Glob:=texta.Glob;',
  21033. ' with texta do begin',
  21034. ' two:=one;',
  21035. ' Glob:=Glob;',
  21036. ' end;',
  21037. ' o.two:=o.one;',
  21038. ' o.Glob:=o.Glob;',
  21039. ' with o do begin',
  21040. ' two:=one;',
  21041. ' Glob:=Glob;',
  21042. ' end;',
  21043. '']);
  21044. ConvertProgram;
  21045. CheckSource('TestExtClassHelper_ClassVar',
  21046. LinesToStr([ // statements
  21047. 'rtl.createHelper($mod, "THelper", null, function () {',
  21048. ' this.One = 1;',
  21049. ' this.Two = 2;',
  21050. ' this.Glob = 0;',
  21051. ' this.Foo = function (w) {',
  21052. ' var Result = 0;',
  21053. ' Result = w;',
  21054. ' $mod.THelper.Two = 1 + w;',
  21055. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21056. ' Result = $mod.THelper.Glob;',
  21057. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21058. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21059. ' return Result;',
  21060. ' };',
  21061. ' this.Bar = function (w) {',
  21062. ' var Result = 0;',
  21063. ' Result = w;',
  21064. ' $mod.THelper.Two = 1;',
  21065. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21066. ' return Result;',
  21067. ' };',
  21068. '});',
  21069. 'this.o = null;',
  21070. '']),
  21071. LinesToStr([ // $mod.$main
  21072. '$mod.THelper.Two = 1;',
  21073. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21074. '$mod.THelper.Two = 1;',
  21075. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21076. '$mod.THelper.Two = 1;',
  21077. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21078. 'var $with1 = $mod.o;',
  21079. '$mod.THelper.Two = 1;',
  21080. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21081. '']));
  21082. end;
  21083. procedure TTestModule.TestExtClassHelper_Method_Call;
  21084. begin
  21085. StartProgram(false);
  21086. Add([
  21087. '{$modeswitch externalclass}',
  21088. 'type',
  21089. ' TFly = function(w: word): word of object;',
  21090. ' TExtA = class external name ''ExtObj''',
  21091. ' procedure Run(w: word = 10);',
  21092. ' end;',
  21093. ' THelper = class helper for TExtA',
  21094. ' function Foo(w: word = 1): word;',
  21095. ' function Fly(w: word = 2): word; external name ''Fly'';',
  21096. ' end;',
  21097. 'var p: TFly;',
  21098. 'function THelper.foo(w: word): word;',
  21099. 'begin',
  21100. ' Run;',
  21101. ' Run();',
  21102. ' Run(11);',
  21103. ' Foo;',
  21104. ' Foo();',
  21105. ' Foo(12);',
  21106. ' Self.Foo;',
  21107. ' Self.Foo();',
  21108. ' Self.Foo(13);',
  21109. ' Fly;',
  21110. ' Fly();',
  21111. ' with Self do begin',
  21112. ' Foo;',
  21113. ' Foo();',
  21114. ' Foo(14);',
  21115. ' Fly;',
  21116. ' Fly();',
  21117. ' end;',
  21118. ' p:=@Fly;',
  21119. 'end;',
  21120. 'var Obj: TExtA;',
  21121. 'begin',
  21122. ' obj.Foo;',
  21123. ' obj.Foo();',
  21124. ' obj.Foo(21);',
  21125. ' obj.Fly;',
  21126. ' obj.Fly();',
  21127. ' with obj do begin',
  21128. ' Foo;',
  21129. ' Foo();',
  21130. ' Foo(22);',
  21131. ' Fly;',
  21132. ' Fly();',
  21133. ' end;',
  21134. ' p:[email protected];',
  21135. '']);
  21136. ConvertProgram;
  21137. CheckSource('TestExtClassHelper_Method_Call',
  21138. LinesToStr([ // statements
  21139. 'rtl.createHelper($mod, "THelper", null, function () {',
  21140. ' this.Foo = function (w) {',
  21141. ' var Result = 0;',
  21142. ' this.Run(10);',
  21143. ' this.Run(10);',
  21144. ' this.Run(11);',
  21145. ' $mod.THelper.Foo.call(this, 1);',
  21146. ' $mod.THelper.Foo.call(this, 1);',
  21147. ' $mod.THelper.Foo.call(this, 12);',
  21148. ' $mod.THelper.Foo.call(this, 1);',
  21149. ' $mod.THelper.Foo.call(this, 1);',
  21150. ' $mod.THelper.Foo.call(this, 13);',
  21151. ' this.Fly(2);',
  21152. ' this.Fly(2);',
  21153. ' $mod.THelper.Foo.call(this, 1);',
  21154. ' $mod.THelper.Foo.call(this, 1);',
  21155. ' $mod.THelper.Foo.call(this, 14);',
  21156. ' this.Fly(2);',
  21157. ' this.Fly(2);',
  21158. ' $mod.p = rtl.createCallback(this, "Fly");',
  21159. ' return Result;',
  21160. ' };',
  21161. '});',
  21162. 'this.p = null;',
  21163. 'this.Obj = null;',
  21164. '']),
  21165. LinesToStr([ // $mod.$main
  21166. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21167. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21168. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21169. '$mod.Obj.Fly(2);',
  21170. '$mod.Obj.Fly(2);',
  21171. 'var $with1 = $mod.Obj;',
  21172. '$mod.THelper.Foo.call($with1, 1);',
  21173. '$mod.THelper.Foo.call($with1, 1);',
  21174. '$mod.THelper.Foo.call($with1, 22);',
  21175. '$with1.Fly(2);',
  21176. '$with1.Fly(2);',
  21177. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  21178. '']));
  21179. end;
  21180. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  21181. begin
  21182. StartProgram(false);
  21183. Add([
  21184. '{$modeswitch externalclass}',
  21185. 'type',
  21186. ' TExtA = class external name ''ExtObj''',
  21187. ' procedure Run(w: word = 10);',
  21188. ' end;',
  21189. ' THelper = class helper for TExtA',
  21190. ' class procedure Fly;',
  21191. ' end;',
  21192. 'class procedure THelper.Fly;',
  21193. 'begin end;',
  21194. 'begin',
  21195. '']);
  21196. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  21197. nHelperClassMethodForExtClassMustBeStatic);
  21198. ConvertProgram;
  21199. end;
  21200. procedure TTestModule.TestRecordHelper_ClassVar;
  21201. begin
  21202. StartProgram(false);
  21203. Add([
  21204. 'type',
  21205. ' TRec = record',
  21206. ' end;',
  21207. ' THelper = record helper for TRec',
  21208. ' const',
  21209. ' One = 1;',
  21210. ' Two: word = 2;',
  21211. ' class var',
  21212. ' Glob: word;',
  21213. ' function Foo(w: word): word;',
  21214. ' class function Bar(w: word): word; static;',
  21215. ' end;',
  21216. 'function THelper.foo(w: word): word;',
  21217. 'begin',
  21218. ' Result:=w;',
  21219. ' Two:=One+w;',
  21220. ' Glob:=Glob;',
  21221. ' Result:=Self.Glob;',
  21222. ' Self.Glob:=Self.Glob;',
  21223. ' with Self do Glob:=Glob;',
  21224. ' Self:=Self;',
  21225. 'end;',
  21226. 'class function THelper.bar(w: word): word;',
  21227. 'begin',
  21228. ' Result:=w;',
  21229. ' Two:=One;',
  21230. ' Glob:=Glob;',
  21231. 'end;',
  21232. 'var r: TRec;',
  21233. 'begin',
  21234. ' trec.two:=trec.one;',
  21235. ' trec.Glob:=trec.Glob;',
  21236. ' with trec do begin',
  21237. ' two:=one;',
  21238. ' Glob:=Glob;',
  21239. ' end;',
  21240. ' r.two:=r.one;',
  21241. ' r.Glob:=r.Glob;',
  21242. ' with r do begin',
  21243. ' two:=one;',
  21244. ' Glob:=Glob;',
  21245. ' end;',
  21246. '']);
  21247. ConvertProgram;
  21248. CheckSource('TestRecordHelper_ClassVar',
  21249. LinesToStr([ // statements
  21250. 'rtl.recNewT($mod, "TRec", function () {',
  21251. ' this.$eq = function (b) {',
  21252. ' return true;',
  21253. ' };',
  21254. ' this.$assign = function (s) {',
  21255. ' return this;',
  21256. ' };',
  21257. '});',
  21258. 'rtl.createHelper($mod, "THelper", null, function () {',
  21259. ' this.One = 1;',
  21260. ' this.Two = 2;',
  21261. ' this.Glob = 0;',
  21262. ' this.Foo = function (w) {',
  21263. ' var Result = 0;',
  21264. ' Result = w;',
  21265. ' $mod.THelper.Two = 1 + w;',
  21266. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21267. ' Result = $mod.THelper.Glob;',
  21268. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21269. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21270. ' this.$assign(this);',
  21271. ' return Result;',
  21272. ' };',
  21273. ' this.Bar = function (w) {',
  21274. ' var Result = 0;',
  21275. ' Result = w;',
  21276. ' $mod.THelper.Two = 1;',
  21277. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21278. ' return Result;',
  21279. ' };',
  21280. '});',
  21281. 'this.r = $mod.TRec.$new();',
  21282. '']),
  21283. LinesToStr([ // $mod.$main
  21284. '$mod.THelper.Two = 1;',
  21285. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21286. 'var $with1 = $mod.TRec;',
  21287. '$mod.THelper.Two = 1;',
  21288. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21289. '$mod.THelper.Two = 1;',
  21290. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21291. 'var $with2 = $mod.r;',
  21292. '$mod.THelper.Two = 1;',
  21293. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21294. '']));
  21295. end;
  21296. procedure TTestModule.TestRecordHelper_Method_Call;
  21297. begin
  21298. StartProgram(false);
  21299. Add([
  21300. '{$modeswitch AdvancedRecords}',
  21301. 'type',
  21302. ' TRec = record',
  21303. ' procedure Run(w: word = 10);',
  21304. ' end;',
  21305. ' THelper = record helper for TRec',
  21306. ' function Foo(w: word = 1): word;',
  21307. ' end;',
  21308. 'procedure TRec.Run(w: word);',
  21309. 'begin',
  21310. ' Foo;',
  21311. ' Foo();',
  21312. ' Foo(2);',
  21313. ' Self.Foo;',
  21314. ' Self.Foo();',
  21315. ' Self.Foo(3);',
  21316. ' with Self do begin',
  21317. ' Foo;',
  21318. ' Foo();',
  21319. ' Foo(4);',
  21320. ' end;',
  21321. 'end;',
  21322. 'function THelper.foo(w: word): word;',
  21323. 'begin',
  21324. ' Run;',
  21325. ' Run();',
  21326. ' Run(11);',
  21327. ' Foo;',
  21328. ' Foo();',
  21329. ' Foo(12);',
  21330. ' Self.Foo;',
  21331. ' Self.Foo();',
  21332. ' Self.Foo(13);',
  21333. ' with Self do begin',
  21334. ' Foo;',
  21335. ' Foo();',
  21336. ' Foo(14);',
  21337. ' end;',
  21338. 'end;',
  21339. 'var Rec: TRec;',
  21340. 'begin',
  21341. ' Rec.Foo;',
  21342. ' Rec.Foo();',
  21343. ' Rec.Foo(21);',
  21344. ' with Rec do begin',
  21345. ' Foo;',
  21346. ' Foo();',
  21347. ' Foo(22);',
  21348. ' end;',
  21349. '']);
  21350. ConvertProgram;
  21351. CheckSource('TestRecordHelper_Method_Call',
  21352. LinesToStr([ // statements
  21353. 'rtl.recNewT($mod, "TRec", function () {',
  21354. ' this.$eq = function (b) {',
  21355. ' return true;',
  21356. ' };',
  21357. ' this.$assign = function (s) {',
  21358. ' return this;',
  21359. ' };',
  21360. ' this.Run = function (w) {',
  21361. ' $mod.THelper.Foo.call(this, 1);',
  21362. ' $mod.THelper.Foo.call(this, 1);',
  21363. ' $mod.THelper.Foo.call(this, 2);',
  21364. ' $mod.THelper.Foo.call(this, 1);',
  21365. ' $mod.THelper.Foo.call(this, 1);',
  21366. ' $mod.THelper.Foo.call(this, 3);',
  21367. ' $mod.THelper.Foo.call(this, 1);',
  21368. ' $mod.THelper.Foo.call(this, 1);',
  21369. ' $mod.THelper.Foo.call(this, 4);',
  21370. ' };',
  21371. '});',
  21372. 'rtl.createHelper($mod, "THelper", null, function () {',
  21373. ' this.Foo = function (w) {',
  21374. ' var Result = 0;',
  21375. ' this.Run(10);',
  21376. ' this.Run(10);',
  21377. ' this.Run(11);',
  21378. ' $mod.THelper.Foo.call(this, 1);',
  21379. ' $mod.THelper.Foo.call(this, 1);',
  21380. ' $mod.THelper.Foo.call(this, 12);',
  21381. ' $mod.THelper.Foo.call(this, 1);',
  21382. ' $mod.THelper.Foo.call(this, 1);',
  21383. ' $mod.THelper.Foo.call(this, 13);',
  21384. ' $mod.THelper.Foo.call(this, 1);',
  21385. ' $mod.THelper.Foo.call(this, 1);',
  21386. ' $mod.THelper.Foo.call(this, 14);',
  21387. ' return Result;',
  21388. ' };',
  21389. '});',
  21390. 'this.Rec = $mod.TRec.$new();',
  21391. '']),
  21392. LinesToStr([ // $mod.$main
  21393. '$mod.THelper.Foo.call($mod.Rec, 1);',
  21394. '$mod.THelper.Foo.call($mod.Rec, 1);',
  21395. '$mod.THelper.Foo.call($mod.Rec, 21);',
  21396. 'var $with1 = $mod.Rec;',
  21397. '$mod.THelper.Foo.call($with1, 1);',
  21398. '$mod.THelper.Foo.call($with1, 1);',
  21399. '$mod.THelper.Foo.call($with1, 22);',
  21400. '']));
  21401. end;
  21402. procedure TTestModule.TestRecordHelper_Constructor;
  21403. begin
  21404. StartProgram(false);
  21405. Add([
  21406. '{$modeswitch AdvancedRecords}',
  21407. 'type',
  21408. ' TRec = record',
  21409. ' constructor Create(w: word);',
  21410. ' end;',
  21411. ' THelper = record helper for TRec',
  21412. ' constructor NewHlp(w: word);',
  21413. ' end;',
  21414. 'var',
  21415. ' Rec: TRec;',
  21416. 'constructor TRec.Create(w: word);',
  21417. 'begin',
  21418. ' NewHlp(2);', // normal call
  21419. ' trec.NewHlp(3);', // new instance
  21420. 'end;',
  21421. 'constructor THelper.NewHlp(w: word);',
  21422. 'begin',
  21423. ' create(2);', // normal call
  21424. ' trec.create(3);', // new instance
  21425. ' NewHlp(4);', // normal call
  21426. ' trec.NewHlp(5);', // new instance
  21427. 'end;',
  21428. 'begin',
  21429. ' rec.newhlp(2);', // normal call
  21430. ' with rec do newhlp(12);', // normal call
  21431. ' trec.newhlp(3);', // new instance
  21432. ' with trec do newhlp(13);', // new instance
  21433. '']);
  21434. ConvertProgram;
  21435. CheckSource('TestRecordHelper_Constructor',
  21436. LinesToStr([ // statements
  21437. 'rtl.recNewT($mod, "TRec", function () {',
  21438. ' this.$eq = function (b) {',
  21439. ' return true;',
  21440. ' };',
  21441. ' this.$assign = function (s) {',
  21442. ' return this;',
  21443. ' };',
  21444. ' this.Create = function (w) {',
  21445. ' $mod.THelper.NewHlp.call(this, 2);',
  21446. ' $mod.THelper.$new("NewHlp", [3]);',
  21447. ' return this;',
  21448. ' };',
  21449. '}, true);',
  21450. 'rtl.createHelper($mod, "THelper", null, function () {',
  21451. ' this.NewHlp = function (w) {',
  21452. ' this.Create(2);',
  21453. ' $mod.TRec.$new().Create(3);',
  21454. ' $mod.THelper.NewHlp.call(this, 4);',
  21455. ' $mod.THelper.$new("NewHlp", [5]);',
  21456. ' return this;',
  21457. ' };',
  21458. ' this.$new = function (fn, args) {',
  21459. ' return this[fn].apply($mod.TRec.$new(), args);',
  21460. ' };',
  21461. '});',
  21462. 'this.Rec = $mod.TRec.$new();',
  21463. '']),
  21464. LinesToStr([ // $mod.$main
  21465. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  21466. 'var $with1 = $mod.Rec;',
  21467. '$mod.THelper.NewHlp.call($with1, 12);',
  21468. '$mod.THelper.$new("NewHlp", [3]);',
  21469. 'var $with2 = $mod.TRec;',
  21470. '$mod.THelper.$new("NewHlp", [13]);',
  21471. '']));
  21472. end;
  21473. procedure TTestModule.TestTypeHelper_ClassVar;
  21474. begin
  21475. StartProgram(false);
  21476. Add([
  21477. '{$modeswitch typehelpers}',
  21478. 'type',
  21479. ' THelper = type helper for byte',
  21480. ' const',
  21481. ' One = 1;',
  21482. ' Two: word = 2;',
  21483. ' class var',
  21484. ' Glob: word;',
  21485. ' function Foo(w: word): word;',
  21486. ' class function Bar(w: word): word; static;',
  21487. ' end;',
  21488. 'function THelper.foo(w: word): word;',
  21489. 'begin',
  21490. ' Result:=w;',
  21491. ' Two:=One+w;',
  21492. ' Glob:=Glob;',
  21493. ' Result:=Self.Glob;',
  21494. ' Self.Glob:=Self.Glob;',
  21495. ' with Self do Glob:=Glob;',
  21496. 'end;',
  21497. 'class function THelper.bar(w: word): word;',
  21498. 'begin',
  21499. ' Result:=w;',
  21500. ' Two:=One;',
  21501. ' Glob:=Glob;',
  21502. 'end;',
  21503. 'var b: byte;',
  21504. 'begin',
  21505. ' byte.two:=byte.one;',
  21506. ' byte.Glob:=byte.Glob;',
  21507. ' with byte do begin',
  21508. ' two:=one;',
  21509. ' Glob:=Glob;',
  21510. ' end;',
  21511. ' b.two:=b.one;',
  21512. ' b.Glob:=b.Glob;',
  21513. ' with b do begin',
  21514. ' two:=one;',
  21515. ' Glob:=Glob;',
  21516. ' end;',
  21517. '']);
  21518. ConvertProgram;
  21519. CheckSource('TestTypeHelper_ClassVar',
  21520. LinesToStr([ // statements
  21521. 'rtl.createHelper($mod, "THelper", null, function () {',
  21522. ' this.One = 1;',
  21523. ' this.Two = 2;',
  21524. ' this.Glob = 0;',
  21525. ' this.Foo = function (w) {',
  21526. ' var Result = 0;',
  21527. ' Result = w;',
  21528. ' $mod.THelper.Two = 1 + w;',
  21529. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21530. ' Result = $mod.THelper.Glob;',
  21531. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21532. ' var $with1 = this.get();',
  21533. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21534. ' return Result;',
  21535. ' };',
  21536. ' this.Bar = function (w) {',
  21537. ' var Result = 0;',
  21538. ' Result = w;',
  21539. ' $mod.THelper.Two = 1;',
  21540. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21541. ' return Result;',
  21542. ' };',
  21543. '});',
  21544. 'this.b = 0;',
  21545. '']),
  21546. LinesToStr([ // $mod.$main
  21547. '$mod.THelper.Two = 1;',
  21548. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21549. '$mod.THelper.Two = 1;',
  21550. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21551. '$mod.THelper.Two = 1;',
  21552. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21553. 'var $with1 = $mod.b;',
  21554. '$mod.THelper.Two = 1;',
  21555. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21556. '']));
  21557. end;
  21558. procedure TTestModule.TestTypeHelper_PassResultElement;
  21559. begin
  21560. StartProgram(false);
  21561. Add([
  21562. '{$modeswitch typehelpers}',
  21563. 'type',
  21564. ' THelper = type helper for word',
  21565. ' procedure DoIt(e: byte = 123);',
  21566. ' class procedure DoSome(e: byte = 456); static;',
  21567. ' end;',
  21568. 'procedure THelper.DoIt(e: byte);',
  21569. 'begin',
  21570. 'end;',
  21571. 'class procedure THelper.DoSome(e: byte);',
  21572. 'begin',
  21573. 'end;',
  21574. 'function Foo(w: word): word;',
  21575. 'begin',
  21576. ' Result.DoIt;',
  21577. ' Result.DoIt();',
  21578. ' Result.DoSome;',
  21579. ' Result.DoSome();',
  21580. ' with Result do begin',
  21581. ' DoIt;',
  21582. ' DoIt();',
  21583. ' DoSome;',
  21584. ' DoSome();',
  21585. ' end;',
  21586. 'end;',
  21587. 'begin',
  21588. '']);
  21589. ConvertProgram;
  21590. CheckSource('TestTypeHelper_PassResultElement',
  21591. LinesToStr([ // statements
  21592. 'rtl.createHelper($mod, "THelper", null, function () {',
  21593. ' this.DoIt = function (e) {',
  21594. ' };',
  21595. ' this.DoSome = function (e) {',
  21596. ' };',
  21597. '});',
  21598. 'this.Foo = function (w) {',
  21599. ' var Result = 0;',
  21600. ' $mod.THelper.DoIt.call({',
  21601. ' get: function () {',
  21602. ' return Result;',
  21603. ' },',
  21604. ' set: function (v) {',
  21605. ' Result = v;',
  21606. ' }',
  21607. ' }, 123);',
  21608. ' $mod.THelper.DoIt.call({',
  21609. ' get: function () {',
  21610. ' return Result;',
  21611. ' },',
  21612. ' set: function (v) {',
  21613. ' Result = v;',
  21614. ' }',
  21615. ' }, 123);',
  21616. ' $mod.THelper.DoSome(456);',
  21617. ' $mod.THelper.DoSome(456);',
  21618. ' $mod.THelper.DoIt.call({',
  21619. ' get: function () {',
  21620. ' return Result;',
  21621. ' },',
  21622. ' set: function (v) {',
  21623. ' Result = v;',
  21624. ' }',
  21625. ' }, 123);',
  21626. ' $mod.THelper.DoIt.call({',
  21627. ' get: function () {',
  21628. ' return Result;',
  21629. ' },',
  21630. ' set: function (v) {',
  21631. ' Result = v;',
  21632. ' }',
  21633. ' }, 123);',
  21634. ' $mod.THelper.DoSome(456);',
  21635. ' $mod.THelper.DoSome(456);',
  21636. ' return Result;',
  21637. '};',
  21638. '']),
  21639. LinesToStr([ // $mod.$main
  21640. '']));
  21641. end;
  21642. procedure TTestModule.TestTypeHelper_PassArgs;
  21643. begin
  21644. StartProgram(false);
  21645. Add([
  21646. '{$modeswitch typehelpers}',
  21647. 'type',
  21648. ' THelper = type helper for word',
  21649. ' procedure DoIt(e: byte = 123);',
  21650. ' end;',
  21651. 'procedure THelper.DoIt(e: byte);',
  21652. 'begin',
  21653. 'end;',
  21654. 'procedure FooDefault(a: word);',
  21655. 'begin',
  21656. ' a.DoIt;',
  21657. ' with a do DoIt;',
  21658. 'end;',
  21659. 'procedure FooConst(const a: word);',
  21660. 'begin',
  21661. ' a.DoIt;',
  21662. ' with a do DoIt;',
  21663. 'end;',
  21664. 'procedure FooVar(var a: word);',
  21665. 'begin',
  21666. ' a.DoIt;',
  21667. ' with a do DoIt;',
  21668. 'end;',
  21669. 'begin',
  21670. '']);
  21671. ConvertProgram;
  21672. CheckSource('TestTypeHelper_PassArgs',
  21673. LinesToStr([ // statements
  21674. 'rtl.createHelper($mod, "THelper", null, function () {',
  21675. ' this.DoIt = function (e) {',
  21676. ' };',
  21677. '});',
  21678. 'this.FooDefault = function (a) {',
  21679. ' $mod.THelper.DoIt.call({',
  21680. ' get: function () {',
  21681. ' return a;',
  21682. ' },',
  21683. ' set: function (v) {',
  21684. ' a = v;',
  21685. ' }',
  21686. ' }, 123);',
  21687. ' $mod.THelper.DoIt.call({',
  21688. ' get: function () {',
  21689. ' return a;',
  21690. ' },',
  21691. ' set: function (v) {',
  21692. ' a = v;',
  21693. ' }',
  21694. ' }, 123);',
  21695. '};',
  21696. 'this.FooConst = function (a) {',
  21697. ' $mod.THelper.DoIt.call({',
  21698. ' get: function () {',
  21699. ' return a;',
  21700. ' },',
  21701. ' set: function (v) {',
  21702. ' rtl.raiseE("EPropReadOnly");',
  21703. ' }',
  21704. ' }, 123);',
  21705. ' $mod.THelper.DoIt.call({',
  21706. ' get: function () {',
  21707. ' return a;',
  21708. ' },',
  21709. ' set: function () {',
  21710. ' rtl.raiseE("EPropReadOnly");',
  21711. ' }',
  21712. ' }, 123);',
  21713. '};',
  21714. 'this.FooVar = function (a) {',
  21715. ' $mod.THelper.DoIt.call(a, 123);',
  21716. ' var $with1 = a.get();',
  21717. ' $mod.THelper.DoIt.call(a, 123);',
  21718. '};',
  21719. '']),
  21720. LinesToStr([ // $mod.$main
  21721. '']));
  21722. end;
  21723. procedure TTestModule.TestTypeHelper_PassVarConst;
  21724. begin
  21725. StartProgram(false);
  21726. Add([
  21727. '{$modeswitch typehelpers}',
  21728. 'type',
  21729. ' THelper = type helper for word',
  21730. ' procedure DoIt(e: byte = 123);',
  21731. ' end;',
  21732. 'procedure THelper.DoIt(e: byte);',
  21733. 'begin',
  21734. 'end;',
  21735. 'var a: word;',
  21736. 'const c: word = 2;',
  21737. '{$writeableconst off}',
  21738. 'const r: word = 3;',
  21739. 'begin',
  21740. ' a.DoIt;',
  21741. ' with a do DoIt;',
  21742. ' c.DoIt;',
  21743. ' with c do DoIt;',
  21744. ' r.DoIt;',
  21745. ' with r do DoIt;',
  21746. '']);
  21747. ConvertProgram;
  21748. CheckSource('TestTypeHelper_PassVarConst',
  21749. LinesToStr([ // statements
  21750. 'rtl.createHelper($mod, "THelper", null, function () {',
  21751. ' this.DoIt = function (e) {',
  21752. ' };',
  21753. '});',
  21754. 'this.a = 0;',
  21755. 'this.c = 2;',
  21756. 'this.r = 3;',
  21757. '']),
  21758. LinesToStr([ // $mod.$main
  21759. '$mod.THelper.DoIt.call({',
  21760. ' p: $mod,',
  21761. ' get: function () {',
  21762. ' return this.p.a;',
  21763. ' },',
  21764. ' set: function (v) {',
  21765. ' this.p.a = v;',
  21766. ' }',
  21767. '}, 123);',
  21768. 'var $with1 = $mod.a;',
  21769. '$mod.THelper.DoIt.call({',
  21770. ' get: function () {',
  21771. ' return $with1;',
  21772. ' },',
  21773. ' set: function (v) {',
  21774. ' $with1 = v;',
  21775. ' }',
  21776. '}, 123);',
  21777. '$mod.THelper.DoIt.call({',
  21778. ' p: $mod,',
  21779. ' get: function () {',
  21780. ' return this.p.c;',
  21781. ' },',
  21782. ' set: function (v) {',
  21783. ' this.p.c = v;',
  21784. ' }',
  21785. '}, 123);',
  21786. 'var $with2 = $mod.c;',
  21787. '$mod.THelper.DoIt.call({',
  21788. ' get: function () {',
  21789. ' return $with2;',
  21790. ' },',
  21791. ' set: function (v) {',
  21792. ' $with2 = v;',
  21793. ' }',
  21794. '}, 123);',
  21795. '$mod.THelper.DoIt.call({',
  21796. ' get: function () {',
  21797. ' return 3;',
  21798. ' },',
  21799. ' set: function (v) {',
  21800. ' rtl.raiseE("EPropReadOnly");',
  21801. ' }',
  21802. '}, 123);',
  21803. 'var $with3 = 3;',
  21804. ' $mod.THelper.DoIt.call({',
  21805. ' get: function () {',
  21806. ' return $with3;',
  21807. ' },',
  21808. ' set: function () {',
  21809. ' rtl.raiseE("EPropReadOnly");',
  21810. ' }',
  21811. ' }, 123);',
  21812. '']));
  21813. end;
  21814. procedure TTestModule.TestTypeHelper_PassFuncResult;
  21815. begin
  21816. StartProgram(false);
  21817. Add([
  21818. '{$modeswitch typehelpers}',
  21819. 'type',
  21820. ' THelper = type helper for word',
  21821. ' procedure DoIt(e: byte = 123);',
  21822. ' end;',
  21823. 'procedure THelper.DoIt(e: byte);',
  21824. 'begin',
  21825. 'end;',
  21826. 'function Foo(b: byte = 1): word;',
  21827. 'begin',
  21828. 'end;',
  21829. 'begin',
  21830. ' Foo.DoIt;',
  21831. ' Foo().DoIt;',
  21832. ' with Foo do DoIt;',
  21833. ' with Foo() do DoIt;',
  21834. '']);
  21835. ConvertProgram;
  21836. CheckSource('TestTypeHelper_PassFuncResult',
  21837. LinesToStr([ // statements
  21838. 'rtl.createHelper($mod, "THelper", null, function () {',
  21839. ' this.DoIt = function (e) {',
  21840. ' };',
  21841. '});',
  21842. 'this.Foo = function (b) {',
  21843. ' var Result = 0;',
  21844. ' return Result;',
  21845. '};',
  21846. '']),
  21847. LinesToStr([ // $mod.$main
  21848. '$mod.THelper.DoIt.call({',
  21849. ' a: $mod.Foo(1),',
  21850. ' get: function () {',
  21851. ' return this.a;',
  21852. ' },',
  21853. ' set: function (v) {',
  21854. ' this.a = v;',
  21855. ' }',
  21856. '}, 123);',
  21857. '$mod.THelper.DoIt.call({',
  21858. ' a: $mod.Foo(1),',
  21859. ' get: function () {',
  21860. ' return this.a;',
  21861. ' },',
  21862. ' set: function (v) {',
  21863. ' this.a = v;',
  21864. ' }',
  21865. '}, 123);',
  21866. 'var $with1 = $mod.Foo(1);',
  21867. '$mod.THelper.DoIt.call({',
  21868. ' get: function () {',
  21869. ' return $with1;',
  21870. ' },',
  21871. ' set: function (v) {',
  21872. ' $with1 = v;',
  21873. ' }',
  21874. '}, 123);',
  21875. 'var $with2 = $mod.Foo(1);',
  21876. '$mod.THelper.DoIt.call({',
  21877. ' get: function () {',
  21878. ' return $with2;',
  21879. ' },',
  21880. ' set: function (v) {',
  21881. ' $with2 = v;',
  21882. ' }',
  21883. '}, 123);',
  21884. '']));
  21885. end;
  21886. procedure TTestModule.TestTypeHelper_PassPropertyField;
  21887. begin
  21888. StartProgram(false);
  21889. Add([
  21890. '{$modeswitch typehelpers}',
  21891. 'type',
  21892. ' TObject = class',
  21893. ' FField: word;',
  21894. ' procedure SetField(Value: word);',
  21895. ' property Field: word read FField write SetField;',
  21896. ' end;',
  21897. ' THelper = type helper for word',
  21898. ' procedure Fly;',
  21899. ' class procedure Run; static;',
  21900. ' end;',
  21901. 'procedure TObject.SetField(Value: word);',
  21902. 'begin',
  21903. ' Field.Fly;',
  21904. ' Field.Run;',
  21905. ' Self.Field.Fly;',
  21906. ' Self.Field.Run;',
  21907. ' with Self do begin',
  21908. ' Field.Fly;',
  21909. ' Field.Run;',
  21910. ' end;',
  21911. ' with Self.Field do begin',
  21912. ' Fly;',
  21913. ' Run;',
  21914. ' end;',
  21915. 'end;',
  21916. 'procedure THelper.Fly;',
  21917. 'begin',
  21918. 'end;',
  21919. 'class procedure THelper.Run;',
  21920. 'begin',
  21921. 'end;',
  21922. 'var',
  21923. ' o: TObject;',
  21924. 'begin',
  21925. ' o.Field.Fly;',
  21926. ' o.Field.Run;',
  21927. ' with o do begin',
  21928. ' Field.Fly;',
  21929. ' Field.Run;',
  21930. ' end;',
  21931. ' with o.Field do begin',
  21932. ' Fly;',
  21933. ' Run;',
  21934. ' end;',
  21935. '']);
  21936. ConvertProgram;
  21937. CheckSource('TestTypeHelper_PassPropertyField',
  21938. LinesToStr([ // statements
  21939. 'rtl.createClass($mod, "TObject", null, function () {',
  21940. ' this.$init = function () {',
  21941. ' this.FField = 0;',
  21942. ' };',
  21943. ' this.$final = function () {',
  21944. ' };',
  21945. ' this.SetField = function (Value) {',
  21946. ' $mod.THelper.Fly.call({',
  21947. ' p: this,',
  21948. ' get: function () {',
  21949. ' return this.p.FField;',
  21950. ' },',
  21951. ' set: function (v) {',
  21952. ' this.p.FField = v;',
  21953. ' }',
  21954. ' });',
  21955. ' $mod.THelper.Run();',
  21956. ' $mod.THelper.Fly.call({',
  21957. ' p: this,',
  21958. ' get: function () {',
  21959. ' return this.p.FField;',
  21960. ' },',
  21961. ' set: function (v) {',
  21962. ' this.p.FField = v;',
  21963. ' }',
  21964. ' });',
  21965. ' $mod.THelper.Run();',
  21966. ' $mod.THelper.Fly.call({',
  21967. ' p: this,',
  21968. ' get: function () {',
  21969. ' return this.p.FField;',
  21970. ' },',
  21971. ' set: function (v) {',
  21972. ' this.p.FField = v;',
  21973. ' }',
  21974. ' });',
  21975. ' $mod.THelper.Run();',
  21976. ' var $with1 = this.FField;',
  21977. ' $mod.THelper.Fly.call({',
  21978. ' get: function () {',
  21979. ' return $with1;',
  21980. ' },',
  21981. ' set: function (v) {',
  21982. ' $with1 = v;',
  21983. ' }',
  21984. ' });',
  21985. ' $mod.THelper.Run();',
  21986. ' };',
  21987. '});',
  21988. 'rtl.createHelper($mod, "THelper", null, function () {',
  21989. ' this.Fly = function () {',
  21990. ' };',
  21991. ' this.Run = function () {',
  21992. ' };',
  21993. '});',
  21994. 'this.o = null;',
  21995. '']),
  21996. LinesToStr([ // $mod.$main
  21997. '$mod.THelper.Fly.call({',
  21998. ' p: $mod.o,',
  21999. ' get: function () {',
  22000. ' return this.p.FField;',
  22001. ' },',
  22002. ' set: function (v) {',
  22003. ' this.p.FField = v;',
  22004. ' }',
  22005. '});',
  22006. '$mod.THelper.Run();',
  22007. 'var $with1 = $mod.o;',
  22008. '$mod.THelper.Fly.call({',
  22009. ' p: $with1,',
  22010. ' get: function () {',
  22011. ' return this.p.FField;',
  22012. ' },',
  22013. ' set: function (v) {',
  22014. ' this.p.FField = v;',
  22015. ' }',
  22016. '});',
  22017. '$mod.THelper.Run();',
  22018. 'var $with2 = $mod.o.FField;',
  22019. '$mod.THelper.Fly.call({',
  22020. ' get: function () {',
  22021. ' return $with2;',
  22022. ' },',
  22023. ' set: function (v) {',
  22024. ' $with2 = v;',
  22025. ' }',
  22026. '});',
  22027. '$mod.THelper.Run();',
  22028. '']));
  22029. end;
  22030. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  22031. begin
  22032. StartProgram(false);
  22033. Add([
  22034. '{$modeswitch typehelpers}',
  22035. 'type',
  22036. ' TObject = class',
  22037. ' FField: word;',
  22038. ' function GetField: word;',
  22039. ' property Field: word read GetField write FField;',
  22040. ' end;',
  22041. ' THelper = type helper for word',
  22042. ' procedure Fly;',
  22043. ' class procedure Run; static;',
  22044. ' end;',
  22045. 'function TObject.GetField: word;',
  22046. 'begin',
  22047. ' Field.Fly;',
  22048. ' Field.Run;',
  22049. ' Self.Field.Fly;',
  22050. ' Self.Field.Run;',
  22051. ' with Self do begin',
  22052. ' Field.Fly;',
  22053. ' Field.Run;',
  22054. ' end;',
  22055. ' with Self.Field do begin',
  22056. ' Fly;',
  22057. ' Run;',
  22058. ' end;',
  22059. 'end;',
  22060. 'procedure THelper.Fly;',
  22061. 'begin',
  22062. 'end;',
  22063. 'class procedure THelper.Run;',
  22064. 'begin',
  22065. 'end;',
  22066. 'var',
  22067. ' o: TObject;',
  22068. 'begin',
  22069. ' o.Field.Fly;',
  22070. ' o.Field.Run;',
  22071. ' with o do begin',
  22072. ' Field.Fly;',
  22073. ' Field.Run;',
  22074. ' end;',
  22075. ' with o.Field do begin',
  22076. ' Fly;',
  22077. ' Run;',
  22078. ' end;',
  22079. '']);
  22080. ConvertProgram;
  22081. CheckSource('TestTypeHelper_PassPropertyGetter',
  22082. LinesToStr([ // statements
  22083. 'rtl.createClass($mod, "TObject", null, function () {',
  22084. ' this.$init = function () {',
  22085. ' this.FField = 0;',
  22086. ' };',
  22087. ' this.$final = function () {',
  22088. ' };',
  22089. ' this.GetField = function () {',
  22090. ' var Result = 0;',
  22091. ' $mod.THelper.Fly.call({',
  22092. ' p: this.GetField(),',
  22093. ' get: function () {',
  22094. ' return this.p;',
  22095. ' },',
  22096. ' set: function (v) {',
  22097. ' this.p = v;',
  22098. ' }',
  22099. ' });',
  22100. ' $mod.THelper.Run();',
  22101. ' $mod.THelper.Fly.call({',
  22102. ' p: this.GetField(),',
  22103. ' get: function () {',
  22104. ' return this.p;',
  22105. ' },',
  22106. ' set: function (v) {',
  22107. ' this.p = v;',
  22108. ' }',
  22109. ' });',
  22110. ' $mod.THelper.Run();',
  22111. ' $mod.THelper.Fly.call({',
  22112. ' p: this.GetField(),',
  22113. ' get: function () {',
  22114. ' return this.p;',
  22115. ' },',
  22116. ' set: function (v) {',
  22117. ' this.p = v;',
  22118. ' }',
  22119. ' });',
  22120. ' $mod.THelper.Run();',
  22121. ' var $with1 = this.GetField();',
  22122. ' $mod.THelper.Fly.call({',
  22123. ' get: function () {',
  22124. ' return $with1;',
  22125. ' },',
  22126. ' set: function (v) {',
  22127. ' $with1 = v;',
  22128. ' }',
  22129. ' });',
  22130. ' $mod.THelper.Run();',
  22131. ' return Result;',
  22132. ' };',
  22133. '});',
  22134. 'rtl.createHelper($mod, "THelper", null, function () {',
  22135. ' this.Fly = function () {',
  22136. ' };',
  22137. ' this.Run = function () {',
  22138. ' };',
  22139. '});',
  22140. 'this.o = null;',
  22141. '']),
  22142. LinesToStr([ // $mod.$main
  22143. '$mod.THelper.Fly.call({',
  22144. ' p: $mod.o.GetField(),',
  22145. ' get: function () {',
  22146. ' return this.p;',
  22147. ' },',
  22148. ' set: function (v) {',
  22149. ' this.p = v;',
  22150. ' }',
  22151. '});',
  22152. '$mod.THelper.Run();',
  22153. 'var $with1 = $mod.o;',
  22154. '$mod.THelper.Fly.call({',
  22155. ' p: $with1.GetField(),',
  22156. ' get: function () {',
  22157. ' return this.p;',
  22158. ' },',
  22159. ' set: function (v) {',
  22160. ' this.p = v;',
  22161. ' }',
  22162. '});',
  22163. '$mod.THelper.Run();',
  22164. 'var $with2 = $mod.o.GetField();',
  22165. '$mod.THelper.Fly.call({',
  22166. ' get: function () {',
  22167. ' return $with2;',
  22168. ' },',
  22169. ' set: function (v) {',
  22170. ' $with2 = v;',
  22171. ' }',
  22172. '});',
  22173. '$mod.THelper.Run();',
  22174. '']));
  22175. end;
  22176. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  22177. begin
  22178. StartProgram(false);
  22179. Add([
  22180. '{$modeswitch typehelpers}',
  22181. 'type',
  22182. ' TObject = class',
  22183. ' class var FField: word;',
  22184. ' class procedure SetField(Value: word);',
  22185. ' class property Field: word read FField write SetField;',
  22186. ' end;',
  22187. ' THelper = type helper for word',
  22188. ' procedure Fly(n: byte);',
  22189. ' end;',
  22190. 'class procedure TObject.SetField(Value: word);',
  22191. 'begin',
  22192. ' Field.Fly(1);',
  22193. ' Self.Field.Fly(2);',
  22194. ' with Self do Field.Fly(3);',
  22195. ' with Self.Field do Fly(4);',
  22196. ' TObject.Field.Fly(5);',
  22197. ' with TObject do Field.Fly(6);',
  22198. ' with TObject.Field do Fly(7);',
  22199. 'end;',
  22200. 'procedure THelper.Fly(n: byte);',
  22201. 'begin',
  22202. 'end;',
  22203. 'var',
  22204. ' o: TObject;',
  22205. 'begin',
  22206. ' o.Field.Fly(11);',
  22207. ' with o do Field.Fly(12);',
  22208. ' with o.Field do Fly(13);',
  22209. ' TObject.Field.Fly(14);',
  22210. ' with TObject do Field.Fly(15);',
  22211. ' with TObject.Field do Fly(16);',
  22212. '']);
  22213. ConvertProgram;
  22214. CheckSource('TestTypeHelper_PassClassPropertyField',
  22215. LinesToStr([ // statements
  22216. 'rtl.createClass($mod, "TObject", null, function () {',
  22217. ' this.FField = 0;',
  22218. ' this.$init = function () {',
  22219. ' };',
  22220. ' this.$final = function () {',
  22221. ' };',
  22222. ' this.SetField = function (Value) {',
  22223. ' $mod.THelper.Fly.call({',
  22224. ' p: this,',
  22225. ' get: function () {',
  22226. ' return this.p.FField;',
  22227. ' },',
  22228. ' set: function (v) {',
  22229. ' $mod.TObject.FField = v;',
  22230. ' }',
  22231. ' }, 1);',
  22232. ' $mod.THelper.Fly.call({',
  22233. ' p: this,',
  22234. ' get: function () {',
  22235. ' return this.p.FField;',
  22236. ' },',
  22237. ' set: function (v) {',
  22238. ' $mod.TObject.FField = v;',
  22239. ' }',
  22240. ' }, 2);',
  22241. ' $mod.THelper.Fly.call({',
  22242. ' p: this,',
  22243. ' get: function () {',
  22244. ' return this.p.FField;',
  22245. ' },',
  22246. ' set: function (v) {',
  22247. ' $mod.TObject.FField = v;',
  22248. ' }',
  22249. ' }, 3);',
  22250. ' var $with1 = this.FField;',
  22251. ' $mod.THelper.Fly.call({',
  22252. ' get: function () {',
  22253. ' return $with1;',
  22254. ' },',
  22255. ' set: function (v) {',
  22256. ' $with1 = v;',
  22257. ' }',
  22258. ' }, 4);',
  22259. ' $mod.THelper.Fly.call({',
  22260. ' p: $mod.TObject,',
  22261. ' get: function () {',
  22262. ' return this.p.FField;',
  22263. ' },',
  22264. ' set: function (v) {',
  22265. ' $mod.TObject.FField = v;',
  22266. ' }',
  22267. ' }, 5);',
  22268. ' var $with2 = $mod.TObject;',
  22269. ' $mod.THelper.Fly.call({',
  22270. ' p: $with2,',
  22271. ' get: function () {',
  22272. ' return this.p.FField;',
  22273. ' },',
  22274. ' set: function (v) {',
  22275. ' $mod.TObject.FField = v;',
  22276. ' }',
  22277. ' }, 6);',
  22278. ' var $with3 = $mod.TObject.FField;',
  22279. ' $mod.THelper.Fly.call({',
  22280. ' get: function () {',
  22281. ' return $with3;',
  22282. ' },',
  22283. ' set: function (v) {',
  22284. ' $with3 = v;',
  22285. ' }',
  22286. ' }, 7);',
  22287. ' };',
  22288. '});',
  22289. 'rtl.createHelper($mod, "THelper", null, function () {',
  22290. ' this.Fly = function (n) {',
  22291. ' };',
  22292. '});',
  22293. 'this.o = null;',
  22294. '']),
  22295. LinesToStr([ // $mod.$main
  22296. '$mod.THelper.Fly.call({',
  22297. ' p: $mod.o,',
  22298. ' get: function () {',
  22299. ' return this.p.FField;',
  22300. ' },',
  22301. ' set: function (v) {',
  22302. ' $mod.TObject.FField = v;',
  22303. ' }',
  22304. '}, 11);',
  22305. 'var $with1 = $mod.o;',
  22306. '$mod.THelper.Fly.call({',
  22307. ' p: $with1,',
  22308. ' get: function () {',
  22309. ' return this.p.FField;',
  22310. ' },',
  22311. ' set: function (v) {',
  22312. ' $mod.TObject.FField = v;',
  22313. ' }',
  22314. '}, 12);',
  22315. 'var $with2 = $mod.o.FField;',
  22316. '$mod.THelper.Fly.call({',
  22317. ' get: function () {',
  22318. ' return $with2;',
  22319. ' },',
  22320. ' set: function (v) {',
  22321. ' $with2 = v;',
  22322. ' }',
  22323. '}, 13);',
  22324. '$mod.THelper.Fly.call({',
  22325. ' p: $mod.TObject,',
  22326. ' get: function () {',
  22327. ' return this.p.FField;',
  22328. ' },',
  22329. ' set: function (v) {',
  22330. ' $mod.TObject.FField = v;',
  22331. ' }',
  22332. '}, 14);',
  22333. 'var $with3 = $mod.TObject;',
  22334. '$mod.THelper.Fly.call({',
  22335. ' p: $with3,',
  22336. ' get: function () {',
  22337. ' return this.p.FField;',
  22338. ' },',
  22339. ' set: function (v) {',
  22340. ' $mod.TObject.FField = v;',
  22341. ' }',
  22342. '}, 15);',
  22343. 'var $with4 = $mod.TObject.FField;',
  22344. '$mod.THelper.Fly.call({',
  22345. ' get: function () {',
  22346. ' return $with4;',
  22347. ' },',
  22348. ' set: function (v) {',
  22349. ' $with4 = v;',
  22350. ' }',
  22351. '}, 16);',
  22352. '']));
  22353. end;
  22354. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  22355. begin
  22356. StartProgram(false);
  22357. Add([
  22358. '{$modeswitch typehelpers}',
  22359. 'type',
  22360. ' TObject = class',
  22361. ' class var FField: word;',
  22362. ' class function GetField: word; static;',
  22363. ' class property Field: word read GetField write FField;',
  22364. ' end;',
  22365. ' THelper = type helper for word',
  22366. ' procedure Fly(n: byte);',
  22367. ' end;',
  22368. 'class function TObject.GetField: word;',
  22369. 'begin',
  22370. ' Field.Fly(1);',
  22371. ' TObject.Field.Fly(5);',
  22372. ' with TObject do Field.Fly(6);',
  22373. ' with TObject.Field do Fly(7);',
  22374. 'end;',
  22375. 'procedure THelper.Fly(n: byte);',
  22376. 'begin',
  22377. 'end;',
  22378. 'var',
  22379. ' o: TObject;',
  22380. 'begin',
  22381. ' o.Field.Fly(11);',
  22382. ' with o do Field.Fly(12);',
  22383. ' with o.Field do Fly(13);',
  22384. '']);
  22385. ConvertProgram;
  22386. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  22387. LinesToStr([ // statements
  22388. 'rtl.createClass($mod, "TObject", null, function () {',
  22389. ' this.FField = 0;',
  22390. ' this.$init = function () {',
  22391. ' };',
  22392. ' this.$final = function () {',
  22393. ' };',
  22394. ' this.GetField = function () {',
  22395. ' var Result = 0;',
  22396. ' $mod.THelper.Fly.call({',
  22397. ' p: this.GetField(),',
  22398. ' get: function () {',
  22399. ' return this.p;',
  22400. ' },',
  22401. ' set: function (v) {',
  22402. ' this.p = v;',
  22403. ' }',
  22404. ' }, 1);',
  22405. ' $mod.THelper.Fly.call({',
  22406. ' p: $mod.TObject.GetField(),',
  22407. ' get: function () {',
  22408. ' return this.p;',
  22409. ' },',
  22410. ' set: function (v) {',
  22411. ' this.p = v;',
  22412. ' }',
  22413. ' }, 5);',
  22414. ' var $with1 = $mod.TObject;',
  22415. ' $mod.THelper.Fly.call({',
  22416. ' p: $with1.GetField(),',
  22417. ' get: function () {',
  22418. ' return this.p;',
  22419. ' },',
  22420. ' set: function (v) {',
  22421. ' this.p = v;',
  22422. ' }',
  22423. ' }, 6);',
  22424. ' var $with2 = $mod.TObject.GetField();',
  22425. ' $mod.THelper.Fly.call({',
  22426. ' get: function () {',
  22427. ' return $with2;',
  22428. ' },',
  22429. ' set: function (v) {',
  22430. ' $with2 = v;',
  22431. ' }',
  22432. ' }, 7);',
  22433. ' return Result;',
  22434. ' };',
  22435. '});',
  22436. 'rtl.createHelper($mod, "THelper", null, function () {',
  22437. ' this.Fly = function (n) {',
  22438. ' };',
  22439. '});',
  22440. 'this.o = null;',
  22441. '']),
  22442. LinesToStr([ // $mod.$main
  22443. '$mod.THelper.Fly.call({',
  22444. ' p: $mod.o.GetField(),',
  22445. ' get: function () {',
  22446. ' return this.p;',
  22447. ' },',
  22448. ' set: function (v) {',
  22449. ' this.p = v;',
  22450. ' }',
  22451. '}, 11);',
  22452. 'var $with1 = $mod.o;',
  22453. '$mod.THelper.Fly.call({',
  22454. ' p: $with1.GetField(),',
  22455. ' get: function () {',
  22456. ' return this.p;',
  22457. ' },',
  22458. ' set: function (v) {',
  22459. ' this.p = v;',
  22460. ' }',
  22461. '}, 12);',
  22462. 'var $with2 = $mod.o.GetField();',
  22463. '$mod.THelper.Fly.call({',
  22464. ' get: function () {',
  22465. ' return $with2;',
  22466. ' },',
  22467. ' set: function (v) {',
  22468. ' $with2 = v;',
  22469. ' }',
  22470. '}, 13);',
  22471. '']));
  22472. end;
  22473. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  22474. begin
  22475. StartProgram(false);
  22476. Add([
  22477. '{$modeswitch typehelpers}',
  22478. 'type',
  22479. ' TObject = class',
  22480. ' class var FField: word;',
  22481. ' class function GetField: word;',
  22482. ' class property Field: word read GetField write FField;',
  22483. ' end;',
  22484. ' TClass = class of TObject;',
  22485. ' THelper = type helper for word',
  22486. ' procedure Fly(n: byte);',
  22487. ' end;',
  22488. 'class function TObject.GetField: word;',
  22489. 'begin',
  22490. ' Field.Fly(1);',
  22491. ' Self.Field.Fly(5);',
  22492. ' with Self do Field.Fly(6);',
  22493. ' with Self.Field do Fly(7);',
  22494. 'end;',
  22495. 'procedure THelper.Fly(n: byte);',
  22496. 'begin',
  22497. 'end;',
  22498. 'var',
  22499. ' o: TObject;',
  22500. ' c: TClass;',
  22501. 'begin',
  22502. ' o.Field.Fly(11);',
  22503. ' with o do Field.Fly(12);',
  22504. ' with o.Field do Fly(13);',
  22505. ' c.Field.Fly(14);',
  22506. ' with c do Field.Fly(15);',
  22507. ' with c.Field do Fly(16);',
  22508. '']);
  22509. ConvertProgram;
  22510. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  22511. LinesToStr([ // statements
  22512. 'rtl.createClass($mod, "TObject", null, function () {',
  22513. ' this.FField = 0;',
  22514. ' this.$init = function () {',
  22515. ' };',
  22516. ' this.$final = function () {',
  22517. ' };',
  22518. ' this.GetField = function () {',
  22519. ' var Result = 0;',
  22520. ' $mod.THelper.Fly.call({',
  22521. ' p: this.GetField(),',
  22522. ' get: function () {',
  22523. ' return this.p;',
  22524. ' },',
  22525. ' set: function (v) {',
  22526. ' this.p = v;',
  22527. ' }',
  22528. ' }, 1);',
  22529. ' $mod.THelper.Fly.call({',
  22530. ' p: this.GetField(),',
  22531. ' get: function () {',
  22532. ' return this.p;',
  22533. ' },',
  22534. ' set: function (v) {',
  22535. ' this.p = v;',
  22536. ' }',
  22537. ' }, 5);',
  22538. ' $mod.THelper.Fly.call({',
  22539. ' p: this.GetField(),',
  22540. ' get: function () {',
  22541. ' return this.p;',
  22542. ' },',
  22543. ' set: function (v) {',
  22544. ' this.p = v;',
  22545. ' }',
  22546. ' }, 6);',
  22547. ' var $with1 = this.GetField();',
  22548. ' $mod.THelper.Fly.call({',
  22549. ' get: function () {',
  22550. ' return $with1;',
  22551. ' },',
  22552. ' set: function (v) {',
  22553. ' $with1 = v;',
  22554. ' }',
  22555. ' }, 7);',
  22556. ' return Result;',
  22557. ' };',
  22558. '});',
  22559. 'rtl.createHelper($mod, "THelper", null, function () {',
  22560. ' this.Fly = function (n) {',
  22561. ' };',
  22562. '});',
  22563. 'this.o = null;',
  22564. 'this.c = null;',
  22565. '']),
  22566. LinesToStr([ // $mod.$main
  22567. '$mod.THelper.Fly.call({',
  22568. ' p: $mod.o.$class.GetField(),',
  22569. ' get: function () {',
  22570. ' return this.p;',
  22571. ' },',
  22572. ' set: function (v) {',
  22573. ' this.p = v;',
  22574. ' }',
  22575. '}, 11);',
  22576. 'var $with1 = $mod.o;',
  22577. '$mod.THelper.Fly.call({',
  22578. ' p: $with1.$class.GetField(),',
  22579. ' get: function () {',
  22580. ' return this.p;',
  22581. ' },',
  22582. ' set: function (v) {',
  22583. ' this.p = v;',
  22584. ' }',
  22585. '}, 12);',
  22586. 'var $with2 = $mod.o.$class.GetField();',
  22587. '$mod.THelper.Fly.call({',
  22588. ' get: function () {',
  22589. ' return $with2;',
  22590. ' },',
  22591. ' set: function (v) {',
  22592. ' $with2 = v;',
  22593. ' }',
  22594. '}, 13);',
  22595. '$mod.THelper.Fly.call({',
  22596. ' p: $mod.c.GetField(),',
  22597. ' get: function () {',
  22598. ' return this.p;',
  22599. ' },',
  22600. ' set: function (v) {',
  22601. ' this.p = v;',
  22602. ' }',
  22603. '}, 14);',
  22604. 'var $with3 = $mod.c;',
  22605. '$mod.THelper.Fly.call({',
  22606. ' p: $with3.GetField(),',
  22607. ' get: function () {',
  22608. ' return this.p;',
  22609. ' },',
  22610. ' set: function (v) {',
  22611. ' this.p = v;',
  22612. ' }',
  22613. '}, 15);',
  22614. 'var $with4 = $mod.c.GetField();',
  22615. '$mod.THelper.Fly.call({',
  22616. ' get: function () {',
  22617. ' return $with4;',
  22618. ' },',
  22619. ' set: function (v) {',
  22620. ' $with4 = v;',
  22621. ' }',
  22622. '}, 16);',
  22623. '']));
  22624. end;
  22625. procedure TTestModule.TestTypeHelper_Property;
  22626. begin
  22627. StartProgram(false);
  22628. Add([
  22629. '{$modeswitch typehelpers}',
  22630. 'type',
  22631. ' THelper = type helper for word',
  22632. ' function GetSize: longint;',
  22633. ' procedure SetSize(Value: longint);',
  22634. ' property Size: longint read GetSize write SetSize;',
  22635. ' end;',
  22636. 'function THelper.GetSize: longint;',
  22637. 'begin',
  22638. ' Result:=Size+1;',
  22639. ' Size:=2;',
  22640. ' Result:=Self.Size+3;',
  22641. ' Self.Size:=4;',
  22642. ' with Self do begin',
  22643. ' Result:=Size+5;',
  22644. ' Size:=6;',
  22645. ' end;',
  22646. 'end;',
  22647. 'procedure THelper.SetSize(Value: longint);',
  22648. 'begin',
  22649. 'end;',
  22650. 'var w: word;',
  22651. 'begin',
  22652. ' w:=w.Size+7;',
  22653. ' w.Size:=w+8;',
  22654. ' with w do begin',
  22655. ' w:=Size+9;',
  22656. ' Size:=w+10;',
  22657. ' end;',
  22658. '']);
  22659. ConvertProgram;
  22660. CheckSource('TestTypeHelper_Property',
  22661. LinesToStr([ // statements
  22662. 'rtl.createHelper($mod, "THelper", null, function () {',
  22663. ' this.GetSize = function () {',
  22664. ' var Result = 0;',
  22665. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  22666. ' $mod.THelper.SetSize.call(this, 2);',
  22667. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  22668. ' $mod.THelper.SetSize.call(this, 4);',
  22669. ' var $with1 = this.get();',
  22670. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  22671. ' $mod.THelper.SetSize.call(this, 6);',
  22672. ' return Result;',
  22673. ' };',
  22674. ' this.SetSize = function (Value) {',
  22675. ' };',
  22676. '});',
  22677. 'this.w = 0;',
  22678. '']),
  22679. LinesToStr([ // $mod.$main
  22680. '$mod.w = $mod.THelper.GetSize.call({',
  22681. ' p: $mod,',
  22682. ' get: function () {',
  22683. ' return this.p.w;',
  22684. ' },',
  22685. ' set: function (v) {',
  22686. ' this.p.w = v;',
  22687. ' }',
  22688. '}) + 7;',
  22689. '$mod.THelper.SetSize.call({',
  22690. ' p: $mod,',
  22691. ' get: function () {',
  22692. ' return this.p.w;',
  22693. ' },',
  22694. ' set: function (v) {',
  22695. ' this.p.w = v;',
  22696. ' }',
  22697. '}, $mod.w + 8);',
  22698. 'var $with1 = $mod.w;',
  22699. '$mod.w = $mod.THelper.GetSize.call({',
  22700. ' get: function () {',
  22701. ' return $with1;',
  22702. ' },',
  22703. ' set: function (v) {',
  22704. ' $with1 = v;',
  22705. ' }',
  22706. '}) + 9;',
  22707. '$mod.THelper.SetSize.call({',
  22708. ' get: function () {',
  22709. ' return $with1;',
  22710. ' },',
  22711. ' set: function (v) {',
  22712. ' $with1 = v;',
  22713. ' }',
  22714. '}, $mod.w + 10);',
  22715. '']));
  22716. end;
  22717. procedure TTestModule.TestTypeHelper_Property_Array;
  22718. begin
  22719. StartProgram(false);
  22720. Add([
  22721. '{$modeswitch typehelpers}',
  22722. 'type',
  22723. ' THelper = type helper for word',
  22724. ' function GetItems(Index: byte): boolean;',
  22725. ' procedure SetItems(Index: byte; Value: boolean);',
  22726. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  22727. ' end;',
  22728. 'function THelper.GetItems(Index: byte): boolean;',
  22729. 'begin',
  22730. ' Result:=Items[1];',
  22731. ' Items[2]:=false;',
  22732. ' Result:=Self.Items[3];',
  22733. ' Self.Items[4]:=true;',
  22734. ' with Self do begin',
  22735. ' Result:=Items[5];',
  22736. ' Items[6]:=false;',
  22737. ' end;',
  22738. 'end;',
  22739. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  22740. 'begin',
  22741. 'end;',
  22742. 'var',
  22743. ' w: word;',
  22744. ' b: boolean;',
  22745. 'begin',
  22746. ' b:=w.Items[1];',
  22747. ' w.Items[2]:=b;',
  22748. ' with w do begin',
  22749. ' b:=Items[3];',
  22750. ' Items[4]:=b;',
  22751. ' end;',
  22752. '']);
  22753. ConvertProgram;
  22754. CheckSource('TestTypeHelper_Property_Array',
  22755. LinesToStr([ // statements
  22756. 'rtl.createHelper($mod, "THelper", null, function () {',
  22757. ' this.GetItems = function (Index) {',
  22758. ' var Result = false;',
  22759. ' Result = $mod.THelper.GetItems.call(this, 1);',
  22760. ' $mod.THelper.SetItems.call(this, 2, false);',
  22761. ' Result = $mod.THelper.GetItems.call(this, 3);',
  22762. ' $mod.THelper.SetItems.call(this, 4, true);',
  22763. ' var $with1 = this.get();',
  22764. ' Result = $mod.THelper.GetItems.call(this, 5);',
  22765. ' $mod.THelper.SetItems.call(this, 6, false);',
  22766. ' return Result;',
  22767. ' };',
  22768. ' this.SetItems = function (Index, Value) {',
  22769. ' };',
  22770. '});',
  22771. 'this.w = 0;',
  22772. 'this.b = false;',
  22773. '']),
  22774. LinesToStr([ // $mod.$main
  22775. '$mod.b = $mod.THelper.GetItems.call({',
  22776. ' p: $mod,',
  22777. ' get: function () {',
  22778. ' return this.p.w;',
  22779. ' },',
  22780. ' set: function (v) {',
  22781. ' this.p.w = v;',
  22782. ' }',
  22783. '}, 1);',
  22784. '$mod.THelper.SetItems.call({',
  22785. ' p: $mod,',
  22786. ' get: function () {',
  22787. ' return this.p.w;',
  22788. ' },',
  22789. ' set: function (v) {',
  22790. ' this.p.w = v;',
  22791. ' }',
  22792. '}, 2, $mod.b);',
  22793. 'var $with1 = $mod.w;',
  22794. '$mod.b = $mod.THelper.GetItems.call({',
  22795. ' get: function () {',
  22796. ' return $with1;',
  22797. ' },',
  22798. ' set: function (v) {',
  22799. ' $with1 = v;',
  22800. ' }',
  22801. '}, 3);',
  22802. '$mod.THelper.SetItems.call({',
  22803. ' get: function () {',
  22804. ' return $with1;',
  22805. ' },',
  22806. ' set: function (v) {',
  22807. ' $with1 = v;',
  22808. ' }',
  22809. '}, 4, $mod.b);',
  22810. '']));
  22811. end;
  22812. procedure TTestModule.TestTypeHelper_ClassProperty;
  22813. begin
  22814. StartProgram(false);
  22815. Add([
  22816. '{$modeswitch typehelpers}',
  22817. 'type',
  22818. ' THelper = type helper for word',
  22819. ' class function GetSize: longint; static;',
  22820. ' class procedure SetSize(Value: longint); static;',
  22821. ' class property Size: longint read GetSize write SetSize;',
  22822. ' end;',
  22823. 'class function THelper.GetSize: longint;',
  22824. 'begin',
  22825. ' Result:=Size+1;',
  22826. ' Size:=2;',
  22827. 'end;',
  22828. 'class procedure THelper.SetSize(Value: longint);',
  22829. 'begin',
  22830. 'end;',
  22831. 'begin',
  22832. '']);
  22833. ConvertProgram;
  22834. CheckSource('TestTypeHelper_ClassProperty',
  22835. LinesToStr([ // statements
  22836. 'rtl.createHelper($mod, "THelper", null, function () {',
  22837. ' this.GetSize = function () {',
  22838. ' var Result = 0;',
  22839. ' Result = $mod.THelper.GetSize() + 1;',
  22840. ' $mod.THelper.SetSize(2);',
  22841. ' return Result;',
  22842. ' };',
  22843. ' this.SetSize = function (Value) {',
  22844. ' };',
  22845. '});',
  22846. '']),
  22847. LinesToStr([ // $mod.$main
  22848. '']));
  22849. end;
  22850. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  22851. begin
  22852. StartProgram(false);
  22853. Add([
  22854. '{$modeswitch typehelpers}',
  22855. 'type',
  22856. ' THelper = type helper for word',
  22857. ' class function GetItems(Index: byte): boolean; static;',
  22858. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  22859. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  22860. ' end;',
  22861. 'class function THelper.GetItems(Index: byte): boolean;',
  22862. 'begin',
  22863. ' Result:=Items[1];',
  22864. ' Items[2]:=false;',
  22865. 'end;',
  22866. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  22867. 'begin',
  22868. 'end;',
  22869. 'var',
  22870. ' w: word;',
  22871. ' b: boolean;',
  22872. 'begin',
  22873. ' b:=w.Items[1];',
  22874. ' w.Items[2]:=b;',
  22875. ' with w do begin',
  22876. ' b:=Items[3];',
  22877. ' Items[4]:=b;',
  22878. ' end;',
  22879. '']);
  22880. ConvertProgram;
  22881. CheckSource('TestTypeHelper_ClassProperty_Array',
  22882. LinesToStr([ // statements
  22883. 'rtl.createHelper($mod, "THelper", null, function () {',
  22884. ' this.GetItems = function (Index) {',
  22885. ' var Result = false;',
  22886. ' Result = $mod.THelper.GetItems(1);',
  22887. ' $mod.THelper.SetItems(2, false);',
  22888. ' return Result;',
  22889. ' };',
  22890. ' this.SetItems = function (Index, Value) {',
  22891. ' };',
  22892. '});',
  22893. 'this.w = 0;',
  22894. 'this.b = false;',
  22895. '']),
  22896. LinesToStr([ // $mod.$main
  22897. '$mod.b = $mod.THelper.GetItems(1);',
  22898. '$mod.THelper.SetItems(2, $mod.b);',
  22899. 'var $with1 = $mod.w;',
  22900. '$mod.b = $mod.THelper.GetItems(3);',
  22901. '$mod.THelper.SetItems(4, $mod.b);',
  22902. '']));
  22903. end;
  22904. procedure TTestModule.TestTypeHelper_ClassMethod;
  22905. begin
  22906. StartProgram(false);
  22907. Add([
  22908. '{$modeswitch typehelpers}',
  22909. 'type',
  22910. ' THelper = type helper for word',
  22911. ' class procedure DoStatic; static;',
  22912. ' end;',
  22913. 'class procedure THelper.DoStatic;',
  22914. 'begin',
  22915. ' DoStatic;',
  22916. ' DoStatic();',
  22917. 'end;',
  22918. 'var w: word;',
  22919. 'begin',
  22920. ' w.DoStatic;',
  22921. ' w.DoStatic();',
  22922. '']);
  22923. ConvertProgram;
  22924. CheckSource('TestTypeHelper_ClassMethod',
  22925. LinesToStr([ // statements
  22926. 'rtl.createHelper($mod, "THelper", null, function () {',
  22927. ' this.DoStatic = function () {',
  22928. ' $mod.THelper.DoStatic();',
  22929. ' $mod.THelper.DoStatic();',
  22930. ' };',
  22931. '});',
  22932. 'this.w = 0;',
  22933. '']),
  22934. LinesToStr([ // $mod.$main
  22935. '$mod.THelper.DoStatic();',
  22936. '$mod.THelper.DoStatic();',
  22937. '']));
  22938. end;
  22939. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  22940. begin
  22941. StartProgram(false);
  22942. Add([
  22943. '{$modeswitch typehelpers}',
  22944. 'type',
  22945. ' THelper = type helper for word',
  22946. ' procedure Run; external name ''Run'';',
  22947. ' end;',
  22948. 'var w: word;',
  22949. 'begin',
  22950. ' w.Run;',
  22951. '']);
  22952. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  22953. ConvertProgram;
  22954. end;
  22955. procedure TTestModule.TestTypeHelper_Constructor;
  22956. begin
  22957. StartProgram(false);
  22958. Add([
  22959. '{$modeswitch typehelpers}',
  22960. 'type',
  22961. ' THelper = type helper for word',
  22962. ' constructor Init(e: longint);',
  22963. ' end;',
  22964. 'constructor THelper.Init(e: longint);',
  22965. 'begin',
  22966. ' Self:=e;',
  22967. ' Init(e+1);',
  22968. 'end;',
  22969. 'var w: word;',
  22970. 'begin',
  22971. ' w:=word.Init(2);',
  22972. ' w:=w.Init(3);',
  22973. ' with word do w:=Init(4);',
  22974. ' with w do w:=Init(5);',
  22975. '']);
  22976. ConvertProgram;
  22977. CheckSource('TestTypeHelper_Constructor',
  22978. LinesToStr([ // statements
  22979. 'rtl.createHelper($mod, "THelper", null, function () {',
  22980. ' this.Init = function (e) {',
  22981. ' this.set(e);',
  22982. ' $mod.THelper.Init.call(this, e + 1);',
  22983. ' return this.get();',
  22984. ' };',
  22985. ' this.$new = function (fn, args) {',
  22986. ' return this[fn].apply({',
  22987. ' p: 0,',
  22988. ' get: function () {',
  22989. ' return this.p;',
  22990. ' },',
  22991. ' set: function (v) {',
  22992. ' this.p = v;',
  22993. ' }',
  22994. ' }, args);',
  22995. ' };',
  22996. '});',
  22997. 'this.w = 0;',
  22998. '']),
  22999. LinesToStr([ // $mod.$main
  23000. '$mod.w = $mod.THelper.$new("Init", [2]);',
  23001. '$mod.w = $mod.THelper.Init.call({',
  23002. ' p: $mod,',
  23003. ' get: function () {',
  23004. ' return this.p.w;',
  23005. ' },',
  23006. ' set: function (v) {',
  23007. ' this.p.w = v;',
  23008. ' }',
  23009. '}, 3);',
  23010. '$mod.w = $mod.THelper.$new("Init", [4]);',
  23011. 'var $with1 = $mod.w;',
  23012. '$mod.w = $mod.THelper.Init.call({',
  23013. ' get: function () {',
  23014. ' return $with1;',
  23015. ' },',
  23016. ' set: function (v) {',
  23017. ' $with1 = v;',
  23018. ' }',
  23019. '}, 5);',
  23020. '']));
  23021. end;
  23022. procedure TTestModule.TestTypeHelper_Word;
  23023. begin
  23024. StartProgram(false);
  23025. Add([
  23026. '{$modeswitch typehelpers}',
  23027. 'type',
  23028. ' THelper = type helper for word',
  23029. ' procedure DoIt(e: byte = 123);',
  23030. ' end;',
  23031. 'procedure THelper.DoIt(e: byte);',
  23032. 'begin',
  23033. ' Self:=e;',
  23034. ' Self:=Self+1;',
  23035. ' with Self do Doit;',
  23036. 'end;',
  23037. 'begin',
  23038. ' word(3).DoIt;',
  23039. '']);
  23040. ConvertProgram;
  23041. CheckSource('TestTypeHelper_Word',
  23042. LinesToStr([ // statements
  23043. 'rtl.createHelper($mod, "THelper", null, function () {',
  23044. ' this.DoIt = function (e) {',
  23045. ' this.set(e);',
  23046. ' this.set(this.get() + 1);',
  23047. ' var $with1 = this.get();',
  23048. ' $mod.THelper.DoIt.call(this, 123);',
  23049. ' };',
  23050. '});',
  23051. '']),
  23052. LinesToStr([ // $mod.$main
  23053. '$mod.THelper.DoIt.call({',
  23054. ' get: function () {',
  23055. ' return 3;',
  23056. ' },',
  23057. ' set: function (v) {',
  23058. ' rtl.raiseE("EPropReadOnly");',
  23059. ' }',
  23060. '}, 123);',
  23061. '']));
  23062. end;
  23063. procedure TTestModule.TestTypeHelper_Double;
  23064. begin
  23065. StartProgram(false);
  23066. Add([
  23067. '{$modeswitch typehelpers}',
  23068. 'type',
  23069. ' Float = type double;',
  23070. ' THelper = type helper for Float',
  23071. ' const NPI = 3.141592;',
  23072. ' function ToStr: String;',
  23073. ' end;',
  23074. 'function THelper.ToStr: String;',
  23075. 'begin',
  23076. 'end;',
  23077. 'procedure DoIt(s: string);',
  23078. 'begin',
  23079. 'end;',
  23080. 'var f: Float;',
  23081. 'begin',
  23082. ' DoIt(f.toStr);',
  23083. ' DoIt(f.toStr());',
  23084. '']);
  23085. ConvertProgram;
  23086. CheckSource('TestTypeHelper_Double',
  23087. LinesToStr([ // statements
  23088. 'rtl.createHelper($mod, "THelper", null, function () {',
  23089. ' this.NPI = 3.141592;',
  23090. ' this.ToStr = function () {',
  23091. ' var Result = "";',
  23092. ' return Result;',
  23093. ' };',
  23094. '});',
  23095. 'this.DoIt = function (s) {',
  23096. '};',
  23097. 'this.f = 0.0;',
  23098. '']),
  23099. LinesToStr([ // $mod.$main
  23100. '$mod.DoIt($mod.THelper.ToStr.call({',
  23101. ' p: $mod,',
  23102. ' get: function () {',
  23103. ' return this.p.f;',
  23104. ' },',
  23105. ' set: function (v) {',
  23106. ' this.p.f = v;',
  23107. ' }',
  23108. '}));',
  23109. '$mod.DoIt($mod.THelper.ToStr.call({',
  23110. ' p: $mod,',
  23111. ' get: function () {',
  23112. ' return this.p.f;',
  23113. ' },',
  23114. ' set: function (v) {',
  23115. ' this.p.f = v;',
  23116. ' }',
  23117. '}));',
  23118. '']));
  23119. end;
  23120. procedure TTestModule.TestTypeHelper_NativeInt;
  23121. begin
  23122. StartProgram(false);
  23123. Add([
  23124. '{$modeswitch typehelpers}',
  23125. 'type',
  23126. ' MaxInt = type nativeint;',
  23127. ' THelperI = type helper for MaxInt',
  23128. ' function ToStr: String;',
  23129. ' end;',
  23130. ' MaxUInt = type nativeuint;',
  23131. ' THelperU = type helper for MaxUInt',
  23132. ' function ToStr: String;',
  23133. ' end;',
  23134. 'function THelperI.ToStr: String;',
  23135. 'begin',
  23136. ' Result:=str(Self);',
  23137. 'end;',
  23138. 'function THelperU.ToStr: String;',
  23139. 'begin',
  23140. ' Result:=str(Self);',
  23141. 'end;',
  23142. 'procedure DoIt(s: string);',
  23143. 'begin',
  23144. 'end;',
  23145. 'var i: MaxInt;',
  23146. 'begin',
  23147. ' DoIt(i.toStr);',
  23148. ' DoIt(i.toStr());',
  23149. ' (i*i).toStr;',
  23150. ' DoIt((i*i).toStr);',
  23151. '']);
  23152. ConvertProgram;
  23153. CheckSource('TestTypeHelper_NativeInt',
  23154. LinesToStr([ // statements
  23155. 'rtl.createHelper($mod, "THelperI", null, function () {',
  23156. ' this.ToStr = function () {',
  23157. ' var Result = "";',
  23158. ' Result = "" + this.get();',
  23159. ' return Result;',
  23160. ' };',
  23161. '});',
  23162. 'rtl.createHelper($mod, "THelperU", null, function () {',
  23163. ' this.ToStr = function () {',
  23164. ' var Result = "";',
  23165. ' Result = "" + this.get();',
  23166. ' return Result;',
  23167. ' };',
  23168. '});',
  23169. 'this.DoIt = function (s) {',
  23170. '};',
  23171. 'this.i = 0;',
  23172. '']),
  23173. LinesToStr([ // $mod.$main
  23174. '$mod.DoIt($mod.THelperI.ToStr.call({',
  23175. ' p: $mod,',
  23176. ' get: function () {',
  23177. ' return this.p.i;',
  23178. ' },',
  23179. ' set: function (v) {',
  23180. ' this.p.i = v;',
  23181. ' }',
  23182. '}));',
  23183. '$mod.DoIt($mod.THelperI.ToStr.call({',
  23184. ' p: $mod,',
  23185. ' get: function () {',
  23186. ' return this.p.i;',
  23187. ' },',
  23188. ' set: function (v) {',
  23189. ' this.p.i = v;',
  23190. ' }',
  23191. '}));',
  23192. '$mod.THelperI.ToStr.call({',
  23193. ' a: $mod.i * $mod.i,',
  23194. ' get: function () {',
  23195. ' return this.a;',
  23196. ' },',
  23197. ' set: function (v) {',
  23198. ' rtl.raiseE("EPropReadOnly");',
  23199. ' }',
  23200. '});',
  23201. '$mod.DoIt($mod.THelperI.ToStr.call({',
  23202. ' a: $mod.i * $mod.i,',
  23203. ' get: function () {',
  23204. ' return this.a;',
  23205. ' },',
  23206. ' set: function (v) {',
  23207. ' rtl.raiseE("EPropReadOnly");',
  23208. ' }',
  23209. '}));',
  23210. '']));
  23211. end;
  23212. procedure TTestModule.TestTypeHelper_StringChar;
  23213. begin
  23214. StartProgram(false);
  23215. Add([
  23216. '{$modeswitch typehelpers}',
  23217. 'type',
  23218. ' TStringHelper = type helper for string',
  23219. ' procedure DoIt(e: byte = 123);',
  23220. ' end;',
  23221. ' TCharHelper = type helper for char',
  23222. ' procedure Fly;',
  23223. ' end;',
  23224. 'procedure TStringHelper.DoIt(e: byte);',
  23225. 'begin',
  23226. ' Self[1]:=''c'';',
  23227. ' Self[2]:=Self[3];',
  23228. 'end;',
  23229. 'procedure TCharHelper.Fly;',
  23230. 'begin',
  23231. ' Self:=''c'';',
  23232. 'end;',
  23233. 'begin',
  23234. ' ''abc''.DoIt;',
  23235. ' ''xyz''.DoIt();',
  23236. ' ''c''.Fly();',
  23237. '']);
  23238. ConvertProgram;
  23239. CheckSource('TestTypeHelper_StringChar',
  23240. LinesToStr([ // statements
  23241. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  23242. ' this.DoIt = function (e) {',
  23243. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  23244. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  23245. ' };',
  23246. '});',
  23247. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  23248. ' this.Fly = function () {',
  23249. ' this.set("c");',
  23250. ' };',
  23251. '});',
  23252. '']),
  23253. LinesToStr([ // $mod.$main
  23254. '$mod.TStringHelper.DoIt.call({',
  23255. ' get: function () {',
  23256. ' return "abc";',
  23257. ' },',
  23258. ' set: function (v) {',
  23259. ' rtl.raiseE("EPropReadOnly");',
  23260. ' }',
  23261. '}, 123);',
  23262. '$mod.TStringHelper.DoIt.call({',
  23263. ' get: function () {',
  23264. ' return "xyz";',
  23265. ' },',
  23266. ' set: function (v) {',
  23267. ' rtl.raiseE("EPropReadOnly");',
  23268. ' }',
  23269. '}, 123);',
  23270. '$mod.TCharHelper.Fly.call({',
  23271. ' get: function () {',
  23272. ' return "c";',
  23273. ' },',
  23274. ' set: function (v) {',
  23275. ' rtl.raiseE("EPropReadOnly");',
  23276. ' }',
  23277. '});',
  23278. '']));
  23279. end;
  23280. procedure TTestModule.TestTypeHelper_JSValue;
  23281. begin
  23282. StartProgram(false);
  23283. Add([
  23284. '{$modeswitch typehelpers}',
  23285. 'type',
  23286. ' TExtValue = type jsvalue;',
  23287. ' THelper = type helper for TExtValue',
  23288. ' function ToStr: String;',
  23289. ' end;',
  23290. 'function THelper.ToStr: String;',
  23291. 'begin',
  23292. 'end;',
  23293. 'var',
  23294. ' s: string;',
  23295. ' v: TExtValue;',
  23296. 'begin',
  23297. ' s:=v.toStr;',
  23298. ' s:=v.toStr();',
  23299. ' TExtValue(s).toStr;',
  23300. '']);
  23301. ConvertProgram;
  23302. CheckSource('TestTypeHelper_JSValue',
  23303. LinesToStr([ // statements
  23304. 'rtl.createHelper($mod, "THelper", null, function () {',
  23305. ' this.ToStr = function () {',
  23306. ' var Result = "";',
  23307. ' return Result;',
  23308. ' };',
  23309. '});',
  23310. 'this.s = "";',
  23311. 'this.v = undefined;',
  23312. '']),
  23313. LinesToStr([ // $mod.$main
  23314. '$mod.s = $mod.THelper.ToStr.call({',
  23315. ' p: $mod,',
  23316. ' get: function () {',
  23317. ' return this.p.v;',
  23318. ' },',
  23319. ' set: function (v) {',
  23320. ' this.p.v = v;',
  23321. ' }',
  23322. '});',
  23323. '$mod.s = $mod.THelper.ToStr.call({',
  23324. ' p: $mod,',
  23325. ' get: function () {',
  23326. ' return this.p.v;',
  23327. ' },',
  23328. ' set: function (v) {',
  23329. ' this.p.v = v;',
  23330. ' }',
  23331. '});',
  23332. '$mod.THelper.ToStr.call({',
  23333. ' p: $mod,',
  23334. ' get: function () {',
  23335. ' return this.p.s;',
  23336. ' },',
  23337. ' set: function (v) {',
  23338. ' rtl.raiseE("EPropReadOnly");',
  23339. ' }',
  23340. '});',
  23341. '']));
  23342. end;
  23343. procedure TTestModule.TestTypeHelper_Array;
  23344. begin
  23345. StartProgram(false);
  23346. Add([
  23347. '{$modeswitch typehelpers}',
  23348. 'type',
  23349. ' TArrOfBool = array of boolean;',
  23350. ' TArrOfJS = array of jsvalue;',
  23351. ' THelper = type helper for TArrOfBool',
  23352. ' procedure DoIt(e: byte = 123);',
  23353. ' end;',
  23354. 'procedure THelper.DoIt(e: byte);',
  23355. 'begin',
  23356. ' Self[1]:=true;',
  23357. ' Self[2]:=not Self[3];',
  23358. ' SetLength(Self,4);',
  23359. 'end;',
  23360. 'var',
  23361. ' b: TArrOfBool;',
  23362. ' j: TArrOfJS;',
  23363. 'begin',
  23364. ' b.DoIt;',
  23365. ' TArrOfBool(j).DoIt();',
  23366. '']);
  23367. ConvertProgram;
  23368. CheckSource('TestTypeHelper_Array',
  23369. LinesToStr([ // statements
  23370. 'rtl.createHelper($mod, "THelper", null, function () {',
  23371. ' this.DoIt = function (e) {',
  23372. ' this.get()[1] = true;',
  23373. ' this.get()[2] = !this.get()[3];',
  23374. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  23375. ' };',
  23376. '});',
  23377. 'this.b = [];',
  23378. 'this.j = [];',
  23379. '']),
  23380. LinesToStr([ // $mod.$main
  23381. '$mod.THelper.DoIt.call({',
  23382. ' p: $mod,',
  23383. ' get: function () {',
  23384. ' return this.p.b;',
  23385. ' },',
  23386. ' set: function (v) {',
  23387. ' this.p.b = v;',
  23388. ' }',
  23389. '}, 123);',
  23390. '$mod.THelper.DoIt.call({',
  23391. ' p: $mod,',
  23392. ' get: function () {',
  23393. ' return this.p.j;',
  23394. ' },',
  23395. ' set: function (v) {',
  23396. ' this.p.j = v;',
  23397. ' }',
  23398. '}, 123);',
  23399. '']));
  23400. end;
  23401. procedure TTestModule.TestTypeHelper_EnumType;
  23402. begin
  23403. StartProgram(false);
  23404. Add([
  23405. '{$modeswitch typehelpers}',
  23406. 'type',
  23407. ' TEnum = (red,blue);',
  23408. ' THelper = type helper for TEnum',
  23409. ' procedure DoIt(e: byte = 123);',
  23410. ' class procedure Swing(w: word); static;',
  23411. ' end;',
  23412. 'procedure THelper.DoIt(e: byte);',
  23413. 'begin',
  23414. ' Self:=red;',
  23415. ' Self:=succ(Self);',
  23416. ' with Self do Doit;',
  23417. 'end;',
  23418. 'class procedure THelper.Swing(w: word);',
  23419. 'begin',
  23420. 'end;',
  23421. 'var e: TEnum;',
  23422. 'begin',
  23423. ' e.DoIt;',
  23424. ' red.DoIt;',
  23425. ' TEnum.blue.DoIt;',
  23426. ' TEnum(1).DoIt;',
  23427. ' TEnum.Swing(3);',
  23428. '']);
  23429. ConvertProgram;
  23430. CheckSource('TestTypeHelper_EnumType',
  23431. LinesToStr([ // statements
  23432. 'this.TEnum = {',
  23433. ' "0": "red",',
  23434. ' red: 0,',
  23435. ' "1": "blue",',
  23436. ' blue: 1',
  23437. '};',
  23438. 'rtl.createHelper($mod, "THelper", null, function () {',
  23439. ' this.DoIt = function (e) {',
  23440. ' this.set($mod.TEnum.red);',
  23441. ' this.set(this.get() + 1);',
  23442. ' var $with1 = this.get();',
  23443. ' $mod.THelper.DoIt.call(this, 123);',
  23444. ' };',
  23445. ' this.Swing = function (w) {',
  23446. ' };',
  23447. '});',
  23448. 'this.e = 0;',
  23449. '']),
  23450. LinesToStr([ // $mod.$main
  23451. '$mod.THelper.DoIt.call({',
  23452. ' p: $mod,',
  23453. ' get: function () {',
  23454. ' return this.p.e;',
  23455. ' },',
  23456. ' set: function (v) {',
  23457. ' this.p.e = v;',
  23458. ' }',
  23459. '}, 123);',
  23460. '$mod.THelper.DoIt.call({',
  23461. ' p: $mod.TEnum,',
  23462. ' get: function () {',
  23463. ' return this.p.red;',
  23464. ' },',
  23465. ' set: function (v) {',
  23466. ' rtl.raiseE("EPropReadOnly");',
  23467. ' }',
  23468. '}, 123);',
  23469. '$mod.THelper.DoIt.call({',
  23470. ' p: $mod.TEnum,',
  23471. ' get: function () {',
  23472. ' return this.p.blue;',
  23473. ' },',
  23474. ' set: function (v) {',
  23475. ' rtl.raiseE("EPropReadOnly");',
  23476. ' }',
  23477. '}, 123);',
  23478. '$mod.THelper.DoIt.call({',
  23479. ' get: function () {',
  23480. ' return 1;',
  23481. ' },',
  23482. ' set: function (v) {',
  23483. ' rtl.raiseE("EPropReadOnly");',
  23484. ' }',
  23485. '}, 123);',
  23486. '$mod.THelper.Swing(3);',
  23487. '']));
  23488. end;
  23489. procedure TTestModule.TestTypeHelper_SetType;
  23490. begin
  23491. StartProgram(false);
  23492. Add([
  23493. '{$modeswitch typehelpers}',
  23494. 'type',
  23495. ' TEnum = (red,blue);',
  23496. ' TSetOfEnum = set of TEnum;',
  23497. ' THelper = type helper for TSetOfEnum',
  23498. ' procedure DoIt(e: byte = 123);',
  23499. ' constructor Init(e: TEnum);',
  23500. ' constructor InitEmpty;',
  23501. ' end;',
  23502. 'procedure THelper.DoIt(e: byte);',
  23503. 'begin',
  23504. ' Self:=[];',
  23505. ' Self:=[red];',
  23506. ' Include(Self,blue);',
  23507. 'end;',
  23508. 'constructor THelper.Init(e: TEnum);',
  23509. 'begin',
  23510. ' Self:=[];',
  23511. ' Self:=[e];',
  23512. ' Include(Self,blue);',
  23513. 'end;',
  23514. 'constructor THelper.InitEmpty;',
  23515. 'begin',
  23516. 'end;',
  23517. 'var s: TSetOfEnum;',
  23518. 'begin',
  23519. ' s.DoIt;',
  23520. //' [red].DoIt;',
  23521. //' with s do DoIt;',
  23522. //' with [red,blue] do DoIt;',
  23523. ' s:=TSetOfEnum.Init(blue);',
  23524. ' s:=s.Init(blue);',
  23525. '']);
  23526. ConvertProgram;
  23527. CheckSource('TestTypeHelper_SetType',
  23528. LinesToStr([ // statements
  23529. 'this.TEnum = {',
  23530. ' "0": "red",',
  23531. ' red: 0,',
  23532. ' "1": "blue",',
  23533. ' blue: 1',
  23534. '};',
  23535. 'rtl.createHelper($mod, "THelper", null, function () {',
  23536. ' this.DoIt = function (e) {',
  23537. ' this.set({});',
  23538. ' this.set(rtl.createSet($mod.TEnum.red));',
  23539. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  23540. ' };',
  23541. ' this.Init = function (e) {',
  23542. ' this.set({});',
  23543. ' this.set(rtl.createSet(e));',
  23544. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  23545. ' return this.get();',
  23546. ' };',
  23547. ' this.InitEmpty = function () {',
  23548. ' return this.get();',
  23549. ' };',
  23550. ' this.$new = function (fn, args) {',
  23551. ' return this[fn].apply({',
  23552. ' p: {},',
  23553. ' get: function () {',
  23554. ' return this.p;',
  23555. ' },',
  23556. ' set: function (v) {',
  23557. ' this.p = v;',
  23558. ' }',
  23559. ' }, args);',
  23560. ' };',
  23561. '});',
  23562. 'this.s = {};',
  23563. '']),
  23564. LinesToStr([ // $mod.$main
  23565. '$mod.THelper.DoIt.call({',
  23566. ' p: $mod,',
  23567. ' get: function () {',
  23568. ' return this.p.s;',
  23569. ' },',
  23570. ' set: function (v) {',
  23571. ' this.p.s = v;',
  23572. ' }',
  23573. '}, 123);',
  23574. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  23575. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  23576. ' p: $mod,',
  23577. ' get: function () {',
  23578. ' return this.p.s;',
  23579. ' },',
  23580. ' set: function (v) {',
  23581. ' this.p.s = v;',
  23582. ' }',
  23583. '}, $mod.TEnum.blue));',
  23584. '']));
  23585. end;
  23586. procedure TTestModule.TestTypeHelper_NestedSelf;
  23587. begin
  23588. StartProgram(false);
  23589. Add([
  23590. '{$modeswitch typehelpers}',
  23591. 'type',
  23592. ' THelper = type helper for string',
  23593. ' procedure Run(Value: string);',
  23594. ' end;',
  23595. 'procedure THelper.Run(Value: string);',
  23596. ' function Sub(i: nativeint): boolean;',
  23597. ' begin',
  23598. ' Result:=Self[i+1]=Value[i];',
  23599. ' end;',
  23600. 'begin',
  23601. ' if Self[3]=Value[4] then ;',
  23602. 'end;',
  23603. 'begin',
  23604. '']);
  23605. ConvertProgram;
  23606. CheckSource('TestTypeHelper_NestedSelf',
  23607. LinesToStr([ // statements
  23608. 'rtl.createHelper($mod, "THelper", null, function () {',
  23609. ' this.Run = function (Value) {',
  23610. ' var $Self = this;',
  23611. ' function Sub(i) {',
  23612. ' var Result = false;',
  23613. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  23614. ' return Result;',
  23615. ' };',
  23616. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  23617. ' };',
  23618. '});',
  23619. '']),
  23620. LinesToStr([ // $mod.$main
  23621. '']));
  23622. end;
  23623. procedure TTestModule.TestProcType;
  23624. begin
  23625. StartProgram(false);
  23626. Add([
  23627. 'type',
  23628. ' TProcInt = procedure(vI: longint = 1);',
  23629. 'procedure DoIt(vJ: longint);',
  23630. 'begin end;',
  23631. 'var',
  23632. ' b: boolean;',
  23633. ' vP, vQ: tprocint;',
  23634. 'begin',
  23635. ' vp:=nil;',
  23636. ' vp:=vp;',
  23637. ' vp:=@doit;',
  23638. ' vp;',
  23639. ' vp();',
  23640. ' vp(2);',
  23641. ' b:=vp=nil;',
  23642. ' b:=nil=vp;',
  23643. ' b:=vp=vq;',
  23644. ' b:=vp=@doit;',
  23645. ' b:=@doit=vp;',
  23646. ' b:=vp<>nil;',
  23647. ' b:=nil<>vp;',
  23648. ' b:=vp<>vq;',
  23649. ' b:=vp<>@doit;',
  23650. ' b:=@doit<>vp;',
  23651. ' b:=Assigned(vp);',
  23652. ' if Assigned(vp) then ;']);
  23653. ConvertProgram;
  23654. CheckSource('TestProcType',
  23655. LinesToStr([ // statements
  23656. 'this.DoIt = function(vJ) {',
  23657. '};',
  23658. 'this.b = false;',
  23659. 'this.vP = null;',
  23660. 'this.vQ = null;'
  23661. ]),
  23662. LinesToStr([ // $mod.$main
  23663. '$mod.vP = null;',
  23664. '$mod.vP = $mod.vP;',
  23665. '$mod.vP = $mod.DoIt;',
  23666. '$mod.vP(1);',
  23667. '$mod.vP(1);',
  23668. '$mod.vP(2);',
  23669. '$mod.b = $mod.vP === null;',
  23670. '$mod.b = null === $mod.vP;',
  23671. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  23672. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23673. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23674. '$mod.b = $mod.vP !== null;',
  23675. '$mod.b = null !== $mod.vP;',
  23676. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  23677. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23678. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23679. '$mod.b = $mod.vP != null;',
  23680. 'if ($mod.vP != null) ;',
  23681. '']));
  23682. end;
  23683. procedure TTestModule.TestProcType_Arg;
  23684. begin
  23685. StartProgram(false);
  23686. Add([
  23687. 'type',
  23688. ' TProcInt = procedure(vI: longint = 1);',
  23689. 'procedure DoIt(vJ: longint); begin end;',
  23690. 'procedure DoSome(vP, vQ: TProcInt);',
  23691. 'var',
  23692. ' b: boolean;',
  23693. 'begin',
  23694. ' vp:=nil;',
  23695. ' vp:=vp;',
  23696. ' vp:=@doit;',
  23697. ' vp;',
  23698. ' vp();',
  23699. ' vp(2);',
  23700. ' b:=vp=nil;',
  23701. ' b:=nil=vp;',
  23702. ' b:=vp=vq;',
  23703. ' b:=vp=@doit;',
  23704. ' b:=@doit=vp;',
  23705. ' b:=vp<>nil;',
  23706. ' b:=nil<>vp;',
  23707. ' b:=vp<>vq;',
  23708. ' b:=vp<>@doit;',
  23709. ' b:=@doit<>vp;',
  23710. ' b:=Assigned(vp);',
  23711. ' if Assigned(vp) then ;',
  23712. 'end;',
  23713. 'begin',
  23714. ' DoSome(@DoIt,nil);']);
  23715. ConvertProgram;
  23716. CheckSource('TestProcType_Arg',
  23717. LinesToStr([ // statements
  23718. 'this.DoIt = function(vJ) {',
  23719. '};',
  23720. 'this.DoSome = function(vP, vQ) {',
  23721. ' var b = false;',
  23722. ' vP = null;',
  23723. ' vP = vP;',
  23724. ' vP = $mod.DoIt;',
  23725. ' vP(1);',
  23726. ' vP(1);',
  23727. ' vP(2);',
  23728. ' b = vP === null;',
  23729. ' b = null === vP;',
  23730. ' b = rtl.eqCallback(vP,vQ);',
  23731. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  23732. ' b = rtl.eqCallback($mod.DoIt, vP);',
  23733. ' b = vP !== null;',
  23734. ' b = null !== vP;',
  23735. ' b = !rtl.eqCallback(vP, vQ);',
  23736. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  23737. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  23738. ' b = vP != null;',
  23739. ' if (vP != null) ;',
  23740. '};',
  23741. '']),
  23742. LinesToStr([ // $mod.$main
  23743. '$mod.DoSome($mod.DoIt,null);',
  23744. '']));
  23745. end;
  23746. procedure TTestModule.TestProcType_FunctionFPC;
  23747. begin
  23748. StartProgram(false);
  23749. Add('type');
  23750. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23751. Add('function DoIt(vI: longint): longint;');
  23752. Add('begin end;');
  23753. Add('var');
  23754. Add(' b: boolean;');
  23755. Add(' vP, vQ: tfuncint;');
  23756. Add('begin');
  23757. Add(' vp:=nil;');
  23758. Add(' vp:=vp;');
  23759. Add(' vp:=@doit;'); // ok in fpc and delphi
  23760. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23761. Add(' vp;'); // ok in fpc and delphi
  23762. Add(' vp();');
  23763. Add(' vp(2);');
  23764. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23765. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23766. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23767. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23768. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23769. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23770. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23771. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23772. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23773. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23774. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23775. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23776. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23777. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23778. Add(' b:=Assigned(vp);');
  23779. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23780. Add(' doit(vp());'); // ok in fpc and delphi
  23781. Add(' doit(vp(2));'); // ok in fpc and delphi
  23782. ConvertProgram;
  23783. CheckSource('TestProcType_FunctionFPC',
  23784. LinesToStr([ // statements
  23785. 'this.DoIt = function(vI) {',
  23786. ' var Result = 0;',
  23787. ' return Result;',
  23788. '};',
  23789. 'this.b = false;',
  23790. 'this.vP = null;',
  23791. 'this.vQ = null;'
  23792. ]),
  23793. LinesToStr([ // $mod.$main
  23794. '$mod.vP = null;',
  23795. '$mod.vP = $mod.vP;',
  23796. '$mod.vP = $mod.DoIt;',
  23797. '$mod.vP(1);',
  23798. '$mod.vP(1);',
  23799. '$mod.vP(2);',
  23800. '$mod.b = $mod.vP === null;',
  23801. '$mod.b = null === $mod.vP;',
  23802. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  23803. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23804. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23805. '$mod.b = 4 === $mod.vP(1);',
  23806. '$mod.b = $mod.vP !== null;',
  23807. '$mod.b = null !== $mod.vP;',
  23808. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  23809. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23810. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23811. '$mod.b = 6 !== $mod.vP(1);',
  23812. '$mod.b = $mod.vP != null;',
  23813. '$mod.DoIt($mod.vP(1));',
  23814. '$mod.DoIt($mod.vP(2));',
  23815. '']));
  23816. end;
  23817. procedure TTestModule.TestProcType_FunctionDelphi;
  23818. begin
  23819. StartProgram(false);
  23820. Add('{$mode Delphi}');
  23821. Add('type');
  23822. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23823. Add('function DoIt(vI: longint): longint;');
  23824. Add('begin end;');
  23825. Add('var');
  23826. Add(' b: boolean;');
  23827. Add(' vP, vQ: tfuncint;');
  23828. Add('begin');
  23829. Add(' vp:=nil;');
  23830. Add(' vp:=vp;');
  23831. Add(' vp:=@doit;'); // ok in fpc and delphi
  23832. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23833. Add(' vp;'); // ok in fpc and delphi
  23834. Add(' vp();');
  23835. Add(' vp(2);');
  23836. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23837. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23838. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23839. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23840. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23841. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23842. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23843. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23844. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23845. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23846. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23847. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23848. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23849. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23850. Add(' b:=Assigned(vp);');
  23851. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23852. Add(' doit(vp());'); // ok in fpc and delphi
  23853. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  23854. ConvertProgram;
  23855. CheckSource('TestProcType_FunctionDelphi',
  23856. LinesToStr([ // statements
  23857. 'this.DoIt = function(vI) {',
  23858. ' var Result = 0;',
  23859. ' return Result;',
  23860. '};',
  23861. 'this.b = false;',
  23862. 'this.vP = null;',
  23863. 'this.vQ = null;'
  23864. ]),
  23865. LinesToStr([ // $mod.$main
  23866. '$mod.vP = null;',
  23867. '$mod.vP = $mod.vP;',
  23868. '$mod.vP = $mod.DoIt;',
  23869. '$mod.vP = $mod.DoIt;',
  23870. '$mod.vP(1);',
  23871. '$mod.vP(1);',
  23872. '$mod.vP(2);',
  23873. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  23874. '$mod.b = $mod.vP(1) === 3;',
  23875. '$mod.b = 4 === $mod.vP(1);',
  23876. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  23877. '$mod.b = $mod.vP(1) !== 5;',
  23878. '$mod.b = 6 !== $mod.vP(1);',
  23879. '$mod.b = $mod.vP != null;',
  23880. '$mod.DoIt($mod.vP(1));',
  23881. '$mod.DoIt($mod.vP(1));',
  23882. '$mod.DoIt($mod.vP(2));',
  23883. '']));
  23884. end;
  23885. procedure TTestModule.TestProcType_ProcedureDelphi;
  23886. begin
  23887. StartProgram(false);
  23888. Add('{$mode Delphi}');
  23889. Add('type');
  23890. Add(' TProc = procedure;');
  23891. Add('procedure DoIt;');
  23892. Add('begin end;');
  23893. Add('var');
  23894. Add(' b: boolean;');
  23895. Add(' vP, vQ: tproc;');
  23896. Add('begin');
  23897. Add(' vp:=nil;');
  23898. Add(' vp:=vp;');
  23899. Add(' vp:=vq;');
  23900. Add(' vp:=@doit;'); // ok in fpc and delphi, Note that in Delphi type of @F is Pointer, while in FPC it is the proc type
  23901. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23902. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  23903. Add(' vp;'); // ok in fpc and delphi
  23904. Add(' vp();');
  23905. // equal
  23906. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23907. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  23908. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23909. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23910. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  23911. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23912. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23913. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  23914. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23915. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23916. // unequal
  23917. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23918. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  23919. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23920. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  23921. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23922. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  23923. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23924. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  23925. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23926. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  23927. Add(' b:=Assigned(vp);');
  23928. ConvertProgram;
  23929. CheckSource('TestProcType_ProcedureDelphi',
  23930. LinesToStr([ // statements
  23931. 'this.DoIt = function() {',
  23932. '};',
  23933. 'this.b = false;',
  23934. 'this.vP = null;',
  23935. 'this.vQ = null;'
  23936. ]),
  23937. LinesToStr([ // $mod.$main
  23938. '$mod.vP = null;',
  23939. '$mod.vP = $mod.vP;',
  23940. '$mod.vP = $mod.vQ;',
  23941. '$mod.vP = $mod.DoIt;',
  23942. '$mod.vP = $mod.DoIt;',
  23943. '$mod.vP();',
  23944. '$mod.vP();',
  23945. '$mod.b = $mod.vP === null;',
  23946. '$mod.b = null === $mod.vP;',
  23947. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  23948. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23949. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23950. '$mod.b = $mod.vP !== null;',
  23951. '$mod.b = null !== $mod.vP;',
  23952. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  23953. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23954. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23955. '$mod.b = $mod.vP != null;',
  23956. '']));
  23957. end;
  23958. procedure TTestModule.TestProcType_AsParam;
  23959. begin
  23960. StartProgram(false);
  23961. Add('type');
  23962. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23963. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  23964. Add('var vJ: tfuncint;');
  23965. Add('begin');
  23966. Add(' vg:=vg;');
  23967. Add(' vj:=vh;');
  23968. Add(' vi:=vi;');
  23969. Add(' doit(vg,vg,vg);');
  23970. Add(' doit(vh,vh,vj);');
  23971. Add(' doit(vi,vi,vi);');
  23972. Add(' doit(vj,vj,vj);');
  23973. Add('end;');
  23974. Add('var i: tfuncint;');
  23975. Add('begin');
  23976. Add(' doit(i,i,i);');
  23977. ConvertProgram;
  23978. CheckSource('TestProcType_AsParam',
  23979. LinesToStr([ // statements
  23980. 'this.DoIt = function (vG,vH,vI) {',
  23981. ' var vJ = null;',
  23982. ' vG = vG;',
  23983. ' vJ = vH;',
  23984. ' vI.set(vI.get());',
  23985. ' $mod.DoIt(vG, vG, {',
  23986. ' get: function () {',
  23987. ' return vG;',
  23988. ' },',
  23989. ' set: function (v) {',
  23990. ' vG = v;',
  23991. ' }',
  23992. ' });',
  23993. ' $mod.DoIt(vH, vH, {',
  23994. ' get: function () {',
  23995. ' return vJ;',
  23996. ' },',
  23997. ' set: function (v) {',
  23998. ' vJ = v;',
  23999. ' }',
  24000. ' });',
  24001. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  24002. ' $mod.DoIt(vJ, vJ, {',
  24003. ' get: function () {',
  24004. ' return vJ;',
  24005. ' },',
  24006. ' set: function (v) {',
  24007. ' vJ = v;',
  24008. ' }',
  24009. ' });',
  24010. '};',
  24011. 'this.i = null;'
  24012. ]),
  24013. LinesToStr([
  24014. '$mod.DoIt($mod.i,$mod.i,{',
  24015. ' p: $mod,',
  24016. ' get: function () {',
  24017. ' return this.p.i;',
  24018. ' },',
  24019. ' set: function (v) {',
  24020. ' this.p.i = v;',
  24021. ' }',
  24022. '});'
  24023. ]));
  24024. end;
  24025. procedure TTestModule.TestProcType_MethodFPC;
  24026. begin
  24027. StartProgram(false);
  24028. Add('type');
  24029. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24030. Add(' TObject = class');
  24031. Add(' function DoIt(vA: longint = 1): longint;');
  24032. Add(' end;');
  24033. Add('function TObject.DoIt(vA: longint = 1): longint;');
  24034. Add('begin');
  24035. Add('end;');
  24036. Add('var');
  24037. Add(' Obj: TObject;');
  24038. Add(' vP: tfuncint;');
  24039. Add(' b: boolean;');
  24040. Add('begin');
  24041. Add(' vp:[email protected];'); // ok in fpc and delphi
  24042. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  24043. Add(' vp;'); // ok in fpc and delphi
  24044. Add(' vp();');
  24045. Add(' vp(2);');
  24046. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  24047. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  24048. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  24049. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  24050. ConvertProgram;
  24051. CheckSource('TestProcType_MethodFPC',
  24052. LinesToStr([ // statements
  24053. 'rtl.createClass($mod, "TObject", null, function () {',
  24054. ' this.$init = function () {',
  24055. ' };',
  24056. ' this.$final = function () {',
  24057. ' };',
  24058. ' this.DoIt = function (vA) {',
  24059. ' var Result = 0;',
  24060. ' return Result;',
  24061. ' };',
  24062. '});',
  24063. 'this.Obj = null;',
  24064. 'this.vP = null;',
  24065. 'this.b = false;'
  24066. ]),
  24067. LinesToStr([
  24068. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24069. '$mod.vP(1);',
  24070. '$mod.vP(1);',
  24071. '$mod.vP(2);',
  24072. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  24073. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  24074. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  24075. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  24076. '']));
  24077. end;
  24078. procedure TTestModule.TestProcType_MethodDelphi;
  24079. begin
  24080. StartProgram(false);
  24081. Add('{$mode delphi}');
  24082. Add('type');
  24083. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24084. Add(' TObject = class');
  24085. Add(' function DoIt(vA: longint = 1): longint;');
  24086. Add(' end;');
  24087. Add('function TObject.DoIt(vA: longint = 1): longint;');
  24088. Add('begin');
  24089. Add('end;');
  24090. Add('var');
  24091. Add(' Obj: TObject;');
  24092. Add(' vP: tfuncint;');
  24093. Add(' b: boolean;');
  24094. Add('begin');
  24095. Add(' vp:[email protected];'); // ok in fpc and delphi
  24096. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  24097. Add(' vp;'); // ok in fpc and delphi
  24098. Add(' vp();');
  24099. Add(' vp(2);');
  24100. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  24101. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  24102. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  24103. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  24104. ConvertProgram;
  24105. CheckSource('TestProcType_MethodDelphi',
  24106. LinesToStr([ // statements
  24107. 'rtl.createClass($mod, "TObject", null, function () {',
  24108. ' this.$init = function () {',
  24109. ' };',
  24110. ' this.$final = function () {',
  24111. ' };',
  24112. ' this.DoIt = function (vA) {',
  24113. ' var Result = 0;',
  24114. ' return Result;',
  24115. ' };',
  24116. '});',
  24117. 'this.Obj = null;',
  24118. 'this.vP = null;',
  24119. 'this.b = false;'
  24120. ]),
  24121. LinesToStr([
  24122. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24123. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24124. '$mod.vP(1);',
  24125. '$mod.vP(1);',
  24126. '$mod.vP(2);',
  24127. '']));
  24128. end;
  24129. procedure TTestModule.TestProcType_PropertyFPC;
  24130. begin
  24131. StartProgram(false);
  24132. Add('type');
  24133. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24134. Add(' TObject = class');
  24135. Add(' FOnFoo: TFuncInt;');
  24136. Add(' function DoIt(vA: longint = 1): longint;');
  24137. Add(' function GetFoo: TFuncInt;');
  24138. Add(' procedure SetFoo(const Value: TFuncInt);');
  24139. Add(' function GetEvents(Index: longint): TFuncInt;');
  24140. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  24141. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24142. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24143. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  24144. Add(' end;');
  24145. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24146. Add('function tobject.getfoo: tfuncint; begin end;');
  24147. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24148. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  24149. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  24150. Add('var');
  24151. Add(' Obj: TObject;');
  24152. Add(' vP: tfuncint;');
  24153. Add(' b: boolean;');
  24154. Add('begin');
  24155. Add(' obj.onfoo:=nil;');
  24156. Add(' obj.onbar:=nil;');
  24157. Add(' obj.events[1]:=nil;');
  24158. Add(' obj.onfoo:=obj.onfoo;');
  24159. Add(' obj.onbar:=obj.onbar;');
  24160. Add(' obj.events[2]:=obj.events[3];');
  24161. Add(' obj.onfoo:[email protected];');
  24162. Add(' obj.onbar:[email protected];');
  24163. Add(' obj.events[4]:[email protected];');
  24164. //Add(' obj.onfoo:=obj.doit;'); // delphi
  24165. //Add(' obj.onbar:=obj.doit;'); // delphi
  24166. //Add(' obj.events[4]:=obj.doit;'); // delphi
  24167. Add(' obj.onfoo;');
  24168. Add(' obj.onbar;');
  24169. //Add(' obj.events[5];'); ToDo in pasresolver
  24170. Add(' obj.onfoo();');
  24171. Add(' obj.onbar();');
  24172. Add(' obj.events[6]();');
  24173. Add(' b:=obj.onfoo=nil;');
  24174. Add(' b:=obj.onbar=nil;');
  24175. Add(' b:=obj.events[7]=nil;');
  24176. Add(' b:=obj.onfoo<>nil;');
  24177. Add(' b:=obj.onbar<>nil;');
  24178. Add(' b:=obj.events[8]<>nil;');
  24179. Add(' b:=obj.onfoo=vp;');
  24180. Add(' b:=obj.onbar=vp;');
  24181. Add(' b:=obj.events[9]=vp;');
  24182. Add(' b:=obj.onfoo=obj.onfoo;');
  24183. Add(' b:=obj.onbar=obj.onfoo;');
  24184. Add(' b:=obj.events[10]=obj.onfoo;');
  24185. Add(' b:=obj.onfoo<>obj.onfoo;');
  24186. Add(' b:=obj.onbar<>obj.onfoo;');
  24187. Add(' b:=obj.events[11]<>obj.onfoo;');
  24188. Add(' b:[email protected];');
  24189. Add(' b:[email protected];');
  24190. Add(' b:=obj.events[12][email protected];');
  24191. Add(' b:=obj.onfoo<>@obj.doit;');
  24192. Add(' b:=obj.onbar<>@obj.doit;');
  24193. Add(' b:=obj.events[12]<>@obj.doit;');
  24194. Add(' b:=Assigned(obj.onfoo);');
  24195. Add(' b:=Assigned(obj.onbar);');
  24196. Add(' b:=Assigned(obj.events[13]);');
  24197. ConvertProgram;
  24198. CheckSource('TestProcType_PropertyFPC',
  24199. LinesToStr([ // statements
  24200. 'rtl.createClass($mod, "TObject", null, function () {',
  24201. ' this.$init = function () {',
  24202. ' this.FOnFoo = null;',
  24203. ' };',
  24204. ' this.$final = function () {',
  24205. ' this.FOnFoo = undefined;',
  24206. ' };',
  24207. ' this.DoIt = function (vA) {',
  24208. ' var Result = 0;',
  24209. ' return Result;',
  24210. ' };',
  24211. 'this.GetFoo = function () {',
  24212. ' var Result = null;',
  24213. ' return Result;',
  24214. '};',
  24215. 'this.SetFoo = function (Value) {',
  24216. '};',
  24217. 'this.GetEvents = function (Index) {',
  24218. ' var Result = null;',
  24219. ' return Result;',
  24220. '};',
  24221. 'this.SetEvents = function (Index, Value) {',
  24222. '};',
  24223. '});',
  24224. 'this.Obj = null;',
  24225. 'this.vP = null;',
  24226. 'this.b = false;'
  24227. ]),
  24228. LinesToStr([
  24229. '$mod.Obj.FOnFoo = null;',
  24230. '$mod.Obj.SetFoo(null);',
  24231. '$mod.Obj.SetEvents(1, null);',
  24232. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  24233. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  24234. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  24235. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24236. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24237. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24238. '$mod.Obj.FOnFoo(1);',
  24239. '$mod.Obj.GetFoo();',
  24240. '$mod.Obj.FOnFoo(1);',
  24241. '$mod.Obj.GetFoo()(1);',
  24242. '$mod.Obj.GetEvents(6)(1);',
  24243. '$mod.b = $mod.Obj.FOnFoo === null;',
  24244. '$mod.b = $mod.Obj.GetFoo() === null;',
  24245. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  24246. '$mod.b = $mod.Obj.FOnFoo !== null;',
  24247. '$mod.b = $mod.Obj.GetFoo() !== null;',
  24248. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  24249. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  24250. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  24251. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  24252. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  24253. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  24254. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  24255. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  24256. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  24257. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  24258. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  24259. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  24260. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  24261. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  24262. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  24263. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  24264. '$mod.b = $mod.Obj.FOnFoo != null;',
  24265. '$mod.b = $mod.Obj.GetFoo() != null;',
  24266. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  24267. '']));
  24268. end;
  24269. procedure TTestModule.TestProcType_PropertyDelphi;
  24270. begin
  24271. StartProgram(false);
  24272. Add('{$mode delphi}');
  24273. Add('type');
  24274. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24275. Add(' TObject = class');
  24276. Add(' FOnFoo: TFuncInt;');
  24277. Add(' function DoIt(vA: longint = 1): longint;');
  24278. Add(' function GetFoo: TFuncInt;');
  24279. Add(' procedure SetFoo(const Value: TFuncInt);');
  24280. Add(' function GetEvents(Index: longint): TFuncInt;');
  24281. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  24282. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24283. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24284. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  24285. Add(' end;');
  24286. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24287. Add('function tobject.getfoo: tfuncint; begin end;');
  24288. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24289. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  24290. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  24291. Add('var');
  24292. Add(' Obj: TObject;');
  24293. Add(' vP: tfuncint;');
  24294. Add(' b: boolean;');
  24295. Add('begin');
  24296. Add(' obj.onfoo:=nil;');
  24297. Add(' obj.onbar:=nil;');
  24298. Add(' obj.events[1]:=nil;');
  24299. Add(' obj.onfoo:=obj.onfoo;');
  24300. Add(' obj.onbar:=obj.onbar;');
  24301. Add(' obj.events[2]:=obj.events[3];');
  24302. Add(' obj.onfoo:[email protected];');
  24303. Add(' obj.onbar:[email protected];');
  24304. Add(' obj.events[4]:[email protected];');
  24305. Add(' obj.onfoo:=obj.doit;'); // delphi
  24306. Add(' obj.onbar:=obj.doit;'); // delphi
  24307. Add(' obj.events[4]:=obj.doit;'); // delphi
  24308. Add(' obj.onfoo;');
  24309. Add(' obj.onbar;');
  24310. //Add(' obj.events[5];'); ToDo in pasresolver
  24311. Add(' obj.onfoo();');
  24312. Add(' obj.onbar();');
  24313. Add(' obj.events[6]();');
  24314. //Add(' b:=obj.onfoo=nil;'); // fpc
  24315. //Add(' b:=obj.onbar=nil;'); // fpc
  24316. //Add(' b:=obj.events[7]=nil;'); // fpc
  24317. //Add(' b:=obj.onfoo<>nil;'); // fpc
  24318. //Add(' b:=obj.onbar<>nil;'); // fpc
  24319. //Add(' b:=obj.events[8]<>nil;'); // fpc
  24320. Add(' b:=obj.onfoo=vp;');
  24321. Add(' b:=obj.onbar=vp;');
  24322. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  24323. Add(' b:=obj.onfoo=obj.onfoo;');
  24324. Add(' b:=obj.onbar=obj.onfoo;');
  24325. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  24326. Add(' b:=obj.onfoo<>obj.onfoo;');
  24327. Add(' b:=obj.onbar<>obj.onfoo;');
  24328. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  24329. //Add(' b:[email protected];'); // fpc
  24330. //Add(' b:[email protected];'); // fpc
  24331. //Add(' b:=obj.events[12][email protected];'); // fpc
  24332. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  24333. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  24334. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  24335. Add(' b:=Assigned(obj.onfoo);');
  24336. Add(' b:=Assigned(obj.onbar);');
  24337. Add(' b:=Assigned(obj.events[13]);');
  24338. ConvertProgram;
  24339. CheckSource('TestProcType_PropertyDelphi',
  24340. LinesToStr([ // statements
  24341. 'rtl.createClass($mod, "TObject", null, function () {',
  24342. ' this.$init = function () {',
  24343. ' this.FOnFoo = null;',
  24344. ' };',
  24345. ' this.$final = function () {',
  24346. ' this.FOnFoo = undefined;',
  24347. ' };',
  24348. ' this.DoIt = function (vA) {',
  24349. ' var Result = 0;',
  24350. ' return Result;',
  24351. ' };',
  24352. 'this.GetFoo = function () {',
  24353. ' var Result = null;',
  24354. ' return Result;',
  24355. '};',
  24356. 'this.SetFoo = function (Value) {',
  24357. '};',
  24358. 'this.GetEvents = function (Index) {',
  24359. ' var Result = null;',
  24360. ' return Result;',
  24361. '};',
  24362. 'this.SetEvents = function (Index, Value) {',
  24363. '};',
  24364. '});',
  24365. 'this.Obj = null;',
  24366. 'this.vP = null;',
  24367. 'this.b = false;'
  24368. ]),
  24369. LinesToStr([
  24370. '$mod.Obj.FOnFoo = null;',
  24371. '$mod.Obj.SetFoo(null);',
  24372. '$mod.Obj.SetEvents(1, null);',
  24373. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  24374. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  24375. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  24376. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24377. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24378. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24379. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24380. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24381. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24382. '$mod.Obj.FOnFoo(1);',
  24383. '$mod.Obj.GetFoo();',
  24384. '$mod.Obj.FOnFoo(1);',
  24385. '$mod.Obj.GetFoo()(1);',
  24386. '$mod.Obj.GetEvents(6)(1);',
  24387. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  24388. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  24389. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  24390. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  24391. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  24392. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  24393. '$mod.b = $mod.Obj.FOnFoo != null;',
  24394. '$mod.b = $mod.Obj.GetFoo() != null;',
  24395. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  24396. '']));
  24397. end;
  24398. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  24399. begin
  24400. StartProgram(false);
  24401. Add('type');
  24402. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24403. Add(' TObject = class');
  24404. Add(' FOnFoo: TFuncInt;');
  24405. Add(' function DoIt(vA: longint = 1): longint;');
  24406. Add(' function GetFoo: TFuncInt;');
  24407. Add(' procedure SetFoo(const Value: TFuncInt);');
  24408. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24409. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24410. Add(' end;');
  24411. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24412. Add('function tobject.getfoo: tfuncint; begin end;');
  24413. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24414. Add('var');
  24415. Add(' Obj: TObject;');
  24416. Add(' vP: tfuncint;');
  24417. Add(' b: boolean;');
  24418. Add('begin');
  24419. Add('with obj do begin');
  24420. Add(' fonfoo:=nil;');
  24421. Add(' onfoo:=nil;');
  24422. Add(' onbar:=nil;');
  24423. Add(' fonfoo:=fonfoo;');
  24424. Add(' onfoo:=onfoo;');
  24425. Add(' onbar:=onbar;');
  24426. Add(' fonfoo:=@doit;');
  24427. Add(' onfoo:=@doit;');
  24428. Add(' onbar:=@doit;');
  24429. //Add(' fonfoo:=doit;'); // delphi
  24430. //Add(' onfoo:=doit;'); // delphi
  24431. //Add(' onbar:=doit;'); // delphi
  24432. Add(' fonfoo;');
  24433. Add(' onfoo;');
  24434. Add(' onbar;');
  24435. Add(' fonfoo();');
  24436. Add(' onfoo();');
  24437. Add(' onbar();');
  24438. Add(' b:=fonfoo=nil;');
  24439. Add(' b:=onfoo=nil;');
  24440. Add(' b:=onbar=nil;');
  24441. Add(' b:=fonfoo<>nil;');
  24442. Add(' b:=onfoo<>nil;');
  24443. Add(' b:=onbar<>nil;');
  24444. Add(' b:=fonfoo=vp;');
  24445. Add(' b:=onfoo=vp;');
  24446. Add(' b:=onbar=vp;');
  24447. Add(' b:=fonfoo=fonfoo;');
  24448. Add(' b:=onfoo=onfoo;');
  24449. Add(' b:=onbar=onfoo;');
  24450. Add(' b:=fonfoo<>fonfoo;');
  24451. Add(' b:=onfoo<>onfoo;');
  24452. Add(' b:=onbar<>onfoo;');
  24453. Add(' b:=fonfoo=@doit;');
  24454. Add(' b:=onfoo=@doit;');
  24455. Add(' b:=onbar=@doit;');
  24456. Add(' b:=fonfoo<>@doit;');
  24457. Add(' b:=onfoo<>@doit;');
  24458. Add(' b:=onbar<>@doit;');
  24459. Add(' b:=Assigned(fonfoo);');
  24460. Add(' b:=Assigned(onfoo);');
  24461. Add(' b:=Assigned(onbar);');
  24462. Add('end;');
  24463. ConvertProgram;
  24464. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  24465. LinesToStr([ // statements
  24466. 'rtl.createClass($mod, "TObject", null, function () {',
  24467. ' this.$init = function () {',
  24468. ' this.FOnFoo = null;',
  24469. ' };',
  24470. ' this.$final = function () {',
  24471. ' this.FOnFoo = undefined;',
  24472. ' };',
  24473. ' this.DoIt = function (vA) {',
  24474. ' var Result = 0;',
  24475. ' return Result;',
  24476. ' };',
  24477. ' this.GetFoo = function () {',
  24478. ' var Result = null;',
  24479. ' return Result;',
  24480. ' };',
  24481. ' this.SetFoo = function (Value) {',
  24482. ' };',
  24483. '});',
  24484. 'this.Obj = null;',
  24485. 'this.vP = null;',
  24486. 'this.b = false;'
  24487. ]),
  24488. LinesToStr([
  24489. 'var $with1 = $mod.Obj;',
  24490. '$with1.FOnFoo = null;',
  24491. '$with1.FOnFoo = null;',
  24492. '$with1.SetFoo(null);',
  24493. '$with1.FOnFoo = $with1.FOnFoo;',
  24494. '$with1.FOnFoo = $with1.FOnFoo;',
  24495. '$with1.SetFoo($with1.GetFoo());',
  24496. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  24497. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  24498. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  24499. '$with1.FOnFoo(1);',
  24500. '$with1.FOnFoo(1);',
  24501. '$with1.GetFoo();',
  24502. '$with1.FOnFoo(1);',
  24503. '$with1.FOnFoo(1);',
  24504. '$with1.GetFoo()(1);',
  24505. '$mod.b = $with1.FOnFoo === null;',
  24506. '$mod.b = $with1.FOnFoo === null;',
  24507. '$mod.b = $with1.GetFoo() === null;',
  24508. '$mod.b = $with1.FOnFoo !== null;',
  24509. '$mod.b = $with1.FOnFoo !== null;',
  24510. '$mod.b = $with1.GetFoo() !== null;',
  24511. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  24512. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  24513. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  24514. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24515. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24516. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  24517. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24518. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24519. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  24520. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24521. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24522. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  24523. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24524. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24525. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  24526. '$mod.b = $with1.FOnFoo != null;',
  24527. '$mod.b = $with1.FOnFoo != null;',
  24528. '$mod.b = $with1.GetFoo() != null;',
  24529. '']));
  24530. end;
  24531. procedure TTestModule.TestProcType_Nested;
  24532. begin
  24533. StartProgram(false);
  24534. Add([
  24535. 'type',
  24536. ' TProcInt = procedure(vI: longint = 1);',
  24537. 'procedure DoIt(vJ: longint);',
  24538. 'var aProc: TProcInt;',
  24539. ' b: boolean;',
  24540. ' procedure Sub(vK: longint);',
  24541. ' var aSub: TProcInt;',
  24542. ' procedure SubSub(vK: longint);',
  24543. ' var aSubSub: TProcInt;',
  24544. ' begin;',
  24545. ' aProc:=@DoIt;',
  24546. ' aSub:=@DoIt;',
  24547. ' aSubSub:=@DoIt;',
  24548. ' aProc:=@Sub;',
  24549. ' aSub:=@Sub;',
  24550. ' aSubSub:=@Sub;',
  24551. ' aProc:=@SubSub;',
  24552. ' aSub:=@SubSub;',
  24553. ' aSubSub:=@SubSub;',
  24554. ' end;',
  24555. ' begin;',
  24556. ' end;',
  24557. 'begin;',
  24558. ' aProc:=@Sub;',
  24559. ' b:=aProc=@Sub;',
  24560. ' b:=@Sub=aProc;',
  24561. 'end;',
  24562. 'begin',
  24563. '']);
  24564. ConvertProgram;
  24565. CheckSource('TestProcType_Nested',
  24566. LinesToStr([ // statements
  24567. 'this.DoIt = function (vJ) {',
  24568. ' var aProc = null;',
  24569. ' var b = false;',
  24570. ' function Sub(vK) {',
  24571. ' var aSub = null;',
  24572. ' function SubSub(vK) {',
  24573. ' var aSubSub = null;',
  24574. ' aProc = $mod.DoIt;',
  24575. ' aSub = $mod.DoIt;',
  24576. ' aSubSub = $mod.DoIt;',
  24577. ' aProc = Sub;',
  24578. ' aSub = Sub;',
  24579. ' aSubSub = Sub;',
  24580. ' aProc = SubSub;',
  24581. ' aSub = SubSub;',
  24582. ' aSubSub = SubSub;',
  24583. ' };',
  24584. ' };',
  24585. ' aProc = Sub;',
  24586. ' b = rtl.eqCallback(aProc, Sub);',
  24587. ' b = rtl.eqCallback(Sub, aProc);',
  24588. '};',
  24589. '']),
  24590. LinesToStr([ // $mod.$main
  24591. '']));
  24592. end;
  24593. procedure TTestModule.TestProcType_NestedOfObject;
  24594. begin
  24595. StartProgram(false);
  24596. Add([
  24597. 'type',
  24598. ' TProcInt = procedure(vI: longint = 1) of object;',
  24599. ' TObject = class',
  24600. ' procedure DoIt(vJ: longint);',
  24601. ' end;',
  24602. 'procedure TObject.DoIt(vJ: longint);',
  24603. 'var aProc: TProcInt;',
  24604. ' b: boolean;',
  24605. ' procedure Sub(vK: longint);',
  24606. ' var aSub: TProcInt;',
  24607. ' procedure SubSub(vK: longint);',
  24608. ' var aSubSub: TProcInt;',
  24609. ' begin;',
  24610. ' aProc:=@DoIt;',
  24611. ' aSub:=@DoIt;',
  24612. ' aSubSub:=@DoIt;',
  24613. ' aProc:=@Sub;',
  24614. ' aSub:=@Sub;',
  24615. ' aSubSub:=@Sub;',
  24616. ' aProc:=@SubSub;',
  24617. ' aSub:=@SubSub;',
  24618. ' aSubSub:=@SubSub;',
  24619. ' end;',
  24620. ' begin;',
  24621. ' end;',
  24622. 'begin;',
  24623. ' aProc:=@Sub;',
  24624. ' b:=aProc=@Sub;',
  24625. ' b:=@Sub=aProc;',
  24626. 'end;',
  24627. 'begin',
  24628. '']);
  24629. ConvertProgram;
  24630. CheckSource('TestProcType_Nested',
  24631. LinesToStr([ // statements
  24632. 'rtl.createClass($mod, "TObject", null, function () {',
  24633. ' this.$init = function () {',
  24634. ' };',
  24635. ' this.$final = function () {',
  24636. ' };',
  24637. ' this.DoIt = function (vJ) {',
  24638. ' var $Self = this;',
  24639. ' var aProc = null;',
  24640. ' var b = false;',
  24641. ' function Sub(vK) {',
  24642. ' var aSub = null;',
  24643. ' function SubSub(vK) {',
  24644. ' var aSubSub = null;',
  24645. ' aProc = rtl.createCallback($Self, "DoIt");',
  24646. ' aSub = rtl.createCallback($Self, "DoIt");',
  24647. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  24648. ' aProc = Sub;',
  24649. ' aSub = Sub;',
  24650. ' aSubSub = Sub;',
  24651. ' aProc = SubSub;',
  24652. ' aSub = SubSub;',
  24653. ' aSubSub = SubSub;',
  24654. ' };',
  24655. ' };',
  24656. ' aProc = Sub;',
  24657. ' b = rtl.eqCallback(aProc, Sub);',
  24658. ' b = rtl.eqCallback(Sub, aProc);',
  24659. ' };',
  24660. '});',
  24661. '']),
  24662. LinesToStr([ // $mod.$main
  24663. '']));
  24664. end;
  24665. procedure TTestModule.TestProcType_ReferenceToProc;
  24666. begin
  24667. StartProgram(false);
  24668. Add([
  24669. 'type',
  24670. ' TProcRef = reference to procedure(i: longint = 0);',
  24671. ' TFuncRef = reference to function(i: longint = 0): longint;',
  24672. 'var',
  24673. ' p: TProcRef;',
  24674. ' f: TFuncRef;',
  24675. 'procedure DoIt(i: longint);',
  24676. 'begin',
  24677. 'end;',
  24678. 'function GetIt(i: longint): longint;',
  24679. 'begin',
  24680. ' p:=@DoIt;',
  24681. ' f:=@GetIt;',
  24682. ' f;',
  24683. ' f();',
  24684. ' f(1);',
  24685. 'end;',
  24686. 'begin',
  24687. ' p:=@DoIt;',
  24688. ' f:=@GetIt;',
  24689. ' f;',
  24690. ' f();',
  24691. ' f(1);',
  24692. ' p:=TProcRef(f);',
  24693. '']);
  24694. ConvertProgram;
  24695. CheckSource('TestProcType_ReferenceToProc',
  24696. LinesToStr([ // statements
  24697. 'this.p = null;',
  24698. 'this.f = null;',
  24699. 'this.DoIt = function (i) {',
  24700. '};',
  24701. 'this.GetIt = function (i) {',
  24702. ' var Result = 0;',
  24703. ' $mod.p = $mod.DoIt;',
  24704. ' $mod.f = $mod.GetIt;',
  24705. ' $mod.f(0);',
  24706. ' $mod.f(0);',
  24707. ' $mod.f(1);',
  24708. ' return Result;',
  24709. '};',
  24710. '']),
  24711. LinesToStr([ // $mod.$main
  24712. '$mod.p = $mod.DoIt;',
  24713. '$mod.f = $mod.GetIt;',
  24714. '$mod.f(0);',
  24715. '$mod.f(0);',
  24716. '$mod.f(1);',
  24717. '$mod.p = $mod.f;',
  24718. '']));
  24719. end;
  24720. procedure TTestModule.TestProcType_ReferenceToMethod;
  24721. begin
  24722. StartProgram(false);
  24723. Add([
  24724. 'type',
  24725. ' TFuncRef = reference to function(i: longint = 5): longint;',
  24726. ' TObject = class',
  24727. ' function Grow(s: longint): longint;',
  24728. ' end;',
  24729. 'var',
  24730. ' f: tfuncref;',
  24731. 'function tobject.grow(s: longint): longint;',
  24732. ' function GrowSub(i: longint): longint;',
  24733. ' begin',
  24734. ' f:=@grow;',
  24735. ' f:=@growsub;',
  24736. ' end;',
  24737. 'begin',
  24738. ' f:=@grow;',
  24739. ' f:=@growsub;',
  24740. 'end;',
  24741. 'begin',
  24742. '']);
  24743. ConvertProgram;
  24744. CheckSource('TestProcType_ReferenceToMethod',
  24745. LinesToStr([ // statements
  24746. 'rtl.createClass($mod, "TObject", null, function () {',
  24747. ' this.$init = function () {',
  24748. ' };',
  24749. ' this.$final = function () {',
  24750. ' };',
  24751. ' this.Grow = function (s) {',
  24752. ' var $Self = this;',
  24753. ' var Result = 0;',
  24754. ' function GrowSub(i) {',
  24755. ' var Result = 0;',
  24756. ' $mod.f = rtl.createCallback($Self, "Grow");',
  24757. ' $mod.f = GrowSub;',
  24758. ' return Result;',
  24759. ' };',
  24760. ' $mod.f = rtl.createCallback($Self, "Grow");',
  24761. ' $mod.f = GrowSub;',
  24762. ' return Result;',
  24763. ' };',
  24764. '});',
  24765. 'this.f = null;',
  24766. '']),
  24767. LinesToStr([ // $mod.$main
  24768. '']));
  24769. end;
  24770. procedure TTestModule.TestProcType_Typecast;
  24771. begin
  24772. StartProgram(false);
  24773. Add([
  24774. 'type',
  24775. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  24776. ' TEvent = procedure of object;',
  24777. ' TGetter = function:longint of object;',
  24778. ' TProcA = procedure(i: longint);',
  24779. ' TFuncB = function(i, j: longint): longint;',
  24780. 'procedure DoIt(); varargs; begin end;',
  24781. 'var',
  24782. ' Notify: tnotifyevent;',
  24783. ' Event: tevent;',
  24784. ' Getter: tgetter;',
  24785. ' ProcA: tproca;',
  24786. ' FuncB: tfuncb;',
  24787. ' p: pointer;',
  24788. 'begin',
  24789. ' notify:=tnotifyevent(event);',
  24790. ' event:=tevent(event);',
  24791. ' event:=tevent(notify);',
  24792. ' event:=tevent(getter);',
  24793. ' event:=tevent(proca);',
  24794. ' proca:=tproca(funcb);',
  24795. ' funcb:=tfuncb(funcb);',
  24796. ' funcb:=tfuncb(proca);',
  24797. ' funcb:=tfuncb(getter);',
  24798. ' proca:=tproca(p);',
  24799. ' funcb:=tfuncb(p);',
  24800. ' getter:=tgetter(p);',
  24801. ' p:=pointer(notify);',
  24802. ' p:=notify;',
  24803. ' p:=pointer(proca);',
  24804. ' p:=proca;',
  24805. ' p:=pointer(funcb);',
  24806. ' p:=funcb;',
  24807. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  24808. '']);
  24809. ConvertProgram;
  24810. CheckSource('TestProcType_Typecast',
  24811. LinesToStr([ // statements
  24812. 'this.DoIt = function () {',
  24813. '};',
  24814. 'this.Notify = null;',
  24815. 'this.Event = null;',
  24816. 'this.Getter = null;',
  24817. 'this.ProcA = null;',
  24818. 'this.FuncB = null;',
  24819. 'this.p = null;',
  24820. '']),
  24821. LinesToStr([ // $mod.$main
  24822. '$mod.Notify = $mod.Event;',
  24823. '$mod.Event = $mod.Event;',
  24824. '$mod.Event = $mod.Notify;',
  24825. '$mod.Event = $mod.Getter;',
  24826. '$mod.Event = $mod.ProcA;',
  24827. '$mod.ProcA = $mod.FuncB;',
  24828. '$mod.FuncB = $mod.FuncB;',
  24829. '$mod.FuncB = $mod.ProcA;',
  24830. '$mod.FuncB = $mod.Getter;',
  24831. '$mod.ProcA = $mod.p;',
  24832. '$mod.FuncB = $mod.p;',
  24833. '$mod.Getter = $mod.p;',
  24834. '$mod.p = $mod.Notify;',
  24835. '$mod.p = $mod.Notify;',
  24836. '$mod.p = $mod.ProcA;',
  24837. '$mod.p = $mod.ProcA;',
  24838. '$mod.p = $mod.FuncB;',
  24839. '$mod.p = $mod.FuncB;',
  24840. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  24841. '']));
  24842. end;
  24843. procedure TTestModule.TestProcType_PassProcToUntyped;
  24844. begin
  24845. StartProgram(false);
  24846. Add([
  24847. 'type',
  24848. ' TEvent = procedure of object;',
  24849. ' TFunc = function: longint;',
  24850. 'procedure DoIt(); varargs; begin end;',
  24851. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  24852. 'var',
  24853. ' Event: tevent;',
  24854. ' Func: TFunc;',
  24855. 'begin',
  24856. ' doit(event,func);',
  24857. ' dosome(event,event,event);',
  24858. ' dosome(func,func,func);',
  24859. '']);
  24860. ConvertProgram;
  24861. CheckSource('TestProcType_PassProcToUntyped',
  24862. LinesToStr([ // statements
  24863. 'this.DoIt = function () {',
  24864. '};',
  24865. 'this.DoSome = function (a, b, p) {',
  24866. '};',
  24867. 'this.Event = null;',
  24868. 'this.Func = null;',
  24869. '']),
  24870. LinesToStr([ // $mod.$main
  24871. '$mod.DoIt($mod.Event, $mod.Func);',
  24872. '$mod.DoSome($mod.Event, {',
  24873. ' p: $mod,',
  24874. ' get: function () {',
  24875. ' return this.p.Event;',
  24876. ' },',
  24877. ' set: function (v) {',
  24878. ' this.p.Event = v;',
  24879. ' }',
  24880. '}, $mod.Event);',
  24881. '$mod.DoSome($mod.Func, {',
  24882. ' p: $mod,',
  24883. ' get: function () {',
  24884. ' return this.p.Func;',
  24885. ' },',
  24886. ' set: function (v) {',
  24887. ' this.p.Func = v;',
  24888. ' }',
  24889. '}, $mod.Func);',
  24890. '']));
  24891. end;
  24892. procedure TTestModule.TestProcType_PassProcToArray;
  24893. begin
  24894. StartProgram(false);
  24895. Add([
  24896. 'type',
  24897. ' TFunc = function: longint;',
  24898. ' TArrFunc = array of TFunc;',
  24899. 'procedure DoIt(Arr: TArrFunc); begin end;',
  24900. 'function GetIt: longint; begin end;',
  24901. 'var',
  24902. ' Func: tfunc;',
  24903. 'begin',
  24904. ' doit([]);',
  24905. ' doit([@GetIt]);',
  24906. ' doit([Func]);',
  24907. '']);
  24908. ConvertProgram;
  24909. CheckSource('TestProcType_PassProcToArray',
  24910. LinesToStr([ // statements
  24911. 'this.DoIt = function (Arr) {',
  24912. '};',
  24913. 'this.GetIt = function () {',
  24914. ' var Result = 0;',
  24915. ' return Result;',
  24916. '};',
  24917. 'this.Func = null;',
  24918. '']),
  24919. LinesToStr([ // $mod.$main
  24920. '$mod.DoIt([]);',
  24921. '$mod.DoIt([$mod.GetIt]);',
  24922. '$mod.DoIt([$mod.Func]);',
  24923. '']));
  24924. end;
  24925. procedure TTestModule.TestPointer;
  24926. begin
  24927. StartProgram(false);
  24928. Add(['type',
  24929. ' TObject = class end;',
  24930. ' TClass = class of TObject;',
  24931. ' TArrInt = array of longint;',
  24932. 'const',
  24933. ' n = nil;',
  24934. 'var',
  24935. ' v: jsvalue;',
  24936. ' Obj: tobject;',
  24937. ' C: tclass;',
  24938. ' a: tarrint;',
  24939. ' p: Pointer = nil;',
  24940. ' s: string;',
  24941. 'begin',
  24942. ' p:=p;',
  24943. ' p:=nil;',
  24944. ' if p=nil then;',
  24945. ' if nil=p then;',
  24946. ' if Assigned(p) then;',
  24947. ' p:=Pointer(v);',
  24948. ' p:=obj;',
  24949. ' p:=c;',
  24950. ' p:=a;',
  24951. ' p:=tobject;',
  24952. ' obj:=TObject(p);',
  24953. ' c:=TClass(p);',
  24954. ' a:=TArrInt(p);',
  24955. ' p:=n;',
  24956. ' p:=Pointer(a);',
  24957. ' p:=pointer(s);',
  24958. ' s:=string(p);',
  24959. '']);
  24960. ConvertProgram;
  24961. CheckSource('TestPointer',
  24962. LinesToStr([ // statements
  24963. 'rtl.createClass($mod, "TObject", null, function () {',
  24964. ' this.$init = function () {',
  24965. ' };',
  24966. ' this.$final = function () {',
  24967. ' };',
  24968. '});',
  24969. 'this.n = null;',
  24970. 'this.v = undefined;',
  24971. 'this.Obj = null;',
  24972. 'this.C = null;',
  24973. 'this.a = [];',
  24974. 'this.p = null;',
  24975. 'this.s = "";',
  24976. '']),
  24977. LinesToStr([ // $mod.$main
  24978. '$mod.p = $mod.p;',
  24979. '$mod.p = null;',
  24980. 'if ($mod.p === null) ;',
  24981. 'if (null === $mod.p) ;',
  24982. 'if ($mod.p != null) ;',
  24983. '$mod.p = $mod.v;',
  24984. '$mod.p = $mod.Obj;',
  24985. '$mod.p = $mod.C;',
  24986. '$mod.p = $mod.a;',
  24987. '$mod.p = $mod.TObject;',
  24988. '$mod.Obj = $mod.p;',
  24989. '$mod.C = $mod.p;',
  24990. '$mod.a = $mod.p;',
  24991. '$mod.p = null;',
  24992. '$mod.p = $mod.a;',
  24993. '$mod.p = $mod.s;',
  24994. '$mod.s = $mod.p;',
  24995. '']));
  24996. end;
  24997. procedure TTestModule.TestPointer_Proc;
  24998. begin
  24999. StartProgram(false);
  25000. Add('type');
  25001. Add(' TObject = class');
  25002. Add(' procedure DoIt; virtual; abstract;');
  25003. Add(' end;');
  25004. Add('procedure DoSome; begin end;');
  25005. Add('var');
  25006. Add(' o: TObject;');
  25007. Add(' p: Pointer;');
  25008. Add('begin');
  25009. Add(' p:=@DoSome;');
  25010. Add(' p:[email protected];');
  25011. ConvertProgram;
  25012. CheckSource('TestPointer_Proc',
  25013. LinesToStr([ // statements
  25014. 'rtl.createClass($mod, "TObject", null, function () {',
  25015. ' this.$init = function () {',
  25016. ' };',
  25017. ' this.$final = function () {',
  25018. ' };',
  25019. '});',
  25020. 'this.DoSome = function () {',
  25021. '};',
  25022. 'this.o = null;',
  25023. 'this.p = null;',
  25024. '']),
  25025. LinesToStr([ // $mod.$main
  25026. '$mod.p = $mod.DoSome;',
  25027. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  25028. '']));
  25029. end;
  25030. procedure TTestModule.TestPointer_AssignRecordFail;
  25031. begin
  25032. StartProgram(false);
  25033. Add('type');
  25034. Add(' TRec = record end;');
  25035. Add('var');
  25036. Add(' p: Pointer;');
  25037. Add(' r: TRec;');
  25038. Add('begin');
  25039. Add(' p:=r;');
  25040. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  25041. nIncompatibleTypesGotExpected);
  25042. ConvertProgram;
  25043. end;
  25044. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  25045. begin
  25046. StartProgram(false);
  25047. Add('type');
  25048. Add(' TArr = array[boolean] of longint;');
  25049. Add('var');
  25050. Add(' p: Pointer;');
  25051. Add(' a: TArr;');
  25052. Add('begin');
  25053. Add(' p:=a;');
  25054. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  25055. nIncompatibleTypesGotExpected);
  25056. ConvertProgram;
  25057. end;
  25058. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  25059. begin
  25060. StartProgram(false);
  25061. Add([
  25062. 'procedure DoIt(args: array of jsvalue); begin end;',
  25063. 'procedure DoAll; varargs; begin end;',
  25064. 'var',
  25065. ' v: jsvalue;',
  25066. 'begin',
  25067. ' DoIt([pointer(v)]);',
  25068. ' DoAll(pointer(v));',
  25069. '']);
  25070. ConvertProgram;
  25071. CheckSource('TestPointer_TypeCastJSValueToPointer',
  25072. LinesToStr([ // statements
  25073. 'this.DoIt = function (args) {',
  25074. '};',
  25075. 'this.DoAll = function () {',
  25076. '};',
  25077. 'this.v = undefined;',
  25078. '']),
  25079. LinesToStr([ // $mod.$main
  25080. '$mod.DoIt([$mod.v]);',
  25081. '$mod.DoAll($mod.v);',
  25082. '']));
  25083. end;
  25084. procedure TTestModule.TestPointer_NonRecordFail;
  25085. begin
  25086. StartProgram(false);
  25087. Add([
  25088. 'type',
  25089. ' p = ^longint;',
  25090. 'begin',
  25091. '']);
  25092. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  25093. ConvertProgram;
  25094. end;
  25095. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  25096. begin
  25097. StartProgram(false);
  25098. Add([
  25099. 'procedure DoIt(p: ^longint); begin end;',
  25100. 'begin',
  25101. '']);
  25102. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25103. ConvertProgram;
  25104. end;
  25105. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  25106. begin
  25107. StartProgram(false);
  25108. Add([
  25109. 'var p: ^longint;',
  25110. 'begin',
  25111. '']);
  25112. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25113. ConvertProgram;
  25114. end;
  25115. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  25116. begin
  25117. StartProgram(false);
  25118. Add([
  25119. 'function DoIt: ^longint; begin end;',
  25120. 'begin',
  25121. '']);
  25122. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25123. ConvertProgram;
  25124. end;
  25125. procedure TTestModule.TestPointer_AddrOperatorFail;
  25126. begin
  25127. StartProgram(false);
  25128. Add([
  25129. 'var i: longint;',
  25130. 'begin',
  25131. ' if @i=nil then ;',
  25132. '']);
  25133. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  25134. ConvertProgram;
  25135. end;
  25136. procedure TTestModule.TestPointer_ArrayParamsFail;
  25137. begin
  25138. StartProgram(false);
  25139. Add([
  25140. 'var',
  25141. ' p: Pointer;',
  25142. 'begin',
  25143. ' p:=p[1];',
  25144. '']);
  25145. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  25146. ConvertProgram;
  25147. end;
  25148. procedure TTestModule.TestPointer_PointerAddFail;
  25149. begin
  25150. StartProgram(false);
  25151. Add([
  25152. 'var',
  25153. ' p: Pointer;',
  25154. 'begin',
  25155. ' p:=p+1;',
  25156. '']);
  25157. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  25158. ConvertProgram;
  25159. end;
  25160. procedure TTestModule.TestPointer_IncPointerFail;
  25161. begin
  25162. StartProgram(false);
  25163. Add([
  25164. 'var',
  25165. ' p: Pointer;',
  25166. 'begin',
  25167. ' inc(p,1);',
  25168. '']);
  25169. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  25170. nIncompatibleTypeArgNo);
  25171. ConvertProgram;
  25172. end;
  25173. procedure TTestModule.TestPointer_Record;
  25174. begin
  25175. StartProgram(false);
  25176. Add([
  25177. 'type',
  25178. ' TRec = record x: longint; end;',
  25179. ' PRec = ^TRec;',
  25180. 'var',
  25181. ' r: TRec;',
  25182. ' p: PRec;',
  25183. ' q: ^TRec;',
  25184. ' Ptr: pointer;',
  25185. 'begin',
  25186. ' new(p);',
  25187. ' p:=@r;',
  25188. ' r:=p^;',
  25189. ' r.x:=p^.x;',
  25190. ' p^.x:=r.x;',
  25191. ' if p^.x=3 then ;',
  25192. ' if 4=p^.x then ;',
  25193. ' dispose(p);',
  25194. ' new(q);',
  25195. ' dispose(q);',
  25196. ' Ptr:=p;',
  25197. ' p:=PRec(ptr);',
  25198. '']);
  25199. ConvertProgram;
  25200. CheckSource('TestPointer_Record',
  25201. LinesToStr([ // statements
  25202. 'rtl.recNewT($mod, "TRec", function () {',
  25203. ' this.x = 0;',
  25204. ' this.$eq = function (b) {',
  25205. ' return this.x === b.x;',
  25206. ' };',
  25207. ' this.$assign = function (s) {',
  25208. ' this.x = s.x;',
  25209. ' return this;',
  25210. ' };',
  25211. '});',
  25212. 'this.r = $mod.TRec.$new();',
  25213. 'this.p = null;',
  25214. 'this.q = null;',
  25215. 'this.Ptr = null;',
  25216. '']),
  25217. LinesToStr([ // $mod.$main
  25218. '$mod.p = $mod.TRec.$new();',
  25219. '$mod.p = $mod.r;',
  25220. '$mod.r.$assign($mod.p);',
  25221. '$mod.r.x = $mod.p.x;',
  25222. '$mod.p.x = $mod.r.x;',
  25223. 'if ($mod.p.x === 3) ;',
  25224. 'if (4 === $mod.p.x) ;',
  25225. '$mod.p = null;',
  25226. '$mod.q = $mod.TRec.$new();',
  25227. '$mod.q = null;',
  25228. '$mod.Ptr = $mod.p;',
  25229. '$mod.p = $mod.Ptr;',
  25230. '']));
  25231. end;
  25232. procedure TTestModule.TestPointer_RecordArg;
  25233. begin
  25234. StartProgram(false);
  25235. Add([
  25236. '{$modeswitch autoderef}',
  25237. 'type',
  25238. ' TRec = record x: longint; end;',
  25239. ' PRec = ^TRec;',
  25240. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  25241. 'begin',
  25242. ' a.x:=a.x;',
  25243. ' a^.x:=a^.x;',
  25244. ' with a^ do',
  25245. ' x:=x;',
  25246. 'end;',
  25247. 'function GetIt(p: PRec): PRec;',
  25248. 'begin',
  25249. ' p.x:=p.x;',
  25250. ' p^.x:=p^.x;',
  25251. ' with p^ do',
  25252. ' x:=x;',
  25253. 'end;',
  25254. 'var',
  25255. ' r: TRec;',
  25256. ' p: PRec;',
  25257. 'begin',
  25258. ' p:=GetIt(p);',
  25259. ' p^:=GetIt(@r)^;',
  25260. ' DoIt(p,p,p);',
  25261. ' DoIt(@r,p,p);',
  25262. '']);
  25263. ConvertProgram;
  25264. CheckSource('TestPointer_Record',
  25265. LinesToStr([ // statements
  25266. 'rtl.recNewT($mod, "TRec", function () {',
  25267. ' this.x = 0;',
  25268. ' this.$eq = function (b) {',
  25269. ' return this.x === b.x;',
  25270. ' };',
  25271. ' this.$assign = function (s) {',
  25272. ' this.x = s.x;',
  25273. ' return this;',
  25274. ' };',
  25275. '});',
  25276. 'this.DoIt = function (a, b, c) {',
  25277. ' var Result = $mod.TRec.$new();',
  25278. ' a.x = a.x;',
  25279. ' a.x = a.x;',
  25280. ' a.x = a.x;',
  25281. ' return Result;',
  25282. '};',
  25283. 'this.GetIt = function (p) {',
  25284. ' var Result = null;',
  25285. ' p.x = p.x;',
  25286. ' p.x = p.x;',
  25287. ' p.x = p.x;',
  25288. ' return Result;',
  25289. '};',
  25290. 'this.r = $mod.TRec.$new();',
  25291. 'this.p = null;',
  25292. '']),
  25293. LinesToStr([ // $mod.$main
  25294. '$mod.p = $mod.GetIt($mod.p);',
  25295. '$mod.p.$assign($mod.GetIt($mod.r));',
  25296. '$mod.DoIt($mod.p, {',
  25297. ' p: $mod,',
  25298. ' get: function () {',
  25299. ' return this.p.p;',
  25300. ' },',
  25301. ' set: function (v) {',
  25302. ' this.p.p = v;',
  25303. ' }',
  25304. '}, {',
  25305. ' p: $mod,',
  25306. ' get: function () {',
  25307. ' return this.p.p;',
  25308. ' },',
  25309. ' set: function (v) {',
  25310. ' this.p.p = v;',
  25311. ' }',
  25312. '});',
  25313. '$mod.DoIt($mod.r, {',
  25314. ' p: $mod,',
  25315. ' get: function () {',
  25316. ' return this.p.p;',
  25317. ' },',
  25318. ' set: function (v) {',
  25319. ' this.p.p = v;',
  25320. ' }',
  25321. '}, {',
  25322. ' p: $mod,',
  25323. ' get: function () {',
  25324. ' return this.p.p;',
  25325. ' },',
  25326. ' set: function (v) {',
  25327. ' this.p.p = v;',
  25328. ' }',
  25329. '});',
  25330. '']));
  25331. end;
  25332. procedure TTestModule.TestJSValue_AssignToJSValue;
  25333. begin
  25334. StartProgram(false);
  25335. Add('var');
  25336. Add(' v: jsvalue;');
  25337. Add(' i: longint;');
  25338. Add(' s: string;');
  25339. Add(' b: boolean;');
  25340. Add(' d: double;');
  25341. Add(' p: pointer;');
  25342. Add('begin');
  25343. Add(' v:=v;');
  25344. Add(' v:=1;');
  25345. Add(' v:=i;');
  25346. Add(' v:='''';');
  25347. Add(' v:=''c'';');
  25348. Add(' v:=''foo'';');
  25349. Add(' v:=s;');
  25350. Add(' v:=false;');
  25351. Add(' v:=true;');
  25352. Add(' v:=b;');
  25353. Add(' v:=0.1;');
  25354. Add(' v:=d;');
  25355. Add(' v:=nil;');
  25356. Add(' v:=p;');
  25357. ConvertProgram;
  25358. CheckSource('TestJSValue_AssignToJSValue',
  25359. LinesToStr([ // statements
  25360. 'this.v = undefined;',
  25361. 'this.i = 0;',
  25362. 'this.s = "";',
  25363. 'this.b = false;',
  25364. 'this.d = 0.0;',
  25365. 'this.p = null;',
  25366. '']),
  25367. LinesToStr([ // $mod.$main
  25368. '$mod.v = $mod.v;',
  25369. '$mod.v = 1;',
  25370. '$mod.v = $mod.i;',
  25371. '$mod.v = "";',
  25372. '$mod.v = "c";',
  25373. '$mod.v = "foo";',
  25374. '$mod.v = $mod.s;',
  25375. '$mod.v = false;',
  25376. '$mod.v = true;',
  25377. '$mod.v = $mod.b;',
  25378. '$mod.v = 0.1;',
  25379. '$mod.v = $mod.d;',
  25380. '$mod.v = null;',
  25381. '$mod.v = $mod.p;',
  25382. '']));
  25383. end;
  25384. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  25385. begin
  25386. StartProgram(false);
  25387. Add('type');
  25388. Add(' integer = longint;');
  25389. Add(' TYesNo = boolean;');
  25390. Add(' TFloat = double;');
  25391. Add(' TCaption = string;');
  25392. Add(' TChar = char;');
  25393. Add('var');
  25394. Add(' v: jsvalue;');
  25395. Add(' i: integer;');
  25396. Add(' s: TCaption;');
  25397. Add(' b: TYesNo;');
  25398. Add(' d: TFloat;');
  25399. Add(' c: char;');
  25400. Add('begin');
  25401. Add(' i:=longint(v);');
  25402. Add(' i:=integer(v);');
  25403. Add(' s:=string(v);');
  25404. Add(' s:=TCaption(v);');
  25405. Add(' b:=boolean(v);');
  25406. Add(' b:=TYesNo(v);');
  25407. Add(' d:=double(v);');
  25408. Add(' d:=TFloat(v);');
  25409. Add(' c:=char(v);');
  25410. Add(' c:=TChar(v);');
  25411. ConvertProgram;
  25412. CheckSource('TestJSValue_TypeCastToBaseType',
  25413. LinesToStr([ // statements
  25414. 'this.v = undefined;',
  25415. 'this.i = 0;',
  25416. 'this.s = "";',
  25417. 'this.b = false;',
  25418. 'this.d = 0.0;',
  25419. 'this.c = "";',
  25420. '']),
  25421. LinesToStr([ // $mod.$main
  25422. '$mod.i = rtl.trunc($mod.v);',
  25423. '$mod.i = rtl.trunc($mod.v);',
  25424. '$mod.s = "" + $mod.v;',
  25425. '$mod.s = "" + $mod.v;',
  25426. '$mod.b = !($mod.v == false);',
  25427. '$mod.b = !($mod.v == false);',
  25428. '$mod.d = rtl.getNumber($mod.v);',
  25429. '$mod.d = rtl.getNumber($mod.v);',
  25430. '$mod.c = rtl.getChar($mod.v);',
  25431. '$mod.c = rtl.getChar($mod.v);',
  25432. '']));
  25433. end;
  25434. procedure TTestModule.TestJSValue_TypecastToJSValue;
  25435. begin
  25436. StartProgram(false);
  25437. Add([
  25438. 'type',
  25439. ' TArr = array of word;',
  25440. ' TRec = record end;',
  25441. ' TSet = set of boolean;',
  25442. 'procedure Fly(v: jsvalue);',
  25443. 'begin',
  25444. 'end;',
  25445. 'var',
  25446. ' a: TArr;',
  25447. ' r: TRec;',
  25448. ' s: TSet;',
  25449. 'begin',
  25450. ' Fly(jsvalue(a));',
  25451. ' Fly(jsvalue(r));',
  25452. ' Fly(jsvalue(s));',
  25453. '']);
  25454. ConvertProgram;
  25455. CheckSource('TestJSValue_TypecastToJSValue',
  25456. LinesToStr([ // statements
  25457. 'rtl.recNewT($mod, "TRec", function () {',
  25458. ' this.$eq = function (b) {',
  25459. ' return true;',
  25460. ' };',
  25461. ' this.$assign = function (s) {',
  25462. ' return this;',
  25463. ' };',
  25464. '});',
  25465. 'this.Fly = function (v) {',
  25466. '};',
  25467. 'this.a = [];',
  25468. 'this.r = $mod.TRec.$new();',
  25469. 'this.s = {};',
  25470. '']),
  25471. LinesToStr([ // $mod.$main
  25472. '$mod.Fly($mod.a);',
  25473. '$mod.Fly($mod.r);',
  25474. '$mod.Fly($mod.s);',
  25475. '']));
  25476. end;
  25477. procedure TTestModule.TestJSValue_Equal;
  25478. begin
  25479. StartProgram(false);
  25480. Add('type');
  25481. Add(' integer = longint;');
  25482. Add(' TYesNo = boolean;');
  25483. Add(' TFloat = double;');
  25484. Add(' TCaption = string;');
  25485. Add(' TChar = char;');
  25486. Add(' TMulti = JSValue;');
  25487. Add('var');
  25488. Add(' v: jsvalue;');
  25489. Add(' i: integer;');
  25490. Add(' s: TCaption;');
  25491. Add(' b: TYesNo;');
  25492. Add(' d: TFloat;');
  25493. Add(' c: char;');
  25494. Add(' m: TMulti;');
  25495. Add('begin');
  25496. Add(' b:=v=v;');
  25497. Add(' b:=v<>v;');
  25498. Add(' b:=v=1;');
  25499. Add(' b:=v<>1;');
  25500. Add(' b:=2=v;');
  25501. Add(' b:=2<>v;');
  25502. Add(' b:=v=i;');
  25503. Add(' b:=i=v;');
  25504. Add(' b:=v=nil;');
  25505. Add(' b:=nil=v;');
  25506. Add(' b:=v=false;');
  25507. Add(' b:=true=v;');
  25508. Add(' b:=v=b;');
  25509. Add(' b:=b=v;');
  25510. Add(' b:=v=s;');
  25511. Add(' b:=s=v;');
  25512. Add(' b:=v=''foo'';');
  25513. Add(' b:=''''=v;');
  25514. Add(' b:=v=d;');
  25515. Add(' b:=d=v;');
  25516. Add(' b:=v=3.4;');
  25517. Add(' b:=5.6=v;');
  25518. Add(' b:=v=c;');
  25519. Add(' b:=c=v;');
  25520. Add(' b:=m=m;');
  25521. Add(' b:=v=m;');
  25522. Add(' b:=m=v;');
  25523. ConvertProgram;
  25524. CheckSource('TestJSValue_Equal',
  25525. LinesToStr([ // statements
  25526. 'this.v = undefined;',
  25527. 'this.i = 0;',
  25528. 'this.s = "";',
  25529. 'this.b = false;',
  25530. 'this.d = 0.0;',
  25531. 'this.c = "";',
  25532. 'this.m = undefined;',
  25533. '']),
  25534. LinesToStr([ // $mod.$main
  25535. '$mod.b = $mod.v == $mod.v;',
  25536. '$mod.b = $mod.v != $mod.v;',
  25537. '$mod.b = $mod.v == 1;',
  25538. '$mod.b = $mod.v != 1;',
  25539. '$mod.b = 2 == $mod.v;',
  25540. '$mod.b = 2 != $mod.v;',
  25541. '$mod.b = $mod.v == $mod.i;',
  25542. '$mod.b = $mod.i == $mod.v;',
  25543. '$mod.b = $mod.v == null;',
  25544. '$mod.b = null == $mod.v;',
  25545. '$mod.b = $mod.v == false;',
  25546. '$mod.b = true == $mod.v;',
  25547. '$mod.b = $mod.v == $mod.b;',
  25548. '$mod.b = $mod.b == $mod.v;',
  25549. '$mod.b = $mod.v == $mod.s;',
  25550. '$mod.b = $mod.s == $mod.v;',
  25551. '$mod.b = $mod.v == "foo";',
  25552. '$mod.b = "" == $mod.v;',
  25553. '$mod.b = $mod.v == $mod.d;',
  25554. '$mod.b = $mod.d == $mod.v;',
  25555. '$mod.b = $mod.v == 3.4;',
  25556. '$mod.b = 5.6 == $mod.v;',
  25557. '$mod.b = $mod.v == $mod.c;',
  25558. '$mod.b = $mod.c == $mod.v;',
  25559. '$mod.b = $mod.m == $mod.m;',
  25560. '$mod.b = $mod.v == $mod.m;',
  25561. '$mod.b = $mod.m == $mod.v;',
  25562. '']));
  25563. end;
  25564. procedure TTestModule.TestJSValue_If;
  25565. begin
  25566. StartProgram(false);
  25567. Add([
  25568. 'var',
  25569. ' v: jsvalue;',
  25570. 'begin',
  25571. ' if v then ;',
  25572. ' while v do ;',
  25573. ' repeat until v;',
  25574. '']);
  25575. ConvertProgram;
  25576. CheckSource('TestJSValue_If',
  25577. LinesToStr([ // statements
  25578. 'this.v = undefined;',
  25579. '']),
  25580. LinesToStr([ // $mod.$main
  25581. 'if ($mod.v) ;',
  25582. 'while($mod.v){',
  25583. '};',
  25584. 'do{',
  25585. '} while(!$mod.v);',
  25586. '']));
  25587. end;
  25588. procedure TTestModule.TestJSValue_Not;
  25589. begin
  25590. StartProgram(false);
  25591. Add([
  25592. 'var',
  25593. ' v: jsvalue;',
  25594. ' b: boolean;',
  25595. 'begin',
  25596. ' b:=not v;',
  25597. ' if not v then ;',
  25598. ' while not v do ;',
  25599. ' repeat until not v;',
  25600. '']);
  25601. ConvertProgram;
  25602. CheckSource('TestJSValue_If',
  25603. LinesToStr([ // statements
  25604. 'this.v = undefined;',
  25605. 'this.b = false;',
  25606. '']),
  25607. LinesToStr([ // $mod.$main
  25608. '$mod.b=!$mod.v;',
  25609. 'if (!$mod.v) ;',
  25610. 'while(!$mod.v){',
  25611. '};',
  25612. 'do{',
  25613. '} while($mod.v);',
  25614. '']));
  25615. end;
  25616. procedure TTestModule.TestJSValue_Enum;
  25617. begin
  25618. StartProgram(false);
  25619. Add('type');
  25620. Add(' TColor = (red, blue);');
  25621. Add(' TRedBlue = TColor;');
  25622. Add('var');
  25623. Add(' v: jsvalue;');
  25624. Add(' e: TColor;');
  25625. Add('begin');
  25626. Add(' v:=e;');
  25627. Add(' v:=TColor(e);');
  25628. Add(' v:=TRedBlue(e);');
  25629. Add(' e:=TColor(v);');
  25630. Add(' e:=TRedBlue(v);');
  25631. ConvertProgram;
  25632. CheckSource('TestJSValue_Enum',
  25633. LinesToStr([ // statements
  25634. 'this.TColor = {',
  25635. ' "0": "red",',
  25636. ' red: 0,',
  25637. ' "1": "blue",',
  25638. ' blue: 1',
  25639. '};',
  25640. 'this.v = undefined;',
  25641. 'this.e = 0;',
  25642. '']),
  25643. LinesToStr([ // $mod.$main
  25644. '$mod.v = $mod.e;',
  25645. '$mod.v = $mod.e;',
  25646. '$mod.v = $mod.e;',
  25647. '$mod.e = $mod.v;',
  25648. '$mod.e = $mod.v;',
  25649. '']));
  25650. end;
  25651. procedure TTestModule.TestJSValue_ClassInstance;
  25652. begin
  25653. StartProgram(false);
  25654. Add([
  25655. 'type',
  25656. ' TObject = class',
  25657. ' end;',
  25658. ' TBirdObject = TObject;',
  25659. 'var',
  25660. ' v: jsvalue;',
  25661. ' o: TObject;',
  25662. 'begin',
  25663. ' v:=o;',
  25664. ' v:=TObject(o);',
  25665. ' v:=TBirdObject(o);',
  25666. ' o:=TObject(v);',
  25667. ' o:=TBirdObject(v);',
  25668. ' if v is TObject then ;',
  25669. '']);
  25670. ConvertProgram;
  25671. CheckSource('TestJSValue_ClassInstance',
  25672. LinesToStr([ // statements
  25673. 'rtl.createClass($mod, "TObject", null, function () {',
  25674. ' this.$init = function () {',
  25675. ' };',
  25676. ' this.$final = function () {',
  25677. ' };',
  25678. '});',
  25679. 'this.v = undefined;',
  25680. 'this.o = null;',
  25681. '']),
  25682. LinesToStr([ // $mod.$main
  25683. '$mod.v = $mod.o;',
  25684. '$mod.v = $mod.o;',
  25685. '$mod.v = $mod.o;',
  25686. '$mod.o = rtl.getObject($mod.v);',
  25687. '$mod.o = rtl.getObject($mod.v);',
  25688. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  25689. '']));
  25690. end;
  25691. procedure TTestModule.TestJSValue_ClassOf;
  25692. begin
  25693. StartProgram(false);
  25694. Add([
  25695. 'type',
  25696. ' TClass = class of TObject;',
  25697. ' TObject = class',
  25698. ' end;',
  25699. ' TBirds = class of TBird;',
  25700. ' TBird = class(TObject) end;',
  25701. 'var',
  25702. ' v: jsvalue;',
  25703. ' c: TClass;',
  25704. 'begin',
  25705. ' v:=c;',
  25706. ' v:=TObject;',
  25707. ' v:=TClass(c);',
  25708. ' v:=TBirds(c);',
  25709. ' c:=TClass(v);',
  25710. ' c:=TBirds(v);',
  25711. ' if v is TClass then ;',
  25712. '']);
  25713. ConvertProgram;
  25714. CheckSource('TestJSValue_ClassOf',
  25715. LinesToStr([ // statements
  25716. 'rtl.createClass($mod, "TObject", null, function () {',
  25717. ' this.$init = function () {',
  25718. ' };',
  25719. ' this.$final = function () {',
  25720. ' };',
  25721. '});',
  25722. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  25723. '});',
  25724. 'this.v = undefined;',
  25725. 'this.c = null;',
  25726. '']),
  25727. LinesToStr([ // $mod.$main
  25728. '$mod.v = $mod.c;',
  25729. '$mod.v = $mod.TObject;',
  25730. '$mod.v = $mod.c;',
  25731. '$mod.v = $mod.c;',
  25732. '$mod.c = rtl.getObject($mod.v);',
  25733. '$mod.c = rtl.getObject($mod.v);',
  25734. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  25735. '']));
  25736. end;
  25737. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  25738. begin
  25739. StartProgram(false);
  25740. Add([
  25741. 'type',
  25742. ' integer = longint;',
  25743. ' TArray = array of JSValue;',
  25744. ' TArrgh = tarray;',
  25745. ' TArrInt = array of integer;',
  25746. 'var',
  25747. ' v: jsvalue;',
  25748. ' TheArray: tarray = (1,''2'');',
  25749. ' Arr: tarrgh;',
  25750. ' i: integer;',
  25751. ' ArrInt: tarrint;',
  25752. 'begin',
  25753. ' arr:=thearray;',
  25754. ' thearray:=arr;',
  25755. ' setlength(arr,2);',
  25756. ' setlength(thearray,3);',
  25757. ' arr[4]:=v;',
  25758. ' arr[5]:=length(thearray);',
  25759. ' arr[6]:=nil;',
  25760. ' arr[7]:=thearray[8];',
  25761. ' arr[low(arr)]:=high(thearray);',
  25762. ' arr:=arrint;',
  25763. ' arrInt:=tarrint(arr);',
  25764. ' if TheArray = nil then ;',
  25765. ' if nil = TheArray then ;',
  25766. ' if TheArray <> nil then ;',
  25767. ' if nil <> TheArray then ;',
  25768. '']);
  25769. ConvertProgram;
  25770. CheckSource('TestJSValue_ArrayOfJSValue',
  25771. LinesToStr([ // statements
  25772. 'this.v = undefined;',
  25773. 'this.TheArray = [1, "2"];',
  25774. 'this.Arr = [];',
  25775. 'this.i = 0;',
  25776. 'this.ArrInt = [];',
  25777. '']),
  25778. LinesToStr([ // $mod.$main
  25779. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  25780. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  25781. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  25782. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  25783. '$mod.Arr[4] = $mod.v;',
  25784. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  25785. '$mod.Arr[6] = null;',
  25786. '$mod.Arr[7] = $mod.TheArray[8];',
  25787. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  25788. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  25789. '$mod.ArrInt = $mod.Arr;',
  25790. 'if (rtl.length($mod.TheArray) === 0) ;',
  25791. 'if (rtl.length($mod.TheArray) === 0) ;',
  25792. 'if (rtl.length($mod.TheArray) > 0) ;',
  25793. 'if (rtl.length($mod.TheArray) > 0) ;',
  25794. '']));
  25795. end;
  25796. procedure TTestModule.TestJSValue_ArrayLit;
  25797. begin
  25798. StartProgram(false);
  25799. Add([
  25800. 'type',
  25801. ' TFlag = (big,small);',
  25802. ' TArray = array of JSValue;',
  25803. ' TObject = class end;',
  25804. ' TClass = class of TObject;',
  25805. 'var',
  25806. ' v: jsvalue;',
  25807. ' a: TArray;',
  25808. ' o: TObject;',
  25809. 'begin',
  25810. ' a:=[];',
  25811. ' a:=[1];',
  25812. ' a:=[1,2];',
  25813. ' a:=[big];',
  25814. ' a:=[1,big];',
  25815. ' a:=[o,nil];',
  25816. '']);
  25817. ConvertProgram;
  25818. CheckSource('TestJSValue_ArrayLit',
  25819. LinesToStr([ // statements
  25820. 'this.TFlag = {',
  25821. ' "0": "big",',
  25822. ' big: 0,',
  25823. ' "1": "small",',
  25824. ' small: 1',
  25825. '};',
  25826. 'rtl.createClass($mod, "TObject", null, function () {',
  25827. ' this.$init = function () {',
  25828. ' };',
  25829. ' this.$final = function () {',
  25830. ' };',
  25831. '});',
  25832. 'this.v = undefined;',
  25833. 'this.a = [];',
  25834. 'this.o = null;',
  25835. '']),
  25836. LinesToStr([ // $mod.$main
  25837. '$mod.a = [];',
  25838. '$mod.a = [1];',
  25839. '$mod.a = [1, 2];',
  25840. '$mod.a = [$mod.TFlag.big];',
  25841. '$mod.a = [1, $mod.TFlag.big];',
  25842. '$mod.a = [$mod.o, null];',
  25843. '']));
  25844. end;
  25845. procedure TTestModule.TestJSValue_Params;
  25846. begin
  25847. StartProgram(false);
  25848. Add('type');
  25849. Add(' integer = longint;');
  25850. Add(' TYesNo = boolean;');
  25851. Add(' TFloat = double;');
  25852. Add(' TCaption = string;');
  25853. Add(' TChar = char;');
  25854. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  25855. Add('var');
  25856. Add(' l: jsvalue;');
  25857. Add('begin');
  25858. Add(' a:=a;');
  25859. Add(' l:=b;');
  25860. Add(' c:=c;');
  25861. Add(' d:=d;');
  25862. Add(' Result:=l;');
  25863. Add('end;');
  25864. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  25865. Add('var');
  25866. Add(' v: jsvalue;');
  25867. Add(' i: integer;');
  25868. Add(' b: TYesNo;');
  25869. Add(' d: TFloat;');
  25870. Add(' s: TCaption;');
  25871. Add(' c: TChar;');
  25872. Add('begin');
  25873. Add(' v:=doit(v,v,v,v);');
  25874. Add(' i:=integer(dosome(i,i));');
  25875. Add(' b:=TYesNo(dosome(b,b));');
  25876. Add(' d:=TFloat(dosome(d,d));');
  25877. Add(' s:=TCaption(dosome(s,s));');
  25878. Add(' c:=TChar(dosome(c,c));');
  25879. ConvertProgram;
  25880. CheckSource('TestJSValue_Params',
  25881. LinesToStr([ // statements
  25882. 'this.DoIt = function (a, b, c, d) {',
  25883. ' var Result = undefined;',
  25884. ' var l = undefined;',
  25885. ' a = a;',
  25886. ' l = b;',
  25887. ' c.set(c.get());',
  25888. ' d.set(d.get());',
  25889. ' Result = l;',
  25890. ' return Result;',
  25891. '};',
  25892. 'this.DoSome = function (a, b) {',
  25893. ' var Result = undefined;',
  25894. ' return Result;',
  25895. '};',
  25896. 'this.v = undefined;',
  25897. 'this.i = 0;',
  25898. 'this.b = false;',
  25899. 'this.d = 0.0;',
  25900. 'this.s = "";',
  25901. 'this.c = "";',
  25902. '']),
  25903. LinesToStr([ // $mod.$main
  25904. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  25905. ' p: $mod,',
  25906. ' get: function () {',
  25907. ' return this.p.v;',
  25908. ' },',
  25909. ' set: function (v) {',
  25910. ' this.p.v = v;',
  25911. ' }',
  25912. '}, {',
  25913. ' p: $mod,',
  25914. ' get: function () {',
  25915. ' return this.p.v;',
  25916. ' },',
  25917. ' set: function (v) {',
  25918. ' this.p.v = v;',
  25919. ' }',
  25920. '});',
  25921. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  25922. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  25923. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  25924. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  25925. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  25926. '']));
  25927. end;
  25928. procedure TTestModule.TestJSValue_UntypedParam;
  25929. begin
  25930. StartProgram(false);
  25931. Add('function DoIt(const a; var b; out c): jsvalue;');
  25932. Add('begin');
  25933. Add(' Result:=a;');
  25934. Add(' Result:=b;');
  25935. Add(' Result:=c;');
  25936. Add(' b:=Result;');
  25937. Add(' c:=Result;');
  25938. Add('end;');
  25939. Add('var i: longint;');
  25940. Add('begin');
  25941. Add(' doit(i,i,i);');
  25942. ConvertProgram;
  25943. CheckSource('TestJSValue_UntypedParam',
  25944. LinesToStr([ // statements
  25945. 'this.DoIt = function (a, b, c) {',
  25946. ' var Result = undefined;',
  25947. ' Result = a;',
  25948. ' Result = b.get();',
  25949. ' Result = c.get();',
  25950. ' b.set(Result);',
  25951. ' c.set(Result);',
  25952. ' return Result;',
  25953. '};',
  25954. 'this.i = 0;',
  25955. '']),
  25956. LinesToStr([ // $mod.$main
  25957. '$mod.DoIt($mod.i, {',
  25958. ' p: $mod,',
  25959. ' get: function () {',
  25960. ' return this.p.i;',
  25961. ' },',
  25962. ' set: function (v) {',
  25963. ' this.p.i = v;',
  25964. ' }',
  25965. '}, {',
  25966. ' p: $mod,',
  25967. ' get: function () {',
  25968. ' return this.p.i;',
  25969. ' },',
  25970. ' set: function (v) {',
  25971. ' this.p.i = v;',
  25972. ' }',
  25973. '});',
  25974. '']));
  25975. end;
  25976. procedure TTestModule.TestJSValue_FuncResultType;
  25977. begin
  25978. StartProgram(false);
  25979. Add('type');
  25980. Add(' integer = longint;');
  25981. Add(' TJSValueArray = array of JSValue;');
  25982. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  25983. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  25984. Add('begin');
  25985. Add(' while Compare(P,aList[0])>0 do ;');
  25986. Add('end;');
  25987. Add('var');
  25988. Add(' Compare: TListSortCompare;');
  25989. Add(' V: JSValue;');
  25990. Add(' i: integer;');
  25991. Add('begin');
  25992. Add(' if Compare(V,V)>0 then ;');
  25993. Add(' if Compare(i,i)>1 then ;');
  25994. Add(' if Compare(nil,false)>2 then ;');
  25995. Add(' if Compare(1,true)>3 then ;');
  25996. ConvertProgram;
  25997. CheckSource('TestJSValue_UntypedParam',
  25998. LinesToStr([ // statements
  25999. 'this.Sort = function (P, aList, Compare) {',
  26000. ' while (Compare(P, aList[0]) > 0) {',
  26001. ' };',
  26002. '};',
  26003. 'this.Compare = null;',
  26004. 'this.V = undefined;',
  26005. 'this.i = 0;',
  26006. '']),
  26007. LinesToStr([ // $mod.$main
  26008. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  26009. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  26010. 'if ($mod.Compare(null, false) > 2) ;',
  26011. 'if ($mod.Compare(1, true) > 3) ;',
  26012. '']));
  26013. end;
  26014. procedure TTestModule.TestJSValue_ProcType_Assign;
  26015. begin
  26016. StartProgram(false);
  26017. Add('type');
  26018. Add(' integer = longint;');
  26019. Add(' TObject = class');
  26020. Add(' class function GetGlob: integer;');
  26021. Add(' function Getter: integer;');
  26022. Add(' end;');
  26023. Add('class function TObject.GetGlob: integer;');
  26024. Add('var v1: jsvalue;');
  26025. Add('begin');
  26026. Add(' v1:=@GetGlob;');
  26027. Add(' v1:[email protected];');
  26028. Add('end;');
  26029. Add('function TObject.Getter: integer;');
  26030. Add('var v2: jsvalue;');
  26031. Add('begin');
  26032. Add(' v2:=@Getter;');
  26033. Add(' v2:[email protected];');
  26034. Add(' v2:=@GetGlob;');
  26035. Add(' v2:[email protected];');
  26036. Add('end;');
  26037. Add('function GetIt(i: integer): integer;');
  26038. Add('var v3: jsvalue;');
  26039. Add('begin');
  26040. Add(' v3:=@GetIt;');
  26041. Add('end;');
  26042. Add('var');
  26043. Add(' V: JSValue;');
  26044. Add(' o: TObject;');
  26045. Add('begin');
  26046. Add(' v:=@GetIt;');
  26047. Add(' v:[email protected];');
  26048. Add(' v:[email protected];');
  26049. ConvertProgram;
  26050. CheckSource('TestJSValue_ProcType_Assign',
  26051. LinesToStr([ // statements
  26052. 'rtl.createClass($mod, "TObject", null, function () {',
  26053. ' this.$init = function () {',
  26054. ' };',
  26055. ' this.$final = function () {',
  26056. ' };',
  26057. ' this.GetGlob = function () {',
  26058. ' var Result = 0;',
  26059. ' var v1 = undefined;',
  26060. ' v1 = rtl.createCallback(this, "GetGlob");',
  26061. ' v1 = rtl.createCallback(this, "GetGlob");',
  26062. ' return Result;',
  26063. ' };',
  26064. ' this.Getter = function () {',
  26065. ' var Result = 0;',
  26066. ' var v2 = undefined;',
  26067. ' v2 = rtl.createCallback(this, "Getter");',
  26068. ' v2 = rtl.createCallback(this, "Getter");',
  26069. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  26070. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  26071. ' return Result;',
  26072. ' };',
  26073. '});',
  26074. 'this.GetIt = function (i) {',
  26075. ' var Result = 0;',
  26076. ' var v3 = undefined;',
  26077. ' v3 = $mod.GetIt;',
  26078. ' return Result;',
  26079. '};',
  26080. 'this.V = undefined;',
  26081. 'this.o = null;',
  26082. '']),
  26083. LinesToStr([ // $mod.$main
  26084. '$mod.V = $mod.GetIt;',
  26085. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  26086. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  26087. '']));
  26088. end;
  26089. procedure TTestModule.TestJSValue_ProcType_Equal;
  26090. begin
  26091. StartProgram(false);
  26092. Add('type');
  26093. Add(' integer = longint;');
  26094. Add(' TObject = class');
  26095. Add(' class function GetGlob: integer;');
  26096. Add(' function Getter: integer;');
  26097. Add(' end;');
  26098. Add('class function TObject.GetGlob: integer;');
  26099. Add('var v1: jsvalue;');
  26100. Add('begin');
  26101. Add(' if v1=@GetGlob then;');
  26102. Add(' if [email protected] then ;');
  26103. Add('end;');
  26104. Add('function TObject.Getter: integer;');
  26105. Add('var v2: jsvalue;');
  26106. Add('begin');
  26107. Add(' if v2=@Getter then;');
  26108. Add(' if [email protected] then ;');
  26109. Add(' if v2=@GetGlob then;');
  26110. Add(' if [email protected] then;');
  26111. Add('end;');
  26112. Add('function GetIt(i: integer): integer;');
  26113. Add('var v3: jsvalue;');
  26114. Add('begin');
  26115. Add(' if v3=@GetIt then;');
  26116. Add('end;');
  26117. Add('var');
  26118. Add(' V: JSValue;');
  26119. Add(' o: TObject;');
  26120. Add('begin');
  26121. Add(' if v=@GetIt then;');
  26122. Add(' if [email protected] then;');
  26123. Add(' if [email protected] then;');
  26124. Add(' if @GetIt=v then;');
  26125. Add(' if @o.Getter=v then;');
  26126. Add(' if @o.GetGlob=v then;');
  26127. ConvertProgram;
  26128. CheckSource('TestJSValue_ProcType_Equal',
  26129. LinesToStr([ // statements
  26130. 'rtl.createClass($mod, "TObject", null, function () {',
  26131. ' this.$init = function () {',
  26132. ' };',
  26133. ' this.$final = function () {',
  26134. ' };',
  26135. ' this.GetGlob = function () {',
  26136. ' var Result = 0;',
  26137. ' var v1 = undefined;',
  26138. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  26139. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  26140. ' return Result;',
  26141. ' };',
  26142. ' this.Getter = function () {',
  26143. ' var Result = 0;',
  26144. ' var v2 = undefined;',
  26145. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  26146. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  26147. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  26148. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  26149. ' return Result;',
  26150. ' };',
  26151. '});',
  26152. 'this.GetIt = function (i) {',
  26153. ' var Result = 0;',
  26154. ' var v3 = undefined;',
  26155. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  26156. ' return Result;',
  26157. '};',
  26158. 'this.V = undefined;',
  26159. 'this.o = null;',
  26160. '']),
  26161. LinesToStr([ // $mod.$main
  26162. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  26163. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  26164. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  26165. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  26166. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  26167. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  26168. '']));
  26169. end;
  26170. procedure TTestModule.TestJSValue_ProcType_Param;
  26171. begin
  26172. StartProgram(false);
  26173. Add([
  26174. 'type',
  26175. ' variant = jsvalue;',
  26176. ' TArrVariant = array of variant;',
  26177. ' TArrVar2 = TArrVariant;',
  26178. ' TFuncInt = function: longint;',
  26179. 'function GetIt: longint;',
  26180. 'begin',
  26181. 'end;',
  26182. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  26183. 'var v: variant;',
  26184. 'begin',
  26185. ' v:=arr[1];',
  26186. 'end;',
  26187. 'var s: string;',
  26188. 'begin',
  26189. ' DoIt(GetIt,[]);',
  26190. ' DoIt(@GetIt,[]);',
  26191. ' DoIt(1,[s,GetIt]);',
  26192. ' DoIt(1,[s,@GetIt]);',
  26193. '']);
  26194. ConvertProgram;
  26195. CheckSource('TestJSValue_ProcType_Param',
  26196. LinesToStr([ // statements
  26197. 'this.GetIt = function () {',
  26198. ' var Result = 0;',
  26199. ' return Result;',
  26200. '};',
  26201. 'this.DoIt = function (p, Arr) {',
  26202. ' var v = undefined;',
  26203. ' v = Arr[1];',
  26204. '};',
  26205. 'this.s = "";',
  26206. '']),
  26207. LinesToStr([ // $mod.$main
  26208. '$mod.DoIt($mod.GetIt(), []);',
  26209. '$mod.DoIt($mod.GetIt, []);',
  26210. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  26211. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  26212. '']));
  26213. end;
  26214. procedure TTestModule.TestJSValue_AssignToPointerFail;
  26215. begin
  26216. StartProgram(false);
  26217. Add([
  26218. 'var',
  26219. ' v: JSValue;',
  26220. ' p: Pointer;',
  26221. 'begin',
  26222. ' p:=v;',
  26223. '']);
  26224. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  26225. nIncompatibleTypesGotExpected);
  26226. ConvertProgram;
  26227. end;
  26228. procedure TTestModule.TestJSValue_OverloadDouble;
  26229. begin
  26230. StartProgram(false);
  26231. Add([
  26232. 'type',
  26233. ' integer = longint;',
  26234. ' tdatetime = double;',
  26235. 'procedure DoIt(d: double); begin end;',
  26236. 'procedure DoIt(v: jsvalue); begin end;',
  26237. 'var',
  26238. ' d: double;',
  26239. ' dt: tdatetime;',
  26240. ' i: integer;',
  26241. ' b: byte;',
  26242. ' shi: shortint;',
  26243. ' w: word;',
  26244. ' smi: smallint;',
  26245. ' lw: longword;',
  26246. ' li: longint;',
  26247. ' ni: nativeint;',
  26248. ' nu: nativeuint;',
  26249. 'begin',
  26250. ' DoIt(d);',
  26251. ' DoIt(dt);',
  26252. ' DoIt(i);',
  26253. ' DoIt(b);',
  26254. ' DoIt(shi);',
  26255. ' DoIt(w);',
  26256. ' DoIt(smi);',
  26257. ' DoIt(lw);',
  26258. ' DoIt(li);',
  26259. ' DoIt(ni);',
  26260. ' DoIt(nu);',
  26261. '']);
  26262. ConvertProgram;
  26263. CheckSource('TestJSValue_OverloadDouble',
  26264. LinesToStr([ // statements
  26265. 'this.DoIt = function (d) {',
  26266. '};',
  26267. 'this.DoIt$1 = function (v) {',
  26268. '};',
  26269. 'this.d = 0.0;',
  26270. 'this.dt = 0.0;',
  26271. 'this.i = 0;',
  26272. 'this.b = 0;',
  26273. 'this.shi = 0;',
  26274. 'this.w = 0;',
  26275. 'this.smi = 0;',
  26276. 'this.lw = 0;',
  26277. 'this.li = 0;',
  26278. 'this.ni = 0;',
  26279. 'this.nu = 0;',
  26280. '']),
  26281. LinesToStr([ // $mod.$main
  26282. '$mod.DoIt($mod.d);',
  26283. '$mod.DoIt($mod.dt);',
  26284. '$mod.DoIt$1($mod.i);',
  26285. '$mod.DoIt$1($mod.b);',
  26286. '$mod.DoIt$1($mod.shi);',
  26287. '$mod.DoIt$1($mod.w);',
  26288. '$mod.DoIt$1($mod.smi);',
  26289. '$mod.DoIt$1($mod.lw);',
  26290. '$mod.DoIt$1($mod.li);',
  26291. '$mod.DoIt$1($mod.ni);',
  26292. '$mod.DoIt$1($mod.nu);',
  26293. '']));
  26294. end;
  26295. procedure TTestModule.TestJSValue_OverloadNativeInt;
  26296. begin
  26297. StartProgram(false);
  26298. Add([
  26299. 'type',
  26300. ' integer = longint;',
  26301. ' int53 = nativeint;',
  26302. ' tdatetime = double;',
  26303. 'procedure DoIt(n: nativeint); begin end;',
  26304. 'procedure DoIt(v: jsvalue); begin end;',
  26305. 'var',
  26306. ' d: double;',
  26307. ' dt: tdatetime;',
  26308. ' i: integer;',
  26309. ' b: byte;',
  26310. ' shi: shortint;',
  26311. ' w: word;',
  26312. ' smi: smallint;',
  26313. ' lw: longword;',
  26314. ' li: longint;',
  26315. ' ni: nativeint;',
  26316. ' nu: nativeuint;',
  26317. 'begin',
  26318. ' DoIt(d);',
  26319. ' DoIt(dt);',
  26320. ' DoIt(i);',
  26321. ' DoIt(b);',
  26322. ' DoIt(shi);',
  26323. ' DoIt(w);',
  26324. ' DoIt(smi);',
  26325. ' DoIt(lw);',
  26326. ' DoIt(li);',
  26327. ' DoIt(ni);',
  26328. ' DoIt(nu);',
  26329. '']);
  26330. ConvertProgram;
  26331. CheckSource('TestJSValue_OverloadNativeInt',
  26332. LinesToStr([ // statements
  26333. 'this.DoIt = function (n) {',
  26334. '};',
  26335. 'this.DoIt$1 = function (v) {',
  26336. '};',
  26337. 'this.d = 0.0;',
  26338. 'this.dt = 0.0;',
  26339. 'this.i = 0;',
  26340. 'this.b = 0;',
  26341. 'this.shi = 0;',
  26342. 'this.w = 0;',
  26343. 'this.smi = 0;',
  26344. 'this.lw = 0;',
  26345. 'this.li = 0;',
  26346. 'this.ni = 0;',
  26347. 'this.nu = 0;',
  26348. '']),
  26349. LinesToStr([ // $mod.$main
  26350. '$mod.DoIt$1($mod.d);',
  26351. '$mod.DoIt$1($mod.dt);',
  26352. '$mod.DoIt($mod.i);',
  26353. '$mod.DoIt($mod.b);',
  26354. '$mod.DoIt($mod.shi);',
  26355. '$mod.DoIt($mod.w);',
  26356. '$mod.DoIt($mod.smi);',
  26357. '$mod.DoIt($mod.lw);',
  26358. '$mod.DoIt($mod.li);',
  26359. '$mod.DoIt($mod.ni);',
  26360. '$mod.DoIt($mod.nu);',
  26361. '']));
  26362. end;
  26363. procedure TTestModule.TestJSValue_OverloadWord;
  26364. begin
  26365. StartProgram(false);
  26366. Add([
  26367. 'type',
  26368. ' integer = longint;',
  26369. ' int53 = nativeint;',
  26370. ' tdatetime = double;',
  26371. 'procedure DoIt(w: word); begin end;',
  26372. 'procedure DoIt(v: jsvalue); begin end;',
  26373. 'var',
  26374. ' d: double;',
  26375. ' dt: tdatetime;',
  26376. ' i: integer;',
  26377. ' b: byte;',
  26378. ' shi: shortint;',
  26379. ' w: word;',
  26380. ' smi: smallint;',
  26381. ' lw: longword;',
  26382. ' li: longint;',
  26383. ' ni: nativeint;',
  26384. ' nu: nativeuint;',
  26385. 'begin',
  26386. ' DoIt(d);',
  26387. ' DoIt(dt);',
  26388. ' DoIt(i);',
  26389. ' DoIt(b);',
  26390. ' DoIt(shi);',
  26391. ' DoIt(w);',
  26392. ' DoIt(smi);',
  26393. ' DoIt(lw);',
  26394. ' DoIt(li);',
  26395. ' DoIt(ni);',
  26396. ' DoIt(nu);',
  26397. '']);
  26398. ConvertProgram;
  26399. CheckSource('TestJSValue_OverloadWord',
  26400. LinesToStr([ // statements
  26401. 'this.DoIt = function (w) {',
  26402. '};',
  26403. 'this.DoIt$1 = function (v) {',
  26404. '};',
  26405. 'this.d = 0.0;',
  26406. 'this.dt = 0.0;',
  26407. 'this.i = 0;',
  26408. 'this.b = 0;',
  26409. 'this.shi = 0;',
  26410. 'this.w = 0;',
  26411. 'this.smi = 0;',
  26412. 'this.lw = 0;',
  26413. 'this.li = 0;',
  26414. 'this.ni = 0;',
  26415. 'this.nu = 0;',
  26416. '']),
  26417. LinesToStr([ // $mod.$main
  26418. '$mod.DoIt$1($mod.d);',
  26419. '$mod.DoIt$1($mod.dt);',
  26420. '$mod.DoIt$1($mod.i);',
  26421. '$mod.DoIt($mod.b);',
  26422. '$mod.DoIt($mod.shi);',
  26423. '$mod.DoIt($mod.w);',
  26424. '$mod.DoIt$1($mod.smi);',
  26425. '$mod.DoIt$1($mod.lw);',
  26426. '$mod.DoIt$1($mod.li);',
  26427. '$mod.DoIt$1($mod.ni);',
  26428. '$mod.DoIt$1($mod.nu);',
  26429. '']));
  26430. end;
  26431. procedure TTestModule.TestJSValue_OverloadString;
  26432. begin
  26433. StartProgram(false);
  26434. Add([
  26435. 'type',
  26436. ' uni = string;',
  26437. ' WChar = char;',
  26438. 'procedure DoIt(s: string); begin end;',
  26439. 'procedure DoIt(v: jsvalue); begin end;',
  26440. 'var',
  26441. ' s: string;',
  26442. ' c: char;',
  26443. ' u: uni;',
  26444. 'begin',
  26445. ' DoIt(s);',
  26446. ' DoIt(c);',
  26447. ' DoIt(u);',
  26448. '']);
  26449. ConvertProgram;
  26450. CheckSource('TestJSValue_OverloadString',
  26451. LinesToStr([ // statements
  26452. 'this.DoIt = function (s) {',
  26453. '};',
  26454. 'this.DoIt$1 = function (v) {',
  26455. '};',
  26456. 'this.s = "";',
  26457. 'this.c = "";',
  26458. 'this.u = "";',
  26459. '']),
  26460. LinesToStr([ // $mod.$main
  26461. '$mod.DoIt($mod.s);',
  26462. '$mod.DoIt($mod.c);',
  26463. '$mod.DoIt($mod.u);',
  26464. '']));
  26465. end;
  26466. procedure TTestModule.TestJSValue_OverloadChar;
  26467. begin
  26468. StartProgram(false);
  26469. Add([
  26470. 'type',
  26471. ' uni = string;',
  26472. ' WChar = char;',
  26473. 'procedure DoIt(c: char); begin end;',
  26474. 'procedure DoIt(v: jsvalue); begin end;',
  26475. 'var',
  26476. ' s: string;',
  26477. ' c: char;',
  26478. ' u: uni;',
  26479. 'begin',
  26480. ' DoIt(s);',
  26481. ' DoIt(c);',
  26482. ' DoIt(u);',
  26483. '']);
  26484. ConvertProgram;
  26485. CheckSource('TestJSValue_OverloadChar',
  26486. LinesToStr([ // statements
  26487. 'this.DoIt = function (c) {',
  26488. '};',
  26489. 'this.DoIt$1 = function (v) {',
  26490. '};',
  26491. 'this.s = "";',
  26492. 'this.c = "";',
  26493. 'this.u = "";',
  26494. '']),
  26495. LinesToStr([ // $mod.$main
  26496. '$mod.DoIt$1($mod.s);',
  26497. '$mod.DoIt($mod.c);',
  26498. '$mod.DoIt$1($mod.u);',
  26499. '']));
  26500. end;
  26501. procedure TTestModule.TestJSValue_OverloadPointer;
  26502. begin
  26503. StartProgram(false);
  26504. Add([
  26505. 'type',
  26506. ' TObject = class end;',
  26507. 'procedure DoIt(p: pointer); begin end;',
  26508. 'procedure DoIt(v: jsvalue); begin end;',
  26509. 'var',
  26510. ' o: TObject;',
  26511. 'begin',
  26512. ' DoIt(o);',
  26513. '']);
  26514. ConvertProgram;
  26515. CheckSource('TestJSValue_OverloadPointer',
  26516. LinesToStr([ // statements
  26517. 'rtl.createClass($mod, "TObject", null, function () {',
  26518. ' this.$init = function () {',
  26519. ' };',
  26520. ' this.$final = function () {',
  26521. ' };',
  26522. '});',
  26523. 'this.DoIt = function (p) {',
  26524. '};',
  26525. 'this.DoIt$1 = function (v) {',
  26526. '};',
  26527. 'this.o = null;',
  26528. '']),
  26529. LinesToStr([ // $mod.$main
  26530. '$mod.DoIt($mod.o);',
  26531. '']));
  26532. end;
  26533. procedure TTestModule.TestJSValue_ForIn;
  26534. begin
  26535. StartProgram(false);
  26536. Add([
  26537. 'var',
  26538. ' v: JSValue;',
  26539. ' key: string;',
  26540. 'begin',
  26541. ' for key in v do begin',
  26542. ' if key=''abc'' then ;',
  26543. ' end;',
  26544. '']);
  26545. ConvertProgram;
  26546. CheckSource('TestJSValue_ForIn',
  26547. LinesToStr([ // statements
  26548. 'this.v = undefined;',
  26549. 'this.key = "";',
  26550. '']),
  26551. LinesToStr([ // $mod.$main
  26552. 'for ($mod.key in $mod.v) {',
  26553. ' if ($mod.key === "abc") ;',
  26554. '};',
  26555. '']));
  26556. end;
  26557. procedure TTestModule.TestRTTI_IntRange;
  26558. begin
  26559. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26560. StartProgram(false);
  26561. Add([
  26562. '{$modeswitch externalclass}',
  26563. 'type',
  26564. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  26565. ' end;',
  26566. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  26567. ' end;',
  26568. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  26569. ' TColor = type TGraphicsColor;',
  26570. 'var',
  26571. ' p: TTypeInfo;',
  26572. 'begin',
  26573. ' p:=typeinfo(TGraphicsColor);',
  26574. ' p:=typeinfo(TColor);',
  26575. '']);
  26576. ConvertProgram;
  26577. CheckSource('TestRTTI_IntRange',
  26578. LinesToStr([ // statements
  26579. '$mod.$rtti.$Int("TGraphicsColor", {',
  26580. ' minvalue: -2147483648,',
  26581. ' maxvalue: 2147483647,',
  26582. ' ordtype: 4',
  26583. '});',
  26584. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  26585. 'this.p = null;',
  26586. '']),
  26587. LinesToStr([ // $mod.$main
  26588. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  26589. '$mod.p = $mod.$rtti["TColor"];',
  26590. '']));
  26591. end;
  26592. procedure TTestModule.TestRTTI_Double;
  26593. begin
  26594. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26595. StartProgram(false);
  26596. Add([
  26597. '{$modeswitch externalclass}',
  26598. 'type',
  26599. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  26600. ' end;',
  26601. ' TFloat = type double;',
  26602. 'var',
  26603. ' p: TTypeInfo;',
  26604. 'begin',
  26605. ' p:=typeinfo(double);',
  26606. ' p:=typeinfo(TFloat);',
  26607. '']);
  26608. ConvertProgram;
  26609. CheckSource('TestRTTI_Double',
  26610. LinesToStr([ // statements
  26611. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  26612. 'this.p = null;',
  26613. '']),
  26614. LinesToStr([ // $mod.$main
  26615. '$mod.p = rtl.double;',
  26616. '$mod.p = $mod.$rtti["TFloat"];',
  26617. '']));
  26618. end;
  26619. procedure TTestModule.TestRTTI_ProcType;
  26620. begin
  26621. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26622. StartProgram(false);
  26623. Add('type');
  26624. Add(' TProcA = procedure;');
  26625. Add(' TMethodB = procedure of object;');
  26626. Add(' TProcC = procedure; varargs;');
  26627. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  26628. Add(' TProcE = function: nativeint;');
  26629. Add(' TProcF = function(const p: TProcA): nativeuint;');
  26630. Add('var p: pointer;');
  26631. Add('begin');
  26632. Add(' p:=typeinfo(tproca);');
  26633. ConvertProgram;
  26634. CheckSource('TestRTTI_ProcType',
  26635. LinesToStr([ // statements
  26636. '$mod.$rtti.$ProcVar("TProcA", {',
  26637. ' procsig: rtl.newTIProcSig(null)',
  26638. '});',
  26639. '$mod.$rtti.$MethodVar("TMethodB", {',
  26640. ' procsig: rtl.newTIProcSig(null),',
  26641. ' methodkind: 0',
  26642. '});',
  26643. '$mod.$rtti.$ProcVar("TProcC", {',
  26644. ' procsig: rtl.newTIProcSig(null, 2)',
  26645. '});',
  26646. '$mod.$rtti.$ProcVar("TProcD", {',
  26647. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  26648. '});',
  26649. '$mod.$rtti.$ProcVar("TProcE", {',
  26650. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  26651. '});',
  26652. '$mod.$rtti.$ProcVar("TProcF", {',
  26653. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  26654. '});',
  26655. 'this.p = null;',
  26656. '']),
  26657. LinesToStr([ // $mod.$main
  26658. '$mod.p = $mod.$rtti["TProcA"];',
  26659. '']));
  26660. end;
  26661. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  26662. begin
  26663. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26664. AddModuleWithIntfImplSrc('unit2.pas',
  26665. LinesToStr([
  26666. 'type',
  26667. ' TObject = class end;'
  26668. ]),
  26669. '');
  26670. StartUnit(true);
  26671. Add('interface');
  26672. Add('uses unit2;');
  26673. Add('type');
  26674. Add(' TProcA = function(o: tobject): tobject;');
  26675. Add('implementation');
  26676. Add('type');
  26677. Add(' TProcB = function(o: tobject): tobject;');
  26678. Add('var p: Pointer;');
  26679. Add('initialization');
  26680. Add(' p:=typeinfo(tproca);');
  26681. Add(' p:=typeinfo(tprocb);');
  26682. ConvertUnit;
  26683. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  26684. LinesToStr([ // statements
  26685. 'var $impl = $mod.$impl;',
  26686. '$mod.$rtti.$ProcVar("TProcA", {',
  26687. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  26688. '});',
  26689. '']),
  26690. LinesToStr([ // this.$init
  26691. '$impl.p = $mod.$rtti["TProcA"];',
  26692. '$impl.p = $mod.$rtti["TProcB"];',
  26693. '']),
  26694. LinesToStr([ // implementation
  26695. '$mod.$rtti.$ProcVar("TProcB", {',
  26696. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  26697. '});',
  26698. '$impl.p = null;',
  26699. '']) );
  26700. end;
  26701. procedure TTestModule.TestRTTI_EnumAndSetType;
  26702. begin
  26703. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26704. StartProgram(false);
  26705. Add('type');
  26706. Add(' TFlag = (light,dark);');
  26707. Add(' TFlags = set of TFlag;');
  26708. Add(' TProc = function(f: TFlags): TFlag;');
  26709. Add('var p: pointer;');
  26710. Add('begin');
  26711. Add(' p:=typeinfo(tflag);');
  26712. Add(' p:=typeinfo(tflags);');
  26713. ConvertProgram;
  26714. CheckSource('TestRTTI_EnumAndType',
  26715. LinesToStr([ // statements
  26716. 'this.TFlag = {',
  26717. ' "0": "light",',
  26718. ' light: 0,',
  26719. ' "1": "dark",',
  26720. ' dark: 1',
  26721. '};',
  26722. '$mod.$rtti.$Enum("TFlag", {',
  26723. ' minvalue: 0,',
  26724. ' maxvalue: 1,',
  26725. ' ordtype: 1,',
  26726. ' enumtype: this.TFlag',
  26727. '});',
  26728. '$mod.$rtti.$Set("TFlags", {',
  26729. ' comptype: $mod.$rtti["TFlag"]',
  26730. '});',
  26731. '$mod.$rtti.$ProcVar("TProc", {',
  26732. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  26733. '});',
  26734. 'this.p = null;',
  26735. '']),
  26736. LinesToStr([ // $mod.$main
  26737. '$mod.p = $mod.$rtti["TFlag"];',
  26738. '$mod.p = $mod.$rtti["TFlags"];',
  26739. '']));
  26740. end;
  26741. procedure TTestModule.TestRTTI_EnumRange;
  26742. begin
  26743. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26744. StartProgram(false);
  26745. Add([
  26746. 'type',
  26747. ' TCol = (red,green,blue);',
  26748. ' TColRg = green..blue;',
  26749. ' TSetOfColRg = set of TColRg;',
  26750. 'var p: pointer;',
  26751. 'begin',
  26752. ' p:=typeinfo(tcolrg);',
  26753. ' p:=typeinfo(tsetofcolrg);',
  26754. '']);
  26755. ConvertProgram;
  26756. end;
  26757. procedure TTestModule.TestRTTI_AnonymousEnumType;
  26758. begin
  26759. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26760. StartProgram(false);
  26761. Add('type');
  26762. Add(' TFlags = set of (red, green);');
  26763. Add('var');
  26764. Add(' f: TFlags;');
  26765. Add('begin');
  26766. Add(' Include(f,red);');
  26767. ConvertProgram;
  26768. CheckSource('TestRTTI_AnonymousEnumType',
  26769. LinesToStr([ // statements
  26770. 'this.TFlags$a = {',
  26771. ' "0": "red",',
  26772. ' red: 0,',
  26773. ' "1": "green",',
  26774. ' green: 1',
  26775. '};',
  26776. '$mod.$rtti.$Enum("TFlags$a", {',
  26777. ' minvalue: 0,',
  26778. ' maxvalue: 1,',
  26779. ' ordtype: 1,',
  26780. ' enumtype: this.TFlags$a',
  26781. '});',
  26782. '$mod.$rtti.$Set("TFlags", {',
  26783. ' comptype: $mod.$rtti["TFlags$a"]',
  26784. '});',
  26785. 'this.f = {};',
  26786. '']),
  26787. LinesToStr([
  26788. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  26789. '']));
  26790. end;
  26791. procedure TTestModule.TestRTTI_StaticArray;
  26792. begin
  26793. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26794. StartProgram(false);
  26795. Add('type');
  26796. Add(' TFlag = (light,dark);');
  26797. Add(' TFlagNames = array[TFlag] of string;');
  26798. Add(' TBoolNames = array[boolean] of string;');
  26799. Add(' TByteArray = array[1..32768] of byte;');
  26800. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  26801. Add('var p: pointer;');
  26802. Add('begin');
  26803. Add(' p:=typeinfo(TFlagNames);');
  26804. Add(' p:=typeinfo(TBoolNames);');
  26805. ConvertProgram;
  26806. CheckSource('TestRTTI_StaticArray',
  26807. LinesToStr([ // statements
  26808. 'this.TFlag = {',
  26809. ' "0": "light",',
  26810. ' light: 0,',
  26811. ' "1": "dark",',
  26812. ' dark: 1',
  26813. '};',
  26814. '$mod.$rtti.$Enum("TFlag", {',
  26815. ' minvalue: 0,',
  26816. ' maxvalue: 1,',
  26817. ' ordtype: 1,',
  26818. ' enumtype: this.TFlag',
  26819. '});',
  26820. '$mod.$rtti.$StaticArray("TFlagNames", {',
  26821. ' dims: [2],',
  26822. ' eltype: rtl.string',
  26823. '});',
  26824. '$mod.$rtti.$StaticArray("TBoolNames", {',
  26825. ' dims: [2],',
  26826. ' eltype: rtl.string',
  26827. '});',
  26828. '$mod.$rtti.$StaticArray("TByteArray", {',
  26829. ' dims: [32768],',
  26830. ' eltype: rtl.byte',
  26831. '});',
  26832. '$mod.$rtti.$ProcVar("TProc", {',
  26833. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  26834. '});',
  26835. 'this.p = null;',
  26836. '']),
  26837. LinesToStr([ // $mod.$main
  26838. '$mod.p = $mod.$rtti["TFlagNames"];',
  26839. '$mod.p = $mod.$rtti["TBoolNames"];',
  26840. '']));
  26841. end;
  26842. procedure TTestModule.TestRTTI_DynArray;
  26843. begin
  26844. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26845. StartProgram(false);
  26846. Add('type');
  26847. Add(' TArrStr = array of string;');
  26848. Add(' TArr2Dim = array of tarrstr;');
  26849. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  26850. Add('var p: pointer;');
  26851. Add('begin');
  26852. Add(' p:=typeinfo(tarrstr);');
  26853. Add(' p:=typeinfo(tarr2dim);');
  26854. ConvertProgram;
  26855. CheckSource('TestRTTI_DynArray',
  26856. LinesToStr([ // statements
  26857. '$mod.$rtti.$DynArray("TArrStr", {',
  26858. ' eltype: rtl.string',
  26859. '});',
  26860. '$mod.$rtti.$DynArray("TArr2Dim", {',
  26861. ' eltype: $mod.$rtti["TArrStr"]',
  26862. '});',
  26863. '$mod.$rtti.$ProcVar("TProc", {',
  26864. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  26865. '});',
  26866. 'this.p = null;',
  26867. '']),
  26868. LinesToStr([ // $mod.$main
  26869. '$mod.p = $mod.$rtti["TArrStr"];',
  26870. '$mod.p = $mod.$rtti["TArr2Dim"];',
  26871. '']));
  26872. end;
  26873. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  26874. begin
  26875. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26876. StartProgram(false);
  26877. Add('type');
  26878. Add(' TArr = array of array of longint;');
  26879. Add('var a: TArr;');
  26880. Add('begin');
  26881. ConvertProgram;
  26882. CheckSource('TestRTTI_ArrayNestedAnonymous',
  26883. LinesToStr([ // statements
  26884. '$mod.$rtti.$DynArray("TArr$a", {',
  26885. ' eltype: rtl.longint',
  26886. '});',
  26887. '$mod.$rtti.$DynArray("TArr", {',
  26888. ' eltype: $mod.$rtti["TArr$a"]',
  26889. '});',
  26890. 'this.a = [];',
  26891. '']),
  26892. LinesToStr([ // $mod.$main
  26893. ]));
  26894. end;
  26895. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  26896. begin
  26897. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26898. StartProgram(false);
  26899. Add('type');
  26900. Add(' TObject = class');
  26901. Add(' published');
  26902. Add(' procedure Proc; virtual; abstract;');
  26903. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  26904. Add(' end;');
  26905. Add('begin');
  26906. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  26907. nDuplicatePublishedMethodXAtY);
  26908. ConvertProgram;
  26909. end;
  26910. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  26911. begin
  26912. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26913. StartProgram(false);
  26914. Add('type');
  26915. Add(' TObject = class');
  26916. Add(' published');
  26917. Add(' procedure Proc; external name ''foo'';');
  26918. Add(' end;');
  26919. Add('begin');
  26920. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  26921. nPublishedNameMustMatchExternal);
  26922. ConvertProgram;
  26923. end;
  26924. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  26925. begin
  26926. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26927. StartProgram(false);
  26928. Add('type');
  26929. Add(' TObject = class');
  26930. Add(' class var FA: longint;');
  26931. Add(' published');
  26932. Add(' class property A: longint read FA;');
  26933. Add(' end;');
  26934. Add('begin');
  26935. SetExpectedPasResolverError('Invalid published property modifier "class"',
  26936. nInvalidXModifierY);
  26937. ConvertProgram;
  26938. end;
  26939. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  26940. begin
  26941. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26942. StartProgram(false);
  26943. Add('type');
  26944. Add(' TObject = class');
  26945. Add(' published');
  26946. Add(' class var FA: longint;');
  26947. Add(' end;');
  26948. Add('begin');
  26949. SetExpectedPasResolverError(sSymbolCannotBePublished,
  26950. nSymbolCannotBePublished);
  26951. ConvertProgram;
  26952. end;
  26953. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  26954. begin
  26955. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26956. StartProgram(false);
  26957. Add('{$modeswitch externalclass}');
  26958. Add('type');
  26959. Add(' TObject = class');
  26960. Add(' published');
  26961. Add(' V: longint; external name ''foo'';');
  26962. Add(' end;');
  26963. Add('begin');
  26964. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  26965. nPublishedNameMustMatchExternal);
  26966. ConvertProgram;
  26967. end;
  26968. procedure TTestModule.TestRTTI_Class_Field;
  26969. begin
  26970. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26971. StartProgram(false);
  26972. Add('{$modeswitch externalclass}');
  26973. Add('type');
  26974. Add(' TObject = class');
  26975. Add(' private');
  26976. Add(' FPropA: string;');
  26977. Add(' published');
  26978. Add(' VarLI: longint;');
  26979. Add(' VarC: char;');
  26980. Add(' VarS: string;');
  26981. Add(' VarD: double;');
  26982. Add(' VarB: boolean;');
  26983. Add(' VarLW: longword;');
  26984. Add(' VarSmI: smallint;');
  26985. Add(' VarW: word;');
  26986. Add(' VarShI: shortint;');
  26987. Add(' VarBy: byte;');
  26988. Add(' VarExt: longint external name ''VarExt'';');
  26989. Add(' ArrA, ArrB: array of byte;');
  26990. Add(' end;');
  26991. Add('var p: pointer;');
  26992. Add(' Obj: tobject;');
  26993. Add('begin');
  26994. Add(' p:=typeinfo(tobject);');
  26995. Add(' p:=typeinfo(p);');
  26996. Add(' p:=typeinfo(obj);');
  26997. ConvertProgram;
  26998. CheckSource('TestRTTI_Class_Field',
  26999. LinesToStr([ // statements
  27000. 'rtl.createClass($mod, "TObject", null, function () {',
  27001. ' this.$init = function () {',
  27002. ' this.FPropA = "";',
  27003. ' this.VarLI = 0;',
  27004. ' this.VarC = "";',
  27005. ' this.VarS = "";',
  27006. ' this.VarD = 0.0;',
  27007. ' this.VarB = false;',
  27008. ' this.VarLW = 0;',
  27009. ' this.VarSmI = 0;',
  27010. ' this.VarW = 0;',
  27011. ' this.VarShI = 0;',
  27012. ' this.VarBy = 0;',
  27013. ' this.ArrA = [];',
  27014. ' this.ArrB = [];',
  27015. ' };',
  27016. ' this.$final = function () {',
  27017. ' this.ArrA = undefined;',
  27018. ' this.ArrB = undefined;',
  27019. ' };',
  27020. ' var $r = this.$rtti;',
  27021. ' $r.addField("VarLI", rtl.longint);',
  27022. ' $r.addField("VarC", rtl.char);',
  27023. ' $r.addField("VarS", rtl.string);',
  27024. ' $r.addField("VarD", rtl.double);',
  27025. ' $r.addField("VarB", rtl.boolean);',
  27026. ' $r.addField("VarLW", rtl.longword);',
  27027. ' $r.addField("VarSmI", rtl.smallint);',
  27028. ' $r.addField("VarW", rtl.word);',
  27029. ' $r.addField("VarShI", rtl.shortint);',
  27030. ' $r.addField("VarBy", rtl.byte);',
  27031. ' $r.addField("VarExt", rtl.longint);',
  27032. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  27033. ' eltype: rtl.byte',
  27034. ' });',
  27035. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  27036. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  27037. '});',
  27038. 'this.p = null;',
  27039. 'this.Obj = null;',
  27040. '']),
  27041. LinesToStr([ // $mod.$main
  27042. '$mod.p = $mod.$rtti["TObject"];',
  27043. '$mod.p = rtl.pointer;',
  27044. '$mod.p = $mod.Obj.$rtti;',
  27045. '']));
  27046. end;
  27047. procedure TTestModule.TestRTTI_Class_Method;
  27048. begin
  27049. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27050. StartProgram(false);
  27051. Add('type');
  27052. Add(' TObject = class');
  27053. Add(' private');
  27054. Add(' procedure Internal; external name ''$intern'';');
  27055. Add(' published');
  27056. Add(' procedure Click; virtual; abstract;');
  27057. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  27058. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  27059. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  27060. Add(' end;');
  27061. Add('begin');
  27062. ConvertProgram;
  27063. CheckSource('TestRTTI_Class_Method',
  27064. LinesToStr([ // statements
  27065. 'rtl.createClass($mod, "TObject", null, function () {',
  27066. ' this.$init = function () {',
  27067. ' };',
  27068. ' this.$final = function () {',
  27069. ' };',
  27070. ' var $r = this.$rtti;',
  27071. ' $r.addMethod("Click", 0, null);',
  27072. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  27073. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  27074. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  27075. ' flags: 2',
  27076. ' });',
  27077. '});',
  27078. '']),
  27079. LinesToStr([ // $mod.$main
  27080. '']));
  27081. end;
  27082. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  27083. begin
  27084. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27085. StartProgram(false);
  27086. Add('type');
  27087. Add(' TObject = class');
  27088. Add(' published');
  27089. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  27090. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  27091. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  27092. Add(' end;');
  27093. Add('begin');
  27094. ConvertProgram;
  27095. CheckSource('TestRTTI_Class_MethodOpenArray',
  27096. LinesToStr([ // statements
  27097. 'rtl.createClass($mod, "TObject", null, function () {',
  27098. ' this.$init = function () {',
  27099. ' };',
  27100. ' this.$final = function () {',
  27101. ' };',
  27102. ' var $r = this.$rtti;',
  27103. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  27104. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  27105. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  27106. '});',
  27107. '']),
  27108. LinesToStr([ // $mod.$main
  27109. '']));
  27110. end;
  27111. procedure TTestModule.TestRTTI_Class_Property;
  27112. begin
  27113. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27114. StartProgram(false);
  27115. Add('{$modeswitch externalclass}');
  27116. Add('type');
  27117. Add(' TObject = class');
  27118. Add(' private');
  27119. Add(' FColor: longint;');
  27120. Add(' FColorStored: boolean;');
  27121. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  27122. Add(' function GetColor: longint; virtual; abstract;');
  27123. Add(' function GetColorStored: boolean; virtual; abstract;');
  27124. Add(' FExtSize: longint external name ''$extSize'';');
  27125. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  27126. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  27127. Add(' function GetExtSize: longint; external name ''$getSize'';');
  27128. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  27129. Add(' published');
  27130. Add(' property ColorA: longint read FColor;');
  27131. Add(' property ColorB: longint write FColor;');
  27132. Add(' property ColorC: longint read GetColor write SetColor;');
  27133. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  27134. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  27135. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  27136. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  27137. Add(' end;');
  27138. Add('begin');
  27139. ConvertProgram;
  27140. CheckSource('TestRTTI_Class_Property',
  27141. LinesToStr([ // statements
  27142. 'rtl.createClass($mod, "TObject", null, function () {',
  27143. ' this.$init = function () {',
  27144. ' this.FColor = 0;',
  27145. ' this.FColorStored = false;',
  27146. ' };',
  27147. ' this.$final = function () {',
  27148. ' };',
  27149. ' var $r = this.$rtti;',
  27150. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  27151. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  27152. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  27153. ' $r.addProperty(',
  27154. ' "ColorD",',
  27155. ' 8,',
  27156. ' rtl.longint,',
  27157. ' "FColor",',
  27158. ' "FColor",',
  27159. ' {',
  27160. ' stored: "FColorStored"',
  27161. ' }',
  27162. ' );',
  27163. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  27164. ' $r.addProperty(',
  27165. ' "ExtSizeB",',
  27166. ' 11,',
  27167. ' rtl.longint,',
  27168. ' "$getSize",',
  27169. ' "$setSize",',
  27170. ' {',
  27171. ' stored: "$extSizeStored"',
  27172. ' }',
  27173. ' );',
  27174. ' $r.addProperty(',
  27175. ' "ExtSizeC",',
  27176. ' 12,',
  27177. ' rtl.longint,',
  27178. ' "$extSize",',
  27179. ' "$extSize",',
  27180. ' {',
  27181. ' stored: "$getExtSizeStored"',
  27182. ' }',
  27183. ' );',
  27184. '});',
  27185. '']),
  27186. LinesToStr([ // $mod.$main
  27187. '']));
  27188. end;
  27189. procedure TTestModule.TestRTTI_Class_PropertyParams;
  27190. begin
  27191. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27192. StartProgram(false);
  27193. Add('{$modeswitch externalclass}');
  27194. Add('type');
  27195. Add(' integer = longint;');
  27196. Add(' TObject = class');
  27197. Add(' private');
  27198. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  27199. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  27200. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  27201. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  27202. Add(' published');
  27203. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  27204. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  27205. Add(' end;');
  27206. Add('begin');
  27207. ConvertProgram;
  27208. CheckSource('TestRTTI_Class_PropertyParams',
  27209. LinesToStr([ // statements
  27210. 'rtl.createClass($mod, "TObject", null, function () {',
  27211. ' this.$init = function () {',
  27212. ' };',
  27213. ' this.$final = function () {',
  27214. ' };',
  27215. ' var $r = this.$rtti;',
  27216. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  27217. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  27218. '});',
  27219. '']),
  27220. LinesToStr([ // $mod.$main
  27221. '']));
  27222. end;
  27223. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  27224. begin
  27225. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27226. AddModuleWithIntfImplSrc('unit1.pas',
  27227. 'type TColor = -5..5;',
  27228. '');
  27229. StartProgram(true);
  27230. Add([
  27231. 'uses unit1;',
  27232. 'type',
  27233. ' TColorAlias = TColor;',
  27234. ' TColorTypeAlias = type TColor;',
  27235. ' TObject = class',
  27236. ' private',
  27237. ' fColor: TColor;',
  27238. ' fAlias: TColorAlias;',
  27239. ' fTypeAlias: TColorTypeAlias;',
  27240. ' published',
  27241. ' property Color: TColor read fcolor;',
  27242. ' property Alias: TColorAlias read falias;',
  27243. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  27244. ' end;',
  27245. 'begin',
  27246. '']);
  27247. ConvertProgram;
  27248. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  27249. LinesToStr([ // statements
  27250. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  27251. 'rtl.createClass($mod, "TObject", null, function () {',
  27252. ' this.$init = function () {',
  27253. ' this.fColor = 0;',
  27254. ' this.fAlias = 0;',
  27255. ' this.fTypeAlias = 0;',
  27256. ' };',
  27257. ' this.$final = function () {',
  27258. ' };',
  27259. ' var $r = this.$rtti;',
  27260. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  27261. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  27262. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  27263. '});',
  27264. '']),
  27265. LinesToStr([ // $mod.$main
  27266. '']));
  27267. end;
  27268. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  27269. begin
  27270. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27271. StartProgram(false);
  27272. Add([
  27273. '{$modeswitch omitrtti}',
  27274. 'type',
  27275. ' TObject = class',
  27276. ' private',
  27277. ' FA: byte;',
  27278. ' published',
  27279. ' property A: byte read FA write FA;',
  27280. ' end;',
  27281. 'begin']);
  27282. ConvertProgram;
  27283. CheckSource('TestRTTI_Class_OmitRTTI',
  27284. LinesToStr([ // statements
  27285. 'rtl.createClass($mod, "TObject", null, function () {',
  27286. ' this.$init = function () {',
  27287. ' this.FA = 0;',
  27288. ' };',
  27289. ' this.$final = function () {',
  27290. ' };',
  27291. '});',
  27292. '']),
  27293. LinesToStr([ // $mod.$main
  27294. '']));
  27295. end;
  27296. procedure TTestModule.TestRTTI_IndexModifier;
  27297. begin
  27298. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27299. StartProgram(false);
  27300. Add([
  27301. 'type',
  27302. ' TEnum = (red, blue);',
  27303. ' TObject = class',
  27304. ' FB: boolean;',
  27305. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  27306. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  27307. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  27308. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  27309. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  27310. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  27311. ' published',
  27312. ' property B1: boolean index 1 read FB write SetIntBool;',
  27313. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  27314. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  27315. ' end;',
  27316. 'begin']);
  27317. ConvertProgram;
  27318. CheckSource('TestRTTI_IndexModifier',
  27319. LinesToStr([ // statements
  27320. 'this.TEnum = {',
  27321. ' "0": "red",',
  27322. ' red: 0,',
  27323. ' "1": "blue",',
  27324. ' blue: 1',
  27325. '};',
  27326. '$mod.$rtti.$Enum("TEnum", {',
  27327. ' minvalue: 0,',
  27328. ' maxvalue: 1,',
  27329. ' ordtype: 1,',
  27330. ' enumtype: this.TEnum',
  27331. '});',
  27332. 'rtl.createClass($mod, "TObject", null, function () {',
  27333. ' this.$init = function () {',
  27334. ' this.FB = false;',
  27335. ' };',
  27336. ' this.$final = function () {',
  27337. ' };',
  27338. ' var $r = this.$rtti;',
  27339. ' $r.addProperty(',
  27340. ' "B1",',
  27341. ' 18,',
  27342. ' rtl.boolean,',
  27343. ' "FB",',
  27344. ' "SetIntBool",',
  27345. ' {',
  27346. ' index: 1',
  27347. ' }',
  27348. ' );',
  27349. ' $r.addProperty(',
  27350. ' "B2",',
  27351. ' 17,',
  27352. ' rtl.boolean,',
  27353. ' "GetEnumBool",',
  27354. ' "FB",',
  27355. ' {',
  27356. ' index: $mod.TEnum.blue',
  27357. ' }',
  27358. ' );',
  27359. ' $r.addProperty(',
  27360. ' "I1",',
  27361. ' 19,',
  27362. ' rtl.boolean,',
  27363. ' "GetStrIntBool",',
  27364. ' "SetStrIntBool",',
  27365. ' {',
  27366. ' index: 2',
  27367. ' }',
  27368. ' );',
  27369. '});',
  27370. '']),
  27371. LinesToStr([ // $mod.$main
  27372. '']));
  27373. end;
  27374. procedure TTestModule.TestRTTI_StoredModifier;
  27375. begin
  27376. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27377. StartProgram(false);
  27378. Add([
  27379. 'const',
  27380. ' ConstB = true;',
  27381. 'type',
  27382. ' TObject = class',
  27383. ' private',
  27384. ' FB: boolean;',
  27385. ' function IsBStored: boolean; virtual; abstract;',
  27386. ' published',
  27387. ' property BoolA: boolean read FB stored true;',
  27388. ' property BoolB: boolean read FB stored false;',
  27389. ' property BoolC: boolean read FB stored FB;',
  27390. ' property BoolD: boolean read FB stored ConstB;',
  27391. ' property BoolE: boolean read FB stored IsBStored;',
  27392. ' end;',
  27393. 'begin']);
  27394. ConvertProgram;
  27395. CheckSource('TestRTTI_StoredModifier',
  27396. LinesToStr([ // statements
  27397. 'this.ConstB = true;',
  27398. 'rtl.createClass($mod, "TObject", null, function () {',
  27399. ' this.$init = function () {',
  27400. ' this.FB = false;',
  27401. ' };',
  27402. ' this.$final = function () {',
  27403. ' };',
  27404. ' var $r = this.$rtti;',
  27405. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  27406. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  27407. ' $r.addProperty(',
  27408. ' "BoolC",',
  27409. ' 8,',
  27410. ' rtl.boolean,',
  27411. ' "FB",',
  27412. ' "",',
  27413. ' {',
  27414. ' stored: "FB"',
  27415. ' }',
  27416. ' );',
  27417. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  27418. ' $r.addProperty(',
  27419. ' "BoolE",',
  27420. ' 12,',
  27421. ' rtl.boolean,',
  27422. ' "FB",',
  27423. ' "",',
  27424. ' {',
  27425. ' stored: "IsBStored"',
  27426. ' }',
  27427. ' );',
  27428. '});',
  27429. '']),
  27430. LinesToStr([ // $mod.$main
  27431. '']));
  27432. end;
  27433. procedure TTestModule.TestRTTI_DefaultValue;
  27434. begin
  27435. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27436. StartProgram(false);
  27437. Add([
  27438. 'type',
  27439. ' TEnum = (red, blue);',
  27440. 'const',
  27441. ' CB = true or false;',
  27442. ' CI = 1+2;',
  27443. 'type',
  27444. ' TObject = class',
  27445. ' FB: boolean;',
  27446. ' FI: longint;',
  27447. ' FE: TEnum;',
  27448. ' published',
  27449. ' property B1: boolean read FB default true;',
  27450. ' property B2: boolean read FB default CB;',
  27451. ' property B3: boolean read FB default test1.cb;',
  27452. ' property I1: longint read FI default 2;',
  27453. ' property I2: longint read FI default CI;',
  27454. ' property E1: TEnum read FE default red;',
  27455. ' property E2: TEnum read FE default TEnum.blue;',
  27456. ' end;',
  27457. 'begin']);
  27458. ConvertProgram;
  27459. CheckSource('TestRTTI_DefaultValue',
  27460. LinesToStr([ // statements
  27461. 'this.TEnum = {',
  27462. ' "0": "red",',
  27463. ' red: 0,',
  27464. ' "1": "blue",',
  27465. ' blue: 1',
  27466. '};',
  27467. '$mod.$rtti.$Enum("TEnum", {',
  27468. ' minvalue: 0,',
  27469. ' maxvalue: 1,',
  27470. ' ordtype: 1,',
  27471. ' enumtype: this.TEnum',
  27472. '});',
  27473. 'this.CB = true || false;',
  27474. 'this.CI = 1 + 2;',
  27475. 'rtl.createClass($mod, "TObject", null, function () {',
  27476. ' this.$init = function () {',
  27477. ' this.FB = false;',
  27478. ' this.FI = 0;',
  27479. ' this.FE = 0;',
  27480. ' };',
  27481. ' this.$final = function () {',
  27482. ' };',
  27483. ' var $r = this.$rtti;',
  27484. ' $r.addProperty(',
  27485. ' "B1",',
  27486. ' 0,',
  27487. ' rtl.boolean,',
  27488. ' "FB",',
  27489. ' "",',
  27490. ' {',
  27491. ' Default: true',
  27492. ' }',
  27493. ' );',
  27494. ' $r.addProperty(',
  27495. ' "B2",',
  27496. ' 0,',
  27497. ' rtl.boolean,',
  27498. ' "FB",',
  27499. ' "",',
  27500. ' {',
  27501. ' Default: true',
  27502. ' }',
  27503. ' );',
  27504. ' $r.addProperty(',
  27505. ' "B3",',
  27506. ' 0,',
  27507. ' rtl.boolean,',
  27508. ' "FB",',
  27509. ' "",',
  27510. ' {',
  27511. ' Default: true',
  27512. ' }',
  27513. ' );',
  27514. ' $r.addProperty(',
  27515. ' "I1",',
  27516. ' 0,',
  27517. ' rtl.longint,',
  27518. ' "FI",',
  27519. ' "",',
  27520. ' {',
  27521. ' Default: 2',
  27522. ' }',
  27523. ' );',
  27524. ' $r.addProperty(',
  27525. ' "I2",',
  27526. ' 0,',
  27527. ' rtl.longint,',
  27528. ' "FI",',
  27529. ' "",',
  27530. ' {',
  27531. ' Default: 3',
  27532. ' }',
  27533. ' );',
  27534. ' $r.addProperty(',
  27535. ' "E1",',
  27536. ' 0,',
  27537. ' $mod.$rtti["TEnum"],',
  27538. ' "FE",',
  27539. ' "",',
  27540. ' {',
  27541. ' Default: $mod.TEnum.red',
  27542. ' }',
  27543. ' );',
  27544. ' $r.addProperty(',
  27545. ' "E2",',
  27546. ' 0,',
  27547. ' $mod.$rtti["TEnum"],',
  27548. ' "FE",',
  27549. ' "",',
  27550. ' {',
  27551. ' Default: $mod.TEnum.blue',
  27552. ' }',
  27553. ' );',
  27554. '});',
  27555. '']),
  27556. LinesToStr([ // $mod.$main
  27557. '']));
  27558. end;
  27559. procedure TTestModule.TestRTTI_DefaultValueSet;
  27560. begin
  27561. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27562. StartProgram(false);
  27563. Add([
  27564. 'type',
  27565. ' TEnum = (red, blue);',
  27566. ' TSet = set of TEnum;',
  27567. 'const',
  27568. ' CSet = [red,blue];',
  27569. 'type',
  27570. ' TObject = class',
  27571. ' FSet: TSet;',
  27572. ' published',
  27573. ' property Set1: TSet read FSet default [];',
  27574. ' property Set2: TSet read FSet default [red];',
  27575. ' property Set3: TSet read FSet default [red,blue];',
  27576. ' property Set4: TSet read FSet default CSet;',
  27577. ' end;',
  27578. 'begin']);
  27579. ConvertProgram;
  27580. CheckSource('TestRTTI_DefaultValueSet',
  27581. LinesToStr([ // statements
  27582. 'this.TEnum = {',
  27583. ' "0": "red",',
  27584. ' red: 0,',
  27585. ' "1": "blue",',
  27586. ' blue: 1',
  27587. '};',
  27588. '$mod.$rtti.$Enum("TEnum", {',
  27589. ' minvalue: 0,',
  27590. ' maxvalue: 1,',
  27591. ' ordtype: 1,',
  27592. ' enumtype: this.TEnum',
  27593. '});',
  27594. '$mod.$rtti.$Set("TSet", {',
  27595. ' comptype: $mod.$rtti["TEnum"]',
  27596. '});',
  27597. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  27598. 'rtl.createClass($mod, "TObject", null, function () {',
  27599. ' this.$init = function () {',
  27600. ' this.FSet = {};',
  27601. ' };',
  27602. ' this.$final = function () {',
  27603. ' this.FSet = undefined;',
  27604. ' };',
  27605. ' var $r = this.$rtti;',
  27606. ' $r.addProperty(',
  27607. ' "Set1",',
  27608. ' 0,',
  27609. ' $mod.$rtti["TSet"],',
  27610. ' "FSet",',
  27611. ' "",',
  27612. ' {',
  27613. ' Default: {}',
  27614. ' }',
  27615. ' );',
  27616. ' $r.addProperty(',
  27617. ' "Set2",',
  27618. ' 0,',
  27619. ' $mod.$rtti["TSet"],',
  27620. ' "FSet",',
  27621. ' "",',
  27622. ' {',
  27623. ' Default: rtl.createSet($mod.TEnum.red)',
  27624. ' }',
  27625. ' );',
  27626. ' $r.addProperty(',
  27627. ' "Set3",',
  27628. ' 0,',
  27629. ' $mod.$rtti["TSet"],',
  27630. ' "FSet",',
  27631. ' "",',
  27632. ' {',
  27633. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  27634. ' }',
  27635. ' );',
  27636. ' $r.addProperty(',
  27637. ' "Set4",',
  27638. ' 0,',
  27639. ' $mod.$rtti["TSet"],',
  27640. ' "FSet",',
  27641. ' "",',
  27642. ' {',
  27643. ' Default: $mod.CSet',
  27644. ' }',
  27645. ' );',
  27646. '});',
  27647. '']),
  27648. LinesToStr([ // $mod.$main
  27649. '']));
  27650. end;
  27651. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  27652. begin
  27653. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27654. StartProgram(false);
  27655. Add([
  27656. 'type',
  27657. ' TRg = -1..1;',
  27658. 'const',
  27659. ' l = low(TRg);',
  27660. ' h = high(TRg);',
  27661. 'type',
  27662. ' TObject = class',
  27663. ' FV: TRg;',
  27664. ' published',
  27665. ' property V1: TRg read FV default -1;',
  27666. ' end;',
  27667. 'begin']);
  27668. ConvertProgram;
  27669. CheckSource('TestRTTI_DefaultValueRangeType',
  27670. LinesToStr([ // statements
  27671. '$mod.$rtti.$Int("TRg", {',
  27672. ' minvalue: -1,',
  27673. ' maxvalue: 1,',
  27674. ' ordtype: 0',
  27675. '});',
  27676. 'this.l = -1;',
  27677. 'this.h = 1;',
  27678. 'rtl.createClass($mod, "TObject", null, function () {',
  27679. ' this.$init = function () {',
  27680. ' this.FV = 0;',
  27681. ' };',
  27682. ' this.$final = function () {',
  27683. ' };',
  27684. ' var $r = this.$rtti;',
  27685. ' $r.addProperty(',
  27686. ' "V1",',
  27687. ' 0,',
  27688. ' $mod.$rtti["TRg"],',
  27689. ' "FV",',
  27690. ' "",',
  27691. ' {',
  27692. ' Default: -1',
  27693. ' }',
  27694. ' );',
  27695. '});',
  27696. '']),
  27697. LinesToStr([ // $mod.$main
  27698. '']));
  27699. end;
  27700. procedure TTestModule.TestRTTI_DefaultValueInherit;
  27701. begin
  27702. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27703. StartProgram(false);
  27704. Add([
  27705. 'type',
  27706. ' TObject = class',
  27707. ' FA, FB: byte;',
  27708. ' property A: byte read FA default 1;',
  27709. ' property B: byte read FB default 2;',
  27710. ' end;',
  27711. ' TBird = class',
  27712. ' published',
  27713. ' property A;',
  27714. ' property B nodefault;',
  27715. ' end;',
  27716. 'begin']);
  27717. ConvertProgram;
  27718. CheckSource('TestRTTI_DefaultValueInherit',
  27719. LinesToStr([ // statements
  27720. 'rtl.createClass($mod, "TObject", null, function () {',
  27721. ' this.$init = function () {',
  27722. ' this.FA = 0;',
  27723. ' this.FB = 0;',
  27724. ' };',
  27725. ' this.$final = function () {',
  27726. ' };',
  27727. '});',
  27728. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27729. ' var $r = this.$rtti;',
  27730. ' $r.addProperty(',
  27731. ' "A",',
  27732. ' 0,',
  27733. ' rtl.byte,',
  27734. ' "FA",',
  27735. ' "",',
  27736. ' {',
  27737. ' Default: 1',
  27738. ' }',
  27739. ' );',
  27740. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  27741. '});',
  27742. '']),
  27743. LinesToStr([ // $mod.$main
  27744. '']));
  27745. end;
  27746. procedure TTestModule.TestRTTI_OverrideMethod;
  27747. begin
  27748. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27749. StartProgram(false);
  27750. Add('type');
  27751. Add(' TObject = class');
  27752. Add(' published');
  27753. Add(' procedure DoIt; virtual; abstract;');
  27754. Add(' end;');
  27755. Add(' TSky = class');
  27756. Add(' published');
  27757. Add(' procedure DoIt; override;');
  27758. Add(' end;');
  27759. Add('procedure TSky.DoIt; begin end;');
  27760. Add('begin');
  27761. ConvertProgram;
  27762. CheckSource('TestRTTI_OverrideMethod',
  27763. LinesToStr([ // statements
  27764. 'rtl.createClass($mod, "TObject", null, function () {',
  27765. ' this.$init = function () {',
  27766. ' };',
  27767. ' this.$final = function () {',
  27768. ' };',
  27769. ' var $r = this.$rtti;',
  27770. ' $r.addMethod("DoIt", 0, null);',
  27771. '});',
  27772. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27773. ' this.DoIt = function () {',
  27774. ' };',
  27775. '});',
  27776. '']),
  27777. LinesToStr([ // $mod.$main
  27778. '']));
  27779. end;
  27780. procedure TTestModule.TestRTTI_ReintroduceMethod;
  27781. begin
  27782. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27783. StartProgram(false);
  27784. Add([
  27785. 'type',
  27786. ' TObject = class',
  27787. ' published',
  27788. ' procedure DoIt;',
  27789. ' end;',
  27790. ' TSky = class',
  27791. ' published',
  27792. ' procedure DoIt; reintroduce;',
  27793. ' end;',
  27794. 'procedure TObject.DoIt; begin end;',
  27795. 'procedure TSky.DoIt;',
  27796. 'begin',
  27797. ' inherited DoIt;',
  27798. 'end;',
  27799. 'begin']);
  27800. ConvertProgram;
  27801. CheckSource('TestRTTI_ReintroduceMethod',
  27802. LinesToStr([ // statements
  27803. 'rtl.createClass($mod, "TObject", null, function () {',
  27804. ' this.$init = function () {',
  27805. ' };',
  27806. ' this.$final = function () {',
  27807. ' };',
  27808. ' this.DoIt = function () {',
  27809. ' };',
  27810. ' var $r = this.$rtti;',
  27811. ' $r.addMethod("DoIt", 0, null);',
  27812. '});',
  27813. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27814. ' this.DoIt = function () {',
  27815. ' $mod.TObject.DoIt.call(this);',
  27816. ' };',
  27817. ' var $r = this.$rtti;',
  27818. ' $r.addMethod("DoIt", 0, null);',
  27819. '});',
  27820. '']),
  27821. LinesToStr([ // $mod.$main
  27822. '']));
  27823. end;
  27824. procedure TTestModule.TestRTTI_OverloadProperty;
  27825. begin
  27826. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27827. StartProgram(false);
  27828. Add('type');
  27829. Add(' TObject = class');
  27830. Add(' protected');
  27831. Add(' FFlag: longint;');
  27832. Add(' published');
  27833. Add(' property Flag: longint read fflag;');
  27834. Add(' end;');
  27835. Add(' TSky = class');
  27836. Add(' published');
  27837. Add(' property FLAG: longint write fflag;');
  27838. Add(' end;');
  27839. Add('begin');
  27840. ConvertProgram;
  27841. CheckSource('TestRTTI_OverrideMethod',
  27842. LinesToStr([ // statements
  27843. 'rtl.createClass($mod, "TObject", null, function () {',
  27844. ' this.$init = function () {',
  27845. ' this.FFlag = 0;',
  27846. ' };',
  27847. ' this.$final = function () {',
  27848. ' };',
  27849. ' var $r = this.$rtti;',
  27850. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  27851. '});',
  27852. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27853. ' var $r = this.$rtti;',
  27854. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  27855. '});',
  27856. '']),
  27857. LinesToStr([ // $mod.$main
  27858. '']));
  27859. end;
  27860. procedure TTestModule.TestRTTI_ClassForward;
  27861. begin
  27862. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27863. StartProgram(false);
  27864. Add('type');
  27865. Add(' TObject = class end;');
  27866. Add(' tbridge = class;');
  27867. Add(' TProc = function: tbridge;');
  27868. Add(' TOger = class');
  27869. Add(' published');
  27870. Add(' FBridge: tbridge;');
  27871. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  27872. Add(' property Bridge: tbridge read fbridge write setbridge;');
  27873. Add(' end;');
  27874. Add(' TBridge = class');
  27875. Add(' FOger: toger;');
  27876. Add(' end;');
  27877. Add('var p: Pointer;');
  27878. Add(' b: tbridge;');
  27879. Add('begin');
  27880. Add(' p:=typeinfo(tbridge);');
  27881. Add(' p:=typeinfo(b);');
  27882. ConvertProgram;
  27883. CheckSource('TestRTTI_ClassForward',
  27884. LinesToStr([ // statements
  27885. 'rtl.createClass($mod, "TObject", null, function () {',
  27886. ' this.$init = function () {',
  27887. ' };',
  27888. ' this.$final = function () {',
  27889. ' };',
  27890. '});',
  27891. '$mod.$rtti.$Class("TBridge");',
  27892. '$mod.$rtti.$ProcVar("TProc", {',
  27893. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  27894. '});',
  27895. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  27896. ' this.$init = function () {',
  27897. ' $mod.TObject.$init.call(this);',
  27898. ' this.FBridge = null;',
  27899. ' };',
  27900. ' this.$final = function () {',
  27901. ' this.FBridge = undefined;',
  27902. ' $mod.TObject.$final.call(this);',
  27903. ' };',
  27904. ' var $r = this.$rtti;',
  27905. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  27906. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  27907. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  27908. '});',
  27909. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  27910. ' this.$init = function () {',
  27911. ' $mod.TObject.$init.call(this);',
  27912. ' this.FOger = null;',
  27913. ' };',
  27914. ' this.$final = function () {',
  27915. ' this.FOger = undefined;',
  27916. ' $mod.TObject.$final.call(this);',
  27917. ' };',
  27918. '});',
  27919. 'this.p = null;',
  27920. 'this.b = null;',
  27921. '']),
  27922. LinesToStr([ // $mod.$main
  27923. '$mod.p = $mod.$rtti["TBridge"];',
  27924. '$mod.p = $mod.b.$rtti;',
  27925. '']));
  27926. end;
  27927. procedure TTestModule.TestRTTI_ClassOf;
  27928. begin
  27929. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27930. StartProgram(false);
  27931. Add('type');
  27932. Add(' TClass = class of tobject;');
  27933. Add(' TProcA = function: TClass;');
  27934. Add(' TObject = class');
  27935. Add(' published');
  27936. Add(' C: tclass;');
  27937. Add(' end;');
  27938. Add(' tfox = class;');
  27939. Add(' TBird = class end;');
  27940. Add(' TBirds = class of tbird;');
  27941. Add(' TFox = class end;');
  27942. Add(' TFoxes = class of tfox;');
  27943. Add(' TCows = class of TCow;');
  27944. Add(' TCow = class;');
  27945. Add(' TCow = class end;');
  27946. Add('begin');
  27947. ConvertProgram;
  27948. CheckSource('TestRTTI_ClassOf',
  27949. LinesToStr([ // statements
  27950. '$mod.$rtti.$Class("TObject");',
  27951. '$mod.$rtti.$ClassRef("TClass", {',
  27952. ' instancetype: $mod.$rtti["TObject"]',
  27953. '});',
  27954. '$mod.$rtti.$ProcVar("TProcA", {',
  27955. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  27956. '});',
  27957. 'rtl.createClass($mod, "TObject", null, function () {',
  27958. ' this.$init = function () {',
  27959. ' this.C = null;',
  27960. ' };',
  27961. ' this.$final = function () {',
  27962. ' this.C = undefined;',
  27963. ' };',
  27964. ' var $r = this.$rtti;',
  27965. ' $r.addField("C", $mod.$rtti["TClass"]);',
  27966. '});',
  27967. '$mod.$rtti.$Class("TFox");',
  27968. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27969. '});',
  27970. '$mod.$rtti.$ClassRef("TBirds", {',
  27971. ' instancetype: $mod.$rtti["TBird"]',
  27972. '});',
  27973. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  27974. '});',
  27975. '$mod.$rtti.$ClassRef("TFoxes", {',
  27976. ' instancetype: $mod.$rtti["TFox"]',
  27977. '});',
  27978. '$mod.$rtti.$Class("TCow");',
  27979. '$mod.$rtti.$ClassRef("TCows", {',
  27980. ' instancetype: $mod.$rtti["TCow"]',
  27981. '});',
  27982. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  27983. '});',
  27984. '']),
  27985. LinesToStr([ // $mod.$main
  27986. '']));
  27987. end;
  27988. procedure TTestModule.TestRTTI_Record;
  27989. begin
  27990. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27991. StartProgram(false);
  27992. Add('type');
  27993. Add(' integer = longint;');
  27994. Add(' TPoint = record');
  27995. Add(' x,y: integer;');
  27996. Add(' end;');
  27997. Add('var p: pointer;');
  27998. Add(' r: tpoint;');
  27999. Add('begin');
  28000. Add(' p:=typeinfo(tpoint);');
  28001. Add(' p:=typeinfo(r);');
  28002. Add(' p:=typeinfo(r.x);');
  28003. ConvertProgram;
  28004. CheckSource('TestRTTI_Record',
  28005. LinesToStr([ // statements
  28006. 'rtl.recNewT($mod, "TPoint", function () {',
  28007. ' this.x = 0;',
  28008. ' this.y = 0;',
  28009. ' this.$eq = function (b) {',
  28010. ' return (this.x === b.x) && (this.y === b.y);',
  28011. ' };',
  28012. ' this.$assign = function (s) {',
  28013. ' this.x = s.x;',
  28014. ' this.y = s.y;',
  28015. ' return this;',
  28016. ' };',
  28017. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  28018. ' $r.addField("x", rtl.longint);',
  28019. ' $r.addField("y", rtl.longint);',
  28020. '});',
  28021. 'this.p = null;',
  28022. 'this.r = $mod.TPoint.$new();',
  28023. '']),
  28024. LinesToStr([ // $mod.$main
  28025. '$mod.p = $mod.$rtti["TPoint"];',
  28026. '$mod.p = $mod.$rtti["TPoint"];',
  28027. '$mod.p = rtl.longint;',
  28028. '']));
  28029. end;
  28030. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  28031. begin
  28032. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28033. StartProgram(false);
  28034. Add('type');
  28035. Add(' TFloatRec = record');
  28036. Add(' c,d: array of char;');
  28037. // Add(' i: array of array of longint;');
  28038. Add(' end;');
  28039. Add('var p: pointer;');
  28040. Add(' r: tfloatrec;');
  28041. Add('begin');
  28042. Add(' p:=typeinfo(tfloatrec);');
  28043. Add(' p:=typeinfo(r);');
  28044. Add(' p:=typeinfo(r.d);');
  28045. ConvertProgram;
  28046. CheckSource('TestRTTI_Record',
  28047. LinesToStr([ // statements
  28048. 'rtl.recNewT($mod, "TFloatRec", function () {',
  28049. ' this.$new = function () {',
  28050. ' var r = Object.create(this);',
  28051. ' r.c = [];',
  28052. ' r.d = [];',
  28053. ' return r;',
  28054. ' };',
  28055. ' this.$eq = function (b) {',
  28056. ' return (this.c === b.c) && (this.d === b.d);',
  28057. ' };',
  28058. ' this.$assign = function (s) {',
  28059. ' this.c = rtl.arrayRef(s.c);',
  28060. ' this.d = rtl.arrayRef(s.d);',
  28061. ' return this;',
  28062. ' };',
  28063. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  28064. ' eltype: rtl.char',
  28065. ' });',
  28066. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  28067. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  28068. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  28069. '});',
  28070. 'this.p = null;',
  28071. 'this.r = $mod.TFloatRec.$new();',
  28072. '']),
  28073. LinesToStr([ // $mod.$main
  28074. '$mod.p = $mod.$rtti["TFloatRec"];',
  28075. '$mod.p = $mod.$rtti["TFloatRec"];',
  28076. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  28077. '']));
  28078. end;
  28079. procedure TTestModule.TestRTTI_LocalTypes;
  28080. begin
  28081. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28082. StartProgram(false);
  28083. Add([
  28084. 'procedure DoIt;',
  28085. 'type',
  28086. ' integer = longint;',
  28087. ' TPoint = record',
  28088. ' x,y: integer;',
  28089. ' end;',
  28090. 'var p: TPoint;',
  28091. 'begin',
  28092. 'end;',
  28093. 'begin']);
  28094. ConvertProgram;
  28095. CheckSource('TestRTTI_LocalTypes',
  28096. LinesToStr([ // statements
  28097. 'var TPoint = rtl.recNewT(null, "", function () {',
  28098. ' this.x = 0;',
  28099. ' this.y = 0;',
  28100. ' this.$eq = function (b) {',
  28101. ' return (this.x === b.x) && (this.y === b.y);',
  28102. ' };',
  28103. ' this.$assign = function (s) {',
  28104. ' this.x = s.x;',
  28105. ' this.y = s.y;',
  28106. ' return this;',
  28107. ' };',
  28108. '});',
  28109. 'this.DoIt = function () {',
  28110. ' var p = TPoint.$new();',
  28111. '};',
  28112. '']),
  28113. LinesToStr([ // $mod.$main
  28114. '']));
  28115. end;
  28116. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  28117. begin
  28118. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28119. StartProgram(false);
  28120. Add([
  28121. 'type',
  28122. ' TCaption = string;',
  28123. ' TYesNo = boolean;',
  28124. ' TLetter = char;',
  28125. ' TFloat = double;',
  28126. ' TPtr = pointer;',
  28127. ' TShortInt = shortint;',
  28128. ' TByte = byte;',
  28129. ' TSmallInt = smallint;',
  28130. ' TWord = word;',
  28131. ' TInt32 = longint;',
  28132. ' TDWord = longword;',
  28133. ' TValue = jsvalue;',
  28134. 'var p: TPtr;',
  28135. 'begin',
  28136. ' p:=typeinfo(string);',
  28137. ' p:=typeinfo(tcaption);',
  28138. ' p:=typeinfo(boolean);',
  28139. ' p:=typeinfo(tyesno);',
  28140. ' p:=typeinfo(char);',
  28141. ' p:=typeinfo(tletter);',
  28142. ' p:=typeinfo(double);',
  28143. ' p:=typeinfo(tfloat);',
  28144. ' p:=typeinfo(pointer);',
  28145. ' p:=typeinfo(tptr);',
  28146. ' p:=typeinfo(shortint);',
  28147. ' p:=typeinfo(tshortint);',
  28148. ' p:=typeinfo(byte);',
  28149. ' p:=typeinfo(tbyte);',
  28150. ' p:=typeinfo(smallint);',
  28151. ' p:=typeinfo(tsmallint);',
  28152. ' p:=typeinfo(word);',
  28153. ' p:=typeinfo(tword);',
  28154. ' p:=typeinfo(longword);',
  28155. ' p:=typeinfo(tdword);',
  28156. ' p:=typeinfo(jsvalue);',
  28157. ' p:=typeinfo(tvalue);',
  28158. '']);
  28159. ConvertProgram;
  28160. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  28161. LinesToStr([ // statements
  28162. 'this.p = null;',
  28163. '']),
  28164. LinesToStr([ // $mod.$main
  28165. '$mod.p = rtl.string;',
  28166. '$mod.p = rtl.string;',
  28167. '$mod.p = rtl.boolean;',
  28168. '$mod.p = rtl.boolean;',
  28169. '$mod.p = rtl.char;',
  28170. '$mod.p = rtl.char;',
  28171. '$mod.p = rtl.double;',
  28172. '$mod.p = rtl.double;',
  28173. '$mod.p = rtl.pointer;',
  28174. '$mod.p = rtl.pointer;',
  28175. '$mod.p = rtl.shortint;',
  28176. '$mod.p = rtl.shortint;',
  28177. '$mod.p = rtl.byte;',
  28178. '$mod.p = rtl.byte;',
  28179. '$mod.p = rtl.smallint;',
  28180. '$mod.p = rtl.smallint;',
  28181. '$mod.p = rtl.word;',
  28182. '$mod.p = rtl.word;',
  28183. '$mod.p = rtl.longword;',
  28184. '$mod.p = rtl.longword;',
  28185. '$mod.p = rtl.jsvalue;',
  28186. '$mod.p = rtl.jsvalue;',
  28187. '']));
  28188. end;
  28189. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  28190. begin
  28191. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28192. StartProgram(false);
  28193. Add([
  28194. 'type',
  28195. ' TCaption = type string;',
  28196. ' TYesNo = type boolean;',
  28197. ' TLetter = type char;',
  28198. ' TFloat = type double;',
  28199. ' TPtr = type pointer;',
  28200. ' TShortInt = type shortint;',
  28201. ' TByte = type byte;',
  28202. ' TSmallInt = type smallint;',
  28203. ' TWord = type word;',
  28204. ' TInt32 = type longint;',
  28205. ' TDWord = type longword;',
  28206. ' TValue = type jsvalue;',
  28207. ' TAliasValue = type TValue;',
  28208. 'var',
  28209. ' p: TPtr;',
  28210. ' a: TAliasValue;',
  28211. 'begin',
  28212. ' p:=typeinfo(tcaption);',
  28213. ' p:=typeinfo(tyesno);',
  28214. ' p:=typeinfo(tletter);',
  28215. ' p:=typeinfo(tfloat);',
  28216. ' p:=typeinfo(tptr);',
  28217. ' p:=typeinfo(tshortint);',
  28218. ' p:=typeinfo(tbyte);',
  28219. ' p:=typeinfo(tsmallint);',
  28220. ' p:=typeinfo(tword);',
  28221. ' p:=typeinfo(tdword);',
  28222. ' p:=typeinfo(tvalue);',
  28223. ' p:=typeinfo(taliasvalue);',
  28224. ' p:=typeinfo(a);',
  28225. '']);
  28226. ConvertProgram;
  28227. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  28228. LinesToStr([ // statements
  28229. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  28230. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  28231. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  28232. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  28233. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  28234. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  28235. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  28236. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  28237. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  28238. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  28239. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  28240. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  28241. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  28242. 'this.p = null;',
  28243. 'this.a = undefined;',
  28244. '']),
  28245. LinesToStr([ // $mod.$main
  28246. '$mod.p = $mod.$rtti["TCaption"];',
  28247. '$mod.p = $mod.$rtti["TYesNo"];',
  28248. '$mod.p = $mod.$rtti["TLetter"];',
  28249. '$mod.p = $mod.$rtti["TFloat"];',
  28250. '$mod.p = $mod.$rtti["TPtr"];',
  28251. '$mod.p = $mod.$rtti["TShortInt"];',
  28252. '$mod.p = $mod.$rtti["TByte"];',
  28253. '$mod.p = $mod.$rtti["TSmallInt"];',
  28254. '$mod.p = $mod.$rtti["TWord"];',
  28255. '$mod.p = $mod.$rtti["TDWord"];',
  28256. '$mod.p = $mod.$rtti["TValue"];',
  28257. '$mod.p = $mod.$rtti["TAliasValue"];',
  28258. '$mod.p = $mod.$rtti["TAliasValue"];',
  28259. '']));
  28260. end;
  28261. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  28262. begin
  28263. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28264. StartProgram(false);
  28265. Add('procedure DoIt;');
  28266. Add('type');
  28267. Add(' integer = longint;');
  28268. Add(' TPoint = record');
  28269. Add(' x,y: integer;');
  28270. Add(' end;');
  28271. Add('var p: pointer;');
  28272. Add('begin');
  28273. Add(' p:=typeinfo(tpoint);');
  28274. Add('end;');
  28275. Add('begin');
  28276. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  28277. ConvertProgram;
  28278. end;
  28279. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  28280. begin
  28281. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28282. StartProgram(false);
  28283. Add([
  28284. '{$modeswitch externalclass}',
  28285. 'type',
  28286. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28287. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  28288. ' TFlag = (up,down);',
  28289. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  28290. ' TFlags = set of TFlag;',
  28291. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  28292. 'var',
  28293. ' ti: TTypeInfo;',
  28294. ' tiInt: TTypeInfoInteger;',
  28295. ' tiEnum: TTypeInfoEnum;',
  28296. ' tiSet: TTypeInfoSet;',
  28297. 'begin',
  28298. ' ti:=typeinfo(string);',
  28299. ' ti:=typeinfo(boolean);',
  28300. ' ti:=typeinfo(char);',
  28301. ' ti:=typeinfo(double);',
  28302. ' tiInt:=typeinfo(shortint);',
  28303. ' tiInt:=typeinfo(byte);',
  28304. ' tiInt:=typeinfo(smallint);',
  28305. ' tiInt:=typeinfo(word);',
  28306. ' tiInt:=typeinfo(longint);',
  28307. ' tiInt:=typeinfo(longword);',
  28308. ' ti:=typeinfo(jsvalue);',
  28309. ' tiEnum:=typeinfo(tflag);',
  28310. ' tiSet:=typeinfo(tflags);']);
  28311. ConvertProgram;
  28312. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  28313. LinesToStr([ // statements
  28314. 'this.TFlag = {',
  28315. ' "0": "up",',
  28316. ' up: 0,',
  28317. ' "1": "down",',
  28318. ' down: 1',
  28319. '};',
  28320. '$mod.$rtti.$Enum("TFlag", {',
  28321. ' minvalue: 0,',
  28322. ' maxvalue: 1,',
  28323. ' ordtype: 1,',
  28324. ' enumtype: this.TFlag',
  28325. '});',
  28326. '$mod.$rtti.$Set("TFlags", {',
  28327. ' comptype: $mod.$rtti["TFlag"]',
  28328. '});',
  28329. 'this.ti = null;',
  28330. 'this.tiInt = null;',
  28331. 'this.tiEnum = null;',
  28332. 'this.tiSet = null;',
  28333. '']),
  28334. LinesToStr([ // $mod.$main
  28335. '$mod.ti = rtl.string;',
  28336. '$mod.ti = rtl.boolean;',
  28337. '$mod.ti = rtl.char;',
  28338. '$mod.ti = rtl.double;',
  28339. '$mod.tiInt = rtl.shortint;',
  28340. '$mod.tiInt = rtl.byte;',
  28341. '$mod.tiInt = rtl.smallint;',
  28342. '$mod.tiInt = rtl.word;',
  28343. '$mod.tiInt = rtl.longint;',
  28344. '$mod.tiInt = rtl.longword;',
  28345. '$mod.ti = rtl.jsvalue;',
  28346. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  28347. '$mod.tiSet = $mod.$rtti["TFlags"];',
  28348. '']));
  28349. end;
  28350. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  28351. begin
  28352. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28353. StartProgram(false);
  28354. Add('{$modeswitch externalclass}');
  28355. Add('type');
  28356. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  28357. Add(' TStaticArr = array[boolean] of string;');
  28358. Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
  28359. Add(' TDynArr = array of string;');
  28360. Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
  28361. Add(' TProc = procedure;');
  28362. Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
  28363. Add(' TMethod = procedure of object;');
  28364. Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
  28365. Add('var');
  28366. Add(' StaticArray: TStaticArr;');
  28367. Add(' tiStaticArray: TTypeInfoStaticArray;');
  28368. Add(' DynArray: TDynArr;');
  28369. Add(' tiDynArray: TTypeInfoDynArray;');
  28370. Add(' ProcVar: TProc;');
  28371. Add(' tiProcVar: TTypeInfoProcVar;');
  28372. Add(' MethodVar: TMethod;');
  28373. Add(' tiMethodVar: TTypeInfoMethodVar;');
  28374. Add('begin');
  28375. Add(' tiStaticArray:=typeinfo(StaticArray);');
  28376. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  28377. Add(' tiDynArray:=typeinfo(DynArray);');
  28378. Add(' tiDynArray:=typeinfo(TDynArr);');
  28379. Add(' tiProcVar:=typeinfo(ProcVar);');
  28380. Add(' tiProcVar:=typeinfo(TProc);');
  28381. Add(' tiMethodVar:=typeinfo(MethodVar);');
  28382. Add(' tiMethodVar:=typeinfo(TMethod);');
  28383. ConvertProgram;
  28384. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  28385. LinesToStr([ // statements
  28386. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  28387. ' dims: [2],',
  28388. ' eltype: rtl.string',
  28389. '});',
  28390. '$mod.$rtti.$DynArray("TDynArr", {',
  28391. ' eltype: rtl.string',
  28392. '});',
  28393. '$mod.$rtti.$ProcVar("TProc", {',
  28394. ' procsig: rtl.newTIProcSig(null)',
  28395. '});',
  28396. '$mod.$rtti.$MethodVar("TMethod", {',
  28397. ' procsig: rtl.newTIProcSig(null),',
  28398. ' methodkind: 0',
  28399. '});',
  28400. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  28401. 'this.tiStaticArray = null;',
  28402. 'this.DynArray = [];',
  28403. 'this.tiDynArray = null;',
  28404. 'this.ProcVar = null;',
  28405. 'this.tiProcVar = null;',
  28406. 'this.MethodVar = null;',
  28407. 'this.tiMethodVar = null;',
  28408. '']),
  28409. LinesToStr([ // $mod.$main
  28410. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  28411. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  28412. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  28413. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  28414. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  28415. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  28416. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  28417. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  28418. '']));
  28419. end;
  28420. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  28421. begin
  28422. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28423. StartProgram(false);
  28424. Add('{$modeswitch externalclass}');
  28425. Add('type');
  28426. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  28427. Add(' TRec = record end;');
  28428. Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
  28429. // ToDo: ^PRec
  28430. Add(' TObject = class end;');
  28431. Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
  28432. Add(' TClass = class of tobject;');
  28433. Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
  28434. Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
  28435. Add('var');
  28436. Add(' Rec: trec;');
  28437. Add(' tiRecord: ttypeinforecord;');
  28438. Add(' Obj: tobject;');
  28439. Add(' tiClass: ttypeinfoclass;');
  28440. Add(' aClass: tclass;');
  28441. Add(' tiClassRef: ttypeinfoclassref;');
  28442. // ToDo: ^PRec
  28443. Add(' tiPointer: ttypeinfopointer;');
  28444. Add('begin');
  28445. Add(' tirecord:=typeinfo(trec);');
  28446. Add(' tirecord:=typeinfo(trec);');
  28447. Add(' ticlass:=typeinfo(obj);');
  28448. Add(' ticlass:=typeinfo(tobject);');
  28449. Add(' ticlass:=typeinfo(aclass);');
  28450. Add(' ticlassref:=typeinfo(tclass);');
  28451. ConvertProgram;
  28452. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  28453. LinesToStr([ // statements
  28454. 'rtl.recNewT($mod, "TRec", function () {',
  28455. ' this.$eq = function (b) {',
  28456. ' return true;',
  28457. ' };',
  28458. ' this.$assign = function (s) {',
  28459. ' return this;',
  28460. ' };',
  28461. ' $mod.$rtti.$Record("TRec", {});',
  28462. '});',
  28463. 'rtl.createClass($mod, "TObject", null, function () {',
  28464. ' this.$init = function () {',
  28465. ' };',
  28466. ' this.$final = function () {',
  28467. ' };',
  28468. '});',
  28469. '$mod.$rtti.$ClassRef("TClass", {',
  28470. ' instancetype: $mod.$rtti["TObject"]',
  28471. '});',
  28472. 'this.Rec = $mod.TRec.$new();',
  28473. 'this.tiRecord = null;',
  28474. 'this.Obj = null;',
  28475. 'this.tiClass = null;',
  28476. 'this.aClass = null;',
  28477. 'this.tiClassRef = null;',
  28478. 'this.tiPointer = null;',
  28479. '']),
  28480. LinesToStr([ // $mod.$main
  28481. '$mod.tiRecord = $mod.$rtti["TRec"];',
  28482. '$mod.tiRecord = $mod.$rtti["TRec"];',
  28483. '$mod.tiClass = $mod.Obj.$rtti;',
  28484. '$mod.tiClass = $mod.$rtti["TObject"];',
  28485. '$mod.tiClass = $mod.aClass.$rtti;',
  28486. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  28487. '']));
  28488. end;
  28489. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  28490. begin
  28491. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28492. StartProgram(false);
  28493. Add([
  28494. '{$modeswitch externalclass}',
  28495. 'type',
  28496. ' TClass = class of tobject;',
  28497. ' TObject = class',
  28498. ' function MyClass: TClass;',
  28499. ' class function ClassType: TClass;',
  28500. ' end;',
  28501. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28502. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  28503. 'function TObject.MyClass: TClass;',
  28504. 'var t: TTypeInfoClass;',
  28505. 'begin',
  28506. ' t:=TypeInfo(Self);',
  28507. ' t:=TypeInfo(Result);',
  28508. ' t:=TypeInfo(TObject);',
  28509. 'end;',
  28510. 'class function TObject.ClassType: TClass;',
  28511. 'var t: TTypeInfoClass;',
  28512. 'begin',
  28513. ' t:=TypeInfo(Self);',
  28514. ' t:=TypeInfo(Result);',
  28515. 'end;',
  28516. 'var',
  28517. ' Obj: TObject;',
  28518. ' t: TTypeInfoClass;',
  28519. 'begin',
  28520. ' t:=TypeInfo(TObject.ClassType);',
  28521. ' t:=TypeInfo(Obj.ClassType);',
  28522. ' t:=TypeInfo(Obj.MyClass);',
  28523. '']);
  28524. ConvertProgram;
  28525. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  28526. LinesToStr([ // statements
  28527. '$mod.$rtti.$Class("TObject");',
  28528. '$mod.$rtti.$ClassRef("TClass", {',
  28529. ' instancetype: $mod.$rtti["TObject"]',
  28530. '});',
  28531. 'rtl.createClass($mod, "TObject", null, function () {',
  28532. ' this.$init = function () {',
  28533. ' };',
  28534. ' this.$final = function () {',
  28535. ' };',
  28536. ' this.MyClass = function () {',
  28537. ' var Result = null;',
  28538. ' var t = null;',
  28539. ' t = this.$rtti;',
  28540. ' t = Result.$rtti;',
  28541. ' t = $mod.$rtti["TObject"];',
  28542. ' return Result;',
  28543. ' };',
  28544. ' this.ClassType = function () {',
  28545. ' var Result = null;',
  28546. ' var t = null;',
  28547. ' t = this.$rtti;',
  28548. ' t = Result.$rtti;',
  28549. ' return Result;',
  28550. ' };',
  28551. '});',
  28552. 'this.Obj = null;',
  28553. 'this.t = null;',
  28554. '']),
  28555. LinesToStr([ // $mod.$main
  28556. '$mod.t = $mod.TObject.ClassType().$rtti;',
  28557. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  28558. '$mod.t = $mod.Obj.MyClass().$rtti;',
  28559. '']));
  28560. end;
  28561. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  28562. begin
  28563. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28564. AddModuleWithIntfImplSrc('typinfo.pas',
  28565. LinesToStr([
  28566. '{$modeswitch externalclass}',
  28567. 'type',
  28568. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28569. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  28570. '']),
  28571. '');
  28572. AddModuleWithIntfImplSrc('unit2.pas',
  28573. LinesToStr([
  28574. 'uses typinfo;',
  28575. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  28576. 'procedure DoPtr(p: PTypeInfo);',
  28577. 'procedure DoInfo(t: TTypeInfo);',
  28578. 'procedure DoInt(t: TTypeInfoInteger);',
  28579. '']),
  28580. LinesToStr([
  28581. 'procedure DoPtr(p: PTypeInfo);',
  28582. 'begin end;',
  28583. 'procedure DoInfo(t: TTypeInfo);',
  28584. 'begin end;',
  28585. 'procedure DoInt(t: TTypeInfoInteger);',
  28586. 'begin end;',
  28587. '']));
  28588. StartUnit(true);
  28589. Add([
  28590. 'interface',
  28591. 'uses unit2;', // does not use unit typinfo
  28592. 'implementation',
  28593. 'var',
  28594. ' i: byte;',
  28595. ' p: pointer;',
  28596. ' t: PTypeInfo;',
  28597. 'initialization',
  28598. ' p:=typeinfo(i);',
  28599. ' t:=typeinfo(i);',
  28600. ' if p=t then ;',
  28601. ' if p=typeinfo(i) then ;',
  28602. ' if typeinfo(i)=p then ;',
  28603. ' if t=typeinfo(i) then ;',
  28604. ' if typeinfo(i)=t then ;',
  28605. ' DoPtr(p);',
  28606. ' DoPtr(t);',
  28607. ' DoPtr(typeinfo(i));',
  28608. ' DoInfo(p);',
  28609. ' DoInfo(t);',
  28610. ' DoInfo(typeinfo(i));',
  28611. ' DoInt(typeinfo(i));',
  28612. '']);
  28613. ConvertUnit;
  28614. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  28615. LinesToStr([ // statements
  28616. 'var $impl = $mod.$impl;',
  28617. '']),
  28618. LinesToStr([ // this.$init
  28619. '$impl.p = rtl.byte;',
  28620. '$impl.t = rtl.byte;',
  28621. 'if ($impl.p === $impl.t) ;',
  28622. 'if ($impl.p === rtl.byte) ;',
  28623. 'if (rtl.byte === $impl.p) ;',
  28624. 'if ($impl.t === rtl.byte) ;',
  28625. 'if (rtl.byte === $impl.t) ;',
  28626. 'pas.unit2.DoPtr($impl.p);',
  28627. 'pas.unit2.DoPtr($impl.t);',
  28628. 'pas.unit2.DoPtr(rtl.byte);',
  28629. 'pas.unit2.DoInfo($impl.p);',
  28630. 'pas.unit2.DoInfo($impl.t);',
  28631. 'pas.unit2.DoInfo(rtl.byte);',
  28632. 'pas.unit2.DoInt(rtl.byte);',
  28633. '']),
  28634. LinesToStr([ // implementation
  28635. '$impl.i = 0;',
  28636. '$impl.p = null;',
  28637. '$impl.t = null;',
  28638. '']) );
  28639. end;
  28640. procedure TTestModule.TestRTTI_Interface_Corba;
  28641. begin
  28642. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28643. StartProgram(false);
  28644. Add([
  28645. '{$interfaces corba}',
  28646. '{$modeswitch externalclass}',
  28647. 'type',
  28648. ' IUnknown = interface',
  28649. ' end;',
  28650. ' IBird = interface',
  28651. ' function GetItem: longint;',
  28652. ' procedure SetItem(Value: longint);',
  28653. ' property Item: longint read GetItem write SetItem;',
  28654. ' end;',
  28655. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28656. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  28657. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  28658. 'var',
  28659. ' i: IBird;',
  28660. ' t: TTypeInfoInterface;',
  28661. 'begin',
  28662. ' t:=TypeInfo(IBird);',
  28663. ' t:=TypeInfo(i);',
  28664. ' DoIt(t);',
  28665. ' DoIt(TypeInfo(IBird));',
  28666. '']);
  28667. ConvertProgram;
  28668. CheckSource('TestRTTI_Interface_Corba',
  28669. LinesToStr([ // statements
  28670. 'rtl.createInterface(',
  28671. ' $mod,',
  28672. ' "IUnknown",',
  28673. ' "{B92D5841-758A-322B-B800-000000000000}",',
  28674. ' [],',
  28675. ' null,',
  28676. ' function () {',
  28677. ' }',
  28678. ');',
  28679. 'rtl.createInterface(',
  28680. ' $mod,',
  28681. ' "IBird",',
  28682. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  28683. ' ["GetItem", "SetItem"],',
  28684. ' null,',
  28685. ' function () {',
  28686. ' var $r = this.$rtti;',
  28687. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28688. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  28689. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  28690. ' }',
  28691. ');',
  28692. 'this.DoIt = function (t) {',
  28693. '}; ',
  28694. 'this.i = null;',
  28695. 'this.t = null;',
  28696. '']),
  28697. LinesToStr([ // $mod.$main
  28698. '$mod.t = $mod.$rtti["IBird"];',
  28699. '$mod.t = $mod.i.$rtti;',
  28700. '$mod.DoIt($mod.t);',
  28701. '$mod.DoIt($mod.$rtti["IBird"]);',
  28702. '']));
  28703. end;
  28704. procedure TTestModule.TestRTTI_Interface_COM;
  28705. begin
  28706. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28707. StartProgram(false);
  28708. Add([
  28709. '{$interfaces com}',
  28710. '{$modeswitch externalclass}',
  28711. 'type',
  28712. ' TGuid = record end;',
  28713. ' integer = longint;',
  28714. ' IUnknown = interface',
  28715. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  28716. ' function _AddRef: Integer;',
  28717. ' function _Release: Integer;',
  28718. ' end;',
  28719. ' IBird = interface',
  28720. ' function GetItem: longint;',
  28721. ' procedure SetItem(Value: longint);',
  28722. ' property Item: longint read GetItem write SetItem;',
  28723. ' end;',
  28724. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28725. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  28726. 'var',
  28727. ' i: IBird;',
  28728. ' t: TTypeInfoInterface;',
  28729. 'begin',
  28730. ' t:=TypeInfo(IBird);',
  28731. ' t:=TypeInfo(i);',
  28732. '']);
  28733. ConvertProgram;
  28734. CheckSource('TestRTTI_Interface_COM',
  28735. LinesToStr([ // statements
  28736. 'rtl.recNewT($mod, "TGuid", function () {',
  28737. ' this.$eq = function (b) {',
  28738. ' return true;',
  28739. ' };',
  28740. ' this.$assign = function (s) {',
  28741. ' return this;',
  28742. ' };',
  28743. ' $mod.$rtti.$Record("TGuid", {});',
  28744. '});',
  28745. 'rtl.createInterface(',
  28746. ' $mod,',
  28747. ' "IUnknown",',
  28748. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  28749. ' ["QueryInterface", "_AddRef", "_Release"],',
  28750. ' null,',
  28751. ' function () {',
  28752. ' this.$kind = "com";',
  28753. ' var $r = this.$rtti;',
  28754. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  28755. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  28756. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  28757. ' }',
  28758. ');',
  28759. 'rtl.createInterface(',
  28760. ' $mod,',
  28761. ' "IBird",',
  28762. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  28763. ' ["GetItem", "SetItem"],',
  28764. ' $mod.IUnknown,',
  28765. ' function () {',
  28766. ' var $r = this.$rtti;',
  28767. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28768. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  28769. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  28770. ' }',
  28771. ');',
  28772. 'this.i = null;',
  28773. 'this.t = null;',
  28774. '']),
  28775. LinesToStr([ // $mod.$main
  28776. '$mod.t = $mod.$rtti["IBird"];',
  28777. '$mod.t = $mod.i.$rtti;',
  28778. '']));
  28779. end;
  28780. procedure TTestModule.TestRTTI_ClassHelper;
  28781. begin
  28782. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28783. StartProgram(false);
  28784. Add([
  28785. '{$interfaces com}',
  28786. '{$modeswitch externalclass}',
  28787. 'type',
  28788. ' TObject = class',
  28789. ' end;',
  28790. ' THelper = class helper for TObject',
  28791. ' published',
  28792. ' function GetItem: longint;',
  28793. ' property Item: longint read GetItem;',
  28794. ' end;',
  28795. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28796. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  28797. 'function THelper.GetItem: longint;',
  28798. 'begin',
  28799. 'end;',
  28800. 'var',
  28801. ' t: TTypeInfoHelper;',
  28802. 'begin',
  28803. ' t:=TypeInfo(THelper);',
  28804. '']);
  28805. ConvertProgram;
  28806. CheckSource('TestRTTI_ClassHelper',
  28807. LinesToStr([ // statements
  28808. 'rtl.createClass($mod, "TObject", null, function () {',
  28809. ' this.$init = function () {',
  28810. ' };',
  28811. ' this.$final = function () {',
  28812. ' };',
  28813. '});',
  28814. 'rtl.createHelper($mod, "THelper", null, function () {',
  28815. ' this.GetItem = function () {',
  28816. ' var Result = 0;',
  28817. ' return Result;',
  28818. ' };',
  28819. ' var $r = this.$rtti;',
  28820. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28821. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  28822. '});',
  28823. 'this.t = null;',
  28824. '']),
  28825. LinesToStr([ // $mod.$main
  28826. '$mod.t = $mod.$rtti["THelper"];',
  28827. '']));
  28828. end;
  28829. procedure TTestModule.TestResourcestringProgram;
  28830. begin
  28831. StartProgram(false);
  28832. Add([
  28833. 'const Bar = ''bar'';',
  28834. 'resourcestring',
  28835. ' Red = ''red'';',
  28836. ' Foobar = ''fOo''+bar;',
  28837. 'var s: string;',
  28838. ' c: char;',
  28839. 'begin',
  28840. ' s:=red;',
  28841. ' s:=test1.red;',
  28842. ' c:=red[1];',
  28843. ' c:=test1.red[2];',
  28844. ' if red=foobar then ;',
  28845. ' if red[3]=red[4] then ;']);
  28846. ConvertProgram;
  28847. CheckSource('TestResourcestringProgram',
  28848. LinesToStr([ // statements
  28849. 'this.Bar = "bar";',
  28850. 'this.s = "";',
  28851. 'this.c = "";',
  28852. '$mod.$resourcestrings = {',
  28853. ' Red: {',
  28854. ' org: "red"',
  28855. ' },',
  28856. ' Foobar: {',
  28857. ' org: "fOobar"',
  28858. ' }',
  28859. '};',
  28860. '']),
  28861. LinesToStr([ // $mod.$main
  28862. '$mod.s = rtl.getResStr(pas.program, "Red");',
  28863. '$mod.s = rtl.getResStr(pas.program, "Red");',
  28864. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  28865. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  28866. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  28867. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  28868. '']));
  28869. end;
  28870. procedure TTestModule.TestResourcestringUnit;
  28871. begin
  28872. StartUnit(false);
  28873. Add([
  28874. 'interface',
  28875. 'const Red = ''rEd'';',
  28876. 'resourcestring',
  28877. ' Blue = ''blue'';',
  28878. ' NotRed = ''not''+Red;',
  28879. 'var s: string;',
  28880. 'implementation',
  28881. 'resourcestring',
  28882. ' ImplGreen = ''green'';',
  28883. 'initialization',
  28884. ' s:=blue+ImplGreen;',
  28885. ' s:=test1.blue+test1.implgreen;',
  28886. ' s:=blue[1]+implgreen[2];']);
  28887. ConvertUnit;
  28888. CheckSource('TestResourcestringUnit',
  28889. LinesToStr([ // statements
  28890. 'this.Red = "rEd";',
  28891. 'this.s = "";',
  28892. '$mod.$resourcestrings = {',
  28893. ' Blue: {',
  28894. ' org: "blue"',
  28895. ' },',
  28896. ' NotRed: {',
  28897. ' org: "notrEd"',
  28898. ' },',
  28899. ' ImplGreen: {',
  28900. ' org: "green"',
  28901. ' }',
  28902. '};',
  28903. '']),
  28904. LinesToStr([ // $mod.$main
  28905. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28906. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28907. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  28908. '']));
  28909. end;
  28910. procedure TTestModule.TestResourcestringImplementation;
  28911. begin
  28912. StartUnit(false);
  28913. Add([
  28914. 'interface',
  28915. 'implementation',
  28916. 'resourcestring',
  28917. ' ImplRed = ''red'';']);
  28918. ConvertUnit;
  28919. CheckSource('TestResourcestringImplementation',
  28920. LinesToStr([ // intf statements
  28921. 'var $impl = $mod.$impl;']),
  28922. LinesToStr([ // $mod.$init
  28923. '']),
  28924. LinesToStr([ // impl statements
  28925. '$mod.$resourcestrings = {',
  28926. ' ImplRed: {',
  28927. ' org: "red"',
  28928. ' }',
  28929. '};',
  28930. '']));
  28931. end;
  28932. procedure TTestModule.TestAtributes_Ignore;
  28933. begin
  28934. StartProgram(false);
  28935. Add([
  28936. '{$modeswitch ignoreattributes}',
  28937. 'type',
  28938. ' [custom1, custom2(1+3,''foo'')] [mod1.custom3]',
  28939. ' TObject = class',
  28940. ' [custom5()] FS: string;',
  28941. ' [customProp] property S: string read FS;',
  28942. ' end;',
  28943. 'var',
  28944. ' [custom6]',
  28945. ' o: TObject;',
  28946. 'begin',
  28947. '']);
  28948. ConvertProgram;
  28949. CheckSource('TestAtributes_Ignore',
  28950. LinesToStr([ // statements
  28951. 'rtl.createClass($mod, "TObject", null, function () {',
  28952. ' this.$init = function () {',
  28953. ' this.FS = "";',
  28954. ' };',
  28955. ' this.$final = function () {',
  28956. ' };',
  28957. '});',
  28958. 'this.o = null;',
  28959. '']),
  28960. LinesToStr([ // $mod.$main
  28961. '']));
  28962. end;
  28963. procedure TTestModule.TestAssert;
  28964. begin
  28965. StartProgram(false);
  28966. Add([
  28967. 'procedure DoIt;',
  28968. 'var',
  28969. ' b: boolean;',
  28970. ' s: string;',
  28971. 'begin',
  28972. ' {$Assertions on}',
  28973. ' Assert(b);',
  28974. 'end;',
  28975. 'begin',
  28976. ' DoIt;',
  28977. '']);
  28978. ConvertProgram;
  28979. CheckSource('TestAssert',
  28980. LinesToStr([ // statements
  28981. 'this.DoIt = function () {',
  28982. ' var b = false;',
  28983. ' var s = "";',
  28984. ' if (!b) throw "assert failed";',
  28985. '};',
  28986. '']),
  28987. LinesToStr([ // $mod.$main
  28988. '$mod.DoIt();',
  28989. '']));
  28990. end;
  28991. procedure TTestModule.TestAssert_SysUtils;
  28992. begin
  28993. AddModuleWithIntfImplSrc('SysUtils.pas',
  28994. LinesToStr([
  28995. 'type',
  28996. ' TObject = class',
  28997. ' constructor Create;',
  28998. ' end;',
  28999. ' EAssertionFailed = class',
  29000. ' constructor Create(s: string);',
  29001. ' end;',
  29002. '']),
  29003. LinesToStr([
  29004. 'constructor TObject.Create;',
  29005. 'begin end;',
  29006. 'constructor EAssertionFailed.Create(s: string);',
  29007. 'begin end;',
  29008. '']) );
  29009. StartProgram(true);
  29010. Add([
  29011. 'uses sysutils;',
  29012. 'procedure DoIt;',
  29013. 'var',
  29014. ' b: boolean;',
  29015. ' s: string;',
  29016. 'begin',
  29017. ' {$Assertions on}',
  29018. ' Assert(b);',
  29019. ' Assert(b,''msg'');',
  29020. 'end;',
  29021. 'begin',
  29022. ' DoIt;',
  29023. '']);
  29024. ConvertProgram;
  29025. CheckSource('TestAssert_SysUtils',
  29026. LinesToStr([ // statements
  29027. 'this.DoIt = function () {',
  29028. ' var b = false;',
  29029. ' var s = "";',
  29030. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  29031. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  29032. '};',
  29033. '']),
  29034. LinesToStr([ // $mod.$main
  29035. '$mod.DoIt();',
  29036. '']));
  29037. end;
  29038. procedure TTestModule.TestObjectChecks;
  29039. begin
  29040. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  29041. StartProgram(false);
  29042. Add([
  29043. 'type',
  29044. ' TObject = class',
  29045. ' procedure DoIt;',
  29046. ' end;',
  29047. ' TClass = class of tobject;',
  29048. ' TBird = class',
  29049. ' end;',
  29050. ' TBirdClass = class of TBird;',
  29051. 'var',
  29052. ' o : TObject;',
  29053. ' c: TClass;',
  29054. ' b: TBird;',
  29055. ' bc: TBirdClass;',
  29056. 'procedure TObject.DoIt;',
  29057. 'begin',
  29058. ' b:=TBird(o);',
  29059. 'end;',
  29060. 'begin',
  29061. ' o.DoIt;',
  29062. ' b:=TBird(o);',
  29063. ' bc:=TBirdClass(c);',
  29064. '']);
  29065. ConvertProgram;
  29066. CheckSource('TestCheckMethodCall',
  29067. LinesToStr([ // statements
  29068. 'rtl.createClass($mod, "TObject", null, function () {',
  29069. ' this.$init = function () {',
  29070. ' };',
  29071. ' this.$final = function () {',
  29072. ' };',
  29073. ' this.DoIt = function () {',
  29074. ' rtl.checkMethodCall(this,$mod.TObject);',
  29075. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  29076. ' };',
  29077. '});',
  29078. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  29079. '});',
  29080. 'this.o = null;',
  29081. 'this.c = null;',
  29082. 'this.b = null;',
  29083. 'this.bc = null;',
  29084. '']),
  29085. LinesToStr([ // $mod.$main
  29086. '$mod.o.DoIt();',
  29087. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  29088. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  29089. '']));
  29090. end;
  29091. procedure TTestModule.TestRangeChecks_AssignInt;
  29092. begin
  29093. Scanner.Options:=Scanner.Options+[po_CAssignments];
  29094. StartProgram(false);
  29095. Add([
  29096. '{$R+}',
  29097. 'var',
  29098. ' b: byte = 2;',
  29099. ' w: word = 3;',
  29100. 'procedure DoIt(p: byte);',
  29101. 'begin',
  29102. ' b:=w;',
  29103. ' b+=w;',
  29104. ' b:=1;',
  29105. 'end;',
  29106. '{$R-}',
  29107. 'procedure DoSome;',
  29108. 'begin',
  29109. ' DoIt(w);',
  29110. ' b:=w;',
  29111. ' b:=2;',
  29112. 'end;',
  29113. 'begin',
  29114. '{$R+}',
  29115. '']);
  29116. ConvertProgram;
  29117. CheckSource('TestRangeChecks_AssignInt',
  29118. LinesToStr([ // statements
  29119. 'this.b = 2;',
  29120. 'this.w = 3;',
  29121. 'this.DoIt = function (p) {',
  29122. ' rtl.rc(p, 0, 255);',
  29123. ' $mod.b = rtl.rc($mod.w,0,255);',
  29124. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  29125. ' $mod.b = 1;',
  29126. '};',
  29127. 'this.DoSome = function () {',
  29128. ' $mod.DoIt($mod.w);',
  29129. ' $mod.b = $mod.w;',
  29130. ' $mod.b = 2;',
  29131. '};',
  29132. '']),
  29133. LinesToStr([ // $mod.$main
  29134. '']));
  29135. end;
  29136. procedure TTestModule.TestRangeChecks_AssignIntRange;
  29137. begin
  29138. Scanner.Options:=Scanner.Options+[po_CAssignments];
  29139. StartProgram(false);
  29140. Add([
  29141. '{$R+}',
  29142. 'type Ten = 1..10;',
  29143. 'var',
  29144. ' b: Ten = 2;',
  29145. ' w: Ten = 3;',
  29146. 'procedure DoIt(p: Ten);',
  29147. 'begin',
  29148. ' b:=w;',
  29149. ' b+=w;',
  29150. ' b:=1;',
  29151. 'end;',
  29152. '{$R-}',
  29153. 'procedure DoSome;',
  29154. 'begin',
  29155. ' DoIt(w);',
  29156. ' b:=w;',
  29157. ' b:=2;',
  29158. 'end;',
  29159. 'begin',
  29160. '{$R+}',
  29161. '']);
  29162. ConvertProgram;
  29163. CheckSource('TestRangeChecks_AssignIntRange',
  29164. LinesToStr([ // statements
  29165. 'this.b = 2;',
  29166. 'this.w = 3;',
  29167. 'this.DoIt = function (p) {',
  29168. ' rtl.rc(p, 1, 10);',
  29169. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  29170. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  29171. ' $mod.b = 1;',
  29172. '};',
  29173. 'this.DoSome = function () {',
  29174. ' $mod.DoIt($mod.w);',
  29175. ' $mod.b = $mod.w;',
  29176. ' $mod.b = 2;',
  29177. '};',
  29178. '']),
  29179. LinesToStr([ // $mod.$main
  29180. '']));
  29181. end;
  29182. procedure TTestModule.TestRangeChecks_AssignEnum;
  29183. begin
  29184. StartProgram(false);
  29185. Add([
  29186. '{$R+}',
  29187. 'type TEnum = (red,green);',
  29188. 'var',
  29189. ' e: TEnum = red;',
  29190. 'procedure DoIt(p: TEnum);',
  29191. 'begin',
  29192. ' e:=p;',
  29193. ' p:=TEnum(0);',
  29194. ' p:=succ(e);',
  29195. 'end;',
  29196. '{$R-}',
  29197. 'procedure DoSome;',
  29198. 'begin',
  29199. ' DoIt(e);',
  29200. ' e:=TEnum(1);',
  29201. ' e:=pred(e);',
  29202. 'end;',
  29203. 'begin',
  29204. '{$R+}',
  29205. '']);
  29206. ConvertProgram;
  29207. CheckSource('TestRangeChecks_AssignEnum',
  29208. LinesToStr([ // statements
  29209. 'this.TEnum = {',
  29210. ' "0": "red",',
  29211. ' red: 0,',
  29212. ' "1": "green",',
  29213. ' green: 1',
  29214. '};',
  29215. 'this.e = $mod.TEnum.red;',
  29216. 'this.DoIt = function (p) {',
  29217. ' rtl.rc(p, 0, 1);',
  29218. ' $mod.e = rtl.rc(p, 0, 1);',
  29219. ' p = 0;',
  29220. ' p = rtl.rc($mod.e + 1, 0, 1);',
  29221. '};',
  29222. 'this.DoSome = function () {',
  29223. ' $mod.DoIt($mod.e);',
  29224. ' $mod.e = 1;',
  29225. ' $mod.e = $mod.e - 1;',
  29226. '};',
  29227. '']),
  29228. LinesToStr([ // $mod.$main
  29229. '']));
  29230. end;
  29231. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  29232. begin
  29233. StartProgram(false);
  29234. Add([
  29235. '{$R+}',
  29236. 'type',
  29237. ' TEnum = (red,green);',
  29238. ' TEnumRg = red..green;',
  29239. 'var',
  29240. ' e: TEnumRg = red;',
  29241. 'procedure DoIt(p: TEnumRg);',
  29242. 'begin',
  29243. ' e:=p;',
  29244. ' p:=TEnumRg(0);',
  29245. ' p:=succ(e);',
  29246. 'end;',
  29247. '{$R-}',
  29248. 'procedure DoSome;',
  29249. 'begin',
  29250. ' DoIt(e);',
  29251. ' e:=TEnum(1);',
  29252. ' e:=pred(e);',
  29253. 'end;',
  29254. 'begin',
  29255. '{$R+}',
  29256. '']);
  29257. ConvertProgram;
  29258. CheckSource('TestRangeChecks_AssignEnumRange',
  29259. LinesToStr([ // statements
  29260. 'this.TEnum = {',
  29261. ' "0": "red",',
  29262. ' red: 0,',
  29263. ' "1": "green",',
  29264. ' green: 1',
  29265. '};',
  29266. 'this.e = $mod.TEnum.red;',
  29267. 'this.DoIt = function (p) {',
  29268. ' rtl.rc(p, 0, 1);',
  29269. ' $mod.e = rtl.rc(p, 0, 1);',
  29270. ' p = 0;',
  29271. ' p = rtl.rc($mod.e + 1, 0, 1);',
  29272. '};',
  29273. 'this.DoSome = function () {',
  29274. ' $mod.DoIt($mod.e);',
  29275. ' $mod.e = 1;',
  29276. ' $mod.e = $mod.e - 1;',
  29277. '};',
  29278. '']),
  29279. LinesToStr([ // $mod.$main
  29280. '']));
  29281. end;
  29282. procedure TTestModule.TestRangeChecks_AssignChar;
  29283. begin
  29284. StartProgram(false);
  29285. Add([
  29286. '{$R+}',
  29287. 'type',
  29288. ' TLetter = char;',
  29289. 'var',
  29290. ' b: TLetter = ''2'';',
  29291. ' w: TLetter = ''3'';',
  29292. 'procedure DoIt(p: TLetter);',
  29293. 'begin',
  29294. ' b:=w;',
  29295. ' b:=''1'';',
  29296. 'end;',
  29297. '{$R-}',
  29298. 'procedure DoSome;',
  29299. 'begin',
  29300. ' DoIt(w);',
  29301. ' b:=w;',
  29302. ' b:=''2'';',
  29303. 'end;',
  29304. 'begin',
  29305. '{$R+}',
  29306. '']);
  29307. ConvertProgram;
  29308. CheckSource('TestRangeChecks_AssignChar',
  29309. LinesToStr([ // statements
  29310. 'this.b = "2";',
  29311. 'this.w = "3";',
  29312. 'this.DoIt = function (p) {',
  29313. ' rtl.rcc(p, 0, 65535);',
  29314. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  29315. ' $mod.b = "1";',
  29316. '};',
  29317. 'this.DoSome = function () {',
  29318. ' $mod.DoIt($mod.w);',
  29319. ' $mod.b = $mod.w;',
  29320. ' $mod.b = "2";',
  29321. '};',
  29322. '']),
  29323. LinesToStr([ // $mod.$main
  29324. '']));
  29325. end;
  29326. procedure TTestModule.TestRangeChecks_AssignCharRange;
  29327. begin
  29328. StartProgram(false);
  29329. Add([
  29330. '{$R+}',
  29331. 'type TDigit = ''0''..''9'';',
  29332. 'var',
  29333. ' b: TDigit = ''2'';',
  29334. ' w: TDigit = ''3'';',
  29335. 'procedure DoIt(p: TDigit);',
  29336. 'begin',
  29337. ' b:=w;',
  29338. ' b:=''1'';',
  29339. 'end;',
  29340. '{$R-}',
  29341. 'procedure DoSome;',
  29342. 'begin',
  29343. ' DoIt(w);',
  29344. ' b:=w;',
  29345. ' b:=''2'';',
  29346. 'end;',
  29347. 'begin',
  29348. '{$R+}',
  29349. '']);
  29350. ConvertProgram;
  29351. CheckSource('TestRangeChecks_AssignCharRange',
  29352. LinesToStr([ // statements
  29353. 'this.b = "2";',
  29354. 'this.w = "3";',
  29355. 'this.DoIt = function (p) {',
  29356. ' rtl.rcc(p, 48, 57);',
  29357. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  29358. ' $mod.b = "1";',
  29359. '};',
  29360. 'this.DoSome = function () {',
  29361. ' $mod.DoIt($mod.w);',
  29362. ' $mod.b = $mod.w;',
  29363. ' $mod.b = "2";',
  29364. '};',
  29365. '']),
  29366. LinesToStr([ // $mod.$main
  29367. '']));
  29368. end;
  29369. procedure TTestModule.TestRangeChecks_ArrayIndex;
  29370. begin
  29371. StartProgram(false);
  29372. Add([
  29373. '{$R+}',
  29374. 'type',
  29375. ' Ten = 1..10;',
  29376. ' TArr = array of Ten;',
  29377. ' TArrArr = array of TArr;',
  29378. ' TArrByte = array[byte] of Ten;',
  29379. ' TArrChar = array[''0''..''9''] of Ten;',
  29380. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  29381. ' TObject = class',
  29382. ' A: TArr;',
  29383. ' end;',
  29384. 'procedure DoIt;',
  29385. 'var',
  29386. ' Arr: TArr;',
  29387. ' ArrArr: TArrArr;',
  29388. ' ArrByte: TArrByte;',
  29389. ' ArrChar: TArrChar;',
  29390. ' ArrByteChar: TArrByteChar;',
  29391. ' i: Ten;',
  29392. ' c: char;',
  29393. ' o: tobject;',
  29394. 'begin',
  29395. ' i:=Arr[1];',
  29396. ' i:=ArrByteChar[1,''2''];',
  29397. ' Arr[1]:=Arr[1];',
  29398. ' Arr[i]:=Arr[i];',
  29399. ' ArrByte[3]:=ArrByte[3];',
  29400. ' ArrByte[i]:=ArrByte[i];',
  29401. ' ArrChar[''5'']:=ArrChar[''5''];',
  29402. ' ArrChar[c]:=ArrChar[c];',
  29403. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  29404. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  29405. ' o.a[i]:=o.a[i];',
  29406. 'end;',
  29407. 'begin',
  29408. '']);
  29409. ConvertProgram;
  29410. CheckSource('TestRangeChecks_ArrayIndex',
  29411. LinesToStr([ // statements
  29412. 'rtl.createClass($mod, "TObject", null, function () {',
  29413. ' this.$init = function () {',
  29414. ' this.A = [];',
  29415. ' };',
  29416. ' this.$final = function () {',
  29417. ' this.A = undefined;',
  29418. ' };',
  29419. '});',
  29420. 'this.DoIt = function () {',
  29421. ' var Arr = [];',
  29422. ' var ArrArr = [];',
  29423. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  29424. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  29425. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  29426. ' var i = 0;',
  29427. ' var c = "";',
  29428. ' var o = null;',
  29429. ' i = rtl.rc(Arr[1], 1, 10);',
  29430. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  29431. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  29432. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  29433. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  29434. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  29435. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  29436. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  29437. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  29438. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  29439. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  29440. '};',
  29441. '']),
  29442. LinesToStr([ // $mod.$main
  29443. '']));
  29444. end;
  29445. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  29446. begin
  29447. StartProgram(false);
  29448. Add([
  29449. '{$R+}',
  29450. 'type',
  29451. ' Ten = 1..10;',
  29452. ' TRec = record x: Ten end;',
  29453. ' TArr = array of TRec;',
  29454. ' TArrArr = array of TArr;',
  29455. ' TObject = class',
  29456. ' A: TArr;',
  29457. ' end;',
  29458. 'procedure DoIt;',
  29459. 'var',
  29460. ' Arr: TArr;',
  29461. ' ArrArr: TArrArr;',
  29462. ' i: Ten;',
  29463. ' o: tobject;',
  29464. 'begin',
  29465. ' Arr[1]:=Arr[1];',
  29466. ' Arr[i]:=Arr[i+1];',
  29467. ' o.a[i]:=o.a[i+2];',
  29468. 'end;',
  29469. 'begin',
  29470. '']);
  29471. ConvertProgram;
  29472. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  29473. LinesToStr([ // statements
  29474. 'rtl.recNewT($mod, "TRec", function () {',
  29475. ' this.x = 0;',
  29476. ' this.$eq = function (b) {',
  29477. ' return this.x === b.x;',
  29478. ' };',
  29479. ' this.$assign = function (s) {',
  29480. ' this.x = s.x;',
  29481. ' return this;',
  29482. ' };',
  29483. '});',
  29484. 'rtl.createClass($mod, "TObject", null, function () {',
  29485. ' this.$init = function () {',
  29486. ' this.A = [];',
  29487. ' };',
  29488. ' this.$final = function () {',
  29489. ' this.A = undefined;',
  29490. ' };',
  29491. '});',
  29492. 'this.DoIt = function () {',
  29493. ' var Arr = [];',
  29494. ' var ArrArr = [];',
  29495. ' var i = 0;',
  29496. ' var o = null;',
  29497. ' Arr[1].$assign(Arr[1]);',
  29498. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  29499. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  29500. '};',
  29501. '']),
  29502. LinesToStr([ // $mod.$main
  29503. '']));
  29504. end;
  29505. procedure TTestModule.TestRangeChecks_StringIndex;
  29506. begin
  29507. StartProgram(false);
  29508. Add([
  29509. 'type',
  29510. ' TObject = class',
  29511. ' S: string;',
  29512. ' end;',
  29513. '{$R+}',
  29514. 'procedure DoIt(var h: string);',
  29515. 'var',
  29516. ' s: string;',
  29517. ' i: longint;',
  29518. ' c: char;',
  29519. ' o: tobject;',
  29520. 'begin',
  29521. ' c:=s[1];',
  29522. ' s[i]:=s[i];',
  29523. ' h[i]:=h[i];',
  29524. ' c:=o.s[i];',
  29525. ' o.s[i]:=c;',
  29526. 'end;',
  29527. 'begin',
  29528. '']);
  29529. ConvertProgram;
  29530. CheckSource('TestRangeChecks_StringIndex',
  29531. LinesToStr([ // statements
  29532. 'rtl.createClass($mod, "TObject", null, function () {',
  29533. ' this.$init = function () {',
  29534. ' this.S = "";',
  29535. ' };',
  29536. ' this.$final = function () {',
  29537. ' };',
  29538. '});',
  29539. 'this.DoIt = function (h) {',
  29540. ' var s = "";',
  29541. ' var i = 0;',
  29542. ' var c = "";',
  29543. ' var o = null;',
  29544. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  29545. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  29546. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  29547. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  29548. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  29549. '};',
  29550. '']),
  29551. LinesToStr([ // $mod.$main
  29552. '']));
  29553. end;
  29554. procedure TTestModule.TestRangeChecks_TypecastInt;
  29555. begin
  29556. StartProgram(false);
  29557. Add([
  29558. '{$R+}',
  29559. 'var',
  29560. ' i: nativeint;',
  29561. ' b: byte;',
  29562. ' sh: shortint;',
  29563. ' w: word;',
  29564. ' sm: smallint;',
  29565. ' lw: longword;',
  29566. ' li: longint;',
  29567. 'begin',
  29568. ' b:=12+byte(i);',
  29569. ' sh:=12+shortint(i);',
  29570. ' w:=12+word(i);',
  29571. ' sm:=12+smallint(i);',
  29572. ' lw:=12+longword(i);',
  29573. ' li:=12+longint(i);',
  29574. '']);
  29575. ConvertProgram;
  29576. CheckSource('TestRangeChecks_TypecastInt',
  29577. LinesToStr([
  29578. 'this.i = 0;',
  29579. 'this.b = 0;',
  29580. 'this.sh = 0;',
  29581. 'this.w = 0;',
  29582. 'this.sm = 0;',
  29583. 'this.lw = 0;',
  29584. 'this.li = 0;',
  29585. '']),
  29586. LinesToStr([
  29587. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  29588. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  29589. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  29590. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  29591. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  29592. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  29593. '']));
  29594. end;
  29595. Initialization
  29596. RegisterTests([TTestModule]);
  29597. end.