tcmodules.pas 798 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142
  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 TestCurrency;
  245. Procedure TestForBoolDo;
  246. Procedure TestForIntDo;
  247. Procedure TestForIntInDo;
  248. // strings
  249. Procedure TestCharConst;
  250. Procedure TestChar_Compare;
  251. Procedure TestChar_BuiltInProcs;
  252. Procedure TestStringConst;
  253. Procedure TestStringConstSurrogate;
  254. Procedure TestString_Length;
  255. Procedure TestString_Compare;
  256. Procedure TestString_SetLength;
  257. Procedure TestString_CharAt;
  258. Procedure TestStringHMinusFail;
  259. Procedure TestStr;
  260. Procedure TestBaseType_AnsiStringFail;
  261. Procedure TestBaseType_WideStringFail;
  262. Procedure TestBaseType_ShortStringFail;
  263. Procedure TestBaseType_RawByteStringFail;
  264. Procedure TestTypeShortstring_Fail;
  265. Procedure TestCharSet_Custom;
  266. Procedure TestForCharDo;
  267. Procedure TestForCharInDo;
  268. // alias types
  269. Procedure TestAliasTypeRef;
  270. Procedure TestTypeCast_BaseTypes;
  271. Procedure TestTypeCast_AliasBaseTypes;
  272. // functions
  273. Procedure TestEmptyProc;
  274. Procedure TestProcOneParam;
  275. Procedure TestFunctionWithoutParams;
  276. Procedure TestProcedureWithoutParams;
  277. Procedure TestPrgProcVar;
  278. Procedure TestProcTwoArgs;
  279. Procedure TestProc_DefaultValue;
  280. Procedure TestUnitProcVar;
  281. Procedure TestImplProc;
  282. Procedure TestFunctionResult;
  283. Procedure TestNestedProc;
  284. Procedure TestNestedProc_ResultString;
  285. Procedure TestForwardProc;
  286. Procedure TestNestedForwardProc;
  287. Procedure TestAssignFunctionResult;
  288. Procedure TestFunctionResultInCondition;
  289. Procedure TestFunctionResultInForLoop;
  290. Procedure TestFunctionResultInTypeCast;
  291. Procedure TestExit;
  292. Procedure TestBreak;
  293. Procedure TestBreakAsVar;
  294. Procedure TestContinue;
  295. Procedure TestProc_External;
  296. Procedure TestProc_ExternalOtherUnit;
  297. Procedure TestProc_Asm;
  298. Procedure TestProc_Assembler;
  299. Procedure TestProc_VarParam;
  300. Procedure TestProc_VarParamString;
  301. Procedure TestProc_VarParamV;
  302. Procedure TestProc_Overload;
  303. Procedure TestProc_OverloadForward;
  304. Procedure TestProc_OverloadIntfImpl;
  305. Procedure TestProc_OverloadNested;
  306. Procedure TestProc_OverloadUnitCycle;
  307. Procedure TestProc_Varargs;
  308. Procedure TestProc_ConstOrder;
  309. Procedure TestProc_DuplicateConst;
  310. Procedure TestProc_LocalVarAbsolute;
  311. Procedure TestProc_LocalVarInit;
  312. Procedure TestProc_ReservedWords;
  313. Procedure TestProc_ConstRefWord;
  314. // anonymous functions
  315. Procedure TestAnonymousProc_Assign_ObjFPC;
  316. Procedure TestAnonymousProc_Assign_Delphi;
  317. Procedure TestAnonymousProc_Arg;
  318. Procedure TestAnonymousProc_Typecast;
  319. Procedure TestAnonymousProc_With;
  320. Procedure TestAnonymousProc_ExceptOn;
  321. Procedure TestAnonymousProc_Nested;
  322. Procedure TestAnonymousProc_NestedAssignResult;
  323. Procedure TestAnonymousProc_Class;
  324. Procedure TestAnonymousProc_ForLoop;
  325. // enums, sets
  326. Procedure TestEnum_Name;
  327. Procedure TestEnum_Number;
  328. Procedure TestEnum_ConstFail;
  329. Procedure TestEnum_Functions;
  330. Procedure TestEnum_AsParams;
  331. Procedure TestEnumRange_Array;
  332. Procedure TestEnum_ForIn;
  333. Procedure TestEnum_ScopedNumber;
  334. Procedure TestEnum_InFunction;
  335. Procedure TestSet_Enum;
  336. Procedure TestSet_Operators;
  337. Procedure TestSet_Operator_In;
  338. Procedure TestSet_Functions;
  339. Procedure TestSet_PassAsArgClone;
  340. Procedure TestSet_AsParams;
  341. Procedure TestSet_Property;
  342. Procedure TestSet_EnumConst;
  343. Procedure TestSet_IntConst;
  344. Procedure TestSet_AnonymousEnumType;
  345. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  346. Procedure TestSet_ConstEnum;
  347. Procedure TestSet_ConstChar;
  348. Procedure TestSet_ConstInt;
  349. Procedure TestSet_InFunction;
  350. Procedure TestSet_ForIn;
  351. // statements
  352. Procedure TestNestBegin;
  353. Procedure TestIncDec;
  354. Procedure TestLoHiFpcMode;
  355. Procedure TestLoHiDelphiMode;
  356. Procedure TestAssignments;
  357. Procedure TestArithmeticOperators1;
  358. Procedure TestLogicalOperators;
  359. Procedure TestBitwiseOperators;
  360. Procedure TestBitwiseOperatorsLongword;
  361. Procedure TestFunctionInt;
  362. Procedure TestFunctionString;
  363. Procedure TestIfThen;
  364. Procedure TestForLoop;
  365. Procedure TestForLoopInsideFunction;
  366. Procedure TestForLoop_ReadVarAfter;
  367. Procedure TestForLoop_Nested;
  368. Procedure TestRepeatUntil;
  369. Procedure TestAsmBlock;
  370. Procedure TestAsmPas_Impl; // ToDo
  371. Procedure TestTryFinally;
  372. Procedure TestTryExcept;
  373. Procedure TestTryExcept_ReservedWords;
  374. Procedure TestIfThenRaiseElse;
  375. Procedure TestCaseOf;
  376. Procedure TestCaseOf_UseSwitch;
  377. Procedure TestCaseOfNoElse;
  378. Procedure TestCaseOfNoElse_UseSwitch;
  379. Procedure TestCaseOfRange;
  380. Procedure TestCaseOfString;
  381. Procedure TestCaseOfChar;
  382. Procedure TestCaseOfExternalClassConst;
  383. Procedure TestDebugger;
  384. // arrays
  385. Procedure TestArray_Dynamic;
  386. Procedure TestArray_Dynamic_Nil;
  387. Procedure TestArray_DynMultiDimensional;
  388. Procedure TestArray_StaticInt;
  389. Procedure TestArray_StaticBool;
  390. Procedure TestArray_StaticChar;
  391. Procedure TestArray_StaticMultiDim;
  392. Procedure TestArray_StaticInFunction;
  393. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  394. Procedure TestArrayOfRecord;
  395. Procedure TestArray_StaticRecord;
  396. Procedure TestArrayOfSet;
  397. Procedure TestArray_DynAsParam;
  398. Procedure TestArray_StaticAsParam;
  399. Procedure TestArrayElement_AsParams;
  400. Procedure TestArrayElementFromFuncResult_AsParams;
  401. Procedure TestArrayEnumTypeRange;
  402. Procedure TestArray_SetLengthOutArg;
  403. Procedure TestArray_SetLengthProperty;
  404. Procedure TestArray_SetLengthMultiDim;
  405. Procedure TestArray_SetLengthDynOfStatic;
  406. Procedure TestArray_OpenArrayOfString;
  407. Procedure TestArray_ConstRef;
  408. Procedure TestArray_Concat;
  409. Procedure TestArray_Copy;
  410. Procedure TestArray_InsertDelete;
  411. Procedure TestArray_DynArrayConstObjFPC;
  412. Procedure TestArray_DynArrayConstDelphi;
  413. Procedure TestArray_ArrayLitAsParam;
  414. Procedure TestArray_ArrayLitMultiDimAsParam;
  415. Procedure TestArray_ArrayLitStaticAsParam;
  416. Procedure TestArray_ForInArrOfString;
  417. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  418. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  419. Procedure TestArrayOfConst_TVarRec;
  420. Procedure TestArrayOfConst_PassBaseTypes;
  421. Procedure TestArrayOfConst_PassObj;
  422. // ToDo: tcfiler TPasModuleScope.SystemTVarRec TPas2JSModuleScope.SystemVarRecs
  423. // record
  424. Procedure TestRecord_Empty;
  425. Procedure TestRecord_Var;
  426. Procedure TestRecord_VarExternal;
  427. Procedure TestRecord_WithDo;
  428. Procedure TestRecord_Assign;
  429. Procedure TestRecord_AsParams;
  430. Procedure TestRecord_ConstRef;
  431. Procedure TestRecordElement_AsParams;
  432. Procedure TestRecordElementFromFuncResult_AsParams;
  433. Procedure TestRecordElementFromWith_AsParams;
  434. Procedure TestRecord_Equal;
  435. Procedure TestRecord_TypeCastJSValueToRecord;
  436. Procedure TestRecord_VariantFail;
  437. Procedure TestRecord_FieldArray;
  438. Procedure TestRecord_Const;
  439. Procedure TestRecord_TypecastFail;
  440. Procedure TestRecord_InFunction;
  441. Procedure TestRecord_AnonymousFail;
  442. // ToDo: RTTI of local record
  443. // ToDo: pcu local record, name clash and rtti
  444. // advanced record
  445. Procedure TestAdvRecord_Function;
  446. Procedure TestAdvRecord_Property;
  447. Procedure TestAdvRecord_PropertyDefault;
  448. Procedure TestAdvRecord_Property_ClassMethod;
  449. Procedure TestAdvRecord_Const;
  450. Procedure TestAdvRecord_ExternalField;
  451. Procedure TestAdvRecord_SubRecord;
  452. Procedure TestAdvRecord_SubClass;
  453. Procedure TestAdvRecord_SubInterfaceFail;
  454. Procedure TestAdvRecord_Constructor;
  455. // ToDo: class constructor
  456. // classes
  457. Procedure TestClass_TObjectDefaultConstructor;
  458. Procedure TestClass_TObjectConstructorWithParams;
  459. Procedure TestClass_TObjectConstructorWithDefaultParam;
  460. Procedure TestClass_Var;
  461. Procedure TestClass_Method;
  462. Procedure TestClass_Implementation;
  463. Procedure TestClass_Inheritance;
  464. Procedure TestClass_TypeAlias;
  465. Procedure TestClass_AbstractMethod;
  466. Procedure TestClass_CallInherited_ProcNoParams;
  467. Procedure TestClass_CallInherited_WithParams;
  468. Procedure TestClasS_CallInheritedConstructor;
  469. Procedure TestClass_ClassVar_Assign;
  470. Procedure TestClass_CallClassMethod;
  471. Procedure TestClass_Property;
  472. Procedure TestClass_Property_ClassMethod;
  473. Procedure TestClass_Property_Indexed;
  474. Procedure TestClass_Property_IndexSpec;
  475. Procedure TestClass_PropertyOfTypeArray;
  476. Procedure TestClass_PropertyDefault;
  477. Procedure TestClass_PropertyDefault2;
  478. Procedure TestClass_PropertyOverride;
  479. Procedure TestClass_PropertyIncVisibility;
  480. Procedure TestClass_Assigned;
  481. Procedure TestClass_WithClassDoCreate;
  482. Procedure TestClass_WithClassInstDoProperty;
  483. Procedure TestClass_WithClassInstDoPropertyWithParams;
  484. Procedure TestClass_WithClassInstDoFunc;
  485. Procedure TestClass_TypeCast;
  486. Procedure TestClass_TypeCastUntypedParam;
  487. Procedure TestClass_Overloads;
  488. Procedure TestClass_OverloadsAncestor;
  489. Procedure TestClass_OverloadConstructor;
  490. Procedure TestClass_OverloadDelphiOverride;
  491. Procedure TestClass_ReintroduceVarDelphi;
  492. Procedure TestClass_ReintroducedVar;
  493. Procedure TestClass_RaiseDescendant;
  494. Procedure TestClass_ExternalMethod;
  495. Procedure TestClass_ExternalVirtualNameMismatchFail;
  496. Procedure TestClass_ExternalOverrideFail;
  497. Procedure TestClass_ExternalVar;
  498. Procedure TestClass_Const;
  499. Procedure TestClass_LocalVarSelfFail;
  500. Procedure TestClass_ArgSelfFail;
  501. Procedure TestClass_NestedProcSelf;
  502. Procedure TestClass_NestedProcSelf2;
  503. Procedure TestClass_NestedProcClassSelf;
  504. Procedure TestClass_NestedProcCallInherited;
  505. Procedure TestClass_TObjectFree;
  506. Procedure TestClass_TObjectFreeNewInstance;
  507. Procedure TestClass_TObjectFreeLowerCase;
  508. Procedure TestClass_TObjectFreeFunctionFail;
  509. Procedure TestClass_TObjectFreePropertyFail;
  510. Procedure TestClass_ForIn;
  511. // class of
  512. Procedure TestClassOf_Create;
  513. Procedure TestClassOf_Call;
  514. Procedure TestClassOf_Assign;
  515. Procedure TestClassOf_Is;
  516. Procedure TestClassOf_Compare;
  517. Procedure TestClassOf_ClassVar;
  518. Procedure TestClassOf_ClassMethod;
  519. Procedure TestClassOf_ClassProperty;
  520. Procedure TestClassOf_ClassMethodSelf;
  521. Procedure TestClassOf_TypeCast;
  522. Procedure TestClassOf_ImplicitFunctionCall;
  523. Procedure TestClassOf_Const;
  524. // nested class
  525. Procedure TestNestedClass_Alias;
  526. Procedure TestNestedClass_Record;
  527. Procedure TestNestedClass_Class;
  528. // external class
  529. Procedure TestExternalClass_Var;
  530. Procedure TestExternalClass_Const;
  531. Procedure TestExternalClass_Dollar;
  532. Procedure TestExternalClass_DuplicateVarFail;
  533. Procedure TestExternalClass_Method;
  534. Procedure TestExternalClass_ClassMethod;
  535. Procedure TestExternalClass_ClassMethodStatic;
  536. Procedure TestExternalClass_FunctionResultInTypeCast;
  537. Procedure TestExternalClass_NonExternalOverride;
  538. Procedure TestExternalClass_OverloadHint;
  539. Procedure TestExternalClass_SameNamePublishedProperty;
  540. Procedure TestExternalClass_Property;
  541. Procedure TestExternalClass_PropertyDate;
  542. Procedure TestExternalClass_ClassProperty;
  543. Procedure TestExternalClass_ClassOf;
  544. Procedure TestExternalClass_ClassOtherUnit;
  545. Procedure TestExternalClass_Is;
  546. Procedure TestExternalClass_As;
  547. Procedure TestExternalClass_DestructorFail;
  548. Procedure TestExternalClass_New;
  549. Procedure TestExternalClass_ClassOf_New;
  550. Procedure TestExternalClass_FuncClassOf_New;
  551. Procedure TestExternalClass_New_PasClassFail;
  552. Procedure TestExternalClass_New_PasClassBracketsFail;
  553. Procedure TestExternalClass_LocalConstSameName;
  554. Procedure TestExternalClass_ReintroduceOverload;
  555. Procedure TestExternalClass_Inherited;
  556. Procedure TestExternalClass_PascalAncestorFail;
  557. Procedure TestExternalClass_NewInstance;
  558. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  559. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  560. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  561. Procedure TestExternalClass_PascalProperty;
  562. Procedure TestExternalClass_TypeCastToRootClass;
  563. Procedure TestExternalClass_TypeCastToJSObject;
  564. Procedure TestExternalClass_TypeCastStringToExternalString;
  565. Procedure TestExternalClass_TypeCastToJSFunction;
  566. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  567. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  568. Procedure TestExternalClass_BracketAccessor;
  569. Procedure TestExternalClass_BracketAccessor_Call;
  570. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  571. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  572. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  573. Procedure TestExternalClass_BracketAccessor_MultiType;
  574. Procedure TestExternalClass_BracketAccessor_Index;
  575. Procedure TestExternalClass_ForInJSObject;
  576. Procedure TestExternalClass_ForInJSArray;
  577. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  578. // class interfaces
  579. Procedure TestClassInterface_Corba;
  580. Procedure TestClassInterface_ProcExternalFail;
  581. Procedure TestClassInterface_Overloads;
  582. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  583. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  584. Procedure TestClassInterface_AncestorImpl;
  585. Procedure TestClassInterface_ImplReintroduce;
  586. Procedure TestClassInterface_MethodResolution;
  587. Procedure TestClassInterface_AncestorMoreInterfaces;
  588. Procedure TestClassInterface_MethodOverride;
  589. Procedure TestClassInterface_Corba_Delegation;
  590. Procedure TestClassInterface_Corba_DelegationStatic;
  591. Procedure TestClassInterface_Corba_Operators;
  592. Procedure TestClassInterface_Corba_Args;
  593. Procedure TestClassInterface_Corba_ForIn;
  594. Procedure TestClassInterface_COM_AssignVar;
  595. Procedure TestClassInterface_COM_AssignArg;
  596. Procedure TestClassInterface_COM_FunctionResult;
  597. Procedure TestClassInterface_COM_InheritedFuncResult;
  598. Procedure TestClassInterface_COM_IsAsTypeCasts;
  599. Procedure TestClassInterface_COM_PassAsArg;
  600. Procedure TestClassInterface_COM_PassToUntypedParam;
  601. Procedure TestClassInterface_COM_FunctionInExpr;
  602. Procedure TestClassInterface_COM_Property;
  603. Procedure TestClassInterface_COM_IntfProperty;
  604. Procedure TestClassInterface_COM_Delegation;
  605. Procedure TestClassInterface_COM_With;
  606. Procedure TestClassInterface_COM_ForIn;
  607. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  608. Procedure TestClassInterface_COM_RecordIntfFail;
  609. Procedure TestClassInterface_COM_UnitInitialization;
  610. Procedure TestClassInterface_GUID;
  611. Procedure TestClassInterface_GUIDProperty;
  612. // helpers
  613. Procedure TestClassHelper_ClassVar;
  614. Procedure TestClassHelper_Method_AccessInstanceFields;
  615. Procedure TestClassHelper_Method_Call;
  616. Procedure TestClassHelper_Method_Nested_Call;
  617. Procedure TestClassHelper_ClassMethod_Call;
  618. Procedure TestClassHelper_ClassOf;
  619. Procedure TestClassHelper_MethodRefObjFPC;
  620. Procedure TestClassHelper_Constructor;
  621. Procedure TestClassHelper_InheritedObjFPC;
  622. Procedure TestClassHelper_Property;
  623. Procedure TestClassHelper_Property_Array;
  624. Procedure TestClassHelper_Property_Array_Default;
  625. Procedure TestClassHelper_Property_Array_DefaultDefault;
  626. Procedure TestClassHelper_ClassProperty;
  627. Procedure TestClassHelper_ClassPropertyStatic;
  628. Procedure TestClassHelper_ClassProperty_Array;
  629. Procedure TestClassHelper_ForIn;
  630. Procedure TestClassHelper_PassProperty;
  631. Procedure TestExtClassHelper_ClassVar;
  632. Procedure TestExtClassHelper_Method_Call;
  633. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  634. Procedure TestRecordHelper_ClassVar;
  635. Procedure TestRecordHelper_Method_Call;
  636. Procedure TestRecordHelper_Constructor;
  637. Procedure TestTypeHelper_ClassVar;
  638. Procedure TestTypeHelper_PassResultElement;
  639. Procedure TestTypeHelper_PassArgs;
  640. Procedure TestTypeHelper_PassVarConst;
  641. Procedure TestTypeHelper_PassFuncResult;
  642. Procedure TestTypeHelper_PassPropertyField;
  643. Procedure TestTypeHelper_PassPropertyGetter;
  644. Procedure TestTypeHelper_PassClassPropertyField;
  645. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  646. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  647. Procedure TestTypeHelper_Property;
  648. Procedure TestTypeHelper_Property_Array;
  649. Procedure TestTypeHelper_ClassProperty;
  650. Procedure TestTypeHelper_ClassProperty_Array;
  651. Procedure TestTypeHelper_ClassMethod;
  652. Procedure TestTypeHelper_ExtClassMethodFail;
  653. Procedure TestTypeHelper_Constructor;
  654. Procedure TestTypeHelper_Word;
  655. Procedure TestTypeHelper_Double;
  656. Procedure TestTypeHelper_NativeInt;
  657. Procedure TestTypeHelper_StringChar;
  658. Procedure TestTypeHelper_JSValue;
  659. Procedure TestTypeHelper_Array;
  660. Procedure TestTypeHelper_EnumType;
  661. Procedure TestTypeHelper_SetType;
  662. Procedure TestTypeHelper_NestedSelf;
  663. // proc types
  664. Procedure TestProcType;
  665. Procedure TestProcType_Arg;
  666. Procedure TestProcType_FunctionFPC;
  667. Procedure TestProcType_FunctionDelphi;
  668. Procedure TestProcType_ProcedureDelphi;
  669. Procedure TestProcType_AsParam;
  670. Procedure TestProcType_MethodFPC;
  671. Procedure TestProcType_MethodDelphi;
  672. Procedure TestProcType_PropertyFPC;
  673. Procedure TestProcType_PropertyDelphi;
  674. Procedure TestProcType_WithClassInstDoPropertyFPC;
  675. Procedure TestProcType_Nested;
  676. Procedure TestProcType_NestedOfObject;
  677. Procedure TestProcType_ReferenceToProc;
  678. Procedure TestProcType_ReferenceToMethod;
  679. Procedure TestProcType_Typecast;
  680. Procedure TestProcType_PassProcToUntyped;
  681. Procedure TestProcType_PassProcToArray;
  682. // pointer
  683. Procedure TestPointer;
  684. Procedure TestPointer_Proc;
  685. Procedure TestPointer_AssignRecordFail;
  686. Procedure TestPointer_AssignStaticArrayFail;
  687. Procedure TestPointer_TypeCastJSValueToPointer;
  688. Procedure TestPointer_NonRecordFail;
  689. Procedure TestPointer_AnonymousArgTypeFail;
  690. Procedure TestPointer_AnonymousVarTypeFail;
  691. Procedure TestPointer_AnonymousResultTypeFail;
  692. Procedure TestPointer_AddrOperatorFail;
  693. Procedure TestPointer_ArrayParamsFail;
  694. Procedure TestPointer_PointerAddFail;
  695. Procedure TestPointer_IncPointerFail;
  696. Procedure TestPointer_Record;
  697. Procedure TestPointer_RecordArg;
  698. // jsvalue
  699. Procedure TestJSValue_AssignToJSValue;
  700. Procedure TestJSValue_TypeCastToBaseType;
  701. Procedure TestJSValue_TypecastToJSValue;
  702. Procedure TestJSValue_Equal;
  703. Procedure TestJSValue_If;
  704. Procedure TestJSValue_Not;
  705. Procedure TestJSValue_Enum;
  706. Procedure TestJSValue_ClassInstance;
  707. Procedure TestJSValue_ClassOf;
  708. Procedure TestJSValue_ArrayOfJSValue;
  709. Procedure TestJSValue_ArrayLit;
  710. Procedure TestJSValue_Params;
  711. Procedure TestJSValue_UntypedParam;
  712. Procedure TestJSValue_FuncResultType;
  713. Procedure TestJSValue_ProcType_Assign;
  714. Procedure TestJSValue_ProcType_Equal;
  715. Procedure TestJSValue_ProcType_Param;
  716. Procedure TestJSValue_AssignToPointerFail;
  717. Procedure TestJSValue_OverloadDouble;
  718. Procedure TestJSValue_OverloadNativeInt;
  719. Procedure TestJSValue_OverloadWord;
  720. Procedure TestJSValue_OverloadString;
  721. Procedure TestJSValue_OverloadChar;
  722. Procedure TestJSValue_OverloadPointer;
  723. Procedure TestJSValue_ForIn;
  724. // RTTI
  725. Procedure TestRTTI_IntRange;
  726. Procedure TestRTTI_Double;
  727. Procedure TestRTTI_ProcType;
  728. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  729. Procedure TestRTTI_EnumAndSetType;
  730. Procedure TestRTTI_EnumRange;
  731. Procedure TestRTTI_AnonymousEnumType;
  732. Procedure TestRTTI_StaticArray;
  733. Procedure TestRTTI_DynArray;
  734. Procedure TestRTTI_ArrayNestedAnonymous;
  735. Procedure TestRTTI_PublishedMethodOverloadFail;
  736. Procedure TestRTTI_PublishedMethodExternalFail;
  737. Procedure TestRTTI_PublishedClassPropertyFail;
  738. Procedure TestRTTI_PublishedClassFieldFail;
  739. Procedure TestRTTI_PublishedFieldExternalFail;
  740. Procedure TestRTTI_Class_Field;
  741. Procedure TestRTTI_Class_Method;
  742. Procedure TestRTTI_Class_MethodArgFlags;
  743. Procedure TestRTTI_Class_Property;
  744. Procedure TestRTTI_Class_PropertyParams;
  745. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  746. Procedure TestRTTI_Class_OmitRTTI;
  747. Procedure TestRTTI_IndexModifier;
  748. Procedure TestRTTI_StoredModifier;
  749. Procedure TestRTTI_DefaultValue;
  750. Procedure TestRTTI_DefaultValueSet;
  751. Procedure TestRTTI_DefaultValueRangeType;
  752. Procedure TestRTTI_DefaultValueInherit;
  753. Procedure TestRTTI_OverrideMethod;
  754. Procedure TestRTTI_ReintroduceMethod;
  755. Procedure TestRTTI_OverloadProperty;
  756. // ToDo: array argument
  757. Procedure TestRTTI_ClassForward;
  758. Procedure TestRTTI_ClassOf;
  759. Procedure TestRTTI_Record;
  760. Procedure TestRTTI_RecordAnonymousArray;
  761. Procedure TestRTTI_LocalTypes;
  762. Procedure TestRTTI_TypeInfo_BaseTypes;
  763. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  764. Procedure TestRTTI_TypeInfo_LocalFail;
  765. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  766. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  767. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  768. Procedure TestRTTI_TypeInfo_FunctionClassType;
  769. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  770. Procedure TestRTTI_Interface_Corba;
  771. Procedure TestRTTI_Interface_COM;
  772. Procedure TestRTTI_ClassHelper;
  773. // Resourcestring
  774. Procedure TestResourcestringProgram;
  775. Procedure TestResourcestringUnit;
  776. Procedure TestResourcestringImplementation;
  777. // Attributes
  778. Procedure TestAtributes_Ignore;
  779. // Assertions, checks
  780. procedure TestAssert;
  781. procedure TestAssert_SysUtils;
  782. procedure TestObjectChecks;
  783. procedure TestRangeChecks_AssignInt;
  784. procedure TestRangeChecks_AssignIntRange;
  785. procedure TestRangeChecks_AssignEnum;
  786. procedure TestRangeChecks_AssignEnumRange;
  787. procedure TestRangeChecks_AssignChar;
  788. procedure TestRangeChecks_AssignCharRange;
  789. procedure TestRangeChecks_ArrayIndex;
  790. procedure TestRangeChecks_ArrayOfRecIndex;
  791. procedure TestRangeChecks_StringIndex;
  792. procedure TestRangeChecks_TypecastInt;
  793. end;
  794. function LinesToStr(Args: array of const): string;
  795. function ExtractFileUnitName(aFilename: string): string;
  796. function JSToStr(El: TJSElement): string;
  797. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  798. implementation
  799. function LinesToStr(Args: array of const): string;
  800. var
  801. s: String;
  802. i: Integer;
  803. begin
  804. s:='';
  805. for i:=Low(Args) to High(Args) do
  806. case Args[i].VType of
  807. vtChar: s += Args[i].VChar+LineEnding;
  808. vtString: s += Args[i].VString^+LineEnding;
  809. vtPChar: s += Args[i].VPChar+LineEnding;
  810. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  811. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  812. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  813. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  814. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  815. end;
  816. Result:=s;
  817. end;
  818. function ExtractFileUnitName(aFilename: string): string;
  819. var
  820. p: Integer;
  821. begin
  822. Result:=ExtractFileName(aFilename);
  823. if Result='' then exit;
  824. for p:=length(Result) downto 1 do
  825. case Result[p] of
  826. '/','\': exit;
  827. '.':
  828. begin
  829. Delete(Result,p,length(Result));
  830. exit;
  831. end;
  832. end;
  833. end;
  834. function JSToStr(El: TJSElement): string;
  835. var
  836. aWriter: TBufferWriter;
  837. aJSWriter: TJSWriter;
  838. begin
  839. aJSWriter:=nil;
  840. aWriter:=TBufferWriter.Create(1000);
  841. try
  842. aJSWriter:=TJSWriter.Create(aWriter);
  843. aJSWriter.IndentSize:=2;
  844. aJSWriter.WriteJS(El);
  845. Result:=aWriter.AsString;
  846. finally
  847. aJSWriter.Free;
  848. aWriter.Free;
  849. end;
  850. end;
  851. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  852. // search diff, ignore changes in spaces
  853. const
  854. SpaceChars = [#9,#10,#13,' '];
  855. var
  856. ExpectedP, ActualP: PChar;
  857. function FindLineEnd(p: PChar): PChar;
  858. begin
  859. Result:=p;
  860. while not (Result^ in [#0,#10,#13]) do inc(Result);
  861. end;
  862. function FindLineStart(p, MinP: PChar): PChar;
  863. begin
  864. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  865. Result:=p;
  866. end;
  867. procedure SkipLineEnd(var p: PChar);
  868. begin
  869. if p^ in [#10,#13] then
  870. begin
  871. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  872. inc(p,2)
  873. else
  874. inc(p);
  875. end;
  876. end;
  877. procedure DiffFound;
  878. var
  879. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  880. ExpLine, ActLine: String;
  881. i, LineNo, DiffLineNo: Integer;
  882. begin
  883. writeln('Diff found "',Msg,'". Lines:');
  884. // write correct lines
  885. p:=PChar(Expected);
  886. LineNo:=0;
  887. DiffLineNo:=0;
  888. repeat
  889. StartPos:=p;
  890. while not (p^ in [#0,#10,#13]) do inc(p);
  891. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  892. SkipLineEnd(p);
  893. inc(LineNo);
  894. if (p<=ExpectedP) and (p^<>#0) then
  895. begin
  896. writeln('= ',ExpLine);
  897. end else begin
  898. // diff line
  899. if DiffLineNo=0 then DiffLineNo:=LineNo;
  900. // write actual line
  901. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  902. ActLineEndP:=FindLineEnd(ActualP);
  903. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  904. writeln('- ',ActLine);
  905. // write expected line
  906. writeln('+ ',ExpLine);
  907. // write empty line with pointer ^
  908. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  909. writeln('^');
  910. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  911. CheckSrcDiff:=false;
  912. // write up to three following actual lines to get some context
  913. for i:=1 to 3 do begin
  914. ActLineStartP:=ActLineEndP;
  915. SkipLineEnd(ActLineStartP);
  916. if ActLineStartP^=#0 then break;
  917. ActLineEndP:=FindLineEnd(ActLineStartP);
  918. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  919. writeln('~ ',ActLine);
  920. end;
  921. exit;
  922. end;
  923. until p^=#0;
  924. writeln('DiffFound Actual:-----------------------');
  925. writeln(Actual);
  926. writeln('DiffFound Expected:---------------------');
  927. writeln(Expected);
  928. writeln('DiffFound ------------------------------');
  929. Msg:='diff found, but lines are the same, internal error';
  930. CheckSrcDiff:=false;
  931. end;
  932. var
  933. IsSpaceNeeded: Boolean;
  934. LastChar, Quote: Char;
  935. begin
  936. Result:=true;
  937. Msg:='';
  938. if Expected='' then Expected:=' ';
  939. if Actual='' then Actual:=' ';
  940. ExpectedP:=PChar(Expected);
  941. ActualP:=PChar(Actual);
  942. repeat
  943. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  944. case ExpectedP^ of
  945. #0:
  946. begin
  947. // check that rest of Actual has only spaces
  948. while ActualP^ in SpaceChars do inc(ActualP);
  949. if ActualP^<>#0 then
  950. begin
  951. DiffFound;
  952. exit;
  953. end;
  954. exit(true);
  955. end;
  956. ' ',#9,#10,#13:
  957. begin
  958. // skip space in Expected
  959. IsSpaceNeeded:=false;
  960. if ExpectedP>PChar(Expected) then
  961. LastChar:=ExpectedP[-1]
  962. else
  963. LastChar:=#0;
  964. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  965. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  966. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  967. IsSpaceNeeded:=true;
  968. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  969. begin
  970. DiffFound;
  971. exit;
  972. end;
  973. while ActualP^ in SpaceChars do inc(ActualP);
  974. end;
  975. '''','"':
  976. begin
  977. while ActualP^ in SpaceChars do inc(ActualP);
  978. if ExpectedP^<>ActualP^ then
  979. begin
  980. DiffFound;
  981. exit;
  982. end;
  983. Quote:=ExpectedP^;
  984. repeat
  985. inc(ExpectedP);
  986. inc(ActualP);
  987. if ExpectedP^<>ActualP^ then
  988. begin
  989. DiffFound;
  990. exit;
  991. end;
  992. if (ExpectedP^ in [#0,#10,#13]) then
  993. break
  994. else if (ExpectedP^=Quote) then
  995. begin
  996. inc(ExpectedP);
  997. inc(ActualP);
  998. break;
  999. end;
  1000. until false;
  1001. end;
  1002. else
  1003. while ActualP^ in SpaceChars do inc(ActualP);
  1004. if ExpectedP^<>ActualP^ then
  1005. begin
  1006. DiffFound;
  1007. exit;
  1008. end;
  1009. inc(ExpectedP);
  1010. inc(ActualP);
  1011. end;
  1012. until false;
  1013. end;
  1014. { TTestEnginePasResolver }
  1015. destructor TTestEnginePasResolver.Destroy;
  1016. begin
  1017. FreeAndNil(FStreamResolver);
  1018. FreeAndNil(FParser);
  1019. FreeAndNil(FScanner);
  1020. FreeAndNil(FStreamResolver);
  1021. if Module<>nil then
  1022. begin
  1023. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1024. FModule:=nil;
  1025. end;
  1026. inherited Destroy;
  1027. end;
  1028. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1029. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1030. begin
  1031. Result:=nil;
  1032. if InFilename<>'' then
  1033. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1034. if Assigned(OnFindUnit) then
  1035. Result:=OnFindUnit(AName);
  1036. if NameExpr=nil then ;
  1037. end;
  1038. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1039. begin
  1040. // do not parse recursively
  1041. // parse via the queue
  1042. if Section=nil then ;
  1043. end;
  1044. { TCustomTestModule }
  1045. function TCustomTestModule.GetMsgCount: integer;
  1046. begin
  1047. Result:=FHintMsgs.Count;
  1048. end;
  1049. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1050. begin
  1051. Result:=TTestHintMessage(FHintMsgs[Index]);
  1052. end;
  1053. function TCustomTestModule.GetResolverCount: integer;
  1054. begin
  1055. Result:=FModules.Count;
  1056. end;
  1057. function TCustomTestModule.GetResolvers(Index: integer
  1058. ): TTestEnginePasResolver;
  1059. begin
  1060. Result:=TTestEnginePasResolver(FModules[Index]);
  1061. end;
  1062. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1063. ): TPasModule;
  1064. var
  1065. DefNamespace: String;
  1066. begin
  1067. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1068. if (Pos('.',aUnitName)<1) then
  1069. begin
  1070. DefNamespace:=GetDefaultNamespace;
  1071. if DefNamespace<>'' then
  1072. begin
  1073. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1074. if Result<>nil then exit;
  1075. end;
  1076. end;
  1077. Result:=LoadUnit(aUnitName);
  1078. if Result<>nil then exit;
  1079. {$IFDEF VerbosePas2JS}
  1080. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1081. {$ENDIF}
  1082. Fail('can''t find unit "'+aUnitName+'"');
  1083. end;
  1084. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1085. var
  1086. aParser: TPasParser;
  1087. Item: TTestHintMessage;
  1088. begin
  1089. aParser:=Sender as TPasParser;
  1090. Item:=TTestHintMessage.Create;
  1091. Item.Id:=aParser.LastMsgNumber;
  1092. Item.MsgType:=aParser.LastMsgType;
  1093. Item.MsgNumber:=aParser.LastMsgNumber;
  1094. Item.Msg:=Msg;
  1095. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1096. {$IFDEF VerbosePas2JS}
  1097. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1098. {$ENDIF}
  1099. FHintMsgs.Add(Item);
  1100. end;
  1101. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1102. );
  1103. var
  1104. aResolver: TTestEnginePasResolver;
  1105. Item: TTestHintMessage;
  1106. begin
  1107. aResolver:=Sender as TTestEnginePasResolver;
  1108. Item:=TTestHintMessage.Create;
  1109. Item.Id:=aResolver.LastMsgId;
  1110. Item.MsgType:=aResolver.LastMsgType;
  1111. Item.MsgNumber:=aResolver.LastMsgNumber;
  1112. Item.Msg:=Msg;
  1113. Item.SourcePos:=aResolver.LastSourcePos;
  1114. {$IFDEF VerbosePas2JS}
  1115. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1116. {$ENDIF}
  1117. FHintMsgs.Add(Item);
  1118. end;
  1119. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1120. var
  1121. Item: TTestHintMessage;
  1122. aScanner: TPas2jsPasScanner;
  1123. begin
  1124. aScanner:=Sender as TPas2jsPasScanner;
  1125. Item:=TTestHintMessage.Create;
  1126. Item.Id:=aScanner.LastMsgNumber;
  1127. Item.MsgType:=aScanner.LastMsgType;
  1128. Item.MsgNumber:=aScanner.LastMsgNumber;
  1129. Item.Msg:=Msg;
  1130. Item.SourcePos:=aScanner.CurSourcePos;
  1131. {$IFDEF VerbosePas2JS}
  1132. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1133. {$ENDIF}
  1134. FHintMsgs.Add(Item);
  1135. end;
  1136. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1137. var
  1138. i: Integer;
  1139. CurEngine: TTestEnginePasResolver;
  1140. CurUnitName: String;
  1141. begin
  1142. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1143. Result:=nil;
  1144. if (Module.ClassType=TPasModule)
  1145. and (CompareText(Module.Name,aUnitName)=0) then
  1146. exit(Module);
  1147. for i:=0 to ResolverCount-1 do
  1148. begin
  1149. CurEngine:=Resolvers[i];
  1150. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1151. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1152. if CompareText(aUnitName,CurUnitName)=0 then
  1153. begin
  1154. Result:=CurEngine.Module;
  1155. if Result<>nil then exit;
  1156. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1157. FileResolver.FindSourceFile(aUnitName);
  1158. CurEngine.StreamResolver:=TStreamResolver.Create;
  1159. CurEngine.StreamResolver.OwnsStreams:=True;
  1160. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1161. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1162. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1163. InitScanner(CurEngine.Scanner);
  1164. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1165. CurEngine.Parser.Options:=po_tcmodules;
  1166. if CompareText(CurUnitName,'System')=0 then
  1167. CurEngine.Parser.ImplicitUses.Clear;
  1168. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1169. try
  1170. CurEngine.Parser.NextToken;
  1171. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1172. except
  1173. on E: Exception do
  1174. HandleException(E);
  1175. end;
  1176. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1177. Result:=CurEngine.Module;
  1178. exit;
  1179. end;
  1180. end;
  1181. end;
  1182. procedure TCustomTestModule.SetUp;
  1183. begin
  1184. {$IFDEF EnablePasTreeGlobalRefCount}
  1185. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1186. {$ENDIF}
  1187. if FModules<>nil then
  1188. begin
  1189. writeln('TCustomTestModule.SetUp FModules<>nil');
  1190. Halt;
  1191. end;
  1192. inherited SetUp;
  1193. FSkipTests:=false;
  1194. FSource:=TStringList.Create;
  1195. FModules:=TObjectList.Create(true);
  1196. FFilename:='test1.pp';
  1197. FFileResolver:=TStreamResolver.Create;
  1198. FFileResolver.OwnsStreams:=True;
  1199. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1200. InitScanner(FScanner);
  1201. FEngine:=AddModule(Filename);
  1202. FEngine.Scanner:=FScanner;
  1203. FScanner.Resolver:=FEngine;
  1204. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1205. FParser.OnLog:=@OnParserLog;
  1206. FEngine.Parser:=FParser;
  1207. Parser.Options:=po_tcmodules;
  1208. FModule:=Nil;
  1209. FConverter:=CreateConverter;
  1210. FExpectedErrorClass:=nil;
  1211. end;
  1212. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1213. begin
  1214. Result:=TPasToJSConverter.Create;
  1215. Result.Options:=co_tcmodules;
  1216. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1217. end;
  1218. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1219. begin
  1220. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1221. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1222. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1223. aScanner.AllowedBoolSwitches:=msAllPas2jsBoolSwitches;
  1224. aScanner.ReadOnlyBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly;
  1225. aScanner.CurrentBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1226. aScanner.OnLog:=@OnScannerLog;
  1227. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1228. end;
  1229. procedure TCustomTestModule.TearDown;
  1230. {$IFDEF CheckPasTreeRefCount}
  1231. var
  1232. El: TPasElement;
  1233. {$ENDIF}
  1234. var
  1235. i: Integer;
  1236. CurModule: TPasModule;
  1237. begin
  1238. FHintMsgs.Clear;
  1239. FHintMsgsGood.Clear;
  1240. FSkipTests:=false;
  1241. FJSRegModuleCall:=nil;
  1242. FJSModuleCallArgs:=nil;
  1243. FJSImplentationUses:=nil;
  1244. FJSInterfaceUses:=nil;
  1245. FJSModuleSrc:=nil;
  1246. FJSInitBody:=nil;
  1247. FreeAndNil(FJSSource);
  1248. FreeAndNil(FJSModule);
  1249. FreeAndNil(FConverter);
  1250. Engine.Clear;
  1251. FreeAndNil(FSource);
  1252. FreeAndNil(FFileResolver);
  1253. if FModules<>nil then
  1254. begin
  1255. for i:=0 to FModules.Count-1 do
  1256. begin
  1257. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1258. if CurModule=nil then continue;
  1259. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1260. CurModule.ReleaseUsedUnits;
  1261. end;
  1262. if FModule<>nil then
  1263. FModule.ReleaseUsedUnits;
  1264. for i:=0 to FModules.Count-1 do
  1265. begin
  1266. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1267. if CurModule=nil then continue;
  1268. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1269. end;
  1270. FreeAndNil(FModules);
  1271. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1272. FEngine:=nil;
  1273. end;
  1274. inherited TearDown;
  1275. {$IFDEF EnablePasTreeGlobalRefCount}
  1276. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1277. begin
  1278. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1279. {$IFDEF CheckPasTreeRefCount}
  1280. El:=TPasElement.FirstRefEl;
  1281. while El<>nil do
  1282. begin
  1283. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1284. for i:=0 to El.RefIds.Count-1 do
  1285. writeln(' ',El.RefIds[i]);
  1286. El:=El.NextRefEl;
  1287. end;
  1288. {$ENDIF}
  1289. Halt;
  1290. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1291. end;
  1292. {$ENDIF}
  1293. end;
  1294. procedure TCustomTestModule.Add(Line: string);
  1295. begin
  1296. Source.Add(Line);
  1297. end;
  1298. procedure TCustomTestModule.Add(const Lines: array of string);
  1299. var
  1300. i: Integer;
  1301. begin
  1302. for i:=low(Lines) to high(Lines) do
  1303. Add(Lines[i]);
  1304. end;
  1305. procedure TCustomTestModule.StartParsing;
  1306. var
  1307. Src: String;
  1308. begin
  1309. Src:=Source.Text;
  1310. FEngine.Source:=Src;
  1311. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1312. Scanner.OpenFile(FileName);
  1313. Writeln('// Test : ',Self.TestName);
  1314. Writeln(Src);
  1315. end;
  1316. procedure TCustomTestModule.ParseModuleQueue;
  1317. var
  1318. i: Integer;
  1319. CurResolver: TTestEnginePasResolver;
  1320. Found: Boolean;
  1321. Section: TPasSection;
  1322. begin
  1323. // parse til exception or all modules finished
  1324. while not SkipTests do
  1325. begin
  1326. Found:=false;
  1327. for i:=0 to ResolverCount-1 do
  1328. begin
  1329. CurResolver:=Resolvers[i];
  1330. if CurResolver.CurrentParser=nil then continue;
  1331. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1332. continue;
  1333. CurResolver.Parser.ParseContinue;
  1334. Found:=true;
  1335. break;
  1336. end;
  1337. if not Found then break;
  1338. end;
  1339. for i:=0 to ResolverCount-1 do
  1340. begin
  1341. CurResolver:=Resolvers[i];
  1342. if CurResolver.Parser=nil then
  1343. begin
  1344. if CurResolver.CurrentParser<>nil then
  1345. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1346. continue;
  1347. end;
  1348. if CurResolver.Parser.CurModule<>nil then
  1349. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1350. end;
  1351. end;
  1352. procedure TCustomTestModule.ParseModule;
  1353. begin
  1354. if SkipTests then exit;
  1355. FFirstPasStatement:=nil;
  1356. try
  1357. StartParsing;
  1358. Parser.ParseMain(FModule);
  1359. ParseModuleQueue;
  1360. except
  1361. on E: Exception do
  1362. HandleException(E);
  1363. end;
  1364. if SkipTests then exit;
  1365. AssertNotNull('Module resulted in Module',Module);
  1366. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1367. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1368. end;
  1369. procedure TCustomTestModule.ParseProgram;
  1370. begin
  1371. if SkipTests then exit;
  1372. ParseModule;
  1373. if SkipTests then exit;
  1374. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1375. FPasProgram:=TPasProgram(Module);
  1376. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1377. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1378. if (PasProgram.InitializationSection.Elements.Count>0) then
  1379. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1380. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1381. end;
  1382. procedure TCustomTestModule.ParseUnit;
  1383. begin
  1384. if SkipTests then exit;
  1385. ParseModule;
  1386. if SkipTests then exit;
  1387. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1388. AssertNotNull('Has interface section',Module.InterfaceSection);
  1389. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1390. if (Module.InitializationSection<>nil)
  1391. and (Module.InitializationSection.Elements.Count>0)
  1392. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1393. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1394. end;
  1395. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1396. ): TTestEnginePasResolver;
  1397. var
  1398. i: Integer;
  1399. begin
  1400. for i:=0 to ResolverCount-1 do
  1401. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1402. exit(Resolvers[i]);
  1403. Result:=nil;
  1404. end;
  1405. function TCustomTestModule.AddModule(aFilename: string
  1406. ): TTestEnginePasResolver;
  1407. begin
  1408. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1409. if FindModuleWithFilename(aFilename)<>nil then
  1410. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1411. Result:=TTestEnginePasResolver.Create;
  1412. Result.Filename:=aFilename;
  1413. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1414. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1415. Result.OnLog:=@OnPasResolverLog;
  1416. FModules.Add(Result);
  1417. end;
  1418. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1419. ): TTestEnginePasResolver;
  1420. begin
  1421. Result:=AddModule(aFilename);
  1422. Result.Source:=Src;
  1423. end;
  1424. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1425. ImplementationSrc: string): TTestEnginePasResolver;
  1426. var
  1427. Src: String;
  1428. begin
  1429. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1430. Src+=LineEnding;
  1431. Src+='interface'+LineEnding;
  1432. Src+=LineEnding;
  1433. Src+=InterfaceSrc;
  1434. Src+='implementation'+LineEnding;
  1435. Src+=LineEnding;
  1436. Src+=ImplementationSrc;
  1437. Src+='end.'+LineEnding;
  1438. Result:=AddModuleWithSrc(aFilename,Src);
  1439. end;
  1440. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1441. var
  1442. Intf, Impl: TStringList;
  1443. begin
  1444. Intf:=TStringList.Create;
  1445. // interface
  1446. if supTVarRec in Parts then
  1447. Intf.Add('{$modeswitch externalclass}');
  1448. Intf.Add('type');
  1449. Intf.Add(' integer=longint;');
  1450. Intf.Add(' sizeint=nativeint;');
  1451. //'const',
  1452. //' LineEnding = #10;',
  1453. //' DirectorySeparator = ''/'';',
  1454. //' DriveSeparator = '''';',
  1455. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1456. //' AllowDriveSeparators : set of char = [];',
  1457. if supTObject in Parts then
  1458. Intf.AddStrings([
  1459. 'type',
  1460. ' TClass = class of TObject;',
  1461. ' TObject = class',
  1462. ' constructor Create;',
  1463. ' destructor Destroy; virtual;',
  1464. ' class function ClassType: TClass; assembler;',
  1465. ' class function ClassName: String; assembler;',
  1466. ' class function ClassNameIs(const Name: string): boolean;',
  1467. ' class function ClassParent: TClass; assembler;',
  1468. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1469. ' class function UnitName: String; assembler;',
  1470. ' procedure AfterConstruction; virtual;',
  1471. ' procedure BeforeDestruction;virtual;',
  1472. ' function Equals(Obj: TObject): boolean; virtual;',
  1473. ' function ToString: String; virtual;',
  1474. ' end;']);
  1475. if supTVarRec in Parts then
  1476. Intf.AddStrings([
  1477. 'const',
  1478. ' vtInteger = 0;',
  1479. ' vtBoolean = 1;',
  1480. ' vtJSValue = 19;',
  1481. 'type',
  1482. ' PVarRec = ^TVarRec;',
  1483. ' TVarRec = record',
  1484. ' VType : byte;',
  1485. ' VJSValue: JSValue;',
  1486. ' vInteger: longint external name ''VJSValue'';',
  1487. ' vBoolean: boolean external name ''VJSValue'';',
  1488. ' end;',
  1489. ' TVarRecArray = array of TVarRec;',
  1490. 'function VarRecs: TVarRecArray; varargs;',
  1491. '']);
  1492. Intf.Add('var');
  1493. Intf.Add(' ExitCode: Longint = 0;');
  1494. // implementation
  1495. Impl:=TStringList.Create;
  1496. if supTObject in Parts then
  1497. Impl.AddStrings([
  1498. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1499. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1500. 'asm',
  1501. 'end;',
  1502. 'constructor TObject.Create; begin end;',
  1503. 'destructor TObject.Destroy; begin end;',
  1504. 'class function TObject.ClassType: TClass; assembler;',
  1505. 'asm',
  1506. 'end;',
  1507. 'class function TObject.ClassName: String; assembler;',
  1508. 'asm',
  1509. 'end;',
  1510. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1511. 'begin',
  1512. ' Result:=SameText(Name,ClassName);',
  1513. 'end;',
  1514. 'class function TObject.ClassParent: TClass; assembler;',
  1515. 'asm',
  1516. 'end;',
  1517. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1518. 'asm',
  1519. 'end;',
  1520. 'class function TObject.UnitName: String; assembler;',
  1521. 'asm',
  1522. 'end;',
  1523. 'procedure TObject.AfterConstruction; begin end;',
  1524. 'procedure TObject.BeforeDestruction; begin end;',
  1525. 'function TObject.Equals(Obj: TObject): boolean;',
  1526. 'begin',
  1527. ' Result:=Obj=Self;',
  1528. 'end;',
  1529. 'function TObject.ToString: String;',
  1530. 'begin',
  1531. ' Result:=ClassName;',
  1532. 'end;'
  1533. ]);
  1534. if supTVarRec in Parts then
  1535. Impl.AddStrings([
  1536. 'function VarRecs: TVarRecArray; varargs;',
  1537. 'var',
  1538. ' v: PVarRec;',
  1539. 'begin',
  1540. ' v^.VType:=1;',
  1541. ' v^.VJSValue:=2;',
  1542. 'end;',
  1543. '']);
  1544. try
  1545. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1546. finally
  1547. Intf.Free;
  1548. Impl.Free;
  1549. end;
  1550. end;
  1551. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1552. SystemUnitParts: TSystemUnitParts);
  1553. begin
  1554. if NeedSystemUnit then
  1555. AddSystemUnit(SystemUnitParts)
  1556. else
  1557. Parser.ImplicitUses.Clear;
  1558. Add('program '+ExtractFileUnitName(Filename)+';');
  1559. Add('');
  1560. end;
  1561. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1562. SystemUnitParts: TSystemUnitParts);
  1563. begin
  1564. if NeedSystemUnit then
  1565. AddSystemUnit(SystemUnitParts)
  1566. else
  1567. Parser.ImplicitUses.Clear;
  1568. Add('unit Test1;');
  1569. Add('');
  1570. end;
  1571. procedure TCustomTestModule.ConvertModule;
  1572. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1573. out UsesLit: TJSArrayLiteral);
  1574. var
  1575. i: Integer;
  1576. Item: TJSElement;
  1577. Lit: TJSLiteral;
  1578. begin
  1579. UsesLit:=nil;
  1580. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1581. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1582. exit; // null is ok
  1583. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1584. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1585. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1586. begin
  1587. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1588. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1589. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1590. Lit:=TJSLiteral(Item);
  1591. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1592. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1593. end;
  1594. end;
  1595. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1596. out Src: TJSSourceElements);
  1597. var
  1598. FunDecl: TJSFunctionDeclarationStatement;
  1599. FunDef: TJSFuncDef;
  1600. FunBody: TJSFunctionBody;
  1601. begin
  1602. Src:=nil;
  1603. AssertNotNull(ParamName,Arg.Expr);
  1604. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1605. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1606. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1607. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1608. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1609. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1610. AssertNotNull(ParamName+' body',FunDef.Body);
  1611. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1612. FunBody:=FunDef.Body as TJSFunctionBody;
  1613. AssertNotNull(ParamName+' body.A',FunBody.A);
  1614. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1615. Src:=FunBody.A as TJSSourceElements;
  1616. end;
  1617. var
  1618. ModuleNameExpr: TJSLiteral;
  1619. InitFunction: TJSFunctionDeclarationStatement;
  1620. InitAssign: TJSSimpleAssignStatement;
  1621. InitName: String;
  1622. LastNode: TJSElement;
  1623. Arg: TJSArrayLiteralElement;
  1624. begin
  1625. if SkipTests then exit;
  1626. try
  1627. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1628. except
  1629. on E: Exception do
  1630. HandleException(E);
  1631. end;
  1632. if SkipTests then exit;
  1633. if ExpectedErrorClass<>nil then
  1634. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1635. FJSSource:=TStringList.Create;
  1636. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1637. {$IFDEF VerbosePas2JS}
  1638. writeln('TTestModule.ConvertModule JS:');
  1639. write(FJSSource.Text);
  1640. {$ENDIF}
  1641. // rtl.module(...
  1642. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1643. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1644. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1645. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1646. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1647. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1648. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1649. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1650. // parameter 'unitname'
  1651. if JSModuleCallArgs.Elements.Count<1 then
  1652. Fail('rtl.module first param unit missing');
  1653. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1654. AssertNotNull('module name param',Arg.Expr);
  1655. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1656. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1657. if Module is TPasProgram then
  1658. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1659. else
  1660. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1661. // main uses section
  1662. if JSModuleCallArgs.Elements.Count<2 then
  1663. Fail('rtl.module second param main uses missing');
  1664. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1665. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1666. // program/library/interface function()
  1667. if JSModuleCallArgs.Elements.Count<3 then
  1668. Fail('rtl.module third param intf-function missing');
  1669. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1670. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1671. // search for $mod.$init or $mod.$main - the last statement
  1672. if Module is TPasProgram then
  1673. begin
  1674. InitName:='$main';
  1675. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1676. end
  1677. else
  1678. InitName:='$init';
  1679. FJSInitBody:=nil;
  1680. if JSModuleSrc.Statements.Count>0 then
  1681. begin
  1682. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1683. if LastNode is TJSSimpleAssignStatement then
  1684. begin
  1685. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1686. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1687. begin
  1688. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1689. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1690. end
  1691. else if Module is TPasProgram then
  1692. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1693. end;
  1694. end;
  1695. // optional: implementation uses section
  1696. if JSModuleCallArgs.Elements.Count<4 then
  1697. exit;
  1698. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1699. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1700. // optional: implementation function()
  1701. if JSModuleCallArgs.Elements.Count<5 then
  1702. exit;
  1703. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1704. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1705. end;
  1706. procedure TCustomTestModule.ConvertProgram;
  1707. begin
  1708. Add('end.');
  1709. ParseProgram;
  1710. ConvertModule;
  1711. end;
  1712. procedure TCustomTestModule.ConvertUnit;
  1713. begin
  1714. Add('end.');
  1715. ParseUnit;
  1716. ConvertModule;
  1717. end;
  1718. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1719. begin
  1720. Result:=tcmodules.JSToStr(El);
  1721. end;
  1722. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1723. DottedName: string);
  1724. begin
  1725. if DottedName='' then
  1726. begin
  1727. AssertNull(Msg,El);
  1728. end
  1729. else
  1730. begin
  1731. AssertNotNull(Msg,El);
  1732. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1733. end;
  1734. end;
  1735. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1736. begin
  1737. if El=nil then
  1738. Result:=''
  1739. else if El is TJSPrimaryExpressionIdent then
  1740. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1741. else if El is TJSDotMemberExpression then
  1742. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1743. else
  1744. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1745. end;
  1746. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1747. InitStatements: string; ImplStatements: string);
  1748. var
  1749. ActualSrc, ExpectedSrc, InitName: String;
  1750. begin
  1751. ActualSrc:=JSToStr(JSModuleSrc);
  1752. ExpectedSrc:=
  1753. 'var $mod = this;'+LineEnding
  1754. +Statements;
  1755. if coUseStrict in Converter.Options then
  1756. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1757. if Module is TPasProgram then
  1758. InitName:='$main'
  1759. else
  1760. InitName:='$init';
  1761. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1762. ExpectedSrc:=ExpectedSrc+LineEnding
  1763. +'$mod.'+InitName+' = function () {'+LineEnding
  1764. +InitStatements
  1765. +'};'+LineEnding;
  1766. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1767. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1768. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1769. if (JSImplementationSrc<>nil) then
  1770. begin
  1771. ActualSrc:=JSToStr(JSImplementationSrc);
  1772. ExpectedSrc:=
  1773. 'var $mod = this;'+LineEnding
  1774. +'var $impl = $mod.$impl;'+LineEnding
  1775. +ImplStatements;
  1776. end
  1777. else
  1778. begin
  1779. ActualSrc:='';
  1780. ExpectedSrc:=ImplStatements;
  1781. end;
  1782. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1783. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1784. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1785. end;
  1786. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1787. // search diff, ignore changes in spaces
  1788. var
  1789. s: string;
  1790. begin
  1791. if CheckSrcDiff(Expected,Actual,s) then exit;
  1792. Fail(Msg+': '+s);
  1793. end;
  1794. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1795. var
  1796. aResolver: TTestEnginePasResolver;
  1797. aConverter: TPasToJSConverter;
  1798. aJSModule: TJSSourceElements;
  1799. ActualSrc: String;
  1800. begin
  1801. aResolver:=GetResolver(Filename);
  1802. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1803. {$IFDEF VerbosePas2JS}
  1804. writeln('CheckUnit '+Filename+' converting ...');
  1805. {$ENDIF}
  1806. aConverter:=CreateConverter;
  1807. aJSModule:=nil;
  1808. try
  1809. try
  1810. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1811. except
  1812. on E: Exception do
  1813. HandleException(E);
  1814. end;
  1815. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1816. {$IFDEF VerbosePas2JS}
  1817. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1818. write(aResolver.Source);
  1819. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1820. write(ActualSrc);
  1821. {$ENDIF}
  1822. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1823. finally
  1824. aJSModule.Free;
  1825. aConverter.Free;
  1826. end;
  1827. end;
  1828. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1829. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1830. var
  1831. i: Integer;
  1832. Item: TTestHintMessage;
  1833. Expected,Actual: string;
  1834. begin
  1835. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1836. for i:=0 to MsgCount-1 do
  1837. begin
  1838. Item:=Msgs[i];
  1839. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1840. if (Marker<>nil) then
  1841. begin
  1842. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1843. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1844. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1845. end;
  1846. // found
  1847. FHintMsgsGood.Add(Item);
  1848. str(Item.MsgType,Actual);
  1849. str(MsgType,Expected);
  1850. AssertEquals('MsgType',Expected,Actual);
  1851. exit;
  1852. end;
  1853. // needed message missing -> show emitted messages
  1854. WriteSources('',0,0);
  1855. for i:=0 to MsgCount-1 do
  1856. begin
  1857. Item:=Msgs[i];
  1858. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1859. ' ('+IntToStr(Item.MsgNumber),')');
  1860. if Marker<>nil then
  1861. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1862. writeln(' {',Item.Msg,'}');
  1863. end;
  1864. str(MsgType,Expected);
  1865. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1866. if Marker<>nil then
  1867. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1868. Actual:=Actual+' '+Msg;
  1869. Fail(Actual);
  1870. end;
  1871. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1872. );
  1873. var
  1874. i: Integer;
  1875. s, Txt: String;
  1876. Msg: TTestHintMessage;
  1877. begin
  1878. for i:=0 to MsgCount-1 do
  1879. begin
  1880. Msg:=Msgs[i];
  1881. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1882. s:='';
  1883. str(Msg.MsgType,s);
  1884. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1885. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1886. if WithSourcePos then
  1887. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1888. Txt:=Txt+' {'+Msg.Msg+'}';
  1889. Fail(Txt);
  1890. end;
  1891. end;
  1892. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1893. MsgNumber: integer);
  1894. begin
  1895. ExpectedErrorClass:=EScannerError;
  1896. ExpectedErrorMsg:=Msg;
  1897. ExpectedErrorNumber:=MsgNumber;
  1898. end;
  1899. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1900. MsgNumber: integer);
  1901. begin
  1902. ExpectedErrorClass:=EParserError;
  1903. ExpectedErrorMsg:=Msg;
  1904. ExpectedErrorNumber:=MsgNumber;
  1905. end;
  1906. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1907. MsgNumber: integer);
  1908. begin
  1909. ExpectedErrorClass:=EPasResolve;
  1910. ExpectedErrorMsg:=Msg;
  1911. ExpectedErrorNumber:=MsgNumber;
  1912. end;
  1913. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1914. MsgNumber: integer);
  1915. begin
  1916. ExpectedErrorClass:=EPas2JS;
  1917. ExpectedErrorMsg:=Msg;
  1918. ExpectedErrorNumber:=MsgNumber;
  1919. end;
  1920. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1921. var
  1922. MsgNumber: Integer;
  1923. Msg: String;
  1924. begin
  1925. Result:=false;
  1926. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1927. Msg:=E.Message;
  1928. if E is EPas2JS then
  1929. MsgNumber:=EPas2JS(E).MsgNumber
  1930. else if E is EPasResolve then
  1931. MsgNumber:=EPasResolve(E).MsgNumber
  1932. else if E is EParserError then
  1933. MsgNumber:=Parser.LastMsgNumber
  1934. else if E is EScannerError then
  1935. begin
  1936. MsgNumber:=Scanner.LastMsgNumber;
  1937. Msg:=Scanner.LastMsg;
  1938. end
  1939. else
  1940. MsgNumber:=0;
  1941. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1942. if Result then
  1943. SkipTests:=true;
  1944. end;
  1945. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1946. begin
  1947. if IsErrorExpected(E) then exit;
  1948. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1949. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1950. +' '+Scanner.CurFilename
  1951. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1952. FailException(E);
  1953. end;
  1954. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1955. begin
  1956. if IsErrorExpected(E) then exit;
  1957. WriteSources(E.Filename,E.Row,E.Column);
  1958. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1959. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1960. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1961. );
  1962. FailException(E);
  1963. end;
  1964. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1965. var
  1966. P: TPasSourcePos;
  1967. begin
  1968. if IsErrorExpected(E) then exit;
  1969. P:=E.SourcePos;
  1970. WriteSources(P.FileName,P.Row,P.Column);
  1971. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  1972. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  1973. FailException(E);
  1974. end;
  1975. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  1976. var
  1977. Row, Col: integer;
  1978. begin
  1979. if IsErrorExpected(E) then exit;
  1980. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  1981. WriteSources(E.PasElement.SourceFilename,Row,Col);
  1982. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  1983. +' '+E.PasElement.SourceFilename
  1984. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  1985. FailException(E);
  1986. end;
  1987. procedure TCustomTestModule.HandleException(E: Exception);
  1988. begin
  1989. if E is EScannerError then
  1990. HandleScannerError(EScannerError(E))
  1991. else if E is EParserError then
  1992. HandleParserError(EParserError(E))
  1993. else if E is EPasResolve then
  1994. HandlePasResolveError(EPasResolve(E))
  1995. else if E is EPas2JS then
  1996. HandlePas2JSError(EPas2JS(E))
  1997. else
  1998. begin
  1999. if IsErrorExpected(E) then exit;
  2000. if not (E is EAssertionFailedError) then
  2001. begin
  2002. WriteSources('',0,0);
  2003. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2004. end;
  2005. FailException(E);
  2006. end;
  2007. end;
  2008. procedure TCustomTestModule.FailException(E: Exception);
  2009. var
  2010. MsgNumber: Integer;
  2011. begin
  2012. if ExpectedErrorClass<>nil then
  2013. begin
  2014. if FExpectedErrorClass=E.ClassType then
  2015. begin
  2016. if E is EPas2JS then
  2017. MsgNumber:=EPas2JS(E).MsgNumber
  2018. else if E is EPasResolve then
  2019. MsgNumber:=EPasResolve(E).MsgNumber
  2020. else if E is EParserError then
  2021. MsgNumber:=Parser.LastMsgNumber
  2022. else if E is EScannerError then
  2023. MsgNumber:=Scanner.LastMsgNumber
  2024. else
  2025. MsgNumber:=0;
  2026. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2027. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2028. ExpectedErrorNumber,MsgNumber);
  2029. end else begin
  2030. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2031. end;
  2032. end;
  2033. Fail(E.Message);
  2034. end;
  2035. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2036. aCol: integer);
  2037. var
  2038. IsSrc: Boolean;
  2039. i, j: Integer;
  2040. SrcLines: TStringList;
  2041. Line: string;
  2042. aModule: TTestEnginePasResolver;
  2043. begin
  2044. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2045. for i:=0 to ResolverCount-1 do
  2046. begin
  2047. aModule:=Resolvers[i];
  2048. SrcLines:=TStringList.Create;
  2049. try
  2050. SrcLines.Text:=aModule.Source;
  2051. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2052. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2053. for j:=1 to SrcLines.Count do
  2054. begin
  2055. Line:=SrcLines[j-1];
  2056. if IsSrc and (j=aRow) then
  2057. begin
  2058. write('*');
  2059. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2060. end;
  2061. writeln(Format('%:4d: ',[j]),Line);
  2062. end;
  2063. finally
  2064. SrcLines.Free;
  2065. end;
  2066. end;
  2067. end;
  2068. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2069. var
  2070. i: Integer;
  2071. begin
  2072. for i:=0 to ResolverCount-1 do
  2073. if Filename=Resolvers[i].Filename then exit(i);
  2074. Result:=-1;
  2075. end;
  2076. function TCustomTestModule.GetResolver(const Filename: string
  2077. ): TTestEnginePasResolver;
  2078. var
  2079. i: Integer;
  2080. begin
  2081. i:=IndexOfResolver(Filename);
  2082. if i<0 then exit(nil);
  2083. Result:=Resolvers[i];
  2084. end;
  2085. function TCustomTestModule.GetDefaultNamespace: string;
  2086. var
  2087. C: TClass;
  2088. begin
  2089. Result:='';
  2090. if FModule=nil then exit;
  2091. C:=FModule.ClassType;
  2092. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2093. Result:=Engine.DefaultNameSpace;
  2094. end;
  2095. constructor TCustomTestModule.Create;
  2096. begin
  2097. inherited Create;
  2098. FHintMsgs:=TObjectList.Create(true);
  2099. FHintMsgsGood:=TFPList.Create;
  2100. end;
  2101. destructor TCustomTestModule.Destroy;
  2102. begin
  2103. FreeAndNil(FHintMsgs);
  2104. FreeAndNil(FHintMsgsGood);
  2105. inherited Destroy;
  2106. end;
  2107. { TTestModule }
  2108. procedure TTestModule.TestReservedWords;
  2109. var
  2110. i: integer;
  2111. begin
  2112. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2113. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2114. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2115. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2116. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2117. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2118. end;
  2119. procedure TTestModule.TestEmptyProgram;
  2120. begin
  2121. StartProgram(false);
  2122. Add('begin');
  2123. ConvertProgram;
  2124. CheckSource('TestEmptyProgram','','');
  2125. end;
  2126. procedure TTestModule.TestEmptyProgramUseStrict;
  2127. begin
  2128. Converter.Options:=Converter.Options+[coUseStrict];
  2129. StartProgram(false);
  2130. Add('begin');
  2131. ConvertProgram;
  2132. CheckSource('TestEmptyProgramUseStrict','','');
  2133. end;
  2134. procedure TTestModule.TestEmptyUnit;
  2135. begin
  2136. StartUnit(false);
  2137. Add('interface');
  2138. Add('implementation');
  2139. ConvertUnit;
  2140. CheckSource('TestEmptyUnit',
  2141. LinesToStr([
  2142. ]),
  2143. '');
  2144. end;
  2145. procedure TTestModule.TestEmptyUnitUseStrict;
  2146. begin
  2147. Converter.Options:=Converter.Options+[coUseStrict];
  2148. StartUnit(false);
  2149. Add('interface');
  2150. Add('implementation');
  2151. ConvertUnit;
  2152. CheckSource('TestEmptyUnitUseStrict',
  2153. LinesToStr([
  2154. ''
  2155. ]),
  2156. '');
  2157. end;
  2158. procedure TTestModule.TestDottedUnitNames;
  2159. begin
  2160. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2161. LinesToStr([
  2162. 'var iV: longint;'
  2163. ]),
  2164. '');
  2165. FFilename:='ns1.test1.pp';
  2166. StartProgram(true);
  2167. Add('uses unIt2;');
  2168. Add('implementation');
  2169. Add('var');
  2170. Add(' i: longint;');
  2171. Add('begin');
  2172. Add(' i:=iv;');
  2173. Add(' i:=uNit2.iv;');
  2174. Add(' i:=Ns1.TEst1.i;');
  2175. ConvertProgram;
  2176. CheckSource('TestDottedUnitNames',
  2177. LinesToStr([
  2178. 'this.i = 0;',
  2179. '']),
  2180. LinesToStr([ // this.$init
  2181. '$mod.i = pas["NS1.Unit2"].iV;',
  2182. '$mod.i = pas["NS1.Unit2"].iV;',
  2183. '$mod.i = $mod.i;',
  2184. '']) );
  2185. end;
  2186. procedure TTestModule.TestDottedUnitNameImpl;
  2187. begin
  2188. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2189. LinesToStr([
  2190. 'type',
  2191. ' TObject = class end;',
  2192. ' TTestA = class',
  2193. ' end;'
  2194. ]),
  2195. LinesToStr(['uses TEST.UnitB;'])
  2196. );
  2197. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2198. LinesToStr([
  2199. 'uses TEST.UnitA;',
  2200. 'type TTestB = class(TTestA);'
  2201. ]),
  2202. ''
  2203. );
  2204. StartProgram(true);
  2205. Add('uses TEST.UnitA;');
  2206. Add('begin');
  2207. ConvertProgram;
  2208. CheckSource('TestDottedUnitNameImpl',
  2209. LinesToStr([
  2210. '']),
  2211. LinesToStr([ // this.$init
  2212. '']) );
  2213. CheckUnit('TEST.UnitA.pas',
  2214. LinesToStr([
  2215. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2216. ' var $mod = this;',
  2217. ' rtl.createClass($mod, "TObject", null, function () {',
  2218. ' this.$init = function () {',
  2219. ' };',
  2220. ' this.$final = function () {',
  2221. ' };',
  2222. ' });',
  2223. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2224. ' });',
  2225. '}, ["TEST.UnitB"]);'
  2226. ]));
  2227. CheckUnit('TEST.UnitB.pas',
  2228. LinesToStr([
  2229. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2230. ' var $mod = this;',
  2231. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2232. ' });',
  2233. '});'
  2234. ]));
  2235. end;
  2236. procedure TTestModule.TestDottedUnitExpr;
  2237. begin
  2238. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2239. LinesToStr([
  2240. 'procedure DoIt;'
  2241. ]),
  2242. 'procedure DoIt; begin end;');
  2243. FFilename:='Ns1.SubNs1.Test1.pp';
  2244. StartProgram(true);
  2245. Add('uses Ns2.sUbnS2.unIt2;');
  2246. Add('implementation');
  2247. Add('var');
  2248. Add(' i: longint;');
  2249. Add('begin');
  2250. Add(' ns2.subns2.unit2.doit;');
  2251. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2252. ConvertProgram;
  2253. CheckSource('TestDottedUnitExpr',
  2254. LinesToStr([
  2255. 'this.i = 0;',
  2256. '']),
  2257. LinesToStr([ // this.$init
  2258. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2259. '$mod.i = $mod.i;',
  2260. '']) );
  2261. end;
  2262. procedure TTestModule.Test_ModeFPCFail;
  2263. begin
  2264. StartProgram(false);
  2265. Add('{$mode FPC}');
  2266. Add('begin');
  2267. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2268. ConvertProgram;
  2269. end;
  2270. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2271. begin
  2272. StartProgram(false);
  2273. Add('{$modeswitch cblocks-}');
  2274. Add('begin');
  2275. SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
  2276. ConvertProgram;
  2277. end;
  2278. procedure TTestModule.TestUnit_UseSystem;
  2279. begin
  2280. StartUnit(true);
  2281. Add([
  2282. 'interface',
  2283. 'var i: integer;',
  2284. 'implementation']);
  2285. ConvertUnit;
  2286. CheckSource('TestUnit_UseSystem',
  2287. LinesToStr([
  2288. 'this.i = 0;',
  2289. '']),
  2290. LinesToStr([
  2291. '']) );
  2292. end;
  2293. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2294. begin
  2295. AddModuleWithIntfImplSrc('unit1.pp',
  2296. LinesToStr([
  2297. 'type number = longint;']),
  2298. LinesToStr([
  2299. 'uses test1;',
  2300. 'procedure DoIt;',
  2301. 'begin',
  2302. ' i:=3;',
  2303. 'end;']));
  2304. StartUnit(true);
  2305. Add([
  2306. 'interface',
  2307. 'uses unit1;',
  2308. 'var i: number;',
  2309. 'implementation']);
  2310. ConvertUnit;
  2311. CheckSource('TestUnit_Intf1Impl2Intf1',
  2312. LinesToStr([
  2313. 'this.i = 0;',
  2314. '']),
  2315. LinesToStr([
  2316. '']) );
  2317. end;
  2318. procedure TTestModule.TestIncludeVersion;
  2319. begin
  2320. StartProgram(false);
  2321. Add([
  2322. 'var',
  2323. ' s: string;',
  2324. ' i: word;',
  2325. 'begin',
  2326. ' s:={$I %line%};',
  2327. ' i:={$I %linenum%};',
  2328. ' s:={$I %currentroutine%};',
  2329. ' s:={$I %pas2jsversion%};',
  2330. ' s:={$I %pas2jstarget%};',
  2331. ' s:={$I %pas2jstargetos%};',
  2332. ' s:={$I %pas2jstargetcpu%};',
  2333. ' s:={$I %file%};',
  2334. '']);
  2335. ConvertProgram;
  2336. CheckSource('TestIncludeVersion',
  2337. LinesToStr([
  2338. 'this.s="";',
  2339. 'this.i = 0;']),
  2340. LinesToStr([
  2341. '$mod.s = "7";',
  2342. '$mod.i = 8;',
  2343. '$mod.s = "<anonymous>";',
  2344. '$mod.s = "Comp.Ver.tcmodules";',
  2345. '$mod.s = "Browser";',
  2346. '$mod.s = "Browser";',
  2347. '$mod.s = "ECMAScript5";',
  2348. '$mod.s = "test1.pp";',
  2349. '']));
  2350. end;
  2351. procedure TTestModule.TestVarInt;
  2352. begin
  2353. StartProgram(false);
  2354. Add('var MyI: longint;');
  2355. Add('begin');
  2356. ConvertProgram;
  2357. CheckSource('TestVarInt','this.MyI=0;','');
  2358. end;
  2359. procedure TTestModule.TestVarBaseTypes;
  2360. begin
  2361. StartProgram(false);
  2362. Add('var');
  2363. Add(' i: longint;');
  2364. Add(' s: string;');
  2365. Add(' c: char;');
  2366. Add(' b: boolean;');
  2367. Add(' d: double;');
  2368. Add(' i2: longint = 3;');
  2369. Add(' s2: string = ''foo'';');
  2370. Add(' c2: char = ''4'';');
  2371. Add(' b2: boolean = true;');
  2372. Add(' d2: double = 5.6;');
  2373. Add(' i3: longint = $707;');
  2374. Add(' i4: nativeint = 4503599627370495;');
  2375. Add(' i5: nativeint = -4503599627370495-1;');
  2376. Add(' i6: nativeint = $fffffffffffff;');
  2377. Add(' i7: nativeint = -$fffffffffffff-1;');
  2378. Add(' i8: byte = 00;');
  2379. Add(' u8: nativeuint = $fffffffffffff;');
  2380. Add(' u9: nativeuint = $0000000000000;');
  2381. Add(' u10: nativeuint = $00ff00;');
  2382. Add('begin');
  2383. ConvertProgram;
  2384. CheckSource('TestVarBaseTypes',
  2385. LinesToStr([
  2386. 'this.i = 0;',
  2387. 'this.s = "";',
  2388. 'this.c = "";',
  2389. 'this.b = false;',
  2390. 'this.d = 0.0;',
  2391. 'this.i2 = 3;',
  2392. 'this.s2 = "foo";',
  2393. 'this.c2 = "4";',
  2394. 'this.b2 = true;',
  2395. 'this.d2 = 5.6;',
  2396. 'this.i3 = 0x707;',
  2397. 'this.i4 = 4503599627370495;',
  2398. 'this.i5 = -4503599627370495-1;',
  2399. 'this.i6 = 0xfffffffffffff;',
  2400. 'this.i7 =-0xfffffffffffff-1;',
  2401. 'this.i8 = 0;',
  2402. 'this.u8 = 0xfffffffffffff;',
  2403. 'this.u9 = 0x0;',
  2404. 'this.u10 = 0xff00;'
  2405. ]),
  2406. '');
  2407. end;
  2408. procedure TTestModule.TestBaseTypeSingleFail;
  2409. begin
  2410. StartProgram(false);
  2411. Add('var s: single;');
  2412. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2413. ConvertProgram;
  2414. end;
  2415. procedure TTestModule.TestBaseTypeExtendedFail;
  2416. begin
  2417. StartProgram(false);
  2418. Add('var e: extended;');
  2419. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2420. ConvertProgram;
  2421. end;
  2422. procedure TTestModule.TestConstBaseTypes;
  2423. begin
  2424. StartProgram(false);
  2425. Add('const');
  2426. Add(' i: longint = 3;');
  2427. Add(' s: string = ''foo'';');
  2428. Add(' c: char = ''4'';');
  2429. Add(' b: boolean = true;');
  2430. Add(' d: double = 5.6;');
  2431. Add(' e = low(word);');
  2432. Add(' f = high(word);');
  2433. Add('begin');
  2434. ConvertProgram;
  2435. CheckSource('TestVarBaseTypes',
  2436. LinesToStr([
  2437. 'this.i=3;',
  2438. 'this.s="foo";',
  2439. 'this.c="4";',
  2440. 'this.b=true;',
  2441. 'this.d=5.6;',
  2442. 'this.e = 0;',
  2443. 'this.f = 65535;'
  2444. ]),
  2445. '');
  2446. end;
  2447. procedure TTestModule.TestAliasTypeRef;
  2448. begin
  2449. StartProgram(false);
  2450. Add('type');
  2451. Add(' a=longint;');
  2452. Add(' b=a;');
  2453. Add('var');
  2454. Add(' c: A;');
  2455. Add(' d: B;');
  2456. Add('begin');
  2457. ConvertProgram;
  2458. CheckSource('TestAliasTypeRef',
  2459. LinesToStr([ // statements
  2460. 'this.c = 0;',
  2461. 'this.d = 0;'
  2462. ]),
  2463. LinesToStr([ // this.$main
  2464. ''
  2465. ]));
  2466. end;
  2467. procedure TTestModule.TestTypeCast_BaseTypes;
  2468. begin
  2469. StartProgram(false);
  2470. Add([
  2471. 'var',
  2472. ' i: longint;',
  2473. ' b: boolean;',
  2474. ' d: double;',
  2475. ' s: string;',
  2476. ' c: char;',
  2477. 'begin',
  2478. ' i:=longint(i);',
  2479. ' i:=longint(b);',
  2480. ' b:=boolean(b);',
  2481. ' b:=boolean(i);',
  2482. ' d:=double(d);',
  2483. ' d:=double(i);',
  2484. ' s:=string(s);',
  2485. ' s:=string(c);',
  2486. ' c:=char(c);',
  2487. ' c:=char(i);',
  2488. ' c:=char(65);',
  2489. ' c:=char(#10);',
  2490. ' c:=char(#$E000);',
  2491. '']);
  2492. ConvertProgram;
  2493. CheckSource('TestAliasTypeRef',
  2494. LinesToStr([ // statements
  2495. 'this.i = 0;',
  2496. 'this.b = false;',
  2497. 'this.d = 0.0;',
  2498. 'this.s = "";',
  2499. 'this.c = "";',
  2500. '']),
  2501. LinesToStr([ // this.$main
  2502. '$mod.i = $mod.i;',
  2503. '$mod.i = ($mod.b ? 1 : 0);',
  2504. '$mod.b = $mod.b;',
  2505. '$mod.b = $mod.i != 0;',
  2506. '$mod.d = $mod.d;',
  2507. '$mod.d = $mod.i;',
  2508. '$mod.s = $mod.s;',
  2509. '$mod.s = $mod.c;',
  2510. '$mod.c = $mod.c;',
  2511. '$mod.c = String.fromCharCode($mod.i);',
  2512. '$mod.c = "A";',
  2513. '$mod.c = "\n";',
  2514. '$mod.c = "";',
  2515. '']));
  2516. end;
  2517. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2518. begin
  2519. StartProgram(false);
  2520. Add('type');
  2521. Add(' integer = longint;');
  2522. Add(' TYesNo = boolean;');
  2523. Add(' TFloat = double;');
  2524. Add(' TCaption = string;');
  2525. Add(' TChar = char;');
  2526. Add('var');
  2527. Add(' i: integer;');
  2528. Add(' b: TYesNo;');
  2529. Add(' d: TFloat;');
  2530. Add(' s: TCaption;');
  2531. Add(' c: TChar;');
  2532. Add('begin');
  2533. Add(' i:=integer(i);');
  2534. Add(' i:=integer(b);');
  2535. Add(' b:=TYesNo(b);');
  2536. Add(' b:=TYesNo(i);');
  2537. Add(' d:=TFloat(d);');
  2538. Add(' d:=TFloat(i);');
  2539. Add(' s:=TCaption(s);');
  2540. Add(' s:=TCaption(c);');
  2541. Add(' c:=TChar(c);');
  2542. ConvertProgram;
  2543. CheckSource('TestAliasTypeRef',
  2544. LinesToStr([ // statements
  2545. 'this.i = 0;',
  2546. 'this.b = false;',
  2547. 'this.d = 0.0;',
  2548. 'this.s = "";',
  2549. 'this.c = "";',
  2550. '']),
  2551. LinesToStr([ // this.$main
  2552. '$mod.i = $mod.i;',
  2553. '$mod.i = ($mod.b ? 1 : 0);',
  2554. '$mod.b = $mod.b;',
  2555. '$mod.b = $mod.i != 0;',
  2556. '$mod.d = $mod.d;',
  2557. '$mod.d = $mod.i;',
  2558. '$mod.s = $mod.s;',
  2559. '$mod.s = $mod.c;',
  2560. '$mod.c = $mod.c;',
  2561. '']));
  2562. end;
  2563. procedure TTestModule.TestEmptyProc;
  2564. begin
  2565. StartProgram(false);
  2566. Add('procedure Test;');
  2567. Add('begin');
  2568. Add('end;');
  2569. Add('begin');
  2570. ConvertProgram;
  2571. CheckSource('TestEmptyProc',
  2572. LinesToStr([ // statements
  2573. 'this.Test = function () {',
  2574. '};'
  2575. ]),
  2576. LinesToStr([ // this.$main
  2577. ''
  2578. ]));
  2579. end;
  2580. procedure TTestModule.TestProcOneParam;
  2581. begin
  2582. StartProgram(false);
  2583. Add('procedure ProcA(i: longint);');
  2584. Add('begin');
  2585. Add('end;');
  2586. Add('begin');
  2587. Add(' PROCA(3);');
  2588. ConvertProgram;
  2589. CheckSource('TestProcOneParam',
  2590. LinesToStr([ // statements
  2591. 'this.ProcA = function (i) {',
  2592. '};'
  2593. ]),
  2594. LinesToStr([ // this.$main
  2595. '$mod.ProcA(3);'
  2596. ]));
  2597. end;
  2598. procedure TTestModule.TestFunctionWithoutParams;
  2599. begin
  2600. StartProgram(false);
  2601. Add('function FuncA: longint;');
  2602. Add('begin');
  2603. Add('end;');
  2604. Add('var i: longint;');
  2605. Add('begin');
  2606. Add(' I:=FUNCA();');
  2607. Add(' I:=FUNCA;');
  2608. Add(' FUNCA();');
  2609. Add(' FUNCA;');
  2610. ConvertProgram;
  2611. CheckSource('TestProcWithoutParams',
  2612. LinesToStr([ // statements
  2613. 'this.FuncA = function () {',
  2614. ' var Result = 0;',
  2615. ' return Result;',
  2616. '};',
  2617. 'this.i=0;'
  2618. ]),
  2619. LinesToStr([ // this.$main
  2620. '$mod.i=$mod.FuncA();',
  2621. '$mod.i=$mod.FuncA();',
  2622. '$mod.FuncA();',
  2623. '$mod.FuncA();'
  2624. ]));
  2625. end;
  2626. procedure TTestModule.TestProcedureWithoutParams;
  2627. begin
  2628. StartProgram(false);
  2629. Add('procedure ProcA;');
  2630. Add('begin');
  2631. Add('end;');
  2632. Add('begin');
  2633. Add(' PROCA();');
  2634. Add(' PROCA;');
  2635. ConvertProgram;
  2636. CheckSource('TestProcWithoutParams',
  2637. LinesToStr([ // statements
  2638. 'this.ProcA = function () {',
  2639. '};'
  2640. ]),
  2641. LinesToStr([ // this.$main
  2642. '$mod.ProcA();',
  2643. '$mod.ProcA();'
  2644. ]));
  2645. end;
  2646. procedure TTestModule.TestIncDec;
  2647. begin
  2648. StartProgram(false);
  2649. Add([
  2650. 'procedure DoIt(var i: longint);',
  2651. 'begin',
  2652. ' inc(i);',
  2653. ' inc(i,2);',
  2654. 'end;',
  2655. 'var',
  2656. ' Bar: longint;',
  2657. 'begin',
  2658. ' inc(bar);',
  2659. ' inc(bar,2);',
  2660. ' dec(bar);',
  2661. ' dec(bar,3);',
  2662. '']);
  2663. ConvertProgram;
  2664. CheckSource('TestIncDec',
  2665. LinesToStr([ // statements
  2666. 'this.DoIt = function (i) {',
  2667. ' i.set(i.get()+1);',
  2668. ' i.set(i.get()+2);',
  2669. '};',
  2670. 'this.Bar = 0;'
  2671. ]),
  2672. LinesToStr([ // this.$main
  2673. '$mod.Bar+=1;',
  2674. '$mod.Bar+=2;',
  2675. '$mod.Bar-=1;',
  2676. '$mod.Bar-=3;'
  2677. ]));
  2678. end;
  2679. procedure TTestModule.TestLoHiFpcMode;
  2680. begin
  2681. StartProgram(false);
  2682. Add([
  2683. '{$mode objfpc}',
  2684. 'const',
  2685. ' LoByte1 = Lo(Word($1234));',
  2686. ' HiByte1 = Hi(Word($1234));',
  2687. ' LoByte2 = Lo(SmallInt($1234));',
  2688. ' HiByte2 = Hi(SmallInt($1234));',
  2689. ' LoWord1 = Lo($1234CDEF);',
  2690. ' HiWord1 = Hi($1234CDEF);',
  2691. ' LoWord2 = Lo(-$1234CDEF);',
  2692. ' HiWord2 = Hi(-$1234CDEF);',
  2693. ' lo4:byte=lo(byte($34));',
  2694. ' hi4:byte=hi(byte($34));',
  2695. ' lo5:byte=lo(shortint(-$34));',
  2696. ' hi5:byte=hi(shortint(-$34));',
  2697. ' lo6:longword=lo($123456789ABCD);',
  2698. ' hi6:longword=hi($123456789ABCD);',
  2699. ' lo7:longword=lo(-$123456789ABCD);',
  2700. ' hi7:longword=hi(-$123456789ABCD);',
  2701. 'var',
  2702. ' b: Byte;',
  2703. ' ss: shortint;',
  2704. ' w: Word;',
  2705. ' si: SmallInt;',
  2706. ' lw: LongWord;',
  2707. ' li: LongInt;',
  2708. ' b2: Byte;',
  2709. ' ni: nativeint;',
  2710. 'begin',
  2711. ' w := $1234;',
  2712. ' ss := -$12;',
  2713. ' b := lo(ss);',
  2714. ' b := HI(ss);',
  2715. ' b := lo(w);',
  2716. ' b := HI(w);',
  2717. ' b2 := lo(b);',
  2718. ' b2 := hi(b);',
  2719. ' lw := $1234CDEF;',
  2720. ' w := lo(lw);',
  2721. ' w := hi(lw);',
  2722. ' ni := $123456789ABCD;',
  2723. ' lw := lo(ni);',
  2724. ' lw := hi(ni);',
  2725. '']);
  2726. ConvertProgram;
  2727. CheckSource('TestLoHiFpcMode',
  2728. LinesToStr([ // statements
  2729. 'this.LoByte1 = 0x1234 & 0xFF;',
  2730. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2731. 'this.LoByte2 = 0x1234 & 0xFF;',
  2732. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2733. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2734. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2735. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2736. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2737. 'this.lo4 = 0x34 & 0xF;',
  2738. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2739. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2740. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2741. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2742. 'this.hi6 = 74565 >>> 0;',
  2743. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2744. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2745. 'this.b = 0;',
  2746. 'this.ss = 0;',
  2747. 'this.w = 0;',
  2748. 'this.si = 0;',
  2749. 'this.lw = 0;',
  2750. 'this.li = 0;',
  2751. 'this.b2 = 0;',
  2752. 'this.ni = 0;',
  2753. '']),
  2754. LinesToStr([ // this.$main
  2755. '$mod.w = 0x1234;',
  2756. '$mod.ss = -0x12;',
  2757. '$mod.b = $mod.ss & 0xFF;',
  2758. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2759. '$mod.b = $mod.w & 0xFF;',
  2760. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2761. '$mod.b2 = $mod.b & 0xF;',
  2762. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2763. '$mod.lw = 0x1234CDEF;',
  2764. '$mod.w = $mod.lw & 0xFFFF;',
  2765. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2766. '$mod.ni = 0x123456789ABCD;',
  2767. '$mod.lw = $mod.ni >>> 0;',
  2768. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2769. '']));
  2770. end;
  2771. procedure TTestModule.TestLoHiDelphiMode;
  2772. begin
  2773. StartProgram(false);
  2774. Add([
  2775. '{$mode delphi}',
  2776. 'const',
  2777. ' LoByte1 = Lo(Word($1234));',
  2778. ' HiByte1 = Hi(Word($1234));',
  2779. ' LoByte2 = Lo(SmallInt($1234));',
  2780. ' HiByte2 = Hi(SmallInt($1234));',
  2781. ' LoByte3 = Lo($1234CDEF);',
  2782. ' HiByte3 = Hi($1234CDEF);',
  2783. ' LoByte4 = Lo(-$1234CDEF);',
  2784. ' HiByte4 = Hi(-$1234CDEF);',
  2785. 'var',
  2786. ' b: Byte;',
  2787. ' w: Word;',
  2788. ' si: SmallInt;',
  2789. ' lw: LongWord;',
  2790. ' li: LongInt;',
  2791. 'begin',
  2792. ' w := $1234;',
  2793. ' b := lo(w);',
  2794. ' b := HI(w);',
  2795. ' lw := $1234CDEF;',
  2796. ' b := lo(lw);',
  2797. ' b := hi(lw);',
  2798. '']);
  2799. ConvertProgram;
  2800. CheckSource('TestLoHiDelphiMode',
  2801. LinesToStr([ // statements
  2802. 'this.LoByte1 = 0x1234 & 0xFF;',
  2803. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2804. 'this.LoByte2 = 0x1234 & 0xFF;',
  2805. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2806. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2807. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2808. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2809. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2810. 'this.b = 0;',
  2811. 'this.w = 0;',
  2812. 'this.si = 0;',
  2813. 'this.lw = 0;',
  2814. 'this.li = 0;'
  2815. ]),
  2816. LinesToStr([ // this.$main
  2817. '$mod.w = 0x1234;',
  2818. '$mod.b = $mod.w & 0xFF;',
  2819. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2820. '$mod.lw = 0x1234CDEF;',
  2821. '$mod.b = $mod.lw & 0xFF;',
  2822. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2823. ]));
  2824. end;
  2825. procedure TTestModule.TestAssignments;
  2826. begin
  2827. StartProgram(false);
  2828. Parser.Options:=Parser.Options+[po_cassignments];
  2829. Add('var');
  2830. Add(' Bar:longint;');
  2831. Add('begin');
  2832. Add(' bar:=3;');
  2833. Add(' bar+=4;');
  2834. Add(' bar-=5;');
  2835. Add(' bar*=6;');
  2836. ConvertProgram;
  2837. CheckSource('TestAssignments',
  2838. LinesToStr([ // statements
  2839. 'this.Bar = 0;'
  2840. ]),
  2841. LinesToStr([ // this.$main
  2842. '$mod.Bar=3;',
  2843. '$mod.Bar+=4;',
  2844. '$mod.Bar-=5;',
  2845. '$mod.Bar*=6;'
  2846. ]));
  2847. end;
  2848. procedure TTestModule.TestArithmeticOperators1;
  2849. begin
  2850. StartProgram(false);
  2851. Add('var');
  2852. Add(' vA,vB,vC:longint;');
  2853. Add('begin');
  2854. Add(' va:=1;');
  2855. Add(' vb:=va+va;');
  2856. Add(' vb:=va div vb;');
  2857. Add(' vb:=va mod vb;');
  2858. Add(' vb:=va+va*vb+va div vb;');
  2859. Add(' vc:=-va;');
  2860. Add(' va:=va-vb;');
  2861. Add(' vb:=va;');
  2862. Add(' if va<vb then vc:=va else vc:=vb;');
  2863. ConvertProgram;
  2864. CheckSource('TestArithmeticOperators1',
  2865. LinesToStr([ // statements
  2866. 'this.vA = 0;',
  2867. 'this.vB = 0;',
  2868. 'this.vC = 0;'
  2869. ]),
  2870. LinesToStr([ // this.$main
  2871. '$mod.vA = 1;',
  2872. '$mod.vB = $mod.vA + $mod.vA;',
  2873. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2874. '$mod.vB = $mod.vA % $mod.vB;',
  2875. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2876. '$mod.vC = -$mod.vA;',
  2877. '$mod.vA = $mod.vA - $mod.vB;',
  2878. '$mod.vB = $mod.vA;',
  2879. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2880. ]));
  2881. end;
  2882. procedure TTestModule.TestLogicalOperators;
  2883. begin
  2884. StartProgram(false);
  2885. Add('var');
  2886. Add(' vA,vB,vC:boolean;');
  2887. Add('begin');
  2888. Add(' va:=vb and vc;');
  2889. Add(' va:=vb or vc;');
  2890. Add(' va:=vb xor vc;');
  2891. Add(' va:=true and vc;');
  2892. Add(' va:=(vb and vc) or (va and vb);');
  2893. Add(' va:=not vb;');
  2894. ConvertProgram;
  2895. CheckSource('TestLogicalOperators',
  2896. LinesToStr([ // statements
  2897. 'this.vA = false;',
  2898. 'this.vB = false;',
  2899. 'this.vC = false;'
  2900. ]),
  2901. LinesToStr([ // this.$main
  2902. '$mod.vA = $mod.vB && $mod.vC;',
  2903. '$mod.vA = $mod.vB || $mod.vC;',
  2904. '$mod.vA = $mod.vB ^ $mod.vC;',
  2905. '$mod.vA = true && $mod.vC;',
  2906. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2907. '$mod.vA = !$mod.vB;'
  2908. ]));
  2909. end;
  2910. procedure TTestModule.TestBitwiseOperators;
  2911. begin
  2912. StartProgram(false);
  2913. Add([
  2914. 'var',
  2915. ' vA,vB,vC:longint;',
  2916. ' X,Y,Z: nativeint;',
  2917. 'begin',
  2918. ' va:=vb and vc;',
  2919. ' va:=vb or vc;',
  2920. ' va:=vb xor vc;',
  2921. ' va:=vb shl vc;',
  2922. ' va:=vb shr vc;',
  2923. ' va:=3 and vc;',
  2924. ' va:=(vb and vc) or (va and vb);',
  2925. ' va:=not vb;',
  2926. ' X:=Y and Z;',
  2927. ' X:=Y and va;',
  2928. ' X:=Y or Z;',
  2929. ' X:=Y or va;',
  2930. ' X:=Y xor Z;',
  2931. ' X:=Y xor va;',
  2932. '']);
  2933. ConvertProgram;
  2934. CheckSource('TestBitwiseOperators',
  2935. LinesToStr([ // statements
  2936. 'this.vA = 0;',
  2937. 'this.vB = 0;',
  2938. 'this.vC = 0;',
  2939. 'this.X = 0;',
  2940. 'this.Y = 0;',
  2941. 'this.Z = 0;',
  2942. '']),
  2943. LinesToStr([ // this.$main
  2944. '$mod.vA = $mod.vB & $mod.vC;',
  2945. '$mod.vA = $mod.vB | $mod.vC;',
  2946. '$mod.vA = $mod.vB ^ $mod.vC;',
  2947. '$mod.vA = $mod.vB << $mod.vC;',
  2948. '$mod.vA = $mod.vB >>> $mod.vC;',
  2949. '$mod.vA = 3 & $mod.vC;',
  2950. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2951. '$mod.vA = ~$mod.vB;',
  2952. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  2953. '$mod.X = $mod.Y & $mod.vA;',
  2954. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  2955. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  2956. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  2957. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  2958. '']));
  2959. end;
  2960. procedure TTestModule.TestBitwiseOperatorsLongword;
  2961. begin
  2962. StartProgram(false);
  2963. Add([
  2964. 'var',
  2965. ' a,b,c:longword;',
  2966. ' i: longint;',
  2967. 'begin',
  2968. ' a:=$12345678;',
  2969. ' b:=$EDCBA987;',
  2970. ' c:=not a;',
  2971. ' c:=a and b;',
  2972. ' c:=a and $ffff0000;',
  2973. ' c:=a or b;',
  2974. ' c:=a or $ff00ff00;',
  2975. ' c:=a xor b;',
  2976. ' c:=a xor $f0f0f0f0;',
  2977. ' c:=a shl 1;',
  2978. ' c:=a shl 16;',
  2979. ' c:=a shl 24;',
  2980. ' c:=a shl b;',
  2981. ' c:=a shr 1;',
  2982. ' c:=a shr 16;',
  2983. ' c:=a shr 24;',
  2984. ' c:=a shr b;',
  2985. ' c:=(b and c) or (a and b);',
  2986. ' c:=i and a;',
  2987. ' c:=i or a;',
  2988. ' c:=i xor a;',
  2989. '']);
  2990. ConvertProgram;
  2991. CheckSource('TestBitwiseOperatorsLongword',
  2992. LinesToStr([ // statements
  2993. 'this.a = 0;',
  2994. 'this.b = 0;',
  2995. 'this.c = 0;',
  2996. 'this.i = 0;',
  2997. '']),
  2998. LinesToStr([ // this.$main
  2999. '$mod.a = 0x12345678;',
  3000. '$mod.b = 0xEDCBA987;',
  3001. '$mod.c = rtl.lw(~$mod.a);',
  3002. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3003. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3004. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3005. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3006. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3007. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3008. '$mod.c = rtl.lw($mod.a << 1);',
  3009. '$mod.c = rtl.lw($mod.a << 16);',
  3010. '$mod.c = rtl.lw($mod.a << 24);',
  3011. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3012. '$mod.c = rtl.lw($mod.a >>> 1);',
  3013. '$mod.c = rtl.lw($mod.a >>> 16);',
  3014. '$mod.c = rtl.lw($mod.a >>> 24);',
  3015. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3016. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3017. '$mod.c = $mod.i & $mod.a;',
  3018. '$mod.c = $mod.i | $mod.a;',
  3019. '$mod.c = $mod.i ^ $mod.a;',
  3020. '']));
  3021. end;
  3022. procedure TTestModule.TestPrgProcVar;
  3023. begin
  3024. StartProgram(false);
  3025. Add('procedure Proc1;');
  3026. Add('type');
  3027. Add(' t1=longint;');
  3028. Add('var');
  3029. Add(' vA:t1;');
  3030. Add('begin');
  3031. Add('end;');
  3032. Add('begin');
  3033. ConvertProgram;
  3034. CheckSource('TestPrgProcVar',
  3035. LinesToStr([ // statements
  3036. 'this.Proc1 = function () {',
  3037. ' var vA=0;',
  3038. '};'
  3039. ]),
  3040. LinesToStr([ // this.$main
  3041. ''
  3042. ]));
  3043. end;
  3044. procedure TTestModule.TestUnitProcVar;
  3045. begin
  3046. StartUnit(false);
  3047. Add('interface');
  3048. Add('');
  3049. Add('type tA=string; // unit scope');
  3050. Add('procedure Proc1;');
  3051. Add('');
  3052. Add('implementation');
  3053. Add('');
  3054. Add('procedure Proc1;');
  3055. Add('type tA=longint; // local proc scope');
  3056. Add('var v1:tA; // using local tA');
  3057. Add('begin');
  3058. Add('end;');
  3059. Add('var v2:tA; // using interface tA');
  3060. ConvertUnit;
  3061. CheckSource('TestUnitProcVar',
  3062. LinesToStr([ // statements
  3063. 'var $impl = $mod.$impl;',
  3064. 'this.Proc1 = function () {',
  3065. ' var v1 = 0;',
  3066. '};',
  3067. '']),
  3068. // this.$init
  3069. '',
  3070. // implementation
  3071. LinesToStr([
  3072. '$impl.v2 = "";',
  3073. '']));
  3074. end;
  3075. procedure TTestModule.TestImplProc;
  3076. begin
  3077. StartUnit(false);
  3078. Add('interface');
  3079. Add('');
  3080. Add('procedure Proc1;');
  3081. Add('');
  3082. Add('implementation');
  3083. Add('');
  3084. Add('procedure Proc1; begin end;');
  3085. Add('procedure Proc2; begin end;');
  3086. Add('initialization');
  3087. Add(' Proc1;');
  3088. Add(' Proc2;');
  3089. ConvertUnit;
  3090. CheckSource('TestImplProc',
  3091. LinesToStr([ // statements
  3092. 'var $impl = $mod.$impl;',
  3093. 'this.Proc1 = function () {',
  3094. '};',
  3095. '']),
  3096. LinesToStr([ // this.$init
  3097. '$mod.Proc1();',
  3098. '$impl.Proc2();',
  3099. '']),
  3100. LinesToStr([ // implementation
  3101. '$impl.Proc2 = function () {',
  3102. '};',
  3103. ''])
  3104. );
  3105. end;
  3106. procedure TTestModule.TestFunctionResult;
  3107. begin
  3108. StartProgram(false);
  3109. Add('function Func1: longint;');
  3110. Add('begin');
  3111. Add(' Result:=3;');
  3112. Add(' Func1:=4;');
  3113. Add('end;');
  3114. Add('begin');
  3115. ConvertProgram;
  3116. CheckSource('TestFunctionResult',
  3117. LinesToStr([ // statements
  3118. 'this.Func1 = function () {',
  3119. ' var Result = 0;',
  3120. ' Result = 3;',
  3121. ' Result = 4;',
  3122. ' return Result;',
  3123. '};'
  3124. ]),
  3125. '');
  3126. end;
  3127. procedure TTestModule.TestNestedProc;
  3128. begin
  3129. StartProgram(false);
  3130. Add([
  3131. 'var vInUnit: longint;',
  3132. 'function DoIt(pA,pD: longint): longint;',
  3133. 'var',
  3134. ' vB: longint;',
  3135. ' vC: longint;',
  3136. ' function Nesty(pA: longint): longint; ',
  3137. ' var vB: longint;',
  3138. ' begin',
  3139. ' Result:=pa+vb+vc+pd+vInUnit;',
  3140. ' nesty:=3;',
  3141. ' doit:=4;',
  3142. ' exit;',
  3143. ' end;',
  3144. 'begin',
  3145. ' Result:=pa+vb+vc;',
  3146. ' doit:=6;',
  3147. ' exit;',
  3148. 'end;',
  3149. 'begin']);
  3150. ConvertProgram;
  3151. CheckSource('TestNestedProc',
  3152. LinesToStr([ // statements
  3153. 'this.vInUnit = 0;',
  3154. 'this.DoIt = function (pA, pD) {',
  3155. ' var Result = 0;',
  3156. ' var vB = 0;',
  3157. ' var vC = 0;',
  3158. ' function Nesty(pA) {',
  3159. ' var Result$1 = 0;',
  3160. ' var vB = 0;',
  3161. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3162. ' Result$1 = 3;',
  3163. ' Result = 4;',
  3164. ' return Result$1;',
  3165. ' return Result$1;',
  3166. ' };',
  3167. ' Result = pA + vB + vC;',
  3168. ' Result = 6;',
  3169. ' return Result;',
  3170. ' return Result;',
  3171. '};'
  3172. ]),
  3173. '');
  3174. end;
  3175. procedure TTestModule.TestNestedProc_ResultString;
  3176. begin
  3177. StartProgram(false);
  3178. Add([
  3179. 'function DoIt: string;',
  3180. ' function Nesty: string; ',
  3181. ' begin',
  3182. ' nesty:=#65#66;',
  3183. ' nesty[1]:=#67;',
  3184. ' doit:=#68;',
  3185. ' doit[2]:=#69;',
  3186. ' end;',
  3187. 'begin',
  3188. ' doit:=#70;',
  3189. ' doit[3]:=#71;',
  3190. 'end;',
  3191. 'begin']);
  3192. ConvertProgram;
  3193. CheckSource('TestNestedProc_ResultString',
  3194. LinesToStr([ // statements
  3195. 'this.DoIt = function () {',
  3196. ' var Result = "";',
  3197. ' function Nesty() {',
  3198. ' var Result$1 = "";',
  3199. ' Result$1 = "AB";',
  3200. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3201. ' Result = "D";',
  3202. ' Result = rtl.setCharAt(Result, 1, "E");',
  3203. ' return Result$1;',
  3204. ' };',
  3205. ' Result = "F";',
  3206. ' Result = rtl.setCharAt(Result, 2, "G");',
  3207. ' return Result;',
  3208. '};'
  3209. ]),
  3210. '');
  3211. end;
  3212. procedure TTestModule.TestForwardProc;
  3213. begin
  3214. StartProgram(false);
  3215. Add('procedure FuncA(Bar: longint); forward;');
  3216. Add('procedure FuncB(Bar: longint);');
  3217. Add('begin');
  3218. Add(' funca(bar);');
  3219. Add('end;');
  3220. Add('procedure funca(bar: longint);');
  3221. Add('begin');
  3222. Add(' if bar=3 then ;');
  3223. Add('end;');
  3224. Add('begin');
  3225. Add(' funca(4);');
  3226. Add(' funcb(5);');
  3227. ConvertProgram;
  3228. CheckSource('TestForwardProc',
  3229. LinesToStr([ // statements'
  3230. 'this.FuncB = function (Bar) {',
  3231. ' $mod.FuncA(Bar);',
  3232. '};',
  3233. 'this.FuncA = function (Bar) {',
  3234. ' if (Bar === 3);',
  3235. '};'
  3236. ]),
  3237. LinesToStr([
  3238. '$mod.FuncA(4);',
  3239. '$mod.FuncB(5);'
  3240. ])
  3241. );
  3242. end;
  3243. procedure TTestModule.TestNestedForwardProc;
  3244. begin
  3245. StartProgram(false);
  3246. Add('procedure FuncA;');
  3247. Add(' procedure FuncB(i: longint); forward;');
  3248. Add(' procedure FuncC(i: longint);');
  3249. Add(' begin');
  3250. Add(' funcb(i);');
  3251. Add(' end;');
  3252. Add(' procedure FuncB(i: longint);');
  3253. Add(' begin');
  3254. Add(' if i=3 then ;');
  3255. Add(' end;');
  3256. Add('begin');
  3257. Add(' funcc(4)');
  3258. Add('end;');
  3259. Add('begin');
  3260. Add(' funca;');
  3261. ConvertProgram;
  3262. CheckSource('TestNestedForwardProc',
  3263. LinesToStr([ // statements'
  3264. 'this.FuncA = function () {',
  3265. ' function FuncC(i) {',
  3266. ' FuncB(i);',
  3267. ' };',
  3268. ' function FuncB(i) {',
  3269. ' if (i === 3);',
  3270. ' };',
  3271. ' FuncC(4);',
  3272. '};'
  3273. ]),
  3274. LinesToStr([
  3275. '$mod.FuncA();'
  3276. ])
  3277. );
  3278. end;
  3279. procedure TTestModule.TestAssignFunctionResult;
  3280. begin
  3281. StartProgram(false);
  3282. Add('function Func1: longint;');
  3283. Add('begin');
  3284. Add('end;');
  3285. Add('var i: longint;');
  3286. Add('begin');
  3287. Add(' i:=func1();');
  3288. Add(' i:=func1()+func1();');
  3289. ConvertProgram;
  3290. CheckSource('TestAssignFunctionResult',
  3291. LinesToStr([ // statements
  3292. 'this.Func1 = function () {',
  3293. ' var Result = 0;',
  3294. ' return Result;',
  3295. '};',
  3296. 'this.i = 0;'
  3297. ]),
  3298. LinesToStr([
  3299. '$mod.i = $mod.Func1();',
  3300. '$mod.i = $mod.Func1() + $mod.Func1();'
  3301. ]));
  3302. end;
  3303. procedure TTestModule.TestFunctionResultInCondition;
  3304. begin
  3305. StartProgram(false);
  3306. Add('function Func1: longint;');
  3307. Add('begin');
  3308. Add('end;');
  3309. Add('function Func2: boolean;');
  3310. Add('begin');
  3311. Add('end;');
  3312. Add('var i: longint;');
  3313. Add('begin');
  3314. Add(' if func2 then ;');
  3315. Add(' if i=func1() then ;');
  3316. Add(' if i=func1 then ;');
  3317. ConvertProgram;
  3318. CheckSource('TestFunctionResultInCondition',
  3319. LinesToStr([ // statements
  3320. 'this.Func1 = function () {',
  3321. ' var Result = 0;',
  3322. ' return Result;',
  3323. '};',
  3324. 'this.Func2 = function () {',
  3325. ' var Result = false;',
  3326. ' return Result;',
  3327. '};',
  3328. 'this.i = 0;'
  3329. ]),
  3330. LinesToStr([
  3331. 'if ($mod.Func2());',
  3332. 'if ($mod.i === $mod.Func1());',
  3333. 'if ($mod.i === $mod.Func1());'
  3334. ]));
  3335. end;
  3336. procedure TTestModule.TestFunctionResultInForLoop;
  3337. begin
  3338. StartProgram(false);
  3339. Add([
  3340. 'function Func1(a: array of longint): longint;',
  3341. 'begin',
  3342. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3343. ' for Result in a do if a[Result]=0 then exit;',
  3344. 'end;',
  3345. 'begin',
  3346. ' Func1([1,2,3])']);
  3347. ConvertProgram;
  3348. CheckSource('TestFunctionResultInForLoop',
  3349. LinesToStr([ // statements
  3350. 'this.Func1 = function (a) {',
  3351. ' var Result = 0;',
  3352. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3353. ' Result = $l1;',
  3354. ' if (a[Result] === 0) return Result;',
  3355. ' };',
  3356. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3357. ' Result = $in2[$l3];',
  3358. ' if (a[Result] === 0) return Result;',
  3359. ' };',
  3360. ' return Result;',
  3361. '};',
  3362. '']),
  3363. LinesToStr([
  3364. '$mod.Func1([1, 2, 3]);'
  3365. ]));
  3366. end;
  3367. procedure TTestModule.TestFunctionResultInTypeCast;
  3368. begin
  3369. StartProgram(false);
  3370. Add([
  3371. 'function GetInt: longint;',
  3372. 'begin',
  3373. 'end;',
  3374. 'begin',
  3375. ' if Byte(GetInt)=0 then ;',
  3376. '']);
  3377. ConvertProgram;
  3378. CheckSource('TestFunctionResultInTypeCast',
  3379. LinesToStr([ // statements
  3380. 'this.GetInt = function () {',
  3381. ' var Result = 0;',
  3382. ' return Result;',
  3383. '};',
  3384. '']),
  3385. LinesToStr([
  3386. 'if (($mod.GetInt() & 255) === 0) ;'
  3387. ]));
  3388. end;
  3389. procedure TTestModule.TestExit;
  3390. begin
  3391. StartProgram(false);
  3392. Add('procedure ProcA;');
  3393. Add('begin');
  3394. Add(' exit;');
  3395. Add('end;');
  3396. Add('function FuncB: longint;');
  3397. Add('begin');
  3398. Add(' exit;');
  3399. Add(' exit(3);');
  3400. Add('end;');
  3401. Add('function FuncC: string;');
  3402. Add('begin');
  3403. Add(' exit;');
  3404. Add(' exit(''a'');');
  3405. Add(' exit(''abc'');');
  3406. Add('end;');
  3407. Add('begin');
  3408. Add(' exit;');
  3409. Add(' exit(1);');
  3410. ConvertProgram;
  3411. CheckSource('TestExit',
  3412. LinesToStr([ // statements
  3413. 'this.ProcA = function () {',
  3414. ' return;',
  3415. '};',
  3416. 'this.FuncB = function () {',
  3417. ' var Result = 0;',
  3418. ' return Result;',
  3419. ' return 3;',
  3420. ' return Result;',
  3421. '};',
  3422. 'this.FuncC = function () {',
  3423. ' var Result = "";',
  3424. ' return Result;',
  3425. ' return "a";',
  3426. ' return "abc";',
  3427. ' return Result;',
  3428. '};'
  3429. ]),
  3430. LinesToStr([
  3431. 'return;',
  3432. 'return 1;',
  3433. '']));
  3434. end;
  3435. procedure TTestModule.TestBreak;
  3436. begin
  3437. StartProgram(false);
  3438. Add([
  3439. 'var',
  3440. ' i: longint;',
  3441. 'begin',
  3442. ' repeat',
  3443. ' break;',
  3444. ' until true;',
  3445. ' while true do',
  3446. ' break;',
  3447. ' for i:=1 to 2 do',
  3448. ' break;']);
  3449. ConvertProgram;
  3450. CheckSource('TestBreak',
  3451. LinesToStr([ // statements
  3452. 'this.i = 0;'
  3453. ]),
  3454. LinesToStr([
  3455. 'do {',
  3456. ' break;',
  3457. '} while (!true);',
  3458. 'while (true) break;',
  3459. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3460. '']));
  3461. end;
  3462. procedure TTestModule.TestBreakAsVar;
  3463. begin
  3464. StartProgram(false);
  3465. Add([
  3466. 'procedure DoIt(break: boolean);',
  3467. 'begin',
  3468. ' if break then ;',
  3469. 'end;',
  3470. 'var',
  3471. ' break: boolean;',
  3472. 'begin',
  3473. ' if break then ;']);
  3474. ConvertProgram;
  3475. CheckSource('TestBreakAsVar',
  3476. LinesToStr([ // statements
  3477. 'this.DoIt = function (Break) {',
  3478. ' if (Break) ;',
  3479. '};',
  3480. 'this.Break = false;',
  3481. '']),
  3482. LinesToStr([
  3483. 'if($mod.Break) ;',
  3484. '']));
  3485. end;
  3486. procedure TTestModule.TestContinue;
  3487. begin
  3488. StartProgram(false);
  3489. Add('var i: longint;');
  3490. Add('begin');
  3491. Add(' repeat');
  3492. Add(' continue;');
  3493. Add(' until true;');
  3494. Add(' while true do');
  3495. Add(' continue;');
  3496. Add(' for i:=1 to 2 do');
  3497. Add(' continue;');
  3498. ConvertProgram;
  3499. CheckSource('TestContinue',
  3500. LinesToStr([ // statements
  3501. 'this.i = 0;'
  3502. ]),
  3503. LinesToStr([
  3504. 'do {',
  3505. ' continue;',
  3506. '} while (!true);',
  3507. 'while (true) continue;',
  3508. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3509. '']));
  3510. end;
  3511. procedure TTestModule.TestProc_External;
  3512. begin
  3513. StartProgram(false);
  3514. Add('procedure Foo; external name ''console.log'';');
  3515. Add('function Bar: longint; external name ''get.item'';');
  3516. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3517. Add('var');
  3518. Add(' i: longint;');
  3519. Add('begin');
  3520. Add(' Foo;');
  3521. Add(' i:=Bar;');
  3522. Add(' i:=Bla(''abc'');');
  3523. ConvertProgram;
  3524. CheckSource('TestProc_External',
  3525. LinesToStr([ // statements
  3526. 'this.i = 0;'
  3527. ]),
  3528. LinesToStr([
  3529. 'console.log();',
  3530. '$mod.i = get.item();',
  3531. '$mod.i = apply.something("abc");'
  3532. ]));
  3533. end;
  3534. procedure TTestModule.TestProc_ExternalOtherUnit;
  3535. begin
  3536. AddModuleWithIntfImplSrc('unit2.pas',
  3537. LinesToStr([
  3538. 'procedure Now; external name ''Date.now'';',
  3539. 'procedure DoIt;'
  3540. ]),
  3541. 'procedure doit; begin end;');
  3542. StartUnit(true);
  3543. Add('interface');
  3544. Add('uses unit2;');
  3545. Add('implementation');
  3546. Add('begin');
  3547. Add(' now;');
  3548. Add(' now();');
  3549. Add(' uNit2.now;');
  3550. Add(' uNit2.now();');
  3551. Add(' doit;');
  3552. Add(' uNit2.doit;');
  3553. ConvertUnit;
  3554. CheckSource('TestProc_ExternalOtherUnit',
  3555. LinesToStr([
  3556. '']),
  3557. LinesToStr([
  3558. 'Date.now();',
  3559. 'Date.now();',
  3560. 'Date.now();',
  3561. 'Date.now();',
  3562. 'pas.unit2.DoIt();',
  3563. 'pas.unit2.DoIt();',
  3564. '']));
  3565. end;
  3566. procedure TTestModule.TestProc_Asm;
  3567. begin
  3568. StartProgram(false);
  3569. Add([
  3570. '{$mode delphi}',
  3571. 'function DoIt: longint;',
  3572. 'begin;',
  3573. ' asm',
  3574. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3575. ' end;',
  3576. ' asm console.log(); end;',
  3577. ' asm',
  3578. ' s = "'' ";',
  3579. ' s = ''" '';',
  3580. ' s = s + "world" + "''";',
  3581. ' // end',
  3582. ' s = ''end'';',
  3583. ' s = "end";',
  3584. ' s = "foo\"bar";',
  3585. ' s = ''a\''b'';',
  3586. ' s = `${expr}\`-"-''-`;',
  3587. ' s = `multi',
  3588. 'line`;',
  3589. ' end;',
  3590. 'end;',
  3591. 'procedure Fly;',
  3592. 'asm',
  3593. ' return;',
  3594. 'end;',
  3595. 'begin']);
  3596. ConvertProgram;
  3597. CheckSource('TestProc_Asm',
  3598. LinesToStr([ // statements
  3599. 'this.DoIt = function () {',
  3600. ' var Result = 0;',
  3601. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3602. ' console.log();',
  3603. ' s = "'' ";',
  3604. ' s = ''" '';',
  3605. ' s = s + "world" + "''";',
  3606. ' // end',
  3607. ' s = ''end'';',
  3608. ' s = "end";',
  3609. ' s = "foo\"bar";',
  3610. ' s = ''a\''b'';',
  3611. ' s = `${expr}\`-"-''-`;',
  3612. ' s = `multi',
  3613. 'line`;',
  3614. ' return Result;',
  3615. '};',
  3616. 'this.Fly = function () {',
  3617. ' return;',
  3618. '};',
  3619. '']),
  3620. LinesToStr([
  3621. ''
  3622. ]));
  3623. end;
  3624. procedure TTestModule.TestProc_Assembler;
  3625. begin
  3626. StartProgram(false);
  3627. Add('function DoIt: longint; assembler;');
  3628. Add('asm');
  3629. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3630. Add('end;');
  3631. Add('begin');
  3632. ConvertProgram;
  3633. CheckSource('TestProc_Assembler',
  3634. LinesToStr([ // statements
  3635. 'this.DoIt = function () {',
  3636. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3637. '};'
  3638. ]),
  3639. LinesToStr([
  3640. ''
  3641. ]));
  3642. end;
  3643. procedure TTestModule.TestProc_VarParam;
  3644. begin
  3645. StartProgram(false);
  3646. Add('type integer = longint;');
  3647. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3648. Add('var vJ: integer;');
  3649. Add('begin');
  3650. Add(' vg:=vg+1;');
  3651. Add(' vj:=vh+2;');
  3652. Add(' vi:=vi+3;');
  3653. Add(' doit(vg,vg,vg);');
  3654. Add(' doit(vh,vh,vj);');
  3655. Add(' doit(vi,vi,vi);');
  3656. Add(' doit(vj,vj,vj);');
  3657. Add('end;');
  3658. Add('var i: integer;');
  3659. Add('begin');
  3660. Add(' doit(i,i,i);');
  3661. ConvertProgram;
  3662. CheckSource('TestProc_VarParam',
  3663. LinesToStr([ // statements
  3664. 'this.DoIt = function (vG,vH,vI) {',
  3665. ' var vJ = 0;',
  3666. ' vG = vG + 1;',
  3667. ' vJ = vH + 2;',
  3668. ' vI.set(vI.get()+3);',
  3669. ' $mod.DoIt(vG, vG, {',
  3670. ' get: function () {',
  3671. ' return vG;',
  3672. ' },',
  3673. ' set: function (v) {',
  3674. ' vG = v;',
  3675. ' }',
  3676. ' });',
  3677. ' $mod.DoIt(vH, vH, {',
  3678. ' get: function () {',
  3679. ' return vJ;',
  3680. ' },',
  3681. ' set: function (v) {',
  3682. ' vJ = v;',
  3683. ' }',
  3684. ' });',
  3685. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3686. ' $mod.DoIt(vJ, vJ, {',
  3687. ' get: function () {',
  3688. ' return vJ;',
  3689. ' },',
  3690. ' set: function (v) {',
  3691. ' vJ = v;',
  3692. ' }',
  3693. ' });',
  3694. '};',
  3695. 'this.i = 0;'
  3696. ]),
  3697. LinesToStr([
  3698. '$mod.DoIt($mod.i,$mod.i,{',
  3699. ' p: $mod,',
  3700. ' get: function () {',
  3701. ' return this.p.i;',
  3702. ' },',
  3703. ' set: function (v) {',
  3704. ' this.p.i = v;',
  3705. ' }',
  3706. '});'
  3707. ]));
  3708. end;
  3709. procedure TTestModule.TestProc_VarParamString;
  3710. begin
  3711. StartProgram(false);
  3712. Add(['type TCaption = string;',
  3713. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3714. 'var c: char;',
  3715. 'begin',
  3716. ' va[1]:=c;',
  3717. ' vb[2]:=c;',
  3718. ' vc[3]:=c;',
  3719. 'end;',
  3720. 'begin']);
  3721. ConvertProgram;
  3722. CheckSource('TestProc_VarParamString',
  3723. LinesToStr([ // statements
  3724. 'this.DoIt = function (vA,vB,vC) {',
  3725. ' var c = "";',
  3726. ' vA = rtl.setCharAt(vA, 0, c);',
  3727. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3728. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3729. '};',
  3730. '']),
  3731. LinesToStr([
  3732. ]));
  3733. end;
  3734. procedure TTestModule.TestProc_VarParamV;
  3735. begin
  3736. StartProgram(false);
  3737. Add([
  3738. 'procedure Inc2(var i: longint);',
  3739. 'begin',
  3740. ' i:=i+2;',
  3741. 'end;',
  3742. 'procedure DoIt(v: longint);',
  3743. 'var p: array of longint;',
  3744. 'begin',
  3745. ' Inc2(v);',
  3746. ' Inc2(p[v]);',
  3747. 'end;',
  3748. 'begin']);
  3749. ConvertProgram;
  3750. CheckSource('TestProc_VarParamV',
  3751. LinesToStr([ // statements
  3752. 'this.Inc2 = function (i) {',
  3753. ' i.set(i.get()+2);',
  3754. '};',
  3755. 'this.DoIt = function (v) {',
  3756. ' var p = [];',
  3757. ' $mod.Inc2({get: function () {',
  3758. ' return v;',
  3759. ' }, set: function (w) {',
  3760. ' v = w;',
  3761. ' }});',
  3762. ' $mod.Inc2({',
  3763. ' a: v,',
  3764. ' p: p,',
  3765. ' get: function () {',
  3766. ' return this.p[this.a];',
  3767. ' },',
  3768. ' set: function (v) {',
  3769. ' this.p[this.a] = v;',
  3770. ' }',
  3771. ' });',
  3772. '};',
  3773. '']),
  3774. LinesToStr([
  3775. '']));
  3776. end;
  3777. procedure TTestModule.TestProc_Overload;
  3778. begin
  3779. StartProgram(false);
  3780. Add('procedure DoIt(vI: longint); begin end;');
  3781. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3782. Add('procedure DoIt(vD: double); begin end;');
  3783. Add('begin');
  3784. Add(' DoIt(1);');
  3785. Add(' DoIt(2,3);');
  3786. Add(' DoIt(4.5);');
  3787. ConvertProgram;
  3788. CheckSource('TestProcedureOverload',
  3789. LinesToStr([ // statements
  3790. 'this.DoIt = function (vI) {',
  3791. '};',
  3792. 'this.DoIt$1 = function (vI, vJ) {',
  3793. '};',
  3794. 'this.DoIt$2 = function (vD) {',
  3795. '};',
  3796. '']),
  3797. LinesToStr([
  3798. '$mod.DoIt(1);',
  3799. '$mod.DoIt$1(2, 3);',
  3800. '$mod.DoIt$2(4.5);',
  3801. '']));
  3802. end;
  3803. procedure TTestModule.TestProc_OverloadForward;
  3804. begin
  3805. StartProgram(false);
  3806. Add('procedure DoIt(vI: longint); forward;');
  3807. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3808. Add('procedure doit(vi: longint); begin end;');
  3809. Add('begin');
  3810. Add(' doit(1);');
  3811. Add(' doit(2,3);');
  3812. ConvertProgram;
  3813. CheckSource('TestProcedureOverloadForward',
  3814. LinesToStr([ // statements
  3815. 'this.DoIt$1 = function (vI, vJ) {',
  3816. '};',
  3817. 'this.DoIt = function (vI) {',
  3818. '};',
  3819. '']),
  3820. LinesToStr([
  3821. '$mod.DoIt(1);',
  3822. '$mod.DoIt$1(2, 3);',
  3823. '']));
  3824. end;
  3825. procedure TTestModule.TestProc_OverloadIntfImpl;
  3826. begin
  3827. StartUnit(false);
  3828. Add('interface');
  3829. Add('procedure DoIt(vI: longint);');
  3830. Add('procedure DoIt(vI, vJ: longint);');
  3831. Add('implementation');
  3832. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3833. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3834. Add('procedure DoIt(vi: longint); begin end;');
  3835. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3836. Add('procedure DoIt(vi, vj: longint); begin end;');
  3837. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3838. Add('begin');
  3839. Add(' doit(1);');
  3840. Add(' doit(2,3);');
  3841. Add(' doit(4,5,6);');
  3842. Add(' doit(7,8,9,10);');
  3843. Add(' doit(11,12,13,14,15);');
  3844. ConvertUnit;
  3845. CheckSource('TestProcedureOverloadUnit',
  3846. LinesToStr([ // statements
  3847. 'var $impl = $mod.$impl;',
  3848. 'this.DoIt = function (vI) {',
  3849. '};',
  3850. 'this.DoIt$1 = function (vI, vJ) {',
  3851. '};',
  3852. '']),
  3853. LinesToStr([ // this.$init
  3854. '$mod.DoIt(1);',
  3855. '$mod.DoIt$1(2, 3);',
  3856. '$impl.DoIt$3(4,5,6);',
  3857. '$impl.DoIt$4(7,8,9,10);',
  3858. '$impl.DoIt$2(11,12,13,14,15);',
  3859. '']),
  3860. LinesToStr([ // implementation
  3861. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3862. '};',
  3863. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3864. '};',
  3865. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3866. '};',
  3867. '']));
  3868. end;
  3869. procedure TTestModule.TestProc_OverloadNested;
  3870. begin
  3871. StartProgram(false);
  3872. Add([
  3873. 'procedure DoIt(vA: longint); overload; forward;',
  3874. 'procedure DoIt(vB, vC: longint); overload;',
  3875. 'begin // 2 param overload',
  3876. ' doit(1);',
  3877. ' doit(1,2);',
  3878. 'end;',
  3879. 'procedure doit(vA: longint);',
  3880. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3881. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3882. ' begin // 4 param overload',
  3883. ' doit(1);',
  3884. ' doit(1,2);',
  3885. ' doit(1,2,3);',
  3886. ' doit(1,2,3,4);',
  3887. ' end;',
  3888. ' procedure doit(vA, vB, vC: longint);',
  3889. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3890. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3891. ' begin // 6 param overload',
  3892. ' doit(1);',
  3893. ' doit(1,2);',
  3894. ' doit(1,2,3);',
  3895. ' doit(1,2,3,4);',
  3896. ' doit(1,2,3,4,5);',
  3897. ' doit(1,2,3,4,5,6);',
  3898. ' end;',
  3899. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3900. ' begin // 5 param overload',
  3901. ' doit(1);',
  3902. ' doit(1,2);',
  3903. ' doit(1,2,3);',
  3904. ' doit(1,2,3,4);',
  3905. ' doit(1,2,3,4,5);',
  3906. ' doit(1,2,3,4,5,6);',
  3907. ' end;',
  3908. ' begin // 3 param overload',
  3909. ' doit(1);',
  3910. ' doit(1,2);',
  3911. ' doit(1,2,3);',
  3912. ' doit(1,2,3,4);',
  3913. ' doit(1,2,3,4,5);',
  3914. ' doit(1,2,3,4,5,6);',
  3915. ' end;',
  3916. 'begin // 1 param overload',
  3917. ' doit(1);',
  3918. ' doit(1,2);',
  3919. ' doit(1,2,3);',
  3920. ' doit(1,2,3,4);',
  3921. 'end;',
  3922. 'begin // main',
  3923. ' doit(1);',
  3924. ' doit(1,2);']);
  3925. ConvertProgram;
  3926. CheckSource('TestProcedureOverloadNested',
  3927. LinesToStr([ // statements
  3928. 'this.DoIt$1 = function (vB, vC) {',
  3929. ' $mod.DoIt(1);',
  3930. ' $mod.DoIt$1(1, 2);',
  3931. '};',
  3932. 'this.DoIt = function (vA) {',
  3933. ' function DoIt$3(vA, vB, vC, vD) {',
  3934. ' $mod.DoIt(1);',
  3935. ' $mod.DoIt$1(1, 2);',
  3936. ' DoIt$2(1, 2, 3);',
  3937. ' DoIt$3(1, 2, 3, 4);',
  3938. ' };',
  3939. ' function DoIt$2(vA, vB, vC) {',
  3940. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  3941. ' $mod.DoIt(1);',
  3942. ' $mod.DoIt$1(1, 2);',
  3943. ' DoIt$2(1, 2, 3);',
  3944. ' DoIt$3(1, 2, 3, 4);',
  3945. ' DoIt$4(1, 2, 3, 4, 5);',
  3946. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3947. ' };',
  3948. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  3949. ' $mod.DoIt(1);',
  3950. ' $mod.DoIt$1(1, 2);',
  3951. ' DoIt$2(1, 2, 3);',
  3952. ' DoIt$3(1, 2, 3, 4);',
  3953. ' DoIt$4(1, 2, 3, 4, 5);',
  3954. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3955. ' };',
  3956. ' $mod.DoIt(1);',
  3957. ' $mod.DoIt$1(1, 2);',
  3958. ' DoIt$2(1, 2, 3);',
  3959. ' DoIt$3(1, 2, 3, 4);',
  3960. ' DoIt$4(1, 2, 3, 4, 5);',
  3961. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3962. ' };',
  3963. ' $mod.DoIt(1);',
  3964. ' $mod.DoIt$1(1, 2);',
  3965. ' DoIt$2(1, 2, 3);',
  3966. ' DoIt$3(1, 2, 3, 4);',
  3967. '};',
  3968. '']),
  3969. LinesToStr([
  3970. '$mod.DoIt(1);',
  3971. '$mod.DoIt$1(1, 2);',
  3972. '']));
  3973. end;
  3974. procedure TTestModule.TestProc_OverloadUnitCycle;
  3975. begin
  3976. AddModuleWithIntfImplSrc('Unit2.pas',
  3977. LinesToStr([
  3978. 'type',
  3979. ' TObject = class',
  3980. ' procedure DoIt(b: boolean); virtual; abstract;',
  3981. ' procedure DoIt(i: longint); virtual; abstract;',
  3982. ' end;',
  3983. '']),
  3984. 'uses test1;');
  3985. StartUnit(true);
  3986. Add([
  3987. 'interface',
  3988. 'uses unit2;',
  3989. 'type',
  3990. ' TEagle = class(TObject)',
  3991. ' procedure DoIt(b: boolean); override;',
  3992. ' procedure DoIt(i: longint); override;',
  3993. ' end;',
  3994. 'implementation',
  3995. 'procedure TEagle.DoIt(b: boolean); begin end;',
  3996. 'procedure TEagle.DoIt(i: longint); begin end;',
  3997. '']);
  3998. ConvertUnit;
  3999. CheckSource('TestProc_OverloadUnitCycle',
  4000. LinesToStr([ // statements
  4001. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  4002. ' this.DoIt = function (b) {',
  4003. ' };',
  4004. ' this.DoIt$1 = function (i) {',
  4005. ' };',
  4006. '});',
  4007. '']),
  4008. '',
  4009. LinesToStr([
  4010. '']));
  4011. end;
  4012. procedure TTestModule.TestProc_Varargs;
  4013. begin
  4014. StartProgram(false);
  4015. Add([
  4016. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4017. 'procedure ProcB; varargs; external name ''ProcB'';',
  4018. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4019. 'function GetIt: longint; begin end;',
  4020. 'begin',
  4021. ' ProcA(1);',
  4022. ' ProcA(1,2);',
  4023. ' ProcA(1,2.0);',
  4024. ' ProcA(1,2,3);',
  4025. ' ProcA(1,''2'');',
  4026. ' ProcA(2,'''');',
  4027. ' ProcA(3,false);',
  4028. ' ProcB;',
  4029. ' ProcB();',
  4030. ' ProcB(4);',
  4031. ' ProcB(''foo'');',
  4032. ' ProcC;',
  4033. ' ProcC();',
  4034. ' ProcC(4);',
  4035. ' ProcC(5,''foo'');',
  4036. ' ProcB(GetIt);',
  4037. ' ProcB(GetIt());',
  4038. ' ProcB(GetIt,GetIt());']);
  4039. ConvertProgram;
  4040. CheckSource('TestProc_Varargs',
  4041. LinesToStr([ // statements
  4042. 'this.GetIt = function () {',
  4043. ' var Result = 0;',
  4044. ' return Result;',
  4045. '};',
  4046. '']),
  4047. LinesToStr([
  4048. 'ProcA(1);',
  4049. 'ProcA(1, 2);',
  4050. 'ProcA(1, 2.0);',
  4051. 'ProcA(1, 2, 3);',
  4052. 'ProcA(1, "2");',
  4053. 'ProcA(2, "");',
  4054. 'ProcA(3, false);',
  4055. 'ProcB();',
  4056. 'ProcB();',
  4057. 'ProcB(4);',
  4058. 'ProcB("foo");',
  4059. 'ProcC(17);',
  4060. 'ProcC(17);',
  4061. 'ProcC(4);',
  4062. 'ProcC(5, "foo");',
  4063. 'ProcB($mod.GetIt());',
  4064. 'ProcB($mod.GetIt());',
  4065. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4066. '']));
  4067. end;
  4068. procedure TTestModule.TestProc_ConstOrder;
  4069. begin
  4070. StartProgram(false);
  4071. Add([
  4072. 'const A = 3;',
  4073. 'const B = A+1;',
  4074. 'procedure DoIt;',
  4075. 'const C = A+1;',
  4076. 'const D = B+1;',
  4077. 'const E = D+C+B+A;',
  4078. 'begin',
  4079. 'end;',
  4080. 'begin'
  4081. ]);
  4082. ConvertProgram;
  4083. CheckSource('TestProc_ConstOrder',
  4084. LinesToStr([ // statements
  4085. 'this.A = 3;',
  4086. 'this.B = 3 + 1;',
  4087. 'var C = 3 + 1;',
  4088. 'var D = 4 + 1;',
  4089. 'var E = 5 + 4 + 4 + 3;',
  4090. 'this.DoIt = function () {',
  4091. '};',
  4092. '']),
  4093. LinesToStr([
  4094. ''
  4095. ]));
  4096. end;
  4097. procedure TTestModule.TestProc_DuplicateConst;
  4098. begin
  4099. StartProgram(false);
  4100. Add([
  4101. 'const A = 1;',
  4102. 'procedure DoIt;',
  4103. 'const A = 2;',
  4104. ' procedure SubIt;',
  4105. ' const A = 21;',
  4106. ' begin',
  4107. ' end;',
  4108. 'begin',
  4109. 'end;',
  4110. 'procedure DoSome;',
  4111. 'const A = 3;',
  4112. 'begin',
  4113. 'end;',
  4114. 'begin'
  4115. ]);
  4116. ConvertProgram;
  4117. CheckSource('TestProc_DuplicateConst',
  4118. LinesToStr([ // statements
  4119. 'this.A = 1;',
  4120. 'var A$1 = 2;',
  4121. 'var A$2 = 21;',
  4122. 'this.DoIt = function () {',
  4123. ' function SubIt() {',
  4124. ' };',
  4125. '};',
  4126. 'var A$3 = 3;',
  4127. 'this.DoSome = function () {',
  4128. '};',
  4129. '']),
  4130. LinesToStr([
  4131. ''
  4132. ]));
  4133. end;
  4134. procedure TTestModule.TestProc_LocalVarAbsolute;
  4135. begin
  4136. StartProgram(false);
  4137. Add([
  4138. 'type',
  4139. ' TObject = class',
  4140. ' Index: longint;',
  4141. ' procedure DoAbs(Item: pointer);',
  4142. ' end;',
  4143. 'procedure TObject.DoAbs(Item: pointer);',
  4144. 'var',
  4145. ' o: TObject absolute Item;',
  4146. 'begin',
  4147. ' if o.Index<o.Index then o.Index:=o.Index;',
  4148. 'end;',
  4149. 'procedure DoIt(i: longint; p: pointer);',
  4150. 'var',
  4151. ' d: double absolute i;',
  4152. ' s: string absolute d;',
  4153. ' oi: TObject absolute i;',
  4154. ' op: TObject absolute p;',
  4155. 'begin',
  4156. ' if d=d then d:=d;',
  4157. ' if s=s then s:=s;',
  4158. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4159. ' if op.Index=op.Index then op.Index:=op.Index;',
  4160. 'end;',
  4161. 'begin']);
  4162. ConvertProgram;
  4163. CheckSource('TestProc_LocalVarAbsolute',
  4164. LinesToStr([ // statements
  4165. 'rtl.createClass($mod, "TObject", null, function () {',
  4166. ' this.$init = function () {',
  4167. ' this.Index = 0;',
  4168. ' };',
  4169. ' this.$final = function () {',
  4170. ' };',
  4171. ' this.DoAbs = function (Item) {',
  4172. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4173. ' };',
  4174. '});',
  4175. 'this.DoIt = function (i, p) {',
  4176. ' if (i === i) i = i;',
  4177. ' if (i === i) i = i;',
  4178. ' if (i.Index < i.Index) i.Index = i.Index;',
  4179. ' if (p.Index === p.Index) p.Index = p.Index;',
  4180. '};'
  4181. ]),
  4182. LinesToStr([
  4183. ]));
  4184. end;
  4185. procedure TTestModule.TestProc_LocalVarInit;
  4186. begin
  4187. StartProgram(false);
  4188. Add([
  4189. 'type TBytes = array of byte;',
  4190. 'procedure DoIt;',
  4191. 'const c = 4;',
  4192. 'var',
  4193. ' b: byte = 1;',
  4194. ' w: word = 2+c;',
  4195. ' p: pointer = nil;',
  4196. ' Buffer: TBytes = nil;',
  4197. 'begin',
  4198. 'end;',
  4199. 'begin']);
  4200. ConvertProgram;
  4201. CheckSource('TestProc_LocalVarInit',
  4202. LinesToStr([ // statements
  4203. 'var c = 4;',
  4204. 'this.DoIt = function () {',
  4205. ' var b = 1;',
  4206. ' var w = 2 + 4;',
  4207. ' var p = null;',
  4208. ' var Buffer = [];',
  4209. '};',
  4210. '']),
  4211. LinesToStr([
  4212. ]));
  4213. end;
  4214. procedure TTestModule.TestProc_ReservedWords;
  4215. begin
  4216. StartProgram(false);
  4217. Add([
  4218. 'procedure Date(ArrayBuffer: longint);',
  4219. 'const',
  4220. ' NaN: longint = 3;',
  4221. 'var',
  4222. ' &Boolean: longint;',
  4223. ' procedure Error(ArrayBuffer: longint);',
  4224. ' begin',
  4225. ' end;',
  4226. 'begin',
  4227. ' Nan:=&bOolean;',
  4228. 'end;',
  4229. 'begin',
  4230. ' Date(1);']);
  4231. ConvertProgram;
  4232. CheckSource('TestProc_ReservedWords',
  4233. LinesToStr([ // statements
  4234. 'var naN = 3;',
  4235. 'this.Date = function (arrayBuffer) {',
  4236. ' var boolean = 0;',
  4237. ' function error(arrayBuffer) {',
  4238. ' };',
  4239. ' naN = boolean;',
  4240. '};',
  4241. '']),
  4242. LinesToStr([
  4243. ' $mod.Date(1);'
  4244. ]));
  4245. end;
  4246. procedure TTestModule.TestProc_ConstRefWord;
  4247. begin
  4248. StartProgram(false);
  4249. Add([
  4250. 'procedure Run(constref w: word);',
  4251. 'var l: word;',
  4252. 'begin',
  4253. ' l:=w;',
  4254. ' Run(w);',
  4255. ' Run(l);',
  4256. 'end;',
  4257. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4258. 'begin',
  4259. ' Run(a);',
  4260. ' Run(b);',
  4261. ' Run(c);',
  4262. ' Run(d);',
  4263. ' Run(e);',
  4264. 'end;',
  4265. 'begin',
  4266. ' Run(1);']);
  4267. ConvertProgram;
  4268. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4269. CheckSource('TestProc_ConstRefWord',
  4270. LinesToStr([ // statements
  4271. 'this.Run = function (w) {',
  4272. ' var l = 0;',
  4273. ' l = w;',
  4274. ' $mod.Run(w);',
  4275. ' $mod.Run(l);',
  4276. '};',
  4277. 'this.Fly = function (a, b, c, d, e) {',
  4278. ' $mod.Run(a);',
  4279. ' $mod.Run(b.get());',
  4280. ' $mod.Run(c.get());',
  4281. ' $mod.Run(d);',
  4282. ' $mod.Run(e);',
  4283. '};',
  4284. '']),
  4285. LinesToStr([
  4286. '$mod.Run(1);'
  4287. ]));
  4288. end;
  4289. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4290. begin
  4291. StartProgram(false);
  4292. Add([
  4293. '{$mode objfpc}',
  4294. 'type',
  4295. ' TFunc = reference to function(x: word): word;',
  4296. 'var Func: TFunc;',
  4297. 'procedure DoIt(a: word);',
  4298. 'begin',
  4299. ' Func:=function(b:word): word',
  4300. ' begin',
  4301. ' Result:=a+b;',
  4302. ' exit(b);',
  4303. ' exit(Result);',
  4304. ' end;',// test semicolon
  4305. ' a:=3;',
  4306. 'end;',
  4307. 'begin',
  4308. ' Func:=function(c:word):word begin',
  4309. ' Result:=3+c;',
  4310. ' exit(c);',
  4311. ' exit(Result);',
  4312. ' end;']);
  4313. ConvertProgram;
  4314. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4315. LinesToStr([ // statements
  4316. 'this.Func = null;',
  4317. 'this.DoIt = function (a) {',
  4318. ' $mod.Func = function (b) {',
  4319. ' var Result = 0;',
  4320. ' Result = a + b;',
  4321. ' return b;',
  4322. ' return Result;',
  4323. ' return Result;',
  4324. ' };',
  4325. ' a = 3;',
  4326. '};',
  4327. '']),
  4328. LinesToStr([
  4329. '$mod.Func = function (c) {',
  4330. ' var Result = 0;',
  4331. ' Result = 3 + c;',
  4332. ' return c;',
  4333. ' return Result;',
  4334. ' return Result;',
  4335. '};',
  4336. '']));
  4337. end;
  4338. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4339. begin
  4340. StartProgram(false);
  4341. Add([
  4342. '{$mode delphi}',
  4343. 'type',
  4344. ' TProc = reference to procedure(x: word);',
  4345. 'procedure DoIt(a: word);',
  4346. 'var Proc: TProc;',
  4347. 'begin',
  4348. ' Proc:=procedure(b:word) begin end;',
  4349. 'end;',
  4350. 'var Proc: TProc;',
  4351. 'begin',
  4352. ' Proc:=procedure(c:word) begin end;',
  4353. '']);
  4354. ConvertProgram;
  4355. CheckSource('TestAnonymousProc_Assign_Delphi',
  4356. LinesToStr([ // statements
  4357. 'this.DoIt = function (a) {',
  4358. ' var Proc = null;',
  4359. ' Proc = function (b) {',
  4360. ' };',
  4361. '};',
  4362. 'this.Proc = null;',
  4363. '']),
  4364. LinesToStr([
  4365. '$mod.Proc = function (c) {',
  4366. '};',
  4367. '']));
  4368. end;
  4369. procedure TTestModule.TestAnonymousProc_Arg;
  4370. begin
  4371. StartProgram(false);
  4372. Add([
  4373. 'type',
  4374. ' TProc = reference to procedure;',
  4375. ' TFunc = reference to function(x: word): word;',
  4376. 'procedure DoMore(f,g: TProc);',
  4377. 'begin',
  4378. 'end;',
  4379. 'procedure DoOdd(v: jsvalue);',
  4380. 'begin',
  4381. 'end;',
  4382. 'procedure DoIt(f: TFunc);',
  4383. 'begin',
  4384. ' DoIt(function(b:word): word',
  4385. ' begin',
  4386. ' Result:=1+b;',
  4387. ' end);',
  4388. ' DoMore(procedure begin end, procedure begin end);',
  4389. ' DoOdd(procedure begin end);',
  4390. 'end;',
  4391. 'begin',
  4392. ' DoMore(procedure begin end,',
  4393. ' procedure assembler asm',
  4394. ' console.log("c");',
  4395. ' end);',
  4396. '']);
  4397. ConvertProgram;
  4398. CheckSource('TestAnonymousProc_Arg',
  4399. LinesToStr([ // statements
  4400. 'this.DoMore = function (f, g) {',
  4401. '};',
  4402. 'this.DoOdd = function (v) {',
  4403. '};',
  4404. 'this.DoIt = function (f) {',
  4405. ' $mod.DoIt(function (b) {',
  4406. ' var Result = 0;',
  4407. ' Result = 1 + b;',
  4408. ' return Result;',
  4409. ' });',
  4410. ' $mod.DoMore(function () {',
  4411. ' }, function () {',
  4412. ' });',
  4413. ' $mod.DoOdd(function () {',
  4414. ' });',
  4415. '};',
  4416. '']),
  4417. LinesToStr([
  4418. '$mod.DoMore(function () {',
  4419. '}, function () {',
  4420. ' console.log("c");',
  4421. '});',
  4422. '']));
  4423. end;
  4424. procedure TTestModule.TestAnonymousProc_Typecast;
  4425. begin
  4426. StartProgram(false);
  4427. Add([
  4428. 'type',
  4429. ' TProc = reference to procedure(w: word);',
  4430. ' TArr = array of word;',
  4431. ' TFuncArr = reference to function: TArr;',
  4432. 'procedure DoIt(p: TProc);',
  4433. 'var',
  4434. ' w: word;',
  4435. ' a: TArr;',
  4436. 'begin',
  4437. ' p:=TProc(procedure(b: smallint) begin end);',
  4438. ' a:=TFuncArr(function: TArr begin end)();',
  4439. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4440. 'end;',
  4441. 'begin']);
  4442. ConvertProgram;
  4443. CheckSource('TestAnonymousProc_Typecast',
  4444. LinesToStr([ // statements
  4445. 'this.DoIt = function (p) {',
  4446. ' var w = 0;',
  4447. ' var a = [];',
  4448. ' p = function (b) {',
  4449. ' };',
  4450. ' a = function () {',
  4451. ' var Result = [];',
  4452. ' return Result;',
  4453. ' }();',
  4454. ' w = function () {',
  4455. ' var Result = [];',
  4456. ' return Result;',
  4457. ' }()[3];',
  4458. '};',
  4459. '']),
  4460. LinesToStr([
  4461. '']));
  4462. end;
  4463. procedure TTestModule.TestAnonymousProc_With;
  4464. begin
  4465. StartProgram(false);
  4466. Add([
  4467. 'type',
  4468. ' TProc = reference to procedure(w: word);',
  4469. ' TObject = class',
  4470. ' b: boolean;',
  4471. ' end;',
  4472. 'var',
  4473. ' p: TProc;',
  4474. ' bird: TObject;',
  4475. 'begin',
  4476. ' with bird do',
  4477. ' p:=procedure(w: word)',
  4478. ' begin',
  4479. ' b:=w>2;',
  4480. ' end;',
  4481. '']);
  4482. ConvertProgram;
  4483. CheckSource('TestAnonymousProc_With',
  4484. LinesToStr([ // statements
  4485. 'rtl.createClass($mod, "TObject", null, function () {',
  4486. ' this.$init = function () {',
  4487. ' this.b = false;',
  4488. ' };',
  4489. ' this.$final = function () {',
  4490. ' };',
  4491. '});',
  4492. 'this.p = null;',
  4493. 'this.bird = null;',
  4494. '']),
  4495. LinesToStr([
  4496. 'var $with1 = $mod.bird;',
  4497. '$mod.p = function (w) {',
  4498. ' $with1.b = w > 2;',
  4499. '};',
  4500. '']));
  4501. end;
  4502. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4503. begin
  4504. StartProgram(false);
  4505. Add([
  4506. 'type',
  4507. ' TProc = reference to procedure;',
  4508. ' TObject = class',
  4509. ' b: boolean;',
  4510. ' end;',
  4511. 'procedure DoIt;',
  4512. 'var',
  4513. ' p: TProc;',
  4514. 'begin',
  4515. ' try',
  4516. ' except',
  4517. ' on E: TObject do',
  4518. ' p:=procedure',
  4519. ' begin',
  4520. ' E.b:=true;',
  4521. ' end;',
  4522. ' end;',
  4523. 'end;',
  4524. 'begin']);
  4525. ConvertProgram;
  4526. CheckSource('TestAnonymousProc_ExceptOn',
  4527. LinesToStr([ // statements
  4528. 'rtl.createClass($mod, "TObject", null, function () {',
  4529. ' this.$init = function () {',
  4530. ' this.b = false;',
  4531. ' };',
  4532. ' this.$final = function () {',
  4533. ' };',
  4534. '});',
  4535. 'this.DoIt = function () {',
  4536. ' var p = null;',
  4537. ' try {} catch ($e) {',
  4538. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4539. ' var E = $e;',
  4540. ' p = function () {',
  4541. ' E.b = true;',
  4542. ' };',
  4543. ' } else throw $e',
  4544. ' };',
  4545. '};',
  4546. '']),
  4547. LinesToStr([
  4548. '']));
  4549. end;
  4550. procedure TTestModule.TestAnonymousProc_Nested;
  4551. begin
  4552. StartProgram(false);
  4553. Add([
  4554. 'type',
  4555. ' TProc = reference to procedure;',
  4556. ' TObject = class',
  4557. ' i: byte;',
  4558. ' procedure DoIt;',
  4559. ' end;',
  4560. 'procedure TObject.DoIt;',
  4561. 'var',
  4562. ' p: TProc;',
  4563. ' procedure Sub;',
  4564. ' begin',
  4565. ' p:=procedure',
  4566. ' begin',
  4567. ' i:=3;',
  4568. ' Self.i:=4;',
  4569. ' p:=procedure',
  4570. ' procedure SubSub;',
  4571. ' begin',
  4572. ' i:=13;',
  4573. ' Self.i:=14;',
  4574. ' end;',
  4575. ' begin',
  4576. ' i:=13;',
  4577. ' Self.i:=14;',
  4578. ' end;',
  4579. ' end;',
  4580. ' end;',
  4581. 'begin',
  4582. 'end;',
  4583. 'begin']);
  4584. ConvertProgram;
  4585. CheckSource('TestAnonymousProc_Nested',
  4586. LinesToStr([ // statements
  4587. 'rtl.createClass($mod, "TObject", null, function () {',
  4588. ' this.$init = function () {',
  4589. ' this.i = 0;',
  4590. ' };',
  4591. ' this.$final = function () {',
  4592. ' };',
  4593. ' this.DoIt = function () {',
  4594. ' var $Self = this;',
  4595. ' var p = null;',
  4596. ' function Sub() {',
  4597. ' p = function () {',
  4598. ' $Self.i = 3;',
  4599. ' $Self.i = 4;',
  4600. ' p = function () {',
  4601. ' function SubSub() {',
  4602. ' $Self.i = 13;',
  4603. ' $Self.i = 14;',
  4604. ' };',
  4605. ' $Self.i = 13;',
  4606. ' $Self.i = 14;',
  4607. ' };',
  4608. ' };',
  4609. ' };',
  4610. ' };',
  4611. '});',
  4612. '']),
  4613. LinesToStr([
  4614. '']));
  4615. end;
  4616. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4617. begin
  4618. StartProgram(false);
  4619. Add([
  4620. 'type',
  4621. ' TProc = reference to procedure;',
  4622. 'function DoIt: TProc;',
  4623. ' function Sub: TProc;',
  4624. ' begin',
  4625. ' Result:=procedure',
  4626. ' begin',
  4627. ' Sub:=procedure',
  4628. ' procedure SubSub;',
  4629. ' begin',
  4630. ' Result:=nil;',
  4631. ' Sub:=nil;',
  4632. ' DoIt:=nil;',
  4633. ' end;',
  4634. ' begin',
  4635. ' Result:=nil;',
  4636. ' Sub:=nil;',
  4637. ' DoIt:=nil;',
  4638. ' end;',
  4639. ' end;',
  4640. ' end;',
  4641. 'begin',
  4642. 'end;',
  4643. 'begin']);
  4644. ConvertProgram;
  4645. CheckSource('TestAnonymousProc_NestedAssignResult',
  4646. LinesToStr([ // statements
  4647. 'this.DoIt = function () {',
  4648. ' var Result = null;',
  4649. ' function Sub() {',
  4650. ' var Result$1 = null;',
  4651. ' Result$1 = function () {',
  4652. ' Result$1 = function () {',
  4653. ' function SubSub() {',
  4654. ' Result$1 = null;',
  4655. ' Result$1 = null;',
  4656. ' Result = null;',
  4657. ' };',
  4658. ' Result$1 = null;',
  4659. ' Result$1 = null;',
  4660. ' Result = null;',
  4661. ' };',
  4662. ' };',
  4663. ' return Result$1;',
  4664. ' };',
  4665. ' return Result;',
  4666. '};',
  4667. '']),
  4668. LinesToStr([
  4669. '']));
  4670. end;
  4671. procedure TTestModule.TestAnonymousProc_Class;
  4672. begin
  4673. StartProgram(false);
  4674. Add([
  4675. 'type',
  4676. ' TProc = reference to procedure;',
  4677. ' TEvent = procedure of object;',
  4678. ' TObject = class',
  4679. ' Size: word;',
  4680. ' function GetIt: TProc;',
  4681. ' procedure DoIt; virtual; abstract;',
  4682. ' end;',
  4683. 'function TObject.GetIt: TProc;',
  4684. 'begin',
  4685. ' Result:=procedure',
  4686. ' var p: TEvent;',
  4687. ' begin',
  4688. ' Size:=Size;',
  4689. ' Size:=Self.Size;',
  4690. ' p:=@DoIt;',
  4691. ' p:[email protected];',
  4692. ' end;',
  4693. 'end;',
  4694. 'begin']);
  4695. ConvertProgram;
  4696. CheckSource('TestAnonymousProc_Class',
  4697. LinesToStr([ // statements
  4698. 'rtl.createClass($mod, "TObject", null, function () {',
  4699. ' this.$init = function () {',
  4700. ' this.Size = 0;',
  4701. ' };',
  4702. ' this.$final = function () {',
  4703. ' };',
  4704. ' this.GetIt = function () {',
  4705. ' var $Self = this;',
  4706. ' var Result = null;',
  4707. ' Result = function () {',
  4708. ' var p = null;',
  4709. ' $Self.Size = $Self.Size;',
  4710. ' $Self.Size = $Self.Size;',
  4711. ' p = rtl.createCallback($Self, "DoIt");',
  4712. ' p = rtl.createCallback($Self, "DoIt");',
  4713. ' };',
  4714. ' return Result;',
  4715. ' };',
  4716. '});',
  4717. '']),
  4718. LinesToStr([
  4719. '']));
  4720. end;
  4721. procedure TTestModule.TestAnonymousProc_ForLoop;
  4722. begin
  4723. StartProgram(false);
  4724. Add([
  4725. 'type TProc = reference to procedure;',
  4726. 'procedure Foo(p: TProc);',
  4727. 'begin',
  4728. 'end;',
  4729. 'procedure DoIt;',
  4730. 'var i: word;',
  4731. ' a: word;',
  4732. 'begin',
  4733. ' for i:=1 to 10 do begin',
  4734. ' Foo(procedure begin a:=3; end);',
  4735. ' end;',
  4736. 'end;',
  4737. 'begin',
  4738. ' DoIt;']);
  4739. ConvertProgram;
  4740. CheckSource('TestAnonymousProc_ForLoop',
  4741. LinesToStr([ // statements
  4742. 'this.Foo = function (p) {',
  4743. '};',
  4744. 'this.DoIt = function () {',
  4745. ' var i = 0;',
  4746. ' var a = 0;',
  4747. ' for (i = 1; i <= 10; i++) {',
  4748. ' $mod.Foo(function () {',
  4749. ' a = 3;',
  4750. ' });',
  4751. ' };',
  4752. '};',
  4753. '']),
  4754. LinesToStr([
  4755. '$mod.DoIt();'
  4756. ]));
  4757. end;
  4758. procedure TTestModule.TestEnum_Name;
  4759. begin
  4760. StartProgram(false);
  4761. Add('type TMyEnum = (Red, Green, Blue);');
  4762. Add('var e: TMyEnum;');
  4763. Add('var f: TMyEnum = Blue;');
  4764. Add('begin');
  4765. Add(' e:=green;');
  4766. Add(' e:=default(TMyEnum);');
  4767. ConvertProgram;
  4768. CheckSource('TestEnumName',
  4769. LinesToStr([ // statements
  4770. 'this.TMyEnum = {',
  4771. ' "0":"Red",',
  4772. ' Red:0,',
  4773. ' "1":"Green",',
  4774. ' Green:1,',
  4775. ' "2":"Blue",',
  4776. ' Blue:2',
  4777. ' };',
  4778. 'this.e = 0;',
  4779. 'this.f = $mod.TMyEnum.Blue;'
  4780. ]),
  4781. LinesToStr([
  4782. '$mod.e=$mod.TMyEnum.Green;',
  4783. '$mod.e=$mod.TMyEnum.Red;'
  4784. ]));
  4785. end;
  4786. procedure TTestModule.TestEnum_Number;
  4787. begin
  4788. Converter.Options:=Converter.Options+[coEnumNumbers];
  4789. StartProgram(false);
  4790. Add('type TMyEnum = (Red, Green);');
  4791. Add('var');
  4792. Add(' e: TMyEnum;');
  4793. Add(' f: TMyEnum = Green;');
  4794. Add(' i: longint;');
  4795. Add('begin');
  4796. Add(' e:=green;');
  4797. Add(' i:=longint(e);');
  4798. ConvertProgram;
  4799. CheckSource('TestEnumNumber',
  4800. LinesToStr([ // statements
  4801. 'this.TMyEnum = {',
  4802. ' "0":"Red",',
  4803. ' Red:0,',
  4804. ' "1":"Green",',
  4805. ' Green:1',
  4806. ' };',
  4807. 'this.e = 0;',
  4808. 'this.f = 1;',
  4809. 'this.i = 0;'
  4810. ]),
  4811. LinesToStr([
  4812. '$mod.e=1;',
  4813. '$mod.i=$mod.e;'
  4814. ]));
  4815. end;
  4816. procedure TTestModule.TestEnum_ConstFail;
  4817. begin
  4818. StartProgram(false);
  4819. Add([
  4820. 'type TMyEnum = (Red = 100, Green = 101);',
  4821. 'var',
  4822. ' e: TMyEnum;',
  4823. ' f: TMyEnum = Green;',
  4824. 'begin',
  4825. ' e:=green;']);
  4826. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] enum const',3002);
  4827. ConvertProgram;
  4828. end;
  4829. procedure TTestModule.TestEnum_Functions;
  4830. begin
  4831. StartProgram(false);
  4832. Add([
  4833. 'type TMyEnum = (Red, Green);',
  4834. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4835. 'var',
  4836. ' v: longint;',
  4837. ' s: string;',
  4838. 'begin',
  4839. ' val(s,e,v);',
  4840. ' val(s,e,i);',
  4841. 'end;',
  4842. 'var',
  4843. ' e: TMyEnum;',
  4844. ' i: longint;',
  4845. ' s: string;',
  4846. ' b: boolean;',
  4847. 'begin',
  4848. ' i:=ord(red);',
  4849. ' i:=ord(green);',
  4850. ' i:=ord(e);',
  4851. ' i:=ord(b);',
  4852. ' e:=low(tmyenum);',
  4853. ' e:=low(e);',
  4854. ' b:=low(boolean);',
  4855. ' e:=high(tmyenum);',
  4856. ' e:=high(e);',
  4857. ' b:=high(boolean);',
  4858. ' e:=pred(green);',
  4859. ' e:=pred(e);',
  4860. ' b:=pred(b);',
  4861. ' e:=succ(red);',
  4862. ' e:=succ(e);',
  4863. ' b:=succ(b);',
  4864. ' e:=tmyenum(1);',
  4865. ' e:=tmyenum(i);',
  4866. ' s:=str(e);',
  4867. ' str(e,s);',
  4868. ' str(red,s);',
  4869. ' s:=str(e:3);',
  4870. ' writestr(s,e:3,red);',
  4871. ' val(s,e,i);',
  4872. ' e:=TMyEnum(i);',
  4873. ' i:=longint(e);']);
  4874. ConvertProgram;
  4875. CheckSource('TestEnum_Functions',
  4876. LinesToStr([ // statements
  4877. 'this.TMyEnum = {',
  4878. ' "0":"Red",',
  4879. ' Red:0,',
  4880. ' "1":"Green",',
  4881. ' Green:1',
  4882. ' };',
  4883. 'this.DoIt = function (e, i) {',
  4884. ' var v = 0;',
  4885. ' var s = "";',
  4886. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4887. ' v = w;',
  4888. ' }));',
  4889. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4890. '};',
  4891. 'this.e = 0;',
  4892. 'this.i = 0;',
  4893. 'this.s = "";',
  4894. 'this.b = false;',
  4895. '']),
  4896. LinesToStr([
  4897. '$mod.i=$mod.TMyEnum.Red;',
  4898. '$mod.i=$mod.TMyEnum.Green;',
  4899. '$mod.i=$mod.e;',
  4900. '$mod.i=$mod.b+0;',
  4901. '$mod.e=$mod.TMyEnum.Red;',
  4902. '$mod.e=$mod.TMyEnum.Red;',
  4903. '$mod.b=false;',
  4904. '$mod.e=$mod.TMyEnum.Green;',
  4905. '$mod.e=$mod.TMyEnum.Green;',
  4906. '$mod.b=true;',
  4907. '$mod.e=$mod.TMyEnum.Green-1;',
  4908. '$mod.e=$mod.e-1;',
  4909. '$mod.b=false;',
  4910. '$mod.e=$mod.TMyEnum.Red+1;',
  4911. '$mod.e=$mod.e+1;',
  4912. '$mod.b=true;',
  4913. '$mod.e=1;',
  4914. '$mod.e=$mod.i;',
  4915. '$mod.s = $mod.TMyEnum[$mod.e];',
  4916. '$mod.s = $mod.TMyEnum[$mod.e];',
  4917. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  4918. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  4919. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  4920. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  4921. ' $mod.i = v;',
  4922. '});',
  4923. '$mod.e=$mod.i;',
  4924. '$mod.i=$mod.e;',
  4925. '']));
  4926. end;
  4927. procedure TTestModule.TestEnum_AsParams;
  4928. begin
  4929. StartProgram(false);
  4930. Add('type TEnum = (Red,Blue);');
  4931. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  4932. Add('var vJ: TEnum;');
  4933. Add('begin');
  4934. Add(' vg:=vg;');
  4935. Add(' vj:=vh;');
  4936. Add(' vi:=vi;');
  4937. Add(' doit(vg,vg,vg);');
  4938. Add(' doit(vh,vh,vj);');
  4939. Add(' doit(vi,vi,vi);');
  4940. Add(' doit(vj,vj,vj);');
  4941. Add('end;');
  4942. Add('var i: TEnum;');
  4943. Add('begin');
  4944. Add(' doit(i,i,i);');
  4945. ConvertProgram;
  4946. CheckSource('TestEnum_AsParams',
  4947. LinesToStr([ // statements
  4948. 'this.TEnum = {',
  4949. ' "0": "Red",',
  4950. ' Red: 0,',
  4951. ' "1": "Blue",',
  4952. ' Blue: 1',
  4953. '};',
  4954. 'this.DoIt = function (vG,vH,vI) {',
  4955. ' var vJ = 0;',
  4956. ' vG = vG;',
  4957. ' vJ = vH;',
  4958. ' vI.set(vI.get());',
  4959. ' $mod.DoIt(vG, vG, {',
  4960. ' get: function () {',
  4961. ' return vG;',
  4962. ' },',
  4963. ' set: function (v) {',
  4964. ' vG = v;',
  4965. ' }',
  4966. ' });',
  4967. ' $mod.DoIt(vH, vH, {',
  4968. ' get: function () {',
  4969. ' return vJ;',
  4970. ' },',
  4971. ' set: function (v) {',
  4972. ' vJ = v;',
  4973. ' }',
  4974. ' });',
  4975. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4976. ' $mod.DoIt(vJ, vJ, {',
  4977. ' get: function () {',
  4978. ' return vJ;',
  4979. ' },',
  4980. ' set: function (v) {',
  4981. ' vJ = v;',
  4982. ' }',
  4983. ' });',
  4984. '};',
  4985. 'this.i = 0;'
  4986. ]),
  4987. LinesToStr([
  4988. '$mod.DoIt($mod.i,$mod.i,{',
  4989. ' p: $mod,',
  4990. ' get: function () {',
  4991. ' return this.p.i;',
  4992. ' },',
  4993. ' set: function (v) {',
  4994. ' this.p.i = v;',
  4995. ' }',
  4996. '});'
  4997. ]));
  4998. end;
  4999. procedure TTestModule.TestEnumRange_Array;
  5000. begin
  5001. StartProgram(false);
  5002. Add([
  5003. 'type',
  5004. ' TEnum = (Red, Green, Blue);',
  5005. ' TEnumRg = green..blue;',
  5006. ' TArr = array[TEnumRg] of byte;',
  5007. ' TArr2 = array[green..blue] of byte;',
  5008. 'var',
  5009. ' a: TArr;',
  5010. ' b: TArr = (3,4);',
  5011. ' c: TArr2 = (5,6);',
  5012. 'begin',
  5013. ' a[green] := b[blue];',
  5014. ' c[green] := c[blue];',
  5015. '']);
  5016. ConvertProgram;
  5017. CheckSource('TestEnumRange_Array',
  5018. LinesToStr([ // statements
  5019. 'this.TEnum = {',
  5020. ' "0": "Red",',
  5021. ' Red: 0,',
  5022. ' "1": "Green",',
  5023. ' Green: 1,',
  5024. ' "2": "Blue",',
  5025. ' Blue: 2',
  5026. '};',
  5027. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5028. 'this.b = [3, 4];',
  5029. 'this.c = [5, 6];',
  5030. '']),
  5031. LinesToStr([
  5032. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5033. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5034. '']));
  5035. end;
  5036. procedure TTestModule.TestEnum_ForIn;
  5037. begin
  5038. StartProgram(false);
  5039. Add([
  5040. 'type',
  5041. ' TEnum = (Red, Green, Blue);',
  5042. ' TEnumRg = green..blue;',
  5043. ' TArr = array[TEnum] of byte;',
  5044. ' TArrRg = array[TEnumRg] of byte;',
  5045. 'var',
  5046. ' e: TEnum;',
  5047. ' a1: TArr = (3,4,5);',
  5048. ' a2: TArrRg = (11,12);',
  5049. ' b: byte;',
  5050. 'begin',
  5051. ' for e in TEnum do ;',
  5052. ' for e in TEnumRg do ;',
  5053. ' for e in TArr do ;',
  5054. ' for e in TArrRg do ;',
  5055. ' for b in a1 do ;',
  5056. ' for b in a2 do ;',
  5057. '']);
  5058. ConvertProgram;
  5059. CheckSource('TestEnum_ForIn',
  5060. LinesToStr([ // statements
  5061. 'this.TEnum = {',
  5062. ' "0": "Red",',
  5063. ' Red: 0,',
  5064. ' "1": "Green",',
  5065. ' Green: 1,',
  5066. ' "2": "Blue",',
  5067. ' Blue: 2',
  5068. '};',
  5069. 'this.e = 0;',
  5070. 'this.a1 = [3, 4, 5];',
  5071. 'this.a2 = [11, 12];',
  5072. 'this.b = 0;',
  5073. '']),
  5074. LinesToStr([
  5075. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5076. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5077. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5078. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5079. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  5080. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  5081. '']));
  5082. end;
  5083. procedure TTestModule.TestEnum_ScopedNumber;
  5084. begin
  5085. Converter.Options:=Converter.Options+[coEnumNumbers];
  5086. StartProgram(false);
  5087. Add([
  5088. 'type',
  5089. ' TEnum = (Red, Green);',
  5090. 'var',
  5091. ' e: TEnum;',
  5092. 'begin',
  5093. ' e:=TEnum.Green;',
  5094. '']);
  5095. ConvertProgram;
  5096. CheckSource('TestEnum_ScopedNumber',
  5097. LinesToStr([ // statements
  5098. 'this.TEnum = {',
  5099. ' "0": "Red",',
  5100. ' Red: 0,',
  5101. ' "1": "Green",',
  5102. ' Green: 1',
  5103. '};',
  5104. 'this.e = 0;',
  5105. '']),
  5106. LinesToStr([
  5107. '$mod.e = 1;']));
  5108. end;
  5109. procedure TTestModule.TestEnum_InFunction;
  5110. begin
  5111. StartProgram(false);
  5112. Add([
  5113. 'const TEnum = 3;',
  5114. 'procedure DoIt;',
  5115. 'type',
  5116. ' TEnum = (Red, Green, Blue);',
  5117. ' procedure Sub;',
  5118. ' type',
  5119. ' TEnumSub = (Left, Right);',
  5120. ' var',
  5121. ' es: TEnumSub;',
  5122. ' begin',
  5123. ' es:=Left;',
  5124. ' end;',
  5125. 'var',
  5126. ' e, e2: TEnum;',
  5127. 'begin',
  5128. ' if e in [red,blue] then e2:=e;',
  5129. 'end;',
  5130. 'begin']);
  5131. ConvertProgram;
  5132. CheckSource('TestEnum_InFunction',
  5133. LinesToStr([ // statements
  5134. 'this.TEnum = 3;',
  5135. 'var TEnum$1 = {',
  5136. ' "0":"Red",',
  5137. ' Red:0,',
  5138. ' "1":"Green",',
  5139. ' Green:1,',
  5140. ' "2":"Blue",',
  5141. ' Blue:2',
  5142. ' };',
  5143. 'var TEnumSub = {',
  5144. ' "0": "Left",',
  5145. ' Left: 0,',
  5146. ' "1": "Right",',
  5147. ' Right: 1',
  5148. '};',
  5149. 'this.DoIt = function () {',
  5150. ' function Sub() {',
  5151. ' var es = 0;',
  5152. ' es = TEnumSub.Left;',
  5153. ' };',
  5154. ' var e = 0;',
  5155. ' var e2 = 0;',
  5156. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5157. '};',
  5158. '']),
  5159. LinesToStr([
  5160. '']));
  5161. end;
  5162. procedure TTestModule.TestSet_Enum;
  5163. begin
  5164. StartProgram(false);
  5165. Add([
  5166. 'type',
  5167. ' TColor = (Red, Green, Blue);',
  5168. ' TColors = set of TColor;',
  5169. 'var',
  5170. ' c: TColor;',
  5171. ' s: TColors;',
  5172. ' t: TColors = [];',
  5173. ' u: TColors = [Red];',
  5174. 'begin',
  5175. ' s:=[];',
  5176. ' s:=[Green];',
  5177. ' s:=[Green,Blue];',
  5178. ' s:=[Red..Blue];',
  5179. ' s:=[Red,Green..Blue];',
  5180. ' s:=[Red,c];',
  5181. ' s:=t;',
  5182. ' s:=default(TColors);',
  5183. '']);
  5184. ConvertProgram;
  5185. CheckSource('TestSet',
  5186. LinesToStr([ // statements
  5187. 'this.TColor = {',
  5188. ' "0":"Red",',
  5189. ' Red:0,',
  5190. ' "1":"Green",',
  5191. ' Green:1,',
  5192. ' "2":"Blue",',
  5193. ' Blue:2',
  5194. ' };',
  5195. 'this.c = 0;',
  5196. 'this.s = {};',
  5197. 'this.t = {};',
  5198. 'this.u = rtl.createSet($mod.TColor.Red);'
  5199. ]),
  5200. LinesToStr([
  5201. '$mod.s={};',
  5202. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5203. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5204. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5205. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5206. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5207. '$mod.s=rtl.refSet($mod.t);',
  5208. '$mod.s={};',
  5209. '']));
  5210. end;
  5211. procedure TTestModule.TestSet_Operators;
  5212. begin
  5213. StartProgram(false);
  5214. Add('type');
  5215. Add(' TColor = (Red, Green, Blue);');
  5216. Add(' TColors = set of tcolor;');
  5217. Add('var');
  5218. Add(' vC: TColor;');
  5219. Add(' vS: TColors;');
  5220. Add(' vT: TColors;');
  5221. Add(' vU: TColors;');
  5222. Add(' B: boolean;');
  5223. Add('begin');
  5224. Add(' include(vs,green);');
  5225. Add(' exclude(vs,vc);');
  5226. Add(' vs:=vt+vu;');
  5227. Add(' vs:=vt+[red];');
  5228. Add(' vs:=[red]+vt;');
  5229. Add(' vs:=[red]+[green];');
  5230. Add(' vs:=vt-vu;');
  5231. Add(' vs:=vt-[red];');
  5232. Add(' vs:=[red]-vt;');
  5233. Add(' vs:=[red]-[green];');
  5234. Add(' vs:=vt*vu;');
  5235. Add(' vs:=vt*[red];');
  5236. Add(' vs:=[red]*vt;');
  5237. Add(' vs:=[red]*[green];');
  5238. Add(' vs:=vt><vu;');
  5239. Add(' vs:=vt><[red];');
  5240. Add(' vs:=[red]><vt;');
  5241. Add(' vs:=[red]><[green];');
  5242. Add(' b:=vt=vu;');
  5243. Add(' b:=vt=[red];');
  5244. Add(' b:=[red]=vt;');
  5245. Add(' b:=[red]=[green];');
  5246. Add(' b:=vt<>vu;');
  5247. Add(' b:=vt<>[red];');
  5248. Add(' b:=[red]<>vt;');
  5249. Add(' b:=[red]<>[green];');
  5250. Add(' b:=vt<=vu;');
  5251. Add(' b:=vt<=[red];');
  5252. Add(' b:=[red]<=vt;');
  5253. Add(' b:=[red]<=[green];');
  5254. Add(' b:=vt>=vu;');
  5255. Add(' b:=vt>=[red];');
  5256. Add(' b:=[red]>=vt;');
  5257. Add(' b:=[red]>=[green];');
  5258. ConvertProgram;
  5259. CheckSource('TestSet_Operators',
  5260. LinesToStr([ // statements
  5261. 'this.TColor = {',
  5262. ' "0":"Red",',
  5263. ' Red:0,',
  5264. ' "1":"Green",',
  5265. ' Green:1,',
  5266. ' "2":"Blue",',
  5267. ' Blue:2',
  5268. ' };',
  5269. 'this.vC = 0;',
  5270. 'this.vS = {};',
  5271. 'this.vT = {};',
  5272. 'this.vU = {};',
  5273. 'this.B = false;'
  5274. ]),
  5275. LinesToStr([
  5276. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5277. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5278. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5279. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5280. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5281. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5282. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5283. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5284. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5285. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5286. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5287. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5288. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5289. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5290. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5291. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5292. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5293. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5294. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5295. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5296. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5297. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5298. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5299. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5300. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5301. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5302. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5303. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5304. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5305. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5306. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5307. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5308. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5309. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5310. '']));
  5311. end;
  5312. procedure TTestModule.TestSet_Operator_In;
  5313. begin
  5314. StartProgram(false);
  5315. Add('type');
  5316. Add(' TColor = (Red, Green, Blue);');
  5317. Add(' TColors = set of tcolor;');
  5318. Add('var');
  5319. Add(' vC: tcolor;');
  5320. Add(' vT: tcolors;');
  5321. Add(' B: boolean;');
  5322. Add('begin');
  5323. Add(' b:=red in vt;');
  5324. Add(' b:=vc in vt;');
  5325. Add(' b:=green in [red..blue];');
  5326. Add(' b:=vc in [red..blue];');
  5327. Add(' ');
  5328. Add(' if red in vt then ;');
  5329. Add(' while vC in vt do ;');
  5330. Add(' repeat');
  5331. Add(' until vC in vt;');
  5332. ConvertProgram;
  5333. CheckSource('TestSet_Operator_In',
  5334. LinesToStr([ // statements
  5335. 'this.TColor = {',
  5336. ' "0":"Red",',
  5337. ' Red:0,',
  5338. ' "1":"Green",',
  5339. ' Green:1,',
  5340. ' "2":"Blue",',
  5341. ' Blue:2',
  5342. ' };',
  5343. 'this.vC = 0;',
  5344. 'this.vT = {};',
  5345. 'this.B = false;'
  5346. ]),
  5347. LinesToStr([
  5348. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5349. '$mod.B = $mod.vC in $mod.vT;',
  5350. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5351. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5352. 'if ($mod.TColor.Red in $mod.vT) ;',
  5353. 'while ($mod.vC in $mod.vT) {',
  5354. '};',
  5355. 'do {',
  5356. '} while (!($mod.vC in $mod.vT));',
  5357. '']));
  5358. end;
  5359. procedure TTestModule.TestSet_Functions;
  5360. begin
  5361. StartProgram(false);
  5362. Add('type');
  5363. Add(' TMyEnum = (Red, Green);');
  5364. Add(' TMyEnums = set of TMyEnum;');
  5365. Add('var');
  5366. Add(' e: TMyEnum;');
  5367. Add(' s: TMyEnums;');
  5368. Add('begin');
  5369. Add(' e:=Low(TMyEnums);');
  5370. Add(' e:=Low(s);');
  5371. Add(' e:=High(TMyEnums);');
  5372. Add(' e:=High(s);');
  5373. ConvertProgram;
  5374. CheckSource('TestSetFunctions',
  5375. LinesToStr([ // statements
  5376. 'this.TMyEnum = {',
  5377. ' "0":"Red",',
  5378. ' Red:0,',
  5379. ' "1":"Green",',
  5380. ' Green:1',
  5381. ' };',
  5382. 'this.e = 0;',
  5383. 'this.s = {};'
  5384. ]),
  5385. LinesToStr([
  5386. '$mod.e=$mod.TMyEnum.Red;',
  5387. '$mod.e=$mod.TMyEnum.Red;',
  5388. '$mod.e=$mod.TMyEnum.Green;',
  5389. '$mod.e=$mod.TMyEnum.Green;',
  5390. '']));
  5391. end;
  5392. procedure TTestModule.TestSet_PassAsArgClone;
  5393. begin
  5394. StartProgram(false);
  5395. Add('type');
  5396. Add(' TMyEnum = (Red, Green);');
  5397. Add(' TMyEnums = set of TMyEnum;');
  5398. Add('procedure DoDefault(s: tmyenums); begin end;');
  5399. Add('procedure DoConst(const s: tmyenums); begin end;');
  5400. Add('var');
  5401. Add(' aSet: tmyenums;');
  5402. Add('begin');
  5403. Add(' dodefault(aset);');
  5404. Add(' doconst(aset);');
  5405. ConvertProgram;
  5406. CheckSource('TestSetFunctions',
  5407. LinesToStr([ // statements
  5408. 'this.TMyEnum = {',
  5409. ' "0":"Red",',
  5410. ' Red:0,',
  5411. ' "1":"Green",',
  5412. ' Green:1',
  5413. ' };',
  5414. 'this.DoDefault = function (s) {',
  5415. '};',
  5416. 'this.DoConst = function (s) {',
  5417. '};',
  5418. 'this.aSet = {};'
  5419. ]),
  5420. LinesToStr([
  5421. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5422. '$mod.DoConst($mod.aSet);',
  5423. '']));
  5424. end;
  5425. procedure TTestModule.TestSet_AsParams;
  5426. begin
  5427. StartProgram(false);
  5428. Add([
  5429. 'type TEnum = (Red,Blue);',
  5430. 'type TEnums = set of TEnum;',
  5431. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5432. 'var vJ: TEnums;',
  5433. 'begin',
  5434. ' Include(vg,red);',
  5435. ' Include(result,blue);',
  5436. ' vg:=vg;',
  5437. ' vj:=vh;',
  5438. ' vi:=vi;',
  5439. ' doit(vg,vg,vg);',
  5440. ' doit(vh,vh,vj);',
  5441. ' doit(vi,vi,vi);',
  5442. ' doit(vj,vj,vj);',
  5443. 'end;',
  5444. 'var i: TEnums;',
  5445. 'begin',
  5446. ' doit(i,i,i);']);
  5447. ConvertProgram;
  5448. CheckSource('TestSet_AsParams',
  5449. LinesToStr([ // statements
  5450. 'this.TEnum = {',
  5451. ' "0": "Red",',
  5452. ' Red: 0,',
  5453. ' "1": "Blue",',
  5454. ' Blue: 1',
  5455. '};',
  5456. 'this.DoIt = function (vG,vH,vI) {',
  5457. ' var Result = {};',
  5458. ' var vJ = {};',
  5459. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5460. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5461. ' vG = rtl.refSet(vG);',
  5462. ' vJ = rtl.refSet(vH);',
  5463. ' vI.set(rtl.refSet(vI.get()));',
  5464. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5465. ' get: function () {',
  5466. ' return vG;',
  5467. ' },',
  5468. ' set: function (v) {',
  5469. ' vG = v;',
  5470. ' }',
  5471. ' });',
  5472. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5473. ' get: function () {',
  5474. ' return vJ;',
  5475. ' },',
  5476. ' set: function (v) {',
  5477. ' vJ = v;',
  5478. ' }',
  5479. ' });',
  5480. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5481. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5482. ' get: function () {',
  5483. ' return vJ;',
  5484. ' },',
  5485. ' set: function (v) {',
  5486. ' vJ = v;',
  5487. ' }',
  5488. ' });',
  5489. ' return Result;',
  5490. '};',
  5491. 'this.i = {};'
  5492. ]),
  5493. LinesToStr([
  5494. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5495. ' p: $mod,',
  5496. ' get: function () {',
  5497. ' return this.p.i;',
  5498. ' },',
  5499. ' set: function (v) {',
  5500. ' this.p.i = v;',
  5501. ' }',
  5502. '});'
  5503. ]));
  5504. end;
  5505. procedure TTestModule.TestSet_Property;
  5506. begin
  5507. StartProgram(false);
  5508. Add('type');
  5509. Add(' TEnum = (Red,Blue);');
  5510. Add(' TEnums = set of TEnum;');
  5511. Add(' TObject = class');
  5512. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5513. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5514. Add(' property Colors: TEnums read GetColors write SetColors;');
  5515. Add(' end;');
  5516. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5517. Add('begin end;');
  5518. Add('var Obj: TObject;');
  5519. Add('begin');
  5520. Add(' Include(Obj.Colors,Red);');
  5521. Add(' Exclude(Obj.Colors,Red);');
  5522. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5523. ConvertProgram;
  5524. CheckSource('TestSet_Property',
  5525. LinesToStr([ // statements
  5526. 'this.TEnum = {',
  5527. ' "0": "Red",',
  5528. ' Red: 0,',
  5529. ' "1": "Blue",',
  5530. ' Blue: 1',
  5531. '};',
  5532. 'rtl.createClass($mod, "TObject", null, function () {',
  5533. ' this.$init = function () {',
  5534. ' };',
  5535. ' this.$final = function () {',
  5536. ' };',
  5537. '});',
  5538. 'this.DoIt = function (i, j, k, l) {',
  5539. '};',
  5540. 'this.Obj = null;',
  5541. '']),
  5542. LinesToStr([
  5543. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5544. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5545. '']));
  5546. end;
  5547. procedure TTestModule.TestSet_EnumConst;
  5548. begin
  5549. StartProgram(false);
  5550. Add([
  5551. 'type',
  5552. ' TEnum = (Red,Blue);',
  5553. ' TEnums = set of TEnum;',
  5554. 'const',
  5555. ' Orange = red;',
  5556. 'var',
  5557. ' Enum: tenum;',
  5558. ' Enums: tenums;',
  5559. 'begin',
  5560. ' Include(enums,orange);',
  5561. ' Exclude(enums,orange);',
  5562. ' if orange in enums then;',
  5563. ' if orange in [orange,red] then;']);
  5564. ConvertProgram;
  5565. CheckSource('TestSet_EnumConst',
  5566. LinesToStr([ // statements
  5567. 'this.TEnum = {',
  5568. ' "0": "Red",',
  5569. ' Red: 0,',
  5570. ' "1": "Blue",',
  5571. ' Blue: 1',
  5572. '};',
  5573. 'this.Orange = $mod.TEnum.Red;',
  5574. 'this.Enum = 0;',
  5575. 'this.Enums = {};',
  5576. '']),
  5577. LinesToStr([
  5578. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5579. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5580. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5581. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5582. '']));
  5583. end;
  5584. procedure TTestModule.TestSet_IntConst;
  5585. begin
  5586. StartProgram(false);
  5587. Add([
  5588. 'type',
  5589. ' TEnums = set of Byte;',
  5590. 'const',
  5591. ' Orange = 0;',
  5592. 'var',
  5593. ' Enum: byte;',
  5594. ' Enums: tenums;',
  5595. 'begin',
  5596. ' Enums:=[];',
  5597. ' Enums:=[0];',
  5598. ' Enums:=[1..2];',
  5599. //' Include(enums,orange);',
  5600. //' Exclude(enums,orange);',
  5601. ' if orange in enums then;',
  5602. ' if orange in [orange,1] then;']);
  5603. ConvertProgram;
  5604. CheckSource('TestSet_IntConst',
  5605. LinesToStr([ // statements
  5606. 'this.Orange = 0;',
  5607. 'this.Enum = 0;',
  5608. 'this.Enums = {};',
  5609. '']),
  5610. LinesToStr([
  5611. '$mod.Enums = {};',
  5612. '$mod.Enums = rtl.createSet(0);',
  5613. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5614. 'if (0 in $mod.Enums) ;',
  5615. 'if (0 in rtl.createSet(0, 1)) ;',
  5616. '']));
  5617. end;
  5618. procedure TTestModule.TestSet_AnonymousEnumType;
  5619. begin
  5620. StartProgram(false);
  5621. Add('type');
  5622. Add(' TFlags = set of (red, green);');
  5623. Add('const');
  5624. Add(' favorite = red;');
  5625. Add('var');
  5626. Add(' f: TFlags;');
  5627. Add(' i: longint;');
  5628. Add('begin');
  5629. Add(' Include(f,red);');
  5630. Add(' Include(f,favorite);');
  5631. Add(' i:=ord(red);');
  5632. Add(' i:=ord(favorite);');
  5633. Add(' i:=ord(low(TFlags));');
  5634. Add(' i:=ord(low(f));');
  5635. Add(' i:=ord(low(favorite));');
  5636. Add(' i:=ord(high(TFlags));');
  5637. Add(' i:=ord(high(f));');
  5638. Add(' i:=ord(high(favorite));');
  5639. Add(' f:=[green,favorite];');
  5640. ConvertProgram;
  5641. CheckSource('TestSet_AnonymousEnumType',
  5642. LinesToStr([ // statements
  5643. 'this.TFlags$a = {',
  5644. ' "0": "red",',
  5645. ' red: 0,',
  5646. ' "1": "green",',
  5647. ' green: 1',
  5648. '};',
  5649. 'this.favorite = $mod.TFlags$a.red;',
  5650. 'this.f = {};',
  5651. 'this.i = 0;',
  5652. '']),
  5653. LinesToStr([
  5654. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5655. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5656. '$mod.i = $mod.TFlags$a.red;',
  5657. '$mod.i = $mod.TFlags$a.red;',
  5658. '$mod.i = $mod.TFlags$a.red;',
  5659. '$mod.i = $mod.TFlags$a.red;',
  5660. '$mod.i = $mod.TFlags$a.red;',
  5661. '$mod.i = $mod.TFlags$a.green;',
  5662. '$mod.i = $mod.TFlags$a.green;',
  5663. '$mod.i = $mod.TFlags$a.green;',
  5664. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5665. '']));
  5666. end;
  5667. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5668. begin
  5669. exit;
  5670. StartProgram(false);
  5671. Add([
  5672. 'type',
  5673. ' TAtoZ = ''A''..''Z'';',
  5674. ' TSetOfAZ = set of TAtoZ;',
  5675. 'var',
  5676. ' c: char;',
  5677. ' a: TAtoZ;',
  5678. ' s: TSetOfAZ = [''P'',''A''];',
  5679. ' i: longint;',
  5680. 'begin',
  5681. ' Include(s,''S'');',
  5682. ' Include(s,c);',
  5683. ' Include(s,a);',
  5684. ' c:=low(TAtoZ);',
  5685. ' i:=ord(low(TAtoZ));',
  5686. ' a:=high(TAtoZ);',
  5687. ' a:=high(TSetOfAtoZ);',
  5688. ' s:=[a,c,''M''];',
  5689. '']);
  5690. ConvertProgram;
  5691. CheckSource('TestSet_AnonymousEnumTypeChar',
  5692. LinesToStr([ // statements
  5693. '']),
  5694. LinesToStr([
  5695. '']));
  5696. end;
  5697. procedure TTestModule.TestSet_ConstEnum;
  5698. begin
  5699. StartProgram(false);
  5700. Add([
  5701. 'type',
  5702. ' TEnum = (red,blue,green);',
  5703. ' TEnums = set of TEnum;',
  5704. 'const',
  5705. ' teAny = [low(TEnum)..high(TEnum)];',
  5706. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5707. 'var',
  5708. ' e: TEnum;',
  5709. ' s: TEnums;',
  5710. 'begin',
  5711. ' if blue in teAny then;',
  5712. ' if blue in teAny+[e] then;',
  5713. ' if blue in teAny+teRedBlue then;',
  5714. ' if e in [red,blue] then;',
  5715. ' s:=teAny;',
  5716. ' s:=teAny+[e];',
  5717. ' s:=[e]+teAny;',
  5718. ' s:=teAny+teRedBlue;',
  5719. ' s:=teAny+teRedBlue+[e];',
  5720. '']);
  5721. ConvertProgram;
  5722. CheckSource('TestSet_ConstEnum',
  5723. LinesToStr([ // statements
  5724. 'this.TEnum = {',
  5725. ' "0": "red",',
  5726. ' red: 0,',
  5727. ' "1": "blue",',
  5728. ' blue: 1,',
  5729. ' "2": "green",',
  5730. ' green: 2',
  5731. '};',
  5732. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5733. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5734. 'this.e = 0;',
  5735. 'this.s = {};',
  5736. '']),
  5737. LinesToStr([
  5738. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5739. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5740. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5741. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5742. '$mod.s = rtl.refSet($mod.teAny);',
  5743. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5744. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5745. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5746. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5747. '']));
  5748. end;
  5749. procedure TTestModule.TestSet_ConstChar;
  5750. begin
  5751. StartProgram(false);
  5752. Add([
  5753. 'const',
  5754. ' LowChars = [''a''..''z''];',
  5755. ' Chars = LowChars+[''A''..''Z''];',
  5756. ' sc = [''А'', ''Я''];',
  5757. 'var',
  5758. ' c: char;',
  5759. ' s: string;',
  5760. 'begin',
  5761. ' if c in lowchars then ;',
  5762. ' if ''a'' in lowchars then ;',
  5763. ' if s[1] in lowchars then ;',
  5764. ' if c in chars then ;',
  5765. ' if c in [''a''..''z'',''_''] then ;',
  5766. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5767. ' if ''Я'' in sc then ;',
  5768. '']);
  5769. ConvertProgram;
  5770. CheckSource('TestSet_ConstChar',
  5771. LinesToStr([ // statements
  5772. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5773. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5774. 'this.sc = rtl.createSet(1040, 1071);',
  5775. 'this.c = "";',
  5776. 'this.s = "";',
  5777. '']),
  5778. LinesToStr([
  5779. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5780. 'if (97 in $mod.LowChars) ;',
  5781. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5782. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5783. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5784. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5785. 'if (1071 in $mod.sc) ;',
  5786. '']));
  5787. end;
  5788. procedure TTestModule.TestSet_ConstInt;
  5789. begin
  5790. StartProgram(false);
  5791. Add([
  5792. 'const',
  5793. ' Months = [1..12];',
  5794. ' Mirror = [-12..-1]+Months;',
  5795. 'var',
  5796. ' i: smallint;',
  5797. 'begin',
  5798. ' if 3 in Months then;',
  5799. ' if i in Months+[i] then;',
  5800. ' if i in Months+Mirror then;',
  5801. ' if i in [4..6,8] then;',
  5802. '']);
  5803. ConvertProgram;
  5804. CheckSource('TestSet_ConstInt',
  5805. LinesToStr([ // statements
  5806. 'this.Months = rtl.createSet(null, 1, 12);',
  5807. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5808. 'this.i = 0;',
  5809. '']),
  5810. LinesToStr([
  5811. 'if (3 in $mod.Months) ;',
  5812. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5813. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5814. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5815. '']));
  5816. end;
  5817. procedure TTestModule.TestSet_InFunction;
  5818. begin
  5819. StartProgram(false);
  5820. Add([
  5821. 'const',
  5822. ' TEnum = 3;',
  5823. ' TSetOfEnum = 4;',
  5824. ' TSetOfAno = 5;',
  5825. 'procedure DoIt;',
  5826. 'type',
  5827. ' TEnum = (red, blue);',
  5828. ' TSetOfEnum = set of TEnum;',
  5829. ' TSetOfAno = set of (up,down);',
  5830. 'var',
  5831. ' e: TEnum;',
  5832. ' se: TSetOfEnum;',
  5833. ' sa: TSetOfAno;',
  5834. 'begin',
  5835. ' se:=[e];',
  5836. ' sa:=[up];',
  5837. 'end;',
  5838. 'begin',
  5839. '']);
  5840. ConvertProgram;
  5841. CheckSource('TestSet_InFunction',
  5842. LinesToStr([ // statements
  5843. 'this.TEnum = 3;',
  5844. 'this.TSetOfEnum = 4;',
  5845. 'this.TSetOfAno = 5;',
  5846. 'var TEnum$1 = {',
  5847. ' "0": "red",',
  5848. ' red: 0,',
  5849. ' "1": "blue",',
  5850. ' blue: 1',
  5851. '};',
  5852. 'var TSetOfAno$a = {',
  5853. ' "0": "up",',
  5854. ' up: 0,',
  5855. ' "1": "down",',
  5856. ' down: 1',
  5857. '};',
  5858. 'this.DoIt = function () {',
  5859. ' var e = 0;',
  5860. ' var se = {};',
  5861. ' var sa = {};',
  5862. ' se = rtl.createSet(e);',
  5863. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5864. '};',
  5865. '']),
  5866. LinesToStr([
  5867. '']));
  5868. end;
  5869. procedure TTestModule.TestSet_ForIn;
  5870. begin
  5871. StartProgram(false);
  5872. Add([
  5873. 'type',
  5874. ' TEnum = (Red, Green, Blue);',
  5875. ' TEnumRg = green..blue;',
  5876. ' TSetOfEnum = set of TEnum;',
  5877. ' TSetOfEnumRg = set of TEnumRg;',
  5878. 'var',
  5879. ' e, e2: TEnum;',
  5880. ' er: TEnum;',
  5881. ' s: TSetOfEnum;',
  5882. 'begin',
  5883. ' for e in TSetOfEnum do ;',
  5884. ' for e in TSetOfEnumRg do ;',
  5885. ' for e in [] do e2:=e;',
  5886. ' for e in [red..green] do e2:=e;',
  5887. ' for e in [green,blue] do e2:=e;',
  5888. ' for e in [red,blue] do e2:=e;',
  5889. ' for e in s do e2:=e;',
  5890. ' for er in TSetOfEnumRg do ;',
  5891. '']);
  5892. ConvertProgram;
  5893. CheckSource('TestSet_ForIn',
  5894. LinesToStr([ // statements
  5895. 'this.TEnum = {',
  5896. ' "0":"Red",',
  5897. ' Red:0,',
  5898. ' "1":"Green",',
  5899. ' Green:1,',
  5900. ' "2":"Blue",',
  5901. ' Blue:2',
  5902. ' };',
  5903. 'this.e = 0;',
  5904. 'this.e2 = 0;',
  5905. 'this.er = 0;',
  5906. 'this.s = {};',
  5907. '']),
  5908. LinesToStr([
  5909. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5910. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5911. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5912. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5913. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5914. 'for (var $l1 in $mod.s){',
  5915. ' $mod.e = +$l1;',
  5916. ' $mod.e2 = $mod.e;',
  5917. '};',
  5918. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  5919. '']));
  5920. end;
  5921. procedure TTestModule.TestNestBegin;
  5922. begin
  5923. StartProgram(false);
  5924. Add('begin');
  5925. Add(' begin');
  5926. Add(' begin');
  5927. Add(' end;');
  5928. Add(' begin');
  5929. Add(' if true then ;');
  5930. Add(' end;');
  5931. Add(' end;');
  5932. ConvertProgram;
  5933. CheckSource('TestNestBegin',
  5934. '',
  5935. 'if (true) ;');
  5936. end;
  5937. procedure TTestModule.TestUnitImplVars;
  5938. begin
  5939. StartUnit(false);
  5940. Add('interface');
  5941. Add('implementation');
  5942. Add('var');
  5943. Add(' V1:longint;');
  5944. Add(' V2:longint = 3;');
  5945. Add(' V3:string = ''abc'';');
  5946. ConvertUnit;
  5947. CheckSource('TestUnitImplVars',
  5948. LinesToStr([ // statements
  5949. 'var $impl = $mod.$impl;',
  5950. '']),
  5951. '', // this.$init
  5952. LinesToStr([ // implementation
  5953. '$impl.V1 = 0;',
  5954. '$impl.V2 = 3;',
  5955. '$impl.V3 = "abc";',
  5956. '']) );
  5957. end;
  5958. procedure TTestModule.TestUnitImplConsts;
  5959. begin
  5960. StartUnit(false);
  5961. Add('interface');
  5962. Add('implementation');
  5963. Add('const');
  5964. Add(' v1 = 3;');
  5965. Add(' v2:longint = 4;');
  5966. Add(' v3:string = ''abc'';');
  5967. ConvertUnit;
  5968. CheckSource('TestUnitImplConsts',
  5969. LinesToStr([ // statements
  5970. 'var $impl = $mod.$impl;',
  5971. '']),
  5972. '', // this.$init
  5973. LinesToStr([ // implementation
  5974. '$impl.v1 = 3;',
  5975. '$impl.v2 = 4;',
  5976. '$impl.v3 = "abc";',
  5977. '']) );
  5978. end;
  5979. procedure TTestModule.TestUnitImplRecord;
  5980. begin
  5981. StartUnit(false);
  5982. Add('interface');
  5983. Add('implementation');
  5984. Add('type');
  5985. Add(' TMyRecord = record');
  5986. Add(' i: longint;');
  5987. Add(' end;');
  5988. Add('var aRec: TMyRecord;');
  5989. Add('initialization');
  5990. Add(' arec.i:=3;');
  5991. ConvertUnit;
  5992. CheckSource('TestUnitImplRecord',
  5993. LinesToStr([ // statements
  5994. 'var $impl = $mod.$impl;',
  5995. '']),
  5996. // this.$init
  5997. '$impl.aRec.i = 3;',
  5998. LinesToStr([ // implementation
  5999. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6000. ' this.i = 0;',
  6001. ' this.$eq = function (b) {',
  6002. ' return this.i === b.i;',
  6003. ' };',
  6004. ' this.$assign = function (s) {',
  6005. ' this.i = s.i;',
  6006. ' return this;',
  6007. ' };',
  6008. '});',
  6009. '$impl.aRec = $impl.TMyRecord.$new();',
  6010. '']) );
  6011. end;
  6012. procedure TTestModule.TestRenameJSNameConflict;
  6013. begin
  6014. StartProgram(false);
  6015. Add('var apply: longint;');
  6016. Add('var bind: longint;');
  6017. Add('var call: longint;');
  6018. Add('begin');
  6019. ConvertProgram;
  6020. CheckSource('TestRenameJSNameConflict',
  6021. LinesToStr([ // statements
  6022. 'this.Apply = 0;',
  6023. 'this.Bind = 0;',
  6024. 'this.Call = 0;'
  6025. ]),
  6026. LinesToStr([ // this.$main
  6027. ''
  6028. ]));
  6029. end;
  6030. procedure TTestModule.TestLocalConst;
  6031. begin
  6032. StartProgram(false);
  6033. Add('procedure DoIt;');
  6034. Add('const');
  6035. Add(' cA: longint = 1;');
  6036. Add(' cB = 2;');
  6037. Add(' procedure Sub;');
  6038. Add(' const');
  6039. Add(' csA = 3;');
  6040. Add(' cB: double = 4;');
  6041. Add(' begin');
  6042. Add(' cb:=cb+csa;');
  6043. Add(' ca:=ca+csa+5;');
  6044. Add(' end;');
  6045. Add('begin');
  6046. Add(' ca:=ca+cb+6;');
  6047. Add('end;');
  6048. Add('begin');
  6049. ConvertProgram;
  6050. CheckSource('TestLocalConst',
  6051. LinesToStr([
  6052. 'var cA = 1;',
  6053. 'var cB = 2;',
  6054. 'var csA = 3;',
  6055. 'var cB$1 = 4;',
  6056. 'this.DoIt = function () {',
  6057. ' function Sub() {',
  6058. ' cB$1 = cB$1 + 3;',
  6059. ' cA = cA + 3 + 5;',
  6060. ' };',
  6061. ' cA = cA + 2 + 6;',
  6062. '};'
  6063. ]),
  6064. LinesToStr([
  6065. ]));
  6066. end;
  6067. procedure TTestModule.TestVarExternal;
  6068. begin
  6069. StartProgram(false);
  6070. Add('var');
  6071. Add(' NaN: double; external name ''Global.NaN'';');
  6072. Add(' d: double;');
  6073. Add('begin');
  6074. Add(' d:=NaN;');
  6075. ConvertProgram;
  6076. CheckSource('TestVarExternal',
  6077. LinesToStr([
  6078. 'this.d = 0.0;'
  6079. ]),
  6080. LinesToStr([
  6081. '$mod.d = Global.NaN;'
  6082. ]));
  6083. end;
  6084. procedure TTestModule.TestVarExternalOtherUnit;
  6085. begin
  6086. AddModuleWithIntfImplSrc('unit2.pas',
  6087. LinesToStr([
  6088. 'var NaN: double; external name ''Global.NaN'';',
  6089. 'var iV: longint;'
  6090. ]),
  6091. '');
  6092. StartUnit(true);
  6093. Add('interface');
  6094. Add('uses unit2;');
  6095. Add('implementation');
  6096. Add('var');
  6097. Add(' d: double;');
  6098. Add(' i: longint; external name ''$i'';');
  6099. Add('begin');
  6100. Add(' d:=nan;');
  6101. Add(' d:=uNit2.nan;');
  6102. Add(' d:=test1.d;');
  6103. Add(' i:=iv;');
  6104. Add(' i:=uNit2.iv;');
  6105. Add(' i:=test1.i;');
  6106. ConvertUnit;
  6107. CheckSource('TestVarExternalOtherUnit',
  6108. LinesToStr([
  6109. 'var $impl = $mod.$impl;',
  6110. '']),
  6111. LinesToStr([ // this.$init
  6112. '$impl.d = Global.NaN;',
  6113. '$impl.d = Global.NaN;',
  6114. '$impl.d = $impl.d;',
  6115. '$i = pas.unit2.iV;',
  6116. '$i = pas.unit2.iV;',
  6117. '$i = $i;',
  6118. '']),
  6119. LinesToStr([ // implementation
  6120. '$impl.d = 0.0;',
  6121. '']) );
  6122. end;
  6123. procedure TTestModule.TestVarAbsoluteFail;
  6124. begin
  6125. StartProgram(false);
  6126. Add([
  6127. 'var',
  6128. ' a: longint;',
  6129. ' b: longword absolute a;',
  6130. 'begin']);
  6131. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6132. ConvertProgram;
  6133. end;
  6134. procedure TTestModule.TestConstExternal;
  6135. begin
  6136. StartProgram(false);
  6137. Add([
  6138. 'const',
  6139. ' PI: double; external name ''Global.PI'';',
  6140. ' Tau = 2*pi;',
  6141. 'var d: double;',
  6142. 'begin',
  6143. ' d:=pi;',
  6144. ' d:=tau+pi;']);
  6145. ConvertProgram;
  6146. CheckSource('TestConstExternal',
  6147. LinesToStr([
  6148. 'this.Tau = 2*Global.PI;',
  6149. 'this.d = 0.0;'
  6150. ]),
  6151. LinesToStr([
  6152. '$mod.d = Global.PI;',
  6153. '$mod.d = $mod.Tau + Global.PI;'
  6154. ]));
  6155. end;
  6156. procedure TTestModule.TestDouble;
  6157. begin
  6158. StartProgram(false);
  6159. Add([
  6160. 'type',
  6161. ' TDateTime = double;',
  6162. 'const',
  6163. ' a = TDateTime(2.7);',
  6164. ' b = a + TDateTime(1.7);',
  6165. ' c = 0.9 + 0.1;',
  6166. ' f0_1 = 0.1;',
  6167. ' f0_3 = 0.3;',
  6168. ' fn0_1 = -0.1;',
  6169. ' fn0_3 = -0.3;',
  6170. ' fn0_003 = -0.003;',
  6171. ' fn0_123456789 = -0.123456789;',
  6172. ' fn300_0 = -300.0;',
  6173. ' fn123456_0 = -123456.0;',
  6174. ' fn1234567_8 = -1234567.8;',
  6175. ' fn12345678_9 = -12345678.9;',
  6176. ' f1_0En12 = 1E-12;',
  6177. ' fn1_0En12 = -1E-12;',
  6178. ' maxdouble = 1.7e+308;',
  6179. ' mindouble = -1.7e+308;',
  6180. ' MinSafeIntDouble = -$10000000000000;',
  6181. ' MinSafeIntDouble2 = -$fffffffffffff-1;',
  6182. ' MaxSafeIntDouble = $fffffffffffff;',
  6183. ' DZeroResolution = 1E-12;',
  6184. ' Minus1 = -1E-12;',
  6185. ' EPS = 1E-9;',
  6186. ' DELTA = 0.001;',
  6187. ' Big = 129.789E+100;',
  6188. ' Test0_15 = 0.15;',
  6189. ' Test999 = 2.9999999999999;',
  6190. ' Test111999 = 211199999999999000.0;',
  6191. ' TestMinus111999 = -211199999999999000.0;',
  6192. 'var',
  6193. ' d: double = b;',
  6194. 'begin',
  6195. ' d:=1.0;',
  6196. ' d:=1.0/3.0;',
  6197. ' d:=1/3;',
  6198. ' d:=5.0E-324;',
  6199. ' d:=1.7E308;',
  6200. ' d:=001.00E00;',
  6201. ' d:=002.00E001;',
  6202. ' d:=003.000E000;',
  6203. ' d:=-004.00E-00;',
  6204. ' d:=-005.00E-001;',
  6205. ' d:=10**3;',
  6206. ' d:=10 mod 3;',
  6207. ' d:=10 div 3;',
  6208. ' d:=c;',
  6209. ' d:=f0_1;',
  6210. ' d:=f0_3;',
  6211. ' d:=fn0_1;',
  6212. ' d:=fn0_3;',
  6213. ' d:=fn0_003;',
  6214. ' d:=fn0_123456789;',
  6215. ' d:=fn300_0;',
  6216. ' d:=fn123456_0;',
  6217. ' d:=fn1234567_8;',
  6218. ' d:=fn12345678_9;',
  6219. ' d:=f1_0En12;',
  6220. ' d:=fn1_0En12;',
  6221. ' d:=maxdouble;',
  6222. ' d:=mindouble;',
  6223. ' d:=MinSafeIntDouble;',
  6224. ' d:=double(MinSafeIntDouble);',
  6225. ' d:=MinSafeIntDouble2;',
  6226. ' d:=double(MinSafeIntDouble2);',
  6227. ' d:=MaxSafeIntDouble;',
  6228. ' d:=default(double);',
  6229. '']);
  6230. ConvertProgram;
  6231. CheckSource('TestDouble',
  6232. LinesToStr([
  6233. 'this.a = 2.7;',
  6234. 'this.b = 2.7 + 1.7;',
  6235. 'this.c = 0.9 + 0.1;',
  6236. 'this.f0_1 = 0.1;',
  6237. 'this.f0_3 = 0.3;',
  6238. 'this.fn0_1 = -0.1;',
  6239. 'this.fn0_3 = -0.3;',
  6240. 'this.fn0_003 = -0.003;',
  6241. 'this.fn0_123456789 = -0.123456789;',
  6242. 'this.fn300_0 = -300.0;',
  6243. 'this.fn123456_0 = -123456.0;',
  6244. 'this.fn1234567_8 = -1234567.8;',
  6245. 'this.fn12345678_9 = -12345678.9;',
  6246. 'this.f1_0En12 = 1E-12;',
  6247. 'this.fn1_0En12 = -1E-12;',
  6248. 'this.maxdouble = 1.7e+308;',
  6249. 'this.mindouble = -1.7e+308;',
  6250. 'this.MinSafeIntDouble = -0x10000000000000;',
  6251. 'this.MinSafeIntDouble2 = -0xfffffffffffff - 1;',
  6252. 'this.MaxSafeIntDouble = 0xfffffffffffff;',
  6253. 'this.DZeroResolution = 1E-12;',
  6254. 'this.Minus1 = -1E-12;',
  6255. 'this.EPS = 1E-9;',
  6256. 'this.DELTA = 0.001;',
  6257. 'this.Big = 129.789E+100;',
  6258. 'this.Test0_15 = 0.15;',
  6259. 'this.Test999 = 2.9999999999999;',
  6260. 'this.Test111999 = 211199999999999000.0;',
  6261. 'this.TestMinus111999 = -211199999999999000.0;',
  6262. 'this.d = 4.4;'
  6263. ]),
  6264. LinesToStr([
  6265. '$mod.d = 1.0;',
  6266. '$mod.d = 1.0 / 3.0;',
  6267. '$mod.d = 1 / 3;',
  6268. '$mod.d = 5.0E-324;',
  6269. '$mod.d = 1.7E308;',
  6270. '$mod.d = 1.00E0;',
  6271. '$mod.d = 2.00E1;',
  6272. '$mod.d = 3.000E0;',
  6273. '$mod.d = -4.00E-0;',
  6274. '$mod.d = -5.00E-1;',
  6275. '$mod.d = Math.pow(10, 3);',
  6276. '$mod.d = 10 % 3;',
  6277. '$mod.d = Math.floor(10 / 3);',
  6278. '$mod.d = 1;',
  6279. '$mod.d = 0.1;',
  6280. '$mod.d = 0.3;',
  6281. '$mod.d = -0.1;',
  6282. '$mod.d = -0.3;',
  6283. '$mod.d = -0.003;',
  6284. '$mod.d = -0.123456789;',
  6285. '$mod.d = -300;',
  6286. '$mod.d = -123456;',
  6287. '$mod.d = -1234567.8;',
  6288. '$mod.d = -1.23456789E7;',
  6289. '$mod.d = 1E-12;',
  6290. '$mod.d = -1E-12;',
  6291. '$mod.d = 1.7E308;',
  6292. '$mod.d = -1.7E308;',
  6293. '$mod.d = -4503599627370496;',
  6294. '$mod.d = -4503599627370496;',
  6295. '$mod.d = -4503599627370496;',
  6296. '$mod.d = -4503599627370496;',
  6297. '$mod.d = 4503599627370495;',
  6298. '$mod.d = 0.0;',
  6299. '']));
  6300. end;
  6301. procedure TTestModule.TestInteger;
  6302. begin
  6303. StartProgram(false);
  6304. Add([
  6305. 'const',
  6306. ' MinInt = low(NativeInt);',
  6307. ' MaxInt = high(NativeInt);',
  6308. 'type',
  6309. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6310. 'const',
  6311. ' a = low(TMyInt)+High(TMyInt);',
  6312. 'var',
  6313. ' i: TMyInt;',
  6314. 'begin',
  6315. ' i:=-MinInt;',
  6316. ' i:=default(TMyInt);',
  6317. ' i:=low(i)+high(i);',
  6318. '']);
  6319. ConvertProgram;
  6320. CheckSource('TestIntegerRange',
  6321. LinesToStr([
  6322. 'this.MinInt = -4503599627370496;',
  6323. 'this.MaxInt = 4503599627370495;',
  6324. 'this.a = -4503599627370496 + 4503599627370495;',
  6325. 'this.i = 0;',
  6326. '']),
  6327. LinesToStr([
  6328. '$mod.i = - -4503599627370496;',
  6329. '$mod.i = -4503599627370496;',
  6330. '$mod.i = -4503599627370496 + 4503599627370495;',
  6331. '']));
  6332. end;
  6333. procedure TTestModule.TestIntegerRange;
  6334. begin
  6335. StartProgram(false);
  6336. Add([
  6337. 'const',
  6338. ' MinInt = -1;',
  6339. ' MaxInt = +1;',
  6340. 'type',
  6341. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6342. ' TInt2 = 1..3;',
  6343. 'const',
  6344. ' a = low(TMyInt)+High(TMyInt);',
  6345. ' b = low(TInt2)+High(TInt2);',
  6346. ' s1 = [1];',
  6347. ' s2 = [1,2];',
  6348. ' s3 = [1..3];',
  6349. ' s4 = [low(shortint)..high(shortint)];',
  6350. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6351. ' s6 = 1 in s2;',
  6352. 'var',
  6353. ' i: TMyInt;',
  6354. ' i2: TInt2;',
  6355. 'begin',
  6356. ' i:=i2;',
  6357. ' i:=default(TMyInt);',
  6358. ' if i=i2 then ;']);
  6359. ConvertProgram;
  6360. CheckSource('TestIntegerRange',
  6361. LinesToStr([
  6362. 'this.MinInt = -1;',
  6363. 'this.MaxInt = +1;',
  6364. 'this.a = -1 + 1;',
  6365. 'this.b = 1 + 3;',
  6366. 'this.s1 = rtl.createSet(1);',
  6367. 'this.s2 = rtl.createSet(1, 2);',
  6368. 'this.s3 = rtl.createSet(null, 1, 3);',
  6369. 'this.s4 = rtl.createSet(null, -128, 127);',
  6370. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6371. 'this.s6 = 1 in $mod.s2;',
  6372. 'this.i = 0;',
  6373. 'this.i2 = 0;',
  6374. '']),
  6375. LinesToStr([
  6376. '$mod.i = $mod.i2;',
  6377. '$mod.i = -1;',
  6378. 'if ($mod.i === $mod.i2) ;',
  6379. '']));
  6380. end;
  6381. procedure TTestModule.TestIntegerTypecasts;
  6382. begin
  6383. StartProgram(false);
  6384. Add([
  6385. 'var',
  6386. ' i: nativeint;',
  6387. ' b: byte;',
  6388. ' sh: shortint;',
  6389. ' w: word;',
  6390. ' sm: smallint;',
  6391. ' lw: longword;',
  6392. ' li: longint;',
  6393. 'begin',
  6394. ' b:=byte(i);',
  6395. ' sh:=shortint(i);',
  6396. ' w:=word(i);',
  6397. ' sm:=smallint(i);',
  6398. ' lw:=longword(i);',
  6399. ' li:=longint(i);',
  6400. '']);
  6401. ConvertProgram;
  6402. CheckSource('TestIntegerTypecasts',
  6403. LinesToStr([
  6404. 'this.i = 0;',
  6405. 'this.b = 0;',
  6406. 'this.sh = 0;',
  6407. 'this.w = 0;',
  6408. 'this.sm = 0;',
  6409. 'this.lw = 0;',
  6410. 'this.li = 0;',
  6411. '']),
  6412. LinesToStr([
  6413. '$mod.b = $mod.i & 255;',
  6414. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6415. '$mod.w = $mod.i & 65535;',
  6416. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6417. '$mod.lw = $mod.i >>> 0;',
  6418. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6419. '']));
  6420. end;
  6421. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6422. begin
  6423. StartProgram(false);
  6424. Add([
  6425. 'var',
  6426. ' i,j: nativeint;',
  6427. 'begin',
  6428. ' i:=i shr 0;',
  6429. ' i:=i shr 1;',
  6430. ' i:=i shr 3;',
  6431. ' i:=i shr 54;',
  6432. ' i:=j shr i;',
  6433. '']);
  6434. ConvertProgram;
  6435. CheckResolverUnexpectedHints;
  6436. CheckSource('TestInteger_BitwiseShrNativeInt',
  6437. LinesToStr([
  6438. 'this.i = 0;',
  6439. 'this.j = 0;',
  6440. '']),
  6441. LinesToStr([
  6442. '$mod.i = $mod.i;',
  6443. '$mod.i = Math.floor($mod.i / 2);',
  6444. '$mod.i = Math.floor($mod.i / 8);',
  6445. '$mod.i = 0;',
  6446. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6447. '']));
  6448. end;
  6449. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6450. begin
  6451. StartProgram(false);
  6452. Add([
  6453. 'var',
  6454. ' i: nativeint;',
  6455. 'begin',
  6456. ' i:=i shl 0;',
  6457. ' i:=i shl 54;',
  6458. ' i:=123456789012 shl 1;',
  6459. ' i:=i shl 1;',
  6460. '']);
  6461. ConvertProgram;
  6462. CheckResolverUnexpectedHints;
  6463. CheckSource('TestInteger_BitwiseShrNativeInt',
  6464. LinesToStr([
  6465. 'this.i = 0;',
  6466. '']),
  6467. LinesToStr([
  6468. '$mod.i = $mod.i;',
  6469. '$mod.i = 0;',
  6470. '$mod.i = 246913578024;',
  6471. '$mod.i = rtl.shl($mod.i, 1);',
  6472. '']));
  6473. end;
  6474. procedure TTestModule.TestCurrency;
  6475. begin
  6476. StartProgram(false);
  6477. Add([
  6478. 'type',
  6479. ' TCoin = currency;',
  6480. 'const',
  6481. ' a = TCoin(2.7);',
  6482. ' b = a + TCoin(1.7);',
  6483. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6484. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6485. 'var',
  6486. ' c: TCoin = b;',
  6487. ' i: nativeint;',
  6488. ' d: double;',
  6489. ' j: jsvalue;',
  6490. 'function DoIt(c: currency): currency; begin end;',
  6491. 'function GetIt(d: double): double; begin end;',
  6492. 'procedure Write(v: jsvalue); begin end;',
  6493. 'begin',
  6494. ' c:=1.0;',
  6495. ' c:=0.1;',
  6496. ' c:=1.0/3.0;',
  6497. ' c:=1/3;',
  6498. ' c:=a;',
  6499. ' d:=c;',
  6500. ' c:=d;',
  6501. ' c:=currency(c);',
  6502. ' c:=currency(d);',
  6503. ' d:=double(c);',
  6504. ' c:=i;',
  6505. ' c:=currency(i);',
  6506. //' i:=c;', not allowed
  6507. ' i:=nativeint(c);',
  6508. ' c:=c+a;',
  6509. ' c:=-c-a;',
  6510. ' c:=d+c;',
  6511. ' c:=c+d;',
  6512. ' c:=d-c;',
  6513. ' c:=c-d;',
  6514. ' c:=c*a;',
  6515. ' c:=a*c;',
  6516. ' c:=d*c;',
  6517. ' c:=c*d;',
  6518. ' c:=c/a;',
  6519. ' c:=a/c;',
  6520. ' c:=d/c;',
  6521. ' c:=c/d;',
  6522. ' c:=c**a;',
  6523. ' c:=a**c;',
  6524. ' c:=d**c;',
  6525. ' c:=c**d;',
  6526. ' if c=c then ;',
  6527. ' if c=a then ;',
  6528. ' if a=c then ;',
  6529. ' if d=c then ;',
  6530. ' if c=d then ;',
  6531. ' c:=DoIt(c);',
  6532. ' c:=DoIt(i);',
  6533. ' c:=DoIt(d);',
  6534. ' c:=GetIt(c);',
  6535. ' j:=c;',
  6536. ' Write(c);',
  6537. ' c:=default(currency);',
  6538. ' j:=str(c);',
  6539. ' j:=str(c:0:3);',
  6540. '']);
  6541. ConvertProgram;
  6542. CheckSource('TestCurrency',
  6543. LinesToStr([
  6544. 'this.a = 27000;',
  6545. 'this.b = $mod.a + 17000;',
  6546. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6547. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6548. 'this.c = $mod.b;',
  6549. 'this.i = 0;',
  6550. 'this.d = 0.0;',
  6551. 'this.j = undefined;',
  6552. 'this.DoIt = function (c) {',
  6553. ' var Result = 0;',
  6554. ' return Result;',
  6555. '};',
  6556. 'this.GetIt = function (d) {',
  6557. ' var Result = 0.0;',
  6558. ' return Result;',
  6559. '};',
  6560. 'this.Write = function (v) {',
  6561. '};',
  6562. '']),
  6563. LinesToStr([
  6564. '$mod.c = 10000;',
  6565. '$mod.c = 1000;',
  6566. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6567. '$mod.c = Math.floor((1 / 3) * 10000);',
  6568. '$mod.c = $mod.a;',
  6569. '$mod.d = $mod.c / 10000;',
  6570. '$mod.c = Math.floor($mod.d * 10000);',
  6571. '$mod.c = $mod.c;',
  6572. '$mod.c = $mod.d * 10000;',
  6573. '$mod.d = $mod.c / 10000;',
  6574. '$mod.c = $mod.i * 10000;',
  6575. '$mod.c = $mod.i * 10000;',
  6576. '$mod.i = Math.floor($mod.c / 10000);',
  6577. '$mod.c = $mod.c + $mod.a;',
  6578. '$mod.c = -$mod.c - $mod.a;',
  6579. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6580. '$mod.c = $mod.c + ($mod.d * 10000);',
  6581. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6582. '$mod.c = $mod.c - ($mod.d * 10000);',
  6583. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6584. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6585. '$mod.c = $mod.d * $mod.c;',
  6586. '$mod.c = $mod.c * $mod.d;',
  6587. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6588. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6589. '$mod.c = Math.floor($mod.d / $mod.c);',
  6590. '$mod.c = Math.floor($mod.c / $mod.d);',
  6591. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6592. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6593. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6594. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6595. 'if ($mod.c === $mod.c) ;',
  6596. 'if ($mod.c === $mod.a) ;',
  6597. 'if ($mod.a === $mod.c) ;',
  6598. 'if (($mod.d * 10000) === $mod.c) ;',
  6599. 'if ($mod.c === ($mod.d * 10000)) ;',
  6600. '$mod.c = $mod.DoIt($mod.c);',
  6601. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6602. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6603. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6604. '$mod.j = $mod.c / 10000;',
  6605. '$mod.Write($mod.c / 10000);',
  6606. '$mod.c = 0;',
  6607. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6608. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6609. '']));
  6610. end;
  6611. procedure TTestModule.TestForBoolDo;
  6612. begin
  6613. StartProgram(false);
  6614. Add([
  6615. 'var b: boolean;',
  6616. 'begin',
  6617. ' for b:=false to true do ;',
  6618. ' for b:=b downto false do ;',
  6619. ' for b in boolean do ;',
  6620. '']);
  6621. ConvertProgram;
  6622. CheckSource('TestForBoolDo',
  6623. LinesToStr([ // statements
  6624. 'this.b = false;']),
  6625. LinesToStr([ // this.$main
  6626. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6627. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6628. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6629. '']));
  6630. end;
  6631. procedure TTestModule.TestForIntDo;
  6632. begin
  6633. StartProgram(false);
  6634. Add([
  6635. 'var i: longint;',
  6636. 'begin',
  6637. ' for i:=3 to 5 do ;',
  6638. ' for i:=i downto 2 do ;',
  6639. ' for i in byte do ;',
  6640. '']);
  6641. ConvertProgram;
  6642. CheckSource('TestForIntDo',
  6643. LinesToStr([ // statements
  6644. 'this.i = 0;']),
  6645. LinesToStr([ // this.$main
  6646. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6647. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6648. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6649. '']));
  6650. end;
  6651. procedure TTestModule.TestForIntInDo;
  6652. begin
  6653. StartProgram(false);
  6654. Add([
  6655. 'type',
  6656. ' TSetOfInt = set of byte;',
  6657. ' TIntRg = 3..7;',
  6658. ' TSetOfIntRg = set of TIntRg;',
  6659. 'var',
  6660. ' i,i2: longint;',
  6661. ' a1: array of byte;',
  6662. ' a2: array[1..3] of byte;',
  6663. ' soi: TSetOfInt;',
  6664. ' soir: TSetOfIntRg;',
  6665. ' ir: TIntRg;',
  6666. 'begin',
  6667. ' for i in byte do ;',
  6668. ' for i in a1 do ;',
  6669. ' for i in a2 do ;',
  6670. ' for i in [11..13] do ;',
  6671. ' for i in TSetOfInt do ;',
  6672. ' for i in TIntRg do ;',
  6673. ' for i in soi do i2:=i;',
  6674. ' for i in TSetOfIntRg do ;',
  6675. ' for i in soir do ;',
  6676. ' for ir in TIntRg do ;',
  6677. ' for ir in TSetOfIntRg do ;',
  6678. ' for ir in soir do ;',
  6679. '']);
  6680. ConvertProgram;
  6681. CheckSource('TestForIntInDo',
  6682. LinesToStr([ // statements
  6683. 'this.i = 0;',
  6684. 'this.i2 = 0;',
  6685. 'this.a1 = [];',
  6686. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6687. 'this.soi = {};',
  6688. 'this.soir = {};',
  6689. 'this.ir = 0;',
  6690. '']),
  6691. LinesToStr([ // this.$main
  6692. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6693. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6694. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6695. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6696. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6697. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6698. 'for (var $l11 in $mod.soi) {',
  6699. ' $mod.i = +$l11;',
  6700. ' $mod.i2 = $mod.i;',
  6701. '};',
  6702. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6703. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6704. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6705. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6706. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6707. '']));
  6708. end;
  6709. procedure TTestModule.TestCharConst;
  6710. begin
  6711. StartProgram(false);
  6712. Add([
  6713. 'const',
  6714. ' a = #$00F3;',
  6715. ' c: char = ''1'';',
  6716. 'begin',
  6717. ' c:=#0;',
  6718. ' c:=#1;',
  6719. ' c:=#9;',
  6720. ' c:=#10;',
  6721. ' c:=#13;',
  6722. ' c:=#31;',
  6723. ' c:=#32;',
  6724. ' c:=#$A;',
  6725. ' c:=#$0A;',
  6726. ' c:=#$b;',
  6727. ' c:=#$0b;',
  6728. ' c:=^A;',
  6729. ' c:=''"'';',
  6730. ' c:=default(char);',
  6731. ' c:=#$00E4;', // ä
  6732. ' c:=''ä'';',
  6733. ' c:=#$E4;', // ä
  6734. ' c:=#$D800;', // invalid UTF-16
  6735. ' c:=#$DFFF;', // invalid UTF-16
  6736. ' c:=#$FFFF;', // last UCS-2
  6737. ' c:=high(c);', // last UCS-2
  6738. '']);
  6739. ConvertProgram;
  6740. CheckSource('TestCharConst',
  6741. LinesToStr([
  6742. 'this.a="ó";',
  6743. 'this.c="1";'
  6744. ]),
  6745. LinesToStr([
  6746. '$mod.c="\x00";',
  6747. '$mod.c="\x01";',
  6748. '$mod.c="\t";',
  6749. '$mod.c="\n";',
  6750. '$mod.c="\r";',
  6751. '$mod.c="\x1F";',
  6752. '$mod.c=" ";',
  6753. '$mod.c="\n";',
  6754. '$mod.c="\n";',
  6755. '$mod.c="\x0B";',
  6756. '$mod.c="\x0B";',
  6757. '$mod.c="\x01";',
  6758. '$mod.c=''"'';',
  6759. '$mod.c="\x00";',
  6760. '$mod.c = "ä";',
  6761. '$mod.c = "ä";',
  6762. '$mod.c = "ä";',
  6763. '$mod.c="\uD800";',
  6764. '$mod.c="\uDFFF";',
  6765. '$mod.c="\uFFFF";',
  6766. '$mod.c="\uFFFF";',
  6767. '']));
  6768. end;
  6769. procedure TTestModule.TestChar_Compare;
  6770. begin
  6771. StartProgram(false);
  6772. Add('var');
  6773. Add(' c: char;');
  6774. Add(' b: boolean;');
  6775. Add('begin');
  6776. Add(' b:=c=''1'';');
  6777. Add(' b:=''2''=c;');
  6778. Add(' b:=''3''=''4'';');
  6779. Add(' b:=c<>''5'';');
  6780. Add(' b:=''6''<>c;');
  6781. Add(' b:=c>''7'';');
  6782. Add(' b:=''8''>c;');
  6783. Add(' b:=c>=''9'';');
  6784. Add(' b:=''A''>=c;');
  6785. Add(' b:=c<''B'';');
  6786. Add(' b:=''C''<c;');
  6787. Add(' b:=c<=''D'';');
  6788. Add(' b:=''E''<=c;');
  6789. ConvertProgram;
  6790. CheckSource('TestChar_Compare',
  6791. LinesToStr([
  6792. 'this.c="";',
  6793. 'this.b = false;'
  6794. ]),
  6795. LinesToStr([
  6796. '$mod.b = $mod.c === "1";',
  6797. '$mod.b = "2" === $mod.c;',
  6798. '$mod.b = "3" === "4";',
  6799. '$mod.b = $mod.c !== "5";',
  6800. '$mod.b = "6" !== $mod.c;',
  6801. '$mod.b = $mod.c > "7";',
  6802. '$mod.b = "8" > $mod.c;',
  6803. '$mod.b = $mod.c >= "9";',
  6804. '$mod.b = "A" >= $mod.c;',
  6805. '$mod.b = $mod.c < "B";',
  6806. '$mod.b = "C" < $mod.c;',
  6807. '$mod.b = $mod.c <= "D";',
  6808. '$mod.b = "E" <= $mod.c;',
  6809. '']));
  6810. end;
  6811. procedure TTestModule.TestChar_BuiltInProcs;
  6812. begin
  6813. StartProgram(false);
  6814. Add([
  6815. 'var',
  6816. ' c: char;',
  6817. ' i: longint;',
  6818. ' s: string;',
  6819. 'begin',
  6820. ' i:=ord(c);',
  6821. ' i:=ord(s[i]);',
  6822. ' c:=chr(i);',
  6823. ' c:=pred(c);',
  6824. ' c:=succ(c);',
  6825. ' c:=low(c);',
  6826. ' c:=high(c);',
  6827. ' i:=byte(c);',
  6828. ' i:=word(c);',
  6829. ' i:=longint(c);',
  6830. '']);
  6831. ConvertProgram;
  6832. CheckSource('TestChar_BuiltInProcs',
  6833. LinesToStr([
  6834. 'this.c = "";',
  6835. 'this.i = 0;',
  6836. 'this.s = "";'
  6837. ]),
  6838. LinesToStr([
  6839. '$mod.i = $mod.c.charCodeAt();',
  6840. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6841. '$mod.c = String.fromCharCode($mod.i);',
  6842. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6843. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6844. '$mod.c = "\x00";',
  6845. '$mod.c = "\uFFFF";',
  6846. '$mod.i = $mod.c.charCodeAt() & 255;',
  6847. '$mod.i = $mod.c.charCodeAt();',
  6848. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  6849. '']));
  6850. end;
  6851. procedure TTestModule.TestStringConst;
  6852. begin
  6853. StartProgram(false);
  6854. Add([
  6855. '{$H+}',
  6856. 'const',
  6857. ' a = #$00F3#$017C;', // first <256, then >=256
  6858. ' b = string(''a'');',
  6859. ' c = string(''ä'');',
  6860. ' d = UnicodeString(''b'');',
  6861. ' e = UnicodeString(''ö'');',
  6862. 'var',
  6863. ' s: string = ''abc'';',
  6864. 'begin',
  6865. ' s:='''';',
  6866. ' s:=#13#10;',
  6867. ' s:=#9''foo'';',
  6868. ' s:=#$A9;',
  6869. ' s:=''foo''#13''bar'';',
  6870. ' s:=''"'';',
  6871. ' s:=''"''''"'';',
  6872. ' s:=#$20AC;', // euro
  6873. ' s:=#$10437;', // outside BMP
  6874. ' s:=default(string);',
  6875. ' s:=concat(s);',
  6876. ' s:=concat(s,''a'',s)',
  6877. '']);
  6878. ConvertProgram;
  6879. CheckSource('TestStringConst',
  6880. LinesToStr([
  6881. 'this.a = "óż";',
  6882. 'this.b = "a";',
  6883. 'this.c = "ä";',
  6884. 'this.d = "b";',
  6885. 'this.e = "ö";',
  6886. 'this.s="abc";',
  6887. '']),
  6888. LinesToStr([
  6889. '$mod.s="";',
  6890. '$mod.s="\r\n";',
  6891. '$mod.s="\tfoo";',
  6892. '$mod.s="©";',
  6893. '$mod.s="foo\rbar";',
  6894. '$mod.s=''"'';',
  6895. '$mod.s=''"\''"'';',
  6896. '$mod.s="€";',
  6897. '$mod.s="'#$F0#$90#$90#$B7'";',
  6898. '$mod.s="";',
  6899. '$mod.s = $mod.s;',
  6900. '$mod.s = $mod.s.concat("a", $mod.s);',
  6901. '']));
  6902. end;
  6903. procedure TTestModule.TestStringConstSurrogate;
  6904. begin
  6905. StartProgram(false);
  6906. Add([
  6907. 'var',
  6908. ' s: string;',
  6909. 'begin',
  6910. ' s:=''😊'';', // 1F60A
  6911. '']);
  6912. ConvertProgram;
  6913. CheckSource('TestStringConstSurrogate',
  6914. LinesToStr([
  6915. 'this.s="";'
  6916. ]),
  6917. LinesToStr([
  6918. '$mod.s="😊";'
  6919. ]));
  6920. end;
  6921. procedure TTestModule.TestString_Length;
  6922. begin
  6923. StartProgram(false);
  6924. Add('const c = ''foo'';');
  6925. Add('var');
  6926. Add(' s: string;');
  6927. Add(' i: longint;');
  6928. Add('begin');
  6929. Add(' i:=length(s);');
  6930. Add(' i:=length(s+s);');
  6931. Add(' i:=length(''abc'');');
  6932. Add(' i:=length(c);');
  6933. ConvertProgram;
  6934. CheckSource('TestString_Length',
  6935. LinesToStr([
  6936. 'this.c = "foo";',
  6937. 'this.s = "";',
  6938. 'this.i = 0;',
  6939. '']),
  6940. LinesToStr([
  6941. '$mod.i = $mod.s.length;',
  6942. '$mod.i = ($mod.s+$mod.s).length;',
  6943. '$mod.i = "abc".length;',
  6944. '$mod.i = $mod.c.length;',
  6945. '']));
  6946. end;
  6947. procedure TTestModule.TestString_Compare;
  6948. begin
  6949. StartProgram(false);
  6950. Add('var');
  6951. Add(' s, t: string;');
  6952. Add(' b: boolean;');
  6953. Add('begin');
  6954. Add(' b:=s=t;');
  6955. Add(' b:=s<>t;');
  6956. Add(' b:=s>t;');
  6957. Add(' b:=s>=t;');
  6958. Add(' b:=s<t;');
  6959. Add(' b:=s<=t;');
  6960. ConvertProgram;
  6961. CheckSource('TestString_Compare',
  6962. LinesToStr([ // statements
  6963. 'this.s = "";',
  6964. 'this.t = "";',
  6965. 'this.b =false;'
  6966. ]),
  6967. LinesToStr([ // this.$main
  6968. '$mod.b = $mod.s === $mod.t;',
  6969. '$mod.b = $mod.s !== $mod.t;',
  6970. '$mod.b = $mod.s > $mod.t;',
  6971. '$mod.b = $mod.s >= $mod.t;',
  6972. '$mod.b = $mod.s < $mod.t;',
  6973. '$mod.b = $mod.s <= $mod.t;',
  6974. '']));
  6975. end;
  6976. procedure TTestModule.TestString_SetLength;
  6977. begin
  6978. StartProgram(false);
  6979. Add([
  6980. 'procedure DoIt(var s: string);',
  6981. 'begin',
  6982. ' SetLength(s,2);',
  6983. 'end;',
  6984. 'var s: string;',
  6985. 'begin',
  6986. ' SetLength(s,3);',
  6987. '']);
  6988. ConvertProgram;
  6989. CheckSource('TestString_SetLength',
  6990. LinesToStr([ // statements
  6991. 'this.DoIt = function (s) {',
  6992. ' s.set(rtl.strSetLength(s.get(), 2));',
  6993. '};',
  6994. 'this.s = "";',
  6995. '']),
  6996. LinesToStr([ // this.$main
  6997. '$mod.s = rtl.strSetLength($mod.s, 3);'
  6998. ]));
  6999. end;
  7000. procedure TTestModule.TestString_CharAt;
  7001. begin
  7002. StartProgram(false);
  7003. Add([
  7004. 'var',
  7005. ' s: string;',
  7006. ' c: char;',
  7007. ' b: boolean;',
  7008. 'begin',
  7009. ' b:= s[1] = c;',
  7010. ' b:= c = s[1];',
  7011. ' b:= c <> s[1];',
  7012. ' b:= c > s[1];',
  7013. ' b:= c >= s[1];',
  7014. ' b:= c < s[2];',
  7015. ' b:= c <= s[1];',
  7016. ' s[1] := c;',
  7017. ' s[2+3] := c;']);
  7018. ConvertProgram;
  7019. CheckSource('TestString_CharAt',
  7020. LinesToStr([ // statements
  7021. 'this.s = "";',
  7022. 'this.c = "";',
  7023. 'this.b = false;'
  7024. ]),
  7025. LinesToStr([ // this.$main
  7026. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7027. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7028. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7029. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7030. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7031. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7032. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7033. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7034. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7035. '']));
  7036. end;
  7037. procedure TTestModule.TestStringHMinusFail;
  7038. begin
  7039. StartProgram(false);
  7040. Add([
  7041. '{$H-}',
  7042. 'var s: string;',
  7043. 'begin']);
  7044. ConvertProgram;
  7045. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7046. end;
  7047. procedure TTestModule.TestStr;
  7048. begin
  7049. StartProgram(false);
  7050. Add('var');
  7051. Add(' b: boolean;');
  7052. Add(' i: longint;');
  7053. Add(' d: double;');
  7054. Add(' s: string;');
  7055. Add('begin');
  7056. Add(' str(b,s);');
  7057. Add(' str(i,s);');
  7058. Add(' str(d,s);');
  7059. Add(' str(i:3,s);');
  7060. Add(' str(d:3:2,s);');
  7061. Add(' Str(12.456:12:1,s);');
  7062. Add(' Str(12.456:12,s);');
  7063. Add(' s:=str(b);');
  7064. Add(' s:=str(i);');
  7065. Add(' s:=str(d);');
  7066. Add(' s:=str(i,i);');
  7067. Add(' s:=str(i:3);');
  7068. Add(' s:=str(d:3:2);');
  7069. Add(' s:=str(i:4,i);');
  7070. Add(' s:=str(i,i:5);');
  7071. Add(' s:=str(i:4,i:5);');
  7072. Add(' s:=str(s,s);');
  7073. Add(' s:=str(s,''foo'');');
  7074. ConvertProgram;
  7075. CheckSource('TestStr',
  7076. LinesToStr([ // statements
  7077. 'this.b = false;',
  7078. 'this.i = 0;',
  7079. 'this.d = 0.0;',
  7080. 'this.s = "";',
  7081. '']),
  7082. LinesToStr([ // this.$main
  7083. '$mod.s = ""+$mod.b;',
  7084. '$mod.s = ""+$mod.i;',
  7085. '$mod.s = rtl.floatToStr($mod.d);',
  7086. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7087. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7088. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7089. '$mod.s = rtl.floatToStr(12.456,12);',
  7090. '$mod.s = ""+$mod.b;',
  7091. '$mod.s = ""+$mod.i;',
  7092. '$mod.s = rtl.floatToStr($mod.d);',
  7093. '$mod.s = ""+$mod.i+$mod.i;',
  7094. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7095. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7096. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7097. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7098. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7099. '$mod.s = $mod.s + $mod.s;',
  7100. '$mod.s = $mod.s + "foo";',
  7101. '']));
  7102. end;
  7103. procedure TTestModule.TestBaseType_AnsiStringFail;
  7104. begin
  7105. StartProgram(false);
  7106. Add('var s: AnsiString');
  7107. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7108. ConvertProgram;
  7109. end;
  7110. procedure TTestModule.TestBaseType_WideStringFail;
  7111. begin
  7112. StartProgram(false);
  7113. Add('var s: WideString');
  7114. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7115. ConvertProgram;
  7116. end;
  7117. procedure TTestModule.TestBaseType_ShortStringFail;
  7118. begin
  7119. StartProgram(false);
  7120. Add('var s: ShortString');
  7121. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7122. ConvertProgram;
  7123. end;
  7124. procedure TTestModule.TestBaseType_RawByteStringFail;
  7125. begin
  7126. StartProgram(false);
  7127. Add('var s: RawByteString');
  7128. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7129. ConvertProgram;
  7130. end;
  7131. procedure TTestModule.TestTypeShortstring_Fail;
  7132. begin
  7133. StartProgram(false);
  7134. Add('type t = string[12];');
  7135. Add('var s: t;');
  7136. Add('begin');
  7137. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7138. ConvertProgram;
  7139. end;
  7140. procedure TTestModule.TestCharSet_Custom;
  7141. begin
  7142. StartProgram(false);
  7143. Add([
  7144. 'type',
  7145. ' TCharRg = ''a''..''z'';',
  7146. ' TSetOfCharRg = set of TCharRg;',
  7147. ' TCharRg2 = ''m''..''p'';',
  7148. 'const',
  7149. ' crg: TCharRg = ''b'';',
  7150. 'var',
  7151. ' c: char;',
  7152. ' crg2: TCharRg2;',
  7153. ' s: TSetOfCharRg;',
  7154. 'begin',
  7155. ' c:=crg;',
  7156. ' crg:=c;',
  7157. ' crg2:=crg;',
  7158. ' if c=crg then ;',
  7159. ' if crg=c then ;',
  7160. ' if crg=crg2 then ;',
  7161. ' if c in s then ;',
  7162. ' if crg2 in s then ;',
  7163. ' c:=default(TCharRg);',
  7164. '']);
  7165. ConvertProgram;
  7166. CheckSource('TestCharSet_Custom',
  7167. LinesToStr([ // statements
  7168. 'this.crg = "b";',
  7169. 'this.c = "";',
  7170. 'this.crg2 = "m";',
  7171. 'this.s = {};',
  7172. '']),
  7173. LinesToStr([ // this.$main
  7174. '$mod.c = $mod.crg;',
  7175. '$mod.crg = $mod.c;',
  7176. '$mod.crg2 = $mod.crg;',
  7177. 'if ($mod.c === $mod.crg) ;',
  7178. 'if ($mod.crg === $mod.c) ;',
  7179. 'if ($mod.crg === $mod.crg2) ;',
  7180. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7181. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7182. '$mod.c = "a";',
  7183. '']));
  7184. end;
  7185. procedure TTestModule.TestForCharDo;
  7186. begin
  7187. StartProgram(false);
  7188. Add([
  7189. 'var c: char;',
  7190. 'begin',
  7191. ' for c:=''a'' to ''c'' do ;',
  7192. ' for c:=c downto ''a'' do ;',
  7193. ' for c:=''Б'' to ''Я'' do ;',
  7194. '']);
  7195. ConvertProgram;
  7196. CheckSource('TestForCharDo',
  7197. LinesToStr([ // statements
  7198. 'this.c = "";']),
  7199. LinesToStr([ // this.$main
  7200. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  7201. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  7202. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  7203. '']));
  7204. end;
  7205. procedure TTestModule.TestForCharInDo;
  7206. begin
  7207. StartProgram(false);
  7208. Add([
  7209. 'type',
  7210. ' TSetOfChar = set of char;',
  7211. ' TCharRg = ''a''..''z'';',
  7212. ' TSetOfCharRg = set of TCharRg;',
  7213. 'const Foo = ''foo'';',
  7214. 'var',
  7215. ' c,c2: char;',
  7216. ' s: string;',
  7217. ' a1: array of char;',
  7218. ' a2: array[1..3] of char;',
  7219. ' soc: TSetOfChar;',
  7220. ' socr: TSetOfCharRg;',
  7221. ' cr: TCharRg;',
  7222. 'begin',
  7223. ' for c in foo do ;',
  7224. ' for c in s do ;',
  7225. ' for c in char do ;',
  7226. ' for c in a1 do ;',
  7227. ' for c in a2 do ;',
  7228. ' for c in [''1''..''3''] do ;',
  7229. ' for c in TSetOfChar do ;',
  7230. ' for c in TCharRg do ;',
  7231. ' for c in soc do c2:=c;',
  7232. ' for c in TSetOfCharRg do ;',
  7233. ' for c in socr do ;',
  7234. ' for cr in TCharRg do ;',
  7235. ' for cr in TSetOfCharRg do ;',
  7236. ' for cr in socr do ;',
  7237. '']);
  7238. ConvertProgram;
  7239. CheckSource('TestForCharInDo',
  7240. LinesToStr([ // statements
  7241. 'this.Foo = "foo";',
  7242. 'this.c = "";',
  7243. 'this.c2 = "";',
  7244. 'this.s = "";',
  7245. 'this.a1 = [];',
  7246. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7247. 'this.soc = {};',
  7248. 'this.socr = {};',
  7249. 'this.cr = "a";',
  7250. '']),
  7251. LinesToStr([ // this.$main
  7252. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  7253. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  7254. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  7255. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  7256. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  7257. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  7258. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  7259. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  7260. 'for (var $l17 in $mod.soc) {',
  7261. ' $mod.c = String.fromCharCode($l17);',
  7262. ' $mod.c2 = $mod.c;',
  7263. '};',
  7264. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  7265. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  7266. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  7267. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  7268. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  7269. '']));
  7270. end;
  7271. procedure TTestModule.TestProcTwoArgs;
  7272. begin
  7273. StartProgram(false);
  7274. Add('procedure Test(a,b: longint);');
  7275. Add('begin');
  7276. Add('end;');
  7277. Add('begin');
  7278. ConvertProgram;
  7279. CheckSource('TestProcTwoArgs',
  7280. LinesToStr([ // statements
  7281. 'this.Test = function (a,b) {',
  7282. '};'
  7283. ]),
  7284. LinesToStr([ // this.$main
  7285. ''
  7286. ]));
  7287. end;
  7288. procedure TTestModule.TestProc_DefaultValue;
  7289. begin
  7290. StartProgram(false);
  7291. Add('procedure p1(i: longint = 1);');
  7292. Add('begin');
  7293. Add('end;');
  7294. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7295. Add('begin');
  7296. Add('end;');
  7297. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7298. Add('begin');
  7299. Add('end;');
  7300. Add('begin');
  7301. Add(' p1;');
  7302. Add(' p1();');
  7303. Add(' p1(11);');
  7304. Add(' p2;');
  7305. Add(' p2();');
  7306. Add(' p2(12);');
  7307. Add(' p2(13,''b'');');
  7308. Add(' p3();');
  7309. ConvertProgram;
  7310. CheckSource('TestProc_DefaultValue',
  7311. LinesToStr([ // statements
  7312. 'this.p1 = function (i) {',
  7313. '};',
  7314. 'this.p2 = function (i,c) {',
  7315. '};',
  7316. 'this.p3 = function (d,b,s) {',
  7317. '};'
  7318. ]),
  7319. LinesToStr([ // this.$main
  7320. ' $mod.p1(1);',
  7321. ' $mod.p1(1);',
  7322. ' $mod.p1(11);',
  7323. ' $mod.p2(1,"a");',
  7324. ' $mod.p2(1,"a");',
  7325. ' $mod.p2(12,"a");',
  7326. ' $mod.p2(13,"b");',
  7327. ' $mod.p3(1.0,false,"abc");'
  7328. ]));
  7329. end;
  7330. procedure TTestModule.TestFunctionInt;
  7331. begin
  7332. StartProgram(false);
  7333. Add('function MyTest(Bar: longint): longint;');
  7334. Add('begin');
  7335. Add(' Result:=2*bar');
  7336. Add('end;');
  7337. Add('begin');
  7338. ConvertProgram;
  7339. CheckSource('TestFunctionInt',
  7340. LinesToStr([ // statements
  7341. 'this.MyTest = function (Bar) {',
  7342. ' var Result = 0;',
  7343. ' Result = 2*Bar;',
  7344. ' return Result;',
  7345. '};'
  7346. ]),
  7347. LinesToStr([ // this.$main
  7348. ''
  7349. ]));
  7350. end;
  7351. procedure TTestModule.TestFunctionString;
  7352. begin
  7353. StartProgram(false);
  7354. Add('function Test(Bar: string): string;');
  7355. Add('begin');
  7356. Add(' Result:=bar+BAR');
  7357. Add('end;');
  7358. Add('begin');
  7359. ConvertProgram;
  7360. CheckSource('TestFunctionString',
  7361. LinesToStr([ // statements
  7362. 'this.Test = function (Bar) {',
  7363. ' var Result = "";',
  7364. ' Result = Bar+Bar;',
  7365. ' return Result;',
  7366. '};'
  7367. ]),
  7368. LinesToStr([ // this.$main
  7369. ''
  7370. ]));
  7371. end;
  7372. procedure TTestModule.TestIfThen;
  7373. begin
  7374. StartProgram(false);
  7375. Add([
  7376. 'var b: boolean;',
  7377. 'begin',
  7378. ' if b then ;',
  7379. ' if b then else ;']);
  7380. ConvertProgram;
  7381. CheckSource('TestIfThen',
  7382. LinesToStr([ // statements
  7383. 'this.b = false;',
  7384. '']),
  7385. LinesToStr([ // this.$main
  7386. 'if ($mod.b) ;',
  7387. 'if ($mod.b) ;',
  7388. '']));
  7389. end;
  7390. procedure TTestModule.TestForLoop;
  7391. begin
  7392. StartProgram(false);
  7393. Add('var');
  7394. Add(' vI, vJ, vN: longint;');
  7395. Add('begin');
  7396. Add(' VJ:=0;');
  7397. Add(' VN:=3;');
  7398. Add(' for VI:=1 to VN do');
  7399. Add(' begin');
  7400. Add(' VJ:=VJ+VI;');
  7401. Add(' end;');
  7402. ConvertProgram;
  7403. CheckSource('TestForLoop',
  7404. LinesToStr([ // statements
  7405. 'this.vI = 0;',
  7406. 'this.vJ = 0;',
  7407. 'this.vN = 0;'
  7408. ]),
  7409. LinesToStr([ // this.$main
  7410. ' $mod.vJ = 0;',
  7411. ' $mod.vN = 3;',
  7412. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  7413. ' $mod.vI = $l1;',
  7414. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7415. ' };',
  7416. '']));
  7417. end;
  7418. procedure TTestModule.TestForLoopInsideFunction;
  7419. begin
  7420. StartProgram(false);
  7421. Add('function SumNumbers(Count: longint): longint;');
  7422. Add('var');
  7423. Add(' vI, vJ: longint;');
  7424. Add('begin');
  7425. Add(' vj:=0;');
  7426. Add(' for vi:=1 to count do');
  7427. Add(' begin');
  7428. Add(' vj:=vj+vi;');
  7429. Add(' end;');
  7430. Add('end;');
  7431. Add('begin');
  7432. Add(' sumnumbers(3);');
  7433. ConvertProgram;
  7434. CheckSource('TestForLoopInsideFunction',
  7435. LinesToStr([ // statements
  7436. 'this.SumNumbers = function (Count) {',
  7437. ' var Result = 0;',
  7438. ' var vI = 0;',
  7439. ' var vJ = 0;',
  7440. ' vJ = 0;',
  7441. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7442. ' vI = $l1;',
  7443. ' vJ = vJ + vI;',
  7444. ' };',
  7445. ' return Result;',
  7446. '};'
  7447. ]),
  7448. LinesToStr([ // $mod.$main
  7449. ' $mod.SumNumbers(3);'
  7450. ]));
  7451. end;
  7452. procedure TTestModule.TestForLoop_ReadVarAfter;
  7453. begin
  7454. StartProgram(false);
  7455. Add('var');
  7456. Add(' vI: longint;');
  7457. Add('begin');
  7458. Add(' for vi:=1 to 2 do ;');
  7459. Add(' if vi=3 then ;');
  7460. ConvertProgram;
  7461. CheckSource('TestForLoop',
  7462. LinesToStr([ // statements
  7463. 'this.vI = 0;'
  7464. ]),
  7465. LinesToStr([ // this.$main
  7466. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7467. ' if ($mod.vI===3) ;'
  7468. ]));
  7469. end;
  7470. procedure TTestModule.TestForLoop_Nested;
  7471. begin
  7472. StartProgram(false);
  7473. Add('function SumNumbers(Count: longint): longint;');
  7474. Add('var');
  7475. Add(' vI, vJ, vK: longint;');
  7476. Add('begin');
  7477. Add(' VK:=0;');
  7478. Add(' for VI:=1 to count do');
  7479. Add(' begin');
  7480. Add(' for vj:=1 to vi do');
  7481. Add(' begin');
  7482. Add(' vk:=VK+VI;');
  7483. Add(' end;');
  7484. Add(' end;');
  7485. Add('end;');
  7486. Add('begin');
  7487. Add(' sumnumbers(3);');
  7488. ConvertProgram;
  7489. CheckSource('TestForLoopInFunction',
  7490. LinesToStr([ // statements
  7491. 'this.SumNumbers = function (Count) {',
  7492. ' var Result = 0;',
  7493. ' var vI = 0;',
  7494. ' var vJ = 0;',
  7495. ' var vK = 0;',
  7496. ' vK = 0;',
  7497. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7498. ' vI = $l1;',
  7499. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7500. ' vJ = $l3;',
  7501. ' vK = vK + vI;',
  7502. ' };',
  7503. ' };',
  7504. ' return Result;',
  7505. '};'
  7506. ]),
  7507. LinesToStr([ // $mod.$main
  7508. ' $mod.SumNumbers(3);'
  7509. ]));
  7510. end;
  7511. procedure TTestModule.TestRepeatUntil;
  7512. begin
  7513. StartProgram(false);
  7514. Add('var');
  7515. Add(' vI, vJ, vN: longint;');
  7516. Add('begin');
  7517. Add(' vn:=3;');
  7518. Add(' vj:=0;');
  7519. Add(' VI:=0;');
  7520. Add(' repeat');
  7521. Add(' VI:=vi+1;');
  7522. Add(' vj:=VJ+vI;');
  7523. Add(' until vi>=vn');
  7524. ConvertProgram;
  7525. CheckSource('TestRepeatUntil',
  7526. LinesToStr([ // statements
  7527. 'this.vI = 0;',
  7528. 'this.vJ = 0;',
  7529. 'this.vN = 0;'
  7530. ]),
  7531. LinesToStr([ // $mod.$main
  7532. ' $mod.vN = 3;',
  7533. ' $mod.vJ = 0;',
  7534. ' $mod.vI = 0;',
  7535. ' do{',
  7536. ' $mod.vI = $mod.vI + 1;',
  7537. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7538. ' }while(!($mod.vI>=$mod.vN));'
  7539. ]));
  7540. end;
  7541. procedure TTestModule.TestAsmBlock;
  7542. begin
  7543. StartProgram(false);
  7544. Add([
  7545. 'var',
  7546. ' vI: longint;',
  7547. 'begin',
  7548. ' vi:=1;',
  7549. ' asm',
  7550. ' if (vI===1) {',
  7551. ' vI=2;',
  7552. //' console.log(''end;'');', ToDo
  7553. ' }',
  7554. ' if (vI===2){ vI=3; }',
  7555. ' end;',
  7556. ' VI:=4;']);
  7557. ConvertProgram;
  7558. CheckSource('TestAsmBlock',
  7559. LinesToStr([ // statements
  7560. 'this.vI = 0;'
  7561. ]),
  7562. LinesToStr([ // $mod.$main
  7563. '$mod.vI = 1;',
  7564. 'if (vI===1) {',
  7565. ' vI=2;',
  7566. '}',
  7567. 'if (vI===2){ vI=3; }',
  7568. ';',
  7569. '$mod.vI = 4;'
  7570. ]));
  7571. end;
  7572. procedure TTestModule.TestAsmPas_Impl;
  7573. begin
  7574. StartUnit(false);
  7575. Add('interface');
  7576. Add('const cIntf: longint = 1;');
  7577. Add('var vIntf: longint;');
  7578. Add('implementation');
  7579. Add('const cImpl: longint = 2;');
  7580. Add('var vImpl: longint;');
  7581. Add('procedure DoIt;');
  7582. Add('const cLoc: longint = 3;');
  7583. Add('var vLoc: longint;');
  7584. Add('begin;');
  7585. Add(' asm');
  7586. //Add(' pas(vIntf)=pas(cIntf);');
  7587. //Add(' pas(vImpl)=pas(cImpl);');
  7588. //Add(' pas(vLoc)=pas(cLoc);');
  7589. Add(' end;');
  7590. Add('end;');
  7591. ConvertUnit;
  7592. CheckSource('TestAsmPas_Impl',
  7593. LinesToStr([
  7594. 'var $impl = $mod.$impl;',
  7595. 'this.cIntf = 1;',
  7596. 'this.vIntf = 0;',
  7597. '']),
  7598. '', // this.$init
  7599. LinesToStr([ // implementation
  7600. '$impl.cImpl = 2;',
  7601. '$impl.vImpl = 0;',
  7602. 'var cLoc = 3;',
  7603. '$impl.DoIt = function () {',
  7604. ' var vLoc = 0;',
  7605. '};',
  7606. '']) );
  7607. end;
  7608. procedure TTestModule.TestTryFinally;
  7609. begin
  7610. StartProgram(false);
  7611. Add('var i: longint;');
  7612. Add('begin');
  7613. Add(' try');
  7614. Add(' i:=0; i:=2 div i;');
  7615. Add(' finally');
  7616. Add(' i:=3');
  7617. Add(' end;');
  7618. ConvertProgram;
  7619. CheckSource('TestTryFinally',
  7620. LinesToStr([ // statements
  7621. 'this.i = 0;'
  7622. ]),
  7623. LinesToStr([ // $mod.$main
  7624. 'try {',
  7625. ' $mod.i = 0;',
  7626. ' $mod.i = Math.floor(2 / $mod.i);',
  7627. '} finally {',
  7628. ' $mod.i = 3;',
  7629. '};'
  7630. ]));
  7631. end;
  7632. procedure TTestModule.TestTryExcept;
  7633. begin
  7634. StartProgram(false);
  7635. Add('type');
  7636. Add(' TObject = class end;');
  7637. Add(' Exception = class Msg: string; end;');
  7638. Add(' EInvalidCast = class(Exception) end;');
  7639. Add('var vI: longint;');
  7640. Add('begin');
  7641. Add(' try');
  7642. Add(' vi:=1;');
  7643. Add(' except');
  7644. Add(' vi:=2');
  7645. Add(' end;');
  7646. Add(' try');
  7647. Add(' vi:=3;');
  7648. Add(' except');
  7649. Add(' raise;');
  7650. Add(' end;');
  7651. Add(' try');
  7652. Add(' VI:=4;');
  7653. Add(' except');
  7654. Add(' on einvalidcast do');
  7655. Add(' raise;');
  7656. Add(' on E: exception do');
  7657. Add(' if e.msg='''' then');
  7658. Add(' raise e;');
  7659. Add(' else');
  7660. Add(' vi:=5');
  7661. Add(' end;');
  7662. Add(' try');
  7663. Add(' VI:=6;');
  7664. Add(' except');
  7665. Add(' on einvalidcast do ;');
  7666. Add(' end;');
  7667. ConvertProgram;
  7668. CheckSource('TestTryExcept',
  7669. LinesToStr([ // statements
  7670. 'rtl.createClass($mod, "TObject", null, function () {',
  7671. ' this.$init = function () {',
  7672. ' };',
  7673. ' this.$final = function () {',
  7674. ' };',
  7675. '});',
  7676. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7677. ' this.$init = function () {',
  7678. ' $mod.TObject.$init.call(this);',
  7679. ' this.Msg = "";',
  7680. ' };',
  7681. '});',
  7682. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7683. '});',
  7684. 'this.vI = 0;'
  7685. ]),
  7686. LinesToStr([ // $mod.$main
  7687. 'try {',
  7688. ' $mod.vI = 1;',
  7689. '} catch ($e) {',
  7690. ' $mod.vI = 2;',
  7691. '};',
  7692. 'try {',
  7693. ' $mod.vI = 3;',
  7694. '} catch ($e) {',
  7695. ' throw $e;',
  7696. '};',
  7697. 'try {',
  7698. ' $mod.vI = 4;',
  7699. '} catch ($e) {',
  7700. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7701. ' throw $e',
  7702. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7703. ' var E = $e;',
  7704. ' if (E.Msg === "") throw E;',
  7705. ' } else {',
  7706. ' $mod.vI = 5;',
  7707. ' }',
  7708. '};',
  7709. 'try {',
  7710. ' $mod.vI = 6;',
  7711. '} catch ($e) {',
  7712. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7713. ' } else throw $e',
  7714. '};',
  7715. '']));
  7716. end;
  7717. procedure TTestModule.TestTryExcept_ReservedWords;
  7718. begin
  7719. StartProgram(false);
  7720. Add([
  7721. 'type',
  7722. ' TObject = class end;',
  7723. ' Exception = class',
  7724. ' Symbol: string;',
  7725. ' end;',
  7726. 'var &try: longint;',
  7727. 'begin',
  7728. ' try',
  7729. ' &try:=4;',
  7730. ' except',
  7731. ' on Error: exception do',
  7732. ' if errOR.symBol='''' then',
  7733. ' raise ERRor;',
  7734. ' end;',
  7735. '']);
  7736. ConvertProgram;
  7737. CheckSource('TestTryExcept_ReservedWords',
  7738. LinesToStr([ // statements
  7739. 'rtl.createClass($mod, "TObject", null, function () {',
  7740. ' this.$init = function () {',
  7741. ' };',
  7742. ' this.$final = function () {',
  7743. ' };',
  7744. '});',
  7745. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7746. ' this.$init = function () {',
  7747. ' $mod.TObject.$init.call(this);',
  7748. ' this.Symbol = "";',
  7749. ' };',
  7750. '});',
  7751. 'this.Try = 0;',
  7752. '']),
  7753. LinesToStr([ // $mod.$main
  7754. 'try {',
  7755. ' $mod.Try = 4;',
  7756. '} catch ($e) {',
  7757. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7758. ' var error = $e;',
  7759. ' if (error.Symbol === "") throw error;',
  7760. ' } else throw $e',
  7761. '};',
  7762. '']));
  7763. end;
  7764. procedure TTestModule.TestIfThenRaiseElse;
  7765. begin
  7766. StartProgram(false);
  7767. Add([
  7768. 'type',
  7769. ' TObject = class',
  7770. ' constructor Create;',
  7771. ' end;',
  7772. 'constructor TObject.Create;',
  7773. 'begin',
  7774. 'end;',
  7775. 'var b: boolean;',
  7776. 'begin',
  7777. ' if b then',
  7778. ' raise TObject.Create',
  7779. ' else',
  7780. ' b:=false;',
  7781. '']);
  7782. ConvertProgram;
  7783. CheckSource('TestIfThenRaiseElse',
  7784. LinesToStr([ // statements
  7785. 'rtl.createClass($mod, "TObject", null, function () {',
  7786. ' this.$init = function () {',
  7787. ' };',
  7788. ' this.$final = function () {',
  7789. ' };',
  7790. ' this.Create = function () {',
  7791. ' return this;',
  7792. ' };',
  7793. '});',
  7794. 'this.b = false;',
  7795. '']),
  7796. LinesToStr([ // $mod.$main
  7797. 'if ($mod.b) {',
  7798. ' throw $mod.TObject.$create("Create")}',
  7799. ' else $mod.b = false;',
  7800. '']));
  7801. end;
  7802. procedure TTestModule.TestCaseOf;
  7803. begin
  7804. StartProgram(false);
  7805. Add([
  7806. 'const e: longint; external name ''$e'';',
  7807. 'var vI: longint;',
  7808. 'begin',
  7809. ' case vi of',
  7810. ' 1: ;',
  7811. ' 2: vi:=3;',
  7812. ' e: ;',
  7813. ' else',
  7814. ' VI:=4',
  7815. ' end;']);
  7816. ConvertProgram;
  7817. CheckSource('TestCaseOf',
  7818. LinesToStr([ // statements
  7819. 'this.vI = 0;'
  7820. ]),
  7821. LinesToStr([ // $mod.$main
  7822. 'var $tmp1 = $mod.vI;',
  7823. 'if ($tmp1 === 1) {}',
  7824. 'else if ($tmp1 === 2) {',
  7825. ' $mod.vI = 3}',
  7826. ' else if ($tmp1 === $e) {}',
  7827. 'else {',
  7828. ' $mod.vI = 4;',
  7829. '};'
  7830. ]));
  7831. end;
  7832. procedure TTestModule.TestCaseOf_UseSwitch;
  7833. begin
  7834. StartProgram(false);
  7835. Converter.UseSwitchStatement:=true;
  7836. Add('var Vi: longint;');
  7837. Add('begin');
  7838. Add(' case vi of');
  7839. Add(' 1: ;');
  7840. Add(' 2: VI:=3;');
  7841. Add(' else');
  7842. Add(' vi:=4');
  7843. Add(' end;');
  7844. ConvertProgram;
  7845. CheckSource('TestCaseOf_UseSwitch',
  7846. LinesToStr([ // statements
  7847. 'this.Vi = 0;'
  7848. ]),
  7849. LinesToStr([ // $mod.$main
  7850. 'switch ($mod.Vi) {',
  7851. 'case 1:',
  7852. ' break;',
  7853. 'case 2:',
  7854. ' $mod.Vi = 3;',
  7855. ' break;',
  7856. 'default:',
  7857. ' $mod.Vi = 4;',
  7858. '};'
  7859. ]));
  7860. end;
  7861. procedure TTestModule.TestCaseOfNoElse;
  7862. begin
  7863. StartProgram(false);
  7864. Add('var Vi: longint;');
  7865. Add('begin');
  7866. Add(' case vi of');
  7867. Add(' 1: begin vi:=2; VI:=3; end;');
  7868. Add(' end;');
  7869. ConvertProgram;
  7870. CheckSource('TestCaseOfNoElse',
  7871. LinesToStr([ // statements
  7872. 'this.Vi = 0;'
  7873. ]),
  7874. LinesToStr([ // $mod.$main
  7875. 'var $tmp1 = $mod.Vi;',
  7876. 'if ($tmp1 === 1) {',
  7877. ' $mod.Vi = 2;',
  7878. ' $mod.Vi = 3;',
  7879. '};'
  7880. ]));
  7881. end;
  7882. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7883. begin
  7884. StartProgram(false);
  7885. Converter.UseSwitchStatement:=true;
  7886. Add('var vI: longint;');
  7887. Add('begin');
  7888. Add(' case vi of');
  7889. Add(' 1: begin VI:=2; vi:=3; end;');
  7890. Add(' end;');
  7891. ConvertProgram;
  7892. CheckSource('TestCaseOfNoElse_UseSwitch',
  7893. LinesToStr([ // statements
  7894. 'this.vI = 0;'
  7895. ]),
  7896. LinesToStr([ // $mod.$main
  7897. 'switch ($mod.vI) {',
  7898. 'case 1:',
  7899. ' $mod.vI = 2;',
  7900. ' $mod.vI = 3;',
  7901. ' break;',
  7902. '};'
  7903. ]));
  7904. end;
  7905. procedure TTestModule.TestCaseOfRange;
  7906. begin
  7907. StartProgram(false);
  7908. Add('var vI: longint;');
  7909. Add('begin');
  7910. Add(' case vi of');
  7911. Add(' 1..3: vi:=14;');
  7912. Add(' 4,5: vi:=16;');
  7913. Add(' 6..7,9..10: ;');
  7914. Add(' else ;');
  7915. Add(' end;');
  7916. ConvertProgram;
  7917. CheckSource('TestCaseOfRange',
  7918. LinesToStr([ // statements
  7919. 'this.vI = 0;'
  7920. ]),
  7921. LinesToStr([ // $mod.$main
  7922. 'var $tmp1 = $mod.vI;',
  7923. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  7924. ' $mod.vI = 14',
  7925. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  7926. ' $mod.vI = 16',
  7927. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  7928. ]));
  7929. end;
  7930. procedure TTestModule.TestCaseOfString;
  7931. begin
  7932. StartProgram(false);
  7933. Add([
  7934. 'var s,h: string;',
  7935. 'begin',
  7936. ' case s of',
  7937. ' ''foo'': s:=h;',
  7938. ' ''a''..''z'': h:=s;',
  7939. ' ''ў'', ''ё'': ;',
  7940. ' ''Б''..''Я'': ;',
  7941. ' end;',
  7942. '']);
  7943. ConvertProgram;
  7944. CheckSource('TestCaseOfString',
  7945. LinesToStr([ // statements
  7946. 'this.s = "";',
  7947. 'this.h = "";',
  7948. '']),
  7949. LinesToStr([ // $mod.$main
  7950. 'var $tmp1 = $mod.s;',
  7951. 'if ($tmp1 === "foo") {',
  7952. ' $mod.s = $mod.h}',
  7953. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7954. ' $mod.h = $mod.s}',
  7955. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7956. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7957. '']));
  7958. end;
  7959. procedure TTestModule.TestCaseOfChar;
  7960. begin
  7961. StartProgram(false);
  7962. Add([
  7963. 'var s,h: char;',
  7964. 'begin',
  7965. ' case s of',
  7966. ' ''a''..''z'': h:=s;',
  7967. ' ''ä'': ;',
  7968. ' ''ў'', ''ё'': ;',
  7969. ' ''Б''..''Я'': ;',
  7970. ' end;',
  7971. '']);
  7972. ConvertProgram;
  7973. CheckSource('TestCaseOfString',
  7974. LinesToStr([ // statements
  7975. 'this.s = "";',
  7976. 'this.h = "";',
  7977. '']),
  7978. LinesToStr([ // $mod.$main
  7979. 'var $tmp1 = $mod.s;',
  7980. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7981. ' $mod.h = $mod.s}',
  7982. ' else if ($tmp1 === "ä") {}',
  7983. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7984. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7985. '']));
  7986. end;
  7987. procedure TTestModule.TestCaseOfExternalClassConst;
  7988. begin
  7989. StartProgram(false);
  7990. Add([
  7991. '{$modeswitch externalclass}',
  7992. 'type',
  7993. ' TBird = class external name ''Bird''',
  7994. ' const e: longint;',
  7995. ' end;',
  7996. 'var vI: longint;',
  7997. 'begin',
  7998. ' case vi of',
  7999. ' 1: vi:=3;',
  8000. ' TBird.e: ;',
  8001. ' end;']);
  8002. ConvertProgram;
  8003. CheckSource('TestCaseOfExternalClassConst',
  8004. LinesToStr([ // statements
  8005. 'this.vI = 0;'
  8006. ]),
  8007. LinesToStr([ // $mod.$main
  8008. 'var $tmp1 = $mod.vI;',
  8009. 'if ($tmp1 === 1) {',
  8010. ' $mod.vI = 3}',
  8011. ' else if ($tmp1 === Bird.e) ;'
  8012. ]));
  8013. end;
  8014. procedure TTestModule.TestDebugger;
  8015. begin
  8016. StartProgram(false);
  8017. Add([
  8018. 'procedure DoIt;',
  8019. 'begin',
  8020. ' deBugger;',
  8021. ' DeBugger();',
  8022. 'end;',
  8023. 'begin',
  8024. ' Debugger;']);
  8025. ConvertProgram;
  8026. CheckSource('TestDebugger',
  8027. LinesToStr([ // statements
  8028. 'this.DoIt = function () {',
  8029. ' debugger;',
  8030. ' debugger;',
  8031. '};',
  8032. '']),
  8033. LinesToStr([ // $mod.$main
  8034. 'debugger;',
  8035. '']));
  8036. end;
  8037. procedure TTestModule.TestArray_Dynamic;
  8038. begin
  8039. StartProgram(false);
  8040. Add([
  8041. 'type',
  8042. ' TArrayInt = array of longint;',
  8043. 'var',
  8044. ' Arr: TArrayInt;',
  8045. ' i: longint;',
  8046. ' b: boolean;',
  8047. 'begin',
  8048. ' SetLength(arr,3);',
  8049. ' arr[0]:=4;',
  8050. ' arr[1]:=length(arr)+arr[0];',
  8051. ' arr[i]:=5;',
  8052. ' arr[arr[i]]:=arr[6];',
  8053. ' i:=low(arr);',
  8054. ' i:=high(arr);',
  8055. ' b:=Assigned(arr);',
  8056. ' Arr:=default(TArrayInt);']);
  8057. ConvertProgram;
  8058. CheckSource('TestArray_Dynamic',
  8059. LinesToStr([ // statements
  8060. 'this.Arr = [];',
  8061. 'this.i = 0;',
  8062. 'this.b = false;'
  8063. ]),
  8064. LinesToStr([ // $mod.$main
  8065. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8066. '$mod.Arr[0] = 4;',
  8067. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8068. '$mod.Arr[$mod.i] = 5;',
  8069. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8070. '$mod.i = 0;',
  8071. '$mod.i = rtl.length($mod.Arr) - 1;',
  8072. '$mod.b = rtl.length($mod.Arr) > 0;',
  8073. '$mod.Arr = [];',
  8074. '']));
  8075. end;
  8076. procedure TTestModule.TestArray_Dynamic_Nil;
  8077. begin
  8078. StartProgram(false);
  8079. Add('type');
  8080. Add(' TArrayInt = array of longint;');
  8081. Add('var');
  8082. Add(' Arr: TArrayInt;');
  8083. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8084. Add('begin');
  8085. Add(' arr:=nil;');
  8086. Add(' if arr=nil then;');
  8087. Add(' if nil=arr then;');
  8088. Add(' if arr<>nil then;');
  8089. Add(' if nil<>arr then;');
  8090. Add(' DoIt(nil,nil);');
  8091. ConvertProgram;
  8092. CheckSource('TestArray_Dynamic',
  8093. LinesToStr([ // statements
  8094. 'this.Arr = [];',
  8095. 'this.DoIt = function(i,j){',
  8096. '};'
  8097. ]),
  8098. LinesToStr([ // $mod.$main
  8099. '$mod.Arr = [];',
  8100. 'if (rtl.length($mod.Arr) === 0) ;',
  8101. 'if (rtl.length($mod.Arr) === 0) ;',
  8102. 'if (rtl.length($mod.Arr) > 0) ;',
  8103. 'if (rtl.length($mod.Arr) > 0) ;',
  8104. '$mod.DoIt([],[]);',
  8105. '']));
  8106. end;
  8107. procedure TTestModule.TestArray_DynMultiDimensional;
  8108. begin
  8109. StartProgram(false);
  8110. Add('type');
  8111. Add(' TArrayInt = array of longint;');
  8112. Add(' TArrayArrayInt = array of TArrayInt;');
  8113. Add('var');
  8114. Add(' Arr: TArrayInt;');
  8115. Add(' Arr2: TArrayArrayInt;');
  8116. Add(' i: longint;');
  8117. Add('begin');
  8118. Add(' arr2:=nil;');
  8119. Add(' if arr2=nil then;');
  8120. Add(' if nil=arr2 then;');
  8121. Add(' i:=low(arr2);');
  8122. Add(' i:=low(arr2[1]);');
  8123. Add(' i:=high(arr2);');
  8124. Add(' i:=high(arr2[2]);');
  8125. Add(' arr2[3]:=arr;');
  8126. Add(' arr2[4][5]:=i;');
  8127. Add(' i:=arr2[6][7];');
  8128. Add(' arr2[8,9]:=i;');
  8129. Add(' i:=arr2[10,11];');
  8130. Add(' SetLength(arr2,14);');
  8131. Add(' SetLength(arr2[15],16);');
  8132. ConvertProgram;
  8133. CheckSource('TestArray_Dynamic',
  8134. LinesToStr([ // statements
  8135. 'this.Arr = [];',
  8136. 'this.Arr2 = [];',
  8137. 'this.i = 0;'
  8138. ]),
  8139. LinesToStr([ // $mod.$main
  8140. '$mod.Arr2 = [];',
  8141. 'if (rtl.length($mod.Arr2) === 0) ;',
  8142. 'if (rtl.length($mod.Arr2) === 0) ;',
  8143. '$mod.i = 0;',
  8144. '$mod.i = 0;',
  8145. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8146. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8147. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8148. '$mod.Arr2[4][5] = $mod.i;',
  8149. '$mod.i = $mod.Arr2[6][7];',
  8150. '$mod.Arr2[8][9] = $mod.i;',
  8151. '$mod.i = $mod.Arr2[10][11];',
  8152. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8153. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8154. '']));
  8155. end;
  8156. procedure TTestModule.TestArray_StaticInt;
  8157. begin
  8158. StartProgram(false);
  8159. Add('type');
  8160. Add(' TArrayInt = array[2..4] of longint;');
  8161. Add('var');
  8162. Add(' Arr: TArrayInt;');
  8163. Add(' Arr2: TArrayInt = (5,6,7);');
  8164. Add(' i: longint;');
  8165. Add(' b: boolean;');
  8166. Add('begin');
  8167. Add(' arr[2]:=4;');
  8168. Add(' arr[3]:=arr[2]+arr[3];');
  8169. Add(' arr[i]:=5;');
  8170. Add(' arr[arr[i]]:=arr[high(arr)];');
  8171. Add(' i:=low(arr);');
  8172. Add(' i:=high(arr);');
  8173. Add(' b:=arr[2]=arr[3];');
  8174. Add(' arr:=default(TArrayInt);');
  8175. ConvertProgram;
  8176. CheckSource('TestArray_StaticInt',
  8177. LinesToStr([ // statements
  8178. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8179. 'this.Arr2 = [5, 6, 7];',
  8180. 'this.i = 0;',
  8181. 'this.b = false;'
  8182. ]),
  8183. LinesToStr([ // $mod.$main
  8184. '$mod.Arr[0] = 4;',
  8185. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8186. '$mod.Arr[$mod.i-2] = 5;',
  8187. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8188. '$mod.i = 2;',
  8189. '$mod.i = 4;',
  8190. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8191. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8192. '']));
  8193. end;
  8194. procedure TTestModule.TestArray_StaticBool;
  8195. begin
  8196. StartProgram(false);
  8197. Add('type');
  8198. Add(' TBools = array[boolean] of boolean;');
  8199. Add(' TBool2 = array[true..true] of boolean;');
  8200. Add('var');
  8201. Add(' Arr: TBools;');
  8202. Add(' Arr2: TBool2;');
  8203. Add(' Arr3: TBools = (true,false);');
  8204. Add(' b: boolean;');
  8205. Add('begin');
  8206. Add(' b:=low(arr);');
  8207. Add(' b:=high(arr);');
  8208. Add(' arr[true]:=false;');
  8209. Add(' arr[false]:=arr[b] or arr[true];');
  8210. Add(' arr[b]:=true;');
  8211. Add(' arr[arr[b]]:=arr[high(arr)];');
  8212. Add(' b:=arr[false]=arr[true];');
  8213. Add(' b:=low(arr2);');
  8214. Add(' b:=high(arr2);');
  8215. Add(' arr2[true]:=true;');
  8216. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8217. Add(' arr2[b]:=false;');
  8218. ConvertProgram;
  8219. CheckSource('TestArray_StaticBool',
  8220. LinesToStr([ // statements
  8221. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8222. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8223. 'this.Arr3 = [true, false];',
  8224. 'this.b = false;'
  8225. ]),
  8226. LinesToStr([ // $mod.$main
  8227. '$mod.b = false;',
  8228. '$mod.b = true;',
  8229. '$mod.Arr[1] = false;',
  8230. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8231. '$mod.Arr[+$mod.b] = true;',
  8232. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8233. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8234. '$mod.b = true;',
  8235. '$mod.b = true;',
  8236. '$mod.Arr2[0] = true;',
  8237. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8238. '$mod.Arr2[1-$mod.b] = false;',
  8239. '']));
  8240. end;
  8241. procedure TTestModule.TestArray_StaticChar;
  8242. begin
  8243. StartProgram(false);
  8244. Add([
  8245. 'type',
  8246. ' TChars = array[char] of char;',
  8247. ' TChars2 = array[''a''..''z''] of char;',
  8248. 'var',
  8249. ' Arr: TChars;',
  8250. ' Arr2: TChars2;',
  8251. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8252. ' Arr4: array[11..13] of char = ''pas'';',
  8253. ' Arr5: array[21..22] of char = ''äö'';',
  8254. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8255. ' c: char;',
  8256. ' b: boolean;',
  8257. 'begin',
  8258. ' c:=low(arr);',
  8259. ' c:=high(arr);',
  8260. ' arr[''B'']:=''a'';',
  8261. ' arr[''D'']:=arr[c];',
  8262. ' arr[c]:=arr[''d''];',
  8263. ' arr[arr[c]]:=arr[high(arr)];',
  8264. ' b:=arr[low(arr)]=arr[''e''];',
  8265. ' c:=low(arr2);',
  8266. ' c:=high(arr2);',
  8267. ' arr2[''b'']:=''f'';',
  8268. ' arr2[''a'']:=arr2[c];',
  8269. ' arr2[c]:=arr2[''g''];']);
  8270. ConvertProgram;
  8271. CheckSource('TestArray_StaticChar',
  8272. LinesToStr([ // statements
  8273. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8274. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8275. 'this.Arr3 = ["p", "a", "s"];',
  8276. 'this.Arr4 = ["p", "a", "s"];',
  8277. 'this.Arr5 = ["ä", "ö"];',
  8278. 'this.Arr6 = ["ä", "ö"];',
  8279. 'this.c = "";',
  8280. 'this.b = false;',
  8281. '']),
  8282. LinesToStr([ // $mod.$main
  8283. '$mod.c = "\x00";',
  8284. '$mod.c = "\uFFFF";',
  8285. '$mod.Arr[66] = "a";',
  8286. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8287. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8288. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8289. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8290. '$mod.c = "a";',
  8291. '$mod.c = "z";',
  8292. '$mod.Arr2[1] = "f";',
  8293. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8294. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8295. '']));
  8296. end;
  8297. procedure TTestModule.TestArray_StaticMultiDim;
  8298. begin
  8299. StartProgram(false);
  8300. Add([
  8301. 'type',
  8302. ' TArrayInt = array[1..3] of longint;',
  8303. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8304. 'var',
  8305. ' Arr: TArrayInt;',
  8306. ' Arr2: TArrayArrayInt;',
  8307. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8308. ' i: longint;',
  8309. 'begin',
  8310. ' i:=low(arr);',
  8311. ' i:=low(arr2);',
  8312. ' i:=low(arr2[5]);',
  8313. ' i:=high(arr);',
  8314. ' i:=high(arr2);',
  8315. ' i:=high(arr2[6]);',
  8316. ' arr2[5]:=arr;',
  8317. ' arr2[6][2]:=i;',
  8318. ' i:=arr2[6][3];',
  8319. ' arr2[6,3]:=i;',
  8320. ' i:=arr2[5,2];',
  8321. ' arr2:=arr2;',// clone multi dim static array
  8322. //' arr3:=arr3;',// clone anonymous multi dim static array
  8323. '']);
  8324. ConvertProgram;
  8325. CheckSource('TestArray_StaticMultiDim',
  8326. LinesToStr([ // statements
  8327. 'this.TArrayArrayInt$clone = function (a) {',
  8328. ' var r = [];',
  8329. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8330. ' return r;',
  8331. '};',
  8332. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8333. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8334. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8335. 'this.i = 0;'
  8336. ]),
  8337. LinesToStr([ // $mod.$main
  8338. '$mod.i = 1;',
  8339. '$mod.i = 5;',
  8340. '$mod.i = 1;',
  8341. '$mod.i = 3;',
  8342. '$mod.i = 6;',
  8343. '$mod.i = 3;',
  8344. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8345. '$mod.Arr2[1][1] = $mod.i;',
  8346. '$mod.i = $mod.Arr2[1][2];',
  8347. '$mod.Arr2[1][2] = $mod.i;',
  8348. '$mod.i = $mod.Arr2[0][1];',
  8349. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8350. '']));
  8351. end;
  8352. procedure TTestModule.TestArray_StaticInFunction;
  8353. begin
  8354. StartProgram(false);
  8355. Add([
  8356. 'const TArrayInt = 3;',
  8357. 'const TArrayArrayInt = 4;',
  8358. 'procedure DoIt;',
  8359. 'type',
  8360. ' TArrayInt = array[1..3] of longint;',
  8361. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8362. 'var',
  8363. ' Arr: TArrayInt;',
  8364. ' Arr2: TArrayArrayInt;',
  8365. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8366. ' i: longint;',
  8367. 'begin',
  8368. ' arr2[5]:=arr;',
  8369. ' arr2:=arr2;',// clone multi dim static array
  8370. 'end;',
  8371. 'begin',
  8372. '']);
  8373. ConvertProgram;
  8374. CheckSource('TestArray_StaticInFunction',
  8375. LinesToStr([ // statements
  8376. 'this.TArrayInt = 3;',
  8377. 'this.TArrayArrayInt = 4;',
  8378. 'var TArrayArrayInt$1$clone = function (a) {',
  8379. ' var r = [];',
  8380. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8381. ' return r;',
  8382. '};',
  8383. 'this.DoIt = function () {',
  8384. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8385. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8386. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8387. ' var i = 0;',
  8388. ' Arr2[0] = Arr.slice(0);',
  8389. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8390. '};',
  8391. '']),
  8392. LinesToStr([ // $mod.$main
  8393. '']));
  8394. end;
  8395. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  8396. begin
  8397. StartProgram(false);
  8398. Add([
  8399. 'type',
  8400. ' TArrayInt = array[1..3,1..2] of longint;',
  8401. 'var',
  8402. ' a,b: TArrayInt;',
  8403. 'begin',
  8404. ' if a=b then ;',
  8405. '']);
  8406. SetExpectedPasResolverError('compare static array is not supported',
  8407. nXIsNotSupported);
  8408. ConvertProgram;
  8409. end;
  8410. procedure TTestModule.TestArrayOfRecord;
  8411. begin
  8412. StartProgram(false);
  8413. Add([
  8414. 'type',
  8415. ' TRec = record',
  8416. ' Int: longint;',
  8417. ' end;',
  8418. ' TArrayRec = array of TRec;',
  8419. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8420. 'begin',
  8421. 'end;',
  8422. 'var',
  8423. ' Arr: TArrayRec;',
  8424. ' r: TRec;',
  8425. ' i: longint;',
  8426. 'begin',
  8427. ' SetLength(arr,3);',
  8428. ' arr[0].int:=4;',
  8429. ' arr[1].int:=length(arr)+arr[2].int;',
  8430. ' arr[arr[i].int].int:=arr[5].int;',
  8431. ' arr[7]:=r;',
  8432. ' r:=arr[8];',
  8433. ' i:=low(arr);',
  8434. ' i:=high(arr);',
  8435. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8436. ConvertProgram;
  8437. CheckSource('TestArrayOfRecord',
  8438. LinesToStr([ // statements
  8439. 'rtl.recNewT($mod, "TRec", function () {',
  8440. ' this.Int = 0;',
  8441. ' this.$eq = function (b) {',
  8442. ' return this.Int === b.Int;',
  8443. ' };',
  8444. ' this.$assign = function (s) {',
  8445. ' this.Int = s.Int;',
  8446. ' return this;',
  8447. ' };',
  8448. '});',
  8449. 'this.DoIt = function (vd, vc, vv) {',
  8450. '};',
  8451. 'this.Arr = [];',
  8452. 'this.r = $mod.TRec.$new();',
  8453. 'this.i = 0;'
  8454. ]),
  8455. LinesToStr([ // $mod.$main
  8456. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8457. '$mod.Arr[0].Int = 4;',
  8458. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8459. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8460. '$mod.Arr[7].$assign($mod.r);',
  8461. '$mod.r.$assign($mod.Arr[8]);',
  8462. '$mod.i = 0;',
  8463. '$mod.i = rtl.length($mod.Arr)-1;',
  8464. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8465. '']));
  8466. end;
  8467. procedure TTestModule.TestArray_StaticRecord;
  8468. begin
  8469. StartProgram(false);
  8470. Add([
  8471. 'type',
  8472. ' TRec = record',
  8473. ' Int: longint;',
  8474. ' end;',
  8475. ' TArrayRec = array[1..2] of TRec;',
  8476. 'var',
  8477. ' Arr: TArrayRec;',
  8478. 'begin',
  8479. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8480. '']);
  8481. ConvertProgram;
  8482. CheckSource('TestArray_StaticRecord',
  8483. LinesToStr([ // statements
  8484. 'rtl.recNewT($mod, "TRec", function () {',
  8485. ' this.Int = 0;',
  8486. ' this.$eq = function (b) {',
  8487. ' return this.Int === b.Int;',
  8488. ' };',
  8489. ' this.$assign = function (s) {',
  8490. ' this.Int = s.Int;',
  8491. ' return this;',
  8492. ' };',
  8493. '});',
  8494. 'this.TArrayRec$clone = function (a) {',
  8495. ' var r = [];',
  8496. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8497. ' return r;',
  8498. '};',
  8499. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8500. '']),
  8501. LinesToStr([ // $mod.$main
  8502. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8503. end;
  8504. procedure TTestModule.TestArrayOfSet;
  8505. begin
  8506. StartProgram(false);
  8507. Add([
  8508. 'type',
  8509. ' TFlag = (big,small);',
  8510. ' TSetOfFlag = set of tflag;',
  8511. ' TArrayFlag = array of TSetOfFlag;',
  8512. 'procedure DoIt(const a: Tarrayflag);',
  8513. 'begin',
  8514. 'end;',
  8515. 'var',
  8516. ' f: TFlag;',
  8517. ' s: TSetOfFlag;',
  8518. ' Arr: TArrayFlag;',
  8519. ' i: longint;',
  8520. 'begin',
  8521. ' SetLength(arr,3);',
  8522. ' arr[0]:=s;',
  8523. ' arr[1]:=[big];',
  8524. ' arr[2]:=[big]+s;',
  8525. ' arr[3]:=s+[big];',
  8526. ' arr[4]:=arr[5];',
  8527. ' s:=arr[6];',
  8528. ' i:=low(arr);',
  8529. ' i:=high(arr);',
  8530. ' DoIt(arr);',
  8531. ' DoIt([s]);',
  8532. ' DoIt([[],s]);',
  8533. ' DoIt([s,[]]);',
  8534. '']);
  8535. ConvertProgram;
  8536. CheckSource('TestArrayOfSet',
  8537. LinesToStr([ // statements
  8538. 'this.TFlag = {',
  8539. ' "0": "big",',
  8540. ' big: 0,',
  8541. ' "1": "small",',
  8542. ' small: 1',
  8543. '};',
  8544. 'this.DoIt = function (a) {',
  8545. '};',
  8546. 'this.f = 0;',
  8547. 'this.s = {};',
  8548. 'this.Arr = [];',
  8549. 'this.i = 0;',
  8550. '']),
  8551. LinesToStr([ // $mod.$main
  8552. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8553. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8554. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8555. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8556. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8557. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8558. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8559. '$mod.i = 0;',
  8560. '$mod.i = rtl.length($mod.Arr) - 1;',
  8561. '$mod.DoIt($mod.Arr);',
  8562. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8563. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8564. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8565. '']));
  8566. end;
  8567. procedure TTestModule.TestArray_DynAsParam;
  8568. begin
  8569. StartProgram(false);
  8570. Add([
  8571. 'type integer = longint;',
  8572. 'type TArrInt = array of integer;',
  8573. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8574. 'var vJ: TArrInt;',
  8575. 'begin',
  8576. ' vg:=vg;',
  8577. ' vj:=vh;',
  8578. ' vi:=vi;',
  8579. ' doit(vg,vg,vg);',
  8580. ' doit(vh,vh,vj);',
  8581. ' doit(vi,vi,vi);',
  8582. ' doit(vj,vj,vj);',
  8583. 'end;',
  8584. 'var i: TArrInt;',
  8585. 'begin',
  8586. ' doit(i,i,i);']);
  8587. ConvertProgram;
  8588. CheckSource('TestArray_DynAsParams',
  8589. LinesToStr([ // statements
  8590. 'this.DoIt = function (vG,vH,vI) {',
  8591. ' var vJ = [];',
  8592. ' vG = rtl.arrayRef(vG);',
  8593. ' vJ = rtl.arrayRef(vH);',
  8594. ' vI.set(rtl.arrayRef(vI.get()));',
  8595. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  8596. ' get: function () {',
  8597. ' return vG;',
  8598. ' },',
  8599. ' set: function (v) {',
  8600. ' vG = v;',
  8601. ' }',
  8602. ' });',
  8603. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  8604. ' get: function () {',
  8605. ' return vJ;',
  8606. ' },',
  8607. ' set: function (v) {',
  8608. ' vJ = v;',
  8609. ' }',
  8610. ' });',
  8611. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  8612. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  8613. ' get: function () {',
  8614. ' return vJ;',
  8615. ' },',
  8616. ' set: function (v) {',
  8617. ' vJ = v;',
  8618. ' }',
  8619. ' });',
  8620. '};',
  8621. 'this.i = [];'
  8622. ]),
  8623. LinesToStr([
  8624. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  8625. ' p: $mod,',
  8626. ' get: function () {',
  8627. ' return this.p.i;',
  8628. ' },',
  8629. ' set: function (v) {',
  8630. ' this.p.i = v;',
  8631. ' }',
  8632. '});'
  8633. ]));
  8634. end;
  8635. procedure TTestModule.TestArray_StaticAsParam;
  8636. begin
  8637. StartProgram(false);
  8638. Add([
  8639. 'type integer = longint;',
  8640. 'type TArrInt = array[1..2] of integer;',
  8641. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8642. 'var vJ: TArrInt;',
  8643. 'begin',
  8644. ' vg:=vg;',
  8645. ' vj:=vh;',
  8646. ' vi:=vi;',
  8647. ' doit(vg,vg,vg);',
  8648. ' doit(vh,vh,vj);',
  8649. ' doit(vi,vi,vi);',
  8650. ' doit(vj,vj,vj);',
  8651. 'end;',
  8652. 'var i: TArrInt;',
  8653. 'begin',
  8654. ' doit(i,i,i);']);
  8655. ConvertProgram;
  8656. CheckSource('TestArray_StaticAsParams',
  8657. LinesToStr([ // statements
  8658. 'this.DoIt = function (vG,vH,vI) {',
  8659. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8660. ' vG = vG.slice(0);',
  8661. ' vJ = vH.slice(0);',
  8662. ' vI.set(vI.get().slice(0));',
  8663. ' $mod.DoIt(vG.slice(0), vG, {',
  8664. ' get: function () {',
  8665. ' return vG;',
  8666. ' },',
  8667. ' set: function (v) {',
  8668. ' vG = v;',
  8669. ' }',
  8670. ' });',
  8671. ' $mod.DoIt(vH.slice(0), vH, {',
  8672. ' get: function () {',
  8673. ' return vJ;',
  8674. ' },',
  8675. ' set: function (v) {',
  8676. ' vJ = v;',
  8677. ' }',
  8678. ' });',
  8679. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8680. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8681. ' get: function () {',
  8682. ' return vJ;',
  8683. ' },',
  8684. ' set: function (v) {',
  8685. ' vJ = v;',
  8686. ' }',
  8687. ' });',
  8688. '};',
  8689. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8690. ]),
  8691. LinesToStr([
  8692. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  8693. ' p: $mod,',
  8694. ' get: function () {',
  8695. ' return this.p.i;',
  8696. ' },',
  8697. ' set: function (v) {',
  8698. ' this.p.i = v;',
  8699. ' }',
  8700. '});'
  8701. ]));
  8702. end;
  8703. procedure TTestModule.TestArrayElement_AsParams;
  8704. begin
  8705. StartProgram(false);
  8706. Add('type integer = longint;');
  8707. Add('type TArrayInt = array of integer;');
  8708. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  8709. Add('var vJ: tarrayint;');
  8710. Add('begin');
  8711. Add(' vi:=vi;');
  8712. Add(' doit(vi,vi,vi);');
  8713. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  8714. Add('end;');
  8715. Add('var a: TArrayInt;');
  8716. Add('begin');
  8717. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  8718. ConvertProgram;
  8719. CheckSource('TestArrayElement_AsParams',
  8720. LinesToStr([ // statements
  8721. 'this.DoIt = function (vG,vH,vI) {',
  8722. ' var vJ = [];',
  8723. ' vI.set(vI.get());',
  8724. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8725. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  8726. ' a:1+3,',
  8727. ' p:vJ,',
  8728. ' get: function () {',
  8729. ' return this.p[this.a];',
  8730. ' },',
  8731. ' set: function (v) {',
  8732. ' this.p[this.a] = v;',
  8733. ' }',
  8734. ' });',
  8735. '};',
  8736. 'this.a = [];'
  8737. ]),
  8738. LinesToStr([
  8739. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8740. ' a: 1+6,',
  8741. ' p: $mod.a,',
  8742. ' get: function () {',
  8743. ' return this.p[this.a];',
  8744. ' },',
  8745. ' set: function (v) {',
  8746. ' this.p[this.a] = v;',
  8747. ' }',
  8748. '});'
  8749. ]));
  8750. end;
  8751. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8752. begin
  8753. StartProgram(false);
  8754. Add('type Integer = longint;');
  8755. Add('type TArrayInt = array of integer;');
  8756. Add('function GetArr(vB: integer = 0): tarrayint;');
  8757. Add('begin');
  8758. Add('end;');
  8759. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8760. Add('begin');
  8761. Add('end;');
  8762. Add('begin');
  8763. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8764. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8765. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8766. ConvertProgram;
  8767. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8768. LinesToStr([ // statements
  8769. 'this.GetArr = function (vB) {',
  8770. ' var Result = [];',
  8771. ' return Result;',
  8772. '};',
  8773. 'this.DoIt = function (vG,vH,vI) {',
  8774. '};'
  8775. ]),
  8776. LinesToStr([
  8777. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8778. ' a: 1+3,',
  8779. ' p: $mod.GetArr(0),',
  8780. ' get: function () {',
  8781. ' return this.p[this.a];',
  8782. ' },',
  8783. ' set: function (v) {',
  8784. ' this.p[this.a] = v;',
  8785. ' }',
  8786. '});',
  8787. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8788. ' a: 2+3,',
  8789. ' p: $mod.GetArr(0),',
  8790. ' get: function () {',
  8791. ' return this.p[this.a];',
  8792. ' },',
  8793. ' set: function (v) {',
  8794. ' this.p[this.a] = v;',
  8795. ' }',
  8796. '});',
  8797. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8798. ' a: 3+3,',
  8799. ' p: $mod.GetArr(9),',
  8800. ' get: function () {',
  8801. ' return this.p[this.a];',
  8802. ' },',
  8803. ' set: function (v) {',
  8804. ' this.p[this.a] = v;',
  8805. ' }',
  8806. '});',
  8807. '']));
  8808. end;
  8809. procedure TTestModule.TestArrayEnumTypeRange;
  8810. begin
  8811. StartProgram(false);
  8812. Add([
  8813. 'type',
  8814. ' TEnum = (red,blue);',
  8815. ' TEnumArray = array[TEnum] of longint;',
  8816. 'var',
  8817. ' e: TEnum;',
  8818. ' i: longint;',
  8819. ' a: TEnumArray;',
  8820. ' numbers: TEnumArray = (1,2);',
  8821. ' names: array[TEnum] of string = (''red'',''blue'');',
  8822. 'begin',
  8823. ' e:=low(a);',
  8824. ' e:=high(a);',
  8825. ' i:=a[red];',
  8826. ' a[e]:=a[e];']);
  8827. ConvertProgram;
  8828. CheckSource('TestArrayEnumTypeRange',
  8829. LinesToStr([ // statements
  8830. ' this.TEnum = {',
  8831. ' "0": "red",',
  8832. ' red: 0,',
  8833. ' "1": "blue",',
  8834. ' blue: 1',
  8835. '};',
  8836. 'this.e = 0;',
  8837. 'this.i = 0;',
  8838. 'this.a = rtl.arraySetLength(null,0,2);',
  8839. 'this.numbers = [1, 2];',
  8840. 'this.names = ["red", "blue"];',
  8841. '']),
  8842. LinesToStr([ // $mod.$main
  8843. '$mod.e = $mod.TEnum.red;',
  8844. '$mod.e = $mod.TEnum.blue;',
  8845. '$mod.i = $mod.a[$mod.TEnum.red];',
  8846. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8847. '']));
  8848. end;
  8849. procedure TTestModule.TestArray_SetLengthOutArg;
  8850. begin
  8851. StartProgram(false);
  8852. Add([
  8853. 'type TArrInt = array of longint;',
  8854. 'procedure DoIt(out a: TArrInt);',
  8855. 'begin',
  8856. ' SetLength(a,2);',
  8857. 'end;',
  8858. 'begin',
  8859. '']);
  8860. ConvertProgram;
  8861. CheckSource('TestArray_SetLengthOutArg',
  8862. LinesToStr([ // statements
  8863. 'this.DoIt = function (a) {',
  8864. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8865. '};',
  8866. '']),
  8867. LinesToStr([
  8868. '']));
  8869. end;
  8870. procedure TTestModule.TestArray_SetLengthProperty;
  8871. begin
  8872. StartProgram(false);
  8873. Add('type');
  8874. Add(' TArrInt = array of longint;');
  8875. Add(' TObject = class');
  8876. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8877. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8878. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8879. Add(' end;');
  8880. Add('var Obj: TObject;');
  8881. Add('begin');
  8882. Add(' SetLength(Obj.Colors,2);');
  8883. ConvertProgram;
  8884. CheckSource('TestArray_SetLengthProperty',
  8885. LinesToStr([ // statements
  8886. 'rtl.createClass($mod, "TObject", null, function () {',
  8887. ' this.$init = function () {',
  8888. ' };',
  8889. ' this.$final = function () {',
  8890. ' };',
  8891. '});',
  8892. 'this.Obj = null;',
  8893. '']),
  8894. LinesToStr([
  8895. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  8896. '']));
  8897. end;
  8898. procedure TTestModule.TestArray_SetLengthMultiDim;
  8899. begin
  8900. StartProgram(false);
  8901. Add([
  8902. 'type',
  8903. ' TArrArrInt = array of array of longint;',
  8904. ' TArrStaInt = array of array[1..2] of longint;',
  8905. 'var',
  8906. ' a: TArrArrInt;',
  8907. ' b: TArrStaInt;',
  8908. 'begin',
  8909. ' SetLength(a,2);',
  8910. ' SetLength(a,3,4);',
  8911. ' SetLength(b,5);',
  8912. '']);
  8913. ConvertProgram;
  8914. CheckSource('TestArray_SetLengthMultiDim',
  8915. LinesToStr([ // statements
  8916. 'this.a = [];',
  8917. 'this.b = [];',
  8918. '']),
  8919. LinesToStr([
  8920. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  8921. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  8922. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  8923. '']));
  8924. end;
  8925. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  8926. begin
  8927. StartProgram(false);
  8928. Add([
  8929. 'type',
  8930. ' TStaArr1 = array[1..3] of boolean;',
  8931. //' TStaArr2 = array[5..6] of TStaArr1;',
  8932. ' TDynArr1StaArr1 = array of TStaArr1;',
  8933. //' TDynArr1StaArr2 = array of TStaArr2;',
  8934. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  8935. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  8936. 'var',
  8937. ' DynArr1StaArr1: TDynArr1StaArr1;',
  8938. //' DynArr1StaArr2: TDynArr1StaArr1;',
  8939. ' DynArr2StaArr1: TDynArr2StaArr1;',
  8940. //' DynArr2StaArr2: TDynArr2StaArr2;',
  8941. 'begin',
  8942. ' SetLength(DynArr1StaArr1,11);',
  8943. ' SetLength(DynArr2StaArr1,12);',
  8944. ' SetLength(DynArr2StaArr1[13],14);',
  8945. ' SetLength(DynArr2StaArr1,15,16);',
  8946. //' SetLength(DynArr1StaArr2,21);',
  8947. //' SetLength(DynArr2StaArr2,22);',
  8948. //' SetLength(DynArr2StaArr2[23],24);',
  8949. //' SetLength(DynArr2StaArr2,25,26);',
  8950. '']);
  8951. ConvertProgram;
  8952. CheckSource('TestArray_DynOfStatic',
  8953. LinesToStr([ // statements
  8954. 'this.DynArr1StaArr1 = [];',
  8955. 'this.DynArr2StaArr1 = [];',
  8956. '']),
  8957. LinesToStr([ // $mod.$main
  8958. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  8959. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  8960. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  8961. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  8962. ' $mod.DynArr2StaArr1,',
  8963. ' false,',
  8964. ' 15,',
  8965. ' 16,',
  8966. ' "s",',
  8967. ' 3',
  8968. ');',
  8969. '']));
  8970. end;
  8971. procedure TTestModule.TestArray_OpenArrayOfString;
  8972. begin
  8973. StartProgram(false);
  8974. Add('procedure DoIt(const a: array of String);');
  8975. Add('var');
  8976. Add(' i: longint;');
  8977. Add(' s: string;');
  8978. Add('begin');
  8979. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  8980. Add('end;');
  8981. Add('var s: string;');
  8982. Add('begin');
  8983. Add(' DoIt([]);');
  8984. Add(' DoIt([s,''foo'','''',s+s]);');
  8985. ConvertProgram;
  8986. CheckSource('TestArray_OpenArrayOfString',
  8987. LinesToStr([ // statements
  8988. 'this.DoIt = function (a) {',
  8989. ' var i = 0;',
  8990. ' var s = "";',
  8991. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  8992. ' i = $l1;',
  8993. ' s = a[rtl.length(a) - i - 1];',
  8994. ' };',
  8995. '};',
  8996. 'this.s = "";',
  8997. '']),
  8998. LinesToStr([
  8999. '$mod.DoIt([]);',
  9000. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9001. '']));
  9002. end;
  9003. procedure TTestModule.TestArray_ConstRef;
  9004. begin
  9005. StartProgram(false);
  9006. Add([
  9007. 'type TArr = array of word;',
  9008. 'procedure Run(constref a: TArr);',
  9009. 'begin',
  9010. 'end;',
  9011. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9012. 'var l: TArr;',
  9013. 'begin',
  9014. ' Run(l);',
  9015. ' Run(a);',
  9016. ' Run(b);',
  9017. ' Run(c);',
  9018. ' Run(d);',
  9019. ' Run(e);',
  9020. 'end;',
  9021. 'begin',
  9022. '']);
  9023. ConvertProgram;
  9024. CheckResolverUnexpectedHints();
  9025. CheckSource('TestArray_ConstRef',
  9026. LinesToStr([ // statements
  9027. 'this.Run = function (a) {',
  9028. '};',
  9029. 'this.Fly = function (a, b, c, d, e) {',
  9030. ' var l = [];',
  9031. ' $mod.Run(l);',
  9032. ' $mod.Run(a);',
  9033. ' $mod.Run(b.get());',
  9034. ' $mod.Run(c.get());',
  9035. ' $mod.Run(d);',
  9036. ' $mod.Run(e);',
  9037. '};',
  9038. '']),
  9039. LinesToStr([
  9040. '']));
  9041. end;
  9042. procedure TTestModule.TestArray_Concat;
  9043. begin
  9044. StartProgram(false);
  9045. Add([
  9046. 'type',
  9047. ' integer = longint;',
  9048. ' TFlag = (big,small);',
  9049. ' TFlags = set of TFlag;',
  9050. ' TRec = record',
  9051. ' i: integer;',
  9052. ' end;',
  9053. ' TArrInt = array of integer;',
  9054. ' TArrRec = array of TRec;',
  9055. ' TArrFlag = array of TFlag;',
  9056. ' TArrSet = array of TFlags;',
  9057. ' TArrJSValue = array of jsvalue;',
  9058. 'var',
  9059. ' ArrInt: tarrint;',
  9060. ' ArrRec: tarrrec;',
  9061. ' ArrFlag: tarrflag;',
  9062. ' ArrSet: tarrset;',
  9063. ' ArrJSValue: tarrjsvalue;',
  9064. 'begin',
  9065. ' arrint:=concat(arrint);',
  9066. ' arrint:=concat(arrint,arrint);',
  9067. ' arrint:=concat(arrint,arrint,arrint);',
  9068. ' arrrec:=concat(arrrec);',
  9069. ' arrrec:=concat(arrrec,arrrec);',
  9070. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9071. ' arrset:=concat(arrset);',
  9072. ' arrset:=concat(arrset,arrset);',
  9073. ' arrset:=concat(arrset,arrset,arrset);',
  9074. ' arrjsvalue:=concat(arrjsvalue);',
  9075. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9076. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9077. ' arrint:=concat([1],arrint);',
  9078. ' arrflag:=concat([big]);',
  9079. ' arrflag:=concat([big],arrflag);',
  9080. ' arrflag:=concat(arrflag,[small]);',
  9081. '']);
  9082. ConvertProgram;
  9083. CheckSource('TestArray_Concat',
  9084. LinesToStr([ // statements
  9085. 'this.TFlag = {',
  9086. ' "0": "big",',
  9087. ' big: 0,',
  9088. ' "1": "small",',
  9089. ' small: 1',
  9090. '};',
  9091. 'rtl.recNewT($mod, "TRec", function () {',
  9092. ' this.i = 0;',
  9093. ' this.$eq = function (b) {',
  9094. ' return this.i === b.i;',
  9095. ' };',
  9096. ' this.$assign = function (s) {',
  9097. ' this.i = s.i;',
  9098. ' return this;',
  9099. ' };',
  9100. '});',
  9101. 'this.ArrInt = [];',
  9102. 'this.ArrRec = [];',
  9103. 'this.ArrFlag = [];',
  9104. 'this.ArrSet = [];',
  9105. 'this.ArrJSValue = [];',
  9106. '']),
  9107. LinesToStr([ // $mod.$main
  9108. '$mod.ArrInt = $mod.ArrInt;',
  9109. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9110. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9111. '$mod.ArrRec = $mod.ArrRec;',
  9112. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9113. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9114. '$mod.ArrSet = $mod.ArrSet;',
  9115. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9116. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9117. '$mod.ArrJSValue = $mod.ArrJSValue;',
  9118. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9119. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9120. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9121. '$mod.ArrFlag = [$mod.TFlag.big];',
  9122. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9123. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9124. '']));
  9125. end;
  9126. procedure TTestModule.TestArray_Copy;
  9127. begin
  9128. StartProgram(false);
  9129. Add([
  9130. 'type',
  9131. ' integer = longint;',
  9132. ' TFlag = (big,small);',
  9133. ' TFlags = set of TFlag;',
  9134. ' TRec = record',
  9135. ' i: integer;',
  9136. ' end;',
  9137. ' TArrInt = array of integer;',
  9138. ' TArrRec = array of TRec;',
  9139. ' TArrSet = array of TFlags;',
  9140. ' TArrJSValue = array of jsvalue;',
  9141. 'var',
  9142. ' ArrInt: tarrint;',
  9143. ' ArrRec: tarrrec;',
  9144. ' ArrSet: tarrset;',
  9145. ' ArrJSValue: tarrjsvalue;',
  9146. 'begin',
  9147. ' arrint:=copy(arrint);',
  9148. ' arrint:=copy(arrint,2);',
  9149. ' arrint:=copy(arrint,3,4);',
  9150. ' arrint:=copy([1,1],1,2);',
  9151. ' arrrec:=copy(arrrec);',
  9152. ' arrrec:=copy(arrrec,5);',
  9153. ' arrrec:=copy(arrrec,6,7);',
  9154. ' arrset:=copy(arrset);',
  9155. ' arrset:=copy(arrset,8);',
  9156. ' arrset:=copy(arrset,9,10);',
  9157. ' arrjsvalue:=copy(arrjsvalue);',
  9158. ' arrjsvalue:=copy(arrjsvalue,11);',
  9159. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9160. ' ']);
  9161. ConvertProgram;
  9162. CheckSource('TestArray_Copy',
  9163. LinesToStr([ // statements
  9164. 'this.TFlag = {',
  9165. ' "0": "big",',
  9166. ' big: 0,',
  9167. ' "1": "small",',
  9168. ' small: 1',
  9169. '};',
  9170. 'rtl.recNewT($mod, "TRec", function () {',
  9171. ' this.i = 0;',
  9172. ' this.$eq = function (b) {',
  9173. ' return this.i === b.i;',
  9174. ' };',
  9175. ' this.$assign = function (s) {',
  9176. ' this.i = s.i;',
  9177. ' return this;',
  9178. ' };',
  9179. '});',
  9180. 'this.ArrInt = [];',
  9181. 'this.ArrRec = [];',
  9182. 'this.ArrSet = [];',
  9183. 'this.ArrJSValue = [];',
  9184. '']),
  9185. LinesToStr([ // $mod.$main
  9186. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9187. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9188. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9189. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9190. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9191. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9192. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9193. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9194. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9195. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9196. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9197. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9198. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9199. '']));
  9200. end;
  9201. procedure TTestModule.TestArray_InsertDelete;
  9202. begin
  9203. StartProgram(false);
  9204. Add([
  9205. 'type',
  9206. ' integer = longint;',
  9207. ' TFlag = (big,small);',
  9208. ' TFlags = set of TFlag;',
  9209. ' TRec = record',
  9210. ' i: integer;',
  9211. ' end;',
  9212. ' TArrInt = array of integer;',
  9213. ' TArrRec = array of TRec;',
  9214. ' TArrSet = array of TFlags;',
  9215. ' TArrJSValue = array of jsvalue;',
  9216. ' TArrArrInt = array of TArrInt;',
  9217. 'var',
  9218. ' ArrInt: tarrint;',
  9219. ' ArrRec: tarrrec;',
  9220. ' ArrSet: tarrset;',
  9221. ' ArrJSValue: tarrjsvalue;',
  9222. ' ArrArrInt: TArrArrInt;',
  9223. 'begin',
  9224. ' Insert(1,arrint,2);',
  9225. ' Insert(arrint[3],arrint,4);',
  9226. ' Insert(arrrec[5],arrrec,6);',
  9227. ' Insert(arrset[7],arrset,7);',
  9228. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9229. ' Insert(10,arrjsvalue,11);',
  9230. ' Insert([23],arrarrint,22);',
  9231. ' Delete(arrint,12,13);',
  9232. ' Delete(arrrec,14,15);',
  9233. ' Delete(arrset,17,18);',
  9234. ' Delete(arrjsvalue,19,10);']);
  9235. ConvertProgram;
  9236. CheckSource('TestArray_InsertDelete',
  9237. LinesToStr([ // statements
  9238. 'this.TFlag = {',
  9239. ' "0": "big",',
  9240. ' big: 0,',
  9241. ' "1": "small",',
  9242. ' small: 1',
  9243. '};',
  9244. 'rtl.recNewT($mod, "TRec", function () {',
  9245. ' this.i = 0;',
  9246. ' this.$eq = function (b) {',
  9247. ' return this.i === b.i;',
  9248. ' };',
  9249. ' this.$assign = function (s) {',
  9250. ' this.i = s.i;',
  9251. ' return this;',
  9252. ' };',
  9253. '});',
  9254. 'this.ArrInt = [];',
  9255. 'this.ArrRec = [];',
  9256. 'this.ArrSet = [];',
  9257. 'this.ArrJSValue = [];',
  9258. 'this.ArrArrInt = [];',
  9259. '']),
  9260. LinesToStr([ // $mod.$main
  9261. '$mod.ArrInt.splice(2, 0, 1);',
  9262. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9263. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9264. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9265. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9266. '$mod.ArrJSValue.splice(11, 0, 10);',
  9267. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9268. '$mod.ArrInt.splice(12, 13);',
  9269. '$mod.ArrRec.splice(14, 15);',
  9270. '$mod.ArrSet.splice(17, 18);',
  9271. '$mod.ArrJSValue.splice(19, 10);',
  9272. '']));
  9273. end;
  9274. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9275. begin
  9276. StartProgram(false);
  9277. Add([
  9278. '{$modeswitch arrayoperators}',
  9279. 'type',
  9280. ' integer = longint;',
  9281. ' TArrInt = array of integer;',
  9282. ' TArrStr = array of string;',
  9283. 'const',
  9284. ' Ints: TArrInt = (1,2,3);',
  9285. ' Aliases: TarrStr = (''foo'',''b'');',
  9286. ' OneInt: TArrInt = (7);',
  9287. ' OneStr: array of integer = (7);',
  9288. ' Chars: array of char = ''aoc'';',
  9289. ' Names: array of string = (''a'',''foo'');',
  9290. ' NameCount = low(Names)+high(Names)+length(Names);',
  9291. 'var i: integer;',
  9292. 'begin',
  9293. ' Ints:=[];',
  9294. ' Ints:=[1,1];',
  9295. ' Ints:=[1]+[2];',
  9296. ' Ints:=[2];',
  9297. ' Ints:=[]+ints;',
  9298. ' Ints:=Ints+[];',
  9299. ' Ints:=Ints+OneInt;',
  9300. ' Ints:=Ints+[1,1];',
  9301. ' Ints:=[i,i]+Ints;',
  9302. ' Ints:=[1]+[i]+[3];',
  9303. '']);
  9304. ConvertProgram;
  9305. CheckSource('TestArray_DynArrayConstObjFPC',
  9306. LinesToStr([ // statements
  9307. 'this.Ints = [1, 2, 3];',
  9308. 'this.Aliases = ["foo", "b"];',
  9309. 'this.OneInt = [7];',
  9310. 'this.OneStr = [7];',
  9311. 'this.Chars = ["a", "o", "c"];',
  9312. 'this.Names = ["a", "foo"];',
  9313. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9314. 'this.i = 0;',
  9315. '']),
  9316. LinesToStr([ // $mod.$main
  9317. '$mod.Ints = [];',
  9318. '$mod.Ints = [1, 1];',
  9319. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9320. '$mod.Ints = [2];',
  9321. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9322. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9323. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9324. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9325. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9326. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9327. '']));
  9328. end;
  9329. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9330. begin
  9331. StartProgram(false);
  9332. // Note: const c = [1,1]; defines a set!
  9333. Add([
  9334. '{$mode delphi}',
  9335. 'type',
  9336. ' integer = longint;',
  9337. ' TArrInt = array of integer;',
  9338. ' TArrStr = array of string;',
  9339. 'const',
  9340. ' Ints: TArrInt = [1,1,2];',
  9341. ' Aliases: TarrStr = [''foo'',''b''];',
  9342. ' OneInt: TArrInt = [7];',
  9343. ' OneStr: array of integer = [7]+[8];',
  9344. ' Chars: array of char = ''aoc'';',
  9345. ' Names: array of string = [''a'',''a''];',
  9346. ' NameCount = low(Names)+high(Names)+length(Names);',
  9347. 'begin',
  9348. '']);
  9349. ConvertProgram;
  9350. CheckSource('TestArray_DynArrayConstDelphi',
  9351. LinesToStr([ // statements
  9352. 'this.Ints = [1, 1, 2];',
  9353. 'this.Aliases = ["foo", "b"];',
  9354. 'this.OneInt = [7];',
  9355. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9356. 'this.Chars = ["a", "o", "c"];',
  9357. 'this.Names = ["a", "a"];',
  9358. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9359. '']),
  9360. LinesToStr([ // $mod.$main
  9361. '']));
  9362. end;
  9363. procedure TTestModule.TestArray_ArrayLitAsParam;
  9364. begin
  9365. StartProgram(false);
  9366. Add([
  9367. '{$modeswitch arrayoperators}',
  9368. 'type',
  9369. ' integer = longint;',
  9370. ' TArrInt = array of integer;',
  9371. ' TArrSet = array of (red,green,blue);',
  9372. 'procedure DoOpenInt(a: array of integer); forward;',
  9373. 'procedure DoInt(a: TArrInt);',
  9374. 'begin',
  9375. ' DoInt(a+[1]);',
  9376. ' DoInt([1]+a);',
  9377. ' DoOpenInt(a);',
  9378. ' DoOpenInt(a+[1]);',
  9379. ' DoOpenInt([1]+a);',
  9380. 'end;',
  9381. 'procedure DoOpenInt(a: array of integer);',
  9382. 'begin',
  9383. ' DoOpenInt(a+[1]);',
  9384. ' DoOpenInt([1]+a);',
  9385. ' DoInt(a);',
  9386. ' DoInt(a+[1]);',
  9387. ' DoInt([1]+a);',
  9388. 'end;',
  9389. 'procedure DoSet(a: TArrSet);',
  9390. 'begin',
  9391. ' DoSet(a+[red]);',
  9392. ' DoSet([blue]+a);',
  9393. 'end;',
  9394. 'var',
  9395. ' i: TArrInt;',
  9396. ' s: TArrSet;',
  9397. 'begin',
  9398. ' DoInt([1]);',
  9399. ' DoInt([1]+[2]);',
  9400. ' DoInt(i+[1]);',
  9401. ' DoInt([1]+i);',
  9402. ' DoOpenInt([1]);',
  9403. ' DoOpenInt([1]+[2]);',
  9404. ' DoOpenInt(i+[1]);',
  9405. ' DoOpenInt([1]+i);',
  9406. ' DoSet([red]);',
  9407. ' DoSet([blue]+[green]);',
  9408. ' DoSet(s+[blue]);',
  9409. ' DoSet([red]+s);',
  9410. '']);
  9411. ConvertProgram;
  9412. CheckSource('TestArray_ArrayLitAsParam',
  9413. LinesToStr([ // statements
  9414. 'this.TArrSet$a = {',
  9415. ' "0": "red",',
  9416. ' red: 0,',
  9417. ' "1": "green",',
  9418. ' green: 1,',
  9419. ' "2": "blue",',
  9420. ' blue: 2',
  9421. '};',
  9422. 'this.DoInt = function (a) {',
  9423. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9424. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9425. ' $mod.DoOpenInt(a);',
  9426. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9427. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9428. '};',
  9429. 'this.DoOpenInt = function (a) {',
  9430. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9431. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9432. ' $mod.DoInt(rtl.arrayRef(a));',
  9433. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9434. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9435. '};',
  9436. 'this.DoSet = function (a) {',
  9437. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9438. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9439. '};',
  9440. 'this.i = [];',
  9441. 'this.s = [];',
  9442. '']),
  9443. LinesToStr([ // $mod.$main
  9444. '$mod.DoInt([1]);',
  9445. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9446. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9447. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9448. '$mod.DoOpenInt([1]);',
  9449. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9450. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9451. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9452. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9453. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9454. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9455. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9456. '']));
  9457. end;
  9458. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9459. begin
  9460. StartProgram(false);
  9461. Add([
  9462. '{$modeswitch arrayoperators}',
  9463. 'type',
  9464. ' integer = longint;',
  9465. ' TArrInt = array of integer;',
  9466. ' TArrArrInt = array of TArrInt;',
  9467. 'procedure DoInt(const a: TArrArrInt);',
  9468. 'begin',
  9469. ' DoInt(a+[[1]]);',
  9470. ' DoInt([[1]]+a);',
  9471. ' DoInt(a);',
  9472. 'end;',
  9473. 'var',
  9474. ' i: TArrInt;',
  9475. ' a: TArrArrInt;',
  9476. 'begin',
  9477. ' a:=[[1]];',
  9478. ' a:=[i];',
  9479. ' a:=a+[i];',
  9480. ' a:=[i]+a;',
  9481. ' a:=[[1]+i];',
  9482. ' a:=[[1]+[2]];',
  9483. ' a:=[i+[2]];',
  9484. ' DoInt([[1]]);',
  9485. ' DoInt([[1]+[2],[3,4],[5]]);',
  9486. ' DoInt([i+[1]]+a);',
  9487. ' DoInt([i]+a);',
  9488. '']);
  9489. ConvertProgram;
  9490. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9491. LinesToStr([ // statements
  9492. 'this.DoInt = function (a) {',
  9493. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9494. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9495. ' $mod.DoInt(a);',
  9496. '};',
  9497. 'this.i = [];',
  9498. 'this.a = [];',
  9499. '']),
  9500. LinesToStr([ // $mod.$main
  9501. '$mod.a = [[1]];',
  9502. '$mod.a = [$mod.i];',
  9503. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9504. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9505. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9506. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9507. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9508. '$mod.DoInt([[1]]);',
  9509. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9510. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9511. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9512. '']));
  9513. end;
  9514. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9515. begin
  9516. StartProgram(false);
  9517. Add([
  9518. '{$modeswitch arrayoperators}',
  9519. 'type',
  9520. ' integer = longint;',
  9521. ' TArrInt = array[1..2] of integer;',
  9522. ' TArrArrInt = array of TArrInt;',
  9523. 'procedure DoInt(const a: TArrArrInt);',
  9524. 'begin',
  9525. ' DoInt(a+[[1,2]]);',
  9526. ' DoInt([[1,2]]+a);',
  9527. ' DoInt(a);',
  9528. 'end;',
  9529. 'var',
  9530. ' i: TArrInt;',
  9531. ' a: TArrArrInt;',
  9532. 'begin',
  9533. ' a:=[[1,1]];',
  9534. ' a:=[i];',
  9535. ' a:=a+[i];',
  9536. ' a:=[i]+a;',
  9537. ' DoInt([[1,1]]);',
  9538. ' DoInt([[1,2],[3,4]]);',
  9539. '']);
  9540. ConvertProgram;
  9541. CheckSource('TestArray_ArrayLitStaticAsParam',
  9542. LinesToStr([ // statements
  9543. 'this.DoInt = function (a) {',
  9544. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9545. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9546. ' $mod.DoInt(a);',
  9547. '};',
  9548. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9549. 'this.a = [];',
  9550. '']),
  9551. LinesToStr([ // $mod.$main
  9552. '$mod.a = [[1, 1]];',
  9553. '$mod.a = [$mod.i.slice(0)];',
  9554. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9555. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9556. '$mod.DoInt([[1, 1]]);',
  9557. '$mod.DoInt([[1, 2], [3, 4]]);',
  9558. '']));
  9559. end;
  9560. procedure TTestModule.TestArray_ForInArrOfString;
  9561. begin
  9562. StartProgram(false);
  9563. Add([
  9564. 'type',
  9565. 'type',
  9566. ' TMonthNameArray = array [1..12] of string;',
  9567. ' TMonthNames = TMonthNameArray;',
  9568. ' TObject = class',
  9569. ' private',
  9570. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9571. ' public',
  9572. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9573. ' end;',
  9574. 'var',
  9575. ' f: TObject;',
  9576. ' Month: string;',
  9577. ' Names: array of string = (''a'',''foo'',''bar'');',
  9578. ' i: longint;',
  9579. 'begin',
  9580. ' for Month in f.LongMonthNames do ;',
  9581. ' for Month in Names do ;',
  9582. ' for i:=low(Names) to high(Names) do ;',
  9583. '']);
  9584. ConvertProgram;
  9585. CheckSource('TestArray_ForInArrOfString',
  9586. LinesToStr([ // statements
  9587. 'rtl.createClass($mod, "TObject", null, function () {',
  9588. ' this.$init = function () {',
  9589. ' };',
  9590. ' this.$final = function () {',
  9591. ' };',
  9592. '});',
  9593. 'this.f = null;',
  9594. 'this.Month = "";',
  9595. 'this.Names = ["a", "foo", "bar"];',
  9596. 'this.i = 0;',
  9597. '']),
  9598. LinesToStr([ // $mod.$main
  9599. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9600. 'for (var $in4 = $mod.Names, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.Month = $in4[$l5];',
  9601. 'for (var $l7 = 0, $end8 = rtl.length($mod.Names) - 1; $l7 <= $end8; $l7++) $mod.i = $l7;',
  9602. '']));
  9603. end;
  9604. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9605. begin
  9606. StartProgram(false);
  9607. Add([
  9608. '{$modeswitch externalclass}',
  9609. 'type',
  9610. ' TJSObject = class external name ''Object''',
  9611. ' end;',
  9612. ' TJSArray = class external name ''Array''',
  9613. ' class function isArray(Value: JSValue) : boolean;',
  9614. ' function concat() : TJSArray; varargs;',
  9615. ' end;',
  9616. 'var',
  9617. ' aObj: TJSArray;',
  9618. ' a: array of longint;',
  9619. ' o: TJSObject;',
  9620. 'begin',
  9621. ' if TJSArray.isArray(65) then ;',
  9622. ' aObj:=TJSArray(a).concat(a);',
  9623. ' o:=TJSObject(a);']);
  9624. ConvertProgram;
  9625. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9626. LinesToStr([ // statements
  9627. 'this.aObj = null;',
  9628. 'this.a = [];',
  9629. 'this.o = null;',
  9630. '']),
  9631. LinesToStr([ // $mod.$main
  9632. 'if (Array.isArray(65)) ;',
  9633. '$mod.aObj = $mod.a.concat($mod.a);',
  9634. '$mod.o = $mod.a;',
  9635. '']));
  9636. end;
  9637. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9638. begin
  9639. StartProgram(false);
  9640. Add([
  9641. '{$modeswitch externalclass}',
  9642. 'type',
  9643. ' TArrStr = array of string;',
  9644. ' TJSArray = class external name ''Array''',
  9645. ' end;',
  9646. ' TJSObject = class external name ''Object''',
  9647. ' end;',
  9648. 'var',
  9649. ' aObj: TJSArray;',
  9650. ' a: TArrStr;',
  9651. ' jo: TJSObject;',
  9652. 'begin',
  9653. ' a:=TArrStr(aObj);',
  9654. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  9655. ' a:=TarrStr(jo);',
  9656. '']);
  9657. ConvertProgram;
  9658. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  9659. LinesToStr([ // statements
  9660. 'this.aObj = null;',
  9661. 'this.a = [];',
  9662. 'this.jo = null;',
  9663. '']),
  9664. LinesToStr([ // $mod.$main
  9665. '$mod.a = $mod.aObj;',
  9666. '$mod.aObj[1] = $mod.aObj[2];',
  9667. '$mod.a = $mod.jo;',
  9668. '']));
  9669. end;
  9670. procedure TTestModule.TestArrayOfConst_TVarRec;
  9671. begin
  9672. StartProgram(true,[supTVarRec]);
  9673. Add([
  9674. 'procedure Say(args: array of const);',
  9675. 'var',
  9676. ' i: longint;',
  9677. ' v: TVarRec;',
  9678. 'begin',
  9679. ' for i:=low(args) to high(args) do begin',
  9680. ' v:=args[i];',
  9681. ' case v.vtype of',
  9682. ' vtInteger: if length(args)=args[i].vInteger then ;',
  9683. ' end;',
  9684. ' end;',
  9685. ' for v in args do ;',
  9686. ' args:=nil;',
  9687. ' SetLength(args,2);',
  9688. 'end;',
  9689. 'begin']);
  9690. ConvertProgram;
  9691. CheckSource('TestArrayOfConst_TVarRec',
  9692. LinesToStr([ // statements
  9693. 'this.Say = function (args) {',
  9694. ' var i = 0;',
  9695. ' var v = pas.system.TVarRec.$new();',
  9696. ' for (var $l1 = 0, $end2 = rtl.length(args) - 1; $l1 <= $end2; $l1++) {',
  9697. ' i = $l1;',
  9698. ' v.$assign(args[i]);',
  9699. ' var $tmp3 = v.VType;',
  9700. ' if ($tmp3 === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  9701. ' };',
  9702. ' for (var $in4 = args, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) v = $in4[$l5];',
  9703. ' args = [];',
  9704. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  9705. '};',
  9706. '']),
  9707. LinesToStr([ // $mod.$main
  9708. ]));
  9709. end;
  9710. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  9711. begin
  9712. StartProgram(true,[supTVarRec]);
  9713. Add([
  9714. 'procedure Say(args: array of const);',
  9715. 'begin',
  9716. ' Say(args);',
  9717. 'end;',
  9718. 'var',
  9719. ' p: Pointer;',
  9720. ' j: jsvalue;',
  9721. ' c: currency;',
  9722. 'begin',
  9723. ' Say([]);',
  9724. ' Say([1]);',
  9725. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  9726. '']);
  9727. ConvertProgram;
  9728. CheckSource('TestArrayOfConst_PassBaseTypes',
  9729. LinesToStr([ // statements
  9730. 'this.Say = function (args) {',
  9731. ' $mod.Say(args);',
  9732. '};',
  9733. 'this.p = null;',
  9734. 'this.j = undefined;',
  9735. 'this.c = 0;',
  9736. '']),
  9737. LinesToStr([ // $mod.$main
  9738. '$mod.Say([]);',
  9739. '$mod.Say(pas.system.VarRecs(0, 1));',
  9740. '$mod.Say(pas.system.VarRecs(',
  9741. ' 9,',
  9742. ' "c",',
  9743. ' 18,',
  9744. ' "foo",',
  9745. ' 5,',
  9746. ' null,',
  9747. ' 1,',
  9748. ' true,',
  9749. ' 3,',
  9750. ' 1.3,',
  9751. ' 5,',
  9752. ' $mod.p,',
  9753. ' 20,',
  9754. ' $mod.j,',
  9755. ' 12,',
  9756. ' $mod.c',
  9757. ' ));',
  9758. '']));
  9759. end;
  9760. procedure TTestModule.TestArrayOfConst_PassObj;
  9761. begin
  9762. StartProgram(true,[supTVarRec]);
  9763. Add([
  9764. '{$interfaces corba}',
  9765. 'type',
  9766. ' TObject = class',
  9767. ' end;',
  9768. ' TClass = class of TObject;',
  9769. ' IUnknown = interface',
  9770. ' end;',
  9771. 'procedure Say(args: array of const);',
  9772. 'begin',
  9773. 'end;',
  9774. 'var',
  9775. ' o: TObject;',
  9776. ' c: TClass;',
  9777. ' i: IUnknown;',
  9778. 'begin',
  9779. ' Say([o,c,TObject]);',
  9780. ' Say([nil,i]);',
  9781. '']);
  9782. ConvertProgram;
  9783. CheckSource('TestArrayOfConst_PassObj',
  9784. LinesToStr([ // statements
  9785. 'rtl.createClass($mod, "TObject", null, function () {',
  9786. ' this.$init = function () {',
  9787. ' };',
  9788. ' this.$final = function () {',
  9789. ' };',
  9790. '});',
  9791. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  9792. 'this.Say = function (args) {',
  9793. '};',
  9794. 'this.o = null;',
  9795. 'this.c = null;',
  9796. 'this.i = null;',
  9797. '']),
  9798. LinesToStr([ // $mod.$main
  9799. '$mod.Say(pas.system.VarRecs(',
  9800. ' 7,',
  9801. ' $mod.o,',
  9802. ' 8,',
  9803. ' $mod.c,',
  9804. ' 8,',
  9805. ' $mod.TObject',
  9806. '));',
  9807. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  9808. '']));
  9809. end;
  9810. procedure TTestModule.TestRecord_Empty;
  9811. begin
  9812. StartProgram(false);
  9813. Add([
  9814. 'type',
  9815. ' TRecA = record',
  9816. ' end;',
  9817. 'var a,b: TRecA;',
  9818. 'begin',
  9819. ' if a=b then ;']);
  9820. ConvertProgram;
  9821. CheckSource('TestRecord_Empty',
  9822. LinesToStr([ // statements
  9823. 'rtl.recNewT($mod, "TRecA", function () {',
  9824. ' this.$eq = function (b) {',
  9825. ' return true;',
  9826. ' };',
  9827. ' this.$assign = function (s) {',
  9828. ' return this;',
  9829. ' };',
  9830. '});',
  9831. 'this.a = $mod.TRecA.$new();',
  9832. 'this.b = $mod.TRecA.$new();',
  9833. '']),
  9834. LinesToStr([ // $mod.$main
  9835. 'if ($mod.a.$eq($mod.b)) ;'
  9836. ]));
  9837. end;
  9838. procedure TTestModule.TestRecord_Var;
  9839. begin
  9840. StartProgram(false);
  9841. Add('type');
  9842. Add(' TRecA = record');
  9843. Add(' Bold: longint;');
  9844. Add(' end;');
  9845. Add('var Rec: TRecA;');
  9846. Add('begin');
  9847. Add(' rec.bold:=123');
  9848. ConvertProgram;
  9849. CheckSource('TestRecord_Var',
  9850. LinesToStr([ // statements
  9851. 'rtl.recNewT($mod, "TRecA", function () {',
  9852. ' this.Bold = 0;',
  9853. ' this.$eq = function (b) {',
  9854. ' return this.Bold === b.Bold;',
  9855. ' };',
  9856. ' this.$assign = function (s) {',
  9857. ' this.Bold = s.Bold;',
  9858. ' return this;',
  9859. ' };',
  9860. '});',
  9861. 'this.Rec = $mod.TRecA.$new();',
  9862. '']),
  9863. LinesToStr([ // $mod.$main
  9864. '$mod.Rec.Bold = 123;'
  9865. ]));
  9866. end;
  9867. procedure TTestModule.TestRecord_VarExternal;
  9868. begin
  9869. StartProgram(false);
  9870. Add([
  9871. '{$modeswitch externalclass}',
  9872. 'type',
  9873. ' TRecA = record',
  9874. ' i: byte;',
  9875. ' length_: longint external name ''length'';',
  9876. ' end;',
  9877. 'var Rec: TRecA;',
  9878. 'begin',
  9879. ' rec.length_ := rec.length_',
  9880. '']);
  9881. ConvertProgram;
  9882. CheckSource('TestRecord_VarExternal',
  9883. LinesToStr([ // statements
  9884. 'rtl.recNewT($mod, "TRecA", function () {',
  9885. ' this.i = 0;',
  9886. ' this.$eq = function (b) {',
  9887. ' return (this.i === b.i) && (this.length === b.length);',
  9888. ' };',
  9889. ' this.$assign = function (s) {',
  9890. ' this.i = s.i;',
  9891. ' this.length = s.length;',
  9892. ' return this;',
  9893. ' };',
  9894. '});',
  9895. 'this.Rec = $mod.TRecA.$new();',
  9896. '']),
  9897. LinesToStr([ // $mod.$main
  9898. '$mod.Rec.length = $mod.Rec.length;'
  9899. ]));
  9900. end;
  9901. procedure TTestModule.TestRecord_WithDo;
  9902. begin
  9903. StartProgram(false);
  9904. Add('type');
  9905. Add(' TRec = record');
  9906. Add(' vI: longint;');
  9907. Add(' end;');
  9908. Add('var');
  9909. Add(' Int: longint;');
  9910. Add(' r: TRec;');
  9911. Add('begin');
  9912. Add(' with r do');
  9913. Add(' int:=vi;');
  9914. Add(' with r do begin');
  9915. Add(' int:=vi;');
  9916. Add(' vi:=int;');
  9917. Add(' end;');
  9918. ConvertProgram;
  9919. CheckSource('TestWithRecordDo',
  9920. LinesToStr([ // statements
  9921. 'rtl.recNewT($mod, "TRec", function () {',
  9922. ' this.vI = 0;',
  9923. ' this.$eq = function (b) {',
  9924. ' return this.vI === b.vI;',
  9925. ' };',
  9926. ' this.$assign = function (s) {',
  9927. ' this.vI = s.vI;',
  9928. ' return this;',
  9929. ' };',
  9930. '});',
  9931. 'this.Int = 0;',
  9932. 'this.r = $mod.TRec.$new();',
  9933. '']),
  9934. LinesToStr([ // $mod.$main
  9935. 'var $with1 = $mod.r;',
  9936. '$mod.Int = $with1.vI;',
  9937. 'var $with2 = $mod.r;',
  9938. '$mod.Int = $with2.vI;',
  9939. '$with2.vI = $mod.Int;'
  9940. ]));
  9941. end;
  9942. procedure TTestModule.TestRecord_Assign;
  9943. begin
  9944. StartProgram(false);
  9945. Add('type');
  9946. Add(' TEnum = (red,green);');
  9947. Add(' TEnums = set of TEnum;');
  9948. Add(' TSmallRec = record');
  9949. Add(' N: longint;');
  9950. Add(' end;');
  9951. Add(' TBigRec = record');
  9952. Add(' Int: longint;');
  9953. Add(' D: double;');
  9954. Add(' Arr: array of longint;');
  9955. Add(' Arr2: array[1..2] of longint;');
  9956. Add(' Small: TSmallRec;');
  9957. Add(' Enums: TEnums;');
  9958. Add(' end;');
  9959. Add('var');
  9960. Add(' r, s: TBigRec;');
  9961. Add('begin');
  9962. Add(' r:=s;');
  9963. Add(' r:=default(TBigRec);');
  9964. Add(' r:=default(s);');
  9965. ConvertProgram;
  9966. CheckSource('TestRecord_Assign',
  9967. LinesToStr([ // statements
  9968. 'this.TEnum = {',
  9969. ' "0": "red",',
  9970. ' red: 0,',
  9971. ' "1": "green",',
  9972. ' green: 1',
  9973. '};',
  9974. 'rtl.recNewT($mod, "TSmallRec", function () {',
  9975. ' this.N = 0;',
  9976. ' this.$eq = function (b) {',
  9977. ' return this.N === b.N;',
  9978. ' };',
  9979. ' this.$assign = function (s) {',
  9980. ' this.N = s.N;',
  9981. ' return this;',
  9982. ' };',
  9983. '});',
  9984. 'rtl.recNewT($mod, "TBigRec", function () {',
  9985. ' this.Int = 0;',
  9986. ' this.D = 0.0;',
  9987. ' this.Arr = [];',
  9988. ' this.$new = function () {',
  9989. ' var r = Object.create(this);',
  9990. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  9991. ' r.Small = $mod.TSmallRec.$new();',
  9992. ' r.Enums = {};',
  9993. ' return r;',
  9994. ' };',
  9995. ' this.$eq = function (b) {',
  9996. ' 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);',
  9997. ' };',
  9998. ' this.$assign = function (s) {',
  9999. ' this.Int = s.Int;',
  10000. ' this.D = s.D;',
  10001. ' this.Arr = s.Arr;',
  10002. ' this.Arr2 = s.Arr2.slice(0);',
  10003. ' this.Small.$assign(s.Small);',
  10004. ' this.Enums = rtl.refSet(s.Enums);',
  10005. ' return this;',
  10006. ' };',
  10007. '});',
  10008. 'this.r = $mod.TBigRec.$new();',
  10009. 'this.s = $mod.TBigRec.$new();',
  10010. '']),
  10011. LinesToStr([ // $mod.$main
  10012. '$mod.r.$assign($mod.s);',
  10013. '$mod.r.$assign($mod.TBigRec.$new());',
  10014. '$mod.r.$assign($mod.TBigRec.$new());',
  10015. '']));
  10016. end;
  10017. procedure TTestModule.TestRecord_AsParams;
  10018. begin
  10019. StartProgram(false);
  10020. Add([
  10021. 'type',
  10022. ' integer = longint;',
  10023. ' TRecord = record',
  10024. ' i: integer;',
  10025. ' end;',
  10026. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10027. 'var vL: TRecord;',
  10028. 'begin',
  10029. ' vd:=vd;',
  10030. ' vd.i:=vd.i;',
  10031. ' vl:=vc;',
  10032. ' vv:=vv;',
  10033. ' vv.i:=vv.i;',
  10034. ' U:=vl;',
  10035. ' U:=vd;',
  10036. ' U:=vc;',
  10037. ' U:=vv;',
  10038. ' doit(vd,vd,vd,vd);',
  10039. ' doit(vc,vc,vl,vl);',
  10040. ' doit(vv,vv,vv,vv);',
  10041. ' doit(vl,vl,vl,vl);',
  10042. 'end;',
  10043. 'var i: TRecord;',
  10044. 'begin',
  10045. ' doit(i,i,i,i);']);
  10046. ConvertProgram;
  10047. CheckSource('TestRecord_AsParams',
  10048. LinesToStr([ // statements
  10049. 'rtl.recNewT($mod, "TRecord", function () {',
  10050. ' this.i = 0;',
  10051. ' this.$eq = function (b) {',
  10052. ' return this.i === b.i;',
  10053. ' };',
  10054. ' this.$assign = function (s) {',
  10055. ' this.i = s.i;',
  10056. ' return this;',
  10057. ' };',
  10058. '});',
  10059. 'this.DoIt = function (vD, vC, vV, U) {',
  10060. ' var vL = $mod.TRecord.$new();',
  10061. ' vD.$assign(vD);',
  10062. ' vD.i = vD.i;',
  10063. ' vL.$assign(vC);',
  10064. ' vV.$assign(vV);',
  10065. ' vV.i = vV.i;',
  10066. ' U.set(vL);',
  10067. ' U.set(vD);',
  10068. ' U.set(vC);',
  10069. ' U.set(vV);',
  10070. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, {',
  10071. ' get: function () {',
  10072. ' return vD;',
  10073. ' },',
  10074. ' set: function (v) {',
  10075. ' vD.$assign(v);',
  10076. ' }',
  10077. ' });',
  10078. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, {',
  10079. ' get: function () {',
  10080. ' return vL;',
  10081. ' },',
  10082. ' set: function (v) {',
  10083. ' vL.$assign(v);',
  10084. ' }',
  10085. ' });',
  10086. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, {',
  10087. ' get: function () {',
  10088. ' return vV;',
  10089. ' },',
  10090. ' set: function (v) {',
  10091. ' vV.$assign(v);',
  10092. ' }',
  10093. ' });',
  10094. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, {',
  10095. ' get: function () {',
  10096. ' return vL;',
  10097. ' },',
  10098. ' set: function (v) {',
  10099. ' vL.$assign(v);',
  10100. ' }',
  10101. ' });',
  10102. '};',
  10103. 'this.i = $mod.TRecord.$new();'
  10104. ]),
  10105. LinesToStr([
  10106. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, {',
  10107. ' p: $mod,',
  10108. ' get: function () {',
  10109. ' return this.p.i;',
  10110. ' },',
  10111. ' set: function (v) {',
  10112. ' this.p.i.$assign(v);',
  10113. ' }',
  10114. '});',
  10115. '']));
  10116. end;
  10117. procedure TTestModule.TestRecord_ConstRef;
  10118. begin
  10119. StartProgram(false);
  10120. Add([
  10121. 'type TRec = record i: word; end;',
  10122. 'procedure Run(constref a: TRec);',
  10123. 'begin',
  10124. 'end;',
  10125. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10126. 'var l: TRec;',
  10127. 'begin',
  10128. ' Run(l);',
  10129. ' Run(a);',
  10130. ' Run(b);',
  10131. ' Run(c);',
  10132. ' Run(d);',
  10133. ' Run(e);',
  10134. 'end;',
  10135. 'begin',
  10136. '']);
  10137. ConvertProgram;
  10138. CheckResolverUnexpectedHints();
  10139. CheckSource('TestRecord_ConstRef',
  10140. LinesToStr([ // statements
  10141. 'rtl.recNewT($mod, "TRec", function () {',
  10142. ' this.i = 0;',
  10143. ' this.$eq = function (b) {',
  10144. ' return this.i === b.i;',
  10145. ' };',
  10146. ' this.$assign = function (s) {',
  10147. ' this.i = s.i;',
  10148. ' return this;',
  10149. ' };',
  10150. '});',
  10151. 'this.Run = function (a) {',
  10152. '};',
  10153. 'this.Fly = function (a, b, c, d, e) {',
  10154. ' var l = $mod.TRec.$new();',
  10155. ' $mod.Run(l);',
  10156. ' $mod.Run(a);',
  10157. ' $mod.Run(b);',
  10158. ' $mod.Run(c);',
  10159. ' $mod.Run(d);',
  10160. ' $mod.Run(e);',
  10161. '};',
  10162. '']),
  10163. LinesToStr([
  10164. '']));
  10165. end;
  10166. procedure TTestModule.TestRecordElement_AsParams;
  10167. begin
  10168. StartProgram(false);
  10169. Add('type');
  10170. Add(' integer = longint;');
  10171. Add(' TRecord = record');
  10172. Add(' i: integer;');
  10173. Add(' end;');
  10174. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10175. Add('var vJ: TRecord;');
  10176. Add('begin');
  10177. Add(' doit(vj.i,vj.i,vj.i);');
  10178. Add('end;');
  10179. Add('var r: TRecord;');
  10180. Add('begin');
  10181. Add(' doit(r.i,r.i,r.i);');
  10182. ConvertProgram;
  10183. CheckSource('TestRecordElement_AsParams',
  10184. LinesToStr([ // statements
  10185. 'rtl.recNewT($mod, "TRecord", function () {',
  10186. ' this.i = 0;',
  10187. ' this.$eq = function (b) {',
  10188. ' return this.i === b.i;',
  10189. ' };',
  10190. ' this.$assign = function (s) {',
  10191. ' this.i = s.i;',
  10192. ' return this;',
  10193. ' };',
  10194. '});',
  10195. 'this.DoIt = function (vG,vH,vI) {',
  10196. ' var vJ = $mod.TRecord.$new();',
  10197. ' $mod.DoIt(vJ.i, vJ.i, {',
  10198. ' p: vJ,',
  10199. ' get: function () {',
  10200. ' return this.p.i;',
  10201. ' },',
  10202. ' set: function (v) {',
  10203. ' this.p.i = v;',
  10204. ' }',
  10205. ' });',
  10206. '};',
  10207. 'this.r = $mod.TRecord.$new();'
  10208. ]),
  10209. LinesToStr([
  10210. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10211. ' p: $mod.r,',
  10212. ' get: function () {',
  10213. ' return this.p.i;',
  10214. ' },',
  10215. ' set: function (v) {',
  10216. ' this.p.i = v;',
  10217. ' }',
  10218. '});'
  10219. ]));
  10220. end;
  10221. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10222. begin
  10223. StartProgram(false);
  10224. Add('type');
  10225. Add(' integer = longint;');
  10226. Add(' TRecord = record');
  10227. Add(' i: integer;');
  10228. Add(' end;');
  10229. Add('function GetRec(vB: integer = 0): TRecord;');
  10230. Add('begin');
  10231. Add('end;');
  10232. Add('procedure DoIt(vG: integer; const vH: integer);');
  10233. Add('begin');
  10234. Add('end;');
  10235. Add('begin');
  10236. Add(' doit(getrec.i,getrec.i);');
  10237. Add(' doit(getrec().i,getrec().i);');
  10238. Add(' doit(getrec(1).i,getrec(2).i);');
  10239. ConvertProgram;
  10240. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10241. LinesToStr([ // statements
  10242. 'rtl.recNewT($mod, "TRecord", function () {',
  10243. ' this.i = 0;',
  10244. ' this.$eq = function (b) {',
  10245. ' return this.i === b.i;',
  10246. ' };',
  10247. ' this.$assign = function (s) {',
  10248. ' this.i = s.i;',
  10249. ' return this;',
  10250. ' };',
  10251. '});',
  10252. 'this.GetRec = function (vB) {',
  10253. ' var Result = $mod.TRecord.$new();',
  10254. ' return Result;',
  10255. '};',
  10256. 'this.DoIt = function (vG, vH) {',
  10257. '};',
  10258. '']),
  10259. LinesToStr([
  10260. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10261. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10262. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10263. '']));
  10264. end;
  10265. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10266. begin
  10267. StartProgram(false);
  10268. Add('type');
  10269. Add(' integer = longint;');
  10270. Add(' TRecord = record');
  10271. Add(' i: integer;');
  10272. Add(' end;');
  10273. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10274. Add('begin');
  10275. Add('end;');
  10276. Add('var r: trecord;');
  10277. Add('begin');
  10278. Add(' with r do ');
  10279. Add(' doit(i,i,i);');
  10280. ConvertProgram;
  10281. CheckSource('TestRecordElementFromWith_AsParams',
  10282. LinesToStr([ // statements
  10283. 'rtl.recNewT($mod, "TRecord", function () {',
  10284. ' this.i = 0;',
  10285. ' this.$eq = function (b) {',
  10286. ' return this.i === b.i;',
  10287. ' };',
  10288. ' this.$assign = function (s) {',
  10289. ' this.i = s.i;',
  10290. ' return this;',
  10291. ' };',
  10292. '});',
  10293. 'this.DoIt = function (vG,vH,vI) {',
  10294. '};',
  10295. 'this.r = $mod.TRecord.$new();'
  10296. ]),
  10297. LinesToStr([
  10298. 'var $with1 = $mod.r;',
  10299. '$mod.DoIt($with1.i,$with1.i,{',
  10300. ' p: $with1,',
  10301. ' get: function () {',
  10302. ' return this.p.i;',
  10303. ' },',
  10304. ' set: function (v) {',
  10305. ' this.p.i = v;',
  10306. ' }',
  10307. '});',
  10308. '']));
  10309. end;
  10310. procedure TTestModule.TestRecord_Equal;
  10311. begin
  10312. StartProgram(false);
  10313. Add('type');
  10314. Add(' integer = longint;');
  10315. Add(' TFlag = (red,blue);');
  10316. Add(' TFlags = set of TFlag;');
  10317. Add(' TProc = procedure;');
  10318. Add(' TRecord = record');
  10319. Add(' i: integer;');
  10320. Add(' Event: TProc;');
  10321. Add(' f: TFlags;');
  10322. Add(' end;');
  10323. Add(' TNested = record');
  10324. Add(' r: TRecord;');
  10325. Add(' end;');
  10326. Add('var');
  10327. Add(' b: boolean;');
  10328. Add(' r,s: trecord;');
  10329. Add('begin');
  10330. Add(' b:=r=s;');
  10331. Add(' b:=r<>s;');
  10332. ConvertProgram;
  10333. CheckSource('TestRecord_Equal',
  10334. LinesToStr([ // statements
  10335. 'this.TFlag = {',
  10336. ' "0": "red",',
  10337. ' red: 0,',
  10338. ' "1": "blue",',
  10339. ' blue: 1',
  10340. '};',
  10341. 'rtl.recNewT($mod, "TRecord", function () {',
  10342. ' this.i = 0;',
  10343. ' this.Event = null;',
  10344. ' this.$new = function () {',
  10345. ' var r = Object.create(this);',
  10346. ' r.f = {};',
  10347. ' return r;',
  10348. ' };',
  10349. ' this.$eq = function (b) {',
  10350. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10351. ' };',
  10352. ' this.$assign = function (s) {',
  10353. ' this.i = s.i;',
  10354. ' this.Event = s.Event;',
  10355. ' this.f = rtl.refSet(s.f);',
  10356. ' return this;',
  10357. ' };',
  10358. '});',
  10359. 'rtl.recNewT($mod, "TNested", function () {',
  10360. ' this.$new = function () {',
  10361. ' var r = Object.create(this);',
  10362. ' r.r = $mod.TRecord.$new();',
  10363. ' return r;',
  10364. ' };',
  10365. ' this.$eq = function (b) {',
  10366. ' return this.r.$eq(b.r);',
  10367. ' };',
  10368. ' this.$assign = function (s) {',
  10369. ' this.r.$assign(s.r);',
  10370. ' return this;',
  10371. ' };',
  10372. '});',
  10373. 'this.b = false;',
  10374. 'this.r = $mod.TRecord.$new();',
  10375. 'this.s = $mod.TRecord.$new();',
  10376. '']),
  10377. LinesToStr([
  10378. '$mod.b = $mod.r.$eq($mod.s);',
  10379. '$mod.b = !$mod.r.$eq($mod.s);',
  10380. '']));
  10381. end;
  10382. procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
  10383. begin
  10384. StartProgram(false);
  10385. Add('type');
  10386. Add(' TRecord = record');
  10387. Add(' i: longint;');
  10388. Add(' end;');
  10389. Add('var');
  10390. Add(' Jv: jsvalue;');
  10391. Add(' Rec: trecord;');
  10392. Add('begin');
  10393. Add(' rec:=trecord(jv);');
  10394. ConvertProgram;
  10395. CheckSource('TestRecord_TypeCastJSValueToRecord',
  10396. LinesToStr([ // statements
  10397. 'rtl.recNewT($mod, "TRecord", function () {',
  10398. ' this.i = 0;',
  10399. ' this.$eq = function (b) {',
  10400. ' return this.i === b.i;',
  10401. ' };',
  10402. ' this.$assign = function (s) {',
  10403. ' this.i = s.i;',
  10404. ' return this;',
  10405. ' };',
  10406. '});',
  10407. 'this.Jv = undefined;',
  10408. 'this.Rec = $mod.TRecord.$new();',
  10409. '']),
  10410. LinesToStr([
  10411. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10412. '']));
  10413. end;
  10414. procedure TTestModule.TestRecord_VariantFail;
  10415. begin
  10416. StartProgram(false);
  10417. Add([
  10418. 'type',
  10419. ' TRec = record',
  10420. ' case word of',
  10421. ' 0: (b0, b1: Byte);',
  10422. ' 1: (i: word);',
  10423. ' end;',
  10424. 'begin']);
  10425. SetExpectedPasResolverError('variant record is not supported',
  10426. nXIsNotSupported);
  10427. ConvertProgram;
  10428. end;
  10429. procedure TTestModule.TestRecord_FieldArray;
  10430. begin
  10431. StartProgram(false);
  10432. Add([
  10433. 'type',
  10434. ' TArrInt = array[3..4] of longint;',
  10435. ' TArrArrInt = array[3..4] of longint;',
  10436. ' TRec = record',
  10437. ' a: array of longint;',
  10438. ' s: array[1..2] of longint;',
  10439. ' m: array[1..2,3..4] of longint;',
  10440. ' o: TArrArrInt;',
  10441. ' end;',
  10442. 'begin']);
  10443. ConvertProgram;
  10444. CheckSource('TestRecord_FieldArray',
  10445. LinesToStr([ // statements
  10446. 'rtl.recNewT($mod, "TRec", function () {',
  10447. ' this.a = [];',
  10448. ' this.$new = function () {',
  10449. ' var r = Object.create(this);',
  10450. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10451. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10452. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10453. ' return r;',
  10454. ' };',
  10455. ' this.$eq = function (b) {',
  10456. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10457. ' };',
  10458. ' this.$assign = function (s) {',
  10459. ' this.a = s.a;',
  10460. ' this.s = s.s.slice(0);',
  10461. ' this.m = s.m.slice(0);',
  10462. ' this.o = s.o.slice(0);',
  10463. ' return this;',
  10464. ' };',
  10465. '});',
  10466. '']),
  10467. LinesToStr([ // $mod.$main
  10468. '']));
  10469. end;
  10470. procedure TTestModule.TestRecord_Const;
  10471. begin
  10472. StartProgram(false);
  10473. Add([
  10474. 'type',
  10475. ' TArrInt = array[3..4] of longint;',
  10476. ' TPoint = record x,y: longint; end;',
  10477. ' TRec = record',
  10478. ' i: longint;',
  10479. ' a: array of longint;',
  10480. ' s: array[1..2] of longint;',
  10481. ' m: array[1..2,3..4] of longint;',
  10482. ' p: TPoint;',
  10483. ' end;',
  10484. ' TPoints = array of TPoint;',
  10485. 'const',
  10486. ' r: TRec = (',
  10487. ' i:1;',
  10488. ' a:(2,3);',
  10489. ' s:(4,5);',
  10490. ' m:( (11,12), (13,14) );',
  10491. ' p: (x:21; y:22)',
  10492. ' );',
  10493. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10494. 'begin']);
  10495. ConvertProgram;
  10496. CheckSource('TestRecord_Const',
  10497. LinesToStr([ // statements
  10498. 'rtl.recNewT($mod, "TPoint", function () {',
  10499. ' this.x = 0;',
  10500. ' this.y = 0;',
  10501. ' this.$eq = function (b) {',
  10502. ' return (this.x === b.x) && (this.y === b.y);',
  10503. ' };',
  10504. ' this.$assign = function (s) {',
  10505. ' this.x = s.x;',
  10506. ' this.y = s.y;',
  10507. ' return this;',
  10508. ' };',
  10509. '});',
  10510. 'rtl.recNewT($mod, "TRec", function () {',
  10511. ' this.i = 0;',
  10512. ' this.a = [];',
  10513. ' this.$new = function () {',
  10514. ' var r = Object.create(this);',
  10515. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10516. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10517. ' r.p = $mod.TPoint.$new();',
  10518. ' return r;',
  10519. ' };',
  10520. ' this.$eq = function (b) {',
  10521. ' 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);',
  10522. ' };',
  10523. ' this.$assign = function (s) {',
  10524. ' this.i = s.i;',
  10525. ' this.a = s.a;',
  10526. ' this.s = s.s.slice(0);',
  10527. ' this.m = s.m.slice(0);',
  10528. ' this.p.$assign(s.p);',
  10529. ' return this;',
  10530. ' };',
  10531. '});',
  10532. 'this.r = $mod.TRec.$clone({',
  10533. ' i: 1,',
  10534. ' a: [2, 3],',
  10535. ' s: [4, 5],',
  10536. ' m: [[11, 12], [13, 14]],',
  10537. ' p: $mod.TPoint.$clone({',
  10538. ' x: 21,',
  10539. ' y: 22',
  10540. ' })',
  10541. '});',
  10542. 'this.p = [$mod.TPoint.$clone({',
  10543. ' x: 1,',
  10544. ' y: 2',
  10545. '}), $mod.TPoint.$clone({',
  10546. ' x: 3,',
  10547. ' y: 4',
  10548. '})];',
  10549. '']),
  10550. LinesToStr([ // $mod.$main
  10551. '']));
  10552. end;
  10553. procedure TTestModule.TestRecord_TypecastFail;
  10554. begin
  10555. StartProgram(false);
  10556. Add([
  10557. 'type',
  10558. ' TPoint = record x,y: longint; end;',
  10559. ' TRec = record l: longint end;',
  10560. 'var p: TPoint;',
  10561. 'begin',
  10562. ' if TRec(p).l=2 then ;']);
  10563. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10564. nIllegalTypeConversionTo);
  10565. ConvertProgram;
  10566. end;
  10567. procedure TTestModule.TestRecord_InFunction;
  10568. begin
  10569. StartProgram(false);
  10570. Add([
  10571. 'var TPoint: longint = 3;',
  10572. 'procedure DoIt;',
  10573. 'type',
  10574. ' TPoint = record x,y: longint; end;',
  10575. ' TPoints = array of TPoint;',
  10576. 'var',
  10577. ' r: TPoint;',
  10578. ' p: TPoints;',
  10579. 'begin',
  10580. ' SetLength(p,2);',
  10581. 'end;',
  10582. 'begin']);
  10583. ConvertProgram;
  10584. CheckSource('TestRecord_InFunction',
  10585. LinesToStr([ // statements
  10586. 'this.TPoint = 3;',
  10587. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10588. ' this.x = 0;',
  10589. ' this.y = 0;',
  10590. ' this.$eq = function (b) {',
  10591. ' return (this.x === b.x) && (this.y === b.y);',
  10592. ' };',
  10593. ' this.$assign = function (s) {',
  10594. ' this.x = s.x;',
  10595. ' this.y = s.y;',
  10596. ' return this;',
  10597. ' };',
  10598. '});',
  10599. 'this.DoIt = function () {',
  10600. ' var r = TPoint$1.$new();',
  10601. ' var p = [];',
  10602. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10603. '};',
  10604. '']),
  10605. LinesToStr([ // $mod.$main
  10606. '']));
  10607. end;
  10608. procedure TTestModule.TestRecord_AnonymousFail;
  10609. begin
  10610. StartProgram(false);
  10611. Add([
  10612. 'var',
  10613. ' r: record x: word end;',
  10614. 'begin']);
  10615. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] anonymous record type',
  10616. nNotYetImplemented);
  10617. ConvertProgram;
  10618. end;
  10619. procedure TTestModule.TestAdvRecord_Function;
  10620. begin
  10621. StartProgram(false);
  10622. Parser.Options:=Parser.Options+[po_cassignments];
  10623. Add([
  10624. '{$modeswitch AdvancedRecords}',
  10625. 'type',
  10626. ' TPoint = record',
  10627. ' x,y: word;',
  10628. ' function Add(const apt: TPoint): TPoint;',
  10629. ' end;',
  10630. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10631. 'begin',
  10632. ' Result:=Self;',
  10633. ' Result.x+=apt.x;',
  10634. ' Result.y:=Result.y+apt.y;',
  10635. ' Self:=apt;',
  10636. 'end;',
  10637. 'var p,q: TPoint;',
  10638. 'begin',
  10639. ' p.add(q);',
  10640. ' p:=default(TPoint);',
  10641. ' p:=q;',
  10642. '']);
  10643. ConvertProgram;
  10644. CheckSource('TestAdvRecord_Function',
  10645. LinesToStr([ // statements
  10646. 'rtl.recNewT($mod, "TPoint", function () {',
  10647. ' this.x = 0;',
  10648. ' this.y = 0;',
  10649. ' this.$eq = function (b) {',
  10650. ' return (this.x === b.x) && (this.y === b.y);',
  10651. ' };',
  10652. ' this.$assign = function (s) {',
  10653. ' this.x = s.x;',
  10654. ' this.y = s.y;',
  10655. ' return this;',
  10656. ' };',
  10657. ' this.Add = function (apt) {',
  10658. ' var Result = $mod.TPoint.$new();',
  10659. ' Result.$assign(this);',
  10660. ' Result.x += apt.x;',
  10661. ' Result.y = Result.y + apt.y;',
  10662. ' this.$assign(apt);',
  10663. ' return Result;',
  10664. ' };',
  10665. '});',
  10666. 'this.p = $mod.TPoint.$new();',
  10667. 'this.q = $mod.TPoint.$new();',
  10668. '']),
  10669. LinesToStr([ // $mod.$main
  10670. '$mod.p.Add($mod.q);',
  10671. '$mod.p.$assign($mod.TPoint.$new());',
  10672. '$mod.p.$assign($mod.q);',
  10673. '']));
  10674. end;
  10675. procedure TTestModule.TestAdvRecord_Property;
  10676. begin
  10677. StartProgram(false);
  10678. Add([
  10679. '{$modeswitch AdvancedRecords}',
  10680. 'type',
  10681. ' TPoint = record',
  10682. ' x,y: word;',
  10683. ' strict private',
  10684. ' function GetSize: longword;',
  10685. ' procedure SetSize(Value: longword);',
  10686. ' public',
  10687. ' property Size: longword read GetSize write SetSize;',
  10688. ' property Left: word read x write y;',
  10689. ' end;',
  10690. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  10691. 'function TPoint.GetSize: longword;',
  10692. 'begin',
  10693. ' x:=y;',
  10694. ' Size:=Size;',
  10695. ' Left:=Left;',
  10696. 'end;',
  10697. 'procedure TPoint.SetSize(Value: longword);',
  10698. 'begin',
  10699. 'end;',
  10700. 'var p,q: TPoint;',
  10701. 'begin',
  10702. ' p.Size:=q.Size;',
  10703. ' p.Left:=q.Left;',
  10704. '']);
  10705. ConvertProgram;
  10706. CheckSource('TestAdvRecord_Property',
  10707. LinesToStr([ // statements
  10708. 'rtl.recNewT($mod, "TPoint", function () {',
  10709. ' this.x = 0;',
  10710. ' this.y = 0;',
  10711. ' this.$eq = function (b) {',
  10712. ' return (this.x === b.x) && (this.y === b.y);',
  10713. ' };',
  10714. ' this.$assign = function (s) {',
  10715. ' this.x = s.x;',
  10716. ' this.y = s.y;',
  10717. ' return this;',
  10718. ' };',
  10719. ' this.GetSize = function () {',
  10720. ' var Result = 0;',
  10721. ' this.x = this.y;',
  10722. ' this.SetSize(this.GetSize());',
  10723. ' this.y = this.x;',
  10724. ' return Result;',
  10725. ' };',
  10726. ' this.SetSize = function (Value) {',
  10727. ' };',
  10728. '});',
  10729. 'this.SetSize = function (Value) {',
  10730. '};',
  10731. 'this.p = $mod.TPoint.$new();',
  10732. 'this.q = $mod.TPoint.$new();',
  10733. '']),
  10734. LinesToStr([ // $mod.$main
  10735. '$mod.p.SetSize($mod.q.GetSize());',
  10736. '$mod.p.y = $mod.q.x;',
  10737. '']));
  10738. end;
  10739. procedure TTestModule.TestAdvRecord_PropertyDefault;
  10740. begin
  10741. StartProgram(false);
  10742. Add([
  10743. '{$modeswitch AdvancedRecords}',
  10744. 'type',
  10745. ' TPoint = record',
  10746. ' strict private',
  10747. ' function GetItems(Index: word): word;',
  10748. ' procedure SetItems(Index: word; Value: word);',
  10749. ' public',
  10750. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  10751. ' end;',
  10752. 'function TPoint.GetItems(Index: word): word;',
  10753. 'begin',
  10754. ' Items[index]:=Items[index];',
  10755. ' self.Items[index]:=self.Items[index];',
  10756. 'end;',
  10757. 'procedure TPoint.SetItems(Index: word; Value: word);',
  10758. 'begin',
  10759. 'end;',
  10760. 'var p: TPoint;',
  10761. 'begin',
  10762. ' p[1]:=p[2];',
  10763. ' p.Items[3]:=p.Items[4];',
  10764. '']);
  10765. ConvertProgram;
  10766. CheckSource('TestAdvRecord_PropertyDefault',
  10767. LinesToStr([ // statements
  10768. 'rtl.recNewT($mod, "TPoint", function () {',
  10769. ' this.$eq = function (b) {',
  10770. ' return true;',
  10771. ' };',
  10772. ' this.$assign = function (s) {',
  10773. ' return this;',
  10774. ' };',
  10775. ' this.GetItems = function (Index) {',
  10776. ' var Result = 0;',
  10777. ' this.SetItems(Index, this.GetItems(Index));',
  10778. ' this.SetItems(Index, this.GetItems(Index));',
  10779. ' return Result;',
  10780. ' };',
  10781. ' this.SetItems = function (Index, Value) {',
  10782. ' };',
  10783. '});',
  10784. 'this.p = $mod.TPoint.$new();',
  10785. '']),
  10786. LinesToStr([ // $mod.$main
  10787. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  10788. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  10789. '']));
  10790. end;
  10791. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  10792. begin
  10793. StartProgram(false);
  10794. Add([
  10795. '{$modeswitch AdvancedRecords}',
  10796. 'type',
  10797. ' TRec = record',
  10798. ' class var',
  10799. ' Fx: longint;',
  10800. ' Fy: longint;',
  10801. ' class function GetInt: longint; static;',
  10802. ' class procedure SetInt(Value: longint); static;',
  10803. ' class procedure DoIt; static;',
  10804. ' class property IntA: longint read Fx write Fy;',
  10805. ' class property IntB: longint read GetInt write SetInt;',
  10806. ' end;',
  10807. 'class function trec.getint: longint;',
  10808. 'begin',
  10809. ' result:=fx;',
  10810. 'end;',
  10811. 'class procedure trec.setint(value: longint);',
  10812. 'begin',
  10813. 'end;',
  10814. 'class procedure trec.doit;',
  10815. 'begin',
  10816. ' IntA:=IntA+1;',
  10817. ' IntB:=IntB+1;',
  10818. 'end;',
  10819. 'var r: trec;',
  10820. 'begin',
  10821. ' trec.inta:=trec.inta+1;',
  10822. ' if trec.intb=2 then;',
  10823. ' trec.intb:=trec.intb+2;',
  10824. ' trec.setint(trec.inta);',
  10825. ' r.inta:=r.inta+1;',
  10826. ' if r.intb=2 then;',
  10827. ' r.intb:=r.intb+2;',
  10828. ' r.setint(r.inta);']);
  10829. ConvertProgram;
  10830. CheckSource('TestAdvRecord_Property_ClassMethod',
  10831. LinesToStr([ // statements
  10832. 'rtl.recNewT($mod, "TRec", function () {',
  10833. ' this.Fx = 0;',
  10834. ' this.Fy = 0;',
  10835. ' this.$eq = function (b) {',
  10836. ' return true;',
  10837. ' };',
  10838. ' this.$assign = function (s) {',
  10839. ' return this;',
  10840. ' };',
  10841. ' this.GetInt = function () {',
  10842. ' var Result = 0;',
  10843. ' Result = this.Fx;',
  10844. ' return Result;',
  10845. ' };',
  10846. ' this.SetInt = function (Value) {',
  10847. ' };',
  10848. ' this.DoIt = function () {',
  10849. ' $mod.TRec.Fy = this.Fx + 1;',
  10850. ' this.SetInt(this.GetInt() + 1);',
  10851. ' };',
  10852. '}, true);',
  10853. 'this.r = $mod.TRec.$new();',
  10854. '']),
  10855. LinesToStr([ // $mod.$main
  10856. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10857. 'if ($mod.TRec.GetInt() === 2) ;',
  10858. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  10859. '$mod.TRec.SetInt($mod.TRec.Fx);',
  10860. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  10861. 'if ($mod.r.GetInt() === 2) ;',
  10862. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  10863. '$mod.r.SetInt($mod.r.Fx);',
  10864. '']));
  10865. end;
  10866. procedure TTestModule.TestAdvRecord_Const;
  10867. begin
  10868. StartProgram(false);
  10869. Add([
  10870. '{$modeswitch AdvancedRecords}',
  10871. 'type',
  10872. ' TArrInt = array[3..4] of longint;',
  10873. ' TPoint = record',
  10874. ' x,y: longint;',
  10875. ' class var Count: nativeint;',
  10876. ' end;',
  10877. ' TRec = record',
  10878. ' i: longint;',
  10879. ' a: array of longint;',
  10880. ' s: array[1..2] of longint;',
  10881. ' m: array[1..2,3..4] of longint;',
  10882. ' p: TPoint;',
  10883. ' end;',
  10884. ' TPoints = array of TPoint;',
  10885. 'const',
  10886. ' r: TRec = (',
  10887. ' i:1;',
  10888. ' a:(2,3);',
  10889. ' s:(4,5);',
  10890. ' m:( (11,12), (13,14) );',
  10891. ' p: (x:21)',
  10892. ' );',
  10893. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10894. 'begin']);
  10895. ConvertProgram;
  10896. CheckSource('TestAdvRecord_Const',
  10897. LinesToStr([ // statements
  10898. 'rtl.recNewT($mod, "TPoint", function () {',
  10899. ' this.x = 0;',
  10900. ' this.y = 0;',
  10901. ' this.Count = 0;',
  10902. ' this.$eq = function (b) {',
  10903. ' return (this.x === b.x) && (this.y === b.y);',
  10904. ' };',
  10905. ' this.$assign = function (s) {',
  10906. ' this.x = s.x;',
  10907. ' this.y = s.y;',
  10908. ' return this;',
  10909. ' };',
  10910. '}, true);',
  10911. 'rtl.recNewT($mod, "TRec", function () {',
  10912. ' this.i = 0;',
  10913. ' this.a = [];',
  10914. ' this.$new = function () {',
  10915. ' var r = Object.create(this);',
  10916. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10917. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10918. ' r.p = $mod.TPoint.$new();',
  10919. ' return r;',
  10920. ' };',
  10921. ' this.$eq = function (b) {',
  10922. ' 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);',
  10923. ' };',
  10924. ' this.$assign = function (s) {',
  10925. ' this.i = s.i;',
  10926. ' this.a = s.a;',
  10927. ' this.s = s.s.slice(0);',
  10928. ' this.m = s.m.slice(0);',
  10929. ' this.p.$assign(s.p);',
  10930. ' return this;',
  10931. ' };',
  10932. '});',
  10933. 'this.r = $mod.TRec.$clone({',
  10934. ' i: 1,',
  10935. ' a: [2, 3],',
  10936. ' s: [4, 5],',
  10937. ' m: [[11, 12], [13, 14]],',
  10938. ' p: $mod.TPoint.$clone({',
  10939. ' x: 21,',
  10940. ' y: 0',
  10941. ' })',
  10942. '});',
  10943. 'this.p = [$mod.TPoint.$clone({',
  10944. ' x: 1,',
  10945. ' y: 2',
  10946. '}), $mod.TPoint.$clone({',
  10947. ' x: 3,',
  10948. ' y: 4',
  10949. '})];',
  10950. '']),
  10951. LinesToStr([ // $mod.$main
  10952. '']));
  10953. end;
  10954. procedure TTestModule.TestAdvRecord_ExternalField;
  10955. begin
  10956. StartProgram(false);
  10957. Add([
  10958. '{$modeswitch AdvancedRecords}',
  10959. '{$modeswitch externalclass}',
  10960. 'type',
  10961. ' TCar = record',
  10962. ' public',
  10963. ' Intern: longint external name ''$Intern'';',
  10964. ' Intern2: longint external name ''$Intern2'';',
  10965. ' Bracket: longint external name ''["A B"]'';',
  10966. ' procedure DoIt;',
  10967. ' end;',
  10968. 'implementation',
  10969. 'procedure tcar.doit;',
  10970. 'begin',
  10971. ' Intern:=Intern+1;',
  10972. ' Intern2:=Intern2+2;',
  10973. ' Bracket:=Bracket+3;',
  10974. 'end;',
  10975. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  10976. 'begin',
  10977. ' Rec.intern:=Rec.intern+1;',
  10978. ' Rec.intern2:=Rec.intern2+2;',
  10979. ' Rec.Bracket:=Rec.Bracket+3;',
  10980. ' with Rec do begin',
  10981. ' intern:=intern+1;',
  10982. ' intern2:=intern2+2;',
  10983. ' Bracket:=Bracket+3;',
  10984. ' end;']);
  10985. ConvertProgram;
  10986. CheckSource('TestAdvRecord_ExternalField',
  10987. LinesToStr([ // statements
  10988. 'rtl.recNewT($mod, "TCar", function () {',
  10989. ' this.$eq = function (b) {',
  10990. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  10991. ' };',
  10992. ' this.$assign = function (s) {',
  10993. ' this.$Intern = s.$Intern;',
  10994. ' this.$Intern2 = s.$Intern2;',
  10995. ' this["A B"] = s["A B"];',
  10996. ' return this;',
  10997. ' };',
  10998. ' this.DoIt = function () {',
  10999. ' this.$Intern = this.$Intern + 1;',
  11000. ' this.$Intern2 = this.$Intern2 + 2;',
  11001. ' this["A B"] = this["A B"] + 3;',
  11002. ' };',
  11003. '});',
  11004. 'this.Rec = $mod.TCar.$clone({',
  11005. ' $Intern: 11,',
  11006. ' $Intern2: 12,',
  11007. ' "A B": 13',
  11008. '});',
  11009. '']),
  11010. LinesToStr([ // $mod.$main
  11011. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11012. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11013. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11014. 'var $with1 = $mod.Rec;',
  11015. '$with1.$Intern = $with1.$Intern + 1;',
  11016. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  11017. '$with1["A B"] = $with1["A B"] + 3;',
  11018. '']));
  11019. end;
  11020. procedure TTestModule.TestAdvRecord_SubRecord;
  11021. begin
  11022. StartProgram(false);
  11023. Add([
  11024. '{$modeswitch AdvancedRecords}',
  11025. 'type',
  11026. ' TRec = record',
  11027. ' type',
  11028. ' TPoint = record',
  11029. ' x,y: longint;',
  11030. ' class var Count: nativeint;',
  11031. ' procedure DoIt;',
  11032. ' class procedure DoThat; static;',
  11033. ' end;',
  11034. ' var',
  11035. ' i: longint;',
  11036. ' p: TPoint;',
  11037. ' procedure DoSome;',
  11038. ' end;',
  11039. 'const',
  11040. ' r: TRec = (',
  11041. ' i:1;',
  11042. ' p: (x:21;y:22)',
  11043. ' );',
  11044. 'procedure TRec.DoSome;',
  11045. 'begin',
  11046. ' p.x:=p.y+1;',
  11047. ' p.Count:=p.Count+2;',
  11048. 'end;',
  11049. 'procedure TRec.TPoint.DoIt;',
  11050. 'begin',
  11051. ' Count:=Count+3;',
  11052. 'end;',
  11053. 'class procedure TRec.TPoint.DoThat;',
  11054. 'begin',
  11055. ' Count:=Count+4;',
  11056. 'end;',
  11057. 'begin']);
  11058. ConvertProgram;
  11059. CheckSource('TestAdvRecord_SubRecord',
  11060. LinesToStr([ // statements
  11061. 'rtl.recNewT($mod, "TRec", function () {',
  11062. ' rtl.recNewT(this, "TPoint", function () {',
  11063. ' this.x = 0;',
  11064. ' this.y = 0;',
  11065. ' this.Count = 0;',
  11066. ' this.$eq = function (b) {',
  11067. ' return (this.x === b.x) && (this.y === b.y);',
  11068. ' };',
  11069. ' this.$assign = function (s) {',
  11070. ' this.x = s.x;',
  11071. ' this.y = s.y;',
  11072. ' return this;',
  11073. ' };',
  11074. ' this.DoIt = function () {',
  11075. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11076. ' };',
  11077. ' this.DoThat = function () {',
  11078. ' $mod.TRec.TPoint.Count = this.Count + 4;',
  11079. ' };',
  11080. ' }, true);',
  11081. ' this.i = 0;',
  11082. ' this.$new = function () {',
  11083. ' var r = Object.create(this);',
  11084. ' r.p = this.TPoint.$new();',
  11085. ' return r;',
  11086. ' };',
  11087. ' this.$eq = function (b) {',
  11088. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11089. ' };',
  11090. ' this.$assign = function (s) {',
  11091. ' this.i = s.i;',
  11092. ' this.p.$assign(s.p);',
  11093. ' return this;',
  11094. ' };',
  11095. ' this.DoSome = function () {',
  11096. ' this.p.x = this.p.y + 1;',
  11097. ' this.TPoint.Count = this.p.Count + 2;',
  11098. ' };',
  11099. '}, true);',
  11100. 'this.r = $mod.TRec.$clone({',
  11101. ' i: 1,',
  11102. ' p: $mod.TRec.TPoint.$clone({',
  11103. ' x: 21,',
  11104. ' y: 22',
  11105. ' })',
  11106. '});',
  11107. '']),
  11108. LinesToStr([ // $mod.$main
  11109. '']));
  11110. end;
  11111. procedure TTestModule.TestAdvRecord_SubClass;
  11112. begin
  11113. StartProgram(false);
  11114. Add([
  11115. '{$modeswitch AdvancedRecords}',
  11116. 'type',
  11117. ' TObject = class end;',
  11118. ' TPoint = record',
  11119. ' type',
  11120. ' TBird = class',
  11121. ' procedure DoIt;',
  11122. ' class procedure Glob;',
  11123. ' end;',
  11124. ' procedure DoIt(b: TBird);',
  11125. ' end;',
  11126. 'procedure TPoint.TBird.DoIt;',
  11127. 'begin',
  11128. ' doit;',
  11129. ' self.doit;',
  11130. ' glob;',
  11131. ' self.glob;',
  11132. 'end;',
  11133. 'class procedure TPoint.TBird.Glob;',
  11134. 'begin',
  11135. ' glob;',
  11136. ' self.glob;',
  11137. 'end;',
  11138. 'procedure TPoint.DoIt(b: TBird);',
  11139. 'begin',
  11140. ' b.doit;',
  11141. ' b.glob;',
  11142. ' TBird.glob;',
  11143. 'end;',
  11144. 'begin',
  11145. '']);
  11146. ConvertProgram;
  11147. CheckSource('TestAdvRecord_SubClass',
  11148. LinesToStr([ // statements
  11149. 'rtl.createClass($mod, "TObject", null, function () {',
  11150. ' this.$init = function () {',
  11151. ' };',
  11152. ' this.$final = function () {',
  11153. ' };',
  11154. '});',
  11155. 'rtl.recNewT($mod, "TPoint", function () {',
  11156. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11157. ' this.DoIt = function () {',
  11158. ' this.DoIt();',
  11159. ' this.DoIt();',
  11160. ' this.$class.Glob();',
  11161. ' this.$class.Glob();',
  11162. ' };',
  11163. ' this.Glob = function () {',
  11164. ' this.Glob();',
  11165. ' this.Glob();',
  11166. ' };',
  11167. ' });',
  11168. ' this.$eq = function (b) {',
  11169. ' return true;',
  11170. ' };',
  11171. ' this.$assign = function (s) {',
  11172. ' return this;',
  11173. ' };',
  11174. ' this.DoIt = function (b) {',
  11175. ' b.DoIt();',
  11176. ' b.$class.Glob();',
  11177. ' this.TBird.Glob();',
  11178. ' };',
  11179. '}, true);',
  11180. '']),
  11181. LinesToStr([ // $mod.$main
  11182. '']));
  11183. end;
  11184. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11185. begin
  11186. StartProgram(false);
  11187. Add([
  11188. '{$modeswitch AdvancedRecords}',
  11189. 'type',
  11190. ' IUnknown = interface end;',
  11191. ' TPoint = record',
  11192. ' type IBird = interface end;',
  11193. ' end;',
  11194. 'begin',
  11195. '']);
  11196. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] interface inside record',
  11197. nNotYetImplemented);
  11198. ParseProgram;
  11199. end;
  11200. procedure TTestModule.TestAdvRecord_Constructor;
  11201. begin
  11202. StartProgram(false);
  11203. Add([
  11204. '{$modeswitch AdvancedRecords}',
  11205. 'type',
  11206. ' TPoint = record',
  11207. ' x,y: longint;',
  11208. ' constructor Create(ax: longint; ay: longint = -1);',
  11209. ' end;',
  11210. 'constructor tpoint.create(ax,ay: longint);',
  11211. 'begin',
  11212. ' x:=ax;',
  11213. ' self.y:=ay;',
  11214. 'end;',
  11215. 'var r: TPoint;',
  11216. 'begin',
  11217. ' r:=TPoint.Create(1,2);',
  11218. ' with TPoint do r:=Create(1,2);',
  11219. ' r.Create(3);',
  11220. ' r:=r.Create(4);',
  11221. '']);
  11222. ConvertProgram;
  11223. CheckSource('TestAdvRecord_Constructor',
  11224. LinesToStr([ // statements
  11225. 'rtl.recNewT($mod, "TPoint", function () {',
  11226. ' this.x = 0;',
  11227. ' this.y = 0;',
  11228. ' this.$eq = function (b) {',
  11229. ' return (this.x === b.x) && (this.y === b.y);',
  11230. ' };',
  11231. ' this.$assign = function (s) {',
  11232. ' this.x = s.x;',
  11233. ' this.y = s.y;',
  11234. ' return this;',
  11235. ' };',
  11236. ' this.Create = function (ax, ay) {',
  11237. ' this.x = ax;',
  11238. ' this.y = ay;',
  11239. ' return this;',
  11240. ' };',
  11241. '}, true);',
  11242. 'this.r = $mod.TPoint.$new();',
  11243. '']),
  11244. LinesToStr([ // $mod.$main
  11245. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11246. 'var $with1 = $mod.TPoint;',
  11247. '$mod.r.$assign($with1.$new().Create(1, 2));',
  11248. '$mod.r.Create(3, -1);',
  11249. '$mod.r.$assign($mod.r.Create(4, -1));',
  11250. '']));
  11251. end;
  11252. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11253. begin
  11254. StartProgram(false);
  11255. Add(['type',
  11256. ' TObject = class',
  11257. ' public',
  11258. ' constructor Create;',
  11259. ' destructor Destroy;',
  11260. ' end;',
  11261. ' TBird = TObject;',
  11262. 'constructor tobject.create;',
  11263. 'begin end;',
  11264. 'destructor tobject.destroy;',
  11265. 'begin end;',
  11266. 'var Obj: tobject;',
  11267. 'begin',
  11268. ' obj:=tobject.create;',
  11269. ' obj:=tobject.create();',
  11270. ' obj:=tbird.create;',
  11271. ' obj:=tbird.create();',
  11272. ' obj:=obj.create();',
  11273. ' obj.destroy;',
  11274. '']);
  11275. ConvertProgram;
  11276. CheckSource('TestClass_TObjectDefaultConstructor',
  11277. LinesToStr([ // statements
  11278. 'rtl.createClass($mod,"TObject",null,function(){',
  11279. ' this.$init = function () {',
  11280. ' };',
  11281. ' this.$final = function () {',
  11282. ' };',
  11283. ' this.Create = function(){',
  11284. ' return this;',
  11285. ' };',
  11286. ' this.Destroy = function(){',
  11287. ' };',
  11288. '});',
  11289. 'this.Obj = null;'
  11290. ]),
  11291. LinesToStr([ // $mod.$main
  11292. '$mod.Obj = $mod.TObject.$create("Create");',
  11293. '$mod.Obj = $mod.TObject.$create("Create");',
  11294. '$mod.Obj = $mod.TObject.$create("Create");',
  11295. '$mod.Obj = $mod.TObject.$create("Create");',
  11296. '$mod.Obj = $mod.Obj.Create();',
  11297. '$mod.Obj.$destroy("Destroy");',
  11298. '']));
  11299. end;
  11300. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11301. begin
  11302. StartProgram(false);
  11303. Add('type');
  11304. Add(' TObject = class');
  11305. Add(' public');
  11306. Add(' constructor Create(Par: longint);');
  11307. Add(' end;');
  11308. Add('constructor tobject.create(par: longint);');
  11309. Add('begin end;');
  11310. Add('var Obj: tobject;');
  11311. Add('begin');
  11312. Add(' obj:=tobject.create(3);');
  11313. ConvertProgram;
  11314. CheckSource('TestClass_TObjectConstructorWithParams',
  11315. LinesToStr([ // statements
  11316. 'rtl.createClass($mod,"TObject",null,function(){',
  11317. ' this.$init = function () {',
  11318. ' };',
  11319. ' this.$final = function () {',
  11320. ' };',
  11321. ' this.Create = function(Par){',
  11322. ' return this;',
  11323. ' };',
  11324. '});',
  11325. 'this.Obj = null;'
  11326. ]),
  11327. LinesToStr([ // $mod.$main
  11328. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11329. ]));
  11330. end;
  11331. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11332. begin
  11333. StartProgram(false);
  11334. Add('type');
  11335. Add(' TObject = class');
  11336. Add(' public');
  11337. Add(' constructor Create;');
  11338. Add(' end;');
  11339. Add(' TTest = class(TObject)');
  11340. Add(' public');
  11341. Add(' constructor Create(const Par: longint = 1);');
  11342. Add(' end;');
  11343. Add('constructor tobject.create;');
  11344. Add('begin end;');
  11345. Add('constructor ttest.create(const par: longint);');
  11346. Add('begin end;');
  11347. Add('var t: ttest;');
  11348. Add('begin');
  11349. Add(' t:=ttest.create;');
  11350. Add(' t:=ttest.create(2);');
  11351. ConvertProgram;
  11352. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11353. LinesToStr([ // statements
  11354. 'rtl.createClass($mod,"TObject",null,function(){',
  11355. ' this.$init = function () {',
  11356. ' };',
  11357. ' this.$final = function () {',
  11358. ' };',
  11359. ' this.Create = function(){',
  11360. ' return this;',
  11361. ' };',
  11362. '});',
  11363. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11364. ' this.Create$1 = function (Par) {',
  11365. ' return this;',
  11366. ' };',
  11367. '});',
  11368. 'this.t = null;'
  11369. ]),
  11370. LinesToStr([ // $mod.$main
  11371. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11372. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11373. ]));
  11374. end;
  11375. procedure TTestModule.TestClass_Var;
  11376. begin
  11377. StartProgram(false);
  11378. Add([
  11379. 'type',
  11380. ' TObject = class',
  11381. ' public',
  11382. ' vI: longint;',
  11383. ' constructor Create(Par: longint);',
  11384. ' end;',
  11385. 'constructor tobject.create(par: longint);',
  11386. 'begin',
  11387. ' vi:=par+3',
  11388. 'end;',
  11389. 'var Obj: tobject;',
  11390. 'begin',
  11391. ' obj:=tobject.create(4);',
  11392. ' obj.vi:=obj.VI+5;']);
  11393. ConvertProgram;
  11394. CheckSource('TestClass_Var',
  11395. LinesToStr([ // statements
  11396. 'rtl.createClass($mod,"TObject",null,function(){',
  11397. ' this.$init = function () {',
  11398. ' this.vI = 0;',
  11399. ' };',
  11400. ' this.$final = function () {',
  11401. ' };',
  11402. ' this.Create = function(Par){',
  11403. ' this.vI = Par+3;',
  11404. ' return this;',
  11405. ' };',
  11406. '});',
  11407. 'this.Obj = null;'
  11408. ]),
  11409. LinesToStr([ // $mod.$main
  11410. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11411. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11412. ]));
  11413. end;
  11414. procedure TTestModule.TestClass_Method;
  11415. begin
  11416. StartProgram(false);
  11417. Add('type');
  11418. Add(' TObject = class');
  11419. Add(' public');
  11420. Add(' vI: longint;');
  11421. Add(' Sub: TObject;');
  11422. Add(' constructor Create;');
  11423. Add(' function GetIt(Par: longint): tobject;');
  11424. Add(' end;');
  11425. Add('constructor tobject.create; begin end;');
  11426. Add('function tobject.getit(par: longint): tobject;');
  11427. Add('begin');
  11428. Add(' Self.vi:=par+3;');
  11429. Add(' Result:=self.sub;');
  11430. Add('end;');
  11431. Add('var Obj: tobject;');
  11432. Add('begin');
  11433. Add(' obj:=tobject.create;');
  11434. Add(' obj.getit(4);');
  11435. Add(' obj.sub.sub:=nil;');
  11436. Add(' obj.sub.getit(5);');
  11437. Add(' obj.sub.getit(6).SUB:=nil;');
  11438. Add(' obj.sub.getit(7).GETIT(8);');
  11439. Add(' obj.sub.getit(9).SuB.getit(10);');
  11440. ConvertProgram;
  11441. CheckSource('TestClass_Method',
  11442. LinesToStr([ // statements
  11443. 'rtl.createClass($mod,"TObject",null,function(){',
  11444. ' this.$init = function () {',
  11445. ' this.vI = 0;',
  11446. ' this.Sub = null;',
  11447. ' };',
  11448. ' this.$final = function () {',
  11449. ' this.Sub = undefined;',
  11450. ' };',
  11451. ' this.Create = function(){',
  11452. ' return this;',
  11453. ' };',
  11454. ' this.GetIt = function(Par){',
  11455. ' var Result = null;',
  11456. ' this.vI = Par + 3;',
  11457. ' Result = this.Sub;',
  11458. ' return Result;',
  11459. ' };',
  11460. '});',
  11461. 'this.Obj = null;'
  11462. ]),
  11463. LinesToStr([ // $mod.$main
  11464. '$mod.Obj = $mod.TObject.$create("Create");',
  11465. '$mod.Obj.GetIt(4);',
  11466. '$mod.Obj.Sub.Sub=null;',
  11467. '$mod.Obj.Sub.GetIt(5);',
  11468. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11469. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11470. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11471. ]));
  11472. end;
  11473. procedure TTestModule.TestClass_Implementation;
  11474. begin
  11475. StartUnit(false);
  11476. Add([
  11477. 'interface',
  11478. 'type',
  11479. ' TObject = class',
  11480. ' constructor Create;',
  11481. ' end;',
  11482. 'implementation',
  11483. 'type',
  11484. ' TIntClass = class',
  11485. ' constructor Create; reintroduce;',
  11486. ' class procedure DoGlob;',
  11487. ' end;',
  11488. 'constructor tintclass.create;',
  11489. 'begin',
  11490. ' inherited;',
  11491. ' inherited create;',
  11492. ' doglob;',
  11493. 'end;',
  11494. 'class procedure tintclass.doglob;',
  11495. 'begin',
  11496. 'end;',
  11497. 'constructor tobject.create;',
  11498. 'var',
  11499. ' iC: tintclass;',
  11500. 'begin',
  11501. ' ic:=tintclass.create;',
  11502. ' tintclass.doglob;',
  11503. ' ic.doglob;',
  11504. 'end;',
  11505. 'initialization',
  11506. ' tintclass.doglob;',
  11507. '']);
  11508. ConvertUnit;
  11509. CheckSource('TestClass_Implementation',
  11510. LinesToStr([ // statements
  11511. 'var $impl = $mod.$impl;',
  11512. 'rtl.createClass($mod, "TObject", null, function () {',
  11513. ' this.$init = function () {',
  11514. ' };',
  11515. ' this.$final = function () {',
  11516. ' };',
  11517. ' this.Create = function () {',
  11518. ' var iC = null;',
  11519. ' iC = $impl.TIntClass.$create("Create$1");',
  11520. ' $impl.TIntClass.DoGlob();',
  11521. ' iC.$class.DoGlob();',
  11522. ' return this;',
  11523. ' };',
  11524. '});',
  11525. '']),
  11526. LinesToStr([ // $mod.$main
  11527. '$impl.TIntClass.DoGlob();',
  11528. '']),
  11529. LinesToStr([
  11530. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  11531. ' this.Create$1 = function () {',
  11532. ' $mod.TObject.Create.call(this);',
  11533. ' $mod.TObject.Create.call(this);',
  11534. ' this.$class.DoGlob();',
  11535. ' return this;',
  11536. ' };',
  11537. ' this.DoGlob = function () {',
  11538. ' };',
  11539. '});',
  11540. '']));
  11541. end;
  11542. procedure TTestModule.TestClass_Inheritance;
  11543. begin
  11544. StartProgram(false);
  11545. Add('type');
  11546. Add(' TObject = class');
  11547. Add(' public');
  11548. Add(' constructor Create;');
  11549. Add(' end;');
  11550. Add(' TClassA = class');
  11551. Add(' end;');
  11552. Add(' TClassB = class(TObject)');
  11553. Add(' procedure ProcB;');
  11554. Add(' end;');
  11555. Add('constructor tobject.create; begin end;');
  11556. Add('procedure tclassb.procb; begin end;');
  11557. Add('var');
  11558. Add(' oO: TObject;');
  11559. Add(' oA: TClassA;');
  11560. Add(' oB: TClassB;');
  11561. Add('begin');
  11562. Add(' oO:=tobject.Create;');
  11563. Add(' oA:=tclassa.Create;');
  11564. Add(' ob:=tclassb.Create;');
  11565. Add(' if oo is tclassa then ;');
  11566. Add(' ob:=oo as tclassb;');
  11567. Add(' (oo as tclassb).procb;');
  11568. ConvertProgram;
  11569. CheckSource('TestClass_Inheritance',
  11570. LinesToStr([ // statements
  11571. 'rtl.createClass($mod,"TObject",null,function(){',
  11572. ' this.$init = function () {',
  11573. ' };',
  11574. ' this.$final = function () {',
  11575. ' };',
  11576. ' this.Create = function () {',
  11577. ' return this;',
  11578. ' };',
  11579. '});',
  11580. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  11581. '});',
  11582. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  11583. ' this.ProcB = function () {',
  11584. ' };',
  11585. '});',
  11586. 'this.oO = null;',
  11587. 'this.oA = null;',
  11588. 'this.oB = null;'
  11589. ]),
  11590. LinesToStr([ // $mod.$main
  11591. '$mod.oO = $mod.TObject.$create("Create");',
  11592. '$mod.oA = $mod.TClassA.$create("Create");',
  11593. '$mod.oB = $mod.TClassB.$create("Create");',
  11594. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  11595. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  11596. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  11597. ]));
  11598. end;
  11599. procedure TTestModule.TestClass_TypeAlias;
  11600. begin
  11601. StartProgram(false);
  11602. Add([
  11603. '{$interfaces corba}',
  11604. 'type',
  11605. ' IObject = interface',
  11606. ' end;',
  11607. ' IBird = type IObject;',
  11608. ' TObject = class',
  11609. ' end;',
  11610. ' TBird = type TObject;',
  11611. 'var',
  11612. ' oObj: TObject;',
  11613. ' oBird: TBird;',
  11614. ' IntfObj: IObject;',
  11615. ' IntfBird: IBird;',
  11616. 'begin',
  11617. ' oObj:=oBird;',
  11618. '']);
  11619. ConvertProgram;
  11620. CheckSource('TestClass_TypeAlias',
  11621. LinesToStr([ // statements
  11622. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  11623. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  11624. 'rtl.createClass($mod, "TObject", null, function () {',
  11625. ' this.$init = function () {',
  11626. ' };',
  11627. ' this.$final = function () {',
  11628. ' };',
  11629. '});',
  11630. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11631. '});',
  11632. 'this.oObj = null;',
  11633. 'this.oBird = null;',
  11634. 'this.IntfObj = null;',
  11635. 'this.IntfBird = null;',
  11636. '']),
  11637. LinesToStr([ // $mod.$main
  11638. '$mod.oObj = $mod.oBird;',
  11639. '']));
  11640. end;
  11641. procedure TTestModule.TestClass_AbstractMethod;
  11642. begin
  11643. StartProgram(false);
  11644. Add('type');
  11645. Add(' TObject = class');
  11646. Add(' public');
  11647. Add(' procedure DoIt; virtual; abstract;');
  11648. Add(' end;');
  11649. Add('begin');
  11650. ConvertProgram;
  11651. CheckSource('TestClass_AbstractMethod',
  11652. LinesToStr([ // statements
  11653. 'rtl.createClass($mod,"TObject",null,function(){',
  11654. ' this.$init = function () {',
  11655. ' };',
  11656. ' this.$final = function () {',
  11657. ' };',
  11658. '});'
  11659. ]),
  11660. LinesToStr([ // this.$main
  11661. ''
  11662. ]));
  11663. end;
  11664. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  11665. begin
  11666. StartProgram(false);
  11667. Add([
  11668. 'type',
  11669. ' TObject = class',
  11670. ' procedure DoAbstract; virtual; abstract;',
  11671. ' procedure DoVirtual; virtual;',
  11672. ' procedure DoIt;',
  11673. ' end;',
  11674. ' TA = class',
  11675. ' procedure doabstract; override;',
  11676. ' procedure dovirtual; override;',
  11677. ' procedure DoSome;',
  11678. ' end;',
  11679. 'procedure tobject.dovirtual;',
  11680. 'begin',
  11681. ' inherited; // call non existing ancestor -> ignore silently',
  11682. 'end;',
  11683. 'procedure tobject.doit;',
  11684. 'begin',
  11685. 'end;',
  11686. 'procedure ta.doabstract;',
  11687. 'begin',
  11688. ' inherited dovirtual; // call TObject.DoVirtual',
  11689. 'end;',
  11690. 'procedure ta.dovirtual;',
  11691. 'begin',
  11692. ' inherited; // call TObject.DoVirtual',
  11693. ' inherited dovirtual; // call TObject.DoVirtual',
  11694. ' inherited dovirtual(); // call TObject.DoVirtual',
  11695. ' doit;',
  11696. ' doit();',
  11697. 'end;',
  11698. 'procedure ta.dosome;',
  11699. 'begin',
  11700. ' inherited; // call non existing ancestor method -> silently ignore',
  11701. 'end;',
  11702. 'begin']);
  11703. ConvertProgram;
  11704. CheckSource('TestClass_CallInherited_ProcNoParams',
  11705. LinesToStr([ // statements
  11706. 'rtl.createClass($mod,"TObject",null,function(){',
  11707. ' this.$init = function () {',
  11708. ' };',
  11709. ' this.$final = function () {',
  11710. ' };',
  11711. ' this.DoVirtual = function () {',
  11712. ' };',
  11713. ' this.DoIt = function () {',
  11714. ' };',
  11715. '});',
  11716. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11717. ' this.DoAbstract = function () {',
  11718. ' $mod.TObject.DoVirtual.call(this);',
  11719. ' };',
  11720. ' this.DoVirtual = function () {',
  11721. ' $mod.TObject.DoVirtual.call(this);',
  11722. ' $mod.TObject.DoVirtual.call(this);',
  11723. ' $mod.TObject.DoVirtual.call(this);',
  11724. ' this.DoIt();',
  11725. ' this.DoIt();',
  11726. ' };',
  11727. ' this.DoSome = function () {',
  11728. ' };',
  11729. '});'
  11730. ]),
  11731. LinesToStr([ // this.$main
  11732. ''
  11733. ]));
  11734. end;
  11735. procedure TTestModule.TestClass_CallInherited_WithParams;
  11736. begin
  11737. StartProgram(false);
  11738. Add([
  11739. 'type',
  11740. ' TObject = class',
  11741. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  11742. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  11743. ' procedure DoIt(pA: longint; pB: longint = 0);',
  11744. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  11745. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11746. ' end;',
  11747. ' TClassA = class',
  11748. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  11749. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  11750. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11751. ' end;',
  11752. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  11753. 'begin',
  11754. 'end;',
  11755. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  11756. 'begin',
  11757. 'end;',
  11758. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  11759. 'begin',
  11760. 'end;',
  11761. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  11762. 'begin',
  11763. 'end;',
  11764. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  11765. 'begin',
  11766. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11767. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11768. 'end;',
  11769. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  11770. 'begin',
  11771. ' inherited; // call TObject.DoVirtual(pA,pB)',
  11772. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11773. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11774. ' doit(pa,pb);',
  11775. ' doit(pa);',
  11776. ' doit2(pa);',
  11777. ' doit2;',
  11778. 'end;',
  11779. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  11780. 'begin',
  11781. ' pa:=inherited;',
  11782. 'end;',
  11783. 'begin']);
  11784. ConvertProgram;
  11785. CheckSource('TestClass_CallInherited_WithParams',
  11786. LinesToStr([ // statements
  11787. 'rtl.createClass($mod,"TObject",null,function(){',
  11788. ' this.$init = function () {',
  11789. ' };',
  11790. ' this.$final = function () {',
  11791. ' };',
  11792. ' this.DoVirtual = function (pA,pB) {',
  11793. ' };',
  11794. ' this.DoIt = function (pA,pB) {',
  11795. ' };',
  11796. ' this.DoIt2 = function (pA,pB) {',
  11797. ' };',
  11798. ' this.GetIt = function (pA, pB) {',
  11799. ' var Result = 0;',
  11800. ' return Result;',
  11801. ' };',
  11802. '});',
  11803. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  11804. ' this.DoAbstract = function (pA,pB) {',
  11805. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11806. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11807. ' };',
  11808. ' this.DoVirtual = function (pA,pB) {',
  11809. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  11810. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11811. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11812. ' this.DoIt(pA,pB);',
  11813. ' this.DoIt(pA,0);',
  11814. ' this.DoIt2(pA,2);',
  11815. ' this.DoIt2(1,2);',
  11816. ' };',
  11817. ' this.GetIt$1 = function (pA, pB) {',
  11818. ' var Result = 0;',
  11819. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  11820. ' return Result;',
  11821. ' };',
  11822. '});'
  11823. ]),
  11824. LinesToStr([ // this.$main
  11825. ''
  11826. ]));
  11827. end;
  11828. procedure TTestModule.TestClasS_CallInheritedConstructor;
  11829. begin
  11830. StartProgram(false);
  11831. Add('type');
  11832. Add(' TObject = class');
  11833. Add(' constructor Create; virtual;');
  11834. Add(' constructor CreateWithB(b: boolean);');
  11835. Add(' end;');
  11836. Add(' TA = class');
  11837. Add(' constructor Create; override;');
  11838. Add(' constructor CreateWithC(c: char);');
  11839. Add(' procedure DoIt;');
  11840. Add(' class function DoSome: TObject;');
  11841. Add(' end;');
  11842. Add('constructor tobject.create;');
  11843. Add('begin');
  11844. Add(' inherited; // call non existing ancestor -> ignore silently');
  11845. Add('end;');
  11846. Add('constructor tobject.createwithb(b: boolean);');
  11847. Add('begin');
  11848. Add(' inherited; // call non existing ancestor -> ignore silently');
  11849. Add(' create; // normal call');
  11850. Add('end;');
  11851. Add('constructor ta.create;');
  11852. Add('begin');
  11853. Add(' inherited; // normal call TObject.Create');
  11854. Add(' inherited create; // normal call TObject.Create');
  11855. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  11856. Add('end;');
  11857. Add('constructor ta.createwithc(c: char);');
  11858. Add('begin');
  11859. Add(' inherited create; // call TObject.Create');
  11860. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  11861. Add(' doit;');
  11862. Add(' doit();');
  11863. Add(' dosome;');
  11864. Add('end;');
  11865. Add('procedure ta.doit;');
  11866. Add('begin');
  11867. Add(' create; // normal call');
  11868. Add(' createwithb(false); // normal call');
  11869. Add(' createwithc(''c''); // normal call');
  11870. Add('end;');
  11871. Add('class function ta.dosome: TObject;');
  11872. Add('begin');
  11873. Add(' Result:=create; // constructor');
  11874. Add(' Result:=createwithb(true); // constructor');
  11875. Add(' Result:=createwithc(''c''); // constructor');
  11876. Add('end;');
  11877. Add('begin');
  11878. ConvertProgram;
  11879. CheckSource('TestClass_CallInheritedConstructor',
  11880. LinesToStr([ // statements
  11881. 'rtl.createClass($mod,"TObject",null,function(){',
  11882. ' this.$init = function () {',
  11883. ' };',
  11884. ' this.$final = function () {',
  11885. ' };',
  11886. ' this.Create = function () {',
  11887. ' return this;',
  11888. ' };',
  11889. ' this.CreateWithB = function (b) {',
  11890. ' this.Create();',
  11891. ' return this;',
  11892. ' };',
  11893. '});',
  11894. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11895. ' this.Create = function () {',
  11896. ' $mod.TObject.Create.call(this);',
  11897. ' $mod.TObject.Create.call(this);',
  11898. ' $mod.TObject.CreateWithB.call(this, false);',
  11899. ' return this;',
  11900. ' };',
  11901. ' this.CreateWithC = function (c) {',
  11902. ' $mod.TObject.Create.call(this);',
  11903. ' $mod.TObject.CreateWithB.call(this, true);',
  11904. ' this.DoIt();',
  11905. ' this.DoIt();',
  11906. ' this.$class.DoSome();',
  11907. ' return this;',
  11908. ' };',
  11909. ' this.DoIt = function () {',
  11910. ' this.Create();',
  11911. ' this.CreateWithB(false);',
  11912. ' this.CreateWithC("c");',
  11913. ' };',
  11914. ' this.DoSome = function () {',
  11915. ' var Result = null;',
  11916. ' Result = this.$create("Create");',
  11917. ' Result = this.$create("CreateWithB", [true]);',
  11918. ' Result = this.$create("CreateWithC", ["c"]);',
  11919. ' return Result;',
  11920. ' };',
  11921. '});'
  11922. ]),
  11923. LinesToStr([ // this.$main
  11924. ''
  11925. ]));
  11926. end;
  11927. procedure TTestModule.TestClass_ClassVar_Assign;
  11928. begin
  11929. StartProgram(false);
  11930. Add([
  11931. 'type',
  11932. ' TObject = class',
  11933. ' public',
  11934. ' class var vI: longint;',
  11935. ' class var Sub: TObject;',
  11936. ' constructor Create;',
  11937. ' class function GetIt(var Par: longint): tobject;',
  11938. ' end;',
  11939. 'constructor tobject.create;',
  11940. 'begin',
  11941. ' vi:=vi+1;',
  11942. ' Self.vi:=Self.vi+1;',
  11943. ' inc(vi);',
  11944. 'end;',
  11945. 'class function tobject.getit(var par: longint): tobject;',
  11946. 'begin',
  11947. ' vi:=vi+3;',
  11948. ' Self.vi:=Self.vi+4;',
  11949. ' inc(vi);',
  11950. ' Result:=self.sub;',
  11951. ' GetIt(vi);',
  11952. 'end;',
  11953. 'var Obj: tobject;',
  11954. 'begin',
  11955. ' obj:=tobject.create;',
  11956. ' tobject.vi:=3;',
  11957. ' if tobject.vi=4 then ;',
  11958. ' tobject.sub:=nil;',
  11959. ' obj.sub:=nil;',
  11960. ' obj.sub.sub:=nil;']);
  11961. ConvertProgram;
  11962. CheckSource('TestClass_ClassVar_Assign',
  11963. LinesToStr([ // statements
  11964. 'rtl.createClass($mod,"TObject",null,function(){',
  11965. ' this.vI = 0;',
  11966. ' this.Sub = null;',
  11967. ' this.$init = function () {',
  11968. ' };',
  11969. ' this.$final = function () {',
  11970. ' };',
  11971. ' this.Create = function(){',
  11972. ' $mod.TObject.vI = this.vI+1;',
  11973. ' $mod.TObject.vI = this.vI+1;',
  11974. ' $mod.TObject.vI += 1;',
  11975. ' return this;',
  11976. ' };',
  11977. ' this.GetIt = function(Par){',
  11978. ' var Result = null;',
  11979. ' $mod.TObject.vI = this.vI + 3;',
  11980. ' $mod.TObject.vI = this.vI + 4;',
  11981. ' $mod.TObject.vI += 1;',
  11982. ' Result = this.Sub;',
  11983. ' this.GetIt({',
  11984. ' p: $mod.TObject,',
  11985. ' get: function () {',
  11986. ' return this.p.vI;',
  11987. ' },',
  11988. ' set: function (v) {',
  11989. ' this.p.vI = v;',
  11990. ' }',
  11991. ' });',
  11992. ' return Result;',
  11993. ' };',
  11994. '});',
  11995. 'this.Obj = null;'
  11996. ]),
  11997. LinesToStr([ // $mod.$main
  11998. '$mod.Obj = $mod.TObject.$create("Create");',
  11999. '$mod.TObject.vI = 3;',
  12000. 'if ($mod.TObject.vI === 4);',
  12001. '$mod.TObject.Sub=null;',
  12002. '$mod.TObject.Sub=null;',
  12003. '$mod.TObject.Sub=null;',
  12004. '']));
  12005. end;
  12006. procedure TTestModule.TestClass_CallClassMethod;
  12007. begin
  12008. StartProgram(false);
  12009. Add('type');
  12010. Add(' TObject = class');
  12011. Add(' public');
  12012. Add(' class var vI: longint;');
  12013. Add(' class var Sub: TObject;');
  12014. Add(' constructor Create;');
  12015. Add(' function GetMore(Par: longint): longint;');
  12016. Add(' class function GetIt(Par: longint): tobject;');
  12017. Add(' end;');
  12018. Add('constructor tobject.create;');
  12019. Add('begin');
  12020. Add(' sub:=getit(3);');
  12021. Add(' vi:=getmore(4);');
  12022. Add(' sub:=Self.getit(5);');
  12023. Add(' vi:=Self.getmore(6);');
  12024. Add('end;');
  12025. Add('function tobject.getmore(par: longint): longint;');
  12026. Add('begin');
  12027. Add(' sub:=getit(11);');
  12028. Add(' vi:=getmore(12);');
  12029. Add(' sub:=self.getit(13);');
  12030. Add(' vi:=self.getmore(14);');
  12031. Add('end;');
  12032. Add('class function tobject.getit(par: longint): tobject;');
  12033. Add('begin');
  12034. Add(' sub:=getit(21);');
  12035. Add(' vi:=sub.getmore(22);');
  12036. Add(' sub:=self.getit(23);');
  12037. Add(' vi:=self.sub.getmore(24);');
  12038. Add('end;');
  12039. Add('var Obj: tobject;');
  12040. Add('begin');
  12041. Add(' obj:=tobject.create;');
  12042. Add(' tobject.getit(5);');
  12043. Add(' obj.getit(6);');
  12044. Add(' obj.sub.getit(7);');
  12045. Add(' obj.sub.getit(8).SUB:=nil;');
  12046. Add(' obj.sub.getit(9).GETIT(10);');
  12047. Add(' obj.sub.getit(11).SuB.getit(12);');
  12048. ConvertProgram;
  12049. CheckSource('TestClass_CallClassMethod',
  12050. LinesToStr([ // statements
  12051. 'rtl.createClass($mod,"TObject",null,function(){',
  12052. ' this.vI = 0;',
  12053. ' this.Sub = null;',
  12054. ' this.$init = function () {',
  12055. ' };',
  12056. ' this.$final = function () {',
  12057. ' };',
  12058. ' this.Create = function(){',
  12059. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12060. ' $mod.TObject.vI = this.GetMore(4);',
  12061. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12062. ' $mod.TObject.vI = this.GetMore(6);',
  12063. ' return this;',
  12064. ' };',
  12065. ' this.GetMore = function(Par){',
  12066. ' var Result = 0;',
  12067. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12068. ' $mod.TObject.vI = this.GetMore(12);',
  12069. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12070. ' $mod.TObject.vI = this.GetMore(14);',
  12071. ' return Result;',
  12072. ' };',
  12073. ' this.GetIt = function(Par){',
  12074. ' var Result = null;',
  12075. ' $mod.TObject.Sub = this.GetIt(21);',
  12076. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12077. ' $mod.TObject.Sub = this.GetIt(23);',
  12078. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12079. ' return Result;',
  12080. ' };',
  12081. '});',
  12082. 'this.Obj = null;'
  12083. ]),
  12084. LinesToStr([ // $mod.$main
  12085. '$mod.Obj = $mod.TObject.$create("Create");',
  12086. '$mod.TObject.GetIt(5);',
  12087. '$mod.Obj.$class.GetIt(6);',
  12088. '$mod.Obj.Sub.$class.GetIt(7);',
  12089. '$mod.TObject.Sub=null;',
  12090. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12091. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12092. '']));
  12093. end;
  12094. procedure TTestModule.TestClass_Property;
  12095. begin
  12096. StartProgram(false);
  12097. Add('type');
  12098. Add(' TObject = class');
  12099. Add(' Fx: longint;');
  12100. Add(' Fy: longint;');
  12101. Add(' function GetInt: longint;');
  12102. Add(' procedure SetInt(Value: longint);');
  12103. Add(' procedure DoIt;');
  12104. Add(' property IntA: longint read Fx write Fy;');
  12105. Add(' property IntB: longint read GetInt write SetInt;');
  12106. Add(' end;');
  12107. Add('function tobject.getint: longint;');
  12108. Add('begin');
  12109. Add(' result:=fx;');
  12110. Add('end;');
  12111. Add('procedure tobject.setint(value: longint);');
  12112. Add('begin');
  12113. Add(' if value=fy then exit;');
  12114. Add(' fy:=value;');
  12115. Add('end;');
  12116. Add('procedure tobject.doit;');
  12117. Add('begin');
  12118. Add(' IntA:=IntA+1;');
  12119. Add(' Self.IntA:=Self.IntA+1;');
  12120. Add(' IntB:=IntB+1;');
  12121. Add(' Self.IntB:=Self.IntB+1;');
  12122. Add('end;');
  12123. Add('var Obj: tobject;');
  12124. Add('begin');
  12125. Add(' obj.inta:=obj.inta+1;');
  12126. Add(' if obj.intb=2 then;');
  12127. Add(' obj.intb:=obj.intb+2;');
  12128. Add(' obj.setint(obj.inta);');
  12129. ConvertProgram;
  12130. CheckSource('TestClass_Property',
  12131. LinesToStr([ // statements
  12132. 'rtl.createClass($mod, "TObject", null, function () {',
  12133. ' this.$init = function () {',
  12134. ' this.Fx = 0;',
  12135. ' this.Fy = 0;',
  12136. ' };',
  12137. ' this.$final = function () {',
  12138. ' };',
  12139. ' this.GetInt = function () {',
  12140. ' var Result = 0;',
  12141. ' Result = this.Fx;',
  12142. ' return Result;',
  12143. ' };',
  12144. ' this.SetInt = function (Value) {',
  12145. ' if (Value === this.Fy) return;',
  12146. ' this.Fy = Value;',
  12147. ' };',
  12148. ' this.DoIt = function () {',
  12149. ' this.Fy = this.Fx + 1;',
  12150. ' this.Fy = this.Fx + 1;',
  12151. ' this.SetInt(this.GetInt() + 1);',
  12152. ' this.SetInt(this.GetInt() + 1);',
  12153. ' };',
  12154. '});',
  12155. 'this.Obj = null;'
  12156. ]),
  12157. LinesToStr([ // $mod.$main
  12158. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12159. 'if ($mod.Obj.GetInt() === 2);',
  12160. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12161. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12162. ]));
  12163. end;
  12164. procedure TTestModule.TestClass_Property_ClassMethod;
  12165. begin
  12166. StartProgram(false);
  12167. Add([
  12168. 'type',
  12169. ' TObject = class',
  12170. ' class var Fx: longint;',
  12171. ' class var Fy: longint;',
  12172. ' class function GetInt: longint;',
  12173. ' class procedure SetInt(Value: longint);',
  12174. ' end;',
  12175. ' TBird = class',
  12176. ' class procedure DoIt;',
  12177. ' class property IntA: longint read Fx write Fy;',
  12178. ' class property IntB: longint read GetInt write SetInt;',
  12179. ' end;',
  12180. 'class function tobject.getint: longint;',
  12181. 'begin',
  12182. ' result:=fx;',
  12183. 'end;',
  12184. 'class procedure tobject.setint(value: longint);',
  12185. 'begin',
  12186. 'end;',
  12187. 'class procedure tbird.doit;',
  12188. 'begin',
  12189. ' FX:=3;',
  12190. ' IntA:=IntA+1;',
  12191. ' Self.IntA:=Self.IntA+1;',
  12192. ' IntB:=IntB+1;',
  12193. ' Self.IntB:=Self.IntB+1;',
  12194. ' with Self do begin',
  12195. ' FX:=11;',
  12196. ' IntA:=IntA+12;',
  12197. ' IntB:=IntB+13;',
  12198. ' end;',
  12199. 'end;',
  12200. 'var Obj: tbird;',
  12201. 'begin',
  12202. ' tbird.fx:=tbird.fx+1;',
  12203. ' tbird.inta:=tbird.inta+1;',
  12204. ' if tbird.intb=2 then;',
  12205. ' tbird.intb:=tbird.intb+2;',
  12206. ' tbird.setint(tbird.inta);',
  12207. ' obj.inta:=obj.inta+1;',
  12208. ' if obj.intb=2 then;',
  12209. ' obj.intb:=obj.intb+2;',
  12210. ' obj.setint(obj.inta);',
  12211. ' with Tbird do begin',
  12212. ' FX:=FY+1;',
  12213. ' inta:=inta+2;',
  12214. ' intb:=intb+3;',
  12215. ' end;',
  12216. ' with Obj do begin',
  12217. ' FX:=FY+1;',
  12218. ' inta:=inta+2;',
  12219. ' intb:=intb+3;',
  12220. ' end;',
  12221. '']);
  12222. ConvertProgram;
  12223. CheckSource('TestClass_Property_ClassMethod',
  12224. LinesToStr([ // statements
  12225. 'rtl.createClass($mod, "TObject", null, function () {',
  12226. ' this.Fx = 0;',
  12227. ' this.Fy = 0;',
  12228. ' this.$init = function () {',
  12229. ' };',
  12230. ' this.$final = function () {',
  12231. ' };',
  12232. ' this.GetInt = function () {',
  12233. ' var Result = 0;',
  12234. ' Result = this.Fx;',
  12235. ' return Result;',
  12236. ' };',
  12237. ' this.SetInt = function (Value) {',
  12238. ' };',
  12239. '});',
  12240. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12241. ' this.DoIt = function () {',
  12242. ' $mod.TObject.Fx = 3;',
  12243. ' $mod.TObject.Fy = this.Fx + 1;',
  12244. ' $mod.TObject.Fy = this.Fx + 1;',
  12245. ' this.SetInt(this.GetInt() + 1);',
  12246. ' this.SetInt(this.GetInt() + 1);',
  12247. ' $mod.TObject.Fx = 11;',
  12248. ' $mod.TObject.Fy = this.Fx + 12;',
  12249. ' this.SetInt(this.GetInt() + 13);',
  12250. ' };',
  12251. '});',
  12252. 'this.Obj = null;'
  12253. ]),
  12254. LinesToStr([ // $mod.$main
  12255. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12256. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12257. 'if ($mod.TBird.GetInt() === 2);',
  12258. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12259. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12260. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12261. 'if ($mod.Obj.$class.GetInt() === 2);',
  12262. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12263. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12264. 'var $with1 = $mod.TBird;',
  12265. '$mod.TObject.Fx = $with1.Fy + 1;',
  12266. '$mod.TObject.Fy = $with1.Fx + 2;',
  12267. '$with1.SetInt($with1.GetInt() + 3);',
  12268. 'var $with2 = $mod.Obj;',
  12269. '$mod.TObject.Fx = $with2.Fy + 1;',
  12270. '$mod.TObject.Fy = $with2.Fx + 2;',
  12271. '$with2.$class.SetInt($with2.$class.GetInt() + 3);',
  12272. '']));
  12273. end;
  12274. procedure TTestModule.TestClass_Property_Indexed;
  12275. begin
  12276. StartProgram(false);
  12277. Add('type');
  12278. Add(' TObject = class');
  12279. Add(' FItems: array of longint;');
  12280. Add(' function GetItems(Index: longint): longint;');
  12281. Add(' procedure SetItems(Index: longint; Value: longint);');
  12282. Add(' procedure DoIt;');
  12283. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  12284. Add(' end;');
  12285. Add('function tobject.getitems(index: longint): longint;');
  12286. Add('begin');
  12287. Add(' Result:=fitems[index];');
  12288. Add('end;');
  12289. Add('procedure tobject.setitems(index: longint; value: longint);');
  12290. Add('begin');
  12291. Add(' fitems[index]:=value;');
  12292. Add('end;');
  12293. Add('procedure tobject.doit;');
  12294. Add('begin');
  12295. Add(' items[1]:=2;');
  12296. Add(' items[3]:=items[4];');
  12297. Add(' self.items[5]:=self.items[6];');
  12298. Add(' items[items[7]]:=items[items[8]];');
  12299. Add('end;');
  12300. Add('var Obj: tobject;');
  12301. Add('begin');
  12302. Add(' obj.Items[11]:=obj.Items[12];');
  12303. ConvertProgram;
  12304. CheckSource('TestClass_Property_Indexed',
  12305. LinesToStr([ // statements
  12306. 'rtl.createClass($mod, "TObject", null, function () {',
  12307. ' this.$init = function () {',
  12308. ' this.FItems = [];',
  12309. ' };',
  12310. ' this.$final = function () {',
  12311. ' this.FItems = undefined;',
  12312. ' };',
  12313. ' this.GetItems = function (Index) {',
  12314. ' var Result = 0;',
  12315. ' Result = this.FItems[Index];',
  12316. ' return Result;',
  12317. ' };',
  12318. ' this.SetItems = function (Index, Value) {',
  12319. ' this.FItems[Index] = Value;',
  12320. ' };',
  12321. ' this.DoIt = function () {',
  12322. ' this.SetItems(1, 2);',
  12323. ' this.SetItems(3,this.GetItems(4));',
  12324. ' this.SetItems(5,this.GetItems(6));',
  12325. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12326. ' };',
  12327. '});',
  12328. 'this.Obj = null;'
  12329. ]),
  12330. LinesToStr([ // $mod.$main
  12331. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12332. ]));
  12333. end;
  12334. procedure TTestModule.TestClass_Property_IndexSpec;
  12335. begin
  12336. StartProgram(false);
  12337. Add([
  12338. 'type',
  12339. ' TEnum = (red, blue);',
  12340. ' TObject = class',
  12341. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12342. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12343. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12344. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12345. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12346. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12347. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12348. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12349. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12350. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12351. ' end;',
  12352. 'procedure DoIt(b: boolean); begin end;',
  12353. 'var',
  12354. ' o: TObject;',
  12355. 'begin',
  12356. ' o.B1:=o.B1;',
  12357. ' o.B2:=o.B2;',
  12358. ' o.B3:=o.B3;',
  12359. ' o.I1[''a'']:=o.I1[''b''];',
  12360. ' doit(o.b1);',
  12361. ' doit(o.b2);',
  12362. ' doit(o.i1[''c'']);',
  12363. '']);
  12364. ConvertProgram;
  12365. CheckSource('TestClass_Property_IndexSpec',
  12366. LinesToStr([ // statements
  12367. 'this.TEnum = {',
  12368. ' "0": "red",',
  12369. ' red: 0,',
  12370. ' "1": "blue",',
  12371. ' blue: 1',
  12372. '};',
  12373. 'rtl.createClass($mod, "TObject", null, function () {',
  12374. ' this.$init = function () {',
  12375. ' };',
  12376. ' this.$final = function () {',
  12377. ' };',
  12378. '});',
  12379. 'this.DoIt = function (b) {',
  12380. '};',
  12381. 'this.o = null;',
  12382. '']),
  12383. LinesToStr([ // $mod.$main
  12384. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12385. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12386. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12387. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12388. '$mod.DoIt($mod.o.GetIntBool(1));',
  12389. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12390. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12391. '']));
  12392. end;
  12393. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12394. begin
  12395. StartProgram(false);
  12396. Add('type');
  12397. Add(' TArray = array of longint;');
  12398. Add(' TObject = class');
  12399. Add(' FItems: TArray;');
  12400. Add(' function GetItems: tarray;');
  12401. Add(' procedure SetItems(Value: tarray);');
  12402. Add(' property Items: tarray read getitems write setitems;');
  12403. Add(' procedure SetNumbers(const Value: tarray);');
  12404. Add(' property Numbers: tarray write setnumbers;');
  12405. Add(' end;');
  12406. Add('function tobject.getitems: tarray;');
  12407. Add('begin');
  12408. Add(' Result:=fitems;');
  12409. Add('end;');
  12410. Add('procedure tobject.setitems(value: tarray);');
  12411. Add('begin');
  12412. Add(' fitems:=value;');
  12413. Add(' fitems:=nil;');
  12414. Add(' Items:=nil;');
  12415. Add(' Items:=Items;');
  12416. Add(' Items[1]:=2;');
  12417. Add(' fitems[3]:=Items[4];');
  12418. Add(' Items[5]:=Items[6];');
  12419. Add(' Self.Items[7]:=8;');
  12420. Add(' Self.Items[9]:=Self.Items[10];');
  12421. Add(' Items[Items[11]]:=Items[Items[12]];');
  12422. Add('end;');
  12423. Add('procedure tobject.SetNumbers(const Value: tarray);');
  12424. Add('begin;');
  12425. Add(' Numbers:=nil;');
  12426. Add(' Numbers:=Value;');
  12427. Add(' Self.Numbers:=Value;');
  12428. Add('end;');
  12429. Add('var Obj: tobject;');
  12430. Add('begin');
  12431. Add(' obj.items:=nil;');
  12432. Add(' obj.items:=obj.items;');
  12433. Add(' obj.items[11]:=obj.items[12];');
  12434. ConvertProgram;
  12435. CheckSource('TestClass_PropertyOfTypeArray',
  12436. LinesToStr([ // statements
  12437. 'rtl.createClass($mod, "TObject", null, function () {',
  12438. ' this.$init = function () {',
  12439. ' this.FItems = [];',
  12440. ' };',
  12441. ' this.$final = function () {',
  12442. ' this.FItems = undefined;',
  12443. ' };',
  12444. ' this.GetItems = function () {',
  12445. ' var Result = [];',
  12446. ' Result = rtl.arrayRef(this.FItems);',
  12447. ' return Result;',
  12448. ' };',
  12449. ' this.SetItems = function (Value) {',
  12450. ' this.FItems = rtl.arrayRef(Value);',
  12451. ' this.FItems = [];',
  12452. ' this.SetItems([]);',
  12453. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  12454. ' this.GetItems()[1] = 2;',
  12455. ' this.FItems[3] = this.GetItems()[4];',
  12456. ' this.GetItems()[5] = this.GetItems()[6];',
  12457. ' this.GetItems()[7] = 8;',
  12458. ' this.GetItems()[9] = this.GetItems()[10];',
  12459. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12460. ' };',
  12461. ' this.SetNumbers = function (Value) {',
  12462. ' this.SetNumbers([]);',
  12463. ' this.SetNumbers(Value);',
  12464. ' this.SetNumbers(Value);',
  12465. ' };',
  12466. '});',
  12467. 'this.Obj = null;'
  12468. ]),
  12469. LinesToStr([ // $mod.$main
  12470. '$mod.Obj.SetItems([]);',
  12471. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12472. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12473. ]));
  12474. end;
  12475. procedure TTestModule.TestClass_PropertyDefault;
  12476. begin
  12477. StartProgram(false);
  12478. Add([
  12479. 'type',
  12480. ' TArray = array of longint;',
  12481. ' TObject = class',
  12482. ' FItems: TArray;',
  12483. ' function GetItems(Index: longint): longint;',
  12484. ' procedure SetItems(Index, Value: longint);',
  12485. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12486. ' end;',
  12487. 'function tobject.getitems(index: longint): longint;',
  12488. 'begin',
  12489. 'end;',
  12490. 'procedure tobject.setitems(index, value: longint);',
  12491. 'begin',
  12492. ' Self[1]:=2;',
  12493. ' Self[3]:=Self[index];',
  12494. ' Self[index]:=Self[Self[value]];',
  12495. ' Self[Self[4]]:=value;',
  12496. 'end;',
  12497. 'var Obj: tobject;',
  12498. 'begin',
  12499. ' obj[11]:=12;',
  12500. ' obj[13]:=obj[14];',
  12501. ' obj[obj[15]]:=obj[obj[15]];',
  12502. ' TObject(obj)[16]:=TObject(obj)[17];']);
  12503. ConvertProgram;
  12504. CheckSource('TestClass_PropertyDefault',
  12505. LinesToStr([ // statements
  12506. 'rtl.createClass($mod, "TObject", null, function () {',
  12507. ' this.$init = function () {',
  12508. ' this.FItems = [];',
  12509. ' };',
  12510. ' this.$final = function () {',
  12511. ' this.FItems = undefined;',
  12512. ' };',
  12513. ' this.GetItems = function (Index) {',
  12514. ' var Result = 0;',
  12515. ' return Result;',
  12516. ' };',
  12517. ' this.SetItems = function (Index, Value) {',
  12518. ' this.SetItems(1, 2);',
  12519. ' this.SetItems(3, this.GetItems(Index));',
  12520. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  12521. ' this.SetItems(this.GetItems(4), Value);',
  12522. ' };',
  12523. '});',
  12524. 'this.Obj = null;'
  12525. ]),
  12526. LinesToStr([ // $mod.$main
  12527. '$mod.Obj.SetItems(11, 12);',
  12528. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  12529. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  12530. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  12531. '']));
  12532. end;
  12533. procedure TTestModule.TestClass_PropertyDefault2;
  12534. begin
  12535. StartProgram(false);
  12536. Add([
  12537. 'type',
  12538. ' TObject = class end;',
  12539. ' TAlphaList = class',
  12540. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  12541. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  12542. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  12543. ' end;',
  12544. ' TBetaList = class',
  12545. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  12546. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  12547. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  12548. ' end;',
  12549. ' TBird = class',
  12550. ' procedure DoIt;',
  12551. ' end;',
  12552. 'procedure TBird.DoIt;',
  12553. 'var',
  12554. ' List: TAlphaList;',
  12555. 'begin',
  12556. ' if TBetaList(List[true])[3]=nil then ;',
  12557. ' TBetaList(List[false])[5]:=nil;',
  12558. 'end;',
  12559. 'var',
  12560. ' List: TAlphaList;',
  12561. 'begin',
  12562. ' if TBetaList(List[true])[3]=nil then ;',
  12563. ' TBetaList(List[false])[5]:=nil;',
  12564. '']);
  12565. ConvertProgram;
  12566. CheckSource('TestClass_PropertyDefault2',
  12567. LinesToStr([ // statements
  12568. 'rtl.createClass($mod, "TObject", null, function () {',
  12569. ' this.$init = function () {',
  12570. ' };',
  12571. ' this.$final = function () {',
  12572. ' };',
  12573. '});',
  12574. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  12575. '});',
  12576. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  12577. '});',
  12578. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12579. ' this.DoIt = function () {',
  12580. ' var List = null;',
  12581. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  12582. ' List.GetAlphas(false).SetBetas(5, null);',
  12583. ' };',
  12584. '});',
  12585. 'this.List = null;',
  12586. '']),
  12587. LinesToStr([ // $mod.$main
  12588. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  12589. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  12590. '']));
  12591. end;
  12592. procedure TTestModule.TestClass_PropertyOverride;
  12593. begin
  12594. StartProgram(false);
  12595. Add('type');
  12596. Add(' integer = longint;');
  12597. Add(' TObject = class');
  12598. Add(' FItem: integer;');
  12599. Add(' function GetItem: integer; external name ''GetItem'';');
  12600. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  12601. Add(' property Item: integer read getitem write setitem;');
  12602. Add(' end;');
  12603. Add(' TCar = class');
  12604. Add(' FBag: integer;');
  12605. Add(' function GetBag: integer; external name ''GetBag'';');
  12606. Add(' property Item read getbag;');
  12607. Add(' end;');
  12608. Add('var');
  12609. Add(' Obj: tobject;');
  12610. Add(' Car: tcar;');
  12611. Add('begin');
  12612. Add(' Obj.Item:=Obj.Item;');
  12613. Add(' Car.Item:=Car.Item;');
  12614. ConvertProgram;
  12615. CheckSource('TestClass_PropertyOverride',
  12616. LinesToStr([ // statements
  12617. 'rtl.createClass($mod, "TObject", null, function () {',
  12618. ' this.$init = function () {',
  12619. ' this.FItem = 0;',
  12620. ' };',
  12621. ' this.$final = function () {',
  12622. ' };',
  12623. '});',
  12624. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12625. ' this.$init = function () {',
  12626. ' $mod.TObject.$init.call(this);',
  12627. ' this.FBag = 0;',
  12628. ' };',
  12629. '});',
  12630. 'this.Obj = null;',
  12631. 'this.Car = null;',
  12632. '']),
  12633. LinesToStr([ // $mod.$main
  12634. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  12635. '$mod.Car.SetItem($mod.Car.GetBag());',
  12636. '']));
  12637. end;
  12638. procedure TTestModule.TestClass_PropertyIncVisibility;
  12639. begin
  12640. AddModuleWithIntfImplSrc('unit1.pp',
  12641. LinesToStr([
  12642. 'type',
  12643. ' TNumber = longint;',
  12644. ' TInteger = longint;',
  12645. ' TObject = class',
  12646. ' private',
  12647. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  12648. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  12649. ' protected',
  12650. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  12651. ' end;']),
  12652. LinesToStr([
  12653. '']));
  12654. StartProgram(true);
  12655. Add([
  12656. 'uses unit1;',
  12657. 'type',
  12658. ' TBird = class',
  12659. ' public',
  12660. ' property Items;',
  12661. ' end;',
  12662. 'procedure DoIt(i: TInteger);',
  12663. 'begin',
  12664. 'end;',
  12665. 'var b: TBird;',
  12666. 'begin',
  12667. ' b.Items[1]:=2;',
  12668. ' b.Items[3]:=b.Items[4];',
  12669. ' DoIt(b.Items[5]);',
  12670. '']);
  12671. ConvertProgram;
  12672. CheckSource('TestClass_PropertyIncVisibility',
  12673. LinesToStr([ // statements
  12674. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  12675. '});',
  12676. 'this.DoIt = function (i) {',
  12677. '};',
  12678. 'this.b = null;'
  12679. ]),
  12680. LinesToStr([ // $mod.$main
  12681. '$mod.b.SetItems(1, 2);',
  12682. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  12683. '$mod.DoIt($mod.b.GetItems(5));'
  12684. ]));
  12685. end;
  12686. procedure TTestModule.TestClass_Assigned;
  12687. begin
  12688. StartProgram(false);
  12689. Add('type');
  12690. Add(' TObject = class');
  12691. Add(' end;');
  12692. Add('var');
  12693. Add(' Obj: tobject;');
  12694. Add(' b: boolean;');
  12695. Add('begin');
  12696. Add(' if Assigned(obj) then ;');
  12697. Add(' b:=Assigned(obj) or false;');
  12698. ConvertProgram;
  12699. CheckSource('TestClass_Assigned',
  12700. LinesToStr([ // statements
  12701. 'rtl.createClass($mod, "TObject", null, function () {',
  12702. ' this.$init = function () {',
  12703. ' };',
  12704. ' this.$final = function () {',
  12705. ' };',
  12706. '});',
  12707. 'this.Obj = null;',
  12708. 'this.b = false;'
  12709. ]),
  12710. LinesToStr([ // $mod.$main
  12711. 'if ($mod.Obj != null);',
  12712. '$mod.b = ($mod.Obj != null) || false;'
  12713. ]));
  12714. end;
  12715. procedure TTestModule.TestClass_WithClassDoCreate;
  12716. begin
  12717. StartProgram(false);
  12718. Add('type');
  12719. Add(' TObject = class');
  12720. Add(' aBool: boolean;');
  12721. Add(' Arr: array of boolean;');
  12722. Add(' constructor Create;');
  12723. Add(' end;');
  12724. Add('constructor TObject.Create; begin end;');
  12725. Add('var');
  12726. Add(' Obj: tobject;');
  12727. Add(' b: boolean;');
  12728. Add('begin');
  12729. Add(' with tobject.create do begin');
  12730. Add(' b:=abool;');
  12731. Add(' abool:=b;');
  12732. Add(' b:=arr[1];');
  12733. Add(' arr[2]:=b;');
  12734. Add(' end;');
  12735. Add(' with tobject do');
  12736. Add(' obj:=create;');
  12737. Add(' with obj do begin');
  12738. Add(' create;');
  12739. Add(' b:=abool;');
  12740. Add(' abool:=b;');
  12741. Add(' b:=arr[3];');
  12742. Add(' arr[4]:=b;');
  12743. Add(' end;');
  12744. ConvertProgram;
  12745. CheckSource('TestClass_WithClassDoCreate',
  12746. LinesToStr([ // statements
  12747. 'rtl.createClass($mod, "TObject", null, function () {',
  12748. ' this.$init = function () {',
  12749. ' this.aBool = false;',
  12750. ' this.Arr = [];',
  12751. ' };',
  12752. ' this.$final = function () {',
  12753. ' this.Arr = undefined;',
  12754. ' };',
  12755. ' this.Create = function () {',
  12756. ' return this;',
  12757. ' };',
  12758. '});',
  12759. 'this.Obj = null;',
  12760. 'this.b = false;'
  12761. ]),
  12762. LinesToStr([ // $mod.$main
  12763. 'var $with1 = $mod.TObject.$create("Create");',
  12764. '$mod.b = $with1.aBool;',
  12765. '$with1.aBool = $mod.b;',
  12766. '$mod.b = $with1.Arr[1];',
  12767. '$with1.Arr[2] = $mod.b;',
  12768. 'var $with2 = $mod.TObject;',
  12769. '$mod.Obj = $with2.$create("Create");',
  12770. 'var $with3 = $mod.Obj;',
  12771. '$with3.Create();',
  12772. '$mod.b = $with3.aBool;',
  12773. '$with3.aBool = $mod.b;',
  12774. '$mod.b = $with3.Arr[3];',
  12775. '$with3.Arr[4] = $mod.b;',
  12776. '']));
  12777. end;
  12778. procedure TTestModule.TestClass_WithClassInstDoProperty;
  12779. begin
  12780. StartProgram(false);
  12781. Add('type');
  12782. Add(' TObject = class');
  12783. Add(' FInt: longint;');
  12784. Add(' constructor Create;');
  12785. Add(' function GetSize: longint;');
  12786. Add(' procedure SetSize(Value: longint);');
  12787. Add(' property Int: longint read FInt write FInt;');
  12788. Add(' property Size: longint read GetSize write SetSize;');
  12789. Add(' end;');
  12790. Add('constructor TObject.Create; begin end;');
  12791. Add('function TObject.GetSize: longint; begin; end;');
  12792. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12793. Add('var');
  12794. Add(' Obj: tobject;');
  12795. Add(' i: longint;');
  12796. Add('begin');
  12797. Add(' with TObject.Create do begin');
  12798. Add(' i:=int;');
  12799. Add(' int:=i;');
  12800. Add(' i:=size;');
  12801. Add(' size:=i;');
  12802. Add(' end;');
  12803. Add(' with obj do begin');
  12804. Add(' i:=int;');
  12805. Add(' int:=i;');
  12806. Add(' i:=size;');
  12807. Add(' size:=i;');
  12808. Add(' end;');
  12809. ConvertProgram;
  12810. CheckSource('TestClass_WithClassInstDoProperty',
  12811. LinesToStr([ // statements
  12812. 'rtl.createClass($mod, "TObject", null, function () {',
  12813. ' this.$init = function () {',
  12814. ' this.FInt = 0;',
  12815. ' };',
  12816. ' this.$final = function () {',
  12817. ' };',
  12818. ' this.Create = function () {',
  12819. ' return this;',
  12820. ' };',
  12821. ' this.GetSize = function () {',
  12822. ' var Result = 0;',
  12823. ' return Result;',
  12824. ' };',
  12825. ' this.SetSize = function (Value) {',
  12826. ' };',
  12827. '});',
  12828. 'this.Obj = null;',
  12829. 'this.i = 0;'
  12830. ]),
  12831. LinesToStr([ // $mod.$main
  12832. 'var $with1 = $mod.TObject.$create("Create");',
  12833. '$mod.i = $with1.FInt;',
  12834. '$with1.FInt = $mod.i;',
  12835. '$mod.i = $with1.GetSize();',
  12836. '$with1.SetSize($mod.i);',
  12837. 'var $with2 = $mod.Obj;',
  12838. '$mod.i = $with2.FInt;',
  12839. '$with2.FInt = $mod.i;',
  12840. '$mod.i = $with2.GetSize();',
  12841. '$with2.SetSize($mod.i);',
  12842. '']));
  12843. end;
  12844. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  12845. begin
  12846. StartProgram(false);
  12847. Add('type');
  12848. Add(' TObject = class');
  12849. Add(' constructor Create;');
  12850. Add(' function GetItems(Index: longint): longint;');
  12851. Add(' procedure SetItems(Index, Value: longint);');
  12852. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  12853. Add(' end;');
  12854. Add('constructor TObject.Create; begin end;');
  12855. Add('function tobject.getitems(index: longint): longint; begin; end;');
  12856. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  12857. Add('var');
  12858. Add(' Obj: tobject;');
  12859. Add(' i: longint;');
  12860. Add('begin');
  12861. Add(' with TObject.Create do begin');
  12862. Add(' i:=Items[1];');
  12863. Add(' Items[2]:=i;');
  12864. Add(' end;');
  12865. Add(' with obj do begin');
  12866. Add(' i:=Items[3];');
  12867. Add(' Items[4]:=i;');
  12868. Add(' end;');
  12869. ConvertProgram;
  12870. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  12871. LinesToStr([ // statements
  12872. 'rtl.createClass($mod, "TObject", null, function () {',
  12873. ' this.$init = function () {',
  12874. ' };',
  12875. ' this.$final = function () {',
  12876. ' };',
  12877. ' this.Create = function () {',
  12878. ' return this;',
  12879. ' };',
  12880. ' this.GetItems = function (Index) {',
  12881. ' var Result = 0;',
  12882. ' return Result;',
  12883. ' };',
  12884. ' this.SetItems = function (Index, Value) {',
  12885. ' };',
  12886. '});',
  12887. 'this.Obj = null;',
  12888. 'this.i = 0;'
  12889. ]),
  12890. LinesToStr([ // $mod.$main
  12891. 'var $with1 = $mod.TObject.$create("Create");',
  12892. '$mod.i = $with1.GetItems(1);',
  12893. '$with1.SetItems(2, $mod.i);',
  12894. 'var $with2 = $mod.Obj;',
  12895. '$mod.i = $with2.GetItems(3);',
  12896. '$with2.SetItems(4, $mod.i);',
  12897. '']));
  12898. end;
  12899. procedure TTestModule.TestClass_WithClassInstDoFunc;
  12900. begin
  12901. StartProgram(false);
  12902. Add('type');
  12903. Add(' TObject = class');
  12904. Add(' constructor Create;');
  12905. Add(' function GetSize: longint;');
  12906. Add(' procedure SetSize(Value: longint);');
  12907. Add(' end;');
  12908. Add('constructor TObject.Create; begin end;');
  12909. Add('function TObject.GetSize: longint; begin; end;');
  12910. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12911. Add('var');
  12912. Add(' Obj: tobject;');
  12913. Add(' i: longint;');
  12914. Add('begin');
  12915. Add(' with TObject.Create do begin');
  12916. Add(' i:=GetSize;');
  12917. Add(' i:=GetSize();');
  12918. Add(' SetSize(i);');
  12919. Add(' end;');
  12920. Add(' with obj do begin');
  12921. Add(' i:=GetSize;');
  12922. Add(' i:=GetSize();');
  12923. Add(' SetSize(i);');
  12924. Add(' end;');
  12925. ConvertProgram;
  12926. CheckSource('TestClass_WithClassInstDoFunc',
  12927. LinesToStr([ // statements
  12928. 'rtl.createClass($mod, "TObject", null, function () {',
  12929. ' this.$init = function () {',
  12930. ' };',
  12931. ' this.$final = function () {',
  12932. ' };',
  12933. ' this.Create = function () {',
  12934. ' return this;',
  12935. ' };',
  12936. ' this.GetSize = function () {',
  12937. ' var Result = 0;',
  12938. ' return Result;',
  12939. ' };',
  12940. ' this.SetSize = function (Value) {',
  12941. ' };',
  12942. '});',
  12943. 'this.Obj = null;',
  12944. 'this.i = 0;'
  12945. ]),
  12946. LinesToStr([ // $mod.$main
  12947. 'var $with1 = $mod.TObject.$create("Create");',
  12948. '$mod.i = $with1.GetSize();',
  12949. '$mod.i = $with1.GetSize();',
  12950. '$with1.SetSize($mod.i);',
  12951. 'var $with2 = $mod.Obj;',
  12952. '$mod.i = $with2.GetSize();',
  12953. '$mod.i = $with2.GetSize();',
  12954. '$with2.SetSize($mod.i);',
  12955. '']));
  12956. end;
  12957. procedure TTestModule.TestClass_TypeCast;
  12958. begin
  12959. StartProgram(false);
  12960. Add('type');
  12961. Add(' TObject = class');
  12962. Add(' Next: TObject;');
  12963. Add(' constructor Create;');
  12964. Add(' end;');
  12965. Add(' TControl = class(TObject)');
  12966. Add(' Arr: array of TObject;');
  12967. Add(' function GetIt(vI: longint = 0): TObject;');
  12968. Add(' end;');
  12969. Add('constructor tobject.create; begin end;');
  12970. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  12971. Add('var');
  12972. Add(' Obj: tobject;');
  12973. Add('begin');
  12974. Add(' obj:=tcontrol(obj).next;');
  12975. Add(' tcontrol(obj):=nil;');
  12976. Add(' obj:=tcontrol(obj);');
  12977. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  12978. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  12979. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  12980. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  12981. Add(' obj:=tcontrol(nil);');
  12982. ConvertProgram;
  12983. CheckSource('TestClass_TypeCast',
  12984. LinesToStr([ // statements
  12985. 'rtl.createClass($mod, "TObject", null, function () {',
  12986. ' this.$init = function () {',
  12987. ' this.Next = null;',
  12988. ' };',
  12989. ' this.$final = function () {',
  12990. ' this.Next = undefined;',
  12991. ' };',
  12992. ' this.Create = function () {',
  12993. ' return this;',
  12994. ' };',
  12995. '});',
  12996. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  12997. ' this.$init = function () {',
  12998. ' $mod.TObject.$init.call(this);',
  12999. ' this.Arr = [];',
  13000. ' };',
  13001. ' this.$final = function () {',
  13002. ' this.Arr = undefined;',
  13003. ' $mod.TObject.$final.call(this);',
  13004. ' };',
  13005. ' this.GetIt = function (vI) {',
  13006. ' var Result = null;',
  13007. ' return Result;',
  13008. ' };',
  13009. '});',
  13010. 'this.Obj = null;'
  13011. ]),
  13012. LinesToStr([ // $mod.$main
  13013. '$mod.Obj = $mod.Obj.Next;',
  13014. '$mod.Obj = null;',
  13015. '$mod.Obj = $mod.Obj;',
  13016. '$mod.Obj = $mod.Obj.GetIt(0);',
  13017. '$mod.Obj = $mod.Obj.GetIt(0);',
  13018. '$mod.Obj = $mod.Obj.GetIt(1);',
  13019. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13020. '$mod.Obj = null;',
  13021. '']));
  13022. end;
  13023. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13024. begin
  13025. StartProgram(false);
  13026. Add('type');
  13027. Add(' TObject = class end;');
  13028. Add('procedure ProcA(var A);');
  13029. Add('begin');
  13030. Add(' TObject(A):=nil;');
  13031. Add(' TObject(A):=TObject(A);');
  13032. Add(' if TObject(A)=nil then ;');
  13033. Add(' if nil=TObject(A) then ;');
  13034. Add('end;');
  13035. Add('procedure ProcB(out A);');
  13036. Add('begin');
  13037. Add(' TObject(A):=nil;');
  13038. Add(' TObject(A):=TObject(A);');
  13039. Add(' if TObject(A)=nil then ;');
  13040. Add(' if nil=TObject(A) then ;');
  13041. Add('end;');
  13042. Add('procedure ProcC(const A);');
  13043. Add('begin');
  13044. Add(' if TObject(A)=nil then ;');
  13045. Add(' if nil=TObject(A) then ;');
  13046. Add('end;');
  13047. Add('var o: TObject;');
  13048. Add('begin');
  13049. Add(' ProcA(o);');
  13050. Add(' ProcB(o);');
  13051. Add(' ProcC(o);');
  13052. ConvertProgram;
  13053. CheckSource('TestClass_TypeCastUntypedParam',
  13054. LinesToStr([ // statements
  13055. 'rtl.createClass($mod, "TObject", null, function () {',
  13056. ' this.$init = function () {',
  13057. ' };',
  13058. ' this.$final = function () {',
  13059. ' };',
  13060. '});',
  13061. 'this.ProcA = function (A) {',
  13062. ' A.set(null);',
  13063. ' A.set(A.get());',
  13064. ' if (A.get() === null);',
  13065. ' if (null === A.get());',
  13066. '};',
  13067. 'this.ProcB = function (A) {',
  13068. ' A.set(null);',
  13069. ' A.set(A.get());',
  13070. ' if (A.get() === null);',
  13071. ' if (null === A.get());',
  13072. '};',
  13073. 'this.ProcC = function (A) {',
  13074. ' if (A === null);',
  13075. ' if (null === A);',
  13076. '};',
  13077. 'this.o = null;',
  13078. '']),
  13079. LinesToStr([ // $mod.$main
  13080. '$mod.ProcA({',
  13081. ' p: $mod,',
  13082. ' get: function () {',
  13083. ' return this.p.o;',
  13084. ' },',
  13085. ' set: function (v) {',
  13086. ' this.p.o = v;',
  13087. ' }',
  13088. '});',
  13089. '$mod.ProcB({',
  13090. ' p: $mod,',
  13091. ' get: function () {',
  13092. ' return this.p.o;',
  13093. ' },',
  13094. ' set: function (v) {',
  13095. ' this.p.o = v;',
  13096. ' }',
  13097. '});',
  13098. '$mod.ProcC($mod.o);',
  13099. '']));
  13100. end;
  13101. procedure TTestModule.TestClass_Overloads;
  13102. begin
  13103. StartProgram(false);
  13104. Add('type');
  13105. Add(' TObject = class');
  13106. Add(' procedure DoIt;');
  13107. Add(' procedure DoIt(vI: longint);');
  13108. Add(' end;');
  13109. Add('procedure TObject.DoIt;');
  13110. Add('begin');
  13111. Add(' DoIt;');
  13112. Add(' DoIt(1);');
  13113. Add('end;');
  13114. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13115. Add('begin');
  13116. ConvertProgram;
  13117. CheckSource('TestClass_Overloads',
  13118. LinesToStr([ // statements
  13119. 'rtl.createClass($mod, "TObject", null, function () {',
  13120. ' this.$init = function () {',
  13121. ' };',
  13122. ' this.$final = function () {',
  13123. ' };',
  13124. ' this.DoIt = function () {',
  13125. ' this.DoIt();',
  13126. ' this.DoIt$1(1);',
  13127. ' };',
  13128. ' this.DoIt$1 = function (vI) {',
  13129. ' };',
  13130. '});',
  13131. '']),
  13132. LinesToStr([ // $mod.$main
  13133. '']));
  13134. end;
  13135. procedure TTestModule.TestClass_OverloadsAncestor;
  13136. begin
  13137. StartProgram(false);
  13138. Add('type');
  13139. Add(' TObject = class;');
  13140. Add(' TObject = class');
  13141. Add(' procedure DoIt(vA: longint);');
  13142. Add(' procedure DoIt(vA, vB: longint);');
  13143. Add(' end;');
  13144. Add(' TCar = class;');
  13145. Add(' TCar = class');
  13146. Add(' procedure DoIt(vA: longint);');
  13147. Add(' procedure DoIt(vA, vB: longint);');
  13148. Add(' end;');
  13149. Add('procedure tobject.doit(va: longint);');
  13150. Add('begin');
  13151. Add(' doit(1);');
  13152. Add(' doit(1,2);');
  13153. Add('end;');
  13154. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13155. Add('procedure tcar.doit(va: longint);');
  13156. Add('begin');
  13157. Add(' doit(1);');
  13158. Add(' doit(1,2);');
  13159. Add(' inherited doit(1);');
  13160. Add(' inherited doit(1,2);');
  13161. Add('end;');
  13162. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13163. Add('begin');
  13164. ConvertProgram;
  13165. CheckSource('TestClass_OverloadsAncestor',
  13166. LinesToStr([ // statements
  13167. 'rtl.createClass($mod, "TObject", null, function () {',
  13168. ' this.$init = function () {',
  13169. ' };',
  13170. ' this.$final = function () {',
  13171. ' };',
  13172. ' this.DoIt = function (vA) {',
  13173. ' this.DoIt(1);',
  13174. ' this.DoIt$1(1,2);',
  13175. ' };',
  13176. ' this.DoIt$1 = function (vA, vB) {',
  13177. ' };',
  13178. '});',
  13179. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13180. ' this.DoIt$2 = function (vA) {',
  13181. ' this.DoIt$2(1);',
  13182. ' this.DoIt$3(1, 2);',
  13183. ' $mod.TObject.DoIt.call(this, 1);',
  13184. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13185. ' };',
  13186. ' this.DoIt$3 = function (vA, vB) {',
  13187. ' };',
  13188. '});',
  13189. '']),
  13190. LinesToStr([ // $mod.$main
  13191. '']));
  13192. end;
  13193. procedure TTestModule.TestClass_OverloadConstructor;
  13194. begin
  13195. StartProgram(false);
  13196. Add('type');
  13197. Add(' TObject = class');
  13198. Add(' constructor Create(vA: longint);');
  13199. Add(' constructor Create(vA, vB: longint);');
  13200. Add(' end;');
  13201. Add(' TCar = class');
  13202. Add(' constructor Create(vA: longint);');
  13203. Add(' constructor Create(vA, vB: longint);');
  13204. Add(' end;');
  13205. Add('constructor tobject.create(va: longint);');
  13206. Add('begin');
  13207. Add(' create(1);');
  13208. Add(' create(1,2);');
  13209. Add('end;');
  13210. Add('constructor tobject.create(va, vb: longint); begin end;');
  13211. Add('constructor tcar.create(va: longint);');
  13212. Add('begin');
  13213. Add(' create(1);');
  13214. Add(' create(1,2);');
  13215. Add(' inherited create(1);');
  13216. Add(' inherited create(1,2);');
  13217. Add('end;');
  13218. Add('constructor tcar.create(va, vb: longint); begin end;');
  13219. Add('begin');
  13220. Add(' tobject.create(1);');
  13221. Add(' tobject.create(1,2);');
  13222. Add(' tcar.create(1);');
  13223. Add(' tcar.create(1,2);');
  13224. ConvertProgram;
  13225. CheckSource('TestClass_OverloadConstructor',
  13226. LinesToStr([ // statements
  13227. 'rtl.createClass($mod, "TObject", null, function () {',
  13228. ' this.$init = function () {',
  13229. ' };',
  13230. ' this.$final = function () {',
  13231. ' };',
  13232. ' this.Create = function (vA) {',
  13233. ' this.Create(1);',
  13234. ' this.Create$1(1,2);',
  13235. ' return this;',
  13236. ' };',
  13237. ' this.Create$1 = function (vA, vB) {',
  13238. ' return this;',
  13239. ' };',
  13240. '});',
  13241. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13242. ' this.Create$2 = function (vA) {',
  13243. ' this.Create$2(1);',
  13244. ' this.Create$3(1, 2);',
  13245. ' $mod.TObject.Create.call(this, 1);',
  13246. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13247. ' return this;',
  13248. ' };',
  13249. ' this.Create$3 = function (vA, vB) {',
  13250. ' return this;',
  13251. ' };',
  13252. '});',
  13253. '']),
  13254. LinesToStr([ // $mod.$main
  13255. '$mod.TObject.$create("Create", [1]);',
  13256. '$mod.TObject.$create("Create$1", [1, 2]);',
  13257. '$mod.TCar.$create("Create$2", [1]);',
  13258. '$mod.TCar.$create("Create$3", [1, 2]);',
  13259. '']));
  13260. end;
  13261. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13262. begin
  13263. StartProgram(false);
  13264. Add([
  13265. '{$mode delphi}',
  13266. 'type',
  13267. ' TObject = class end;',
  13268. ' TBird = class',
  13269. ' function {#a}GetValue: longint; overload; virtual;',
  13270. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  13271. ' end;',
  13272. ' TEagle = class(TBird)',
  13273. ' function {#c}GetValue: longint; overload; override;',
  13274. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  13275. ' end;',
  13276. 'function TBird.GetValue: longint;',
  13277. 'begin',
  13278. ' if 3={@a}GetValue then ;',
  13279. ' if 4={@b}GetValue(5) then ;',
  13280. 'end;',
  13281. 'function TBird.GetValue(AValue: longint): longint;',
  13282. 'begin',
  13283. 'end;',
  13284. 'function TEagle.GetValue: longint;',
  13285. 'begin',
  13286. ' if 13={@c}GetValue then ;',
  13287. ' if 14={@d}GetValue(15) then ;',
  13288. ' if 15=inherited {@a}GetValue then ;',
  13289. ' if 16=inherited {@b}GetValue(17) then ;',
  13290. 'end;',
  13291. 'function TEagle.GetValue(AValue: longint): longint;',
  13292. 'begin',
  13293. 'end;',
  13294. 'var',
  13295. ' e: TEagle;',
  13296. 'begin',
  13297. ' if 23=e.{@c}GetValue then ;',
  13298. ' if 24=e.{@d}GetValue(25) then ;']);
  13299. ConvertProgram;
  13300. CheckSource('TestClass_OverloadDelphiOverride',
  13301. LinesToStr([ // statements
  13302. 'rtl.createClass($mod, "TObject", null, function () {',
  13303. ' this.$init = function () {',
  13304. ' };',
  13305. ' this.$final = function () {',
  13306. ' };',
  13307. '});',
  13308. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13309. ' this.GetValue = function () {',
  13310. ' var Result = 0;',
  13311. ' if (3 === this.GetValue()) ;',
  13312. ' if (4 === this.GetValue$1(5)) ;',
  13313. ' return Result;',
  13314. ' };',
  13315. ' this.GetValue$1 = function (AValue) {',
  13316. ' var Result = 0;',
  13317. ' return Result;',
  13318. ' };',
  13319. '});',
  13320. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13321. ' this.GetValue = function () {',
  13322. ' var Result = 0;',
  13323. ' if (13 === this.GetValue()) ;',
  13324. ' if (14 === this.GetValue$1(15)) ;',
  13325. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13326. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13327. ' return Result;',
  13328. ' };',
  13329. ' this.GetValue$1 = function (AValue) {',
  13330. ' var Result = 0;',
  13331. ' return Result;',
  13332. ' };',
  13333. '});',
  13334. 'this.e = null;',
  13335. '']),
  13336. LinesToStr([ // $mod.$main
  13337. 'if (23 === $mod.e.GetValue()) ;',
  13338. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13339. '']));
  13340. end;
  13341. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  13342. begin
  13343. StartProgram(false);
  13344. Add([
  13345. '{$mode delphi}',
  13346. 'type',
  13347. ' TObject = class end;',
  13348. ' TAnimal = class',
  13349. ' public',
  13350. ' {#animal_a}A: longint;',
  13351. ' function {#animal_b}B: longint;',
  13352. ' end;',
  13353. ' TBird = class(TAnimal)',
  13354. ' public',
  13355. ' {#bird_a}A: double;',
  13356. ' {#bird_b}B: boolean;',
  13357. ' end;',
  13358. ' TEagle = class(TBird)',
  13359. ' public',
  13360. ' function {#eagle_a}A: boolean;',
  13361. ' {#eagle_b}B: double;',
  13362. ' end;',
  13363. 'function TAnimal.B: longint;',
  13364. 'begin',
  13365. 'end;',
  13366. 'function TEagle.A: boolean;',
  13367. 'begin',
  13368. ' {@eagle_b}B:=3.3;',
  13369. ' {@eagle_a}A();',
  13370. ' TBird(Self).{@bird_b}B:=true;',
  13371. ' TAnimal(Self).{@animal_a}A:=17;',
  13372. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  13373. 'end;',
  13374. 'var',
  13375. ' e: TEagle;',
  13376. 'begin',
  13377. ' e.{@eagle_b}B:=5.3;',
  13378. ' if e.{@eagle_a}A then ;',
  13379. '']);
  13380. ConvertProgram;
  13381. CheckSource('TestClass_ReintroduceVarDelphi',
  13382. LinesToStr([ // statements
  13383. 'rtl.createClass($mod, "TObject", null, function () {',
  13384. ' this.$init = function () {',
  13385. ' };',
  13386. ' this.$final = function () {',
  13387. ' };',
  13388. '});',
  13389. 'rtl.createClass($mod, "TAnimal", $mod.TObject, function () {',
  13390. ' this.$init = function () {',
  13391. ' $mod.TObject.$init.call(this);',
  13392. ' this.A = 0;',
  13393. ' };',
  13394. ' this.B = function () {',
  13395. ' var Result = 0;',
  13396. ' return Result;',
  13397. ' };',
  13398. '});',
  13399. 'rtl.createClass($mod, "TBird", $mod.TAnimal, function () {',
  13400. ' this.$init = function () {',
  13401. ' $mod.TAnimal.$init.call(this);',
  13402. ' this.A$1 = 0.0;',
  13403. ' this.B$1 = false;',
  13404. ' };',
  13405. '});',
  13406. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13407. ' this.$init = function () {',
  13408. ' $mod.TBird.$init.call(this);',
  13409. ' this.B$2 = 0.0;',
  13410. ' };',
  13411. ' this.A$2 = function () {',
  13412. ' var Result = false;',
  13413. ' this.B$2 = 3.3;',
  13414. ' this.A$2();',
  13415. ' this.B$1 = true;',
  13416. ' this.A = 17;',
  13417. ' this.B$1 = this.A$1 > 1;',
  13418. ' return Result;',
  13419. ' };',
  13420. '});',
  13421. 'this.e = null;',
  13422. '']),
  13423. LinesToStr([ // $mod.$main
  13424. '$mod.e.B$2 = 5.3;',
  13425. 'if ($mod.e.A$2()) ;',
  13426. '']));
  13427. end;
  13428. procedure TTestModule.TestClass_ReintroducedVar;
  13429. begin
  13430. StartProgram(false);
  13431. Add('type');
  13432. Add(' TObject = class');
  13433. Add(' strict private');
  13434. Add(' Some: longint;');
  13435. Add(' end;');
  13436. Add(' TMobile = class');
  13437. Add(' strict private');
  13438. Add(' Some: string;');
  13439. Add(' end;');
  13440. Add(' TCar = class(tmobile)');
  13441. Add(' procedure Some;');
  13442. Add(' procedure Some(vA: longint);');
  13443. Add(' end;');
  13444. Add('procedure tcar.some;');
  13445. Add('begin');
  13446. Add(' Some;');
  13447. Add(' Some(1);');
  13448. Add('end;');
  13449. Add('procedure tcar.some(va: longint); begin end;');
  13450. Add('begin');
  13451. ConvertProgram;
  13452. CheckSource('TestClass_ReintroducedVar',
  13453. LinesToStr([ // statements
  13454. 'rtl.createClass($mod, "TObject", null, function () {',
  13455. ' this.$init = function () {',
  13456. ' this.Some = 0;',
  13457. ' };',
  13458. ' this.$final = function () {',
  13459. ' };',
  13460. '});',
  13461. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13462. ' this.$init = function () {',
  13463. ' $mod.TObject.$init.call(this);',
  13464. ' this.Some$1 = "";',
  13465. ' };',
  13466. '});',
  13467. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13468. ' this.Some$2 = function () {',
  13469. ' this.Some$2();',
  13470. ' this.Some$3(1);',
  13471. ' };',
  13472. ' this.Some$3 = function (vA) {',
  13473. ' };',
  13474. '});',
  13475. '']),
  13476. LinesToStr([ // $mod.$main
  13477. '']));
  13478. end;
  13479. procedure TTestModule.TestClass_RaiseDescendant;
  13480. begin
  13481. StartProgram(false);
  13482. Add([
  13483. 'type',
  13484. ' TObject = class',
  13485. ' constructor Create(Msg: string);',
  13486. ' end;',
  13487. ' Exception = class',
  13488. ' end;',
  13489. ' EConvertError = class(Exception)',
  13490. ' end;',
  13491. 'constructor TObject.Create(Msg: string); begin end;',
  13492. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  13493. 'begin',
  13494. ' raise Exception.Create(''Bar1'');',
  13495. ' raise EConvertError.Create(''Bar2'');',
  13496. ' raise AssertConv(''Bar2'');',
  13497. ' raise AssertConv;',
  13498. '']);
  13499. ConvertProgram;
  13500. CheckSource('TestClass_RaiseDescendant',
  13501. LinesToStr([ // statements
  13502. 'rtl.createClass($mod, "TObject", null, function () {',
  13503. ' this.$init = function () {',
  13504. ' };',
  13505. ' this.$final = function () {',
  13506. ' };',
  13507. ' this.Create = function (Msg) {',
  13508. ' return this;',
  13509. ' };',
  13510. '});',
  13511. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  13512. '});',
  13513. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  13514. '});',
  13515. 'this.AssertConv = function (Msg) {',
  13516. ' var Result = null;',
  13517. ' return Result;',
  13518. '};',
  13519. '']),
  13520. LinesToStr([ // $mod.$main
  13521. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  13522. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  13523. 'throw $mod.AssertConv("Bar2");',
  13524. 'throw $mod.AssertConv("def");',
  13525. '']));
  13526. end;
  13527. procedure TTestModule.TestClass_ExternalMethod;
  13528. begin
  13529. AddModuleWithIntfImplSrc('unit2.pas',
  13530. LinesToStr([
  13531. 'type',
  13532. ' TObject = class',
  13533. ' public',
  13534. ' procedure Intern; external name ''$DoIntern'';',
  13535. ' end;',
  13536. '']),
  13537. LinesToStr([
  13538. '']));
  13539. StartUnit(true);
  13540. Add('interface');
  13541. Add('uses unit2;');
  13542. Add('type');
  13543. Add(' TCar = class(TObject)');
  13544. Add(' public');
  13545. Add(' procedure Intern2; external name ''$DoIntern2'';');
  13546. Add(' procedure DoIt;');
  13547. Add(' end;');
  13548. Add('implementation');
  13549. Add('procedure tcar.doit;');
  13550. Add('begin');
  13551. Add(' Intern;');
  13552. Add(' Intern();');
  13553. Add(' Intern2;');
  13554. Add(' Intern2();');
  13555. Add('end;');
  13556. Add('var Obj: TCar;');
  13557. Add('begin');
  13558. Add(' obj.intern;');
  13559. Add(' obj.intern();');
  13560. Add(' obj.intern2;');
  13561. Add(' obj.intern2();');
  13562. Add(' obj.doit;');
  13563. Add(' obj.doit();');
  13564. Add(' with obj do begin');
  13565. Add(' Intern;');
  13566. Add(' Intern();');
  13567. Add(' Intern2;');
  13568. Add(' Intern2();');
  13569. Add(' end;');
  13570. ConvertUnit;
  13571. CheckSource('TestClass_ExternalMethod',
  13572. LinesToStr([
  13573. 'var $impl = $mod.$impl;',
  13574. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13575. ' this.DoIt = function () {',
  13576. ' this.$DoIntern();',
  13577. ' this.$DoIntern();',
  13578. ' this.$DoIntern2();',
  13579. ' this.$DoIntern2();',
  13580. ' };',
  13581. ' });',
  13582. '']),
  13583. LinesToStr([ // this.$init
  13584. '$impl.Obj.$DoIntern();',
  13585. '$impl.Obj.$DoIntern();',
  13586. '$impl.Obj.$DoIntern2();',
  13587. '$impl.Obj.$DoIntern2();',
  13588. '$impl.Obj.DoIt();',
  13589. '$impl.Obj.DoIt();',
  13590. 'var $with1 = $impl.Obj;',
  13591. '$with1.$DoIntern();',
  13592. '$with1.$DoIntern();',
  13593. '$with1.$DoIntern2();',
  13594. '$with1.$DoIntern2();',
  13595. '']),
  13596. LinesToStr([ // implementation
  13597. '$impl.Obj = null;',
  13598. '']) );
  13599. end;
  13600. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  13601. begin
  13602. StartProgram(false);
  13603. Add('type');
  13604. Add(' TObject = class');
  13605. Add(' procedure DoIt; virtual; external name ''Foo'';');
  13606. Add(' end;');
  13607. Add('begin');
  13608. SetExpectedPasResolverError('Virtual method name must match external',
  13609. nVirtualMethodNameMustMatchExternal);
  13610. ConvertProgram;
  13611. end;
  13612. procedure TTestModule.TestClass_ExternalOverrideFail;
  13613. begin
  13614. StartProgram(false);
  13615. Add('type');
  13616. Add(' TObject = class');
  13617. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  13618. Add(' end;');
  13619. Add(' TCar = class');
  13620. Add(' procedure DoIt; override; external name ''DoIt'';');
  13621. Add(' end;');
  13622. Add('begin');
  13623. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  13624. nInvalidXModifierY);
  13625. ConvertProgram;
  13626. end;
  13627. procedure TTestModule.TestClass_ExternalVar;
  13628. begin
  13629. AddModuleWithIntfImplSrc('unit2.pas',
  13630. LinesToStr([
  13631. '{$modeswitch externalclass}',
  13632. 'type',
  13633. ' TObject = class',
  13634. ' public',
  13635. ' Intern: longint external name ''$Intern'';',
  13636. ' Bracket: longint external name ''["A B"]'';',
  13637. ' end;',
  13638. '']),
  13639. LinesToStr([
  13640. '']));
  13641. StartUnit(true);
  13642. Add([
  13643. 'interface',
  13644. 'uses unit2;',
  13645. '{$modeswitch externalclass}',
  13646. 'type',
  13647. ' TCar = class(tobject)',
  13648. ' public',
  13649. ' Intern2: longint external name ''$Intern2'';',
  13650. ' procedure DoIt;',
  13651. ' end;',
  13652. 'implementation',
  13653. 'procedure tcar.doit;',
  13654. 'begin',
  13655. ' Intern:=Intern+1;',
  13656. ' Intern2:=Intern2+2;',
  13657. ' Bracket:=Bracket+3;',
  13658. 'end;',
  13659. 'var Obj: TCar;',
  13660. 'begin',
  13661. ' obj.intern:=obj.intern+1;',
  13662. ' obj.intern2:=obj.intern2+2;',
  13663. ' obj.Bracket:=obj.Bracket+3;',
  13664. ' with obj do begin',
  13665. ' intern:=intern+1;',
  13666. ' intern2:=intern2+2;',
  13667. ' Bracket:=Bracket+3;',
  13668. ' end;']);
  13669. ConvertUnit;
  13670. CheckSource('TestClass_ExternalVar',
  13671. LinesToStr([
  13672. 'var $impl = $mod.$impl;',
  13673. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13674. ' this.DoIt = function () {',
  13675. ' this.$Intern = this.$Intern + 1;',
  13676. ' this.$Intern2 = this.$Intern2 + 2;',
  13677. ' this["A B"] = this["A B"] + 3;',
  13678. ' };',
  13679. ' });',
  13680. '']),
  13681. LinesToStr([
  13682. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  13683. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  13684. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  13685. 'var $with1 = $impl.Obj;',
  13686. '$with1.$Intern = $with1.$Intern + 1;',
  13687. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  13688. '$with1["A B"] = $with1["A B"] + 3;',
  13689. '']),
  13690. LinesToStr([ // implementation
  13691. '$impl.Obj = null;',
  13692. '']));
  13693. end;
  13694. procedure TTestModule.TestClass_Const;
  13695. begin
  13696. StartProgram(false);
  13697. Add('type');
  13698. Add(' integer = longint;');
  13699. Add(' TClass = class of TObject;');
  13700. Add(' TObject = class');
  13701. Add(' public');
  13702. Add(' const cI: integer = 3;');
  13703. Add(' procedure DoIt;');
  13704. Add(' class procedure DoMore;');
  13705. Add(' end;');
  13706. Add('implementation');
  13707. Add('procedure tobject.doit;');
  13708. Add('begin');
  13709. Add(' if cI=4 then;');
  13710. Add(' if 5=cI then;');
  13711. Add(' if Self.cI=6 then;');
  13712. Add(' if 7=Self.cI then;');
  13713. Add(' with Self do begin');
  13714. Add(' if cI=11 then;');
  13715. Add(' if 12=cI then;');
  13716. Add(' end;');
  13717. Add('end;');
  13718. Add('class procedure tobject.domore;');
  13719. Add('begin');
  13720. Add(' if cI=8 then;');
  13721. Add(' if Self.cI=9 then;');
  13722. Add(' if 10=cI then;');
  13723. Add(' if 11=Self.cI then;');
  13724. Add(' with Self do begin');
  13725. Add(' if cI=13 then;');
  13726. Add(' if 14=cI then;');
  13727. Add(' end;');
  13728. Add('end;');
  13729. Add('var');
  13730. Add(' Obj: TObject;');
  13731. Add(' Cla: TClass;');
  13732. Add('begin');
  13733. Add(' if TObject.cI=21 then ;');
  13734. Add(' if Obj.cI=22 then ;');
  13735. Add(' if Cla.cI=23 then ;');
  13736. Add(' with obj do if ci=24 then;');
  13737. Add(' with TObject do if ci=25 then;');
  13738. Add(' with Cla do if ci=26 then;');
  13739. ConvertProgram;
  13740. CheckSource('TestClass_Const',
  13741. LinesToStr([
  13742. 'rtl.createClass($mod, "TObject", null, function () {',
  13743. ' this.cI = 3;',
  13744. ' this.$init = function () {',
  13745. ' };',
  13746. ' this.$final = function () {',
  13747. ' };',
  13748. ' this.DoIt = function () {',
  13749. ' if (this.cI === 4) ;',
  13750. ' if (5 === this.cI) ;',
  13751. ' if (this.cI === 6) ;',
  13752. ' if (7 === this.cI) ;',
  13753. ' if (this.cI === 11) ;',
  13754. ' if (12 === this.cI) ;',
  13755. ' };',
  13756. ' this.DoMore = function () {',
  13757. ' if (this.cI === 8) ;',
  13758. ' if (this.cI === 9) ;',
  13759. ' if (10 === this.cI) ;',
  13760. ' if (11 === this.cI) ;',
  13761. ' if (this.cI === 13) ;',
  13762. ' if (14 === this.cI) ;',
  13763. ' };',
  13764. '});',
  13765. 'this.Obj = null;',
  13766. 'this.Cla = null;',
  13767. '']),
  13768. LinesToStr([
  13769. 'if ($mod.TObject.cI === 21) ;',
  13770. 'if ($mod.Obj.cI === 22) ;',
  13771. 'if ($mod.Cla.cI === 23) ;',
  13772. 'var $with1 = $mod.Obj;',
  13773. 'if ($with1.cI === 24) ;',
  13774. 'var $with2 = $mod.TObject;',
  13775. 'if ($with2.cI === 25) ;',
  13776. 'var $with3 = $mod.Cla;',
  13777. 'if ($with3.cI === 26) ;',
  13778. '']));
  13779. end;
  13780. procedure TTestModule.TestClass_LocalVarSelfFail;
  13781. begin
  13782. StartProgram(false);
  13783. Add([
  13784. 'type',
  13785. ' TObject = class',
  13786. ' constructor Create;',
  13787. ' end;',
  13788. 'constructor tobject.create;',
  13789. 'var self: longint;',
  13790. 'begin',
  13791. 'end',
  13792. 'begin',
  13793. '']);
  13794. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  13795. ConvertProgram;
  13796. end;
  13797. procedure TTestModule.TestClass_ArgSelfFail;
  13798. begin
  13799. StartProgram(false);
  13800. Add([
  13801. 'type',
  13802. ' TObject = class',
  13803. ' procedure DoIt(Self: longint);',
  13804. ' end;',
  13805. 'procedure tobject.doit(self: longint);',
  13806. 'begin',
  13807. 'end',
  13808. 'begin',
  13809. '']);
  13810. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  13811. ConvertProgram;
  13812. end;
  13813. procedure TTestModule.TestClass_NestedProcSelf;
  13814. begin
  13815. StartProgram(false);
  13816. Add([
  13817. 'type',
  13818. ' TObject = class',
  13819. ' Key: longint;',
  13820. ' class var State: longint;',
  13821. ' procedure DoIt;',
  13822. ' function GetSize: longint; virtual; abstract;',
  13823. ' procedure SetSize(Value: longint); virtual; abstract;',
  13824. ' property Size: longint read GetSize write SetSize;',
  13825. ' end;',
  13826. 'procedure tobject.doit;',
  13827. ' procedure Sub;',
  13828. ' begin',
  13829. ' key:=key+2;',
  13830. ' self.key:=self.key+3;',
  13831. ' state:=state+4;',
  13832. ' self.state:=self.state+5;',
  13833. ' tobject.state:=tobject.state+6;',
  13834. ' size:=size+7;',
  13835. ' self.size:=self.size+8;',
  13836. ' end;',
  13837. 'begin',
  13838. ' sub;',
  13839. ' key:=key+12;',
  13840. ' self.key:=self.key+13;',
  13841. ' state:=state+14;',
  13842. ' self.state:=self.state+15;',
  13843. ' tobject.state:=tobject.state+16;',
  13844. ' size:=size+17;',
  13845. ' self.size:=self.size+18;',
  13846. 'end;',
  13847. 'begin',
  13848. '']);
  13849. ConvertProgram;
  13850. CheckSource('TestClass_NestedProcSelf',
  13851. LinesToStr([ // statements
  13852. 'rtl.createClass($mod, "TObject", null, function () {',
  13853. ' this.State = 0;',
  13854. ' this.$init = function () {',
  13855. ' this.Key = 0;',
  13856. ' };',
  13857. ' this.$final = function () {',
  13858. ' };',
  13859. ' this.DoIt = function () {',
  13860. ' var $Self = this;',
  13861. ' function Sub() {',
  13862. ' $Self.Key = $Self.Key + 2;',
  13863. ' $Self.Key = $Self.Key + 3;',
  13864. ' $mod.TObject.State = $Self.State + 4;',
  13865. ' $mod.TObject.State = $Self.State + 5;',
  13866. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13867. ' $Self.SetSize($Self.GetSize() + 7);',
  13868. ' $Self.SetSize($Self.GetSize() + 8);',
  13869. ' };',
  13870. ' Sub();',
  13871. ' $Self.Key = $Self.Key + 12;',
  13872. ' $Self.Key = $Self.Key + 13;',
  13873. ' $mod.TObject.State = $Self.State + 14;',
  13874. ' $mod.TObject.State = $Self.State + 15;',
  13875. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13876. ' $Self.SetSize($Self.GetSize() + 17);',
  13877. ' $Self.SetSize($Self.GetSize() + 18);',
  13878. ' };',
  13879. '});',
  13880. '']),
  13881. LinesToStr([ // $mod.$main
  13882. '']));
  13883. end;
  13884. procedure TTestModule.TestClass_NestedProcSelf2;
  13885. begin
  13886. StartProgram(false);
  13887. Add([
  13888. 'type',
  13889. ' TObject = class',
  13890. ' Key: longint;',
  13891. ' class var State: longint;',
  13892. ' function GetSize: longint; virtual; abstract;',
  13893. ' procedure SetSize(Value: longint); virtual; abstract;',
  13894. ' property Size: longint read GetSize write SetSize;',
  13895. ' end;',
  13896. ' TBird = class',
  13897. ' procedure DoIt;',
  13898. ' end;',
  13899. 'procedure tbird.doit;',
  13900. ' procedure Sub;',
  13901. ' begin',
  13902. ' key:=key+2;',
  13903. ' self.key:=self.key+3;',
  13904. ' state:=state+4;',
  13905. ' self.state:=self.state+5;',
  13906. ' tobject.state:=tobject.state+6;',
  13907. ' size:=size+7;',
  13908. ' self.size:=self.size+8;',
  13909. ' end;',
  13910. 'begin',
  13911. ' sub;',
  13912. ' key:=key+12;',
  13913. ' self.key:=self.key+13;',
  13914. ' state:=state+14;',
  13915. ' self.state:=self.state+15;',
  13916. ' tobject.state:=tobject.state+16;',
  13917. ' size:=size+17;',
  13918. ' self.size:=self.size+18;',
  13919. 'end;',
  13920. 'begin',
  13921. '']);
  13922. ConvertProgram;
  13923. CheckSource('TestClass_NestedProcSelf2',
  13924. LinesToStr([ // statements
  13925. 'rtl.createClass($mod, "TObject", null, function () {',
  13926. ' this.State = 0;',
  13927. ' this.$init = function () {',
  13928. ' this.Key = 0;',
  13929. ' };',
  13930. ' this.$final = function () {',
  13931. ' };',
  13932. '});',
  13933. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13934. ' this.DoIt = function () {',
  13935. ' var $Self = this;',
  13936. ' function Sub() {',
  13937. ' $Self.Key = $Self.Key + 2;',
  13938. ' $Self.Key = $Self.Key + 3;',
  13939. ' $mod.TObject.State = $Self.State + 4;',
  13940. ' $mod.TObject.State = $Self.State + 5;',
  13941. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13942. ' $Self.SetSize($Self.GetSize() + 7);',
  13943. ' $Self.SetSize($Self.GetSize() + 8);',
  13944. ' };',
  13945. ' Sub();',
  13946. ' $Self.Key = $Self.Key + 12;',
  13947. ' $Self.Key = $Self.Key + 13;',
  13948. ' $mod.TObject.State = $Self.State + 14;',
  13949. ' $mod.TObject.State = $Self.State + 15;',
  13950. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13951. ' $Self.SetSize($Self.GetSize() + 17);',
  13952. ' $Self.SetSize($Self.GetSize() + 18);',
  13953. ' };',
  13954. '});',
  13955. '']),
  13956. LinesToStr([ // $mod.$main
  13957. '']));
  13958. end;
  13959. procedure TTestModule.TestClass_NestedProcClassSelf;
  13960. begin
  13961. StartProgram(false);
  13962. Add([
  13963. 'type',
  13964. ' TObject = class',
  13965. ' class var State: longint;',
  13966. ' class procedure DoIt;',
  13967. ' class function GetSize: longint; virtual; abstract;',
  13968. ' class procedure SetSize(Value: longint); virtual; abstract;',
  13969. ' class property Size: longint read GetSize write SetSize;',
  13970. ' end;',
  13971. 'class procedure tobject.doit;',
  13972. ' procedure Sub;',
  13973. ' begin',
  13974. ' state:=state+2;',
  13975. ' self.state:=self.state+3;',
  13976. ' tobject.state:=tobject.state+4;',
  13977. ' size:=size+5;',
  13978. ' self.size:=self.size+6;',
  13979. ' tobject.size:=tobject.size+7;',
  13980. ' end;',
  13981. 'begin',
  13982. ' sub;',
  13983. ' state:=state+12;',
  13984. ' self.state:=self.state+13;',
  13985. ' tobject.state:=tobject.state+14;',
  13986. ' size:=size+15;',
  13987. ' self.size:=self.size+16;',
  13988. ' tobject.size:=tobject.size+17;',
  13989. 'end;',
  13990. 'begin',
  13991. '']);
  13992. ConvertProgram;
  13993. CheckSource('TestClass_NestedProcClassSelf',
  13994. LinesToStr([ // statements
  13995. 'rtl.createClass($mod, "TObject", null, function () {',
  13996. ' this.State = 0;',
  13997. ' this.$init = function () {',
  13998. ' };',
  13999. ' this.$final = function () {',
  14000. ' };',
  14001. ' this.DoIt = function () {',
  14002. ' var $Self = this;',
  14003. ' function Sub() {',
  14004. ' $mod.TObject.State = $Self.State + 2;',
  14005. ' $mod.TObject.State = $Self.State + 3;',
  14006. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14007. ' $Self.SetSize($Self.GetSize() + 5);',
  14008. ' $Self.SetSize($Self.GetSize() + 6);',
  14009. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  14010. ' };',
  14011. ' Sub();',
  14012. ' $mod.TObject.State = $Self.State + 12;',
  14013. ' $mod.TObject.State = $Self.State + 13;',
  14014. ' $mod.TObject.State = $mod.TObject.State + 14;',
  14015. ' $Self.SetSize($Self.GetSize() + 15);',
  14016. ' $Self.SetSize($Self.GetSize() + 16);',
  14017. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  14018. ' };',
  14019. '});',
  14020. '']),
  14021. LinesToStr([ // $mod.$main
  14022. '']));
  14023. end;
  14024. procedure TTestModule.TestClass_NestedProcCallInherited;
  14025. begin
  14026. StartProgram(false);
  14027. Add([
  14028. 'type',
  14029. ' TObject = class',
  14030. ' function DoIt(k: boolean): longint; virtual;',
  14031. ' end;',
  14032. ' TBird = class',
  14033. ' function DoIt(k: boolean): longint; override;',
  14034. ' end;',
  14035. 'function tobject.doit(k: boolean): longint;',
  14036. 'begin',
  14037. 'end;',
  14038. 'function tbird.doit(k: boolean): longint;',
  14039. ' procedure Sub;',
  14040. ' begin',
  14041. ' inherited DoIt(true);',
  14042. //' if inherited DoIt(false)=4 then ;',
  14043. ' end;',
  14044. 'begin',
  14045. ' Sub;',
  14046. ' inherited;',
  14047. ' inherited DoIt(true);',
  14048. //' if inherited DoIt(false)=14 then ;',
  14049. 'end;',
  14050. 'begin',
  14051. '']);
  14052. ConvertProgram;
  14053. CheckSource('TestClass_NestedProcCallInherited',
  14054. LinesToStr([ // statements
  14055. 'rtl.createClass($mod, "TObject", null, function () {',
  14056. ' this.$init = function () {',
  14057. ' };',
  14058. ' this.$final = function () {',
  14059. ' };',
  14060. ' this.DoIt = function (k) {',
  14061. ' var Result = 0;',
  14062. ' return Result;',
  14063. ' };',
  14064. '});',
  14065. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14066. ' this.DoIt = function (k) {',
  14067. ' var $Self = this;',
  14068. ' var Result = 0;',
  14069. ' function Sub() {',
  14070. ' $mod.TObject.DoIt.call($Self, true);',
  14071. ' };',
  14072. ' Sub();',
  14073. ' $mod.TObject.DoIt.apply($Self, arguments);',
  14074. ' $mod.TObject.DoIt.call($Self, true);',
  14075. ' return Result;',
  14076. ' };',
  14077. '});',
  14078. '']),
  14079. LinesToStr([ // $mod.$main
  14080. '']));
  14081. end;
  14082. procedure TTestModule.TestClass_TObjectFree;
  14083. begin
  14084. StartProgram(false);
  14085. Add([
  14086. 'type',
  14087. ' TObject = class',
  14088. ' Obj: tobject;',
  14089. ' procedure Free;',
  14090. ' procedure Release;',
  14091. ' end;',
  14092. 'procedure tobject.free;',
  14093. 'begin',
  14094. 'end;',
  14095. 'procedure tobject.release;',
  14096. 'begin',
  14097. ' free;',
  14098. ' if true then free;',
  14099. 'end;',
  14100. 'function DoIt(o: tobject): tobject;',
  14101. 'var l: tobject;',
  14102. 'begin',
  14103. ' o.free;',
  14104. ' o.free();',
  14105. ' l.free;',
  14106. ' l.free();',
  14107. ' o.obj.free;',
  14108. ' o.obj.free();',
  14109. ' with o do obj.free;',
  14110. ' with o do obj.free();',
  14111. ' result.Free;',
  14112. ' result.Free();',
  14113. 'end;',
  14114. 'var o: tobject;',
  14115. ' a: array of tobject;',
  14116. 'begin',
  14117. ' o.free;',
  14118. ' o.obj.free;',
  14119. ' a[1+2].free;',
  14120. '']);
  14121. ConvertProgram;
  14122. CheckSource('TestClass_TObjectFree',
  14123. LinesToStr([ // statements
  14124. 'rtl.createClass($mod, "TObject", null, function () {',
  14125. ' this.$init = function () {',
  14126. ' this.Obj = null;',
  14127. ' };',
  14128. ' this.$final = function () {',
  14129. ' this.Obj = undefined;',
  14130. ' };',
  14131. ' this.Free = function () {',
  14132. ' };',
  14133. ' this.Release = function () {',
  14134. ' this.Free();',
  14135. ' if (true) this.Free();',
  14136. ' };',
  14137. '});',
  14138. 'this.DoIt = function (o) {',
  14139. ' var Result = null;',
  14140. ' var l = null;',
  14141. ' o = rtl.freeLoc(o);',
  14142. ' o = rtl.freeLoc(o);',
  14143. ' l = rtl.freeLoc(l);',
  14144. ' l = rtl.freeLoc(l);',
  14145. ' rtl.free(o, "Obj");',
  14146. ' rtl.free(o, "Obj");',
  14147. ' rtl.free(o, "Obj");',
  14148. ' rtl.free(o, "Obj");',
  14149. ' Result = rtl.freeLoc(Result);',
  14150. ' Result = rtl.freeLoc(Result);',
  14151. ' return Result;',
  14152. '};',
  14153. 'this.o = null;',
  14154. 'this.a = [];',
  14155. '']),
  14156. LinesToStr([ // $mod.$main
  14157. 'rtl.free($mod, "o");',
  14158. 'rtl.free($mod.o, "Obj");',
  14159. 'rtl.free($mod.a, 1 + 2);',
  14160. '']));
  14161. end;
  14162. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  14163. begin
  14164. StartProgram(false);
  14165. Add([
  14166. 'type',
  14167. ' TObject = class',
  14168. ' constructor Create;',
  14169. ' procedure Free;',
  14170. ' end;',
  14171. 'constructor TObject.Create; begin end;',
  14172. 'procedure tobject.free; begin end;',
  14173. 'begin',
  14174. ' with tobject.create do free;',
  14175. '']);
  14176. ConvertProgram;
  14177. CheckSource('TestClass_TObjectFreeNewInstance',
  14178. LinesToStr([ // statements
  14179. 'rtl.createClass($mod, "TObject", null, function () {',
  14180. ' this.$init = function () {',
  14181. ' };',
  14182. ' this.$final = function () {',
  14183. ' };',
  14184. ' this.Create = function () {',
  14185. ' return this;',
  14186. ' };',
  14187. ' this.Free = function () {',
  14188. ' };',
  14189. '});',
  14190. '']),
  14191. LinesToStr([ // $mod.$main
  14192. 'var $with1 = $mod.TObject.$create("Create");',
  14193. '$with1=rtl.freeLoc($with1);',
  14194. '']));
  14195. end;
  14196. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  14197. begin
  14198. StartProgram(false);
  14199. Add([
  14200. 'type',
  14201. ' TObject = class',
  14202. ' destructor Destroy;',
  14203. ' procedure Free;',
  14204. ' end;',
  14205. 'destructor TObject.Destroy; begin end;',
  14206. 'procedure tobject.free; begin end;',
  14207. 'var o: tobject;',
  14208. 'begin',
  14209. ' o.free;',
  14210. '']);
  14211. Converter.UseLowerCase:=true;
  14212. ConvertProgram;
  14213. CheckSource('TestClass_TObjectFreeLowerCase',
  14214. LinesToStr([ // statements
  14215. 'rtl.createClass($mod, "tobject", null, function () {',
  14216. ' this.$init = function () {',
  14217. ' };',
  14218. ' this.$final = function () {',
  14219. ' };',
  14220. ' rtl.tObjectDestroy = "destroy";',
  14221. ' this.destroy = function () {',
  14222. ' };',
  14223. ' this.free = function () {',
  14224. ' };',
  14225. '});',
  14226. 'this.o = null;',
  14227. '']),
  14228. LinesToStr([ // $mod.$main
  14229. 'rtl.free($mod, "o");',
  14230. '']));
  14231. end;
  14232. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  14233. begin
  14234. StartProgram(false);
  14235. Add([
  14236. 'type',
  14237. ' TObject = class',
  14238. ' procedure Free;',
  14239. ' function GetObj: tobject; virtual; abstract;',
  14240. ' end;',
  14241. 'procedure tobject.free;',
  14242. 'begin',
  14243. 'end;',
  14244. 'var o: tobject;',
  14245. 'begin',
  14246. ' o.getobj.free;',
  14247. '']);
  14248. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14249. ConvertProgram;
  14250. end;
  14251. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  14252. begin
  14253. StartProgram(false);
  14254. Add([
  14255. 'type',
  14256. ' TObject = class',
  14257. ' procedure Free;',
  14258. ' FObj: TObject;',
  14259. ' property Obj: tobject read FObj write FObj;',
  14260. ' end;',
  14261. 'procedure tobject.free;',
  14262. 'begin',
  14263. 'end;',
  14264. 'var o: tobject;',
  14265. 'begin',
  14266. ' o.obj.free;',
  14267. '']);
  14268. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14269. ConvertProgram;
  14270. end;
  14271. procedure TTestModule.TestClass_ForIn;
  14272. begin
  14273. StartProgram(false);
  14274. Add([
  14275. 'type',
  14276. ' TObject = class end;',
  14277. ' TItem = TObject;',
  14278. ' TEnumerator = class',
  14279. ' FCurrent: TItem;',
  14280. ' property Current: TItem read FCurrent;',
  14281. ' function MoveNext: boolean;',
  14282. ' end;',
  14283. ' TBird = class',
  14284. ' function GetEnumerator: TEnumerator;',
  14285. ' end;',
  14286. 'function TEnumerator.MoveNext: boolean;',
  14287. 'begin',
  14288. 'end;',
  14289. 'function TBird.GetEnumerator: TEnumerator;',
  14290. 'begin',
  14291. 'end;',
  14292. 'var',
  14293. ' b: TBird;',
  14294. ' i, i2: TItem;',
  14295. 'begin',
  14296. ' for i in b do i2:=i;']);
  14297. ConvertProgram;
  14298. CheckSource('TestClass_ForIn',
  14299. LinesToStr([ // statements
  14300. 'rtl.createClass($mod, "TObject", null, function () {',
  14301. ' this.$init = function () {',
  14302. ' };',
  14303. ' this.$final = function () {',
  14304. ' };',
  14305. '});',
  14306. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  14307. ' this.$init = function () {',
  14308. ' $mod.TObject.$init.call(this);',
  14309. ' this.FCurrent = null;',
  14310. ' };',
  14311. ' this.$final = function () {',
  14312. ' this.FCurrent = undefined;',
  14313. ' $mod.TObject.$final.call(this);',
  14314. ' };',
  14315. ' this.MoveNext = function () {',
  14316. ' var Result = false;',
  14317. ' return Result;',
  14318. ' };',
  14319. '});',
  14320. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14321. ' this.GetEnumerator = function () {',
  14322. ' var Result = null;',
  14323. ' return Result;',
  14324. ' };',
  14325. '});',
  14326. 'this.b = null;',
  14327. 'this.i = null;',
  14328. 'this.i2 = null;'
  14329. ]),
  14330. LinesToStr([ // $mod.$main
  14331. 'var $in1 = $mod.b.GetEnumerator();',
  14332. 'try {',
  14333. ' while ($in1.MoveNext()){',
  14334. ' $mod.i = $in1.FCurrent;',
  14335. ' $mod.i2 = $mod.i;',
  14336. ' }',
  14337. '} finally {',
  14338. ' $in1 = rtl.freeLoc($in1)',
  14339. '};',
  14340. '']));
  14341. end;
  14342. procedure TTestModule.TestClassOf_Create;
  14343. begin
  14344. StartProgram(false);
  14345. Add('type');
  14346. Add(' TObject = class');
  14347. Add(' constructor Create;');
  14348. Add(' end;');
  14349. Add(' TClass = class of TObject;');
  14350. Add('constructor tobject.create; begin end;');
  14351. Add('var');
  14352. Add(' Obj: tobject;');
  14353. Add(' C: tclass;');
  14354. Add('begin');
  14355. Add(' obj:=C.create;');
  14356. Add(' with c do obj:=create;');
  14357. ConvertProgram;
  14358. CheckSource('TestClassOf_Create',
  14359. LinesToStr([ // statements
  14360. 'rtl.createClass($mod, "TObject", null, function () {',
  14361. ' this.$init = function () {',
  14362. ' };',
  14363. ' this.$final = function () {',
  14364. ' };',
  14365. ' this.Create = function () {',
  14366. ' return this;',
  14367. ' };',
  14368. '});',
  14369. 'this.Obj = null;',
  14370. 'this.C = null;'
  14371. ]),
  14372. LinesToStr([ // $mod.$main
  14373. '$mod.Obj = $mod.C.$create("Create");',
  14374. 'var $with1 = $mod.C;',
  14375. '$mod.Obj = $with1.$create("Create");',
  14376. '']));
  14377. end;
  14378. procedure TTestModule.TestClassOf_Call;
  14379. begin
  14380. StartProgram(false);
  14381. Add('type');
  14382. Add(' TObject = class');
  14383. Add(' class procedure DoIt;');
  14384. Add(' end;');
  14385. Add(' TClass = class of TObject;');
  14386. Add('class procedure tobject.doit; begin end;');
  14387. Add('var');
  14388. Add(' C: tclass;');
  14389. Add('begin');
  14390. Add(' c.doit;');
  14391. Add(' with c do doit;');
  14392. ConvertProgram;
  14393. CheckSource('TestClassOf_Call',
  14394. LinesToStr([ // statements
  14395. 'rtl.createClass($mod, "TObject", null, function () {',
  14396. ' this.$init = function () {',
  14397. ' };',
  14398. ' this.$final = function () {',
  14399. ' };',
  14400. ' this.DoIt = function () {',
  14401. ' };',
  14402. '});',
  14403. 'this.C = null;'
  14404. ]),
  14405. LinesToStr([ // $mod.$main
  14406. '$mod.C.DoIt();',
  14407. 'var $with1 = $mod.C;',
  14408. '$with1.DoIt();',
  14409. '']));
  14410. end;
  14411. procedure TTestModule.TestClassOf_Assign;
  14412. begin
  14413. StartProgram(false);
  14414. Add('type');
  14415. Add(' TClass = class of TObject;');
  14416. Add(' TObject = class');
  14417. Add(' ClassType: TClass; ');
  14418. Add(' end;');
  14419. Add('var');
  14420. Add(' Obj: tobject;');
  14421. Add(' C: tclass;');
  14422. Add('begin');
  14423. Add(' c:=nil;');
  14424. Add(' c:=obj.classtype;');
  14425. ConvertProgram;
  14426. CheckSource('TestClassOf_Assign',
  14427. LinesToStr([ // statements
  14428. 'rtl.createClass($mod, "TObject", null, function () {',
  14429. ' this.$init = function () {',
  14430. ' this.ClassType = null;',
  14431. ' };',
  14432. ' this.$final = function () {',
  14433. ' this.ClassType = undefined;',
  14434. ' };',
  14435. '});',
  14436. 'this.Obj = null;',
  14437. 'this.C = null;'
  14438. ]),
  14439. LinesToStr([ // $mod.$main
  14440. '$mod.C = null;',
  14441. '$mod.C = $mod.Obj.ClassType;',
  14442. '']));
  14443. end;
  14444. procedure TTestModule.TestClassOf_Is;
  14445. begin
  14446. StartProgram(false);
  14447. Add('type');
  14448. Add(' TClass = class of TObject;');
  14449. Add(' TObject = class');
  14450. Add(' end;');
  14451. Add(' TCar = class');
  14452. Add(' end;');
  14453. Add(' TCars = class of TCar;');
  14454. Add('var');
  14455. Add(' Obj: tobject;');
  14456. Add(' C: tclass;');
  14457. Add(' Cars: tcars;');
  14458. Add('begin');
  14459. Add(' if c is tcar then ;');
  14460. Add(' if c is tcars then ;');
  14461. ConvertProgram;
  14462. CheckSource('TestClassOf_Is',
  14463. LinesToStr([ // statements
  14464. 'rtl.createClass($mod, "TObject", null, function () {',
  14465. ' this.$init = function () {',
  14466. ' };',
  14467. ' this.$final = function () {',
  14468. ' };',
  14469. '});',
  14470. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  14471. '});',
  14472. 'this.Obj = null;',
  14473. 'this.C = null;',
  14474. 'this.Cars = null;'
  14475. ]),
  14476. LinesToStr([ // $mod.$main
  14477. 'if(rtl.is($mod.C,$mod.TCar));',
  14478. 'if(rtl.is($mod.C,$mod.TCar));',
  14479. '']));
  14480. end;
  14481. procedure TTestModule.TestClassOf_Compare;
  14482. begin
  14483. StartProgram(false);
  14484. Add('type');
  14485. Add(' TClass = class of TObject;');
  14486. Add(' TObject = class');
  14487. Add(' ClassType: TClass; ');
  14488. Add(' end;');
  14489. Add('var');
  14490. Add(' b: boolean;');
  14491. Add(' Obj: tobject;');
  14492. Add(' C: tclass;');
  14493. Add('begin');
  14494. Add(' b:=c=nil;');
  14495. Add(' b:=nil=c;');
  14496. Add(' b:=c=obj.classtype;');
  14497. Add(' b:=obj.classtype=c;');
  14498. Add(' b:=c=TObject;');
  14499. Add(' b:=TObject=c;');
  14500. Add(' b:=c<>nil;');
  14501. Add(' b:=nil<>c;');
  14502. Add(' b:=c<>obj.classtype;');
  14503. Add(' b:=obj.classtype<>c;');
  14504. Add(' b:=c<>TObject;');
  14505. Add(' b:=TObject<>c;');
  14506. ConvertProgram;
  14507. CheckSource('TestClassOf_Compare',
  14508. LinesToStr([ // statements
  14509. 'rtl.createClass($mod, "TObject", null, function () {',
  14510. ' this.$init = function () {',
  14511. ' this.ClassType = null;',
  14512. ' };',
  14513. ' this.$final = function () {',
  14514. ' this.ClassType = undefined;',
  14515. ' };',
  14516. '});',
  14517. 'this.b = false;',
  14518. 'this.Obj = null;',
  14519. 'this.C = null;'
  14520. ]),
  14521. LinesToStr([ // $mod.$main
  14522. '$mod.b = $mod.C === null;',
  14523. '$mod.b = null === $mod.C;',
  14524. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  14525. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  14526. '$mod.b = $mod.C === $mod.TObject;',
  14527. '$mod.b = $mod.TObject === $mod.C;',
  14528. '$mod.b = $mod.C !== null;',
  14529. '$mod.b = null !== $mod.C;',
  14530. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  14531. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  14532. '$mod.b = $mod.C !== $mod.TObject;',
  14533. '$mod.b = $mod.TObject !== $mod.C;',
  14534. '']));
  14535. end;
  14536. procedure TTestModule.TestClassOf_ClassVar;
  14537. begin
  14538. StartProgram(false);
  14539. Add('type');
  14540. Add(' TObject = class');
  14541. Add(' class var id: longint;');
  14542. Add(' end;');
  14543. Add(' TClass = class of TObject;');
  14544. Add('var');
  14545. Add(' C: tclass;');
  14546. Add('begin');
  14547. Add(' C.id:=C.id;');
  14548. ConvertProgram;
  14549. CheckSource('TestClassOf_ClassVar',
  14550. LinesToStr([ // statements
  14551. 'rtl.createClass($mod, "TObject", null, function () {',
  14552. ' this.id = 0;',
  14553. ' this.$init = function () {',
  14554. ' };',
  14555. ' this.$final = function () {',
  14556. ' };',
  14557. '});',
  14558. 'this.C = null;'
  14559. ]),
  14560. LinesToStr([ // $mod.$main
  14561. '$mod.TObject.id = $mod.C.id;',
  14562. '']));
  14563. end;
  14564. procedure TTestModule.TestClassOf_ClassMethod;
  14565. begin
  14566. StartProgram(false);
  14567. Add('type');
  14568. Add(' TObject = class');
  14569. Add(' class function DoIt(i: longint = 0): longint;');
  14570. Add(' end;');
  14571. Add(' TClass = class of TObject;');
  14572. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  14573. Add('var');
  14574. Add(' i: longint;');
  14575. Add(' C: tclass;');
  14576. Add('begin');
  14577. Add(' C.DoIt;');
  14578. Add(' C.DoIt();');
  14579. Add(' i:=C.DoIt;');
  14580. Add(' i:=C.DoIt();');
  14581. ConvertProgram;
  14582. CheckSource('TestClassOf_ClassMethod',
  14583. LinesToStr([ // statements
  14584. 'rtl.createClass($mod, "TObject", null, function () {',
  14585. ' this.$init = function () {',
  14586. ' };',
  14587. ' this.$final = function () {',
  14588. ' };',
  14589. ' this.DoIt = function (i) {',
  14590. ' var Result = 0;',
  14591. ' return Result;',
  14592. ' };',
  14593. '});',
  14594. 'this.i = 0;',
  14595. 'this.C = null;'
  14596. ]),
  14597. LinesToStr([ // $mod.$main
  14598. '$mod.C.DoIt(0);',
  14599. '$mod.C.DoIt(0);',
  14600. '$mod.i = $mod.C.DoIt(0);',
  14601. '$mod.i = $mod.C.DoIt(0);',
  14602. '']));
  14603. end;
  14604. procedure TTestModule.TestClassOf_ClassProperty;
  14605. begin
  14606. StartProgram(false);
  14607. Add([
  14608. 'type',
  14609. ' TObject = class',
  14610. ' class var FA: longint;',
  14611. ' class function GetA: longint;',
  14612. ' class procedure SetA(Value: longint);',
  14613. ' class property pA: longint read fa write fa;',
  14614. ' class property pB: longint read geta write seta;',
  14615. ' end;',
  14616. ' TObjectClass = class of tobject;',
  14617. 'class function tobject.geta: longint; begin end;',
  14618. 'class procedure tobject.seta(value: longint); begin end;',
  14619. 'var',
  14620. ' b: boolean;',
  14621. ' Obj: tobject;',
  14622. ' Cla: tobjectclass;',
  14623. 'begin',
  14624. ' obj.pa:=obj.pa;',
  14625. ' obj.pb:=obj.pb;',
  14626. ' b:=obj.pa=4;',
  14627. ' b:=obj.pb=obj.pb;',
  14628. ' b:=5=obj.pa;',
  14629. ' cla.pa:=6;',
  14630. ' cla.pa:=cla.pa;',
  14631. ' cla.pb:=cla.pb;',
  14632. ' b:=cla.pa=7;',
  14633. ' b:=cla.pb=cla.pb;',
  14634. ' b:=8=cla.pa;',
  14635. ' tobject.pa:=9;',
  14636. ' tobject.pb:=tobject.pb;',
  14637. ' b:=tobject.pa=10;',
  14638. ' b:=11=tobject.pa;',
  14639. '']);
  14640. ConvertProgram;
  14641. CheckSource('TestClassOf_ClassProperty',
  14642. LinesToStr([ // statements
  14643. 'rtl.createClass($mod, "TObject", null, function () {',
  14644. ' this.FA = 0;',
  14645. ' this.$init = function () {',
  14646. ' };',
  14647. ' this.$final = function () {',
  14648. ' };',
  14649. ' this.GetA = function () {',
  14650. ' var Result = 0;',
  14651. ' return Result;',
  14652. ' };',
  14653. ' this.SetA = function (Value) {',
  14654. ' };',
  14655. '});',
  14656. 'this.b = false;',
  14657. 'this.Obj = null;',
  14658. 'this.Cla = null;'
  14659. ]),
  14660. LinesToStr([ // $mod.$main
  14661. '$mod.TObject.FA = $mod.Obj.FA;',
  14662. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  14663. '$mod.b = $mod.Obj.FA === 4;',
  14664. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  14665. '$mod.b = 5 === $mod.Obj.FA;',
  14666. '$mod.TObject.FA = 6;',
  14667. '$mod.TObject.FA = $mod.Cla.FA;',
  14668. '$mod.Cla.SetA($mod.Cla.GetA());',
  14669. '$mod.b = $mod.Cla.FA === 7;',
  14670. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  14671. '$mod.b = 8 === $mod.Cla.FA;',
  14672. '$mod.TObject.FA = 9;',
  14673. '$mod.TObject.SetA($mod.TObject.GetA());',
  14674. '$mod.b = $mod.TObject.FA === 10;',
  14675. '$mod.b = 11 === $mod.TObject.FA;',
  14676. '']));
  14677. end;
  14678. procedure TTestModule.TestClassOf_ClassMethodSelf;
  14679. begin
  14680. StartProgram(false);
  14681. Add('type');
  14682. Add(' TObject = class');
  14683. Add(' class var GlobalId: longint;');
  14684. Add(' class procedure ProcA;');
  14685. Add(' end;');
  14686. Add('class procedure tobject.proca;');
  14687. Add('var b: boolean;');
  14688. Add('begin');
  14689. Add(' b:=self=nil;');
  14690. Add(' b:=self.globalid=3;');
  14691. Add(' b:=4=self.globalid;');
  14692. Add(' self.globalid:=5;');
  14693. Add(' self.proca;');
  14694. Add('end;');
  14695. Add('begin');
  14696. ConvertProgram;
  14697. CheckSource('TestClassOf_ClassMethodSelf',
  14698. LinesToStr([ // statements
  14699. 'rtl.createClass($mod, "TObject", null, function () {',
  14700. ' this.GlobalId = 0;',
  14701. ' this.$init = function () {',
  14702. ' };',
  14703. ' this.$final = function () {',
  14704. ' };',
  14705. ' this.ProcA = function () {',
  14706. ' var b = false;',
  14707. ' b = this === null;',
  14708. ' b = this.GlobalId === 3;',
  14709. ' b = 4 === this.GlobalId;',
  14710. ' $mod.TObject.GlobalId = 5;',
  14711. ' this.ProcA();',
  14712. ' };',
  14713. '});'
  14714. ]),
  14715. LinesToStr([ // $mod.$main
  14716. '']));
  14717. end;
  14718. procedure TTestModule.TestClassOf_TypeCast;
  14719. begin
  14720. StartProgram(false);
  14721. Add('type');
  14722. Add(' TObject = class');
  14723. Add(' class procedure {#TObject_DoIt}DoIt;');
  14724. Add(' end;');
  14725. Add(' TClass = class of TObject;');
  14726. Add(' TMobile = class');
  14727. Add(' class procedure {#TMobile_DoIt}DoIt;');
  14728. Add(' end;');
  14729. Add(' TMobileClass = class of TMobile;');
  14730. Add(' TCar = class(TMobile)');
  14731. Add(' class procedure {#TCar_DoIt}DoIt;');
  14732. Add(' end;');
  14733. Add(' TCarClass = class of TCar;');
  14734. Add('class procedure TObject.DoIt;');
  14735. Add('begin');
  14736. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14737. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14738. Add('end;');
  14739. Add('class procedure TMobile.DoIt;');
  14740. Add('begin');
  14741. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14742. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14743. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  14744. Add('end;');
  14745. Add('class procedure TCar.DoIt; begin end;');
  14746. Add('var');
  14747. Add(' ObjC: TClass;');
  14748. Add(' MobileC: TMobileClass;');
  14749. Add(' CarC: TCarClass;');
  14750. Add('begin');
  14751. Add(' ObjC.{@TObject_DoIt}DoIt;');
  14752. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  14753. Add(' CarC.{@TCar_DoIt}DoIt;');
  14754. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  14755. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  14756. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  14757. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  14758. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  14759. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  14760. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  14761. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  14762. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  14763. ConvertProgram;
  14764. CheckSource('TestClassOf_TypeCast',
  14765. LinesToStr([ // statements
  14766. 'rtl.createClass($mod, "TObject", null, function () {',
  14767. ' this.$init = function () {',
  14768. ' };',
  14769. ' this.$final = function () {',
  14770. ' };',
  14771. ' this.DoIt = function () {',
  14772. ' this.DoIt();',
  14773. ' this.DoIt$1();',
  14774. ' };',
  14775. '});',
  14776. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  14777. ' this.DoIt$1 = function () {',
  14778. ' this.DoIt();',
  14779. ' this.DoIt$1();',
  14780. ' this.DoIt$2();',
  14781. ' };',
  14782. '});',
  14783. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  14784. ' this.DoIt$2 = function () {',
  14785. ' };',
  14786. '});',
  14787. 'this.ObjC = null;',
  14788. 'this.MobileC = null;',
  14789. 'this.CarC = null;',
  14790. '']),
  14791. LinesToStr([ // $mod.$main
  14792. '$mod.ObjC.DoIt();',
  14793. '$mod.MobileC.DoIt$1();',
  14794. '$mod.CarC.DoIt$2();',
  14795. '$mod.ObjC.DoIt();',
  14796. '$mod.ObjC.DoIt$1();',
  14797. '$mod.ObjC.DoIt$2();',
  14798. '$mod.MobileC.DoIt();',
  14799. '$mod.MobileC.DoIt$1();',
  14800. '$mod.MobileC.DoIt$2();',
  14801. '$mod.CarC.DoIt();',
  14802. '$mod.CarC.DoIt$1();',
  14803. '$mod.CarC.DoIt$2();',
  14804. '']));
  14805. end;
  14806. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  14807. begin
  14808. StartProgram(false);
  14809. Add('type');
  14810. Add(' TObject = class');
  14811. Add(' function CurNow: longint; ');
  14812. Add(' class function Now: longint; ');
  14813. Add(' end;');
  14814. Add('function TObject.CurNow: longint; begin end;');
  14815. Add('class function TObject.Now: longint; begin end;');
  14816. Add('var');
  14817. Add(' Obj: tobject;');
  14818. Add(' vI: longint;');
  14819. Add('begin');
  14820. Add(' obj.curnow;');
  14821. Add(' vi:=obj.curnow;');
  14822. Add(' tobject.now;');
  14823. Add(' vi:=tobject.now;');
  14824. ConvertProgram;
  14825. CheckSource('TestClassOf_ImplicitFunctionCall',
  14826. LinesToStr([ // statements
  14827. 'rtl.createClass($mod, "TObject", null, function () {',
  14828. ' this.$init = function () {',
  14829. ' };',
  14830. ' this.$final = function () {',
  14831. ' };',
  14832. ' this.CurNow = function () {',
  14833. ' var Result = 0;',
  14834. ' return Result;',
  14835. ' };',
  14836. ' this.Now = function () {',
  14837. ' var Result = 0;',
  14838. ' return Result;',
  14839. ' };',
  14840. '});',
  14841. 'this.Obj = null;',
  14842. 'this.vI = 0;',
  14843. '']),
  14844. LinesToStr([ // $mod.$main
  14845. '$mod.Obj.CurNow();',
  14846. '$mod.vI = $mod.Obj.CurNow();',
  14847. '$mod.TObject.Now();',
  14848. '$mod.vI = $mod.TObject.Now();',
  14849. '']));
  14850. end;
  14851. procedure TTestModule.TestClassOf_Const;
  14852. begin
  14853. StartProgram(false);
  14854. Add([
  14855. 'type',
  14856. ' TObject = class',
  14857. ' end;',
  14858. ' TBird = TObject;',
  14859. ' TBirds = class of TBird;',
  14860. ' TEagles = TBirds;',
  14861. ' THawk = class(TBird);',
  14862. 'const',
  14863. ' Hawk: TEagles = THawk;',
  14864. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  14865. ' TBird,',
  14866. ' THawk',
  14867. ' );',
  14868. 'begin']);
  14869. ConvertProgram;
  14870. CheckSource('TestClassOf_Const',
  14871. LinesToStr([ // statements
  14872. 'rtl.createClass($mod, "TObject", null, function () {',
  14873. ' this.$init = function () {',
  14874. ' };',
  14875. ' this.$final = function () {',
  14876. ' };',
  14877. '});',
  14878. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  14879. '});',
  14880. 'this.Hawk = $mod.THawk;',
  14881. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  14882. '']),
  14883. LinesToStr([ // $mod.$main
  14884. '']));
  14885. end;
  14886. procedure TTestModule.TestNestedClass_Alias;
  14887. begin
  14888. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14889. StartProgram(false);
  14890. Add([
  14891. 'type',
  14892. ' TObject = class',
  14893. ' type TNested = type longint;',
  14894. ' end;',
  14895. 'type TAlias = type tobject.tnested;',
  14896. 'var i: tobject.tnested = 3;',
  14897. 'var j: TAlias = 4;',
  14898. 'begin',
  14899. ' if typeinfo(TAlias)=nil then ;',
  14900. ' if typeinfo(tobject.tnested)=nil then ;',
  14901. '']);
  14902. ConvertProgram;
  14903. CheckSource('TestNestedClass_Alias',
  14904. LinesToStr([ // statements
  14905. 'rtl.createClass($mod, "TObject", null, function () {',
  14906. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  14907. ' this.$init = function () {',
  14908. ' };',
  14909. ' this.$final = function () {',
  14910. ' };',
  14911. '});',
  14912. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  14913. 'this.i = 3;',
  14914. 'this.j = 4;',
  14915. '']),
  14916. LinesToStr([ // $mod.$main
  14917. 'if ($mod.$rtti["TAlias"] === null) ;',
  14918. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  14919. '']));
  14920. end;
  14921. procedure TTestModule.TestNestedClass_Record;
  14922. begin
  14923. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14924. StartProgram(false);
  14925. Add([
  14926. 'type',
  14927. ' TObject = class',
  14928. ' type TPoint = record',
  14929. ' x,y: byte;',
  14930. ' end;',
  14931. ' procedure DoIt(t: TPoint);',
  14932. ' end;',
  14933. 'procedure tobject.DoIt(t: TPoint);',
  14934. 'var p: TPoint;',
  14935. 'begin',
  14936. ' t.x:=t.y;',
  14937. ' p:=t;',
  14938. 'end;',
  14939. 'var',
  14940. ' p: tobject.tpoint = (x:2; y:4);',
  14941. ' o: TObject;',
  14942. 'begin',
  14943. ' p:=p;',
  14944. ' o.doit(p);',
  14945. '']);
  14946. ConvertProgram;
  14947. CheckSource('TestNestedClass_Record',
  14948. LinesToStr([ // statements
  14949. 'rtl.createClass($mod, "TObject", null, function () {',
  14950. ' rtl.recNewT(this, "TPoint", function () {',
  14951. ' this.x = 0;',
  14952. ' this.y = 0;',
  14953. ' this.$eq = function (b) {',
  14954. ' return (this.x === b.x) && (this.y === b.y);',
  14955. ' };',
  14956. ' this.$assign = function (s) {',
  14957. ' this.x = s.x;',
  14958. ' this.y = s.y;',
  14959. ' return this;',
  14960. ' };',
  14961. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  14962. ' $r.addField("x", rtl.byte);',
  14963. ' $r.addField("y", rtl.byte);',
  14964. ' });',
  14965. ' this.$init = function () {',
  14966. ' };',
  14967. ' this.$final = function () {',
  14968. ' };',
  14969. ' this.DoIt = function (t) {',
  14970. ' var p = this.TPoint.$new();',
  14971. ' t.x = t.y;',
  14972. ' p.$assign(t);',
  14973. ' };',
  14974. '});',
  14975. 'this.p = $mod.TObject.TPoint.$clone({',
  14976. ' x: 2,',
  14977. ' y: 4',
  14978. '});',
  14979. 'this.o = null;',
  14980. '']),
  14981. LinesToStr([ // $mod.$main
  14982. '$mod.p.$assign($mod.p);',
  14983. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  14984. '']));
  14985. end;
  14986. procedure TTestModule.TestNestedClass_Class;
  14987. begin
  14988. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14989. StartProgram(false);
  14990. Add([
  14991. 'type',
  14992. ' TObject = class end;',
  14993. ' TBird = class',
  14994. ' type TLeg = class',
  14995. ' FId: longint;',
  14996. ' constructor Create;',
  14997. ' function Create(i: longint): TLeg;',
  14998. ' end;',
  14999. ' function DoIt(b: TBird): Tleg;',
  15000. ' end;',
  15001. 'constructor tbird.tleg.create;',
  15002. 'begin',
  15003. ' FId:=3;',
  15004. 'end;',
  15005. 'function tbird.tleg.Create(i: longint): TLeg;',
  15006. 'begin',
  15007. ' Create;',
  15008. ' Result:=TLeg.Create;',
  15009. ' Result:=TBird.TLeg.Create;',
  15010. ' Result:=Create(3);',
  15011. ' FId:=i;',
  15012. 'end;',
  15013. 'function tbird.DoIt(b: tbird): tleg;',
  15014. 'begin',
  15015. ' Result.Create;',
  15016. ' Result:=TLeg.Create;',
  15017. ' Result:=TBird.TLeg.Create;',
  15018. ' Result:=Result.Create(3);',
  15019. 'end;',
  15020. 'var',
  15021. ' b: Tbird.tleg;',
  15022. 'begin',
  15023. ' b.Create;',
  15024. ' b:=TBird.TLeg.Create;',
  15025. ' b:=b.Create(3);',
  15026. '']);
  15027. ConvertProgram;
  15028. CheckSource('TestNestedClass_Class',
  15029. LinesToStr([ // statements
  15030. 'rtl.createClass($mod, "TObject", null, function () {',
  15031. ' this.$init = function () {',
  15032. ' };',
  15033. ' this.$final = function () {',
  15034. ' };',
  15035. '});',
  15036. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15037. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  15038. ' this.$init = function () {',
  15039. ' $mod.TObject.$init.call(this);',
  15040. ' this.FId = 0;',
  15041. ' };',
  15042. ' this.Create = function () {',
  15043. ' this.FId = 3;',
  15044. ' return this;',
  15045. ' };',
  15046. ' this.Create$1 = function (i) {',
  15047. ' var Result = null;',
  15048. ' this.Create();',
  15049. ' Result = $mod.TBird.TLeg.$create("Create");',
  15050. ' Result = $mod.TBird.TLeg.$create("Create");',
  15051. ' Result = this.Create$1(3);',
  15052. ' this.FId = i;',
  15053. ' return Result;',
  15054. ' };',
  15055. ' });',
  15056. ' this.DoIt = function (b) {',
  15057. ' var Result = null;',
  15058. ' Result.Create();',
  15059. ' Result = this.TLeg.$create("Create");',
  15060. ' Result = $mod.TBird.TLeg.$create("Create");',
  15061. ' Result = Result.Create$1(3);',
  15062. ' return Result;',
  15063. ' };',
  15064. '});',
  15065. 'this.b = null;',
  15066. '']),
  15067. LinesToStr([ // $mod.$main
  15068. '$mod.b.Create();',
  15069. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  15070. '$mod.b = $mod.b.Create$1(3);',
  15071. '']));
  15072. end;
  15073. procedure TTestModule.TestExternalClass_Var;
  15074. begin
  15075. StartProgram(false);
  15076. Add([
  15077. '{$modeswitch externalclass}',
  15078. 'type',
  15079. ' TExtA = class external name ''ExtObj''',
  15080. ' Id: longint external name ''$Id'';',
  15081. ' B: longint;',
  15082. ' end;',
  15083. 'var Obj: TExtA;',
  15084. 'begin',
  15085. ' obj.id:=obj.id+1;',
  15086. ' obj.B:=obj.B+1;']);
  15087. ConvertProgram;
  15088. CheckSource('TestExternalClass_Var',
  15089. LinesToStr([ // statements
  15090. 'this.Obj = null;',
  15091. '']),
  15092. LinesToStr([ // $mod.$main
  15093. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  15094. '$mod.Obj.B = $mod.Obj.B + 1;',
  15095. '']));
  15096. end;
  15097. procedure TTestModule.TestExternalClass_Const;
  15098. begin
  15099. StartProgram(false);
  15100. Add([
  15101. '{$modeswitch externalclass}',
  15102. 'type',
  15103. ' TExtA = class external name ''ExtObj''',
  15104. ' const Two: longint = 2;',
  15105. ' const Three = 3;',
  15106. ' const Id: longint;',
  15107. ' end;',
  15108. ' TExtB = class external name ''ExtB''',
  15109. ' A: TExtA;',
  15110. ' end;',
  15111. 'var',
  15112. ' A: texta;',
  15113. ' B: textb;',
  15114. ' i: longint;',
  15115. 'begin',
  15116. ' i:=a.two;',
  15117. ' i:=texta.two;',
  15118. ' i:=a.three;',
  15119. ' i:=texta.three;',
  15120. ' i:=a.id;',
  15121. ' i:=texta.id;',
  15122. '']);
  15123. ConvertProgram;
  15124. CheckSource('TestExternalClass_Const',
  15125. LinesToStr([ // statements
  15126. 'this.A = null;',
  15127. 'this.B = null;',
  15128. 'this.i = 0;',
  15129. '']),
  15130. LinesToStr([ // $mod.$main
  15131. '$mod.i = 2;',
  15132. '$mod.i = 2;',
  15133. '$mod.i = 3;',
  15134. '$mod.i = 3;',
  15135. '$mod.i = $mod.A.Id;',
  15136. '$mod.i = ExtObj.Id;',
  15137. '']));
  15138. end;
  15139. procedure TTestModule.TestExternalClass_Dollar;
  15140. begin
  15141. StartProgram(false);
  15142. Add([
  15143. '{$modeswitch externalclass}',
  15144. 'type',
  15145. ' TExtA = class external name ''$''',
  15146. ' Id: longint external name ''$'';',
  15147. ' function Bla(i: longint): longint; external name ''$'';',
  15148. ' end;',
  15149. 'function dollar(k: longint): longint; external name ''$'';',
  15150. 'var Obj: TExtA;',
  15151. 'begin',
  15152. ' dollar(1);',
  15153. ' obj.id:=obj.id+2;',
  15154. ' obj.Bla(3);',
  15155. '']);
  15156. ConvertProgram;
  15157. CheckSource('TestExternalClass_Dollar',
  15158. LinesToStr([ // statements
  15159. 'this.Obj = null;',
  15160. '']),
  15161. LinesToStr([ // $mod.$main
  15162. '$(1);',
  15163. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  15164. '$mod.Obj.$(3);',
  15165. '']));
  15166. end;
  15167. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  15168. begin
  15169. StartProgram(false);
  15170. Add('{$modeswitch externalclass}');
  15171. Add('type');
  15172. Add(' TExtA = class external name ''ExtA''');
  15173. Add(' Id: longint external name ''$Id'';');
  15174. Add(' end;');
  15175. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  15176. Add(' Id: longint;');
  15177. Add(' end;');
  15178. Add('begin');
  15179. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  15180. ConvertProgram;
  15181. end;
  15182. procedure TTestModule.TestExternalClass_Method;
  15183. begin
  15184. StartProgram(false);
  15185. Add('{$modeswitch externalclass}');
  15186. Add('type');
  15187. Add(' TExtA = class external name ''ExtObj''');
  15188. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  15189. Add(' procedure DoSome(Id: longint = 1);');
  15190. Add(' end;');
  15191. Add('var Obj: texta;');
  15192. Add('begin');
  15193. Add(' obj.doit;');
  15194. Add(' obj.doit();');
  15195. Add(' obj.doit(2);');
  15196. Add(' with obj do begin');
  15197. Add(' doit;');
  15198. Add(' doit();');
  15199. Add(' doit(3);');
  15200. Add(' end;');
  15201. ConvertProgram;
  15202. CheckSource('TestExternalClass_Method',
  15203. LinesToStr([ // statements
  15204. 'this.Obj = null;',
  15205. '']),
  15206. LinesToStr([ // $mod.$main
  15207. '$mod.Obj.$Execute(1);',
  15208. '$mod.Obj.$Execute(1);',
  15209. '$mod.Obj.$Execute(2);',
  15210. 'var $with1 = $mod.Obj;',
  15211. '$with1.$Execute(1);',
  15212. '$with1.$Execute(1);',
  15213. '$with1.$Execute(3);',
  15214. '']));
  15215. end;
  15216. procedure TTestModule.TestExternalClass_ClassMethod;
  15217. begin
  15218. StartProgram(false);
  15219. Add([
  15220. '{$modeswitch externalclass}',
  15221. 'type',
  15222. ' TExtA = class external name ''ExtObj''',
  15223. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15224. ' end;',
  15225. ' TExtB = TExtA;',
  15226. 'var p: Pointer;',
  15227. 'begin',
  15228. ' texta.doit;',
  15229. ' texta.doit();',
  15230. ' texta.doit(2);',
  15231. ' p:[email protected];',
  15232. ' with texta do begin',
  15233. ' doit;',
  15234. ' doit();',
  15235. ' doit(3);',
  15236. ' p:=@DoIt;',
  15237. ' end;',
  15238. ' textb.doit;',
  15239. ' textb.doit();',
  15240. ' textb.doit(4);',
  15241. ' with textb do begin',
  15242. ' doit;',
  15243. ' doit();',
  15244. ' doit(5);',
  15245. ' end;',
  15246. '']);
  15247. ConvertProgram;
  15248. CheckSource('TestExternalClass_ClassMethod',
  15249. LinesToStr([ // statements
  15250. 'this.p = null;',
  15251. '']),
  15252. LinesToStr([ // $mod.$main
  15253. 'ExtObj.$Execute(1);',
  15254. 'ExtObj.$Execute(1);',
  15255. 'ExtObj.$Execute(2);',
  15256. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15257. 'ExtObj.$Execute(1);',
  15258. 'ExtObj.$Execute(1);',
  15259. 'ExtObj.$Execute(3);',
  15260. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15261. 'ExtObj.$Execute(1);',
  15262. 'ExtObj.$Execute(1);',
  15263. 'ExtObj.$Execute(4);',
  15264. 'ExtObj.$Execute(1);',
  15265. 'ExtObj.$Execute(1);',
  15266. 'ExtObj.$Execute(5);',
  15267. '']));
  15268. end;
  15269. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  15270. begin
  15271. StartProgram(false);
  15272. Add([
  15273. '{$modeswitch externalclass}',
  15274. 'type',
  15275. ' TExtA = class external name ''ExtObj''',
  15276. ' class procedure DoIt(Id: longint = 1); static;',
  15277. ' end;',
  15278. 'var p: Pointer;',
  15279. 'begin',
  15280. ' texta.doit;',
  15281. ' texta.doit();',
  15282. ' texta.doit(2);',
  15283. ' p:[email protected];',
  15284. ' with texta do begin',
  15285. ' doit;',
  15286. ' doit();',
  15287. ' doit(3);',
  15288. ' p:=@DoIt;',
  15289. ' end;',
  15290. '']);
  15291. ConvertProgram;
  15292. CheckSource('TestExternalClass_ClassMethodStatic',
  15293. LinesToStr([ // statements
  15294. 'this.p = null;',
  15295. '']),
  15296. LinesToStr([ // $mod.$main
  15297. 'ExtObj.DoIt(1);',
  15298. 'ExtObj.DoIt(1);',
  15299. 'ExtObj.DoIt(2);',
  15300. '$mod.p = ExtObj.DoIt;',
  15301. 'ExtObj.DoIt(1);',
  15302. 'ExtObj.DoIt(1);',
  15303. 'ExtObj.DoIt(3);',
  15304. '$mod.p = ExtObj.DoIt;',
  15305. '']));
  15306. end;
  15307. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  15308. begin
  15309. StartProgram(false);
  15310. Add([
  15311. '{$modeswitch externalclass}',
  15312. 'type',
  15313. ' TBird = class external name ''Array''',
  15314. ' end;',
  15315. 'function GetPtr: Pointer;',
  15316. 'begin',
  15317. 'end;',
  15318. 'procedure Write(const p);',
  15319. 'begin',
  15320. 'end;',
  15321. 'procedure WriteLn; varargs;',
  15322. 'begin',
  15323. 'end;',
  15324. 'begin',
  15325. ' if TBird(GetPtr)=nil then ;',
  15326. ' Write(GetPtr);',
  15327. ' WriteLn(GetPtr);',
  15328. ' Write(TBird(GetPtr));',
  15329. ' WriteLn(TBird(GetPtr));',
  15330. '']);
  15331. ConvertProgram;
  15332. CheckSource('TestFunctionResultInTypeCast',
  15333. LinesToStr([ // statements
  15334. 'this.GetPtr = function () {',
  15335. ' var Result = null;',
  15336. ' return Result;',
  15337. '};',
  15338. 'this.Write = function (p) {',
  15339. '};',
  15340. 'this.WriteLn = function () {',
  15341. '};',
  15342. '']),
  15343. LinesToStr([
  15344. 'if ($mod.GetPtr() === null) ;',
  15345. '$mod.Write($mod.GetPtr());',
  15346. '$mod.WriteLn($mod.GetPtr());',
  15347. '$mod.Write($mod.GetPtr());',
  15348. '$mod.WriteLn($mod.GetPtr());',
  15349. '']));
  15350. end;
  15351. procedure TTestModule.TestExternalClass_NonExternalOverride;
  15352. begin
  15353. StartProgram(false);
  15354. Add([
  15355. '{$modeswitch externalclass}',
  15356. 'type',
  15357. ' TExtA = class external name ''ExtObjA''',
  15358. ' procedure ProcA; virtual;',
  15359. ' procedure ProcB; virtual;',
  15360. ' end;',
  15361. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  15362. ' end;',
  15363. ' TExtC = class (TExtB)',
  15364. ' procedure ProcA; override;',
  15365. ' end;',
  15366. 'procedure TExtC.ProcA;',
  15367. 'begin',
  15368. ' ProcA;',
  15369. ' Self.ProcA;',
  15370. ' ProcB;',
  15371. ' Self.ProcB;',
  15372. 'end;',
  15373. 'var',
  15374. ' A: texta;',
  15375. ' B: textb;',
  15376. ' C: textc;',
  15377. 'begin',
  15378. ' a.proca;',
  15379. ' b.proca;',
  15380. ' c.proca;']);
  15381. ConvertProgram;
  15382. CheckSource('TestExternalClass_NonExternalOverride',
  15383. LinesToStr([ // statements
  15384. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  15385. ' this.$init = function () {',
  15386. ' };',
  15387. ' this.$final = function () {',
  15388. ' };',
  15389. ' this.ProcA = function () {',
  15390. ' this.ProcA();',
  15391. ' this.ProcA();',
  15392. ' this.ProcB();',
  15393. ' this.ProcB();',
  15394. ' };',
  15395. '});',
  15396. 'this.A = null;',
  15397. 'this.B = null;',
  15398. 'this.C = null;',
  15399. '']),
  15400. LinesToStr([ // $mod.$main
  15401. '$mod.A.ProcA();',
  15402. '$mod.B.ProcA();',
  15403. '$mod.C.ProcA();',
  15404. '']));
  15405. end;
  15406. procedure TTestModule.TestExternalClass_OverloadHint;
  15407. begin
  15408. StartProgram(false);
  15409. Add([
  15410. '{$modeswitch externalclass}',
  15411. 'type',
  15412. ' TExtA = class external name ''ExtObjA''',
  15413. ' procedure DoIt;',
  15414. ' procedure DoIt(i: longint);',
  15415. ' end;',
  15416. 'begin',
  15417. '']);
  15418. ConvertProgram;
  15419. CheckResolverUnexpectedHints(true);
  15420. CheckSource('TestExternalClass_OverloadHint',
  15421. LinesToStr([ // statements
  15422. '']),
  15423. LinesToStr([ // $mod.$main
  15424. '']));
  15425. end;
  15426. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  15427. begin
  15428. StartProgram(false);
  15429. Add([
  15430. '{$modeswitch externalclass}',
  15431. 'type',
  15432. ' JSwiper = class external name ''Swiper''',
  15433. ' constructor New;',
  15434. ' end;',
  15435. ' TObject = class',
  15436. ' private',
  15437. ' FSwiper: JSwiper;',
  15438. ' published',
  15439. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  15440. ' end;',
  15441. 'begin',
  15442. ' JSwiper.new;',
  15443. '']);
  15444. ConvertProgram;
  15445. CheckSource('TestExternalClass_SameNamePublishedProperty',
  15446. LinesToStr([ // statements
  15447. 'rtl.createClass($mod, "TObject", null, function () {',
  15448. ' this.$init = function () {',
  15449. ' this.FSwiper = null;',
  15450. ' };',
  15451. ' this.$final = function () {',
  15452. ' this.FSwiper = undefined;',
  15453. ' };',
  15454. ' var $r = this.$rtti;',
  15455. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  15456. '});',
  15457. '']),
  15458. LinesToStr([ // $mod.$main
  15459. 'new Swiper();',
  15460. '']));
  15461. end;
  15462. procedure TTestModule.TestExternalClass_Property;
  15463. begin
  15464. StartProgram(false);
  15465. Add([
  15466. '{$modeswitch externalclass}',
  15467. 'type',
  15468. ' TExtA = class external name ''ExtA''',
  15469. ' function getYear: longint;',
  15470. ' procedure setYear(Value: longint);',
  15471. ' property Year: longint read getyear write setyear;',
  15472. ' end;',
  15473. ' TExtB = class (TExtA)',
  15474. ' procedure OtherSetYear(Value: longint);',
  15475. ' property year write othersetyear;',
  15476. ' end;',
  15477. 'procedure textb.othersetyear(value: longint);',
  15478. 'begin',
  15479. ' setYear(Value+4);',
  15480. 'end;',
  15481. 'var',
  15482. ' A: texta;',
  15483. ' B: textb;',
  15484. 'begin',
  15485. ' a.year:=a.year+1;',
  15486. ' b.year:=b.year+2;']);
  15487. ConvertProgram;
  15488. CheckSource('TestExternalClass_NonExternalOverride',
  15489. LinesToStr([ // statements
  15490. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15491. ' this.$init = function () {',
  15492. ' };',
  15493. ' this.$final = function () {',
  15494. ' };',
  15495. ' this.OtherSetYear = function (Value) {',
  15496. ' this.setYear(Value+4);',
  15497. ' };',
  15498. '});',
  15499. 'this.A = null;',
  15500. 'this.B = null;',
  15501. '']),
  15502. LinesToStr([ // $mod.$main
  15503. '$mod.A.setYear($mod.A.getYear()+1);',
  15504. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  15505. '']));
  15506. end;
  15507. procedure TTestModule.TestExternalClass_PropertyDate;
  15508. begin
  15509. StartProgram(false);
  15510. Add([
  15511. '{$modeswitch externalclass}',
  15512. 'type',
  15513. ' TExtA = class external name ''ExtA''',
  15514. ' end;',
  15515. ' TExtB = class (TExtA)',
  15516. ' FDate: string;',
  15517. ' property Date: string read FDate write FDate;',
  15518. ' property ExtA: string read FDate write FDate;',
  15519. ' end;',
  15520. ' {$M+}',
  15521. ' TObject = class',
  15522. ' FDate: string;',
  15523. ' published',
  15524. ' property Date: string read FDate write FDate;',
  15525. ' property ExtA: string read FDate write FDate;',
  15526. ' end;',
  15527. 'var',
  15528. ' B: textb;',
  15529. ' o: TObject;',
  15530. 'begin',
  15531. ' b.date:=b.exta;',
  15532. ' o.date:=o.exta;']);
  15533. ConvertProgram;
  15534. CheckSource('TestExternalClass_PropertyDate',
  15535. LinesToStr([ // statements
  15536. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15537. ' this.$init = function () {',
  15538. ' this.FDate = "";',
  15539. ' };',
  15540. ' this.$final = function () {',
  15541. ' };',
  15542. '});',
  15543. 'rtl.createClass($mod, "TObject", null, function () {',
  15544. ' this.$init = function () {',
  15545. ' this.FDate = "";',
  15546. ' };',
  15547. ' this.$final = function () {',
  15548. ' };',
  15549. ' var $r = this.$rtti;',
  15550. ' $r.addField("FDate", rtl.string);',
  15551. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  15552. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  15553. '});',
  15554. 'this.B = null;',
  15555. 'this.o = null;',
  15556. '']),
  15557. LinesToStr([ // $mod.$main
  15558. '$mod.B.FDate = $mod.B.FDate;',
  15559. '$mod.o.FDate = $mod.o.FDate;',
  15560. '']));
  15561. end;
  15562. procedure TTestModule.TestExternalClass_ClassProperty;
  15563. begin
  15564. StartProgram(false);
  15565. Add('{$modeswitch externalclass}');
  15566. Add('type');
  15567. Add(' TExtA = class external name ''ExtA''');
  15568. Add(' class function getYear: longint;');
  15569. Add(' class procedure setYear(Value: longint);');
  15570. Add(' class property Year: longint read getyear write setyear;');
  15571. Add(' end;');
  15572. Add(' TExtB = class (TExtA)');
  15573. Add(' class function GetCentury: longint;');
  15574. Add(' class procedure SetCentury(Value: longint);');
  15575. Add(' class property Century: longint read getcentury write setcentury;');
  15576. Add(' end;');
  15577. Add('class function textb.getcentury: longint;');
  15578. Add('begin');
  15579. Add('end;');
  15580. Add('class procedure textb.setcentury(value: longint);');
  15581. Add('begin');
  15582. Add(' setyear(value+11);');
  15583. Add(' texta.year:=texta.year+12;');
  15584. Add(' year:=year+13;');
  15585. Add(' textb.century:=textb.century+14;');
  15586. Add(' century:=century+15;');
  15587. Add('end;');
  15588. Add('var');
  15589. Add(' A: texta;');
  15590. Add(' B: textb;');
  15591. Add('begin');
  15592. Add(' texta.year:=texta.year+1;');
  15593. Add(' textb.year:=textb.year+2;');
  15594. Add(' TextA.year:=TextA.year+3;');
  15595. Add(' b.year:=b.year+4;');
  15596. Add(' textb.century:=textb.century+5;');
  15597. Add(' b.century:=b.century+6;');
  15598. ConvertProgram;
  15599. CheckSource('TestExternalClass_ClassProperty',
  15600. LinesToStr([ // statements
  15601. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15602. ' this.$init = function () {',
  15603. ' };',
  15604. ' this.$final = function () {',
  15605. ' };',
  15606. ' this.GetCentury = function () {',
  15607. ' var Result = 0;',
  15608. ' return Result;',
  15609. ' };',
  15610. ' this.SetCentury = function (Value) {',
  15611. ' this.setYear(Value + 11);',
  15612. ' ExtA.setYear(ExtA.getYear() + 12);',
  15613. ' this.setYear(this.getYear() + 13);',
  15614. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  15615. ' this.SetCentury(this.GetCentury() + 15);',
  15616. ' };',
  15617. '});',
  15618. 'this.A = null;',
  15619. 'this.B = null;',
  15620. '']),
  15621. LinesToStr([ // $mod.$main
  15622. 'ExtA.setYear(ExtA.getYear() + 1);',
  15623. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  15624. 'ExtA.setYear(ExtA.getYear() + 3);',
  15625. '$mod.B.setYear($mod.B.getYear() + 4);',
  15626. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  15627. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  15628. '']));
  15629. end;
  15630. procedure TTestModule.TestExternalClass_ClassOf;
  15631. begin
  15632. StartProgram(false);
  15633. Add('{$modeswitch externalclass}');
  15634. Add('type');
  15635. Add(' TExtA = class external name ''ExtA''');
  15636. Add(' procedure ProcA; virtual;');
  15637. Add(' procedure ProcB; virtual;');
  15638. Add(' end;');
  15639. Add(' TExtAClass = class of TExtA;');
  15640. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15641. Add(' end;');
  15642. Add(' TExtBClass = class of TExtB;');
  15643. Add(' TExtC = class (TExtB)');
  15644. Add(' procedure ProcA; override;');
  15645. Add(' end;');
  15646. Add(' TExtCClass = class of TExtC;');
  15647. Add('procedure TExtC.ProcA; begin end;');
  15648. Add('var');
  15649. Add(' A: texta; ClA: TExtAClass;');
  15650. Add(' B: textb; ClB: TExtBClass;');
  15651. Add(' C: textc; ClC: TExtCClass;');
  15652. Add('begin');
  15653. Add(' ClA:=texta;');
  15654. Add(' ClA:=textb;');
  15655. Add(' ClA:=textc;');
  15656. Add(' ClB:=textb;');
  15657. Add(' ClB:=textc;');
  15658. Add(' ClC:=textc;');
  15659. ConvertProgram;
  15660. CheckSource('TestExternalClass_ClassOf',
  15661. LinesToStr([ // statements
  15662. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15663. ' this.$init = function () {',
  15664. ' };',
  15665. ' this.$final = function () {',
  15666. ' };',
  15667. ' this.ProcA = function () {',
  15668. ' };',
  15669. '});',
  15670. 'this.A = null;',
  15671. 'this.ClA = null;',
  15672. 'this.B = null;',
  15673. 'this.ClB = null;',
  15674. 'this.C = null;',
  15675. 'this.ClC = null;',
  15676. '']),
  15677. LinesToStr([ // $mod.$main
  15678. '$mod.ClA = ExtA;',
  15679. '$mod.ClA = ExtB;',
  15680. '$mod.ClA = $mod.TExtC;',
  15681. '$mod.ClB = ExtB;',
  15682. '$mod.ClB = $mod.TExtC;',
  15683. '$mod.ClC = $mod.TExtC;',
  15684. '']));
  15685. end;
  15686. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  15687. begin
  15688. AddModuleWithIntfImplSrc('unit2.pas',
  15689. LinesToStr([
  15690. '{$modeswitch externalclass}',
  15691. 'type',
  15692. ' TExtA = class external name ''ExtA''',
  15693. ' class var Id: longint;',
  15694. ' end;',
  15695. '']),
  15696. '');
  15697. StartUnit(true);
  15698. Add('interface');
  15699. Add('uses unit2;');
  15700. Add('implementation');
  15701. Add('begin');
  15702. Add(' unit2.texta.id:=unit2.texta.id+1;');
  15703. ConvertUnit;
  15704. CheckSource('TestExternalClass_ClassOtherUnit',
  15705. LinesToStr([
  15706. '']),
  15707. LinesToStr([
  15708. 'ExtA.Id = ExtA.Id + 1;',
  15709. '']));
  15710. end;
  15711. procedure TTestModule.TestExternalClass_Is;
  15712. begin
  15713. StartProgram(false);
  15714. Add('{$modeswitch externalclass}');
  15715. Add('type');
  15716. Add(' TExtA = class external name ''ExtA''');
  15717. Add(' end;');
  15718. Add(' TExtAClass = class of TExtA;');
  15719. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15720. Add(' end;');
  15721. Add(' TExtBClass = class of TExtB;');
  15722. Add(' TExtC = class (TExtB)');
  15723. Add(' end;');
  15724. Add(' TExtCClass = class of TExtC;');
  15725. Add('var');
  15726. Add(' A: texta; ClA: TExtAClass;');
  15727. Add(' B: textb; ClB: TExtBClass;');
  15728. Add(' C: textc; ClC: TExtCClass;');
  15729. Add('begin');
  15730. Add(' if a is textb then ;');
  15731. Add(' if a is textc then ;');
  15732. Add(' if b is textc then ;');
  15733. Add(' if cla is textb then ;');
  15734. Add(' if cla is textc then ;');
  15735. Add(' if clb is textc then ;');
  15736. ConvertProgram;
  15737. CheckSource('TestExternalClass_Is',
  15738. LinesToStr([ // statements
  15739. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15740. ' this.$init = function () {',
  15741. ' };',
  15742. ' this.$final = function () {',
  15743. ' };',
  15744. '});',
  15745. 'this.A = null;',
  15746. 'this.ClA = null;',
  15747. 'this.B = null;',
  15748. 'this.ClB = null;',
  15749. 'this.C = null;',
  15750. 'this.ClC = null;',
  15751. '']),
  15752. LinesToStr([ // $mod.$main
  15753. 'if (rtl.isExt($mod.A, ExtB)) ;',
  15754. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  15755. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  15756. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  15757. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  15758. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  15759. '']));
  15760. end;
  15761. procedure TTestModule.TestExternalClass_As;
  15762. begin
  15763. StartProgram(false);
  15764. Add('{$modeswitch externalclass}');
  15765. Add('type');
  15766. Add(' TExtA = class external name ''ExtA''');
  15767. Add(' end;');
  15768. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15769. Add(' end;');
  15770. Add(' TExtC = class (TExtB)');
  15771. Add(' end;');
  15772. Add('var');
  15773. Add(' A: texta;');
  15774. Add(' B: textb;');
  15775. Add(' C: textc;');
  15776. Add('begin');
  15777. Add(' b:=a as textb;');
  15778. Add(' c:=a as textc;');
  15779. Add(' c:=b as textc;');
  15780. ConvertProgram;
  15781. CheckSource('TestExternalClass_Is',
  15782. LinesToStr([ // statements
  15783. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15784. ' this.$init = function () {',
  15785. ' };',
  15786. ' this.$final = function () {',
  15787. ' };',
  15788. '});',
  15789. 'this.A = null;',
  15790. 'this.B = null;',
  15791. 'this.C = null;',
  15792. '']),
  15793. LinesToStr([ // $mod.$main
  15794. '$mod.B = rtl.asExt($mod.A, ExtB);',
  15795. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  15796. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  15797. '']));
  15798. end;
  15799. procedure TTestModule.TestExternalClass_DestructorFail;
  15800. begin
  15801. StartProgram(false);
  15802. Add('{$modeswitch externalclass}');
  15803. Add('type');
  15804. Add(' TExtA = class external name ''ExtA''');
  15805. Add(' destructor Free;');
  15806. Add(' end;');
  15807. SetExpectedPasResolverError('Pascal element not supported: destructor',
  15808. nPasElementNotSupported);
  15809. ConvertProgram;
  15810. end;
  15811. procedure TTestModule.TestExternalClass_New;
  15812. begin
  15813. StartProgram(false);
  15814. Add('{$modeswitch externalclass}');
  15815. Add('type');
  15816. Add(' TExtA = class external name ''ExtA''');
  15817. Add(' constructor New;');
  15818. Add(' constructor New(i: longint; j: longint = 2);');
  15819. Add(' end;');
  15820. Add('var');
  15821. Add(' A: texta;');
  15822. Add('begin');
  15823. Add(' a:=texta.new;');
  15824. Add(' a:=texta(texta.new);');
  15825. Add(' a:=texta.new();');
  15826. Add(' a:=texta.new(1);');
  15827. Add(' with texta do begin');
  15828. Add(' a:=new;');
  15829. Add(' a:=new();');
  15830. Add(' a:=new(2);');
  15831. Add(' end;');
  15832. Add(' a:=test1.texta.new;');
  15833. Add(' a:=test1.texta.new();');
  15834. Add(' a:=test1.texta.new(3);');
  15835. ConvertProgram;
  15836. CheckSource('TestExternalClass_New',
  15837. LinesToStr([ // statements
  15838. 'this.A = null;',
  15839. '']),
  15840. LinesToStr([ // $mod.$main
  15841. '$mod.A = new ExtA();',
  15842. '$mod.A = new ExtA();',
  15843. '$mod.A = new ExtA();',
  15844. '$mod.A = new ExtA(1,2);',
  15845. '$mod.A = new ExtA();',
  15846. '$mod.A = new ExtA();',
  15847. '$mod.A = new ExtA(2,2);',
  15848. '$mod.A = new ExtA();',
  15849. '$mod.A = new ExtA();',
  15850. '$mod.A = new ExtA(3,2);',
  15851. '']));
  15852. end;
  15853. procedure TTestModule.TestExternalClass_ClassOf_New;
  15854. begin
  15855. StartProgram(false);
  15856. Add('{$modeswitch externalclass}');
  15857. Add('type');
  15858. Add(' TExtAClass = class of TExtA;');
  15859. Add(' TExtA = class external name ''ExtA''');
  15860. Add(' C: TExtAClass;');
  15861. Add(' constructor New;');
  15862. Add(' end;');
  15863. Add('var');
  15864. Add(' A: texta;');
  15865. Add(' C: textaclass;');
  15866. Add('begin');
  15867. Add(' a:=c.new;');
  15868. Add(' a:=c.new();');
  15869. Add(' with C do begin');
  15870. Add(' a:=new;');
  15871. Add(' a:=new();');
  15872. Add(' end;');
  15873. Add(' a:=test1.c.new;');
  15874. Add(' a:=test1.c.new();');
  15875. Add(' a:=A.c.new();');
  15876. ConvertProgram;
  15877. CheckSource('TestExternalClass_ClassOf_New',
  15878. LinesToStr([ // statements
  15879. 'this.A = null;',
  15880. 'this.C = null;',
  15881. '']),
  15882. LinesToStr([ // $mod.$main
  15883. '$mod.A = new $mod.C();',
  15884. '$mod.A = new $mod.C();',
  15885. 'var $with1 = $mod.C;',
  15886. '$mod.A = new $with1();',
  15887. '$mod.A = new $with1();',
  15888. '$mod.A = new $mod.C();',
  15889. '$mod.A = new $mod.C();',
  15890. '$mod.A = new $mod.A.C();',
  15891. '']));
  15892. end;
  15893. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  15894. begin
  15895. StartProgram(false);
  15896. Add([
  15897. '{$modeswitch externalclass}',
  15898. 'type',
  15899. ' TExtAClass = class of TExtA;',
  15900. ' TExtA = class external name ''ExtA''',
  15901. ' constructor New;',
  15902. ' end;',
  15903. 'function GetCreator: TExtAClass;',
  15904. 'begin',
  15905. ' Result:=TExtA;',
  15906. 'end;',
  15907. 'var',
  15908. ' A: texta;',
  15909. 'begin',
  15910. ' a:=getcreator.new;',
  15911. ' a:=getcreator().new;',
  15912. ' a:=getcreator().new();',
  15913. ' a:=getcreator.new();',
  15914. ' with getcreator do begin',
  15915. ' a:=new;',
  15916. ' a:=new();',
  15917. ' end;']);
  15918. ConvertProgram;
  15919. CheckSource('TestExternalClass_FuncClassOf_New',
  15920. LinesToStr([ // statements
  15921. 'this.GetCreator = function () {',
  15922. ' var Result = null;',
  15923. ' Result = ExtA;',
  15924. ' return Result;',
  15925. '};',
  15926. 'this.A = null;',
  15927. '']),
  15928. LinesToStr([ // $mod.$main
  15929. '$mod.A = new ($mod.GetCreator())();',
  15930. '$mod.A = new ($mod.GetCreator())();',
  15931. '$mod.A = new ($mod.GetCreator())();',
  15932. '$mod.A = new ($mod.GetCreator())();',
  15933. 'var $with1 = $mod.GetCreator();',
  15934. '$mod.A = new $with1();',
  15935. '$mod.A = new $with1();',
  15936. '']));
  15937. end;
  15938. procedure TTestModule.TestExternalClass_New_PasClassFail;
  15939. begin
  15940. StartProgram(false);
  15941. Add([
  15942. '{$modeswitch externalclass}',
  15943. 'type',
  15944. ' TExtA = class external name ''ExtA''',
  15945. ' constructor New;',
  15946. ' end;',
  15947. ' TBird = class(TExtA)',
  15948. ' end;',
  15949. 'begin',
  15950. ' TBird.new;',
  15951. '']);
  15952. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15953. ConvertProgram;
  15954. end;
  15955. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  15956. begin
  15957. StartProgram(false);
  15958. Add([
  15959. '{$modeswitch externalclass}',
  15960. 'type',
  15961. ' TExtA = class external name ''ExtA''',
  15962. ' constructor New;',
  15963. ' end;',
  15964. ' TBird = class(TExtA)',
  15965. ' end;',
  15966. 'begin',
  15967. ' TBird.new();',
  15968. '']);
  15969. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15970. ConvertProgram;
  15971. end;
  15972. procedure TTestModule.TestExternalClass_LocalConstSameName;
  15973. begin
  15974. StartProgram(false);
  15975. Add('{$modeswitch externalclass}');
  15976. Add('type');
  15977. Add(' TExtA = class external name ''ExtA''');
  15978. Add(' constructor New;');
  15979. Add(' end;');
  15980. Add('function DoIt: longint;');
  15981. Add('const ExtA: longint = 3;');
  15982. Add('begin');
  15983. Add(' Result:=ExtA;');
  15984. Add('end;');
  15985. Add('var');
  15986. Add(' A: texta;');
  15987. Add('begin');
  15988. Add(' a:=texta.new;');
  15989. ConvertProgram;
  15990. CheckSource('TestExternalClass_LocalConstSameName',
  15991. LinesToStr([ // statements
  15992. 'var ExtA$1 = 3;',
  15993. 'this.DoIt = function () {',
  15994. ' var Result = 0;',
  15995. ' Result = ExtA$1;',
  15996. ' return Result;',
  15997. '};',
  15998. 'this.A = null;',
  15999. '']),
  16000. LinesToStr([ // $mod.$main
  16001. '$mod.A = new ExtA();',
  16002. '']));
  16003. end;
  16004. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  16005. begin
  16006. StartProgram(false);
  16007. Add('{$modeswitch externalclass}');
  16008. Add('type');
  16009. Add(' TExtA = class external name ''ExtA''');
  16010. Add(' procedure DoIt;');
  16011. Add(' end;');
  16012. Add(' TMyA = class(TExtA)');
  16013. Add(' procedure DoIt;');
  16014. Add(' end;');
  16015. Add('procedure TMyA.DoIt; begin end;');
  16016. Add('begin');
  16017. ConvertProgram;
  16018. CheckSource('TestExternalClass_ReintroduceOverload',
  16019. LinesToStr([ // statements
  16020. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  16021. ' this.$init = function () {',
  16022. ' };',
  16023. ' this.$final = function () {',
  16024. ' };',
  16025. ' this.DoIt$1 = function () {',
  16026. ' };',
  16027. '});',
  16028. '']),
  16029. LinesToStr([ // $mod.$main
  16030. '']));
  16031. end;
  16032. procedure TTestModule.TestExternalClass_Inherited;
  16033. begin
  16034. StartProgram(false);
  16035. Add('{$modeswitch externalclass}');
  16036. Add('type');
  16037. Add(' TExtA = class external name ''ExtA''');
  16038. Add(' procedure DoIt(i: longint = 1); virtual;');
  16039. Add(' procedure DoSome(j: longint = 2);');
  16040. Add(' end;');
  16041. Add(' TExtB = class external name ''ExtB''(TExtA)');
  16042. Add(' end;');
  16043. Add(' TMyC = class(TExtB)');
  16044. Add(' procedure DoIt(i: longint = 1); override;');
  16045. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  16046. Add(' end;');
  16047. Add('procedure TMyC.DoIt(i: longint);');
  16048. Add('begin');
  16049. Add(' inherited;');
  16050. Add(' inherited DoIt;');
  16051. Add(' inherited DoIt();');
  16052. Add(' inherited DoIt(3);');
  16053. Add(' inherited DoSome;');
  16054. Add(' inherited DoSome();');
  16055. Add(' inherited DoSome(4);');
  16056. Add('end;');
  16057. Add('procedure TMyC.DoSome(j: longint);');
  16058. Add('begin');
  16059. Add(' inherited;');
  16060. Add('end;');
  16061. Add('begin');
  16062. ConvertProgram;
  16063. CheckSource('TestExternalClass_ReintroduceOverload',
  16064. LinesToStr([ // statements
  16065. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  16066. ' this.$init = function () {',
  16067. ' };',
  16068. ' this.$final = function () {',
  16069. ' };',
  16070. ' this.DoIt = function (i) {',
  16071. ' ExtB.DoIt.apply(this, arguments);',
  16072. ' ExtB.DoIt.call(this, 1);',
  16073. ' ExtB.DoIt.call(this, 1);',
  16074. ' ExtB.DoIt.call(this, 3);',
  16075. ' ExtB.DoSome.call(this, 2);',
  16076. ' ExtB.DoSome.call(this, 2);',
  16077. ' ExtB.DoSome.call(this, 4);',
  16078. ' };',
  16079. ' this.DoSome$1 = function (j) {',
  16080. ' ExtB.DoSome.apply(this, arguments);',
  16081. ' };',
  16082. '});',
  16083. '']),
  16084. LinesToStr([ // $mod.$main
  16085. '']));
  16086. end;
  16087. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  16088. begin
  16089. StartProgram(false);
  16090. Add('{$modeswitch externalclass}');
  16091. Add('type');
  16092. Add(' TObject = class');
  16093. Add(' end;');
  16094. Add(' TExtA = class external name ''ExtA''(TObject)');
  16095. Add(' end;');
  16096. Add('begin');
  16097. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  16098. ConvertProgram;
  16099. end;
  16100. procedure TTestModule.TestExternalClass_NewInstance;
  16101. begin
  16102. StartProgram(false);
  16103. Add('{$modeswitch externalclass}');
  16104. Add('type');
  16105. Add(' TExtA = class external name ''ExtA''');
  16106. Add(' end;');
  16107. Add(' TMyB = class(TExtA)');
  16108. Add(' protected');
  16109. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  16110. Add(' end;');
  16111. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16112. Add('begin end;');
  16113. Add('begin');
  16114. ConvertProgram;
  16115. CheckSource('TestExternalClass_NewInstance',
  16116. LinesToStr([ // statements
  16117. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  16118. ' this.$init = function () {',
  16119. ' };',
  16120. ' this.$final = function () {',
  16121. ' };',
  16122. ' this.NewInstance = function (fnname, paramarray) {',
  16123. ' var Result = null;',
  16124. ' return Result;',
  16125. ' };',
  16126. '});',
  16127. '']),
  16128. LinesToStr([ // $mod.$main
  16129. '']));
  16130. end;
  16131. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  16132. begin
  16133. StartProgram(false);
  16134. Add('{$modeswitch externalclass}');
  16135. Add('type');
  16136. Add(' TExtA = class external name ''ExtA''');
  16137. Add(' end;');
  16138. Add(' TMyB = class(TExtA)');
  16139. Add(' protected');
  16140. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  16141. Add(' end;');
  16142. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16143. Add('begin end;');
  16144. Add('begin');
  16145. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  16146. ConvertProgram;
  16147. end;
  16148. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  16149. begin
  16150. StartProgram(false);
  16151. Add('{$modeswitch externalclass}');
  16152. Add('type');
  16153. Add(' TExtA = class external name ''ExtA''');
  16154. Add(' end;');
  16155. Add(' TMyB = class(TExtA)');
  16156. Add(' protected');
  16157. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  16158. Add(' end;');
  16159. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  16160. Add('begin end;');
  16161. Add('begin');
  16162. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  16163. nIncompatibleTypeArgNo);
  16164. ConvertProgram;
  16165. end;
  16166. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  16167. begin
  16168. StartProgram(false);
  16169. Add('{$modeswitch externalclass}');
  16170. Add('type');
  16171. Add(' TExtA = class external name ''ExtA''');
  16172. Add(' end;');
  16173. Add(' TMyB = class(TExtA)');
  16174. Add(' protected');
  16175. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  16176. Add(' end;');
  16177. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  16178. Add('begin end;');
  16179. Add('begin');
  16180. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  16181. nIncompatibleTypeArgNo);
  16182. ConvertProgram;
  16183. end;
  16184. procedure TTestModule.TestExternalClass_PascalProperty;
  16185. begin
  16186. StartProgram(false);
  16187. Add('{$modeswitch externalclass}');
  16188. Add('type');
  16189. Add(' TJSElement = class;');
  16190. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  16191. Add(' TJSElement = class external name ''ExtA''');
  16192. Add(' end;');
  16193. Add(' TControl = class(TJSElement)');
  16194. Add(' private');
  16195. Add(' FOnClick: TJSNotifyEvent;');
  16196. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  16197. Add(' procedure Click(Sender: TJSElement);');
  16198. Add(' end;');
  16199. Add('procedure TControl.Click(Sender: TJSElement);');
  16200. Add('begin');
  16201. Add(' OnClick(Self);');
  16202. Add('end;');
  16203. Add('var');
  16204. Add(' Ctrl: TControl;');
  16205. Add('begin');
  16206. Add(' Ctrl.OnClick:[email protected];');
  16207. Add(' Ctrl.OnClick(Ctrl);');
  16208. ConvertProgram;
  16209. CheckSource('TestExternalClass_PascalProperty',
  16210. LinesToStr([ // statements
  16211. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  16212. ' this.$init = function () {',
  16213. ' this.FOnClick = null;',
  16214. ' };',
  16215. ' this.$final = function () {',
  16216. ' this.FOnClick = undefined;',
  16217. ' };',
  16218. ' this.Click = function (Sender) {',
  16219. ' this.FOnClick(this);',
  16220. ' };',
  16221. '});',
  16222. 'this.Ctrl = null;',
  16223. '']),
  16224. LinesToStr([ // $mod.$main
  16225. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  16226. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  16227. '']));
  16228. end;
  16229. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  16230. begin
  16231. StartProgram(false);
  16232. Add([
  16233. '{$modeswitch externalclass}',
  16234. 'type',
  16235. ' IUnknown = interface end;',
  16236. ' TObject = class',
  16237. ' end;',
  16238. ' TChild = class',
  16239. ' end;',
  16240. ' TExtRootA = class external name ''ExtRootA''',
  16241. ' end;',
  16242. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  16243. ' end;',
  16244. ' TExtRootB = class external name ''ExtRootB''',
  16245. ' end;',
  16246. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  16247. ' end;',
  16248. 'var',
  16249. ' Obj: TObject;',
  16250. ' Child: TChild;',
  16251. ' RootA: TExtRootA;',
  16252. ' ChildA: TExtChildA;',
  16253. ' RootB: TExtRootB;',
  16254. ' ChildB: TExtChildB;',
  16255. ' i: IUnknown;',
  16256. 'begin',
  16257. ' obj:=tobject(roota);',
  16258. ' obj:=tobject(childa);',
  16259. ' child:=tchild(tobject(roota));',
  16260. ' roota:=textroota(obj);',
  16261. ' roota:=textroota(child);',
  16262. ' roota:=textroota(rootb);',
  16263. ' roota:=textroota(childb);',
  16264. ' childa:=textchilda(textroota(obj));',
  16265. ' roota:=TExtRootA(i)',
  16266. '']);
  16267. ConvertProgram;
  16268. CheckSource('TestExternalClass_TypeCastToRootClass',
  16269. LinesToStr([ // statements
  16270. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16271. 'rtl.createClass($mod, "TObject", null, function () {',
  16272. ' this.$init = function () {',
  16273. ' };',
  16274. ' this.$final = function () {',
  16275. ' };',
  16276. '});',
  16277. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16278. '});',
  16279. 'this.Obj = null;',
  16280. 'this.Child = null;',
  16281. 'this.RootA = null;',
  16282. 'this.ChildA = null;',
  16283. 'this.RootB = null;',
  16284. 'this.ChildB = null;',
  16285. 'this.i = null;',
  16286. '']),
  16287. LinesToStr([ // $mod.$main
  16288. '$mod.Obj = $mod.RootA;',
  16289. '$mod.Obj = $mod.ChildA;',
  16290. '$mod.Child = $mod.RootA;',
  16291. '$mod.RootA = $mod.Obj;',
  16292. '$mod.RootA = $mod.Child;',
  16293. '$mod.RootA = $mod.RootB;',
  16294. '$mod.RootA = $mod.ChildB;',
  16295. '$mod.ChildA = $mod.Obj;',
  16296. '$mod.RootA = $mod.i;',
  16297. '']));
  16298. end;
  16299. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  16300. begin
  16301. StartProgram(false);
  16302. Add([
  16303. '{$modeswitch externalclass}',
  16304. 'type',
  16305. ' IUnknown = interface end;',
  16306. ' IBird = interface(IUnknown) end;',
  16307. ' TClass = class of TObject;',
  16308. ' TObject = class',
  16309. ' end;',
  16310. ' TChild = class',
  16311. ' end;',
  16312. ' TJSObject = class external name ''Object''',
  16313. ' end;',
  16314. ' TRec = record end;',
  16315. 'var',
  16316. ' Obj: TObject;',
  16317. ' Child: TChild;',
  16318. ' i: IUnknown;',
  16319. ' Bird: IBird;',
  16320. ' j: TJSObject;',
  16321. ' r: TRec;',
  16322. ' c: TClass;',
  16323. 'begin',
  16324. ' j:=tjsobject(IUnknown);',
  16325. ' j:=tjsobject(IBird);',
  16326. ' j:=tjsobject(TObject);',
  16327. ' j:=tjsobject(TChild);',
  16328. ' j:=tjsobject(TRec);',
  16329. ' j:=tjsobject(Obj);',
  16330. ' j:=tjsobject(Child);',
  16331. ' j:=tjsobject(i);',
  16332. ' j:=tjsobject(Bird);',
  16333. ' j:=tjsobject(r);',
  16334. ' j:=tjsobject(c);',
  16335. '']);
  16336. ConvertProgram;
  16337. CheckSource('TestExternalClass_TypeCastToJSObject',
  16338. LinesToStr([ // statements
  16339. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16340. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  16341. 'rtl.createClass($mod, "TObject", null, function () {',
  16342. ' this.$init = function () {',
  16343. ' };',
  16344. ' this.$final = function () {',
  16345. ' };',
  16346. '});',
  16347. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16348. '});',
  16349. 'rtl.recNewT($mod, "TRec", function () {',
  16350. ' this.$eq = function (b) {',
  16351. ' return true;',
  16352. ' };',
  16353. ' this.$assign = function (s) {',
  16354. ' return this;',
  16355. ' };',
  16356. '});',
  16357. 'this.Obj = null;',
  16358. 'this.Child = null;',
  16359. 'this.i = null;',
  16360. 'this.Bird = null;',
  16361. 'this.j = null;',
  16362. 'this.r = $mod.TRec.$new();',
  16363. 'this.c = null;',
  16364. '']),
  16365. LinesToStr([ // $mod.$main
  16366. '$mod.j = $mod.IUnknown;',
  16367. '$mod.j = $mod.IBird;',
  16368. '$mod.j = $mod.TObject;',
  16369. '$mod.j = $mod.TChild;',
  16370. '$mod.j = $mod.TRec;',
  16371. '$mod.j = $mod.Obj;',
  16372. '$mod.j = $mod.Child;',
  16373. '$mod.j = $mod.i;',
  16374. '$mod.j = $mod.Bird;',
  16375. '$mod.j = $mod.r;',
  16376. '$mod.j = $mod.c;',
  16377. '']));
  16378. end;
  16379. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  16380. begin
  16381. StartProgram(false);
  16382. Add('{$modeswitch externalclass}');
  16383. Add('type');
  16384. Add(' TJSString = class external name ''String''');
  16385. Add(' class function fromCharCode() : string; varargs;');
  16386. Add(' function anchor(const aName : string) : string;');
  16387. Add(' end;');
  16388. Add('var');
  16389. Add(' s: string;');
  16390. Add('begin');
  16391. Add(' s:=TJSString.fromCharCode(65,66);');
  16392. Add(' s:=TJSString(s).anchor(s);');
  16393. Add(' s:=TJSString(''foo'').anchor(s);');
  16394. ConvertProgram;
  16395. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  16396. LinesToStr([ // statements
  16397. 'this.s = "";',
  16398. '']),
  16399. LinesToStr([ // $mod.$main
  16400. '$mod.s = String.fromCharCode(65, 66);',
  16401. '$mod.s = $mod.s.anchor($mod.s);',
  16402. '$mod.s = "foo".anchor($mod.s);',
  16403. '']));
  16404. end;
  16405. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  16406. begin
  16407. StartProgram(false);
  16408. Add([
  16409. '{$modeswitch externalclass}',
  16410. 'type',
  16411. ' TJSObject = class external name ''Object'' end;',
  16412. ' TJSFunction = class external name ''Function''',
  16413. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  16414. ' function call(thisArg: TJSObject): JSValue; varargs;',
  16415. ' end;',
  16416. ' TObject = class',
  16417. ' procedure DoIt(i: longint);',
  16418. ' end;',
  16419. ' TFuncInt = function(o: TObject): longint;',
  16420. 'function GetIt(o: TObject): longint;',
  16421. ' procedure Sub; begin end;',
  16422. 'var',
  16423. ' f: TJSFunction;',
  16424. ' fi: TFuncInt;',
  16425. 'begin',
  16426. ' fi:=TFuncInt(f);',
  16427. ' f:=TJSFunction(fi);',
  16428. ' f:=TJSFunction(@GetIt);',
  16429. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  16430. ' f:=TJSFunction(@Sub);',
  16431. ' f:=TJSFunction(@o.doit);',
  16432. ' f:=TJSFunction(fi).bind(nil,4)',
  16433. 'end;',
  16434. 'procedure TObject.DoIt(i: longint);',
  16435. ' procedure Sub; begin end;',
  16436. 'var f: TJSFunction;',
  16437. 'begin',
  16438. ' f:=TJSFunction(@DoIt);',
  16439. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  16440. ' f:=TJSFunction(@Sub);',
  16441. ' f:=TJSFunction(@GetIt);',
  16442. 'end;',
  16443. 'begin']);
  16444. ConvertProgram;
  16445. CheckSource('TestExternalClass_TypeCastToJSFunction',
  16446. LinesToStr([ // statements
  16447. 'rtl.createClass($mod, "TObject", null, function () {',
  16448. ' this.$init = function () {',
  16449. ' };',
  16450. ' this.$final = function () {',
  16451. ' };',
  16452. ' this.DoIt = function (i) {',
  16453. ' var $Self = this;',
  16454. ' function Sub() {',
  16455. ' };',
  16456. ' var f = null;',
  16457. ' f = rtl.createCallback($Self, "DoIt");',
  16458. ' f = rtl.createCallback($Self, "DoIt").bind(null, 13);',
  16459. ' f = Sub;',
  16460. ' f = $mod.GetIt;',
  16461. ' };',
  16462. '});',
  16463. 'this.GetIt = function (o) {',
  16464. ' var Result = 0;',
  16465. ' function Sub() {',
  16466. ' };',
  16467. ' var f = null;',
  16468. ' var fi = null;',
  16469. ' fi = f;',
  16470. ' f = fi;',
  16471. ' f = $mod.GetIt;',
  16472. ' f = $mod.GetIt.bind(null, 3);',
  16473. ' f = Sub;',
  16474. ' f = rtl.createCallback(o, "DoIt");',
  16475. ' f = fi.bind(null, 4);',
  16476. ' return Result;',
  16477. '};',
  16478. '']),
  16479. LinesToStr([ // $mod.$main
  16480. '']));
  16481. end;
  16482. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  16483. begin
  16484. StartProgram(false);
  16485. Add([
  16486. '{$mode delphi}',
  16487. '{$modeswitch externalclass}',
  16488. 'type',
  16489. ' TJSObject = class external name ''Object'' end;',
  16490. ' TJSWindow = class external name ''Window''(TJSObject)',
  16491. ' procedure Open;',
  16492. ' end;',
  16493. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  16494. ' procedure Execute;',
  16495. ' end;',
  16496. 'procedure Fly;',
  16497. 'var',
  16498. ' w: TJSWindow;',
  16499. ' e: TJSEventTarget;',
  16500. 'begin',
  16501. ' w:=TJSWindow(e);',
  16502. ' e:=TJSEventTarget(w);',
  16503. 'end;',
  16504. 'begin']);
  16505. ConvertProgram;
  16506. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  16507. LinesToStr([ // statements
  16508. 'this.Fly = function () {',
  16509. ' var w = null;',
  16510. ' var e = null;',
  16511. ' w = e;',
  16512. ' e = w;',
  16513. '};',
  16514. '']),
  16515. LinesToStr([ // $mod.$main
  16516. '']));
  16517. end;
  16518. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  16519. begin
  16520. StartProgram(false);
  16521. Add('{$modeswitch externalclass}');
  16522. Add('type');
  16523. Add(' TJSString = class external name ''String''');
  16524. Add(' class function fromCharCode() : string; varargs;');
  16525. Add(' end;');
  16526. Add('var');
  16527. Add(' s: string;');
  16528. Add(' sObj: TJSString;');
  16529. Add('begin');
  16530. Add(' s:=sObj.fromCharCode(65,66);');
  16531. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  16532. nExternalClassInstanceCannotAccessStaticX);
  16533. ConvertProgram;
  16534. end;
  16535. procedure TTestModule.TestExternalClass_BracketAccessor;
  16536. begin
  16537. StartProgram(false);
  16538. Add([
  16539. '{$modeswitch externalclass}',
  16540. 'type',
  16541. ' TJSArray = class external name ''Array2''',
  16542. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16543. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16544. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16545. ' end;',
  16546. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  16547. 'begin end;',
  16548. 'var',
  16549. ' Arr: tjsarray;',
  16550. ' s: string;',
  16551. ' i: longint;',
  16552. ' v: jsvalue;',
  16553. 'begin',
  16554. ' v:=arr[0];',
  16555. ' v:=arr.items[1];',
  16556. ' arr[2]:=s;',
  16557. ' arr.items[3]:=s;',
  16558. ' arr[4]:=i;',
  16559. ' arr[5]:=arr[6];',
  16560. ' arr.items[7]:=arr.items[8];',
  16561. ' with arr do items[9]:=items[10];',
  16562. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  16563. ' with arr do begin',
  16564. ' v:=GetItems(14);',
  16565. ' setitems(15,16);',
  16566. ' end;',
  16567. ' v:=test1.arr.items[17];',
  16568. ' test1.arr.items[18]:=v;',
  16569. '']);
  16570. ConvertProgram;
  16571. CheckSource('TestExternalClass_BracketAccessor',
  16572. LinesToStr([ // statements
  16573. 'this.DoIt = function (vI, vJ, vK, vL) {',
  16574. '};',
  16575. 'this.Arr = null;',
  16576. 'this.s = "";',
  16577. 'this.i = 0;',
  16578. 'this.v = undefined;',
  16579. '']),
  16580. LinesToStr([ // $mod.$main
  16581. '$mod.v = $mod.Arr[0];',
  16582. '$mod.v = $mod.Arr[1];',
  16583. '$mod.Arr[2] = $mod.s;',
  16584. '$mod.Arr[3] = $mod.s;',
  16585. '$mod.Arr[4] = $mod.i;',
  16586. '$mod.Arr[5] = $mod.Arr[6];',
  16587. '$mod.Arr[7] = $mod.Arr[8];',
  16588. 'var $with1 = $mod.Arr;',
  16589. '$with1[9] = $with1[10];',
  16590. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  16591. ' a: 9,',
  16592. ' p: $mod.Arr,',
  16593. ' get: function () {',
  16594. ' return this.p[this.a];',
  16595. ' },',
  16596. ' set: function (v) {',
  16597. ' this.p[this.a] = v;',
  16598. ' }',
  16599. '}, {',
  16600. ' a: 10,',
  16601. ' p: $mod.Arr,',
  16602. ' get: function () {',
  16603. ' return this.p[this.a];',
  16604. ' },',
  16605. ' set: function (v) {',
  16606. ' this.p[this.a] = v;',
  16607. ' }',
  16608. '});',
  16609. 'var $with2 = $mod.Arr;',
  16610. '$mod.v = $with2[14];',
  16611. '$with2[15] = 16;',
  16612. '$mod.v = $mod.Arr[17];',
  16613. '$mod.Arr[18] = $mod.v;',
  16614. '']));
  16615. end;
  16616. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  16617. begin
  16618. StartProgram(false);
  16619. Add([
  16620. '{$modeswitch externalclass}',
  16621. 'type',
  16622. ' TJSArray = class external name ''Array2''',
  16623. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16624. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16625. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16626. ' end;',
  16627. ' TMyArr = class(TJSArray)',
  16628. ' procedure DoIt;',
  16629. ' end;',
  16630. 'procedure tmyarr.DoIt;',
  16631. 'begin',
  16632. ' Items[1]:=Items[2];',
  16633. ' SetItems(3,getItems(4));',
  16634. 'end;',
  16635. 'var',
  16636. ' Arr: tmyarr;',
  16637. ' s: string;',
  16638. ' i: longint;',
  16639. ' v: jsvalue;',
  16640. 'begin',
  16641. ' v:=arr[0];',
  16642. ' v:=arr.items[1];',
  16643. ' arr[2]:=s;',
  16644. ' arr.items[3]:=s;',
  16645. ' arr[4]:=i;',
  16646. ' arr[5]:=arr[6];',
  16647. ' arr.items[7]:=arr.items[8];',
  16648. ' with arr do items[9]:=items[10];',
  16649. ' with arr do begin',
  16650. ' v:=GetItems(14);',
  16651. ' setitems(15,16);',
  16652. ' end;',
  16653. '']);
  16654. ConvertProgram;
  16655. CheckSource('TestExternalClass_BracketAccessor_Call',
  16656. LinesToStr([ // statements
  16657. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  16658. ' this.$init = function () {',
  16659. ' };',
  16660. ' this.$final = function () {',
  16661. ' };',
  16662. ' this.DoIt = function () {',
  16663. ' this[1] = this[2];',
  16664. ' this[3] = this[4];',
  16665. ' };',
  16666. '});',
  16667. 'this.Arr = null;',
  16668. 'this.s = "";',
  16669. 'this.i = 0;',
  16670. 'this.v = undefined;',
  16671. '']),
  16672. LinesToStr([ // $mod.$main
  16673. '$mod.v = $mod.Arr[0];',
  16674. '$mod.v = $mod.Arr[1];',
  16675. '$mod.Arr[2] = $mod.s;',
  16676. '$mod.Arr[3] = $mod.s;',
  16677. '$mod.Arr[4] = $mod.i;',
  16678. '$mod.Arr[5] = $mod.Arr[6];',
  16679. '$mod.Arr[7] = $mod.Arr[8];',
  16680. 'var $with1 = $mod.Arr;',
  16681. '$with1[9] = $with1[10];',
  16682. 'var $with2 = $mod.Arr;',
  16683. '$mod.v = $with2[14];',
  16684. '$with2[15] = 16;',
  16685. '']));
  16686. end;
  16687. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  16688. begin
  16689. StartProgram(false);
  16690. Add('{$modeswitch externalclass}');
  16691. Add('type');
  16692. Add(' TJSArray = class external name ''Array2''');
  16693. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  16694. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  16695. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  16696. Add(' end;');
  16697. Add('begin');
  16698. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  16699. nBracketAccessorOfExternalClassMustHaveOneParameter);
  16700. ConvertProgram;
  16701. end;
  16702. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  16703. begin
  16704. StartProgram(false);
  16705. Add('{$modeswitch externalclass}');
  16706. Add('type');
  16707. Add(' TJSArray = class external name ''Array2''');
  16708. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16709. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  16710. Add(' end;');
  16711. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  16712. Add('begin end;');
  16713. Add('var');
  16714. Add(' Arr: tjsarray;');
  16715. Add(' v: jsvalue;');
  16716. Add('begin');
  16717. Add(' v:=arr[0];');
  16718. Add(' v:=arr.items[1];');
  16719. Add(' with arr do v:=items[2];');
  16720. Add(' doit(arr[3],arr[4]);');
  16721. ConvertProgram;
  16722. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  16723. LinesToStr([ // statements
  16724. 'this.DoIt = function (vI, vJ) {',
  16725. '};',
  16726. 'this.Arr = null;',
  16727. 'this.v = undefined;',
  16728. '']),
  16729. LinesToStr([ // $mod.$main
  16730. '$mod.v = $mod.Arr[0];',
  16731. '$mod.v = $mod.Arr[1];',
  16732. 'var $with1 = $mod.Arr;',
  16733. '$mod.v = $with1[2];',
  16734. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  16735. '']));
  16736. end;
  16737. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  16738. begin
  16739. StartProgram(false);
  16740. Add('{$modeswitch externalclass}');
  16741. Add('type');
  16742. Add(' TJSArray = class external name ''Array2''');
  16743. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16744. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16745. Add(' end;');
  16746. Add('var');
  16747. Add(' Arr: tjsarray;');
  16748. Add(' s: string;');
  16749. Add(' i: longint;');
  16750. Add(' v: jsvalue;');
  16751. Add('begin');
  16752. Add(' arr[2]:=s;');
  16753. Add(' arr.items[3]:=s;');
  16754. Add(' arr[4]:=i;');
  16755. Add(' with arr do items[5]:=i;');
  16756. ConvertProgram;
  16757. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  16758. LinesToStr([ // statements
  16759. 'this.Arr = null;',
  16760. 'this.s = "";',
  16761. 'this.i = 0;',
  16762. 'this.v = undefined;',
  16763. '']),
  16764. LinesToStr([ // $mod.$main
  16765. '$mod.Arr[2] = $mod.s;',
  16766. '$mod.Arr[3] = $mod.s;',
  16767. '$mod.Arr[4] = $mod.i;',
  16768. 'var $with1 = $mod.Arr;',
  16769. '$with1[5] = $mod.i;',
  16770. '']));
  16771. end;
  16772. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  16773. begin
  16774. StartProgram(false);
  16775. Add('{$modeswitch externalclass}');
  16776. Add('type');
  16777. Add(' TJSArray = class external name ''Array2''');
  16778. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16779. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16780. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  16781. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  16782. Add(' end;');
  16783. Add('var');
  16784. Add(' Arr: tjsarray;');
  16785. Add(' s: string;');
  16786. Add(' i: longint;');
  16787. Add(' v: jsvalue;');
  16788. Add('begin');
  16789. Add(' arr[2]:=s;');
  16790. Add(' arr.items[3]:=s;');
  16791. Add(' arr.numbers[4]:=i;');
  16792. Add(' with arr do items[5]:=i;');
  16793. Add(' with arr do numbers[6]:=i;');
  16794. ConvertProgram;
  16795. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  16796. LinesToStr([ // statements
  16797. 'this.Arr = null;',
  16798. 'this.s = "";',
  16799. 'this.i = 0;',
  16800. 'this.v = undefined;',
  16801. '']),
  16802. LinesToStr([ // $mod.$main
  16803. '$mod.Arr[2] = $mod.s;',
  16804. '$mod.Arr[3] = $mod.s;',
  16805. '$mod.Arr[4] = $mod.i;',
  16806. 'var $with1 = $mod.Arr;',
  16807. '$with1[5] = $mod.i;',
  16808. 'var $with2 = $mod.Arr;',
  16809. '$with2[6] = $mod.i;',
  16810. '']));
  16811. end;
  16812. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  16813. begin
  16814. StartProgram(false);
  16815. Add('{$modeswitch externalclass}');
  16816. Add('type');
  16817. Add(' TJSArray = class external name ''Array2''');
  16818. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16819. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16820. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  16821. Add(' end;');
  16822. Add('var');
  16823. Add(' Arr: tjsarray;');
  16824. Add(' i: longint;');
  16825. Add(' IntArr: array of longint;');
  16826. Add(' v: jsvalue;');
  16827. Add('begin');
  16828. Add(' v:=arr.items[i];');
  16829. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  16830. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  16831. ConvertProgram;
  16832. CheckSource('TestExternalClass_BracketAccessor_Index',
  16833. LinesToStr([ // statements
  16834. 'this.Arr = null;',
  16835. 'this.i = 0;',
  16836. 'this.IntArr = [];',
  16837. 'this.v = undefined;',
  16838. '']),
  16839. LinesToStr([ // $mod.$main
  16840. '$mod.v = $mod.Arr[$mod.i];',
  16841. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  16842. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  16843. '']));
  16844. end;
  16845. procedure TTestModule.TestExternalClass_ForInJSObject;
  16846. begin
  16847. StartProgram(false);
  16848. Add([
  16849. '{$modeswitch externalclass}',
  16850. 'type',
  16851. ' TJSObject = class external name ''Object''',
  16852. ' end;',
  16853. 'var',
  16854. ' o: TJSObject;',
  16855. ' key: string;',
  16856. 'begin',
  16857. ' for key in o do',
  16858. ' if key=''abc'' then ;',
  16859. '']);
  16860. ConvertProgram;
  16861. CheckSource('TestExternalClass_ForInJSObject',
  16862. LinesToStr([ // statements
  16863. 'this.o = null;',
  16864. 'this.key = "";',
  16865. '']),
  16866. LinesToStr([ // $mod.$main
  16867. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  16868. '']));
  16869. end;
  16870. procedure TTestModule.TestExternalClass_ForInJSArray;
  16871. begin
  16872. StartProgram(false);
  16873. Add([
  16874. '{$modeswitch externalclass}',
  16875. 'type',
  16876. ' TJSInt8Array = class external name ''Int8Array''',
  16877. ' private',
  16878. ' flength: NativeInt external name ''length'';',
  16879. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  16880. ' public',
  16881. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  16882. ' property Length: NativeInt read flength;',
  16883. ' end;',
  16884. 'var',
  16885. ' a: TJSInt8Array;',
  16886. ' value: shortint;',
  16887. 'begin',
  16888. ' for value in a do',
  16889. ' if value=3 then ;',
  16890. '']);
  16891. ConvertProgram;
  16892. CheckSource('TestExternalClass_ForInJSArray',
  16893. LinesToStr([ // statements
  16894. 'this.a = null;',
  16895. 'this.value = 0;',
  16896. '']),
  16897. LinesToStr([ // $mod.$main
  16898. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  16899. ' $mod.value = $in1[$l2];',
  16900. ' if ($mod.value === 3) ;',
  16901. '};',
  16902. '']));
  16903. end;
  16904. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  16905. begin
  16906. AddModuleWithIntfImplSrc('unit2.pas',
  16907. LinesToStr([
  16908. '{$modeswitch externalclass}',
  16909. 'type',
  16910. ' TJSBufferSource = class external name ''BufferSource''',
  16911. ' end;',
  16912. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  16913. '']),
  16914. '');
  16915. AddModuleWithIntfImplSrc('unit3.pas',
  16916. LinesToStr([
  16917. '{$modeswitch externalclass}',
  16918. 'type',
  16919. ' TJSBufferSource = class external name ''BufferSource''',
  16920. ' end;',
  16921. '']),
  16922. '');
  16923. StartUnit(true);
  16924. Add([
  16925. 'interface',
  16926. 'uses unit2, unit3;',
  16927. 'procedure DoSome(s: TJSBufferSource);',
  16928. 'implementation',
  16929. 'procedure DoSome(s: TJSBufferSource);',
  16930. 'begin',
  16931. ' DoIt(s);',
  16932. 'end;',
  16933. '']);
  16934. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  16935. nIncompatibleTypeArgNo);
  16936. ConvertUnit;
  16937. end;
  16938. procedure TTestModule.TestClassInterface_Corba;
  16939. begin
  16940. StartProgram(false);
  16941. Add([
  16942. '{$interfaces corba}',
  16943. 'type',
  16944. ' IUnknown = interface;',
  16945. ' IUnknown = interface',
  16946. ' [''{00000000-0000-0000-C000-000000000046}'']',
  16947. ' end;',
  16948. ' IInterface = IUnknown;',
  16949. ' IBird = interface(IInterface)',
  16950. ' function GetSize: longint;',
  16951. ' procedure SetSize(i: longint);',
  16952. ' property Size: longint read GetSize write SetSize;',
  16953. ' procedure DoIt(i: longint);',
  16954. ' end;',
  16955. ' TObject = class',
  16956. ' end;',
  16957. ' TBird = class(TObject,IBird)',
  16958. ' function GetSize: longint; virtual; abstract;',
  16959. ' procedure SetSize(i: longint); virtual; abstract;',
  16960. ' procedure DoIt(i: longint); virtual; abstract;',
  16961. ' end;',
  16962. 'var',
  16963. ' BirdIntf: IBird;',
  16964. 'begin',
  16965. ' BirdIntf.Size:=BirdIntf.Size;',
  16966. '']);
  16967. ConvertProgram;
  16968. CheckSource('TestClassInterface_Corba',
  16969. LinesToStr([ // statements
  16970. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  16971. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  16972. 'rtl.createClass($mod, "TObject", null, function () {',
  16973. ' this.$init = function () {',
  16974. ' };',
  16975. ' this.$final = function () {',
  16976. ' };',
  16977. '});',
  16978. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16979. ' rtl.addIntf(this, $mod.IBird);',
  16980. '});',
  16981. 'this.BirdIntf = null;',
  16982. '']),
  16983. LinesToStr([ // $mod.$main
  16984. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  16985. '']));
  16986. end;
  16987. procedure TTestModule.TestClassInterface_ProcExternalFail;
  16988. begin
  16989. StartProgram(false);
  16990. Add([
  16991. '{$interfaces corba}',
  16992. 'type',
  16993. ' IUnknown = interface',
  16994. ' procedure DoIt; external name ''foo'';',
  16995. ' end;',
  16996. 'begin']);
  16997. SetExpectedParserError(
  16998. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  16999. nParserNoFieldsAllowed);
  17000. ConvertProgram;
  17001. end;
  17002. procedure TTestModule.TestClassInterface_Overloads;
  17003. begin
  17004. StartProgram(false);
  17005. Add([
  17006. '{$interfaces corba}',
  17007. 'type',
  17008. ' integer = longint;',
  17009. ' IUnknown = interface',
  17010. ' procedure DoIt(i: integer);',
  17011. ' procedure DoIt(s: string);',
  17012. ' end;',
  17013. ' IBird = interface(IUnknown)',
  17014. ' procedure DoIt(b: boolean); overload;',
  17015. ' end;',
  17016. ' TObject = class',
  17017. ' end;',
  17018. ' TBird = class(TObject,IBird)',
  17019. ' procedure DoIt(o: TObject);',
  17020. ' procedure DoIt(s: string);',
  17021. ' procedure DoIt(i: integer);',
  17022. ' procedure DoIt(b: boolean);',
  17023. ' end;',
  17024. 'procedure TBird.DoIt(o: TObject); begin end;',
  17025. 'procedure TBird.DoIt(s: string); begin end;',
  17026. 'procedure TBird.DoIt(i: integer); begin end;',
  17027. 'procedure TBird.DoIt(b: boolean); begin end;',
  17028. 'var',
  17029. ' BirdIntf: IBird;',
  17030. 'begin',
  17031. ' BirdIntf.DoIt(3);',
  17032. ' BirdIntf.DoIt(''abc'');',
  17033. ' BirdIntf.DoIt(true);',
  17034. '']);
  17035. ConvertProgram;
  17036. CheckSource('TestClassInterface_Overloads',
  17037. LinesToStr([ // statements
  17038. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  17039. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  17040. 'rtl.createClass($mod, "TObject", null, function () {',
  17041. ' this.$init = function () {',
  17042. ' };',
  17043. ' this.$final = function () {',
  17044. ' };',
  17045. '});',
  17046. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17047. ' this.DoIt = function (o) {',
  17048. ' };',
  17049. ' this.DoIt$1 = function (s) {',
  17050. ' };',
  17051. ' this.DoIt$2 = function (i) {',
  17052. ' };',
  17053. ' this.DoIt$3 = function (b) {',
  17054. ' };',
  17055. ' rtl.addIntf(this, $mod.IBird, {',
  17056. ' DoIt$2: "DoIt$3",',
  17057. ' DoIt: "DoIt$2"',
  17058. ' });',
  17059. '});',
  17060. 'this.BirdIntf = null;',
  17061. '']),
  17062. LinesToStr([ // $mod.$main
  17063. '$mod.BirdIntf.DoIt(3);',
  17064. '$mod.BirdIntf.DoIt$1("abc");',
  17065. '$mod.BirdIntf.DoIt$2(true);',
  17066. '']));
  17067. end;
  17068. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  17069. begin
  17070. StartProgram(false);
  17071. Add([
  17072. '{$interfaces corba}',
  17073. 'type',
  17074. ' IBird = interface',
  17075. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17076. ' end;',
  17077. ' IDog = interface',
  17078. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17079. ' end;',
  17080. ' TObject = class(IBird,IDog)',
  17081. ' end;',
  17082. 'begin']);
  17083. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  17084. nDuplicateGUIDXInYZ);
  17085. ConvertProgram;
  17086. end;
  17087. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  17088. begin
  17089. StartProgram(false);
  17090. Add([
  17091. '{$interfaces corba}',
  17092. 'type',
  17093. ' IAnimal = interface',
  17094. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17095. ' end;',
  17096. ' IBird = interface(IAnimal)',
  17097. ' end;',
  17098. ' IHawk = interface(IBird)',
  17099. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17100. ' end;',
  17101. 'begin']);
  17102. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  17103. nDuplicateGUIDXInYZ);
  17104. ConvertProgram;
  17105. end;
  17106. procedure TTestModule.TestClassInterface_AncestorImpl;
  17107. begin
  17108. StartProgram(false);
  17109. Add([
  17110. '{$interfaces corba}',
  17111. 'type',
  17112. ' integer = longint;',
  17113. ' IUnknown = interface',
  17114. ' procedure DoIt(i: integer);',
  17115. ' end;',
  17116. ' IBird = interface',
  17117. ' procedure Fly(i: integer);',
  17118. ' end;',
  17119. ' TObject = class(IUnknown)',
  17120. ' procedure DoIt(i: integer);',
  17121. ' end;',
  17122. ' TBird = class(IBird)',
  17123. ' procedure Fly(i: integer);',
  17124. ' end;',
  17125. 'procedure TObject.DoIt(i: integer); begin end;',
  17126. 'procedure TBird.Fly(i: integer); begin end;',
  17127. 'begin',
  17128. '']);
  17129. ConvertProgram;
  17130. CheckSource('TestClassInterface_AncestorIntf',
  17131. LinesToStr([ // statements
  17132. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  17133. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  17134. 'rtl.createClass($mod, "TObject", null, function () {',
  17135. ' this.$init = function () {',
  17136. ' };',
  17137. ' this.$final = function () {',
  17138. ' };',
  17139. ' this.DoIt = function (i) {',
  17140. ' };',
  17141. ' rtl.addIntf(this, $mod.IUnknown);',
  17142. '});',
  17143. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17144. ' this.Fly = function (i) {',
  17145. ' };',
  17146. ' rtl.addIntf(this, $mod.IBird);',
  17147. ' rtl.addIntf(this, $mod.IUnknown);',
  17148. '});',
  17149. '']),
  17150. LinesToStr([ // $mod.$main
  17151. '']));
  17152. end;
  17153. procedure TTestModule.TestClassInterface_ImplReintroduce;
  17154. begin
  17155. StartProgram(false);
  17156. Add([
  17157. '{$interfaces corba}',
  17158. 'type',
  17159. ' integer = longint;',
  17160. ' IBird = interface',
  17161. ' procedure DoIt(i: integer);',
  17162. ' end;',
  17163. ' TObject = class',
  17164. ' procedure DoIt(i: integer);',
  17165. ' end;',
  17166. ' TBird = class(IBird)',
  17167. ' procedure DoIt(i: integer); virtual; reintroduce;',
  17168. ' end;',
  17169. 'procedure TObject.DoIt(i: integer); begin end;',
  17170. 'procedure TBird.DoIt(i: integer); begin end;',
  17171. 'begin',
  17172. '']);
  17173. ConvertProgram;
  17174. CheckSource('TestClassInterface_ImplReintroduce',
  17175. LinesToStr([ // statements
  17176. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  17177. 'rtl.createClass($mod, "TObject", null, function () {',
  17178. ' this.$init = function () {',
  17179. ' };',
  17180. ' this.$final = function () {',
  17181. ' };',
  17182. ' this.DoIt = function (i) {',
  17183. ' };',
  17184. '});',
  17185. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17186. ' this.DoIt$1 = function (i) {',
  17187. ' };',
  17188. ' rtl.addIntf(this, $mod.IBird, {',
  17189. ' DoIt: "DoIt$1"',
  17190. ' });',
  17191. '});',
  17192. '']),
  17193. LinesToStr([ // $mod.$main
  17194. '']));
  17195. end;
  17196. procedure TTestModule.TestClassInterface_MethodResolution;
  17197. begin
  17198. StartProgram(false);
  17199. Add([
  17200. '{$interfaces corba}',
  17201. 'type',
  17202. ' IUnknown = interface',
  17203. ' procedure Walk(i: longint);',
  17204. ' end;',
  17205. ' IBird = interface(IUnknown)',
  17206. ' procedure Walk(b: boolean); overload;',
  17207. ' procedure Fly(s: string);',
  17208. ' end;',
  17209. ' TObject = class',
  17210. ' end;',
  17211. ' TBird = class(TObject,IBird)',
  17212. ' procedure IBird.Fly = Move;',
  17213. ' procedure IBird.Walk = Hop;',
  17214. ' procedure Hop(i: longint);',
  17215. ' procedure Move(s: string);',
  17216. ' procedure Hop(b: boolean);',
  17217. ' end;',
  17218. 'procedure TBird.Move(s: string); begin end;',
  17219. 'procedure TBird.Hop(i: longint); begin end;',
  17220. 'procedure TBird.Hop(b: boolean); begin end;',
  17221. 'var',
  17222. ' BirdIntf: IBird;',
  17223. 'begin',
  17224. ' BirdIntf.Walk(3);',
  17225. ' BirdIntf.Walk(true);',
  17226. ' BirdIntf.Fly(''abc'');',
  17227. '']);
  17228. ConvertProgram;
  17229. CheckSource('TestClassInterface_MethodResolution',
  17230. LinesToStr([ // statements
  17231. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  17232. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  17233. 'rtl.createClass($mod, "TObject", null, function () {',
  17234. ' this.$init = function () {',
  17235. ' };',
  17236. ' this.$final = function () {',
  17237. ' };',
  17238. '});',
  17239. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17240. ' this.Hop = function (i) {',
  17241. ' };',
  17242. ' this.Move = function (s) {',
  17243. ' };',
  17244. ' this.Hop$1 = function (b) {',
  17245. ' };',
  17246. ' rtl.addIntf(this, $mod.IBird, {',
  17247. ' Walk$1: "Hop$1",',
  17248. ' Fly: "Move",',
  17249. ' Walk: "Hop"',
  17250. ' });',
  17251. '});',
  17252. 'this.BirdIntf = null;',
  17253. '']),
  17254. LinesToStr([ // $mod.$main
  17255. '$mod.BirdIntf.Walk(3);',
  17256. '$mod.BirdIntf.Walk$1(true);',
  17257. '$mod.BirdIntf.Fly("abc");',
  17258. '']));
  17259. end;
  17260. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  17261. begin
  17262. StartProgram(false);
  17263. Add([
  17264. '{$interfaces com}',
  17265. 'type',
  17266. ' IUnknown = interface',
  17267. ' function _AddRef: longint;',
  17268. ' procedure Walk;',
  17269. ' end;',
  17270. ' IBird = interface end;',
  17271. ' IDog = interface end;',
  17272. ' TObject = class(IBird,IDog)',
  17273. ' function _AddRef: longint; virtual; abstract;',
  17274. ' procedure Walk; virtual; abstract;',
  17275. ' end;',
  17276. ' TBird = class(IUnknown)',
  17277. ' end;',
  17278. 'begin',
  17279. '']);
  17280. ConvertProgram;
  17281. CheckSource('TestClassInterface_COM_AncestorLess',
  17282. LinesToStr([ // statements
  17283. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  17284. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  17285. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  17286. 'rtl.createClass($mod, "TObject", null, function () {',
  17287. ' this.$init = function () {',
  17288. ' };',
  17289. ' this.$final = function () {',
  17290. ' };',
  17291. ' rtl.addIntf(this, $mod.IBird);',
  17292. ' rtl.addIntf(this, $mod.IDog);',
  17293. '});',
  17294. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17295. ' rtl.addIntf(this, $mod.IUnknown);',
  17296. ' rtl.addIntf(this, $mod.IBird);',
  17297. ' rtl.addIntf(this, $mod.IDog);',
  17298. '});',
  17299. '']),
  17300. LinesToStr([ // $mod.$main
  17301. '']));
  17302. end;
  17303. procedure TTestModule.TestClassInterface_MethodOverride;
  17304. begin
  17305. StartProgram(false);
  17306. Add([
  17307. '{$interfaces corba}',
  17308. 'type',
  17309. ' IUnknown = interface',
  17310. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  17311. ' procedure Go;',
  17312. ' end;',
  17313. ' TObject = class(IUnknown)',
  17314. ' procedure Go; virtual; abstract;',
  17315. ' end;',
  17316. ' TBird = class',
  17317. ' procedure Go; override;',
  17318. ' end;',
  17319. ' TCat = class(TObject)',
  17320. ' procedure Go; override;',
  17321. ' end;',
  17322. ' TDog = class(TObject, IUnknown)',
  17323. ' procedure Go; override;',
  17324. ' end;',
  17325. 'procedure TBird.Go; begin end;',
  17326. 'procedure TCat.Go; begin end;',
  17327. 'procedure TDog.Go; begin end;',
  17328. 'begin',
  17329. '']);
  17330. ConvertProgram;
  17331. CheckSource('TestClassInterface_MethodOverride',
  17332. LinesToStr([ // statements
  17333. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  17334. 'rtl.createClass($mod, "TObject", null, function () {',
  17335. ' this.$init = function () {',
  17336. ' };',
  17337. ' this.$final = function () {',
  17338. ' };',
  17339. ' rtl.addIntf(this, $mod.IUnknown);',
  17340. '});',
  17341. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17342. ' this.Go = function () {',
  17343. ' };',
  17344. ' rtl.addIntf(this, $mod.IUnknown);',
  17345. '});',
  17346. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  17347. ' this.Go = function () {',
  17348. ' };',
  17349. ' rtl.addIntf(this, $mod.IUnknown);',
  17350. '});',
  17351. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  17352. ' this.Go = function () {',
  17353. ' };',
  17354. ' rtl.addIntf(this, $mod.IUnknown);',
  17355. '});',
  17356. '']),
  17357. LinesToStr([ // $mod.$main
  17358. '']));
  17359. end;
  17360. procedure TTestModule.TestClassInterface_Corba_Delegation;
  17361. begin
  17362. StartProgram(false);
  17363. Add([
  17364. '{$interfaces corba}',
  17365. 'type',
  17366. ' IUnknown = interface',
  17367. ' end;',
  17368. ' IBird = interface(IUnknown)',
  17369. ' procedure Fly(s: string);',
  17370. ' end;',
  17371. ' IEagle = interface(IBird)',
  17372. ' end;',
  17373. ' IDove = interface(IBird)',
  17374. ' end;',
  17375. ' ISwallow = interface(IBird)',
  17376. ' end;',
  17377. ' TObject = class',
  17378. ' end;',
  17379. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17380. ' procedure Fly(s: string); virtual; abstract;',
  17381. ' end;',
  17382. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17383. ' FBirdIntf: IBird;',
  17384. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  17385. ' function GetEagleIntf: IEagle; virtual; abstract;',
  17386. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17387. ' FDoveObj: TBird;',
  17388. ' property DoveObj: TBird read FDoveObj implements IDove;',
  17389. ' function GetSwallowObj: TBird; virtual; abstract;',
  17390. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17391. ' end;',
  17392. 'begin',
  17393. '']);
  17394. ConvertProgram;
  17395. CheckSource('TestClassInterface_Delegation',
  17396. LinesToStr([ // statements
  17397. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17398. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17399. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17400. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17401. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17402. 'rtl.createClass($mod, "TObject", null, function () {',
  17403. ' this.$init = function () {',
  17404. ' };',
  17405. ' this.$final = function () {',
  17406. ' };',
  17407. '});',
  17408. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17409. ' rtl.addIntf(this, $mod.IBird);',
  17410. ' rtl.addIntf(this, $mod.IEagle);',
  17411. ' rtl.addIntf(this, $mod.IDove);',
  17412. ' rtl.addIntf(this, $mod.ISwallow);',
  17413. '});',
  17414. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17415. ' this.$init = function () {',
  17416. ' $mod.TObject.$init.call(this);',
  17417. ' this.FBirdIntf = null;',
  17418. ' this.FDoveObj = null;',
  17419. ' };',
  17420. ' this.$final = function () {',
  17421. ' this.FBirdIntf = undefined;',
  17422. ' this.FDoveObj = undefined;',
  17423. ' $mod.TObject.$final.call(this);',
  17424. ' };',
  17425. ' this.$intfmaps = {',
  17426. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17427. ' return this.FBirdIntf;',
  17428. ' },',
  17429. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17430. ' return this.GetEagleIntf();',
  17431. ' },',
  17432. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17433. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17434. ' },',
  17435. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17436. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17437. ' }',
  17438. ' };',
  17439. '});',
  17440. '']),
  17441. LinesToStr([ // $mod.$main
  17442. '']));
  17443. end;
  17444. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  17445. begin
  17446. StartProgram(false);
  17447. Add([
  17448. '{$interfaces corba}',
  17449. 'type',
  17450. ' IUnknown = interface',
  17451. ' end;',
  17452. ' IBird = interface(IUnknown)',
  17453. ' procedure Fly(s: string);',
  17454. ' end;',
  17455. ' IEagle = interface(IBird)',
  17456. ' end;',
  17457. ' IDove = interface(IBird)',
  17458. ' end;',
  17459. ' ISwallow = interface(IBird)',
  17460. ' end;',
  17461. ' TObject = class',
  17462. ' end;',
  17463. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17464. ' procedure Fly(s: string); virtual; abstract;',
  17465. ' end;',
  17466. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17467. ' private',
  17468. ' class var FBirdIntf: IBird;',
  17469. ' class var FDoveObj: TBird;',
  17470. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  17471. ' class function GetSwallowObj: TBird; virtual; abstract;',
  17472. ' protected',
  17473. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  17474. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17475. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  17476. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17477. ' end;',
  17478. 'begin',
  17479. '']);
  17480. ConvertProgram;
  17481. CheckSource('TestClassInterface_DelegationStatic',
  17482. LinesToStr([ // statements
  17483. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17484. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17485. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17486. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17487. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17488. 'rtl.createClass($mod, "TObject", null, function () {',
  17489. ' this.$init = function () {',
  17490. ' };',
  17491. ' this.$final = function () {',
  17492. ' };',
  17493. '});',
  17494. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17495. ' rtl.addIntf(this, $mod.IBird);',
  17496. ' rtl.addIntf(this, $mod.IEagle);',
  17497. ' rtl.addIntf(this, $mod.IDove);',
  17498. ' rtl.addIntf(this, $mod.ISwallow);',
  17499. '});',
  17500. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17501. ' this.FBirdIntf = null;',
  17502. ' this.FDoveObj = null;',
  17503. ' this.$intfmaps = {',
  17504. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17505. ' return this.FBirdIntf;',
  17506. ' },',
  17507. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17508. ' return this.$class.GetEagleIntf();',
  17509. ' },',
  17510. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17511. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17512. ' },',
  17513. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17514. ' return rtl.getIntfT(this.$class.GetSwallowObj(), $mod.ISwallow);',
  17515. ' }',
  17516. ' };',
  17517. '});',
  17518. '']),
  17519. LinesToStr([ // $mod.$main
  17520. '']));
  17521. end;
  17522. procedure TTestModule.TestClassInterface_Corba_Operators;
  17523. begin
  17524. StartProgram(false);
  17525. Add([
  17526. '{$interfaces corba}',
  17527. 'type',
  17528. ' IUnknown = interface',
  17529. ' end;',
  17530. ' IBird = interface(IUnknown)',
  17531. ' function GetItems(Index: longint): longint;',
  17532. ' procedure SetItems(Index: longint; Value: longint);',
  17533. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  17534. ' end;',
  17535. ' TObject = class',
  17536. ' end;',
  17537. ' TBird = class(TObject,IBird)',
  17538. ' function GetItems(Index: longint): longint; virtual; abstract;',
  17539. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  17540. ' end;',
  17541. 'var',
  17542. ' IntfVar: IBird = nil;',
  17543. ' IntfVar2: IBird;',
  17544. ' ObjVar: TBird;',
  17545. ' v: JSValue;',
  17546. 'begin',
  17547. ' IntfVar:=nil;',
  17548. ' IntfVar[3]:=IntfVar[4];',
  17549. ' if Assigned(IntfVar) then ;',
  17550. ' IntfVar:=IntfVar2;',
  17551. ' IntfVar:=ObjVar;',
  17552. ' if IntfVar=IntfVar2 then ;',
  17553. ' if IntfVar<>IntfVar2 then ;',
  17554. ' if IntfVar is IBird then ;',
  17555. ' if IntfVar is TBird then ;',
  17556. ' if ObjVar is IBird then ;',
  17557. ' IntfVar:=IntfVar2 as IBird;',
  17558. ' ObjVar:=IntfVar2 as TBird;',
  17559. ' IntfVar:=ObjVar as IBird;',
  17560. ' IntfVar:=IBird(IntfVar2);',
  17561. ' ObjVar:=TBird(IntfVar);',
  17562. ' IntfVar:=IBird(ObjVar);',
  17563. ' v:=IntfVar;',
  17564. ' IntfVar:=IBird(v);',
  17565. ' if v is IBird then ;',
  17566. ' v:=JSValue(IntfVar);',
  17567. ' v:=IBird;',
  17568. '']);
  17569. ConvertProgram;
  17570. CheckSource('TestClassInterface_Corba_Operators',
  17571. LinesToStr([ // statements
  17572. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17573. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  17574. 'rtl.createClass($mod, "TObject", null, function () {',
  17575. ' this.$init = function () {',
  17576. ' };',
  17577. ' this.$final = function () {',
  17578. ' };',
  17579. '});',
  17580. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17581. ' rtl.addIntf(this, $mod.IBird);',
  17582. '});',
  17583. 'this.IntfVar = null;',
  17584. 'this.IntfVar2 = null;',
  17585. 'this.ObjVar = null;',
  17586. 'this.v = undefined;',
  17587. '']),
  17588. LinesToStr([ // $mod.$main
  17589. '$mod.IntfVar = null;',
  17590. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  17591. 'if ($mod.IntfVar != null) ;',
  17592. '$mod.IntfVar = $mod.IntfVar2;',
  17593. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  17594. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  17595. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  17596. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  17597. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  17598. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  17599. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  17600. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  17601. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17602. '$mod.IntfVar = $mod.IntfVar2;',
  17603. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  17604. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17605. '$mod.v = $mod.IntfVar;',
  17606. '$mod.IntfVar = rtl.getObject($mod.v);',
  17607. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  17608. '$mod.v = $mod.IntfVar;',
  17609. '$mod.v = $mod.IBird;',
  17610. '']));
  17611. end;
  17612. procedure TTestModule.TestClassInterface_Corba_Args;
  17613. begin
  17614. StartProgram(false);
  17615. Add([
  17616. '{$interfaces corba}',
  17617. 'type',
  17618. ' IUnknown = interface',
  17619. ' end;',
  17620. ' IBird = interface(IUnknown)',
  17621. ' end;',
  17622. ' TObject = class',
  17623. ' end;',
  17624. ' TBird = class(TObject,IBird)',
  17625. ' end;',
  17626. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  17627. 'begin',
  17628. ' DoIt(i,i,i);',
  17629. 'end;',
  17630. 'procedure Change(var i: IBird; out j: IBird);',
  17631. 'begin',
  17632. ' DoIt(i,i,i);',
  17633. ' Change(i,i);',
  17634. 'end;',
  17635. 'var',
  17636. ' i: IBird;',
  17637. ' o: TBird;',
  17638. 'begin',
  17639. ' DoIt(i,i,i);',
  17640. ' Change(i,i);',
  17641. ' DoIt(o,o,o);',
  17642. '']);
  17643. ConvertProgram;
  17644. CheckSource('TestClassInterface_Corba_Args',
  17645. LinesToStr([ // statements
  17646. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17647. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  17648. 'rtl.createClass($mod, "TObject", null, function () {',
  17649. ' this.$init = function () {',
  17650. ' };',
  17651. ' this.$final = function () {',
  17652. ' };',
  17653. '});',
  17654. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17655. ' rtl.addIntf(this, $mod.IBird);',
  17656. '});',
  17657. 'this.DoIt = function (u, i, j) {',
  17658. ' $mod.DoIt({',
  17659. ' get: function () {',
  17660. ' return i;',
  17661. ' },',
  17662. ' set: function (v) {',
  17663. ' i = v;',
  17664. ' }',
  17665. ' }, i, i);',
  17666. '};',
  17667. 'this.Change = function (i, j) {',
  17668. ' $mod.DoIt(i, i.get(), i.get());',
  17669. ' $mod.Change(i, i);',
  17670. '};',
  17671. 'this.i = null;',
  17672. 'this.o = null;',
  17673. '']),
  17674. LinesToStr([ // $mod.$main
  17675. '$mod.DoIt({',
  17676. ' p: $mod,',
  17677. ' get: function () {',
  17678. ' return this.p.i;',
  17679. ' },',
  17680. ' set: function (v) {',
  17681. ' this.p.i = v;',
  17682. ' }',
  17683. '}, $mod.i, $mod.i);',
  17684. '$mod.Change({',
  17685. ' p: $mod,',
  17686. ' get: function () {',
  17687. ' return this.p.i;',
  17688. ' },',
  17689. ' set: function (v) {',
  17690. ' this.p.i = v;',
  17691. ' }',
  17692. '}, {',
  17693. ' p: $mod,',
  17694. ' get: function () {',
  17695. ' return this.p.i;',
  17696. ' },',
  17697. ' set: function (v) {',
  17698. ' this.p.i = v;',
  17699. ' }',
  17700. '});',
  17701. '$mod.DoIt({',
  17702. ' p: $mod,',
  17703. ' get: function () {',
  17704. ' return this.p.o;',
  17705. ' },',
  17706. ' set: function (v) {',
  17707. ' this.p.o = v;',
  17708. ' }',
  17709. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  17710. '']));
  17711. end;
  17712. procedure TTestModule.TestClassInterface_Corba_ForIn;
  17713. begin
  17714. StartProgram(false);
  17715. Add([
  17716. '{$interfaces corba}',
  17717. 'type',
  17718. ' IUnknown = interface end;',
  17719. ' TObject = class',
  17720. ' Id: longint;',
  17721. ' end;',
  17722. ' IEnumerator = interface(IUnknown)',
  17723. ' function GetCurrent: TObject;',
  17724. ' function MoveNext: Boolean;',
  17725. ' property Current: TObject read GetCurrent;',
  17726. ' end;',
  17727. ' IEnumerable = interface(IUnknown)',
  17728. ' function GetEnumerator: IEnumerator;',
  17729. ' end;',
  17730. 'var',
  17731. ' o: TObject;',
  17732. ' i: IEnumerable;',
  17733. 'begin',
  17734. ' for o in i do o.Id:=3;',
  17735. '']);
  17736. ConvertProgram;
  17737. CheckSource('TestClassInterface_Corba_ForIn',
  17738. LinesToStr([ // statements
  17739. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17740. 'rtl.createClass($mod, "TObject", null, function () {',
  17741. ' this.$init = function () {',
  17742. ' this.Id = 0;',
  17743. ' };',
  17744. ' this.$final = function () {',
  17745. ' };',
  17746. '});',
  17747. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17748. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17749. 'this.o = null;',
  17750. 'this.i = null;',
  17751. '']),
  17752. LinesToStr([ // $mod.$main
  17753. 'var $in1 = $mod.i.GetEnumerator();',
  17754. 'while ($in1.MoveNext()) {',
  17755. ' $mod.o = $in1.GetCurrent();',
  17756. ' $mod.o.Id = 3;',
  17757. '};',
  17758. '']));
  17759. end;
  17760. procedure TTestModule.TestClassInterface_COM_AssignVar;
  17761. begin
  17762. StartProgram(false);
  17763. Add([
  17764. '{$interfaces com}',
  17765. 'type',
  17766. ' IUnknown = interface',
  17767. ' function _AddRef: longint;',
  17768. ' function _Release: longint;',
  17769. ' end;',
  17770. ' TObject = class(IUnknown)',
  17771. ' function _AddRef: longint; virtual; abstract;',
  17772. ' function _Release: longint; virtual; abstract;',
  17773. ' end;',
  17774. 'var',
  17775. ' i: IUnknown;',
  17776. 'procedure DoGlobal(o: TObject);',
  17777. 'begin',
  17778. ' i:=nil;',
  17779. ' i:=o;',
  17780. ' i:=i;',
  17781. 'end;',
  17782. 'procedure DoLocal(o: TObject);',
  17783. 'const k: IUnknown = nil;',
  17784. 'var j: IUnknown;',
  17785. 'begin',
  17786. ' k:=o;',
  17787. ' k:=i;',
  17788. ' j:=o;',
  17789. ' j:=i;',
  17790. 'end;',
  17791. 'var o: TObject;',
  17792. 'begin',
  17793. ' i:=nil;',
  17794. ' i:=o;',
  17795. '']);
  17796. ConvertProgram;
  17797. CheckSource('TestClassInterface_COM_AssignVar',
  17798. LinesToStr([ // statements
  17799. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17800. 'rtl.createClass($mod, "TObject", null, function () {',
  17801. ' this.$init = function () {',
  17802. ' };',
  17803. ' this.$final = function () {',
  17804. ' };',
  17805. ' rtl.addIntf(this, $mod.IUnknown);',
  17806. '});',
  17807. 'this.i = null;',
  17808. 'this.DoGlobal = function (o) {',
  17809. ' rtl.setIntfP($mod, "i", null);',
  17810. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  17811. ' rtl.setIntfP($mod, "i", $mod.i);',
  17812. '};',
  17813. 'var k = null;',
  17814. 'this.DoLocal = function (o) {',
  17815. ' var j = null;',
  17816. ' try{',
  17817. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17818. ' k = rtl.setIntfL(k, $mod.i);',
  17819. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17820. ' j = rtl.setIntfL(j, $mod.i);',
  17821. ' }finally{',
  17822. ' rtl._Release(j);',
  17823. ' };',
  17824. '};',
  17825. 'this.o = null;',
  17826. '']),
  17827. LinesToStr([ // $mod.$main
  17828. 'rtl.setIntfP($mod, "i", null);',
  17829. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  17830. '']));
  17831. end;
  17832. procedure TTestModule.TestClassInterface_COM_AssignArg;
  17833. begin
  17834. StartProgram(false);
  17835. Add([
  17836. '{$interfaces com}',
  17837. 'type',
  17838. ' IUnknown = interface',
  17839. ' function _AddRef: longint;',
  17840. ' function _Release: longint;',
  17841. ' end;',
  17842. ' TObject = class(IUnknown)',
  17843. ' function _AddRef: longint; virtual; abstract;',
  17844. ' function _Release: longint; virtual; abstract;',
  17845. ' end;',
  17846. 'procedure DoDefault(i, j: IUnknown);',
  17847. 'begin',
  17848. ' i:=nil;',
  17849. ' i:=j;',
  17850. 'end;',
  17851. 'begin',
  17852. '']);
  17853. ConvertProgram;
  17854. CheckSource('TestClassInterface_COM_AssignArg',
  17855. LinesToStr([ // statements
  17856. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17857. 'rtl.createClass($mod, "TObject", null, function () {',
  17858. ' this.$init = function () {',
  17859. ' };',
  17860. ' this.$final = function () {',
  17861. ' };',
  17862. ' rtl.addIntf(this, $mod.IUnknown);',
  17863. '});',
  17864. 'this.DoDefault = function (i, j) {',
  17865. ' rtl._AddRef(i);',
  17866. ' try {',
  17867. ' i = rtl.setIntfL(i, null);',
  17868. ' i = rtl.setIntfL(i, j);',
  17869. ' } finally {',
  17870. ' rtl._Release(i);',
  17871. ' };',
  17872. '};',
  17873. '']),
  17874. LinesToStr([ // $mod.$main
  17875. '']));
  17876. end;
  17877. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  17878. begin
  17879. StartProgram(false);
  17880. Add([
  17881. '{$interfaces com}',
  17882. 'type',
  17883. ' IUnknown = interface',
  17884. ' function _AddRef: longint;',
  17885. ' function _Release: longint;',
  17886. ' end;',
  17887. ' TObject = class(IUnknown)',
  17888. ' function _AddRef: longint; virtual; abstract;',
  17889. ' function _Release: longint; virtual; abstract;',
  17890. ' end;',
  17891. 'function DoDefault(i: IUnknown): IUnknown;',
  17892. 'begin',
  17893. ' Result:=i;',
  17894. ' if Result<>nil then exit;',
  17895. 'end;',
  17896. 'begin',
  17897. '']);
  17898. ConvertProgram;
  17899. CheckSource('TestClassInterface_COM_FunctionResult',
  17900. LinesToStr([ // statements
  17901. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17902. 'rtl.createClass($mod, "TObject", null, function () {',
  17903. ' this.$init = function () {',
  17904. ' };',
  17905. ' this.$final = function () {',
  17906. ' };',
  17907. ' rtl.addIntf(this, $mod.IUnknown);',
  17908. '});',
  17909. 'this.DoDefault = function (i) {',
  17910. ' var Result = null;',
  17911. ' var $ok = false;',
  17912. ' try {',
  17913. ' Result = rtl.setIntfL(Result, i);',
  17914. ' if(Result !== null){',
  17915. ' $ok = true;',
  17916. ' return Result;',
  17917. ' };',
  17918. ' $ok = true;',
  17919. ' } finally {',
  17920. ' if(!$ok) rtl._Release(Result);',
  17921. ' };',
  17922. ' return Result;',
  17923. '};',
  17924. '']),
  17925. LinesToStr([ // $mod.$main
  17926. '']));
  17927. end;
  17928. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  17929. begin
  17930. StartProgram(false);
  17931. Add([
  17932. '{$interfaces com}',
  17933. 'type',
  17934. ' IUnknown = interface',
  17935. ' function _AddRef: longint;',
  17936. ' function _Release: longint;',
  17937. ' end;',
  17938. ' TObject = class(IUnknown)',
  17939. ' function _AddRef: longint; virtual; abstract;',
  17940. ' function _Release: longint; virtual; abstract;',
  17941. ' function GetIntf: IUnknown; virtual;',
  17942. ' end;',
  17943. ' TMouse = class',
  17944. ' function GetIntf: IUnknown; override;',
  17945. ' end;',
  17946. 'function TObject.GetIntf: IUnknown; begin end;',
  17947. 'function TMouse.GetIntf: IUnknown;',
  17948. 'var i: IUnknown;',
  17949. 'begin',
  17950. ' inherited;',
  17951. ' inherited GetIntf;',
  17952. ' inherited GetIntf();',
  17953. ' Result:=inherited GetIntf;',
  17954. ' Result:=inherited GetIntf();',
  17955. ' i:=inherited GetIntf;',
  17956. ' i:=inherited GetIntf();',
  17957. 'end;',
  17958. 'begin',
  17959. '']);
  17960. ConvertProgram;
  17961. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  17962. LinesToStr([ // statements
  17963. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17964. 'rtl.createClass($mod, "TObject", null, function () {',
  17965. ' this.$init = function () {',
  17966. ' };',
  17967. ' this.$final = function () {',
  17968. ' };',
  17969. ' this.GetIntf = function () {',
  17970. ' var Result = null;',
  17971. ' return Result;',
  17972. ' };',
  17973. ' rtl.addIntf(this, $mod.IUnknown);',
  17974. '});',
  17975. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  17976. ' this.GetIntf = function () {',
  17977. ' var Result = null;',
  17978. ' var i = null;',
  17979. ' var $ir = rtl.createIntfRefs();',
  17980. ' var $ok = false;',
  17981. ' try {',
  17982. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  17983. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  17984. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  17985. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17986. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17987. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17988. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17989. ' $ok = true;',
  17990. ' } finally {',
  17991. ' $ir.free();',
  17992. ' rtl._Release(i);',
  17993. ' if (!$ok) rtl._Release(Result);',
  17994. ' };',
  17995. ' return Result;',
  17996. ' };',
  17997. ' rtl.addIntf(this, $mod.IUnknown);',
  17998. '});',
  17999. '']),
  18000. LinesToStr([ // $mod.$main
  18001. '']));
  18002. end;
  18003. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  18004. begin
  18005. StartProgram(false);
  18006. Add([
  18007. '{$interfaces com}',
  18008. 'type',
  18009. ' IUnknown = interface',
  18010. ' function _AddRef: longint;',
  18011. ' function _Release: longint;',
  18012. ' end;',
  18013. ' TObject = class(IUnknown)',
  18014. ' function _AddRef: longint; virtual; abstract;',
  18015. ' function _Release: longint; virtual; abstract;',
  18016. ' end;',
  18017. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  18018. 'begin',
  18019. ' if i is IUnknown then ;',
  18020. ' if o is IUnknown then ;',
  18021. ' if i is TObject then ;',
  18022. ' i:=j as IUnknown;',
  18023. ' i:=o as IUnknown;',
  18024. ' o:=j as TObject;',
  18025. ' i:=IUnknown(j);',
  18026. ' i:=IUnknown(o);',
  18027. ' o:=TObject(i);',
  18028. 'end;',
  18029. 'begin',
  18030. '']);
  18031. ConvertProgram;
  18032. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  18033. LinesToStr([ // statements
  18034. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18035. 'rtl.createClass($mod, "TObject", null, function () {',
  18036. ' this.$init = function () {',
  18037. ' };',
  18038. ' this.$final = function () {',
  18039. ' };',
  18040. ' rtl.addIntf(this, $mod.IUnknown);',
  18041. '});',
  18042. 'this.DoDefault = function (i, j, o) {',
  18043. ' rtl._AddRef(i);',
  18044. ' try {',
  18045. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  18046. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  18047. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  18048. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  18049. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18050. ' o = rtl.intfAsClass(j, $mod.TObject);',
  18051. ' i = rtl.setIntfL(i, j);',
  18052. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18053. ' o = rtl.intfToClass(i, $mod.TObject);',
  18054. ' } finally {',
  18055. ' rtl._Release(i);',
  18056. ' };',
  18057. '};',
  18058. '']),
  18059. LinesToStr([ // $mod.$main
  18060. '']));
  18061. end;
  18062. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  18063. begin
  18064. StartProgram(false);
  18065. Add([
  18066. '{$interfaces com}',
  18067. 'type',
  18068. ' IUnknown = interface',
  18069. ' function _AddRef: longint;',
  18070. ' function _Release: longint;',
  18071. ' end;',
  18072. ' TObject = class(IUnknown)',
  18073. ' function _AddRef: longint; virtual; abstract;',
  18074. ' function _Release: longint; virtual; abstract;',
  18075. ' end;',
  18076. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  18077. 'var o: TObject;',
  18078. 'begin',
  18079. ' DoIt(v,v,v,v);',
  18080. ' DoIt(o,o,k,k);',
  18081. 'end;',
  18082. 'procedure DoSome;',
  18083. 'var v: IUnknown;',
  18084. 'begin',
  18085. ' DoIt(v,v,v,v);',
  18086. 'end;',
  18087. 'var i: IUnknown;',
  18088. 'begin',
  18089. ' DoIt(i,i,i,i);',
  18090. '']);
  18091. ConvertProgram;
  18092. CheckSource('TestClassInterface_COM_PassAsArg',
  18093. LinesToStr([ // statements
  18094. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18095. 'rtl.createClass($mod, "TObject", null, function () {',
  18096. ' this.$init = function () {',
  18097. ' };',
  18098. ' this.$final = function () {',
  18099. ' };',
  18100. ' rtl.addIntf(this, $mod.IUnknown);',
  18101. '});',
  18102. 'this.DoIt = function (v, j, k, l) {',
  18103. ' var o = null;',
  18104. ' var $ir = rtl.createIntfRefs();',
  18105. ' rtl._AddRef(v);',
  18106. ' try {',
  18107. ' $mod.DoIt(v, v, {',
  18108. ' get: function () {',
  18109. ' return v;',
  18110. ' },',
  18111. ' set: function (w) {',
  18112. ' v = rtl.setIntfL(v, w);',
  18113. ' }',
  18114. ' }, {',
  18115. ' get: function () {',
  18116. ' return v;',
  18117. ' },',
  18118. ' set: function (w) {',
  18119. ' v = rtl.setIntfL(v, w);',
  18120. ' }',
  18121. ' });',
  18122. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  18123. ' } finally {',
  18124. ' $ir.free();',
  18125. ' rtl._Release(v);',
  18126. ' };',
  18127. '};',
  18128. 'this.DoSome = function () {',
  18129. ' var v = null;',
  18130. ' try {',
  18131. ' $mod.DoIt(v, v, {',
  18132. ' get: function () {',
  18133. ' return v;',
  18134. ' },',
  18135. ' set: function (w) {',
  18136. ' v = rtl.setIntfL(v, w);',
  18137. ' }',
  18138. ' }, {',
  18139. ' get: function () {',
  18140. ' return v;',
  18141. ' },',
  18142. ' set: function (w) {',
  18143. ' v = rtl.setIntfL(v, w);',
  18144. ' }',
  18145. ' });',
  18146. ' } finally {',
  18147. ' rtl._Release(v);',
  18148. ' };',
  18149. '};',
  18150. 'this.i = null;',
  18151. '']),
  18152. LinesToStr([ // $mod.$main
  18153. '$mod.DoIt($mod.i, $mod.i, {',
  18154. ' p: $mod,',
  18155. ' get: function () {',
  18156. ' return this.p.i;',
  18157. ' },',
  18158. ' set: function (v) {',
  18159. ' rtl.setIntfP(this.p, "i", v);',
  18160. ' }',
  18161. '}, {',
  18162. ' p: $mod,',
  18163. ' get: function () {',
  18164. ' return this.p.i;',
  18165. ' },',
  18166. ' set: function (v) {',
  18167. ' rtl.setIntfP(this.p, "i", v);',
  18168. ' }',
  18169. '});',
  18170. '']));
  18171. end;
  18172. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  18173. begin
  18174. StartProgram(false);
  18175. Add([
  18176. '{$interfaces com}',
  18177. 'type',
  18178. ' IUnknown = interface',
  18179. ' function _AddRef: longint;',
  18180. ' function _Release: longint;',
  18181. ' end;',
  18182. ' TObject = class(IUnknown)',
  18183. ' function _AddRef: longint; virtual; abstract;',
  18184. ' function _Release: longint; virtual; abstract;',
  18185. ' end;',
  18186. 'procedure DoIt(out i);',
  18187. 'begin end;',
  18188. 'procedure DoSome;',
  18189. 'var v: IUnknown;',
  18190. 'begin',
  18191. ' DoIt(v);',
  18192. 'end;',
  18193. 'function GetIt: IUnknown;',
  18194. 'begin',
  18195. ' DoIt(Result);',
  18196. 'end;',
  18197. 'var i: IUnknown;',
  18198. 'begin',
  18199. ' DoIt(i);',
  18200. '']);
  18201. ConvertProgram;
  18202. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  18203. LinesToStr([ // statements
  18204. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18205. 'rtl.createClass($mod, "TObject", null, function () {',
  18206. ' this.$init = function () {',
  18207. ' };',
  18208. ' this.$final = function () {',
  18209. ' };',
  18210. ' rtl.addIntf(this, $mod.IUnknown);',
  18211. '});',
  18212. 'this.DoIt = function (i) {',
  18213. '};',
  18214. 'this.DoSome = function () {',
  18215. ' var v = null;',
  18216. ' try {',
  18217. ' $mod.DoIt({',
  18218. ' get: function () {',
  18219. ' return v;',
  18220. ' },',
  18221. ' set: function (w) {',
  18222. ' v = w;',
  18223. ' }',
  18224. ' });',
  18225. ' } finally {',
  18226. ' rtl._Release(v);',
  18227. ' };',
  18228. '};',
  18229. 'this.GetIt = function () {',
  18230. ' var Result = null;',
  18231. ' var $ok = false;',
  18232. ' try {',
  18233. ' $mod.DoIt({',
  18234. ' get: function () {',
  18235. ' return Result;',
  18236. ' },',
  18237. ' set: function (v) {',
  18238. ' Result = v;',
  18239. ' }',
  18240. ' });',
  18241. ' $ok = true;',
  18242. ' } finally {',
  18243. ' if (!$ok) rtl._Release(Result);',
  18244. ' };',
  18245. ' return Result;',
  18246. '};',
  18247. 'this.i = null;',
  18248. '']),
  18249. LinesToStr([ // $mod.$main
  18250. 'try {',
  18251. ' $mod.DoIt({',
  18252. ' p: $mod,',
  18253. ' get: function () {',
  18254. ' return this.p.i;',
  18255. ' },',
  18256. ' set: function (v) {',
  18257. ' this.p.i = v;',
  18258. ' }',
  18259. ' });',
  18260. '} finally {',
  18261. ' rtl._Release($mod.i);',
  18262. '};',
  18263. '']));
  18264. end;
  18265. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  18266. begin
  18267. StartProgram(false);
  18268. Add([
  18269. '{$interfaces com}',
  18270. 'type',
  18271. ' IUnknown = interface',
  18272. ' function _AddRef: longint;',
  18273. ' function _Release: longint;',
  18274. ' end;',
  18275. ' TObject = class(IUnknown)',
  18276. ' function _AddRef: longint; virtual; abstract;',
  18277. ' function _Release: longint; virtual; abstract;',
  18278. ' end;',
  18279. 'function GetIt: IUnknown;',
  18280. 'begin',
  18281. 'end;',
  18282. 'procedure DoSome;',
  18283. 'var v: IUnknown;',
  18284. ' i: longint;',
  18285. 'begin',
  18286. ' v:=GetIt;',
  18287. ' v:=GetIt();',
  18288. ' GetIt()._AddRef;',
  18289. ' i:=GetIt()._AddRef;',
  18290. 'end;',
  18291. 'var v: IUnknown;',
  18292. ' i: longint;',
  18293. 'begin',
  18294. ' v:=GetIt;',
  18295. ' v:=GetIt();',
  18296. ' GetIt()._AddRef;',
  18297. ' i:=GetIt()._AddRef;',
  18298. '']);
  18299. ConvertProgram;
  18300. CheckSource('TestClassInterface_COM_FunctionInExpr',
  18301. LinesToStr([ // statements
  18302. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18303. 'rtl.createClass($mod, "TObject", null, function () {',
  18304. ' this.$init = function () {',
  18305. ' };',
  18306. ' this.$final = function () {',
  18307. ' };',
  18308. ' rtl.addIntf(this, $mod.IUnknown);',
  18309. '});',
  18310. 'this.GetIt = function () {',
  18311. ' var Result = null;',
  18312. ' return Result;',
  18313. '};',
  18314. 'this.DoSome = function () {',
  18315. ' var v = null;',
  18316. ' var i = 0;',
  18317. ' var $ir = rtl.createIntfRefs();',
  18318. ' try {',
  18319. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  18320. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  18321. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18322. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18323. ' } finally {',
  18324. ' $ir.free();',
  18325. ' rtl._Release(v);',
  18326. ' };',
  18327. '};',
  18328. 'this.v = null;',
  18329. 'this.i = 0;',
  18330. '']),
  18331. LinesToStr([ // $mod.$main
  18332. 'var $ir = rtl.createIntfRefs();',
  18333. 'try {',
  18334. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18335. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18336. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18337. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18338. '} finally {',
  18339. ' $ir.free();',
  18340. '};',
  18341. '']));
  18342. end;
  18343. procedure TTestModule.TestClassInterface_COM_Property;
  18344. begin
  18345. StartProgram(false);
  18346. Add([
  18347. '{$interfaces com}',
  18348. 'type',
  18349. ' IUnknown = interface',
  18350. ' function _AddRef: longint;',
  18351. ' function _Release: longint;',
  18352. ' end;',
  18353. ' TObject = class(IUnknown)',
  18354. ' FAnt: IUnknown;',
  18355. ' function _AddRef: longint; virtual; abstract;',
  18356. ' function _Release: longint; virtual; abstract;',
  18357. ' function GetBird: IUnknown; virtual; abstract;',
  18358. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18359. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18360. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18361. ' property Ant: IUnknown read FAnt write FAnt;',
  18362. ' property Bird: IUnknown read GetBird write SetBird;',
  18363. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18364. ' end;',
  18365. 'procedure DoIt;',
  18366. 'var',
  18367. ' o: TObject;',
  18368. ' v: IUnknown;',
  18369. 'begin',
  18370. ' v:=o.Ant;',
  18371. ' o.Ant:=v;',
  18372. ' o.Ant:=o.Ant;',
  18373. ' v:=o.Bird;',
  18374. ' o.Bird:=v;',
  18375. ' o.Bird:=o.Bird;',
  18376. ' v:=o.Items[1];',
  18377. ' o.Items[2]:=v;',
  18378. ' o.Items[3]:=o.Items[4];',
  18379. ' v:=o[5];',
  18380. ' o[6]:=v;',
  18381. ' o[7]:=o[8];',
  18382. 'end;',
  18383. 'begin',
  18384. '']);
  18385. ConvertProgram;
  18386. CheckSource('TestClassInterface_COM_Property',
  18387. LinesToStr([ // statements
  18388. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18389. 'rtl.createClass($mod, "TObject", null, function () {',
  18390. ' this.$init = function () {',
  18391. ' this.FAnt = null;',
  18392. ' };',
  18393. ' this.$final = function () {',
  18394. ' this.FAnt = undefined;',
  18395. ' };',
  18396. ' rtl.addIntf(this, $mod.IUnknown);',
  18397. '});',
  18398. 'this.DoIt = function () {',
  18399. ' var o = null;',
  18400. ' var v = null;',
  18401. ' var $ir = rtl.createIntfRefs();',
  18402. ' try {',
  18403. ' v = rtl.setIntfL(v, o.FAnt);',
  18404. ' rtl.setIntfP(o, "FAnt", v);',
  18405. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  18406. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  18407. ' o.SetBird(v);',
  18408. ' o.SetBird($ir.ref(1, o.GetBird()));',
  18409. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  18410. ' o.SetItems(2, v);',
  18411. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  18412. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  18413. ' o.SetItems(6, v);',
  18414. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  18415. ' } finally {',
  18416. ' $ir.free();',
  18417. ' rtl._Release(v);',
  18418. ' };',
  18419. '};',
  18420. '']),
  18421. LinesToStr([ // $mod.$main
  18422. '']));
  18423. end;
  18424. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  18425. begin
  18426. StartProgram(false);
  18427. Add([
  18428. '{$interfaces com}',
  18429. 'type',
  18430. ' IUnknown = interface',
  18431. ' function _AddRef: longint;',
  18432. ' function _Release: longint;',
  18433. ' function GetBird: IUnknown;',
  18434. ' procedure SetBird(Value: IUnknown);',
  18435. ' function GetItems(Index: longint): IUnknown;',
  18436. ' procedure SetItems(Index: longint; Value: IUnknown);',
  18437. ' property Bird: IUnknown read GetBird write SetBird;',
  18438. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18439. ' end;',
  18440. ' TObject = class(IUnknown)',
  18441. ' function _AddRef: longint; virtual; abstract;',
  18442. ' function _Release: longint; virtual; abstract;',
  18443. ' function GetBird: IUnknown; virtual; abstract;',
  18444. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18445. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18446. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18447. ' end;',
  18448. 'procedure DoIt;',
  18449. 'var',
  18450. ' o: TObject;',
  18451. ' v: IUnknown;',
  18452. 'begin',
  18453. ' v:=v.Items[1];',
  18454. ' v.Items[2]:=v;',
  18455. ' v.Items[3]:=v.Items[4];',
  18456. ' v:=v[5];',
  18457. ' v[6]:=v;',
  18458. ' v[7]:=v[8];',
  18459. ' v[9].Bird.Bird:=v;',
  18460. ' v:=v.Bird[10].Bird',
  18461. 'end;',
  18462. 'begin',
  18463. '']);
  18464. ConvertProgram;
  18465. CheckSource('TestClassInterface_COM_IntfProperty',
  18466. LinesToStr([ // statements
  18467. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  18468. ' "_AddRef",',
  18469. ' "_Release",',
  18470. ' "GetBird",',
  18471. ' "SetBird",',
  18472. ' "GetItems",',
  18473. ' "SetItems"',
  18474. '], null);',
  18475. 'rtl.createClass($mod, "TObject", null, function () {',
  18476. ' this.$init = function () {',
  18477. ' };',
  18478. ' this.$final = function () {',
  18479. ' };',
  18480. ' rtl.addIntf(this, $mod.IUnknown);',
  18481. '});',
  18482. 'this.DoIt = function () {',
  18483. ' var o = null;',
  18484. ' var v = null;',
  18485. ' var $ir = rtl.createIntfRefs();',
  18486. ' try {',
  18487. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  18488. ' v.SetItems(2, v);',
  18489. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  18490. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  18491. ' v.SetItems(6, v);',
  18492. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  18493. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  18494. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  18495. ' } finally {',
  18496. ' $ir.free();',
  18497. ' rtl._Release(v);',
  18498. ' };',
  18499. '};',
  18500. '']),
  18501. LinesToStr([ // $mod.$main
  18502. '']));
  18503. end;
  18504. procedure TTestModule.TestClassInterface_COM_Delegation;
  18505. begin
  18506. StartProgram(false);
  18507. Add([
  18508. '{$interfaces com}',
  18509. 'type',
  18510. ' IUnknown = interface',
  18511. ' function _AddRef: longint;',
  18512. ' function _Release: longint;',
  18513. ' end;',
  18514. ' IBird = interface(IUnknown)',
  18515. ' procedure Fly(s: string);',
  18516. ' end;',
  18517. ' IEagle = interface(IBird) end;',
  18518. ' IDove = interface(IBird) end;',
  18519. ' ISwallow = interface(IBird) end;',
  18520. ' TObject = class',
  18521. ' end;',
  18522. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18523. ' function _AddRef: longint; virtual; abstract;',
  18524. ' function _Release: longint; virtual; abstract;',
  18525. ' procedure Fly(s: string); virtual; abstract;',
  18526. ' end;',
  18527. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18528. ' function _AddRef: longint; virtual; abstract;',
  18529. ' function _Release: longint; virtual; abstract;',
  18530. ' FBirdIntf: IBird;',
  18531. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18532. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18533. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18534. ' FDoveObj: TBird;',
  18535. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18536. ' function GetSwallowObj: TBird; virtual; abstract;',
  18537. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18538. ' end;',
  18539. 'begin',
  18540. '']);
  18541. ConvertProgram;
  18542. CheckSource('TestClassInterface_COM_Delegation',
  18543. LinesToStr([ // statements
  18544. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18545. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  18546. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  18547. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  18548. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  18549. 'rtl.createClass($mod, "TObject", null, function () {',
  18550. ' this.$init = function () {',
  18551. ' };',
  18552. ' this.$final = function () {',
  18553. ' };',
  18554. '});',
  18555. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18556. ' rtl.addIntf(this, $mod.IBird);',
  18557. ' rtl.addIntf(this, $mod.IEagle);',
  18558. ' rtl.addIntf(this, $mod.IDove);',
  18559. ' rtl.addIntf(this, $mod.ISwallow);',
  18560. '});',
  18561. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18562. ' this.$init = function () {',
  18563. ' $mod.TObject.$init.call(this);',
  18564. ' this.FBirdIntf = null;',
  18565. ' this.FDoveObj = null;',
  18566. ' };',
  18567. ' this.$final = function () {',
  18568. ' this.FBirdIntf = undefined;',
  18569. ' this.FDoveObj = undefined;',
  18570. ' $mod.TObject.$final.call(this);',
  18571. ' };',
  18572. ' this.$intfmaps = {',
  18573. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  18574. ' return rtl._AddRef(this.FBirdIntf);',
  18575. ' },',
  18576. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  18577. ' return this.GetEagleIntf();',
  18578. ' },',
  18579. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  18580. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  18581. ' },',
  18582. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  18583. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18584. ' }',
  18585. ' };',
  18586. '});',
  18587. '']),
  18588. LinesToStr([ // $mod.$main
  18589. '']));
  18590. end;
  18591. procedure TTestModule.TestClassInterface_COM_With;
  18592. begin
  18593. StartProgram(false);
  18594. Add([
  18595. '{$interfaces com}',
  18596. 'type',
  18597. ' IUnknown = interface',
  18598. ' function _AddRef: longint;',
  18599. ' function _Release: longint;',
  18600. ' function GetAnt: IUnknown;',
  18601. ' property Ant: IUnknown read GetAnt;',
  18602. ' end;',
  18603. ' TObject = class(IUnknown)',
  18604. ' function _AddRef: longint; virtual; abstract;',
  18605. ' function _Release: longint; virtual; abstract;',
  18606. ' function GetAnt: IUnknown; virtual; abstract;',
  18607. ' property Ant: IUnknown read GetAnt;',
  18608. ' end;',
  18609. 'procedure DoIt;',
  18610. 'var',
  18611. ' i: IUnknown;',
  18612. 'begin',
  18613. ' with i do ',
  18614. ' GetAnt;',
  18615. ' with i.Ant, Ant do ',
  18616. ' GetAnt;',
  18617. 'end;',
  18618. 'begin',
  18619. '']);
  18620. ConvertProgram;
  18621. CheckSource('TestClassInterface_COM_With',
  18622. LinesToStr([ // statements
  18623. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  18624. 'rtl.createClass($mod, "TObject", null, function () {',
  18625. ' this.$init = function () {',
  18626. ' };',
  18627. ' this.$final = function () {',
  18628. ' };',
  18629. ' rtl.addIntf(this, $mod.IUnknown);',
  18630. '});',
  18631. 'this.DoIt = function () {',
  18632. ' var i = null;',
  18633. ' var $ir = rtl.createIntfRefs();',
  18634. ' try {',
  18635. ' $ir.ref(1, i.GetAnt());',
  18636. ' var $with1 = $ir.ref(2, i.GetAnt());',
  18637. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  18638. ' $ir.ref(4, $with2.GetAnt());',
  18639. ' } finally {',
  18640. ' $ir.free();',
  18641. ' };',
  18642. '};',
  18643. '']),
  18644. LinesToStr([ // $mod.$main
  18645. '']));
  18646. end;
  18647. procedure TTestModule.TestClassInterface_COM_ForIn;
  18648. begin
  18649. StartProgram(false);
  18650. Add([
  18651. '{$interfaces com}',
  18652. 'type',
  18653. ' IUnknown = interface end;',
  18654. ' TObject = class',
  18655. ' Id: longint;',
  18656. ' end;',
  18657. ' IEnumerator = interface(IUnknown)',
  18658. ' function GetCurrent: TObject;',
  18659. ' function MoveNext: Boolean;',
  18660. ' property Current: TObject read GetCurrent;',
  18661. ' end;',
  18662. ' IEnumerable = interface(IUnknown)',
  18663. ' function GetEnumerator: IEnumerator;',
  18664. ' end;',
  18665. 'var',
  18666. ' o: TObject;',
  18667. ' i: IEnumerable;',
  18668. 'begin',
  18669. ' for o in i do o.Id:=3;',
  18670. '']);
  18671. ConvertProgram;
  18672. CheckSource('TestClassInterface_COM_ForIn',
  18673. LinesToStr([ // statements
  18674. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18675. 'rtl.createClass($mod, "TObject", null, function () {',
  18676. ' this.$init = function () {',
  18677. ' this.Id = 0;',
  18678. ' };',
  18679. ' this.$final = function () {',
  18680. ' };',
  18681. '});',
  18682. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18683. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18684. 'this.o = null;',
  18685. 'this.i = null;',
  18686. '']),
  18687. LinesToStr([ // $mod.$main
  18688. 'var $in1 = $mod.i.GetEnumerator();',
  18689. 'try {',
  18690. ' while ($in1.MoveNext()) {',
  18691. ' $mod.o = $in1.GetCurrent();',
  18692. ' $mod.o.Id = 3;',
  18693. ' }',
  18694. '} finally {',
  18695. ' rtl._Release($in1)',
  18696. '};',
  18697. '']));
  18698. end;
  18699. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  18700. begin
  18701. StartProgram(false);
  18702. Add([
  18703. '{$interfaces com}',
  18704. 'type',
  18705. ' IUnknown = interface',
  18706. ' function _AddRef: longint;',
  18707. ' function _Release: longint;',
  18708. ' end;',
  18709. ' TObject = class',
  18710. ' end;',
  18711. ' TArrOfIntf = array of IUnknown;',
  18712. 'begin',
  18713. '']);
  18714. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  18715. ConvertProgram;
  18716. end;
  18717. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  18718. begin
  18719. StartProgram(false);
  18720. Add([
  18721. '{$interfaces com}',
  18722. 'type',
  18723. ' IUnknown = interface',
  18724. ' function _AddRef: longint;',
  18725. ' function _Release: longint;',
  18726. ' end;',
  18727. ' TRec = record',
  18728. ' i: IUnknown;',
  18729. ' end;',
  18730. 'begin',
  18731. '']);
  18732. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  18733. ConvertProgram;
  18734. end;
  18735. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  18736. begin
  18737. StartUnit(false);
  18738. Add([
  18739. '{$interfaces com}',
  18740. 'interface',
  18741. 'implementation',
  18742. 'type',
  18743. ' IUnknown = interface',
  18744. ' function _AddRef: longint;',
  18745. ' end;',
  18746. ' TObject = class(IUnknown)',
  18747. ' function _AddRef: longint;',
  18748. ' end;',
  18749. 'function TObject._AddRef: longint; begin end;',
  18750. 'var i: IUnknown;',
  18751. ' o: TObject;',
  18752. 'initialization',
  18753. ' i:=nil;',
  18754. ' i:=i;',
  18755. ' i:=o;',
  18756. ' if (o as IUnknown)=nil then ;',
  18757. '']);
  18758. ConvertUnit;
  18759. CheckSource('TestClassInterface_COM_UnitInitialization',
  18760. LinesToStr([ // statements
  18761. 'var $impl = $mod.$impl;',
  18762. '']),
  18763. LinesToStr([ // this.$init
  18764. 'var $ir = rtl.createIntfRefs();',
  18765. 'try {',
  18766. ' rtl.setIntfP($impl, "i", null);',
  18767. ' rtl.setIntfP($impl, "i", $impl.i);',
  18768. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  18769. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  18770. '} finally {',
  18771. ' $ir.free();',
  18772. '};',
  18773. '']),
  18774. LinesToStr([ // implementation
  18775. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  18776. 'rtl.createClass($impl, "TObject", null, function () {',
  18777. ' this.$init = function () {',
  18778. ' };',
  18779. ' this.$final = function () {',
  18780. ' };',
  18781. ' this._AddRef = function () {',
  18782. ' var Result = 0;',
  18783. ' return Result;',
  18784. ' };',
  18785. ' rtl.addIntf(this, $impl.IUnknown);',
  18786. '});',
  18787. '$impl.i = null;',
  18788. '$impl.o = null;',
  18789. ''])
  18790. );
  18791. end;
  18792. procedure TTestModule.TestClassInterface_GUID;
  18793. begin
  18794. StartProgram(false);
  18795. Add([
  18796. '{$interfaces corba}',
  18797. 'type',
  18798. ' IUnknown = interface',
  18799. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18800. ' end;',
  18801. ' TObject = class end;',
  18802. ' TGUID = record D1, D2, D3, D4: word; end;',
  18803. ' TAliasGUID = TGUID;',
  18804. ' TGUIDString = type string;',
  18805. ' TAliasGUIDString = TGUIDString;',
  18806. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  18807. 'begin end;',
  18808. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  18809. 'begin end;',
  18810. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  18811. 'begin end;',
  18812. 'var',
  18813. ' i: IUnknown;',
  18814. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  18815. ' s: TAliasGUIDString;',
  18816. 'begin',
  18817. ' DoConstGUIDIt(IUnknown);',
  18818. ' DoDefGUID(IUnknown);',
  18819. ' DoStr(IUnknown);',
  18820. ' DoConstGUIDIt(i);',
  18821. ' DoDefGUID(i);',
  18822. ' DoStr(i);',
  18823. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18824. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  18825. ' DoStr(g);',
  18826. ' g:=i;',
  18827. ' g:=IUnknown;',
  18828. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18829. ' s:=i;',
  18830. ' s:=IUnknown;',
  18831. ' s:=g;',
  18832. ' if g=i then ;',
  18833. ' if i=g then ;',
  18834. ' if g=IUnknown then ;',
  18835. ' if IUnknown=g then ;',
  18836. ' if s=i then ;',
  18837. ' if i=s then ;',
  18838. ' if s=IUnknown then ;',
  18839. ' if IUnknown=s then ;',
  18840. ' if s=g then ;',
  18841. ' if g=s then ;',
  18842. '']);
  18843. ConvertProgram;
  18844. CheckSource('TestClassInterface_GUID',
  18845. LinesToStr([ // statements
  18846. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  18847. 'rtl.createClass($mod, "TObject", null, function () {',
  18848. ' this.$init = function () {',
  18849. ' };',
  18850. ' this.$final = function () {',
  18851. ' };',
  18852. '});',
  18853. 'rtl.recNewT($mod, "TGUID", function () {',
  18854. ' this.D1 = 0;',
  18855. ' this.D2 = 0;',
  18856. ' this.D3 = 0;',
  18857. ' this.D4 = 0;',
  18858. ' this.$eq = function (b) {',
  18859. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  18860. ' };',
  18861. ' this.$assign = function (s) {',
  18862. ' this.D1 = s.D1;',
  18863. ' this.D2 = s.D2;',
  18864. ' this.D3 = s.D3;',
  18865. ' this.D4 = s.D4;',
  18866. ' return this;',
  18867. ' };',
  18868. '});',
  18869. 'this.DoConstGUIDIt = function (g) {',
  18870. '};',
  18871. 'this.DoDefGUID = function (g) {',
  18872. '};',
  18873. 'this.DoStr = function (s) {',
  18874. '};',
  18875. 'this.i = null;',
  18876. 'this.g = $mod.TGUID.$clone({',
  18877. ' D1: 0xD91C9AF4,',
  18878. ' D2: 0x3C93,',
  18879. ' D3: 0x420F,',
  18880. ' D4: [',
  18881. ' 0xA3,',
  18882. ' 0x03,',
  18883. ' 0xBF,',
  18884. ' 0x5B,',
  18885. ' 0xA8,',
  18886. ' 0x2B,',
  18887. ' 0xFD,',
  18888. ' 0x23',
  18889. ' ]',
  18890. '});',
  18891. 'this.s = "";',
  18892. '']),
  18893. LinesToStr([ // $mod.$main
  18894. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  18895. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  18896. '$mod.DoStr($mod.IUnknown.$guid);',
  18897. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  18898. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  18899. '$mod.DoStr($mod.i.$guid);',
  18900. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  18901. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  18902. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  18903. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  18904. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  18905. '$mod.g.$assign({',
  18906. ' D1: 0xD91C9AF4,',
  18907. ' D2: 0x3C93,',
  18908. ' D3: 0x420F,',
  18909. ' D4: [',
  18910. ' 0xA3,',
  18911. ' 0x03,',
  18912. ' 0xBF,',
  18913. ' 0x5B,',
  18914. ' 0xA8,',
  18915. ' 0x2B,',
  18916. ' 0xFD,',
  18917. ' 0x23',
  18918. ' ]',
  18919. '});',
  18920. '$mod.s = $mod.i.$guid;',
  18921. '$mod.s = $mod.IUnknown.$guid;',
  18922. '$mod.s = rtl.guidrToStr($mod.g);',
  18923. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  18924. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  18925. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  18926. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  18927. 'if ($mod.s === $mod.i.$guid) ;',
  18928. 'if ($mod.i.$guid === $mod.s) ;',
  18929. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  18930. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  18931. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  18932. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  18933. '']));
  18934. end;
  18935. procedure TTestModule.TestClassInterface_GUIDProperty;
  18936. begin
  18937. StartProgram(false);
  18938. Add([
  18939. '{$interfaces corba}',
  18940. 'type',
  18941. ' IUnknown = interface',
  18942. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  18943. ' end;',
  18944. ' TGUID = record D1, D2, D3, D4: word; end;',
  18945. ' TAliasGUID = TGUID;',
  18946. ' TGUIDString = type string;',
  18947. ' TAliasGUIDString = TGUIDString;',
  18948. ' TObject = class',
  18949. ' function GetG: TAliasGUID; virtual; abstract;',
  18950. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  18951. ' function GetS: TAliasGUIDString; virtual; abstract;',
  18952. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  18953. ' property g: TAliasGUID read GetG write SetG;',
  18954. ' property s: TAliasGUIDString read GetS write SetS;',
  18955. ' end;',
  18956. 'var o: TObject;',
  18957. 'begin',
  18958. ' o.g:=IUnknown;',
  18959. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  18960. ' o.s:=IUnknown;',
  18961. ' o.s:=o.g;',
  18962. '']);
  18963. ConvertProgram;
  18964. CheckSource('TestClassInterface_GUIDProperty',
  18965. LinesToStr([ // statements
  18966. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  18967. 'rtl.recNewT($mod, "TGUID", function () {',
  18968. ' this.D1 = 0;',
  18969. ' this.D2 = 0;',
  18970. ' this.D3 = 0;',
  18971. ' this.D4 = 0;',
  18972. ' this.$eq = function (b) {',
  18973. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  18974. ' };',
  18975. ' this.$assign = function (s) {',
  18976. ' this.D1 = s.D1;',
  18977. ' this.D2 = s.D2;',
  18978. ' this.D3 = s.D3;',
  18979. ' this.D4 = s.D4;',
  18980. ' return this;',
  18981. ' };',
  18982. '});',
  18983. 'rtl.createClass($mod, "TObject", null, function () {',
  18984. ' this.$init = function () {',
  18985. ' };',
  18986. ' this.$final = function () {',
  18987. ' };',
  18988. '});',
  18989. 'this.o = null;',
  18990. '']),
  18991. LinesToStr([ // $mod.$main
  18992. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  18993. '$mod.o.SetG({',
  18994. ' D1: 0xD91C9AF4,',
  18995. ' D2: 0x3C93,',
  18996. ' D3: 0x420F,',
  18997. ' D4: [',
  18998. ' 0xA3,',
  18999. ' 0x03,',
  19000. ' 0xBF,',
  19001. ' 0x5B,',
  19002. ' 0xA8,',
  19003. ' 0x2B,',
  19004. ' 0xFD,',
  19005. ' 0x23',
  19006. ' ]',
  19007. '});',
  19008. '$mod.o.SetS($mod.IUnknown.$guid);',
  19009. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  19010. '']));
  19011. end;
  19012. procedure TTestModule.TestClassHelper_ClassVar;
  19013. begin
  19014. StartProgram(false);
  19015. Add([
  19016. 'type',
  19017. ' TObject = class',
  19018. ' end;',
  19019. ' THelper = class helper for TObject',
  19020. ' const',
  19021. ' One = 1;',
  19022. ' Two: word = 2;',
  19023. ' class var',
  19024. ' Glob: word;',
  19025. ' function Foo(w: word): word;',
  19026. ' class function Bar(w: word): word;',
  19027. ' end;',
  19028. 'function THelper.foo(w: word): word;',
  19029. 'begin',
  19030. ' Result:=w;',
  19031. ' Two:=One+w;',
  19032. ' Glob:=Glob;',
  19033. ' Result:=Self.Glob;',
  19034. ' Self.Glob:=Self.Glob;',
  19035. ' with Self do Glob:=Glob;',
  19036. 'end;',
  19037. 'class function THelper.bar(w: word): word;',
  19038. 'begin',
  19039. ' Result:=w;',
  19040. ' Two:=One;',
  19041. ' Glob:=Glob;',
  19042. ' Self.Glob:=Self.Glob;',
  19043. ' with Self do Glob:=Glob;',
  19044. 'end;',
  19045. 'var o: TObject;',
  19046. 'begin',
  19047. ' tobject.two:=tobject.one;',
  19048. ' tobject.Glob:=tobject.Glob;',
  19049. ' with tobject do begin',
  19050. ' two:=one;',
  19051. ' Glob:=Glob;',
  19052. ' end;',
  19053. ' o.two:=o.one;',
  19054. ' o.Glob:=o.Glob;',
  19055. ' with o do begin',
  19056. ' two:=one;',
  19057. ' Glob:=Glob;',
  19058. ' end;',
  19059. '']);
  19060. ConvertProgram;
  19061. CheckSource('TestClassHelper_ClassVar',
  19062. LinesToStr([ // statements
  19063. 'rtl.createClass($mod, "TObject", null, function () {',
  19064. ' this.$init = function () {',
  19065. ' };',
  19066. ' this.$final = function () {',
  19067. ' };',
  19068. '});',
  19069. 'rtl.createHelper($mod, "THelper", null, function () {',
  19070. ' this.One = 1;',
  19071. ' this.Two = 2;',
  19072. ' this.Glob = 0;',
  19073. ' this.Foo = function (w) {',
  19074. ' var Result = 0;',
  19075. ' Result = w;',
  19076. ' $mod.THelper.Two = 1 + w;',
  19077. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19078. ' Result = $mod.THelper.Glob;',
  19079. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19080. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19081. ' return Result;',
  19082. ' };',
  19083. ' this.Bar = function (w) {',
  19084. ' var Result = 0;',
  19085. ' Result = w;',
  19086. ' $mod.THelper.Two = 1;',
  19087. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19088. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19089. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19090. ' return Result;',
  19091. ' };',
  19092. '});',
  19093. 'this.o = null;',
  19094. '']),
  19095. LinesToStr([ // $mod.$main
  19096. '$mod.THelper.Two = 1;',
  19097. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19098. 'var $with1 = $mod.TObject;',
  19099. '$mod.THelper.Two = 1;',
  19100. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19101. '$mod.THelper.Two = 1;',
  19102. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19103. 'var $with2 = $mod.o;',
  19104. '$mod.THelper.Two = 1;',
  19105. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19106. '']));
  19107. end;
  19108. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  19109. begin
  19110. StartProgram(false);
  19111. Add([
  19112. 'type',
  19113. ' TObject = class',
  19114. ' FSize: word;',
  19115. ' property Size: word read FSize write FSize;',
  19116. ' end;',
  19117. ' THelper = class helper for TObject',
  19118. ' function Foo(w: word = 1): word;',
  19119. ' end;',
  19120. 'function THelper.foo(w: word): word;',
  19121. 'begin',
  19122. ' Result:=Size;',
  19123. ' Size:=Size+2;',
  19124. ' Self.Size:=Self.Size+3;',
  19125. ' FSize:=FSize+4;',
  19126. ' Self.FSize:=Self.FSize+5;',
  19127. ' with Self do begin',
  19128. ' Size:=Size+6;',
  19129. ' FSize:=FSize+7;',
  19130. ' FSize:=FSize+8;',
  19131. ' end;',
  19132. 'end;',
  19133. 'begin',
  19134. '']);
  19135. ConvertProgram;
  19136. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  19137. LinesToStr([ // statements
  19138. 'rtl.createClass($mod, "TObject", null, function () {',
  19139. ' this.$init = function () {',
  19140. ' this.FSize = 0;',
  19141. ' };',
  19142. ' this.$final = function () {',
  19143. ' };',
  19144. '});',
  19145. 'rtl.createHelper($mod, "THelper", null, function () {',
  19146. ' this.Foo = function (w) {',
  19147. ' var Result = 0;',
  19148. ' Result = this.FSize;',
  19149. ' this.FSize = this.FSize + 2;',
  19150. ' this.FSize = this.FSize + 3;',
  19151. ' this.FSize = this.FSize + 4;',
  19152. ' this.FSize = this.FSize + 5;',
  19153. ' this.FSize = this.FSize + 6;',
  19154. ' this.FSize = this.FSize + 7;',
  19155. ' this.FSize = this.FSize + 8;',
  19156. ' return Result;',
  19157. ' };',
  19158. '});',
  19159. '']),
  19160. LinesToStr([ // $mod.$main
  19161. '']));
  19162. end;
  19163. procedure TTestModule.TestClassHelper_Method_Call;
  19164. begin
  19165. StartProgram(false);
  19166. Add([
  19167. 'type',
  19168. ' TObject = class',
  19169. ' procedure Run(w: word = 10);',
  19170. ' end;',
  19171. ' THelper = class helper for TObject',
  19172. ' function Foo(w: word = 1): word;',
  19173. ' end;',
  19174. 'procedure TObject.Run(w: word);',
  19175. 'var o: TObject;',
  19176. 'begin',
  19177. ' Foo;',
  19178. ' Foo();',
  19179. ' Foo(2);',
  19180. ' Self.Foo;',
  19181. ' Self.Foo();',
  19182. ' Self.Foo(3);',
  19183. ' with Self do begin',
  19184. ' Foo;',
  19185. ' Foo();',
  19186. ' Foo(4);',
  19187. ' end;',
  19188. ' with o do Foo(5);',
  19189. 'end;',
  19190. 'function THelper.foo(w: word): word;',
  19191. 'begin',
  19192. ' Run;',
  19193. ' Run();',
  19194. ' Run(11);',
  19195. ' Foo;',
  19196. ' Foo();',
  19197. ' Foo(12);',
  19198. ' Self.Foo;',
  19199. ' Self.Foo();',
  19200. ' Self.Foo(13);',
  19201. ' with Self do begin',
  19202. ' Foo;',
  19203. ' Foo();',
  19204. ' Foo(14);',
  19205. ' end;',
  19206. 'end;',
  19207. 'var Obj: TObject;',
  19208. 'begin',
  19209. ' obj.Foo;',
  19210. ' obj.Foo();',
  19211. ' obj.Foo(21);',
  19212. ' with obj do begin',
  19213. ' Foo;',
  19214. ' Foo();',
  19215. ' Foo(22);',
  19216. ' end;',
  19217. '']);
  19218. ConvertProgram;
  19219. CheckSource('TestClassHelper_Method_Call',
  19220. LinesToStr([ // statements
  19221. 'rtl.createClass($mod, "TObject", null, function () {',
  19222. ' this.$init = function () {',
  19223. ' };',
  19224. ' this.$final = function () {',
  19225. ' };',
  19226. ' this.Run = function (w) {',
  19227. ' var o = null;',
  19228. ' $mod.THelper.Foo.call(this, 1);',
  19229. ' $mod.THelper.Foo.call(this, 1);',
  19230. ' $mod.THelper.Foo.call(this, 2);',
  19231. ' $mod.THelper.Foo.call(this, 1);',
  19232. ' $mod.THelper.Foo.call(this, 1);',
  19233. ' $mod.THelper.Foo.call(this, 3);',
  19234. ' $mod.THelper.Foo.call(this, 1);',
  19235. ' $mod.THelper.Foo.call(this, 1);',
  19236. ' $mod.THelper.Foo.call(this, 4);',
  19237. ' $mod.THelper.Foo.call(o, 5);',
  19238. ' };',
  19239. '});',
  19240. 'rtl.createHelper($mod, "THelper", null, function () {',
  19241. ' this.Foo = function (w) {',
  19242. ' var Result = 0;',
  19243. ' this.Run(10);',
  19244. ' this.Run(10);',
  19245. ' this.Run(11);',
  19246. ' $mod.THelper.Foo.call(this, 1);',
  19247. ' $mod.THelper.Foo.call(this, 1);',
  19248. ' $mod.THelper.Foo.call(this, 12);',
  19249. ' $mod.THelper.Foo.call(this, 1);',
  19250. ' $mod.THelper.Foo.call(this, 1);',
  19251. ' $mod.THelper.Foo.call(this, 13);',
  19252. ' $mod.THelper.Foo.call(this, 1);',
  19253. ' $mod.THelper.Foo.call(this, 1);',
  19254. ' $mod.THelper.Foo.call(this, 14);',
  19255. ' return Result;',
  19256. ' };',
  19257. '});',
  19258. 'this.Obj = null;',
  19259. '']),
  19260. LinesToStr([ // $mod.$main
  19261. '$mod.THelper.Foo.call($mod.Obj, 1);',
  19262. '$mod.THelper.Foo.call($mod.Obj, 1);',
  19263. '$mod.THelper.Foo.call($mod.Obj, 21);',
  19264. 'var $with1 = $mod.Obj;',
  19265. '$mod.THelper.Foo.call($with1, 1);',
  19266. '$mod.THelper.Foo.call($with1, 1);',
  19267. '$mod.THelper.Foo.call($with1, 22);',
  19268. '']));
  19269. end;
  19270. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  19271. begin
  19272. StartProgram(false);
  19273. Add([
  19274. 'type',
  19275. ' TObject = class',
  19276. ' procedure Run(w: word = 10);',
  19277. ' end;',
  19278. ' THelper = class helper for TObject',
  19279. ' function Foo(w: word = 1): word;',
  19280. ' end;',
  19281. 'procedure TObject.Run(w: word);',
  19282. ' procedure Sub(Self: TObject);',
  19283. ' begin',
  19284. ' Foo;',
  19285. ' Foo();',
  19286. ' Self.Foo;',
  19287. ' Self.Foo();',
  19288. ' with Self do begin',
  19289. ' Foo;',
  19290. ' Foo();',
  19291. ' end;',
  19292. ' end;',
  19293. 'begin',
  19294. 'end;',
  19295. 'function THelper.foo(w: word): word;',
  19296. ' procedure Sub(Self: TObject);',
  19297. ' begin',
  19298. ' Run;',
  19299. ' Run();',
  19300. ' Foo;',
  19301. ' Foo();',
  19302. ' Self.Foo;',
  19303. ' Self.Foo();',
  19304. ' with Self do begin',
  19305. ' Foo;',
  19306. ' Foo();',
  19307. ' end;',
  19308. ' end;',
  19309. 'begin',
  19310. 'end;',
  19311. 'begin',
  19312. '']);
  19313. ConvertProgram;
  19314. CheckSource('TestClassHelper_Method_Nested_Call',
  19315. LinesToStr([ // statements
  19316. 'rtl.createClass($mod, "TObject", null, function () {',
  19317. ' this.$init = function () {',
  19318. ' };',
  19319. ' this.$final = function () {',
  19320. ' };',
  19321. ' this.Run = function (w) {',
  19322. ' var $Self = this;',
  19323. ' function Sub(Self) {',
  19324. ' $mod.THelper.Foo.call($Self, 1);',
  19325. ' $mod.THelper.Foo.call($Self, 1);',
  19326. ' $mod.THelper.Foo.call(Self, 1);',
  19327. ' $mod.THelper.Foo.call(Self, 1);',
  19328. ' $mod.THelper.Foo.call(Self, 1);',
  19329. ' $mod.THelper.Foo.call(Self, 1);',
  19330. ' };',
  19331. ' };',
  19332. '});',
  19333. 'rtl.createHelper($mod, "THelper", null, function () {',
  19334. ' this.Foo = function (w) {',
  19335. ' var $Self = this;',
  19336. ' var Result = 0;',
  19337. ' function Sub(Self) {',
  19338. ' $Self.Run(10);',
  19339. ' $Self.Run(10);',
  19340. ' $mod.THelper.Foo.call($Self, 1);',
  19341. ' $mod.THelper.Foo.call($Self, 1);',
  19342. ' $mod.THelper.Foo.call(Self, 1);',
  19343. ' $mod.THelper.Foo.call(Self, 1);',
  19344. ' $mod.THelper.Foo.call(Self, 1);',
  19345. ' $mod.THelper.Foo.call(Self, 1);',
  19346. ' };',
  19347. ' return Result;',
  19348. ' };',
  19349. '});',
  19350. '']),
  19351. LinesToStr([ // $mod.$main
  19352. '']));
  19353. end;
  19354. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  19355. begin
  19356. StartProgram(false);
  19357. Add([
  19358. 'type',
  19359. ' TObject = class',
  19360. ' class procedure Run(w: word = 10);',
  19361. ' end;',
  19362. ' THelper = class helper for TObject',
  19363. ' class function Foo(w: word = 1): word;',
  19364. ' end;',
  19365. 'class procedure TObject.Run(w: word);',
  19366. 'begin',
  19367. ' Foo;',
  19368. ' Foo();',
  19369. ' Self.Foo;',
  19370. ' Self.Foo();',
  19371. ' with Self do begin',
  19372. ' Foo;',
  19373. ' Foo();',
  19374. ' end;',
  19375. 'end;',
  19376. 'class function THelper.foo(w: word): word;',
  19377. 'begin',
  19378. ' Run;',
  19379. ' Run();',
  19380. ' Foo;',
  19381. ' Foo();',
  19382. ' Self.Foo;',
  19383. ' Self.Foo();',
  19384. ' with Self do begin',
  19385. ' Foo;',
  19386. ' Foo();',
  19387. ' end;',
  19388. 'end;',
  19389. 'var',
  19390. ' Obj: TObject;',
  19391. 'begin',
  19392. ' obj.Foo;',
  19393. ' obj.Foo();',
  19394. ' with obj do begin',
  19395. ' Foo;',
  19396. ' Foo();',
  19397. ' end;',
  19398. ' tobject.Foo;',
  19399. ' tobject.Foo();',
  19400. ' with tobject do begin',
  19401. ' Foo;',
  19402. ' Foo();',
  19403. ' end;',
  19404. '']);
  19405. ConvertProgram;
  19406. CheckSource('TestClassHelper_ClassMethod_Call',
  19407. LinesToStr([ // statements
  19408. 'rtl.createClass($mod, "TObject", null, function () {',
  19409. ' this.$init = function () {',
  19410. ' };',
  19411. ' this.$final = function () {',
  19412. ' };',
  19413. ' this.Run = function (w) {',
  19414. ' $mod.THelper.Foo.call(this, 1);',
  19415. ' $mod.THelper.Foo.call(this, 1);',
  19416. ' $mod.THelper.Foo.call(this, 1);',
  19417. ' $mod.THelper.Foo.call(this, 1);',
  19418. ' $mod.THelper.Foo.call(this, 1);',
  19419. ' $mod.THelper.Foo.call(this, 1);',
  19420. ' };',
  19421. '});',
  19422. 'rtl.createHelper($mod, "THelper", null, function () {',
  19423. ' this.Foo = function (w) {',
  19424. ' var Result = 0;',
  19425. ' this.Run(10);',
  19426. ' this.Run(10);',
  19427. ' $mod.THelper.Foo.call(this, 1);',
  19428. ' $mod.THelper.Foo.call(this, 1);',
  19429. ' $mod.THelper.Foo.call(this, 1);',
  19430. ' $mod.THelper.Foo.call(this, 1);',
  19431. ' $mod.THelper.Foo.call(this, 1);',
  19432. ' $mod.THelper.Foo.call(this, 1);',
  19433. ' return Result;',
  19434. ' };',
  19435. '});',
  19436. 'this.Obj = null;',
  19437. '']),
  19438. LinesToStr([ // $mod.$main
  19439. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19440. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19441. 'var $with1 = $mod.Obj;',
  19442. '$mod.THelper.Foo.call($with1.$class, 1);',
  19443. '$mod.THelper.Foo.call($with1.$class, 1);',
  19444. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19445. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19446. 'var $with2 = $mod.TObject;',
  19447. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19448. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19449. '']));
  19450. end;
  19451. procedure TTestModule.TestClassHelper_ClassOf;
  19452. begin
  19453. StartProgram(false);
  19454. Add([
  19455. 'type',
  19456. ' TObject = class',
  19457. ' end;',
  19458. ' TClass = class of TObject;',
  19459. ' THelper = class helper for TObject',
  19460. ' class function Foo(w: word = 1): word;',
  19461. ' end;',
  19462. 'class function THelper.foo(w: word): word;',
  19463. 'begin',
  19464. 'end;',
  19465. 'var',
  19466. ' c: TClass;',
  19467. 'begin',
  19468. ' c.Foo;',
  19469. ' c.Foo();',
  19470. ' with c do begin',
  19471. ' Foo;',
  19472. ' Foo();',
  19473. ' end;',
  19474. '']);
  19475. ConvertProgram;
  19476. CheckSource('TestClassHelper_ClassOf',
  19477. LinesToStr([ // statements
  19478. 'rtl.createClass($mod, "TObject", null, function () {',
  19479. ' this.$init = function () {',
  19480. ' };',
  19481. ' this.$final = function () {',
  19482. ' };',
  19483. '});',
  19484. 'rtl.createHelper($mod, "THelper", null, function () {',
  19485. ' this.Foo = function (w) {',
  19486. ' var Result = 0;',
  19487. ' return Result;',
  19488. ' };',
  19489. '});',
  19490. 'this.c = null;',
  19491. '']),
  19492. LinesToStr([ // $mod.$main
  19493. '$mod.THelper.Foo.call($mod.c, 1);',
  19494. '$mod.THelper.Foo.call($mod.c, 1);',
  19495. 'var $with1 = $mod.c;',
  19496. '$mod.THelper.Foo.call($with1, 1);',
  19497. '$mod.THelper.Foo.call($with1, 1);',
  19498. '']));
  19499. end;
  19500. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  19501. begin
  19502. StartProgram(false);
  19503. Add([
  19504. '{$mode objfpc}',
  19505. 'type',
  19506. ' TObject = class',
  19507. ' procedure DoIt;',
  19508. ' end;',
  19509. ' THelper = class helper for TObject',
  19510. ' procedure Fly(w: word = 1);',
  19511. ' class procedure Glide(w: word = 1);',
  19512. ' class procedure Run(w: word = 1); static;',
  19513. ' end;',
  19514. ' TFly = procedure(w: word) of object;',
  19515. ' TGlide = TFly;',
  19516. ' TRun = procedure(w: word);',
  19517. 'var',
  19518. ' f: TFly;',
  19519. ' g: TGlide;',
  19520. ' r: TRun;',
  19521. 'procedure TObject.DoIt;',
  19522. 'begin',
  19523. ' f:=@fly;',
  19524. ' g:=@glide;',
  19525. ' r:=@run;',
  19526. ' f:[email protected];',
  19527. ' g:[email protected];',
  19528. ' r:[email protected];',
  19529. ' with self do begin',
  19530. ' f:=@fly;',
  19531. ' g:=@glide;',
  19532. ' r:=@run;',
  19533. ' end;',
  19534. 'end;',
  19535. 'procedure THelper.fly(w: word);',
  19536. 'begin',
  19537. ' f:=@fly;',
  19538. ' g:=@glide;',
  19539. ' r:=@run;',
  19540. 'end;',
  19541. 'class procedure THelper.glide(w: word);',
  19542. 'begin',
  19543. ' g:=@glide;',
  19544. ' r:=@run;',
  19545. 'end;',
  19546. 'class procedure THelper.run(w: word);',
  19547. 'begin',
  19548. ' g:=@glide;',
  19549. ' r:=@run;',
  19550. 'end;',
  19551. 'var',
  19552. ' Obj: TObject;',
  19553. 'begin',
  19554. ' f:[email protected];',
  19555. ' g:[email protected];',
  19556. ' r:[email protected];',
  19557. ' with obj do begin',
  19558. ' f:=@fly;',
  19559. ' g:=@glide;',
  19560. ' r:=@run;',
  19561. ' end;',
  19562. ' g:[email protected];',
  19563. ' r:[email protected];',
  19564. ' with tobject do begin',
  19565. ' g:=@glide;',
  19566. ' r:=@run;',
  19567. ' end;',
  19568. '']);
  19569. ConvertProgram;
  19570. CheckSource('TestClassHelper_MethodRefObjFPC',
  19571. LinesToStr([ // statements
  19572. 'rtl.createClass($mod, "TObject", null, function () {',
  19573. ' this.$init = function () {',
  19574. ' };',
  19575. ' this.$final = function () {',
  19576. ' };',
  19577. ' this.DoIt = function () {',
  19578. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19579. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19580. ' $mod.r = $mod.THelper.Run;',
  19581. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19582. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19583. ' $mod.r = $mod.THelper.Run;',
  19584. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19585. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19586. ' $mod.r = $mod.THelper.Run;',
  19587. ' };',
  19588. '});',
  19589. 'rtl.createHelper($mod, "THelper", null, function () {',
  19590. ' this.Fly = function (w) {',
  19591. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19592. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19593. ' $mod.r = $mod.THelper.Run;',
  19594. ' };',
  19595. ' this.Glide = function (w) {',
  19596. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  19597. ' $mod.r = $mod.THelper.Run;',
  19598. ' };',
  19599. ' this.Run = function (w) {',
  19600. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  19601. ' $mod.r = $mod.THelper.Run;',
  19602. ' };',
  19603. '});',
  19604. 'this.f = null;',
  19605. 'this.g = null;',
  19606. 'this.r = null;',
  19607. 'this.Obj = null;',
  19608. '']),
  19609. LinesToStr([ // $mod.$main
  19610. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  19611. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  19612. '$mod.r = $mod.THelper.Run;',
  19613. 'var $with1 = $mod.Obj;',
  19614. '$mod.f = rtl.createCallback($with1, $mod.THelper.Fly);',
  19615. '$mod.g = rtl.createCallback($with1.$class, $mod.THelper.Glide);',
  19616. '$mod.r = $mod.THelper.Run;',
  19617. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  19618. '$mod.r = $mod.THelper.Run;',
  19619. 'var $with2 = $mod.TObject;',
  19620. '$mod.g = rtl.createCallback($with2, $mod.THelper.Glide);',
  19621. '$mod.r = $mod.THelper.Run;',
  19622. '']));
  19623. end;
  19624. procedure TTestModule.TestClassHelper_Constructor;
  19625. begin
  19626. StartProgram(false);
  19627. Add([
  19628. 'type',
  19629. ' TObject = class',
  19630. ' constructor Create;',
  19631. ' end;',
  19632. ' TClass = class of TObject;',
  19633. ' THelper = class helper for TObject',
  19634. ' constructor NewHlp(w: word);',
  19635. ' end;',
  19636. 'var',
  19637. ' obj: TObject;',
  19638. ' c: TClass;',
  19639. 'constructor TObject.Create;',
  19640. 'begin',
  19641. ' NewHlp(2);', // normal call
  19642. ' tobject.NewHlp(3);', // new instance
  19643. ' c.newhlp(4);', // new instance
  19644. 'end;',
  19645. 'constructor THelper.NewHlp(w: word);',
  19646. 'begin',
  19647. ' create;', // normal call
  19648. ' tobject.create;', // new instance
  19649. ' NewHlp(2);', // normal call
  19650. ' tobject.NewHlp(3);', // new instance
  19651. ' c.newhlp(4);', // new instance
  19652. 'end;',
  19653. 'begin',
  19654. ' obj.newhlp(2);', // normal call
  19655. ' with Obj do newhlp(12);', // normal call
  19656. ' tobject.newhlp(3);', // new instance
  19657. ' with tobject do newhlp(13);', // new instance
  19658. ' c.newhlp(4);', // new instance
  19659. ' with c do newhlp(14);', // new instance
  19660. '']);
  19661. ConvertProgram;
  19662. CheckSource('TestClassHelper_Constructor',
  19663. LinesToStr([ // statements
  19664. 'rtl.createClass($mod, "TObject", null, function () {',
  19665. ' this.$init = function () {',
  19666. ' };',
  19667. ' this.$final = function () {',
  19668. ' };',
  19669. ' this.Create = function () {',
  19670. ' $mod.THelper.NewHlp.call(this, 2);',
  19671. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19672. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19673. ' return this;',
  19674. ' };',
  19675. '});',
  19676. 'rtl.createHelper($mod, "THelper", null, function () {',
  19677. ' this.NewHlp = function (w) {',
  19678. ' this.Create();',
  19679. ' $mod.TObject.$create("Create");',
  19680. ' $mod.THelper.NewHlp.call(this, 2);',
  19681. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19682. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19683. ' return this;',
  19684. ' };',
  19685. '});',
  19686. 'this.obj = null;',
  19687. 'this.c = null;',
  19688. '']),
  19689. LinesToStr([ // $mod.$main
  19690. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  19691. 'var $with1 = $mod.obj;',
  19692. '$mod.THelper.NewHlp.call($with1, 12);',
  19693. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19694. 'var $with2 = $mod.TObject;',
  19695. '$with2.$create($mod.THelper.NewHlp, [13]);',
  19696. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  19697. 'var $with3 = $mod.c;',
  19698. '$with3.$create($mod.THelper.NewHlp, [14]);',
  19699. '']));
  19700. end;
  19701. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  19702. begin
  19703. StartProgram(false);
  19704. Add([
  19705. 'type',
  19706. ' TObject = class',
  19707. ' procedure Fly;',
  19708. ' end;',
  19709. ' TObjHelper = class helper for TObject',
  19710. ' procedure Fly;',
  19711. ' end;',
  19712. ' TBird = class',
  19713. ' procedure Fly;',
  19714. ' end;',
  19715. ' TBirdHelper = class helper for TBird',
  19716. ' procedure Fly;',
  19717. ' procedure Walk(w: word);',
  19718. ' end;',
  19719. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  19720. ' procedure Fly;',
  19721. ' procedure Walk(w: word);',
  19722. ' end;',
  19723. 'procedure Tobject.fly;',
  19724. 'begin',
  19725. ' inherited;', // ignore
  19726. 'end;',
  19727. 'procedure Tobjhelper.fly;',
  19728. 'begin',
  19729. ' {@TObject_Fly}inherited;',
  19730. ' inherited {@TObject_Fly}Fly;',
  19731. 'end;',
  19732. 'procedure Tbird.fly;',
  19733. 'begin',
  19734. ' {@TObjHelper_Fly}inherited;',
  19735. ' inherited {@TObjHelper_Fly}Fly;',
  19736. 'end;',
  19737. 'procedure Tbirdhelper.fly;',
  19738. 'begin',
  19739. ' {@TBird_Fly}inherited;',
  19740. ' inherited {@TBird_Fly}Fly;',
  19741. 'end;',
  19742. 'procedure Tbirdhelper.walk(w: word);',
  19743. 'begin',
  19744. 'end;',
  19745. 'procedure teagleHelper.fly;',
  19746. 'begin',
  19747. ' {@TBird_Fly}inherited;',
  19748. ' inherited {@TBird_Fly}Fly;',
  19749. 'end;',
  19750. 'procedure teagleHelper.walk(w: word);',
  19751. 'begin',
  19752. ' {@TBirdHelper_Walk}inherited;',
  19753. ' inherited {@TBirdHelper_Walk}Walk(3);',
  19754. 'end;',
  19755. 'begin',
  19756. '']);
  19757. ConvertProgram;
  19758. CheckSource('TestClassHelper_InheritedObjFPC',
  19759. LinesToStr([ // statements
  19760. 'rtl.createClass($mod, "TObject", null, function () {',
  19761. ' this.$init = function () {',
  19762. ' };',
  19763. ' this.$final = function () {',
  19764. ' };',
  19765. ' this.Fly = function () {',
  19766. ' };',
  19767. '});',
  19768. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19769. ' this.Fly = function () {',
  19770. ' $mod.TObject.Fly.call(this);',
  19771. ' $mod.TObject.Fly.call(this);',
  19772. ' };',
  19773. '});',
  19774. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19775. ' this.Fly$1 = function () {',
  19776. ' $mod.TObjHelper.Fly.call(this);',
  19777. ' $mod.TObjHelper.Fly.call(this);',
  19778. ' };',
  19779. '});',
  19780. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  19781. ' this.Fly = function () {',
  19782. ' $mod.TBird.Fly$1.call(this);',
  19783. ' $mod.TBird.Fly$1.call(this);',
  19784. ' };',
  19785. ' this.Walk = function (w) {',
  19786. ' };',
  19787. '});',
  19788. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  19789. ' this.Fly$1 = function () {',
  19790. ' $mod.TBird.Fly$1.call(this);',
  19791. ' $mod.TBird.Fly$1.call(this);',
  19792. ' };',
  19793. ' this.Walk$1 = function (w) {',
  19794. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  19795. ' $mod.TBirdHelper.Walk.call(this, 3);',
  19796. ' };',
  19797. '});',
  19798. '']),
  19799. LinesToStr([ // $mod.$main
  19800. '']));
  19801. end;
  19802. procedure TTestModule.TestClassHelper_Property;
  19803. begin
  19804. StartProgram(false);
  19805. Add([
  19806. 'type',
  19807. ' TObject = class',
  19808. ' FSize: word;',
  19809. ' function GetSpeed: word;',
  19810. ' procedure SetSpeed(Value: word);',
  19811. ' end;',
  19812. ' TObjHelper = class helper for TObject',
  19813. ' function GetLeft: word;',
  19814. ' procedure SetLeft(Value: word);',
  19815. ' property Size: word read FSize write FSize;',
  19816. ' property Speed: word read GetSpeed write SetSpeed;',
  19817. ' property Left: word read GetLeft write SetLeft;',
  19818. ' end;',
  19819. ' TBird = class',
  19820. ' property NotRight: word read GetLeft write SetLeft;',
  19821. ' procedure DoIt;',
  19822. ' end;',
  19823. 'var',
  19824. ' b: TBird;',
  19825. 'function Tobject.GetSpeed: word;',
  19826. 'begin',
  19827. ' Size:=Size+11;',
  19828. ' Speed:=Speed+12;',
  19829. ' Result:=Left+13;',
  19830. ' Left:=13;',
  19831. ' Left:=Left+13;',
  19832. ' Self.Size:=Self.Size+21;',
  19833. ' Self.Speed:=Self.Speed+22;',
  19834. ' Self.Left:=Self.Left+23;',
  19835. ' with Self do begin',
  19836. ' Size:=Size+31;',
  19837. ' Speed:=Speed+32;',
  19838. ' Left:=Left+33;',
  19839. ' end;',
  19840. 'end;',
  19841. 'procedure Tobject.SetSpeed(Value: word);',
  19842. 'begin',
  19843. 'end;',
  19844. 'function TObjHelper.GetLeft: word;',
  19845. 'begin',
  19846. ' Size:=Size+11;',
  19847. ' Speed:=Speed+12;',
  19848. ' Left:=Left+13;',
  19849. ' Self.Size:=Self.Size+21;',
  19850. ' Self.Speed:=Self.Speed+22;',
  19851. ' Self.Left:=Self.Left+23;',
  19852. ' with Self do begin',
  19853. ' Size:=Size+31;',
  19854. ' Speed:=Speed+32;',
  19855. ' Left:=Left+33;',
  19856. ' end;',
  19857. 'end;',
  19858. 'procedure TObjHelper.SetLeft(Value: word);',
  19859. 'begin',
  19860. 'end;',
  19861. 'procedure TBird.DoIt;',
  19862. 'begin',
  19863. ' NotRight:=NotRight+11;',
  19864. ' Self.NotRight:=Self.NotRight+21;',
  19865. ' with Self do begin',
  19866. ' NotRight:=NotRight+31;',
  19867. ' end;',
  19868. 'end;',
  19869. 'begin',
  19870. ' b.Size:=b.Size+11;',
  19871. ' b.Speed:=b.Speed+12;',
  19872. ' b.Left:=b.Left+13;',
  19873. ' b.NotRight:=b.NotRight+14;',
  19874. ' with b do begin',
  19875. ' Size:=Size+31;',
  19876. ' Speed:=Speed+32;',
  19877. ' Left:=Left+33;',
  19878. ' NotRight:=NotRight+34;',
  19879. ' end;',
  19880. '']);
  19881. ConvertProgram;
  19882. CheckSource('TestClassHelper_Property',
  19883. LinesToStr([ // statements
  19884. 'rtl.createClass($mod, "TObject", null, function () {',
  19885. ' this.$init = function () {',
  19886. ' this.FSize = 0;',
  19887. ' };',
  19888. ' this.$final = function () {',
  19889. ' };',
  19890. ' this.GetSpeed = function () {',
  19891. ' var Result = 0;',
  19892. ' this.FSize = this.FSize + 11;',
  19893. ' this.SetSpeed(this.GetSpeed() + 12);',
  19894. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  19895. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  19896. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19897. ' this.FSize = this.FSize + 21;',
  19898. ' this.SetSpeed(this.GetSpeed() + 22);',
  19899. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19900. ' this.FSize = this.FSize + 31;',
  19901. ' this.SetSpeed(this.GetSpeed() + 32);',
  19902. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19903. ' return Result;',
  19904. ' };',
  19905. ' this.SetSpeed = function (Value) {',
  19906. ' };',
  19907. '});',
  19908. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19909. ' this.GetLeft = function () {',
  19910. ' var Result = 0;',
  19911. ' this.FSize = this.FSize + 11;',
  19912. ' this.SetSpeed(this.GetSpeed() + 12);',
  19913. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  19914. ' this.FSize = this.FSize + 21;',
  19915. ' this.SetSpeed(this.GetSpeed() + 22);',
  19916. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  19917. ' this.FSize = this.FSize + 31;',
  19918. ' this.SetSpeed(this.GetSpeed() + 32);',
  19919. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  19920. ' return Result;',
  19921. ' };',
  19922. ' this.SetLeft = function (Value) {',
  19923. ' };',
  19924. '});',
  19925. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19926. ' this.DoIt = function () {',
  19927. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  19928. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  19929. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  19930. ' };',
  19931. '});',
  19932. 'this.b = null;',
  19933. '']),
  19934. LinesToStr([ // $mod.$main
  19935. '$mod.b.FSize = $mod.b.FSize + 11;',
  19936. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  19937. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  19938. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  19939. 'var $with1 = $mod.b;',
  19940. '$with1.FSize = $with1.FSize + 31;',
  19941. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  19942. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  19943. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  19944. '']));
  19945. end;
  19946. procedure TTestModule.TestClassHelper_Property_Array;
  19947. begin
  19948. StartProgram(false);
  19949. Add([
  19950. 'type',
  19951. ' TObject = class',
  19952. ' function GetSpeed(Index: boolean): word;',
  19953. ' procedure SetSpeed(Index: boolean; Value: word);',
  19954. ' end;',
  19955. ' TObjHelper = class helper for TObject',
  19956. ' function GetSize(Index: boolean): word;',
  19957. ' procedure SetSize(Index: boolean; Value: word);',
  19958. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  19959. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  19960. ' end;',
  19961. ' TBird = class',
  19962. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  19963. ' procedure DoIt;',
  19964. ' end;',
  19965. 'var',
  19966. ' b: TBird;',
  19967. 'function Tobject.GetSpeed(Index: boolean): word;',
  19968. 'begin',
  19969. ' Result:=Size[false];',
  19970. ' Size[true]:=Size[false]+11;',
  19971. ' Speed[true]:=Speed[false]+12;',
  19972. ' Self.Size[true]:=Self.Size[false]+21;',
  19973. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19974. ' with Self do begin',
  19975. ' Size[true]:=Size[false]+31;',
  19976. ' Speed[true]:=Speed[false]+32;',
  19977. ' end;',
  19978. 'end;',
  19979. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  19980. 'begin',
  19981. 'end;',
  19982. 'function TObjHelper.GetSize(Index: boolean): word;',
  19983. 'begin',
  19984. ' Size[true]:=Size[false]+11;',
  19985. ' Speed[true]:=Speed[false]+12;',
  19986. ' Self.Size[true]:=Self.Size[false]+21;',
  19987. ' Self.Speed[true]:=Self.Speed[false]+22;',
  19988. ' with Self do begin',
  19989. ' Size[true]:=Size[false]+31;',
  19990. ' Speed[true]:=Speed[false]+32;',
  19991. ' end;',
  19992. 'end;',
  19993. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  19994. 'begin',
  19995. 'end;',
  19996. 'procedure TBird.DoIt;',
  19997. 'begin',
  19998. ' Items[true]:=Items[false]+11;',
  19999. ' Self.Items[true]:=Self.Items[false]+21;',
  20000. ' with Self do Items[true]:=Items[false]+31;',
  20001. 'end;',
  20002. 'begin',
  20003. ' b.Size[true]:=b.Size[false]+11;',
  20004. ' b.Speed[true]:=b.Speed[false]+12;',
  20005. ' b.Items[true]:=b.Items[false]+13;',
  20006. ' with b do begin',
  20007. ' Size[true]:=Size[false]+21;',
  20008. ' Speed[true]:=Speed[false]+22;',
  20009. ' Items[true]:=Items[false]+23;',
  20010. ' end;',
  20011. '']);
  20012. ConvertProgram;
  20013. CheckSource('TestClassHelper_Property_Array',
  20014. LinesToStr([ // statements
  20015. 'rtl.createClass($mod, "TObject", null, function () {',
  20016. ' this.$init = function () {',
  20017. ' };',
  20018. ' this.$final = function () {',
  20019. ' };',
  20020. ' this.GetSpeed = function (Index) {',
  20021. ' var Result = 0;',
  20022. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  20023. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20024. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20025. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20026. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20027. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20028. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20029. ' return Result;',
  20030. ' };',
  20031. ' this.SetSpeed = function (Index, Value) {',
  20032. ' };',
  20033. '});',
  20034. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20035. ' this.GetSize = function (Index) {',
  20036. ' var Result = 0;',
  20037. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20038. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20039. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20040. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20041. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20042. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20043. ' return Result;',
  20044. ' };',
  20045. ' this.SetSize = function (Index, Value) {',
  20046. ' };',
  20047. '});',
  20048. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20049. ' this.DoIt = function () {',
  20050. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20051. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20052. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20053. ' };',
  20054. '});',
  20055. 'this.b = null;',
  20056. '']),
  20057. LinesToStr([ // $mod.$main
  20058. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  20059. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  20060. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  20061. 'var $with1 = $mod.b;',
  20062. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  20063. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  20064. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  20065. '']));
  20066. end;
  20067. procedure TTestModule.TestClassHelper_Property_Array_Default;
  20068. begin
  20069. StartProgram(false);
  20070. Add([
  20071. 'type',
  20072. ' TObject = class',
  20073. ' function GetSpeed(Index: boolean): word;',
  20074. ' procedure SetSpeed(Index: boolean; Value: word);',
  20075. ' end;',
  20076. ' TObjHelper = class helper for TObject',
  20077. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  20078. ' end;',
  20079. ' TBird = class',
  20080. ' end;',
  20081. ' TBirdHelper = class helper for TBird',
  20082. ' function GetSize(Index: word): boolean;',
  20083. ' procedure SetSize(Index: word; Value: boolean);',
  20084. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  20085. ' end;',
  20086. 'function Tobject.GetSpeed(Index: boolean): word;',
  20087. 'begin',
  20088. ' Self[true]:=Self[false]+1;',
  20089. 'end;',
  20090. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20091. 'begin',
  20092. 'end;',
  20093. 'function TBirdHelper.GetSize(Index: word): boolean;',
  20094. 'begin',
  20095. ' Self[1]:=not Self[2];',
  20096. 'end;',
  20097. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  20098. 'begin',
  20099. 'end;',
  20100. 'var',
  20101. ' o: TObject;',
  20102. ' b: TBird;',
  20103. 'begin',
  20104. ' o[true]:=o[false]+1;',
  20105. ' b[3]:=not b[4];',
  20106. '']);
  20107. ConvertProgram;
  20108. CheckSource('TestClassHelper_Property_Array_Default',
  20109. LinesToStr([ // statements
  20110. 'rtl.createClass($mod, "TObject", null, function () {',
  20111. ' this.$init = function () {',
  20112. ' };',
  20113. ' this.$final = function () {',
  20114. ' };',
  20115. ' this.GetSpeed = function (Index) {',
  20116. ' var Result = 0;',
  20117. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  20118. ' return Result;',
  20119. ' };',
  20120. ' this.SetSpeed = function (Index, Value) {',
  20121. ' };',
  20122. '});',
  20123. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20124. '});',
  20125. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20126. '});',
  20127. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20128. ' this.GetSize = function (Index) {',
  20129. ' var Result = false;',
  20130. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  20131. ' return Result;',
  20132. ' };',
  20133. ' this.SetSize = function (Index, Value) {',
  20134. ' };',
  20135. '});',
  20136. 'this.o = null;',
  20137. 'this.b = null;',
  20138. '']),
  20139. LinesToStr([ // $mod.$main
  20140. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  20141. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  20142. '']));
  20143. end;
  20144. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  20145. begin
  20146. StartProgram(false);
  20147. Add([
  20148. 'type',
  20149. ' TObject = class',
  20150. ' end;',
  20151. ' TObjHelper = class helper for TObject',
  20152. ' function GetItems(Index: word): TObject;',
  20153. ' procedure SetItems(Index: word; Value: TObject);',
  20154. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  20155. ' end;',
  20156. 'function Tobjhelper.GetItems(Index: word): TObject;',
  20157. 'begin',
  20158. ' Self[1][2]:=Self[3][4];',
  20159. 'end;',
  20160. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  20161. 'begin',
  20162. 'end;',
  20163. 'var',
  20164. ' o: TObject;',
  20165. 'begin',
  20166. ' o[1][2]:=o[3][4];',
  20167. '']);
  20168. ConvertProgram;
  20169. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  20170. LinesToStr([ // statements
  20171. 'rtl.createClass($mod, "TObject", null, function () {',
  20172. ' this.$init = function () {',
  20173. ' };',
  20174. ' this.$final = function () {',
  20175. ' };',
  20176. '});',
  20177. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20178. ' this.GetItems = function (Index) {',
  20179. ' var Result = null;',
  20180. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  20181. ' return Result;',
  20182. ' };',
  20183. ' this.SetItems = function (Index, Value) {',
  20184. ' };',
  20185. '});',
  20186. 'this.o = null;',
  20187. '']),
  20188. LinesToStr([ // $mod.$main
  20189. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  20190. '']));
  20191. end;
  20192. procedure TTestModule.TestClassHelper_ClassProperty;
  20193. begin
  20194. StartProgram(false);
  20195. Add([
  20196. 'type',
  20197. ' TObject = class',
  20198. ' class var FSize: word;',
  20199. ' class function GetSpeed: word;',
  20200. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  20201. ' end;',
  20202. ' TObjHelper = class helper for TObject',
  20203. ' class function GetLeft: word;',
  20204. ' class procedure SetLeft(Value: word);',
  20205. ' class property Size: word read FSize write FSize;',
  20206. ' class property Speed: word read GetSpeed write SetSpeed;',
  20207. ' class property Left: word read GetLeft write SetLeft;',
  20208. ' end;',
  20209. ' TBird = class',
  20210. ' class property NotRight: word read GetLeft write SetLeft;',
  20211. ' class procedure DoIt;',
  20212. ' end;',
  20213. ' TBirdClass = class of TBird;',
  20214. 'class function Tobject.GetSpeed: word;',
  20215. 'begin',
  20216. ' Size:=Size+11;',
  20217. ' Speed:=Speed+12;',
  20218. ' Left:=Left+13;',
  20219. ' Self.Size:=Self.Size+21;',
  20220. ' Self.Speed:=Self.Speed+22;',
  20221. ' Self.Left:=Self.Left+23;',
  20222. ' with Self do begin',
  20223. ' Size:=Size+31;',
  20224. ' Speed:=Speed+32;',
  20225. ' Left:=Left+33;',
  20226. ' end;',
  20227. 'end;',
  20228. 'class function TObjHelper.GetLeft: word;',
  20229. 'begin',
  20230. ' Size:=Size+11;',
  20231. ' Speed:=Speed+12;',
  20232. ' Left:=Left+13;',
  20233. ' Self.Size:=Self.Size+21;',
  20234. ' Self.Speed:=Self.Speed+22;',
  20235. ' Self.Left:=Self.Left+23;',
  20236. ' with Self do begin',
  20237. ' Size:=Size+31;',
  20238. ' Speed:=Speed+32;',
  20239. ' Left:=Left+33;',
  20240. ' end;',
  20241. 'end;',
  20242. 'class procedure TObjHelper.SetLeft(Value: word);',
  20243. 'begin',
  20244. 'end;',
  20245. 'class procedure TBird.DoIt;',
  20246. 'begin',
  20247. ' NotRight:=NotRight+11;',
  20248. ' Self.NotRight:=Self.NotRight+21;',
  20249. ' with Self do NotRight:=NotRight+31;',
  20250. 'end;',
  20251. 'var',
  20252. ' b: TBird;',
  20253. ' c: TBirdClass;',
  20254. 'begin',
  20255. ' b.Size:=b.Size+11;',
  20256. ' b.Speed:=b.Speed+12;',
  20257. ' b.Left:=b.Left+13;',
  20258. ' b.NotRight:=b.NotRight+14;',
  20259. ' with b do begin',
  20260. ' Size:=Size+31;',
  20261. ' Speed:=Speed+32;',
  20262. ' Left:=Left+33;',
  20263. ' NotRight:=NotRight+34;',
  20264. ' end;',
  20265. ' c.Size:=c.Size+11;',
  20266. ' c.Speed:=c.Speed+12;',
  20267. ' c.Left:=c.Left+13;',
  20268. ' c.NotRight:=c.NotRight+14;',
  20269. ' with c do begin',
  20270. ' Size:=Size+31;',
  20271. ' Speed:=Speed+32;',
  20272. ' Left:=Left+33;',
  20273. ' NotRight:=NotRight+34;',
  20274. ' end;',
  20275. ' tbird.Size:=tbird.Size+11;',
  20276. ' tbird.Speed:=tbird.Speed+12;',
  20277. ' tbird.Left:=tbird.Left+13;',
  20278. ' tbird.NotRight:=tbird.NotRight+14;',
  20279. ' with tbird do begin',
  20280. ' Size:=Size+31;',
  20281. ' Speed:=Speed+32;',
  20282. ' Left:=Left+33;',
  20283. ' NotRight:=NotRight+34;',
  20284. ' end;',
  20285. '']);
  20286. ConvertProgram;
  20287. CheckSource('TestClassHelper_ClassProperty',
  20288. LinesToStr([ // statements
  20289. 'rtl.createClass($mod, "TObject", null, function () {',
  20290. ' this.FSize = 0;',
  20291. ' this.$init = function () {',
  20292. ' };',
  20293. ' this.$final = function () {',
  20294. ' };',
  20295. ' this.GetSpeed = function () {',
  20296. ' var Result = 0;',
  20297. ' $mod.TObject.FSize = this.FSize + 11;',
  20298. ' this.SetSpeed(this.GetSpeed() + 12);',
  20299. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20300. ' $mod.TObject.FSize = this.FSize + 21;',
  20301. ' this.SetSpeed(this.GetSpeed() + 22);',
  20302. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20303. ' $mod.TObject.FSize = this.FSize + 31;',
  20304. ' this.SetSpeed(this.GetSpeed() + 32);',
  20305. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20306. ' return Result;',
  20307. ' };',
  20308. '});',
  20309. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20310. ' this.GetLeft = function () {',
  20311. ' var Result = 0;',
  20312. ' $mod.TObject.FSize = this.FSize + 11;',
  20313. ' this.SetSpeed(this.GetSpeed() + 12);',
  20314. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20315. ' $mod.TObject.FSize = this.FSize + 21;',
  20316. ' this.SetSpeed(this.GetSpeed() + 22);',
  20317. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20318. ' $mod.TObject.FSize = this.FSize + 31;',
  20319. ' this.SetSpeed(this.GetSpeed() + 32);',
  20320. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20321. ' return Result;',
  20322. ' };',
  20323. ' this.SetLeft = function (Value) {',
  20324. ' };',
  20325. '});',
  20326. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20327. ' this.DoIt = function () {',
  20328. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20329. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20330. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20331. ' };',
  20332. '});',
  20333. 'this.b = null;',
  20334. 'this.c = null;',
  20335. '']),
  20336. LinesToStr([ // $mod.$main
  20337. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  20338. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  20339. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  20340. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  20341. 'var $with1 = $mod.b;',
  20342. '$mod.TObject.FSize = $with1.FSize + 31;',
  20343. '$with1.$class.SetSpeed($with1.$class.GetSpeed() + 32);',
  20344. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 33);',
  20345. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 34);',
  20346. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  20347. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20348. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  20349. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  20350. 'var $with2 = $mod.c;',
  20351. '$mod.TObject.FSize = $with2.FSize + 31;',
  20352. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20353. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 33);',
  20354. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 34);',
  20355. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  20356. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20357. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  20358. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  20359. 'var $with3 = $mod.TBird;',
  20360. '$mod.TObject.FSize = $with3.FSize + 31;',
  20361. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20362. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  20363. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  20364. '']));
  20365. end;
  20366. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  20367. begin
  20368. StartProgram(false);
  20369. Add([
  20370. 'type',
  20371. ' TObject = class',
  20372. ' class function GetSpeed: word; static;',
  20373. ' class procedure SetSpeed(Value: word); static;',
  20374. ' end;',
  20375. ' TObjHelper = class helper for TObject',
  20376. ' class function GetLeft: word; static;',
  20377. ' class procedure SetLeft(Value: word); static;',
  20378. ' class property Speed: word read GetSpeed write SetSpeed;',
  20379. ' class property Left: word read GetLeft write SetLeft;',
  20380. ' end;',
  20381. ' TBird = class',
  20382. ' class property NotRight: word read GetLeft write SetLeft;',
  20383. ' class procedure DoIt; static;',
  20384. ' class procedure DoSome;',
  20385. ' end;',
  20386. ' TBirdClass = class of TBird;',
  20387. 'class function Tobject.GetSpeed: word;',
  20388. 'begin',
  20389. ' Speed:=Speed+12;',
  20390. ' Left:=Left+13;',
  20391. 'end;',
  20392. 'class procedure TObject.SetSpeed(Value: word);',
  20393. 'begin',
  20394. 'end;',
  20395. 'class function TObjHelper.GetLeft: word;',
  20396. 'begin',
  20397. ' Speed:=Speed+12;',
  20398. ' Left:=Left+13;',
  20399. 'end;',
  20400. 'class procedure TObjHelper.SetLeft(Value: word);',
  20401. 'begin',
  20402. 'end;',
  20403. 'class procedure TBird.DoIt;',
  20404. 'begin',
  20405. ' NotRight:=NotRight+11;',
  20406. 'end;',
  20407. 'class procedure TBird.DoSome;',
  20408. 'begin',
  20409. ' Speed:=Speed+12;',
  20410. ' Left:=Left+13;',
  20411. ' Self.Speed:=Self.Speed+22;',
  20412. ' Self.Left:=Self.Left+23;',
  20413. ' with Self do begin',
  20414. ' Speed:=Speed+32;',
  20415. ' Left:=Left+33;',
  20416. ' end;',
  20417. ' NotRight:=NotRight+11;',
  20418. ' Self.NotRight:=Self.NotRight+21;',
  20419. ' with Self do NotRight:=NotRight+31;',
  20420. 'end;',
  20421. 'var',
  20422. ' b: TBird;',
  20423. ' c: TBirdClass;',
  20424. 'begin',
  20425. ' b.Speed:=b.Speed+12;',
  20426. ' b.Left:=b.Left+13;',
  20427. ' b.NotRight:=b.NotRight+14;',
  20428. ' with b do begin',
  20429. ' Speed:=Speed+32;',
  20430. ' Left:=Left+33;',
  20431. ' NotRight:=NotRight+34;',
  20432. ' end;',
  20433. ' c.Speed:=c.Speed+12;',
  20434. ' c.Left:=c.Left+13;',
  20435. ' c.NotRight:=c.NotRight+14;',
  20436. ' with c do begin',
  20437. ' Speed:=Speed+32;',
  20438. ' Left:=Left+33;',
  20439. ' NotRight:=NotRight+34;',
  20440. ' end;',
  20441. ' tbird.Speed:=tbird.Speed+12;',
  20442. ' tbird.Left:=tbird.Left+13;',
  20443. ' tbird.NotRight:=tbird.NotRight+14;',
  20444. ' with tbird do begin',
  20445. ' Speed:=Speed+32;',
  20446. ' Left:=Left+33;',
  20447. ' NotRight:=NotRight+34;',
  20448. ' end;',
  20449. '']);
  20450. ConvertProgram;
  20451. CheckSource('TestClassHelper_ClassPropertyStatic',
  20452. LinesToStr([ // statements
  20453. 'rtl.createClass($mod, "TObject", null, function () {',
  20454. ' this.$init = function () {',
  20455. ' };',
  20456. ' this.$final = function () {',
  20457. ' };',
  20458. ' this.GetSpeed = function () {',
  20459. ' var Result = 0;',
  20460. ' this.SetSpeed(this.GetSpeed() + 12);',
  20461. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20462. ' return Result;',
  20463. ' };',
  20464. ' this.SetSpeed = function (Value) {',
  20465. ' };',
  20466. '});',
  20467. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20468. ' this.GetLeft = function () {',
  20469. ' var Result = 0;',
  20470. ' this.SetSpeed(this.GetSpeed() + 12);',
  20471. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20472. ' return Result;',
  20473. ' };',
  20474. ' this.SetLeft = function (Value) {',
  20475. ' };',
  20476. '});',
  20477. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20478. ' this.DoIt = function () {',
  20479. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20480. ' };',
  20481. ' this.DoSome = function () {',
  20482. ' this.SetSpeed(this.GetSpeed() + 12);',
  20483. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20484. ' this.SetSpeed(this.GetSpeed() + 22);',
  20485. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  20486. ' this.SetSpeed(this.GetSpeed() + 32);',
  20487. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20488. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20489. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  20490. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  20491. ' };',
  20492. '});',
  20493. 'this.b = null;',
  20494. 'this.c = null;',
  20495. '']),
  20496. LinesToStr([ // $mod.$main
  20497. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20498. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20499. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20500. 'var $with1 = $mod.b;',
  20501. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20502. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20503. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20504. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20505. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20506. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20507. 'var $with2 = $mod.c;',
  20508. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20509. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20510. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20511. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20512. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20513. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20514. 'var $with3 = $mod.TBird;',
  20515. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20516. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20517. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20518. '']));
  20519. end;
  20520. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  20521. begin
  20522. StartProgram(false);
  20523. Add([
  20524. 'type',
  20525. ' TObject = class',
  20526. ' class function GetSpeed(Index: boolean): word;',
  20527. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  20528. ' end;',
  20529. ' TObjHelper = class helper for TObject',
  20530. ' class function GetSize(Index: boolean): word;',
  20531. ' class procedure SetSize(Index: boolean; Value: word);',
  20532. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  20533. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20534. ' end;',
  20535. ' TBird = class',
  20536. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  20537. ' class procedure DoIt;',
  20538. ' end;',
  20539. ' TBirdClass = class of TBird;',
  20540. 'class function Tobject.GetSpeed(Index: boolean): word;',
  20541. 'begin',
  20542. ' Size[true]:=Size[false]+11;',
  20543. ' Speed[true]:=Speed[false]+12;',
  20544. ' Self.Size[true]:=Self.Size[false]+21;',
  20545. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20546. ' with Self do begin',
  20547. ' Size[true]:=Size[false]+31;',
  20548. ' Speed[true]:=Speed[false]+32;',
  20549. ' end;',
  20550. 'end;',
  20551. 'class function TObjHelper.GetSize(Index: boolean): word;',
  20552. 'begin',
  20553. ' Size[true]:=Size[false]+11;',
  20554. ' Speed[true]:=Speed[false]+12;',
  20555. ' Self.Size[true]:=Self.Size[false]+21;',
  20556. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20557. ' with Self do begin',
  20558. ' Size[true]:=Size[false]+31;',
  20559. ' Speed[true]:=Speed[false]+32;',
  20560. ' end;',
  20561. 'end;',
  20562. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20563. 'begin',
  20564. 'end;',
  20565. 'class procedure TBird.DoIt;',
  20566. 'begin',
  20567. ' Items[true]:=Items[false]+11;',
  20568. ' Self.Items[true]:=Self.Items[false]+21;',
  20569. ' with Self do Items[true]:=Items[false]+31;',
  20570. 'end;',
  20571. 'var',
  20572. ' b: TBird;',
  20573. ' c: TBirdClass;',
  20574. 'begin',
  20575. ' b.Size[true]:=b.Size[false]+11;',
  20576. ' b.Speed[true]:=b.Speed[false]+12;',
  20577. ' b.Items[true]:=b.Items[false]+13;',
  20578. ' with b do begin',
  20579. ' Size[true]:=Size[false]+21;',
  20580. ' Speed[true]:=Speed[false]+22;',
  20581. ' Items[true]:=Items[false]+23;',
  20582. ' end;',
  20583. ' c.Size[true]:=c.Size[false]+11;',
  20584. ' c.Speed[true]:=c.Speed[false]+12;',
  20585. ' c.Items[true]:=c.Items[false]+13;',
  20586. ' with c do begin',
  20587. ' Size[true]:=Size[false]+21;',
  20588. ' Speed[true]:=Speed[false]+22;',
  20589. ' Items[true]:=Items[false]+23;',
  20590. ' end;',
  20591. ' TBird.Size[true]:=TBird.Size[false]+11;',
  20592. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  20593. ' TBird.Items[true]:=TBird.Items[false]+13;',
  20594. ' with TBird do begin',
  20595. ' Size[true]:=Size[false]+21;',
  20596. ' Speed[true]:=Speed[false]+22;',
  20597. ' Items[true]:=Items[false]+23;',
  20598. ' end;',
  20599. '']);
  20600. ConvertProgram;
  20601. CheckSource('TestClassHelper_ClassProperty_Array',
  20602. LinesToStr([ // statements
  20603. 'rtl.createClass($mod, "TObject", null, function () {',
  20604. ' this.$init = function () {',
  20605. ' };',
  20606. ' this.$final = function () {',
  20607. ' };',
  20608. ' this.GetSpeed = function (Index) {',
  20609. ' var Result = 0;',
  20610. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20611. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20612. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20613. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20614. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20615. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20616. ' return Result;',
  20617. ' };',
  20618. '});',
  20619. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20620. ' this.GetSize = function (Index) {',
  20621. ' var Result = 0;',
  20622. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20623. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20624. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20625. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20626. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20627. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20628. ' return Result;',
  20629. ' };',
  20630. ' this.SetSize = function (Index, Value) {',
  20631. ' };',
  20632. '});',
  20633. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20634. ' this.DoIt = function () {',
  20635. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20636. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20637. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20638. ' };',
  20639. '});',
  20640. 'this.b = null;',
  20641. 'this.c = null;',
  20642. '']),
  20643. LinesToStr([ // $mod.$main
  20644. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  20645. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  20646. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  20647. 'var $with1 = $mod.b;',
  20648. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 21);',
  20649. '$with1.$class.SetSpeed(true, $with1.$class.GetSpeed(false) + 22);',
  20650. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 23);',
  20651. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  20652. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  20653. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  20654. 'var $with2 = $mod.c;',
  20655. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 21);',
  20656. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  20657. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 23);',
  20658. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  20659. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  20660. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  20661. 'var $with3 = $mod.TBird;',
  20662. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  20663. '$with3.SetSpeed(true, $with3.GetSpeed(false) + 22);',
  20664. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  20665. '']));
  20666. end;
  20667. procedure TTestModule.TestClassHelper_ForIn;
  20668. begin
  20669. StartProgram(false);
  20670. Add([
  20671. 'type',
  20672. ' TObject = class end;',
  20673. ' TItem = TObject;',
  20674. ' TEnumerator = class',
  20675. ' FCurrent: TItem;',
  20676. ' property Current: TItem read FCurrent;',
  20677. ' function MoveNext: boolean;',
  20678. ' end;',
  20679. ' TBird = class',
  20680. ' end;',
  20681. ' TBirdHelper = class helper for TBird',
  20682. ' function GetEnumerator: TEnumerator;',
  20683. ' end;',
  20684. 'function TEnumerator.MoveNext: boolean;',
  20685. 'begin',
  20686. 'end;',
  20687. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  20688. 'begin',
  20689. 'end;',
  20690. 'var',
  20691. ' b: TBird;',
  20692. ' i, i2: TItem;',
  20693. 'begin',
  20694. ' for i in b do i2:=i;']);
  20695. ConvertProgram;
  20696. CheckSource('TestClassHelper_ForIn',
  20697. LinesToStr([ // statements
  20698. 'rtl.createClass($mod, "TObject", null, function () {',
  20699. ' this.$init = function () {',
  20700. ' };',
  20701. ' this.$final = function () {',
  20702. ' };',
  20703. '});',
  20704. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  20705. ' this.$init = function () {',
  20706. ' $mod.TObject.$init.call(this);',
  20707. ' this.FCurrent = null;',
  20708. ' };',
  20709. ' this.$final = function () {',
  20710. ' this.FCurrent = undefined;',
  20711. ' $mod.TObject.$final.call(this);',
  20712. ' };',
  20713. ' this.MoveNext = function () {',
  20714. ' var Result = false;',
  20715. ' return Result;',
  20716. ' };',
  20717. '});',
  20718. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20719. '});',
  20720. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20721. ' this.GetEnumerator = function () {',
  20722. ' var Result = null;',
  20723. ' return Result;',
  20724. ' };',
  20725. '});',
  20726. 'this.b = null;',
  20727. 'this.i = null;',
  20728. 'this.i2 = null;'
  20729. ]),
  20730. LinesToStr([ // $mod.$main
  20731. 'var $in1 = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  20732. 'try {',
  20733. ' while ($in1.MoveNext()){',
  20734. ' $mod.i = $in1.FCurrent;',
  20735. ' $mod.i2 = $mod.i;',
  20736. ' }',
  20737. '} finally {',
  20738. ' $in1 = rtl.freeLoc($in1)',
  20739. '};',
  20740. '']));
  20741. end;
  20742. procedure TTestModule.TestClassHelper_PassProperty;
  20743. begin
  20744. StartProgram(false);
  20745. Add([
  20746. 'type',
  20747. ' TObject = class',
  20748. ' FField: TObject;',
  20749. ' property Field: TObject read FField write FField;',
  20750. ' end;',
  20751. ' THelper = class helper for TObject',
  20752. ' procedure Fly;',
  20753. ' class procedure Run;',
  20754. ' class procedure Jump; static;',
  20755. ' end;',
  20756. 'procedure THelper.Fly;',
  20757. 'begin',
  20758. ' Field.Fly;',
  20759. ' Field.Run;',
  20760. ' Field.Jump;',
  20761. ' with Field do begin',
  20762. ' Fly;',
  20763. ' Run;',
  20764. ' Jump;',
  20765. ' end;',
  20766. 'end;',
  20767. 'class procedure THelper.Run;',
  20768. 'begin',
  20769. 'end;',
  20770. 'class procedure THelper.Jump;',
  20771. 'begin',
  20772. 'end;',
  20773. 'var',
  20774. ' b: TObject;',
  20775. 'begin',
  20776. ' b.Field.Fly;',
  20777. ' b.Field.Run;',
  20778. ' b.Field.Jump;',
  20779. ' with b do begin',
  20780. ' Field.Run;',
  20781. ' Field.Fly;',
  20782. ' Field.Jump;',
  20783. ' end;',
  20784. ' with b.Field do begin',
  20785. ' Run;',
  20786. ' Fly;',
  20787. ' Jump;',
  20788. ' end;',
  20789. '']);
  20790. ConvertProgram;
  20791. CheckSource('TestClassHelper_PassProperty',
  20792. LinesToStr([ // statements
  20793. 'rtl.createClass($mod, "TObject", null, function () {',
  20794. ' this.$init = function () {',
  20795. ' this.FField = null;',
  20796. ' };',
  20797. ' this.$final = function () {',
  20798. ' this.FField = undefined;',
  20799. ' };',
  20800. '});',
  20801. 'rtl.createHelper($mod, "THelper", null, function () {',
  20802. ' this.Fly = function () {',
  20803. ' $mod.THelper.Fly.call(this.FField);',
  20804. ' $mod.THelper.Run.call(this.FField.$class);',
  20805. ' $mod.THelper.Jump();',
  20806. ' var $with1 = this.FField;',
  20807. ' $mod.THelper.Fly.call($with1);',
  20808. ' $mod.THelper.Run.call($with1.$class);',
  20809. ' $mod.THelper.Jump();',
  20810. ' };',
  20811. ' this.Run = function () {',
  20812. ' };',
  20813. ' this.Jump = function () {',
  20814. ' };',
  20815. '});',
  20816. 'this.b = null;',
  20817. '']),
  20818. LinesToStr([ // $mod.$main
  20819. '$mod.THelper.Fly.call($mod.b.FField);',
  20820. '$mod.THelper.Run.call($mod.b.FField.$class);',
  20821. '$mod.THelper.Jump();',
  20822. 'var $with1 = $mod.b;',
  20823. '$mod.THelper.Run.call($with1.FField.$class);',
  20824. '$mod.THelper.Fly.call($with1.FField);',
  20825. '$mod.THelper.Jump();',
  20826. 'var $with2 = $mod.b.FField;',
  20827. '$mod.THelper.Run.call($with2.$class);',
  20828. '$mod.THelper.Fly.call($with2);',
  20829. '$mod.THelper.Jump();',
  20830. '']));
  20831. end;
  20832. procedure TTestModule.TestExtClassHelper_ClassVar;
  20833. begin
  20834. StartProgram(false);
  20835. Add([
  20836. '{$modeswitch externalclass}',
  20837. 'type',
  20838. ' TExtA = class external name ''ExtObj''',
  20839. ' end;',
  20840. ' THelper = class helper for TExtA',
  20841. ' const',
  20842. ' One = 1;',
  20843. ' Two: word = 2;',
  20844. ' class var',
  20845. ' Glob: word;',
  20846. ' function Foo(w: word): word;',
  20847. ' class function Bar(w: word): word; static;',
  20848. ' end;',
  20849. 'function THelper.foo(w: word): word;',
  20850. 'begin',
  20851. ' Result:=w;',
  20852. ' Two:=One+w;',
  20853. ' Glob:=Glob;',
  20854. ' Result:=Self.Glob;',
  20855. ' Self.Glob:=Self.Glob;',
  20856. ' with Self do Glob:=Glob;',
  20857. 'end;',
  20858. 'class function THelper.bar(w: word): word;',
  20859. 'begin',
  20860. ' Result:=w;',
  20861. ' Two:=One;',
  20862. ' Glob:=Glob;',
  20863. 'end;',
  20864. 'var o: TExtA;',
  20865. 'begin',
  20866. ' texta.two:=texta.one;',
  20867. ' texta.Glob:=texta.Glob;',
  20868. ' with texta do begin',
  20869. ' two:=one;',
  20870. ' Glob:=Glob;',
  20871. ' end;',
  20872. ' o.two:=o.one;',
  20873. ' o.Glob:=o.Glob;',
  20874. ' with o do begin',
  20875. ' two:=one;',
  20876. ' Glob:=Glob;',
  20877. ' end;',
  20878. '']);
  20879. ConvertProgram;
  20880. CheckSource('TestExtClassHelper_ClassVar',
  20881. LinesToStr([ // statements
  20882. 'rtl.createHelper($mod, "THelper", null, function () {',
  20883. ' this.One = 1;',
  20884. ' this.Two = 2;',
  20885. ' this.Glob = 0;',
  20886. ' this.Foo = function (w) {',
  20887. ' var Result = 0;',
  20888. ' Result = w;',
  20889. ' $mod.THelper.Two = 1 + w;',
  20890. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20891. ' Result = $mod.THelper.Glob;',
  20892. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20893. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20894. ' return Result;',
  20895. ' };',
  20896. ' this.Bar = function (w) {',
  20897. ' var Result = 0;',
  20898. ' Result = w;',
  20899. ' $mod.THelper.Two = 1;',
  20900. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20901. ' return Result;',
  20902. ' };',
  20903. '});',
  20904. 'this.o = null;',
  20905. '']),
  20906. LinesToStr([ // $mod.$main
  20907. '$mod.THelper.Two = 1;',
  20908. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20909. '$mod.THelper.Two = 1;',
  20910. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20911. '$mod.THelper.Two = 1;',
  20912. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20913. 'var $with1 = $mod.o;',
  20914. '$mod.THelper.Two = 1;',
  20915. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20916. '']));
  20917. end;
  20918. procedure TTestModule.TestExtClassHelper_Method_Call;
  20919. begin
  20920. StartProgram(false);
  20921. Add([
  20922. '{$modeswitch externalclass}',
  20923. 'type',
  20924. ' TFly = function(w: word): word of object;',
  20925. ' TExtA = class external name ''ExtObj''',
  20926. ' procedure Run(w: word = 10);',
  20927. ' end;',
  20928. ' THelper = class helper for TExtA',
  20929. ' function Foo(w: word = 1): word;',
  20930. ' function Fly(w: word = 2): word; external name ''Fly'';',
  20931. ' end;',
  20932. 'var p: TFly;',
  20933. 'function THelper.foo(w: word): word;',
  20934. 'begin',
  20935. ' Run;',
  20936. ' Run();',
  20937. ' Run(11);',
  20938. ' Foo;',
  20939. ' Foo();',
  20940. ' Foo(12);',
  20941. ' Self.Foo;',
  20942. ' Self.Foo();',
  20943. ' Self.Foo(13);',
  20944. ' Fly;',
  20945. ' Fly();',
  20946. ' with Self do begin',
  20947. ' Foo;',
  20948. ' Foo();',
  20949. ' Foo(14);',
  20950. ' Fly;',
  20951. ' Fly();',
  20952. ' end;',
  20953. ' p:=@Fly;',
  20954. 'end;',
  20955. 'var Obj: TExtA;',
  20956. 'begin',
  20957. ' obj.Foo;',
  20958. ' obj.Foo();',
  20959. ' obj.Foo(21);',
  20960. ' obj.Fly;',
  20961. ' obj.Fly();',
  20962. ' with obj do begin',
  20963. ' Foo;',
  20964. ' Foo();',
  20965. ' Foo(22);',
  20966. ' Fly;',
  20967. ' Fly();',
  20968. ' end;',
  20969. ' p:[email protected];',
  20970. '']);
  20971. ConvertProgram;
  20972. CheckSource('TestExtClassHelper_Method_Call',
  20973. LinesToStr([ // statements
  20974. 'rtl.createHelper($mod, "THelper", null, function () {',
  20975. ' this.Foo = function (w) {',
  20976. ' var Result = 0;',
  20977. ' this.Run(10);',
  20978. ' this.Run(10);',
  20979. ' this.Run(11);',
  20980. ' $mod.THelper.Foo.call(this, 1);',
  20981. ' $mod.THelper.Foo.call(this, 1);',
  20982. ' $mod.THelper.Foo.call(this, 12);',
  20983. ' $mod.THelper.Foo.call(this, 1);',
  20984. ' $mod.THelper.Foo.call(this, 1);',
  20985. ' $mod.THelper.Foo.call(this, 13);',
  20986. ' this.Fly(2);',
  20987. ' this.Fly(2);',
  20988. ' $mod.THelper.Foo.call(this, 1);',
  20989. ' $mod.THelper.Foo.call(this, 1);',
  20990. ' $mod.THelper.Foo.call(this, 14);',
  20991. ' this.Fly(2);',
  20992. ' this.Fly(2);',
  20993. ' $mod.p = rtl.createCallback(this, "Fly");',
  20994. ' return Result;',
  20995. ' };',
  20996. '});',
  20997. 'this.p = null;',
  20998. 'this.Obj = null;',
  20999. '']),
  21000. LinesToStr([ // $mod.$main
  21001. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21002. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21003. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21004. '$mod.Obj.Fly(2);',
  21005. '$mod.Obj.Fly(2);',
  21006. 'var $with1 = $mod.Obj;',
  21007. '$mod.THelper.Foo.call($with1, 1);',
  21008. '$mod.THelper.Foo.call($with1, 1);',
  21009. '$mod.THelper.Foo.call($with1, 22);',
  21010. '$with1.Fly(2);',
  21011. '$with1.Fly(2);',
  21012. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  21013. '']));
  21014. end;
  21015. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  21016. begin
  21017. StartProgram(false);
  21018. Add([
  21019. '{$modeswitch externalclass}',
  21020. 'type',
  21021. ' TExtA = class external name ''ExtObj''',
  21022. ' procedure Run(w: word = 10);',
  21023. ' end;',
  21024. ' THelper = class helper for TExtA',
  21025. ' class procedure Fly;',
  21026. ' end;',
  21027. 'class procedure THelper.Fly;',
  21028. 'begin end;',
  21029. 'begin',
  21030. '']);
  21031. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  21032. nHelperClassMethodForExtClassMustBeStatic);
  21033. ConvertProgram;
  21034. end;
  21035. procedure TTestModule.TestRecordHelper_ClassVar;
  21036. begin
  21037. StartProgram(false);
  21038. Add([
  21039. 'type',
  21040. ' TRec = record',
  21041. ' end;',
  21042. ' THelper = record helper for TRec',
  21043. ' const',
  21044. ' One = 1;',
  21045. ' Two: word = 2;',
  21046. ' class var',
  21047. ' Glob: word;',
  21048. ' function Foo(w: word): word;',
  21049. ' class function Bar(w: word): word; static;',
  21050. ' end;',
  21051. 'function THelper.foo(w: word): word;',
  21052. 'begin',
  21053. ' Result:=w;',
  21054. ' Two:=One+w;',
  21055. ' Glob:=Glob;',
  21056. ' Result:=Self.Glob;',
  21057. ' Self.Glob:=Self.Glob;',
  21058. ' with Self do Glob:=Glob;',
  21059. ' Self:=Self;',
  21060. 'end;',
  21061. 'class function THelper.bar(w: word): word;',
  21062. 'begin',
  21063. ' Result:=w;',
  21064. ' Two:=One;',
  21065. ' Glob:=Glob;',
  21066. 'end;',
  21067. 'var r: TRec;',
  21068. 'begin',
  21069. ' trec.two:=trec.one;',
  21070. ' trec.Glob:=trec.Glob;',
  21071. ' with trec do begin',
  21072. ' two:=one;',
  21073. ' Glob:=Glob;',
  21074. ' end;',
  21075. ' r.two:=r.one;',
  21076. ' r.Glob:=r.Glob;',
  21077. ' with r do begin',
  21078. ' two:=one;',
  21079. ' Glob:=Glob;',
  21080. ' end;',
  21081. '']);
  21082. ConvertProgram;
  21083. CheckSource('TestRecordHelper_ClassVar',
  21084. LinesToStr([ // statements
  21085. 'rtl.recNewT($mod, "TRec", function () {',
  21086. ' this.$eq = function (b) {',
  21087. ' return true;',
  21088. ' };',
  21089. ' this.$assign = function (s) {',
  21090. ' return this;',
  21091. ' };',
  21092. '});',
  21093. 'rtl.createHelper($mod, "THelper", null, function () {',
  21094. ' this.One = 1;',
  21095. ' this.Two = 2;',
  21096. ' this.Glob = 0;',
  21097. ' this.Foo = function (w) {',
  21098. ' var Result = 0;',
  21099. ' Result = w;',
  21100. ' $mod.THelper.Two = 1 + w;',
  21101. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21102. ' Result = $mod.THelper.Glob;',
  21103. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21104. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21105. ' this.$assign(this);',
  21106. ' return Result;',
  21107. ' };',
  21108. ' this.Bar = function (w) {',
  21109. ' var Result = 0;',
  21110. ' Result = w;',
  21111. ' $mod.THelper.Two = 1;',
  21112. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21113. ' return Result;',
  21114. ' };',
  21115. '});',
  21116. 'this.r = $mod.TRec.$new();',
  21117. '']),
  21118. LinesToStr([ // $mod.$main
  21119. '$mod.THelper.Two = 1;',
  21120. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21121. 'var $with1 = $mod.TRec;',
  21122. '$mod.THelper.Two = 1;',
  21123. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21124. '$mod.THelper.Two = 1;',
  21125. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21126. 'var $with2 = $mod.r;',
  21127. '$mod.THelper.Two = 1;',
  21128. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21129. '']));
  21130. end;
  21131. procedure TTestModule.TestRecordHelper_Method_Call;
  21132. begin
  21133. StartProgram(false);
  21134. Add([
  21135. '{$modeswitch AdvancedRecords}',
  21136. 'type',
  21137. ' TRec = record',
  21138. ' procedure Run(w: word = 10);',
  21139. ' end;',
  21140. ' THelper = record helper for TRec',
  21141. ' function Foo(w: word = 1): word;',
  21142. ' end;',
  21143. 'procedure TRec.Run(w: word);',
  21144. 'begin',
  21145. ' Foo;',
  21146. ' Foo();',
  21147. ' Foo(2);',
  21148. ' Self.Foo;',
  21149. ' Self.Foo();',
  21150. ' Self.Foo(3);',
  21151. ' with Self do begin',
  21152. ' Foo;',
  21153. ' Foo();',
  21154. ' Foo(4);',
  21155. ' end;',
  21156. 'end;',
  21157. 'function THelper.foo(w: word): word;',
  21158. 'begin',
  21159. ' Run;',
  21160. ' Run();',
  21161. ' Run(11);',
  21162. ' Foo;',
  21163. ' Foo();',
  21164. ' Foo(12);',
  21165. ' Self.Foo;',
  21166. ' Self.Foo();',
  21167. ' Self.Foo(13);',
  21168. ' with Self do begin',
  21169. ' Foo;',
  21170. ' Foo();',
  21171. ' Foo(14);',
  21172. ' end;',
  21173. 'end;',
  21174. 'var Rec: TRec;',
  21175. 'begin',
  21176. ' Rec.Foo;',
  21177. ' Rec.Foo();',
  21178. ' Rec.Foo(21);',
  21179. ' with Rec do begin',
  21180. ' Foo;',
  21181. ' Foo();',
  21182. ' Foo(22);',
  21183. ' end;',
  21184. '']);
  21185. ConvertProgram;
  21186. CheckSource('TestRecordHelper_Method_Call',
  21187. LinesToStr([ // statements
  21188. 'rtl.recNewT($mod, "TRec", function () {',
  21189. ' this.$eq = function (b) {',
  21190. ' return true;',
  21191. ' };',
  21192. ' this.$assign = function (s) {',
  21193. ' return this;',
  21194. ' };',
  21195. ' this.Run = function (w) {',
  21196. ' $mod.THelper.Foo.call(this, 1);',
  21197. ' $mod.THelper.Foo.call(this, 1);',
  21198. ' $mod.THelper.Foo.call(this, 2);',
  21199. ' $mod.THelper.Foo.call(this, 1);',
  21200. ' $mod.THelper.Foo.call(this, 1);',
  21201. ' $mod.THelper.Foo.call(this, 3);',
  21202. ' $mod.THelper.Foo.call(this, 1);',
  21203. ' $mod.THelper.Foo.call(this, 1);',
  21204. ' $mod.THelper.Foo.call(this, 4);',
  21205. ' };',
  21206. '});',
  21207. 'rtl.createHelper($mod, "THelper", null, function () {',
  21208. ' this.Foo = function (w) {',
  21209. ' var Result = 0;',
  21210. ' this.Run(10);',
  21211. ' this.Run(10);',
  21212. ' this.Run(11);',
  21213. ' $mod.THelper.Foo.call(this, 1);',
  21214. ' $mod.THelper.Foo.call(this, 1);',
  21215. ' $mod.THelper.Foo.call(this, 12);',
  21216. ' $mod.THelper.Foo.call(this, 1);',
  21217. ' $mod.THelper.Foo.call(this, 1);',
  21218. ' $mod.THelper.Foo.call(this, 13);',
  21219. ' $mod.THelper.Foo.call(this, 1);',
  21220. ' $mod.THelper.Foo.call(this, 1);',
  21221. ' $mod.THelper.Foo.call(this, 14);',
  21222. ' return Result;',
  21223. ' };',
  21224. '});',
  21225. 'this.Rec = $mod.TRec.$new();',
  21226. '']),
  21227. LinesToStr([ // $mod.$main
  21228. '$mod.THelper.Foo.call($mod.Rec, 1);',
  21229. '$mod.THelper.Foo.call($mod.Rec, 1);',
  21230. '$mod.THelper.Foo.call($mod.Rec, 21);',
  21231. 'var $with1 = $mod.Rec;',
  21232. '$mod.THelper.Foo.call($with1, 1);',
  21233. '$mod.THelper.Foo.call($with1, 1);',
  21234. '$mod.THelper.Foo.call($with1, 22);',
  21235. '']));
  21236. end;
  21237. procedure TTestModule.TestRecordHelper_Constructor;
  21238. begin
  21239. StartProgram(false);
  21240. Add([
  21241. '{$modeswitch AdvancedRecords}',
  21242. 'type',
  21243. ' TRec = record',
  21244. ' constructor Create(w: word);',
  21245. ' end;',
  21246. ' THelper = record helper for TRec',
  21247. ' constructor NewHlp(w: word);',
  21248. ' end;',
  21249. 'var',
  21250. ' Rec: TRec;',
  21251. 'constructor TRec.Create(w: word);',
  21252. 'begin',
  21253. ' NewHlp(2);', // normal call
  21254. ' trec.NewHlp(3);', // new instance
  21255. 'end;',
  21256. 'constructor THelper.NewHlp(w: word);',
  21257. 'begin',
  21258. ' create(2);', // normal call
  21259. ' trec.create(3);', // new instance
  21260. ' NewHlp(4);', // normal call
  21261. ' trec.NewHlp(5);', // new instance
  21262. 'end;',
  21263. 'begin',
  21264. ' rec.newhlp(2);', // normal call
  21265. ' with rec do newhlp(12);', // normal call
  21266. ' trec.newhlp(3);', // new instance
  21267. ' with trec do newhlp(13);', // new instance
  21268. '']);
  21269. ConvertProgram;
  21270. CheckSource('TestRecordHelper_Constructor',
  21271. LinesToStr([ // statements
  21272. 'rtl.recNewT($mod, "TRec", function () {',
  21273. ' this.$eq = function (b) {',
  21274. ' return true;',
  21275. ' };',
  21276. ' this.$assign = function (s) {',
  21277. ' return this;',
  21278. ' };',
  21279. ' this.Create = function (w) {',
  21280. ' $mod.THelper.NewHlp.call(this, 2);',
  21281. ' $mod.THelper.$new("NewHlp", [3]);',
  21282. ' return this;',
  21283. ' };',
  21284. '}, true);',
  21285. 'rtl.createHelper($mod, "THelper", null, function () {',
  21286. ' this.NewHlp = function (w) {',
  21287. ' this.Create(2);',
  21288. ' $mod.TRec.$new().Create(3);',
  21289. ' $mod.THelper.NewHlp.call(this, 4);',
  21290. ' $mod.THelper.$new("NewHlp", [5]);',
  21291. ' return this;',
  21292. ' };',
  21293. ' this.$new = function (fn, args) {',
  21294. ' return this[fn].apply($mod.TRec.$new(), args);',
  21295. ' };',
  21296. '});',
  21297. 'this.Rec = $mod.TRec.$new();',
  21298. '']),
  21299. LinesToStr([ // $mod.$main
  21300. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  21301. 'var $with1 = $mod.Rec;',
  21302. '$mod.THelper.NewHlp.call($with1, 12);',
  21303. '$mod.THelper.$new("NewHlp", [3]);',
  21304. 'var $with2 = $mod.TRec;',
  21305. '$mod.THelper.$new("NewHlp", [13]);',
  21306. '']));
  21307. end;
  21308. procedure TTestModule.TestTypeHelper_ClassVar;
  21309. begin
  21310. StartProgram(false);
  21311. Add([
  21312. '{$modeswitch typehelpers}',
  21313. 'type',
  21314. ' THelper = type helper for byte',
  21315. ' const',
  21316. ' One = 1;',
  21317. ' Two: word = 2;',
  21318. ' class var',
  21319. ' Glob: word;',
  21320. ' function Foo(w: word): word;',
  21321. ' class function Bar(w: word): word; static;',
  21322. ' end;',
  21323. 'function THelper.foo(w: word): word;',
  21324. 'begin',
  21325. ' Result:=w;',
  21326. ' Two:=One+w;',
  21327. ' Glob:=Glob;',
  21328. ' Result:=Self.Glob;',
  21329. ' Self.Glob:=Self.Glob;',
  21330. ' with Self do Glob:=Glob;',
  21331. 'end;',
  21332. 'class function THelper.bar(w: word): word;',
  21333. 'begin',
  21334. ' Result:=w;',
  21335. ' Two:=One;',
  21336. ' Glob:=Glob;',
  21337. 'end;',
  21338. 'var b: byte;',
  21339. 'begin',
  21340. ' byte.two:=byte.one;',
  21341. ' byte.Glob:=byte.Glob;',
  21342. ' with byte do begin',
  21343. ' two:=one;',
  21344. ' Glob:=Glob;',
  21345. ' end;',
  21346. ' b.two:=b.one;',
  21347. ' b.Glob:=b.Glob;',
  21348. ' with b do begin',
  21349. ' two:=one;',
  21350. ' Glob:=Glob;',
  21351. ' end;',
  21352. '']);
  21353. ConvertProgram;
  21354. CheckSource('TestTypeHelper_ClassVar',
  21355. LinesToStr([ // statements
  21356. 'rtl.createHelper($mod, "THelper", null, function () {',
  21357. ' this.One = 1;',
  21358. ' this.Two = 2;',
  21359. ' this.Glob = 0;',
  21360. ' this.Foo = function (w) {',
  21361. ' var Result = 0;',
  21362. ' Result = w;',
  21363. ' $mod.THelper.Two = 1 + w;',
  21364. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21365. ' Result = $mod.THelper.Glob;',
  21366. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21367. ' var $with1 = this.get();',
  21368. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21369. ' return Result;',
  21370. ' };',
  21371. ' this.Bar = function (w) {',
  21372. ' var Result = 0;',
  21373. ' Result = w;',
  21374. ' $mod.THelper.Two = 1;',
  21375. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21376. ' return Result;',
  21377. ' };',
  21378. '});',
  21379. 'this.b = 0;',
  21380. '']),
  21381. LinesToStr([ // $mod.$main
  21382. '$mod.THelper.Two = 1;',
  21383. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21384. '$mod.THelper.Two = 1;',
  21385. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21386. '$mod.THelper.Two = 1;',
  21387. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21388. 'var $with1 = $mod.b;',
  21389. '$mod.THelper.Two = 1;',
  21390. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21391. '']));
  21392. end;
  21393. procedure TTestModule.TestTypeHelper_PassResultElement;
  21394. begin
  21395. StartProgram(false);
  21396. Add([
  21397. '{$modeswitch typehelpers}',
  21398. 'type',
  21399. ' THelper = type helper for word',
  21400. ' procedure DoIt(e: byte = 123);',
  21401. ' class procedure DoSome(e: byte = 456); static;',
  21402. ' end;',
  21403. 'procedure THelper.DoIt(e: byte);',
  21404. 'begin',
  21405. 'end;',
  21406. 'class procedure THelper.DoSome(e: byte);',
  21407. 'begin',
  21408. 'end;',
  21409. 'function Foo(w: word): word;',
  21410. 'begin',
  21411. ' Result.DoIt;',
  21412. ' Result.DoIt();',
  21413. ' Result.DoSome;',
  21414. ' Result.DoSome();',
  21415. ' with Result do begin',
  21416. ' DoIt;',
  21417. ' DoIt();',
  21418. ' DoSome;',
  21419. ' DoSome();',
  21420. ' end;',
  21421. 'end;',
  21422. 'begin',
  21423. '']);
  21424. ConvertProgram;
  21425. CheckSource('TestTypeHelper_PassResultElement',
  21426. LinesToStr([ // statements
  21427. 'rtl.createHelper($mod, "THelper", null, function () {',
  21428. ' this.DoIt = function (e) {',
  21429. ' };',
  21430. ' this.DoSome = function (e) {',
  21431. ' };',
  21432. '});',
  21433. 'this.Foo = function (w) {',
  21434. ' var Result = 0;',
  21435. ' $mod.THelper.DoIt.call({',
  21436. ' get: function () {',
  21437. ' return Result;',
  21438. ' },',
  21439. ' set: function (v) {',
  21440. ' Result = v;',
  21441. ' }',
  21442. ' }, 123);',
  21443. ' $mod.THelper.DoIt.call({',
  21444. ' get: function () {',
  21445. ' return Result;',
  21446. ' },',
  21447. ' set: function (v) {',
  21448. ' Result = v;',
  21449. ' }',
  21450. ' }, 123);',
  21451. ' $mod.THelper.DoSome(456);',
  21452. ' $mod.THelper.DoSome(456);',
  21453. ' $mod.THelper.DoIt.call({',
  21454. ' get: function () {',
  21455. ' return Result;',
  21456. ' },',
  21457. ' set: function (v) {',
  21458. ' Result = v;',
  21459. ' }',
  21460. ' }, 123);',
  21461. ' $mod.THelper.DoIt.call({',
  21462. ' get: function () {',
  21463. ' return Result;',
  21464. ' },',
  21465. ' set: function (v) {',
  21466. ' Result = v;',
  21467. ' }',
  21468. ' }, 123);',
  21469. ' $mod.THelper.DoSome(456);',
  21470. ' $mod.THelper.DoSome(456);',
  21471. ' return Result;',
  21472. '};',
  21473. '']),
  21474. LinesToStr([ // $mod.$main
  21475. '']));
  21476. end;
  21477. procedure TTestModule.TestTypeHelper_PassArgs;
  21478. begin
  21479. StartProgram(false);
  21480. Add([
  21481. '{$modeswitch typehelpers}',
  21482. 'type',
  21483. ' THelper = type helper for word',
  21484. ' procedure DoIt(e: byte = 123);',
  21485. ' end;',
  21486. 'procedure THelper.DoIt(e: byte);',
  21487. 'begin',
  21488. 'end;',
  21489. 'procedure FooDefault(a: word);',
  21490. 'begin',
  21491. ' a.DoIt;',
  21492. ' with a do DoIt;',
  21493. 'end;',
  21494. 'procedure FooConst(const a: word);',
  21495. 'begin',
  21496. ' a.DoIt;',
  21497. ' with a do DoIt;',
  21498. 'end;',
  21499. 'procedure FooVar(var a: word);',
  21500. 'begin',
  21501. ' a.DoIt;',
  21502. ' with a do DoIt;',
  21503. 'end;',
  21504. 'begin',
  21505. '']);
  21506. ConvertProgram;
  21507. CheckSource('TestTypeHelper_PassArgs',
  21508. LinesToStr([ // statements
  21509. 'rtl.createHelper($mod, "THelper", null, function () {',
  21510. ' this.DoIt = function (e) {',
  21511. ' };',
  21512. '});',
  21513. 'this.FooDefault = function (a) {',
  21514. ' $mod.THelper.DoIt.call({',
  21515. ' get: function () {',
  21516. ' return a;',
  21517. ' },',
  21518. ' set: function (v) {',
  21519. ' a = v;',
  21520. ' }',
  21521. ' }, 123);',
  21522. ' $mod.THelper.DoIt.call({',
  21523. ' get: function () {',
  21524. ' return a;',
  21525. ' },',
  21526. ' set: function (v) {',
  21527. ' a = v;',
  21528. ' }',
  21529. ' }, 123);',
  21530. '};',
  21531. 'this.FooConst = function (a) {',
  21532. ' $mod.THelper.DoIt.call({',
  21533. ' get: function () {',
  21534. ' return a;',
  21535. ' },',
  21536. ' set: function (v) {',
  21537. ' rtl.raiseE("EPropReadOnly");',
  21538. ' }',
  21539. ' }, 123);',
  21540. ' $mod.THelper.DoIt.call({',
  21541. ' get: function () {',
  21542. ' return a;',
  21543. ' },',
  21544. ' set: function () {',
  21545. ' rtl.raiseE("EPropReadOnly");',
  21546. ' }',
  21547. ' }, 123);',
  21548. '};',
  21549. 'this.FooVar = function (a) {',
  21550. ' $mod.THelper.DoIt.call(a, 123);',
  21551. ' var $with1 = a.get();',
  21552. ' $mod.THelper.DoIt.call(a, 123);',
  21553. '};',
  21554. '']),
  21555. LinesToStr([ // $mod.$main
  21556. '']));
  21557. end;
  21558. procedure TTestModule.TestTypeHelper_PassVarConst;
  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. ' end;',
  21567. 'procedure THelper.DoIt(e: byte);',
  21568. 'begin',
  21569. 'end;',
  21570. 'var a: word;',
  21571. 'const c: word = 2;',
  21572. '{$writeableconst off}',
  21573. 'const r: word = 3;',
  21574. 'begin',
  21575. ' a.DoIt;',
  21576. ' with a do DoIt;',
  21577. ' c.DoIt;',
  21578. ' with c do DoIt;',
  21579. ' r.DoIt;',
  21580. ' with r do DoIt;',
  21581. '']);
  21582. ConvertProgram;
  21583. CheckSource('TestTypeHelper_PassVarConst',
  21584. LinesToStr([ // statements
  21585. 'rtl.createHelper($mod, "THelper", null, function () {',
  21586. ' this.DoIt = function (e) {',
  21587. ' };',
  21588. '});',
  21589. 'this.a = 0;',
  21590. 'this.c = 2;',
  21591. 'this.r = 3;',
  21592. '']),
  21593. LinesToStr([ // $mod.$main
  21594. '$mod.THelper.DoIt.call({',
  21595. ' p: $mod,',
  21596. ' get: function () {',
  21597. ' return this.p.a;',
  21598. ' },',
  21599. ' set: function (v) {',
  21600. ' this.p.a = v;',
  21601. ' }',
  21602. '}, 123);',
  21603. 'var $with1 = $mod.a;',
  21604. '$mod.THelper.DoIt.call({',
  21605. ' get: function () {',
  21606. ' return $with1;',
  21607. ' },',
  21608. ' set: function (v) {',
  21609. ' $with1 = v;',
  21610. ' }',
  21611. '}, 123);',
  21612. '$mod.THelper.DoIt.call({',
  21613. ' p: $mod,',
  21614. ' get: function () {',
  21615. ' return this.p.c;',
  21616. ' },',
  21617. ' set: function (v) {',
  21618. ' this.p.c = v;',
  21619. ' }',
  21620. '}, 123);',
  21621. 'var $with2 = $mod.c;',
  21622. '$mod.THelper.DoIt.call({',
  21623. ' get: function () {',
  21624. ' return $with2;',
  21625. ' },',
  21626. ' set: function (v) {',
  21627. ' $with2 = v;',
  21628. ' }',
  21629. '}, 123);',
  21630. '$mod.THelper.DoIt.call({',
  21631. ' get: function () {',
  21632. ' return 3;',
  21633. ' },',
  21634. ' set: function (v) {',
  21635. ' rtl.raiseE("EPropReadOnly");',
  21636. ' }',
  21637. '}, 123);',
  21638. 'var $with3 = 3;',
  21639. ' $mod.THelper.DoIt.call({',
  21640. ' get: function () {',
  21641. ' return $with3;',
  21642. ' },',
  21643. ' set: function () {',
  21644. ' rtl.raiseE("EPropReadOnly");',
  21645. ' }',
  21646. ' }, 123);',
  21647. '']));
  21648. end;
  21649. procedure TTestModule.TestTypeHelper_PassFuncResult;
  21650. begin
  21651. StartProgram(false);
  21652. Add([
  21653. '{$modeswitch typehelpers}',
  21654. 'type',
  21655. ' THelper = type helper for word',
  21656. ' procedure DoIt(e: byte = 123);',
  21657. ' end;',
  21658. 'procedure THelper.DoIt(e: byte);',
  21659. 'begin',
  21660. 'end;',
  21661. 'function Foo(b: byte = 1): word;',
  21662. 'begin',
  21663. 'end;',
  21664. 'begin',
  21665. ' Foo.DoIt;',
  21666. ' Foo().DoIt;',
  21667. ' with Foo do DoIt;',
  21668. ' with Foo() do DoIt;',
  21669. '']);
  21670. ConvertProgram;
  21671. CheckSource('TestTypeHelper_PassFuncResult',
  21672. LinesToStr([ // statements
  21673. 'rtl.createHelper($mod, "THelper", null, function () {',
  21674. ' this.DoIt = function (e) {',
  21675. ' };',
  21676. '});',
  21677. 'this.Foo = function (b) {',
  21678. ' var Result = 0;',
  21679. ' return Result;',
  21680. '};',
  21681. '']),
  21682. LinesToStr([ // $mod.$main
  21683. '$mod.THelper.DoIt.call({',
  21684. ' a: $mod.Foo(1),',
  21685. ' get: function () {',
  21686. ' return this.a;',
  21687. ' },',
  21688. ' set: function (v) {',
  21689. ' this.a = v;',
  21690. ' }',
  21691. '}, 123);',
  21692. '$mod.THelper.DoIt.call({',
  21693. ' a: $mod.Foo(1),',
  21694. ' get: function () {',
  21695. ' return this.a;',
  21696. ' },',
  21697. ' set: function (v) {',
  21698. ' this.a = v;',
  21699. ' }',
  21700. '}, 123);',
  21701. 'var $with1 = $mod.Foo(1);',
  21702. '$mod.THelper.DoIt.call({',
  21703. ' get: function () {',
  21704. ' return $with1;',
  21705. ' },',
  21706. ' set: function (v) {',
  21707. ' $with1 = v;',
  21708. ' }',
  21709. '}, 123);',
  21710. 'var $with2 = $mod.Foo(1);',
  21711. '$mod.THelper.DoIt.call({',
  21712. ' get: function () {',
  21713. ' return $with2;',
  21714. ' },',
  21715. ' set: function (v) {',
  21716. ' $with2 = v;',
  21717. ' }',
  21718. '}, 123);',
  21719. '']));
  21720. end;
  21721. procedure TTestModule.TestTypeHelper_PassPropertyField;
  21722. begin
  21723. StartProgram(false);
  21724. Add([
  21725. '{$modeswitch typehelpers}',
  21726. 'type',
  21727. ' TObject = class',
  21728. ' FField: word;',
  21729. ' procedure SetField(Value: word);',
  21730. ' property Field: word read FField write SetField;',
  21731. ' end;',
  21732. ' THelper = type helper for word',
  21733. ' procedure Fly;',
  21734. ' class procedure Run; static;',
  21735. ' end;',
  21736. 'procedure TObject.SetField(Value: word);',
  21737. 'begin',
  21738. ' Field.Fly;',
  21739. ' Field.Run;',
  21740. ' Self.Field.Fly;',
  21741. ' Self.Field.Run;',
  21742. ' with Self do begin',
  21743. ' Field.Fly;',
  21744. ' Field.Run;',
  21745. ' end;',
  21746. ' with Self.Field do begin',
  21747. ' Fly;',
  21748. ' Run;',
  21749. ' end;',
  21750. 'end;',
  21751. 'procedure THelper.Fly;',
  21752. 'begin',
  21753. 'end;',
  21754. 'class procedure THelper.Run;',
  21755. 'begin',
  21756. 'end;',
  21757. 'var',
  21758. ' o: TObject;',
  21759. 'begin',
  21760. ' o.Field.Fly;',
  21761. ' o.Field.Run;',
  21762. ' with o do begin',
  21763. ' Field.Fly;',
  21764. ' Field.Run;',
  21765. ' end;',
  21766. ' with o.Field do begin',
  21767. ' Fly;',
  21768. ' Run;',
  21769. ' end;',
  21770. '']);
  21771. ConvertProgram;
  21772. CheckSource('TestTypeHelper_PassPropertyField',
  21773. LinesToStr([ // statements
  21774. 'rtl.createClass($mod, "TObject", null, function () {',
  21775. ' this.$init = function () {',
  21776. ' this.FField = 0;',
  21777. ' };',
  21778. ' this.$final = function () {',
  21779. ' };',
  21780. ' this.SetField = function (Value) {',
  21781. ' $mod.THelper.Fly.call({',
  21782. ' p: this,',
  21783. ' get: function () {',
  21784. ' return this.p.FField;',
  21785. ' },',
  21786. ' set: function (v) {',
  21787. ' this.p.FField = v;',
  21788. ' }',
  21789. ' });',
  21790. ' $mod.THelper.Run();',
  21791. ' $mod.THelper.Fly.call({',
  21792. ' p: this,',
  21793. ' get: function () {',
  21794. ' return this.p.FField;',
  21795. ' },',
  21796. ' set: function (v) {',
  21797. ' this.p.FField = v;',
  21798. ' }',
  21799. ' });',
  21800. ' $mod.THelper.Run();',
  21801. ' $mod.THelper.Fly.call({',
  21802. ' p: this,',
  21803. ' get: function () {',
  21804. ' return this.p.FField;',
  21805. ' },',
  21806. ' set: function (v) {',
  21807. ' this.p.FField = v;',
  21808. ' }',
  21809. ' });',
  21810. ' $mod.THelper.Run();',
  21811. ' var $with1 = this.FField;',
  21812. ' $mod.THelper.Fly.call({',
  21813. ' get: function () {',
  21814. ' return $with1;',
  21815. ' },',
  21816. ' set: function (v) {',
  21817. ' $with1 = v;',
  21818. ' }',
  21819. ' });',
  21820. ' $mod.THelper.Run();',
  21821. ' };',
  21822. '});',
  21823. 'rtl.createHelper($mod, "THelper", null, function () {',
  21824. ' this.Fly = function () {',
  21825. ' };',
  21826. ' this.Run = function () {',
  21827. ' };',
  21828. '});',
  21829. 'this.o = null;',
  21830. '']),
  21831. LinesToStr([ // $mod.$main
  21832. '$mod.THelper.Fly.call({',
  21833. ' p: $mod.o,',
  21834. ' get: function () {',
  21835. ' return this.p.FField;',
  21836. ' },',
  21837. ' set: function (v) {',
  21838. ' this.p.FField = v;',
  21839. ' }',
  21840. '});',
  21841. '$mod.THelper.Run();',
  21842. 'var $with1 = $mod.o;',
  21843. '$mod.THelper.Fly.call({',
  21844. ' p: $with1,',
  21845. ' get: function () {',
  21846. ' return this.p.FField;',
  21847. ' },',
  21848. ' set: function (v) {',
  21849. ' this.p.FField = v;',
  21850. ' }',
  21851. '});',
  21852. '$mod.THelper.Run();',
  21853. 'var $with2 = $mod.o.FField;',
  21854. '$mod.THelper.Fly.call({',
  21855. ' get: function () {',
  21856. ' return $with2;',
  21857. ' },',
  21858. ' set: function (v) {',
  21859. ' $with2 = v;',
  21860. ' }',
  21861. '});',
  21862. '$mod.THelper.Run();',
  21863. '']));
  21864. end;
  21865. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  21866. begin
  21867. StartProgram(false);
  21868. Add([
  21869. '{$modeswitch typehelpers}',
  21870. 'type',
  21871. ' TObject = class',
  21872. ' FField: word;',
  21873. ' function GetField: word;',
  21874. ' property Field: word read GetField write FField;',
  21875. ' end;',
  21876. ' THelper = type helper for word',
  21877. ' procedure Fly;',
  21878. ' class procedure Run; static;',
  21879. ' end;',
  21880. 'function TObject.GetField: word;',
  21881. 'begin',
  21882. ' Field.Fly;',
  21883. ' Field.Run;',
  21884. ' Self.Field.Fly;',
  21885. ' Self.Field.Run;',
  21886. ' with Self do begin',
  21887. ' Field.Fly;',
  21888. ' Field.Run;',
  21889. ' end;',
  21890. ' with Self.Field do begin',
  21891. ' Fly;',
  21892. ' Run;',
  21893. ' end;',
  21894. 'end;',
  21895. 'procedure THelper.Fly;',
  21896. 'begin',
  21897. 'end;',
  21898. 'class procedure THelper.Run;',
  21899. 'begin',
  21900. 'end;',
  21901. 'var',
  21902. ' o: TObject;',
  21903. 'begin',
  21904. ' o.Field.Fly;',
  21905. ' o.Field.Run;',
  21906. ' with o do begin',
  21907. ' Field.Fly;',
  21908. ' Field.Run;',
  21909. ' end;',
  21910. ' with o.Field do begin',
  21911. ' Fly;',
  21912. ' Run;',
  21913. ' end;',
  21914. '']);
  21915. ConvertProgram;
  21916. CheckSource('TestTypeHelper_PassPropertyGetter',
  21917. LinesToStr([ // statements
  21918. 'rtl.createClass($mod, "TObject", null, function () {',
  21919. ' this.$init = function () {',
  21920. ' this.FField = 0;',
  21921. ' };',
  21922. ' this.$final = function () {',
  21923. ' };',
  21924. ' this.GetField = function () {',
  21925. ' var Result = 0;',
  21926. ' $mod.THelper.Fly.call({',
  21927. ' p: this.GetField(),',
  21928. ' get: function () {',
  21929. ' return this.p;',
  21930. ' },',
  21931. ' set: function (v) {',
  21932. ' this.p = v;',
  21933. ' }',
  21934. ' });',
  21935. ' $mod.THelper.Run();',
  21936. ' $mod.THelper.Fly.call({',
  21937. ' p: this.GetField(),',
  21938. ' get: function () {',
  21939. ' return this.p;',
  21940. ' },',
  21941. ' set: function (v) {',
  21942. ' this.p = v;',
  21943. ' }',
  21944. ' });',
  21945. ' $mod.THelper.Run();',
  21946. ' $mod.THelper.Fly.call({',
  21947. ' p: this.GetField(),',
  21948. ' get: function () {',
  21949. ' return this.p;',
  21950. ' },',
  21951. ' set: function (v) {',
  21952. ' this.p = v;',
  21953. ' }',
  21954. ' });',
  21955. ' $mod.THelper.Run();',
  21956. ' var $with1 = this.GetField();',
  21957. ' $mod.THelper.Fly.call({',
  21958. ' get: function () {',
  21959. ' return $with1;',
  21960. ' },',
  21961. ' set: function (v) {',
  21962. ' $with1 = v;',
  21963. ' }',
  21964. ' });',
  21965. ' $mod.THelper.Run();',
  21966. ' return Result;',
  21967. ' };',
  21968. '});',
  21969. 'rtl.createHelper($mod, "THelper", null, function () {',
  21970. ' this.Fly = function () {',
  21971. ' };',
  21972. ' this.Run = function () {',
  21973. ' };',
  21974. '});',
  21975. 'this.o = null;',
  21976. '']),
  21977. LinesToStr([ // $mod.$main
  21978. '$mod.THelper.Fly.call({',
  21979. ' p: $mod.o.GetField(),',
  21980. ' get: function () {',
  21981. ' return this.p;',
  21982. ' },',
  21983. ' set: function (v) {',
  21984. ' this.p = v;',
  21985. ' }',
  21986. '});',
  21987. '$mod.THelper.Run();',
  21988. 'var $with1 = $mod.o;',
  21989. '$mod.THelper.Fly.call({',
  21990. ' p: $with1.GetField(),',
  21991. ' get: function () {',
  21992. ' return this.p;',
  21993. ' },',
  21994. ' set: function (v) {',
  21995. ' this.p = v;',
  21996. ' }',
  21997. '});',
  21998. '$mod.THelper.Run();',
  21999. 'var $with2 = $mod.o.GetField();',
  22000. '$mod.THelper.Fly.call({',
  22001. ' get: function () {',
  22002. ' return $with2;',
  22003. ' },',
  22004. ' set: function (v) {',
  22005. ' $with2 = v;',
  22006. ' }',
  22007. '});',
  22008. '$mod.THelper.Run();',
  22009. '']));
  22010. end;
  22011. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  22012. begin
  22013. StartProgram(false);
  22014. Add([
  22015. '{$modeswitch typehelpers}',
  22016. 'type',
  22017. ' TObject = class',
  22018. ' class var FField: word;',
  22019. ' class procedure SetField(Value: word);',
  22020. ' class property Field: word read FField write SetField;',
  22021. ' end;',
  22022. ' THelper = type helper for word',
  22023. ' procedure Fly(n: byte);',
  22024. ' end;',
  22025. 'class procedure TObject.SetField(Value: word);',
  22026. 'begin',
  22027. ' Field.Fly(1);',
  22028. ' Self.Field.Fly(2);',
  22029. ' with Self do Field.Fly(3);',
  22030. ' with Self.Field do Fly(4);',
  22031. ' TObject.Field.Fly(5);',
  22032. ' with TObject do Field.Fly(6);',
  22033. ' with TObject.Field do Fly(7);',
  22034. 'end;',
  22035. 'procedure THelper.Fly(n: byte);',
  22036. 'begin',
  22037. 'end;',
  22038. 'var',
  22039. ' o: TObject;',
  22040. 'begin',
  22041. ' o.Field.Fly(11);',
  22042. ' with o do Field.Fly(12);',
  22043. ' with o.Field do Fly(13);',
  22044. ' TObject.Field.Fly(14);',
  22045. ' with TObject do Field.Fly(15);',
  22046. ' with TObject.Field do Fly(16);',
  22047. '']);
  22048. ConvertProgram;
  22049. CheckSource('TestTypeHelper_PassClassPropertyField',
  22050. LinesToStr([ // statements
  22051. 'rtl.createClass($mod, "TObject", null, function () {',
  22052. ' this.FField = 0;',
  22053. ' this.$init = function () {',
  22054. ' };',
  22055. ' this.$final = function () {',
  22056. ' };',
  22057. ' this.SetField = function (Value) {',
  22058. ' $mod.THelper.Fly.call({',
  22059. ' p: this,',
  22060. ' get: function () {',
  22061. ' return this.p.FField;',
  22062. ' },',
  22063. ' set: function (v) {',
  22064. ' $mod.TObject.FField = v;',
  22065. ' }',
  22066. ' }, 1);',
  22067. ' $mod.THelper.Fly.call({',
  22068. ' p: this,',
  22069. ' get: function () {',
  22070. ' return this.p.FField;',
  22071. ' },',
  22072. ' set: function (v) {',
  22073. ' $mod.TObject.FField = v;',
  22074. ' }',
  22075. ' }, 2);',
  22076. ' $mod.THelper.Fly.call({',
  22077. ' p: this,',
  22078. ' get: function () {',
  22079. ' return this.p.FField;',
  22080. ' },',
  22081. ' set: function (v) {',
  22082. ' $mod.TObject.FField = v;',
  22083. ' }',
  22084. ' }, 3);',
  22085. ' var $with1 = this.FField;',
  22086. ' $mod.THelper.Fly.call({',
  22087. ' get: function () {',
  22088. ' return $with1;',
  22089. ' },',
  22090. ' set: function (v) {',
  22091. ' $with1 = v;',
  22092. ' }',
  22093. ' }, 4);',
  22094. ' $mod.THelper.Fly.call({',
  22095. ' p: $mod.TObject,',
  22096. ' get: function () {',
  22097. ' return this.p.FField;',
  22098. ' },',
  22099. ' set: function (v) {',
  22100. ' $mod.TObject.FField = v;',
  22101. ' }',
  22102. ' }, 5);',
  22103. ' var $with2 = $mod.TObject;',
  22104. ' $mod.THelper.Fly.call({',
  22105. ' p: $with2,',
  22106. ' get: function () {',
  22107. ' return this.p.FField;',
  22108. ' },',
  22109. ' set: function (v) {',
  22110. ' $mod.TObject.FField = v;',
  22111. ' }',
  22112. ' }, 6);',
  22113. ' var $with3 = $mod.TObject.FField;',
  22114. ' $mod.THelper.Fly.call({',
  22115. ' get: function () {',
  22116. ' return $with3;',
  22117. ' },',
  22118. ' set: function (v) {',
  22119. ' $with3 = v;',
  22120. ' }',
  22121. ' }, 7);',
  22122. ' };',
  22123. '});',
  22124. 'rtl.createHelper($mod, "THelper", null, function () {',
  22125. ' this.Fly = function (n) {',
  22126. ' };',
  22127. '});',
  22128. 'this.o = null;',
  22129. '']),
  22130. LinesToStr([ // $mod.$main
  22131. '$mod.THelper.Fly.call({',
  22132. ' p: $mod.o,',
  22133. ' get: function () {',
  22134. ' return this.p.FField;',
  22135. ' },',
  22136. ' set: function (v) {',
  22137. ' $mod.TObject.FField = v;',
  22138. ' }',
  22139. '}, 11);',
  22140. 'var $with1 = $mod.o;',
  22141. '$mod.THelper.Fly.call({',
  22142. ' p: $with1,',
  22143. ' get: function () {',
  22144. ' return this.p.FField;',
  22145. ' },',
  22146. ' set: function (v) {',
  22147. ' $mod.TObject.FField = v;',
  22148. ' }',
  22149. '}, 12);',
  22150. 'var $with2 = $mod.o.FField;',
  22151. '$mod.THelper.Fly.call({',
  22152. ' get: function () {',
  22153. ' return $with2;',
  22154. ' },',
  22155. ' set: function (v) {',
  22156. ' $with2 = v;',
  22157. ' }',
  22158. '}, 13);',
  22159. '$mod.THelper.Fly.call({',
  22160. ' p: $mod.TObject,',
  22161. ' get: function () {',
  22162. ' return this.p.FField;',
  22163. ' },',
  22164. ' set: function (v) {',
  22165. ' $mod.TObject.FField = v;',
  22166. ' }',
  22167. '}, 14);',
  22168. 'var $with3 = $mod.TObject;',
  22169. '$mod.THelper.Fly.call({',
  22170. ' p: $with3,',
  22171. ' get: function () {',
  22172. ' return this.p.FField;',
  22173. ' },',
  22174. ' set: function (v) {',
  22175. ' $mod.TObject.FField = v;',
  22176. ' }',
  22177. '}, 15);',
  22178. 'var $with4 = $mod.TObject.FField;',
  22179. '$mod.THelper.Fly.call({',
  22180. ' get: function () {',
  22181. ' return $with4;',
  22182. ' },',
  22183. ' set: function (v) {',
  22184. ' $with4 = v;',
  22185. ' }',
  22186. '}, 16);',
  22187. '']));
  22188. end;
  22189. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  22190. begin
  22191. StartProgram(false);
  22192. Add([
  22193. '{$modeswitch typehelpers}',
  22194. 'type',
  22195. ' TObject = class',
  22196. ' class var FField: word;',
  22197. ' class function GetField: word; static;',
  22198. ' class property Field: word read GetField write FField;',
  22199. ' end;',
  22200. ' THelper = type helper for word',
  22201. ' procedure Fly(n: byte);',
  22202. ' end;',
  22203. 'class function TObject.GetField: word;',
  22204. 'begin',
  22205. ' Field.Fly(1);',
  22206. ' TObject.Field.Fly(5);',
  22207. ' with TObject do Field.Fly(6);',
  22208. ' with TObject.Field do Fly(7);',
  22209. 'end;',
  22210. 'procedure THelper.Fly(n: byte);',
  22211. 'begin',
  22212. 'end;',
  22213. 'var',
  22214. ' o: TObject;',
  22215. 'begin',
  22216. ' o.Field.Fly(11);',
  22217. ' with o do Field.Fly(12);',
  22218. ' with o.Field do Fly(13);',
  22219. '']);
  22220. ConvertProgram;
  22221. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  22222. LinesToStr([ // statements
  22223. 'rtl.createClass($mod, "TObject", null, function () {',
  22224. ' this.FField = 0;',
  22225. ' this.$init = function () {',
  22226. ' };',
  22227. ' this.$final = function () {',
  22228. ' };',
  22229. ' this.GetField = function () {',
  22230. ' var Result = 0;',
  22231. ' $mod.THelper.Fly.call({',
  22232. ' p: this.GetField(),',
  22233. ' get: function () {',
  22234. ' return this.p;',
  22235. ' },',
  22236. ' set: function (v) {',
  22237. ' this.p = v;',
  22238. ' }',
  22239. ' }, 1);',
  22240. ' $mod.THelper.Fly.call({',
  22241. ' p: $mod.TObject.GetField(),',
  22242. ' get: function () {',
  22243. ' return this.p;',
  22244. ' },',
  22245. ' set: function (v) {',
  22246. ' this.p = v;',
  22247. ' }',
  22248. ' }, 5);',
  22249. ' var $with1 = $mod.TObject;',
  22250. ' $mod.THelper.Fly.call({',
  22251. ' p: $with1.GetField(),',
  22252. ' get: function () {',
  22253. ' return this.p;',
  22254. ' },',
  22255. ' set: function (v) {',
  22256. ' this.p = v;',
  22257. ' }',
  22258. ' }, 6);',
  22259. ' var $with2 = $mod.TObject.GetField();',
  22260. ' $mod.THelper.Fly.call({',
  22261. ' get: function () {',
  22262. ' return $with2;',
  22263. ' },',
  22264. ' set: function (v) {',
  22265. ' $with2 = v;',
  22266. ' }',
  22267. ' }, 7);',
  22268. ' return Result;',
  22269. ' };',
  22270. '});',
  22271. 'rtl.createHelper($mod, "THelper", null, function () {',
  22272. ' this.Fly = function (n) {',
  22273. ' };',
  22274. '});',
  22275. 'this.o = null;',
  22276. '']),
  22277. LinesToStr([ // $mod.$main
  22278. '$mod.THelper.Fly.call({',
  22279. ' p: $mod.o.GetField(),',
  22280. ' get: function () {',
  22281. ' return this.p;',
  22282. ' },',
  22283. ' set: function (v) {',
  22284. ' this.p = v;',
  22285. ' }',
  22286. '}, 11);',
  22287. 'var $with1 = $mod.o;',
  22288. '$mod.THelper.Fly.call({',
  22289. ' p: $with1.GetField(),',
  22290. ' get: function () {',
  22291. ' return this.p;',
  22292. ' },',
  22293. ' set: function (v) {',
  22294. ' this.p = v;',
  22295. ' }',
  22296. '}, 12);',
  22297. 'var $with2 = $mod.o.GetField();',
  22298. '$mod.THelper.Fly.call({',
  22299. ' get: function () {',
  22300. ' return $with2;',
  22301. ' },',
  22302. ' set: function (v) {',
  22303. ' $with2 = v;',
  22304. ' }',
  22305. '}, 13);',
  22306. '']));
  22307. end;
  22308. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  22309. begin
  22310. StartProgram(false);
  22311. Add([
  22312. '{$modeswitch typehelpers}',
  22313. 'type',
  22314. ' TObject = class',
  22315. ' class var FField: word;',
  22316. ' class function GetField: word;',
  22317. ' class property Field: word read GetField write FField;',
  22318. ' end;',
  22319. ' TClass = class of TObject;',
  22320. ' THelper = type helper for word',
  22321. ' procedure Fly(n: byte);',
  22322. ' end;',
  22323. 'class function TObject.GetField: word;',
  22324. 'begin',
  22325. ' Field.Fly(1);',
  22326. ' Self.Field.Fly(5);',
  22327. ' with Self do Field.Fly(6);',
  22328. ' with Self.Field do Fly(7);',
  22329. 'end;',
  22330. 'procedure THelper.Fly(n: byte);',
  22331. 'begin',
  22332. 'end;',
  22333. 'var',
  22334. ' o: TObject;',
  22335. ' c: TClass;',
  22336. 'begin',
  22337. ' o.Field.Fly(11);',
  22338. ' with o do Field.Fly(12);',
  22339. ' with o.Field do Fly(13);',
  22340. ' c.Field.Fly(14);',
  22341. ' with c do Field.Fly(15);',
  22342. ' with c.Field do Fly(16);',
  22343. '']);
  22344. ConvertProgram;
  22345. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  22346. LinesToStr([ // statements
  22347. 'rtl.createClass($mod, "TObject", null, function () {',
  22348. ' this.FField = 0;',
  22349. ' this.$init = function () {',
  22350. ' };',
  22351. ' this.$final = function () {',
  22352. ' };',
  22353. ' this.GetField = function () {',
  22354. ' var Result = 0;',
  22355. ' $mod.THelper.Fly.call({',
  22356. ' p: this.GetField(),',
  22357. ' get: function () {',
  22358. ' return this.p;',
  22359. ' },',
  22360. ' set: function (v) {',
  22361. ' this.p = v;',
  22362. ' }',
  22363. ' }, 1);',
  22364. ' $mod.THelper.Fly.call({',
  22365. ' p: this.GetField(),',
  22366. ' get: function () {',
  22367. ' return this.p;',
  22368. ' },',
  22369. ' set: function (v) {',
  22370. ' this.p = v;',
  22371. ' }',
  22372. ' }, 5);',
  22373. ' $mod.THelper.Fly.call({',
  22374. ' p: this.GetField(),',
  22375. ' get: function () {',
  22376. ' return this.p;',
  22377. ' },',
  22378. ' set: function (v) {',
  22379. ' this.p = v;',
  22380. ' }',
  22381. ' }, 6);',
  22382. ' var $with1 = this.GetField();',
  22383. ' $mod.THelper.Fly.call({',
  22384. ' get: function () {',
  22385. ' return $with1;',
  22386. ' },',
  22387. ' set: function (v) {',
  22388. ' $with1 = v;',
  22389. ' }',
  22390. ' }, 7);',
  22391. ' return Result;',
  22392. ' };',
  22393. '});',
  22394. 'rtl.createHelper($mod, "THelper", null, function () {',
  22395. ' this.Fly = function (n) {',
  22396. ' };',
  22397. '});',
  22398. 'this.o = null;',
  22399. 'this.c = null;',
  22400. '']),
  22401. LinesToStr([ // $mod.$main
  22402. '$mod.THelper.Fly.call({',
  22403. ' p: $mod.o.$class.GetField(),',
  22404. ' get: function () {',
  22405. ' return this.p;',
  22406. ' },',
  22407. ' set: function (v) {',
  22408. ' this.p = v;',
  22409. ' }',
  22410. '}, 11);',
  22411. 'var $with1 = $mod.o;',
  22412. '$mod.THelper.Fly.call({',
  22413. ' p: $with1.$class.GetField(),',
  22414. ' get: function () {',
  22415. ' return this.p;',
  22416. ' },',
  22417. ' set: function (v) {',
  22418. ' this.p = v;',
  22419. ' }',
  22420. '}, 12);',
  22421. 'var $with2 = $mod.o.$class.GetField();',
  22422. '$mod.THelper.Fly.call({',
  22423. ' get: function () {',
  22424. ' return $with2;',
  22425. ' },',
  22426. ' set: function (v) {',
  22427. ' $with2 = v;',
  22428. ' }',
  22429. '}, 13);',
  22430. '$mod.THelper.Fly.call({',
  22431. ' p: $mod.c.GetField(),',
  22432. ' get: function () {',
  22433. ' return this.p;',
  22434. ' },',
  22435. ' set: function (v) {',
  22436. ' this.p = v;',
  22437. ' }',
  22438. '}, 14);',
  22439. 'var $with3 = $mod.c;',
  22440. '$mod.THelper.Fly.call({',
  22441. ' p: $with3.GetField(),',
  22442. ' get: function () {',
  22443. ' return this.p;',
  22444. ' },',
  22445. ' set: function (v) {',
  22446. ' this.p = v;',
  22447. ' }',
  22448. '}, 15);',
  22449. 'var $with4 = $mod.c.GetField();',
  22450. '$mod.THelper.Fly.call({',
  22451. ' get: function () {',
  22452. ' return $with4;',
  22453. ' },',
  22454. ' set: function (v) {',
  22455. ' $with4 = v;',
  22456. ' }',
  22457. '}, 16);',
  22458. '']));
  22459. end;
  22460. procedure TTestModule.TestTypeHelper_Property;
  22461. begin
  22462. StartProgram(false);
  22463. Add([
  22464. '{$modeswitch typehelpers}',
  22465. 'type',
  22466. ' THelper = type helper for word',
  22467. ' function GetSize: longint;',
  22468. ' procedure SetSize(Value: longint);',
  22469. ' property Size: longint read GetSize write SetSize;',
  22470. ' end;',
  22471. 'function THelper.GetSize: longint;',
  22472. 'begin',
  22473. ' Result:=Size+1;',
  22474. ' Size:=2;',
  22475. ' Result:=Self.Size+3;',
  22476. ' Self.Size:=4;',
  22477. ' with Self do begin',
  22478. ' Result:=Size+5;',
  22479. ' Size:=6;',
  22480. ' end;',
  22481. 'end;',
  22482. 'procedure THelper.SetSize(Value: longint);',
  22483. 'begin',
  22484. 'end;',
  22485. 'var w: word;',
  22486. 'begin',
  22487. ' w:=w.Size+7;',
  22488. ' w.Size:=w+8;',
  22489. ' with w do begin',
  22490. ' w:=Size+9;',
  22491. ' Size:=w+10;',
  22492. ' end;',
  22493. '']);
  22494. ConvertProgram;
  22495. CheckSource('TestTypeHelper_Property',
  22496. LinesToStr([ // statements
  22497. 'rtl.createHelper($mod, "THelper", null, function () {',
  22498. ' this.GetSize = function () {',
  22499. ' var Result = 0;',
  22500. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  22501. ' $mod.THelper.SetSize.call(this, 2);',
  22502. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  22503. ' $mod.THelper.SetSize.call(this, 4);',
  22504. ' var $with1 = this.get();',
  22505. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  22506. ' $mod.THelper.SetSize.call(this, 6);',
  22507. ' return Result;',
  22508. ' };',
  22509. ' this.SetSize = function (Value) {',
  22510. ' };',
  22511. '});',
  22512. 'this.w = 0;',
  22513. '']),
  22514. LinesToStr([ // $mod.$main
  22515. '$mod.w = $mod.THelper.GetSize.call({',
  22516. ' p: $mod,',
  22517. ' get: function () {',
  22518. ' return this.p.w;',
  22519. ' },',
  22520. ' set: function (v) {',
  22521. ' this.p.w = v;',
  22522. ' }',
  22523. '}) + 7;',
  22524. '$mod.THelper.SetSize.call({',
  22525. ' p: $mod,',
  22526. ' get: function () {',
  22527. ' return this.p.w;',
  22528. ' },',
  22529. ' set: function (v) {',
  22530. ' this.p.w = v;',
  22531. ' }',
  22532. '}, $mod.w + 8);',
  22533. 'var $with1 = $mod.w;',
  22534. '$mod.w = $mod.THelper.GetSize.call({',
  22535. ' get: function () {',
  22536. ' return $with1;',
  22537. ' },',
  22538. ' set: function (v) {',
  22539. ' $with1 = v;',
  22540. ' }',
  22541. '}) + 9;',
  22542. '$mod.THelper.SetSize.call({',
  22543. ' get: function () {',
  22544. ' return $with1;',
  22545. ' },',
  22546. ' set: function (v) {',
  22547. ' $with1 = v;',
  22548. ' }',
  22549. '}, $mod.w + 10);',
  22550. '']));
  22551. end;
  22552. procedure TTestModule.TestTypeHelper_Property_Array;
  22553. begin
  22554. StartProgram(false);
  22555. Add([
  22556. '{$modeswitch typehelpers}',
  22557. 'type',
  22558. ' THelper = type helper for word',
  22559. ' function GetItems(Index: byte): boolean;',
  22560. ' procedure SetItems(Index: byte; Value: boolean);',
  22561. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  22562. ' end;',
  22563. 'function THelper.GetItems(Index: byte): boolean;',
  22564. 'begin',
  22565. ' Result:=Items[1];',
  22566. ' Items[2]:=false;',
  22567. ' Result:=Self.Items[3];',
  22568. ' Self.Items[4]:=true;',
  22569. ' with Self do begin',
  22570. ' Result:=Items[5];',
  22571. ' Items[6]:=false;',
  22572. ' end;',
  22573. 'end;',
  22574. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  22575. 'begin',
  22576. 'end;',
  22577. 'var',
  22578. ' w: word;',
  22579. ' b: boolean;',
  22580. 'begin',
  22581. ' b:=w.Items[1];',
  22582. ' w.Items[2]:=b;',
  22583. ' with w do begin',
  22584. ' b:=Items[3];',
  22585. ' Items[4]:=b;',
  22586. ' end;',
  22587. '']);
  22588. ConvertProgram;
  22589. CheckSource('TestTypeHelper_Property_Array',
  22590. LinesToStr([ // statements
  22591. 'rtl.createHelper($mod, "THelper", null, function () {',
  22592. ' this.GetItems = function (Index) {',
  22593. ' var Result = false;',
  22594. ' Result = $mod.THelper.GetItems.call(this, 1);',
  22595. ' $mod.THelper.SetItems.call(this, 2, false);',
  22596. ' Result = $mod.THelper.GetItems.call(this, 3);',
  22597. ' $mod.THelper.SetItems.call(this, 4, true);',
  22598. ' var $with1 = this.get();',
  22599. ' Result = $mod.THelper.GetItems.call(this, 5);',
  22600. ' $mod.THelper.SetItems.call(this, 6, false);',
  22601. ' return Result;',
  22602. ' };',
  22603. ' this.SetItems = function (Index, Value) {',
  22604. ' };',
  22605. '});',
  22606. 'this.w = 0;',
  22607. 'this.b = false;',
  22608. '']),
  22609. LinesToStr([ // $mod.$main
  22610. '$mod.b = $mod.THelper.GetItems.call({',
  22611. ' p: $mod,',
  22612. ' get: function () {',
  22613. ' return this.p.w;',
  22614. ' },',
  22615. ' set: function (v) {',
  22616. ' this.p.w = v;',
  22617. ' }',
  22618. '}, 1);',
  22619. '$mod.THelper.SetItems.call({',
  22620. ' p: $mod,',
  22621. ' get: function () {',
  22622. ' return this.p.w;',
  22623. ' },',
  22624. ' set: function (v) {',
  22625. ' this.p.w = v;',
  22626. ' }',
  22627. '}, 2, $mod.b);',
  22628. 'var $with1 = $mod.w;',
  22629. '$mod.b = $mod.THelper.GetItems.call({',
  22630. ' get: function () {',
  22631. ' return $with1;',
  22632. ' },',
  22633. ' set: function (v) {',
  22634. ' $with1 = v;',
  22635. ' }',
  22636. '}, 3);',
  22637. '$mod.THelper.SetItems.call({',
  22638. ' get: function () {',
  22639. ' return $with1;',
  22640. ' },',
  22641. ' set: function (v) {',
  22642. ' $with1 = v;',
  22643. ' }',
  22644. '}, 4, $mod.b);',
  22645. '']));
  22646. end;
  22647. procedure TTestModule.TestTypeHelper_ClassProperty;
  22648. begin
  22649. StartProgram(false);
  22650. Add([
  22651. '{$modeswitch typehelpers}',
  22652. 'type',
  22653. ' THelper = type helper for word',
  22654. ' class function GetSize: longint; static;',
  22655. ' class procedure SetSize(Value: longint); static;',
  22656. ' class property Size: longint read GetSize write SetSize;',
  22657. ' end;',
  22658. 'class function THelper.GetSize: longint;',
  22659. 'begin',
  22660. ' Result:=Size+1;',
  22661. ' Size:=2;',
  22662. 'end;',
  22663. 'class procedure THelper.SetSize(Value: longint);',
  22664. 'begin',
  22665. 'end;',
  22666. 'begin',
  22667. '']);
  22668. ConvertProgram;
  22669. CheckSource('TestTypeHelper_ClassProperty',
  22670. LinesToStr([ // statements
  22671. 'rtl.createHelper($mod, "THelper", null, function () {',
  22672. ' this.GetSize = function () {',
  22673. ' var Result = 0;',
  22674. ' Result = $mod.THelper.GetSize() + 1;',
  22675. ' $mod.THelper.SetSize(2);',
  22676. ' return Result;',
  22677. ' };',
  22678. ' this.SetSize = function (Value) {',
  22679. ' };',
  22680. '});',
  22681. '']),
  22682. LinesToStr([ // $mod.$main
  22683. '']));
  22684. end;
  22685. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  22686. begin
  22687. StartProgram(false);
  22688. Add([
  22689. '{$modeswitch typehelpers}',
  22690. 'type',
  22691. ' THelper = type helper for word',
  22692. ' class function GetItems(Index: byte): boolean; static;',
  22693. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  22694. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  22695. ' end;',
  22696. 'class function THelper.GetItems(Index: byte): boolean;',
  22697. 'begin',
  22698. ' Result:=Items[1];',
  22699. ' Items[2]:=false;',
  22700. 'end;',
  22701. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  22702. 'begin',
  22703. 'end;',
  22704. 'var',
  22705. ' w: word;',
  22706. ' b: boolean;',
  22707. 'begin',
  22708. ' b:=w.Items[1];',
  22709. ' w.Items[2]:=b;',
  22710. ' with w do begin',
  22711. ' b:=Items[3];',
  22712. ' Items[4]:=b;',
  22713. ' end;',
  22714. '']);
  22715. ConvertProgram;
  22716. CheckSource('TestTypeHelper_ClassProperty_Array',
  22717. LinesToStr([ // statements
  22718. 'rtl.createHelper($mod, "THelper", null, function () {',
  22719. ' this.GetItems = function (Index) {',
  22720. ' var Result = false;',
  22721. ' Result = $mod.THelper.GetItems(1);',
  22722. ' $mod.THelper.SetItems(2, false);',
  22723. ' return Result;',
  22724. ' };',
  22725. ' this.SetItems = function (Index, Value) {',
  22726. ' };',
  22727. '});',
  22728. 'this.w = 0;',
  22729. 'this.b = false;',
  22730. '']),
  22731. LinesToStr([ // $mod.$main
  22732. '$mod.b = $mod.THelper.GetItems(1);',
  22733. '$mod.THelper.SetItems(2, $mod.b);',
  22734. 'var $with1 = $mod.w;',
  22735. '$mod.b = $mod.THelper.GetItems(3);',
  22736. '$mod.THelper.SetItems(4, $mod.b);',
  22737. '']));
  22738. end;
  22739. procedure TTestModule.TestTypeHelper_ClassMethod;
  22740. begin
  22741. StartProgram(false);
  22742. Add([
  22743. '{$modeswitch typehelpers}',
  22744. 'type',
  22745. ' THelper = type helper for word',
  22746. ' class procedure DoStatic; static;',
  22747. ' end;',
  22748. 'class procedure THelper.DoStatic;',
  22749. 'begin',
  22750. ' DoStatic;',
  22751. ' DoStatic();',
  22752. 'end;',
  22753. 'var w: word;',
  22754. 'begin',
  22755. ' w.DoStatic;',
  22756. ' w.DoStatic();',
  22757. '']);
  22758. ConvertProgram;
  22759. CheckSource('TestTypeHelper_ClassMethod',
  22760. LinesToStr([ // statements
  22761. 'rtl.createHelper($mod, "THelper", null, function () {',
  22762. ' this.DoStatic = function () {',
  22763. ' $mod.THelper.DoStatic();',
  22764. ' $mod.THelper.DoStatic();',
  22765. ' };',
  22766. '});',
  22767. 'this.w = 0;',
  22768. '']),
  22769. LinesToStr([ // $mod.$main
  22770. '$mod.THelper.DoStatic();',
  22771. '$mod.THelper.DoStatic();',
  22772. '']));
  22773. end;
  22774. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  22775. begin
  22776. StartProgram(false);
  22777. Add([
  22778. '{$modeswitch typehelpers}',
  22779. 'type',
  22780. ' THelper = type helper for word',
  22781. ' procedure Run; external name ''Run'';',
  22782. ' end;',
  22783. 'var w: word;',
  22784. 'begin',
  22785. ' w.Run;',
  22786. '']);
  22787. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  22788. ConvertProgram;
  22789. end;
  22790. procedure TTestModule.TestTypeHelper_Constructor;
  22791. begin
  22792. StartProgram(false);
  22793. Add([
  22794. '{$modeswitch typehelpers}',
  22795. 'type',
  22796. ' THelper = type helper for word',
  22797. ' constructor Init(e: longint);',
  22798. ' end;',
  22799. 'constructor THelper.Init(e: longint);',
  22800. 'begin',
  22801. ' Self:=e;',
  22802. ' Init(e+1);',
  22803. 'end;',
  22804. 'var w: word;',
  22805. 'begin',
  22806. ' w:=word.Init(2);',
  22807. ' w:=w.Init(3);',
  22808. ' with word do w:=Init(4);',
  22809. ' with w do w:=Init(5);',
  22810. '']);
  22811. ConvertProgram;
  22812. CheckSource('TestTypeHelper_Constructor',
  22813. LinesToStr([ // statements
  22814. 'rtl.createHelper($mod, "THelper", null, function () {',
  22815. ' this.Init = function (e) {',
  22816. ' this.set(e);',
  22817. ' $mod.THelper.Init.call(this, e + 1);',
  22818. ' return this.get();',
  22819. ' };',
  22820. ' this.$new = function (fn, args) {',
  22821. ' return this[fn].apply({',
  22822. ' p: 0,',
  22823. ' get: function () {',
  22824. ' return this.p;',
  22825. ' },',
  22826. ' set: function (v) {',
  22827. ' this.p = v;',
  22828. ' }',
  22829. ' }, args);',
  22830. ' };',
  22831. '});',
  22832. 'this.w = 0;',
  22833. '']),
  22834. LinesToStr([ // $mod.$main
  22835. '$mod.w = $mod.THelper.$new("Init", [2]);',
  22836. '$mod.w = $mod.THelper.Init.call({',
  22837. ' p: $mod,',
  22838. ' get: function () {',
  22839. ' return this.p.w;',
  22840. ' },',
  22841. ' set: function (v) {',
  22842. ' this.p.w = v;',
  22843. ' }',
  22844. '}, 3);',
  22845. '$mod.w = $mod.THelper.$new("Init", [4]);',
  22846. 'var $with1 = $mod.w;',
  22847. '$mod.w = $mod.THelper.Init.call({',
  22848. ' get: function () {',
  22849. ' return $with1;',
  22850. ' },',
  22851. ' set: function (v) {',
  22852. ' $with1 = v;',
  22853. ' }',
  22854. '}, 5);',
  22855. '']));
  22856. end;
  22857. procedure TTestModule.TestTypeHelper_Word;
  22858. begin
  22859. StartProgram(false);
  22860. Add([
  22861. '{$modeswitch typehelpers}',
  22862. 'type',
  22863. ' THelper = type helper for word',
  22864. ' procedure DoIt(e: byte = 123);',
  22865. ' end;',
  22866. 'procedure THelper.DoIt(e: byte);',
  22867. 'begin',
  22868. ' Self:=e;',
  22869. ' Self:=Self+1;',
  22870. ' with Self do Doit;',
  22871. 'end;',
  22872. 'begin',
  22873. ' word(3).DoIt;',
  22874. '']);
  22875. ConvertProgram;
  22876. CheckSource('TestTypeHelper_Word',
  22877. LinesToStr([ // statements
  22878. 'rtl.createHelper($mod, "THelper", null, function () {',
  22879. ' this.DoIt = function (e) {',
  22880. ' this.set(e);',
  22881. ' this.set(this.get() + 1);',
  22882. ' var $with1 = this.get();',
  22883. ' $mod.THelper.DoIt.call(this, 123);',
  22884. ' };',
  22885. '});',
  22886. '']),
  22887. LinesToStr([ // $mod.$main
  22888. '$mod.THelper.DoIt.call({',
  22889. ' get: function () {',
  22890. ' return 3;',
  22891. ' },',
  22892. ' set: function (v) {',
  22893. ' rtl.raiseE("EPropReadOnly");',
  22894. ' }',
  22895. '}, 123);',
  22896. '']));
  22897. end;
  22898. procedure TTestModule.TestTypeHelper_Double;
  22899. begin
  22900. StartProgram(false);
  22901. Add([
  22902. '{$modeswitch typehelpers}',
  22903. 'type',
  22904. ' Float = type double;',
  22905. ' THelper = type helper for Float',
  22906. ' const NPI = 3.141592;',
  22907. ' function ToStr: String;',
  22908. ' end;',
  22909. 'function THelper.ToStr: String;',
  22910. 'begin',
  22911. 'end;',
  22912. 'procedure DoIt(s: string);',
  22913. 'begin',
  22914. 'end;',
  22915. 'var f: Float;',
  22916. 'begin',
  22917. ' DoIt(f.toStr);',
  22918. ' DoIt(f.toStr());',
  22919. '']);
  22920. ConvertProgram;
  22921. CheckSource('TestTypeHelper_Double',
  22922. LinesToStr([ // statements
  22923. 'rtl.createHelper($mod, "THelper", null, function () {',
  22924. ' this.NPI = 3.141592;',
  22925. ' this.ToStr = function () {',
  22926. ' var Result = "";',
  22927. ' return Result;',
  22928. ' };',
  22929. '});',
  22930. 'this.DoIt = function (s) {',
  22931. '};',
  22932. 'this.f = 0.0;',
  22933. '']),
  22934. LinesToStr([ // $mod.$main
  22935. '$mod.DoIt($mod.THelper.ToStr.call({',
  22936. ' p: $mod,',
  22937. ' get: function () {',
  22938. ' return this.p.f;',
  22939. ' },',
  22940. ' set: function (v) {',
  22941. ' this.p.f = v;',
  22942. ' }',
  22943. '}));',
  22944. '$mod.DoIt($mod.THelper.ToStr.call({',
  22945. ' p: $mod,',
  22946. ' get: function () {',
  22947. ' return this.p.f;',
  22948. ' },',
  22949. ' set: function (v) {',
  22950. ' this.p.f = v;',
  22951. ' }',
  22952. '}));',
  22953. '']));
  22954. end;
  22955. procedure TTestModule.TestTypeHelper_NativeInt;
  22956. begin
  22957. StartProgram(false);
  22958. Add([
  22959. '{$modeswitch typehelpers}',
  22960. 'type',
  22961. ' MaxInt = type nativeint;',
  22962. ' THelperI = type helper for MaxInt',
  22963. ' function ToStr: String;',
  22964. ' end;',
  22965. ' MaxUInt = type nativeuint;',
  22966. ' THelperU = type helper for MaxUInt',
  22967. ' function ToStr: String;',
  22968. ' end;',
  22969. 'function THelperI.ToStr: String;',
  22970. 'begin',
  22971. ' Result:=str(Self);',
  22972. 'end;',
  22973. 'function THelperU.ToStr: String;',
  22974. 'begin',
  22975. ' Result:=str(Self);',
  22976. 'end;',
  22977. 'procedure DoIt(s: string);',
  22978. 'begin',
  22979. 'end;',
  22980. 'var i: MaxInt;',
  22981. 'begin',
  22982. ' DoIt(i.toStr);',
  22983. ' DoIt(i.toStr());',
  22984. ' (i*i).toStr;',
  22985. ' DoIt((i*i).toStr);',
  22986. '']);
  22987. ConvertProgram;
  22988. CheckSource('TestTypeHelper_NativeInt',
  22989. LinesToStr([ // statements
  22990. 'rtl.createHelper($mod, "THelperI", null, function () {',
  22991. ' this.ToStr = function () {',
  22992. ' var Result = "";',
  22993. ' Result = "" + this.get();',
  22994. ' return Result;',
  22995. ' };',
  22996. '});',
  22997. 'rtl.createHelper($mod, "THelperU", null, function () {',
  22998. ' this.ToStr = function () {',
  22999. ' var Result = "";',
  23000. ' Result = "" + this.get();',
  23001. ' return Result;',
  23002. ' };',
  23003. '});',
  23004. 'this.DoIt = function (s) {',
  23005. '};',
  23006. 'this.i = 0;',
  23007. '']),
  23008. LinesToStr([ // $mod.$main
  23009. '$mod.DoIt($mod.THelperI.ToStr.call({',
  23010. ' p: $mod,',
  23011. ' get: function () {',
  23012. ' return this.p.i;',
  23013. ' },',
  23014. ' set: function (v) {',
  23015. ' this.p.i = v;',
  23016. ' }',
  23017. '}));',
  23018. '$mod.DoIt($mod.THelperI.ToStr.call({',
  23019. ' p: $mod,',
  23020. ' get: function () {',
  23021. ' return this.p.i;',
  23022. ' },',
  23023. ' set: function (v) {',
  23024. ' this.p.i = v;',
  23025. ' }',
  23026. '}));',
  23027. '$mod.THelperI.ToStr.call({',
  23028. ' a: $mod.i * $mod.i,',
  23029. ' get: function () {',
  23030. ' return this.a;',
  23031. ' },',
  23032. ' set: function (v) {',
  23033. ' rtl.raiseE("EPropReadOnly");',
  23034. ' }',
  23035. '});',
  23036. '$mod.DoIt($mod.THelperI.ToStr.call({',
  23037. ' a: $mod.i * $mod.i,',
  23038. ' get: function () {',
  23039. ' return this.a;',
  23040. ' },',
  23041. ' set: function (v) {',
  23042. ' rtl.raiseE("EPropReadOnly");',
  23043. ' }',
  23044. '}));',
  23045. '']));
  23046. end;
  23047. procedure TTestModule.TestTypeHelper_StringChar;
  23048. begin
  23049. StartProgram(false);
  23050. Add([
  23051. '{$modeswitch typehelpers}',
  23052. 'type',
  23053. ' TStringHelper = type helper for string',
  23054. ' procedure DoIt(e: byte = 123);',
  23055. ' end;',
  23056. ' TCharHelper = type helper for char',
  23057. ' procedure Fly;',
  23058. ' end;',
  23059. 'procedure TStringHelper.DoIt(e: byte);',
  23060. 'begin',
  23061. ' Self[1]:=''c'';',
  23062. ' Self[2]:=Self[3];',
  23063. 'end;',
  23064. 'procedure TCharHelper.Fly;',
  23065. 'begin',
  23066. ' Self:=''c'';',
  23067. 'end;',
  23068. 'begin',
  23069. ' ''abc''.DoIt;',
  23070. ' ''xyz''.DoIt();',
  23071. ' ''c''.Fly();',
  23072. '']);
  23073. ConvertProgram;
  23074. CheckSource('TestTypeHelper_StringChar',
  23075. LinesToStr([ // statements
  23076. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  23077. ' this.DoIt = function (e) {',
  23078. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  23079. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  23080. ' };',
  23081. '});',
  23082. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  23083. ' this.Fly = function () {',
  23084. ' this.set("c");',
  23085. ' };',
  23086. '});',
  23087. '']),
  23088. LinesToStr([ // $mod.$main
  23089. '$mod.TStringHelper.DoIt.call({',
  23090. ' get: function () {',
  23091. ' return "abc";',
  23092. ' },',
  23093. ' set: function (v) {',
  23094. ' rtl.raiseE("EPropReadOnly");',
  23095. ' }',
  23096. '}, 123);',
  23097. '$mod.TStringHelper.DoIt.call({',
  23098. ' get: function () {',
  23099. ' return "xyz";',
  23100. ' },',
  23101. ' set: function (v) {',
  23102. ' rtl.raiseE("EPropReadOnly");',
  23103. ' }',
  23104. '}, 123);',
  23105. '$mod.TCharHelper.Fly.call({',
  23106. ' get: function () {',
  23107. ' return "c";',
  23108. ' },',
  23109. ' set: function (v) {',
  23110. ' rtl.raiseE("EPropReadOnly");',
  23111. ' }',
  23112. '});',
  23113. '']));
  23114. end;
  23115. procedure TTestModule.TestTypeHelper_JSValue;
  23116. begin
  23117. StartProgram(false);
  23118. Add([
  23119. '{$modeswitch typehelpers}',
  23120. 'type',
  23121. ' TExtValue = type jsvalue;',
  23122. ' THelper = type helper for TExtValue',
  23123. ' function ToStr: String;',
  23124. ' end;',
  23125. 'function THelper.ToStr: String;',
  23126. 'begin',
  23127. 'end;',
  23128. 'var',
  23129. ' s: string;',
  23130. ' v: TExtValue;',
  23131. 'begin',
  23132. ' s:=v.toStr;',
  23133. ' s:=v.toStr();',
  23134. ' TExtValue(s).toStr;',
  23135. '']);
  23136. ConvertProgram;
  23137. CheckSource('TestTypeHelper_JSValue',
  23138. LinesToStr([ // statements
  23139. 'rtl.createHelper($mod, "THelper", null, function () {',
  23140. ' this.ToStr = function () {',
  23141. ' var Result = "";',
  23142. ' return Result;',
  23143. ' };',
  23144. '});',
  23145. 'this.s = "";',
  23146. 'this.v = undefined;',
  23147. '']),
  23148. LinesToStr([ // $mod.$main
  23149. '$mod.s = $mod.THelper.ToStr.call({',
  23150. ' p: $mod,',
  23151. ' get: function () {',
  23152. ' return this.p.v;',
  23153. ' },',
  23154. ' set: function (v) {',
  23155. ' this.p.v = v;',
  23156. ' }',
  23157. '});',
  23158. '$mod.s = $mod.THelper.ToStr.call({',
  23159. ' p: $mod,',
  23160. ' get: function () {',
  23161. ' return this.p.v;',
  23162. ' },',
  23163. ' set: function (v) {',
  23164. ' this.p.v = v;',
  23165. ' }',
  23166. '});',
  23167. '$mod.THelper.ToStr.call({',
  23168. ' p: $mod,',
  23169. ' get: function () {',
  23170. ' return this.p.s;',
  23171. ' },',
  23172. ' set: function (v) {',
  23173. ' rtl.raiseE("EPropReadOnly");',
  23174. ' }',
  23175. '});',
  23176. '']));
  23177. end;
  23178. procedure TTestModule.TestTypeHelper_Array;
  23179. begin
  23180. StartProgram(false);
  23181. Add([
  23182. '{$modeswitch typehelpers}',
  23183. 'type',
  23184. ' TArrOfBool = array of boolean;',
  23185. ' TArrOfJS = array of jsvalue;',
  23186. ' THelper = type helper for TArrOfBool',
  23187. ' procedure DoIt(e: byte = 123);',
  23188. ' end;',
  23189. 'procedure THelper.DoIt(e: byte);',
  23190. 'begin',
  23191. ' Self[1]:=true;',
  23192. ' Self[2]:=not Self[3];',
  23193. ' SetLength(Self,4);',
  23194. 'end;',
  23195. 'var',
  23196. ' b: TArrOfBool;',
  23197. ' j: TArrOfJS;',
  23198. 'begin',
  23199. ' b.DoIt;',
  23200. ' TArrOfBool(j).DoIt();',
  23201. '']);
  23202. ConvertProgram;
  23203. CheckSource('TestTypeHelper_Array',
  23204. LinesToStr([ // statements
  23205. 'rtl.createHelper($mod, "THelper", null, function () {',
  23206. ' this.DoIt = function (e) {',
  23207. ' this.get()[1] = true;',
  23208. ' this.get()[2] = !this.get()[3];',
  23209. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  23210. ' };',
  23211. '});',
  23212. 'this.b = [];',
  23213. 'this.j = [];',
  23214. '']),
  23215. LinesToStr([ // $mod.$main
  23216. '$mod.THelper.DoIt.call({',
  23217. ' p: $mod,',
  23218. ' get: function () {',
  23219. ' return this.p.b;',
  23220. ' },',
  23221. ' set: function (v) {',
  23222. ' this.p.b = v;',
  23223. ' }',
  23224. '}, 123);',
  23225. '$mod.THelper.DoIt.call({',
  23226. ' p: $mod,',
  23227. ' get: function () {',
  23228. ' return this.p.j;',
  23229. ' },',
  23230. ' set: function (v) {',
  23231. ' this.p.j = v;',
  23232. ' }',
  23233. '}, 123);',
  23234. '']));
  23235. end;
  23236. procedure TTestModule.TestTypeHelper_EnumType;
  23237. begin
  23238. StartProgram(false);
  23239. Add([
  23240. '{$modeswitch typehelpers}',
  23241. 'type',
  23242. ' TEnum = (red,blue);',
  23243. ' THelper = type helper for TEnum',
  23244. ' procedure DoIt(e: byte = 123);',
  23245. ' class procedure Swing(w: word); static;',
  23246. ' end;',
  23247. 'procedure THelper.DoIt(e: byte);',
  23248. 'begin',
  23249. ' Self:=red;',
  23250. ' Self:=succ(Self);',
  23251. ' with Self do Doit;',
  23252. 'end;',
  23253. 'class procedure THelper.Swing(w: word);',
  23254. 'begin',
  23255. 'end;',
  23256. 'var e: TEnum;',
  23257. 'begin',
  23258. ' e.DoIt;',
  23259. ' red.DoIt;',
  23260. ' TEnum.blue.DoIt;',
  23261. ' TEnum(1).DoIt;',
  23262. ' TEnum.Swing(3);',
  23263. '']);
  23264. ConvertProgram;
  23265. CheckSource('TestTypeHelper_EnumType',
  23266. LinesToStr([ // statements
  23267. 'this.TEnum = {',
  23268. ' "0": "red",',
  23269. ' red: 0,',
  23270. ' "1": "blue",',
  23271. ' blue: 1',
  23272. '};',
  23273. 'rtl.createHelper($mod, "THelper", null, function () {',
  23274. ' this.DoIt = function (e) {',
  23275. ' this.set($mod.TEnum.red);',
  23276. ' this.set(this.get() + 1);',
  23277. ' var $with1 = this.get();',
  23278. ' $mod.THelper.DoIt.call(this, 123);',
  23279. ' };',
  23280. ' this.Swing = function (w) {',
  23281. ' };',
  23282. '});',
  23283. 'this.e = 0;',
  23284. '']),
  23285. LinesToStr([ // $mod.$main
  23286. '$mod.THelper.DoIt.call({',
  23287. ' p: $mod,',
  23288. ' get: function () {',
  23289. ' return this.p.e;',
  23290. ' },',
  23291. ' set: function (v) {',
  23292. ' this.p.e = v;',
  23293. ' }',
  23294. '}, 123);',
  23295. '$mod.THelper.DoIt.call({',
  23296. ' p: $mod.TEnum,',
  23297. ' get: function () {',
  23298. ' return this.p.red;',
  23299. ' },',
  23300. ' set: function (v) {',
  23301. ' rtl.raiseE("EPropReadOnly");',
  23302. ' }',
  23303. '}, 123);',
  23304. '$mod.THelper.DoIt.call({',
  23305. ' p: $mod.TEnum,',
  23306. ' get: function () {',
  23307. ' return this.p.blue;',
  23308. ' },',
  23309. ' set: function (v) {',
  23310. ' rtl.raiseE("EPropReadOnly");',
  23311. ' }',
  23312. '}, 123);',
  23313. '$mod.THelper.DoIt.call({',
  23314. ' get: function () {',
  23315. ' return 1;',
  23316. ' },',
  23317. ' set: function (v) {',
  23318. ' rtl.raiseE("EPropReadOnly");',
  23319. ' }',
  23320. '}, 123);',
  23321. '$mod.THelper.Swing(3);',
  23322. '']));
  23323. end;
  23324. procedure TTestModule.TestTypeHelper_SetType;
  23325. begin
  23326. StartProgram(false);
  23327. Add([
  23328. '{$modeswitch typehelpers}',
  23329. 'type',
  23330. ' TEnum = (red,blue);',
  23331. ' TSetOfEnum = set of TEnum;',
  23332. ' THelper = type helper for TSetOfEnum',
  23333. ' procedure DoIt(e: byte = 123);',
  23334. ' constructor Init(e: TEnum);',
  23335. ' constructor InitEmpty;',
  23336. ' end;',
  23337. 'procedure THelper.DoIt(e: byte);',
  23338. 'begin',
  23339. ' Self:=[];',
  23340. ' Self:=[red];',
  23341. ' Include(Self,blue);',
  23342. 'end;',
  23343. 'constructor THelper.Init(e: TEnum);',
  23344. 'begin',
  23345. ' Self:=[];',
  23346. ' Self:=[e];',
  23347. ' Include(Self,blue);',
  23348. 'end;',
  23349. 'constructor THelper.InitEmpty;',
  23350. 'begin',
  23351. 'end;',
  23352. 'var s: TSetOfEnum;',
  23353. 'begin',
  23354. ' s.DoIt;',
  23355. //' [red].DoIt;',
  23356. //' with s do DoIt;',
  23357. //' with [red,blue] do DoIt;',
  23358. ' s:=TSetOfEnum.Init(blue);',
  23359. ' s:=s.Init(blue);',
  23360. '']);
  23361. ConvertProgram;
  23362. CheckSource('TestTypeHelper_SetType',
  23363. LinesToStr([ // statements
  23364. 'this.TEnum = {',
  23365. ' "0": "red",',
  23366. ' red: 0,',
  23367. ' "1": "blue",',
  23368. ' blue: 1',
  23369. '};',
  23370. 'rtl.createHelper($mod, "THelper", null, function () {',
  23371. ' this.DoIt = function (e) {',
  23372. ' this.set({});',
  23373. ' this.set(rtl.createSet($mod.TEnum.red));',
  23374. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  23375. ' };',
  23376. ' this.Init = function (e) {',
  23377. ' this.set({});',
  23378. ' this.set(rtl.createSet(e));',
  23379. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  23380. ' return this.get();',
  23381. ' };',
  23382. ' this.InitEmpty = function () {',
  23383. ' return this.get();',
  23384. ' };',
  23385. ' this.$new = function (fn, args) {',
  23386. ' return this[fn].apply({',
  23387. ' p: {},',
  23388. ' get: function () {',
  23389. ' return this.p;',
  23390. ' },',
  23391. ' set: function (v) {',
  23392. ' this.p = v;',
  23393. ' }',
  23394. ' }, args);',
  23395. ' };',
  23396. '});',
  23397. 'this.s = {};',
  23398. '']),
  23399. LinesToStr([ // $mod.$main
  23400. '$mod.THelper.DoIt.call({',
  23401. ' p: $mod,',
  23402. ' get: function () {',
  23403. ' return this.p.s;',
  23404. ' },',
  23405. ' set: function (v) {',
  23406. ' this.p.s = v;',
  23407. ' }',
  23408. '}, 123);',
  23409. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  23410. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  23411. ' p: $mod,',
  23412. ' get: function () {',
  23413. ' return this.p.s;',
  23414. ' },',
  23415. ' set: function (v) {',
  23416. ' this.p.s = v;',
  23417. ' }',
  23418. '}, $mod.TEnum.blue));',
  23419. '']));
  23420. end;
  23421. procedure TTestModule.TestTypeHelper_NestedSelf;
  23422. begin
  23423. StartProgram(false);
  23424. Add([
  23425. '{$modeswitch typehelpers}',
  23426. 'type',
  23427. ' THelper = type helper for string',
  23428. ' procedure Run(Value: string);',
  23429. ' end;',
  23430. 'procedure THelper.Run(Value: string);',
  23431. ' function Sub(i: nativeint): boolean;',
  23432. ' begin',
  23433. ' Result:=Self[i+1]=Value[i];',
  23434. ' end;',
  23435. 'begin',
  23436. ' if Self[3]=Value[4] then ;',
  23437. 'end;',
  23438. 'begin',
  23439. '']);
  23440. ConvertProgram;
  23441. CheckSource('TestTypeHelper_NestedSelf',
  23442. LinesToStr([ // statements
  23443. 'rtl.createHelper($mod, "THelper", null, function () {',
  23444. ' this.Run = function (Value) {',
  23445. ' var $Self = this;',
  23446. ' function Sub(i) {',
  23447. ' var Result = false;',
  23448. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  23449. ' return Result;',
  23450. ' };',
  23451. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  23452. ' };',
  23453. '});',
  23454. '']),
  23455. LinesToStr([ // $mod.$main
  23456. '']));
  23457. end;
  23458. procedure TTestModule.TestProcType;
  23459. begin
  23460. StartProgram(false);
  23461. Add([
  23462. 'type',
  23463. ' TProcInt = procedure(vI: longint = 1);',
  23464. 'procedure DoIt(vJ: longint);',
  23465. 'begin end;',
  23466. 'var',
  23467. ' b: boolean;',
  23468. ' vP, vQ: tprocint;',
  23469. 'begin',
  23470. ' vp:=nil;',
  23471. ' vp:=vp;',
  23472. ' vp:=@doit;',
  23473. ' vp;',
  23474. ' vp();',
  23475. ' vp(2);',
  23476. ' b:=vp=nil;',
  23477. ' b:=nil=vp;',
  23478. ' b:=vp=vq;',
  23479. ' b:=vp=@doit;',
  23480. ' b:=@doit=vp;',
  23481. ' b:=vp<>nil;',
  23482. ' b:=nil<>vp;',
  23483. ' b:=vp<>vq;',
  23484. ' b:=vp<>@doit;',
  23485. ' b:=@doit<>vp;',
  23486. ' b:=Assigned(vp);',
  23487. ' if Assigned(vp) then ;']);
  23488. ConvertProgram;
  23489. CheckSource('TestProcType',
  23490. LinesToStr([ // statements
  23491. 'this.DoIt = function(vJ) {',
  23492. '};',
  23493. 'this.b = false;',
  23494. 'this.vP = null;',
  23495. 'this.vQ = null;'
  23496. ]),
  23497. LinesToStr([ // $mod.$main
  23498. '$mod.vP = null;',
  23499. '$mod.vP = $mod.vP;',
  23500. '$mod.vP = $mod.DoIt;',
  23501. '$mod.vP(1);',
  23502. '$mod.vP(1);',
  23503. '$mod.vP(2);',
  23504. '$mod.b = $mod.vP === null;',
  23505. '$mod.b = null === $mod.vP;',
  23506. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  23507. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23508. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23509. '$mod.b = $mod.vP !== null;',
  23510. '$mod.b = null !== $mod.vP;',
  23511. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  23512. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23513. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23514. '$mod.b = $mod.vP != null;',
  23515. 'if ($mod.vP != null) ;',
  23516. '']));
  23517. end;
  23518. procedure TTestModule.TestProcType_Arg;
  23519. begin
  23520. StartProgram(false);
  23521. Add([
  23522. 'type',
  23523. ' TProcInt = procedure(vI: longint = 1);',
  23524. 'procedure DoIt(vJ: longint); begin end;',
  23525. 'procedure DoSome(vP, vQ: TProcInt);',
  23526. 'var',
  23527. ' b: boolean;',
  23528. 'begin',
  23529. ' vp:=nil;',
  23530. ' vp:=vp;',
  23531. ' vp:=@doit;',
  23532. ' vp;',
  23533. ' vp();',
  23534. ' vp(2);',
  23535. ' b:=vp=nil;',
  23536. ' b:=nil=vp;',
  23537. ' b:=vp=vq;',
  23538. ' b:=vp=@doit;',
  23539. ' b:=@doit=vp;',
  23540. ' b:=vp<>nil;',
  23541. ' b:=nil<>vp;',
  23542. ' b:=vp<>vq;',
  23543. ' b:=vp<>@doit;',
  23544. ' b:=@doit<>vp;',
  23545. ' b:=Assigned(vp);',
  23546. ' if Assigned(vp) then ;',
  23547. 'end;',
  23548. 'begin',
  23549. ' DoSome(@DoIt,nil);']);
  23550. ConvertProgram;
  23551. CheckSource('TestProcType_Arg',
  23552. LinesToStr([ // statements
  23553. 'this.DoIt = function(vJ) {',
  23554. '};',
  23555. 'this.DoSome = function(vP, vQ) {',
  23556. ' var b = false;',
  23557. ' vP = null;',
  23558. ' vP = vP;',
  23559. ' vP = $mod.DoIt;',
  23560. ' vP(1);',
  23561. ' vP(1);',
  23562. ' vP(2);',
  23563. ' b = vP === null;',
  23564. ' b = null === vP;',
  23565. ' b = rtl.eqCallback(vP,vQ);',
  23566. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  23567. ' b = rtl.eqCallback($mod.DoIt, vP);',
  23568. ' b = vP !== null;',
  23569. ' b = null !== vP;',
  23570. ' b = !rtl.eqCallback(vP, vQ);',
  23571. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  23572. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  23573. ' b = vP != null;',
  23574. ' if (vP != null) ;',
  23575. '};',
  23576. '']),
  23577. LinesToStr([ // $mod.$main
  23578. '$mod.DoSome($mod.DoIt,null);',
  23579. '']));
  23580. end;
  23581. procedure TTestModule.TestProcType_FunctionFPC;
  23582. begin
  23583. StartProgram(false);
  23584. Add('type');
  23585. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23586. Add('function DoIt(vI: longint): longint;');
  23587. Add('begin end;');
  23588. Add('var');
  23589. Add(' b: boolean;');
  23590. Add(' vP, vQ: tfuncint;');
  23591. Add('begin');
  23592. Add(' vp:=nil;');
  23593. Add(' vp:=vp;');
  23594. Add(' vp:=@doit;'); // ok in fpc and delphi
  23595. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23596. Add(' vp;'); // ok in fpc and delphi
  23597. Add(' vp();');
  23598. Add(' vp(2);');
  23599. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23600. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23601. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23602. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23603. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23604. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23605. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23606. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23607. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23608. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23609. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23610. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23611. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23612. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23613. Add(' b:=Assigned(vp);');
  23614. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23615. Add(' doit(vp());'); // ok in fpc and delphi
  23616. Add(' doit(vp(2));'); // ok in fpc and delphi
  23617. ConvertProgram;
  23618. CheckSource('TestProcType_FunctionFPC',
  23619. LinesToStr([ // statements
  23620. 'this.DoIt = function(vI) {',
  23621. ' var Result = 0;',
  23622. ' return Result;',
  23623. '};',
  23624. 'this.b = false;',
  23625. 'this.vP = null;',
  23626. 'this.vQ = null;'
  23627. ]),
  23628. LinesToStr([ // $mod.$main
  23629. '$mod.vP = null;',
  23630. '$mod.vP = $mod.vP;',
  23631. '$mod.vP = $mod.DoIt;',
  23632. '$mod.vP(1);',
  23633. '$mod.vP(1);',
  23634. '$mod.vP(2);',
  23635. '$mod.b = $mod.vP === null;',
  23636. '$mod.b = null === $mod.vP;',
  23637. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  23638. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23639. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23640. '$mod.b = 4 === $mod.vP(1);',
  23641. '$mod.b = $mod.vP !== null;',
  23642. '$mod.b = null !== $mod.vP;',
  23643. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  23644. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23645. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23646. '$mod.b = 6 !== $mod.vP(1);',
  23647. '$mod.b = $mod.vP != null;',
  23648. '$mod.DoIt($mod.vP(1));',
  23649. '$mod.DoIt($mod.vP(2));',
  23650. '']));
  23651. end;
  23652. procedure TTestModule.TestProcType_FunctionDelphi;
  23653. begin
  23654. StartProgram(false);
  23655. Add('{$mode Delphi}');
  23656. Add('type');
  23657. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23658. Add('function DoIt(vI: longint): longint;');
  23659. Add('begin end;');
  23660. Add('var');
  23661. Add(' b: boolean;');
  23662. Add(' vP, vQ: tfuncint;');
  23663. Add('begin');
  23664. Add(' vp:=nil;');
  23665. Add(' vp:=vp;');
  23666. Add(' vp:=@doit;'); // ok in fpc and delphi
  23667. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23668. Add(' vp;'); // ok in fpc and delphi
  23669. Add(' vp();');
  23670. Add(' vp(2);');
  23671. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23672. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23673. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23674. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23675. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23676. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23677. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23678. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23679. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23680. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23681. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23682. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23683. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23684. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23685. Add(' b:=Assigned(vp);');
  23686. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23687. Add(' doit(vp());'); // ok in fpc and delphi
  23688. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  23689. ConvertProgram;
  23690. CheckSource('TestProcType_FunctionDelphi',
  23691. LinesToStr([ // statements
  23692. 'this.DoIt = function(vI) {',
  23693. ' var Result = 0;',
  23694. ' return Result;',
  23695. '};',
  23696. 'this.b = false;',
  23697. 'this.vP = null;',
  23698. 'this.vQ = null;'
  23699. ]),
  23700. LinesToStr([ // $mod.$main
  23701. '$mod.vP = null;',
  23702. '$mod.vP = $mod.vP;',
  23703. '$mod.vP = $mod.DoIt;',
  23704. '$mod.vP = $mod.DoIt;',
  23705. '$mod.vP(1);',
  23706. '$mod.vP(1);',
  23707. '$mod.vP(2);',
  23708. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  23709. '$mod.b = $mod.vP(1) === 3;',
  23710. '$mod.b = 4 === $mod.vP(1);',
  23711. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  23712. '$mod.b = $mod.vP(1) !== 5;',
  23713. '$mod.b = 6 !== $mod.vP(1);',
  23714. '$mod.b = $mod.vP != null;',
  23715. '$mod.DoIt($mod.vP(1));',
  23716. '$mod.DoIt($mod.vP(1));',
  23717. '$mod.DoIt($mod.vP(2));',
  23718. '']));
  23719. end;
  23720. procedure TTestModule.TestProcType_ProcedureDelphi;
  23721. begin
  23722. StartProgram(false);
  23723. Add('{$mode Delphi}');
  23724. Add('type');
  23725. Add(' TProc = procedure;');
  23726. Add('procedure DoIt;');
  23727. Add('begin end;');
  23728. Add('var');
  23729. Add(' b: boolean;');
  23730. Add(' vP, vQ: tproc;');
  23731. Add('begin');
  23732. Add(' vp:=nil;');
  23733. Add(' vp:=vp;');
  23734. Add(' vp:=vq;');
  23735. 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
  23736. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23737. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  23738. Add(' vp;'); // ok in fpc and delphi
  23739. Add(' vp();');
  23740. // equal
  23741. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23742. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  23743. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23744. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23745. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  23746. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23747. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23748. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  23749. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23750. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23751. // unequal
  23752. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23753. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  23754. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23755. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  23756. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23757. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  23758. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23759. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  23760. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23761. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  23762. Add(' b:=Assigned(vp);');
  23763. ConvertProgram;
  23764. CheckSource('TestProcType_ProcedureDelphi',
  23765. LinesToStr([ // statements
  23766. 'this.DoIt = function() {',
  23767. '};',
  23768. 'this.b = false;',
  23769. 'this.vP = null;',
  23770. 'this.vQ = null;'
  23771. ]),
  23772. LinesToStr([ // $mod.$main
  23773. '$mod.vP = null;',
  23774. '$mod.vP = $mod.vP;',
  23775. '$mod.vP = $mod.vQ;',
  23776. '$mod.vP = $mod.DoIt;',
  23777. '$mod.vP = $mod.DoIt;',
  23778. '$mod.vP();',
  23779. '$mod.vP();',
  23780. '$mod.b = $mod.vP === null;',
  23781. '$mod.b = null === $mod.vP;',
  23782. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  23783. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23784. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23785. '$mod.b = $mod.vP !== null;',
  23786. '$mod.b = null !== $mod.vP;',
  23787. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  23788. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23789. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23790. '$mod.b = $mod.vP != null;',
  23791. '']));
  23792. end;
  23793. procedure TTestModule.TestProcType_AsParam;
  23794. begin
  23795. StartProgram(false);
  23796. Add('type');
  23797. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23798. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  23799. Add('var vJ: tfuncint;');
  23800. Add('begin');
  23801. Add(' vg:=vg;');
  23802. Add(' vj:=vh;');
  23803. Add(' vi:=vi;');
  23804. Add(' doit(vg,vg,vg);');
  23805. Add(' doit(vh,vh,vj);');
  23806. Add(' doit(vi,vi,vi);');
  23807. Add(' doit(vj,vj,vj);');
  23808. Add('end;');
  23809. Add('var i: tfuncint;');
  23810. Add('begin');
  23811. Add(' doit(i,i,i);');
  23812. ConvertProgram;
  23813. CheckSource('TestProcType_AsParam',
  23814. LinesToStr([ // statements
  23815. 'this.DoIt = function (vG,vH,vI) {',
  23816. ' var vJ = null;',
  23817. ' vG = vG;',
  23818. ' vJ = vH;',
  23819. ' vI.set(vI.get());',
  23820. ' $mod.DoIt(vG, vG, {',
  23821. ' get: function () {',
  23822. ' return vG;',
  23823. ' },',
  23824. ' set: function (v) {',
  23825. ' vG = v;',
  23826. ' }',
  23827. ' });',
  23828. ' $mod.DoIt(vH, vH, {',
  23829. ' get: function () {',
  23830. ' return vJ;',
  23831. ' },',
  23832. ' set: function (v) {',
  23833. ' vJ = v;',
  23834. ' }',
  23835. ' });',
  23836. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  23837. ' $mod.DoIt(vJ, vJ, {',
  23838. ' get: function () {',
  23839. ' return vJ;',
  23840. ' },',
  23841. ' set: function (v) {',
  23842. ' vJ = v;',
  23843. ' }',
  23844. ' });',
  23845. '};',
  23846. 'this.i = null;'
  23847. ]),
  23848. LinesToStr([
  23849. '$mod.DoIt($mod.i,$mod.i,{',
  23850. ' p: $mod,',
  23851. ' get: function () {',
  23852. ' return this.p.i;',
  23853. ' },',
  23854. ' set: function (v) {',
  23855. ' this.p.i = v;',
  23856. ' }',
  23857. '});'
  23858. ]));
  23859. end;
  23860. procedure TTestModule.TestProcType_MethodFPC;
  23861. begin
  23862. StartProgram(false);
  23863. Add('type');
  23864. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23865. Add(' TObject = class');
  23866. Add(' function DoIt(vA: longint = 1): longint;');
  23867. Add(' end;');
  23868. Add('function TObject.DoIt(vA: longint = 1): longint;');
  23869. Add('begin');
  23870. Add('end;');
  23871. Add('var');
  23872. Add(' Obj: TObject;');
  23873. Add(' vP: tfuncint;');
  23874. Add(' b: boolean;');
  23875. Add('begin');
  23876. Add(' vp:[email protected];'); // ok in fpc and delphi
  23877. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  23878. Add(' vp;'); // ok in fpc and delphi
  23879. Add(' vp();');
  23880. Add(' vp(2);');
  23881. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  23882. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  23883. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  23884. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  23885. ConvertProgram;
  23886. CheckSource('TestProcType_MethodFPC',
  23887. LinesToStr([ // statements
  23888. 'rtl.createClass($mod, "TObject", null, function () {',
  23889. ' this.$init = function () {',
  23890. ' };',
  23891. ' this.$final = function () {',
  23892. ' };',
  23893. ' this.DoIt = function (vA) {',
  23894. ' var Result = 0;',
  23895. ' return Result;',
  23896. ' };',
  23897. '});',
  23898. 'this.Obj = null;',
  23899. 'this.vP = null;',
  23900. 'this.b = false;'
  23901. ]),
  23902. LinesToStr([
  23903. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23904. '$mod.vP(1);',
  23905. '$mod.vP(1);',
  23906. '$mod.vP(2);',
  23907. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  23908. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  23909. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  23910. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  23911. '']));
  23912. end;
  23913. procedure TTestModule.TestProcType_MethodDelphi;
  23914. begin
  23915. StartProgram(false);
  23916. Add('{$mode delphi}');
  23917. Add('type');
  23918. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23919. Add(' TObject = class');
  23920. Add(' function DoIt(vA: longint = 1): longint;');
  23921. Add(' end;');
  23922. Add('function TObject.DoIt(vA: longint = 1): longint;');
  23923. Add('begin');
  23924. Add('end;');
  23925. Add('var');
  23926. Add(' Obj: TObject;');
  23927. Add(' vP: tfuncint;');
  23928. Add(' b: boolean;');
  23929. Add('begin');
  23930. Add(' vp:[email protected];'); // ok in fpc and delphi
  23931. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  23932. Add(' vp;'); // ok in fpc and delphi
  23933. Add(' vp();');
  23934. Add(' vp(2);');
  23935. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  23936. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  23937. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  23938. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  23939. ConvertProgram;
  23940. CheckSource('TestProcType_MethodDelphi',
  23941. LinesToStr([ // statements
  23942. 'rtl.createClass($mod, "TObject", null, function () {',
  23943. ' this.$init = function () {',
  23944. ' };',
  23945. ' this.$final = function () {',
  23946. ' };',
  23947. ' this.DoIt = function (vA) {',
  23948. ' var Result = 0;',
  23949. ' return Result;',
  23950. ' };',
  23951. '});',
  23952. 'this.Obj = null;',
  23953. 'this.vP = null;',
  23954. 'this.b = false;'
  23955. ]),
  23956. LinesToStr([
  23957. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23958. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  23959. '$mod.vP(1);',
  23960. '$mod.vP(1);',
  23961. '$mod.vP(2);',
  23962. '']));
  23963. end;
  23964. procedure TTestModule.TestProcType_PropertyFPC;
  23965. begin
  23966. StartProgram(false);
  23967. Add('type');
  23968. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  23969. Add(' TObject = class');
  23970. Add(' FOnFoo: TFuncInt;');
  23971. Add(' function DoIt(vA: longint = 1): longint;');
  23972. Add(' function GetFoo: TFuncInt;');
  23973. Add(' procedure SetFoo(const Value: TFuncInt);');
  23974. Add(' function GetEvents(Index: longint): TFuncInt;');
  23975. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  23976. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  23977. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  23978. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  23979. Add(' end;');
  23980. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  23981. Add('function tobject.getfoo: tfuncint; begin end;');
  23982. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  23983. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  23984. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  23985. Add('var');
  23986. Add(' Obj: TObject;');
  23987. Add(' vP: tfuncint;');
  23988. Add(' b: boolean;');
  23989. Add('begin');
  23990. Add(' obj.onfoo:=nil;');
  23991. Add(' obj.onbar:=nil;');
  23992. Add(' obj.events[1]:=nil;');
  23993. Add(' obj.onfoo:=obj.onfoo;');
  23994. Add(' obj.onbar:=obj.onbar;');
  23995. Add(' obj.events[2]:=obj.events[3];');
  23996. Add(' obj.onfoo:[email protected];');
  23997. Add(' obj.onbar:[email protected];');
  23998. Add(' obj.events[4]:[email protected];');
  23999. //Add(' obj.onfoo:=obj.doit;'); // delphi
  24000. //Add(' obj.onbar:=obj.doit;'); // delphi
  24001. //Add(' obj.events[4]:=obj.doit;'); // delphi
  24002. Add(' obj.onfoo;');
  24003. Add(' obj.onbar;');
  24004. //Add(' obj.events[5];'); ToDo in pasresolver
  24005. Add(' obj.onfoo();');
  24006. Add(' obj.onbar();');
  24007. Add(' obj.events[6]();');
  24008. Add(' b:=obj.onfoo=nil;');
  24009. Add(' b:=obj.onbar=nil;');
  24010. Add(' b:=obj.events[7]=nil;');
  24011. Add(' b:=obj.onfoo<>nil;');
  24012. Add(' b:=obj.onbar<>nil;');
  24013. Add(' b:=obj.events[8]<>nil;');
  24014. Add(' b:=obj.onfoo=vp;');
  24015. Add(' b:=obj.onbar=vp;');
  24016. Add(' b:=obj.events[9]=vp;');
  24017. Add(' b:=obj.onfoo=obj.onfoo;');
  24018. Add(' b:=obj.onbar=obj.onfoo;');
  24019. Add(' b:=obj.events[10]=obj.onfoo;');
  24020. Add(' b:=obj.onfoo<>obj.onfoo;');
  24021. Add(' b:=obj.onbar<>obj.onfoo;');
  24022. Add(' b:=obj.events[11]<>obj.onfoo;');
  24023. Add(' b:[email protected];');
  24024. Add(' b:[email protected];');
  24025. Add(' b:=obj.events[12][email protected];');
  24026. Add(' b:=obj.onfoo<>@obj.doit;');
  24027. Add(' b:=obj.onbar<>@obj.doit;');
  24028. Add(' b:=obj.events[12]<>@obj.doit;');
  24029. Add(' b:=Assigned(obj.onfoo);');
  24030. Add(' b:=Assigned(obj.onbar);');
  24031. Add(' b:=Assigned(obj.events[13]);');
  24032. ConvertProgram;
  24033. CheckSource('TestProcType_PropertyFPC',
  24034. LinesToStr([ // statements
  24035. 'rtl.createClass($mod, "TObject", null, function () {',
  24036. ' this.$init = function () {',
  24037. ' this.FOnFoo = null;',
  24038. ' };',
  24039. ' this.$final = function () {',
  24040. ' this.FOnFoo = undefined;',
  24041. ' };',
  24042. ' this.DoIt = function (vA) {',
  24043. ' var Result = 0;',
  24044. ' return Result;',
  24045. ' };',
  24046. 'this.GetFoo = function () {',
  24047. ' var Result = null;',
  24048. ' return Result;',
  24049. '};',
  24050. 'this.SetFoo = function (Value) {',
  24051. '};',
  24052. 'this.GetEvents = function (Index) {',
  24053. ' var Result = null;',
  24054. ' return Result;',
  24055. '};',
  24056. 'this.SetEvents = function (Index, Value) {',
  24057. '};',
  24058. '});',
  24059. 'this.Obj = null;',
  24060. 'this.vP = null;',
  24061. 'this.b = false;'
  24062. ]),
  24063. LinesToStr([
  24064. '$mod.Obj.FOnFoo = null;',
  24065. '$mod.Obj.SetFoo(null);',
  24066. '$mod.Obj.SetEvents(1, null);',
  24067. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  24068. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  24069. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  24070. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24071. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24072. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24073. '$mod.Obj.FOnFoo(1);',
  24074. '$mod.Obj.GetFoo();',
  24075. '$mod.Obj.FOnFoo(1);',
  24076. '$mod.Obj.GetFoo()(1);',
  24077. '$mod.Obj.GetEvents(6)(1);',
  24078. '$mod.b = $mod.Obj.FOnFoo === null;',
  24079. '$mod.b = $mod.Obj.GetFoo() === null;',
  24080. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  24081. '$mod.b = $mod.Obj.FOnFoo !== null;',
  24082. '$mod.b = $mod.Obj.GetFoo() !== null;',
  24083. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  24084. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  24085. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  24086. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  24087. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  24088. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  24089. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  24090. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  24091. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  24092. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  24093. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  24094. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  24095. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  24096. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  24097. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  24098. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  24099. '$mod.b = $mod.Obj.FOnFoo != null;',
  24100. '$mod.b = $mod.Obj.GetFoo() != null;',
  24101. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  24102. '']));
  24103. end;
  24104. procedure TTestModule.TestProcType_PropertyDelphi;
  24105. begin
  24106. StartProgram(false);
  24107. Add('{$mode delphi}');
  24108. Add('type');
  24109. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24110. Add(' TObject = class');
  24111. Add(' FOnFoo: TFuncInt;');
  24112. Add(' function DoIt(vA: longint = 1): longint;');
  24113. Add(' function GetFoo: TFuncInt;');
  24114. Add(' procedure SetFoo(const Value: TFuncInt);');
  24115. Add(' function GetEvents(Index: longint): TFuncInt;');
  24116. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  24117. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24118. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24119. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  24120. Add(' end;');
  24121. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24122. Add('function tobject.getfoo: tfuncint; begin end;');
  24123. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24124. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  24125. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  24126. Add('var');
  24127. Add(' Obj: TObject;');
  24128. Add(' vP: tfuncint;');
  24129. Add(' b: boolean;');
  24130. Add('begin');
  24131. Add(' obj.onfoo:=nil;');
  24132. Add(' obj.onbar:=nil;');
  24133. Add(' obj.events[1]:=nil;');
  24134. Add(' obj.onfoo:=obj.onfoo;');
  24135. Add(' obj.onbar:=obj.onbar;');
  24136. Add(' obj.events[2]:=obj.events[3];');
  24137. Add(' obj.onfoo:[email protected];');
  24138. Add(' obj.onbar:[email protected];');
  24139. Add(' obj.events[4]:[email protected];');
  24140. Add(' obj.onfoo:=obj.doit;'); // delphi
  24141. Add(' obj.onbar:=obj.doit;'); // delphi
  24142. Add(' obj.events[4]:=obj.doit;'); // delphi
  24143. Add(' obj.onfoo;');
  24144. Add(' obj.onbar;');
  24145. //Add(' obj.events[5];'); ToDo in pasresolver
  24146. Add(' obj.onfoo();');
  24147. Add(' obj.onbar();');
  24148. Add(' obj.events[6]();');
  24149. //Add(' b:=obj.onfoo=nil;'); // fpc
  24150. //Add(' b:=obj.onbar=nil;'); // fpc
  24151. //Add(' b:=obj.events[7]=nil;'); // fpc
  24152. //Add(' b:=obj.onfoo<>nil;'); // fpc
  24153. //Add(' b:=obj.onbar<>nil;'); // fpc
  24154. //Add(' b:=obj.events[8]<>nil;'); // fpc
  24155. Add(' b:=obj.onfoo=vp;');
  24156. Add(' b:=obj.onbar=vp;');
  24157. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  24158. Add(' b:=obj.onfoo=obj.onfoo;');
  24159. Add(' b:=obj.onbar=obj.onfoo;');
  24160. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  24161. Add(' b:=obj.onfoo<>obj.onfoo;');
  24162. Add(' b:=obj.onbar<>obj.onfoo;');
  24163. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  24164. //Add(' b:[email protected];'); // fpc
  24165. //Add(' b:[email protected];'); // fpc
  24166. //Add(' b:=obj.events[12][email protected];'); // fpc
  24167. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  24168. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  24169. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  24170. Add(' b:=Assigned(obj.onfoo);');
  24171. Add(' b:=Assigned(obj.onbar);');
  24172. Add(' b:=Assigned(obj.events[13]);');
  24173. ConvertProgram;
  24174. CheckSource('TestProcType_PropertyDelphi',
  24175. LinesToStr([ // statements
  24176. 'rtl.createClass($mod, "TObject", null, function () {',
  24177. ' this.$init = function () {',
  24178. ' this.FOnFoo = null;',
  24179. ' };',
  24180. ' this.$final = function () {',
  24181. ' this.FOnFoo = undefined;',
  24182. ' };',
  24183. ' this.DoIt = function (vA) {',
  24184. ' var Result = 0;',
  24185. ' return Result;',
  24186. ' };',
  24187. 'this.GetFoo = function () {',
  24188. ' var Result = null;',
  24189. ' return Result;',
  24190. '};',
  24191. 'this.SetFoo = function (Value) {',
  24192. '};',
  24193. 'this.GetEvents = function (Index) {',
  24194. ' var Result = null;',
  24195. ' return Result;',
  24196. '};',
  24197. 'this.SetEvents = function (Index, Value) {',
  24198. '};',
  24199. '});',
  24200. 'this.Obj = null;',
  24201. 'this.vP = null;',
  24202. 'this.b = false;'
  24203. ]),
  24204. LinesToStr([
  24205. '$mod.Obj.FOnFoo = null;',
  24206. '$mod.Obj.SetFoo(null);',
  24207. '$mod.Obj.SetEvents(1, null);',
  24208. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  24209. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  24210. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  24211. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24212. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24213. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24214. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24215. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24216. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24217. '$mod.Obj.FOnFoo(1);',
  24218. '$mod.Obj.GetFoo();',
  24219. '$mod.Obj.FOnFoo(1);',
  24220. '$mod.Obj.GetFoo()(1);',
  24221. '$mod.Obj.GetEvents(6)(1);',
  24222. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  24223. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  24224. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  24225. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  24226. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  24227. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  24228. '$mod.b = $mod.Obj.FOnFoo != null;',
  24229. '$mod.b = $mod.Obj.GetFoo() != null;',
  24230. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  24231. '']));
  24232. end;
  24233. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  24234. begin
  24235. StartProgram(false);
  24236. Add('type');
  24237. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24238. Add(' TObject = class');
  24239. Add(' FOnFoo: TFuncInt;');
  24240. Add(' function DoIt(vA: longint = 1): longint;');
  24241. Add(' function GetFoo: TFuncInt;');
  24242. Add(' procedure SetFoo(const Value: TFuncInt);');
  24243. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24244. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24245. Add(' end;');
  24246. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24247. Add('function tobject.getfoo: tfuncint; begin end;');
  24248. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24249. Add('var');
  24250. Add(' Obj: TObject;');
  24251. Add(' vP: tfuncint;');
  24252. Add(' b: boolean;');
  24253. Add('begin');
  24254. Add('with obj do begin');
  24255. Add(' fonfoo:=nil;');
  24256. Add(' onfoo:=nil;');
  24257. Add(' onbar:=nil;');
  24258. Add(' fonfoo:=fonfoo;');
  24259. Add(' onfoo:=onfoo;');
  24260. Add(' onbar:=onbar;');
  24261. Add(' fonfoo:=@doit;');
  24262. Add(' onfoo:=@doit;');
  24263. Add(' onbar:=@doit;');
  24264. //Add(' fonfoo:=doit;'); // delphi
  24265. //Add(' onfoo:=doit;'); // delphi
  24266. //Add(' onbar:=doit;'); // delphi
  24267. Add(' fonfoo;');
  24268. Add(' onfoo;');
  24269. Add(' onbar;');
  24270. Add(' fonfoo();');
  24271. Add(' onfoo();');
  24272. Add(' onbar();');
  24273. Add(' b:=fonfoo=nil;');
  24274. Add(' b:=onfoo=nil;');
  24275. Add(' b:=onbar=nil;');
  24276. Add(' b:=fonfoo<>nil;');
  24277. Add(' b:=onfoo<>nil;');
  24278. Add(' b:=onbar<>nil;');
  24279. Add(' b:=fonfoo=vp;');
  24280. Add(' b:=onfoo=vp;');
  24281. Add(' b:=onbar=vp;');
  24282. Add(' b:=fonfoo=fonfoo;');
  24283. Add(' b:=onfoo=onfoo;');
  24284. Add(' b:=onbar=onfoo;');
  24285. Add(' b:=fonfoo<>fonfoo;');
  24286. Add(' b:=onfoo<>onfoo;');
  24287. Add(' b:=onbar<>onfoo;');
  24288. Add(' b:=fonfoo=@doit;');
  24289. Add(' b:=onfoo=@doit;');
  24290. Add(' b:=onbar=@doit;');
  24291. Add(' b:=fonfoo<>@doit;');
  24292. Add(' b:=onfoo<>@doit;');
  24293. Add(' b:=onbar<>@doit;');
  24294. Add(' b:=Assigned(fonfoo);');
  24295. Add(' b:=Assigned(onfoo);');
  24296. Add(' b:=Assigned(onbar);');
  24297. Add('end;');
  24298. ConvertProgram;
  24299. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  24300. LinesToStr([ // statements
  24301. 'rtl.createClass($mod, "TObject", null, function () {',
  24302. ' this.$init = function () {',
  24303. ' this.FOnFoo = null;',
  24304. ' };',
  24305. ' this.$final = function () {',
  24306. ' this.FOnFoo = undefined;',
  24307. ' };',
  24308. ' this.DoIt = function (vA) {',
  24309. ' var Result = 0;',
  24310. ' return Result;',
  24311. ' };',
  24312. ' this.GetFoo = function () {',
  24313. ' var Result = null;',
  24314. ' return Result;',
  24315. ' };',
  24316. ' this.SetFoo = function (Value) {',
  24317. ' };',
  24318. '});',
  24319. 'this.Obj = null;',
  24320. 'this.vP = null;',
  24321. 'this.b = false;'
  24322. ]),
  24323. LinesToStr([
  24324. 'var $with1 = $mod.Obj;',
  24325. '$with1.FOnFoo = null;',
  24326. '$with1.FOnFoo = null;',
  24327. '$with1.SetFoo(null);',
  24328. '$with1.FOnFoo = $with1.FOnFoo;',
  24329. '$with1.FOnFoo = $with1.FOnFoo;',
  24330. '$with1.SetFoo($with1.GetFoo());',
  24331. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  24332. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  24333. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  24334. '$with1.FOnFoo(1);',
  24335. '$with1.FOnFoo(1);',
  24336. '$with1.GetFoo();',
  24337. '$with1.FOnFoo(1);',
  24338. '$with1.FOnFoo(1);',
  24339. '$with1.GetFoo()(1);',
  24340. '$mod.b = $with1.FOnFoo === null;',
  24341. '$mod.b = $with1.FOnFoo === null;',
  24342. '$mod.b = $with1.GetFoo() === null;',
  24343. '$mod.b = $with1.FOnFoo !== null;',
  24344. '$mod.b = $with1.FOnFoo !== null;',
  24345. '$mod.b = $with1.GetFoo() !== null;',
  24346. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  24347. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  24348. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  24349. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24350. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24351. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  24352. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24353. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24354. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  24355. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24356. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24357. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  24358. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24359. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24360. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  24361. '$mod.b = $with1.FOnFoo != null;',
  24362. '$mod.b = $with1.FOnFoo != null;',
  24363. '$mod.b = $with1.GetFoo() != null;',
  24364. '']));
  24365. end;
  24366. procedure TTestModule.TestProcType_Nested;
  24367. begin
  24368. StartProgram(false);
  24369. Add([
  24370. 'type',
  24371. ' TProcInt = procedure(vI: longint = 1);',
  24372. 'procedure DoIt(vJ: longint);',
  24373. 'var aProc: TProcInt;',
  24374. ' b: boolean;',
  24375. ' procedure Sub(vK: longint);',
  24376. ' var aSub: TProcInt;',
  24377. ' procedure SubSub(vK: longint);',
  24378. ' var aSubSub: TProcInt;',
  24379. ' begin;',
  24380. ' aProc:=@DoIt;',
  24381. ' aSub:=@DoIt;',
  24382. ' aSubSub:=@DoIt;',
  24383. ' aProc:=@Sub;',
  24384. ' aSub:=@Sub;',
  24385. ' aSubSub:=@Sub;',
  24386. ' aProc:=@SubSub;',
  24387. ' aSub:=@SubSub;',
  24388. ' aSubSub:=@SubSub;',
  24389. ' end;',
  24390. ' begin;',
  24391. ' end;',
  24392. 'begin;',
  24393. ' aProc:=@Sub;',
  24394. ' b:=aProc=@Sub;',
  24395. ' b:=@Sub=aProc;',
  24396. 'end;',
  24397. 'begin',
  24398. '']);
  24399. ConvertProgram;
  24400. CheckSource('TestProcType_Nested',
  24401. LinesToStr([ // statements
  24402. 'this.DoIt = function (vJ) {',
  24403. ' var aProc = null;',
  24404. ' var b = false;',
  24405. ' function Sub(vK) {',
  24406. ' var aSub = null;',
  24407. ' function SubSub(vK) {',
  24408. ' var aSubSub = null;',
  24409. ' aProc = $mod.DoIt;',
  24410. ' aSub = $mod.DoIt;',
  24411. ' aSubSub = $mod.DoIt;',
  24412. ' aProc = Sub;',
  24413. ' aSub = Sub;',
  24414. ' aSubSub = Sub;',
  24415. ' aProc = SubSub;',
  24416. ' aSub = SubSub;',
  24417. ' aSubSub = SubSub;',
  24418. ' };',
  24419. ' };',
  24420. ' aProc = Sub;',
  24421. ' b = rtl.eqCallback(aProc, Sub);',
  24422. ' b = rtl.eqCallback(Sub, aProc);',
  24423. '};',
  24424. '']),
  24425. LinesToStr([ // $mod.$main
  24426. '']));
  24427. end;
  24428. procedure TTestModule.TestProcType_NestedOfObject;
  24429. begin
  24430. StartProgram(false);
  24431. Add([
  24432. 'type',
  24433. ' TProcInt = procedure(vI: longint = 1) of object;',
  24434. ' TObject = class',
  24435. ' procedure DoIt(vJ: longint);',
  24436. ' end;',
  24437. 'procedure TObject.DoIt(vJ: longint);',
  24438. 'var aProc: TProcInt;',
  24439. ' b: boolean;',
  24440. ' procedure Sub(vK: longint);',
  24441. ' var aSub: TProcInt;',
  24442. ' procedure SubSub(vK: longint);',
  24443. ' var aSubSub: TProcInt;',
  24444. ' begin;',
  24445. ' aProc:=@DoIt;',
  24446. ' aSub:=@DoIt;',
  24447. ' aSubSub:=@DoIt;',
  24448. ' aProc:=@Sub;',
  24449. ' aSub:=@Sub;',
  24450. ' aSubSub:=@Sub;',
  24451. ' aProc:=@SubSub;',
  24452. ' aSub:=@SubSub;',
  24453. ' aSubSub:=@SubSub;',
  24454. ' end;',
  24455. ' begin;',
  24456. ' end;',
  24457. 'begin;',
  24458. ' aProc:=@Sub;',
  24459. ' b:=aProc=@Sub;',
  24460. ' b:=@Sub=aProc;',
  24461. 'end;',
  24462. 'begin',
  24463. '']);
  24464. ConvertProgram;
  24465. CheckSource('TestProcType_Nested',
  24466. LinesToStr([ // statements
  24467. 'rtl.createClass($mod, "TObject", null, function () {',
  24468. ' this.$init = function () {',
  24469. ' };',
  24470. ' this.$final = function () {',
  24471. ' };',
  24472. ' this.DoIt = function (vJ) {',
  24473. ' var $Self = this;',
  24474. ' var aProc = null;',
  24475. ' var b = false;',
  24476. ' function Sub(vK) {',
  24477. ' var aSub = null;',
  24478. ' function SubSub(vK) {',
  24479. ' var aSubSub = null;',
  24480. ' aProc = rtl.createCallback($Self, "DoIt");',
  24481. ' aSub = rtl.createCallback($Self, "DoIt");',
  24482. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  24483. ' aProc = Sub;',
  24484. ' aSub = Sub;',
  24485. ' aSubSub = Sub;',
  24486. ' aProc = SubSub;',
  24487. ' aSub = SubSub;',
  24488. ' aSubSub = SubSub;',
  24489. ' };',
  24490. ' };',
  24491. ' aProc = Sub;',
  24492. ' b = rtl.eqCallback(aProc, Sub);',
  24493. ' b = rtl.eqCallback(Sub, aProc);',
  24494. ' };',
  24495. '});',
  24496. '']),
  24497. LinesToStr([ // $mod.$main
  24498. '']));
  24499. end;
  24500. procedure TTestModule.TestProcType_ReferenceToProc;
  24501. begin
  24502. StartProgram(false);
  24503. Add([
  24504. 'type',
  24505. ' TProcRef = reference to procedure(i: longint = 0);',
  24506. ' TFuncRef = reference to function(i: longint = 0): longint;',
  24507. 'var',
  24508. ' p: TProcRef;',
  24509. ' f: TFuncRef;',
  24510. 'procedure DoIt(i: longint);',
  24511. 'begin',
  24512. 'end;',
  24513. 'function GetIt(i: longint): longint;',
  24514. 'begin',
  24515. ' p:=@DoIt;',
  24516. ' f:=@GetIt;',
  24517. ' f;',
  24518. ' f();',
  24519. ' f(1);',
  24520. 'end;',
  24521. 'begin',
  24522. ' p:=@DoIt;',
  24523. ' f:=@GetIt;',
  24524. ' f;',
  24525. ' f();',
  24526. ' f(1);',
  24527. ' p:=TProcRef(f);',
  24528. '']);
  24529. ConvertProgram;
  24530. CheckSource('TestProcType_ReferenceToProc',
  24531. LinesToStr([ // statements
  24532. 'this.p = null;',
  24533. 'this.f = null;',
  24534. 'this.DoIt = function (i) {',
  24535. '};',
  24536. 'this.GetIt = function (i) {',
  24537. ' var Result = 0;',
  24538. ' $mod.p = $mod.DoIt;',
  24539. ' $mod.f = $mod.GetIt;',
  24540. ' $mod.f(0);',
  24541. ' $mod.f(0);',
  24542. ' $mod.f(1);',
  24543. ' return Result;',
  24544. '};',
  24545. '']),
  24546. LinesToStr([ // $mod.$main
  24547. '$mod.p = $mod.DoIt;',
  24548. '$mod.f = $mod.GetIt;',
  24549. '$mod.f(0);',
  24550. '$mod.f(0);',
  24551. '$mod.f(1);',
  24552. '$mod.p = $mod.f;',
  24553. '']));
  24554. end;
  24555. procedure TTestModule.TestProcType_ReferenceToMethod;
  24556. begin
  24557. StartProgram(false);
  24558. Add([
  24559. 'type',
  24560. ' TFuncRef = reference to function(i: longint = 5): longint;',
  24561. ' TObject = class',
  24562. ' function Grow(s: longint): longint;',
  24563. ' end;',
  24564. 'var',
  24565. ' f: tfuncref;',
  24566. 'function tobject.grow(s: longint): longint;',
  24567. ' function GrowSub(i: longint): longint;',
  24568. ' begin',
  24569. ' f:=@grow;',
  24570. ' f:=@growsub;',
  24571. ' end;',
  24572. 'begin',
  24573. ' f:=@grow;',
  24574. ' f:=@growsub;',
  24575. 'end;',
  24576. 'begin',
  24577. '']);
  24578. ConvertProgram;
  24579. CheckSource('TestProcType_ReferenceToMethod',
  24580. LinesToStr([ // statements
  24581. 'rtl.createClass($mod, "TObject", null, function () {',
  24582. ' this.$init = function () {',
  24583. ' };',
  24584. ' this.$final = function () {',
  24585. ' };',
  24586. ' this.Grow = function (s) {',
  24587. ' var $Self = this;',
  24588. ' var Result = 0;',
  24589. ' function GrowSub(i) {',
  24590. ' var Result = 0;',
  24591. ' $mod.f = rtl.createCallback($Self, "Grow");',
  24592. ' $mod.f = GrowSub;',
  24593. ' return Result;',
  24594. ' };',
  24595. ' $mod.f = rtl.createCallback($Self, "Grow");',
  24596. ' $mod.f = GrowSub;',
  24597. ' return Result;',
  24598. ' };',
  24599. '});',
  24600. 'this.f = null;',
  24601. '']),
  24602. LinesToStr([ // $mod.$main
  24603. '']));
  24604. end;
  24605. procedure TTestModule.TestProcType_Typecast;
  24606. begin
  24607. StartProgram(false);
  24608. Add([
  24609. 'type',
  24610. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  24611. ' TEvent = procedure of object;',
  24612. ' TGetter = function:longint of object;',
  24613. ' TProcA = procedure(i: longint);',
  24614. ' TFuncB = function(i, j: longint): longint;',
  24615. 'procedure DoIt(); varargs; begin end;',
  24616. 'var',
  24617. ' Notify: tnotifyevent;',
  24618. ' Event: tevent;',
  24619. ' Getter: tgetter;',
  24620. ' ProcA: tproca;',
  24621. ' FuncB: tfuncb;',
  24622. ' p: pointer;',
  24623. 'begin',
  24624. ' notify:=tnotifyevent(event);',
  24625. ' event:=tevent(event);',
  24626. ' event:=tevent(notify);',
  24627. ' event:=tevent(getter);',
  24628. ' event:=tevent(proca);',
  24629. ' proca:=tproca(funcb);',
  24630. ' funcb:=tfuncb(funcb);',
  24631. ' funcb:=tfuncb(proca);',
  24632. ' funcb:=tfuncb(getter);',
  24633. ' proca:=tproca(p);',
  24634. ' funcb:=tfuncb(p);',
  24635. ' getter:=tgetter(p);',
  24636. ' p:=pointer(notify);',
  24637. ' p:=notify;',
  24638. ' p:=pointer(proca);',
  24639. ' p:=proca;',
  24640. ' p:=pointer(funcb);',
  24641. ' p:=funcb;',
  24642. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  24643. '']);
  24644. ConvertProgram;
  24645. CheckSource('TestProcType_Typecast',
  24646. LinesToStr([ // statements
  24647. 'this.DoIt = function () {',
  24648. '};',
  24649. 'this.Notify = null;',
  24650. 'this.Event = null;',
  24651. 'this.Getter = null;',
  24652. 'this.ProcA = null;',
  24653. 'this.FuncB = null;',
  24654. 'this.p = null;',
  24655. '']),
  24656. LinesToStr([ // $mod.$main
  24657. '$mod.Notify = $mod.Event;',
  24658. '$mod.Event = $mod.Event;',
  24659. '$mod.Event = $mod.Notify;',
  24660. '$mod.Event = $mod.Getter;',
  24661. '$mod.Event = $mod.ProcA;',
  24662. '$mod.ProcA = $mod.FuncB;',
  24663. '$mod.FuncB = $mod.FuncB;',
  24664. '$mod.FuncB = $mod.ProcA;',
  24665. '$mod.FuncB = $mod.Getter;',
  24666. '$mod.ProcA = $mod.p;',
  24667. '$mod.FuncB = $mod.p;',
  24668. '$mod.Getter = $mod.p;',
  24669. '$mod.p = $mod.Notify;',
  24670. '$mod.p = $mod.Notify;',
  24671. '$mod.p = $mod.ProcA;',
  24672. '$mod.p = $mod.ProcA;',
  24673. '$mod.p = $mod.FuncB;',
  24674. '$mod.p = $mod.FuncB;',
  24675. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  24676. '']));
  24677. end;
  24678. procedure TTestModule.TestProcType_PassProcToUntyped;
  24679. begin
  24680. StartProgram(false);
  24681. Add([
  24682. 'type',
  24683. ' TEvent = procedure of object;',
  24684. ' TFunc = function: longint;',
  24685. 'procedure DoIt(); varargs; begin end;',
  24686. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  24687. 'var',
  24688. ' Event: tevent;',
  24689. ' Func: TFunc;',
  24690. 'begin',
  24691. ' doit(event,func);',
  24692. ' dosome(event,event,event);',
  24693. ' dosome(func,func,func);',
  24694. '']);
  24695. ConvertProgram;
  24696. CheckSource('TestProcType_PassProcToUntyped',
  24697. LinesToStr([ // statements
  24698. 'this.DoIt = function () {',
  24699. '};',
  24700. 'this.DoSome = function (a, b, p) {',
  24701. '};',
  24702. 'this.Event = null;',
  24703. 'this.Func = null;',
  24704. '']),
  24705. LinesToStr([ // $mod.$main
  24706. '$mod.DoIt($mod.Event, $mod.Func);',
  24707. '$mod.DoSome($mod.Event, {',
  24708. ' p: $mod,',
  24709. ' get: function () {',
  24710. ' return this.p.Event;',
  24711. ' },',
  24712. ' set: function (v) {',
  24713. ' this.p.Event = v;',
  24714. ' }',
  24715. '}, $mod.Event);',
  24716. '$mod.DoSome($mod.Func, {',
  24717. ' p: $mod,',
  24718. ' get: function () {',
  24719. ' return this.p.Func;',
  24720. ' },',
  24721. ' set: function (v) {',
  24722. ' this.p.Func = v;',
  24723. ' }',
  24724. '}, $mod.Func);',
  24725. '']));
  24726. end;
  24727. procedure TTestModule.TestProcType_PassProcToArray;
  24728. begin
  24729. StartProgram(false);
  24730. Add([
  24731. 'type',
  24732. ' TFunc = function: longint;',
  24733. ' TArrFunc = array of TFunc;',
  24734. 'procedure DoIt(Arr: TArrFunc); begin end;',
  24735. 'function GetIt: longint; begin end;',
  24736. 'var',
  24737. ' Func: tfunc;',
  24738. 'begin',
  24739. ' doit([]);',
  24740. ' doit([@GetIt]);',
  24741. ' doit([Func]);',
  24742. '']);
  24743. ConvertProgram;
  24744. CheckSource('TestProcType_PassProcToArray',
  24745. LinesToStr([ // statements
  24746. 'this.DoIt = function (Arr) {',
  24747. '};',
  24748. 'this.GetIt = function () {',
  24749. ' var Result = 0;',
  24750. ' return Result;',
  24751. '};',
  24752. 'this.Func = null;',
  24753. '']),
  24754. LinesToStr([ // $mod.$main
  24755. '$mod.DoIt([]);',
  24756. '$mod.DoIt([$mod.GetIt]);',
  24757. '$mod.DoIt([$mod.Func]);',
  24758. '']));
  24759. end;
  24760. procedure TTestModule.TestPointer;
  24761. begin
  24762. StartProgram(false);
  24763. Add(['type',
  24764. ' TObject = class end;',
  24765. ' TClass = class of TObject;',
  24766. ' TArrInt = array of longint;',
  24767. 'const',
  24768. ' n = nil;',
  24769. 'var',
  24770. ' v: jsvalue;',
  24771. ' Obj: tobject;',
  24772. ' C: tclass;',
  24773. ' a: tarrint;',
  24774. ' p: Pointer = nil;',
  24775. ' s: string;',
  24776. 'begin',
  24777. ' p:=p;',
  24778. ' p:=nil;',
  24779. ' if p=nil then;',
  24780. ' if nil=p then;',
  24781. ' if Assigned(p) then;',
  24782. ' p:=Pointer(v);',
  24783. ' p:=obj;',
  24784. ' p:=c;',
  24785. ' p:=a;',
  24786. ' p:=tobject;',
  24787. ' obj:=TObject(p);',
  24788. ' c:=TClass(p);',
  24789. ' a:=TArrInt(p);',
  24790. ' p:=n;',
  24791. ' p:=Pointer(a);',
  24792. ' p:=pointer(s);',
  24793. ' s:=string(p);',
  24794. '']);
  24795. ConvertProgram;
  24796. CheckSource('TestPointer',
  24797. LinesToStr([ // statements
  24798. 'rtl.createClass($mod, "TObject", null, function () {',
  24799. ' this.$init = function () {',
  24800. ' };',
  24801. ' this.$final = function () {',
  24802. ' };',
  24803. '});',
  24804. 'this.n = null;',
  24805. 'this.v = undefined;',
  24806. 'this.Obj = null;',
  24807. 'this.C = null;',
  24808. 'this.a = [];',
  24809. 'this.p = null;',
  24810. 'this.s = "";',
  24811. '']),
  24812. LinesToStr([ // $mod.$main
  24813. '$mod.p = $mod.p;',
  24814. '$mod.p = null;',
  24815. 'if ($mod.p === null) ;',
  24816. 'if (null === $mod.p) ;',
  24817. 'if ($mod.p != null) ;',
  24818. '$mod.p = $mod.v;',
  24819. '$mod.p = $mod.Obj;',
  24820. '$mod.p = $mod.C;',
  24821. '$mod.p = $mod.a;',
  24822. '$mod.p = $mod.TObject;',
  24823. '$mod.Obj = $mod.p;',
  24824. '$mod.C = $mod.p;',
  24825. '$mod.a = $mod.p;',
  24826. '$mod.p = null;',
  24827. '$mod.p = $mod.a;',
  24828. '$mod.p = $mod.s;',
  24829. '$mod.s = $mod.p;',
  24830. '']));
  24831. end;
  24832. procedure TTestModule.TestPointer_Proc;
  24833. begin
  24834. StartProgram(false);
  24835. Add('type');
  24836. Add(' TObject = class');
  24837. Add(' procedure DoIt; virtual; abstract;');
  24838. Add(' end;');
  24839. Add('procedure DoSome; begin end;');
  24840. Add('var');
  24841. Add(' o: TObject;');
  24842. Add(' p: Pointer;');
  24843. Add('begin');
  24844. Add(' p:=@DoSome;');
  24845. Add(' p:[email protected];');
  24846. ConvertProgram;
  24847. CheckSource('TestPointer_Proc',
  24848. LinesToStr([ // statements
  24849. 'rtl.createClass($mod, "TObject", null, function () {',
  24850. ' this.$init = function () {',
  24851. ' };',
  24852. ' this.$final = function () {',
  24853. ' };',
  24854. '});',
  24855. 'this.DoSome = function () {',
  24856. '};',
  24857. 'this.o = null;',
  24858. 'this.p = null;',
  24859. '']),
  24860. LinesToStr([ // $mod.$main
  24861. '$mod.p = $mod.DoSome;',
  24862. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  24863. '']));
  24864. end;
  24865. procedure TTestModule.TestPointer_AssignRecordFail;
  24866. begin
  24867. StartProgram(false);
  24868. Add('type');
  24869. Add(' TRec = record end;');
  24870. Add('var');
  24871. Add(' p: Pointer;');
  24872. Add(' r: TRec;');
  24873. Add('begin');
  24874. Add(' p:=r;');
  24875. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  24876. nIncompatibleTypesGotExpected);
  24877. ConvertProgram;
  24878. end;
  24879. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  24880. begin
  24881. StartProgram(false);
  24882. Add('type');
  24883. Add(' TArr = array[boolean] of longint;');
  24884. Add('var');
  24885. Add(' p: Pointer;');
  24886. Add(' a: TArr;');
  24887. Add('begin');
  24888. Add(' p:=a;');
  24889. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  24890. nIncompatibleTypesGotExpected);
  24891. ConvertProgram;
  24892. end;
  24893. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  24894. begin
  24895. StartProgram(false);
  24896. Add([
  24897. 'procedure DoIt(args: array of jsvalue); begin end;',
  24898. 'procedure DoAll; varargs; begin end;',
  24899. 'var',
  24900. ' v: jsvalue;',
  24901. 'begin',
  24902. ' DoIt([pointer(v)]);',
  24903. ' DoAll(pointer(v));',
  24904. '']);
  24905. ConvertProgram;
  24906. CheckSource('TestPointer_TypeCastJSValueToPointer',
  24907. LinesToStr([ // statements
  24908. 'this.DoIt = function (args) {',
  24909. '};',
  24910. 'this.DoAll = function () {',
  24911. '};',
  24912. 'this.v = undefined;',
  24913. '']),
  24914. LinesToStr([ // $mod.$main
  24915. '$mod.DoIt([$mod.v]);',
  24916. '$mod.DoAll($mod.v);',
  24917. '']));
  24918. end;
  24919. procedure TTestModule.TestPointer_NonRecordFail;
  24920. begin
  24921. StartProgram(false);
  24922. Add([
  24923. 'type',
  24924. ' p = ^longint;',
  24925. 'begin',
  24926. '']);
  24927. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  24928. ConvertProgram;
  24929. end;
  24930. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  24931. begin
  24932. StartProgram(false);
  24933. Add([
  24934. 'procedure DoIt(p: ^longint); begin end;',
  24935. 'begin',
  24936. '']);
  24937. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24938. ConvertProgram;
  24939. end;
  24940. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  24941. begin
  24942. StartProgram(false);
  24943. Add([
  24944. 'var p: ^longint;',
  24945. 'begin',
  24946. '']);
  24947. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24948. ConvertProgram;
  24949. end;
  24950. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  24951. begin
  24952. StartProgram(false);
  24953. Add([
  24954. 'function DoIt: ^longint; begin end;',
  24955. 'begin',
  24956. '']);
  24957. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  24958. ConvertProgram;
  24959. end;
  24960. procedure TTestModule.TestPointer_AddrOperatorFail;
  24961. begin
  24962. StartProgram(false);
  24963. Add([
  24964. 'var i: longint;',
  24965. 'begin',
  24966. ' if @i=nil then ;',
  24967. '']);
  24968. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  24969. ConvertProgram;
  24970. end;
  24971. procedure TTestModule.TestPointer_ArrayParamsFail;
  24972. begin
  24973. StartProgram(false);
  24974. Add([
  24975. 'var',
  24976. ' p: Pointer;',
  24977. 'begin',
  24978. ' p:=p[1];',
  24979. '']);
  24980. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  24981. ConvertProgram;
  24982. end;
  24983. procedure TTestModule.TestPointer_PointerAddFail;
  24984. begin
  24985. StartProgram(false);
  24986. Add([
  24987. 'var',
  24988. ' p: Pointer;',
  24989. 'begin',
  24990. ' p:=p+1;',
  24991. '']);
  24992. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  24993. ConvertProgram;
  24994. end;
  24995. procedure TTestModule.TestPointer_IncPointerFail;
  24996. begin
  24997. StartProgram(false);
  24998. Add([
  24999. 'var',
  25000. ' p: Pointer;',
  25001. 'begin',
  25002. ' inc(p,1);',
  25003. '']);
  25004. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  25005. nIncompatibleTypeArgNo);
  25006. ConvertProgram;
  25007. end;
  25008. procedure TTestModule.TestPointer_Record;
  25009. begin
  25010. StartProgram(false);
  25011. Add([
  25012. 'type',
  25013. ' TRec = record x: longint; end;',
  25014. ' PRec = ^TRec;',
  25015. 'var',
  25016. ' r: TRec;',
  25017. ' p: PRec;',
  25018. ' q: ^TRec;',
  25019. ' Ptr: pointer;',
  25020. 'begin',
  25021. ' new(p);',
  25022. ' p:=@r;',
  25023. ' r:=p^;',
  25024. ' r.x:=p^.x;',
  25025. ' p^.x:=r.x;',
  25026. ' if p^.x=3 then ;',
  25027. ' if 4=p^.x then ;',
  25028. ' dispose(p);',
  25029. ' new(q);',
  25030. ' dispose(q);',
  25031. ' Ptr:=p;',
  25032. ' p:=PRec(ptr);',
  25033. '']);
  25034. ConvertProgram;
  25035. CheckSource('TestPointer_Record',
  25036. LinesToStr([ // statements
  25037. 'rtl.recNewT($mod, "TRec", function () {',
  25038. ' this.x = 0;',
  25039. ' this.$eq = function (b) {',
  25040. ' return this.x === b.x;',
  25041. ' };',
  25042. ' this.$assign = function (s) {',
  25043. ' this.x = s.x;',
  25044. ' return this;',
  25045. ' };',
  25046. '});',
  25047. 'this.r = $mod.TRec.$new();',
  25048. 'this.p = null;',
  25049. 'this.q = null;',
  25050. 'this.Ptr = null;',
  25051. '']),
  25052. LinesToStr([ // $mod.$main
  25053. '$mod.p = $mod.TRec.$new();',
  25054. '$mod.p = $mod.r;',
  25055. '$mod.r.$assign($mod.p);',
  25056. '$mod.r.x = $mod.p.x;',
  25057. '$mod.p.x = $mod.r.x;',
  25058. 'if ($mod.p.x === 3) ;',
  25059. 'if (4 === $mod.p.x) ;',
  25060. '$mod.p = null;',
  25061. '$mod.q = $mod.TRec.$new();',
  25062. '$mod.q = null;',
  25063. '$mod.Ptr = $mod.p;',
  25064. '$mod.p = $mod.Ptr;',
  25065. '']));
  25066. end;
  25067. procedure TTestModule.TestPointer_RecordArg;
  25068. begin
  25069. StartProgram(false);
  25070. Add([
  25071. '{$modeswitch autoderef}',
  25072. 'type',
  25073. ' TRec = record x: longint; end;',
  25074. ' PRec = ^TRec;',
  25075. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  25076. 'begin',
  25077. ' a.x:=a.x;',
  25078. ' a^.x:=a^.x;',
  25079. ' with a^ do',
  25080. ' x:=x;',
  25081. 'end;',
  25082. 'function GetIt(p: PRec): PRec;',
  25083. 'begin',
  25084. ' p.x:=p.x;',
  25085. ' p^.x:=p^.x;',
  25086. ' with p^ do',
  25087. ' x:=x;',
  25088. 'end;',
  25089. 'var',
  25090. ' r: TRec;',
  25091. ' p: PRec;',
  25092. 'begin',
  25093. ' p:=GetIt(p);',
  25094. ' p^:=GetIt(@r)^;',
  25095. ' DoIt(p,p,p);',
  25096. ' DoIt(@r,p,p);',
  25097. '']);
  25098. ConvertProgram;
  25099. CheckSource('TestPointer_Record',
  25100. LinesToStr([ // statements
  25101. 'rtl.recNewT($mod, "TRec", function () {',
  25102. ' this.x = 0;',
  25103. ' this.$eq = function (b) {',
  25104. ' return this.x === b.x;',
  25105. ' };',
  25106. ' this.$assign = function (s) {',
  25107. ' this.x = s.x;',
  25108. ' return this;',
  25109. ' };',
  25110. '});',
  25111. 'this.DoIt = function (a, b, c) {',
  25112. ' var Result = $mod.TRec.$new();',
  25113. ' a.x = a.x;',
  25114. ' a.x = a.x;',
  25115. ' a.x = a.x;',
  25116. ' return Result;',
  25117. '};',
  25118. 'this.GetIt = function (p) {',
  25119. ' var Result = null;',
  25120. ' p.x = p.x;',
  25121. ' p.x = p.x;',
  25122. ' p.x = p.x;',
  25123. ' return Result;',
  25124. '};',
  25125. 'this.r = $mod.TRec.$new();',
  25126. 'this.p = null;',
  25127. '']),
  25128. LinesToStr([ // $mod.$main
  25129. '$mod.p = $mod.GetIt($mod.p);',
  25130. '$mod.p.$assign($mod.GetIt($mod.r));',
  25131. '$mod.DoIt($mod.p, {',
  25132. ' p: $mod,',
  25133. ' get: function () {',
  25134. ' return this.p.p;',
  25135. ' },',
  25136. ' set: function (v) {',
  25137. ' this.p.p = v;',
  25138. ' }',
  25139. '}, {',
  25140. ' p: $mod,',
  25141. ' get: function () {',
  25142. ' return this.p.p;',
  25143. ' },',
  25144. ' set: function (v) {',
  25145. ' this.p.p = v;',
  25146. ' }',
  25147. '});',
  25148. '$mod.DoIt($mod.r, {',
  25149. ' p: $mod,',
  25150. ' get: function () {',
  25151. ' return this.p.p;',
  25152. ' },',
  25153. ' set: function (v) {',
  25154. ' this.p.p = v;',
  25155. ' }',
  25156. '}, {',
  25157. ' p: $mod,',
  25158. ' get: function () {',
  25159. ' return this.p.p;',
  25160. ' },',
  25161. ' set: function (v) {',
  25162. ' this.p.p = v;',
  25163. ' }',
  25164. '});',
  25165. '']));
  25166. end;
  25167. procedure TTestModule.TestJSValue_AssignToJSValue;
  25168. begin
  25169. StartProgram(false);
  25170. Add('var');
  25171. Add(' v: jsvalue;');
  25172. Add(' i: longint;');
  25173. Add(' s: string;');
  25174. Add(' b: boolean;');
  25175. Add(' d: double;');
  25176. Add(' p: pointer;');
  25177. Add('begin');
  25178. Add(' v:=v;');
  25179. Add(' v:=1;');
  25180. Add(' v:=i;');
  25181. Add(' v:='''';');
  25182. Add(' v:=''c'';');
  25183. Add(' v:=''foo'';');
  25184. Add(' v:=s;');
  25185. Add(' v:=false;');
  25186. Add(' v:=true;');
  25187. Add(' v:=b;');
  25188. Add(' v:=0.1;');
  25189. Add(' v:=d;');
  25190. Add(' v:=nil;');
  25191. Add(' v:=p;');
  25192. ConvertProgram;
  25193. CheckSource('TestJSValue_AssignToJSValue',
  25194. LinesToStr([ // statements
  25195. 'this.v = undefined;',
  25196. 'this.i = 0;',
  25197. 'this.s = "";',
  25198. 'this.b = false;',
  25199. 'this.d = 0.0;',
  25200. 'this.p = null;',
  25201. '']),
  25202. LinesToStr([ // $mod.$main
  25203. '$mod.v = $mod.v;',
  25204. '$mod.v = 1;',
  25205. '$mod.v = $mod.i;',
  25206. '$mod.v = "";',
  25207. '$mod.v = "c";',
  25208. '$mod.v = "foo";',
  25209. '$mod.v = $mod.s;',
  25210. '$mod.v = false;',
  25211. '$mod.v = true;',
  25212. '$mod.v = $mod.b;',
  25213. '$mod.v = 0.1;',
  25214. '$mod.v = $mod.d;',
  25215. '$mod.v = null;',
  25216. '$mod.v = $mod.p;',
  25217. '']));
  25218. end;
  25219. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  25220. begin
  25221. StartProgram(false);
  25222. Add('type');
  25223. Add(' integer = longint;');
  25224. Add(' TYesNo = boolean;');
  25225. Add(' TFloat = double;');
  25226. Add(' TCaption = string;');
  25227. Add(' TChar = char;');
  25228. Add('var');
  25229. Add(' v: jsvalue;');
  25230. Add(' i: integer;');
  25231. Add(' s: TCaption;');
  25232. Add(' b: TYesNo;');
  25233. Add(' d: TFloat;');
  25234. Add(' c: char;');
  25235. Add('begin');
  25236. Add(' i:=longint(v);');
  25237. Add(' i:=integer(v);');
  25238. Add(' s:=string(v);');
  25239. Add(' s:=TCaption(v);');
  25240. Add(' b:=boolean(v);');
  25241. Add(' b:=TYesNo(v);');
  25242. Add(' d:=double(v);');
  25243. Add(' d:=TFloat(v);');
  25244. Add(' c:=char(v);');
  25245. Add(' c:=TChar(v);');
  25246. ConvertProgram;
  25247. CheckSource('TestJSValue_TypeCastToBaseType',
  25248. LinesToStr([ // statements
  25249. 'this.v = undefined;',
  25250. 'this.i = 0;',
  25251. 'this.s = "";',
  25252. 'this.b = false;',
  25253. 'this.d = 0.0;',
  25254. 'this.c = "";',
  25255. '']),
  25256. LinesToStr([ // $mod.$main
  25257. '$mod.i = Math.floor($mod.v);',
  25258. '$mod.i = Math.floor($mod.v);',
  25259. '$mod.s = "" + $mod.v;',
  25260. '$mod.s = "" + $mod.v;',
  25261. '$mod.b = !($mod.v == false);',
  25262. '$mod.b = !($mod.v == false);',
  25263. '$mod.d = rtl.getNumber($mod.v);',
  25264. '$mod.d = rtl.getNumber($mod.v);',
  25265. '$mod.c = rtl.getChar($mod.v);',
  25266. '$mod.c = rtl.getChar($mod.v);',
  25267. '']));
  25268. end;
  25269. procedure TTestModule.TestJSValue_TypecastToJSValue;
  25270. begin
  25271. StartProgram(false);
  25272. Add([
  25273. 'type',
  25274. ' TArr = array of word;',
  25275. ' TRec = record end;',
  25276. ' TSet = set of boolean;',
  25277. 'procedure Fly(v: jsvalue);',
  25278. 'begin',
  25279. 'end;',
  25280. 'var',
  25281. ' a: TArr;',
  25282. ' r: TRec;',
  25283. ' s: TSet;',
  25284. 'begin',
  25285. ' Fly(jsvalue(a));',
  25286. ' Fly(jsvalue(r));',
  25287. ' Fly(jsvalue(s));',
  25288. '']);
  25289. ConvertProgram;
  25290. CheckSource('TestJSValue_TypecastToJSValue',
  25291. LinesToStr([ // statements
  25292. 'rtl.recNewT($mod, "TRec", function () {',
  25293. ' this.$eq = function (b) {',
  25294. ' return true;',
  25295. ' };',
  25296. ' this.$assign = function (s) {',
  25297. ' return this;',
  25298. ' };',
  25299. '});',
  25300. 'this.Fly = function (v) {',
  25301. '};',
  25302. 'this.a = [];',
  25303. 'this.r = $mod.TRec.$new();',
  25304. 'this.s = {};',
  25305. '']),
  25306. LinesToStr([ // $mod.$main
  25307. '$mod.Fly($mod.a);',
  25308. '$mod.Fly($mod.r);',
  25309. '$mod.Fly($mod.s);',
  25310. '']));
  25311. end;
  25312. procedure TTestModule.TestJSValue_Equal;
  25313. begin
  25314. StartProgram(false);
  25315. Add('type');
  25316. Add(' integer = longint;');
  25317. Add(' TYesNo = boolean;');
  25318. Add(' TFloat = double;');
  25319. Add(' TCaption = string;');
  25320. Add(' TChar = char;');
  25321. Add(' TMulti = JSValue;');
  25322. Add('var');
  25323. Add(' v: jsvalue;');
  25324. Add(' i: integer;');
  25325. Add(' s: TCaption;');
  25326. Add(' b: TYesNo;');
  25327. Add(' d: TFloat;');
  25328. Add(' c: char;');
  25329. Add(' m: TMulti;');
  25330. Add('begin');
  25331. Add(' b:=v=v;');
  25332. Add(' b:=v<>v;');
  25333. Add(' b:=v=1;');
  25334. Add(' b:=v<>1;');
  25335. Add(' b:=2=v;');
  25336. Add(' b:=2<>v;');
  25337. Add(' b:=v=i;');
  25338. Add(' b:=i=v;');
  25339. Add(' b:=v=nil;');
  25340. Add(' b:=nil=v;');
  25341. Add(' b:=v=false;');
  25342. Add(' b:=true=v;');
  25343. Add(' b:=v=b;');
  25344. Add(' b:=b=v;');
  25345. Add(' b:=v=s;');
  25346. Add(' b:=s=v;');
  25347. Add(' b:=v=''foo'';');
  25348. Add(' b:=''''=v;');
  25349. Add(' b:=v=d;');
  25350. Add(' b:=d=v;');
  25351. Add(' b:=v=3.4;');
  25352. Add(' b:=5.6=v;');
  25353. Add(' b:=v=c;');
  25354. Add(' b:=c=v;');
  25355. Add(' b:=m=m;');
  25356. Add(' b:=v=m;');
  25357. Add(' b:=m=v;');
  25358. ConvertProgram;
  25359. CheckSource('TestJSValue_Equal',
  25360. LinesToStr([ // statements
  25361. 'this.v = undefined;',
  25362. 'this.i = 0;',
  25363. 'this.s = "";',
  25364. 'this.b = false;',
  25365. 'this.d = 0.0;',
  25366. 'this.c = "";',
  25367. 'this.m = undefined;',
  25368. '']),
  25369. LinesToStr([ // $mod.$main
  25370. '$mod.b = $mod.v == $mod.v;',
  25371. '$mod.b = $mod.v != $mod.v;',
  25372. '$mod.b = $mod.v == 1;',
  25373. '$mod.b = $mod.v != 1;',
  25374. '$mod.b = 2 == $mod.v;',
  25375. '$mod.b = 2 != $mod.v;',
  25376. '$mod.b = $mod.v == $mod.i;',
  25377. '$mod.b = $mod.i == $mod.v;',
  25378. '$mod.b = $mod.v == null;',
  25379. '$mod.b = null == $mod.v;',
  25380. '$mod.b = $mod.v == false;',
  25381. '$mod.b = true == $mod.v;',
  25382. '$mod.b = $mod.v == $mod.b;',
  25383. '$mod.b = $mod.b == $mod.v;',
  25384. '$mod.b = $mod.v == $mod.s;',
  25385. '$mod.b = $mod.s == $mod.v;',
  25386. '$mod.b = $mod.v == "foo";',
  25387. '$mod.b = "" == $mod.v;',
  25388. '$mod.b = $mod.v == $mod.d;',
  25389. '$mod.b = $mod.d == $mod.v;',
  25390. '$mod.b = $mod.v == 3.4;',
  25391. '$mod.b = 5.6 == $mod.v;',
  25392. '$mod.b = $mod.v == $mod.c;',
  25393. '$mod.b = $mod.c == $mod.v;',
  25394. '$mod.b = $mod.m == $mod.m;',
  25395. '$mod.b = $mod.v == $mod.m;',
  25396. '$mod.b = $mod.m == $mod.v;',
  25397. '']));
  25398. end;
  25399. procedure TTestModule.TestJSValue_If;
  25400. begin
  25401. StartProgram(false);
  25402. Add([
  25403. 'var',
  25404. ' v: jsvalue;',
  25405. 'begin',
  25406. ' if v then ;',
  25407. ' while v do ;',
  25408. ' repeat until v;',
  25409. '']);
  25410. ConvertProgram;
  25411. CheckSource('TestJSValue_If',
  25412. LinesToStr([ // statements
  25413. 'this.v = undefined;',
  25414. '']),
  25415. LinesToStr([ // $mod.$main
  25416. 'if ($mod.v) ;',
  25417. 'while($mod.v){',
  25418. '};',
  25419. 'do{',
  25420. '} while(!$mod.v);',
  25421. '']));
  25422. end;
  25423. procedure TTestModule.TestJSValue_Not;
  25424. begin
  25425. StartProgram(false);
  25426. Add([
  25427. 'var',
  25428. ' v: jsvalue;',
  25429. ' b: boolean;',
  25430. 'begin',
  25431. ' b:=not v;',
  25432. ' if not v then ;',
  25433. ' while not v do ;',
  25434. ' repeat until not v;',
  25435. '']);
  25436. ConvertProgram;
  25437. CheckSource('TestJSValue_If',
  25438. LinesToStr([ // statements
  25439. 'this.v = undefined;',
  25440. 'this.b = false;',
  25441. '']),
  25442. LinesToStr([ // $mod.$main
  25443. '$mod.b=!$mod.v;',
  25444. 'if (!$mod.v) ;',
  25445. 'while(!$mod.v){',
  25446. '};',
  25447. 'do{',
  25448. '} while($mod.v);',
  25449. '']));
  25450. end;
  25451. procedure TTestModule.TestJSValue_Enum;
  25452. begin
  25453. StartProgram(false);
  25454. Add('type');
  25455. Add(' TColor = (red, blue);');
  25456. Add(' TRedBlue = TColor;');
  25457. Add('var');
  25458. Add(' v: jsvalue;');
  25459. Add(' e: TColor;');
  25460. Add('begin');
  25461. Add(' v:=e;');
  25462. Add(' v:=TColor(e);');
  25463. Add(' v:=TRedBlue(e);');
  25464. Add(' e:=TColor(v);');
  25465. Add(' e:=TRedBlue(v);');
  25466. ConvertProgram;
  25467. CheckSource('TestJSValue_Enum',
  25468. LinesToStr([ // statements
  25469. 'this.TColor = {',
  25470. ' "0": "red",',
  25471. ' red: 0,',
  25472. ' "1": "blue",',
  25473. ' blue: 1',
  25474. '};',
  25475. 'this.v = undefined;',
  25476. 'this.e = 0;',
  25477. '']),
  25478. LinesToStr([ // $mod.$main
  25479. '$mod.v = $mod.e;',
  25480. '$mod.v = $mod.e;',
  25481. '$mod.v = $mod.e;',
  25482. '$mod.e = $mod.v;',
  25483. '$mod.e = $mod.v;',
  25484. '']));
  25485. end;
  25486. procedure TTestModule.TestJSValue_ClassInstance;
  25487. begin
  25488. StartProgram(false);
  25489. Add([
  25490. 'type',
  25491. ' TObject = class',
  25492. ' end;',
  25493. ' TBirdObject = TObject;',
  25494. 'var',
  25495. ' v: jsvalue;',
  25496. ' o: TObject;',
  25497. 'begin',
  25498. ' v:=o;',
  25499. ' v:=TObject(o);',
  25500. ' v:=TBirdObject(o);',
  25501. ' o:=TObject(v);',
  25502. ' o:=TBirdObject(v);',
  25503. ' if v is TObject then ;',
  25504. '']);
  25505. ConvertProgram;
  25506. CheckSource('TestJSValue_ClassInstance',
  25507. LinesToStr([ // statements
  25508. 'rtl.createClass($mod, "TObject", null, function () {',
  25509. ' this.$init = function () {',
  25510. ' };',
  25511. ' this.$final = function () {',
  25512. ' };',
  25513. '});',
  25514. 'this.v = undefined;',
  25515. 'this.o = null;',
  25516. '']),
  25517. LinesToStr([ // $mod.$main
  25518. '$mod.v = $mod.o;',
  25519. '$mod.v = $mod.o;',
  25520. '$mod.v = $mod.o;',
  25521. '$mod.o = rtl.getObject($mod.v);',
  25522. '$mod.o = rtl.getObject($mod.v);',
  25523. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  25524. '']));
  25525. end;
  25526. procedure TTestModule.TestJSValue_ClassOf;
  25527. begin
  25528. StartProgram(false);
  25529. Add([
  25530. 'type',
  25531. ' TClass = class of TObject;',
  25532. ' TObject = class',
  25533. ' end;',
  25534. ' TBirds = class of TBird;',
  25535. ' TBird = class(TObject) end;',
  25536. 'var',
  25537. ' v: jsvalue;',
  25538. ' c: TClass;',
  25539. 'begin',
  25540. ' v:=c;',
  25541. ' v:=TObject;',
  25542. ' v:=TClass(c);',
  25543. ' v:=TBirds(c);',
  25544. ' c:=TClass(v);',
  25545. ' c:=TBirds(v);',
  25546. ' if v is TClass then ;',
  25547. '']);
  25548. ConvertProgram;
  25549. CheckSource('TestJSValue_ClassOf',
  25550. LinesToStr([ // statements
  25551. 'rtl.createClass($mod, "TObject", null, function () {',
  25552. ' this.$init = function () {',
  25553. ' };',
  25554. ' this.$final = function () {',
  25555. ' };',
  25556. '});',
  25557. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  25558. '});',
  25559. 'this.v = undefined;',
  25560. 'this.c = null;',
  25561. '']),
  25562. LinesToStr([ // $mod.$main
  25563. '$mod.v = $mod.c;',
  25564. '$mod.v = $mod.TObject;',
  25565. '$mod.v = $mod.c;',
  25566. '$mod.v = $mod.c;',
  25567. '$mod.c = rtl.getObject($mod.v);',
  25568. '$mod.c = rtl.getObject($mod.v);',
  25569. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  25570. '']));
  25571. end;
  25572. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  25573. begin
  25574. StartProgram(false);
  25575. Add([
  25576. 'type',
  25577. ' integer = longint;',
  25578. ' TArray = array of JSValue;',
  25579. ' TArrgh = tarray;',
  25580. ' TArrInt = array of integer;',
  25581. 'var',
  25582. ' v: jsvalue;',
  25583. ' TheArray: tarray = (1,''2'');',
  25584. ' Arr: tarrgh;',
  25585. ' i: integer;',
  25586. ' ArrInt: tarrint;',
  25587. 'begin',
  25588. ' arr:=thearray;',
  25589. ' thearray:=arr;',
  25590. ' setlength(arr,2);',
  25591. ' setlength(thearray,3);',
  25592. ' arr[4]:=v;',
  25593. ' arr[5]:=length(thearray);',
  25594. ' arr[6]:=nil;',
  25595. ' arr[7]:=thearray[8];',
  25596. ' arr[low(arr)]:=high(thearray);',
  25597. ' arr:=arrint;',
  25598. ' arrInt:=tarrint(arr);',
  25599. ' if TheArray = nil then ;',
  25600. ' if nil = TheArray then ;',
  25601. ' if TheArray <> nil then ;',
  25602. ' if nil <> TheArray then ;',
  25603. '']);
  25604. ConvertProgram;
  25605. CheckSource('TestJSValue_ArrayOfJSValue',
  25606. LinesToStr([ // statements
  25607. 'this.v = undefined;',
  25608. 'this.TheArray = [1, "2"];',
  25609. 'this.Arr = [];',
  25610. 'this.i = 0;',
  25611. 'this.ArrInt = [];',
  25612. '']),
  25613. LinesToStr([ // $mod.$main
  25614. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  25615. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  25616. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  25617. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  25618. '$mod.Arr[4] = $mod.v;',
  25619. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  25620. '$mod.Arr[6] = null;',
  25621. '$mod.Arr[7] = $mod.TheArray[8];',
  25622. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  25623. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  25624. '$mod.ArrInt = $mod.Arr;',
  25625. 'if (rtl.length($mod.TheArray) === 0) ;',
  25626. 'if (rtl.length($mod.TheArray) === 0) ;',
  25627. 'if (rtl.length($mod.TheArray) > 0) ;',
  25628. 'if (rtl.length($mod.TheArray) > 0) ;',
  25629. '']));
  25630. end;
  25631. procedure TTestModule.TestJSValue_ArrayLit;
  25632. begin
  25633. StartProgram(false);
  25634. Add([
  25635. 'type',
  25636. ' TFlag = (big,small);',
  25637. ' TArray = array of JSValue;',
  25638. ' TObject = class end;',
  25639. ' TClass = class of TObject;',
  25640. 'var',
  25641. ' v: jsvalue;',
  25642. ' a: TArray;',
  25643. ' o: TObject;',
  25644. 'begin',
  25645. ' a:=[];',
  25646. ' a:=[1];',
  25647. ' a:=[1,2];',
  25648. ' a:=[big];',
  25649. ' a:=[1,big];',
  25650. ' a:=[o,nil];',
  25651. '']);
  25652. ConvertProgram;
  25653. CheckSource('TestJSValue_ArrayLit',
  25654. LinesToStr([ // statements
  25655. 'this.TFlag = {',
  25656. ' "0": "big",',
  25657. ' big: 0,',
  25658. ' "1": "small",',
  25659. ' small: 1',
  25660. '};',
  25661. 'rtl.createClass($mod, "TObject", null, function () {',
  25662. ' this.$init = function () {',
  25663. ' };',
  25664. ' this.$final = function () {',
  25665. ' };',
  25666. '});',
  25667. 'this.v = undefined;',
  25668. 'this.a = [];',
  25669. 'this.o = null;',
  25670. '']),
  25671. LinesToStr([ // $mod.$main
  25672. '$mod.a = [];',
  25673. '$mod.a = [1];',
  25674. '$mod.a = [1, 2];',
  25675. '$mod.a = [$mod.TFlag.big];',
  25676. '$mod.a = [1, $mod.TFlag.big];',
  25677. '$mod.a = [$mod.o, null];',
  25678. '']));
  25679. end;
  25680. procedure TTestModule.TestJSValue_Params;
  25681. begin
  25682. StartProgram(false);
  25683. Add('type');
  25684. Add(' integer = longint;');
  25685. Add(' TYesNo = boolean;');
  25686. Add(' TFloat = double;');
  25687. Add(' TCaption = string;');
  25688. Add(' TChar = char;');
  25689. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  25690. Add('var');
  25691. Add(' l: jsvalue;');
  25692. Add('begin');
  25693. Add(' a:=a;');
  25694. Add(' l:=b;');
  25695. Add(' c:=c;');
  25696. Add(' d:=d;');
  25697. Add(' Result:=l;');
  25698. Add('end;');
  25699. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  25700. Add('var');
  25701. Add(' v: jsvalue;');
  25702. Add(' i: integer;');
  25703. Add(' b: TYesNo;');
  25704. Add(' d: TFloat;');
  25705. Add(' s: TCaption;');
  25706. Add(' c: TChar;');
  25707. Add('begin');
  25708. Add(' v:=doit(v,v,v,v);');
  25709. Add(' i:=integer(dosome(i,i));');
  25710. Add(' b:=TYesNo(dosome(b,b));');
  25711. Add(' d:=TFloat(dosome(d,d));');
  25712. Add(' s:=TCaption(dosome(s,s));');
  25713. Add(' c:=TChar(dosome(c,c));');
  25714. ConvertProgram;
  25715. CheckSource('TestJSValue_Params',
  25716. LinesToStr([ // statements
  25717. 'this.DoIt = function (a, b, c, d) {',
  25718. ' var Result = undefined;',
  25719. ' var l = undefined;',
  25720. ' a = a;',
  25721. ' l = b;',
  25722. ' c.set(c.get());',
  25723. ' d.set(d.get());',
  25724. ' Result = l;',
  25725. ' return Result;',
  25726. '};',
  25727. 'this.DoSome = function (a, b) {',
  25728. ' var Result = undefined;',
  25729. ' return Result;',
  25730. '};',
  25731. 'this.v = undefined;',
  25732. 'this.i = 0;',
  25733. 'this.b = false;',
  25734. 'this.d = 0.0;',
  25735. 'this.s = "";',
  25736. 'this.c = "";',
  25737. '']),
  25738. LinesToStr([ // $mod.$main
  25739. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  25740. ' p: $mod,',
  25741. ' get: function () {',
  25742. ' return this.p.v;',
  25743. ' },',
  25744. ' set: function (v) {',
  25745. ' this.p.v = v;',
  25746. ' }',
  25747. '}, {',
  25748. ' p: $mod,',
  25749. ' get: function () {',
  25750. ' return this.p.v;',
  25751. ' },',
  25752. ' set: function (v) {',
  25753. ' this.p.v = v;',
  25754. ' }',
  25755. '});',
  25756. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  25757. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  25758. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  25759. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  25760. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  25761. '']));
  25762. end;
  25763. procedure TTestModule.TestJSValue_UntypedParam;
  25764. begin
  25765. StartProgram(false);
  25766. Add('function DoIt(const a; var b; out c): jsvalue;');
  25767. Add('begin');
  25768. Add(' Result:=a;');
  25769. Add(' Result:=b;');
  25770. Add(' Result:=c;');
  25771. Add(' b:=Result;');
  25772. Add(' c:=Result;');
  25773. Add('end;');
  25774. Add('var i: longint;');
  25775. Add('begin');
  25776. Add(' doit(i,i,i);');
  25777. ConvertProgram;
  25778. CheckSource('TestJSValue_UntypedParam',
  25779. LinesToStr([ // statements
  25780. 'this.DoIt = function (a, b, c) {',
  25781. ' var Result = undefined;',
  25782. ' Result = a;',
  25783. ' Result = b.get();',
  25784. ' Result = c.get();',
  25785. ' b.set(Result);',
  25786. ' c.set(Result);',
  25787. ' return Result;',
  25788. '};',
  25789. 'this.i = 0;',
  25790. '']),
  25791. LinesToStr([ // $mod.$main
  25792. '$mod.DoIt($mod.i, {',
  25793. ' p: $mod,',
  25794. ' get: function () {',
  25795. ' return this.p.i;',
  25796. ' },',
  25797. ' set: function (v) {',
  25798. ' this.p.i = v;',
  25799. ' }',
  25800. '}, {',
  25801. ' p: $mod,',
  25802. ' get: function () {',
  25803. ' return this.p.i;',
  25804. ' },',
  25805. ' set: function (v) {',
  25806. ' this.p.i = v;',
  25807. ' }',
  25808. '});',
  25809. '']));
  25810. end;
  25811. procedure TTestModule.TestJSValue_FuncResultType;
  25812. begin
  25813. StartProgram(false);
  25814. Add('type');
  25815. Add(' integer = longint;');
  25816. Add(' TJSValueArray = array of JSValue;');
  25817. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  25818. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  25819. Add('begin');
  25820. Add(' while Compare(P,aList[0])>0 do ;');
  25821. Add('end;');
  25822. Add('var');
  25823. Add(' Compare: TListSortCompare;');
  25824. Add(' V: JSValue;');
  25825. Add(' i: integer;');
  25826. Add('begin');
  25827. Add(' if Compare(V,V)>0 then ;');
  25828. Add(' if Compare(i,i)>1 then ;');
  25829. Add(' if Compare(nil,false)>2 then ;');
  25830. Add(' if Compare(1,true)>3 then ;');
  25831. ConvertProgram;
  25832. CheckSource('TestJSValue_UntypedParam',
  25833. LinesToStr([ // statements
  25834. 'this.Sort = function (P, aList, Compare) {',
  25835. ' while (Compare(P, aList[0]) > 0) {',
  25836. ' };',
  25837. '};',
  25838. 'this.Compare = null;',
  25839. 'this.V = undefined;',
  25840. 'this.i = 0;',
  25841. '']),
  25842. LinesToStr([ // $mod.$main
  25843. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  25844. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  25845. 'if ($mod.Compare(null, false) > 2) ;',
  25846. 'if ($mod.Compare(1, true) > 3) ;',
  25847. '']));
  25848. end;
  25849. procedure TTestModule.TestJSValue_ProcType_Assign;
  25850. begin
  25851. StartProgram(false);
  25852. Add('type');
  25853. Add(' integer = longint;');
  25854. Add(' TObject = class');
  25855. Add(' class function GetGlob: integer;');
  25856. Add(' function Getter: integer;');
  25857. Add(' end;');
  25858. Add('class function TObject.GetGlob: integer;');
  25859. Add('var v1: jsvalue;');
  25860. Add('begin');
  25861. Add(' v1:=@GetGlob;');
  25862. Add(' v1:[email protected];');
  25863. Add('end;');
  25864. Add('function TObject.Getter: integer;');
  25865. Add('var v2: jsvalue;');
  25866. Add('begin');
  25867. Add(' v2:=@Getter;');
  25868. Add(' v2:[email protected];');
  25869. Add(' v2:=@GetGlob;');
  25870. Add(' v2:[email protected];');
  25871. Add('end;');
  25872. Add('function GetIt(i: integer): integer;');
  25873. Add('var v3: jsvalue;');
  25874. Add('begin');
  25875. Add(' v3:=@GetIt;');
  25876. Add('end;');
  25877. Add('var');
  25878. Add(' V: JSValue;');
  25879. Add(' o: TObject;');
  25880. Add('begin');
  25881. Add(' v:=@GetIt;');
  25882. Add(' v:[email protected];');
  25883. Add(' v:[email protected];');
  25884. ConvertProgram;
  25885. CheckSource('TestJSValue_ProcType_Assign',
  25886. LinesToStr([ // statements
  25887. 'rtl.createClass($mod, "TObject", null, function () {',
  25888. ' this.$init = function () {',
  25889. ' };',
  25890. ' this.$final = function () {',
  25891. ' };',
  25892. ' this.GetGlob = function () {',
  25893. ' var Result = 0;',
  25894. ' var v1 = undefined;',
  25895. ' v1 = rtl.createCallback(this, "GetGlob");',
  25896. ' v1 = rtl.createCallback(this, "GetGlob");',
  25897. ' return Result;',
  25898. ' };',
  25899. ' this.Getter = function () {',
  25900. ' var Result = 0;',
  25901. ' var v2 = undefined;',
  25902. ' v2 = rtl.createCallback(this, "Getter");',
  25903. ' v2 = rtl.createCallback(this, "Getter");',
  25904. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  25905. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  25906. ' return Result;',
  25907. ' };',
  25908. '});',
  25909. 'this.GetIt = function (i) {',
  25910. ' var Result = 0;',
  25911. ' var v3 = undefined;',
  25912. ' v3 = $mod.GetIt;',
  25913. ' return Result;',
  25914. '};',
  25915. 'this.V = undefined;',
  25916. 'this.o = null;',
  25917. '']),
  25918. LinesToStr([ // $mod.$main
  25919. '$mod.V = $mod.GetIt;',
  25920. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  25921. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  25922. '']));
  25923. end;
  25924. procedure TTestModule.TestJSValue_ProcType_Equal;
  25925. begin
  25926. StartProgram(false);
  25927. Add('type');
  25928. Add(' integer = longint;');
  25929. Add(' TObject = class');
  25930. Add(' class function GetGlob: integer;');
  25931. Add(' function Getter: integer;');
  25932. Add(' end;');
  25933. Add('class function TObject.GetGlob: integer;');
  25934. Add('var v1: jsvalue;');
  25935. Add('begin');
  25936. Add(' if v1=@GetGlob then;');
  25937. Add(' if [email protected] then ;');
  25938. Add('end;');
  25939. Add('function TObject.Getter: integer;');
  25940. Add('var v2: jsvalue;');
  25941. Add('begin');
  25942. Add(' if v2=@Getter then;');
  25943. Add(' if [email protected] then ;');
  25944. Add(' if v2=@GetGlob then;');
  25945. Add(' if [email protected] then;');
  25946. Add('end;');
  25947. Add('function GetIt(i: integer): integer;');
  25948. Add('var v3: jsvalue;');
  25949. Add('begin');
  25950. Add(' if v3=@GetIt then;');
  25951. Add('end;');
  25952. Add('var');
  25953. Add(' V: JSValue;');
  25954. Add(' o: TObject;');
  25955. Add('begin');
  25956. Add(' if v=@GetIt then;');
  25957. Add(' if [email protected] then;');
  25958. Add(' if [email protected] then;');
  25959. Add(' if @GetIt=v then;');
  25960. Add(' if @o.Getter=v then;');
  25961. Add(' if @o.GetGlob=v then;');
  25962. ConvertProgram;
  25963. CheckSource('TestJSValue_ProcType_Equal',
  25964. LinesToStr([ // statements
  25965. 'rtl.createClass($mod, "TObject", null, function () {',
  25966. ' this.$init = function () {',
  25967. ' };',
  25968. ' this.$final = function () {',
  25969. ' };',
  25970. ' this.GetGlob = function () {',
  25971. ' var Result = 0;',
  25972. ' var v1 = undefined;',
  25973. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  25974. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  25975. ' return Result;',
  25976. ' };',
  25977. ' this.Getter = function () {',
  25978. ' var Result = 0;',
  25979. ' var v2 = undefined;',
  25980. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  25981. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  25982. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  25983. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  25984. ' return Result;',
  25985. ' };',
  25986. '});',
  25987. 'this.GetIt = function (i) {',
  25988. ' var Result = 0;',
  25989. ' var v3 = undefined;',
  25990. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  25991. ' return Result;',
  25992. '};',
  25993. 'this.V = undefined;',
  25994. 'this.o = null;',
  25995. '']),
  25996. LinesToStr([ // $mod.$main
  25997. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  25998. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  25999. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  26000. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  26001. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  26002. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  26003. '']));
  26004. end;
  26005. procedure TTestModule.TestJSValue_ProcType_Param;
  26006. begin
  26007. StartProgram(false);
  26008. Add([
  26009. 'type',
  26010. ' variant = jsvalue;',
  26011. ' TArrVariant = array of variant;',
  26012. ' TArrVar2 = TArrVariant;',
  26013. ' TFuncInt = function: longint;',
  26014. 'function GetIt: longint;',
  26015. 'begin',
  26016. 'end;',
  26017. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  26018. 'var v: variant;',
  26019. 'begin',
  26020. ' v:=arr[1];',
  26021. 'end;',
  26022. 'var s: string;',
  26023. 'begin',
  26024. ' DoIt(GetIt,[]);',
  26025. ' DoIt(@GetIt,[]);',
  26026. ' DoIt(1,[s,GetIt]);',
  26027. ' DoIt(1,[s,@GetIt]);',
  26028. '']);
  26029. ConvertProgram;
  26030. CheckSource('TestJSValue_ProcType_Param',
  26031. LinesToStr([ // statements
  26032. 'this.GetIt = function () {',
  26033. ' var Result = 0;',
  26034. ' return Result;',
  26035. '};',
  26036. 'this.DoIt = function (p, Arr) {',
  26037. ' var v = undefined;',
  26038. ' v = Arr[1];',
  26039. '};',
  26040. 'this.s = "";',
  26041. '']),
  26042. LinesToStr([ // $mod.$main
  26043. '$mod.DoIt($mod.GetIt(), []);',
  26044. '$mod.DoIt($mod.GetIt, []);',
  26045. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  26046. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  26047. '']));
  26048. end;
  26049. procedure TTestModule.TestJSValue_AssignToPointerFail;
  26050. begin
  26051. StartProgram(false);
  26052. Add([
  26053. 'var',
  26054. ' v: JSValue;',
  26055. ' p: Pointer;',
  26056. 'begin',
  26057. ' p:=v;',
  26058. '']);
  26059. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  26060. nIncompatibleTypesGotExpected);
  26061. ConvertProgram;
  26062. end;
  26063. procedure TTestModule.TestJSValue_OverloadDouble;
  26064. begin
  26065. StartProgram(false);
  26066. Add([
  26067. 'type',
  26068. ' integer = longint;',
  26069. ' tdatetime = double;',
  26070. 'procedure DoIt(d: double); begin end;',
  26071. 'procedure DoIt(v: jsvalue); begin end;',
  26072. 'var',
  26073. ' d: double;',
  26074. ' dt: tdatetime;',
  26075. ' i: integer;',
  26076. ' b: byte;',
  26077. ' shi: shortint;',
  26078. ' w: word;',
  26079. ' smi: smallint;',
  26080. ' lw: longword;',
  26081. ' li: longint;',
  26082. ' ni: nativeint;',
  26083. ' nu: nativeuint;',
  26084. 'begin',
  26085. ' DoIt(d);',
  26086. ' DoIt(dt);',
  26087. ' DoIt(i);',
  26088. ' DoIt(b);',
  26089. ' DoIt(shi);',
  26090. ' DoIt(w);',
  26091. ' DoIt(smi);',
  26092. ' DoIt(lw);',
  26093. ' DoIt(li);',
  26094. ' DoIt(ni);',
  26095. ' DoIt(nu);',
  26096. '']);
  26097. ConvertProgram;
  26098. CheckSource('TestJSValue_OverloadDouble',
  26099. LinesToStr([ // statements
  26100. 'this.DoIt = function (d) {',
  26101. '};',
  26102. 'this.DoIt$1 = function (v) {',
  26103. '};',
  26104. 'this.d = 0.0;',
  26105. 'this.dt = 0.0;',
  26106. 'this.i = 0;',
  26107. 'this.b = 0;',
  26108. 'this.shi = 0;',
  26109. 'this.w = 0;',
  26110. 'this.smi = 0;',
  26111. 'this.lw = 0;',
  26112. 'this.li = 0;',
  26113. 'this.ni = 0;',
  26114. 'this.nu = 0;',
  26115. '']),
  26116. LinesToStr([ // $mod.$main
  26117. '$mod.DoIt($mod.d);',
  26118. '$mod.DoIt($mod.dt);',
  26119. '$mod.DoIt$1($mod.i);',
  26120. '$mod.DoIt$1($mod.b);',
  26121. '$mod.DoIt$1($mod.shi);',
  26122. '$mod.DoIt$1($mod.w);',
  26123. '$mod.DoIt$1($mod.smi);',
  26124. '$mod.DoIt$1($mod.lw);',
  26125. '$mod.DoIt$1($mod.li);',
  26126. '$mod.DoIt$1($mod.ni);',
  26127. '$mod.DoIt$1($mod.nu);',
  26128. '']));
  26129. end;
  26130. procedure TTestModule.TestJSValue_OverloadNativeInt;
  26131. begin
  26132. StartProgram(false);
  26133. Add([
  26134. 'type',
  26135. ' integer = longint;',
  26136. ' int53 = nativeint;',
  26137. ' tdatetime = double;',
  26138. 'procedure DoIt(n: nativeint); begin end;',
  26139. 'procedure DoIt(v: jsvalue); begin end;',
  26140. 'var',
  26141. ' d: double;',
  26142. ' dt: tdatetime;',
  26143. ' i: integer;',
  26144. ' b: byte;',
  26145. ' shi: shortint;',
  26146. ' w: word;',
  26147. ' smi: smallint;',
  26148. ' lw: longword;',
  26149. ' li: longint;',
  26150. ' ni: nativeint;',
  26151. ' nu: nativeuint;',
  26152. 'begin',
  26153. ' DoIt(d);',
  26154. ' DoIt(dt);',
  26155. ' DoIt(i);',
  26156. ' DoIt(b);',
  26157. ' DoIt(shi);',
  26158. ' DoIt(w);',
  26159. ' DoIt(smi);',
  26160. ' DoIt(lw);',
  26161. ' DoIt(li);',
  26162. ' DoIt(ni);',
  26163. ' DoIt(nu);',
  26164. '']);
  26165. ConvertProgram;
  26166. CheckSource('TestJSValue_OverloadNativeInt',
  26167. LinesToStr([ // statements
  26168. 'this.DoIt = function (n) {',
  26169. '};',
  26170. 'this.DoIt$1 = function (v) {',
  26171. '};',
  26172. 'this.d = 0.0;',
  26173. 'this.dt = 0.0;',
  26174. 'this.i = 0;',
  26175. 'this.b = 0;',
  26176. 'this.shi = 0;',
  26177. 'this.w = 0;',
  26178. 'this.smi = 0;',
  26179. 'this.lw = 0;',
  26180. 'this.li = 0;',
  26181. 'this.ni = 0;',
  26182. 'this.nu = 0;',
  26183. '']),
  26184. LinesToStr([ // $mod.$main
  26185. '$mod.DoIt$1($mod.d);',
  26186. '$mod.DoIt$1($mod.dt);',
  26187. '$mod.DoIt($mod.i);',
  26188. '$mod.DoIt($mod.b);',
  26189. '$mod.DoIt($mod.shi);',
  26190. '$mod.DoIt($mod.w);',
  26191. '$mod.DoIt($mod.smi);',
  26192. '$mod.DoIt($mod.lw);',
  26193. '$mod.DoIt($mod.li);',
  26194. '$mod.DoIt($mod.ni);',
  26195. '$mod.DoIt($mod.nu);',
  26196. '']));
  26197. end;
  26198. procedure TTestModule.TestJSValue_OverloadWord;
  26199. begin
  26200. StartProgram(false);
  26201. Add([
  26202. 'type',
  26203. ' integer = longint;',
  26204. ' int53 = nativeint;',
  26205. ' tdatetime = double;',
  26206. 'procedure DoIt(w: word); begin end;',
  26207. 'procedure DoIt(v: jsvalue); begin end;',
  26208. 'var',
  26209. ' d: double;',
  26210. ' dt: tdatetime;',
  26211. ' i: integer;',
  26212. ' b: byte;',
  26213. ' shi: shortint;',
  26214. ' w: word;',
  26215. ' smi: smallint;',
  26216. ' lw: longword;',
  26217. ' li: longint;',
  26218. ' ni: nativeint;',
  26219. ' nu: nativeuint;',
  26220. 'begin',
  26221. ' DoIt(d);',
  26222. ' DoIt(dt);',
  26223. ' DoIt(i);',
  26224. ' DoIt(b);',
  26225. ' DoIt(shi);',
  26226. ' DoIt(w);',
  26227. ' DoIt(smi);',
  26228. ' DoIt(lw);',
  26229. ' DoIt(li);',
  26230. ' DoIt(ni);',
  26231. ' DoIt(nu);',
  26232. '']);
  26233. ConvertProgram;
  26234. CheckSource('TestJSValue_OverloadWord',
  26235. LinesToStr([ // statements
  26236. 'this.DoIt = function (w) {',
  26237. '};',
  26238. 'this.DoIt$1 = function (v) {',
  26239. '};',
  26240. 'this.d = 0.0;',
  26241. 'this.dt = 0.0;',
  26242. 'this.i = 0;',
  26243. 'this.b = 0;',
  26244. 'this.shi = 0;',
  26245. 'this.w = 0;',
  26246. 'this.smi = 0;',
  26247. 'this.lw = 0;',
  26248. 'this.li = 0;',
  26249. 'this.ni = 0;',
  26250. 'this.nu = 0;',
  26251. '']),
  26252. LinesToStr([ // $mod.$main
  26253. '$mod.DoIt$1($mod.d);',
  26254. '$mod.DoIt$1($mod.dt);',
  26255. '$mod.DoIt$1($mod.i);',
  26256. '$mod.DoIt($mod.b);',
  26257. '$mod.DoIt($mod.shi);',
  26258. '$mod.DoIt($mod.w);',
  26259. '$mod.DoIt$1($mod.smi);',
  26260. '$mod.DoIt$1($mod.lw);',
  26261. '$mod.DoIt$1($mod.li);',
  26262. '$mod.DoIt$1($mod.ni);',
  26263. '$mod.DoIt$1($mod.nu);',
  26264. '']));
  26265. end;
  26266. procedure TTestModule.TestJSValue_OverloadString;
  26267. begin
  26268. StartProgram(false);
  26269. Add([
  26270. 'type',
  26271. ' uni = string;',
  26272. ' WChar = char;',
  26273. 'procedure DoIt(s: string); begin end;',
  26274. 'procedure DoIt(v: jsvalue); begin end;',
  26275. 'var',
  26276. ' s: string;',
  26277. ' c: char;',
  26278. ' u: uni;',
  26279. 'begin',
  26280. ' DoIt(s);',
  26281. ' DoIt(c);',
  26282. ' DoIt(u);',
  26283. '']);
  26284. ConvertProgram;
  26285. CheckSource('TestJSValue_OverloadString',
  26286. LinesToStr([ // statements
  26287. 'this.DoIt = function (s) {',
  26288. '};',
  26289. 'this.DoIt$1 = function (v) {',
  26290. '};',
  26291. 'this.s = "";',
  26292. 'this.c = "";',
  26293. 'this.u = "";',
  26294. '']),
  26295. LinesToStr([ // $mod.$main
  26296. '$mod.DoIt($mod.s);',
  26297. '$mod.DoIt($mod.c);',
  26298. '$mod.DoIt($mod.u);',
  26299. '']));
  26300. end;
  26301. procedure TTestModule.TestJSValue_OverloadChar;
  26302. begin
  26303. StartProgram(false);
  26304. Add([
  26305. 'type',
  26306. ' uni = string;',
  26307. ' WChar = char;',
  26308. 'procedure DoIt(c: char); begin end;',
  26309. 'procedure DoIt(v: jsvalue); begin end;',
  26310. 'var',
  26311. ' s: string;',
  26312. ' c: char;',
  26313. ' u: uni;',
  26314. 'begin',
  26315. ' DoIt(s);',
  26316. ' DoIt(c);',
  26317. ' DoIt(u);',
  26318. '']);
  26319. ConvertProgram;
  26320. CheckSource('TestJSValue_OverloadChar',
  26321. LinesToStr([ // statements
  26322. 'this.DoIt = function (c) {',
  26323. '};',
  26324. 'this.DoIt$1 = function (v) {',
  26325. '};',
  26326. 'this.s = "";',
  26327. 'this.c = "";',
  26328. 'this.u = "";',
  26329. '']),
  26330. LinesToStr([ // $mod.$main
  26331. '$mod.DoIt$1($mod.s);',
  26332. '$mod.DoIt($mod.c);',
  26333. '$mod.DoIt$1($mod.u);',
  26334. '']));
  26335. end;
  26336. procedure TTestModule.TestJSValue_OverloadPointer;
  26337. begin
  26338. StartProgram(false);
  26339. Add([
  26340. 'type',
  26341. ' TObject = class end;',
  26342. 'procedure DoIt(p: pointer); begin end;',
  26343. 'procedure DoIt(v: jsvalue); begin end;',
  26344. 'var',
  26345. ' o: TObject;',
  26346. 'begin',
  26347. ' DoIt(o);',
  26348. '']);
  26349. ConvertProgram;
  26350. CheckSource('TestJSValue_OverloadPointer',
  26351. LinesToStr([ // statements
  26352. 'rtl.createClass($mod, "TObject", null, function () {',
  26353. ' this.$init = function () {',
  26354. ' };',
  26355. ' this.$final = function () {',
  26356. ' };',
  26357. '});',
  26358. 'this.DoIt = function (p) {',
  26359. '};',
  26360. 'this.DoIt$1 = function (v) {',
  26361. '};',
  26362. 'this.o = null;',
  26363. '']),
  26364. LinesToStr([ // $mod.$main
  26365. '$mod.DoIt($mod.o);',
  26366. '']));
  26367. end;
  26368. procedure TTestModule.TestJSValue_ForIn;
  26369. begin
  26370. StartProgram(false);
  26371. Add([
  26372. 'var',
  26373. ' v: JSValue;',
  26374. ' key: string;',
  26375. 'begin',
  26376. ' for key in v do begin',
  26377. ' if key=''abc'' then ;',
  26378. ' end;',
  26379. '']);
  26380. ConvertProgram;
  26381. CheckSource('TestJSValue_ForIn',
  26382. LinesToStr([ // statements
  26383. 'this.v = undefined;',
  26384. 'this.key = "";',
  26385. '']),
  26386. LinesToStr([ // $mod.$main
  26387. 'for ($mod.key in $mod.v) {',
  26388. ' if ($mod.key === "abc") ;',
  26389. '};',
  26390. '']));
  26391. end;
  26392. procedure TTestModule.TestRTTI_IntRange;
  26393. begin
  26394. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26395. StartProgram(false);
  26396. Add([
  26397. '{$modeswitch externalclass}',
  26398. 'type',
  26399. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  26400. ' end;',
  26401. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  26402. ' end;',
  26403. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  26404. ' TColor = type TGraphicsColor;',
  26405. 'var',
  26406. ' p: TTypeInfo;',
  26407. 'begin',
  26408. ' p:=typeinfo(TGraphicsColor);',
  26409. ' p:=typeinfo(TColor);',
  26410. '']);
  26411. ConvertProgram;
  26412. CheckSource('TestRTTI_IntRange',
  26413. LinesToStr([ // statements
  26414. '$mod.$rtti.$Int("TGraphicsColor", {',
  26415. ' minvalue: -2147483648,',
  26416. ' maxvalue: 2147483647,',
  26417. ' ordtype: 4',
  26418. '});',
  26419. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  26420. 'this.p = null;',
  26421. '']),
  26422. LinesToStr([ // $mod.$main
  26423. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  26424. '$mod.p = $mod.$rtti["TColor"];',
  26425. '']));
  26426. end;
  26427. procedure TTestModule.TestRTTI_Double;
  26428. begin
  26429. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26430. StartProgram(false);
  26431. Add([
  26432. '{$modeswitch externalclass}',
  26433. 'type',
  26434. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  26435. ' end;',
  26436. ' TFloat = type double;',
  26437. 'var',
  26438. ' p: TTypeInfo;',
  26439. 'begin',
  26440. ' p:=typeinfo(double);',
  26441. ' p:=typeinfo(TFloat);',
  26442. '']);
  26443. ConvertProgram;
  26444. CheckSource('TestRTTI_Double',
  26445. LinesToStr([ // statements
  26446. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  26447. 'this.p = null;',
  26448. '']),
  26449. LinesToStr([ // $mod.$main
  26450. '$mod.p = rtl.double;',
  26451. '$mod.p = $mod.$rtti["TFloat"];',
  26452. '']));
  26453. end;
  26454. procedure TTestModule.TestRTTI_ProcType;
  26455. begin
  26456. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26457. StartProgram(false);
  26458. Add('type');
  26459. Add(' TProcA = procedure;');
  26460. Add(' TMethodB = procedure of object;');
  26461. Add(' TProcC = procedure; varargs;');
  26462. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  26463. Add(' TProcE = function: nativeint;');
  26464. Add(' TProcF = function(const p: TProcA): nativeuint;');
  26465. Add('var p: pointer;');
  26466. Add('begin');
  26467. Add(' p:=typeinfo(tproca);');
  26468. ConvertProgram;
  26469. CheckSource('TestRTTI_ProcType',
  26470. LinesToStr([ // statements
  26471. '$mod.$rtti.$ProcVar("TProcA", {',
  26472. ' procsig: rtl.newTIProcSig(null)',
  26473. '});',
  26474. '$mod.$rtti.$MethodVar("TMethodB", {',
  26475. ' procsig: rtl.newTIProcSig(null),',
  26476. ' methodkind: 0',
  26477. '});',
  26478. '$mod.$rtti.$ProcVar("TProcC", {',
  26479. ' procsig: rtl.newTIProcSig(null, 2)',
  26480. '});',
  26481. '$mod.$rtti.$ProcVar("TProcD", {',
  26482. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  26483. '});',
  26484. '$mod.$rtti.$ProcVar("TProcE", {',
  26485. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  26486. '});',
  26487. '$mod.$rtti.$ProcVar("TProcF", {',
  26488. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  26489. '});',
  26490. 'this.p = null;',
  26491. '']),
  26492. LinesToStr([ // $mod.$main
  26493. '$mod.p = $mod.$rtti["TProcA"];',
  26494. '']));
  26495. end;
  26496. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  26497. begin
  26498. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26499. AddModuleWithIntfImplSrc('unit2.pas',
  26500. LinesToStr([
  26501. 'type',
  26502. ' TObject = class end;'
  26503. ]),
  26504. '');
  26505. StartUnit(true);
  26506. Add('interface');
  26507. Add('uses unit2;');
  26508. Add('type');
  26509. Add(' TProcA = function(o: tobject): tobject;');
  26510. Add('implementation');
  26511. Add('type');
  26512. Add(' TProcB = function(o: tobject): tobject;');
  26513. Add('var p: Pointer;');
  26514. Add('initialization');
  26515. Add(' p:=typeinfo(tproca);');
  26516. Add(' p:=typeinfo(tprocb);');
  26517. ConvertUnit;
  26518. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  26519. LinesToStr([ // statements
  26520. 'var $impl = $mod.$impl;',
  26521. '$mod.$rtti.$ProcVar("TProcA", {',
  26522. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  26523. '});',
  26524. '']),
  26525. LinesToStr([ // this.$init
  26526. '$impl.p = $mod.$rtti["TProcA"];',
  26527. '$impl.p = $mod.$rtti["TProcB"];',
  26528. '']),
  26529. LinesToStr([ // implementation
  26530. '$mod.$rtti.$ProcVar("TProcB", {',
  26531. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  26532. '});',
  26533. '$impl.p = null;',
  26534. '']) );
  26535. end;
  26536. procedure TTestModule.TestRTTI_EnumAndSetType;
  26537. begin
  26538. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26539. StartProgram(false);
  26540. Add('type');
  26541. Add(' TFlag = (light,dark);');
  26542. Add(' TFlags = set of TFlag;');
  26543. Add(' TProc = function(f: TFlags): TFlag;');
  26544. Add('var p: pointer;');
  26545. Add('begin');
  26546. Add(' p:=typeinfo(tflag);');
  26547. Add(' p:=typeinfo(tflags);');
  26548. ConvertProgram;
  26549. CheckSource('TestRTTI_EnumAndType',
  26550. LinesToStr([ // statements
  26551. 'this.TFlag = {',
  26552. ' "0": "light",',
  26553. ' light: 0,',
  26554. ' "1": "dark",',
  26555. ' dark: 1',
  26556. '};',
  26557. '$mod.$rtti.$Enum("TFlag", {',
  26558. ' minvalue: 0,',
  26559. ' maxvalue: 1,',
  26560. ' ordtype: 1,',
  26561. ' enumtype: this.TFlag',
  26562. '});',
  26563. '$mod.$rtti.$Set("TFlags", {',
  26564. ' comptype: $mod.$rtti["TFlag"]',
  26565. '});',
  26566. '$mod.$rtti.$ProcVar("TProc", {',
  26567. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  26568. '});',
  26569. 'this.p = null;',
  26570. '']),
  26571. LinesToStr([ // $mod.$main
  26572. '$mod.p = $mod.$rtti["TFlag"];',
  26573. '$mod.p = $mod.$rtti["TFlags"];',
  26574. '']));
  26575. end;
  26576. procedure TTestModule.TestRTTI_EnumRange;
  26577. begin
  26578. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26579. StartProgram(false);
  26580. Add([
  26581. 'type',
  26582. ' TCol = (red,green,blue);',
  26583. ' TColRg = green..blue;',
  26584. ' TSetOfColRg = set of TColRg;',
  26585. 'var p: pointer;',
  26586. 'begin',
  26587. ' p:=typeinfo(tcolrg);',
  26588. ' p:=typeinfo(tsetofcolrg);',
  26589. '']);
  26590. ConvertProgram;
  26591. end;
  26592. procedure TTestModule.TestRTTI_AnonymousEnumType;
  26593. begin
  26594. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26595. StartProgram(false);
  26596. Add('type');
  26597. Add(' TFlags = set of (red, green);');
  26598. Add('var');
  26599. Add(' f: TFlags;');
  26600. Add('begin');
  26601. Add(' Include(f,red);');
  26602. ConvertProgram;
  26603. CheckSource('TestRTTI_AnonymousEnumType',
  26604. LinesToStr([ // statements
  26605. 'this.TFlags$a = {',
  26606. ' "0": "red",',
  26607. ' red: 0,',
  26608. ' "1": "green",',
  26609. ' green: 1',
  26610. '};',
  26611. '$mod.$rtti.$Enum("TFlags$a", {',
  26612. ' minvalue: 0,',
  26613. ' maxvalue: 1,',
  26614. ' ordtype: 1,',
  26615. ' enumtype: this.TFlags$a',
  26616. '});',
  26617. '$mod.$rtti.$Set("TFlags", {',
  26618. ' comptype: $mod.$rtti["TFlags$a"]',
  26619. '});',
  26620. 'this.f = {};',
  26621. '']),
  26622. LinesToStr([
  26623. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  26624. '']));
  26625. end;
  26626. procedure TTestModule.TestRTTI_StaticArray;
  26627. begin
  26628. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26629. StartProgram(false);
  26630. Add('type');
  26631. Add(' TFlag = (light,dark);');
  26632. Add(' TFlagNames = array[TFlag] of string;');
  26633. Add(' TBoolNames = array[boolean] of string;');
  26634. Add(' TByteArray = array[1..32768] of byte;');
  26635. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  26636. Add('var p: pointer;');
  26637. Add('begin');
  26638. Add(' p:=typeinfo(TFlagNames);');
  26639. Add(' p:=typeinfo(TBoolNames);');
  26640. ConvertProgram;
  26641. CheckSource('TestRTTI_StaticArray',
  26642. LinesToStr([ // statements
  26643. 'this.TFlag = {',
  26644. ' "0": "light",',
  26645. ' light: 0,',
  26646. ' "1": "dark",',
  26647. ' dark: 1',
  26648. '};',
  26649. '$mod.$rtti.$Enum("TFlag", {',
  26650. ' minvalue: 0,',
  26651. ' maxvalue: 1,',
  26652. ' ordtype: 1,',
  26653. ' enumtype: this.TFlag',
  26654. '});',
  26655. '$mod.$rtti.$StaticArray("TFlagNames", {',
  26656. ' dims: [2],',
  26657. ' eltype: rtl.string',
  26658. '});',
  26659. '$mod.$rtti.$StaticArray("TBoolNames", {',
  26660. ' dims: [2],',
  26661. ' eltype: rtl.string',
  26662. '});',
  26663. '$mod.$rtti.$StaticArray("TByteArray", {',
  26664. ' dims: [32768],',
  26665. ' eltype: rtl.byte',
  26666. '});',
  26667. '$mod.$rtti.$ProcVar("TProc", {',
  26668. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  26669. '});',
  26670. 'this.p = null;',
  26671. '']),
  26672. LinesToStr([ // $mod.$main
  26673. '$mod.p = $mod.$rtti["TFlagNames"];',
  26674. '$mod.p = $mod.$rtti["TBoolNames"];',
  26675. '']));
  26676. end;
  26677. procedure TTestModule.TestRTTI_DynArray;
  26678. begin
  26679. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26680. StartProgram(false);
  26681. Add('type');
  26682. Add(' TArrStr = array of string;');
  26683. Add(' TArr2Dim = array of tarrstr;');
  26684. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  26685. Add('var p: pointer;');
  26686. Add('begin');
  26687. Add(' p:=typeinfo(tarrstr);');
  26688. Add(' p:=typeinfo(tarr2dim);');
  26689. ConvertProgram;
  26690. CheckSource('TestRTTI_DynArray',
  26691. LinesToStr([ // statements
  26692. '$mod.$rtti.$DynArray("TArrStr", {',
  26693. ' eltype: rtl.string',
  26694. '});',
  26695. '$mod.$rtti.$DynArray("TArr2Dim", {',
  26696. ' eltype: $mod.$rtti["TArrStr"]',
  26697. '});',
  26698. '$mod.$rtti.$ProcVar("TProc", {',
  26699. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  26700. '});',
  26701. 'this.p = null;',
  26702. '']),
  26703. LinesToStr([ // $mod.$main
  26704. '$mod.p = $mod.$rtti["TArrStr"];',
  26705. '$mod.p = $mod.$rtti["TArr2Dim"];',
  26706. '']));
  26707. end;
  26708. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  26709. begin
  26710. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26711. StartProgram(false);
  26712. Add('type');
  26713. Add(' TArr = array of array of longint;');
  26714. Add('var a: TArr;');
  26715. Add('begin');
  26716. ConvertProgram;
  26717. CheckSource('TestRTTI_ArrayNestedAnonymous',
  26718. LinesToStr([ // statements
  26719. '$mod.$rtti.$DynArray("TArr$a", {',
  26720. ' eltype: rtl.longint',
  26721. '});',
  26722. '$mod.$rtti.$DynArray("TArr", {',
  26723. ' eltype: $mod.$rtti["TArr$a"]',
  26724. '});',
  26725. 'this.a = [];',
  26726. '']),
  26727. LinesToStr([ // $mod.$main
  26728. ]));
  26729. end;
  26730. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  26731. begin
  26732. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26733. StartProgram(false);
  26734. Add('type');
  26735. Add(' TObject = class');
  26736. Add(' published');
  26737. Add(' procedure Proc; virtual; abstract;');
  26738. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  26739. Add(' end;');
  26740. Add('begin');
  26741. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  26742. nDuplicatePublishedMethodXAtY);
  26743. ConvertProgram;
  26744. end;
  26745. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  26746. begin
  26747. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26748. StartProgram(false);
  26749. Add('type');
  26750. Add(' TObject = class');
  26751. Add(' published');
  26752. Add(' procedure Proc; external name ''foo'';');
  26753. Add(' end;');
  26754. Add('begin');
  26755. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  26756. nPublishedNameMustMatchExternal);
  26757. ConvertProgram;
  26758. end;
  26759. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  26760. begin
  26761. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26762. StartProgram(false);
  26763. Add('type');
  26764. Add(' TObject = class');
  26765. Add(' class var FA: longint;');
  26766. Add(' published');
  26767. Add(' class property A: longint read FA;');
  26768. Add(' end;');
  26769. Add('begin');
  26770. SetExpectedPasResolverError('Invalid published property modifier "class"',
  26771. nInvalidXModifierY);
  26772. ConvertProgram;
  26773. end;
  26774. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  26775. begin
  26776. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26777. StartProgram(false);
  26778. Add('type');
  26779. Add(' TObject = class');
  26780. Add(' published');
  26781. Add(' class var FA: longint;');
  26782. Add(' end;');
  26783. Add('begin');
  26784. SetExpectedPasResolverError(sSymbolCannotBePublished,
  26785. nSymbolCannotBePublished);
  26786. ConvertProgram;
  26787. end;
  26788. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  26789. begin
  26790. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26791. StartProgram(false);
  26792. Add('{$modeswitch externalclass}');
  26793. Add('type');
  26794. Add(' TObject = class');
  26795. Add(' published');
  26796. Add(' V: longint; external name ''foo'';');
  26797. Add(' end;');
  26798. Add('begin');
  26799. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  26800. nPublishedNameMustMatchExternal);
  26801. ConvertProgram;
  26802. end;
  26803. procedure TTestModule.TestRTTI_Class_Field;
  26804. begin
  26805. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26806. StartProgram(false);
  26807. Add('{$modeswitch externalclass}');
  26808. Add('type');
  26809. Add(' TObject = class');
  26810. Add(' private');
  26811. Add(' FPropA: string;');
  26812. Add(' published');
  26813. Add(' VarLI: longint;');
  26814. Add(' VarC: char;');
  26815. Add(' VarS: string;');
  26816. Add(' VarD: double;');
  26817. Add(' VarB: boolean;');
  26818. Add(' VarLW: longword;');
  26819. Add(' VarSmI: smallint;');
  26820. Add(' VarW: word;');
  26821. Add(' VarShI: shortint;');
  26822. Add(' VarBy: byte;');
  26823. Add(' VarExt: longint external name ''VarExt'';');
  26824. Add(' end;');
  26825. Add('var p: pointer;');
  26826. Add(' Obj: tobject;');
  26827. Add('begin');
  26828. Add(' p:=typeinfo(tobject);');
  26829. Add(' p:=typeinfo(p);');
  26830. Add(' p:=typeinfo(obj);');
  26831. ConvertProgram;
  26832. CheckSource('TestRTTI_Class_Field',
  26833. LinesToStr([ // statements
  26834. 'rtl.createClass($mod, "TObject", null, function () {',
  26835. ' this.$init = function () {',
  26836. ' this.FPropA = "";',
  26837. ' this.VarLI = 0;',
  26838. ' this.VarC = "";',
  26839. ' this.VarS = "";',
  26840. ' this.VarD = 0.0;',
  26841. ' this.VarB = false;',
  26842. ' this.VarLW = 0;',
  26843. ' this.VarSmI = 0;',
  26844. ' this.VarW = 0;',
  26845. ' this.VarShI = 0;',
  26846. ' this.VarBy = 0;',
  26847. ' };',
  26848. ' this.$final = function () {',
  26849. ' };',
  26850. ' var $r = this.$rtti;',
  26851. ' $r.addField("VarLI", rtl.longint);',
  26852. ' $r.addField("VarC", rtl.char);',
  26853. ' $r.addField("VarS", rtl.string);',
  26854. ' $r.addField("VarD", rtl.double);',
  26855. ' $r.addField("VarB", rtl.boolean);',
  26856. ' $r.addField("VarLW", rtl.longword);',
  26857. ' $r.addField("VarSmI", rtl.smallint);',
  26858. ' $r.addField("VarW", rtl.word);',
  26859. ' $r.addField("VarShI", rtl.shortint);',
  26860. ' $r.addField("VarBy", rtl.byte);',
  26861. ' $r.addField("VarExt", rtl.longint);',
  26862. '});',
  26863. 'this.p = null;',
  26864. 'this.Obj = null;',
  26865. '']),
  26866. LinesToStr([ // $mod.$main
  26867. '$mod.p = $mod.$rtti["TObject"];',
  26868. '$mod.p = rtl.pointer;',
  26869. '$mod.p = $mod.Obj.$rtti;',
  26870. '']));
  26871. end;
  26872. procedure TTestModule.TestRTTI_Class_Method;
  26873. begin
  26874. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26875. StartProgram(false);
  26876. Add('type');
  26877. Add(' TObject = class');
  26878. Add(' private');
  26879. Add(' procedure Internal; external name ''$intern'';');
  26880. Add(' published');
  26881. Add(' procedure Click; virtual; abstract;');
  26882. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  26883. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  26884. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  26885. Add(' end;');
  26886. Add('begin');
  26887. ConvertProgram;
  26888. CheckSource('TestRTTI_Class_Method',
  26889. LinesToStr([ // statements
  26890. 'rtl.createClass($mod, "TObject", null, function () {',
  26891. ' this.$init = function () {',
  26892. ' };',
  26893. ' this.$final = function () {',
  26894. ' };',
  26895. ' var $r = this.$rtti;',
  26896. ' $r.addMethod("Click", 0, null);',
  26897. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  26898. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  26899. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  26900. ' flags: 2',
  26901. ' });',
  26902. '});',
  26903. '']),
  26904. LinesToStr([ // $mod.$main
  26905. '']));
  26906. end;
  26907. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  26908. begin
  26909. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26910. StartProgram(false);
  26911. Add('type');
  26912. Add(' TObject = class');
  26913. Add(' published');
  26914. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  26915. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  26916. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  26917. Add(' end;');
  26918. Add('begin');
  26919. ConvertProgram;
  26920. CheckSource('TestRTTI_Class_MethodOpenArray',
  26921. LinesToStr([ // statements
  26922. 'rtl.createClass($mod, "TObject", null, function () {',
  26923. ' this.$init = function () {',
  26924. ' };',
  26925. ' this.$final = function () {',
  26926. ' };',
  26927. ' var $r = this.$rtti;',
  26928. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  26929. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  26930. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  26931. '});',
  26932. '']),
  26933. LinesToStr([ // $mod.$main
  26934. '']));
  26935. end;
  26936. procedure TTestModule.TestRTTI_Class_Property;
  26937. begin
  26938. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26939. StartProgram(false);
  26940. Add('{$modeswitch externalclass}');
  26941. Add('type');
  26942. Add(' TObject = class');
  26943. Add(' private');
  26944. Add(' FColor: longint;');
  26945. Add(' FColorStored: boolean;');
  26946. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  26947. Add(' function GetColor: longint; virtual; abstract;');
  26948. Add(' function GetColorStored: boolean; virtual; abstract;');
  26949. Add(' FExtSize: longint external name ''$extSize'';');
  26950. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  26951. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  26952. Add(' function GetExtSize: longint; external name ''$getSize'';');
  26953. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  26954. Add(' published');
  26955. Add(' property ColorA: longint read FColor;');
  26956. Add(' property ColorB: longint write FColor;');
  26957. Add(' property ColorC: longint read GetColor write SetColor;');
  26958. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  26959. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  26960. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  26961. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  26962. Add(' end;');
  26963. Add('begin');
  26964. ConvertProgram;
  26965. CheckSource('TestRTTI_Class_Property',
  26966. LinesToStr([ // statements
  26967. 'rtl.createClass($mod, "TObject", null, function () {',
  26968. ' this.$init = function () {',
  26969. ' this.FColor = 0;',
  26970. ' this.FColorStored = false;',
  26971. ' };',
  26972. ' this.$final = function () {',
  26973. ' };',
  26974. ' var $r = this.$rtti;',
  26975. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  26976. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  26977. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  26978. ' $r.addProperty(',
  26979. ' "ColorD",',
  26980. ' 8,',
  26981. ' rtl.longint,',
  26982. ' "FColor",',
  26983. ' "FColor",',
  26984. ' {',
  26985. ' stored: "FColorStored"',
  26986. ' }',
  26987. ' );',
  26988. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  26989. ' $r.addProperty(',
  26990. ' "ExtSizeB",',
  26991. ' 11,',
  26992. ' rtl.longint,',
  26993. ' "$getSize",',
  26994. ' "$setSize",',
  26995. ' {',
  26996. ' stored: "$extSizeStored"',
  26997. ' }',
  26998. ' );',
  26999. ' $r.addProperty(',
  27000. ' "ExtSizeC",',
  27001. ' 12,',
  27002. ' rtl.longint,',
  27003. ' "$extSize",',
  27004. ' "$extSize",',
  27005. ' {',
  27006. ' stored: "$getExtSizeStored"',
  27007. ' }',
  27008. ' );',
  27009. '});',
  27010. '']),
  27011. LinesToStr([ // $mod.$main
  27012. '']));
  27013. end;
  27014. procedure TTestModule.TestRTTI_Class_PropertyParams;
  27015. begin
  27016. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27017. StartProgram(false);
  27018. Add('{$modeswitch externalclass}');
  27019. Add('type');
  27020. Add(' integer = longint;');
  27021. Add(' TObject = class');
  27022. Add(' private');
  27023. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  27024. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  27025. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  27026. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  27027. Add(' published');
  27028. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  27029. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  27030. Add(' end;');
  27031. Add('begin');
  27032. ConvertProgram;
  27033. CheckSource('TestRTTI_Class_PropertyParams',
  27034. LinesToStr([ // statements
  27035. 'rtl.createClass($mod, "TObject", null, function () {',
  27036. ' this.$init = function () {',
  27037. ' };',
  27038. ' this.$final = function () {',
  27039. ' };',
  27040. ' var $r = this.$rtti;',
  27041. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  27042. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  27043. '});',
  27044. '']),
  27045. LinesToStr([ // $mod.$main
  27046. '']));
  27047. end;
  27048. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  27049. begin
  27050. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27051. AddModuleWithIntfImplSrc('unit1.pas',
  27052. 'type TColor = -5..5;',
  27053. '');
  27054. StartProgram(true);
  27055. Add([
  27056. 'uses unit1;',
  27057. 'type',
  27058. ' TColorAlias = TColor;',
  27059. ' TColorTypeAlias = type TColor;',
  27060. ' TObject = class',
  27061. ' private',
  27062. ' fColor: TColor;',
  27063. ' fAlias: TColorAlias;',
  27064. ' fTypeAlias: TColorTypeAlias;',
  27065. ' published',
  27066. ' property Color: TColor read fcolor;',
  27067. ' property Alias: TColorAlias read falias;',
  27068. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  27069. ' end;',
  27070. 'begin',
  27071. '']);
  27072. ConvertProgram;
  27073. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  27074. LinesToStr([ // statements
  27075. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  27076. 'rtl.createClass($mod, "TObject", null, function () {',
  27077. ' this.$init = function () {',
  27078. ' this.fColor = 0;',
  27079. ' this.fAlias = 0;',
  27080. ' this.fTypeAlias = 0;',
  27081. ' };',
  27082. ' this.$final = function () {',
  27083. ' };',
  27084. ' var $r = this.$rtti;',
  27085. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  27086. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  27087. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  27088. '});',
  27089. '']),
  27090. LinesToStr([ // $mod.$main
  27091. '']));
  27092. end;
  27093. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  27094. begin
  27095. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27096. StartProgram(false);
  27097. Add([
  27098. '{$modeswitch omitrtti}',
  27099. 'type',
  27100. ' TObject = class',
  27101. ' private',
  27102. ' FA: byte;',
  27103. ' published',
  27104. ' property A: byte read FA write FA;',
  27105. ' end;',
  27106. 'begin']);
  27107. ConvertProgram;
  27108. CheckSource('TestRTTI_Class_OmitRTTI',
  27109. LinesToStr([ // statements
  27110. 'rtl.createClass($mod, "TObject", null, function () {',
  27111. ' this.$init = function () {',
  27112. ' this.FA = 0;',
  27113. ' };',
  27114. ' this.$final = function () {',
  27115. ' };',
  27116. '});',
  27117. '']),
  27118. LinesToStr([ // $mod.$main
  27119. '']));
  27120. end;
  27121. procedure TTestModule.TestRTTI_IndexModifier;
  27122. begin
  27123. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27124. StartProgram(false);
  27125. Add([
  27126. 'type',
  27127. ' TEnum = (red, blue);',
  27128. ' TObject = class',
  27129. ' FB: boolean;',
  27130. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  27131. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  27132. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  27133. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  27134. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  27135. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  27136. ' published',
  27137. ' property B1: boolean index 1 read FB write SetIntBool;',
  27138. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  27139. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  27140. ' end;',
  27141. 'begin']);
  27142. ConvertProgram;
  27143. CheckSource('TestRTTI_IndexModifier',
  27144. LinesToStr([ // statements
  27145. 'this.TEnum = {',
  27146. ' "0": "red",',
  27147. ' red: 0,',
  27148. ' "1": "blue",',
  27149. ' blue: 1',
  27150. '};',
  27151. '$mod.$rtti.$Enum("TEnum", {',
  27152. ' minvalue: 0,',
  27153. ' maxvalue: 1,',
  27154. ' ordtype: 1,',
  27155. ' enumtype: this.TEnum',
  27156. '});',
  27157. 'rtl.createClass($mod, "TObject", null, function () {',
  27158. ' this.$init = function () {',
  27159. ' this.FB = false;',
  27160. ' };',
  27161. ' this.$final = function () {',
  27162. ' };',
  27163. ' var $r = this.$rtti;',
  27164. ' $r.addProperty(',
  27165. ' "B1",',
  27166. ' 18,',
  27167. ' rtl.boolean,',
  27168. ' "FB",',
  27169. ' "SetIntBool",',
  27170. ' {',
  27171. ' index: 1',
  27172. ' }',
  27173. ' );',
  27174. ' $r.addProperty(',
  27175. ' "B2",',
  27176. ' 17,',
  27177. ' rtl.boolean,',
  27178. ' "GetEnumBool",',
  27179. ' "FB",',
  27180. ' {',
  27181. ' index: $mod.TEnum.blue',
  27182. ' }',
  27183. ' );',
  27184. ' $r.addProperty(',
  27185. ' "I1",',
  27186. ' 19,',
  27187. ' rtl.boolean,',
  27188. ' "GetStrIntBool",',
  27189. ' "SetStrIntBool",',
  27190. ' {',
  27191. ' index: 2',
  27192. ' }',
  27193. ' );',
  27194. '});',
  27195. '']),
  27196. LinesToStr([ // $mod.$main
  27197. '']));
  27198. end;
  27199. procedure TTestModule.TestRTTI_StoredModifier;
  27200. begin
  27201. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27202. StartProgram(false);
  27203. Add([
  27204. 'const',
  27205. ' ConstB = true;',
  27206. 'type',
  27207. ' TObject = class',
  27208. ' private',
  27209. ' FB: boolean;',
  27210. ' function IsBStored: boolean; virtual; abstract;',
  27211. ' published',
  27212. ' property BoolA: boolean read FB stored true;',
  27213. ' property BoolB: boolean read FB stored false;',
  27214. ' property BoolC: boolean read FB stored FB;',
  27215. ' property BoolD: boolean read FB stored ConstB;',
  27216. ' property BoolE: boolean read FB stored IsBStored;',
  27217. ' end;',
  27218. 'begin']);
  27219. ConvertProgram;
  27220. CheckSource('TestRTTI_StoredModifier',
  27221. LinesToStr([ // statements
  27222. 'this.ConstB = true;',
  27223. 'rtl.createClass($mod, "TObject", null, function () {',
  27224. ' this.$init = function () {',
  27225. ' this.FB = false;',
  27226. ' };',
  27227. ' this.$final = function () {',
  27228. ' };',
  27229. ' var $r = this.$rtti;',
  27230. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  27231. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  27232. ' $r.addProperty(',
  27233. ' "BoolC",',
  27234. ' 8,',
  27235. ' rtl.boolean,',
  27236. ' "FB",',
  27237. ' "",',
  27238. ' {',
  27239. ' stored: "FB"',
  27240. ' }',
  27241. ' );',
  27242. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  27243. ' $r.addProperty(',
  27244. ' "BoolE",',
  27245. ' 12,',
  27246. ' rtl.boolean,',
  27247. ' "FB",',
  27248. ' "",',
  27249. ' {',
  27250. ' stored: "IsBStored"',
  27251. ' }',
  27252. ' );',
  27253. '});',
  27254. '']),
  27255. LinesToStr([ // $mod.$main
  27256. '']));
  27257. end;
  27258. procedure TTestModule.TestRTTI_DefaultValue;
  27259. begin
  27260. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27261. StartProgram(false);
  27262. Add([
  27263. 'type',
  27264. ' TEnum = (red, blue);',
  27265. 'const',
  27266. ' CB = true or false;',
  27267. ' CI = 1+2;',
  27268. 'type',
  27269. ' TObject = class',
  27270. ' FB: boolean;',
  27271. ' FI: longint;',
  27272. ' FE: TEnum;',
  27273. ' published',
  27274. ' property B1: boolean read FB default true;',
  27275. ' property B2: boolean read FB default CB;',
  27276. ' property B3: boolean read FB default test1.cb;',
  27277. ' property I1: longint read FI default 2;',
  27278. ' property I2: longint read FI default CI;',
  27279. ' property E1: TEnum read FE default red;',
  27280. ' property E2: TEnum read FE default TEnum.blue;',
  27281. ' end;',
  27282. 'begin']);
  27283. ConvertProgram;
  27284. CheckSource('TestRTTI_DefaultValue',
  27285. LinesToStr([ // statements
  27286. 'this.TEnum = {',
  27287. ' "0": "red",',
  27288. ' red: 0,',
  27289. ' "1": "blue",',
  27290. ' blue: 1',
  27291. '};',
  27292. '$mod.$rtti.$Enum("TEnum", {',
  27293. ' minvalue: 0,',
  27294. ' maxvalue: 1,',
  27295. ' ordtype: 1,',
  27296. ' enumtype: this.TEnum',
  27297. '});',
  27298. 'this.CB = true || false;',
  27299. 'this.CI = 1 + 2;',
  27300. 'rtl.createClass($mod, "TObject", null, function () {',
  27301. ' this.$init = function () {',
  27302. ' this.FB = false;',
  27303. ' this.FI = 0;',
  27304. ' this.FE = 0;',
  27305. ' };',
  27306. ' this.$final = function () {',
  27307. ' };',
  27308. ' var $r = this.$rtti;',
  27309. ' $r.addProperty(',
  27310. ' "B1",',
  27311. ' 0,',
  27312. ' rtl.boolean,',
  27313. ' "FB",',
  27314. ' "",',
  27315. ' {',
  27316. ' Default: true',
  27317. ' }',
  27318. ' );',
  27319. ' $r.addProperty(',
  27320. ' "B2",',
  27321. ' 0,',
  27322. ' rtl.boolean,',
  27323. ' "FB",',
  27324. ' "",',
  27325. ' {',
  27326. ' Default: true',
  27327. ' }',
  27328. ' );',
  27329. ' $r.addProperty(',
  27330. ' "B3",',
  27331. ' 0,',
  27332. ' rtl.boolean,',
  27333. ' "FB",',
  27334. ' "",',
  27335. ' {',
  27336. ' Default: true',
  27337. ' }',
  27338. ' );',
  27339. ' $r.addProperty(',
  27340. ' "I1",',
  27341. ' 0,',
  27342. ' rtl.longint,',
  27343. ' "FI",',
  27344. ' "",',
  27345. ' {',
  27346. ' Default: 2',
  27347. ' }',
  27348. ' );',
  27349. ' $r.addProperty(',
  27350. ' "I2",',
  27351. ' 0,',
  27352. ' rtl.longint,',
  27353. ' "FI",',
  27354. ' "",',
  27355. ' {',
  27356. ' Default: 3',
  27357. ' }',
  27358. ' );',
  27359. ' $r.addProperty(',
  27360. ' "E1",',
  27361. ' 0,',
  27362. ' $mod.$rtti["TEnum"],',
  27363. ' "FE",',
  27364. ' "",',
  27365. ' {',
  27366. ' Default: $mod.TEnum.red',
  27367. ' }',
  27368. ' );',
  27369. ' $r.addProperty(',
  27370. ' "E2",',
  27371. ' 0,',
  27372. ' $mod.$rtti["TEnum"],',
  27373. ' "FE",',
  27374. ' "",',
  27375. ' {',
  27376. ' Default: $mod.TEnum.blue',
  27377. ' }',
  27378. ' );',
  27379. '});',
  27380. '']),
  27381. LinesToStr([ // $mod.$main
  27382. '']));
  27383. end;
  27384. procedure TTestModule.TestRTTI_DefaultValueSet;
  27385. begin
  27386. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27387. StartProgram(false);
  27388. Add([
  27389. 'type',
  27390. ' TEnum = (red, blue);',
  27391. ' TSet = set of TEnum;',
  27392. 'const',
  27393. ' CSet = [red,blue];',
  27394. 'type',
  27395. ' TObject = class',
  27396. ' FSet: TSet;',
  27397. ' published',
  27398. ' property Set1: TSet read FSet default [];',
  27399. ' property Set2: TSet read FSet default [red];',
  27400. ' property Set3: TSet read FSet default [red,blue];',
  27401. ' property Set4: TSet read FSet default CSet;',
  27402. ' end;',
  27403. 'begin']);
  27404. ConvertProgram;
  27405. CheckSource('TestRTTI_DefaultValueSet',
  27406. LinesToStr([ // statements
  27407. 'this.TEnum = {',
  27408. ' "0": "red",',
  27409. ' red: 0,',
  27410. ' "1": "blue",',
  27411. ' blue: 1',
  27412. '};',
  27413. '$mod.$rtti.$Enum("TEnum", {',
  27414. ' minvalue: 0,',
  27415. ' maxvalue: 1,',
  27416. ' ordtype: 1,',
  27417. ' enumtype: this.TEnum',
  27418. '});',
  27419. '$mod.$rtti.$Set("TSet", {',
  27420. ' comptype: $mod.$rtti["TEnum"]',
  27421. '});',
  27422. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  27423. 'rtl.createClass($mod, "TObject", null, function () {',
  27424. ' this.$init = function () {',
  27425. ' this.FSet = {};',
  27426. ' };',
  27427. ' this.$final = function () {',
  27428. ' this.FSet = undefined;',
  27429. ' };',
  27430. ' var $r = this.$rtti;',
  27431. ' $r.addProperty(',
  27432. ' "Set1",',
  27433. ' 0,',
  27434. ' $mod.$rtti["TSet"],',
  27435. ' "FSet",',
  27436. ' "",',
  27437. ' {',
  27438. ' Default: {}',
  27439. ' }',
  27440. ' );',
  27441. ' $r.addProperty(',
  27442. ' "Set2",',
  27443. ' 0,',
  27444. ' $mod.$rtti["TSet"],',
  27445. ' "FSet",',
  27446. ' "",',
  27447. ' {',
  27448. ' Default: rtl.createSet($mod.TEnum.red)',
  27449. ' }',
  27450. ' );',
  27451. ' $r.addProperty(',
  27452. ' "Set3",',
  27453. ' 0,',
  27454. ' $mod.$rtti["TSet"],',
  27455. ' "FSet",',
  27456. ' "",',
  27457. ' {',
  27458. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  27459. ' }',
  27460. ' );',
  27461. ' $r.addProperty(',
  27462. ' "Set4",',
  27463. ' 0,',
  27464. ' $mod.$rtti["TSet"],',
  27465. ' "FSet",',
  27466. ' "",',
  27467. ' {',
  27468. ' Default: $mod.CSet',
  27469. ' }',
  27470. ' );',
  27471. '});',
  27472. '']),
  27473. LinesToStr([ // $mod.$main
  27474. '']));
  27475. end;
  27476. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  27477. begin
  27478. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27479. StartProgram(false);
  27480. Add([
  27481. 'type',
  27482. ' TRg = -1..1;',
  27483. 'const',
  27484. ' l = low(TRg);',
  27485. ' h = high(TRg);',
  27486. 'type',
  27487. ' TObject = class',
  27488. ' FV: TRg;',
  27489. ' published',
  27490. ' property V1: TRg read FV default -1;',
  27491. ' end;',
  27492. 'begin']);
  27493. ConvertProgram;
  27494. CheckSource('TestRTTI_DefaultValueRangeType',
  27495. LinesToStr([ // statements
  27496. '$mod.$rtti.$Int("TRg", {',
  27497. ' minvalue: -1,',
  27498. ' maxvalue: 1,',
  27499. ' ordtype: 0',
  27500. '});',
  27501. 'this.l = -1;',
  27502. 'this.h = 1;',
  27503. 'rtl.createClass($mod, "TObject", null, function () {',
  27504. ' this.$init = function () {',
  27505. ' this.FV = 0;',
  27506. ' };',
  27507. ' this.$final = function () {',
  27508. ' };',
  27509. ' var $r = this.$rtti;',
  27510. ' $r.addProperty(',
  27511. ' "V1",',
  27512. ' 0,',
  27513. ' $mod.$rtti["TRg"],',
  27514. ' "FV",',
  27515. ' "",',
  27516. ' {',
  27517. ' Default: -1',
  27518. ' }',
  27519. ' );',
  27520. '});',
  27521. '']),
  27522. LinesToStr([ // $mod.$main
  27523. '']));
  27524. end;
  27525. procedure TTestModule.TestRTTI_DefaultValueInherit;
  27526. begin
  27527. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27528. StartProgram(false);
  27529. Add([
  27530. 'type',
  27531. ' TObject = class',
  27532. ' FA, FB: byte;',
  27533. ' property A: byte read FA default 1;',
  27534. ' property B: byte read FB default 2;',
  27535. ' end;',
  27536. ' TBird = class',
  27537. ' published',
  27538. ' property A;',
  27539. ' property B nodefault;',
  27540. ' end;',
  27541. 'begin']);
  27542. ConvertProgram;
  27543. CheckSource('TestRTTI_DefaultValueInherit',
  27544. LinesToStr([ // statements
  27545. 'rtl.createClass($mod, "TObject", null, function () {',
  27546. ' this.$init = function () {',
  27547. ' this.FA = 0;',
  27548. ' this.FB = 0;',
  27549. ' };',
  27550. ' this.$final = function () {',
  27551. ' };',
  27552. '});',
  27553. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27554. ' var $r = this.$rtti;',
  27555. ' $r.addProperty(',
  27556. ' "A",',
  27557. ' 0,',
  27558. ' rtl.byte,',
  27559. ' "FA",',
  27560. ' "",',
  27561. ' {',
  27562. ' Default: 1',
  27563. ' }',
  27564. ' );',
  27565. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  27566. '});',
  27567. '']),
  27568. LinesToStr([ // $mod.$main
  27569. '']));
  27570. end;
  27571. procedure TTestModule.TestRTTI_OverrideMethod;
  27572. begin
  27573. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27574. StartProgram(false);
  27575. Add('type');
  27576. Add(' TObject = class');
  27577. Add(' published');
  27578. Add(' procedure DoIt; virtual; abstract;');
  27579. Add(' end;');
  27580. Add(' TSky = class');
  27581. Add(' published');
  27582. Add(' procedure DoIt; override;');
  27583. Add(' end;');
  27584. Add('procedure TSky.DoIt; begin end;');
  27585. Add('begin');
  27586. ConvertProgram;
  27587. CheckSource('TestRTTI_OverrideMethod',
  27588. LinesToStr([ // statements
  27589. 'rtl.createClass($mod, "TObject", null, function () {',
  27590. ' this.$init = function () {',
  27591. ' };',
  27592. ' this.$final = function () {',
  27593. ' };',
  27594. ' var $r = this.$rtti;',
  27595. ' $r.addMethod("DoIt", 0, null);',
  27596. '});',
  27597. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27598. ' this.DoIt = function () {',
  27599. ' };',
  27600. '});',
  27601. '']),
  27602. LinesToStr([ // $mod.$main
  27603. '']));
  27604. end;
  27605. procedure TTestModule.TestRTTI_ReintroduceMethod;
  27606. begin
  27607. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27608. StartProgram(false);
  27609. Add([
  27610. 'type',
  27611. ' TObject = class',
  27612. ' published',
  27613. ' procedure DoIt;',
  27614. ' end;',
  27615. ' TSky = class',
  27616. ' published',
  27617. ' procedure DoIt; reintroduce;',
  27618. ' end;',
  27619. 'procedure TObject.DoIt; begin end;',
  27620. 'procedure TSky.DoIt;',
  27621. 'begin',
  27622. ' inherited DoIt;',
  27623. 'end;',
  27624. 'begin']);
  27625. ConvertProgram;
  27626. CheckSource('TestRTTI_ReintroduceMethod',
  27627. LinesToStr([ // statements
  27628. 'rtl.createClass($mod, "TObject", null, function () {',
  27629. ' this.$init = function () {',
  27630. ' };',
  27631. ' this.$final = function () {',
  27632. ' };',
  27633. ' this.DoIt = function () {',
  27634. ' };',
  27635. ' var $r = this.$rtti;',
  27636. ' $r.addMethod("DoIt", 0, null);',
  27637. '});',
  27638. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27639. ' this.DoIt = function () {',
  27640. ' $mod.TObject.DoIt.call(this);',
  27641. ' };',
  27642. ' var $r = this.$rtti;',
  27643. ' $r.addMethod("DoIt", 0, null);',
  27644. '});',
  27645. '']),
  27646. LinesToStr([ // $mod.$main
  27647. '']));
  27648. end;
  27649. procedure TTestModule.TestRTTI_OverloadProperty;
  27650. begin
  27651. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27652. StartProgram(false);
  27653. Add('type');
  27654. Add(' TObject = class');
  27655. Add(' protected');
  27656. Add(' FFlag: longint;');
  27657. Add(' published');
  27658. Add(' property Flag: longint read fflag;');
  27659. Add(' end;');
  27660. Add(' TSky = class');
  27661. Add(' published');
  27662. Add(' property FLAG: longint write fflag;');
  27663. Add(' end;');
  27664. Add('begin');
  27665. ConvertProgram;
  27666. CheckSource('TestRTTI_OverrideMethod',
  27667. LinesToStr([ // statements
  27668. 'rtl.createClass($mod, "TObject", null, function () {',
  27669. ' this.$init = function () {',
  27670. ' this.FFlag = 0;',
  27671. ' };',
  27672. ' this.$final = function () {',
  27673. ' };',
  27674. ' var $r = this.$rtti;',
  27675. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  27676. '});',
  27677. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27678. ' var $r = this.$rtti;',
  27679. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  27680. '});',
  27681. '']),
  27682. LinesToStr([ // $mod.$main
  27683. '']));
  27684. end;
  27685. procedure TTestModule.TestRTTI_ClassForward;
  27686. begin
  27687. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27688. StartProgram(false);
  27689. Add('type');
  27690. Add(' TObject = class end;');
  27691. Add(' tbridge = class;');
  27692. Add(' TProc = function: tbridge;');
  27693. Add(' TOger = class');
  27694. Add(' published');
  27695. Add(' FBridge: tbridge;');
  27696. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  27697. Add(' property Bridge: tbridge read fbridge write setbridge;');
  27698. Add(' end;');
  27699. Add(' TBridge = class');
  27700. Add(' FOger: toger;');
  27701. Add(' end;');
  27702. Add('var p: Pointer;');
  27703. Add(' b: tbridge;');
  27704. Add('begin');
  27705. Add(' p:=typeinfo(tbridge);');
  27706. Add(' p:=typeinfo(b);');
  27707. ConvertProgram;
  27708. CheckSource('TestRTTI_ClassForward',
  27709. LinesToStr([ // statements
  27710. 'rtl.createClass($mod, "TObject", null, function () {',
  27711. ' this.$init = function () {',
  27712. ' };',
  27713. ' this.$final = function () {',
  27714. ' };',
  27715. '});',
  27716. '$mod.$rtti.$Class("TBridge");',
  27717. '$mod.$rtti.$ProcVar("TProc", {',
  27718. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  27719. '});',
  27720. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  27721. ' this.$init = function () {',
  27722. ' $mod.TObject.$init.call(this);',
  27723. ' this.FBridge = null;',
  27724. ' };',
  27725. ' this.$final = function () {',
  27726. ' this.FBridge = undefined;',
  27727. ' $mod.TObject.$final.call(this);',
  27728. ' };',
  27729. ' var $r = this.$rtti;',
  27730. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  27731. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  27732. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  27733. '});',
  27734. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  27735. ' this.$init = function () {',
  27736. ' $mod.TObject.$init.call(this);',
  27737. ' this.FOger = null;',
  27738. ' };',
  27739. ' this.$final = function () {',
  27740. ' this.FOger = undefined;',
  27741. ' $mod.TObject.$final.call(this);',
  27742. ' };',
  27743. '});',
  27744. 'this.p = null;',
  27745. 'this.b = null;',
  27746. '']),
  27747. LinesToStr([ // $mod.$main
  27748. '$mod.p = $mod.$rtti["TBridge"];',
  27749. '$mod.p = $mod.b.$rtti;',
  27750. '']));
  27751. end;
  27752. procedure TTestModule.TestRTTI_ClassOf;
  27753. begin
  27754. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27755. StartProgram(false);
  27756. Add('type');
  27757. Add(' TClass = class of tobject;');
  27758. Add(' TProcA = function: TClass;');
  27759. Add(' TObject = class');
  27760. Add(' published');
  27761. Add(' C: tclass;');
  27762. Add(' end;');
  27763. Add(' tfox = class;');
  27764. Add(' TBird = class end;');
  27765. Add(' TBirds = class of tbird;');
  27766. Add(' TFox = class end;');
  27767. Add(' TFoxes = class of tfox;');
  27768. Add(' TCows = class of TCow;');
  27769. Add(' TCow = class;');
  27770. Add(' TCow = class end;');
  27771. Add('begin');
  27772. ConvertProgram;
  27773. CheckSource('TestRTTI_ClassOf',
  27774. LinesToStr([ // statements
  27775. '$mod.$rtti.$Class("TObject");',
  27776. '$mod.$rtti.$ClassRef("TClass", {',
  27777. ' instancetype: $mod.$rtti["TObject"]',
  27778. '});',
  27779. '$mod.$rtti.$ProcVar("TProcA", {',
  27780. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  27781. '});',
  27782. 'rtl.createClass($mod, "TObject", null, function () {',
  27783. ' this.$init = function () {',
  27784. ' this.C = null;',
  27785. ' };',
  27786. ' this.$final = function () {',
  27787. ' this.C = undefined;',
  27788. ' };',
  27789. ' var $r = this.$rtti;',
  27790. ' $r.addField("C", $mod.$rtti["TClass"]);',
  27791. '});',
  27792. '$mod.$rtti.$Class("TFox");',
  27793. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27794. '});',
  27795. '$mod.$rtti.$ClassRef("TBirds", {',
  27796. ' instancetype: $mod.$rtti["TBird"]',
  27797. '});',
  27798. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  27799. '});',
  27800. '$mod.$rtti.$ClassRef("TFoxes", {',
  27801. ' instancetype: $mod.$rtti["TFox"]',
  27802. '});',
  27803. '$mod.$rtti.$Class("TCow");',
  27804. '$mod.$rtti.$ClassRef("TCows", {',
  27805. ' instancetype: $mod.$rtti["TCow"]',
  27806. '});',
  27807. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  27808. '});',
  27809. '']),
  27810. LinesToStr([ // $mod.$main
  27811. '']));
  27812. end;
  27813. procedure TTestModule.TestRTTI_Record;
  27814. begin
  27815. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27816. StartProgram(false);
  27817. Add('type');
  27818. Add(' integer = longint;');
  27819. Add(' TPoint = record');
  27820. Add(' x,y: integer;');
  27821. Add(' end;');
  27822. Add('var p: pointer;');
  27823. Add(' r: tpoint;');
  27824. Add('begin');
  27825. Add(' p:=typeinfo(tpoint);');
  27826. Add(' p:=typeinfo(r);');
  27827. Add(' p:=typeinfo(r.x);');
  27828. ConvertProgram;
  27829. CheckSource('TestRTTI_Record',
  27830. LinesToStr([ // statements
  27831. 'rtl.recNewT($mod, "TPoint", function () {',
  27832. ' this.x = 0;',
  27833. ' this.y = 0;',
  27834. ' this.$eq = function (b) {',
  27835. ' return (this.x === b.x) && (this.y === b.y);',
  27836. ' };',
  27837. ' this.$assign = function (s) {',
  27838. ' this.x = s.x;',
  27839. ' this.y = s.y;',
  27840. ' return this;',
  27841. ' };',
  27842. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  27843. ' $r.addField("x", rtl.longint);',
  27844. ' $r.addField("y", rtl.longint);',
  27845. '});',
  27846. 'this.p = null;',
  27847. 'this.r = $mod.TPoint.$new();',
  27848. '']),
  27849. LinesToStr([ // $mod.$main
  27850. '$mod.p = $mod.$rtti["TPoint"];',
  27851. '$mod.p = $mod.$rtti["TPoint"];',
  27852. '$mod.p = rtl.longint;',
  27853. '']));
  27854. end;
  27855. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  27856. begin
  27857. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27858. StartProgram(false);
  27859. Add('type');
  27860. Add(' TFloatRec = record');
  27861. Add(' d: array of char;');
  27862. // Add(' i: array of array of longint;');
  27863. Add(' end;');
  27864. Add('var p: pointer;');
  27865. Add(' r: tfloatrec;');
  27866. Add('begin');
  27867. Add(' p:=typeinfo(tfloatrec);');
  27868. Add(' p:=typeinfo(r);');
  27869. Add(' p:=typeinfo(r.d);');
  27870. ConvertProgram;
  27871. CheckSource('TestRTTI_Record',
  27872. LinesToStr([ // statements
  27873. 'rtl.recNewT($mod, "TFloatRec", function () {',
  27874. ' this.d = [];',
  27875. ' this.$eq = function (b) {',
  27876. ' return this.d === b.d;',
  27877. ' };',
  27878. ' this.$assign = function (s) {',
  27879. ' this.d = s.d;',
  27880. ' return this;',
  27881. ' };',
  27882. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  27883. ' eltype: rtl.char',
  27884. ' });',
  27885. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  27886. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  27887. '});',
  27888. 'this.p = null;',
  27889. 'this.r = $mod.TFloatRec.$new();',
  27890. '']),
  27891. LinesToStr([ // $mod.$main
  27892. '$mod.p = $mod.$rtti["TFloatRec"];',
  27893. '$mod.p = $mod.$rtti["TFloatRec"];',
  27894. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  27895. '']));
  27896. end;
  27897. procedure TTestModule.TestRTTI_LocalTypes;
  27898. begin
  27899. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27900. StartProgram(false);
  27901. Add([
  27902. 'procedure DoIt;',
  27903. 'type',
  27904. ' integer = longint;',
  27905. ' TPoint = record',
  27906. ' x,y: integer;',
  27907. ' end;',
  27908. 'var p: TPoint;',
  27909. 'begin',
  27910. 'end;',
  27911. 'begin']);
  27912. ConvertProgram;
  27913. CheckSource('TestRTTI_LocalTypes',
  27914. LinesToStr([ // statements
  27915. 'var TPoint = rtl.recNewT(null, "", function () {',
  27916. ' this.x = 0;',
  27917. ' this.y = 0;',
  27918. ' this.$eq = function (b) {',
  27919. ' return (this.x === b.x) && (this.y === b.y);',
  27920. ' };',
  27921. ' this.$assign = function (s) {',
  27922. ' this.x = s.x;',
  27923. ' this.y = s.y;',
  27924. ' return this;',
  27925. ' };',
  27926. '});',
  27927. 'this.DoIt = function () {',
  27928. ' var p = TPoint.$new();',
  27929. '};',
  27930. '']),
  27931. LinesToStr([ // $mod.$main
  27932. '']));
  27933. end;
  27934. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  27935. begin
  27936. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27937. StartProgram(false);
  27938. Add([
  27939. 'type',
  27940. ' TCaption = string;',
  27941. ' TYesNo = boolean;',
  27942. ' TLetter = char;',
  27943. ' TFloat = double;',
  27944. ' TPtr = pointer;',
  27945. ' TShortInt = shortint;',
  27946. ' TByte = byte;',
  27947. ' TSmallInt = smallint;',
  27948. ' TWord = word;',
  27949. ' TInt32 = longint;',
  27950. ' TDWord = longword;',
  27951. ' TValue = jsvalue;',
  27952. 'var p: TPtr;',
  27953. 'begin',
  27954. ' p:=typeinfo(string);',
  27955. ' p:=typeinfo(tcaption);',
  27956. ' p:=typeinfo(boolean);',
  27957. ' p:=typeinfo(tyesno);',
  27958. ' p:=typeinfo(char);',
  27959. ' p:=typeinfo(tletter);',
  27960. ' p:=typeinfo(double);',
  27961. ' p:=typeinfo(tfloat);',
  27962. ' p:=typeinfo(pointer);',
  27963. ' p:=typeinfo(tptr);',
  27964. ' p:=typeinfo(shortint);',
  27965. ' p:=typeinfo(tshortint);',
  27966. ' p:=typeinfo(byte);',
  27967. ' p:=typeinfo(tbyte);',
  27968. ' p:=typeinfo(smallint);',
  27969. ' p:=typeinfo(tsmallint);',
  27970. ' p:=typeinfo(word);',
  27971. ' p:=typeinfo(tword);',
  27972. ' p:=typeinfo(longword);',
  27973. ' p:=typeinfo(tdword);',
  27974. ' p:=typeinfo(jsvalue);',
  27975. ' p:=typeinfo(tvalue);',
  27976. '']);
  27977. ConvertProgram;
  27978. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  27979. LinesToStr([ // statements
  27980. 'this.p = null;',
  27981. '']),
  27982. LinesToStr([ // $mod.$main
  27983. '$mod.p = rtl.string;',
  27984. '$mod.p = rtl.string;',
  27985. '$mod.p = rtl.boolean;',
  27986. '$mod.p = rtl.boolean;',
  27987. '$mod.p = rtl.char;',
  27988. '$mod.p = rtl.char;',
  27989. '$mod.p = rtl.double;',
  27990. '$mod.p = rtl.double;',
  27991. '$mod.p = rtl.pointer;',
  27992. '$mod.p = rtl.pointer;',
  27993. '$mod.p = rtl.shortint;',
  27994. '$mod.p = rtl.shortint;',
  27995. '$mod.p = rtl.byte;',
  27996. '$mod.p = rtl.byte;',
  27997. '$mod.p = rtl.smallint;',
  27998. '$mod.p = rtl.smallint;',
  27999. '$mod.p = rtl.word;',
  28000. '$mod.p = rtl.word;',
  28001. '$mod.p = rtl.longword;',
  28002. '$mod.p = rtl.longword;',
  28003. '$mod.p = rtl.jsvalue;',
  28004. '$mod.p = rtl.jsvalue;',
  28005. '']));
  28006. end;
  28007. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  28008. begin
  28009. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28010. StartProgram(false);
  28011. Add([
  28012. 'type',
  28013. ' TCaption = type string;',
  28014. ' TYesNo = type boolean;',
  28015. ' TLetter = type char;',
  28016. ' TFloat = type double;',
  28017. ' TPtr = type pointer;',
  28018. ' TShortInt = type shortint;',
  28019. ' TByte = type byte;',
  28020. ' TSmallInt = type smallint;',
  28021. ' TWord = type word;',
  28022. ' TInt32 = type longint;',
  28023. ' TDWord = type longword;',
  28024. ' TValue = type jsvalue;',
  28025. ' TAliasValue = type TValue;',
  28026. 'var',
  28027. ' p: TPtr;',
  28028. ' a: TAliasValue;',
  28029. 'begin',
  28030. ' p:=typeinfo(tcaption);',
  28031. ' p:=typeinfo(tyesno);',
  28032. ' p:=typeinfo(tletter);',
  28033. ' p:=typeinfo(tfloat);',
  28034. ' p:=typeinfo(tptr);',
  28035. ' p:=typeinfo(tshortint);',
  28036. ' p:=typeinfo(tbyte);',
  28037. ' p:=typeinfo(tsmallint);',
  28038. ' p:=typeinfo(tword);',
  28039. ' p:=typeinfo(tdword);',
  28040. ' p:=typeinfo(tvalue);',
  28041. ' p:=typeinfo(taliasvalue);',
  28042. ' p:=typeinfo(a);',
  28043. '']);
  28044. ConvertProgram;
  28045. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  28046. LinesToStr([ // statements
  28047. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  28048. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  28049. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  28050. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  28051. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  28052. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  28053. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  28054. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  28055. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  28056. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  28057. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  28058. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  28059. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  28060. 'this.p = null;',
  28061. 'this.a = undefined;',
  28062. '']),
  28063. LinesToStr([ // $mod.$main
  28064. '$mod.p = $mod.$rtti["TCaption"];',
  28065. '$mod.p = $mod.$rtti["TYesNo"];',
  28066. '$mod.p = $mod.$rtti["TLetter"];',
  28067. '$mod.p = $mod.$rtti["TFloat"];',
  28068. '$mod.p = $mod.$rtti["TPtr"];',
  28069. '$mod.p = $mod.$rtti["TShortInt"];',
  28070. '$mod.p = $mod.$rtti["TByte"];',
  28071. '$mod.p = $mod.$rtti["TSmallInt"];',
  28072. '$mod.p = $mod.$rtti["TWord"];',
  28073. '$mod.p = $mod.$rtti["TDWord"];',
  28074. '$mod.p = $mod.$rtti["TValue"];',
  28075. '$mod.p = $mod.$rtti["TAliasValue"];',
  28076. '$mod.p = $mod.$rtti["TAliasValue"];',
  28077. '']));
  28078. end;
  28079. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  28080. begin
  28081. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28082. StartProgram(false);
  28083. Add('procedure DoIt;');
  28084. Add('type');
  28085. Add(' integer = longint;');
  28086. Add(' TPoint = record');
  28087. Add(' x,y: integer;');
  28088. Add(' end;');
  28089. Add('var p: pointer;');
  28090. Add('begin');
  28091. Add(' p:=typeinfo(tpoint);');
  28092. Add('end;');
  28093. Add('begin');
  28094. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  28095. ConvertProgram;
  28096. end;
  28097. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  28098. begin
  28099. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28100. StartProgram(false);
  28101. Add([
  28102. '{$modeswitch externalclass}',
  28103. 'type',
  28104. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28105. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  28106. ' TFlag = (up,down);',
  28107. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  28108. ' TFlags = set of TFlag;',
  28109. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  28110. 'var',
  28111. ' ti: TTypeInfo;',
  28112. ' tiInt: TTypeInfoInteger;',
  28113. ' tiEnum: TTypeInfoEnum;',
  28114. ' tiSet: TTypeInfoSet;',
  28115. 'begin',
  28116. ' ti:=typeinfo(string);',
  28117. ' ti:=typeinfo(boolean);',
  28118. ' ti:=typeinfo(char);',
  28119. ' ti:=typeinfo(double);',
  28120. ' tiInt:=typeinfo(shortint);',
  28121. ' tiInt:=typeinfo(byte);',
  28122. ' tiInt:=typeinfo(smallint);',
  28123. ' tiInt:=typeinfo(word);',
  28124. ' tiInt:=typeinfo(longint);',
  28125. ' tiInt:=typeinfo(longword);',
  28126. ' ti:=typeinfo(jsvalue);',
  28127. ' tiEnum:=typeinfo(tflag);',
  28128. ' tiSet:=typeinfo(tflags);']);
  28129. ConvertProgram;
  28130. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  28131. LinesToStr([ // statements
  28132. 'this.TFlag = {',
  28133. ' "0": "up",',
  28134. ' up: 0,',
  28135. ' "1": "down",',
  28136. ' down: 1',
  28137. '};',
  28138. '$mod.$rtti.$Enum("TFlag", {',
  28139. ' minvalue: 0,',
  28140. ' maxvalue: 1,',
  28141. ' ordtype: 1,',
  28142. ' enumtype: this.TFlag',
  28143. '});',
  28144. '$mod.$rtti.$Set("TFlags", {',
  28145. ' comptype: $mod.$rtti["TFlag"]',
  28146. '});',
  28147. 'this.ti = null;',
  28148. 'this.tiInt = null;',
  28149. 'this.tiEnum = null;',
  28150. 'this.tiSet = null;',
  28151. '']),
  28152. LinesToStr([ // $mod.$main
  28153. '$mod.ti = rtl.string;',
  28154. '$mod.ti = rtl.boolean;',
  28155. '$mod.ti = rtl.char;',
  28156. '$mod.ti = rtl.double;',
  28157. '$mod.tiInt = rtl.shortint;',
  28158. '$mod.tiInt = rtl.byte;',
  28159. '$mod.tiInt = rtl.smallint;',
  28160. '$mod.tiInt = rtl.word;',
  28161. '$mod.tiInt = rtl.longint;',
  28162. '$mod.tiInt = rtl.longword;',
  28163. '$mod.ti = rtl.jsvalue;',
  28164. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  28165. '$mod.tiSet = $mod.$rtti["TFlags"];',
  28166. '']));
  28167. end;
  28168. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  28169. begin
  28170. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28171. StartProgram(false);
  28172. Add('{$modeswitch externalclass}');
  28173. Add('type');
  28174. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  28175. Add(' TStaticArr = array[boolean] of string;');
  28176. Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
  28177. Add(' TDynArr = array of string;');
  28178. Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
  28179. Add(' TProc = procedure;');
  28180. Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
  28181. Add(' TMethod = procedure of object;');
  28182. Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
  28183. Add('var');
  28184. Add(' StaticArray: TStaticArr;');
  28185. Add(' tiStaticArray: TTypeInfoStaticArray;');
  28186. Add(' DynArray: TDynArr;');
  28187. Add(' tiDynArray: TTypeInfoDynArray;');
  28188. Add(' ProcVar: TProc;');
  28189. Add(' tiProcVar: TTypeInfoProcVar;');
  28190. Add(' MethodVar: TMethod;');
  28191. Add(' tiMethodVar: TTypeInfoMethodVar;');
  28192. Add('begin');
  28193. Add(' tiStaticArray:=typeinfo(StaticArray);');
  28194. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  28195. Add(' tiDynArray:=typeinfo(DynArray);');
  28196. Add(' tiDynArray:=typeinfo(TDynArr);');
  28197. Add(' tiProcVar:=typeinfo(ProcVar);');
  28198. Add(' tiProcVar:=typeinfo(TProc);');
  28199. Add(' tiMethodVar:=typeinfo(MethodVar);');
  28200. Add(' tiMethodVar:=typeinfo(TMethod);');
  28201. ConvertProgram;
  28202. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  28203. LinesToStr([ // statements
  28204. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  28205. ' dims: [2],',
  28206. ' eltype: rtl.string',
  28207. '});',
  28208. '$mod.$rtti.$DynArray("TDynArr", {',
  28209. ' eltype: rtl.string',
  28210. '});',
  28211. '$mod.$rtti.$ProcVar("TProc", {',
  28212. ' procsig: rtl.newTIProcSig(null)',
  28213. '});',
  28214. '$mod.$rtti.$MethodVar("TMethod", {',
  28215. ' procsig: rtl.newTIProcSig(null),',
  28216. ' methodkind: 0',
  28217. '});',
  28218. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  28219. 'this.tiStaticArray = null;',
  28220. 'this.DynArray = [];',
  28221. 'this.tiDynArray = null;',
  28222. 'this.ProcVar = null;',
  28223. 'this.tiProcVar = null;',
  28224. 'this.MethodVar = null;',
  28225. 'this.tiMethodVar = null;',
  28226. '']),
  28227. LinesToStr([ // $mod.$main
  28228. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  28229. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  28230. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  28231. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  28232. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  28233. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  28234. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  28235. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  28236. '']));
  28237. end;
  28238. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  28239. begin
  28240. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28241. StartProgram(false);
  28242. Add('{$modeswitch externalclass}');
  28243. Add('type');
  28244. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  28245. Add(' TRec = record end;');
  28246. Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
  28247. // ToDo: ^PRec
  28248. Add(' TObject = class end;');
  28249. Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
  28250. Add(' TClass = class of tobject;');
  28251. Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
  28252. Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
  28253. Add('var');
  28254. Add(' Rec: trec;');
  28255. Add(' tiRecord: ttypeinforecord;');
  28256. Add(' Obj: tobject;');
  28257. Add(' tiClass: ttypeinfoclass;');
  28258. Add(' aClass: tclass;');
  28259. Add(' tiClassRef: ttypeinfoclassref;');
  28260. // ToDo: ^PRec
  28261. Add(' tiPointer: ttypeinfopointer;');
  28262. Add('begin');
  28263. Add(' tirecord:=typeinfo(trec);');
  28264. Add(' tirecord:=typeinfo(trec);');
  28265. Add(' ticlass:=typeinfo(obj);');
  28266. Add(' ticlass:=typeinfo(tobject);');
  28267. Add(' ticlass:=typeinfo(aclass);');
  28268. Add(' ticlassref:=typeinfo(tclass);');
  28269. ConvertProgram;
  28270. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  28271. LinesToStr([ // statements
  28272. 'rtl.recNewT($mod, "TRec", function () {',
  28273. ' this.$eq = function (b) {',
  28274. ' return true;',
  28275. ' };',
  28276. ' this.$assign = function (s) {',
  28277. ' return this;',
  28278. ' };',
  28279. ' $mod.$rtti.$Record("TRec", {});',
  28280. '});',
  28281. 'rtl.createClass($mod, "TObject", null, function () {',
  28282. ' this.$init = function () {',
  28283. ' };',
  28284. ' this.$final = function () {',
  28285. ' };',
  28286. '});',
  28287. '$mod.$rtti.$ClassRef("TClass", {',
  28288. ' instancetype: $mod.$rtti["TObject"]',
  28289. '});',
  28290. 'this.Rec = $mod.TRec.$new();',
  28291. 'this.tiRecord = null;',
  28292. 'this.Obj = null;',
  28293. 'this.tiClass = null;',
  28294. 'this.aClass = null;',
  28295. 'this.tiClassRef = null;',
  28296. 'this.tiPointer = null;',
  28297. '']),
  28298. LinesToStr([ // $mod.$main
  28299. '$mod.tiRecord = $mod.$rtti["TRec"];',
  28300. '$mod.tiRecord = $mod.$rtti["TRec"];',
  28301. '$mod.tiClass = $mod.Obj.$rtti;',
  28302. '$mod.tiClass = $mod.$rtti["TObject"];',
  28303. '$mod.tiClass = $mod.aClass.$rtti;',
  28304. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  28305. '']));
  28306. end;
  28307. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  28308. begin
  28309. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28310. StartProgram(false);
  28311. Add([
  28312. '{$modeswitch externalclass}',
  28313. 'type',
  28314. ' TClass = class of tobject;',
  28315. ' TObject = class',
  28316. ' function MyClass: TClass;',
  28317. ' class function ClassType: TClass;',
  28318. ' end;',
  28319. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28320. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  28321. 'function TObject.MyClass: TClass;',
  28322. 'var t: TTypeInfoClass;',
  28323. 'begin',
  28324. ' t:=TypeInfo(Self);',
  28325. ' t:=TypeInfo(Result);',
  28326. ' t:=TypeInfo(TObject);',
  28327. 'end;',
  28328. 'class function TObject.ClassType: TClass;',
  28329. 'var t: TTypeInfoClass;',
  28330. 'begin',
  28331. ' t:=TypeInfo(Self);',
  28332. ' t:=TypeInfo(Result);',
  28333. 'end;',
  28334. 'var',
  28335. ' Obj: TObject;',
  28336. ' t: TTypeInfoClass;',
  28337. 'begin',
  28338. ' t:=TypeInfo(TObject.ClassType);',
  28339. ' t:=TypeInfo(Obj.ClassType);',
  28340. ' t:=TypeInfo(Obj.MyClass);',
  28341. '']);
  28342. ConvertProgram;
  28343. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  28344. LinesToStr([ // statements
  28345. '$mod.$rtti.$Class("TObject");',
  28346. '$mod.$rtti.$ClassRef("TClass", {',
  28347. ' instancetype: $mod.$rtti["TObject"]',
  28348. '});',
  28349. 'rtl.createClass($mod, "TObject", null, function () {',
  28350. ' this.$init = function () {',
  28351. ' };',
  28352. ' this.$final = function () {',
  28353. ' };',
  28354. ' this.MyClass = function () {',
  28355. ' var Result = null;',
  28356. ' var t = null;',
  28357. ' t = this.$rtti;',
  28358. ' t = Result.$rtti;',
  28359. ' t = $mod.$rtti["TObject"];',
  28360. ' return Result;',
  28361. ' };',
  28362. ' this.ClassType = function () {',
  28363. ' var Result = null;',
  28364. ' var t = null;',
  28365. ' t = this.$rtti;',
  28366. ' t = Result.$rtti;',
  28367. ' return Result;',
  28368. ' };',
  28369. '});',
  28370. 'this.Obj = null;',
  28371. 'this.t = null;',
  28372. '']),
  28373. LinesToStr([ // $mod.$main
  28374. '$mod.t = $mod.TObject.ClassType().$rtti;',
  28375. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  28376. '$mod.t = $mod.Obj.MyClass().$rtti;',
  28377. '']));
  28378. end;
  28379. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  28380. begin
  28381. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28382. AddModuleWithIntfImplSrc('typinfo.pas',
  28383. LinesToStr([
  28384. '{$modeswitch externalclass}',
  28385. 'type',
  28386. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28387. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  28388. '']),
  28389. '');
  28390. AddModuleWithIntfImplSrc('unit2.pas',
  28391. LinesToStr([
  28392. 'uses typinfo;',
  28393. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  28394. 'procedure DoPtr(p: PTypeInfo);',
  28395. 'procedure DoInfo(t: TTypeInfo);',
  28396. 'procedure DoInt(t: TTypeInfoInteger);',
  28397. '']),
  28398. LinesToStr([
  28399. 'procedure DoPtr(p: PTypeInfo);',
  28400. 'begin end;',
  28401. 'procedure DoInfo(t: TTypeInfo);',
  28402. 'begin end;',
  28403. 'procedure DoInt(t: TTypeInfoInteger);',
  28404. 'begin end;',
  28405. '']));
  28406. StartUnit(true);
  28407. Add([
  28408. 'interface',
  28409. 'uses unit2;', // does not use unit typinfo
  28410. 'implementation',
  28411. 'var',
  28412. ' i: byte;',
  28413. ' p: pointer;',
  28414. ' t: PTypeInfo;',
  28415. 'initialization',
  28416. ' p:=typeinfo(i);',
  28417. ' t:=typeinfo(i);',
  28418. ' if p=t then ;',
  28419. ' if p=typeinfo(i) then ;',
  28420. ' if typeinfo(i)=p then ;',
  28421. ' if t=typeinfo(i) then ;',
  28422. ' if typeinfo(i)=t then ;',
  28423. ' DoPtr(p);',
  28424. ' DoPtr(t);',
  28425. ' DoPtr(typeinfo(i));',
  28426. ' DoInfo(p);',
  28427. ' DoInfo(t);',
  28428. ' DoInfo(typeinfo(i));',
  28429. ' DoInt(typeinfo(i));',
  28430. '']);
  28431. ConvertUnit;
  28432. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  28433. LinesToStr([ // statements
  28434. 'var $impl = $mod.$impl;',
  28435. '']),
  28436. LinesToStr([ // this.$init
  28437. '$impl.p = rtl.byte;',
  28438. '$impl.t = rtl.byte;',
  28439. 'if ($impl.p === $impl.t) ;',
  28440. 'if ($impl.p === rtl.byte) ;',
  28441. 'if (rtl.byte === $impl.p) ;',
  28442. 'if ($impl.t === rtl.byte) ;',
  28443. 'if (rtl.byte === $impl.t) ;',
  28444. 'pas.unit2.DoPtr($impl.p);',
  28445. 'pas.unit2.DoPtr($impl.t);',
  28446. 'pas.unit2.DoPtr(rtl.byte);',
  28447. 'pas.unit2.DoInfo($impl.p);',
  28448. 'pas.unit2.DoInfo($impl.t);',
  28449. 'pas.unit2.DoInfo(rtl.byte);',
  28450. 'pas.unit2.DoInt(rtl.byte);',
  28451. '']),
  28452. LinesToStr([ // implementation
  28453. '$impl.i = 0;',
  28454. '$impl.p = null;',
  28455. '$impl.t = null;',
  28456. '']) );
  28457. end;
  28458. procedure TTestModule.TestRTTI_Interface_Corba;
  28459. begin
  28460. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28461. StartProgram(false);
  28462. Add([
  28463. '{$interfaces corba}',
  28464. '{$modeswitch externalclass}',
  28465. 'type',
  28466. ' IUnknown = interface',
  28467. ' end;',
  28468. ' IBird = interface',
  28469. ' function GetItem: longint;',
  28470. ' procedure SetItem(Value: longint);',
  28471. ' property Item: longint read GetItem write SetItem;',
  28472. ' end;',
  28473. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28474. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  28475. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  28476. 'var',
  28477. ' i: IBird;',
  28478. ' t: TTypeInfoInterface;',
  28479. 'begin',
  28480. ' t:=TypeInfo(IBird);',
  28481. ' t:=TypeInfo(i);',
  28482. ' DoIt(t);',
  28483. ' DoIt(TypeInfo(IBird));',
  28484. '']);
  28485. ConvertProgram;
  28486. CheckSource('TestRTTI_Interface_Corba',
  28487. LinesToStr([ // statements
  28488. 'rtl.createInterface(',
  28489. ' $mod,',
  28490. ' "IUnknown",',
  28491. ' "{B92D5841-758A-322B-B800-000000000000}",',
  28492. ' [],',
  28493. ' null,',
  28494. ' function () {',
  28495. ' }',
  28496. ');',
  28497. 'rtl.createInterface(',
  28498. ' $mod,',
  28499. ' "IBird",',
  28500. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  28501. ' ["GetItem", "SetItem"],',
  28502. ' null,',
  28503. ' function () {',
  28504. ' var $r = this.$rtti;',
  28505. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28506. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  28507. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  28508. ' }',
  28509. ');',
  28510. 'this.DoIt = function (t) {',
  28511. '}; ',
  28512. 'this.i = null;',
  28513. 'this.t = null;',
  28514. '']),
  28515. LinesToStr([ // $mod.$main
  28516. '$mod.t = $mod.$rtti["IBird"];',
  28517. '$mod.t = $mod.i.$rtti;',
  28518. '$mod.DoIt($mod.t);',
  28519. '$mod.DoIt($mod.$rtti["IBird"]);',
  28520. '']));
  28521. end;
  28522. procedure TTestModule.TestRTTI_Interface_COM;
  28523. begin
  28524. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28525. StartProgram(false);
  28526. Add([
  28527. '{$interfaces com}',
  28528. '{$modeswitch externalclass}',
  28529. 'type',
  28530. ' TGuid = record end;',
  28531. ' integer = longint;',
  28532. ' IUnknown = interface',
  28533. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  28534. ' function _AddRef: Integer;',
  28535. ' function _Release: Integer;',
  28536. ' end;',
  28537. ' IBird = interface',
  28538. ' function GetItem: longint;',
  28539. ' procedure SetItem(Value: longint);',
  28540. ' property Item: longint read GetItem write SetItem;',
  28541. ' end;',
  28542. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28543. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  28544. 'var',
  28545. ' i: IBird;',
  28546. ' t: TTypeInfoInterface;',
  28547. 'begin',
  28548. ' t:=TypeInfo(IBird);',
  28549. ' t:=TypeInfo(i);',
  28550. '']);
  28551. ConvertProgram;
  28552. CheckSource('TestRTTI_Interface_COM',
  28553. LinesToStr([ // statements
  28554. 'rtl.recNewT($mod, "TGuid", function () {',
  28555. ' this.$eq = function (b) {',
  28556. ' return true;',
  28557. ' };',
  28558. ' this.$assign = function (s) {',
  28559. ' return this;',
  28560. ' };',
  28561. ' $mod.$rtti.$Record("TGuid", {});',
  28562. '});',
  28563. 'rtl.createInterface(',
  28564. ' $mod,',
  28565. ' "IUnknown",',
  28566. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  28567. ' ["QueryInterface", "_AddRef", "_Release"],',
  28568. ' null,',
  28569. ' function () {',
  28570. ' this.$kind = "com";',
  28571. ' var $r = this.$rtti;',
  28572. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  28573. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  28574. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  28575. ' }',
  28576. ');',
  28577. 'rtl.createInterface(',
  28578. ' $mod,',
  28579. ' "IBird",',
  28580. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  28581. ' ["GetItem", "SetItem"],',
  28582. ' $mod.IUnknown,',
  28583. ' function () {',
  28584. ' var $r = this.$rtti;',
  28585. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28586. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  28587. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  28588. ' }',
  28589. ');',
  28590. 'this.i = null;',
  28591. 'this.t = null;',
  28592. '']),
  28593. LinesToStr([ // $mod.$main
  28594. '$mod.t = $mod.$rtti["IBird"];',
  28595. '$mod.t = $mod.i.$rtti;',
  28596. '']));
  28597. end;
  28598. procedure TTestModule.TestRTTI_ClassHelper;
  28599. begin
  28600. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28601. StartProgram(false);
  28602. Add([
  28603. '{$interfaces com}',
  28604. '{$modeswitch externalclass}',
  28605. 'type',
  28606. ' TObject = class',
  28607. ' end;',
  28608. ' THelper = class helper for TObject',
  28609. ' published',
  28610. ' function GetItem: longint;',
  28611. ' property Item: longint read GetItem;',
  28612. ' end;',
  28613. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28614. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  28615. 'function THelper.GetItem: longint;',
  28616. 'begin',
  28617. 'end;',
  28618. 'var',
  28619. ' t: TTypeInfoHelper;',
  28620. 'begin',
  28621. ' t:=TypeInfo(THelper);',
  28622. '']);
  28623. ConvertProgram;
  28624. CheckSource('TestRTTI_ClassHelper',
  28625. LinesToStr([ // statements
  28626. 'rtl.createClass($mod, "TObject", null, function () {',
  28627. ' this.$init = function () {',
  28628. ' };',
  28629. ' this.$final = function () {',
  28630. ' };',
  28631. '});',
  28632. 'rtl.createHelper($mod, "THelper", null, function () {',
  28633. ' this.GetItem = function () {',
  28634. ' var Result = 0;',
  28635. ' return Result;',
  28636. ' };',
  28637. ' var $r = this.$rtti;',
  28638. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28639. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  28640. '});',
  28641. 'this.t = null;',
  28642. '']),
  28643. LinesToStr([ // $mod.$main
  28644. '$mod.t = $mod.$rtti["THelper"];',
  28645. '']));
  28646. end;
  28647. procedure TTestModule.TestResourcestringProgram;
  28648. begin
  28649. StartProgram(false);
  28650. Add([
  28651. 'const Bar = ''bar'';',
  28652. 'resourcestring',
  28653. ' Red = ''red'';',
  28654. ' Foobar = ''fOo''+bar;',
  28655. 'var s: string;',
  28656. ' c: char;',
  28657. 'begin',
  28658. ' s:=red;',
  28659. ' s:=test1.red;',
  28660. ' c:=red[1];',
  28661. ' c:=test1.red[2];',
  28662. ' if red=foobar then ;',
  28663. ' if red[3]=red[4] then ;']);
  28664. ConvertProgram;
  28665. CheckSource('TestResourcestringProgram',
  28666. LinesToStr([ // statements
  28667. 'this.Bar = "bar";',
  28668. 'this.s = "";',
  28669. 'this.c = "";',
  28670. '$mod.$resourcestrings = {',
  28671. ' Red: {',
  28672. ' org: "red"',
  28673. ' },',
  28674. ' Foobar: {',
  28675. ' org: "fOobar"',
  28676. ' }',
  28677. '};',
  28678. '']),
  28679. LinesToStr([ // $mod.$main
  28680. '$mod.s = rtl.getResStr(pas.program, "Red");',
  28681. '$mod.s = rtl.getResStr(pas.program, "Red");',
  28682. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  28683. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  28684. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  28685. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  28686. '']));
  28687. end;
  28688. procedure TTestModule.TestResourcestringUnit;
  28689. begin
  28690. StartUnit(false);
  28691. Add([
  28692. 'interface',
  28693. 'const Red = ''rEd'';',
  28694. 'resourcestring',
  28695. ' Blue = ''blue'';',
  28696. ' NotRed = ''not''+Red;',
  28697. 'var s: string;',
  28698. 'implementation',
  28699. 'resourcestring',
  28700. ' ImplGreen = ''green'';',
  28701. 'initialization',
  28702. ' s:=blue+ImplGreen;',
  28703. ' s:=test1.blue+test1.implgreen;',
  28704. ' s:=blue[1]+implgreen[2];']);
  28705. ConvertUnit;
  28706. CheckSource('TestResourcestringUnit',
  28707. LinesToStr([ // statements
  28708. 'this.Red = "rEd";',
  28709. 'this.s = "";',
  28710. '$mod.$resourcestrings = {',
  28711. ' Blue: {',
  28712. ' org: "blue"',
  28713. ' },',
  28714. ' NotRed: {',
  28715. ' org: "notrEd"',
  28716. ' },',
  28717. ' ImplGreen: {',
  28718. ' org: "green"',
  28719. ' }',
  28720. '};',
  28721. '']),
  28722. LinesToStr([ // $mod.$main
  28723. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28724. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28725. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  28726. '']));
  28727. end;
  28728. procedure TTestModule.TestResourcestringImplementation;
  28729. begin
  28730. StartUnit(false);
  28731. Add([
  28732. 'interface',
  28733. 'implementation',
  28734. 'resourcestring',
  28735. ' ImplRed = ''red'';']);
  28736. ConvertUnit;
  28737. CheckSource('TestResourcestringImplementation',
  28738. LinesToStr([ // intf statements
  28739. 'var $impl = $mod.$impl;']),
  28740. LinesToStr([ // $mod.$init
  28741. '']),
  28742. LinesToStr([ // impl statements
  28743. '$mod.$resourcestrings = {',
  28744. ' ImplRed: {',
  28745. ' org: "red"',
  28746. ' }',
  28747. '};',
  28748. '']));
  28749. end;
  28750. procedure TTestModule.TestAtributes_Ignore;
  28751. begin
  28752. StartProgram(false);
  28753. Add([
  28754. '{$modeswitch ignoreattributes}',
  28755. 'type',
  28756. ' [custom1, custom2(1+3,''foo'')] [mod1.custom3]',
  28757. ' TObject = class',
  28758. ' [custom5()] FS: string;',
  28759. ' [customProp] property S: string read FS;',
  28760. ' end;',
  28761. 'var',
  28762. ' [custom6]',
  28763. ' o: TObject;',
  28764. 'begin',
  28765. '']);
  28766. ConvertProgram;
  28767. CheckSource('TestAtributes_Ignore',
  28768. LinesToStr([ // statements
  28769. 'rtl.createClass($mod, "TObject", null, function () {',
  28770. ' this.$init = function () {',
  28771. ' this.FS = "";',
  28772. ' };',
  28773. ' this.$final = function () {',
  28774. ' };',
  28775. '});',
  28776. 'this.o = null;',
  28777. '']),
  28778. LinesToStr([ // $mod.$main
  28779. '']));
  28780. end;
  28781. procedure TTestModule.TestAssert;
  28782. begin
  28783. StartProgram(false);
  28784. Add([
  28785. 'procedure DoIt;',
  28786. 'var',
  28787. ' b: boolean;',
  28788. ' s: string;',
  28789. 'begin',
  28790. ' {$Assertions on}',
  28791. ' Assert(b);',
  28792. 'end;',
  28793. 'begin',
  28794. ' DoIt;',
  28795. '']);
  28796. ConvertProgram;
  28797. CheckSource('TestAssert',
  28798. LinesToStr([ // statements
  28799. 'this.DoIt = function () {',
  28800. ' var b = false;',
  28801. ' var s = "";',
  28802. ' if (!b) throw "assert failed";',
  28803. '};',
  28804. '']),
  28805. LinesToStr([ // $mod.$main
  28806. '$mod.DoIt();',
  28807. '']));
  28808. end;
  28809. procedure TTestModule.TestAssert_SysUtils;
  28810. begin
  28811. AddModuleWithIntfImplSrc('SysUtils.pas',
  28812. LinesToStr([
  28813. 'type',
  28814. ' TObject = class',
  28815. ' constructor Create;',
  28816. ' end;',
  28817. ' EAssertionFailed = class',
  28818. ' constructor Create(s: string);',
  28819. ' end;',
  28820. '']),
  28821. LinesToStr([
  28822. 'constructor TObject.Create;',
  28823. 'begin end;',
  28824. 'constructor EAssertionFailed.Create(s: string);',
  28825. 'begin end;',
  28826. '']) );
  28827. StartProgram(true);
  28828. Add([
  28829. 'uses sysutils;',
  28830. 'procedure DoIt;',
  28831. 'var',
  28832. ' b: boolean;',
  28833. ' s: string;',
  28834. 'begin',
  28835. ' {$Assertions on}',
  28836. ' Assert(b);',
  28837. ' Assert(b,''msg'');',
  28838. 'end;',
  28839. 'begin',
  28840. ' DoIt;',
  28841. '']);
  28842. ConvertProgram;
  28843. CheckSource('TestAssert_SysUtils',
  28844. LinesToStr([ // statements
  28845. 'this.DoIt = function () {',
  28846. ' var b = false;',
  28847. ' var s = "";',
  28848. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  28849. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  28850. '};',
  28851. '']),
  28852. LinesToStr([ // $mod.$main
  28853. '$mod.DoIt();',
  28854. '']));
  28855. end;
  28856. procedure TTestModule.TestObjectChecks;
  28857. begin
  28858. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  28859. StartProgram(false);
  28860. Add([
  28861. 'type',
  28862. ' TObject = class',
  28863. ' procedure DoIt;',
  28864. ' end;',
  28865. ' TClass = class of tobject;',
  28866. ' TBird = class',
  28867. ' end;',
  28868. ' TBirdClass = class of TBird;',
  28869. 'var',
  28870. ' o : TObject;',
  28871. ' c: TClass;',
  28872. ' b: TBird;',
  28873. ' bc: TBirdClass;',
  28874. 'procedure TObject.DoIt;',
  28875. 'begin',
  28876. ' b:=TBird(o);',
  28877. 'end;',
  28878. 'begin',
  28879. ' o.DoIt;',
  28880. ' b:=TBird(o);',
  28881. ' bc:=TBirdClass(c);',
  28882. '']);
  28883. ConvertProgram;
  28884. CheckSource('TestCheckMethodCall',
  28885. LinesToStr([ // statements
  28886. 'rtl.createClass($mod, "TObject", null, function () {',
  28887. ' this.$init = function () {',
  28888. ' };',
  28889. ' this.$final = function () {',
  28890. ' };',
  28891. ' this.DoIt = function () {',
  28892. ' rtl.checkMethodCall(this,$mod.TObject);',
  28893. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  28894. ' };',
  28895. '});',
  28896. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28897. '});',
  28898. 'this.o = null;',
  28899. 'this.c = null;',
  28900. 'this.b = null;',
  28901. 'this.bc = null;',
  28902. '']),
  28903. LinesToStr([ // $mod.$main
  28904. '$mod.o.DoIt();',
  28905. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  28906. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  28907. '']));
  28908. end;
  28909. procedure TTestModule.TestRangeChecks_AssignInt;
  28910. begin
  28911. Scanner.Options:=Scanner.Options+[po_CAssignments];
  28912. StartProgram(false);
  28913. Add([
  28914. '{$R+}',
  28915. 'var',
  28916. ' b: byte = 2;',
  28917. ' w: word = 3;',
  28918. 'procedure DoIt(p: byte);',
  28919. 'begin',
  28920. ' b:=w;',
  28921. ' b+=w;',
  28922. ' b:=1;',
  28923. 'end;',
  28924. '{$R-}',
  28925. 'procedure DoSome;',
  28926. 'begin',
  28927. ' DoIt(w);',
  28928. ' b:=w;',
  28929. ' b:=2;',
  28930. 'end;',
  28931. 'begin',
  28932. '{$R+}',
  28933. '']);
  28934. ConvertProgram;
  28935. CheckSource('TestRangeChecks_AssignInt',
  28936. LinesToStr([ // statements
  28937. 'this.b = 2;',
  28938. 'this.w = 3;',
  28939. 'this.DoIt = function (p) {',
  28940. ' rtl.rc(p, 0, 255);',
  28941. ' $mod.b = rtl.rc($mod.w,0,255);',
  28942. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  28943. ' $mod.b = 1;',
  28944. '};',
  28945. 'this.DoSome = function () {',
  28946. ' $mod.DoIt($mod.w);',
  28947. ' $mod.b = $mod.w;',
  28948. ' $mod.b = 2;',
  28949. '};',
  28950. '']),
  28951. LinesToStr([ // $mod.$main
  28952. '']));
  28953. end;
  28954. procedure TTestModule.TestRangeChecks_AssignIntRange;
  28955. begin
  28956. Scanner.Options:=Scanner.Options+[po_CAssignments];
  28957. StartProgram(false);
  28958. Add([
  28959. '{$R+}',
  28960. 'type Ten = 1..10;',
  28961. 'var',
  28962. ' b: Ten = 2;',
  28963. ' w: Ten = 3;',
  28964. 'procedure DoIt(p: Ten);',
  28965. 'begin',
  28966. ' b:=w;',
  28967. ' b+=w;',
  28968. ' b:=1;',
  28969. 'end;',
  28970. '{$R-}',
  28971. 'procedure DoSome;',
  28972. 'begin',
  28973. ' DoIt(w);',
  28974. ' b:=w;',
  28975. ' b:=2;',
  28976. 'end;',
  28977. 'begin',
  28978. '{$R+}',
  28979. '']);
  28980. ConvertProgram;
  28981. CheckSource('TestRangeChecks_AssignIntRange',
  28982. LinesToStr([ // statements
  28983. 'this.b = 2;',
  28984. 'this.w = 3;',
  28985. 'this.DoIt = function (p) {',
  28986. ' rtl.rc(p, 1, 10);',
  28987. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  28988. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  28989. ' $mod.b = 1;',
  28990. '};',
  28991. 'this.DoSome = function () {',
  28992. ' $mod.DoIt($mod.w);',
  28993. ' $mod.b = $mod.w;',
  28994. ' $mod.b = 2;',
  28995. '};',
  28996. '']),
  28997. LinesToStr([ // $mod.$main
  28998. '']));
  28999. end;
  29000. procedure TTestModule.TestRangeChecks_AssignEnum;
  29001. begin
  29002. StartProgram(false);
  29003. Add([
  29004. '{$R+}',
  29005. 'type TEnum = (red,green);',
  29006. 'var',
  29007. ' e: TEnum = red;',
  29008. 'procedure DoIt(p: TEnum);',
  29009. 'begin',
  29010. ' e:=p;',
  29011. ' p:=TEnum(0);',
  29012. ' p:=succ(e);',
  29013. 'end;',
  29014. '{$R-}',
  29015. 'procedure DoSome;',
  29016. 'begin',
  29017. ' DoIt(e);',
  29018. ' e:=TEnum(1);',
  29019. ' e:=pred(e);',
  29020. 'end;',
  29021. 'begin',
  29022. '{$R+}',
  29023. '']);
  29024. ConvertProgram;
  29025. CheckSource('TestRangeChecks_AssignEnum',
  29026. LinesToStr([ // statements
  29027. 'this.TEnum = {',
  29028. ' "0": "red",',
  29029. ' red: 0,',
  29030. ' "1": "green",',
  29031. ' green: 1',
  29032. '};',
  29033. 'this.e = $mod.TEnum.red;',
  29034. 'this.DoIt = function (p) {',
  29035. ' rtl.rc(p, 0, 1);',
  29036. ' $mod.e = rtl.rc(p, 0, 1);',
  29037. ' p = 0;',
  29038. ' p = rtl.rc($mod.e + 1, 0, 1);',
  29039. '};',
  29040. 'this.DoSome = function () {',
  29041. ' $mod.DoIt($mod.e);',
  29042. ' $mod.e = 1;',
  29043. ' $mod.e = $mod.e - 1;',
  29044. '};',
  29045. '']),
  29046. LinesToStr([ // $mod.$main
  29047. '']));
  29048. end;
  29049. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  29050. begin
  29051. StartProgram(false);
  29052. Add([
  29053. '{$R+}',
  29054. 'type',
  29055. ' TEnum = (red,green);',
  29056. ' TEnumRg = red..green;',
  29057. 'var',
  29058. ' e: TEnumRg = red;',
  29059. 'procedure DoIt(p: TEnumRg);',
  29060. 'begin',
  29061. ' e:=p;',
  29062. ' p:=TEnumRg(0);',
  29063. ' p:=succ(e);',
  29064. 'end;',
  29065. '{$R-}',
  29066. 'procedure DoSome;',
  29067. 'begin',
  29068. ' DoIt(e);',
  29069. ' e:=TEnum(1);',
  29070. ' e:=pred(e);',
  29071. 'end;',
  29072. 'begin',
  29073. '{$R+}',
  29074. '']);
  29075. ConvertProgram;
  29076. CheckSource('TestRangeChecks_AssignEnumRange',
  29077. LinesToStr([ // statements
  29078. 'this.TEnum = {',
  29079. ' "0": "red",',
  29080. ' red: 0,',
  29081. ' "1": "green",',
  29082. ' green: 1',
  29083. '};',
  29084. 'this.e = $mod.TEnum.red;',
  29085. 'this.DoIt = function (p) {',
  29086. ' rtl.rc(p, 0, 1);',
  29087. ' $mod.e = rtl.rc(p, 0, 1);',
  29088. ' p = 0;',
  29089. ' p = rtl.rc($mod.e + 1, 0, 1);',
  29090. '};',
  29091. 'this.DoSome = function () {',
  29092. ' $mod.DoIt($mod.e);',
  29093. ' $mod.e = 1;',
  29094. ' $mod.e = $mod.e - 1;',
  29095. '};',
  29096. '']),
  29097. LinesToStr([ // $mod.$main
  29098. '']));
  29099. end;
  29100. procedure TTestModule.TestRangeChecks_AssignChar;
  29101. begin
  29102. StartProgram(false);
  29103. Add([
  29104. '{$R+}',
  29105. 'type',
  29106. ' TLetter = char;',
  29107. 'var',
  29108. ' b: TLetter = ''2'';',
  29109. ' w: TLetter = ''3'';',
  29110. 'procedure DoIt(p: TLetter);',
  29111. 'begin',
  29112. ' b:=w;',
  29113. ' b:=''1'';',
  29114. 'end;',
  29115. '{$R-}',
  29116. 'procedure DoSome;',
  29117. 'begin',
  29118. ' DoIt(w);',
  29119. ' b:=w;',
  29120. ' b:=''2'';',
  29121. 'end;',
  29122. 'begin',
  29123. '{$R+}',
  29124. '']);
  29125. ConvertProgram;
  29126. CheckSource('TestRangeChecks_AssignChar',
  29127. LinesToStr([ // statements
  29128. 'this.b = "2";',
  29129. 'this.w = "3";',
  29130. 'this.DoIt = function (p) {',
  29131. ' rtl.rcc(p, 0, 65535);',
  29132. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  29133. ' $mod.b = "1";',
  29134. '};',
  29135. 'this.DoSome = function () {',
  29136. ' $mod.DoIt($mod.w);',
  29137. ' $mod.b = $mod.w;',
  29138. ' $mod.b = "2";',
  29139. '};',
  29140. '']),
  29141. LinesToStr([ // $mod.$main
  29142. '']));
  29143. end;
  29144. procedure TTestModule.TestRangeChecks_AssignCharRange;
  29145. begin
  29146. StartProgram(false);
  29147. Add([
  29148. '{$R+}',
  29149. 'type TDigit = ''0''..''9'';',
  29150. 'var',
  29151. ' b: TDigit = ''2'';',
  29152. ' w: TDigit = ''3'';',
  29153. 'procedure DoIt(p: TDigit);',
  29154. 'begin',
  29155. ' b:=w;',
  29156. ' b:=''1'';',
  29157. 'end;',
  29158. '{$R-}',
  29159. 'procedure DoSome;',
  29160. 'begin',
  29161. ' DoIt(w);',
  29162. ' b:=w;',
  29163. ' b:=''2'';',
  29164. 'end;',
  29165. 'begin',
  29166. '{$R+}',
  29167. '']);
  29168. ConvertProgram;
  29169. CheckSource('TestRangeChecks_AssignCharRange',
  29170. LinesToStr([ // statements
  29171. 'this.b = "2";',
  29172. 'this.w = "3";',
  29173. 'this.DoIt = function (p) {',
  29174. ' rtl.rcc(p, 48, 57);',
  29175. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  29176. ' $mod.b = "1";',
  29177. '};',
  29178. 'this.DoSome = function () {',
  29179. ' $mod.DoIt($mod.w);',
  29180. ' $mod.b = $mod.w;',
  29181. ' $mod.b = "2";',
  29182. '};',
  29183. '']),
  29184. LinesToStr([ // $mod.$main
  29185. '']));
  29186. end;
  29187. procedure TTestModule.TestRangeChecks_ArrayIndex;
  29188. begin
  29189. StartProgram(false);
  29190. Add([
  29191. '{$R+}',
  29192. 'type',
  29193. ' Ten = 1..10;',
  29194. ' TArr = array of Ten;',
  29195. ' TArrArr = array of TArr;',
  29196. ' TArrByte = array[byte] of Ten;',
  29197. ' TArrChar = array[''0''..''9''] of Ten;',
  29198. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  29199. ' TObject = class',
  29200. ' A: TArr;',
  29201. ' end;',
  29202. 'procedure DoIt;',
  29203. 'var',
  29204. ' Arr: TArr;',
  29205. ' ArrArr: TArrArr;',
  29206. ' ArrByte: TArrByte;',
  29207. ' ArrChar: TArrChar;',
  29208. ' ArrByteChar: TArrByteChar;',
  29209. ' i: Ten;',
  29210. ' c: char;',
  29211. ' o: tobject;',
  29212. 'begin',
  29213. ' i:=Arr[1];',
  29214. ' i:=ArrByteChar[1,''2''];',
  29215. ' Arr[1]:=Arr[1];',
  29216. ' Arr[i]:=Arr[i];',
  29217. ' ArrByte[3]:=ArrByte[3];',
  29218. ' ArrByte[i]:=ArrByte[i];',
  29219. ' ArrChar[''5'']:=ArrChar[''5''];',
  29220. ' ArrChar[c]:=ArrChar[c];',
  29221. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  29222. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  29223. ' o.a[i]:=o.a[i];',
  29224. 'end;',
  29225. 'begin',
  29226. '']);
  29227. ConvertProgram;
  29228. CheckSource('TestRangeChecks_ArrayIndex',
  29229. LinesToStr([ // statements
  29230. 'rtl.createClass($mod, "TObject", null, function () {',
  29231. ' this.$init = function () {',
  29232. ' this.A = [];',
  29233. ' };',
  29234. ' this.$final = function () {',
  29235. ' this.A = undefined;',
  29236. ' };',
  29237. '});',
  29238. 'this.DoIt = function () {',
  29239. ' var Arr = [];',
  29240. ' var ArrArr = [];',
  29241. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  29242. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  29243. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  29244. ' var i = 0;',
  29245. ' var c = "";',
  29246. ' var o = null;',
  29247. ' i = rtl.rc(Arr[1], 1, 10);',
  29248. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  29249. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  29250. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  29251. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  29252. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  29253. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  29254. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  29255. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  29256. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  29257. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  29258. '};',
  29259. '']),
  29260. LinesToStr([ // $mod.$main
  29261. '']));
  29262. end;
  29263. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  29264. begin
  29265. StartProgram(false);
  29266. Add([
  29267. '{$R+}',
  29268. 'type',
  29269. ' Ten = 1..10;',
  29270. ' TRec = record x: Ten end;',
  29271. ' TArr = array of TRec;',
  29272. ' TArrArr = array of TArr;',
  29273. ' TObject = class',
  29274. ' A: TArr;',
  29275. ' end;',
  29276. 'procedure DoIt;',
  29277. 'var',
  29278. ' Arr: TArr;',
  29279. ' ArrArr: TArrArr;',
  29280. ' i: Ten;',
  29281. ' o: tobject;',
  29282. 'begin',
  29283. ' Arr[1]:=Arr[1];',
  29284. ' Arr[i]:=Arr[i+1];',
  29285. ' o.a[i]:=o.a[i+2];',
  29286. 'end;',
  29287. 'begin',
  29288. '']);
  29289. ConvertProgram;
  29290. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  29291. LinesToStr([ // statements
  29292. 'rtl.recNewT($mod, "TRec", function () {',
  29293. ' this.x = 0;',
  29294. ' this.$eq = function (b) {',
  29295. ' return this.x === b.x;',
  29296. ' };',
  29297. ' this.$assign = function (s) {',
  29298. ' this.x = s.x;',
  29299. ' return this;',
  29300. ' };',
  29301. '});',
  29302. 'rtl.createClass($mod, "TObject", null, function () {',
  29303. ' this.$init = function () {',
  29304. ' this.A = [];',
  29305. ' };',
  29306. ' this.$final = function () {',
  29307. ' this.A = undefined;',
  29308. ' };',
  29309. '});',
  29310. 'this.DoIt = function () {',
  29311. ' var Arr = [];',
  29312. ' var ArrArr = [];',
  29313. ' var i = 0;',
  29314. ' var o = null;',
  29315. ' Arr[1].$assign(Arr[1]);',
  29316. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  29317. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  29318. '};',
  29319. '']),
  29320. LinesToStr([ // $mod.$main
  29321. '']));
  29322. end;
  29323. procedure TTestModule.TestRangeChecks_StringIndex;
  29324. begin
  29325. StartProgram(false);
  29326. Add([
  29327. 'type',
  29328. ' TObject = class',
  29329. ' S: string;',
  29330. ' end;',
  29331. '{$R+}',
  29332. 'procedure DoIt(var h: string);',
  29333. 'var',
  29334. ' s: string;',
  29335. ' i: longint;',
  29336. ' c: char;',
  29337. ' o: tobject;',
  29338. 'begin',
  29339. ' c:=s[1];',
  29340. ' s[i]:=s[i];',
  29341. ' h[i]:=h[i];',
  29342. ' c:=o.s[i];',
  29343. ' o.s[i]:=c;',
  29344. 'end;',
  29345. 'begin',
  29346. '']);
  29347. ConvertProgram;
  29348. CheckSource('TestRangeChecks_StringIndex',
  29349. LinesToStr([ // statements
  29350. 'rtl.createClass($mod, "TObject", null, function () {',
  29351. ' this.$init = function () {',
  29352. ' this.S = "";',
  29353. ' };',
  29354. ' this.$final = function () {',
  29355. ' };',
  29356. '});',
  29357. 'this.DoIt = function (h) {',
  29358. ' var s = "";',
  29359. ' var i = 0;',
  29360. ' var c = "";',
  29361. ' var o = null;',
  29362. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  29363. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  29364. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  29365. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  29366. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  29367. '};',
  29368. '']),
  29369. LinesToStr([ // $mod.$main
  29370. '']));
  29371. end;
  29372. procedure TTestModule.TestRangeChecks_TypecastInt;
  29373. begin
  29374. StartProgram(false);
  29375. Add([
  29376. '{$R+}',
  29377. 'var',
  29378. ' i: nativeint;',
  29379. ' b: byte;',
  29380. ' sh: shortint;',
  29381. ' w: word;',
  29382. ' sm: smallint;',
  29383. ' lw: longword;',
  29384. ' li: longint;',
  29385. 'begin',
  29386. ' b:=12+byte(i);',
  29387. ' sh:=12+shortint(i);',
  29388. ' w:=12+word(i);',
  29389. ' sm:=12+smallint(i);',
  29390. ' lw:=12+longword(i);',
  29391. ' li:=12+longint(i);',
  29392. '']);
  29393. ConvertProgram;
  29394. CheckSource('TestRangeChecks_TypecastInt',
  29395. LinesToStr([
  29396. 'this.i = 0;',
  29397. 'this.b = 0;',
  29398. 'this.sh = 0;',
  29399. 'this.w = 0;',
  29400. 'this.sm = 0;',
  29401. 'this.lw = 0;',
  29402. 'this.li = 0;',
  29403. '']),
  29404. LinesToStr([
  29405. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  29406. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  29407. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  29408. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  29409. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  29410. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  29411. '']));
  29412. end;
  29413. Initialization
  29414. RegisterTests([TTestModule]);
  29415. end.