tcmodules.pas 811 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711
  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. supTypeInfo
  46. );
  47. TSystemUnitParts = set of TSystemUnitPart;
  48. { TTestHintMessage }
  49. TTestHintMessage = class
  50. public
  51. Id: int64;
  52. MsgType: TMessageType;
  53. MsgNumber: integer;
  54. Msg: string;
  55. SourcePos: TPasSourcePos;
  56. end;
  57. { TTestPasParser }
  58. TTestPasParser = Class(TPasParser)
  59. end;
  60. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  61. { TTestEnginePasResolver }
  62. TTestEnginePasResolver = class(TPas2JsResolver)
  63. private
  64. FFilename: string;
  65. FModule: TPasModule;
  66. FOnFindUnit: TOnFindUnit;
  67. FParser: TTestPasParser;
  68. FStreamResolver: TStreamResolver;
  69. FScanner: TPas2jsPasScanner;
  70. FSource: string;
  71. public
  72. destructor Destroy; override;
  73. function FindUnit(const AName, InFilename: String; NameExpr,
  74. InFileExpr: TPasExpr): TPasModule; override;
  75. procedure UsedInterfacesFinished(Section: TPasSection); override;
  76. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  77. property Filename: string read FFilename write FFilename;
  78. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  79. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  80. property Parser: TTestPasParser read FParser write FParser;
  81. property Source: string read FSource write FSource;
  82. property Module: TPasModule read FModule;
  83. end;
  84. { TCustomTestModule }
  85. TCustomTestModule = Class(TTestCase)
  86. private
  87. FConverter: TPasToJSConverter;
  88. FEngine: TTestEnginePasResolver;
  89. FExpectedErrorClass: ExceptClass;
  90. FExpectedErrorMsg: string;
  91. FExpectedErrorNumber: integer;
  92. FFilename: string;
  93. FFileResolver: TStreamResolver;
  94. FJSImplementationSrc: TJSSourceElements;
  95. FJSImplementationUses: TJSArrayLiteral;
  96. FJSInitBody: TJSFunctionBody;
  97. FJSImplentationUses: TJSArrayLiteral;
  98. FJSInterfaceUses: TJSArrayLiteral;
  99. FJSModule: TJSSourceElements;
  100. FJSModuleSrc: TJSSourceElements;
  101. FJSSource: TStringList;
  102. FModule: TPasModule;
  103. FJSModuleCallArgs: TJSArguments;
  104. FModules: TObjectList;// list of TTestEnginePasResolver
  105. FParser: TTestPasParser;
  106. FPasProgram: TPasProgram;
  107. FHintMsgs: TObjectList; // list of TTestHintMessage
  108. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  109. FJSRegModuleCall: TJSCallExpression;
  110. FScanner: TPas2jsPasScanner;
  111. FSkipTests: boolean;
  112. FSource: TStringList;
  113. FFirstPasStatement: TPasImplBlock;
  114. {$IFDEF EnablePasTreeGlobalRefCount}
  115. FElementRefCountAtSetup: int64;
  116. {$ENDIF}
  117. function GetMsgCount: integer;
  118. function GetMsgs(Index: integer): TTestHintMessage;
  119. function GetResolverCount: integer;
  120. function GetResolvers(Index: integer): TTestEnginePasResolver;
  121. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  122. procedure OnParserLog(Sender: TObject; const Msg: String);
  123. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  124. procedure OnScannerLog(Sender: TObject; const Msg: String);
  125. protected
  126. procedure SetUp; override;
  127. function CreateConverter: TPasToJSConverter; virtual;
  128. function LoadUnit(const aUnitName: String): TPasModule;
  129. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  130. procedure TearDown; override;
  131. Procedure Add(Line: string); virtual;
  132. Procedure Add(const Lines: array of string);
  133. Procedure StartParsing; virtual;
  134. procedure ParseModuleQueue; virtual;
  135. procedure ParseModule; virtual;
  136. procedure ParseProgram; virtual;
  137. procedure ParseUnit; virtual;
  138. protected
  139. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  140. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  141. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  142. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  143. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  144. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  145. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  146. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  147. procedure ConvertModule; virtual;
  148. procedure ConvertProgram; virtual;
  149. procedure ConvertUnit; virtual;
  150. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  151. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  152. function GetDottedIdentifier(El: TJSElement): string;
  153. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  154. ImplStatements: string = ''); virtual;
  155. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  156. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  157. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  158. Msg: string; Marker: PSrcMarker = nil); virtual;
  159. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  160. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  161. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  162. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  163. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  164. function IsErrorExpected(E: Exception): boolean;
  165. procedure HandleScannerError(E: EScannerError);
  166. procedure HandleParserError(E: EParserError);
  167. procedure HandlePasResolveError(E: EPasResolve);
  168. procedure HandlePas2JSError(E: EPas2JS);
  169. procedure HandleException(E: Exception);
  170. procedure FailException(E: Exception);
  171. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  172. function IndexOfResolver(const Filename: string): integer;
  173. function GetResolver(const Filename: string): TTestEnginePasResolver;
  174. function GetDefaultNamespace: string;
  175. property PasProgram: TPasProgram Read FPasProgram;
  176. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  177. property ResolverCount: integer read GetResolverCount;
  178. property Engine: TTestEnginePasResolver read FEngine;
  179. property Filename: string read FFilename;
  180. Property Module: TPasModule Read FModule;
  181. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  182. property Converter: TPasToJSConverter read FConverter;
  183. property JSSource: TStringList read FJSSource;
  184. property JSModule: TJSSourceElements read FJSModule;
  185. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  186. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  187. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  188. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  189. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  190. property JSInitBody: TJSFunctionBody read FJSInitBody;
  191. property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
  192. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  193. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  194. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  195. property SkipTests: boolean read FSkipTests write FSkipTests;
  196. public
  197. constructor Create; override;
  198. destructor Destroy; override;
  199. property Source: TStringList read FSource;
  200. property FileResolver: TStreamResolver read FFileResolver;
  201. property Scanner: TPas2jsPasScanner read FScanner;
  202. property Parser: TTestPasParser read FParser;
  203. property MsgCount: integer read GetMsgCount;
  204. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  205. end;
  206. { TTestModule }
  207. TTestModule = class(TCustomTestModule)
  208. Published
  209. Procedure TestReservedWords;
  210. // program/units
  211. Procedure TestEmptyProgram;
  212. Procedure TestEmptyProgramUseStrict;
  213. Procedure TestEmptyUnit;
  214. Procedure TestEmptyUnitUseStrict;
  215. Procedure TestDottedUnitNames;
  216. Procedure TestDottedUnitNameImpl;
  217. Procedure TestDottedUnitExpr;
  218. Procedure Test_ModeFPCFail;
  219. Procedure Test_ModeSwitchCBlocksFail;
  220. Procedure TestUnit_UseSystem;
  221. Procedure TestUnit_Intf1Impl2Intf1;
  222. Procedure TestIncludeVersion;
  223. // vars/const
  224. Procedure TestVarInt;
  225. Procedure TestVarBaseTypes;
  226. Procedure TestBaseTypeSingleFail;
  227. Procedure TestBaseTypeExtendedFail;
  228. Procedure TestConstBaseTypes;
  229. Procedure TestUnitImplVars;
  230. Procedure TestUnitImplConsts;
  231. Procedure TestUnitImplRecord;
  232. Procedure TestRenameJSNameConflict;
  233. Procedure TestLocalConst;
  234. Procedure TestVarExternal;
  235. Procedure TestVarExternalOtherUnit;
  236. Procedure TestVarAbsoluteFail;
  237. Procedure TestConstExternal;
  238. // numbers
  239. Procedure TestDouble;
  240. Procedure TestInteger;
  241. Procedure TestIntegerRange;
  242. Procedure TestIntegerTypecasts;
  243. Procedure TestInteger_BitwiseShrNativeInt;
  244. Procedure TestInteger_BitwiseShlNativeInt;
  245. Procedure TestCurrency;
  246. Procedure TestForBoolDo;
  247. Procedure TestForIntDo;
  248. Procedure TestForIntInDo;
  249. // strings
  250. Procedure TestCharConst;
  251. Procedure TestChar_Compare;
  252. Procedure TestChar_BuiltInProcs;
  253. Procedure TestStringConst;
  254. Procedure TestStringConstSurrogate;
  255. Procedure TestString_Length;
  256. Procedure TestString_Compare;
  257. Procedure TestString_SetLength;
  258. Procedure TestString_CharAt;
  259. Procedure TestStringHMinusFail;
  260. Procedure TestStr;
  261. Procedure TestBaseType_AnsiStringFail;
  262. Procedure TestBaseType_WideStringFail;
  263. Procedure TestBaseType_ShortStringFail;
  264. Procedure TestBaseType_RawByteStringFail;
  265. Procedure TestTypeShortstring_Fail;
  266. Procedure TestCharSet_Custom;
  267. Procedure TestForCharDo;
  268. Procedure TestForCharInDo;
  269. // alias types
  270. Procedure TestAliasTypeRef;
  271. Procedure TestTypeCast_BaseTypes;
  272. Procedure TestTypeCast_AliasBaseTypes;
  273. // functions
  274. Procedure TestEmptyProc;
  275. Procedure TestProcOneParam;
  276. Procedure TestFunctionWithoutParams;
  277. Procedure TestProcedureWithoutParams;
  278. Procedure TestPrgProcVar;
  279. Procedure TestProcTwoArgs;
  280. Procedure TestProc_DefaultValue;
  281. Procedure TestUnitProcVar;
  282. Procedure TestImplProc;
  283. Procedure TestFunctionResult;
  284. Procedure TestNestedProc;
  285. Procedure TestNestedProc_ResultString;
  286. Procedure TestForwardProc;
  287. Procedure TestNestedForwardProc;
  288. Procedure TestAssignFunctionResult;
  289. Procedure TestFunctionResultInCondition;
  290. Procedure TestFunctionResultInForLoop;
  291. Procedure TestFunctionResultInTypeCast;
  292. Procedure TestExit;
  293. Procedure TestBreak;
  294. Procedure TestBreakAsVar;
  295. Procedure TestContinue;
  296. Procedure TestProc_External;
  297. Procedure TestProc_ExternalOtherUnit;
  298. Procedure TestProc_Asm;
  299. Procedure TestProc_Assembler;
  300. Procedure TestProc_VarParam;
  301. Procedure TestProc_VarParamString;
  302. Procedure TestProc_VarParamV;
  303. Procedure TestProc_Overload;
  304. Procedure TestProc_OverloadForward;
  305. Procedure TestProc_OverloadIntfImpl;
  306. Procedure TestProc_OverloadNested;
  307. Procedure TestProc_OverloadNestedForward;
  308. Procedure TestProc_OverloadUnitCycle;
  309. Procedure TestProc_Varargs;
  310. Procedure TestProc_ConstOrder;
  311. Procedure TestProc_DuplicateConst;
  312. Procedure TestProc_LocalVarAbsolute;
  313. Procedure TestProc_LocalVarInit;
  314. Procedure TestProc_ReservedWords;
  315. // anonymous functions
  316. Procedure TestAnonymousProc_Assign_ObjFPC;
  317. Procedure TestAnonymousProc_Assign_Delphi;
  318. Procedure TestAnonymousProc_Arg;
  319. Procedure TestAnonymousProc_Typecast;
  320. Procedure TestAnonymousProc_With;
  321. Procedure TestAnonymousProc_ExceptOn;
  322. Procedure TestAnonymousProc_Nested;
  323. Procedure TestAnonymousProc_NestedAssignResult;
  324. Procedure TestAnonymousProc_Class;
  325. Procedure TestAnonymousProc_ForLoop;
  326. // enums, sets
  327. Procedure TestEnum_Name;
  328. Procedure TestEnum_Number;
  329. Procedure TestEnum_ConstFail;
  330. Procedure TestEnum_Functions;
  331. Procedure TestEnum_AsParams;
  332. Procedure TestEnumRange_Array;
  333. Procedure TestEnum_ForIn;
  334. Procedure TestEnum_ScopedNumber;
  335. Procedure TestEnum_InFunction;
  336. Procedure TestSet_Enum;
  337. Procedure TestSet_Operators;
  338. Procedure TestSet_Operator_In;
  339. Procedure TestSet_Functions;
  340. Procedure TestSet_PassAsArgClone;
  341. Procedure TestSet_AsParams;
  342. Procedure TestSet_Property;
  343. Procedure TestSet_EnumConst;
  344. Procedure TestSet_IntConst;
  345. Procedure TestSet_AnonymousEnumType;
  346. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  347. Procedure TestSet_ConstEnum;
  348. Procedure TestSet_ConstChar;
  349. Procedure TestSet_ConstInt;
  350. Procedure TestSet_InFunction;
  351. Procedure TestSet_ForIn;
  352. // statements
  353. Procedure TestNestBegin;
  354. Procedure TestIncDec;
  355. Procedure TestLoHiFpcMode;
  356. Procedure TestLoHiDelphiMode;
  357. Procedure TestAssignments;
  358. Procedure TestArithmeticOperators1;
  359. Procedure TestLogicalOperators;
  360. Procedure TestBitwiseOperators;
  361. Procedure TestBitwiseOperatorsLongword;
  362. Procedure TestFunctionInt;
  363. Procedure TestFunctionString;
  364. Procedure TestIfThen;
  365. Procedure TestForLoop;
  366. Procedure TestForLoopInsideFunction;
  367. Procedure TestForLoop_ReadVarAfter;
  368. Procedure TestForLoop_Nested;
  369. Procedure TestRepeatUntil;
  370. Procedure TestAsmBlock;
  371. Procedure TestAsmPas_Impl; // ToDo
  372. Procedure TestTryFinally;
  373. Procedure TestTryExcept;
  374. Procedure TestTryExcept_ReservedWords;
  375. Procedure TestIfThenRaiseElse;
  376. Procedure TestCaseOf;
  377. Procedure TestCaseOf_UseSwitch;
  378. Procedure TestCaseOfNoElse;
  379. Procedure TestCaseOfNoElse_UseSwitch;
  380. Procedure TestCaseOfRange;
  381. Procedure TestCaseOfString;
  382. Procedure TestCaseOfChar;
  383. Procedure TestCaseOfExternalClassConst;
  384. Procedure TestDebugger;
  385. // arrays
  386. Procedure TestArray_Dynamic;
  387. Procedure TestArray_Dynamic_Nil;
  388. Procedure TestArray_DynMultiDimensional;
  389. Procedure TestArray_StaticInt;
  390. Procedure TestArray_StaticBool;
  391. Procedure TestArray_StaticChar;
  392. Procedure TestArray_StaticMultiDim;
  393. Procedure TestArray_StaticInFunction;
  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_OpenArrayOfString;
  406. Procedure TestArray_Concat;
  407. Procedure TestArray_Copy;
  408. Procedure TestArray_InsertDelete;
  409. Procedure TestArray_DynArrayConstObjFPC;
  410. Procedure TestArray_DynArrayConstDelphi;
  411. Procedure TestArray_ArrayLitAsParam;
  412. Procedure TestArray_ArrayLitMultiDimAsParam;
  413. Procedure TestArray_ArrayLitStaticAsParam;
  414. Procedure TestArray_ForInArrOfString;
  415. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  416. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  417. Procedure TestArrayOfConst_TVarRec;
  418. Procedure TestArrayOfConst_PassBaseTypes;
  419. Procedure TestArrayOfConst_PassObj;
  420. // record
  421. Procedure TestRecord_Empty;
  422. Procedure TestRecord_Var;
  423. Procedure TestRecord_VarExternal;
  424. Procedure TestRecord_WithDo;
  425. Procedure TestRecord_Assign;
  426. Procedure TestRecord_AsParams;
  427. Procedure TestRecordElement_AsParams;
  428. Procedure TestRecordElementFromFuncResult_AsParams;
  429. Procedure TestRecordElementFromWith_AsParams;
  430. Procedure TestRecord_Equal;
  431. Procedure TestRecord_JSValue;
  432. Procedure TestRecord_VariantFail;
  433. Procedure TestRecord_FieldArray;
  434. Procedure TestRecord_Const;
  435. Procedure TestRecord_TypecastFail;
  436. Procedure TestRecord_InFunction;
  437. Procedure TestRecord_AnonymousFail;
  438. // ToDo: RTTI of local record
  439. // ToDo: pcu local record, name clash and rtti
  440. // advanced record
  441. Procedure TestAdvRecord_Function;
  442. Procedure TestAdvRecord_Property;
  443. Procedure TestAdvRecord_PropertyDefault;
  444. Procedure TestAdvRecord_Property_ClassMethod;
  445. Procedure TestAdvRecord_Const;
  446. Procedure TestAdvRecord_ExternalField;
  447. Procedure TestAdvRecord_SubRecord;
  448. Procedure TestAdvRecord_SubClass;
  449. Procedure TestAdvRecord_SubInterfaceFail;
  450. Procedure TestAdvRecord_Constructor;
  451. Procedure TestAdvRecord_ClassConstructor_Program;
  452. Procedure TestAdvRecord_ClassConstructor_Unit;
  453. // classes
  454. Procedure TestClass_TObjectDefaultConstructor;
  455. Procedure TestClass_TObjectConstructorWithParams;
  456. Procedure TestClass_TObjectConstructorWithDefaultParam;
  457. Procedure TestClass_Var;
  458. Procedure TestClass_Method;
  459. Procedure TestClass_Implementation;
  460. Procedure TestClass_Inheritance;
  461. Procedure TestClass_TypeAlias;
  462. Procedure TestClass_AbstractMethod;
  463. Procedure TestClass_CallInherited_ProcNoParams;
  464. Procedure TestClass_CallInherited_WithParams;
  465. Procedure TestClasS_CallInheritedConstructor;
  466. Procedure TestClass_ClassVar_Assign;
  467. Procedure TestClass_CallClassMethod;
  468. Procedure TestClass_Property;
  469. Procedure TestClass_Property_ClassMethod;
  470. Procedure TestClass_Property_Indexed;
  471. Procedure TestClass_Property_IndexSpec;
  472. Procedure TestClass_PropertyOfTypeArray;
  473. Procedure TestClass_PropertyDefault;
  474. Procedure TestClass_PropertyDefault2;
  475. Procedure TestClass_PropertyOverride;
  476. Procedure TestClass_PropertyIncVisibility;
  477. Procedure TestClass_Assigned;
  478. Procedure TestClass_WithClassDoCreate;
  479. Procedure TestClass_WithClassInstDoProperty;
  480. Procedure TestClass_WithClassInstDoPropertyWithParams;
  481. Procedure TestClass_WithClassInstDoFunc;
  482. Procedure TestClass_TypeCast;
  483. Procedure TestClass_TypeCastUntypedParam;
  484. Procedure TestClass_Overloads;
  485. Procedure TestClass_OverloadsAncestor;
  486. Procedure TestClass_OverloadConstructor;
  487. Procedure TestClass_OverloadDelphiOverride;
  488. Procedure TestClass_ReintroducedVar;
  489. Procedure TestClass_RaiseDescendant;
  490. Procedure TestClass_ExternalMethod;
  491. Procedure TestClass_ExternalVirtualNameMismatchFail;
  492. Procedure TestClass_ExternalOverrideFail;
  493. Procedure TestClass_ExternalVar;
  494. Procedure TestClass_Const;
  495. Procedure TestClass_LocalConstDuplicate;
  496. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  497. Procedure TestClass_LocalVarSelfFail;
  498. Procedure TestClass_ArgSelfFail;
  499. Procedure TestClass_NestedProcSelf;
  500. Procedure TestClass_NestedProcSelf2;
  501. Procedure TestClass_NestedProcClassSelf;
  502. Procedure TestClass_NestedProcCallInherited;
  503. Procedure TestClass_TObjectFree;
  504. Procedure TestClass_TObjectFree_VarArg;
  505. Procedure TestClass_TObjectFreeNewInstance;
  506. Procedure TestClass_TObjectFreeLowerCase;
  507. Procedure TestClass_TObjectFreeFunctionFail;
  508. Procedure TestClass_TObjectFreePropertyFail;
  509. Procedure TestClass_ForIn;
  510. Procedure TestClass_DispatchMessage;
  511. Procedure TestClass_Message_DuplicateIntFail;
  512. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  513. // class of
  514. Procedure TestClassOf_Create;
  515. Procedure TestClassOf_Call;
  516. Procedure TestClassOf_Assign;
  517. Procedure TestClassOf_Is;
  518. Procedure TestClassOf_Compare;
  519. Procedure TestClassOf_ClassVar;
  520. Procedure TestClassOf_ClassMethod;
  521. Procedure TestClassOf_ClassProperty;
  522. Procedure TestClassOf_ClassMethodSelf;
  523. Procedure TestClassOf_TypeCast;
  524. Procedure TestClassOf_ImplicitFunctionCall;
  525. Procedure TestClassOf_Const;
  526. // nested class
  527. Procedure TestNestedClass_Alias;
  528. Procedure TestNestedClass_Record;
  529. Procedure TestNestedClass_Class;
  530. // external class
  531. Procedure TestExternalClass_Var;
  532. Procedure TestExternalClass_Const;
  533. Procedure TestExternalClass_Dollar;
  534. Procedure TestExternalClass_DuplicateVarFail;
  535. Procedure TestExternalClass_Method;
  536. Procedure TestExternalClass_ClassMethod;
  537. Procedure TestExternalClass_ClassMethodStatic;
  538. Procedure TestExternalClass_FunctionResultInTypeCast;
  539. Procedure TestExternalClass_NonExternalOverride;
  540. Procedure TestExternalClass_OverloadHint;
  541. Procedure TestExternalClass_SameNamePublishedProperty;
  542. Procedure TestExternalClass_Property;
  543. Procedure TestExternalClass_PropertyDate;
  544. Procedure TestExternalClass_ClassProperty;
  545. Procedure TestExternalClass_ClassOf;
  546. Procedure TestExternalClass_ClassOtherUnit;
  547. Procedure TestExternalClass_Is;
  548. Procedure TestExternalClass_As;
  549. Procedure TestExternalClass_DestructorFail;
  550. Procedure TestExternalClass_New;
  551. Procedure TestExternalClass_ClassOf_New;
  552. Procedure TestExternalClass_FuncClassOf_New;
  553. Procedure TestExternalClass_New_PasClassFail;
  554. Procedure TestExternalClass_New_PasClassBracketsFail;
  555. Procedure TestExternalClass_NewExtName;
  556. Procedure TestExternalClass_Constructor;
  557. Procedure TestExternalClass_ConstructorBrackets;
  558. Procedure TestExternalClass_LocalConstSameName;
  559. Procedure TestExternalClass_ReintroduceOverload;
  560. Procedure TestExternalClass_Inherited;
  561. Procedure TestExternalClass_PascalAncestorFail;
  562. Procedure TestExternalClass_NewInstance;
  563. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  564. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  565. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  566. Procedure TestExternalClass_PascalProperty;
  567. Procedure TestExternalClass_TypeCastToRootClass;
  568. Procedure TestExternalClass_TypeCastToJSObject;
  569. Procedure TestExternalClass_TypeCastStringToExternalString;
  570. Procedure TestExternalClass_TypeCastToJSFunction;
  571. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  572. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  573. Procedure TestExternalClass_BracketAccessor;
  574. Procedure TestExternalClass_BracketAccessor_Call;
  575. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  576. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  577. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  578. Procedure TestExternalClass_BracketAccessor_MultiType;
  579. Procedure TestExternalClass_BracketAccessor_Index;
  580. Procedure TestExternalClass_ForInJSObject;
  581. Procedure TestExternalClass_ForInJSArray;
  582. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  583. // class interfaces
  584. Procedure TestClassInterface_Corba;
  585. Procedure TestClassInterface_ProcExternalFail;
  586. Procedure TestClassInterface_Overloads;
  587. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  588. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  589. Procedure TestClassInterface_AncestorImpl;
  590. Procedure TestClassInterface_ImplReintroduce;
  591. Procedure TestClassInterface_MethodResolution;
  592. Procedure TestClassInterface_AncestorMoreInterfaces;
  593. Procedure TestClassInterface_MethodOverride;
  594. Procedure TestClassInterface_Corba_Delegation;
  595. Procedure TestClassInterface_Corba_DelegationStatic;
  596. Procedure TestClassInterface_Corba_Operators;
  597. Procedure TestClassInterface_Corba_Args;
  598. Procedure TestClassInterface_Corba_ForIn;
  599. Procedure TestClassInterface_COM_AssignVar;
  600. Procedure TestClassInterface_COM_AssignArg;
  601. Procedure TestClassInterface_COM_FunctionResult;
  602. Procedure TestClassInterface_COM_InheritedFuncResult;
  603. Procedure TestClassInterface_COM_IsAsTypeCasts;
  604. Procedure TestClassInterface_COM_PassAsArg;
  605. Procedure TestClassInterface_COM_PassToUntypedParam;
  606. Procedure TestClassInterface_COM_FunctionInExpr;
  607. Procedure TestClassInterface_COM_Property;
  608. Procedure TestClassInterface_COM_IntfProperty;
  609. Procedure TestClassInterface_COM_Delegation;
  610. Procedure TestClassInterface_COM_With;
  611. Procedure TestClassInterface_COM_ForIn;
  612. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  613. Procedure TestClassInterface_COM_RecordIntfFail;
  614. Procedure TestClassInterface_COM_UnitInitialization;
  615. Procedure TestClassInterface_GUID;
  616. Procedure TestClassInterface_GUIDProperty;
  617. // helpers
  618. Procedure TestClassHelper_ClassVar;
  619. Procedure TestClassHelper_Method_AccessInstanceFields;
  620. Procedure TestClassHelper_Method_Call;
  621. Procedure TestClassHelper_Method_Nested_Call;
  622. Procedure TestClassHelper_ClassMethod_Call;
  623. Procedure TestClassHelper_ClassOf;
  624. Procedure TestClassHelper_MethodRefObjFPC;
  625. Procedure TestClassHelper_Constructor;
  626. Procedure TestClassHelper_InheritedObjFPC;
  627. Procedure TestClassHelper_Property;
  628. Procedure TestClassHelper_Property_Array;
  629. Procedure TestClassHelper_Property_Array_Default;
  630. Procedure TestClassHelper_Property_Array_DefaultDefault;
  631. Procedure TestClassHelper_ClassProperty;
  632. Procedure TestClassHelper_ClassPropertyStatic;
  633. Procedure TestClassHelper_ClassProperty_Array;
  634. Procedure TestClassHelper_ForIn;
  635. Procedure TestClassHelper_PassProperty;
  636. Procedure TestExtClassHelper_ClassVar;
  637. Procedure TestExtClassHelper_Method_Call;
  638. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  639. Procedure TestRecordHelper_ClassVar;
  640. Procedure TestRecordHelper_Method_Call;
  641. Procedure TestRecordHelper_Constructor;
  642. Procedure TestTypeHelper_ClassVar;
  643. Procedure TestTypeHelper_PassResultElement;
  644. Procedure TestTypeHelper_PassArgs;
  645. Procedure TestTypeHelper_PassVarConst;
  646. Procedure TestTypeHelper_PassFuncResult;
  647. Procedure TestTypeHelper_PassPropertyField;
  648. Procedure TestTypeHelper_PassPropertyGetter;
  649. Procedure TestTypeHelper_PassClassPropertyField;
  650. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  651. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  652. Procedure TestTypeHelper_Property;
  653. Procedure TestTypeHelper_Property_Array;
  654. Procedure TestTypeHelper_ClassProperty;
  655. Procedure TestTypeHelper_ClassProperty_Array;
  656. Procedure TestTypeHelper_ClassMethod;
  657. Procedure TestTypeHelper_ExtClassMethodFail;
  658. Procedure TestTypeHelper_Constructor;
  659. Procedure TestTypeHelper_Word;
  660. Procedure TestTypeHelper_Double;
  661. Procedure TestTypeHelper_StringChar;
  662. Procedure TestTypeHelper_JSValue;
  663. Procedure TestTypeHelper_Array;
  664. Procedure TestTypeHelper_EnumType;
  665. Procedure TestTypeHelper_SetType;
  666. Procedure TestTypeHelper_InterfaceType;
  667. // proc types
  668. Procedure TestProcType;
  669. Procedure TestProcType_Arg;
  670. Procedure TestProcType_FunctionFPC;
  671. Procedure TestProcType_FunctionDelphi;
  672. Procedure TestProcType_ProcedureDelphi;
  673. Procedure TestProcType_AsParam;
  674. Procedure TestProcType_MethodFPC;
  675. Procedure TestProcType_MethodDelphi;
  676. Procedure TestProcType_PropertyFPC;
  677. Procedure TestProcType_PropertyDelphi;
  678. Procedure TestProcType_WithClassInstDoPropertyFPC;
  679. Procedure TestProcType_Nested;
  680. Procedure TestProcType_NestedOfObject;
  681. Procedure TestProcType_ReferenceToProc;
  682. Procedure TestProcType_ReferenceToMethod;
  683. Procedure TestProcType_Typecast;
  684. Procedure TestProcType_PassProcToUntyped;
  685. Procedure TestProcType_PassProcToArray;
  686. // pointer
  687. Procedure TestPointer;
  688. Procedure TestPointer_Proc;
  689. Procedure TestPointer_AssignRecordFail;
  690. Procedure TestPointer_AssignStaticArrayFail;
  691. Procedure TestPointer_TypeCastJSValueToPointer;
  692. Procedure TestPointer_NonRecordFail;
  693. Procedure TestPointer_AnonymousArgTypeFail;
  694. Procedure TestPointer_AnonymousVarTypeFail;
  695. Procedure TestPointer_AnonymousResultTypeFail;
  696. Procedure TestPointer_AddrOperatorFail;
  697. Procedure TestPointer_ArrayParamsFail;
  698. Procedure TestPointer_PointerAddFail;
  699. Procedure TestPointer_IncPointerFail;
  700. Procedure TestPointer_Record;
  701. Procedure TestPointer_RecordArg;
  702. // jsvalue
  703. Procedure TestJSValue_AssignToJSValue;
  704. Procedure TestJSValue_TypeCastToBaseType;
  705. Procedure TestJSValue_TypecastToJSValue;
  706. Procedure TestJSValue_Equal;
  707. Procedure TestJSValue_If;
  708. Procedure TestJSValue_Not;
  709. Procedure TestJSValue_Enum;
  710. Procedure TestJSValue_ClassInstance;
  711. Procedure TestJSValue_ClassOf;
  712. Procedure TestJSValue_ArrayOfJSValue;
  713. Procedure TestJSValue_ArrayLit;
  714. Procedure TestJSValue_Params;
  715. Procedure TestJSValue_UntypedParam;
  716. Procedure TestJSValue_FuncResultType;
  717. Procedure TestJSValue_ProcType_Assign;
  718. Procedure TestJSValue_ProcType_Equal;
  719. Procedure TestJSValue_ProcType_Param;
  720. Procedure TestJSValue_AssignToPointerFail;
  721. Procedure TestJSValue_OverloadDouble;
  722. Procedure TestJSValue_OverloadNativeInt;
  723. Procedure TestJSValue_OverloadWord;
  724. Procedure TestJSValue_OverloadString;
  725. Procedure TestJSValue_OverloadChar;
  726. Procedure TestJSValue_OverloadPointer;
  727. Procedure TestJSValue_ForIn;
  728. // RTTI
  729. Procedure TestRTTI_IntRange;
  730. Procedure TestRTTI_Double;
  731. Procedure TestRTTI_ProcType;
  732. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  733. Procedure TestRTTI_EnumAndSetType;
  734. Procedure TestRTTI_EnumRange;
  735. Procedure TestRTTI_AnonymousEnumType;
  736. Procedure TestRTTI_StaticArray;
  737. Procedure TestRTTI_DynArray;
  738. Procedure TestRTTI_ArrayNestedAnonymous;
  739. Procedure TestRTTI_PublishedMethodOverloadFail;
  740. Procedure TestRTTI_PublishedMethodExternalFail;
  741. Procedure TestRTTI_PublishedClassPropertyFail;
  742. Procedure TestRTTI_PublishedClassFieldFail;
  743. Procedure TestRTTI_PublishedFieldExternalFail;
  744. Procedure TestRTTI_Class_Field;
  745. Procedure TestRTTI_Class_Method;
  746. Procedure TestRTTI_Class_MethodArgFlags;
  747. Procedure TestRTTI_Class_Property;
  748. Procedure TestRTTI_Class_PropertyParams;
  749. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  750. Procedure TestRTTI_Class_OmitRTTI;
  751. Procedure TestRTTI_IndexModifier;
  752. Procedure TestRTTI_StoredModifier;
  753. Procedure TestRTTI_DefaultValue;
  754. Procedure TestRTTI_DefaultValueSet;
  755. Procedure TestRTTI_DefaultValueRangeType;
  756. Procedure TestRTTI_DefaultValueInherit;
  757. Procedure TestRTTI_OverrideMethod;
  758. Procedure TestRTTI_ReintroduceMethod;
  759. Procedure TestRTTI_OverloadProperty;
  760. // ToDo: array argument
  761. Procedure TestRTTI_ClassForward;
  762. Procedure TestRTTI_ClassOf;
  763. Procedure TestRTTI_Record;
  764. Procedure TestRTTI_RecordAnonymousArray;
  765. Procedure TestRTTI_LocalTypes;
  766. Procedure TestRTTI_TypeInfo_BaseTypes;
  767. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  768. Procedure TestRTTI_TypeInfo_LocalFail;
  769. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  770. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  771. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  772. Procedure TestRTTI_TypeInfo_FunctionClassType;
  773. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  774. Procedure TestRTTI_Interface_Corba;
  775. Procedure TestRTTI_Interface_COM;
  776. Procedure TestRTTI_ClassHelper;
  777. Procedure TestRTTI_ExternalClass;
  778. // Resourcestring
  779. Procedure TestResourcestringProgram;
  780. Procedure TestResourcestringUnit;
  781. Procedure TestResourcestringImplementation;
  782. // Attributes
  783. Procedure TestAttributes_Members;
  784. Procedure TestAttributes_Types;
  785. Procedure TestAttributes_HelperConstructor_Fail;
  786. // Assertions, checks
  787. procedure TestAssert;
  788. procedure TestAssert_SysUtils;
  789. procedure TestObjectChecks;
  790. procedure TestOverflowChecks_Int;
  791. procedure TestRangeChecks_AssignInt;
  792. procedure TestRangeChecks_AssignIntRange;
  793. procedure TestRangeChecks_AssignEnum;
  794. procedure TestRangeChecks_AssignEnumRange;
  795. procedure TestRangeChecks_AssignChar;
  796. procedure TestRangeChecks_AssignCharRange;
  797. procedure TestRangeChecks_ArrayIndex;
  798. procedure TestRangeChecks_ArrayOfRecIndex;
  799. procedure TestRangeChecks_StringIndex;
  800. procedure TestRangeChecks_TypecastInt;
  801. procedure TestRangeChecks_TypeHelperInt;
  802. end;
  803. function LinesToStr(Args: array of const): string;
  804. function ExtractFileUnitName(aFilename: string): string;
  805. function JSToStr(El: TJSElement): string;
  806. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  807. implementation
  808. function LinesToStr(Args: array of const): string;
  809. var
  810. s: String;
  811. i: Integer;
  812. begin
  813. s:='';
  814. for i:=Low(Args) to High(Args) do
  815. case Args[i].VType of
  816. vtChar: s += Args[i].VChar+LineEnding;
  817. vtString: s += Args[i].VString^+LineEnding;
  818. vtPChar: s += Args[i].VPChar+LineEnding;
  819. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  820. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  821. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  822. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  823. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  824. end;
  825. Result:=s;
  826. end;
  827. function ExtractFileUnitName(aFilename: string): string;
  828. var
  829. p: Integer;
  830. begin
  831. Result:=ExtractFileName(aFilename);
  832. if Result='' then exit;
  833. for p:=length(Result) downto 1 do
  834. case Result[p] of
  835. '/','\': exit;
  836. '.':
  837. begin
  838. Delete(Result,p,length(Result));
  839. exit;
  840. end;
  841. end;
  842. end;
  843. function JSToStr(El: TJSElement): string;
  844. var
  845. aWriter: TBufferWriter;
  846. aJSWriter: TJSWriter;
  847. begin
  848. aJSWriter:=nil;
  849. aWriter:=TBufferWriter.Create(1000);
  850. try
  851. aJSWriter:=TJSWriter.Create(aWriter);
  852. aJSWriter.IndentSize:=2;
  853. aJSWriter.WriteJS(El);
  854. Result:=aWriter.AsString;
  855. finally
  856. aJSWriter.Free;
  857. aWriter.Free;
  858. end;
  859. end;
  860. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  861. // search diff, ignore changes in spaces
  862. const
  863. SpaceChars = [#9,#10,#13,' '];
  864. var
  865. ExpectedP, ActualP: PChar;
  866. function FindLineEnd(p: PChar): PChar;
  867. begin
  868. Result:=p;
  869. while not (Result^ in [#0,#10,#13]) do inc(Result);
  870. end;
  871. function FindLineStart(p, MinP: PChar): PChar;
  872. begin
  873. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  874. Result:=p;
  875. end;
  876. procedure SkipLineEnd(var p: PChar);
  877. begin
  878. if p^ in [#10,#13] then
  879. begin
  880. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  881. inc(p,2)
  882. else
  883. inc(p);
  884. end;
  885. end;
  886. procedure DiffFound;
  887. var
  888. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  889. ExpLine, ActLine: String;
  890. i, LineNo, DiffLineNo: Integer;
  891. begin
  892. writeln('Diff found "',Msg,'". Lines:');
  893. // write correct lines
  894. p:=PChar(Expected);
  895. LineNo:=0;
  896. DiffLineNo:=0;
  897. repeat
  898. StartPos:=p;
  899. while not (p^ in [#0,#10,#13]) do inc(p);
  900. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  901. SkipLineEnd(p);
  902. inc(LineNo);
  903. if (p<=ExpectedP) and (p^<>#0) then
  904. begin
  905. writeln('= ',ExpLine);
  906. end else begin
  907. // diff line
  908. if DiffLineNo=0 then DiffLineNo:=LineNo;
  909. // write actual line
  910. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  911. ActLineEndP:=FindLineEnd(ActualP);
  912. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  913. writeln('- ',ActLine);
  914. // write expected line
  915. writeln('+ ',ExpLine);
  916. // write empty line with pointer ^
  917. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  918. writeln('^');
  919. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  920. CheckSrcDiff:=false;
  921. // write up to three following actual lines to get some context
  922. for i:=1 to 3 do begin
  923. ActLineStartP:=ActLineEndP;
  924. SkipLineEnd(ActLineStartP);
  925. if ActLineStartP^=#0 then break;
  926. ActLineEndP:=FindLineEnd(ActLineStartP);
  927. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  928. writeln('~ ',ActLine);
  929. end;
  930. exit;
  931. end;
  932. until p^=#0;
  933. writeln('DiffFound Actual:-----------------------');
  934. writeln(Actual);
  935. writeln('DiffFound Expected:---------------------');
  936. writeln(Expected);
  937. writeln('DiffFound ------------------------------');
  938. Msg:='diff found, but lines are the same, internal error';
  939. CheckSrcDiff:=false;
  940. end;
  941. var
  942. IsSpaceNeeded: Boolean;
  943. LastChar, Quote: Char;
  944. begin
  945. Result:=true;
  946. Msg:='';
  947. if Expected='' then Expected:=' ';
  948. if Actual='' then Actual:=' ';
  949. ExpectedP:=PChar(Expected);
  950. ActualP:=PChar(Actual);
  951. repeat
  952. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  953. case ExpectedP^ of
  954. #0:
  955. begin
  956. // check that rest of Actual has only spaces
  957. while ActualP^ in SpaceChars do inc(ActualP);
  958. if ActualP^<>#0 then
  959. begin
  960. DiffFound;
  961. exit;
  962. end;
  963. exit(true);
  964. end;
  965. ' ',#9,#10,#13:
  966. begin
  967. // skip space in Expected
  968. IsSpaceNeeded:=false;
  969. if ExpectedP>PChar(Expected) then
  970. LastChar:=ExpectedP[-1]
  971. else
  972. LastChar:=#0;
  973. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  974. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  975. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  976. IsSpaceNeeded:=true;
  977. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  978. begin
  979. DiffFound;
  980. exit;
  981. end;
  982. while ActualP^ in SpaceChars do inc(ActualP);
  983. end;
  984. '''','"':
  985. begin
  986. while ActualP^ in SpaceChars do inc(ActualP);
  987. if ExpectedP^<>ActualP^ then
  988. begin
  989. DiffFound;
  990. exit;
  991. end;
  992. Quote:=ExpectedP^;
  993. repeat
  994. inc(ExpectedP);
  995. inc(ActualP);
  996. if ExpectedP^<>ActualP^ then
  997. begin
  998. DiffFound;
  999. exit;
  1000. end;
  1001. if (ExpectedP^ in [#0,#10,#13]) then
  1002. break
  1003. else if (ExpectedP^=Quote) then
  1004. begin
  1005. inc(ExpectedP);
  1006. inc(ActualP);
  1007. break;
  1008. end;
  1009. until false;
  1010. end;
  1011. else
  1012. while ActualP^ in SpaceChars do inc(ActualP);
  1013. if ExpectedP^<>ActualP^ then
  1014. begin
  1015. DiffFound;
  1016. exit;
  1017. end;
  1018. inc(ExpectedP);
  1019. inc(ActualP);
  1020. end;
  1021. until false;
  1022. end;
  1023. { TTestEnginePasResolver }
  1024. destructor TTestEnginePasResolver.Destroy;
  1025. begin
  1026. FreeAndNil(FStreamResolver);
  1027. FreeAndNil(FParser);
  1028. FreeAndNil(FScanner);
  1029. FreeAndNil(FStreamResolver);
  1030. if Module<>nil then
  1031. begin
  1032. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1033. FModule:=nil;
  1034. end;
  1035. inherited Destroy;
  1036. end;
  1037. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1038. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1039. begin
  1040. Result:=nil;
  1041. if InFilename<>'' then
  1042. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1043. if Assigned(OnFindUnit) then
  1044. Result:=OnFindUnit(AName);
  1045. if NameExpr=nil then ;
  1046. end;
  1047. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1048. begin
  1049. // do not parse recursively
  1050. // parse via the queue
  1051. if Section=nil then ;
  1052. end;
  1053. { TCustomTestModule }
  1054. function TCustomTestModule.GetMsgCount: integer;
  1055. begin
  1056. Result:=FHintMsgs.Count;
  1057. end;
  1058. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1059. begin
  1060. Result:=TTestHintMessage(FHintMsgs[Index]);
  1061. end;
  1062. function TCustomTestModule.GetResolverCount: integer;
  1063. begin
  1064. Result:=FModules.Count;
  1065. end;
  1066. function TCustomTestModule.GetResolvers(Index: integer
  1067. ): TTestEnginePasResolver;
  1068. begin
  1069. Result:=TTestEnginePasResolver(FModules[Index]);
  1070. end;
  1071. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1072. ): TPasModule;
  1073. var
  1074. DefNamespace: String;
  1075. begin
  1076. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1077. if (Pos('.',aUnitName)<1) then
  1078. begin
  1079. DefNamespace:=GetDefaultNamespace;
  1080. if DefNamespace<>'' then
  1081. begin
  1082. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1083. if Result<>nil then exit;
  1084. end;
  1085. end;
  1086. Result:=LoadUnit(aUnitName);
  1087. if Result<>nil then exit;
  1088. {$IFDEF VerbosePas2JS}
  1089. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1090. {$ENDIF}
  1091. Fail('can''t find unit "'+aUnitName+'"');
  1092. end;
  1093. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1094. var
  1095. aParser: TPasParser;
  1096. Item: TTestHintMessage;
  1097. begin
  1098. aParser:=Sender as TPasParser;
  1099. Item:=TTestHintMessage.Create;
  1100. Item.Id:=aParser.LastMsgNumber;
  1101. Item.MsgType:=aParser.LastMsgType;
  1102. Item.MsgNumber:=aParser.LastMsgNumber;
  1103. Item.Msg:=Msg;
  1104. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1105. {$IFDEF VerbosePas2JS}
  1106. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1107. {$ENDIF}
  1108. FHintMsgs.Add(Item);
  1109. end;
  1110. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1111. );
  1112. var
  1113. aResolver: TTestEnginePasResolver;
  1114. Item: TTestHintMessage;
  1115. begin
  1116. aResolver:=Sender as TTestEnginePasResolver;
  1117. Item:=TTestHintMessage.Create;
  1118. Item.Id:=aResolver.LastMsgId;
  1119. Item.MsgType:=aResolver.LastMsgType;
  1120. Item.MsgNumber:=aResolver.LastMsgNumber;
  1121. Item.Msg:=Msg;
  1122. Item.SourcePos:=aResolver.LastSourcePos;
  1123. {$IFDEF VerbosePas2JS}
  1124. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1125. {$ENDIF}
  1126. FHintMsgs.Add(Item);
  1127. end;
  1128. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1129. var
  1130. Item: TTestHintMessage;
  1131. aScanner: TPas2jsPasScanner;
  1132. begin
  1133. aScanner:=Sender as TPas2jsPasScanner;
  1134. Item:=TTestHintMessage.Create;
  1135. Item.Id:=aScanner.LastMsgNumber;
  1136. Item.MsgType:=aScanner.LastMsgType;
  1137. Item.MsgNumber:=aScanner.LastMsgNumber;
  1138. Item.Msg:=Msg;
  1139. Item.SourcePos:=aScanner.CurSourcePos;
  1140. {$IFDEF VerbosePas2JS}
  1141. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1142. {$ENDIF}
  1143. FHintMsgs.Add(Item);
  1144. end;
  1145. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1146. var
  1147. i: Integer;
  1148. CurEngine: TTestEnginePasResolver;
  1149. CurUnitName: String;
  1150. begin
  1151. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1152. Result:=nil;
  1153. if (Module.ClassType=TPasModule)
  1154. and (CompareText(Module.Name,aUnitName)=0) then
  1155. exit(Module);
  1156. for i:=0 to ResolverCount-1 do
  1157. begin
  1158. CurEngine:=Resolvers[i];
  1159. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1160. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1161. if CompareText(aUnitName,CurUnitName)=0 then
  1162. begin
  1163. Result:=CurEngine.Module;
  1164. if Result<>nil then exit;
  1165. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1166. FileResolver.FindSourceFile(aUnitName);
  1167. CurEngine.StreamResolver:=TStreamResolver.Create;
  1168. CurEngine.StreamResolver.OwnsStreams:=True;
  1169. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1170. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1171. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1172. InitScanner(CurEngine.Scanner);
  1173. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1174. CurEngine.Parser.Options:=po_tcmodules;
  1175. if CompareText(CurUnitName,'System')=0 then
  1176. CurEngine.Parser.ImplicitUses.Clear;
  1177. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1178. try
  1179. CurEngine.Parser.NextToken;
  1180. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1181. except
  1182. on E: Exception do
  1183. HandleException(E);
  1184. end;
  1185. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1186. Result:=CurEngine.Module;
  1187. exit;
  1188. end;
  1189. end;
  1190. end;
  1191. procedure TCustomTestModule.SetUp;
  1192. begin
  1193. {$IFDEF EnablePasTreeGlobalRefCount}
  1194. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1195. {$ENDIF}
  1196. if FModules<>nil then
  1197. begin
  1198. writeln('TCustomTestModule.SetUp FModules<>nil');
  1199. Halt;
  1200. end;
  1201. inherited SetUp;
  1202. FSkipTests:=false;
  1203. FSource:=TStringList.Create;
  1204. FModules:=TObjectList.Create(true);
  1205. FFilename:='test1.pp';
  1206. FFileResolver:=TStreamResolver.Create;
  1207. FFileResolver.OwnsStreams:=True;
  1208. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1209. InitScanner(FScanner);
  1210. FEngine:=AddModule(Filename);
  1211. FEngine.Scanner:=FScanner;
  1212. FScanner.Resolver:=FEngine;
  1213. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1214. FParser.OnLog:=@OnParserLog;
  1215. FEngine.Parser:=FParser;
  1216. Parser.Options:=po_tcmodules;
  1217. FModule:=Nil;
  1218. FConverter:=CreateConverter;
  1219. FExpectedErrorClass:=nil;
  1220. end;
  1221. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1222. begin
  1223. Result:=TPasToJSConverter.Create;
  1224. Result.Options:=co_tcmodules;
  1225. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1226. end;
  1227. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1228. begin
  1229. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1230. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1231. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1232. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1233. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1234. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1235. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1236. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1237. aScanner.OnLog:=@OnScannerLog;
  1238. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1239. end;
  1240. procedure TCustomTestModule.TearDown;
  1241. {$IFDEF CheckPasTreeRefCount}
  1242. var
  1243. El: TPasElement;
  1244. {$ENDIF}
  1245. var
  1246. i: Integer;
  1247. CurModule: TPasModule;
  1248. begin
  1249. FHintMsgs.Clear;
  1250. FHintMsgsGood.Clear;
  1251. FSkipTests:=false;
  1252. FJSRegModuleCall:=nil;
  1253. FJSModuleCallArgs:=nil;
  1254. FJSImplentationUses:=nil;
  1255. FJSInterfaceUses:=nil;
  1256. FJSModuleSrc:=nil;
  1257. FJSInitBody:=nil;
  1258. FreeAndNil(FJSSource);
  1259. FreeAndNil(FJSModule);
  1260. FreeAndNil(FConverter);
  1261. Engine.Clear;
  1262. FreeAndNil(FSource);
  1263. FreeAndNil(FFileResolver);
  1264. if FModules<>nil then
  1265. begin
  1266. for i:=0 to FModules.Count-1 do
  1267. begin
  1268. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1269. if CurModule=nil then continue;
  1270. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1271. CurModule.ReleaseUsedUnits;
  1272. end;
  1273. if FModule<>nil then
  1274. FModule.ReleaseUsedUnits;
  1275. for i:=0 to FModules.Count-1 do
  1276. begin
  1277. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1278. if CurModule=nil then continue;
  1279. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1280. end;
  1281. FreeAndNil(FModules);
  1282. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1283. FEngine:=nil;
  1284. end;
  1285. inherited TearDown;
  1286. {$IFDEF EnablePasTreeGlobalRefCount}
  1287. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1288. begin
  1289. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1290. {$IFDEF CheckPasTreeRefCount}
  1291. El:=TPasElement.FirstRefEl;
  1292. while El<>nil do
  1293. begin
  1294. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1295. for i:=0 to El.RefIds.Count-1 do
  1296. writeln(' ',El.RefIds[i]);
  1297. El:=El.NextRefEl;
  1298. end;
  1299. {$ENDIF}
  1300. Halt;
  1301. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1302. end;
  1303. {$ENDIF}
  1304. end;
  1305. procedure TCustomTestModule.Add(Line: string);
  1306. begin
  1307. Source.Add(Line);
  1308. end;
  1309. procedure TCustomTestModule.Add(const Lines: array of string);
  1310. var
  1311. i: Integer;
  1312. begin
  1313. for i:=low(Lines) to high(Lines) do
  1314. Add(Lines[i]);
  1315. end;
  1316. procedure TCustomTestModule.StartParsing;
  1317. var
  1318. Src: String;
  1319. begin
  1320. Src:=Source.Text;
  1321. FEngine.Source:=Src;
  1322. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1323. Scanner.OpenFile(FileName);
  1324. Writeln('// Test : ',Self.TestName);
  1325. Writeln(Src);
  1326. end;
  1327. procedure TCustomTestModule.ParseModuleQueue;
  1328. var
  1329. i: Integer;
  1330. CurResolver: TTestEnginePasResolver;
  1331. Found: Boolean;
  1332. Section: TPasSection;
  1333. begin
  1334. // parse til exception or all modules finished
  1335. while not SkipTests do
  1336. begin
  1337. Found:=false;
  1338. for i:=0 to ResolverCount-1 do
  1339. begin
  1340. CurResolver:=Resolvers[i];
  1341. if CurResolver.CurrentParser=nil then continue;
  1342. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1343. continue;
  1344. CurResolver.Parser.ParseContinue;
  1345. Found:=true;
  1346. break;
  1347. end;
  1348. if not Found then break;
  1349. end;
  1350. for i:=0 to ResolverCount-1 do
  1351. begin
  1352. CurResolver:=Resolvers[i];
  1353. if CurResolver.Parser=nil then
  1354. begin
  1355. if CurResolver.CurrentParser<>nil then
  1356. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1357. continue;
  1358. end;
  1359. if CurResolver.Parser.CurModule<>nil then
  1360. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1361. end;
  1362. end;
  1363. procedure TCustomTestModule.ParseModule;
  1364. begin
  1365. if SkipTests then exit;
  1366. FFirstPasStatement:=nil;
  1367. try
  1368. StartParsing;
  1369. Parser.ParseMain(FModule);
  1370. ParseModuleQueue;
  1371. except
  1372. on E: Exception do
  1373. HandleException(E);
  1374. end;
  1375. if SkipTests then exit;
  1376. AssertNotNull('Module resulted in Module',Module);
  1377. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1378. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1379. end;
  1380. procedure TCustomTestModule.ParseProgram;
  1381. begin
  1382. if SkipTests then exit;
  1383. ParseModule;
  1384. if SkipTests then exit;
  1385. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1386. FPasProgram:=TPasProgram(Module);
  1387. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1388. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1389. if (PasProgram.InitializationSection.Elements.Count>0) then
  1390. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1391. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1392. end;
  1393. procedure TCustomTestModule.ParseUnit;
  1394. begin
  1395. if SkipTests then exit;
  1396. ParseModule;
  1397. if SkipTests then exit;
  1398. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1399. AssertNotNull('Has interface section',Module.InterfaceSection);
  1400. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1401. if (Module.InitializationSection<>nil)
  1402. and (Module.InitializationSection.Elements.Count>0)
  1403. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1404. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1405. end;
  1406. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1407. ): TTestEnginePasResolver;
  1408. var
  1409. i: Integer;
  1410. begin
  1411. for i:=0 to ResolverCount-1 do
  1412. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1413. exit(Resolvers[i]);
  1414. Result:=nil;
  1415. end;
  1416. function TCustomTestModule.AddModule(aFilename: string
  1417. ): TTestEnginePasResolver;
  1418. begin
  1419. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1420. if FindModuleWithFilename(aFilename)<>nil then
  1421. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1422. Result:=TTestEnginePasResolver.Create;
  1423. Result.Filename:=aFilename;
  1424. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1425. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1426. Result.OnLog:=@OnPasResolverLog;
  1427. FModules.Add(Result);
  1428. end;
  1429. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1430. ): TTestEnginePasResolver;
  1431. begin
  1432. Result:=AddModule(aFilename);
  1433. Result.Source:=Src;
  1434. end;
  1435. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1436. ImplementationSrc: string): TTestEnginePasResolver;
  1437. var
  1438. Src: String;
  1439. begin
  1440. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1441. Src+=LineEnding;
  1442. Src+='interface'+LineEnding;
  1443. Src+=LineEnding;
  1444. Src+=InterfaceSrc;
  1445. Src+='implementation'+LineEnding;
  1446. Src+=LineEnding;
  1447. Src+=ImplementationSrc;
  1448. Src+='end.'+LineEnding;
  1449. Result:=AddModuleWithSrc(aFilename,Src);
  1450. end;
  1451. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1452. var
  1453. Intf, Impl: TStringList;
  1454. begin
  1455. Intf:=TStringList.Create;
  1456. // interface
  1457. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1458. Intf.Add('{$modeswitch externalclass}');
  1459. Intf.Add('type');
  1460. Intf.Add(' integer=longint;');
  1461. Intf.Add(' sizeint=nativeint;');
  1462. //'const',
  1463. //' LineEnding = #10;',
  1464. //' DirectorySeparator = ''/'';',
  1465. //' DriveSeparator = '''';',
  1466. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1467. //' AllowDriveSeparators : set of char = [];',
  1468. if supTObject in Parts then
  1469. Intf.AddStrings([
  1470. 'type',
  1471. ' TClass = class of TObject;',
  1472. ' TObject = class',
  1473. ' constructor Create;',
  1474. ' destructor Destroy; virtual;',
  1475. ' class function ClassType: TClass; assembler;',
  1476. ' class function ClassName: String; assembler;',
  1477. ' class function ClassNameIs(const Name: string): boolean;',
  1478. ' class function ClassParent: TClass; assembler;',
  1479. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1480. ' class function UnitName: String; assembler;',
  1481. ' procedure AfterConstruction; virtual;',
  1482. ' procedure BeforeDestruction;virtual;',
  1483. ' function Equals(Obj: TObject): boolean; virtual;',
  1484. ' function ToString: String; virtual;',
  1485. ' end;']);
  1486. if supTVarRec in Parts then
  1487. Intf.AddStrings([
  1488. 'const',
  1489. ' vtInteger = 0;',
  1490. ' vtBoolean = 1;',
  1491. ' vtJSValue = 19;',
  1492. 'type',
  1493. ' PVarRec = ^TVarRec;',
  1494. ' TVarRec = record',
  1495. ' VType : byte;',
  1496. ' VJSValue: JSValue;',
  1497. ' vInteger: longint external name ''VJSValue'';',
  1498. ' vBoolean: boolean external name ''VJSValue'';',
  1499. ' end;',
  1500. ' TVarRecArray = array of TVarRec;',
  1501. 'function VarRecs: TVarRecArray; varargs;',
  1502. '']);
  1503. if supTypeInfo in Parts then
  1504. begin
  1505. Intf.AddStrings([
  1506. 'type',
  1507. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1508. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1509. ' end;',
  1510. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1511. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1512. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1513. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1514. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1515. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1516. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1517. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1518. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1519. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1520. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1521. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1522. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1523. '']);
  1524. end;
  1525. Intf.Add('var');
  1526. Intf.Add(' ExitCode: Longint = 0;');
  1527. // implementation
  1528. Impl:=TStringList.Create;
  1529. if supTObject in Parts then
  1530. Impl.AddStrings([
  1531. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1532. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1533. 'asm',
  1534. 'end;',
  1535. 'constructor TObject.Create; begin end;',
  1536. 'destructor TObject.Destroy; begin end;',
  1537. 'class function TObject.ClassType: TClass; assembler;',
  1538. 'asm',
  1539. 'end;',
  1540. 'class function TObject.ClassName: String; assembler;',
  1541. 'asm',
  1542. 'end;',
  1543. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1544. 'begin',
  1545. ' Result:=SameText(Name,ClassName);',
  1546. 'end;',
  1547. 'class function TObject.ClassParent: TClass; assembler;',
  1548. 'asm',
  1549. 'end;',
  1550. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1551. 'asm',
  1552. 'end;',
  1553. 'class function TObject.UnitName: String; assembler;',
  1554. 'asm',
  1555. 'end;',
  1556. 'procedure TObject.AfterConstruction; begin end;',
  1557. 'procedure TObject.BeforeDestruction; begin end;',
  1558. 'function TObject.Equals(Obj: TObject): boolean;',
  1559. 'begin',
  1560. ' Result:=Obj=Self;',
  1561. 'end;',
  1562. 'function TObject.ToString: String;',
  1563. 'begin',
  1564. ' Result:=ClassName;',
  1565. 'end;'
  1566. ]);
  1567. if supTVarRec in Parts then
  1568. Impl.AddStrings([
  1569. 'function VarRecs: TVarRecArray; varargs;',
  1570. 'var',
  1571. ' v: PVarRec;',
  1572. 'begin',
  1573. ' v^.VType:=1;',
  1574. ' v^.VJSValue:=2;',
  1575. 'end;',
  1576. '']);
  1577. try
  1578. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1579. finally
  1580. Intf.Free;
  1581. Impl.Free;
  1582. end;
  1583. end;
  1584. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1585. SystemUnitParts: TSystemUnitParts);
  1586. begin
  1587. if NeedSystemUnit then
  1588. AddSystemUnit(SystemUnitParts)
  1589. else
  1590. Parser.ImplicitUses.Clear;
  1591. Add('program '+ExtractFileUnitName(Filename)+';');
  1592. Add('');
  1593. end;
  1594. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1595. SystemUnitParts: TSystemUnitParts);
  1596. begin
  1597. if NeedSystemUnit then
  1598. AddSystemUnit(SystemUnitParts)
  1599. else
  1600. Parser.ImplicitUses.Clear;
  1601. Add('unit Test1;');
  1602. Add('');
  1603. end;
  1604. procedure TCustomTestModule.ConvertModule;
  1605. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1606. out UsesLit: TJSArrayLiteral);
  1607. var
  1608. i: Integer;
  1609. Item: TJSElement;
  1610. Lit: TJSLiteral;
  1611. begin
  1612. UsesLit:=nil;
  1613. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1614. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1615. exit; // null is ok
  1616. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1617. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1618. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1619. begin
  1620. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1621. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1622. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1623. Lit:=TJSLiteral(Item);
  1624. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1625. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1626. end;
  1627. end;
  1628. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1629. out Src: TJSSourceElements);
  1630. var
  1631. FunDecl: TJSFunctionDeclarationStatement;
  1632. FunDef: TJSFuncDef;
  1633. FunBody: TJSFunctionBody;
  1634. begin
  1635. Src:=nil;
  1636. AssertNotNull(ParamName,Arg.Expr);
  1637. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1638. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1639. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1640. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1641. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1642. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1643. AssertNotNull(ParamName+' body',FunDef.Body);
  1644. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1645. FunBody:=FunDef.Body as TJSFunctionBody;
  1646. AssertNotNull(ParamName+' body.A',FunBody.A);
  1647. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1648. Src:=FunBody.A as TJSSourceElements;
  1649. end;
  1650. var
  1651. ModuleNameExpr: TJSLiteral;
  1652. InitFunction: TJSFunctionDeclarationStatement;
  1653. InitAssign: TJSSimpleAssignStatement;
  1654. InitName: String;
  1655. LastNode: TJSElement;
  1656. Arg: TJSArrayLiteralElement;
  1657. begin
  1658. if SkipTests then exit;
  1659. try
  1660. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1661. except
  1662. on E: Exception do
  1663. HandleException(E);
  1664. end;
  1665. if SkipTests then exit;
  1666. if ExpectedErrorClass<>nil then
  1667. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1668. FJSSource:=TStringList.Create;
  1669. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1670. {$IFDEF VerbosePas2JS}
  1671. writeln('TTestModule.ConvertModule JS:');
  1672. write(FJSSource.Text);
  1673. {$ENDIF}
  1674. // rtl.module(...
  1675. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1676. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1677. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1678. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1679. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1680. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1681. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1682. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1683. // parameter 'unitname'
  1684. if JSModuleCallArgs.Elements.Count<1 then
  1685. Fail('rtl.module first param unit missing');
  1686. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1687. AssertNotNull('module name param',Arg.Expr);
  1688. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1689. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1690. if Module is TPasProgram then
  1691. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1692. else
  1693. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1694. // main uses section
  1695. if JSModuleCallArgs.Elements.Count<2 then
  1696. Fail('rtl.module second param main uses missing');
  1697. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1698. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1699. // program/library/interface function()
  1700. if JSModuleCallArgs.Elements.Count<3 then
  1701. Fail('rtl.module third param intf-function missing');
  1702. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1703. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1704. // search for $mod.$init or $mod.$main - the last statement
  1705. if Module is TPasProgram then
  1706. begin
  1707. InitName:='$main';
  1708. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1709. end
  1710. else
  1711. InitName:='$init';
  1712. FJSInitBody:=nil;
  1713. if JSModuleSrc.Statements.Count>0 then
  1714. begin
  1715. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1716. if LastNode is TJSSimpleAssignStatement then
  1717. begin
  1718. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1719. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1720. begin
  1721. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1722. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1723. end
  1724. else if Module is TPasProgram then
  1725. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1726. end;
  1727. end;
  1728. // optional: implementation uses section
  1729. if JSModuleCallArgs.Elements.Count<4 then
  1730. exit;
  1731. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1732. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1733. // optional: implementation function()
  1734. if JSModuleCallArgs.Elements.Count<5 then
  1735. exit;
  1736. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1737. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1738. end;
  1739. procedure TCustomTestModule.ConvertProgram;
  1740. begin
  1741. Add('end.');
  1742. ParseProgram;
  1743. ConvertModule;
  1744. end;
  1745. procedure TCustomTestModule.ConvertUnit;
  1746. begin
  1747. Add('end.');
  1748. ParseUnit;
  1749. ConvertModule;
  1750. end;
  1751. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1752. begin
  1753. Result:=tcmodules.JSToStr(El);
  1754. end;
  1755. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1756. DottedName: string);
  1757. begin
  1758. if DottedName='' then
  1759. begin
  1760. AssertNull(Msg,El);
  1761. end
  1762. else
  1763. begin
  1764. AssertNotNull(Msg,El);
  1765. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1766. end;
  1767. end;
  1768. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1769. begin
  1770. if El=nil then
  1771. Result:=''
  1772. else if El is TJSPrimaryExpressionIdent then
  1773. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1774. else if El is TJSDotMemberExpression then
  1775. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1776. else
  1777. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1778. end;
  1779. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1780. InitStatements: string; ImplStatements: string);
  1781. var
  1782. ActualSrc, ExpectedSrc, InitName: String;
  1783. begin
  1784. ActualSrc:=JSToStr(JSModuleSrc);
  1785. ExpectedSrc:=
  1786. 'var $mod = this;'+LineEnding
  1787. +Statements;
  1788. if coUseStrict in Converter.Options then
  1789. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1790. if Module is TPasProgram then
  1791. InitName:='$main'
  1792. else
  1793. InitName:='$init';
  1794. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1795. ExpectedSrc:=ExpectedSrc+LineEnding
  1796. +'$mod.'+InitName+' = function () {'+LineEnding
  1797. +InitStatements
  1798. +'};'+LineEnding;
  1799. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1800. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1801. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1802. if (JSImplementationSrc<>nil) then
  1803. begin
  1804. ActualSrc:=JSToStr(JSImplementationSrc);
  1805. ExpectedSrc:=
  1806. 'var $mod = this;'+LineEnding
  1807. +'var $impl = $mod.$impl;'+LineEnding
  1808. +ImplStatements;
  1809. end
  1810. else
  1811. begin
  1812. ActualSrc:='';
  1813. ExpectedSrc:=ImplStatements;
  1814. end;
  1815. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1816. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1817. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1818. end;
  1819. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1820. // search diff, ignore changes in spaces
  1821. var
  1822. s: string;
  1823. begin
  1824. if CheckSrcDiff(Expected,Actual,s) then exit;
  1825. Fail(Msg+': '+s);
  1826. end;
  1827. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1828. var
  1829. aResolver: TTestEnginePasResolver;
  1830. aConverter: TPasToJSConverter;
  1831. aJSModule: TJSSourceElements;
  1832. ActualSrc: String;
  1833. begin
  1834. aResolver:=GetResolver(Filename);
  1835. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1836. {$IFDEF VerbosePas2JS}
  1837. writeln('CheckUnit '+Filename+' converting ...');
  1838. {$ENDIF}
  1839. aConverter:=CreateConverter;
  1840. aJSModule:=nil;
  1841. try
  1842. try
  1843. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1844. except
  1845. on E: Exception do
  1846. HandleException(E);
  1847. end;
  1848. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1849. {$IFDEF VerbosePas2JS}
  1850. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1851. write(aResolver.Source);
  1852. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1853. write(ActualSrc);
  1854. {$ENDIF}
  1855. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1856. finally
  1857. aJSModule.Free;
  1858. aConverter.Free;
  1859. end;
  1860. end;
  1861. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1862. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1863. var
  1864. i: Integer;
  1865. Item: TTestHintMessage;
  1866. Expected,Actual: string;
  1867. begin
  1868. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1869. for i:=0 to MsgCount-1 do
  1870. begin
  1871. Item:=Msgs[i];
  1872. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1873. if (Marker<>nil) then
  1874. begin
  1875. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1876. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1877. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1878. end;
  1879. // found
  1880. FHintMsgsGood.Add(Item);
  1881. str(Item.MsgType,Actual);
  1882. str(MsgType,Expected);
  1883. AssertEquals('MsgType',Expected,Actual);
  1884. exit;
  1885. end;
  1886. // needed message missing -> show emitted messages
  1887. WriteSources('',0,0);
  1888. for i:=0 to MsgCount-1 do
  1889. begin
  1890. Item:=Msgs[i];
  1891. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1892. ' ('+IntToStr(Item.MsgNumber),')');
  1893. if Marker<>nil then
  1894. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1895. writeln(' {',Item.Msg,'}');
  1896. end;
  1897. str(MsgType,Expected);
  1898. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1899. if Marker<>nil then
  1900. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1901. Actual:=Actual+' '+Msg;
  1902. Fail(Actual);
  1903. end;
  1904. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1905. );
  1906. var
  1907. i: Integer;
  1908. s, Txt: String;
  1909. Msg: TTestHintMessage;
  1910. begin
  1911. for i:=0 to MsgCount-1 do
  1912. begin
  1913. Msg:=Msgs[i];
  1914. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1915. s:='';
  1916. str(Msg.MsgType,s);
  1917. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1918. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1919. if WithSourcePos then
  1920. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1921. Txt:=Txt+' {'+Msg.Msg+'}';
  1922. Fail(Txt);
  1923. end;
  1924. end;
  1925. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1926. MsgNumber: integer);
  1927. begin
  1928. ExpectedErrorClass:=EScannerError;
  1929. ExpectedErrorMsg:=Msg;
  1930. ExpectedErrorNumber:=MsgNumber;
  1931. end;
  1932. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1933. MsgNumber: integer);
  1934. begin
  1935. ExpectedErrorClass:=EParserError;
  1936. ExpectedErrorMsg:=Msg;
  1937. ExpectedErrorNumber:=MsgNumber;
  1938. end;
  1939. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1940. MsgNumber: integer);
  1941. begin
  1942. ExpectedErrorClass:=EPasResolve;
  1943. ExpectedErrorMsg:=Msg;
  1944. ExpectedErrorNumber:=MsgNumber;
  1945. end;
  1946. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1947. MsgNumber: integer);
  1948. begin
  1949. ExpectedErrorClass:=EPas2JS;
  1950. ExpectedErrorMsg:=Msg;
  1951. ExpectedErrorNumber:=MsgNumber;
  1952. end;
  1953. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1954. var
  1955. MsgNumber: Integer;
  1956. Msg: String;
  1957. begin
  1958. Result:=false;
  1959. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1960. Msg:=E.Message;
  1961. if E is EPas2JS then
  1962. MsgNumber:=EPas2JS(E).MsgNumber
  1963. else if E is EPasResolve then
  1964. MsgNumber:=EPasResolve(E).MsgNumber
  1965. else if E is EParserError then
  1966. MsgNumber:=Parser.LastMsgNumber
  1967. else if E is EScannerError then
  1968. begin
  1969. MsgNumber:=Scanner.LastMsgNumber;
  1970. Msg:=Scanner.LastMsg;
  1971. end
  1972. else
  1973. MsgNumber:=0;
  1974. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1975. if Result then
  1976. SkipTests:=true;
  1977. end;
  1978. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1979. begin
  1980. if IsErrorExpected(E) then exit;
  1981. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1982. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1983. +' '+Scanner.CurFilename
  1984. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1985. FailException(E);
  1986. end;
  1987. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1988. begin
  1989. if IsErrorExpected(E) then exit;
  1990. WriteSources(E.Filename,E.Row,E.Column);
  1991. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1992. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1993. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1994. );
  1995. FailException(E);
  1996. end;
  1997. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1998. var
  1999. P: TPasSourcePos;
  2000. begin
  2001. if IsErrorExpected(E) then exit;
  2002. P:=E.SourcePos;
  2003. WriteSources(P.FileName,P.Row,P.Column);
  2004. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2005. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2006. FailException(E);
  2007. end;
  2008. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2009. var
  2010. Row, Col: integer;
  2011. begin
  2012. if IsErrorExpected(E) then exit;
  2013. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2014. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2015. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2016. +' '+E.PasElement.SourceFilename
  2017. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2018. FailException(E);
  2019. end;
  2020. procedure TCustomTestModule.HandleException(E: Exception);
  2021. begin
  2022. if E is EScannerError then
  2023. HandleScannerError(EScannerError(E))
  2024. else if E is EParserError then
  2025. HandleParserError(EParserError(E))
  2026. else if E is EPasResolve then
  2027. HandlePasResolveError(EPasResolve(E))
  2028. else if E is EPas2JS then
  2029. HandlePas2JSError(EPas2JS(E))
  2030. else
  2031. begin
  2032. if IsErrorExpected(E) then exit;
  2033. if not (E is EAssertionFailedError) then
  2034. begin
  2035. WriteSources('',0,0);
  2036. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2037. end;
  2038. FailException(E);
  2039. end;
  2040. end;
  2041. procedure TCustomTestModule.FailException(E: Exception);
  2042. var
  2043. MsgNumber: Integer;
  2044. begin
  2045. if ExpectedErrorClass<>nil then
  2046. begin
  2047. if FExpectedErrorClass=E.ClassType then
  2048. begin
  2049. if E is EPas2JS then
  2050. MsgNumber:=EPas2JS(E).MsgNumber
  2051. else if E is EPasResolve then
  2052. MsgNumber:=EPasResolve(E).MsgNumber
  2053. else if E is EParserError then
  2054. MsgNumber:=Parser.LastMsgNumber
  2055. else if E is EScannerError then
  2056. MsgNumber:=Scanner.LastMsgNumber
  2057. else
  2058. MsgNumber:=0;
  2059. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2060. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2061. ExpectedErrorNumber,MsgNumber);
  2062. end else begin
  2063. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2064. end;
  2065. end;
  2066. Fail(E.Message);
  2067. end;
  2068. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2069. aCol: integer);
  2070. var
  2071. IsSrc: Boolean;
  2072. i, j: Integer;
  2073. SrcLines: TStringList;
  2074. Line: string;
  2075. aModule: TTestEnginePasResolver;
  2076. begin
  2077. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2078. for i:=0 to ResolverCount-1 do
  2079. begin
  2080. aModule:=Resolvers[i];
  2081. SrcLines:=TStringList.Create;
  2082. try
  2083. SrcLines.Text:=aModule.Source;
  2084. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2085. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2086. for j:=1 to SrcLines.Count do
  2087. begin
  2088. Line:=SrcLines[j-1];
  2089. if IsSrc and (j=aRow) then
  2090. begin
  2091. write('*');
  2092. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2093. end;
  2094. writeln(Format('%:4d: ',[j]),Line);
  2095. end;
  2096. finally
  2097. SrcLines.Free;
  2098. end;
  2099. end;
  2100. end;
  2101. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2102. var
  2103. i: Integer;
  2104. begin
  2105. for i:=0 to ResolverCount-1 do
  2106. if Filename=Resolvers[i].Filename then exit(i);
  2107. Result:=-1;
  2108. end;
  2109. function TCustomTestModule.GetResolver(const Filename: string
  2110. ): TTestEnginePasResolver;
  2111. var
  2112. i: Integer;
  2113. begin
  2114. i:=IndexOfResolver(Filename);
  2115. if i<0 then exit(nil);
  2116. Result:=Resolvers[i];
  2117. end;
  2118. function TCustomTestModule.GetDefaultNamespace: string;
  2119. var
  2120. C: TClass;
  2121. begin
  2122. Result:='';
  2123. if FModule=nil then exit;
  2124. C:=FModule.ClassType;
  2125. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2126. Result:=Engine.DefaultNameSpace;
  2127. end;
  2128. constructor TCustomTestModule.Create;
  2129. begin
  2130. inherited Create;
  2131. FHintMsgs:=TObjectList.Create(true);
  2132. FHintMsgsGood:=TFPList.Create;
  2133. end;
  2134. destructor TCustomTestModule.Destroy;
  2135. begin
  2136. FreeAndNil(FHintMsgs);
  2137. FreeAndNil(FHintMsgsGood);
  2138. inherited Destroy;
  2139. end;
  2140. { TTestModule }
  2141. procedure TTestModule.TestReservedWords;
  2142. var
  2143. i: integer;
  2144. begin
  2145. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2146. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2147. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2148. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2149. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2150. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2151. end;
  2152. procedure TTestModule.TestEmptyProgram;
  2153. begin
  2154. StartProgram(false);
  2155. Add('begin');
  2156. ConvertProgram;
  2157. CheckSource('TestEmptyProgram','','');
  2158. end;
  2159. procedure TTestModule.TestEmptyProgramUseStrict;
  2160. begin
  2161. Converter.Options:=Converter.Options+[coUseStrict];
  2162. StartProgram(false);
  2163. Add('begin');
  2164. ConvertProgram;
  2165. CheckSource('TestEmptyProgramUseStrict','','');
  2166. end;
  2167. procedure TTestModule.TestEmptyUnit;
  2168. begin
  2169. StartUnit(false);
  2170. Add('interface');
  2171. Add('implementation');
  2172. ConvertUnit;
  2173. CheckSource('TestEmptyUnit',
  2174. LinesToStr([
  2175. ]),
  2176. '');
  2177. end;
  2178. procedure TTestModule.TestEmptyUnitUseStrict;
  2179. begin
  2180. Converter.Options:=Converter.Options+[coUseStrict];
  2181. StartUnit(false);
  2182. Add('interface');
  2183. Add('implementation');
  2184. ConvertUnit;
  2185. CheckSource('TestEmptyUnitUseStrict',
  2186. LinesToStr([
  2187. ''
  2188. ]),
  2189. '');
  2190. end;
  2191. procedure TTestModule.TestDottedUnitNames;
  2192. begin
  2193. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2194. LinesToStr([
  2195. 'var iV: longint;'
  2196. ]),
  2197. '');
  2198. FFilename:='ns1.test1.pp';
  2199. StartProgram(true);
  2200. Add('uses unIt2;');
  2201. Add('var');
  2202. Add(' i: longint;');
  2203. Add('begin');
  2204. Add(' i:=iv;');
  2205. Add(' i:=uNit2.iv;');
  2206. Add(' i:=Ns1.TEst1.i;');
  2207. ConvertProgram;
  2208. CheckSource('TestDottedUnitNames',
  2209. LinesToStr([
  2210. 'this.i = 0;',
  2211. '']),
  2212. LinesToStr([ // this.$init
  2213. '$mod.i = pas["NS1.Unit2"].iV;',
  2214. '$mod.i = pas["NS1.Unit2"].iV;',
  2215. '$mod.i = $mod.i;',
  2216. '']) );
  2217. end;
  2218. procedure TTestModule.TestDottedUnitNameImpl;
  2219. begin
  2220. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2221. LinesToStr([
  2222. 'type',
  2223. ' TObject = class end;',
  2224. ' TTestA = class',
  2225. ' end;'
  2226. ]),
  2227. LinesToStr(['uses TEST.UnitB;'])
  2228. );
  2229. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2230. LinesToStr([
  2231. 'uses TEST.UnitA;',
  2232. 'type TTestB = class(TTestA);'
  2233. ]),
  2234. ''
  2235. );
  2236. StartProgram(true);
  2237. Add('uses TEST.UnitA;');
  2238. Add('begin');
  2239. ConvertProgram;
  2240. CheckSource('TestDottedUnitNameImpl',
  2241. LinesToStr([
  2242. '']),
  2243. LinesToStr([ // this.$init
  2244. '']) );
  2245. CheckUnit('TEST.UnitA.pas',
  2246. LinesToStr([
  2247. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2248. ' var $mod = this;',
  2249. ' rtl.createClass($mod, "TObject", null, function () {',
  2250. ' this.$init = function () {',
  2251. ' };',
  2252. ' this.$final = function () {',
  2253. ' };',
  2254. ' });',
  2255. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2256. ' });',
  2257. '}, ["TEST.UnitB"]);'
  2258. ]));
  2259. CheckUnit('TEST.UnitB.pas',
  2260. LinesToStr([
  2261. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2262. ' var $mod = this;',
  2263. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2264. ' });',
  2265. '});'
  2266. ]));
  2267. end;
  2268. procedure TTestModule.TestDottedUnitExpr;
  2269. begin
  2270. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2271. LinesToStr([
  2272. 'procedure DoIt;'
  2273. ]),
  2274. 'procedure DoIt; begin end;');
  2275. FFilename:='Ns1.SubNs1.Test1.pp';
  2276. StartProgram(true);
  2277. Add('uses Ns2.sUbnS2.unIt2;');
  2278. Add('var');
  2279. Add(' i: longint;');
  2280. Add('begin');
  2281. Add(' ns2.subns2.unit2.doit;');
  2282. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2283. ConvertProgram;
  2284. CheckSource('TestDottedUnitExpr',
  2285. LinesToStr([
  2286. 'this.i = 0;',
  2287. '']),
  2288. LinesToStr([ // this.$init
  2289. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2290. '$mod.i = $mod.i;',
  2291. '']) );
  2292. end;
  2293. procedure TTestModule.Test_ModeFPCFail;
  2294. begin
  2295. StartProgram(false);
  2296. Add('{$mode FPC}');
  2297. Add('begin');
  2298. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2299. ConvertProgram;
  2300. end;
  2301. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2302. begin
  2303. StartProgram(false);
  2304. Add('{$modeswitch cblocks-}');
  2305. Add('begin');
  2306. ConvertProgram;
  2307. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2308. CheckResolverUnexpectedHints();
  2309. end;
  2310. procedure TTestModule.TestUnit_UseSystem;
  2311. begin
  2312. StartUnit(true);
  2313. Add([
  2314. 'interface',
  2315. 'var i: integer;',
  2316. 'implementation']);
  2317. ConvertUnit;
  2318. CheckSource('TestUnit_UseSystem',
  2319. LinesToStr([
  2320. 'this.i = 0;',
  2321. '']),
  2322. LinesToStr([
  2323. '']) );
  2324. end;
  2325. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2326. begin
  2327. AddModuleWithIntfImplSrc('unit1.pp',
  2328. LinesToStr([
  2329. 'type number = longint;']),
  2330. LinesToStr([
  2331. 'uses test1;',
  2332. 'procedure DoIt;',
  2333. 'begin',
  2334. ' i:=3;',
  2335. 'end;']));
  2336. StartUnit(true);
  2337. Add([
  2338. 'interface',
  2339. 'uses unit1;',
  2340. 'var i: number;',
  2341. 'implementation']);
  2342. ConvertUnit;
  2343. CheckSource('TestUnit_Intf1Impl2Intf1',
  2344. LinesToStr([
  2345. 'this.i = 0;',
  2346. '']),
  2347. LinesToStr([
  2348. '']) );
  2349. end;
  2350. procedure TTestModule.TestIncludeVersion;
  2351. begin
  2352. StartProgram(false);
  2353. Add([
  2354. 'var',
  2355. ' s: string;',
  2356. ' i: word;',
  2357. 'begin',
  2358. ' s:={$I %line%};',
  2359. ' i:={$I %linenum%};',
  2360. ' s:={$I %currentroutine%};',
  2361. ' s:={$I %pas2jsversion%};',
  2362. ' s:={$I %pas2jstarget%};',
  2363. ' s:={$I %pas2jstargetos%};',
  2364. ' s:={$I %pas2jstargetcpu%};',
  2365. ' s:={$I %file%};',
  2366. '']);
  2367. ConvertProgram;
  2368. CheckSource('TestIncludeVersion',
  2369. LinesToStr([
  2370. 'this.s="";',
  2371. 'this.i = 0;']),
  2372. LinesToStr([
  2373. '$mod.s = "7";',
  2374. '$mod.i = 8;',
  2375. '$mod.s = "<anonymous>";',
  2376. '$mod.s = "Comp.Ver.tcmodules";',
  2377. '$mod.s = "Browser";',
  2378. '$mod.s = "Browser";',
  2379. '$mod.s = "ECMAScript5";',
  2380. '$mod.s = "test1.pp";',
  2381. '']));
  2382. end;
  2383. procedure TTestModule.TestVarInt;
  2384. begin
  2385. StartProgram(false);
  2386. Add('var MyI: longint;');
  2387. Add('begin');
  2388. ConvertProgram;
  2389. CheckSource('TestVarInt','this.MyI=0;','');
  2390. end;
  2391. procedure TTestModule.TestVarBaseTypes;
  2392. begin
  2393. StartProgram(false);
  2394. Add('var');
  2395. Add(' i: longint;');
  2396. Add(' s: string;');
  2397. Add(' c: char;');
  2398. Add(' b: boolean;');
  2399. Add(' d: double;');
  2400. Add(' i2: longint = 3;');
  2401. Add(' s2: string = ''foo'';');
  2402. Add(' c2: char = ''4'';');
  2403. Add(' b2: boolean = true;');
  2404. Add(' d2: double = 5.6;');
  2405. Add(' i3: longint = $707;');
  2406. Add(' i4: nativeint = 9007199254740991;');
  2407. Add(' i5: nativeint = -9007199254740991-1;');
  2408. Add(' i6: nativeint = $fffffffffffff;');
  2409. Add(' i7: nativeint = -$fffffffffffff-1;');
  2410. Add(' i8: byte = 00;');
  2411. Add(' u8: nativeuint = $fffffffffffff;');
  2412. Add(' u9: nativeuint = $0000000000000;');
  2413. Add(' u10: nativeuint = $00ff00;');
  2414. Add('begin');
  2415. ConvertProgram;
  2416. CheckSource('TestVarBaseTypes',
  2417. LinesToStr([
  2418. 'this.i = 0;',
  2419. 'this.s = "";',
  2420. 'this.c = "";',
  2421. 'this.b = false;',
  2422. 'this.d = 0.0;',
  2423. 'this.i2 = 3;',
  2424. 'this.s2 = "foo";',
  2425. 'this.c2 = "4";',
  2426. 'this.b2 = true;',
  2427. 'this.d2 = 5.6;',
  2428. 'this.i3 = 0x707;',
  2429. 'this.i4 = 9007199254740991;',
  2430. 'this.i5 = -9007199254740991-1;',
  2431. 'this.i6 = 0xfffffffffffff;',
  2432. 'this.i7 =-0xfffffffffffff-1;',
  2433. 'this.i8 = 0;',
  2434. 'this.u8 = 0xfffffffffffff;',
  2435. 'this.u9 = 0x0;',
  2436. 'this.u10 = 0xff00;'
  2437. ]),
  2438. '');
  2439. end;
  2440. procedure TTestModule.TestBaseTypeSingleFail;
  2441. begin
  2442. StartProgram(false);
  2443. Add('var s: single;');
  2444. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2445. ConvertProgram;
  2446. end;
  2447. procedure TTestModule.TestBaseTypeExtendedFail;
  2448. begin
  2449. StartProgram(false);
  2450. Add('var e: extended;');
  2451. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2452. ConvertProgram;
  2453. end;
  2454. procedure TTestModule.TestConstBaseTypes;
  2455. begin
  2456. StartProgram(false);
  2457. Add('const');
  2458. Add(' i: longint = 3;');
  2459. Add(' s: string = ''foo'';');
  2460. Add(' c: char = ''4'';');
  2461. Add(' b: boolean = true;');
  2462. Add(' d: double = 5.6;');
  2463. Add(' e = low(word);');
  2464. Add(' f = high(word);');
  2465. Add('begin');
  2466. ConvertProgram;
  2467. CheckSource('TestVarBaseTypes',
  2468. LinesToStr([
  2469. 'this.i=3;',
  2470. 'this.s="foo";',
  2471. 'this.c="4";',
  2472. 'this.b=true;',
  2473. 'this.d=5.6;',
  2474. 'this.e = 0;',
  2475. 'this.f = 65535;'
  2476. ]),
  2477. '');
  2478. end;
  2479. procedure TTestModule.TestAliasTypeRef;
  2480. begin
  2481. StartProgram(false);
  2482. Add('type');
  2483. Add(' a=longint;');
  2484. Add(' b=a;');
  2485. Add('var');
  2486. Add(' c: A;');
  2487. Add(' d: B;');
  2488. Add('begin');
  2489. ConvertProgram;
  2490. CheckSource('TestAliasTypeRef',
  2491. LinesToStr([ // statements
  2492. 'this.c = 0;',
  2493. 'this.d = 0;'
  2494. ]),
  2495. LinesToStr([ // this.$main
  2496. ''
  2497. ]));
  2498. end;
  2499. procedure TTestModule.TestTypeCast_BaseTypes;
  2500. begin
  2501. StartProgram(false);
  2502. Add([
  2503. 'var',
  2504. ' i: longint;',
  2505. ' b: boolean;',
  2506. ' d: double;',
  2507. ' s: string;',
  2508. ' c: char;',
  2509. 'begin',
  2510. ' i:=longint(i);',
  2511. ' i:=longint(b);',
  2512. ' b:=boolean(b);',
  2513. ' b:=boolean(i);',
  2514. ' d:=double(d);',
  2515. ' d:=double(i);',
  2516. ' s:=string(s);',
  2517. ' s:=string(c);',
  2518. ' c:=char(c);',
  2519. ' c:=char(i);',
  2520. ' c:=char(65);',
  2521. ' c:=char(#10);',
  2522. ' c:=char(#$E000);',
  2523. '']);
  2524. ConvertProgram;
  2525. CheckSource('TestAliasTypeRef',
  2526. LinesToStr([ // statements
  2527. 'this.i = 0;',
  2528. 'this.b = false;',
  2529. 'this.d = 0.0;',
  2530. 'this.s = "";',
  2531. 'this.c = "";',
  2532. '']),
  2533. LinesToStr([ // this.$main
  2534. '$mod.i = $mod.i;',
  2535. '$mod.i = ($mod.b ? 1 : 0);',
  2536. '$mod.b = $mod.b;',
  2537. '$mod.b = $mod.i != 0;',
  2538. '$mod.d = $mod.d;',
  2539. '$mod.d = $mod.i;',
  2540. '$mod.s = $mod.s;',
  2541. '$mod.s = $mod.c;',
  2542. '$mod.c = $mod.c;',
  2543. '$mod.c = String.fromCharCode($mod.i);',
  2544. '$mod.c = "A";',
  2545. '$mod.c = "\n";',
  2546. '$mod.c = "";',
  2547. '']));
  2548. end;
  2549. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2550. begin
  2551. StartProgram(false);
  2552. Add('type');
  2553. Add(' integer = longint;');
  2554. Add(' TYesNo = boolean;');
  2555. Add(' TFloat = double;');
  2556. Add(' TCaption = string;');
  2557. Add(' TChar = char;');
  2558. Add('var');
  2559. Add(' i: integer;');
  2560. Add(' b: TYesNo;');
  2561. Add(' d: TFloat;');
  2562. Add(' s: TCaption;');
  2563. Add(' c: TChar;');
  2564. Add('begin');
  2565. Add(' i:=integer(i);');
  2566. Add(' i:=integer(b);');
  2567. Add(' b:=TYesNo(b);');
  2568. Add(' b:=TYesNo(i);');
  2569. Add(' d:=TFloat(d);');
  2570. Add(' d:=TFloat(i);');
  2571. Add(' s:=TCaption(s);');
  2572. Add(' s:=TCaption(c);');
  2573. Add(' c:=TChar(c);');
  2574. ConvertProgram;
  2575. CheckSource('TestAliasTypeRef',
  2576. LinesToStr([ // statements
  2577. 'this.i = 0;',
  2578. 'this.b = false;',
  2579. 'this.d = 0.0;',
  2580. 'this.s = "";',
  2581. 'this.c = "";',
  2582. '']),
  2583. LinesToStr([ // this.$main
  2584. '$mod.i = $mod.i;',
  2585. '$mod.i = ($mod.b ? 1 : 0);',
  2586. '$mod.b = $mod.b;',
  2587. '$mod.b = $mod.i != 0;',
  2588. '$mod.d = $mod.d;',
  2589. '$mod.d = $mod.i;',
  2590. '$mod.s = $mod.s;',
  2591. '$mod.s = $mod.c;',
  2592. '$mod.c = $mod.c;',
  2593. '']));
  2594. end;
  2595. procedure TTestModule.TestEmptyProc;
  2596. begin
  2597. StartProgram(false);
  2598. Add('procedure Test;');
  2599. Add('begin');
  2600. Add('end;');
  2601. Add('begin');
  2602. ConvertProgram;
  2603. CheckSource('TestEmptyProc',
  2604. LinesToStr([ // statements
  2605. 'this.Test = function () {',
  2606. '};'
  2607. ]),
  2608. LinesToStr([ // this.$main
  2609. ''
  2610. ]));
  2611. end;
  2612. procedure TTestModule.TestProcOneParam;
  2613. begin
  2614. StartProgram(false);
  2615. Add('procedure ProcA(i: longint);');
  2616. Add('begin');
  2617. Add('end;');
  2618. Add('begin');
  2619. Add(' PROCA(3);');
  2620. ConvertProgram;
  2621. CheckSource('TestProcOneParam',
  2622. LinesToStr([ // statements
  2623. 'this.ProcA = function (i) {',
  2624. '};'
  2625. ]),
  2626. LinesToStr([ // this.$main
  2627. '$mod.ProcA(3);'
  2628. ]));
  2629. end;
  2630. procedure TTestModule.TestFunctionWithoutParams;
  2631. begin
  2632. StartProgram(false);
  2633. Add('function FuncA: longint;');
  2634. Add('begin');
  2635. Add('end;');
  2636. Add('var i: longint;');
  2637. Add('begin');
  2638. Add(' I:=FUNCA();');
  2639. Add(' I:=FUNCA;');
  2640. Add(' FUNCA();');
  2641. Add(' FUNCA;');
  2642. ConvertProgram;
  2643. CheckSource('TestProcWithoutParams',
  2644. LinesToStr([ // statements
  2645. 'this.FuncA = function () {',
  2646. ' var Result = 0;',
  2647. ' return Result;',
  2648. '};',
  2649. 'this.i=0;'
  2650. ]),
  2651. LinesToStr([ // this.$main
  2652. '$mod.i=$mod.FuncA();',
  2653. '$mod.i=$mod.FuncA();',
  2654. '$mod.FuncA();',
  2655. '$mod.FuncA();'
  2656. ]));
  2657. end;
  2658. procedure TTestModule.TestProcedureWithoutParams;
  2659. begin
  2660. StartProgram(false);
  2661. Add('procedure ProcA;');
  2662. Add('begin');
  2663. Add('end;');
  2664. Add('begin');
  2665. Add(' PROCA();');
  2666. Add(' PROCA;');
  2667. ConvertProgram;
  2668. CheckSource('TestProcWithoutParams',
  2669. LinesToStr([ // statements
  2670. 'this.ProcA = function () {',
  2671. '};'
  2672. ]),
  2673. LinesToStr([ // this.$main
  2674. '$mod.ProcA();',
  2675. '$mod.ProcA();'
  2676. ]));
  2677. end;
  2678. procedure TTestModule.TestIncDec;
  2679. begin
  2680. StartProgram(false);
  2681. Add([
  2682. 'procedure DoIt(var i: longint);',
  2683. 'begin',
  2684. ' inc(i);',
  2685. ' inc(i,2);',
  2686. 'end;',
  2687. 'var',
  2688. ' Bar: longint;',
  2689. 'begin',
  2690. ' inc(bar);',
  2691. ' inc(bar,2);',
  2692. ' dec(bar);',
  2693. ' dec(bar,3);',
  2694. '']);
  2695. ConvertProgram;
  2696. CheckSource('TestIncDec',
  2697. LinesToStr([ // statements
  2698. 'this.DoIt = function (i) {',
  2699. ' i.set(i.get()+1);',
  2700. ' i.set(i.get()+2);',
  2701. '};',
  2702. 'this.Bar = 0;'
  2703. ]),
  2704. LinesToStr([ // this.$main
  2705. '$mod.Bar+=1;',
  2706. '$mod.Bar+=2;',
  2707. '$mod.Bar-=1;',
  2708. '$mod.Bar-=3;'
  2709. ]));
  2710. end;
  2711. procedure TTestModule.TestLoHiFpcMode;
  2712. begin
  2713. StartProgram(false);
  2714. Add([
  2715. '{$mode objfpc}',
  2716. 'const',
  2717. ' LoByte1 = Lo(Word($1234));',
  2718. ' HiByte1 = Hi(Word($1234));',
  2719. ' LoByte2 = Lo(SmallInt($1234));',
  2720. ' HiByte2 = Hi(SmallInt($1234));',
  2721. ' LoWord1 = Lo($1234CDEF);',
  2722. ' HiWord1 = Hi($1234CDEF);',
  2723. ' LoWord2 = Lo(-$1234CDEF);',
  2724. ' HiWord2 = Hi(-$1234CDEF);',
  2725. ' lo4:byte=lo(byte($34));',
  2726. ' hi4:byte=hi(byte($34));',
  2727. ' lo5:byte=lo(shortint(-$34));',
  2728. ' hi5:byte=hi(shortint(-$34));',
  2729. ' lo6:longword=lo($123456789ABCD);',
  2730. ' hi6:longword=hi($123456789ABCD);',
  2731. ' lo7:longword=lo(-$123456789ABCD);',
  2732. ' hi7:longword=hi(-$123456789ABCD);',
  2733. 'var',
  2734. ' b: Byte;',
  2735. ' ss: shortint;',
  2736. ' w: Word;',
  2737. ' si: SmallInt;',
  2738. ' lw: LongWord;',
  2739. ' li: LongInt;',
  2740. ' b2: Byte;',
  2741. ' ni: nativeint;',
  2742. 'begin',
  2743. ' w := $1234;',
  2744. ' ss := -$12;',
  2745. ' b := lo(ss);',
  2746. ' b := HI(ss);',
  2747. ' b := lo(w);',
  2748. ' b := HI(w);',
  2749. ' b2 := lo(b);',
  2750. ' b2 := hi(b);',
  2751. ' lw := $1234CDEF;',
  2752. ' w := lo(lw);',
  2753. ' w := hi(lw);',
  2754. ' ni := $123456789ABCD;',
  2755. ' lw := lo(ni);',
  2756. ' lw := hi(ni);',
  2757. '']);
  2758. ConvertProgram;
  2759. CheckSource('TestLoHiFpcMode',
  2760. LinesToStr([ // statements
  2761. 'this.LoByte1 = 0x1234 & 0xFF;',
  2762. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2763. 'this.LoByte2 = 0x1234 & 0xFF;',
  2764. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2765. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2766. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2767. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2768. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2769. 'this.lo4 = 0x34 & 0xF;',
  2770. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2771. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2772. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2773. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2774. 'this.hi6 = 74565 >>> 0;',
  2775. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2776. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2777. 'this.b = 0;',
  2778. 'this.ss = 0;',
  2779. 'this.w = 0;',
  2780. 'this.si = 0;',
  2781. 'this.lw = 0;',
  2782. 'this.li = 0;',
  2783. 'this.b2 = 0;',
  2784. 'this.ni = 0;',
  2785. '']),
  2786. LinesToStr([ // this.$main
  2787. '$mod.w = 0x1234;',
  2788. '$mod.ss = -0x12;',
  2789. '$mod.b = $mod.ss & 0xFF;',
  2790. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2791. '$mod.b = $mod.w & 0xFF;',
  2792. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2793. '$mod.b2 = $mod.b & 0xF;',
  2794. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2795. '$mod.lw = 0x1234CDEF;',
  2796. '$mod.w = $mod.lw & 0xFFFF;',
  2797. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2798. '$mod.ni = 0x123456789ABCD;',
  2799. '$mod.lw = $mod.ni >>> 0;',
  2800. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2801. '']));
  2802. end;
  2803. procedure TTestModule.TestLoHiDelphiMode;
  2804. begin
  2805. StartProgram(false);
  2806. Add([
  2807. '{$mode delphi}',
  2808. 'const',
  2809. ' LoByte1 = Lo(Word($1234));',
  2810. ' HiByte1 = Hi(Word($1234));',
  2811. ' LoByte2 = Lo(SmallInt($1234));',
  2812. ' HiByte2 = Hi(SmallInt($1234));',
  2813. ' LoByte3 = Lo($1234CDEF);',
  2814. ' HiByte3 = Hi($1234CDEF);',
  2815. ' LoByte4 = Lo(-$1234CDEF);',
  2816. ' HiByte4 = Hi(-$1234CDEF);',
  2817. 'var',
  2818. ' b: Byte;',
  2819. ' w: Word;',
  2820. ' si: SmallInt;',
  2821. ' lw: LongWord;',
  2822. ' li: LongInt;',
  2823. 'begin',
  2824. ' w := $1234;',
  2825. ' b := lo(w);',
  2826. ' b := HI(w);',
  2827. ' lw := $1234CDEF;',
  2828. ' b := lo(lw);',
  2829. ' b := hi(lw);',
  2830. '']);
  2831. ConvertProgram;
  2832. CheckSource('TestLoHiDelphiMode',
  2833. LinesToStr([ // statements
  2834. 'this.LoByte1 = 0x1234 & 0xFF;',
  2835. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2836. 'this.LoByte2 = 0x1234 & 0xFF;',
  2837. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2838. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2839. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2840. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2841. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2842. 'this.b = 0;',
  2843. 'this.w = 0;',
  2844. 'this.si = 0;',
  2845. 'this.lw = 0;',
  2846. 'this.li = 0;'
  2847. ]),
  2848. LinesToStr([ // this.$main
  2849. '$mod.w = 0x1234;',
  2850. '$mod.b = $mod.w & 0xFF;',
  2851. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2852. '$mod.lw = 0x1234CDEF;',
  2853. '$mod.b = $mod.lw & 0xFF;',
  2854. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2855. ]));
  2856. end;
  2857. procedure TTestModule.TestAssignments;
  2858. begin
  2859. StartProgram(false);
  2860. Parser.Options:=Parser.Options+[po_cassignments];
  2861. Add('var');
  2862. Add(' Bar:longint;');
  2863. Add('begin');
  2864. Add(' bar:=3;');
  2865. Add(' bar+=4;');
  2866. Add(' bar-=5;');
  2867. Add(' bar*=6;');
  2868. ConvertProgram;
  2869. CheckSource('TestAssignments',
  2870. LinesToStr([ // statements
  2871. 'this.Bar = 0;'
  2872. ]),
  2873. LinesToStr([ // this.$main
  2874. '$mod.Bar=3;',
  2875. '$mod.Bar+=4;',
  2876. '$mod.Bar-=5;',
  2877. '$mod.Bar*=6;'
  2878. ]));
  2879. end;
  2880. procedure TTestModule.TestArithmeticOperators1;
  2881. begin
  2882. StartProgram(false);
  2883. Add('var');
  2884. Add(' vA,vB,vC:longint;');
  2885. Add('begin');
  2886. Add(' va:=1;');
  2887. Add(' vb:=va+va;');
  2888. Add(' vb:=va div vb;');
  2889. Add(' vb:=va mod vb;');
  2890. Add(' vb:=va+va*vb+va div vb;');
  2891. Add(' vc:=-va;');
  2892. Add(' va:=va-vb;');
  2893. Add(' vb:=va;');
  2894. Add(' if va<vb then vc:=va else vc:=vb;');
  2895. ConvertProgram;
  2896. CheckSource('TestArithmeticOperators1',
  2897. LinesToStr([ // statements
  2898. 'this.vA = 0;',
  2899. 'this.vB = 0;',
  2900. 'this.vC = 0;'
  2901. ]),
  2902. LinesToStr([ // this.$main
  2903. '$mod.vA = 1;',
  2904. '$mod.vB = $mod.vA + $mod.vA;',
  2905. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2906. '$mod.vB = $mod.vA % $mod.vB;',
  2907. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2908. '$mod.vC = -$mod.vA;',
  2909. '$mod.vA = $mod.vA - $mod.vB;',
  2910. '$mod.vB = $mod.vA;',
  2911. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2912. ]));
  2913. end;
  2914. procedure TTestModule.TestLogicalOperators;
  2915. begin
  2916. StartProgram(false);
  2917. Add('var');
  2918. Add(' vA,vB,vC:boolean;');
  2919. Add('begin');
  2920. Add(' va:=vb and vc;');
  2921. Add(' va:=vb or vc;');
  2922. Add(' va:=vb xor vc;');
  2923. Add(' va:=true and vc;');
  2924. Add(' va:=(vb and vc) or (va and vb);');
  2925. Add(' va:=not vb;');
  2926. ConvertProgram;
  2927. CheckSource('TestLogicalOperators',
  2928. LinesToStr([ // statements
  2929. 'this.vA = false;',
  2930. 'this.vB = false;',
  2931. 'this.vC = false;'
  2932. ]),
  2933. LinesToStr([ // this.$main
  2934. '$mod.vA = $mod.vB && $mod.vC;',
  2935. '$mod.vA = $mod.vB || $mod.vC;',
  2936. '$mod.vA = $mod.vB ^ $mod.vC;',
  2937. '$mod.vA = true && $mod.vC;',
  2938. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2939. '$mod.vA = !$mod.vB;'
  2940. ]));
  2941. end;
  2942. procedure TTestModule.TestBitwiseOperators;
  2943. begin
  2944. StartProgram(false);
  2945. Add([
  2946. 'var',
  2947. ' vA,vB,vC:longint;',
  2948. ' X,Y,Z: nativeint;',
  2949. 'begin',
  2950. ' va:=vb and vc;',
  2951. ' va:=vb or vc;',
  2952. ' va:=vb xor vc;',
  2953. ' va:=vb shl vc;',
  2954. ' va:=vb shr vc;',
  2955. ' va:=3 and vc;',
  2956. ' va:=(vb and vc) or (va and vb);',
  2957. ' va:=not vb;',
  2958. ' X:=Y and Z;',
  2959. ' X:=Y and va;',
  2960. ' X:=Y or Z;',
  2961. ' X:=Y or va;',
  2962. ' X:=Y xor Z;',
  2963. ' X:=Y xor va;',
  2964. '']);
  2965. ConvertProgram;
  2966. CheckSource('TestBitwiseOperators',
  2967. LinesToStr([ // statements
  2968. 'this.vA = 0;',
  2969. 'this.vB = 0;',
  2970. 'this.vC = 0;',
  2971. 'this.X = 0;',
  2972. 'this.Y = 0;',
  2973. 'this.Z = 0;',
  2974. '']),
  2975. LinesToStr([ // this.$main
  2976. '$mod.vA = $mod.vB & $mod.vC;',
  2977. '$mod.vA = $mod.vB | $mod.vC;',
  2978. '$mod.vA = $mod.vB ^ $mod.vC;',
  2979. '$mod.vA = $mod.vB << $mod.vC;',
  2980. '$mod.vA = $mod.vB >>> $mod.vC;',
  2981. '$mod.vA = 3 & $mod.vC;',
  2982. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2983. '$mod.vA = ~$mod.vB;',
  2984. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  2985. '$mod.X = $mod.Y & $mod.vA;',
  2986. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  2987. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  2988. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  2989. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  2990. '']));
  2991. end;
  2992. procedure TTestModule.TestBitwiseOperatorsLongword;
  2993. begin
  2994. StartProgram(false);
  2995. Add([
  2996. 'var',
  2997. ' a,b,c:longword;',
  2998. ' i: longint;',
  2999. 'begin',
  3000. ' a:=$12345678;',
  3001. ' b:=$EDCBA987;',
  3002. ' c:=not a;',
  3003. ' c:=a and b;',
  3004. ' c:=a and $ffff0000;',
  3005. ' c:=a or b;',
  3006. ' c:=a or $ff00ff00;',
  3007. ' c:=a xor b;',
  3008. ' c:=a xor $f0f0f0f0;',
  3009. ' c:=a shl 1;',
  3010. ' c:=a shl 16;',
  3011. ' c:=a shl 24;',
  3012. ' c:=a shl b;',
  3013. ' c:=a shr 1;',
  3014. ' c:=a shr 16;',
  3015. ' c:=a shr 24;',
  3016. ' c:=a shr b;',
  3017. ' c:=(b and c) or (a and b);',
  3018. ' c:=i and a;',
  3019. ' c:=i or a;',
  3020. ' c:=i xor a;',
  3021. '']);
  3022. ConvertProgram;
  3023. CheckSource('TestBitwiseOperatorsLongword',
  3024. LinesToStr([ // statements
  3025. 'this.a = 0;',
  3026. 'this.b = 0;',
  3027. 'this.c = 0;',
  3028. 'this.i = 0;',
  3029. '']),
  3030. LinesToStr([ // this.$main
  3031. '$mod.a = 0x12345678;',
  3032. '$mod.b = 0xEDCBA987;',
  3033. '$mod.c = rtl.lw(~$mod.a);',
  3034. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3035. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3036. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3037. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3038. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3039. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3040. '$mod.c = rtl.lw($mod.a << 1);',
  3041. '$mod.c = rtl.lw($mod.a << 16);',
  3042. '$mod.c = rtl.lw($mod.a << 24);',
  3043. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3044. '$mod.c = rtl.lw($mod.a >>> 1);',
  3045. '$mod.c = rtl.lw($mod.a >>> 16);',
  3046. '$mod.c = rtl.lw($mod.a >>> 24);',
  3047. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3048. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3049. '$mod.c = $mod.i & $mod.a;',
  3050. '$mod.c = $mod.i | $mod.a;',
  3051. '$mod.c = $mod.i ^ $mod.a;',
  3052. '']));
  3053. end;
  3054. procedure TTestModule.TestPrgProcVar;
  3055. begin
  3056. StartProgram(false);
  3057. Add('procedure Proc1;');
  3058. Add('type');
  3059. Add(' t1=longint;');
  3060. Add('var');
  3061. Add(' vA:t1;');
  3062. Add('begin');
  3063. Add('end;');
  3064. Add('begin');
  3065. ConvertProgram;
  3066. CheckSource('TestPrgProcVar',
  3067. LinesToStr([ // statements
  3068. 'this.Proc1 = function () {',
  3069. ' var vA=0;',
  3070. '};'
  3071. ]),
  3072. LinesToStr([ // this.$main
  3073. ''
  3074. ]));
  3075. end;
  3076. procedure TTestModule.TestUnitProcVar;
  3077. begin
  3078. StartUnit(false);
  3079. Add('interface');
  3080. Add('');
  3081. Add('type tA=string; // unit scope');
  3082. Add('procedure Proc1;');
  3083. Add('');
  3084. Add('implementation');
  3085. Add('');
  3086. Add('procedure Proc1;');
  3087. Add('type tA=longint; // local proc scope');
  3088. Add('var v1:tA; // using local tA');
  3089. Add('begin');
  3090. Add('end;');
  3091. Add('var v2:tA; // using interface tA');
  3092. ConvertUnit;
  3093. CheckSource('TestUnitProcVar',
  3094. LinesToStr([ // statements
  3095. 'var $impl = $mod.$impl;',
  3096. 'this.Proc1 = function () {',
  3097. ' var v1 = 0;',
  3098. '};',
  3099. '']),
  3100. // this.$init
  3101. '',
  3102. // implementation
  3103. LinesToStr([
  3104. '$impl.v2 = "";',
  3105. '']));
  3106. end;
  3107. procedure TTestModule.TestImplProc;
  3108. begin
  3109. StartUnit(false);
  3110. Add('interface');
  3111. Add('');
  3112. Add('procedure Proc1;');
  3113. Add('');
  3114. Add('implementation');
  3115. Add('');
  3116. Add('procedure Proc1; begin end;');
  3117. Add('procedure Proc2; begin end;');
  3118. Add('initialization');
  3119. Add(' Proc1;');
  3120. Add(' Proc2;');
  3121. ConvertUnit;
  3122. CheckSource('TestImplProc',
  3123. LinesToStr([ // statements
  3124. 'var $impl = $mod.$impl;',
  3125. 'this.Proc1 = function () {',
  3126. '};',
  3127. '']),
  3128. LinesToStr([ // this.$init
  3129. '$mod.Proc1();',
  3130. '$impl.Proc2();',
  3131. '']),
  3132. LinesToStr([ // implementation
  3133. '$impl.Proc2 = function () {',
  3134. '};',
  3135. ''])
  3136. );
  3137. end;
  3138. procedure TTestModule.TestFunctionResult;
  3139. begin
  3140. StartProgram(false);
  3141. Add('function Func1: longint;');
  3142. Add('begin');
  3143. Add(' Result:=3;');
  3144. Add(' Func1:=4;');
  3145. Add('end;');
  3146. Add('begin');
  3147. ConvertProgram;
  3148. CheckSource('TestFunctionResult',
  3149. LinesToStr([ // statements
  3150. 'this.Func1 = function () {',
  3151. ' var Result = 0;',
  3152. ' Result = 3;',
  3153. ' Result = 4;',
  3154. ' return Result;',
  3155. '};'
  3156. ]),
  3157. '');
  3158. end;
  3159. procedure TTestModule.TestNestedProc;
  3160. begin
  3161. StartProgram(false);
  3162. Add([
  3163. 'var vInUnit: longint;',
  3164. 'function DoIt(pA,pD: longint): longint;',
  3165. 'var',
  3166. ' vB: longint;',
  3167. ' vC: longint;',
  3168. ' function Nesty(pA: longint): longint; ',
  3169. ' var vB: longint;',
  3170. ' begin',
  3171. ' Result:=pa+vb+vc+pd+vInUnit;',
  3172. ' nesty:=3;',
  3173. ' doit:=4;',
  3174. ' exit;',
  3175. ' end;',
  3176. 'begin',
  3177. ' Result:=pa+vb+vc;',
  3178. ' doit:=6;',
  3179. ' exit;',
  3180. 'end;',
  3181. 'begin']);
  3182. ConvertProgram;
  3183. CheckSource('TestNestedProc',
  3184. LinesToStr([ // statements
  3185. 'this.vInUnit = 0;',
  3186. 'this.DoIt = function (pA, pD) {',
  3187. ' var Result = 0;',
  3188. ' var vB = 0;',
  3189. ' var vC = 0;',
  3190. ' function Nesty(pA) {',
  3191. ' var Result$1 = 0;',
  3192. ' var vB = 0;',
  3193. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3194. ' Result$1 = 3;',
  3195. ' Result = 4;',
  3196. ' return Result$1;',
  3197. ' return Result$1;',
  3198. ' };',
  3199. ' Result = pA + vB + vC;',
  3200. ' Result = 6;',
  3201. ' return Result;',
  3202. ' return Result;',
  3203. '};'
  3204. ]),
  3205. '');
  3206. end;
  3207. procedure TTestModule.TestNestedProc_ResultString;
  3208. begin
  3209. StartProgram(false);
  3210. Add([
  3211. 'function DoIt: string;',
  3212. ' function Nesty: string; ',
  3213. ' begin',
  3214. ' nesty:=#65#66;',
  3215. ' nesty[1]:=#67;',
  3216. ' doit:=#68;',
  3217. ' doit[2]:=#69;',
  3218. ' end;',
  3219. 'begin',
  3220. ' doit:=#70;',
  3221. ' doit[3]:=#71;',
  3222. 'end;',
  3223. 'begin']);
  3224. ConvertProgram;
  3225. CheckSource('TestNestedProc_ResultString',
  3226. LinesToStr([ // statements
  3227. 'this.DoIt = function () {',
  3228. ' var Result = "";',
  3229. ' function Nesty() {',
  3230. ' var Result$1 = "";',
  3231. ' Result$1 = "AB";',
  3232. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3233. ' Result = "D";',
  3234. ' Result = rtl.setCharAt(Result, 1, "E");',
  3235. ' return Result$1;',
  3236. ' };',
  3237. ' Result = "F";',
  3238. ' Result = rtl.setCharAt(Result, 2, "G");',
  3239. ' return Result;',
  3240. '};'
  3241. ]),
  3242. '');
  3243. end;
  3244. procedure TTestModule.TestForwardProc;
  3245. begin
  3246. StartProgram(false);
  3247. Add('procedure FuncA(Bar: longint); forward;');
  3248. Add('procedure FuncB(Bar: longint);');
  3249. Add('begin');
  3250. Add(' funca(bar);');
  3251. Add('end;');
  3252. Add('procedure funca(bar: longint);');
  3253. Add('begin');
  3254. Add(' if bar=3 then ;');
  3255. Add('end;');
  3256. Add('begin');
  3257. Add(' funca(4);');
  3258. Add(' funcb(5);');
  3259. ConvertProgram;
  3260. CheckSource('TestForwardProc',
  3261. LinesToStr([ // statements'
  3262. 'this.FuncB = function (Bar) {',
  3263. ' $mod.FuncA(Bar);',
  3264. '};',
  3265. 'this.FuncA = function (Bar) {',
  3266. ' if (Bar === 3);',
  3267. '};'
  3268. ]),
  3269. LinesToStr([
  3270. '$mod.FuncA(4);',
  3271. '$mod.FuncB(5);'
  3272. ])
  3273. );
  3274. end;
  3275. procedure TTestModule.TestNestedForwardProc;
  3276. begin
  3277. StartProgram(false);
  3278. Add('procedure FuncA;');
  3279. Add(' procedure FuncB(i: longint); forward;');
  3280. Add(' procedure FuncC(i: longint);');
  3281. Add(' begin');
  3282. Add(' funcb(i);');
  3283. Add(' end;');
  3284. Add(' procedure FuncB(i: longint);');
  3285. Add(' begin');
  3286. Add(' if i=3 then ;');
  3287. Add(' end;');
  3288. Add('begin');
  3289. Add(' funcc(4)');
  3290. Add('end;');
  3291. Add('begin');
  3292. Add(' funca;');
  3293. ConvertProgram;
  3294. CheckSource('TestNestedForwardProc',
  3295. LinesToStr([ // statements'
  3296. 'this.FuncA = function () {',
  3297. ' function FuncC(i) {',
  3298. ' FuncB(i);',
  3299. ' };',
  3300. ' function FuncB(i) {',
  3301. ' if (i === 3);',
  3302. ' };',
  3303. ' FuncC(4);',
  3304. '};'
  3305. ]),
  3306. LinesToStr([
  3307. '$mod.FuncA();'
  3308. ])
  3309. );
  3310. end;
  3311. procedure TTestModule.TestAssignFunctionResult;
  3312. begin
  3313. StartProgram(false);
  3314. Add('function Func1: longint;');
  3315. Add('begin');
  3316. Add('end;');
  3317. Add('var i: longint;');
  3318. Add('begin');
  3319. Add(' i:=func1();');
  3320. Add(' i:=func1()+func1();');
  3321. ConvertProgram;
  3322. CheckSource('TestAssignFunctionResult',
  3323. LinesToStr([ // statements
  3324. 'this.Func1 = function () {',
  3325. ' var Result = 0;',
  3326. ' return Result;',
  3327. '};',
  3328. 'this.i = 0;'
  3329. ]),
  3330. LinesToStr([
  3331. '$mod.i = $mod.Func1();',
  3332. '$mod.i = $mod.Func1() + $mod.Func1();'
  3333. ]));
  3334. end;
  3335. procedure TTestModule.TestFunctionResultInCondition;
  3336. begin
  3337. StartProgram(false);
  3338. Add('function Func1: longint;');
  3339. Add('begin');
  3340. Add('end;');
  3341. Add('function Func2: boolean;');
  3342. Add('begin');
  3343. Add('end;');
  3344. Add('var i: longint;');
  3345. Add('begin');
  3346. Add(' if func2 then ;');
  3347. Add(' if i=func1() then ;');
  3348. Add(' if i=func1 then ;');
  3349. ConvertProgram;
  3350. CheckSource('TestFunctionResultInCondition',
  3351. LinesToStr([ // statements
  3352. 'this.Func1 = function () {',
  3353. ' var Result = 0;',
  3354. ' return Result;',
  3355. '};',
  3356. 'this.Func2 = function () {',
  3357. ' var Result = false;',
  3358. ' return Result;',
  3359. '};',
  3360. 'this.i = 0;'
  3361. ]),
  3362. LinesToStr([
  3363. 'if ($mod.Func2());',
  3364. 'if ($mod.i === $mod.Func1());',
  3365. 'if ($mod.i === $mod.Func1());'
  3366. ]));
  3367. end;
  3368. procedure TTestModule.TestFunctionResultInForLoop;
  3369. begin
  3370. StartProgram(false);
  3371. Add([
  3372. 'function Func1(a: array of longint): longint;',
  3373. 'begin',
  3374. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3375. ' for Result in a do if a[Result]=0 then exit;',
  3376. 'end;',
  3377. 'begin',
  3378. ' Func1([1,2,3])']);
  3379. ConvertProgram;
  3380. CheckSource('TestFunctionResultInForLoop',
  3381. LinesToStr([ // statements
  3382. 'this.Func1 = function (a) {',
  3383. ' var Result = 0;',
  3384. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3385. ' Result = $l1;',
  3386. ' if (a[Result] === 0) return Result;',
  3387. ' };',
  3388. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3389. ' Result = $in2[$l3];',
  3390. ' if (a[Result] === 0) return Result;',
  3391. ' };',
  3392. ' return Result;',
  3393. '};',
  3394. '']),
  3395. LinesToStr([
  3396. '$mod.Func1([1, 2, 3]);'
  3397. ]));
  3398. end;
  3399. procedure TTestModule.TestFunctionResultInTypeCast;
  3400. begin
  3401. StartProgram(false);
  3402. Add([
  3403. 'function GetInt: longint;',
  3404. 'begin',
  3405. 'end;',
  3406. 'begin',
  3407. ' if Byte(GetInt)=0 then ;',
  3408. '']);
  3409. ConvertProgram;
  3410. CheckSource('TestFunctionResultInTypeCast',
  3411. LinesToStr([ // statements
  3412. 'this.GetInt = function () {',
  3413. ' var Result = 0;',
  3414. ' return Result;',
  3415. '};',
  3416. '']),
  3417. LinesToStr([
  3418. 'if (($mod.GetInt() & 255) === 0) ;'
  3419. ]));
  3420. end;
  3421. procedure TTestModule.TestExit;
  3422. begin
  3423. StartProgram(false);
  3424. Add('procedure ProcA;');
  3425. Add('begin');
  3426. Add(' exit;');
  3427. Add('end;');
  3428. Add('function FuncB: longint;');
  3429. Add('begin');
  3430. Add(' exit;');
  3431. Add(' exit(3);');
  3432. Add('end;');
  3433. Add('function FuncC: string;');
  3434. Add('begin');
  3435. Add(' exit;');
  3436. Add(' exit(''a'');');
  3437. Add(' exit(''abc'');');
  3438. Add('end;');
  3439. Add('begin');
  3440. Add(' exit;');
  3441. Add(' exit(1);');
  3442. ConvertProgram;
  3443. CheckSource('TestExit',
  3444. LinesToStr([ // statements
  3445. 'this.ProcA = function () {',
  3446. ' return;',
  3447. '};',
  3448. 'this.FuncB = function () {',
  3449. ' var Result = 0;',
  3450. ' return Result;',
  3451. ' return 3;',
  3452. ' return Result;',
  3453. '};',
  3454. 'this.FuncC = function () {',
  3455. ' var Result = "";',
  3456. ' return Result;',
  3457. ' return "a";',
  3458. ' return "abc";',
  3459. ' return Result;',
  3460. '};'
  3461. ]),
  3462. LinesToStr([
  3463. 'return;',
  3464. 'return 1;',
  3465. '']));
  3466. end;
  3467. procedure TTestModule.TestBreak;
  3468. begin
  3469. StartProgram(false);
  3470. Add([
  3471. 'var',
  3472. ' i: longint;',
  3473. 'begin',
  3474. ' repeat',
  3475. ' break;',
  3476. ' until true;',
  3477. ' while true do',
  3478. ' break;',
  3479. ' for i:=1 to 2 do',
  3480. ' break;']);
  3481. ConvertProgram;
  3482. CheckSource('TestBreak',
  3483. LinesToStr([ // statements
  3484. 'this.i = 0;'
  3485. ]),
  3486. LinesToStr([
  3487. 'do {',
  3488. ' break;',
  3489. '} while (!true);',
  3490. 'while (true) break;',
  3491. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3492. '']));
  3493. end;
  3494. procedure TTestModule.TestBreakAsVar;
  3495. begin
  3496. StartProgram(false);
  3497. Add([
  3498. 'procedure DoIt(break: boolean);',
  3499. 'begin',
  3500. ' if break then ;',
  3501. 'end;',
  3502. 'var',
  3503. ' break: boolean;',
  3504. 'begin',
  3505. ' if break then ;']);
  3506. ConvertProgram;
  3507. CheckSource('TestBreakAsVar',
  3508. LinesToStr([ // statements
  3509. 'this.DoIt = function (Break) {',
  3510. ' if (Break) ;',
  3511. '};',
  3512. 'this.Break = false;',
  3513. '']),
  3514. LinesToStr([
  3515. 'if($mod.Break) ;',
  3516. '']));
  3517. end;
  3518. procedure TTestModule.TestContinue;
  3519. begin
  3520. StartProgram(false);
  3521. Add('var i: longint;');
  3522. Add('begin');
  3523. Add(' repeat');
  3524. Add(' continue;');
  3525. Add(' until true;');
  3526. Add(' while true do');
  3527. Add(' continue;');
  3528. Add(' for i:=1 to 2 do');
  3529. Add(' continue;');
  3530. ConvertProgram;
  3531. CheckSource('TestContinue',
  3532. LinesToStr([ // statements
  3533. 'this.i = 0;'
  3534. ]),
  3535. LinesToStr([
  3536. 'do {',
  3537. ' continue;',
  3538. '} while (!true);',
  3539. 'while (true) continue;',
  3540. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3541. '']));
  3542. end;
  3543. procedure TTestModule.TestProc_External;
  3544. begin
  3545. StartProgram(false);
  3546. Add('procedure Foo; external name ''console.log'';');
  3547. Add('function Bar: longint; external name ''get.item'';');
  3548. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3549. Add('var');
  3550. Add(' i: longint;');
  3551. Add('begin');
  3552. Add(' Foo;');
  3553. Add(' i:=Bar;');
  3554. Add(' i:=Bla(''abc'');');
  3555. ConvertProgram;
  3556. CheckSource('TestProc_External',
  3557. LinesToStr([ // statements
  3558. 'this.i = 0;'
  3559. ]),
  3560. LinesToStr([
  3561. 'console.log();',
  3562. '$mod.i = get.item();',
  3563. '$mod.i = apply.something("abc");'
  3564. ]));
  3565. end;
  3566. procedure TTestModule.TestProc_ExternalOtherUnit;
  3567. begin
  3568. AddModuleWithIntfImplSrc('unit2.pas',
  3569. LinesToStr([
  3570. 'procedure Now; external name ''Date.now'';',
  3571. 'procedure DoIt;'
  3572. ]),
  3573. 'procedure doit; begin end;');
  3574. StartUnit(true);
  3575. Add('interface');
  3576. Add('uses unit2;');
  3577. Add('implementation');
  3578. Add('begin');
  3579. Add(' now;');
  3580. Add(' now();');
  3581. Add(' uNit2.now;');
  3582. Add(' uNit2.now();');
  3583. Add(' doit;');
  3584. Add(' uNit2.doit;');
  3585. ConvertUnit;
  3586. CheckSource('TestProc_ExternalOtherUnit',
  3587. LinesToStr([
  3588. '']),
  3589. LinesToStr([
  3590. 'Date.now();',
  3591. 'Date.now();',
  3592. 'Date.now();',
  3593. 'Date.now();',
  3594. 'pas.unit2.DoIt();',
  3595. 'pas.unit2.DoIt();',
  3596. '']));
  3597. end;
  3598. procedure TTestModule.TestProc_Asm;
  3599. begin
  3600. StartProgram(false);
  3601. Add([
  3602. '{$mode delphi}',
  3603. 'function DoIt: longint;',
  3604. 'begin;',
  3605. ' asm',
  3606. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3607. ' end;',
  3608. ' asm console.log(); end;',
  3609. ' asm',
  3610. ' s = "'' ";',
  3611. ' s = ''" '';',
  3612. ' s = s + "world" + "''";',
  3613. ' // end',
  3614. ' s = ''end'';',
  3615. ' s = "end";',
  3616. ' s = "foo\"bar";',
  3617. ' s = ''a\''b'';',
  3618. ' s = `${expr}\`-"-''-`;',
  3619. ' s = `multi',
  3620. 'line`;',
  3621. ' end;',
  3622. 'end;',
  3623. 'procedure Fly;',
  3624. 'asm',
  3625. ' return;',
  3626. 'end;',
  3627. 'begin']);
  3628. ConvertProgram;
  3629. CheckSource('TestProc_Asm',
  3630. LinesToStr([ // statements
  3631. 'this.DoIt = function () {',
  3632. ' var Result = 0;',
  3633. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3634. ' console.log();',
  3635. ' s = "'' ";',
  3636. ' s = ''" '';',
  3637. ' s = s + "world" + "''";',
  3638. ' // end',
  3639. ' s = ''end'';',
  3640. ' s = "end";',
  3641. ' s = "foo\"bar";',
  3642. ' s = ''a\''b'';',
  3643. ' s = `${expr}\`-"-''-`;',
  3644. ' s = `multi',
  3645. 'line`;',
  3646. ' return Result;',
  3647. '};',
  3648. 'this.Fly = function () {',
  3649. ' return;',
  3650. '};',
  3651. '']),
  3652. LinesToStr([
  3653. ''
  3654. ]));
  3655. end;
  3656. procedure TTestModule.TestProc_Assembler;
  3657. begin
  3658. StartProgram(false);
  3659. Add('function DoIt: longint; assembler;');
  3660. Add('asm');
  3661. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3662. Add('end;');
  3663. Add('begin');
  3664. ConvertProgram;
  3665. CheckSource('TestProc_Assembler',
  3666. LinesToStr([ // statements
  3667. 'this.DoIt = function () {',
  3668. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3669. '};'
  3670. ]),
  3671. LinesToStr([
  3672. ''
  3673. ]));
  3674. end;
  3675. procedure TTestModule.TestProc_VarParam;
  3676. begin
  3677. StartProgram(false);
  3678. Add('type integer = longint;');
  3679. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3680. Add('var vJ: integer;');
  3681. Add('begin');
  3682. Add(' vg:=vg+1;');
  3683. Add(' vj:=vh+2;');
  3684. Add(' vi:=vi+3;');
  3685. Add(' doit(vg,vg,vg);');
  3686. Add(' doit(vh,vh,vj);');
  3687. Add(' doit(vi,vi,vi);');
  3688. Add(' doit(vj,vj,vj);');
  3689. Add('end;');
  3690. Add('var i: integer;');
  3691. Add('begin');
  3692. Add(' doit(i,i,i);');
  3693. ConvertProgram;
  3694. CheckSource('TestProc_VarParam',
  3695. LinesToStr([ // statements
  3696. 'this.DoIt = function (vG,vH,vI) {',
  3697. ' var vJ = 0;',
  3698. ' vG = vG + 1;',
  3699. ' vJ = vH + 2;',
  3700. ' vI.set(vI.get()+3);',
  3701. ' $mod.DoIt(vG, vG, {',
  3702. ' get: function () {',
  3703. ' return vG;',
  3704. ' },',
  3705. ' set: function (v) {',
  3706. ' vG = v;',
  3707. ' }',
  3708. ' });',
  3709. ' $mod.DoIt(vH, vH, {',
  3710. ' get: function () {',
  3711. ' return vJ;',
  3712. ' },',
  3713. ' set: function (v) {',
  3714. ' vJ = v;',
  3715. ' }',
  3716. ' });',
  3717. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3718. ' $mod.DoIt(vJ, vJ, {',
  3719. ' get: function () {',
  3720. ' return vJ;',
  3721. ' },',
  3722. ' set: function (v) {',
  3723. ' vJ = v;',
  3724. ' }',
  3725. ' });',
  3726. '};',
  3727. 'this.i = 0;'
  3728. ]),
  3729. LinesToStr([
  3730. '$mod.DoIt($mod.i,$mod.i,{',
  3731. ' p: $mod,',
  3732. ' get: function () {',
  3733. ' return this.p.i;',
  3734. ' },',
  3735. ' set: function (v) {',
  3736. ' this.p.i = v;',
  3737. ' }',
  3738. '});'
  3739. ]));
  3740. end;
  3741. procedure TTestModule.TestProc_VarParamString;
  3742. begin
  3743. StartProgram(false);
  3744. Add(['type TCaption = string;',
  3745. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3746. 'var c: char;',
  3747. 'begin',
  3748. ' va[1]:=c;',
  3749. ' vb[2]:=c;',
  3750. ' vc[3]:=c;',
  3751. 'end;',
  3752. 'begin']);
  3753. ConvertProgram;
  3754. CheckSource('TestProc_VarParamString',
  3755. LinesToStr([ // statements
  3756. 'this.DoIt = function (vA,vB,vC) {',
  3757. ' var c = "";',
  3758. ' vA = rtl.setCharAt(vA, 0, c);',
  3759. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3760. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3761. '};',
  3762. '']),
  3763. LinesToStr([
  3764. ]));
  3765. end;
  3766. procedure TTestModule.TestProc_VarParamV;
  3767. begin
  3768. StartProgram(false);
  3769. Add([
  3770. 'procedure Inc2(var i: longint);',
  3771. 'begin',
  3772. ' i:=i+2;',
  3773. 'end;',
  3774. 'procedure DoIt(v: longint);',
  3775. 'var p: array of longint;',
  3776. 'begin',
  3777. ' Inc2(v);',
  3778. ' Inc2(p[v]);',
  3779. 'end;',
  3780. 'begin']);
  3781. ConvertProgram;
  3782. CheckSource('TestProc_VarParamV',
  3783. LinesToStr([ // statements
  3784. 'this.Inc2 = function (i) {',
  3785. ' i.set(i.get()+2);',
  3786. '};',
  3787. 'this.DoIt = function (v) {',
  3788. ' var p = [];',
  3789. ' $mod.Inc2({get: function () {',
  3790. ' return v;',
  3791. ' }, set: function (w) {',
  3792. ' v = w;',
  3793. ' }});',
  3794. ' $mod.Inc2({',
  3795. ' a: v,',
  3796. ' p: p,',
  3797. ' get: function () {',
  3798. ' return this.p[this.a];',
  3799. ' },',
  3800. ' set: function (v) {',
  3801. ' this.p[this.a] = v;',
  3802. ' }',
  3803. ' });',
  3804. '};',
  3805. '']),
  3806. LinesToStr([
  3807. '']));
  3808. end;
  3809. procedure TTestModule.TestProc_Overload;
  3810. begin
  3811. StartProgram(false);
  3812. Add('procedure DoIt(vI: longint); begin end;');
  3813. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3814. Add('procedure DoIt(vD: double); begin end;');
  3815. Add('begin');
  3816. Add(' DoIt(1);');
  3817. Add(' DoIt(2,3);');
  3818. Add(' DoIt(4.5);');
  3819. ConvertProgram;
  3820. CheckSource('TestProcedureOverload',
  3821. LinesToStr([ // statements
  3822. 'this.DoIt = function (vI) {',
  3823. '};',
  3824. 'this.DoIt$1 = function (vI, vJ) {',
  3825. '};',
  3826. 'this.DoIt$2 = function (vD) {',
  3827. '};',
  3828. '']),
  3829. LinesToStr([
  3830. '$mod.DoIt(1);',
  3831. '$mod.DoIt$1(2, 3);',
  3832. '$mod.DoIt$2(4.5);',
  3833. '']));
  3834. end;
  3835. procedure TTestModule.TestProc_OverloadForward;
  3836. begin
  3837. StartProgram(false);
  3838. Add('procedure DoIt(vI: longint); forward;');
  3839. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3840. Add('procedure doit(vi: longint); begin end;');
  3841. Add('begin');
  3842. Add(' doit(1);');
  3843. Add(' doit(2,3);');
  3844. ConvertProgram;
  3845. CheckSource('TestProcedureOverloadForward',
  3846. LinesToStr([ // statements
  3847. 'this.DoIt$1 = function (vI, vJ) {',
  3848. '};',
  3849. 'this.DoIt = function (vI) {',
  3850. '};',
  3851. '']),
  3852. LinesToStr([
  3853. '$mod.DoIt(1);',
  3854. '$mod.DoIt$1(2, 3);',
  3855. '']));
  3856. end;
  3857. procedure TTestModule.TestProc_OverloadIntfImpl;
  3858. begin
  3859. StartUnit(false);
  3860. Add('interface');
  3861. Add('procedure DoIt(vI: longint);');
  3862. Add('procedure DoIt(vI, vJ: longint);');
  3863. Add('implementation');
  3864. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3865. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3866. Add('procedure DoIt(vi: longint); begin end;');
  3867. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3868. Add('procedure DoIt(vi, vj: longint); begin end;');
  3869. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3870. Add('begin');
  3871. Add(' doit(1);');
  3872. Add(' doit(2,3);');
  3873. Add(' doit(4,5,6);');
  3874. Add(' doit(7,8,9,10);');
  3875. Add(' doit(11,12,13,14,15);');
  3876. ConvertUnit;
  3877. CheckSource('TestProcedureOverloadUnit',
  3878. LinesToStr([ // statements
  3879. 'var $impl = $mod.$impl;',
  3880. 'this.DoIt = function (vI) {',
  3881. '};',
  3882. 'this.DoIt$1 = function (vI, vJ) {',
  3883. '};',
  3884. '']),
  3885. LinesToStr([ // this.$init
  3886. '$mod.DoIt(1);',
  3887. '$mod.DoIt$1(2, 3);',
  3888. '$impl.DoIt$3(4,5,6);',
  3889. '$impl.DoIt$4(7,8,9,10);',
  3890. '$impl.DoIt$2(11,12,13,14,15);',
  3891. '']),
  3892. LinesToStr([ // implementation
  3893. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3894. '};',
  3895. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3896. '};',
  3897. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3898. '};',
  3899. '']));
  3900. end;
  3901. procedure TTestModule.TestProc_OverloadNested;
  3902. begin
  3903. StartProgram(false);
  3904. Add([
  3905. 'procedure doit(vA: longint);',
  3906. ' procedure DoIt(vA, vB: longint); overload;',
  3907. ' begin',
  3908. ' doit(1);',
  3909. ' doit(1,2);',
  3910. ' end;',
  3911. ' procedure doit(vA, vB, vC: longint);',
  3912. ' begin',
  3913. ' doit(1);',
  3914. ' doit(1,2);',
  3915. ' doit(1,2,3);',
  3916. ' end;',
  3917. 'begin',
  3918. ' doit(1);',
  3919. ' doit(1,2);',
  3920. ' doit(1,2,3);',
  3921. 'end;',
  3922. 'begin // main',
  3923. ' doit(1);']);
  3924. ConvertProgram;
  3925. CheckSource('TestProcedureOverloadNested',
  3926. LinesToStr([ // statements
  3927. 'this.doit = function (vA) {',
  3928. ' function DoIt$1(vA, vB) {',
  3929. ' $mod.doit(1);',
  3930. ' DoIt$1(1, 2);',
  3931. ' };',
  3932. ' function doit$2(vA, vB, vC) {',
  3933. ' $mod.doit(1);',
  3934. ' DoIt$1(1, 2);',
  3935. ' doit$2(1, 2, 3);',
  3936. ' };',
  3937. ' $mod.doit(1);',
  3938. ' DoIt$1(1, 2);',
  3939. ' doit$2(1, 2, 3);',
  3940. '};',
  3941. '']),
  3942. LinesToStr([
  3943. '$mod.doit(1);',
  3944. '']));
  3945. end;
  3946. procedure TTestModule.TestProc_OverloadNestedForward;
  3947. begin
  3948. StartProgram(false);
  3949. Add([
  3950. 'procedure DoIt(vA: longint); overload; forward;',
  3951. 'procedure DoIt(vB, vC: longint); overload;',
  3952. 'begin // 2 param overload',
  3953. ' doit(1);',
  3954. ' doit(1,2);',
  3955. 'end;',
  3956. 'procedure doit(vA: longint);',
  3957. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3958. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3959. ' begin // 4 param overload',
  3960. ' doit(1);',
  3961. ' doit(1,2);',
  3962. ' doit(1,2,3);',
  3963. ' doit(1,2,3,4);',
  3964. ' end;',
  3965. ' procedure doit(vA, vB, vC: longint);',
  3966. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3967. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3968. ' begin // 6 param overload',
  3969. ' doit(1);',
  3970. ' doit(1,2);',
  3971. ' doit(1,2,3);',
  3972. ' doit(1,2,3,4);',
  3973. ' doit(1,2,3,4,5);',
  3974. ' doit(1,2,3,4,5,6);',
  3975. ' end;',
  3976. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3977. ' begin // 5 param overload',
  3978. ' doit(1);',
  3979. ' doit(1,2);',
  3980. ' doit(1,2,3);',
  3981. ' doit(1,2,3,4);',
  3982. ' doit(1,2,3,4,5);',
  3983. ' doit(1,2,3,4,5,6);',
  3984. ' end;',
  3985. ' begin // 3 param overload',
  3986. ' doit(1);',
  3987. ' doit(1,2);',
  3988. ' doit(1,2,3);',
  3989. ' doit(1,2,3,4);',
  3990. ' doit(1,2,3,4,5);',
  3991. ' doit(1,2,3,4,5,6);',
  3992. ' end;',
  3993. 'begin // 1 param overload',
  3994. ' doit(1);',
  3995. ' doit(1,2);',
  3996. ' doit(1,2,3);',
  3997. ' doit(1,2,3,4);',
  3998. 'end;',
  3999. 'begin // main',
  4000. ' doit(1);',
  4001. ' doit(1,2);']);
  4002. ConvertProgram;
  4003. CheckSource('TestProc_OverloadNestedForward',
  4004. LinesToStr([ // statements
  4005. 'this.DoIt$1 = function (vB, vC) {',
  4006. ' $mod.DoIt(1);',
  4007. ' $mod.DoIt$1(1, 2);',
  4008. '};',
  4009. 'this.DoIt = function (vA) {',
  4010. ' function DoIt$3(vA, vB, vC, vD) {',
  4011. ' $mod.DoIt(1);',
  4012. ' $mod.DoIt$1(1, 2);',
  4013. ' DoIt$2(1, 2, 3);',
  4014. ' DoIt$3(1, 2, 3, 4);',
  4015. ' };',
  4016. ' function DoIt$2(vA, vB, vC) {',
  4017. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4018. ' $mod.DoIt(1);',
  4019. ' $mod.DoIt$1(1, 2);',
  4020. ' DoIt$2(1, 2, 3);',
  4021. ' DoIt$3(1, 2, 3, 4);',
  4022. ' DoIt$4(1, 2, 3, 4, 5);',
  4023. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4024. ' };',
  4025. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4026. ' $mod.DoIt(1);',
  4027. ' $mod.DoIt$1(1, 2);',
  4028. ' DoIt$2(1, 2, 3);',
  4029. ' DoIt$3(1, 2, 3, 4);',
  4030. ' DoIt$4(1, 2, 3, 4, 5);',
  4031. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4032. ' };',
  4033. ' $mod.DoIt(1);',
  4034. ' $mod.DoIt$1(1, 2);',
  4035. ' DoIt$2(1, 2, 3);',
  4036. ' DoIt$3(1, 2, 3, 4);',
  4037. ' DoIt$4(1, 2, 3, 4, 5);',
  4038. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4039. ' };',
  4040. ' $mod.DoIt(1);',
  4041. ' $mod.DoIt$1(1, 2);',
  4042. ' DoIt$2(1, 2, 3);',
  4043. ' DoIt$3(1, 2, 3, 4);',
  4044. '};',
  4045. '']),
  4046. LinesToStr([
  4047. '$mod.DoIt(1);',
  4048. '$mod.DoIt$1(1, 2);',
  4049. '']));
  4050. end;
  4051. procedure TTestModule.TestProc_OverloadUnitCycle;
  4052. begin
  4053. AddModuleWithIntfImplSrc('Unit2.pas',
  4054. LinesToStr([
  4055. 'type',
  4056. ' TObject = class',
  4057. ' procedure DoIt(b: boolean); virtual; abstract;',
  4058. ' procedure DoIt(i: longint); virtual; abstract;',
  4059. ' end;',
  4060. '']),
  4061. 'uses test1;');
  4062. StartUnit(true);
  4063. Add([
  4064. 'interface',
  4065. 'uses unit2;',
  4066. 'type',
  4067. ' TEagle = class(TObject)',
  4068. ' procedure DoIt(b: boolean); override;',
  4069. ' procedure DoIt(i: longint); override;',
  4070. ' end;',
  4071. 'implementation',
  4072. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4073. 'procedure TEagle.DoIt(i: longint); begin end;',
  4074. '']);
  4075. ConvertUnit;
  4076. CheckSource('TestProc_OverloadUnitCycle',
  4077. LinesToStr([ // statements
  4078. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  4079. ' this.DoIt = function (b) {',
  4080. ' };',
  4081. ' this.DoIt$1 = function (i) {',
  4082. ' };',
  4083. '});',
  4084. '']),
  4085. '',
  4086. LinesToStr([
  4087. '']));
  4088. end;
  4089. procedure TTestModule.TestProc_Varargs;
  4090. begin
  4091. StartProgram(false);
  4092. Add([
  4093. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4094. 'procedure ProcB; varargs; external name ''ProcB'';',
  4095. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4096. 'function GetIt: longint; begin end;',
  4097. 'begin',
  4098. ' ProcA(1);',
  4099. ' ProcA(1,2);',
  4100. ' ProcA(1,2.0);',
  4101. ' ProcA(1,2,3);',
  4102. ' ProcA(1,''2'');',
  4103. ' ProcA(2,'''');',
  4104. ' ProcA(3,false);',
  4105. ' ProcB;',
  4106. ' ProcB();',
  4107. ' ProcB(4);',
  4108. ' ProcB(''foo'');',
  4109. ' ProcC;',
  4110. ' ProcC();',
  4111. ' ProcC(4);',
  4112. ' ProcC(5,''foo'');',
  4113. ' ProcB(GetIt);',
  4114. ' ProcB(GetIt());',
  4115. ' ProcB(GetIt,GetIt());']);
  4116. ConvertProgram;
  4117. CheckSource('TestProc_Varargs',
  4118. LinesToStr([ // statements
  4119. 'this.GetIt = function () {',
  4120. ' var Result = 0;',
  4121. ' return Result;',
  4122. '};',
  4123. '']),
  4124. LinesToStr([
  4125. 'ProcA(1);',
  4126. 'ProcA(1, 2);',
  4127. 'ProcA(1, 2.0);',
  4128. 'ProcA(1, 2, 3);',
  4129. 'ProcA(1, "2");',
  4130. 'ProcA(2, "");',
  4131. 'ProcA(3, false);',
  4132. 'ProcB();',
  4133. 'ProcB();',
  4134. 'ProcB(4);',
  4135. 'ProcB("foo");',
  4136. 'ProcC(17);',
  4137. 'ProcC(17);',
  4138. 'ProcC(4);',
  4139. 'ProcC(5, "foo");',
  4140. 'ProcB($mod.GetIt());',
  4141. 'ProcB($mod.GetIt());',
  4142. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4143. '']));
  4144. end;
  4145. procedure TTestModule.TestProc_ConstOrder;
  4146. begin
  4147. StartProgram(false);
  4148. Add([
  4149. 'const A = 3;',
  4150. 'const B = A+1;',
  4151. 'procedure DoIt;',
  4152. 'const C = A+1;',
  4153. 'const D = B+1;',
  4154. 'const E = D+C+B+A;',
  4155. 'begin',
  4156. 'end;',
  4157. 'begin'
  4158. ]);
  4159. ConvertProgram;
  4160. CheckSource('TestProc_ConstOrder',
  4161. LinesToStr([ // statements
  4162. 'this.A = 3;',
  4163. 'this.B = 3 + 1;',
  4164. 'var C = 3 + 1;',
  4165. 'var D = 4 + 1;',
  4166. 'var E = 5 + 4 + 4 + 3;',
  4167. 'this.DoIt = function () {',
  4168. '};',
  4169. '']),
  4170. LinesToStr([
  4171. ''
  4172. ]));
  4173. end;
  4174. procedure TTestModule.TestProc_DuplicateConst;
  4175. begin
  4176. StartProgram(false);
  4177. Add([
  4178. 'const A = 1;',
  4179. 'procedure DoIt;',
  4180. 'const A = 2;',
  4181. ' procedure SubIt;',
  4182. ' const A = 21;',
  4183. ' begin',
  4184. ' end;',
  4185. 'begin',
  4186. 'end;',
  4187. 'procedure DoSome;',
  4188. 'const A = 3;',
  4189. 'begin',
  4190. 'end;',
  4191. 'begin'
  4192. ]);
  4193. ConvertProgram;
  4194. CheckSource('TestProc_DuplicateConst',
  4195. LinesToStr([ // statements
  4196. 'this.A = 1;',
  4197. 'var A$1 = 2;',
  4198. 'var A$2 = 21;',
  4199. 'this.DoIt = function () {',
  4200. ' function SubIt() {',
  4201. ' };',
  4202. '};',
  4203. 'var A$3 = 3;',
  4204. 'this.DoSome = function () {',
  4205. '};',
  4206. '']),
  4207. LinesToStr([
  4208. ''
  4209. ]));
  4210. end;
  4211. procedure TTestModule.TestProc_LocalVarAbsolute;
  4212. begin
  4213. StartProgram(false);
  4214. Add([
  4215. 'type',
  4216. ' TObject = class',
  4217. ' Index: longint;',
  4218. ' procedure DoAbs(Item: pointer);',
  4219. ' end;',
  4220. 'procedure TObject.DoAbs(Item: pointer);',
  4221. 'var',
  4222. ' o: TObject absolute Item;',
  4223. 'begin',
  4224. ' if o.Index<o.Index then o.Index:=o.Index;',
  4225. 'end;',
  4226. 'procedure DoIt(i: longint; p: pointer);',
  4227. 'var',
  4228. ' d: double absolute i;',
  4229. ' s: string absolute d;',
  4230. ' oi: TObject absolute i;',
  4231. ' op: TObject absolute p;',
  4232. 'begin',
  4233. ' if d=d then d:=d;',
  4234. ' if s=s then s:=s;',
  4235. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4236. ' if op.Index=op.Index then op.Index:=op.Index;',
  4237. 'end;',
  4238. 'begin']);
  4239. ConvertProgram;
  4240. CheckSource('TestProc_LocalVarAbsolute',
  4241. LinesToStr([ // statements
  4242. 'rtl.createClass($mod, "TObject", null, function () {',
  4243. ' this.$init = function () {',
  4244. ' this.Index = 0;',
  4245. ' };',
  4246. ' this.$final = function () {',
  4247. ' };',
  4248. ' this.DoAbs = function (Item) {',
  4249. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4250. ' };',
  4251. '});',
  4252. 'this.DoIt = function (i, p) {',
  4253. ' if (i === i) i = i;',
  4254. ' if (i === i) i = i;',
  4255. ' if (i.Index < i.Index) i.Index = i.Index;',
  4256. ' if (p.Index === p.Index) p.Index = p.Index;',
  4257. '};'
  4258. ]),
  4259. LinesToStr([
  4260. ]));
  4261. end;
  4262. procedure TTestModule.TestProc_LocalVarInit;
  4263. begin
  4264. StartProgram(false);
  4265. Add([
  4266. 'type TBytes = array of byte;',
  4267. 'procedure DoIt;',
  4268. 'const c = 4;',
  4269. 'var',
  4270. ' b: byte = 1;',
  4271. ' w: word = 2+c;',
  4272. ' p: pointer = nil;',
  4273. ' Buffer: TBytes = nil;',
  4274. 'begin',
  4275. 'end;',
  4276. 'begin']);
  4277. ConvertProgram;
  4278. CheckSource('TestProc_LocalVarInit',
  4279. LinesToStr([ // statements
  4280. 'var c = 4;',
  4281. 'this.DoIt = function () {',
  4282. ' var b = 1;',
  4283. ' var w = 2 + 4;',
  4284. ' var p = null;',
  4285. ' var Buffer = [];',
  4286. '};',
  4287. '']),
  4288. LinesToStr([
  4289. ]));
  4290. end;
  4291. procedure TTestModule.TestProc_ReservedWords;
  4292. begin
  4293. StartProgram(false);
  4294. Add([
  4295. 'procedure Date(ArrayBuffer: longint);',
  4296. 'const',
  4297. ' NaN: longint = 3;',
  4298. 'var',
  4299. ' &Boolean: longint;',
  4300. ' procedure Error(ArrayBuffer: longint);',
  4301. ' begin',
  4302. ' end;',
  4303. 'begin',
  4304. ' Nan:=&bOolean;',
  4305. 'end;',
  4306. 'begin',
  4307. ' Date(1);']);
  4308. ConvertProgram;
  4309. CheckSource('TestProc_ReservedWords',
  4310. LinesToStr([ // statements
  4311. 'var naN = 3;',
  4312. 'this.Date = function (arrayBuffer) {',
  4313. ' var boolean = 0;',
  4314. ' function error(arrayBuffer) {',
  4315. ' };',
  4316. ' naN = boolean;',
  4317. '};',
  4318. '']),
  4319. LinesToStr([
  4320. ' $mod.Date(1);'
  4321. ]));
  4322. end;
  4323. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4324. begin
  4325. StartProgram(false);
  4326. Add([
  4327. '{$mode objfpc}',
  4328. 'type',
  4329. ' TFunc = reference to function(x: word): word;',
  4330. 'var Func: TFunc;',
  4331. 'procedure DoIt(a: word);',
  4332. 'begin',
  4333. ' Func:=function(b:word): word',
  4334. ' begin',
  4335. ' Result:=a+b;',
  4336. ' exit(b);',
  4337. ' exit(Result);',
  4338. ' end;',// test semicolon
  4339. ' a:=3;',
  4340. 'end;',
  4341. 'begin',
  4342. ' Func:=function(c:word):word begin',
  4343. ' Result:=3+c;',
  4344. ' exit(c);',
  4345. ' exit(Result);',
  4346. ' end;']);
  4347. ConvertProgram;
  4348. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4349. LinesToStr([ // statements
  4350. 'this.Func = null;',
  4351. 'this.DoIt = function (a) {',
  4352. ' $mod.Func = function (b) {',
  4353. ' var Result = 0;',
  4354. ' Result = a + b;',
  4355. ' return b;',
  4356. ' return Result;',
  4357. ' return Result;',
  4358. ' };',
  4359. ' a = 3;',
  4360. '};',
  4361. '']),
  4362. LinesToStr([
  4363. '$mod.Func = function (c) {',
  4364. ' var Result = 0;',
  4365. ' Result = 3 + c;',
  4366. ' return c;',
  4367. ' return Result;',
  4368. ' return Result;',
  4369. '};',
  4370. '']));
  4371. end;
  4372. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4373. begin
  4374. StartProgram(false);
  4375. Add([
  4376. '{$mode delphi}',
  4377. 'type',
  4378. ' TProc = reference to procedure(x: word);',
  4379. 'procedure DoIt(a: word);',
  4380. 'var Proc: TProc;',
  4381. 'begin',
  4382. ' Proc:=procedure(b:word) begin end;',
  4383. 'end;',
  4384. 'var Proc: TProc;',
  4385. 'begin',
  4386. ' Proc:=procedure(c:word) begin end;',
  4387. '']);
  4388. ConvertProgram;
  4389. CheckSource('TestAnonymousProc_Assign_Delphi',
  4390. LinesToStr([ // statements
  4391. 'this.DoIt = function (a) {',
  4392. ' var Proc = null;',
  4393. ' Proc = function (b) {',
  4394. ' };',
  4395. '};',
  4396. 'this.Proc = null;',
  4397. '']),
  4398. LinesToStr([
  4399. '$mod.Proc = function (c) {',
  4400. '};',
  4401. '']));
  4402. end;
  4403. procedure TTestModule.TestAnonymousProc_Arg;
  4404. begin
  4405. StartProgram(false);
  4406. Add([
  4407. 'type',
  4408. ' TProc = reference to procedure;',
  4409. ' TFunc = reference to function(x: word): word;',
  4410. 'procedure DoMore(f,g: TProc);',
  4411. 'begin',
  4412. 'end;',
  4413. 'procedure DoOdd(v: jsvalue);',
  4414. 'begin',
  4415. 'end;',
  4416. 'procedure DoIt(f: TFunc);',
  4417. 'begin',
  4418. ' DoIt(function(b:word): word',
  4419. ' begin',
  4420. ' Result:=1+b;',
  4421. ' end);',
  4422. ' DoMore(procedure begin end, procedure begin end);',
  4423. ' DoOdd(procedure begin end);',
  4424. 'end;',
  4425. 'begin',
  4426. ' DoMore(procedure begin end,',
  4427. ' procedure assembler asm',
  4428. ' console.log("c");',
  4429. ' end);',
  4430. '']);
  4431. ConvertProgram;
  4432. CheckSource('TestAnonymousProc_Arg',
  4433. LinesToStr([ // statements
  4434. 'this.DoMore = function (f, g) {',
  4435. '};',
  4436. 'this.DoOdd = function (v) {',
  4437. '};',
  4438. 'this.DoIt = function (f) {',
  4439. ' $mod.DoIt(function (b) {',
  4440. ' var Result = 0;',
  4441. ' Result = 1 + b;',
  4442. ' return Result;',
  4443. ' });',
  4444. ' $mod.DoMore(function () {',
  4445. ' }, function () {',
  4446. ' });',
  4447. ' $mod.DoOdd(function () {',
  4448. ' });',
  4449. '};',
  4450. '']),
  4451. LinesToStr([
  4452. '$mod.DoMore(function () {',
  4453. '}, function () {',
  4454. ' console.log("c");',
  4455. '});',
  4456. '']));
  4457. end;
  4458. procedure TTestModule.TestAnonymousProc_Typecast;
  4459. begin
  4460. StartProgram(false);
  4461. Add([
  4462. 'type',
  4463. ' TProc = reference to procedure(w: word);',
  4464. ' TArr = array of word;',
  4465. ' TFuncArr = reference to function: TArr;',
  4466. 'procedure DoIt(p: TProc);',
  4467. 'var',
  4468. ' w: word;',
  4469. ' a: TArr;',
  4470. 'begin',
  4471. ' p:=TProc(procedure(b: smallint) begin end);',
  4472. ' a:=TFuncArr(function: TArr begin end)();',
  4473. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4474. 'end;',
  4475. 'begin']);
  4476. ConvertProgram;
  4477. CheckSource('TestAnonymousProc_Typecast',
  4478. LinesToStr([ // statements
  4479. 'this.DoIt = function (p) {',
  4480. ' var w = 0;',
  4481. ' var a = [];',
  4482. ' p = function (b) {',
  4483. ' };',
  4484. ' a = function () {',
  4485. ' var Result = [];',
  4486. ' return Result;',
  4487. ' }();',
  4488. ' w = function () {',
  4489. ' var Result = [];',
  4490. ' return Result;',
  4491. ' }()[3];',
  4492. '};',
  4493. '']),
  4494. LinesToStr([
  4495. '']));
  4496. end;
  4497. procedure TTestModule.TestAnonymousProc_With;
  4498. begin
  4499. StartProgram(false);
  4500. Add([
  4501. 'type',
  4502. ' TProc = reference to procedure(w: word);',
  4503. ' TObject = class',
  4504. ' b: boolean;',
  4505. ' end;',
  4506. 'var',
  4507. ' p: TProc;',
  4508. ' bird: TObject;',
  4509. 'begin',
  4510. ' with bird do',
  4511. ' p:=procedure(w: word)',
  4512. ' begin',
  4513. ' b:=w>2;',
  4514. ' end;',
  4515. '']);
  4516. ConvertProgram;
  4517. CheckSource('TestAnonymousProc_With',
  4518. LinesToStr([ // statements
  4519. 'rtl.createClass($mod, "TObject", null, function () {',
  4520. ' this.$init = function () {',
  4521. ' this.b = false;',
  4522. ' };',
  4523. ' this.$final = function () {',
  4524. ' };',
  4525. '});',
  4526. 'this.p = null;',
  4527. 'this.bird = null;',
  4528. '']),
  4529. LinesToStr([
  4530. 'var $with1 = $mod.bird;',
  4531. '$mod.p = function (w) {',
  4532. ' $with1.b = w > 2;',
  4533. '};',
  4534. '']));
  4535. end;
  4536. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4537. begin
  4538. StartProgram(false);
  4539. Add([
  4540. 'type',
  4541. ' TProc = reference to procedure;',
  4542. ' TObject = class',
  4543. ' b: boolean;',
  4544. ' end;',
  4545. 'procedure DoIt;',
  4546. 'var',
  4547. ' p: TProc;',
  4548. 'begin',
  4549. ' try',
  4550. ' except',
  4551. ' on E: TObject do',
  4552. ' p:=procedure',
  4553. ' begin',
  4554. ' E.b:=true;',
  4555. ' end;',
  4556. ' end;',
  4557. 'end;',
  4558. 'begin']);
  4559. ConvertProgram;
  4560. CheckSource('TestAnonymousProc_ExceptOn',
  4561. LinesToStr([ // statements
  4562. 'rtl.createClass($mod, "TObject", null, function () {',
  4563. ' this.$init = function () {',
  4564. ' this.b = false;',
  4565. ' };',
  4566. ' this.$final = function () {',
  4567. ' };',
  4568. '});',
  4569. 'this.DoIt = function () {',
  4570. ' var p = null;',
  4571. ' try {} catch ($e) {',
  4572. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4573. ' var E = $e;',
  4574. ' p = function () {',
  4575. ' E.b = true;',
  4576. ' };',
  4577. ' } else throw $e',
  4578. ' };',
  4579. '};',
  4580. '']),
  4581. LinesToStr([
  4582. '']));
  4583. end;
  4584. procedure TTestModule.TestAnonymousProc_Nested;
  4585. begin
  4586. StartProgram(false);
  4587. Add([
  4588. 'type',
  4589. ' TProc = reference to procedure;',
  4590. ' TObject = class',
  4591. ' i: byte;',
  4592. ' procedure DoIt;',
  4593. ' end;',
  4594. 'procedure TObject.DoIt;',
  4595. 'var',
  4596. ' p: TProc;',
  4597. ' procedure Sub;',
  4598. ' begin',
  4599. ' p:=procedure',
  4600. ' begin',
  4601. ' i:=3;',
  4602. ' Self.i:=4;',
  4603. ' p:=procedure',
  4604. ' procedure SubSub;',
  4605. ' begin',
  4606. ' i:=13;',
  4607. ' Self.i:=14;',
  4608. ' end;',
  4609. ' begin',
  4610. ' i:=13;',
  4611. ' Self.i:=14;',
  4612. ' end;',
  4613. ' end;',
  4614. ' end;',
  4615. 'begin',
  4616. 'end;',
  4617. 'begin']);
  4618. ConvertProgram;
  4619. CheckSource('TestAnonymousProc_Nested',
  4620. LinesToStr([ // statements
  4621. 'rtl.createClass($mod, "TObject", null, function () {',
  4622. ' this.$init = function () {',
  4623. ' this.i = 0;',
  4624. ' };',
  4625. ' this.$final = function () {',
  4626. ' };',
  4627. ' this.DoIt = function () {',
  4628. ' var $Self = this;',
  4629. ' var p = null;',
  4630. ' function Sub() {',
  4631. ' p = function () {',
  4632. ' $Self.i = 3;',
  4633. ' $Self.i = 4;',
  4634. ' p = function () {',
  4635. ' function SubSub() {',
  4636. ' $Self.i = 13;',
  4637. ' $Self.i = 14;',
  4638. ' };',
  4639. ' $Self.i = 13;',
  4640. ' $Self.i = 14;',
  4641. ' };',
  4642. ' };',
  4643. ' };',
  4644. ' };',
  4645. '});',
  4646. '']),
  4647. LinesToStr([
  4648. '']));
  4649. end;
  4650. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4651. begin
  4652. StartProgram(false);
  4653. Add([
  4654. 'type',
  4655. ' TProc = reference to procedure;',
  4656. 'function DoIt: TProc;',
  4657. ' function Sub: TProc;',
  4658. ' begin',
  4659. ' Result:=procedure',
  4660. ' begin',
  4661. ' Sub:=procedure',
  4662. ' procedure SubSub;',
  4663. ' begin',
  4664. ' Result:=nil;',
  4665. ' Sub:=nil;',
  4666. ' DoIt:=nil;',
  4667. ' end;',
  4668. ' begin',
  4669. ' Result:=nil;',
  4670. ' Sub:=nil;',
  4671. ' DoIt:=nil;',
  4672. ' end;',
  4673. ' end;',
  4674. ' end;',
  4675. 'begin',
  4676. 'end;',
  4677. 'begin']);
  4678. ConvertProgram;
  4679. CheckSource('TestAnonymousProc_NestedAssignResult',
  4680. LinesToStr([ // statements
  4681. 'this.DoIt = function () {',
  4682. ' var Result = null;',
  4683. ' function Sub() {',
  4684. ' var Result$1 = null;',
  4685. ' Result$1 = function () {',
  4686. ' Result$1 = function () {',
  4687. ' function SubSub() {',
  4688. ' Result$1 = null;',
  4689. ' Result$1 = null;',
  4690. ' Result = null;',
  4691. ' };',
  4692. ' Result$1 = null;',
  4693. ' Result$1 = null;',
  4694. ' Result = null;',
  4695. ' };',
  4696. ' };',
  4697. ' return Result$1;',
  4698. ' };',
  4699. ' return Result;',
  4700. '};',
  4701. '']),
  4702. LinesToStr([
  4703. '']));
  4704. end;
  4705. procedure TTestModule.TestAnonymousProc_Class;
  4706. begin
  4707. StartProgram(false);
  4708. Add([
  4709. 'type',
  4710. ' TProc = reference to procedure;',
  4711. ' TEvent = procedure of object;',
  4712. ' TObject = class',
  4713. ' Size: word;',
  4714. ' function GetIt: TProc;',
  4715. ' procedure DoIt; virtual; abstract;',
  4716. ' end;',
  4717. 'function TObject.GetIt: TProc;',
  4718. 'begin',
  4719. ' Result:=procedure',
  4720. ' var p: TEvent;',
  4721. ' begin',
  4722. ' Size:=Size;',
  4723. ' Size:=Self.Size;',
  4724. ' p:=@DoIt;',
  4725. ' p:[email protected];',
  4726. ' end;',
  4727. 'end;',
  4728. 'begin']);
  4729. ConvertProgram;
  4730. CheckSource('TestAnonymousProc_Class',
  4731. LinesToStr([ // statements
  4732. 'rtl.createClass($mod, "TObject", null, function () {',
  4733. ' this.$init = function () {',
  4734. ' this.Size = 0;',
  4735. ' };',
  4736. ' this.$final = function () {',
  4737. ' };',
  4738. ' this.GetIt = function () {',
  4739. ' var $Self = this;',
  4740. ' var Result = null;',
  4741. ' Result = function () {',
  4742. ' var p = null;',
  4743. ' $Self.Size = $Self.Size;',
  4744. ' $Self.Size = $Self.Size;',
  4745. ' p = rtl.createCallback($Self, "DoIt");',
  4746. ' p = rtl.createCallback($Self, "DoIt");',
  4747. ' };',
  4748. ' return Result;',
  4749. ' };',
  4750. '});',
  4751. '']),
  4752. LinesToStr([
  4753. '']));
  4754. end;
  4755. procedure TTestModule.TestAnonymousProc_ForLoop;
  4756. begin
  4757. StartProgram(false);
  4758. Add([
  4759. 'type TProc = reference to procedure;',
  4760. 'procedure Foo(p: TProc);',
  4761. 'begin',
  4762. 'end;',
  4763. 'procedure DoIt;',
  4764. 'var i: word;',
  4765. ' a: word;',
  4766. 'begin',
  4767. ' for i:=1 to 10 do begin',
  4768. ' Foo(procedure begin a:=3; end);',
  4769. ' end;',
  4770. 'end;',
  4771. 'begin',
  4772. ' DoIt;']);
  4773. ConvertProgram;
  4774. CheckSource('TestAnonymousProc_ForLoop',
  4775. LinesToStr([ // statements
  4776. 'this.Foo = function (p) {',
  4777. '};',
  4778. 'this.DoIt = function () {',
  4779. ' var i = 0;',
  4780. ' var a = 0;',
  4781. ' for (i = 1; i <= 10; i++) {',
  4782. ' $mod.Foo(function () {',
  4783. ' a = 3;',
  4784. ' });',
  4785. ' };',
  4786. '};',
  4787. '']),
  4788. LinesToStr([
  4789. '$mod.DoIt();'
  4790. ]));
  4791. end;
  4792. procedure TTestModule.TestEnum_Name;
  4793. begin
  4794. StartProgram(false);
  4795. Add('type TMyEnum = (Red, Green, Blue);');
  4796. Add('var e: TMyEnum;');
  4797. Add('var f: TMyEnum = Blue;');
  4798. Add('begin');
  4799. Add(' e:=green;');
  4800. Add(' e:=default(TMyEnum);');
  4801. ConvertProgram;
  4802. CheckSource('TestEnumName',
  4803. LinesToStr([ // statements
  4804. 'this.TMyEnum = {',
  4805. ' "0":"Red",',
  4806. ' Red:0,',
  4807. ' "1":"Green",',
  4808. ' Green:1,',
  4809. ' "2":"Blue",',
  4810. ' Blue:2',
  4811. ' };',
  4812. 'this.e = 0;',
  4813. 'this.f = $mod.TMyEnum.Blue;'
  4814. ]),
  4815. LinesToStr([
  4816. '$mod.e=$mod.TMyEnum.Green;',
  4817. '$mod.e=$mod.TMyEnum.Red;'
  4818. ]));
  4819. end;
  4820. procedure TTestModule.TestEnum_Number;
  4821. begin
  4822. Converter.Options:=Converter.Options+[coEnumNumbers];
  4823. StartProgram(false);
  4824. Add('type TMyEnum = (Red, Green);');
  4825. Add('var');
  4826. Add(' e: TMyEnum;');
  4827. Add(' f: TMyEnum = Green;');
  4828. Add(' i: longint;');
  4829. Add('begin');
  4830. Add(' e:=green;');
  4831. Add(' i:=longint(e);');
  4832. ConvertProgram;
  4833. CheckSource('TestEnumNumber',
  4834. LinesToStr([ // statements
  4835. 'this.TMyEnum = {',
  4836. ' "0":"Red",',
  4837. ' Red:0,',
  4838. ' "1":"Green",',
  4839. ' Green:1',
  4840. ' };',
  4841. 'this.e = 0;',
  4842. 'this.f = 1;',
  4843. 'this.i = 0;'
  4844. ]),
  4845. LinesToStr([
  4846. '$mod.e=1;',
  4847. '$mod.i=$mod.e;'
  4848. ]));
  4849. end;
  4850. procedure TTestModule.TestEnum_ConstFail;
  4851. begin
  4852. StartProgram(false);
  4853. Add([
  4854. 'type TMyEnum = (Red = 100, Green = 101);',
  4855. 'var',
  4856. ' e: TMyEnum;',
  4857. ' f: TMyEnum = Green;',
  4858. 'begin',
  4859. ' e:=green;']);
  4860. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  4861. ConvertProgram;
  4862. end;
  4863. procedure TTestModule.TestEnum_Functions;
  4864. begin
  4865. StartProgram(false);
  4866. Add([
  4867. 'type TMyEnum = (Red, Green);',
  4868. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4869. 'var',
  4870. ' v: longint;',
  4871. ' s: string;',
  4872. 'begin',
  4873. ' val(s,e,v);',
  4874. ' val(s,e,i);',
  4875. 'end;',
  4876. 'var',
  4877. ' e: TMyEnum;',
  4878. ' i: longint;',
  4879. ' s: string;',
  4880. ' b: boolean;',
  4881. 'begin',
  4882. ' i:=ord(red);',
  4883. ' i:=ord(green);',
  4884. ' i:=ord(e);',
  4885. ' i:=ord(b);',
  4886. ' e:=low(tmyenum);',
  4887. ' e:=low(e);',
  4888. ' b:=low(boolean);',
  4889. ' e:=high(tmyenum);',
  4890. ' e:=high(e);',
  4891. ' b:=high(boolean);',
  4892. ' e:=pred(green);',
  4893. ' e:=pred(e);',
  4894. ' b:=pred(b);',
  4895. ' e:=succ(red);',
  4896. ' e:=succ(e);',
  4897. ' b:=succ(b);',
  4898. ' e:=tmyenum(1);',
  4899. ' e:=tmyenum(i);',
  4900. ' s:=str(e);',
  4901. ' str(e,s);',
  4902. ' str(red,s);',
  4903. ' s:=str(e:3);',
  4904. ' writestr(s,e:3,red);',
  4905. ' val(s,e,i);',
  4906. ' e:=TMyEnum(i);',
  4907. ' i:=longint(e);']);
  4908. ConvertProgram;
  4909. CheckSource('TestEnum_Functions',
  4910. LinesToStr([ // statements
  4911. 'this.TMyEnum = {',
  4912. ' "0":"Red",',
  4913. ' Red:0,',
  4914. ' "1":"Green",',
  4915. ' Green:1',
  4916. ' };',
  4917. 'this.DoIt = function (e, i) {',
  4918. ' var v = 0;',
  4919. ' var s = "";',
  4920. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4921. ' v = w;',
  4922. ' }));',
  4923. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4924. '};',
  4925. 'this.e = 0;',
  4926. 'this.i = 0;',
  4927. 'this.s = "";',
  4928. 'this.b = false;',
  4929. '']),
  4930. LinesToStr([
  4931. '$mod.i=$mod.TMyEnum.Red;',
  4932. '$mod.i=$mod.TMyEnum.Green;',
  4933. '$mod.i=$mod.e;',
  4934. '$mod.i=$mod.b+0;',
  4935. '$mod.e=$mod.TMyEnum.Red;',
  4936. '$mod.e=$mod.TMyEnum.Red;',
  4937. '$mod.b=false;',
  4938. '$mod.e=$mod.TMyEnum.Green;',
  4939. '$mod.e=$mod.TMyEnum.Green;',
  4940. '$mod.b=true;',
  4941. '$mod.e=$mod.TMyEnum.Green-1;',
  4942. '$mod.e=$mod.e-1;',
  4943. '$mod.b=false;',
  4944. '$mod.e=$mod.TMyEnum.Red+1;',
  4945. '$mod.e=$mod.e+1;',
  4946. '$mod.b=true;',
  4947. '$mod.e=1;',
  4948. '$mod.e=$mod.i;',
  4949. '$mod.s = $mod.TMyEnum[$mod.e];',
  4950. '$mod.s = $mod.TMyEnum[$mod.e];',
  4951. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  4952. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  4953. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  4954. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  4955. ' $mod.i = v;',
  4956. '});',
  4957. '$mod.e=$mod.i;',
  4958. '$mod.i=$mod.e;',
  4959. '']));
  4960. end;
  4961. procedure TTestModule.TestEnum_AsParams;
  4962. begin
  4963. StartProgram(false);
  4964. Add('type TEnum = (Red,Blue);');
  4965. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  4966. Add('var vJ: TEnum;');
  4967. Add('begin');
  4968. Add(' vg:=vg;');
  4969. Add(' vj:=vh;');
  4970. Add(' vi:=vi;');
  4971. Add(' doit(vg,vg,vg);');
  4972. Add(' doit(vh,vh,vj);');
  4973. Add(' doit(vi,vi,vi);');
  4974. Add(' doit(vj,vj,vj);');
  4975. Add('end;');
  4976. Add('var i: TEnum;');
  4977. Add('begin');
  4978. Add(' doit(i,i,i);');
  4979. ConvertProgram;
  4980. CheckSource('TestEnum_AsParams',
  4981. LinesToStr([ // statements
  4982. 'this.TEnum = {',
  4983. ' "0": "Red",',
  4984. ' Red: 0,',
  4985. ' "1": "Blue",',
  4986. ' Blue: 1',
  4987. '};',
  4988. 'this.DoIt = function (vG,vH,vI) {',
  4989. ' var vJ = 0;',
  4990. ' vG = vG;',
  4991. ' vJ = vH;',
  4992. ' vI.set(vI.get());',
  4993. ' $mod.DoIt(vG, vG, {',
  4994. ' get: function () {',
  4995. ' return vG;',
  4996. ' },',
  4997. ' set: function (v) {',
  4998. ' vG = v;',
  4999. ' }',
  5000. ' });',
  5001. ' $mod.DoIt(vH, vH, {',
  5002. ' get: function () {',
  5003. ' return vJ;',
  5004. ' },',
  5005. ' set: function (v) {',
  5006. ' vJ = v;',
  5007. ' }',
  5008. ' });',
  5009. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5010. ' $mod.DoIt(vJ, vJ, {',
  5011. ' get: function () {',
  5012. ' return vJ;',
  5013. ' },',
  5014. ' set: function (v) {',
  5015. ' vJ = v;',
  5016. ' }',
  5017. ' });',
  5018. '};',
  5019. 'this.i = 0;'
  5020. ]),
  5021. LinesToStr([
  5022. '$mod.DoIt($mod.i,$mod.i,{',
  5023. ' p: $mod,',
  5024. ' get: function () {',
  5025. ' return this.p.i;',
  5026. ' },',
  5027. ' set: function (v) {',
  5028. ' this.p.i = v;',
  5029. ' }',
  5030. '});'
  5031. ]));
  5032. end;
  5033. procedure TTestModule.TestEnumRange_Array;
  5034. begin
  5035. StartProgram(false);
  5036. Add([
  5037. 'type',
  5038. ' TEnum = (Red, Green, Blue);',
  5039. ' TEnumRg = green..blue;',
  5040. ' TArr = array[TEnumRg] of byte;',
  5041. ' TArr2 = array[green..blue] of byte;',
  5042. 'var',
  5043. ' a: TArr;',
  5044. ' b: TArr = (3,4);',
  5045. ' c: TArr2 = (5,6);',
  5046. 'begin',
  5047. ' a[green] := b[blue];',
  5048. ' c[green] := c[blue];',
  5049. '']);
  5050. ConvertProgram;
  5051. CheckSource('TestEnumRange_Array',
  5052. LinesToStr([ // statements
  5053. 'this.TEnum = {',
  5054. ' "0": "Red",',
  5055. ' Red: 0,',
  5056. ' "1": "Green",',
  5057. ' Green: 1,',
  5058. ' "2": "Blue",',
  5059. ' Blue: 2',
  5060. '};',
  5061. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5062. 'this.b = [3, 4];',
  5063. 'this.c = [5, 6];',
  5064. '']),
  5065. LinesToStr([
  5066. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5067. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5068. '']));
  5069. end;
  5070. procedure TTestModule.TestEnum_ForIn;
  5071. begin
  5072. StartProgram(false);
  5073. Add([
  5074. 'type',
  5075. ' TEnum = (Red, Green, Blue);',
  5076. ' TEnumRg = green..blue;',
  5077. ' TArr = array[TEnum] of byte;',
  5078. ' TArrRg = array[TEnumRg] of byte;',
  5079. 'var',
  5080. ' e: TEnum;',
  5081. ' a1: TArr = (3,4,5);',
  5082. ' a2: TArrRg = (11,12);',
  5083. ' b: byte;',
  5084. 'begin',
  5085. ' for e in TEnum do ;',
  5086. ' for e in TEnumRg do ;',
  5087. ' for e in TArr do ;',
  5088. ' for e in TArrRg do ;',
  5089. ' for b in a1 do ;',
  5090. ' for b in a2 do ;',
  5091. '']);
  5092. ConvertProgram;
  5093. CheckSource('TestEnum_ForIn',
  5094. LinesToStr([ // statements
  5095. 'this.TEnum = {',
  5096. ' "0": "Red",',
  5097. ' Red: 0,',
  5098. ' "1": "Green",',
  5099. ' Green: 1,',
  5100. ' "2": "Blue",',
  5101. ' Blue: 2',
  5102. '};',
  5103. 'this.e = 0;',
  5104. 'this.a1 = [3, 4, 5];',
  5105. 'this.a2 = [11, 12];',
  5106. 'this.b = 0;',
  5107. '']),
  5108. LinesToStr([
  5109. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5110. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5111. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5112. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5113. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  5114. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  5115. '']));
  5116. end;
  5117. procedure TTestModule.TestEnum_ScopedNumber;
  5118. begin
  5119. Converter.Options:=Converter.Options+[coEnumNumbers];
  5120. StartProgram(false);
  5121. Add([
  5122. 'type',
  5123. ' TEnum = (Red, Green);',
  5124. 'var',
  5125. ' e: TEnum;',
  5126. 'begin',
  5127. ' e:=TEnum.Green;',
  5128. '']);
  5129. ConvertProgram;
  5130. CheckSource('TestEnum_ScopedNumber',
  5131. LinesToStr([ // statements
  5132. 'this.TEnum = {',
  5133. ' "0": "Red",',
  5134. ' Red: 0,',
  5135. ' "1": "Green",',
  5136. ' Green: 1',
  5137. '};',
  5138. 'this.e = 0;',
  5139. '']),
  5140. LinesToStr([
  5141. '$mod.e = 1;']));
  5142. end;
  5143. procedure TTestModule.TestEnum_InFunction;
  5144. begin
  5145. StartProgram(false);
  5146. Add([
  5147. 'const TEnum = 3;',
  5148. 'procedure DoIt;',
  5149. 'type',
  5150. ' TEnum = (Red, Green, Blue);',
  5151. ' procedure Sub;',
  5152. ' type',
  5153. ' TEnumSub = (Left, Right);',
  5154. ' var',
  5155. ' es: TEnumSub;',
  5156. ' begin',
  5157. ' es:=Left;',
  5158. ' end;',
  5159. 'var',
  5160. ' e, e2: TEnum;',
  5161. 'begin',
  5162. ' if e in [red,blue] then e2:=e;',
  5163. 'end;',
  5164. 'begin']);
  5165. ConvertProgram;
  5166. CheckSource('TestEnum_InFunction',
  5167. LinesToStr([ // statements
  5168. 'this.TEnum = 3;',
  5169. 'var TEnum$1 = {',
  5170. ' "0":"Red",',
  5171. ' Red:0,',
  5172. ' "1":"Green",',
  5173. ' Green:1,',
  5174. ' "2":"Blue",',
  5175. ' Blue:2',
  5176. ' };',
  5177. 'var TEnumSub = {',
  5178. ' "0": "Left",',
  5179. ' Left: 0,',
  5180. ' "1": "Right",',
  5181. ' Right: 1',
  5182. '};',
  5183. 'this.DoIt = function () {',
  5184. ' function Sub() {',
  5185. ' var es = 0;',
  5186. ' es = TEnumSub.Left;',
  5187. ' };',
  5188. ' var e = 0;',
  5189. ' var e2 = 0;',
  5190. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5191. '};',
  5192. '']),
  5193. LinesToStr([
  5194. '']));
  5195. end;
  5196. procedure TTestModule.TestSet_Enum;
  5197. begin
  5198. StartProgram(false);
  5199. Add([
  5200. 'type',
  5201. ' TColor = (Red, Green, Blue);',
  5202. ' TColors = set of TColor;',
  5203. 'var',
  5204. ' c: TColor;',
  5205. ' s: TColors;',
  5206. ' t: TColors = [];',
  5207. ' u: TColors = [Red];',
  5208. 'begin',
  5209. ' s:=[];',
  5210. ' s:=[Green];',
  5211. ' s:=[Green,Blue];',
  5212. ' s:=[Red..Blue];',
  5213. ' s:=[Red,Green..Blue];',
  5214. ' s:=[Red,c];',
  5215. ' s:=t;',
  5216. ' s:=default(TColors);',
  5217. '']);
  5218. ConvertProgram;
  5219. CheckSource('TestSet',
  5220. LinesToStr([ // statements
  5221. 'this.TColor = {',
  5222. ' "0":"Red",',
  5223. ' Red:0,',
  5224. ' "1":"Green",',
  5225. ' Green:1,',
  5226. ' "2":"Blue",',
  5227. ' Blue:2',
  5228. ' };',
  5229. 'this.c = 0;',
  5230. 'this.s = {};',
  5231. 'this.t = {};',
  5232. 'this.u = rtl.createSet($mod.TColor.Red);'
  5233. ]),
  5234. LinesToStr([
  5235. '$mod.s={};',
  5236. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5237. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5238. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5239. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5240. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5241. '$mod.s=rtl.refSet($mod.t);',
  5242. '$mod.s={};',
  5243. '']));
  5244. end;
  5245. procedure TTestModule.TestSet_Operators;
  5246. begin
  5247. StartProgram(false);
  5248. Add('type');
  5249. Add(' TColor = (Red, Green, Blue);');
  5250. Add(' TColors = set of tcolor;');
  5251. Add('var');
  5252. Add(' vC: TColor;');
  5253. Add(' vS: TColors;');
  5254. Add(' vT: TColors;');
  5255. Add(' vU: TColors;');
  5256. Add(' B: boolean;');
  5257. Add('begin');
  5258. Add(' include(vs,green);');
  5259. Add(' exclude(vs,vc);');
  5260. Add(' vs:=vt+vu;');
  5261. Add(' vs:=vt+[red];');
  5262. Add(' vs:=[red]+vt;');
  5263. Add(' vs:=[red]+[green];');
  5264. Add(' vs:=vt-vu;');
  5265. Add(' vs:=vt-[red];');
  5266. Add(' vs:=[red]-vt;');
  5267. Add(' vs:=[red]-[green];');
  5268. Add(' vs:=vt*vu;');
  5269. Add(' vs:=vt*[red];');
  5270. Add(' vs:=[red]*vt;');
  5271. Add(' vs:=[red]*[green];');
  5272. Add(' vs:=vt><vu;');
  5273. Add(' vs:=vt><[red];');
  5274. Add(' vs:=[red]><vt;');
  5275. Add(' vs:=[red]><[green];');
  5276. Add(' b:=vt=vu;');
  5277. Add(' b:=vt=[red];');
  5278. Add(' b:=[red]=vt;');
  5279. Add(' b:=[red]=[green];');
  5280. Add(' b:=vt<>vu;');
  5281. Add(' b:=vt<>[red];');
  5282. Add(' b:=[red]<>vt;');
  5283. Add(' b:=[red]<>[green];');
  5284. Add(' b:=vt<=vu;');
  5285. Add(' b:=vt<=[red];');
  5286. Add(' b:=[red]<=vt;');
  5287. Add(' b:=[red]<=[green];');
  5288. Add(' b:=vt>=vu;');
  5289. Add(' b:=vt>=[red];');
  5290. Add(' b:=[red]>=vt;');
  5291. Add(' b:=[red]>=[green];');
  5292. ConvertProgram;
  5293. CheckSource('TestSet_Operators',
  5294. LinesToStr([ // statements
  5295. 'this.TColor = {',
  5296. ' "0":"Red",',
  5297. ' Red:0,',
  5298. ' "1":"Green",',
  5299. ' Green:1,',
  5300. ' "2":"Blue",',
  5301. ' Blue:2',
  5302. ' };',
  5303. 'this.vC = 0;',
  5304. 'this.vS = {};',
  5305. 'this.vT = {};',
  5306. 'this.vU = {};',
  5307. 'this.B = false;'
  5308. ]),
  5309. LinesToStr([
  5310. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5311. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5312. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5313. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5314. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5315. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5316. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5317. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5318. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5319. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5320. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5321. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5322. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5323. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5324. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5325. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5326. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5327. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5328. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5329. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5330. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5331. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5332. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5333. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5334. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5335. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5336. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5337. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5338. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5339. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5340. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5341. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5342. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5343. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5344. '']));
  5345. end;
  5346. procedure TTestModule.TestSet_Operator_In;
  5347. begin
  5348. StartProgram(false);
  5349. Add('type');
  5350. Add(' TColor = (Red, Green, Blue);');
  5351. Add(' TColors = set of tcolor;');
  5352. Add('var');
  5353. Add(' vC: tcolor;');
  5354. Add(' vT: tcolors;');
  5355. Add(' B: boolean;');
  5356. Add('begin');
  5357. Add(' b:=red in vt;');
  5358. Add(' b:=vc in vt;');
  5359. Add(' b:=green in [red..blue];');
  5360. Add(' b:=vc in [red..blue];');
  5361. Add(' ');
  5362. Add(' if red in vt then ;');
  5363. Add(' while vC in vt do ;');
  5364. Add(' repeat');
  5365. Add(' until vC in vt;');
  5366. ConvertProgram;
  5367. CheckSource('TestSet_Operator_In',
  5368. LinesToStr([ // statements
  5369. 'this.TColor = {',
  5370. ' "0":"Red",',
  5371. ' Red:0,',
  5372. ' "1":"Green",',
  5373. ' Green:1,',
  5374. ' "2":"Blue",',
  5375. ' Blue:2',
  5376. ' };',
  5377. 'this.vC = 0;',
  5378. 'this.vT = {};',
  5379. 'this.B = false;'
  5380. ]),
  5381. LinesToStr([
  5382. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5383. '$mod.B = $mod.vC in $mod.vT;',
  5384. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5385. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5386. 'if ($mod.TColor.Red in $mod.vT) ;',
  5387. 'while ($mod.vC in $mod.vT) {',
  5388. '};',
  5389. 'do {',
  5390. '} while (!($mod.vC in $mod.vT));',
  5391. '']));
  5392. end;
  5393. procedure TTestModule.TestSet_Functions;
  5394. begin
  5395. StartProgram(false);
  5396. Add('type');
  5397. Add(' TMyEnum = (Red, Green);');
  5398. Add(' TMyEnums = set of TMyEnum;');
  5399. Add('var');
  5400. Add(' e: TMyEnum;');
  5401. Add(' s: TMyEnums;');
  5402. Add('begin');
  5403. Add(' e:=Low(TMyEnums);');
  5404. Add(' e:=Low(s);');
  5405. Add(' e:=High(TMyEnums);');
  5406. Add(' e:=High(s);');
  5407. ConvertProgram;
  5408. CheckSource('TestSetFunctions',
  5409. LinesToStr([ // statements
  5410. 'this.TMyEnum = {',
  5411. ' "0":"Red",',
  5412. ' Red:0,',
  5413. ' "1":"Green",',
  5414. ' Green:1',
  5415. ' };',
  5416. 'this.e = 0;',
  5417. 'this.s = {};'
  5418. ]),
  5419. LinesToStr([
  5420. '$mod.e=$mod.TMyEnum.Red;',
  5421. '$mod.e=$mod.TMyEnum.Red;',
  5422. '$mod.e=$mod.TMyEnum.Green;',
  5423. '$mod.e=$mod.TMyEnum.Green;',
  5424. '']));
  5425. end;
  5426. procedure TTestModule.TestSet_PassAsArgClone;
  5427. begin
  5428. StartProgram(false);
  5429. Add('type');
  5430. Add(' TMyEnum = (Red, Green);');
  5431. Add(' TMyEnums = set of TMyEnum;');
  5432. Add('procedure DoDefault(s: tmyenums); begin end;');
  5433. Add('procedure DoConst(const s: tmyenums); begin end;');
  5434. Add('var');
  5435. Add(' aSet: tmyenums;');
  5436. Add('begin');
  5437. Add(' dodefault(aset);');
  5438. Add(' doconst(aset);');
  5439. ConvertProgram;
  5440. CheckSource('TestSetFunctions',
  5441. LinesToStr([ // statements
  5442. 'this.TMyEnum = {',
  5443. ' "0":"Red",',
  5444. ' Red:0,',
  5445. ' "1":"Green",',
  5446. ' Green:1',
  5447. ' };',
  5448. 'this.DoDefault = function (s) {',
  5449. '};',
  5450. 'this.DoConst = function (s) {',
  5451. '};',
  5452. 'this.aSet = {};'
  5453. ]),
  5454. LinesToStr([
  5455. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5456. '$mod.DoConst($mod.aSet);',
  5457. '']));
  5458. end;
  5459. procedure TTestModule.TestSet_AsParams;
  5460. begin
  5461. StartProgram(false);
  5462. Add([
  5463. 'type TEnum = (Red,Blue);',
  5464. 'type TEnums = set of TEnum;',
  5465. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5466. 'var vJ: TEnums;',
  5467. 'begin',
  5468. ' Include(vg,red);',
  5469. ' Include(result,blue);',
  5470. ' vg:=vg;',
  5471. ' vj:=vh;',
  5472. ' vi:=vi;',
  5473. ' doit(vg,vg,vg);',
  5474. ' doit(vh,vh,vj);',
  5475. ' doit(vi,vi,vi);',
  5476. ' doit(vj,vj,vj);',
  5477. 'end;',
  5478. 'var i: TEnums;',
  5479. 'begin',
  5480. ' doit(i,i,i);']);
  5481. ConvertProgram;
  5482. CheckSource('TestSet_AsParams',
  5483. LinesToStr([ // statements
  5484. 'this.TEnum = {',
  5485. ' "0": "Red",',
  5486. ' Red: 0,',
  5487. ' "1": "Blue",',
  5488. ' Blue: 1',
  5489. '};',
  5490. 'this.DoIt = function (vG,vH,vI) {',
  5491. ' var Result = {};',
  5492. ' var vJ = {};',
  5493. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5494. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5495. ' vG = rtl.refSet(vG);',
  5496. ' vJ = rtl.refSet(vH);',
  5497. ' vI.set(rtl.refSet(vI.get()));',
  5498. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5499. ' get: function () {',
  5500. ' return vG;',
  5501. ' },',
  5502. ' set: function (v) {',
  5503. ' vG = v;',
  5504. ' }',
  5505. ' });',
  5506. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5507. ' get: function () {',
  5508. ' return vJ;',
  5509. ' },',
  5510. ' set: function (v) {',
  5511. ' vJ = v;',
  5512. ' }',
  5513. ' });',
  5514. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5515. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5516. ' get: function () {',
  5517. ' return vJ;',
  5518. ' },',
  5519. ' set: function (v) {',
  5520. ' vJ = v;',
  5521. ' }',
  5522. ' });',
  5523. ' return Result;',
  5524. '};',
  5525. 'this.i = {};'
  5526. ]),
  5527. LinesToStr([
  5528. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5529. ' p: $mod,',
  5530. ' get: function () {',
  5531. ' return this.p.i;',
  5532. ' },',
  5533. ' set: function (v) {',
  5534. ' this.p.i = v;',
  5535. ' }',
  5536. '});'
  5537. ]));
  5538. end;
  5539. procedure TTestModule.TestSet_Property;
  5540. begin
  5541. StartProgram(false);
  5542. Add('type');
  5543. Add(' TEnum = (Red,Blue);');
  5544. Add(' TEnums = set of TEnum;');
  5545. Add(' TObject = class');
  5546. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5547. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5548. Add(' property Colors: TEnums read GetColors write SetColors;');
  5549. Add(' end;');
  5550. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5551. Add('begin end;');
  5552. Add('var Obj: TObject;');
  5553. Add('begin');
  5554. Add(' Include(Obj.Colors,Red);');
  5555. Add(' Exclude(Obj.Colors,Red);');
  5556. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5557. ConvertProgram;
  5558. CheckSource('TestSet_Property',
  5559. LinesToStr([ // statements
  5560. 'this.TEnum = {',
  5561. ' "0": "Red",',
  5562. ' Red: 0,',
  5563. ' "1": "Blue",',
  5564. ' Blue: 1',
  5565. '};',
  5566. 'rtl.createClass($mod, "TObject", null, function () {',
  5567. ' this.$init = function () {',
  5568. ' };',
  5569. ' this.$final = function () {',
  5570. ' };',
  5571. '});',
  5572. 'this.DoIt = function (i, j, k, l) {',
  5573. '};',
  5574. 'this.Obj = null;',
  5575. '']),
  5576. LinesToStr([
  5577. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5578. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5579. '']));
  5580. end;
  5581. procedure TTestModule.TestSet_EnumConst;
  5582. begin
  5583. StartProgram(false);
  5584. Add([
  5585. 'type',
  5586. ' TEnum = (Red,Blue);',
  5587. ' TEnums = set of TEnum;',
  5588. 'const',
  5589. ' Orange = red;',
  5590. 'var',
  5591. ' Enum: tenum;',
  5592. ' Enums: tenums;',
  5593. 'begin',
  5594. ' Include(enums,orange);',
  5595. ' Exclude(enums,orange);',
  5596. ' if orange in enums then;',
  5597. ' if orange in [orange,red] then;']);
  5598. ConvertProgram;
  5599. CheckSource('TestSet_EnumConst',
  5600. LinesToStr([ // statements
  5601. 'this.TEnum = {',
  5602. ' "0": "Red",',
  5603. ' Red: 0,',
  5604. ' "1": "Blue",',
  5605. ' Blue: 1',
  5606. '};',
  5607. 'this.Orange = $mod.TEnum.Red;',
  5608. 'this.Enum = 0;',
  5609. 'this.Enums = {};',
  5610. '']),
  5611. LinesToStr([
  5612. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5613. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5614. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5615. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5616. '']));
  5617. end;
  5618. procedure TTestModule.TestSet_IntConst;
  5619. begin
  5620. StartProgram(false);
  5621. Add([
  5622. 'type',
  5623. ' TEnums = set of Byte;',
  5624. 'const',
  5625. ' Orange = 0;',
  5626. 'var',
  5627. ' Enum: byte;',
  5628. ' Enums: tenums;',
  5629. 'begin',
  5630. ' Enums:=[];',
  5631. ' Enums:=[0];',
  5632. ' Enums:=[1..2];',
  5633. //' Include(enums,orange);',
  5634. //' Exclude(enums,orange);',
  5635. ' if orange in enums then;',
  5636. ' if orange in [orange,1] then;']);
  5637. ConvertProgram;
  5638. CheckSource('TestSet_IntConst',
  5639. LinesToStr([ // statements
  5640. 'this.Orange = 0;',
  5641. 'this.Enum = 0;',
  5642. 'this.Enums = {};',
  5643. '']),
  5644. LinesToStr([
  5645. '$mod.Enums = {};',
  5646. '$mod.Enums = rtl.createSet(0);',
  5647. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5648. 'if (0 in $mod.Enums) ;',
  5649. 'if (0 in rtl.createSet(0, 1)) ;',
  5650. '']));
  5651. end;
  5652. procedure TTestModule.TestSet_AnonymousEnumType;
  5653. begin
  5654. StartProgram(false);
  5655. Add('type');
  5656. Add(' TFlags = set of (red, green);');
  5657. Add('const');
  5658. Add(' favorite = red;');
  5659. Add('var');
  5660. Add(' f: TFlags;');
  5661. Add(' i: longint;');
  5662. Add('begin');
  5663. Add(' Include(f,red);');
  5664. Add(' Include(f,favorite);');
  5665. Add(' i:=ord(red);');
  5666. Add(' i:=ord(favorite);');
  5667. Add(' i:=ord(low(TFlags));');
  5668. Add(' i:=ord(low(f));');
  5669. Add(' i:=ord(low(favorite));');
  5670. Add(' i:=ord(high(TFlags));');
  5671. Add(' i:=ord(high(f));');
  5672. Add(' i:=ord(high(favorite));');
  5673. Add(' f:=[green,favorite];');
  5674. ConvertProgram;
  5675. CheckSource('TestSet_AnonymousEnumType',
  5676. LinesToStr([ // statements
  5677. 'this.TFlags$a = {',
  5678. ' "0": "red",',
  5679. ' red: 0,',
  5680. ' "1": "green",',
  5681. ' green: 1',
  5682. '};',
  5683. 'this.favorite = $mod.TFlags$a.red;',
  5684. 'this.f = {};',
  5685. 'this.i = 0;',
  5686. '']),
  5687. LinesToStr([
  5688. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5689. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5690. '$mod.i = $mod.TFlags$a.red;',
  5691. '$mod.i = $mod.TFlags$a.red;',
  5692. '$mod.i = $mod.TFlags$a.red;',
  5693. '$mod.i = $mod.TFlags$a.red;',
  5694. '$mod.i = $mod.TFlags$a.red;',
  5695. '$mod.i = $mod.TFlags$a.green;',
  5696. '$mod.i = $mod.TFlags$a.green;',
  5697. '$mod.i = $mod.TFlags$a.green;',
  5698. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5699. '']));
  5700. end;
  5701. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5702. begin
  5703. exit;
  5704. StartProgram(false);
  5705. Add([
  5706. 'type',
  5707. ' TAtoZ = ''A''..''Z'';',
  5708. ' TSetOfAZ = set of TAtoZ;',
  5709. 'var',
  5710. ' c: char;',
  5711. ' a: TAtoZ;',
  5712. ' s: TSetOfAZ = [''P'',''A''];',
  5713. ' i: longint;',
  5714. 'begin',
  5715. ' Include(s,''S'');',
  5716. ' Include(s,c);',
  5717. ' Include(s,a);',
  5718. ' c:=low(TAtoZ);',
  5719. ' i:=ord(low(TAtoZ));',
  5720. ' a:=high(TAtoZ);',
  5721. ' a:=high(TSetOfAtoZ);',
  5722. ' s:=[a,c,''M''];',
  5723. '']);
  5724. ConvertProgram;
  5725. CheckSource('TestSet_AnonymousEnumTypeChar',
  5726. LinesToStr([ // statements
  5727. '']),
  5728. LinesToStr([
  5729. '']));
  5730. end;
  5731. procedure TTestModule.TestSet_ConstEnum;
  5732. begin
  5733. StartProgram(false);
  5734. Add([
  5735. 'type',
  5736. ' TEnum = (red,blue,green);',
  5737. ' TEnums = set of TEnum;',
  5738. 'const',
  5739. ' teAny = [low(TEnum)..high(TEnum)];',
  5740. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5741. 'var',
  5742. ' e: TEnum;',
  5743. ' s: TEnums;',
  5744. 'begin',
  5745. ' if blue in teAny then;',
  5746. ' if blue in teAny+[e] then;',
  5747. ' if blue in teAny+teRedBlue then;',
  5748. ' if e in [red,blue] then;',
  5749. ' s:=teAny;',
  5750. ' s:=teAny+[e];',
  5751. ' s:=[e]+teAny;',
  5752. ' s:=teAny+teRedBlue;',
  5753. ' s:=teAny+teRedBlue+[e];',
  5754. '']);
  5755. ConvertProgram;
  5756. CheckSource('TestSet_ConstEnum',
  5757. LinesToStr([ // statements
  5758. 'this.TEnum = {',
  5759. ' "0": "red",',
  5760. ' red: 0,',
  5761. ' "1": "blue",',
  5762. ' blue: 1,',
  5763. ' "2": "green",',
  5764. ' green: 2',
  5765. '};',
  5766. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5767. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5768. 'this.e = 0;',
  5769. 'this.s = {};',
  5770. '']),
  5771. LinesToStr([
  5772. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5773. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5774. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5775. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5776. '$mod.s = rtl.refSet($mod.teAny);',
  5777. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5778. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5779. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5780. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5781. '']));
  5782. end;
  5783. procedure TTestModule.TestSet_ConstChar;
  5784. begin
  5785. StartProgram(false);
  5786. Add([
  5787. 'const',
  5788. ' LowChars = [''a''..''z''];',
  5789. ' Chars = LowChars+[''A''..''Z''];',
  5790. ' sc = [''А'', ''Я''];',
  5791. 'var',
  5792. ' c: char;',
  5793. ' s: string;',
  5794. 'begin',
  5795. ' if c in lowchars then ;',
  5796. ' if ''a'' in lowchars then ;',
  5797. ' if s[1] in lowchars then ;',
  5798. ' if c in chars then ;',
  5799. ' if c in [''a''..''z'',''_''] then ;',
  5800. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5801. ' if ''Я'' in sc then ;',
  5802. ' if 3=ord('' '') then ;',
  5803. '']);
  5804. ConvertProgram;
  5805. CheckSource('TestSet_ConstChar',
  5806. LinesToStr([ // statements
  5807. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5808. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5809. 'this.sc = rtl.createSet(1040, 1071);',
  5810. 'this.c = "";',
  5811. 'this.s = "";',
  5812. '']),
  5813. LinesToStr([
  5814. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5815. 'if (97 in $mod.LowChars) ;',
  5816. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5817. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5818. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5819. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5820. 'if (1071 in $mod.sc) ;',
  5821. 'if (3 === 32) ;',
  5822. '']));
  5823. end;
  5824. procedure TTestModule.TestSet_ConstInt;
  5825. begin
  5826. StartProgram(false);
  5827. Add([
  5828. 'const',
  5829. ' Months = [1..12];',
  5830. ' Mirror = [-12..-1]+Months;',
  5831. 'var',
  5832. ' i: smallint;',
  5833. 'begin',
  5834. ' if 3 in Months then;',
  5835. ' if i in Months+[i] then;',
  5836. ' if i in Months+Mirror then;',
  5837. ' if i in [4..6,8] then;',
  5838. '']);
  5839. ConvertProgram;
  5840. CheckSource('TestSet_ConstInt',
  5841. LinesToStr([ // statements
  5842. 'this.Months = rtl.createSet(null, 1, 12);',
  5843. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5844. 'this.i = 0;',
  5845. '']),
  5846. LinesToStr([
  5847. 'if (3 in $mod.Months) ;',
  5848. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5849. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5850. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5851. '']));
  5852. end;
  5853. procedure TTestModule.TestSet_InFunction;
  5854. begin
  5855. StartProgram(false);
  5856. Add([
  5857. 'const',
  5858. ' TEnum = 3;',
  5859. ' TSetOfEnum = 4;',
  5860. ' TSetOfAno = 5;',
  5861. 'procedure DoIt;',
  5862. 'type',
  5863. ' TEnum = (red, blue);',
  5864. ' TSetOfEnum = set of TEnum;',
  5865. ' TSetOfAno = set of (up,down);',
  5866. 'var',
  5867. ' e: TEnum;',
  5868. ' se: TSetOfEnum;',
  5869. ' sa: TSetOfAno;',
  5870. 'begin',
  5871. ' se:=[e];',
  5872. ' sa:=[up];',
  5873. 'end;',
  5874. 'begin',
  5875. '']);
  5876. ConvertProgram;
  5877. CheckSource('TestSet_InFunction',
  5878. LinesToStr([ // statements
  5879. 'this.TEnum = 3;',
  5880. 'this.TSetOfEnum = 4;',
  5881. 'this.TSetOfAno = 5;',
  5882. 'var TEnum$1 = {',
  5883. ' "0": "red",',
  5884. ' red: 0,',
  5885. ' "1": "blue",',
  5886. ' blue: 1',
  5887. '};',
  5888. 'var TSetOfAno$a = {',
  5889. ' "0": "up",',
  5890. ' up: 0,',
  5891. ' "1": "down",',
  5892. ' down: 1',
  5893. '};',
  5894. 'this.DoIt = function () {',
  5895. ' var e = 0;',
  5896. ' var se = {};',
  5897. ' var sa = {};',
  5898. ' se = rtl.createSet(e);',
  5899. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5900. '};',
  5901. '']),
  5902. LinesToStr([
  5903. '']));
  5904. end;
  5905. procedure TTestModule.TestSet_ForIn;
  5906. begin
  5907. StartProgram(false);
  5908. Add([
  5909. 'type',
  5910. ' TEnum = (Red, Green, Blue);',
  5911. ' TEnumRg = green..blue;',
  5912. ' TSetOfEnum = set of TEnum;',
  5913. ' TSetOfEnumRg = set of TEnumRg;',
  5914. 'var',
  5915. ' e, e2: TEnum;',
  5916. ' er: TEnum;',
  5917. ' s: TSetOfEnum;',
  5918. 'begin',
  5919. ' for e in TSetOfEnum do ;',
  5920. ' for e in TSetOfEnumRg do ;',
  5921. ' for e in [] do e2:=e;',
  5922. ' for e in [red..green] do e2:=e;',
  5923. ' for e in [green,blue] do e2:=e;',
  5924. ' for e in [red,blue] do e2:=e;',
  5925. ' for e in s do e2:=e;',
  5926. ' for er in TSetOfEnumRg do ;',
  5927. '']);
  5928. ConvertProgram;
  5929. CheckSource('TestSet_ForIn',
  5930. LinesToStr([ // statements
  5931. 'this.TEnum = {',
  5932. ' "0":"Red",',
  5933. ' Red:0,',
  5934. ' "1":"Green",',
  5935. ' Green:1,',
  5936. ' "2":"Blue",',
  5937. ' Blue:2',
  5938. ' };',
  5939. 'this.e = 0;',
  5940. 'this.e2 = 0;',
  5941. 'this.er = 0;',
  5942. 'this.s = {};',
  5943. '']),
  5944. LinesToStr([
  5945. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5946. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5947. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5948. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5949. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5950. 'for (var $l1 in $mod.s){',
  5951. ' $mod.e = +$l1;',
  5952. ' $mod.e2 = $mod.e;',
  5953. '};',
  5954. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  5955. '']));
  5956. end;
  5957. procedure TTestModule.TestNestBegin;
  5958. begin
  5959. StartProgram(false);
  5960. Add('begin');
  5961. Add(' begin');
  5962. Add(' begin');
  5963. Add(' end;');
  5964. Add(' begin');
  5965. Add(' if true then ;');
  5966. Add(' end;');
  5967. Add(' end;');
  5968. ConvertProgram;
  5969. CheckSource('TestNestBegin',
  5970. '',
  5971. 'if (true) ;');
  5972. end;
  5973. procedure TTestModule.TestUnitImplVars;
  5974. begin
  5975. StartUnit(false);
  5976. Add('interface');
  5977. Add('implementation');
  5978. Add('var');
  5979. Add(' V1:longint;');
  5980. Add(' V2:longint = 3;');
  5981. Add(' V3:string = ''abc'';');
  5982. ConvertUnit;
  5983. CheckSource('TestUnitImplVars',
  5984. LinesToStr([ // statements
  5985. 'var $impl = $mod.$impl;',
  5986. '']),
  5987. '', // this.$init
  5988. LinesToStr([ // implementation
  5989. '$impl.V1 = 0;',
  5990. '$impl.V2 = 3;',
  5991. '$impl.V3 = "abc";',
  5992. '']) );
  5993. end;
  5994. procedure TTestModule.TestUnitImplConsts;
  5995. begin
  5996. StartUnit(false);
  5997. Add('interface');
  5998. Add('implementation');
  5999. Add('const');
  6000. Add(' v1 = 3;');
  6001. Add(' v2:longint = 4;');
  6002. Add(' v3:string = ''abc'';');
  6003. ConvertUnit;
  6004. CheckSource('TestUnitImplConsts',
  6005. LinesToStr([ // statements
  6006. 'var $impl = $mod.$impl;',
  6007. '']),
  6008. '', // this.$init
  6009. LinesToStr([ // implementation
  6010. '$impl.v1 = 3;',
  6011. '$impl.v2 = 4;',
  6012. '$impl.v3 = "abc";',
  6013. '']) );
  6014. end;
  6015. procedure TTestModule.TestUnitImplRecord;
  6016. begin
  6017. StartUnit(false);
  6018. Add('interface');
  6019. Add('implementation');
  6020. Add('type');
  6021. Add(' TMyRecord = record');
  6022. Add(' i: longint;');
  6023. Add(' end;');
  6024. Add('var aRec: TMyRecord;');
  6025. Add('initialization');
  6026. Add(' arec.i:=3;');
  6027. ConvertUnit;
  6028. CheckSource('TestUnitImplRecord',
  6029. LinesToStr([ // statements
  6030. 'var $impl = $mod.$impl;',
  6031. '']),
  6032. // this.$init
  6033. '$impl.aRec.i = 3;',
  6034. LinesToStr([ // implementation
  6035. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6036. ' this.i = 0;',
  6037. ' this.$eq = function (b) {',
  6038. ' return this.i === b.i;',
  6039. ' };',
  6040. ' this.$assign = function (s) {',
  6041. ' this.i = s.i;',
  6042. ' return this;',
  6043. ' };',
  6044. '});',
  6045. '$impl.aRec = $impl.TMyRecord.$new();',
  6046. '']) );
  6047. end;
  6048. procedure TTestModule.TestRenameJSNameConflict;
  6049. begin
  6050. StartProgram(false);
  6051. Add('var apply: longint;');
  6052. Add('var bind: longint;');
  6053. Add('var call: longint;');
  6054. Add('begin');
  6055. ConvertProgram;
  6056. CheckSource('TestRenameJSNameConflict',
  6057. LinesToStr([ // statements
  6058. 'this.Apply = 0;',
  6059. 'this.Bind = 0;',
  6060. 'this.Call = 0;'
  6061. ]),
  6062. LinesToStr([ // this.$main
  6063. ''
  6064. ]));
  6065. end;
  6066. procedure TTestModule.TestLocalConst;
  6067. begin
  6068. StartProgram(false);
  6069. Add('procedure DoIt;');
  6070. Add('const');
  6071. Add(' cA: longint = 1;');
  6072. Add(' cB = 2;');
  6073. Add(' procedure Sub;');
  6074. Add(' const');
  6075. Add(' csA = 3;');
  6076. Add(' cB: double = 4;');
  6077. Add(' begin');
  6078. Add(' cb:=cb+csa;');
  6079. Add(' ca:=ca+csa+5;');
  6080. Add(' end;');
  6081. Add('begin');
  6082. Add(' ca:=ca+cb+6;');
  6083. Add('end;');
  6084. Add('begin');
  6085. ConvertProgram;
  6086. CheckSource('TestLocalConst',
  6087. LinesToStr([
  6088. 'var cA = 1;',
  6089. 'var cB = 2;',
  6090. 'var csA = 3;',
  6091. 'var cB$1 = 4;',
  6092. 'this.DoIt = function () {',
  6093. ' function Sub() {',
  6094. ' cB$1 = cB$1 + 3;',
  6095. ' cA = cA + 3 + 5;',
  6096. ' };',
  6097. ' cA = cA + 2 + 6;',
  6098. '};'
  6099. ]),
  6100. LinesToStr([
  6101. ]));
  6102. end;
  6103. procedure TTestModule.TestVarExternal;
  6104. begin
  6105. StartProgram(false);
  6106. Add('var');
  6107. Add(' NaN: double; external name ''Global.NaN'';');
  6108. Add(' d: double;');
  6109. Add('begin');
  6110. Add(' d:=NaN;');
  6111. ConvertProgram;
  6112. CheckSource('TestVarExternal',
  6113. LinesToStr([
  6114. 'this.d = 0.0;'
  6115. ]),
  6116. LinesToStr([
  6117. '$mod.d = Global.NaN;'
  6118. ]));
  6119. end;
  6120. procedure TTestModule.TestVarExternalOtherUnit;
  6121. begin
  6122. AddModuleWithIntfImplSrc('unit2.pas',
  6123. LinesToStr([
  6124. 'var NaN: double; external name ''Global.NaN'';',
  6125. 'var iV: longint;'
  6126. ]),
  6127. '');
  6128. StartUnit(true);
  6129. Add('interface');
  6130. Add('uses unit2;');
  6131. Add('implementation');
  6132. Add('var');
  6133. Add(' d: double;');
  6134. Add(' i: longint; external name ''$i'';');
  6135. Add('begin');
  6136. Add(' d:=nan;');
  6137. Add(' d:=uNit2.nan;');
  6138. Add(' d:=test1.d;');
  6139. Add(' i:=iv;');
  6140. Add(' i:=uNit2.iv;');
  6141. Add(' i:=test1.i;');
  6142. ConvertUnit;
  6143. CheckSource('TestVarExternalOtherUnit',
  6144. LinesToStr([
  6145. 'var $impl = $mod.$impl;',
  6146. '']),
  6147. LinesToStr([ // this.$init
  6148. '$impl.d = Global.NaN;',
  6149. '$impl.d = Global.NaN;',
  6150. '$impl.d = $impl.d;',
  6151. '$i = pas.unit2.iV;',
  6152. '$i = pas.unit2.iV;',
  6153. '$i = $i;',
  6154. '']),
  6155. LinesToStr([ // implementation
  6156. '$impl.d = 0.0;',
  6157. '']) );
  6158. end;
  6159. procedure TTestModule.TestVarAbsoluteFail;
  6160. begin
  6161. StartProgram(false);
  6162. Add([
  6163. 'var',
  6164. ' a: longint;',
  6165. ' b: longword absolute a;',
  6166. 'begin']);
  6167. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6168. ConvertProgram;
  6169. end;
  6170. procedure TTestModule.TestConstExternal;
  6171. begin
  6172. StartProgram(false);
  6173. Add([
  6174. 'const',
  6175. ' PI: double; external name ''Global.PI'';',
  6176. ' Tau = 2*pi;',
  6177. 'var d: double;',
  6178. 'begin',
  6179. ' d:=pi;',
  6180. ' d:=tau+pi;']);
  6181. ConvertProgram;
  6182. CheckSource('TestConstExternal',
  6183. LinesToStr([
  6184. 'this.Tau = 2*Global.PI;',
  6185. 'this.d = 0.0;'
  6186. ]),
  6187. LinesToStr([
  6188. '$mod.d = Global.PI;',
  6189. '$mod.d = $mod.Tau + Global.PI;'
  6190. ]));
  6191. end;
  6192. procedure TTestModule.TestDouble;
  6193. begin
  6194. StartProgram(false);
  6195. Add([
  6196. 'type',
  6197. ' TDateTime = double;',
  6198. 'const',
  6199. ' a = TDateTime(2.7);',
  6200. ' b = a + TDateTime(1.7);',
  6201. ' c = 0.9 + 0.1;',
  6202. ' f0_1 = 0.1;',
  6203. ' f0_3 = 0.3;',
  6204. ' fn0_1 = -0.1;',
  6205. ' fn0_3 = -0.3;',
  6206. ' fn0_003 = -0.003;',
  6207. ' fn0_123456789 = -0.123456789;',
  6208. ' fn300_0 = -300.0;',
  6209. ' fn123456_0 = -123456.0;',
  6210. ' fn1234567_8 = -1234567.8;',
  6211. ' fn12345678_9 = -12345678.9;',
  6212. ' f1_0En12 = 1E-12;',
  6213. ' fn1_0En12 = -1E-12;',
  6214. ' maxdouble = 1.7e+308;',
  6215. ' mindouble = -1.7e+308;',
  6216. ' MinSafeIntDouble = -$1fffffffffffff;',
  6217. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6218. ' MaxSafeIntDouble = $1fffffffffffff;',
  6219. ' DZeroResolution = 1E-12;',
  6220. ' Minus1 = -1E-12;',
  6221. ' EPS = 1E-9;',
  6222. ' DELTA = 0.001;',
  6223. ' Big = 129.789E+100;',
  6224. ' Test0_15 = 0.15;',
  6225. ' Test999 = 2.9999999999999;',
  6226. ' Test111999 = 211199999999999000.0;',
  6227. ' TestMinus111999 = -211199999999999000.0;',
  6228. 'var',
  6229. ' d: double = b;',
  6230. 'begin',
  6231. ' d:=1.0;',
  6232. ' d:=1.0/3.0;',
  6233. ' d:=1/3;',
  6234. ' d:=5.0E-324;',
  6235. ' d:=1.7E308;',
  6236. ' d:=001.00E00;',
  6237. ' d:=002.00E001;',
  6238. ' d:=003.000E000;',
  6239. ' d:=-004.00E-00;',
  6240. ' d:=-005.00E-001;',
  6241. ' d:=10**3;',
  6242. ' d:=10 mod 3;',
  6243. ' d:=10 div 3;',
  6244. ' d:=c;',
  6245. ' d:=f0_1;',
  6246. ' d:=f0_3;',
  6247. ' d:=fn0_1;',
  6248. ' d:=fn0_3;',
  6249. ' d:=fn0_003;',
  6250. ' d:=fn0_123456789;',
  6251. ' d:=fn300_0;',
  6252. ' d:=fn123456_0;',
  6253. ' d:=fn1234567_8;',
  6254. ' d:=fn12345678_9;',
  6255. ' d:=f1_0En12;',
  6256. ' d:=fn1_0En12;',
  6257. ' d:=maxdouble;',
  6258. ' d:=mindouble;',
  6259. ' d:=MinSafeIntDouble;',
  6260. ' d:=double(MinSafeIntDouble);',
  6261. ' d:=MinSafeIntDouble2;',
  6262. ' d:=double(MinSafeIntDouble2);',
  6263. ' d:=MaxSafeIntDouble;',
  6264. ' d:=default(double);',
  6265. '']);
  6266. ConvertProgram;
  6267. CheckSource('TestDouble',
  6268. LinesToStr([
  6269. 'this.a = 2.7;',
  6270. 'this.b = 2.7 + 1.7;',
  6271. 'this.c = 0.9 + 0.1;',
  6272. 'this.f0_1 = 0.1;',
  6273. 'this.f0_3 = 0.3;',
  6274. 'this.fn0_1 = -0.1;',
  6275. 'this.fn0_3 = -0.3;',
  6276. 'this.fn0_003 = -0.003;',
  6277. 'this.fn0_123456789 = -0.123456789;',
  6278. 'this.fn300_0 = -300.0;',
  6279. 'this.fn123456_0 = -123456.0;',
  6280. 'this.fn1234567_8 = -1234567.8;',
  6281. 'this.fn12345678_9 = -12345678.9;',
  6282. 'this.f1_0En12 = 1E-12;',
  6283. 'this.fn1_0En12 = -1E-12;',
  6284. 'this.maxdouble = 1.7e+308;',
  6285. 'this.mindouble = -1.7e+308;',
  6286. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6287. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6288. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6289. 'this.DZeroResolution = 1E-12;',
  6290. 'this.Minus1 = -1E-12;',
  6291. 'this.EPS = 1E-9;',
  6292. 'this.DELTA = 0.001;',
  6293. 'this.Big = 129.789E+100;',
  6294. 'this.Test0_15 = 0.15;',
  6295. 'this.Test999 = 2.9999999999999;',
  6296. 'this.Test111999 = 211199999999999000.0;',
  6297. 'this.TestMinus111999 = -211199999999999000.0;',
  6298. 'this.d = 4.4;'
  6299. ]),
  6300. LinesToStr([
  6301. '$mod.d = 1.0;',
  6302. '$mod.d = 1.0 / 3.0;',
  6303. '$mod.d = 1 / 3;',
  6304. '$mod.d = 5.0E-324;',
  6305. '$mod.d = 1.7E308;',
  6306. '$mod.d = 1.00E0;',
  6307. '$mod.d = 2.00E1;',
  6308. '$mod.d = 3.000E0;',
  6309. '$mod.d = -4.00E-0;',
  6310. '$mod.d = -5.00E-1;',
  6311. '$mod.d = Math.pow(10, 3);',
  6312. '$mod.d = 10 % 3;',
  6313. '$mod.d = Math.floor(10 / 3);',
  6314. '$mod.d = 1;',
  6315. '$mod.d = 0.1;',
  6316. '$mod.d = 0.3;',
  6317. '$mod.d = -0.1;',
  6318. '$mod.d = -0.3;',
  6319. '$mod.d = -0.003;',
  6320. '$mod.d = -0.123456789;',
  6321. '$mod.d = -300;',
  6322. '$mod.d = -123456;',
  6323. '$mod.d = -1234567.8;',
  6324. '$mod.d = -1.23456789E7;',
  6325. '$mod.d = 1E-12;',
  6326. '$mod.d = -1E-12;',
  6327. '$mod.d = 1.7E308;',
  6328. '$mod.d = -1.7E308;',
  6329. '$mod.d = -9007199254740991;',
  6330. '$mod.d = -9007199254740991;',
  6331. '$mod.d = -9.007199254740992E15;',
  6332. '$mod.d = -9.007199254740992E15;',
  6333. '$mod.d = 9007199254740991;',
  6334. '$mod.d = 0.0;',
  6335. '']));
  6336. end;
  6337. procedure TTestModule.TestInteger;
  6338. begin
  6339. StartProgram(false);
  6340. Add([
  6341. 'const',
  6342. ' MinInt = low(NativeInt);',
  6343. ' MaxInt = high(NativeInt);',
  6344. 'type',
  6345. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6346. 'const',
  6347. ' a = low(TMyInt)+High(TMyInt);',
  6348. 'var',
  6349. ' i: TMyInt;',
  6350. 'begin',
  6351. ' i:=-MinInt;',
  6352. ' i:=default(TMyInt);',
  6353. ' i:=low(i)+high(i);',
  6354. '']);
  6355. ConvertProgram;
  6356. CheckSource('TestIntegerRange',
  6357. LinesToStr([
  6358. 'this.MinInt = -9007199254740991;',
  6359. 'this.MaxInt = 9007199254740991;',
  6360. 'this.a = -9007199254740991 + 9007199254740991;',
  6361. 'this.i = 0;',
  6362. '']),
  6363. LinesToStr([
  6364. '$mod.i = - -9007199254740991;',
  6365. '$mod.i = -9007199254740991;',
  6366. '$mod.i = -9007199254740991 + 9007199254740991;',
  6367. '']));
  6368. end;
  6369. procedure TTestModule.TestIntegerRange;
  6370. begin
  6371. StartProgram(false);
  6372. Add([
  6373. 'const',
  6374. ' MinInt = -1;',
  6375. ' MaxInt = +1;',
  6376. 'type',
  6377. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6378. ' TInt2 = 1..3;',
  6379. 'const',
  6380. ' a = low(TMyInt)+High(TMyInt);',
  6381. ' b = low(TInt2)+High(TInt2);',
  6382. ' s1 = [1];',
  6383. ' s2 = [1,2];',
  6384. ' s3 = [1..3];',
  6385. ' s4 = [low(shortint)..high(shortint)];',
  6386. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6387. ' s6 = 1 in s2;',
  6388. 'var',
  6389. ' i: TMyInt;',
  6390. ' i2: TInt2;',
  6391. 'begin',
  6392. ' i:=i2;',
  6393. ' i:=default(TMyInt);',
  6394. ' if i=i2 then ;']);
  6395. ConvertProgram;
  6396. CheckSource('TestIntegerRange',
  6397. LinesToStr([
  6398. 'this.MinInt = -1;',
  6399. 'this.MaxInt = +1;',
  6400. 'this.a = -1 + 1;',
  6401. 'this.b = 1 + 3;',
  6402. 'this.s1 = rtl.createSet(1);',
  6403. 'this.s2 = rtl.createSet(1, 2);',
  6404. 'this.s3 = rtl.createSet(null, 1, 3);',
  6405. 'this.s4 = rtl.createSet(null, -128, 127);',
  6406. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6407. 'this.s6 = 1 in $mod.s2;',
  6408. 'this.i = 0;',
  6409. 'this.i2 = 0;',
  6410. '']),
  6411. LinesToStr([
  6412. '$mod.i = $mod.i2;',
  6413. '$mod.i = -1;',
  6414. 'if ($mod.i === $mod.i2) ;',
  6415. '']));
  6416. end;
  6417. procedure TTestModule.TestIntegerTypecasts;
  6418. begin
  6419. StartProgram(false);
  6420. Add([
  6421. 'var',
  6422. ' i: nativeint;',
  6423. ' b: byte;',
  6424. ' sh: shortint;',
  6425. ' w: word;',
  6426. ' sm: smallint;',
  6427. ' lw: longword;',
  6428. ' li: longint;',
  6429. 'begin',
  6430. ' b:=byte(i);',
  6431. ' sh:=shortint(i);',
  6432. ' w:=word(i);',
  6433. ' sm:=smallint(i);',
  6434. ' lw:=longword(i);',
  6435. ' li:=longint(i);',
  6436. '']);
  6437. ConvertProgram;
  6438. CheckSource('TestIntegerTypecasts',
  6439. LinesToStr([
  6440. 'this.i = 0;',
  6441. 'this.b = 0;',
  6442. 'this.sh = 0;',
  6443. 'this.w = 0;',
  6444. 'this.sm = 0;',
  6445. 'this.lw = 0;',
  6446. 'this.li = 0;',
  6447. '']),
  6448. LinesToStr([
  6449. '$mod.b = $mod.i & 255;',
  6450. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6451. '$mod.w = $mod.i & 65535;',
  6452. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6453. '$mod.lw = $mod.i >>> 0;',
  6454. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6455. '']));
  6456. end;
  6457. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6458. begin
  6459. StartProgram(false);
  6460. Add([
  6461. 'var',
  6462. ' i,j: nativeint;',
  6463. 'begin',
  6464. ' i:=i shr 0;',
  6465. ' i:=i shr 1;',
  6466. ' i:=i shr 3;',
  6467. ' i:=i shr 54;',
  6468. ' i:=j shr i;',
  6469. '']);
  6470. ConvertProgram;
  6471. CheckResolverUnexpectedHints;
  6472. CheckSource('TestInteger_BitwiseShrNativeInt',
  6473. LinesToStr([
  6474. 'this.i = 0;',
  6475. 'this.j = 0;',
  6476. '']),
  6477. LinesToStr([
  6478. '$mod.i = $mod.i;',
  6479. '$mod.i = Math.floor($mod.i / 2);',
  6480. '$mod.i = Math.floor($mod.i / 8);',
  6481. '$mod.i = 0;',
  6482. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6483. '']));
  6484. end;
  6485. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6486. begin
  6487. StartProgram(false);
  6488. Add([
  6489. 'var',
  6490. ' i: nativeint;',
  6491. 'begin',
  6492. ' i:=i shl 0;',
  6493. ' i:=i shl 54;',
  6494. ' i:=123456789012 shl 1;',
  6495. ' i:=i shl 1;',
  6496. '']);
  6497. ConvertProgram;
  6498. CheckResolverUnexpectedHints;
  6499. CheckSource('TestInteger_BitwiseShrNativeInt',
  6500. LinesToStr([
  6501. 'this.i = 0;',
  6502. '']),
  6503. LinesToStr([
  6504. '$mod.i = $mod.i;',
  6505. '$mod.i = 0;',
  6506. '$mod.i = 246913578024;',
  6507. '$mod.i = rtl.shl($mod.i, 1);',
  6508. '']));
  6509. end;
  6510. procedure TTestModule.TestCurrency;
  6511. begin
  6512. StartProgram(false);
  6513. Add([
  6514. 'type',
  6515. ' TCoin = currency;',
  6516. 'const',
  6517. ' a = TCoin(2.7);',
  6518. ' b = a + TCoin(1.7);',
  6519. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6520. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6521. 'var',
  6522. ' c: TCoin = b;',
  6523. ' i: nativeint;',
  6524. ' d: double;',
  6525. ' j: jsvalue;',
  6526. 'function DoIt(c: currency): currency; begin end;',
  6527. 'function GetIt(d: double): double; begin end;',
  6528. 'procedure Write(v: jsvalue); begin end;',
  6529. 'begin',
  6530. ' c:=1.0;',
  6531. ' c:=0.1;',
  6532. ' c:=1.0/3.0;',
  6533. ' c:=1/3;',
  6534. ' c:=a;',
  6535. ' d:=c;',
  6536. ' c:=d;',
  6537. ' c:=currency(c);',
  6538. ' c:=currency(d);',
  6539. ' d:=double(c);',
  6540. ' c:=i;',
  6541. ' c:=currency(i);',
  6542. //' i:=c;', not allowed
  6543. ' i:=nativeint(c);',
  6544. ' c:=c+a;',
  6545. ' c:=-c-a;',
  6546. ' c:=d+c;',
  6547. ' c:=c+d;',
  6548. ' c:=d-c;',
  6549. ' c:=c-d;',
  6550. ' c:=c*a;',
  6551. ' c:=a*c;',
  6552. ' c:=d*c;',
  6553. ' c:=c*d;',
  6554. ' c:=c/a;',
  6555. ' c:=a/c;',
  6556. ' c:=d/c;',
  6557. ' c:=c/d;',
  6558. ' c:=c**a;',
  6559. ' c:=a**c;',
  6560. ' c:=d**c;',
  6561. ' c:=c**d;',
  6562. ' if c=c then ;',
  6563. ' if c=a then ;',
  6564. ' if a=c then ;',
  6565. ' if d=c then ;',
  6566. ' if c=d then ;',
  6567. ' c:=DoIt(c);',
  6568. ' c:=DoIt(i);',
  6569. ' c:=DoIt(d);',
  6570. ' c:=GetIt(c);',
  6571. ' j:=c;',
  6572. ' Write(c);',
  6573. ' c:=default(currency);',
  6574. ' j:=str(c);',
  6575. ' j:=str(c:0:3);',
  6576. '']);
  6577. ConvertProgram;
  6578. CheckSource('TestCurrency',
  6579. LinesToStr([
  6580. 'this.a = 27000;',
  6581. 'this.b = $mod.a + 17000;',
  6582. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6583. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6584. 'this.c = $mod.b;',
  6585. 'this.i = 0;',
  6586. 'this.d = 0.0;',
  6587. 'this.j = undefined;',
  6588. 'this.DoIt = function (c) {',
  6589. ' var Result = 0;',
  6590. ' return Result;',
  6591. '};',
  6592. 'this.GetIt = function (d) {',
  6593. ' var Result = 0.0;',
  6594. ' return Result;',
  6595. '};',
  6596. 'this.Write = function (v) {',
  6597. '};',
  6598. '']),
  6599. LinesToStr([
  6600. '$mod.c = 10000;',
  6601. '$mod.c = 1000;',
  6602. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6603. '$mod.c = Math.floor((1 / 3) * 10000);',
  6604. '$mod.c = $mod.a;',
  6605. '$mod.d = $mod.c / 10000;',
  6606. '$mod.c = Math.floor($mod.d * 10000);',
  6607. '$mod.c = $mod.c;',
  6608. '$mod.c = $mod.d * 10000;',
  6609. '$mod.d = $mod.c / 10000;',
  6610. '$mod.c = $mod.i * 10000;',
  6611. '$mod.c = $mod.i * 10000;',
  6612. '$mod.i = Math.floor($mod.c / 10000);',
  6613. '$mod.c = $mod.c + $mod.a;',
  6614. '$mod.c = -$mod.c - $mod.a;',
  6615. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6616. '$mod.c = $mod.c + ($mod.d * 10000);',
  6617. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6618. '$mod.c = $mod.c - ($mod.d * 10000);',
  6619. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6620. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6621. '$mod.c = $mod.d * $mod.c;',
  6622. '$mod.c = $mod.c * $mod.d;',
  6623. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6624. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6625. '$mod.c = Math.floor($mod.d / $mod.c);',
  6626. '$mod.c = Math.floor($mod.c / $mod.d);',
  6627. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6628. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6629. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6630. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6631. 'if ($mod.c === $mod.c) ;',
  6632. 'if ($mod.c === $mod.a) ;',
  6633. 'if ($mod.a === $mod.c) ;',
  6634. 'if (($mod.d * 10000) === $mod.c) ;',
  6635. 'if ($mod.c === ($mod.d * 10000)) ;',
  6636. '$mod.c = $mod.DoIt($mod.c);',
  6637. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6638. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6639. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6640. '$mod.j = $mod.c / 10000;',
  6641. '$mod.Write($mod.c / 10000);',
  6642. '$mod.c = 0;',
  6643. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6644. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6645. '']));
  6646. end;
  6647. procedure TTestModule.TestForBoolDo;
  6648. begin
  6649. StartProgram(false);
  6650. Add([
  6651. 'var b: boolean;',
  6652. 'begin',
  6653. ' for b:=false to true do ;',
  6654. ' for b:=b downto false do ;',
  6655. ' for b in boolean do ;',
  6656. '']);
  6657. ConvertProgram;
  6658. CheckSource('TestForBoolDo',
  6659. LinesToStr([ // statements
  6660. 'this.b = false;']),
  6661. LinesToStr([ // this.$main
  6662. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6663. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6664. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6665. '']));
  6666. end;
  6667. procedure TTestModule.TestForIntDo;
  6668. begin
  6669. StartProgram(false);
  6670. Add([
  6671. 'var i: longint;',
  6672. 'begin',
  6673. ' for i:=3 to 5 do ;',
  6674. ' for i:=i downto 2 do ;',
  6675. ' for i in byte do ;',
  6676. '']);
  6677. ConvertProgram;
  6678. CheckSource('TestForIntDo',
  6679. LinesToStr([ // statements
  6680. 'this.i = 0;']),
  6681. LinesToStr([ // this.$main
  6682. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6683. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6684. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6685. '']));
  6686. end;
  6687. procedure TTestModule.TestForIntInDo;
  6688. begin
  6689. StartProgram(false);
  6690. Add([
  6691. 'type',
  6692. ' TSetOfInt = set of byte;',
  6693. ' TIntRg = 3..7;',
  6694. ' TSetOfIntRg = set of TIntRg;',
  6695. 'var',
  6696. ' i,i2: longint;',
  6697. ' a1: array of byte;',
  6698. ' a2: array[1..3] of byte;',
  6699. ' soi: TSetOfInt;',
  6700. ' soir: TSetOfIntRg;',
  6701. ' ir: TIntRg;',
  6702. 'begin',
  6703. ' for i in byte do ;',
  6704. ' for i in a1 do ;',
  6705. ' for i in a2 do ;',
  6706. ' for i in [11..13] do ;',
  6707. ' for i in TSetOfInt do ;',
  6708. ' for i in TIntRg do ;',
  6709. ' for i in soi do i2:=i;',
  6710. ' for i in TSetOfIntRg do ;',
  6711. ' for i in soir do ;',
  6712. ' for ir in TIntRg do ;',
  6713. ' for ir in TSetOfIntRg do ;',
  6714. ' for ir in soir do ;',
  6715. '']);
  6716. ConvertProgram;
  6717. CheckSource('TestForIntInDo',
  6718. LinesToStr([ // statements
  6719. 'this.i = 0;',
  6720. 'this.i2 = 0;',
  6721. 'this.a1 = [];',
  6722. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6723. 'this.soi = {};',
  6724. 'this.soir = {};',
  6725. 'this.ir = 0;',
  6726. '']),
  6727. LinesToStr([ // this.$main
  6728. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6729. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6730. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6731. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6732. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6733. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6734. 'for (var $l11 in $mod.soi) {',
  6735. ' $mod.i = +$l11;',
  6736. ' $mod.i2 = $mod.i;',
  6737. '};',
  6738. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6739. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6740. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6741. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6742. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6743. '']));
  6744. end;
  6745. procedure TTestModule.TestCharConst;
  6746. begin
  6747. StartProgram(false);
  6748. Add([
  6749. 'const',
  6750. ' a = #$00F3;',
  6751. ' c: char = ''1'';',
  6752. 'begin',
  6753. ' c:=#0;',
  6754. ' c:=#1;',
  6755. ' c:=#9;',
  6756. ' c:=#10;',
  6757. ' c:=#13;',
  6758. ' c:=#31;',
  6759. ' c:=#32;',
  6760. ' c:=#$A;',
  6761. ' c:=#$0A;',
  6762. ' c:=#$b;',
  6763. ' c:=#$0b;',
  6764. ' c:=^A;',
  6765. ' c:=''"'';',
  6766. ' c:=default(char);',
  6767. ' c:=#$00E4;', // ä
  6768. ' c:=''ä'';',
  6769. ' c:=#$E4;', // ä
  6770. ' c:=#$D800;', // invalid UTF-16
  6771. ' c:=#$DFFF;', // invalid UTF-16
  6772. ' c:=#$FFFF;', // last UCS-2
  6773. ' c:=high(c);', // last UCS-2
  6774. '']);
  6775. ConvertProgram;
  6776. CheckSource('TestCharConst',
  6777. LinesToStr([
  6778. 'this.a="ó";',
  6779. 'this.c="1";'
  6780. ]),
  6781. LinesToStr([
  6782. '$mod.c="\x00";',
  6783. '$mod.c="\x01";',
  6784. '$mod.c="\t";',
  6785. '$mod.c="\n";',
  6786. '$mod.c="\r";',
  6787. '$mod.c="\x1F";',
  6788. '$mod.c=" ";',
  6789. '$mod.c="\n";',
  6790. '$mod.c="\n";',
  6791. '$mod.c="\x0B";',
  6792. '$mod.c="\x0B";',
  6793. '$mod.c="\x01";',
  6794. '$mod.c=''"'';',
  6795. '$mod.c="\x00";',
  6796. '$mod.c = "ä";',
  6797. '$mod.c = "ä";',
  6798. '$mod.c = "ä";',
  6799. '$mod.c="\uD800";',
  6800. '$mod.c="\uDFFF";',
  6801. '$mod.c="\uFFFF";',
  6802. '$mod.c="\uFFFF";',
  6803. '']));
  6804. end;
  6805. procedure TTestModule.TestChar_Compare;
  6806. begin
  6807. StartProgram(false);
  6808. Add('var');
  6809. Add(' c: char;');
  6810. Add(' b: boolean;');
  6811. Add('begin');
  6812. Add(' b:=c=''1'';');
  6813. Add(' b:=''2''=c;');
  6814. Add(' b:=''3''=''4'';');
  6815. Add(' b:=c<>''5'';');
  6816. Add(' b:=''6''<>c;');
  6817. Add(' b:=c>''7'';');
  6818. Add(' b:=''8''>c;');
  6819. Add(' b:=c>=''9'';');
  6820. Add(' b:=''A''>=c;');
  6821. Add(' b:=c<''B'';');
  6822. Add(' b:=''C''<c;');
  6823. Add(' b:=c<=''D'';');
  6824. Add(' b:=''E''<=c;');
  6825. ConvertProgram;
  6826. CheckSource('TestChar_Compare',
  6827. LinesToStr([
  6828. 'this.c="";',
  6829. 'this.b = false;'
  6830. ]),
  6831. LinesToStr([
  6832. '$mod.b = $mod.c === "1";',
  6833. '$mod.b = "2" === $mod.c;',
  6834. '$mod.b = "3" === "4";',
  6835. '$mod.b = $mod.c !== "5";',
  6836. '$mod.b = "6" !== $mod.c;',
  6837. '$mod.b = $mod.c > "7";',
  6838. '$mod.b = "8" > $mod.c;',
  6839. '$mod.b = $mod.c >= "9";',
  6840. '$mod.b = "A" >= $mod.c;',
  6841. '$mod.b = $mod.c < "B";',
  6842. '$mod.b = "C" < $mod.c;',
  6843. '$mod.b = $mod.c <= "D";',
  6844. '$mod.b = "E" <= $mod.c;',
  6845. '']));
  6846. end;
  6847. procedure TTestModule.TestChar_BuiltInProcs;
  6848. begin
  6849. StartProgram(false);
  6850. Add([
  6851. 'var',
  6852. ' c: char;',
  6853. ' i: longint;',
  6854. ' s: string;',
  6855. 'begin',
  6856. ' i:=ord(c);',
  6857. ' i:=ord(s[i]);',
  6858. ' c:=chr(i);',
  6859. ' c:=pred(c);',
  6860. ' c:=succ(c);',
  6861. ' c:=low(c);',
  6862. ' c:=high(c);',
  6863. ' i:=byte(c);',
  6864. ' i:=word(c);',
  6865. ' i:=longint(c);',
  6866. '']);
  6867. ConvertProgram;
  6868. CheckSource('TestChar_BuiltInProcs',
  6869. LinesToStr([
  6870. 'this.c = "";',
  6871. 'this.i = 0;',
  6872. 'this.s = "";'
  6873. ]),
  6874. LinesToStr([
  6875. '$mod.i = $mod.c.charCodeAt();',
  6876. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6877. '$mod.c = String.fromCharCode($mod.i);',
  6878. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6879. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6880. '$mod.c = "\x00";',
  6881. '$mod.c = "\uFFFF";',
  6882. '$mod.i = $mod.c.charCodeAt() & 255;',
  6883. '$mod.i = $mod.c.charCodeAt();',
  6884. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  6885. '']));
  6886. end;
  6887. procedure TTestModule.TestStringConst;
  6888. begin
  6889. StartProgram(false);
  6890. Add([
  6891. '{$H+}',
  6892. 'const',
  6893. ' a = #$00F3#$017C;', // first <256, then >=256
  6894. ' b = string(''a'');',
  6895. ' c = string(''ä'');',
  6896. ' d = UnicodeString(''b'');',
  6897. ' e = UnicodeString(''ö'');',
  6898. 'var',
  6899. ' s: string = ''abc'';',
  6900. 'begin',
  6901. ' s:='''';',
  6902. ' s:=#13#10;',
  6903. ' s:=#9''foo'';',
  6904. ' s:=#$A9;',
  6905. ' s:=''foo''#13''bar'';',
  6906. ' s:=''"'';',
  6907. ' s:=''"''''"'';',
  6908. ' s:=#$20AC;', // euro
  6909. ' s:=#$10437;', // outside BMP
  6910. ' s:=default(string);',
  6911. ' s:=concat(s);',
  6912. ' s:=concat(s,''a'',s)',
  6913. '']);
  6914. ConvertProgram;
  6915. CheckSource('TestStringConst',
  6916. LinesToStr([
  6917. 'this.a = "óż";',
  6918. 'this.b = "a";',
  6919. 'this.c = "ä";',
  6920. 'this.d = "b";',
  6921. 'this.e = "ö";',
  6922. 'this.s="abc";',
  6923. '']),
  6924. LinesToStr([
  6925. '$mod.s="";',
  6926. '$mod.s="\r\n";',
  6927. '$mod.s="\tfoo";',
  6928. '$mod.s="©";',
  6929. '$mod.s="foo\rbar";',
  6930. '$mod.s=''"'';',
  6931. '$mod.s=''"\''"'';',
  6932. '$mod.s="€";',
  6933. '$mod.s="'#$F0#$90#$90#$B7'";',
  6934. '$mod.s="";',
  6935. '$mod.s = $mod.s;',
  6936. '$mod.s = $mod.s.concat("a", $mod.s);',
  6937. '']));
  6938. end;
  6939. procedure TTestModule.TestStringConstSurrogate;
  6940. begin
  6941. StartProgram(false);
  6942. Add([
  6943. 'var',
  6944. ' s: string;',
  6945. 'begin',
  6946. ' s:=''😊'';', // 1F60A
  6947. '']);
  6948. ConvertProgram;
  6949. CheckSource('TestStringConstSurrogate',
  6950. LinesToStr([
  6951. 'this.s="";'
  6952. ]),
  6953. LinesToStr([
  6954. '$mod.s="😊";'
  6955. ]));
  6956. end;
  6957. procedure TTestModule.TestString_Length;
  6958. begin
  6959. StartProgram(false);
  6960. Add('const c = ''foo'';');
  6961. Add('var');
  6962. Add(' s: string;');
  6963. Add(' i: longint;');
  6964. Add('begin');
  6965. Add(' i:=length(s);');
  6966. Add(' i:=length(s+s);');
  6967. Add(' i:=length(''abc'');');
  6968. Add(' i:=length(c);');
  6969. ConvertProgram;
  6970. CheckSource('TestString_Length',
  6971. LinesToStr([
  6972. 'this.c = "foo";',
  6973. 'this.s = "";',
  6974. 'this.i = 0;',
  6975. '']),
  6976. LinesToStr([
  6977. '$mod.i = $mod.s.length;',
  6978. '$mod.i = ($mod.s+$mod.s).length;',
  6979. '$mod.i = "abc".length;',
  6980. '$mod.i = $mod.c.length;',
  6981. '']));
  6982. end;
  6983. procedure TTestModule.TestString_Compare;
  6984. begin
  6985. StartProgram(false);
  6986. Add('var');
  6987. Add(' s, t: string;');
  6988. Add(' b: boolean;');
  6989. Add('begin');
  6990. Add(' b:=s=t;');
  6991. Add(' b:=s<>t;');
  6992. Add(' b:=s>t;');
  6993. Add(' b:=s>=t;');
  6994. Add(' b:=s<t;');
  6995. Add(' b:=s<=t;');
  6996. ConvertProgram;
  6997. CheckSource('TestString_Compare',
  6998. LinesToStr([ // statements
  6999. 'this.s = "";',
  7000. 'this.t = "";',
  7001. 'this.b =false;'
  7002. ]),
  7003. LinesToStr([ // this.$main
  7004. '$mod.b = $mod.s === $mod.t;',
  7005. '$mod.b = $mod.s !== $mod.t;',
  7006. '$mod.b = $mod.s > $mod.t;',
  7007. '$mod.b = $mod.s >= $mod.t;',
  7008. '$mod.b = $mod.s < $mod.t;',
  7009. '$mod.b = $mod.s <= $mod.t;',
  7010. '']));
  7011. end;
  7012. procedure TTestModule.TestString_SetLength;
  7013. begin
  7014. StartProgram(false);
  7015. Add([
  7016. 'procedure DoIt(var s: string);',
  7017. 'begin',
  7018. ' SetLength(s,2);',
  7019. 'end;',
  7020. 'var s: string;',
  7021. 'begin',
  7022. ' SetLength(s,3);',
  7023. '']);
  7024. ConvertProgram;
  7025. CheckSource('TestString_SetLength',
  7026. LinesToStr([ // statements
  7027. 'this.DoIt = function (s) {',
  7028. ' s.set(rtl.strSetLength(s.get(), 2));',
  7029. '};',
  7030. 'this.s = "";',
  7031. '']),
  7032. LinesToStr([ // this.$main
  7033. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7034. ]));
  7035. end;
  7036. procedure TTestModule.TestString_CharAt;
  7037. begin
  7038. StartProgram(false);
  7039. Add([
  7040. 'var',
  7041. ' s: string;',
  7042. ' c: char;',
  7043. ' b: boolean;',
  7044. 'begin',
  7045. ' b:= s[1] = c;',
  7046. ' b:= c = s[1];',
  7047. ' b:= c <> s[1];',
  7048. ' b:= c > s[1];',
  7049. ' b:= c >= s[1];',
  7050. ' b:= c < s[2];',
  7051. ' b:= c <= s[1];',
  7052. ' s[1] := c;',
  7053. ' s[2+3] := c;']);
  7054. ConvertProgram;
  7055. CheckSource('TestString_CharAt',
  7056. LinesToStr([ // statements
  7057. 'this.s = "";',
  7058. 'this.c = "";',
  7059. 'this.b = false;'
  7060. ]),
  7061. LinesToStr([ // this.$main
  7062. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7063. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7064. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7065. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7066. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7067. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7068. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7069. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7070. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7071. '']));
  7072. end;
  7073. procedure TTestModule.TestStringHMinusFail;
  7074. begin
  7075. StartProgram(false);
  7076. Add([
  7077. '{$H-}',
  7078. 'var s: string;',
  7079. 'begin']);
  7080. ConvertProgram;
  7081. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7082. end;
  7083. procedure TTestModule.TestStr;
  7084. begin
  7085. StartProgram(false);
  7086. Add('var');
  7087. Add(' b: boolean;');
  7088. Add(' i: longint;');
  7089. Add(' d: double;');
  7090. Add(' s: string;');
  7091. Add('begin');
  7092. Add(' str(b,s);');
  7093. Add(' str(i,s);');
  7094. Add(' str(d,s);');
  7095. Add(' str(i:3,s);');
  7096. Add(' str(d:3:2,s);');
  7097. Add(' Str(12.456:12:1,s);');
  7098. Add(' Str(12.456:12,s);');
  7099. Add(' s:=str(b);');
  7100. Add(' s:=str(i);');
  7101. Add(' s:=str(d);');
  7102. Add(' s:=str(i,i);');
  7103. Add(' s:=str(i:3);');
  7104. Add(' s:=str(d:3:2);');
  7105. Add(' s:=str(i:4,i);');
  7106. Add(' s:=str(i,i:5);');
  7107. Add(' s:=str(i:4,i:5);');
  7108. Add(' s:=str(s,s);');
  7109. Add(' s:=str(s,''foo'');');
  7110. ConvertProgram;
  7111. CheckSource('TestStr',
  7112. LinesToStr([ // statements
  7113. 'this.b = false;',
  7114. 'this.i = 0;',
  7115. 'this.d = 0.0;',
  7116. 'this.s = "";',
  7117. '']),
  7118. LinesToStr([ // this.$main
  7119. '$mod.s = ""+$mod.b;',
  7120. '$mod.s = ""+$mod.i;',
  7121. '$mod.s = rtl.floatToStr($mod.d);',
  7122. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7123. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7124. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7125. '$mod.s = rtl.floatToStr(12.456,12);',
  7126. '$mod.s = ""+$mod.b;',
  7127. '$mod.s = ""+$mod.i;',
  7128. '$mod.s = rtl.floatToStr($mod.d);',
  7129. '$mod.s = ""+$mod.i+$mod.i;',
  7130. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7131. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7132. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7133. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7134. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7135. '$mod.s = $mod.s + $mod.s;',
  7136. '$mod.s = $mod.s + "foo";',
  7137. '']));
  7138. end;
  7139. procedure TTestModule.TestBaseType_AnsiStringFail;
  7140. begin
  7141. StartProgram(false);
  7142. Add('var s: AnsiString');
  7143. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7144. ConvertProgram;
  7145. end;
  7146. procedure TTestModule.TestBaseType_WideStringFail;
  7147. begin
  7148. StartProgram(false);
  7149. Add('var s: WideString');
  7150. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7151. ConvertProgram;
  7152. end;
  7153. procedure TTestModule.TestBaseType_ShortStringFail;
  7154. begin
  7155. StartProgram(false);
  7156. Add('var s: ShortString');
  7157. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7158. ConvertProgram;
  7159. end;
  7160. procedure TTestModule.TestBaseType_RawByteStringFail;
  7161. begin
  7162. StartProgram(false);
  7163. Add('var s: RawByteString');
  7164. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7165. ConvertProgram;
  7166. end;
  7167. procedure TTestModule.TestTypeShortstring_Fail;
  7168. begin
  7169. StartProgram(false);
  7170. Add('type t = string[12];');
  7171. Add('var s: t;');
  7172. Add('begin');
  7173. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7174. ConvertProgram;
  7175. end;
  7176. procedure TTestModule.TestCharSet_Custom;
  7177. begin
  7178. StartProgram(false);
  7179. Add([
  7180. 'type',
  7181. ' TCharRg = ''a''..''z'';',
  7182. ' TSetOfCharRg = set of TCharRg;',
  7183. ' TCharRg2 = ''m''..''p'';',
  7184. 'const',
  7185. ' crg: TCharRg = ''b'';',
  7186. 'var',
  7187. ' c: char;',
  7188. ' crg2: TCharRg2;',
  7189. ' s: TSetOfCharRg;',
  7190. 'begin',
  7191. ' c:=crg;',
  7192. ' crg:=c;',
  7193. ' crg2:=crg;',
  7194. ' if c=crg then ;',
  7195. ' if crg=c then ;',
  7196. ' if crg=crg2 then ;',
  7197. ' if c in s then ;',
  7198. ' if crg2 in s then ;',
  7199. ' c:=default(TCharRg);',
  7200. '']);
  7201. ConvertProgram;
  7202. CheckSource('TestCharSet_Custom',
  7203. LinesToStr([ // statements
  7204. 'this.crg = "b";',
  7205. 'this.c = "";',
  7206. 'this.crg2 = "m";',
  7207. 'this.s = {};',
  7208. '']),
  7209. LinesToStr([ // this.$main
  7210. '$mod.c = $mod.crg;',
  7211. '$mod.crg = $mod.c;',
  7212. '$mod.crg2 = $mod.crg;',
  7213. 'if ($mod.c === $mod.crg) ;',
  7214. 'if ($mod.crg === $mod.c) ;',
  7215. 'if ($mod.crg === $mod.crg2) ;',
  7216. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7217. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7218. '$mod.c = "a";',
  7219. '']));
  7220. end;
  7221. procedure TTestModule.TestForCharDo;
  7222. begin
  7223. StartProgram(false);
  7224. Add([
  7225. 'var c: char;',
  7226. 'begin',
  7227. ' for c:=''a'' to ''c'' do ;',
  7228. ' for c:=c downto ''a'' do ;',
  7229. ' for c:=''Б'' to ''Я'' do ;',
  7230. '']);
  7231. ConvertProgram;
  7232. CheckSource('TestForCharDo',
  7233. LinesToStr([ // statements
  7234. 'this.c = "";']),
  7235. LinesToStr([ // this.$main
  7236. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  7237. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  7238. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  7239. '']));
  7240. end;
  7241. procedure TTestModule.TestForCharInDo;
  7242. begin
  7243. StartProgram(false);
  7244. Add([
  7245. 'type',
  7246. ' TSetOfChar = set of char;',
  7247. ' TCharRg = ''a''..''z'';',
  7248. ' TSetOfCharRg = set of TCharRg;',
  7249. 'const Foo = ''foo'';',
  7250. 'var',
  7251. ' c,c2: char;',
  7252. ' s: string;',
  7253. ' a1: array of char;',
  7254. ' a2: array[1..3] of char;',
  7255. ' soc: TSetOfChar;',
  7256. ' socr: TSetOfCharRg;',
  7257. ' cr: TCharRg;',
  7258. 'begin',
  7259. ' for c in foo do ;',
  7260. ' for c in s do ;',
  7261. ' for c in char do ;',
  7262. ' for c in a1 do ;',
  7263. ' for c in a2 do ;',
  7264. ' for c in [''1''..''3''] do ;',
  7265. ' for c in TSetOfChar do ;',
  7266. ' for c in TCharRg do ;',
  7267. ' for c in soc do c2:=c;',
  7268. ' for c in TSetOfCharRg do ;',
  7269. ' for c in socr do ;',
  7270. ' for cr in TCharRg do ;',
  7271. ' for cr in TSetOfCharRg do ;',
  7272. ' for cr in socr do ;',
  7273. '']);
  7274. ConvertProgram;
  7275. CheckSource('TestForCharInDo',
  7276. LinesToStr([ // statements
  7277. 'this.Foo = "foo";',
  7278. 'this.c = "";',
  7279. 'this.c2 = "";',
  7280. 'this.s = "";',
  7281. 'this.a1 = [];',
  7282. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7283. 'this.soc = {};',
  7284. 'this.socr = {};',
  7285. 'this.cr = "a";',
  7286. '']),
  7287. LinesToStr([ // this.$main
  7288. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  7289. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  7290. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  7291. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  7292. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  7293. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  7294. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  7295. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  7296. 'for (var $l17 in $mod.soc) {',
  7297. ' $mod.c = String.fromCharCode($l17);',
  7298. ' $mod.c2 = $mod.c;',
  7299. '};',
  7300. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  7301. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  7302. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  7303. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  7304. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  7305. '']));
  7306. end;
  7307. procedure TTestModule.TestProcTwoArgs;
  7308. begin
  7309. StartProgram(false);
  7310. Add('procedure Test(a,b: longint);');
  7311. Add('begin');
  7312. Add('end;');
  7313. Add('begin');
  7314. ConvertProgram;
  7315. CheckSource('TestProcTwoArgs',
  7316. LinesToStr([ // statements
  7317. 'this.Test = function (a,b) {',
  7318. '};'
  7319. ]),
  7320. LinesToStr([ // this.$main
  7321. ''
  7322. ]));
  7323. end;
  7324. procedure TTestModule.TestProc_DefaultValue;
  7325. begin
  7326. StartProgram(false);
  7327. Add('procedure p1(i: longint = 1);');
  7328. Add('begin');
  7329. Add('end;');
  7330. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7331. Add('begin');
  7332. Add('end;');
  7333. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7334. Add('begin');
  7335. Add('end;');
  7336. Add('begin');
  7337. Add(' p1;');
  7338. Add(' p1();');
  7339. Add(' p1(11);');
  7340. Add(' p2;');
  7341. Add(' p2();');
  7342. Add(' p2(12);');
  7343. Add(' p2(13,''b'');');
  7344. Add(' p3();');
  7345. ConvertProgram;
  7346. CheckSource('TestProc_DefaultValue',
  7347. LinesToStr([ // statements
  7348. 'this.p1 = function (i) {',
  7349. '};',
  7350. 'this.p2 = function (i,c) {',
  7351. '};',
  7352. 'this.p3 = function (d,b,s) {',
  7353. '};'
  7354. ]),
  7355. LinesToStr([ // this.$main
  7356. ' $mod.p1(1);',
  7357. ' $mod.p1(1);',
  7358. ' $mod.p1(11);',
  7359. ' $mod.p2(1,"a");',
  7360. ' $mod.p2(1,"a");',
  7361. ' $mod.p2(12,"a");',
  7362. ' $mod.p2(13,"b");',
  7363. ' $mod.p3(1.0,false,"abc");'
  7364. ]));
  7365. end;
  7366. procedure TTestModule.TestFunctionInt;
  7367. begin
  7368. StartProgram(false);
  7369. Add('function MyTest(Bar: longint): longint;');
  7370. Add('begin');
  7371. Add(' Result:=2*bar');
  7372. Add('end;');
  7373. Add('begin');
  7374. ConvertProgram;
  7375. CheckSource('TestFunctionInt',
  7376. LinesToStr([ // statements
  7377. 'this.MyTest = function (Bar) {',
  7378. ' var Result = 0;',
  7379. ' Result = 2*Bar;',
  7380. ' return Result;',
  7381. '};'
  7382. ]),
  7383. LinesToStr([ // this.$main
  7384. ''
  7385. ]));
  7386. end;
  7387. procedure TTestModule.TestFunctionString;
  7388. begin
  7389. StartProgram(false);
  7390. Add('function Test(Bar: string): string;');
  7391. Add('begin');
  7392. Add(' Result:=bar+BAR');
  7393. Add('end;');
  7394. Add('begin');
  7395. ConvertProgram;
  7396. CheckSource('TestFunctionString',
  7397. LinesToStr([ // statements
  7398. 'this.Test = function (Bar) {',
  7399. ' var Result = "";',
  7400. ' Result = Bar+Bar;',
  7401. ' return Result;',
  7402. '};'
  7403. ]),
  7404. LinesToStr([ // this.$main
  7405. ''
  7406. ]));
  7407. end;
  7408. procedure TTestModule.TestIfThen;
  7409. begin
  7410. StartProgram(false);
  7411. Add([
  7412. 'var b: boolean;',
  7413. 'begin',
  7414. ' if b then ;',
  7415. ' if b then else ;']);
  7416. ConvertProgram;
  7417. CheckSource('TestIfThen',
  7418. LinesToStr([ // statements
  7419. 'this.b = false;',
  7420. '']),
  7421. LinesToStr([ // this.$main
  7422. 'if ($mod.b) ;',
  7423. 'if ($mod.b) ;',
  7424. '']));
  7425. end;
  7426. procedure TTestModule.TestForLoop;
  7427. begin
  7428. StartProgram(false);
  7429. Add('var');
  7430. Add(' vI, vJ, vN: longint;');
  7431. Add('begin');
  7432. Add(' VJ:=0;');
  7433. Add(' VN:=3;');
  7434. Add(' for VI:=1 to VN do');
  7435. Add(' begin');
  7436. Add(' VJ:=VJ+VI;');
  7437. Add(' end;');
  7438. ConvertProgram;
  7439. CheckSource('TestForLoop',
  7440. LinesToStr([ // statements
  7441. 'this.vI = 0;',
  7442. 'this.vJ = 0;',
  7443. 'this.vN = 0;'
  7444. ]),
  7445. LinesToStr([ // this.$main
  7446. ' $mod.vJ = 0;',
  7447. ' $mod.vN = 3;',
  7448. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  7449. ' $mod.vI = $l1;',
  7450. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7451. ' };',
  7452. '']));
  7453. end;
  7454. procedure TTestModule.TestForLoopInsideFunction;
  7455. begin
  7456. StartProgram(false);
  7457. Add('function SumNumbers(Count: longint): longint;');
  7458. Add('var');
  7459. Add(' vI, vJ: longint;');
  7460. Add('begin');
  7461. Add(' vj:=0;');
  7462. Add(' for vi:=1 to count do');
  7463. Add(' begin');
  7464. Add(' vj:=vj+vi;');
  7465. Add(' end;');
  7466. Add('end;');
  7467. Add('begin');
  7468. Add(' sumnumbers(3);');
  7469. ConvertProgram;
  7470. CheckSource('TestForLoopInsideFunction',
  7471. LinesToStr([ // statements
  7472. 'this.SumNumbers = function (Count) {',
  7473. ' var Result = 0;',
  7474. ' var vI = 0;',
  7475. ' var vJ = 0;',
  7476. ' vJ = 0;',
  7477. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7478. ' vI = $l1;',
  7479. ' vJ = vJ + vI;',
  7480. ' };',
  7481. ' return Result;',
  7482. '};'
  7483. ]),
  7484. LinesToStr([ // $mod.$main
  7485. ' $mod.SumNumbers(3);'
  7486. ]));
  7487. end;
  7488. procedure TTestModule.TestForLoop_ReadVarAfter;
  7489. begin
  7490. StartProgram(false);
  7491. Add('var');
  7492. Add(' vI: longint;');
  7493. Add('begin');
  7494. Add(' for vi:=1 to 2 do ;');
  7495. Add(' if vi=3 then ;');
  7496. ConvertProgram;
  7497. CheckSource('TestForLoop',
  7498. LinesToStr([ // statements
  7499. 'this.vI = 0;'
  7500. ]),
  7501. LinesToStr([ // this.$main
  7502. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7503. ' if ($mod.vI===3) ;'
  7504. ]));
  7505. end;
  7506. procedure TTestModule.TestForLoop_Nested;
  7507. begin
  7508. StartProgram(false);
  7509. Add('function SumNumbers(Count: longint): longint;');
  7510. Add('var');
  7511. Add(' vI, vJ, vK: longint;');
  7512. Add('begin');
  7513. Add(' VK:=0;');
  7514. Add(' for VI:=1 to count do');
  7515. Add(' begin');
  7516. Add(' for vj:=1 to vi do');
  7517. Add(' begin');
  7518. Add(' vk:=VK+VI;');
  7519. Add(' end;');
  7520. Add(' end;');
  7521. Add('end;');
  7522. Add('begin');
  7523. Add(' sumnumbers(3);');
  7524. ConvertProgram;
  7525. CheckSource('TestForLoopInFunction',
  7526. LinesToStr([ // statements
  7527. 'this.SumNumbers = function (Count) {',
  7528. ' var Result = 0;',
  7529. ' var vI = 0;',
  7530. ' var vJ = 0;',
  7531. ' var vK = 0;',
  7532. ' vK = 0;',
  7533. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7534. ' vI = $l1;',
  7535. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7536. ' vJ = $l3;',
  7537. ' vK = vK + vI;',
  7538. ' };',
  7539. ' };',
  7540. ' return Result;',
  7541. '};'
  7542. ]),
  7543. LinesToStr([ // $mod.$main
  7544. ' $mod.SumNumbers(3);'
  7545. ]));
  7546. end;
  7547. procedure TTestModule.TestRepeatUntil;
  7548. begin
  7549. StartProgram(false);
  7550. Add('var');
  7551. Add(' vI, vJ, vN: longint;');
  7552. Add('begin');
  7553. Add(' vn:=3;');
  7554. Add(' vj:=0;');
  7555. Add(' VI:=0;');
  7556. Add(' repeat');
  7557. Add(' VI:=vi+1;');
  7558. Add(' vj:=VJ+vI;');
  7559. Add(' until vi>=vn');
  7560. ConvertProgram;
  7561. CheckSource('TestRepeatUntil',
  7562. LinesToStr([ // statements
  7563. 'this.vI = 0;',
  7564. 'this.vJ = 0;',
  7565. 'this.vN = 0;'
  7566. ]),
  7567. LinesToStr([ // $mod.$main
  7568. ' $mod.vN = 3;',
  7569. ' $mod.vJ = 0;',
  7570. ' $mod.vI = 0;',
  7571. ' do{',
  7572. ' $mod.vI = $mod.vI + 1;',
  7573. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7574. ' }while(!($mod.vI>=$mod.vN));'
  7575. ]));
  7576. end;
  7577. procedure TTestModule.TestAsmBlock;
  7578. begin
  7579. StartProgram(false);
  7580. Add([
  7581. 'var',
  7582. ' vI: longint;',
  7583. 'begin',
  7584. ' vi:=1;',
  7585. ' asm',
  7586. ' if (vI===1) {',
  7587. ' vI=2;',
  7588. //' console.log(''end;'');', ToDo
  7589. ' }',
  7590. ' if (vI===2){ vI=3; }',
  7591. ' end;',
  7592. ' VI:=4;']);
  7593. ConvertProgram;
  7594. CheckSource('TestAsmBlock',
  7595. LinesToStr([ // statements
  7596. 'this.vI = 0;'
  7597. ]),
  7598. LinesToStr([ // $mod.$main
  7599. '$mod.vI = 1;',
  7600. 'if (vI===1) {',
  7601. ' vI=2;',
  7602. '}',
  7603. 'if (vI===2){ vI=3; }',
  7604. ';',
  7605. '$mod.vI = 4;'
  7606. ]));
  7607. end;
  7608. procedure TTestModule.TestAsmPas_Impl;
  7609. begin
  7610. StartUnit(false);
  7611. Add('interface');
  7612. Add('const cIntf: longint = 1;');
  7613. Add('var vIntf: longint;');
  7614. Add('implementation');
  7615. Add('const cImpl: longint = 2;');
  7616. Add('var vImpl: longint;');
  7617. Add('procedure DoIt;');
  7618. Add('const cLoc: longint = 3;');
  7619. Add('var vLoc: longint;');
  7620. Add('begin;');
  7621. Add(' asm');
  7622. //Add(' pas(vIntf)=pas(cIntf);');
  7623. //Add(' pas(vImpl)=pas(cImpl);');
  7624. //Add(' pas(vLoc)=pas(cLoc);');
  7625. Add(' end;');
  7626. Add('end;');
  7627. ConvertUnit;
  7628. CheckSource('TestAsmPas_Impl',
  7629. LinesToStr([
  7630. 'var $impl = $mod.$impl;',
  7631. 'this.cIntf = 1;',
  7632. 'this.vIntf = 0;',
  7633. '']),
  7634. '', // this.$init
  7635. LinesToStr([ // implementation
  7636. '$impl.cImpl = 2;',
  7637. '$impl.vImpl = 0;',
  7638. 'var cLoc = 3;',
  7639. '$impl.DoIt = function () {',
  7640. ' var vLoc = 0;',
  7641. '};',
  7642. '']) );
  7643. end;
  7644. procedure TTestModule.TestTryFinally;
  7645. begin
  7646. StartProgram(false);
  7647. Add('var i: longint;');
  7648. Add('begin');
  7649. Add(' try');
  7650. Add(' i:=0; i:=2 div i;');
  7651. Add(' finally');
  7652. Add(' i:=3');
  7653. Add(' end;');
  7654. ConvertProgram;
  7655. CheckSource('TestTryFinally',
  7656. LinesToStr([ // statements
  7657. 'this.i = 0;'
  7658. ]),
  7659. LinesToStr([ // $mod.$main
  7660. 'try {',
  7661. ' $mod.i = 0;',
  7662. ' $mod.i = Math.floor(2 / $mod.i);',
  7663. '} finally {',
  7664. ' $mod.i = 3;',
  7665. '};'
  7666. ]));
  7667. end;
  7668. procedure TTestModule.TestTryExcept;
  7669. begin
  7670. StartProgram(false);
  7671. Add('type');
  7672. Add(' TObject = class end;');
  7673. Add(' Exception = class Msg: string; end;');
  7674. Add(' EInvalidCast = class(Exception) end;');
  7675. Add('var vI: longint;');
  7676. Add('begin');
  7677. Add(' try');
  7678. Add(' vi:=1;');
  7679. Add(' except');
  7680. Add(' vi:=2');
  7681. Add(' end;');
  7682. Add(' try');
  7683. Add(' vi:=3;');
  7684. Add(' except');
  7685. Add(' raise;');
  7686. Add(' end;');
  7687. Add(' try');
  7688. Add(' VI:=4;');
  7689. Add(' except');
  7690. Add(' on einvalidcast do');
  7691. Add(' raise;');
  7692. Add(' on E: exception do');
  7693. Add(' if e.msg='''' then');
  7694. Add(' raise e;');
  7695. Add(' else');
  7696. Add(' vi:=5');
  7697. Add(' end;');
  7698. Add(' try');
  7699. Add(' VI:=6;');
  7700. Add(' except');
  7701. Add(' on einvalidcast do ;');
  7702. Add(' end;');
  7703. ConvertProgram;
  7704. CheckSource('TestTryExcept',
  7705. LinesToStr([ // statements
  7706. 'rtl.createClass($mod, "TObject", null, function () {',
  7707. ' this.$init = function () {',
  7708. ' };',
  7709. ' this.$final = function () {',
  7710. ' };',
  7711. '});',
  7712. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7713. ' this.$init = function () {',
  7714. ' $mod.TObject.$init.call(this);',
  7715. ' this.Msg = "";',
  7716. ' };',
  7717. '});',
  7718. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7719. '});',
  7720. 'this.vI = 0;'
  7721. ]),
  7722. LinesToStr([ // $mod.$main
  7723. 'try {',
  7724. ' $mod.vI = 1;',
  7725. '} catch ($e) {',
  7726. ' $mod.vI = 2;',
  7727. '};',
  7728. 'try {',
  7729. ' $mod.vI = 3;',
  7730. '} catch ($e) {',
  7731. ' throw $e;',
  7732. '};',
  7733. 'try {',
  7734. ' $mod.vI = 4;',
  7735. '} catch ($e) {',
  7736. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7737. ' throw $e',
  7738. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7739. ' var E = $e;',
  7740. ' if (E.Msg === "") throw E;',
  7741. ' } else {',
  7742. ' $mod.vI = 5;',
  7743. ' }',
  7744. '};',
  7745. 'try {',
  7746. ' $mod.vI = 6;',
  7747. '} catch ($e) {',
  7748. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7749. ' } else throw $e',
  7750. '};',
  7751. '']));
  7752. end;
  7753. procedure TTestModule.TestTryExcept_ReservedWords;
  7754. begin
  7755. StartProgram(false);
  7756. Add([
  7757. 'type',
  7758. ' TObject = class end;',
  7759. ' Exception = class',
  7760. ' Symbol: string;',
  7761. ' end;',
  7762. 'var &try: longint;',
  7763. 'begin',
  7764. ' try',
  7765. ' &try:=4;',
  7766. ' except',
  7767. ' on Error: exception do',
  7768. ' if errOR.symBol='''' then',
  7769. ' raise ERRor;',
  7770. ' end;',
  7771. '']);
  7772. ConvertProgram;
  7773. CheckSource('TestTryExcept_ReservedWords',
  7774. LinesToStr([ // statements
  7775. 'rtl.createClass($mod, "TObject", null, function () {',
  7776. ' this.$init = function () {',
  7777. ' };',
  7778. ' this.$final = function () {',
  7779. ' };',
  7780. '});',
  7781. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7782. ' this.$init = function () {',
  7783. ' $mod.TObject.$init.call(this);',
  7784. ' this.Symbol = "";',
  7785. ' };',
  7786. '});',
  7787. 'this.Try = 0;',
  7788. '']),
  7789. LinesToStr([ // $mod.$main
  7790. 'try {',
  7791. ' $mod.Try = 4;',
  7792. '} catch ($e) {',
  7793. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7794. ' var error = $e;',
  7795. ' if (error.Symbol === "") throw error;',
  7796. ' } else throw $e',
  7797. '};',
  7798. '']));
  7799. end;
  7800. procedure TTestModule.TestIfThenRaiseElse;
  7801. begin
  7802. StartProgram(false);
  7803. Add([
  7804. 'type',
  7805. ' TObject = class',
  7806. ' constructor Create;',
  7807. ' end;',
  7808. 'constructor TObject.Create;',
  7809. 'begin',
  7810. 'end;',
  7811. 'var b: boolean;',
  7812. 'begin',
  7813. ' if b then',
  7814. ' raise TObject.Create',
  7815. ' else',
  7816. ' b:=false;',
  7817. '']);
  7818. ConvertProgram;
  7819. CheckSource('TestIfThenRaiseElse',
  7820. LinesToStr([ // statements
  7821. 'rtl.createClass($mod, "TObject", null, function () {',
  7822. ' this.$init = function () {',
  7823. ' };',
  7824. ' this.$final = function () {',
  7825. ' };',
  7826. ' this.Create = function () {',
  7827. ' return this;',
  7828. ' };',
  7829. '});',
  7830. 'this.b = false;',
  7831. '']),
  7832. LinesToStr([ // $mod.$main
  7833. 'if ($mod.b) {',
  7834. ' throw $mod.TObject.$create("Create")}',
  7835. ' else $mod.b = false;',
  7836. '']));
  7837. end;
  7838. procedure TTestModule.TestCaseOf;
  7839. begin
  7840. StartProgram(false);
  7841. Add([
  7842. 'const e: longint; external name ''$e'';',
  7843. 'var vI: longint;',
  7844. 'begin',
  7845. ' case vi of',
  7846. ' 1: ;',
  7847. ' 2: vi:=3;',
  7848. ' e: ;',
  7849. ' else',
  7850. ' VI:=4',
  7851. ' end;']);
  7852. ConvertProgram;
  7853. CheckSource('TestCaseOf',
  7854. LinesToStr([ // statements
  7855. 'this.vI = 0;'
  7856. ]),
  7857. LinesToStr([ // $mod.$main
  7858. 'var $tmp1 = $mod.vI;',
  7859. 'if ($tmp1 === 1) {}',
  7860. 'else if ($tmp1 === 2) {',
  7861. ' $mod.vI = 3}',
  7862. ' else if ($tmp1 === $e) {}',
  7863. 'else {',
  7864. ' $mod.vI = 4;',
  7865. '};'
  7866. ]));
  7867. end;
  7868. procedure TTestModule.TestCaseOf_UseSwitch;
  7869. begin
  7870. StartProgram(false);
  7871. Converter.UseSwitchStatement:=true;
  7872. Add('var Vi: longint;');
  7873. Add('begin');
  7874. Add(' case vi of');
  7875. Add(' 1: ;');
  7876. Add(' 2: VI:=3;');
  7877. Add(' else');
  7878. Add(' vi:=4');
  7879. Add(' end;');
  7880. ConvertProgram;
  7881. CheckSource('TestCaseOf_UseSwitch',
  7882. LinesToStr([ // statements
  7883. 'this.Vi = 0;'
  7884. ]),
  7885. LinesToStr([ // $mod.$main
  7886. 'switch ($mod.Vi) {',
  7887. 'case 1:',
  7888. ' break;',
  7889. 'case 2:',
  7890. ' $mod.Vi = 3;',
  7891. ' break;',
  7892. 'default:',
  7893. ' $mod.Vi = 4;',
  7894. '};'
  7895. ]));
  7896. end;
  7897. procedure TTestModule.TestCaseOfNoElse;
  7898. begin
  7899. StartProgram(false);
  7900. Add('var Vi: longint;');
  7901. Add('begin');
  7902. Add(' case vi of');
  7903. Add(' 1: begin vi:=2; VI:=3; end;');
  7904. Add(' end;');
  7905. ConvertProgram;
  7906. CheckSource('TestCaseOfNoElse',
  7907. LinesToStr([ // statements
  7908. 'this.Vi = 0;'
  7909. ]),
  7910. LinesToStr([ // $mod.$main
  7911. 'var $tmp1 = $mod.Vi;',
  7912. 'if ($tmp1 === 1) {',
  7913. ' $mod.Vi = 2;',
  7914. ' $mod.Vi = 3;',
  7915. '};'
  7916. ]));
  7917. end;
  7918. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7919. begin
  7920. StartProgram(false);
  7921. Converter.UseSwitchStatement:=true;
  7922. Add('var vI: longint;');
  7923. Add('begin');
  7924. Add(' case vi of');
  7925. Add(' 1: begin VI:=2; vi:=3; end;');
  7926. Add(' end;');
  7927. ConvertProgram;
  7928. CheckSource('TestCaseOfNoElse_UseSwitch',
  7929. LinesToStr([ // statements
  7930. 'this.vI = 0;'
  7931. ]),
  7932. LinesToStr([ // $mod.$main
  7933. 'switch ($mod.vI) {',
  7934. 'case 1:',
  7935. ' $mod.vI = 2;',
  7936. ' $mod.vI = 3;',
  7937. ' break;',
  7938. '};'
  7939. ]));
  7940. end;
  7941. procedure TTestModule.TestCaseOfRange;
  7942. begin
  7943. StartProgram(false);
  7944. Add('var vI: longint;');
  7945. Add('begin');
  7946. Add(' case vi of');
  7947. Add(' 1..3: vi:=14;');
  7948. Add(' 4,5: vi:=16;');
  7949. Add(' 6..7,9..10: ;');
  7950. Add(' else ;');
  7951. Add(' end;');
  7952. ConvertProgram;
  7953. CheckSource('TestCaseOfRange',
  7954. LinesToStr([ // statements
  7955. 'this.vI = 0;'
  7956. ]),
  7957. LinesToStr([ // $mod.$main
  7958. 'var $tmp1 = $mod.vI;',
  7959. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  7960. ' $mod.vI = 14',
  7961. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  7962. ' $mod.vI = 16',
  7963. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  7964. ]));
  7965. end;
  7966. procedure TTestModule.TestCaseOfString;
  7967. begin
  7968. StartProgram(false);
  7969. Add([
  7970. 'var s,h: string;',
  7971. 'begin',
  7972. ' case s of',
  7973. ' ''foo'': s:=h;',
  7974. ' ''a''..''z'': h:=s;',
  7975. ' ''ў'', ''ё'': ;',
  7976. ' ''Б''..''Я'': ;',
  7977. ' end;',
  7978. '']);
  7979. ConvertProgram;
  7980. CheckSource('TestCaseOfString',
  7981. LinesToStr([ // statements
  7982. 'this.s = "";',
  7983. 'this.h = "";',
  7984. '']),
  7985. LinesToStr([ // $mod.$main
  7986. 'var $tmp1 = $mod.s;',
  7987. 'if ($tmp1 === "foo") {',
  7988. ' $mod.s = $mod.h}',
  7989. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7990. ' $mod.h = $mod.s}',
  7991. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  7992. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7993. '']));
  7994. end;
  7995. procedure TTestModule.TestCaseOfChar;
  7996. begin
  7997. StartProgram(false);
  7998. Add([
  7999. 'var s,h: char;',
  8000. 'begin',
  8001. ' case s of',
  8002. ' ''a''..''z'': h:=s;',
  8003. ' ''ä'': ;',
  8004. ' ''ў'', ''ё'': ;',
  8005. ' ''Б''..''Я'': ;',
  8006. ' end;',
  8007. '']);
  8008. ConvertProgram;
  8009. CheckSource('TestCaseOfString',
  8010. LinesToStr([ // statements
  8011. 'this.s = "";',
  8012. 'this.h = "";',
  8013. '']),
  8014. LinesToStr([ // $mod.$main
  8015. 'var $tmp1 = $mod.s;',
  8016. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  8017. ' $mod.h = $mod.s}',
  8018. ' else if ($tmp1 === "ä") {}',
  8019. ' else if (($tmp1 === "ў") || ($tmp1 === "ё")) {}',
  8020. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  8021. '']));
  8022. end;
  8023. procedure TTestModule.TestCaseOfExternalClassConst;
  8024. begin
  8025. StartProgram(false);
  8026. Add([
  8027. '{$modeswitch externalclass}',
  8028. 'type',
  8029. ' TBird = class external name ''Bird''',
  8030. ' const e: longint;',
  8031. ' end;',
  8032. 'var vI: longint;',
  8033. 'begin',
  8034. ' case vi of',
  8035. ' 1: vi:=3;',
  8036. ' TBird.e: ;',
  8037. ' end;']);
  8038. ConvertProgram;
  8039. CheckSource('TestCaseOfExternalClassConst',
  8040. LinesToStr([ // statements
  8041. 'this.vI = 0;'
  8042. ]),
  8043. LinesToStr([ // $mod.$main
  8044. 'var $tmp1 = $mod.vI;',
  8045. 'if ($tmp1 === 1) {',
  8046. ' $mod.vI = 3}',
  8047. ' else if ($tmp1 === Bird.e) ;'
  8048. ]));
  8049. end;
  8050. procedure TTestModule.TestDebugger;
  8051. begin
  8052. StartProgram(false);
  8053. Add([
  8054. 'procedure DoIt;',
  8055. 'begin',
  8056. ' deBugger;',
  8057. ' DeBugger();',
  8058. 'end;',
  8059. 'begin',
  8060. ' Debugger;']);
  8061. ConvertProgram;
  8062. CheckSource('TestDebugger',
  8063. LinesToStr([ // statements
  8064. 'this.DoIt = function () {',
  8065. ' debugger;',
  8066. ' debugger;',
  8067. '};',
  8068. '']),
  8069. LinesToStr([ // $mod.$main
  8070. 'debugger;',
  8071. '']));
  8072. end;
  8073. procedure TTestModule.TestArray_Dynamic;
  8074. begin
  8075. StartProgram(false);
  8076. Add([
  8077. 'type',
  8078. ' TArrayInt = array of longint;',
  8079. 'var',
  8080. ' Arr: TArrayInt;',
  8081. ' i: longint;',
  8082. ' b: boolean;',
  8083. 'begin',
  8084. ' SetLength(arr,3);',
  8085. ' arr[0]:=4;',
  8086. ' arr[1]:=length(arr)+arr[0];',
  8087. ' arr[i]:=5;',
  8088. ' arr[arr[i]]:=arr[6];',
  8089. ' i:=low(arr);',
  8090. ' i:=high(arr);',
  8091. ' b:=Assigned(arr);',
  8092. ' Arr:=default(TArrayInt);']);
  8093. ConvertProgram;
  8094. CheckSource('TestArray_Dynamic',
  8095. LinesToStr([ // statements
  8096. 'this.Arr = [];',
  8097. 'this.i = 0;',
  8098. 'this.b = false;'
  8099. ]),
  8100. LinesToStr([ // $mod.$main
  8101. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8102. '$mod.Arr[0] = 4;',
  8103. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8104. '$mod.Arr[$mod.i] = 5;',
  8105. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8106. '$mod.i = 0;',
  8107. '$mod.i = rtl.length($mod.Arr) - 1;',
  8108. '$mod.b = rtl.length($mod.Arr) > 0;',
  8109. '$mod.Arr = [];',
  8110. '']));
  8111. end;
  8112. procedure TTestModule.TestArray_Dynamic_Nil;
  8113. begin
  8114. StartProgram(false);
  8115. Add('type');
  8116. Add(' TArrayInt = array of longint;');
  8117. Add('var');
  8118. Add(' Arr: TArrayInt;');
  8119. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8120. Add('begin');
  8121. Add(' arr:=nil;');
  8122. Add(' if arr=nil then;');
  8123. Add(' if nil=arr then;');
  8124. Add(' if arr<>nil then;');
  8125. Add(' if nil<>arr then;');
  8126. Add(' DoIt(nil,nil);');
  8127. ConvertProgram;
  8128. CheckSource('TestArray_Dynamic',
  8129. LinesToStr([ // statements
  8130. 'this.Arr = [];',
  8131. 'this.DoIt = function(i,j){',
  8132. '};'
  8133. ]),
  8134. LinesToStr([ // $mod.$main
  8135. '$mod.Arr = [];',
  8136. 'if (rtl.length($mod.Arr) === 0) ;',
  8137. 'if (rtl.length($mod.Arr) === 0) ;',
  8138. 'if (rtl.length($mod.Arr) > 0) ;',
  8139. 'if (rtl.length($mod.Arr) > 0) ;',
  8140. '$mod.DoIt([],[]);',
  8141. '']));
  8142. end;
  8143. procedure TTestModule.TestArray_DynMultiDimensional;
  8144. begin
  8145. StartProgram(false);
  8146. Add('type');
  8147. Add(' TArrayInt = array of longint;');
  8148. Add(' TArrayArrayInt = array of TArrayInt;');
  8149. Add('var');
  8150. Add(' Arr: TArrayInt;');
  8151. Add(' Arr2: TArrayArrayInt;');
  8152. Add(' i: longint;');
  8153. Add('begin');
  8154. Add(' arr2:=nil;');
  8155. Add(' if arr2=nil then;');
  8156. Add(' if nil=arr2 then;');
  8157. Add(' i:=low(arr2);');
  8158. Add(' i:=low(arr2[1]);');
  8159. Add(' i:=high(arr2);');
  8160. Add(' i:=high(arr2[2]);');
  8161. Add(' arr2[3]:=arr;');
  8162. Add(' arr2[4][5]:=i;');
  8163. Add(' i:=arr2[6][7];');
  8164. Add(' arr2[8,9]:=i;');
  8165. Add(' i:=arr2[10,11];');
  8166. Add(' SetLength(arr2,14);');
  8167. Add(' SetLength(arr2[15],16);');
  8168. ConvertProgram;
  8169. CheckSource('TestArray_Dynamic',
  8170. LinesToStr([ // statements
  8171. 'this.Arr = [];',
  8172. 'this.Arr2 = [];',
  8173. 'this.i = 0;'
  8174. ]),
  8175. LinesToStr([ // $mod.$main
  8176. '$mod.Arr2 = [];',
  8177. 'if (rtl.length($mod.Arr2) === 0) ;',
  8178. 'if (rtl.length($mod.Arr2) === 0) ;',
  8179. '$mod.i = 0;',
  8180. '$mod.i = 0;',
  8181. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8182. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8183. '$mod.Arr2[3] = $mod.Arr;',
  8184. '$mod.Arr2[4][5] = $mod.i;',
  8185. '$mod.i = $mod.Arr2[6][7];',
  8186. '$mod.Arr2[8][9] = $mod.i;',
  8187. '$mod.i = $mod.Arr2[10][11];',
  8188. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8189. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8190. '']));
  8191. end;
  8192. procedure TTestModule.TestArray_StaticInt;
  8193. begin
  8194. StartProgram(false);
  8195. Add('type');
  8196. Add(' TArrayInt = array[2..4] of longint;');
  8197. Add('var');
  8198. Add(' Arr: TArrayInt;');
  8199. Add(' Arr2: TArrayInt = (5,6,7);');
  8200. Add(' i: longint;');
  8201. Add(' b: boolean;');
  8202. Add('begin');
  8203. Add(' arr[2]:=4;');
  8204. Add(' arr[3]:=arr[2]+arr[3];');
  8205. Add(' arr[i]:=5;');
  8206. Add(' arr[arr[i]]:=arr[high(arr)];');
  8207. Add(' i:=low(arr);');
  8208. Add(' i:=high(arr);');
  8209. Add(' b:=arr[2]=arr[3];');
  8210. Add(' arr:=default(TArrayInt);');
  8211. ConvertProgram;
  8212. CheckSource('TestArray_StaticInt',
  8213. LinesToStr([ // statements
  8214. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8215. 'this.Arr2 = [5, 6, 7];',
  8216. 'this.i = 0;',
  8217. 'this.b = false;'
  8218. ]),
  8219. LinesToStr([ // $mod.$main
  8220. '$mod.Arr[0] = 4;',
  8221. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8222. '$mod.Arr[$mod.i-2] = 5;',
  8223. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8224. '$mod.i = 2;',
  8225. '$mod.i = 4;',
  8226. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8227. '$mod.Arr = rtl.arraySetLength(null,0,3).slice(0);',
  8228. '']));
  8229. end;
  8230. procedure TTestModule.TestArray_StaticBool;
  8231. begin
  8232. StartProgram(false);
  8233. Add('type');
  8234. Add(' TBools = array[boolean] of boolean;');
  8235. Add(' TBool2 = array[true..true] of boolean;');
  8236. Add('var');
  8237. Add(' Arr: TBools;');
  8238. Add(' Arr2: TBool2;');
  8239. Add(' Arr3: TBools = (true,false);');
  8240. Add(' b: boolean;');
  8241. Add('begin');
  8242. Add(' b:=low(arr);');
  8243. Add(' b:=high(arr);');
  8244. Add(' arr[true]:=false;');
  8245. Add(' arr[false]:=arr[b] or arr[true];');
  8246. Add(' arr[b]:=true;');
  8247. Add(' arr[arr[b]]:=arr[high(arr)];');
  8248. Add(' b:=arr[false]=arr[true];');
  8249. Add(' b:=low(arr2);');
  8250. Add(' b:=high(arr2);');
  8251. Add(' arr2[true]:=true;');
  8252. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8253. Add(' arr2[b]:=false;');
  8254. ConvertProgram;
  8255. CheckSource('TestArray_StaticBool',
  8256. LinesToStr([ // statements
  8257. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8258. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8259. 'this.Arr3 = [true, false];',
  8260. 'this.b = false;'
  8261. ]),
  8262. LinesToStr([ // $mod.$main
  8263. '$mod.b = false;',
  8264. '$mod.b = true;',
  8265. '$mod.Arr[1] = false;',
  8266. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8267. '$mod.Arr[+$mod.b] = true;',
  8268. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8269. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8270. '$mod.b = true;',
  8271. '$mod.b = true;',
  8272. '$mod.Arr2[0] = true;',
  8273. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8274. '$mod.Arr2[1-$mod.b] = false;',
  8275. '']));
  8276. end;
  8277. procedure TTestModule.TestArray_StaticChar;
  8278. begin
  8279. StartProgram(false);
  8280. Add([
  8281. 'type',
  8282. ' TChars = array[char] of char;',
  8283. ' TChars2 = array[''a''..''z''] of char;',
  8284. 'var',
  8285. ' Arr: TChars;',
  8286. ' Arr2: TChars2;',
  8287. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8288. ' Arr4: array[11..13] of char = ''pas'';',
  8289. ' Arr5: array[21..22] of char = ''äö'';',
  8290. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8291. ' c: char;',
  8292. ' b: boolean;',
  8293. 'begin',
  8294. ' c:=low(arr);',
  8295. ' c:=high(arr);',
  8296. ' arr[''B'']:=''a'';',
  8297. ' arr[''D'']:=arr[c];',
  8298. ' arr[c]:=arr[''d''];',
  8299. ' arr[arr[c]]:=arr[high(arr)];',
  8300. ' b:=arr[low(arr)]=arr[''e''];',
  8301. ' c:=low(arr2);',
  8302. ' c:=high(arr2);',
  8303. ' arr2[''b'']:=''f'';',
  8304. ' arr2[''a'']:=arr2[c];',
  8305. ' arr2[c]:=arr2[''g''];']);
  8306. ConvertProgram;
  8307. CheckSource('TestArray_StaticChar',
  8308. LinesToStr([ // statements
  8309. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8310. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8311. 'this.Arr3 = ["p", "a", "s"];',
  8312. 'this.Arr4 = ["p", "a", "s"];',
  8313. 'this.Arr5 = ["ä", "ö"];',
  8314. 'this.Arr6 = ["ä", "ö"];',
  8315. 'this.c = "";',
  8316. 'this.b = false;',
  8317. '']),
  8318. LinesToStr([ // $mod.$main
  8319. '$mod.c = "\x00";',
  8320. '$mod.c = "\uFFFF";',
  8321. '$mod.Arr[66] = "a";',
  8322. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8323. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8324. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8325. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8326. '$mod.c = "a";',
  8327. '$mod.c = "z";',
  8328. '$mod.Arr2[1] = "f";',
  8329. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8330. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8331. '']));
  8332. end;
  8333. procedure TTestModule.TestArray_StaticMultiDim;
  8334. begin
  8335. StartProgram(false);
  8336. Add([
  8337. 'type',
  8338. ' TArrayInt = array[1..3] of longint;',
  8339. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8340. 'var',
  8341. ' Arr: TArrayInt;',
  8342. ' Arr2: TArrayArrayInt;',
  8343. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8344. ' i: longint;',
  8345. 'begin',
  8346. ' i:=low(arr);',
  8347. ' i:=low(arr2);',
  8348. ' i:=low(arr2[5]);',
  8349. ' i:=high(arr);',
  8350. ' i:=high(arr2);',
  8351. ' i:=high(arr2[6]);',
  8352. ' arr2[5]:=arr;',
  8353. ' arr2[6][2]:=i;',
  8354. ' i:=arr2[6][3];',
  8355. ' arr2[6,3]:=i;',
  8356. ' i:=arr2[5,2];',
  8357. ' arr2:=arr2;',// clone multi dim static array
  8358. //' arr3:=arr3;',// clone anonymous multi dim static array
  8359. '']);
  8360. ConvertProgram;
  8361. CheckSource('TestArray_StaticMultiDim',
  8362. LinesToStr([ // statements
  8363. 'this.TArrayArrayInt$clone = function (a) {',
  8364. ' var r = [];',
  8365. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8366. ' return r;',
  8367. '};',
  8368. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8369. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8370. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8371. 'this.i = 0;'
  8372. ]),
  8373. LinesToStr([ // $mod.$main
  8374. '$mod.i = 1;',
  8375. '$mod.i = 5;',
  8376. '$mod.i = 1;',
  8377. '$mod.i = 3;',
  8378. '$mod.i = 6;',
  8379. '$mod.i = 3;',
  8380. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8381. '$mod.Arr2[1][1] = $mod.i;',
  8382. '$mod.i = $mod.Arr2[1][2];',
  8383. '$mod.Arr2[1][2] = $mod.i;',
  8384. '$mod.i = $mod.Arr2[0][1];',
  8385. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8386. '']));
  8387. end;
  8388. procedure TTestModule.TestArray_StaticInFunction;
  8389. begin
  8390. StartProgram(false);
  8391. Add([
  8392. 'const TArrayInt = 3;',
  8393. 'const TArrayArrayInt = 4;',
  8394. 'procedure DoIt;',
  8395. 'type',
  8396. ' TArrayInt = array[1..3] of longint;',
  8397. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8398. 'var',
  8399. ' Arr: TArrayInt;',
  8400. ' Arr2: TArrayArrayInt;',
  8401. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8402. ' i: longint;',
  8403. 'begin',
  8404. ' arr2[5]:=arr;',
  8405. ' arr2:=arr2;',// clone multi dim static array
  8406. 'end;',
  8407. 'begin',
  8408. '']);
  8409. ConvertProgram;
  8410. CheckSource('TestArray_StaticInFunction',
  8411. LinesToStr([ // statements
  8412. 'this.TArrayInt = 3;',
  8413. 'this.TArrayArrayInt = 4;',
  8414. 'var TArrayArrayInt$1$clone = function (a) {',
  8415. ' var r = [];',
  8416. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8417. ' return r;',
  8418. '};',
  8419. 'this.DoIt = function () {',
  8420. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8421. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8422. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8423. ' var i = 0;',
  8424. ' Arr2[0] = Arr.slice(0);',
  8425. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8426. '};',
  8427. '']),
  8428. LinesToStr([ // $mod.$main
  8429. '']));
  8430. end;
  8431. procedure TTestModule.TestArrayOfRecord;
  8432. begin
  8433. StartProgram(false);
  8434. Add([
  8435. 'type',
  8436. ' TRec = record',
  8437. ' Int: longint;',
  8438. ' end;',
  8439. ' TArrayRec = array of TRec;',
  8440. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8441. 'begin',
  8442. 'end;',
  8443. 'var',
  8444. ' Arr: TArrayRec;',
  8445. ' r: TRec;',
  8446. ' i: longint;',
  8447. 'begin',
  8448. ' SetLength(arr,3);',
  8449. ' arr[0].int:=4;',
  8450. ' arr[1].int:=length(arr)+arr[2].int;',
  8451. ' arr[arr[i].int].int:=arr[5].int;',
  8452. ' arr[7]:=r;',
  8453. ' r:=arr[8];',
  8454. ' i:=low(arr);',
  8455. ' i:=high(arr);',
  8456. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8457. ConvertProgram;
  8458. CheckSource('TestArrayOfRecord',
  8459. LinesToStr([ // statements
  8460. 'rtl.recNewT($mod, "TRec", function () {',
  8461. ' this.Int = 0;',
  8462. ' this.$eq = function (b) {',
  8463. ' return this.Int === b.Int;',
  8464. ' };',
  8465. ' this.$assign = function (s) {',
  8466. ' this.Int = s.Int;',
  8467. ' return this;',
  8468. ' };',
  8469. '});',
  8470. 'this.DoIt = function (vd, vc, vv) {',
  8471. '};',
  8472. 'this.Arr = [];',
  8473. 'this.r = $mod.TRec.$new();',
  8474. 'this.i = 0;'
  8475. ]),
  8476. LinesToStr([ // $mod.$main
  8477. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8478. '$mod.Arr[0].Int = 4;',
  8479. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8480. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8481. '$mod.Arr[7].$assign($mod.r);',
  8482. '$mod.r.$assign($mod.Arr[8]);',
  8483. '$mod.i = 0;',
  8484. '$mod.i = rtl.length($mod.Arr)-1;',
  8485. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8486. '']));
  8487. end;
  8488. procedure TTestModule.TestArray_StaticRecord;
  8489. begin
  8490. StartProgram(false);
  8491. Add([
  8492. 'type',
  8493. ' TRec = record',
  8494. ' Int: longint;',
  8495. ' end;',
  8496. ' TArrayRec = array[1..2] of TRec;',
  8497. 'var',
  8498. ' Arr: TArrayRec;',
  8499. 'begin',
  8500. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8501. '']);
  8502. ConvertProgram;
  8503. CheckSource('TestArray_StaticRecord',
  8504. LinesToStr([ // statements
  8505. 'rtl.recNewT($mod, "TRec", function () {',
  8506. ' this.Int = 0;',
  8507. ' this.$eq = function (b) {',
  8508. ' return this.Int === b.Int;',
  8509. ' };',
  8510. ' this.$assign = function (s) {',
  8511. ' this.Int = s.Int;',
  8512. ' return this;',
  8513. ' };',
  8514. '});',
  8515. 'this.TArrayRec$clone = function (a) {',
  8516. ' var r = [];',
  8517. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8518. ' return r;',
  8519. '};',
  8520. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8521. '']),
  8522. LinesToStr([ // $mod.$main
  8523. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8524. end;
  8525. procedure TTestModule.TestArrayOfSet;
  8526. begin
  8527. StartProgram(false);
  8528. Add([
  8529. 'type',
  8530. ' TFlag = (big,small);',
  8531. ' TSetOfFlag = set of tflag;',
  8532. ' TArrayFlag = array of TSetOfFlag;',
  8533. 'procedure DoIt(const a: Tarrayflag);',
  8534. 'begin',
  8535. 'end;',
  8536. 'var',
  8537. ' f: TFlag;',
  8538. ' s: TSetOfFlag;',
  8539. ' Arr: TArrayFlag;',
  8540. ' i: longint;',
  8541. 'begin',
  8542. ' SetLength(arr,3);',
  8543. ' arr[0]:=s;',
  8544. ' arr[1]:=[big];',
  8545. ' arr[2]:=[big]+s;',
  8546. ' arr[3]:=s+[big];',
  8547. ' arr[4]:=arr[5];',
  8548. ' s:=arr[6];',
  8549. ' i:=low(arr);',
  8550. ' i:=high(arr);',
  8551. ' DoIt(arr);',
  8552. ' DoIt([s]);',
  8553. ' DoIt([[],s]);',
  8554. ' DoIt([s,[]]);',
  8555. '']);
  8556. ConvertProgram;
  8557. CheckSource('TestArrayOfSet',
  8558. LinesToStr([ // statements
  8559. 'this.TFlag = {',
  8560. ' "0": "big",',
  8561. ' big: 0,',
  8562. ' "1": "small",',
  8563. ' small: 1',
  8564. '};',
  8565. 'this.DoIt = function (a) {',
  8566. '};',
  8567. 'this.f = 0;',
  8568. 'this.s = {};',
  8569. 'this.Arr = [];',
  8570. 'this.i = 0;',
  8571. '']),
  8572. LinesToStr([ // $mod.$main
  8573. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8574. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8575. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8576. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8577. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8578. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8579. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8580. '$mod.i = 0;',
  8581. '$mod.i = rtl.length($mod.Arr) - 1;',
  8582. '$mod.DoIt($mod.Arr);',
  8583. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8584. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8585. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8586. '']));
  8587. end;
  8588. procedure TTestModule.TestArray_DynAsParam;
  8589. begin
  8590. StartProgram(false);
  8591. Add([
  8592. 'type integer = longint;',
  8593. 'type TArrInt = array of integer;',
  8594. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8595. 'var vJ: TArrInt;',
  8596. 'begin',
  8597. ' vg:=vg;',
  8598. ' vj:=vh;',
  8599. ' vi:=vi;',
  8600. ' doit(vg,vg,vg);',
  8601. ' doit(vh,vh,vj);',
  8602. ' doit(vi,vi,vi);',
  8603. ' doit(vj,vj,vj);',
  8604. 'end;',
  8605. 'var i: TArrInt;',
  8606. 'begin',
  8607. ' doit(i,i,i);']);
  8608. ConvertProgram;
  8609. CheckSource('TestArray_DynAsParams',
  8610. LinesToStr([ // statements
  8611. 'this.DoIt = function (vG,vH,vI) {',
  8612. ' var vJ = [];',
  8613. ' vG = vG;',
  8614. ' vJ = vH;',
  8615. ' vI.set(vI.get());',
  8616. ' $mod.DoIt(vG, vG, {',
  8617. ' get: function () {',
  8618. ' return vG;',
  8619. ' },',
  8620. ' set: function (v) {',
  8621. ' vG = v;',
  8622. ' }',
  8623. ' });',
  8624. ' $mod.DoIt(vH, vH, {',
  8625. ' get: function () {',
  8626. ' return vJ;',
  8627. ' },',
  8628. ' set: function (v) {',
  8629. ' vJ = v;',
  8630. ' }',
  8631. ' });',
  8632. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8633. ' $mod.DoIt(vJ, vJ, {',
  8634. ' get: function () {',
  8635. ' return vJ;',
  8636. ' },',
  8637. ' set: function (v) {',
  8638. ' vJ = v;',
  8639. ' }',
  8640. ' });',
  8641. '};',
  8642. 'this.i = [];'
  8643. ]),
  8644. LinesToStr([
  8645. '$mod.DoIt($mod.i,$mod.i,{',
  8646. ' p: $mod,',
  8647. ' get: function () {',
  8648. ' return this.p.i;',
  8649. ' },',
  8650. ' set: function (v) {',
  8651. ' this.p.i = v;',
  8652. ' }',
  8653. '});'
  8654. ]));
  8655. end;
  8656. procedure TTestModule.TestArray_StaticAsParam;
  8657. begin
  8658. StartProgram(false);
  8659. Add([
  8660. 'type integer = longint;',
  8661. 'type TArrInt = array[1..2] of integer;',
  8662. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8663. 'var vJ: TArrInt;',
  8664. 'begin',
  8665. ' vg:=vg;',
  8666. ' vj:=vh;',
  8667. ' vi:=vi;',
  8668. ' doit(vg,vg,vg);',
  8669. ' doit(vh,vh,vj);',
  8670. ' doit(vi,vi,vi);',
  8671. ' doit(vj,vj,vj);',
  8672. 'end;',
  8673. 'var i: TArrInt;',
  8674. 'begin',
  8675. ' doit(i,i,i);']);
  8676. ConvertProgram;
  8677. CheckSource('TestArray_StaticAsParams',
  8678. LinesToStr([ // statements
  8679. 'this.DoIt = function (vG,vH,vI) {',
  8680. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8681. ' vG = vG.slice(0);',
  8682. ' vJ = vH.slice(0);',
  8683. ' vI.set(vI.get().slice(0));',
  8684. ' $mod.DoIt(vG.slice(0), vG, {',
  8685. ' get: function () {',
  8686. ' return vG;',
  8687. ' },',
  8688. ' set: function (v) {',
  8689. ' vG = v;',
  8690. ' }',
  8691. ' });',
  8692. ' $mod.DoIt(vH.slice(0), vH, {',
  8693. ' get: function () {',
  8694. ' return vJ;',
  8695. ' },',
  8696. ' set: function (v) {',
  8697. ' vJ = v;',
  8698. ' }',
  8699. ' });',
  8700. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8701. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8702. ' get: function () {',
  8703. ' return vJ;',
  8704. ' },',
  8705. ' set: function (v) {',
  8706. ' vJ = v;',
  8707. ' }',
  8708. ' });',
  8709. '};',
  8710. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8711. ]),
  8712. LinesToStr([
  8713. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  8714. ' p: $mod,',
  8715. ' get: function () {',
  8716. ' return this.p.i;',
  8717. ' },',
  8718. ' set: function (v) {',
  8719. ' this.p.i = v;',
  8720. ' }',
  8721. '});'
  8722. ]));
  8723. end;
  8724. procedure TTestModule.TestArrayElement_AsParams;
  8725. begin
  8726. StartProgram(false);
  8727. Add('type integer = longint;');
  8728. Add('type TArrayInt = array of integer;');
  8729. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  8730. Add('var vJ: tarrayint;');
  8731. Add('begin');
  8732. Add(' vi:=vi;');
  8733. Add(' doit(vi,vi,vi);');
  8734. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  8735. Add('end;');
  8736. Add('var a: TArrayInt;');
  8737. Add('begin');
  8738. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  8739. ConvertProgram;
  8740. CheckSource('TestArrayElement_AsParams',
  8741. LinesToStr([ // statements
  8742. 'this.DoIt = function (vG,vH,vI) {',
  8743. ' var vJ = [];',
  8744. ' vI.set(vI.get());',
  8745. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8746. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  8747. ' a:1+3,',
  8748. ' p:vJ,',
  8749. ' get: function () {',
  8750. ' return this.p[this.a];',
  8751. ' },',
  8752. ' set: function (v) {',
  8753. ' this.p[this.a] = v;',
  8754. ' }',
  8755. ' });',
  8756. '};',
  8757. 'this.a = [];'
  8758. ]),
  8759. LinesToStr([
  8760. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8761. ' a: 1+6,',
  8762. ' p: $mod.a,',
  8763. ' get: function () {',
  8764. ' return this.p[this.a];',
  8765. ' },',
  8766. ' set: function (v) {',
  8767. ' this.p[this.a] = v;',
  8768. ' }',
  8769. '});'
  8770. ]));
  8771. end;
  8772. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8773. begin
  8774. StartProgram(false);
  8775. Add('type Integer = longint;');
  8776. Add('type TArrayInt = array of integer;');
  8777. Add('function GetArr(vB: integer = 0): tarrayint;');
  8778. Add('begin');
  8779. Add('end;');
  8780. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8781. Add('begin');
  8782. Add('end;');
  8783. Add('begin');
  8784. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8785. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8786. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8787. ConvertProgram;
  8788. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8789. LinesToStr([ // statements
  8790. 'this.GetArr = function (vB) {',
  8791. ' var Result = [];',
  8792. ' return Result;',
  8793. '};',
  8794. 'this.DoIt = function (vG,vH,vI) {',
  8795. '};'
  8796. ]),
  8797. LinesToStr([
  8798. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8799. ' a: 1+3,',
  8800. ' p: $mod.GetArr(0),',
  8801. ' get: function () {',
  8802. ' return this.p[this.a];',
  8803. ' },',
  8804. ' set: function (v) {',
  8805. ' this.p[this.a] = v;',
  8806. ' }',
  8807. '});',
  8808. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8809. ' a: 2+3,',
  8810. ' p: $mod.GetArr(0),',
  8811. ' get: function () {',
  8812. ' return this.p[this.a];',
  8813. ' },',
  8814. ' set: function (v) {',
  8815. ' this.p[this.a] = v;',
  8816. ' }',
  8817. '});',
  8818. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8819. ' a: 3+3,',
  8820. ' p: $mod.GetArr(9),',
  8821. ' get: function () {',
  8822. ' return this.p[this.a];',
  8823. ' },',
  8824. ' set: function (v) {',
  8825. ' this.p[this.a] = v;',
  8826. ' }',
  8827. '});',
  8828. '']));
  8829. end;
  8830. procedure TTestModule.TestArrayEnumTypeRange;
  8831. begin
  8832. StartProgram(false);
  8833. Add([
  8834. 'type',
  8835. ' TEnum = (red,blue);',
  8836. ' TEnumArray = array[TEnum] of longint;',
  8837. 'var',
  8838. ' e: TEnum;',
  8839. ' i: longint;',
  8840. ' a: TEnumArray;',
  8841. ' numbers: TEnumArray = (1,2);',
  8842. ' names: array[TEnum] of string = (''red'',''blue'');',
  8843. 'begin',
  8844. ' e:=low(a);',
  8845. ' e:=high(a);',
  8846. ' i:=a[red];',
  8847. ' a[e]:=a[e];']);
  8848. ConvertProgram;
  8849. CheckSource('TestArrayEnumTypeRange',
  8850. LinesToStr([ // statements
  8851. ' this.TEnum = {',
  8852. ' "0": "red",',
  8853. ' red: 0,',
  8854. ' "1": "blue",',
  8855. ' blue: 1',
  8856. '};',
  8857. 'this.e = 0;',
  8858. 'this.i = 0;',
  8859. 'this.a = rtl.arraySetLength(null,0,2);',
  8860. 'this.numbers = [1, 2];',
  8861. 'this.names = ["red", "blue"];',
  8862. '']),
  8863. LinesToStr([ // $mod.$main
  8864. '$mod.e = $mod.TEnum.red;',
  8865. '$mod.e = $mod.TEnum.blue;',
  8866. '$mod.i = $mod.a[$mod.TEnum.red];',
  8867. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8868. '']));
  8869. end;
  8870. procedure TTestModule.TestArray_SetLengthOutArg;
  8871. begin
  8872. StartProgram(false);
  8873. Add([
  8874. 'type TArrInt = array of longint;',
  8875. 'procedure DoIt(out a: TArrInt);',
  8876. 'begin',
  8877. ' SetLength(a,2);',
  8878. 'end;',
  8879. 'begin',
  8880. '']);
  8881. ConvertProgram;
  8882. CheckSource('TestArray_SetLengthOutArg',
  8883. LinesToStr([ // statements
  8884. 'this.DoIt = function (a) {',
  8885. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8886. '};',
  8887. '']),
  8888. LinesToStr([
  8889. '']));
  8890. end;
  8891. procedure TTestModule.TestArray_SetLengthProperty;
  8892. begin
  8893. StartProgram(false);
  8894. Add('type');
  8895. Add(' TArrInt = array of longint;');
  8896. Add(' TObject = class');
  8897. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8898. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8899. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8900. Add(' end;');
  8901. Add('var Obj: TObject;');
  8902. Add('begin');
  8903. Add(' SetLength(Obj.Colors,2);');
  8904. ConvertProgram;
  8905. CheckSource('TestArray_SetLengthProperty',
  8906. LinesToStr([ // statements
  8907. 'rtl.createClass($mod, "TObject", null, function () {',
  8908. ' this.$init = function () {',
  8909. ' };',
  8910. ' this.$final = function () {',
  8911. ' };',
  8912. '});',
  8913. 'this.Obj = null;',
  8914. '']),
  8915. LinesToStr([
  8916. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  8917. '']));
  8918. end;
  8919. procedure TTestModule.TestArray_SetLengthMultiDim;
  8920. begin
  8921. StartProgram(false);
  8922. Add([
  8923. 'type',
  8924. ' TArrArrInt = array of array of longint;',
  8925. ' TArrStaInt = array of array[1..2] of longint;',
  8926. 'var',
  8927. ' a: TArrArrInt;',
  8928. ' b: TArrStaInt;',
  8929. 'begin',
  8930. ' SetLength(a,2);',
  8931. ' SetLength(a,3,4);',
  8932. ' SetLength(b,5);',
  8933. '']);
  8934. ConvertProgram;
  8935. CheckSource('TestArray_SetLengthMultiDim',
  8936. LinesToStr([ // statements
  8937. 'this.a = [];',
  8938. 'this.b = [];',
  8939. '']),
  8940. LinesToStr([
  8941. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  8942. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  8943. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, 2);',
  8944. '']));
  8945. end;
  8946. procedure TTestModule.TestArray_OpenArrayOfString;
  8947. begin
  8948. StartProgram(false);
  8949. Add('procedure DoIt(const a: array of String);');
  8950. Add('var');
  8951. Add(' i: longint;');
  8952. Add(' s: string;');
  8953. Add('begin');
  8954. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  8955. Add('end;');
  8956. Add('var s: string;');
  8957. Add('begin');
  8958. Add(' DoIt([]);');
  8959. Add(' DoIt([s,''foo'','''',s+s]);');
  8960. ConvertProgram;
  8961. CheckSource('TestArray_OpenArrayOfString',
  8962. LinesToStr([ // statements
  8963. 'this.DoIt = function (a) {',
  8964. ' var i = 0;',
  8965. ' var s = "";',
  8966. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  8967. ' i = $l1;',
  8968. ' s = a[rtl.length(a) - i - 1];',
  8969. ' };',
  8970. '};',
  8971. 'this.s = "";',
  8972. '']),
  8973. LinesToStr([
  8974. '$mod.DoIt([]);',
  8975. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  8976. '']));
  8977. end;
  8978. procedure TTestModule.TestArray_Concat;
  8979. begin
  8980. StartProgram(false);
  8981. Add([
  8982. 'type',
  8983. ' integer = longint;',
  8984. ' TFlag = (big,small);',
  8985. ' TFlags = set of TFlag;',
  8986. ' TRec = record',
  8987. ' i: integer;',
  8988. ' end;',
  8989. ' TArrInt = array of integer;',
  8990. ' TArrRec = array of TRec;',
  8991. ' TArrFlag = array of TFlag;',
  8992. ' TArrSet = array of TFlags;',
  8993. ' TArrJSValue = array of jsvalue;',
  8994. 'var',
  8995. ' ArrInt: tarrint;',
  8996. ' ArrRec: tarrrec;',
  8997. ' ArrFlag: tarrflag;',
  8998. ' ArrSet: tarrset;',
  8999. ' ArrJSValue: tarrjsvalue;',
  9000. 'begin',
  9001. ' arrint:=concat(arrint);',
  9002. ' arrint:=concat(arrint,arrint);',
  9003. ' arrint:=concat(arrint,arrint,arrint);',
  9004. ' arrrec:=concat(arrrec);',
  9005. ' arrrec:=concat(arrrec,arrrec);',
  9006. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9007. ' arrset:=concat(arrset);',
  9008. ' arrset:=concat(arrset,arrset);',
  9009. ' arrset:=concat(arrset,arrset,arrset);',
  9010. ' arrjsvalue:=concat(arrjsvalue);',
  9011. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9012. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9013. ' arrint:=concat([1],arrint);',
  9014. ' arrflag:=concat([big]);',
  9015. ' arrflag:=concat([big],arrflag);',
  9016. ' arrflag:=concat(arrflag,[small]);',
  9017. '']);
  9018. ConvertProgram;
  9019. CheckSource('TestArray_Concat',
  9020. LinesToStr([ // statements
  9021. 'this.TFlag = {',
  9022. ' "0": "big",',
  9023. ' big: 0,',
  9024. ' "1": "small",',
  9025. ' small: 1',
  9026. '};',
  9027. 'rtl.recNewT($mod, "TRec", function () {',
  9028. ' this.i = 0;',
  9029. ' this.$eq = function (b) {',
  9030. ' return this.i === b.i;',
  9031. ' };',
  9032. ' this.$assign = function (s) {',
  9033. ' this.i = s.i;',
  9034. ' return this;',
  9035. ' };',
  9036. '});',
  9037. 'this.ArrInt = [];',
  9038. 'this.ArrRec = [];',
  9039. 'this.ArrFlag = [];',
  9040. 'this.ArrSet = [];',
  9041. 'this.ArrJSValue = [];',
  9042. '']),
  9043. LinesToStr([ // $mod.$main
  9044. '$mod.ArrInt = $mod.ArrInt;',
  9045. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9046. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9047. '$mod.ArrRec = $mod.ArrRec;',
  9048. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9049. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9050. '$mod.ArrSet = $mod.ArrSet;',
  9051. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9052. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9053. '$mod.ArrJSValue = $mod.ArrJSValue;',
  9054. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9055. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9056. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9057. '$mod.ArrFlag = [$mod.TFlag.big];',
  9058. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9059. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9060. '']));
  9061. end;
  9062. procedure TTestModule.TestArray_Copy;
  9063. begin
  9064. StartProgram(false);
  9065. Add([
  9066. 'type',
  9067. ' integer = longint;',
  9068. ' TFlag = (big,small);',
  9069. ' TFlags = set of TFlag;',
  9070. ' TRec = record',
  9071. ' i: integer;',
  9072. ' end;',
  9073. ' TArrInt = array of integer;',
  9074. ' TArrRec = array of TRec;',
  9075. ' TArrSet = array of TFlags;',
  9076. ' TArrJSValue = array of jsvalue;',
  9077. 'var',
  9078. ' ArrInt: tarrint;',
  9079. ' ArrRec: tarrrec;',
  9080. ' ArrSet: tarrset;',
  9081. ' ArrJSValue: tarrjsvalue;',
  9082. 'begin',
  9083. ' arrint:=copy(arrint);',
  9084. ' arrint:=copy(arrint,2);',
  9085. ' arrint:=copy(arrint,3,4);',
  9086. ' arrint:=copy([1,1],1,2);',
  9087. ' arrrec:=copy(arrrec);',
  9088. ' arrrec:=copy(arrrec,5);',
  9089. ' arrrec:=copy(arrrec,6,7);',
  9090. ' arrset:=copy(arrset);',
  9091. ' arrset:=copy(arrset,8);',
  9092. ' arrset:=copy(arrset,9,10);',
  9093. ' arrjsvalue:=copy(arrjsvalue);',
  9094. ' arrjsvalue:=copy(arrjsvalue,11);',
  9095. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9096. ' ']);
  9097. ConvertProgram;
  9098. CheckSource('TestArray_Copy',
  9099. LinesToStr([ // statements
  9100. 'this.TFlag = {',
  9101. ' "0": "big",',
  9102. ' big: 0,',
  9103. ' "1": "small",',
  9104. ' small: 1',
  9105. '};',
  9106. 'rtl.recNewT($mod, "TRec", function () {',
  9107. ' this.i = 0;',
  9108. ' this.$eq = function (b) {',
  9109. ' return this.i === b.i;',
  9110. ' };',
  9111. ' this.$assign = function (s) {',
  9112. ' this.i = s.i;',
  9113. ' return this;',
  9114. ' };',
  9115. '});',
  9116. 'this.ArrInt = [];',
  9117. 'this.ArrRec = [];',
  9118. 'this.ArrSet = [];',
  9119. 'this.ArrJSValue = [];',
  9120. '']),
  9121. LinesToStr([ // $mod.$main
  9122. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9123. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9124. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9125. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9126. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9127. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9128. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9129. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9130. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9131. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9132. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9133. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9134. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9135. '']));
  9136. end;
  9137. procedure TTestModule.TestArray_InsertDelete;
  9138. begin
  9139. StartProgram(false);
  9140. Add([
  9141. 'type',
  9142. ' integer = longint;',
  9143. ' TFlag = (big,small);',
  9144. ' TFlags = set of TFlag;',
  9145. ' TRec = record',
  9146. ' i: integer;',
  9147. ' end;',
  9148. ' TArrInt = array of integer;',
  9149. ' TArrRec = array of TRec;',
  9150. ' TArrSet = array of TFlags;',
  9151. ' TArrJSValue = array of jsvalue;',
  9152. ' TArrArrInt = array of TArrInt;',
  9153. 'var',
  9154. ' ArrInt: tarrint;',
  9155. ' ArrRec: tarrrec;',
  9156. ' ArrSet: tarrset;',
  9157. ' ArrJSValue: tarrjsvalue;',
  9158. ' ArrArrInt: TArrArrInt;',
  9159. 'begin',
  9160. ' Insert(1,arrint,2);',
  9161. ' Insert(arrint[3],arrint,4);',
  9162. ' Insert(arrrec[5],arrrec,6);',
  9163. ' Insert(arrset[7],arrset,7);',
  9164. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9165. ' Insert(10,arrjsvalue,11);',
  9166. ' Insert([23],arrarrint,22);',
  9167. ' Delete(arrint,12,13);',
  9168. ' Delete(arrrec,14,15);',
  9169. ' Delete(arrset,17,18);',
  9170. ' Delete(arrjsvalue,19,10);']);
  9171. ConvertProgram;
  9172. CheckSource('TestArray_InsertDelete',
  9173. LinesToStr([ // statements
  9174. 'this.TFlag = {',
  9175. ' "0": "big",',
  9176. ' big: 0,',
  9177. ' "1": "small",',
  9178. ' small: 1',
  9179. '};',
  9180. 'rtl.recNewT($mod, "TRec", function () {',
  9181. ' this.i = 0;',
  9182. ' this.$eq = function (b) {',
  9183. ' return this.i === b.i;',
  9184. ' };',
  9185. ' this.$assign = function (s) {',
  9186. ' this.i = s.i;',
  9187. ' return this;',
  9188. ' };',
  9189. '});',
  9190. 'this.ArrInt = [];',
  9191. 'this.ArrRec = [];',
  9192. 'this.ArrSet = [];',
  9193. 'this.ArrJSValue = [];',
  9194. 'this.ArrArrInt = [];',
  9195. '']),
  9196. LinesToStr([ // $mod.$main
  9197. '$mod.ArrInt.splice(2, 0, 1);',
  9198. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9199. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9200. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9201. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9202. '$mod.ArrJSValue.splice(11, 0, 10);',
  9203. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9204. '$mod.ArrInt.splice(12, 13);',
  9205. '$mod.ArrRec.splice(14, 15);',
  9206. '$mod.ArrSet.splice(17, 18);',
  9207. '$mod.ArrJSValue.splice(19, 10);',
  9208. '']));
  9209. end;
  9210. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9211. begin
  9212. StartProgram(false);
  9213. Add([
  9214. '{$modeswitch arrayoperators}',
  9215. 'type',
  9216. ' integer = longint;',
  9217. ' TArrInt = array of integer;',
  9218. ' TArrStr = array of string;',
  9219. 'const',
  9220. ' Ints: TArrInt = (1,2,3);',
  9221. ' Aliases: TarrStr = (''foo'',''b'');',
  9222. ' OneInt: TArrInt = (7);',
  9223. ' OneStr: array of integer = (7);',
  9224. ' Chars: array of char = ''aoc'';',
  9225. ' Names: array of string = (''a'',''foo'');',
  9226. ' NameCount = low(Names)+high(Names)+length(Names);',
  9227. 'var i: integer;',
  9228. 'begin',
  9229. ' Ints:=[];',
  9230. ' Ints:=[1,1];',
  9231. ' Ints:=[1]+[2];',
  9232. ' Ints:=[2];',
  9233. ' Ints:=[]+ints;',
  9234. ' Ints:=Ints+[];',
  9235. ' Ints:=Ints+OneInt;',
  9236. ' Ints:=Ints+[1,1];',
  9237. ' Ints:=[i,i]+Ints;',
  9238. ' Ints:=[1]+[i]+[3];',
  9239. '']);
  9240. ConvertProgram;
  9241. CheckSource('TestArray_DynArrayConstObjFPC',
  9242. LinesToStr([ // statements
  9243. 'this.Ints = [1, 2, 3];',
  9244. 'this.Aliases = ["foo", "b"];',
  9245. 'this.OneInt = [7];',
  9246. 'this.OneStr = [7];',
  9247. 'this.Chars = ["a", "o", "c"];',
  9248. 'this.Names = ["a", "foo"];',
  9249. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9250. 'this.i = 0;',
  9251. '']),
  9252. LinesToStr([ // $mod.$main
  9253. '$mod.Ints = [];',
  9254. '$mod.Ints = [1, 1];',
  9255. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9256. '$mod.Ints = [2];',
  9257. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9258. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9259. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9260. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9261. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9262. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9263. '']));
  9264. end;
  9265. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9266. begin
  9267. StartProgram(false);
  9268. // Note: const c = [1,1]; defines a set!
  9269. Add([
  9270. '{$mode delphi}',
  9271. 'type',
  9272. ' integer = longint;',
  9273. ' TArrInt = array of integer;',
  9274. ' TArrStr = array of string;',
  9275. 'const',
  9276. ' Ints: TArrInt = [1,1,2];',
  9277. ' Aliases: TarrStr = [''foo'',''b''];',
  9278. ' OneInt: TArrInt = [7];',
  9279. ' OneStr: array of integer = [7]+[8];',
  9280. ' Chars: array of char = ''aoc'';',
  9281. ' Names: array of string = [''a'',''a''];',
  9282. ' NameCount = low(Names)+high(Names)+length(Names);',
  9283. 'begin',
  9284. '']);
  9285. ConvertProgram;
  9286. CheckSource('TestArray_DynArrayConstDelphi',
  9287. LinesToStr([ // statements
  9288. 'this.Ints = [1, 1, 2];',
  9289. 'this.Aliases = ["foo", "b"];',
  9290. 'this.OneInt = [7];',
  9291. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9292. 'this.Chars = ["a", "o", "c"];',
  9293. 'this.Names = ["a", "a"];',
  9294. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9295. '']),
  9296. LinesToStr([ // $mod.$main
  9297. '']));
  9298. end;
  9299. procedure TTestModule.TestArray_ArrayLitAsParam;
  9300. begin
  9301. StartProgram(false);
  9302. Add([
  9303. '{$modeswitch arrayoperators}',
  9304. 'type',
  9305. ' integer = longint;',
  9306. ' TArrInt = array of integer;',
  9307. ' TArrSet = array of (red,green,blue);',
  9308. 'procedure DoOpenInt(a: array of integer); forward;',
  9309. 'procedure DoInt(a: TArrInt);',
  9310. 'begin',
  9311. ' DoInt(a+[1]);',
  9312. ' DoInt([1]+a);',
  9313. ' DoOpenInt(a);',
  9314. ' DoOpenInt(a+[1]);',
  9315. ' DoOpenInt([1]+a);',
  9316. 'end;',
  9317. 'procedure DoOpenInt(a: array of integer);',
  9318. 'begin',
  9319. ' DoOpenInt(a+[1]);',
  9320. ' DoOpenInt([1]+a);',
  9321. ' DoInt(a);',
  9322. ' DoInt(a+[1]);',
  9323. ' DoInt([1]+a);',
  9324. 'end;',
  9325. 'procedure DoSet(a: TArrSet);',
  9326. 'begin',
  9327. ' DoSet(a+[red]);',
  9328. ' DoSet([blue]+a);',
  9329. 'end;',
  9330. 'var',
  9331. ' i: TArrInt;',
  9332. ' s: TArrSet;',
  9333. 'begin',
  9334. ' DoInt([1]);',
  9335. ' DoInt([1]+[2]);',
  9336. ' DoInt(i+[1]);',
  9337. ' DoInt([1]+i);',
  9338. ' DoOpenInt([1]);',
  9339. ' DoOpenInt([1]+[2]);',
  9340. ' DoOpenInt(i+[1]);',
  9341. ' DoOpenInt([1]+i);',
  9342. ' DoSet([red]);',
  9343. ' DoSet([blue]+[green]);',
  9344. ' DoSet(s+[blue]);',
  9345. ' DoSet([red]+s);',
  9346. '']);
  9347. ConvertProgram;
  9348. CheckSource('TestArray_ArrayLitAsParam',
  9349. LinesToStr([ // statements
  9350. 'this.TArrSet$a = {',
  9351. ' "0": "red",',
  9352. ' red: 0,',
  9353. ' "1": "green",',
  9354. ' green: 1,',
  9355. ' "2": "blue",',
  9356. ' blue: 2',
  9357. '};',
  9358. 'this.DoInt = function (a) {',
  9359. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9360. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9361. ' $mod.DoOpenInt(a);',
  9362. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9363. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9364. '};',
  9365. 'this.DoOpenInt = function (a) {',
  9366. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9367. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9368. ' $mod.DoInt(a);',
  9369. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9370. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9371. '};',
  9372. 'this.DoSet = function (a) {',
  9373. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9374. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9375. '};',
  9376. 'this.i = [];',
  9377. 'this.s = [];',
  9378. '']),
  9379. LinesToStr([ // $mod.$main
  9380. '$mod.DoInt([1]);',
  9381. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9382. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9383. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9384. '$mod.DoOpenInt([1]);',
  9385. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9386. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9387. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9388. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9389. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9390. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9391. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9392. '']));
  9393. end;
  9394. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9395. begin
  9396. StartProgram(false);
  9397. Add([
  9398. '{$modeswitch arrayoperators}',
  9399. 'type',
  9400. ' integer = longint;',
  9401. ' TArrInt = array of integer;',
  9402. ' TArrArrInt = array of TArrInt;',
  9403. 'procedure DoInt(a: TArrArrInt);',
  9404. 'begin',
  9405. ' DoInt(a+[[1]]);',
  9406. ' DoInt([[1]]+a);',
  9407. ' DoInt(a);',
  9408. 'end;',
  9409. 'var',
  9410. ' i: TArrInt;',
  9411. ' a: TArrArrInt;',
  9412. 'begin',
  9413. ' a:=[[1]];',
  9414. ' a:=[i];',
  9415. ' a:=a+[i];',
  9416. ' a:=[i]+a;',
  9417. ' a:=[[1]+i];',
  9418. ' a:=[[1]+[2]];',
  9419. ' a:=[i+[2]];',
  9420. ' DoInt([[1]]);',
  9421. ' DoInt([[1]+[2],[3,4],[5]]);',
  9422. ' DoInt([i+[1]]+a);',
  9423. ' DoInt([i]+a);',
  9424. '']);
  9425. ConvertProgram;
  9426. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9427. LinesToStr([ // statements
  9428. 'this.DoInt = function (a) {',
  9429. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9430. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9431. ' $mod.DoInt(a);',
  9432. '};',
  9433. 'this.i = [];',
  9434. 'this.a = [];',
  9435. '']),
  9436. LinesToStr([ // $mod.$main
  9437. '$mod.a = [[1]];',
  9438. '$mod.a = [$mod.i];',
  9439. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9440. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9441. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9442. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9443. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9444. '$mod.DoInt([[1]]);',
  9445. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9446. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9447. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9448. '']));
  9449. end;
  9450. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9451. begin
  9452. StartProgram(false);
  9453. Add([
  9454. '{$modeswitch arrayoperators}',
  9455. 'type',
  9456. ' integer = longint;',
  9457. ' TArrInt = array[1..2] of integer;',
  9458. ' TArrArrInt = array of TArrInt;',
  9459. 'procedure DoInt(a: TArrArrInt);',
  9460. 'begin',
  9461. ' DoInt(a+[[1,2]]);',
  9462. ' DoInt([[1,2]]+a);',
  9463. ' DoInt(a);',
  9464. 'end;',
  9465. 'var',
  9466. ' i: TArrInt;',
  9467. ' a: TArrArrInt;',
  9468. 'begin',
  9469. ' a:=[[1,1]];',
  9470. ' a:=[i];',
  9471. ' a:=a+[i];',
  9472. ' a:=[i]+a;',
  9473. ' DoInt([[1,1]]);',
  9474. ' DoInt([[1,2],[3,4]]);',
  9475. '']);
  9476. ConvertProgram;
  9477. CheckSource('TestArray_ArrayLitStaticAsParam',
  9478. LinesToStr([ // statements
  9479. 'this.DoInt = function (a) {',
  9480. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9481. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9482. ' $mod.DoInt(a);',
  9483. '};',
  9484. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9485. 'this.a = [];',
  9486. '']),
  9487. LinesToStr([ // $mod.$main
  9488. '$mod.a = [[1, 1]];',
  9489. '$mod.a = [$mod.i.slice(0)];',
  9490. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9491. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9492. '$mod.DoInt([[1, 1]]);',
  9493. '$mod.DoInt([[1, 2], [3, 4]]);',
  9494. '']));
  9495. end;
  9496. procedure TTestModule.TestArray_ForInArrOfString;
  9497. begin
  9498. StartProgram(false);
  9499. Add([
  9500. 'type',
  9501. 'type',
  9502. ' TMonthNameArray = array [1..12] of string;',
  9503. ' TMonthNames = TMonthNameArray;',
  9504. ' TObject = class',
  9505. ' private',
  9506. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9507. ' public',
  9508. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9509. ' end;',
  9510. 'var',
  9511. ' f: TObject;',
  9512. ' Month: string;',
  9513. ' Names: array of string = (''a'',''foo'',''bar'');',
  9514. ' i: longint;',
  9515. 'begin',
  9516. ' for Month in f.LongMonthNames do ;',
  9517. ' for Month in Names do ;',
  9518. ' for i:=low(Names) to high(Names) do ;',
  9519. '']);
  9520. ConvertProgram;
  9521. CheckSource('TestArray_ForInArrOfString',
  9522. LinesToStr([ // statements
  9523. 'rtl.createClass($mod, "TObject", null, function () {',
  9524. ' this.$init = function () {',
  9525. ' };',
  9526. ' this.$final = function () {',
  9527. ' };',
  9528. '});',
  9529. 'this.f = null;',
  9530. 'this.Month = "";',
  9531. 'this.Names = ["a", "foo", "bar"];',
  9532. 'this.i = 0;',
  9533. '']),
  9534. LinesToStr([ // $mod.$main
  9535. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9536. 'for (var $in4 = $mod.Names, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.Month = $in4[$l5];',
  9537. 'for (var $l7 = 0, $end8 = rtl.length($mod.Names) - 1; $l7 <= $end8; $l7++) $mod.i = $l7;',
  9538. '']));
  9539. end;
  9540. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9541. begin
  9542. StartProgram(false);
  9543. Add([
  9544. '{$modeswitch externalclass}',
  9545. 'type',
  9546. ' TJSObject = class external name ''Object''',
  9547. ' end;',
  9548. ' TJSArray = class external name ''Array''',
  9549. ' class function isArray(Value: JSValue) : boolean;',
  9550. ' function concat() : TJSArray; varargs;',
  9551. ' end;',
  9552. 'var',
  9553. ' aObj: TJSArray;',
  9554. ' a: array of longint;',
  9555. ' o: TJSObject;',
  9556. 'begin',
  9557. ' if TJSArray.isArray(65) then ;',
  9558. ' aObj:=TJSArray(a).concat(a);',
  9559. ' o:=TJSObject(a);']);
  9560. ConvertProgram;
  9561. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9562. LinesToStr([ // statements
  9563. 'this.aObj = null;',
  9564. 'this.a = [];',
  9565. 'this.o = null;',
  9566. '']),
  9567. LinesToStr([ // $mod.$main
  9568. 'if (Array.isArray(65)) ;',
  9569. '$mod.aObj = $mod.a.concat($mod.a);',
  9570. '$mod.o = $mod.a;',
  9571. '']));
  9572. end;
  9573. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9574. begin
  9575. StartProgram(false);
  9576. Add([
  9577. '{$modeswitch externalclass}',
  9578. 'type',
  9579. ' TArrStr = array of string;',
  9580. ' TJSArray = class external name ''Array''',
  9581. ' end;',
  9582. ' TJSObject = class external name ''Object''',
  9583. ' end;',
  9584. 'var',
  9585. ' aObj: TJSArray;',
  9586. ' a: TArrStr;',
  9587. ' jo: TJSObject;',
  9588. 'begin',
  9589. ' a:=TArrStr(aObj);',
  9590. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  9591. ' a:=TarrStr(jo);',
  9592. '']);
  9593. ConvertProgram;
  9594. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  9595. LinesToStr([ // statements
  9596. 'this.aObj = null;',
  9597. 'this.a = [];',
  9598. 'this.jo = null;',
  9599. '']),
  9600. LinesToStr([ // $mod.$main
  9601. '$mod.a = $mod.aObj;',
  9602. '$mod.aObj[1] = $mod.aObj[2];',
  9603. '$mod.a = $mod.jo;',
  9604. '']));
  9605. end;
  9606. procedure TTestModule.TestArrayOfConst_TVarRec;
  9607. begin
  9608. StartProgram(true,[supTVarRec]);
  9609. Add([
  9610. 'procedure Say(args: array of const);',
  9611. 'var',
  9612. ' i: longint;',
  9613. ' v: TVarRec;',
  9614. 'begin',
  9615. ' for i:=low(args) to high(args) do begin',
  9616. ' v:=args[i];',
  9617. ' case v.vtype of',
  9618. ' vtInteger: if length(args)=args[i].vInteger then ;',
  9619. ' end;',
  9620. ' end;',
  9621. ' for v in args do ;',
  9622. ' args:=nil;',
  9623. ' SetLength(args,2);',
  9624. 'end;',
  9625. 'begin']);
  9626. ConvertProgram;
  9627. CheckSource('TestArrayOfConst_TVarRec',
  9628. LinesToStr([ // statements
  9629. 'this.Say = function (args) {',
  9630. ' var i = 0;',
  9631. ' var v = pas.system.TVarRec.$new();',
  9632. ' for (var $l1 = 0, $end2 = rtl.length(args) - 1; $l1 <= $end2; $l1++) {',
  9633. ' i = $l1;',
  9634. ' v.$assign(args[i]);',
  9635. ' var $tmp3 = v.VType;',
  9636. ' if ($tmp3 === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  9637. ' };',
  9638. ' for (var $in4 = args, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) v = $in4[$l5];',
  9639. ' args = [];',
  9640. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  9641. '};',
  9642. '']),
  9643. LinesToStr([ // $mod.$main
  9644. ]));
  9645. end;
  9646. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  9647. begin
  9648. StartProgram(true,[supTVarRec]);
  9649. Add([
  9650. 'procedure Say(args: array of const);',
  9651. 'begin',
  9652. ' Say(args);',
  9653. 'end;',
  9654. 'var',
  9655. ' p: Pointer;',
  9656. ' j: jsvalue;',
  9657. ' c: currency;',
  9658. 'begin',
  9659. ' Say([]);',
  9660. ' Say([1]);',
  9661. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  9662. '']);
  9663. ConvertProgram;
  9664. CheckSource('TestArrayOfConst_PassBaseTypes',
  9665. LinesToStr([ // statements
  9666. 'this.Say = function (args) {',
  9667. ' $mod.Say(args);',
  9668. '};',
  9669. 'this.p = null;',
  9670. 'this.j = undefined;',
  9671. 'this.c = 0;',
  9672. '']),
  9673. LinesToStr([ // $mod.$main
  9674. '$mod.Say([]);',
  9675. '$mod.Say(pas.system.VarRecs(0, 1));',
  9676. '$mod.Say(pas.system.VarRecs(',
  9677. ' 9,',
  9678. ' "c",',
  9679. ' 18,',
  9680. ' "foo",',
  9681. ' 5,',
  9682. ' null,',
  9683. ' 1,',
  9684. ' true,',
  9685. ' 3,',
  9686. ' 1.3,',
  9687. ' 5,',
  9688. ' $mod.p,',
  9689. ' 20,',
  9690. ' $mod.j,',
  9691. ' 12,',
  9692. ' $mod.c',
  9693. ' ));',
  9694. '']));
  9695. end;
  9696. procedure TTestModule.TestArrayOfConst_PassObj;
  9697. begin
  9698. StartProgram(true,[supTVarRec]);
  9699. Add([
  9700. '{$interfaces corba}',
  9701. 'type',
  9702. ' TObject = class',
  9703. ' end;',
  9704. ' TClass = class of TObject;',
  9705. ' IUnknown = interface',
  9706. ' end;',
  9707. 'procedure Say(args: array of const);',
  9708. 'begin',
  9709. 'end;',
  9710. 'var',
  9711. ' o: TObject;',
  9712. ' c: TClass;',
  9713. ' i: IUnknown;',
  9714. 'begin',
  9715. ' Say([o,c,TObject]);',
  9716. ' Say([nil,i]);',
  9717. '']);
  9718. ConvertProgram;
  9719. CheckSource('TestArrayOfConst_PassObj',
  9720. LinesToStr([ // statements
  9721. 'rtl.createClass($mod, "TObject", null, function () {',
  9722. ' this.$init = function () {',
  9723. ' };',
  9724. ' this.$final = function () {',
  9725. ' };',
  9726. '});',
  9727. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  9728. 'this.Say = function (args) {',
  9729. '};',
  9730. 'this.o = null;',
  9731. 'this.c = null;',
  9732. 'this.i = null;',
  9733. '']),
  9734. LinesToStr([ // $mod.$main
  9735. '$mod.Say(pas.system.VarRecs(',
  9736. ' 7,',
  9737. ' $mod.o,',
  9738. ' 8,',
  9739. ' $mod.c,',
  9740. ' 8,',
  9741. ' $mod.TObject',
  9742. '));',
  9743. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  9744. '']));
  9745. end;
  9746. procedure TTestModule.TestRecord_Empty;
  9747. begin
  9748. StartProgram(false);
  9749. Add([
  9750. 'type',
  9751. ' TRecA = record',
  9752. ' end;',
  9753. 'var a,b: TRecA;',
  9754. 'begin',
  9755. ' if a=b then ;']);
  9756. ConvertProgram;
  9757. CheckSource('TestRecord_Empty',
  9758. LinesToStr([ // statements
  9759. 'rtl.recNewT($mod, "TRecA", function () {',
  9760. ' this.$eq = function (b) {',
  9761. ' return true;',
  9762. ' };',
  9763. ' this.$assign = function (s) {',
  9764. ' return this;',
  9765. ' };',
  9766. '});',
  9767. 'this.a = $mod.TRecA.$new();',
  9768. 'this.b = $mod.TRecA.$new();',
  9769. '']),
  9770. LinesToStr([ // $mod.$main
  9771. 'if ($mod.a.$eq($mod.b)) ;'
  9772. ]));
  9773. end;
  9774. procedure TTestModule.TestRecord_Var;
  9775. begin
  9776. StartProgram(false);
  9777. Add('type');
  9778. Add(' TRecA = record');
  9779. Add(' Bold: longint;');
  9780. Add(' end;');
  9781. Add('var Rec: TRecA;');
  9782. Add('begin');
  9783. Add(' rec.bold:=123');
  9784. ConvertProgram;
  9785. CheckSource('TestRecord_Var',
  9786. LinesToStr([ // statements
  9787. 'rtl.recNewT($mod, "TRecA", function () {',
  9788. ' this.Bold = 0;',
  9789. ' this.$eq = function (b) {',
  9790. ' return this.Bold === b.Bold;',
  9791. ' };',
  9792. ' this.$assign = function (s) {',
  9793. ' this.Bold = s.Bold;',
  9794. ' return this;',
  9795. ' };',
  9796. '});',
  9797. 'this.Rec = $mod.TRecA.$new();',
  9798. '']),
  9799. LinesToStr([ // $mod.$main
  9800. '$mod.Rec.Bold = 123;'
  9801. ]));
  9802. end;
  9803. procedure TTestModule.TestRecord_VarExternal;
  9804. begin
  9805. StartProgram(false);
  9806. Add([
  9807. '{$modeswitch externalclass}',
  9808. 'type',
  9809. ' TRecA = record',
  9810. ' i: byte;',
  9811. ' length_: longint external name ''length'';',
  9812. ' end;',
  9813. 'var Rec: TRecA;',
  9814. 'begin',
  9815. ' rec.length_ := rec.length_',
  9816. '']);
  9817. ConvertProgram;
  9818. CheckSource('TestRecord_VarExternal',
  9819. LinesToStr([ // statements
  9820. 'rtl.recNewT($mod, "TRecA", function () {',
  9821. ' this.i = 0;',
  9822. ' this.$eq = function (b) {',
  9823. ' return (this.i === b.i) && (this.length === b.length);',
  9824. ' };',
  9825. ' this.$assign = function (s) {',
  9826. ' this.i = s.i;',
  9827. ' this.length = s.length;',
  9828. ' return this;',
  9829. ' };',
  9830. '});',
  9831. 'this.Rec = $mod.TRecA.$new();',
  9832. '']),
  9833. LinesToStr([ // $mod.$main
  9834. '$mod.Rec.length = $mod.Rec.length;'
  9835. ]));
  9836. end;
  9837. procedure TTestModule.TestRecord_WithDo;
  9838. begin
  9839. StartProgram(false);
  9840. Add('type');
  9841. Add(' TRec = record');
  9842. Add(' vI: longint;');
  9843. Add(' end;');
  9844. Add('var');
  9845. Add(' Int: longint;');
  9846. Add(' r: TRec;');
  9847. Add('begin');
  9848. Add(' with r do');
  9849. Add(' int:=vi;');
  9850. Add(' with r do begin');
  9851. Add(' int:=vi;');
  9852. Add(' vi:=int;');
  9853. Add(' end;');
  9854. ConvertProgram;
  9855. CheckSource('TestWithRecordDo',
  9856. LinesToStr([ // statements
  9857. 'rtl.recNewT($mod, "TRec", function () {',
  9858. ' this.vI = 0;',
  9859. ' this.$eq = function (b) {',
  9860. ' return this.vI === b.vI;',
  9861. ' };',
  9862. ' this.$assign = function (s) {',
  9863. ' this.vI = s.vI;',
  9864. ' return this;',
  9865. ' };',
  9866. '});',
  9867. 'this.Int = 0;',
  9868. 'this.r = $mod.TRec.$new();',
  9869. '']),
  9870. LinesToStr([ // $mod.$main
  9871. 'var $with1 = $mod.r;',
  9872. '$mod.Int = $with1.vI;',
  9873. 'var $with2 = $mod.r;',
  9874. '$mod.Int = $with2.vI;',
  9875. '$with2.vI = $mod.Int;'
  9876. ]));
  9877. end;
  9878. procedure TTestModule.TestRecord_Assign;
  9879. begin
  9880. StartProgram(false);
  9881. Add('type');
  9882. Add(' TEnum = (red,green);');
  9883. Add(' TEnums = set of TEnum;');
  9884. Add(' TSmallRec = record');
  9885. Add(' N: longint;');
  9886. Add(' end;');
  9887. Add(' TBigRec = record');
  9888. Add(' Int: longint;');
  9889. Add(' D: double;');
  9890. Add(' Arr: array of longint;');
  9891. Add(' Arr2: array[1..2] of longint;');
  9892. Add(' Small: TSmallRec;');
  9893. Add(' Enums: TEnums;');
  9894. Add(' end;');
  9895. Add('var');
  9896. Add(' r, s: TBigRec;');
  9897. Add('begin');
  9898. Add(' r:=s;');
  9899. Add(' r:=default(TBigRec);');
  9900. Add(' r:=default(s);');
  9901. ConvertProgram;
  9902. CheckSource('TestRecord_Assign',
  9903. LinesToStr([ // statements
  9904. 'this.TEnum = {',
  9905. ' "0": "red",',
  9906. ' red: 0,',
  9907. ' "1": "green",',
  9908. ' green: 1',
  9909. '};',
  9910. 'rtl.recNewT($mod, "TSmallRec", function () {',
  9911. ' this.N = 0;',
  9912. ' this.$eq = function (b) {',
  9913. ' return this.N === b.N;',
  9914. ' };',
  9915. ' this.$assign = function (s) {',
  9916. ' this.N = s.N;',
  9917. ' return this;',
  9918. ' };',
  9919. '});',
  9920. 'rtl.recNewT($mod, "TBigRec", function () {',
  9921. ' this.Int = 0;',
  9922. ' this.D = 0.0;',
  9923. ' this.Arr = [];',
  9924. ' this.$new = function () {',
  9925. ' var r = Object.create(this);',
  9926. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  9927. ' r.Small = $mod.TSmallRec.$new();',
  9928. ' r.Enums = {};',
  9929. ' return r;',
  9930. ' };',
  9931. ' this.$eq = function (b) {',
  9932. ' 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);',
  9933. ' };',
  9934. ' this.$assign = function (s) {',
  9935. ' this.Int = s.Int;',
  9936. ' this.D = s.D;',
  9937. ' this.Arr = s.Arr;',
  9938. ' this.Arr2 = s.Arr2.slice(0);',
  9939. ' this.Small.$assign(s.Small);',
  9940. ' this.Enums = rtl.refSet(s.Enums);',
  9941. ' return this;',
  9942. ' };',
  9943. '});',
  9944. 'this.r = $mod.TBigRec.$new();',
  9945. 'this.s = $mod.TBigRec.$new();',
  9946. '']),
  9947. LinesToStr([ // $mod.$main
  9948. '$mod.r.$assign($mod.s);',
  9949. '$mod.r.$assign($mod.TBigRec.$new());',
  9950. '$mod.r.$assign($mod.TBigRec.$new());',
  9951. '']));
  9952. end;
  9953. procedure TTestModule.TestRecord_AsParams;
  9954. begin
  9955. StartProgram(false);
  9956. Add([
  9957. 'type',
  9958. ' integer = longint;',
  9959. ' TRecord = record',
  9960. ' i: integer;',
  9961. ' end;',
  9962. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  9963. 'var vL: TRecord;',
  9964. 'begin',
  9965. ' vd:=vd;',
  9966. ' vd.i:=vd.i;',
  9967. ' vl:=vc;',
  9968. ' vv:=vv;',
  9969. ' vv.i:=vv.i;',
  9970. ' U:=vl;',
  9971. ' U:=vd;',
  9972. ' U:=vc;',
  9973. ' U:=vv;',
  9974. ' vl:=TRecord(U);',
  9975. ' vd:=TRecord(U);',
  9976. ' vv:=TRecord(U);',
  9977. ' doit(vd,vd,vd,vd);',
  9978. ' doit(vc,vc,vl,vl);',
  9979. ' doit(vv,vv,vv,vv);',
  9980. ' doit(vl,vl,vl,vl);',
  9981. ' TRecord(U).i:=3;',
  9982. 'end;',
  9983. 'var i: TRecord;',
  9984. 'begin',
  9985. ' doit(i,i,i,i);',
  9986. '']);
  9987. ConvertProgram;
  9988. CheckSource('TestRecord_AsParams',
  9989. LinesToStr([ // statements
  9990. 'rtl.recNewT($mod, "TRecord", function () {',
  9991. ' this.i = 0;',
  9992. ' this.$eq = function (b) {',
  9993. ' return this.i === b.i;',
  9994. ' };',
  9995. ' this.$assign = function (s) {',
  9996. ' this.i = s.i;',
  9997. ' return this;',
  9998. ' };',
  9999. '});',
  10000. 'this.DoIt = function (vD, vC, vV, U) {',
  10001. ' var vL = $mod.TRecord.$new();',
  10002. ' vD.$assign(vD);',
  10003. ' vD.i = vD.i;',
  10004. ' vL.$assign(vC);',
  10005. ' vV.$assign(vV);',
  10006. ' vV.i = vV.i;',
  10007. ' U.$assign(vL);',
  10008. ' U.$assign(vD);',
  10009. ' U.$assign(vC);',
  10010. ' U.$assign(vV);',
  10011. ' vL.$assign(U);',
  10012. ' vD.$assign(U);',
  10013. ' vV.$assign(U);',
  10014. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10015. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10016. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10017. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10018. ' U.i = 3;',
  10019. '};',
  10020. 'this.i = $mod.TRecord.$new();'
  10021. ]),
  10022. LinesToStr([
  10023. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10024. '']));
  10025. end;
  10026. procedure TTestModule.TestRecordElement_AsParams;
  10027. begin
  10028. StartProgram(false);
  10029. Add('type');
  10030. Add(' integer = longint;');
  10031. Add(' TRecord = record');
  10032. Add(' i: integer;');
  10033. Add(' end;');
  10034. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10035. Add('var vJ: TRecord;');
  10036. Add('begin');
  10037. Add(' doit(vj.i,vj.i,vj.i);');
  10038. Add('end;');
  10039. Add('var r: TRecord;');
  10040. Add('begin');
  10041. Add(' doit(r.i,r.i,r.i);');
  10042. ConvertProgram;
  10043. CheckSource('TestRecordElement_AsParams',
  10044. LinesToStr([ // statements
  10045. 'rtl.recNewT($mod, "TRecord", function () {',
  10046. ' this.i = 0;',
  10047. ' this.$eq = function (b) {',
  10048. ' return this.i === b.i;',
  10049. ' };',
  10050. ' this.$assign = function (s) {',
  10051. ' this.i = s.i;',
  10052. ' return this;',
  10053. ' };',
  10054. '});',
  10055. 'this.DoIt = function (vG,vH,vI) {',
  10056. ' var vJ = $mod.TRecord.$new();',
  10057. ' $mod.DoIt(vJ.i, vJ.i, {',
  10058. ' p: vJ,',
  10059. ' get: function () {',
  10060. ' return this.p.i;',
  10061. ' },',
  10062. ' set: function (v) {',
  10063. ' this.p.i = v;',
  10064. ' }',
  10065. ' });',
  10066. '};',
  10067. 'this.r = $mod.TRecord.$new();'
  10068. ]),
  10069. LinesToStr([
  10070. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10071. ' p: $mod.r,',
  10072. ' get: function () {',
  10073. ' return this.p.i;',
  10074. ' },',
  10075. ' set: function (v) {',
  10076. ' this.p.i = v;',
  10077. ' }',
  10078. '});'
  10079. ]));
  10080. end;
  10081. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10082. begin
  10083. StartProgram(false);
  10084. Add('type');
  10085. Add(' integer = longint;');
  10086. Add(' TRecord = record');
  10087. Add(' i: integer;');
  10088. Add(' end;');
  10089. Add('function GetRec(vB: integer = 0): TRecord;');
  10090. Add('begin');
  10091. Add('end;');
  10092. Add('procedure DoIt(vG: integer; const vH: integer);');
  10093. Add('begin');
  10094. Add('end;');
  10095. Add('begin');
  10096. Add(' doit(getrec.i,getrec.i);');
  10097. Add(' doit(getrec().i,getrec().i);');
  10098. Add(' doit(getrec(1).i,getrec(2).i);');
  10099. ConvertProgram;
  10100. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10101. LinesToStr([ // statements
  10102. 'rtl.recNewT($mod, "TRecord", function () {',
  10103. ' this.i = 0;',
  10104. ' this.$eq = function (b) {',
  10105. ' return this.i === b.i;',
  10106. ' };',
  10107. ' this.$assign = function (s) {',
  10108. ' this.i = s.i;',
  10109. ' return this;',
  10110. ' };',
  10111. '});',
  10112. 'this.GetRec = function (vB) {',
  10113. ' var Result = $mod.TRecord.$new();',
  10114. ' return Result;',
  10115. '};',
  10116. 'this.DoIt = function (vG, vH) {',
  10117. '};',
  10118. '']),
  10119. LinesToStr([
  10120. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10121. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10122. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10123. '']));
  10124. end;
  10125. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10126. begin
  10127. StartProgram(false);
  10128. Add('type');
  10129. Add(' integer = longint;');
  10130. Add(' TRecord = record');
  10131. Add(' i: integer;');
  10132. Add(' end;');
  10133. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10134. Add('begin');
  10135. Add('end;');
  10136. Add('var r: trecord;');
  10137. Add('begin');
  10138. Add(' with r do ');
  10139. Add(' doit(i,i,i);');
  10140. ConvertProgram;
  10141. CheckSource('TestRecordElementFromWith_AsParams',
  10142. LinesToStr([ // statements
  10143. 'rtl.recNewT($mod, "TRecord", function () {',
  10144. ' this.i = 0;',
  10145. ' this.$eq = function (b) {',
  10146. ' return this.i === b.i;',
  10147. ' };',
  10148. ' this.$assign = function (s) {',
  10149. ' this.i = s.i;',
  10150. ' return this;',
  10151. ' };',
  10152. '});',
  10153. 'this.DoIt = function (vG,vH,vI) {',
  10154. '};',
  10155. 'this.r = $mod.TRecord.$new();'
  10156. ]),
  10157. LinesToStr([
  10158. 'var $with1 = $mod.r;',
  10159. '$mod.DoIt($with1.i,$with1.i,{',
  10160. ' p: $with1,',
  10161. ' get: function () {',
  10162. ' return this.p.i;',
  10163. ' },',
  10164. ' set: function (v) {',
  10165. ' this.p.i = v;',
  10166. ' }',
  10167. '});',
  10168. '']));
  10169. end;
  10170. procedure TTestModule.TestRecord_Equal;
  10171. begin
  10172. StartProgram(false);
  10173. Add('type');
  10174. Add(' integer = longint;');
  10175. Add(' TFlag = (red,blue);');
  10176. Add(' TFlags = set of TFlag;');
  10177. Add(' TProc = procedure;');
  10178. Add(' TRecord = record');
  10179. Add(' i: integer;');
  10180. Add(' Event: TProc;');
  10181. Add(' f: TFlags;');
  10182. Add(' end;');
  10183. Add(' TNested = record');
  10184. Add(' r: TRecord;');
  10185. Add(' end;');
  10186. Add('var');
  10187. Add(' b: boolean;');
  10188. Add(' r,s: trecord;');
  10189. Add('begin');
  10190. Add(' b:=r=s;');
  10191. Add(' b:=r<>s;');
  10192. ConvertProgram;
  10193. CheckSource('TestRecord_Equal',
  10194. LinesToStr([ // statements
  10195. 'this.TFlag = {',
  10196. ' "0": "red",',
  10197. ' red: 0,',
  10198. ' "1": "blue",',
  10199. ' blue: 1',
  10200. '};',
  10201. 'rtl.recNewT($mod, "TRecord", function () {',
  10202. ' this.i = 0;',
  10203. ' this.Event = null;',
  10204. ' this.$new = function () {',
  10205. ' var r = Object.create(this);',
  10206. ' r.f = {};',
  10207. ' return r;',
  10208. ' };',
  10209. ' this.$eq = function (b) {',
  10210. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10211. ' };',
  10212. ' this.$assign = function (s) {',
  10213. ' this.i = s.i;',
  10214. ' this.Event = s.Event;',
  10215. ' this.f = rtl.refSet(s.f);',
  10216. ' return this;',
  10217. ' };',
  10218. '});',
  10219. 'rtl.recNewT($mod, "TNested", function () {',
  10220. ' this.$new = function () {',
  10221. ' var r = Object.create(this);',
  10222. ' r.r = $mod.TRecord.$new();',
  10223. ' return r;',
  10224. ' };',
  10225. ' this.$eq = function (b) {',
  10226. ' return this.r.$eq(b.r);',
  10227. ' };',
  10228. ' this.$assign = function (s) {',
  10229. ' this.r.$assign(s.r);',
  10230. ' return this;',
  10231. ' };',
  10232. '});',
  10233. 'this.b = false;',
  10234. 'this.r = $mod.TRecord.$new();',
  10235. 'this.s = $mod.TRecord.$new();',
  10236. '']),
  10237. LinesToStr([
  10238. '$mod.b = $mod.r.$eq($mod.s);',
  10239. '$mod.b = !$mod.r.$eq($mod.s);',
  10240. '']));
  10241. end;
  10242. procedure TTestModule.TestRecord_JSValue;
  10243. begin
  10244. StartProgram(false);
  10245. Add([
  10246. 'type',
  10247. ' TRecord = record',
  10248. ' i: longint;',
  10249. ' end;',
  10250. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  10251. 'begin',
  10252. 'end;',
  10253. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  10254. 'begin',
  10255. ' if jsvalue(d) then ;',
  10256. ' if jsvalue(c) then ;',
  10257. ' if jsvalue(v) then ;',
  10258. 'end;',
  10259. 'var',
  10260. ' Jv: jsvalue;',
  10261. ' Rec: trecord;',
  10262. 'begin',
  10263. ' rec:=trecord(jv);',
  10264. ' jv:=rec;',
  10265. ' Fly(rec,rec);',
  10266. ' Fly(@rec,@rec);',
  10267. ' if jsvalue(Rec) then ;',
  10268. ' Run(trecord(jv),trecord(jv),rec);',
  10269. '']);
  10270. ConvertProgram;
  10271. CheckSource('TestRecord_JSValue',
  10272. LinesToStr([ // statements
  10273. 'rtl.recNewT($mod, "TRecord", function () {',
  10274. ' this.i = 0;',
  10275. ' this.$eq = function (b) {',
  10276. ' return this.i === b.i;',
  10277. ' };',
  10278. ' this.$assign = function (s) {',
  10279. ' this.i = s.i;',
  10280. ' return this;',
  10281. ' };',
  10282. '});',
  10283. 'this.Fly = function (d, c) {',
  10284. '};',
  10285. 'this.Run = function (d, c, v) {',
  10286. ' if (d) ;',
  10287. ' if (c) ;',
  10288. ' if (v) ;',
  10289. '};',
  10290. 'this.Jv = undefined;',
  10291. 'this.Rec = $mod.TRecord.$new();',
  10292. '']),
  10293. LinesToStr([
  10294. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10295. '$mod.Jv = $mod.Rec;',
  10296. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  10297. '$mod.Fly($mod.Rec, $mod.Rec);',
  10298. 'if ($mod.Rec) ;',
  10299. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  10300. '']));
  10301. end;
  10302. procedure TTestModule.TestRecord_VariantFail;
  10303. begin
  10304. StartProgram(false);
  10305. Add([
  10306. 'type',
  10307. ' TRec = record',
  10308. ' case word of',
  10309. ' 0: (b0, b1: Byte);',
  10310. ' 1: (i: word);',
  10311. ' end;',
  10312. 'begin']);
  10313. SetExpectedPasResolverError('variant record is not supported',
  10314. nXIsNotSupported);
  10315. ConvertProgram;
  10316. end;
  10317. procedure TTestModule.TestRecord_FieldArray;
  10318. begin
  10319. StartProgram(false);
  10320. Add([
  10321. 'type',
  10322. ' TArrInt = array[3..4] of longint;',
  10323. ' TArrArrInt = array[3..4] of longint;',
  10324. ' TRec = record',
  10325. ' a: array of longint;',
  10326. ' s: array[1..2] of longint;',
  10327. ' m: array[1..2,3..4] of longint;',
  10328. ' o: TArrArrInt;',
  10329. ' end;',
  10330. 'begin']);
  10331. ConvertProgram;
  10332. CheckSource('TestRecord_FieldArray',
  10333. LinesToStr([ // statements
  10334. 'rtl.recNewT($mod, "TRec", function () {',
  10335. ' this.a = [];',
  10336. ' this.$new = function () {',
  10337. ' var r = Object.create(this);',
  10338. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10339. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10340. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10341. ' return r;',
  10342. ' };',
  10343. ' this.$eq = function (b) {',
  10344. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10345. ' };',
  10346. ' this.$assign = function (s) {',
  10347. ' this.a = s.a;',
  10348. ' this.s = s.s.slice(0);',
  10349. ' this.m = s.m.slice(0);',
  10350. ' this.o = s.o.slice(0);',
  10351. ' return this;',
  10352. ' };',
  10353. '});',
  10354. '']),
  10355. LinesToStr([ // $mod.$main
  10356. '']));
  10357. end;
  10358. procedure TTestModule.TestRecord_Const;
  10359. begin
  10360. StartProgram(false);
  10361. Add([
  10362. 'type',
  10363. ' TArrInt = array[3..4] of longint;',
  10364. ' TPoint = record x,y: longint; end;',
  10365. ' TRec = record',
  10366. ' i: longint;',
  10367. ' a: array of longint;',
  10368. ' s: array[1..2] of longint;',
  10369. ' m: array[1..2,3..4] of longint;',
  10370. ' p: TPoint;',
  10371. ' end;',
  10372. ' TPoints = array of TPoint;',
  10373. 'const',
  10374. ' r: TRec = (',
  10375. ' i:1;',
  10376. ' a:(2,3);',
  10377. ' s:(4,5);',
  10378. ' m:( (11,12), (13,14) );',
  10379. ' p: (x:21; y:22)',
  10380. ' );',
  10381. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10382. 'begin']);
  10383. ConvertProgram;
  10384. CheckSource('TestRecord_Const',
  10385. LinesToStr([ // statements
  10386. 'rtl.recNewT($mod, "TPoint", function () {',
  10387. ' this.x = 0;',
  10388. ' this.y = 0;',
  10389. ' this.$eq = function (b) {',
  10390. ' return (this.x === b.x) && (this.y === b.y);',
  10391. ' };',
  10392. ' this.$assign = function (s) {',
  10393. ' this.x = s.x;',
  10394. ' this.y = s.y;',
  10395. ' return this;',
  10396. ' };',
  10397. '});',
  10398. 'rtl.recNewT($mod, "TRec", function () {',
  10399. ' this.i = 0;',
  10400. ' this.a = [];',
  10401. ' this.$new = function () {',
  10402. ' var r = Object.create(this);',
  10403. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10404. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10405. ' r.p = $mod.TPoint.$new();',
  10406. ' return r;',
  10407. ' };',
  10408. ' this.$eq = function (b) {',
  10409. ' 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);',
  10410. ' };',
  10411. ' this.$assign = function (s) {',
  10412. ' this.i = s.i;',
  10413. ' this.a = s.a;',
  10414. ' this.s = s.s.slice(0);',
  10415. ' this.m = s.m.slice(0);',
  10416. ' this.p.$assign(s.p);',
  10417. ' return this;',
  10418. ' };',
  10419. '});',
  10420. 'this.r = $mod.TRec.$clone({',
  10421. ' i: 1,',
  10422. ' a: [2, 3],',
  10423. ' s: [4, 5],',
  10424. ' m: [[11, 12], [13, 14]],',
  10425. ' p: $mod.TPoint.$clone({',
  10426. ' x: 21,',
  10427. ' y: 22',
  10428. ' })',
  10429. '});',
  10430. 'this.p = [$mod.TPoint.$clone({',
  10431. ' x: 1,',
  10432. ' y: 2',
  10433. '}), $mod.TPoint.$clone({',
  10434. ' x: 3,',
  10435. ' y: 4',
  10436. '})];',
  10437. '']),
  10438. LinesToStr([ // $mod.$main
  10439. '']));
  10440. end;
  10441. procedure TTestModule.TestRecord_TypecastFail;
  10442. begin
  10443. StartProgram(false);
  10444. Add([
  10445. 'type',
  10446. ' TPoint = record x,y: longint; end;',
  10447. ' TRec = record l: longint end;',
  10448. 'var p: TPoint;',
  10449. 'begin',
  10450. ' if TRec(p).l=2 then ;']);
  10451. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10452. nIllegalTypeConversionTo);
  10453. ConvertProgram;
  10454. end;
  10455. procedure TTestModule.TestRecord_InFunction;
  10456. begin
  10457. StartProgram(false);
  10458. Add([
  10459. 'var TPoint: longint = 3;',
  10460. 'procedure DoIt;',
  10461. 'type',
  10462. ' TPoint = record x,y: longint; end;',
  10463. ' TPoints = array of TPoint;',
  10464. 'var',
  10465. ' r: TPoint;',
  10466. ' p: TPoints;',
  10467. 'begin',
  10468. ' SetLength(p,2);',
  10469. 'end;',
  10470. 'begin']);
  10471. ConvertProgram;
  10472. CheckSource('TestRecord_InFunction',
  10473. LinesToStr([ // statements
  10474. 'this.TPoint = 3;',
  10475. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10476. ' this.x = 0;',
  10477. ' this.y = 0;',
  10478. ' this.$eq = function (b) {',
  10479. ' return (this.x === b.x) && (this.y === b.y);',
  10480. ' };',
  10481. ' this.$assign = function (s) {',
  10482. ' this.x = s.x;',
  10483. ' this.y = s.y;',
  10484. ' return this;',
  10485. ' };',
  10486. '});',
  10487. 'this.DoIt = function () {',
  10488. ' var r = TPoint$1.$new();',
  10489. ' var p = [];',
  10490. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10491. '};',
  10492. '']),
  10493. LinesToStr([ // $mod.$main
  10494. '']));
  10495. end;
  10496. procedure TTestModule.TestRecord_AnonymousFail;
  10497. begin
  10498. StartProgram(false);
  10499. Add([
  10500. 'var',
  10501. ' r: record x: word end;',
  10502. 'begin']);
  10503. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  10504. nNotYetImplemented);
  10505. ConvertProgram;
  10506. end;
  10507. procedure TTestModule.TestAdvRecord_Function;
  10508. begin
  10509. StartProgram(false);
  10510. Parser.Options:=Parser.Options+[po_cassignments];
  10511. Add([
  10512. '{$modeswitch AdvancedRecords}',
  10513. 'type',
  10514. ' TPoint = record',
  10515. ' x,y: word;',
  10516. ' function Add(const apt: TPoint): TPoint;',
  10517. ' end;',
  10518. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10519. 'begin',
  10520. ' Result:=Self;',
  10521. ' Result.x+=apt.x;',
  10522. ' Result.y:=Result.y+apt.y;',
  10523. ' Self:=apt;',
  10524. 'end;',
  10525. 'var p,q: TPoint;',
  10526. 'begin',
  10527. ' p.add(q);',
  10528. ' p:=default(TPoint);',
  10529. ' p:=q;',
  10530. '']);
  10531. ConvertProgram;
  10532. CheckSource('TestAdvRecord_Function',
  10533. LinesToStr([ // statements
  10534. 'rtl.recNewT($mod, "TPoint", function () {',
  10535. ' this.x = 0;',
  10536. ' this.y = 0;',
  10537. ' this.$eq = function (b) {',
  10538. ' return (this.x === b.x) && (this.y === b.y);',
  10539. ' };',
  10540. ' this.$assign = function (s) {',
  10541. ' this.x = s.x;',
  10542. ' this.y = s.y;',
  10543. ' return this;',
  10544. ' };',
  10545. ' this.Add = function (apt) {',
  10546. ' var Result = $mod.TPoint.$new();',
  10547. ' Result.$assign(this);',
  10548. ' Result.x += apt.x;',
  10549. ' Result.y = Result.y + apt.y;',
  10550. ' this.$assign(apt);',
  10551. ' return Result;',
  10552. ' };',
  10553. '});',
  10554. 'this.p = $mod.TPoint.$new();',
  10555. 'this.q = $mod.TPoint.$new();',
  10556. '']),
  10557. LinesToStr([ // $mod.$main
  10558. '$mod.p.Add($mod.q);',
  10559. '$mod.p.$assign($mod.TPoint.$new());',
  10560. '$mod.p.$assign($mod.q);',
  10561. '']));
  10562. end;
  10563. procedure TTestModule.TestAdvRecord_Property;
  10564. begin
  10565. StartProgram(false);
  10566. Add([
  10567. '{$modeswitch AdvancedRecords}',
  10568. 'type',
  10569. ' TPoint = record',
  10570. ' x,y: word;',
  10571. ' strict private',
  10572. ' function GetSize: longword;',
  10573. ' procedure SetSize(Value: longword);',
  10574. ' public',
  10575. ' property Size: longword read GetSize write SetSize;',
  10576. ' property Left: word read x write y;',
  10577. ' end;',
  10578. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  10579. 'function TPoint.GetSize: longword;',
  10580. 'begin',
  10581. ' x:=y;',
  10582. ' Size:=Size;',
  10583. ' Left:=Left;',
  10584. 'end;',
  10585. 'procedure TPoint.SetSize(Value: longword);',
  10586. 'begin',
  10587. 'end;',
  10588. 'var p,q: TPoint;',
  10589. 'begin',
  10590. ' p.Size:=q.Size;',
  10591. ' p.Left:=q.Left;',
  10592. '']);
  10593. ConvertProgram;
  10594. CheckSource('TestAdvRecord_Property',
  10595. LinesToStr([ // statements
  10596. 'rtl.recNewT($mod, "TPoint", function () {',
  10597. ' this.x = 0;',
  10598. ' this.y = 0;',
  10599. ' this.$eq = function (b) {',
  10600. ' return (this.x === b.x) && (this.y === b.y);',
  10601. ' };',
  10602. ' this.$assign = function (s) {',
  10603. ' this.x = s.x;',
  10604. ' this.y = s.y;',
  10605. ' return this;',
  10606. ' };',
  10607. ' this.GetSize = function () {',
  10608. ' var Result = 0;',
  10609. ' this.x = this.y;',
  10610. ' this.SetSize(this.GetSize());',
  10611. ' this.y = this.x;',
  10612. ' return Result;',
  10613. ' };',
  10614. ' this.SetSize = function (Value) {',
  10615. ' };',
  10616. '});',
  10617. 'this.SetSize = function (Value) {',
  10618. '};',
  10619. 'this.p = $mod.TPoint.$new();',
  10620. 'this.q = $mod.TPoint.$new();',
  10621. '']),
  10622. LinesToStr([ // $mod.$main
  10623. '$mod.p.SetSize($mod.q.GetSize());',
  10624. '$mod.p.y = $mod.q.x;',
  10625. '']));
  10626. end;
  10627. procedure TTestModule.TestAdvRecord_PropertyDefault;
  10628. begin
  10629. StartProgram(false);
  10630. Add([
  10631. '{$modeswitch AdvancedRecords}',
  10632. 'type',
  10633. ' TPoint = record',
  10634. ' strict private',
  10635. ' function GetItems(Index: word): word;',
  10636. ' procedure SetItems(Index: word; Value: word);',
  10637. ' public',
  10638. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  10639. ' end;',
  10640. 'function TPoint.GetItems(Index: word): word;',
  10641. 'begin',
  10642. ' Items[index]:=Items[index];',
  10643. ' self.Items[index]:=self.Items[index];',
  10644. 'end;',
  10645. 'procedure TPoint.SetItems(Index: word; Value: word);',
  10646. 'begin',
  10647. 'end;',
  10648. 'var p: TPoint;',
  10649. 'begin',
  10650. ' p[1]:=p[2];',
  10651. ' p.Items[3]:=p.Items[4];',
  10652. '']);
  10653. ConvertProgram;
  10654. CheckSource('TestAdvRecord_PropertyDefault',
  10655. LinesToStr([ // statements
  10656. 'rtl.recNewT($mod, "TPoint", function () {',
  10657. ' this.$eq = function (b) {',
  10658. ' return true;',
  10659. ' };',
  10660. ' this.$assign = function (s) {',
  10661. ' return this;',
  10662. ' };',
  10663. ' this.GetItems = function (Index) {',
  10664. ' var Result = 0;',
  10665. ' this.SetItems(Index, this.GetItems(Index));',
  10666. ' this.SetItems(Index, this.GetItems(Index));',
  10667. ' return Result;',
  10668. ' };',
  10669. ' this.SetItems = function (Index, Value) {',
  10670. ' };',
  10671. '});',
  10672. 'this.p = $mod.TPoint.$new();',
  10673. '']),
  10674. LinesToStr([ // $mod.$main
  10675. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  10676. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  10677. '']));
  10678. end;
  10679. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  10680. begin
  10681. StartProgram(false);
  10682. Add([
  10683. '{$modeswitch AdvancedRecords}',
  10684. 'type',
  10685. ' TRec = record',
  10686. ' class var',
  10687. ' Fx: longint;',
  10688. ' Fy: longint;',
  10689. ' class function GetInt: longint; static;',
  10690. ' class procedure SetInt(Value: longint); static;',
  10691. ' class procedure DoIt; static;',
  10692. ' class property IntA: longint read Fx write Fy;',
  10693. ' class property IntB: longint read GetInt write SetInt;',
  10694. ' end;',
  10695. 'class function trec.getint: longint;',
  10696. 'begin',
  10697. ' result:=fx;',
  10698. 'end;',
  10699. 'class procedure trec.setint(value: longint);',
  10700. 'begin',
  10701. 'end;',
  10702. 'class procedure trec.doit;',
  10703. 'begin',
  10704. ' IntA:=IntA+1;',
  10705. ' IntB:=IntB+1;',
  10706. 'end;',
  10707. 'var r: trec;',
  10708. 'begin',
  10709. ' trec.inta:=trec.inta+1;',
  10710. ' if trec.intb=2 then;',
  10711. ' trec.intb:=trec.intb+2;',
  10712. ' trec.setint(trec.inta);',
  10713. ' r.inta:=r.inta+1;',
  10714. ' if r.intb=2 then;',
  10715. ' r.intb:=r.intb+2;',
  10716. ' r.setint(r.inta);']);
  10717. ConvertProgram;
  10718. CheckSource('TestAdvRecord_Property_ClassMethod',
  10719. LinesToStr([ // statements
  10720. 'rtl.recNewT($mod, "TRec", function () {',
  10721. ' this.Fx = 0;',
  10722. ' this.Fy = 0;',
  10723. ' this.$eq = function (b) {',
  10724. ' return true;',
  10725. ' };',
  10726. ' this.$assign = function (s) {',
  10727. ' return this;',
  10728. ' };',
  10729. ' this.GetInt = function () {',
  10730. ' var Result = 0;',
  10731. ' Result = $mod.TRec.Fx;',
  10732. ' return Result;',
  10733. ' };',
  10734. ' this.SetInt = function (Value) {',
  10735. ' };',
  10736. ' this.DoIt = function () {',
  10737. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10738. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  10739. ' };',
  10740. '}, true);',
  10741. 'this.r = $mod.TRec.$new();',
  10742. '']),
  10743. LinesToStr([ // $mod.$main
  10744. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10745. 'if ($mod.TRec.GetInt() === 2) ;',
  10746. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  10747. '$mod.TRec.SetInt($mod.TRec.Fx);',
  10748. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  10749. 'if ($mod.r.GetInt() === 2) ;',
  10750. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  10751. '$mod.r.SetInt($mod.r.Fx);',
  10752. '']));
  10753. end;
  10754. procedure TTestModule.TestAdvRecord_Const;
  10755. begin
  10756. StartProgram(false);
  10757. Add([
  10758. '{$modeswitch AdvancedRecords}',
  10759. 'type',
  10760. ' TArrInt = array[3..4] of longint;',
  10761. ' TPoint = record',
  10762. ' x,y: longint;',
  10763. ' class var Count: nativeint;',
  10764. ' end;',
  10765. ' TRec = record',
  10766. ' i: longint;',
  10767. ' a: array of longint;',
  10768. ' s: array[1..2] of longint;',
  10769. ' m: array[1..2,3..4] of longint;',
  10770. ' p: TPoint;',
  10771. ' end;',
  10772. ' TPoints = array of TPoint;',
  10773. 'const',
  10774. ' r: TRec = (',
  10775. ' i:1;',
  10776. ' a:(2,3);',
  10777. ' s:(4,5);',
  10778. ' m:( (11,12), (13,14) );',
  10779. ' p: (x:21)',
  10780. ' );',
  10781. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10782. 'begin']);
  10783. ConvertProgram;
  10784. CheckSource('TestAdvRecord_Const',
  10785. LinesToStr([ // statements
  10786. 'rtl.recNewT($mod, "TPoint", function () {',
  10787. ' this.x = 0;',
  10788. ' this.y = 0;',
  10789. ' this.Count = 0;',
  10790. ' this.$eq = function (b) {',
  10791. ' return (this.x === b.x) && (this.y === b.y);',
  10792. ' };',
  10793. ' this.$assign = function (s) {',
  10794. ' this.x = s.x;',
  10795. ' this.y = s.y;',
  10796. ' return this;',
  10797. ' };',
  10798. '}, true);',
  10799. 'rtl.recNewT($mod, "TRec", function () {',
  10800. ' this.i = 0;',
  10801. ' this.a = [];',
  10802. ' this.$new = function () {',
  10803. ' var r = Object.create(this);',
  10804. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10805. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10806. ' r.p = $mod.TPoint.$new();',
  10807. ' return r;',
  10808. ' };',
  10809. ' this.$eq = function (b) {',
  10810. ' 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);',
  10811. ' };',
  10812. ' this.$assign = function (s) {',
  10813. ' this.i = s.i;',
  10814. ' this.a = s.a;',
  10815. ' this.s = s.s.slice(0);',
  10816. ' this.m = s.m.slice(0);',
  10817. ' this.p.$assign(s.p);',
  10818. ' return this;',
  10819. ' };',
  10820. '});',
  10821. 'this.r = $mod.TRec.$clone({',
  10822. ' i: 1,',
  10823. ' a: [2, 3],',
  10824. ' s: [4, 5],',
  10825. ' m: [[11, 12], [13, 14]],',
  10826. ' p: $mod.TPoint.$clone({',
  10827. ' x: 21,',
  10828. ' y: 0',
  10829. ' })',
  10830. '});',
  10831. 'this.p = [$mod.TPoint.$clone({',
  10832. ' x: 1,',
  10833. ' y: 2',
  10834. '}), $mod.TPoint.$clone({',
  10835. ' x: 3,',
  10836. ' y: 4',
  10837. '})];',
  10838. '']),
  10839. LinesToStr([ // $mod.$main
  10840. '']));
  10841. end;
  10842. procedure TTestModule.TestAdvRecord_ExternalField;
  10843. begin
  10844. StartProgram(false);
  10845. Add([
  10846. '{$modeswitch AdvancedRecords}',
  10847. '{$modeswitch externalclass}',
  10848. 'type',
  10849. ' TCar = record',
  10850. ' public',
  10851. ' Intern: longint external name ''$Intern'';',
  10852. ' Intern2: longint external name ''$Intern2'';',
  10853. ' Bracket: longint external name ''["A B"]'';',
  10854. ' procedure DoIt;',
  10855. ' end;',
  10856. 'procedure tcar.doit;',
  10857. 'begin',
  10858. ' Intern:=Intern+1;',
  10859. ' Intern2:=Intern2+2;',
  10860. ' Bracket:=Bracket+3;',
  10861. 'end;',
  10862. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  10863. 'begin',
  10864. ' Rec.intern:=Rec.intern+1;',
  10865. ' Rec.intern2:=Rec.intern2+2;',
  10866. ' Rec.Bracket:=Rec.Bracket+3;',
  10867. ' with Rec do begin',
  10868. ' intern:=intern+1;',
  10869. ' intern2:=intern2+2;',
  10870. ' Bracket:=Bracket+3;',
  10871. ' end;']);
  10872. ConvertProgram;
  10873. CheckSource('TestAdvRecord_ExternalField',
  10874. LinesToStr([ // statements
  10875. 'rtl.recNewT($mod, "TCar", function () {',
  10876. ' this.$eq = function (b) {',
  10877. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  10878. ' };',
  10879. ' this.$assign = function (s) {',
  10880. ' this.$Intern = s.$Intern;',
  10881. ' this.$Intern2 = s.$Intern2;',
  10882. ' this["A B"] = s["A B"];',
  10883. ' return this;',
  10884. ' };',
  10885. ' this.DoIt = function () {',
  10886. ' this.$Intern = this.$Intern + 1;',
  10887. ' this.$Intern2 = this.$Intern2 + 2;',
  10888. ' this["A B"] = this["A B"] + 3;',
  10889. ' };',
  10890. '});',
  10891. 'this.Rec = $mod.TCar.$clone({',
  10892. ' $Intern: 11,',
  10893. ' $Intern2: 12,',
  10894. ' "A B": 13',
  10895. '});',
  10896. '']),
  10897. LinesToStr([ // $mod.$main
  10898. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  10899. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  10900. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  10901. 'var $with1 = $mod.Rec;',
  10902. '$with1.$Intern = $with1.$Intern + 1;',
  10903. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  10904. '$with1["A B"] = $with1["A B"] + 3;',
  10905. '']));
  10906. end;
  10907. procedure TTestModule.TestAdvRecord_SubRecord;
  10908. begin
  10909. StartProgram(false);
  10910. Add([
  10911. '{$modeswitch AdvancedRecords}',
  10912. 'type',
  10913. ' TRec = record',
  10914. ' type',
  10915. ' TPoint = record',
  10916. ' x,y: longint;',
  10917. ' class var Count: nativeint;',
  10918. ' procedure DoIt;',
  10919. ' class procedure DoThat; static;',
  10920. ' end;',
  10921. ' var',
  10922. ' i: longint;',
  10923. ' p: TPoint;',
  10924. ' procedure DoSome;',
  10925. ' end;',
  10926. 'const',
  10927. ' r: TRec = (',
  10928. ' i:1;',
  10929. ' p: (x:21;y:22)',
  10930. ' );',
  10931. 'procedure TRec.DoSome;',
  10932. 'begin',
  10933. ' p.x:=p.y+1;',
  10934. ' p.Count:=p.Count+2;',
  10935. 'end;',
  10936. 'procedure TRec.TPoint.DoIt;',
  10937. 'begin',
  10938. ' Count:=Count+3;',
  10939. 'end;',
  10940. 'class procedure TRec.TPoint.DoThat;',
  10941. 'begin',
  10942. ' Count:=Count+4;',
  10943. 'end;',
  10944. 'begin']);
  10945. ConvertProgram;
  10946. CheckSource('TestAdvRecord_SubRecord',
  10947. LinesToStr([ // statements
  10948. 'rtl.recNewT($mod, "TRec", function () {',
  10949. ' rtl.recNewT(this, "TPoint", function () {',
  10950. ' this.x = 0;',
  10951. ' this.y = 0;',
  10952. ' this.Count = 0;',
  10953. ' this.$eq = function (b) {',
  10954. ' return (this.x === b.x) && (this.y === b.y);',
  10955. ' };',
  10956. ' this.$assign = function (s) {',
  10957. ' this.x = s.x;',
  10958. ' this.y = s.y;',
  10959. ' return this;',
  10960. ' };',
  10961. ' this.DoIt = function () {',
  10962. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  10963. ' };',
  10964. ' this.DoThat = function () {',
  10965. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  10966. ' };',
  10967. ' }, true);',
  10968. ' this.i = 0;',
  10969. ' this.$new = function () {',
  10970. ' var r = Object.create(this);',
  10971. ' r.p = this.TPoint.$new();',
  10972. ' return r;',
  10973. ' };',
  10974. ' this.$eq = function (b) {',
  10975. ' return (this.i === b.i) && this.p.$eq(b.p);',
  10976. ' };',
  10977. ' this.$assign = function (s) {',
  10978. ' this.i = s.i;',
  10979. ' this.p.$assign(s.p);',
  10980. ' return this;',
  10981. ' };',
  10982. ' this.DoSome = function () {',
  10983. ' this.p.x = this.p.y + 1;',
  10984. ' this.TPoint.Count = this.p.Count + 2;',
  10985. ' };',
  10986. '}, true);',
  10987. 'this.r = $mod.TRec.$clone({',
  10988. ' i: 1,',
  10989. ' p: $mod.TRec.TPoint.$clone({',
  10990. ' x: 21,',
  10991. ' y: 22',
  10992. ' })',
  10993. '});',
  10994. '']),
  10995. LinesToStr([ // $mod.$main
  10996. '']));
  10997. end;
  10998. procedure TTestModule.TestAdvRecord_SubClass;
  10999. begin
  11000. StartProgram(false);
  11001. Add([
  11002. '{$modeswitch AdvancedRecords}',
  11003. 'type',
  11004. ' TObject = class end;',
  11005. ' TPoint = record',
  11006. ' type',
  11007. ' TBird = class',
  11008. ' procedure DoIt;',
  11009. ' class procedure Glob;',
  11010. ' end;',
  11011. ' procedure DoIt(b: TBird);',
  11012. ' end;',
  11013. 'procedure TPoint.TBird.DoIt;',
  11014. 'begin',
  11015. ' doit;',
  11016. ' self.doit;',
  11017. ' glob;',
  11018. ' self.glob;',
  11019. 'end;',
  11020. 'class procedure TPoint.TBird.Glob;',
  11021. 'begin',
  11022. ' glob;',
  11023. ' self.glob;',
  11024. 'end;',
  11025. 'procedure TPoint.DoIt(b: TBird);',
  11026. 'begin',
  11027. ' b.doit;',
  11028. ' b.glob;',
  11029. ' TBird.glob;',
  11030. 'end;',
  11031. 'begin',
  11032. '']);
  11033. ConvertProgram;
  11034. CheckSource('TestAdvRecord_SubClass',
  11035. LinesToStr([ // statements
  11036. 'rtl.createClass($mod, "TObject", null, function () {',
  11037. ' this.$init = function () {',
  11038. ' };',
  11039. ' this.$final = function () {',
  11040. ' };',
  11041. '});',
  11042. 'rtl.recNewT($mod, "TPoint", function () {',
  11043. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11044. ' this.DoIt = function () {',
  11045. ' this.DoIt();',
  11046. ' this.DoIt();',
  11047. ' this.$class.Glob();',
  11048. ' this.$class.Glob();',
  11049. ' };',
  11050. ' this.Glob = function () {',
  11051. ' this.Glob();',
  11052. ' this.Glob();',
  11053. ' };',
  11054. ' });',
  11055. ' this.$eq = function (b) {',
  11056. ' return true;',
  11057. ' };',
  11058. ' this.$assign = function (s) {',
  11059. ' return this;',
  11060. ' };',
  11061. ' this.DoIt = function (b) {',
  11062. ' b.DoIt();',
  11063. ' b.$class.Glob();',
  11064. ' this.TBird.Glob();',
  11065. ' };',
  11066. '}, true);',
  11067. '']),
  11068. LinesToStr([ // $mod.$main
  11069. '']));
  11070. end;
  11071. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11072. begin
  11073. StartProgram(false);
  11074. Add([
  11075. '{$modeswitch AdvancedRecords}',
  11076. 'type',
  11077. ' IUnknown = interface end;',
  11078. ' TPoint = record',
  11079. ' type IBird = interface end;',
  11080. ' end;',
  11081. 'begin',
  11082. '']);
  11083. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11084. nNotYetImplemented);
  11085. ParseProgram;
  11086. end;
  11087. procedure TTestModule.TestAdvRecord_Constructor;
  11088. begin
  11089. StartProgram(false);
  11090. Add([
  11091. '{$modeswitch AdvancedRecords}',
  11092. 'type',
  11093. ' TPoint = record',
  11094. ' x,y: longint;',
  11095. ' constructor Create(ax: longint; ay: longint = -1);',
  11096. ' end;',
  11097. 'constructor tpoint.create(ax,ay: longint);',
  11098. 'begin',
  11099. ' x:=ax;',
  11100. ' self.y:=ay;',
  11101. 'end;',
  11102. 'var r: TPoint;',
  11103. 'begin',
  11104. ' r:=TPoint.Create(1,2);',
  11105. ' with TPoint do r:=Create(1,2);',
  11106. ' r.Create(3);',
  11107. ' r:=r.Create(4);',
  11108. '']);
  11109. ConvertProgram;
  11110. CheckSource('TestAdvRecord_Constructor',
  11111. LinesToStr([ // statements
  11112. 'rtl.recNewT($mod, "TPoint", function () {',
  11113. ' this.x = 0;',
  11114. ' this.y = 0;',
  11115. ' this.$eq = function (b) {',
  11116. ' return (this.x === b.x) && (this.y === b.y);',
  11117. ' };',
  11118. ' this.$assign = function (s) {',
  11119. ' this.x = s.x;',
  11120. ' this.y = s.y;',
  11121. ' return this;',
  11122. ' };',
  11123. ' this.Create = function (ax, ay) {',
  11124. ' this.x = ax;',
  11125. ' this.y = ay;',
  11126. ' return this;',
  11127. ' };',
  11128. '}, true);',
  11129. 'this.r = $mod.TPoint.$new();',
  11130. '']),
  11131. LinesToStr([ // $mod.$main
  11132. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11133. 'var $with1 = $mod.TPoint;',
  11134. '$mod.r.$assign($with1.$new().Create(1, 2));',
  11135. '$mod.r.Create(3, -1);',
  11136. '$mod.r.$assign($mod.r.Create(4, -1));',
  11137. '']));
  11138. end;
  11139. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11140. begin
  11141. StartProgram(false);
  11142. Add([
  11143. '{$modeswitch AdvancedRecords}',
  11144. 'type',
  11145. ' TPoint = record',
  11146. ' class var x: longint;',
  11147. ' class procedure Fly; static;',
  11148. ' class constructor Init;',
  11149. ' end;',
  11150. 'var count: word;',
  11151. 'class procedure Tpoint.Fly;',
  11152. 'begin',
  11153. 'end;',
  11154. 'class constructor tpoint.init;',
  11155. 'begin',
  11156. ' count:=count+1;',
  11157. ' x:=x+3;',
  11158. ' tpoint.x:=tpoint.x+4;',
  11159. ' fly;',
  11160. ' tpoint.fly;',
  11161. 'end;',
  11162. 'var r: TPoint;',
  11163. 'begin',
  11164. ' r.x:=r.x+10;',
  11165. ' r.Fly;',
  11166. ' r.Fly();',
  11167. '']);
  11168. ConvertProgram;
  11169. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11170. LinesToStr([ // statements
  11171. 'rtl.recNewT($mod, "TPoint", function () {',
  11172. ' this.x = 0;',
  11173. ' this.$eq = function (b) {',
  11174. ' return true;',
  11175. ' };',
  11176. ' this.$assign = function (s) {',
  11177. ' return this;',
  11178. ' };',
  11179. ' this.Fly = function () {',
  11180. ' };',
  11181. '}, true);',
  11182. 'this.count = 0;',
  11183. 'this.r = $mod.TPoint.$new();',
  11184. '']),
  11185. LinesToStr([ // $mod.$main
  11186. '(function () {',
  11187. ' $mod.count = $mod.count + 1;',
  11188. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11189. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11190. ' $mod.TPoint.Fly();',
  11191. ' $mod.TPoint.Fly();',
  11192. '})();',
  11193. '$mod.TPoint.x = $mod.r.x + 10;',
  11194. '$mod.r.Fly();',
  11195. '$mod.r.Fly();',
  11196. '']));
  11197. end;
  11198. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  11199. begin
  11200. StartUnit(false);
  11201. Add([
  11202. 'interface',
  11203. '{$modeswitch AdvancedRecords}',
  11204. 'type',
  11205. ' TPoint = record',
  11206. ' class var x: longint;',
  11207. ' class procedure Fly; static;',
  11208. ' class constructor Init;',
  11209. ' end;',
  11210. 'implementation',
  11211. 'var count: word;',
  11212. 'class procedure Tpoint.Fly;',
  11213. 'begin',
  11214. 'end;',
  11215. 'class constructor tpoint.init;',
  11216. 'begin',
  11217. ' count:=count+1;',
  11218. ' x:=3;',
  11219. ' tpoint.x:=4;',
  11220. ' fly;',
  11221. ' tpoint.fly;',
  11222. 'end;',
  11223. '']);
  11224. ConvertUnit;
  11225. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  11226. LinesToStr([ // statements
  11227. 'var $impl = $mod.$impl;',
  11228. 'rtl.recNewT($mod, "TPoint", function () {',
  11229. ' this.x = 0;',
  11230. ' this.$eq = function (b) {',
  11231. ' return true;',
  11232. ' };',
  11233. ' this.$assign = function (s) {',
  11234. ' return this;',
  11235. ' };',
  11236. ' this.Fly = function () {',
  11237. ' };',
  11238. '}, true);',
  11239. '']),
  11240. LinesToStr([ // $mod.$init
  11241. '(function () {',
  11242. ' $impl.count = $impl.count + 1;',
  11243. ' $mod.TPoint.x = 3;',
  11244. ' $mod.TPoint.x = 4;',
  11245. ' $mod.TPoint.Fly();',
  11246. ' $mod.TPoint.Fly();',
  11247. '})();',
  11248. '']),
  11249. LinesToStr([ // $mod.$main
  11250. '$impl.count = 0;',
  11251. '']));
  11252. end;
  11253. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11254. begin
  11255. StartProgram(false);
  11256. Add(['type',
  11257. ' TObject = class',
  11258. ' public',
  11259. ' constructor Create;',
  11260. ' destructor Destroy;',
  11261. ' end;',
  11262. ' TBird = TObject;',
  11263. 'constructor tobject.create;',
  11264. 'begin end;',
  11265. 'destructor tobject.destroy;',
  11266. 'begin end;',
  11267. 'var Obj: tobject;',
  11268. 'begin',
  11269. ' obj:=tobject.create;',
  11270. ' obj:=tobject.create();',
  11271. ' obj:=tbird.create;',
  11272. ' obj:=tbird.create();',
  11273. ' obj:=obj.create();',
  11274. ' obj.destroy;',
  11275. '']);
  11276. ConvertProgram;
  11277. CheckSource('TestClass_TObjectDefaultConstructor',
  11278. LinesToStr([ // statements
  11279. 'rtl.createClass($mod,"TObject",null,function(){',
  11280. ' this.$init = function () {',
  11281. ' };',
  11282. ' this.$final = function () {',
  11283. ' };',
  11284. ' this.Create = function(){',
  11285. ' return this;',
  11286. ' };',
  11287. ' this.Destroy = function(){',
  11288. ' };',
  11289. '});',
  11290. 'this.Obj = null;'
  11291. ]),
  11292. LinesToStr([ // $mod.$main
  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.TObject.$create("Create");',
  11297. '$mod.Obj = $mod.Obj.Create();',
  11298. '$mod.Obj.$destroy("Destroy");',
  11299. '']));
  11300. end;
  11301. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11302. begin
  11303. StartProgram(false);
  11304. Add('type');
  11305. Add(' TObject = class');
  11306. Add(' public');
  11307. Add(' constructor Create(Par: longint);');
  11308. Add(' end;');
  11309. Add('constructor tobject.create(par: longint);');
  11310. Add('begin end;');
  11311. Add('var Obj: tobject;');
  11312. Add('begin');
  11313. Add(' obj:=tobject.create(3);');
  11314. ConvertProgram;
  11315. CheckSource('TestClass_TObjectConstructorWithParams',
  11316. LinesToStr([ // statements
  11317. 'rtl.createClass($mod,"TObject",null,function(){',
  11318. ' this.$init = function () {',
  11319. ' };',
  11320. ' this.$final = function () {',
  11321. ' };',
  11322. ' this.Create = function(Par){',
  11323. ' return this;',
  11324. ' };',
  11325. '});',
  11326. 'this.Obj = null;'
  11327. ]),
  11328. LinesToStr([ // $mod.$main
  11329. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11330. ]));
  11331. end;
  11332. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11333. begin
  11334. StartProgram(false);
  11335. Add('type');
  11336. Add(' TObject = class');
  11337. Add(' public');
  11338. Add(' constructor Create;');
  11339. Add(' end;');
  11340. Add(' TTest = class(TObject)');
  11341. Add(' public');
  11342. Add(' constructor Create(const Par: longint = 1);');
  11343. Add(' end;');
  11344. Add('constructor tobject.create;');
  11345. Add('begin end;');
  11346. Add('constructor ttest.create(const par: longint);');
  11347. Add('begin end;');
  11348. Add('var t: ttest;');
  11349. Add('begin');
  11350. Add(' t:=ttest.create;');
  11351. Add(' t:=ttest.create(2);');
  11352. ConvertProgram;
  11353. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11354. LinesToStr([ // statements
  11355. 'rtl.createClass($mod,"TObject",null,function(){',
  11356. ' this.$init = function () {',
  11357. ' };',
  11358. ' this.$final = function () {',
  11359. ' };',
  11360. ' this.Create = function(){',
  11361. ' return this;',
  11362. ' };',
  11363. '});',
  11364. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11365. ' this.Create$1 = function (Par) {',
  11366. ' return this;',
  11367. ' };',
  11368. '});',
  11369. 'this.t = null;'
  11370. ]),
  11371. LinesToStr([ // $mod.$main
  11372. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11373. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11374. ]));
  11375. end;
  11376. procedure TTestModule.TestClass_Var;
  11377. begin
  11378. StartProgram(false);
  11379. Add([
  11380. 'type',
  11381. ' TObject = class',
  11382. ' public',
  11383. ' vI: longint;',
  11384. ' constructor Create(Par: longint);',
  11385. ' end;',
  11386. 'constructor tobject.create(par: longint);',
  11387. 'begin',
  11388. ' vi:=par+3',
  11389. 'end;',
  11390. 'var Obj: tobject;',
  11391. 'begin',
  11392. ' obj:=tobject.create(4);',
  11393. ' obj.vi:=obj.VI+5;']);
  11394. ConvertProgram;
  11395. CheckSource('TestClass_Var',
  11396. LinesToStr([ // statements
  11397. 'rtl.createClass($mod,"TObject",null,function(){',
  11398. ' this.$init = function () {',
  11399. ' this.vI = 0;',
  11400. ' };',
  11401. ' this.$final = function () {',
  11402. ' };',
  11403. ' this.Create = function(Par){',
  11404. ' this.vI = Par+3;',
  11405. ' return this;',
  11406. ' };',
  11407. '});',
  11408. 'this.Obj = null;'
  11409. ]),
  11410. LinesToStr([ // $mod.$main
  11411. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11412. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11413. ]));
  11414. end;
  11415. procedure TTestModule.TestClass_Method;
  11416. begin
  11417. StartProgram(false);
  11418. Add('type');
  11419. Add(' TObject = class');
  11420. Add(' public');
  11421. Add(' vI: longint;');
  11422. Add(' Sub: TObject;');
  11423. Add(' constructor Create;');
  11424. Add(' function GetIt(Par: longint): tobject;');
  11425. Add(' end;');
  11426. Add('constructor tobject.create; begin end;');
  11427. Add('function tobject.getit(par: longint): tobject;');
  11428. Add('begin');
  11429. Add(' Self.vi:=par+3;');
  11430. Add(' Result:=self.sub;');
  11431. Add('end;');
  11432. Add('var Obj: tobject;');
  11433. Add('begin');
  11434. Add(' obj:=tobject.create;');
  11435. Add(' obj.getit(4);');
  11436. Add(' obj.sub.sub:=nil;');
  11437. Add(' obj.sub.getit(5);');
  11438. Add(' obj.sub.getit(6).SUB:=nil;');
  11439. Add(' obj.sub.getit(7).GETIT(8);');
  11440. Add(' obj.sub.getit(9).SuB.getit(10);');
  11441. ConvertProgram;
  11442. CheckSource('TestClass_Method',
  11443. LinesToStr([ // statements
  11444. 'rtl.createClass($mod,"TObject",null,function(){',
  11445. ' this.$init = function () {',
  11446. ' this.vI = 0;',
  11447. ' this.Sub = null;',
  11448. ' };',
  11449. ' this.$final = function () {',
  11450. ' this.Sub = undefined;',
  11451. ' };',
  11452. ' this.Create = function(){',
  11453. ' return this;',
  11454. ' };',
  11455. ' this.GetIt = function(Par){',
  11456. ' var Result = null;',
  11457. ' this.vI = Par + 3;',
  11458. ' Result = this.Sub;',
  11459. ' return Result;',
  11460. ' };',
  11461. '});',
  11462. 'this.Obj = null;'
  11463. ]),
  11464. LinesToStr([ // $mod.$main
  11465. '$mod.Obj = $mod.TObject.$create("Create");',
  11466. '$mod.Obj.GetIt(4);',
  11467. '$mod.Obj.Sub.Sub=null;',
  11468. '$mod.Obj.Sub.GetIt(5);',
  11469. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11470. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11471. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11472. ]));
  11473. end;
  11474. procedure TTestModule.TestClass_Implementation;
  11475. begin
  11476. StartUnit(false);
  11477. Add([
  11478. 'interface',
  11479. 'type',
  11480. ' TObject = class',
  11481. ' constructor Create;',
  11482. ' end;',
  11483. 'implementation',
  11484. 'type',
  11485. ' TIntClass = class',
  11486. ' constructor Create; reintroduce;',
  11487. ' class procedure DoGlob;',
  11488. ' end;',
  11489. 'constructor tintclass.create;',
  11490. 'begin',
  11491. ' inherited;',
  11492. ' inherited create;',
  11493. ' doglob;',
  11494. 'end;',
  11495. 'class procedure tintclass.doglob;',
  11496. 'begin',
  11497. 'end;',
  11498. 'constructor tobject.create;',
  11499. 'var',
  11500. ' iC: tintclass;',
  11501. 'begin',
  11502. ' ic:=tintclass.create;',
  11503. ' tintclass.doglob;',
  11504. ' ic.doglob;',
  11505. 'end;',
  11506. 'initialization',
  11507. ' tintclass.doglob;',
  11508. '']);
  11509. ConvertUnit;
  11510. CheckSource('TestClass_Implementation',
  11511. LinesToStr([ // statements
  11512. 'var $impl = $mod.$impl;',
  11513. 'rtl.createClass($mod, "TObject", null, function () {',
  11514. ' this.$init = function () {',
  11515. ' };',
  11516. ' this.$final = function () {',
  11517. ' };',
  11518. ' this.Create = function () {',
  11519. ' var iC = null;',
  11520. ' iC = $impl.TIntClass.$create("Create$1");',
  11521. ' $impl.TIntClass.DoGlob();',
  11522. ' iC.$class.DoGlob();',
  11523. ' return this;',
  11524. ' };',
  11525. '});',
  11526. '']),
  11527. LinesToStr([ // $mod.$main
  11528. '$impl.TIntClass.DoGlob();',
  11529. '']),
  11530. LinesToStr([
  11531. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  11532. ' this.Create$1 = function () {',
  11533. ' $mod.TObject.Create.call(this);',
  11534. ' $mod.TObject.Create.call(this);',
  11535. ' this.$class.DoGlob();',
  11536. ' return this;',
  11537. ' };',
  11538. ' this.DoGlob = function () {',
  11539. ' };',
  11540. '});',
  11541. '']));
  11542. end;
  11543. procedure TTestModule.TestClass_Inheritance;
  11544. begin
  11545. StartProgram(false);
  11546. Add('type');
  11547. Add(' TObject = class');
  11548. Add(' public');
  11549. Add(' constructor Create;');
  11550. Add(' end;');
  11551. Add(' TClassA = class');
  11552. Add(' end;');
  11553. Add(' TClassB = class(TObject)');
  11554. Add(' procedure ProcB;');
  11555. Add(' end;');
  11556. Add('constructor tobject.create; begin end;');
  11557. Add('procedure tclassb.procb; begin end;');
  11558. Add('var');
  11559. Add(' oO: TObject;');
  11560. Add(' oA: TClassA;');
  11561. Add(' oB: TClassB;');
  11562. Add('begin');
  11563. Add(' oO:=tobject.Create;');
  11564. Add(' oA:=tclassa.Create;');
  11565. Add(' ob:=tclassb.Create;');
  11566. Add(' if oo is tclassa then ;');
  11567. Add(' ob:=oo as tclassb;');
  11568. Add(' (oo as tclassb).procb;');
  11569. ConvertProgram;
  11570. CheckSource('TestClass_Inheritance',
  11571. LinesToStr([ // statements
  11572. 'rtl.createClass($mod,"TObject",null,function(){',
  11573. ' this.$init = function () {',
  11574. ' };',
  11575. ' this.$final = function () {',
  11576. ' };',
  11577. ' this.Create = function () {',
  11578. ' return this;',
  11579. ' };',
  11580. '});',
  11581. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  11582. '});',
  11583. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  11584. ' this.ProcB = function () {',
  11585. ' };',
  11586. '});',
  11587. 'this.oO = null;',
  11588. 'this.oA = null;',
  11589. 'this.oB = null;'
  11590. ]),
  11591. LinesToStr([ // $mod.$main
  11592. '$mod.oO = $mod.TObject.$create("Create");',
  11593. '$mod.oA = $mod.TClassA.$create("Create");',
  11594. '$mod.oB = $mod.TClassB.$create("Create");',
  11595. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  11596. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  11597. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  11598. ]));
  11599. end;
  11600. procedure TTestModule.TestClass_TypeAlias;
  11601. begin
  11602. StartProgram(false);
  11603. Add([
  11604. '{$interfaces corba}',
  11605. 'type',
  11606. ' IObject = interface',
  11607. ' end;',
  11608. ' IBird = type IObject;',
  11609. ' TObject = class',
  11610. ' end;',
  11611. ' TBird = type TObject;',
  11612. 'var',
  11613. ' oObj: TObject;',
  11614. ' oBird: TBird;',
  11615. ' IntfObj: IObject;',
  11616. ' IntfBird: IBird;',
  11617. 'begin',
  11618. ' oObj:=oBird;',
  11619. '']);
  11620. ConvertProgram;
  11621. CheckSource('TestClass_TypeAlias',
  11622. LinesToStr([ // statements
  11623. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  11624. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  11625. 'rtl.createClass($mod, "TObject", null, function () {',
  11626. ' this.$init = function () {',
  11627. ' };',
  11628. ' this.$final = function () {',
  11629. ' };',
  11630. '});',
  11631. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11632. '});',
  11633. 'this.oObj = null;',
  11634. 'this.oBird = null;',
  11635. 'this.IntfObj = null;',
  11636. 'this.IntfBird = null;',
  11637. '']),
  11638. LinesToStr([ // $mod.$main
  11639. '$mod.oObj = $mod.oBird;',
  11640. '']));
  11641. end;
  11642. procedure TTestModule.TestClass_AbstractMethod;
  11643. begin
  11644. StartProgram(false);
  11645. Add('type');
  11646. Add(' TObject = class');
  11647. Add(' public');
  11648. Add(' procedure DoIt; virtual; abstract;');
  11649. Add(' end;');
  11650. Add('begin');
  11651. ConvertProgram;
  11652. CheckSource('TestClass_AbstractMethod',
  11653. LinesToStr([ // statements
  11654. 'rtl.createClass($mod,"TObject",null,function(){',
  11655. ' this.$init = function () {',
  11656. ' };',
  11657. ' this.$final = function () {',
  11658. ' };',
  11659. '});'
  11660. ]),
  11661. LinesToStr([ // this.$main
  11662. ''
  11663. ]));
  11664. end;
  11665. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  11666. begin
  11667. StartProgram(false);
  11668. Add([
  11669. 'type',
  11670. ' TObject = class',
  11671. ' procedure DoAbstract; virtual; abstract;',
  11672. ' procedure DoVirtual; virtual;',
  11673. ' procedure DoIt;',
  11674. ' end;',
  11675. ' TA = class',
  11676. ' procedure doabstract; override;',
  11677. ' procedure dovirtual; override;',
  11678. ' procedure DoSome;',
  11679. ' end;',
  11680. 'procedure tobject.dovirtual;',
  11681. 'begin',
  11682. ' inherited; // call non existing ancestor -> ignore silently',
  11683. 'end;',
  11684. 'procedure tobject.doit;',
  11685. 'begin',
  11686. 'end;',
  11687. 'procedure ta.doabstract;',
  11688. 'begin',
  11689. ' inherited dovirtual; // call TObject.DoVirtual',
  11690. 'end;',
  11691. 'procedure ta.dovirtual;',
  11692. 'begin',
  11693. ' inherited; // call TObject.DoVirtual',
  11694. ' inherited dovirtual; // call TObject.DoVirtual',
  11695. ' inherited dovirtual(); // call TObject.DoVirtual',
  11696. ' doit;',
  11697. ' doit();',
  11698. 'end;',
  11699. 'procedure ta.dosome;',
  11700. 'begin',
  11701. ' inherited; // call non existing ancestor method -> silently ignore',
  11702. 'end;',
  11703. 'begin']);
  11704. ConvertProgram;
  11705. CheckSource('TestClass_CallInherited_ProcNoParams',
  11706. LinesToStr([ // statements
  11707. 'rtl.createClass($mod,"TObject",null,function(){',
  11708. ' this.$init = function () {',
  11709. ' };',
  11710. ' this.$final = function () {',
  11711. ' };',
  11712. ' this.DoVirtual = function () {',
  11713. ' };',
  11714. ' this.DoIt = function () {',
  11715. ' };',
  11716. '});',
  11717. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11718. ' this.DoAbstract = function () {',
  11719. ' $mod.TObject.DoVirtual.call(this);',
  11720. ' };',
  11721. ' this.DoVirtual = function () {',
  11722. ' $mod.TObject.DoVirtual.call(this);',
  11723. ' $mod.TObject.DoVirtual.call(this);',
  11724. ' $mod.TObject.DoVirtual.call(this);',
  11725. ' this.DoIt();',
  11726. ' this.DoIt();',
  11727. ' };',
  11728. ' this.DoSome = function () {',
  11729. ' };',
  11730. '});'
  11731. ]),
  11732. LinesToStr([ // this.$main
  11733. ''
  11734. ]));
  11735. end;
  11736. procedure TTestModule.TestClass_CallInherited_WithParams;
  11737. begin
  11738. StartProgram(false);
  11739. Add([
  11740. 'type',
  11741. ' TObject = class',
  11742. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  11743. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  11744. ' procedure DoIt(pA: longint; pB: longint = 0);',
  11745. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  11746. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11747. ' end;',
  11748. ' TClassA = class',
  11749. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  11750. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  11751. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  11752. ' end;',
  11753. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  11754. 'begin',
  11755. 'end;',
  11756. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  11757. 'begin',
  11758. 'end;',
  11759. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  11760. 'begin',
  11761. 'end;',
  11762. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  11763. 'begin',
  11764. 'end;',
  11765. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  11766. 'begin',
  11767. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11768. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11769. 'end;',
  11770. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  11771. 'begin',
  11772. ' inherited; // call TObject.DoVirtual(pA,pB)',
  11773. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  11774. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  11775. ' doit(pa,pb);',
  11776. ' doit(pa);',
  11777. ' doit2(pa);',
  11778. ' doit2;',
  11779. 'end;',
  11780. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  11781. 'begin',
  11782. ' pa:=inherited;',
  11783. 'end;',
  11784. 'begin']);
  11785. ConvertProgram;
  11786. CheckSource('TestClass_CallInherited_WithParams',
  11787. LinesToStr([ // statements
  11788. 'rtl.createClass($mod,"TObject",null,function(){',
  11789. ' this.$init = function () {',
  11790. ' };',
  11791. ' this.$final = function () {',
  11792. ' };',
  11793. ' this.DoVirtual = function (pA,pB) {',
  11794. ' };',
  11795. ' this.DoIt = function (pA,pB) {',
  11796. ' };',
  11797. ' this.DoIt2 = function (pA,pB) {',
  11798. ' };',
  11799. ' this.GetIt = function (pA, pB) {',
  11800. ' var Result = 0;',
  11801. ' return Result;',
  11802. ' };',
  11803. '});',
  11804. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  11805. ' this.DoAbstract = function (pA,pB) {',
  11806. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11807. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11808. ' };',
  11809. ' this.DoVirtual = function (pA,pB) {',
  11810. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  11811. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11812. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11813. ' this.DoIt(pA,pB);',
  11814. ' this.DoIt(pA,0);',
  11815. ' this.DoIt2(pA,2);',
  11816. ' this.DoIt2(1,2);',
  11817. ' };',
  11818. ' this.GetIt$1 = function (pA, pB) {',
  11819. ' var Result = 0;',
  11820. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  11821. ' return Result;',
  11822. ' };',
  11823. '});'
  11824. ]),
  11825. LinesToStr([ // this.$main
  11826. ''
  11827. ]));
  11828. end;
  11829. procedure TTestModule.TestClasS_CallInheritedConstructor;
  11830. begin
  11831. StartProgram(false);
  11832. Add('type');
  11833. Add(' TObject = class');
  11834. Add(' constructor Create; virtual;');
  11835. Add(' constructor CreateWithB(b: boolean);');
  11836. Add(' end;');
  11837. Add(' TA = class');
  11838. Add(' constructor Create; override;');
  11839. Add(' constructor CreateWithC(c: char);');
  11840. Add(' procedure DoIt;');
  11841. Add(' class function DoSome: TObject;');
  11842. Add(' end;');
  11843. Add('constructor tobject.create;');
  11844. Add('begin');
  11845. Add(' inherited; // call non existing ancestor -> ignore silently');
  11846. Add('end;');
  11847. Add('constructor tobject.createwithb(b: boolean);');
  11848. Add('begin');
  11849. Add(' inherited; // call non existing ancestor -> ignore silently');
  11850. Add(' create; // normal call');
  11851. Add('end;');
  11852. Add('constructor ta.create;');
  11853. Add('begin');
  11854. Add(' inherited; // normal call TObject.Create');
  11855. Add(' inherited create; // normal call TObject.Create');
  11856. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  11857. Add('end;');
  11858. Add('constructor ta.createwithc(c: char);');
  11859. Add('begin');
  11860. Add(' inherited create; // call TObject.Create');
  11861. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  11862. Add(' doit;');
  11863. Add(' doit();');
  11864. Add(' dosome;');
  11865. Add('end;');
  11866. Add('procedure ta.doit;');
  11867. Add('begin');
  11868. Add(' create; // normal call');
  11869. Add(' createwithb(false); // normal call');
  11870. Add(' createwithc(''c''); // normal call');
  11871. Add('end;');
  11872. Add('class function ta.dosome: TObject;');
  11873. Add('begin');
  11874. Add(' Result:=create; // constructor');
  11875. Add(' Result:=createwithb(true); // constructor');
  11876. Add(' Result:=createwithc(''c''); // constructor');
  11877. Add('end;');
  11878. Add('begin');
  11879. ConvertProgram;
  11880. CheckSource('TestClass_CallInheritedConstructor',
  11881. LinesToStr([ // statements
  11882. 'rtl.createClass($mod,"TObject",null,function(){',
  11883. ' this.$init = function () {',
  11884. ' };',
  11885. ' this.$final = function () {',
  11886. ' };',
  11887. ' this.Create = function () {',
  11888. ' return this;',
  11889. ' };',
  11890. ' this.CreateWithB = function (b) {',
  11891. ' this.Create();',
  11892. ' return this;',
  11893. ' };',
  11894. '});',
  11895. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11896. ' this.Create = function () {',
  11897. ' $mod.TObject.Create.call(this);',
  11898. ' $mod.TObject.Create.call(this);',
  11899. ' $mod.TObject.CreateWithB.call(this, false);',
  11900. ' return this;',
  11901. ' };',
  11902. ' this.CreateWithC = function (c) {',
  11903. ' $mod.TObject.Create.call(this);',
  11904. ' $mod.TObject.CreateWithB.call(this, true);',
  11905. ' this.DoIt();',
  11906. ' this.DoIt();',
  11907. ' this.$class.DoSome();',
  11908. ' return this;',
  11909. ' };',
  11910. ' this.DoIt = function () {',
  11911. ' this.Create();',
  11912. ' this.CreateWithB(false);',
  11913. ' this.CreateWithC("c");',
  11914. ' };',
  11915. ' this.DoSome = function () {',
  11916. ' var Result = null;',
  11917. ' Result = this.$create("Create");',
  11918. ' Result = this.$create("CreateWithB", [true]);',
  11919. ' Result = this.$create("CreateWithC", ["c"]);',
  11920. ' return Result;',
  11921. ' };',
  11922. '});'
  11923. ]),
  11924. LinesToStr([ // this.$main
  11925. ''
  11926. ]));
  11927. end;
  11928. procedure TTestModule.TestClass_ClassVar_Assign;
  11929. begin
  11930. StartProgram(false);
  11931. Add([
  11932. 'type',
  11933. ' TObject = class',
  11934. ' public',
  11935. ' class var vI: longint;',
  11936. ' class var Sub: TObject;',
  11937. ' constructor Create;',
  11938. ' class function GetIt(var Par: longint): tobject;',
  11939. ' end;',
  11940. 'constructor tobject.create;',
  11941. 'begin',
  11942. ' vi:=vi+1;',
  11943. ' Self.vi:=Self.vi+1;',
  11944. ' inc(vi);',
  11945. 'end;',
  11946. 'class function tobject.getit(var par: longint): tobject;',
  11947. 'begin',
  11948. ' vi:=vi+3;',
  11949. ' Self.vi:=Self.vi+4;',
  11950. ' inc(vi);',
  11951. ' Result:=self.sub;',
  11952. ' GetIt(vi);',
  11953. 'end;',
  11954. 'var Obj: tobject;',
  11955. 'begin',
  11956. ' obj:=tobject.create;',
  11957. ' tobject.vi:=3;',
  11958. ' if tobject.vi=4 then ;',
  11959. ' tobject.sub:=nil;',
  11960. ' obj.sub:=nil;',
  11961. ' obj.sub.sub:=nil;']);
  11962. ConvertProgram;
  11963. CheckSource('TestClass_ClassVar_Assign',
  11964. LinesToStr([ // statements
  11965. 'rtl.createClass($mod,"TObject",null,function(){',
  11966. ' this.vI = 0;',
  11967. ' this.Sub = null;',
  11968. ' this.$init = function () {',
  11969. ' };',
  11970. ' this.$final = function () {',
  11971. ' };',
  11972. ' this.Create = function(){',
  11973. ' $mod.TObject.vI = this.vI+1;',
  11974. ' $mod.TObject.vI = this.vI+1;',
  11975. ' $mod.TObject.vI += 1;',
  11976. ' return this;',
  11977. ' };',
  11978. ' this.GetIt = function(Par){',
  11979. ' var Result = null;',
  11980. ' $mod.TObject.vI = this.vI + 3;',
  11981. ' $mod.TObject.vI = this.vI + 4;',
  11982. ' $mod.TObject.vI += 1;',
  11983. ' Result = this.Sub;',
  11984. ' this.GetIt({',
  11985. ' p: $mod.TObject,',
  11986. ' get: function () {',
  11987. ' return this.p.vI;',
  11988. ' },',
  11989. ' set: function (v) {',
  11990. ' this.p.vI = v;',
  11991. ' }',
  11992. ' });',
  11993. ' return Result;',
  11994. ' };',
  11995. '});',
  11996. 'this.Obj = null;'
  11997. ]),
  11998. LinesToStr([ // $mod.$main
  11999. '$mod.Obj = $mod.TObject.$create("Create");',
  12000. '$mod.TObject.vI = 3;',
  12001. 'if ($mod.TObject.vI === 4);',
  12002. '$mod.TObject.Sub=null;',
  12003. '$mod.TObject.Sub=null;',
  12004. '$mod.TObject.Sub=null;',
  12005. '']));
  12006. end;
  12007. procedure TTestModule.TestClass_CallClassMethod;
  12008. begin
  12009. StartProgram(false);
  12010. Add('type');
  12011. Add(' TObject = class');
  12012. Add(' public');
  12013. Add(' class var vI: longint;');
  12014. Add(' class var Sub: TObject;');
  12015. Add(' constructor Create;');
  12016. Add(' function GetMore(Par: longint): longint;');
  12017. Add(' class function GetIt(Par: longint): tobject;');
  12018. Add(' end;');
  12019. Add('constructor tobject.create;');
  12020. Add('begin');
  12021. Add(' sub:=getit(3);');
  12022. Add(' vi:=getmore(4);');
  12023. Add(' sub:=Self.getit(5);');
  12024. Add(' vi:=Self.getmore(6);');
  12025. Add('end;');
  12026. Add('function tobject.getmore(par: longint): longint;');
  12027. Add('begin');
  12028. Add(' sub:=getit(11);');
  12029. Add(' vi:=getmore(12);');
  12030. Add(' sub:=self.getit(13);');
  12031. Add(' vi:=self.getmore(14);');
  12032. Add('end;');
  12033. Add('class function tobject.getit(par: longint): tobject;');
  12034. Add('begin');
  12035. Add(' sub:=getit(21);');
  12036. Add(' vi:=sub.getmore(22);');
  12037. Add(' sub:=self.getit(23);');
  12038. Add(' vi:=self.sub.getmore(24);');
  12039. Add('end;');
  12040. Add('var Obj: tobject;');
  12041. Add('begin');
  12042. Add(' obj:=tobject.create;');
  12043. Add(' tobject.getit(5);');
  12044. Add(' obj.getit(6);');
  12045. Add(' obj.sub.getit(7);');
  12046. Add(' obj.sub.getit(8).SUB:=nil;');
  12047. Add(' obj.sub.getit(9).GETIT(10);');
  12048. Add(' obj.sub.getit(11).SuB.getit(12);');
  12049. ConvertProgram;
  12050. CheckSource('TestClass_CallClassMethod',
  12051. LinesToStr([ // statements
  12052. 'rtl.createClass($mod,"TObject",null,function(){',
  12053. ' this.vI = 0;',
  12054. ' this.Sub = null;',
  12055. ' this.$init = function () {',
  12056. ' };',
  12057. ' this.$final = function () {',
  12058. ' };',
  12059. ' this.Create = function(){',
  12060. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12061. ' $mod.TObject.vI = this.GetMore(4);',
  12062. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12063. ' $mod.TObject.vI = this.GetMore(6);',
  12064. ' return this;',
  12065. ' };',
  12066. ' this.GetMore = function(Par){',
  12067. ' var Result = 0;',
  12068. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12069. ' $mod.TObject.vI = this.GetMore(12);',
  12070. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12071. ' $mod.TObject.vI = this.GetMore(14);',
  12072. ' return Result;',
  12073. ' };',
  12074. ' this.GetIt = function(Par){',
  12075. ' var Result = null;',
  12076. ' $mod.TObject.Sub = this.GetIt(21);',
  12077. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12078. ' $mod.TObject.Sub = this.GetIt(23);',
  12079. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12080. ' return Result;',
  12081. ' };',
  12082. '});',
  12083. 'this.Obj = null;'
  12084. ]),
  12085. LinesToStr([ // $mod.$main
  12086. '$mod.Obj = $mod.TObject.$create("Create");',
  12087. '$mod.TObject.GetIt(5);',
  12088. '$mod.Obj.$class.GetIt(6);',
  12089. '$mod.Obj.Sub.$class.GetIt(7);',
  12090. '$mod.TObject.Sub=null;',
  12091. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12092. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12093. '']));
  12094. end;
  12095. procedure TTestModule.TestClass_Property;
  12096. begin
  12097. StartProgram(false);
  12098. Add('type');
  12099. Add(' TObject = class');
  12100. Add(' Fx: longint;');
  12101. Add(' Fy: longint;');
  12102. Add(' function GetInt: longint;');
  12103. Add(' procedure SetInt(Value: longint);');
  12104. Add(' procedure DoIt;');
  12105. Add(' property IntA: longint read Fx write Fy;');
  12106. Add(' property IntB: longint read GetInt write SetInt;');
  12107. Add(' end;');
  12108. Add('function tobject.getint: longint;');
  12109. Add('begin');
  12110. Add(' result:=fx;');
  12111. Add('end;');
  12112. Add('procedure tobject.setint(value: longint);');
  12113. Add('begin');
  12114. Add(' if value=fy then exit;');
  12115. Add(' fy:=value;');
  12116. Add('end;');
  12117. Add('procedure tobject.doit;');
  12118. Add('begin');
  12119. Add(' IntA:=IntA+1;');
  12120. Add(' Self.IntA:=Self.IntA+1;');
  12121. Add(' IntB:=IntB+1;');
  12122. Add(' Self.IntB:=Self.IntB+1;');
  12123. Add('end;');
  12124. Add('var Obj: tobject;');
  12125. Add('begin');
  12126. Add(' obj.inta:=obj.inta+1;');
  12127. Add(' if obj.intb=2 then;');
  12128. Add(' obj.intb:=obj.intb+2;');
  12129. Add(' obj.setint(obj.inta);');
  12130. ConvertProgram;
  12131. CheckSource('TestClass_Property',
  12132. LinesToStr([ // statements
  12133. 'rtl.createClass($mod, "TObject", null, function () {',
  12134. ' this.$init = function () {',
  12135. ' this.Fx = 0;',
  12136. ' this.Fy = 0;',
  12137. ' };',
  12138. ' this.$final = function () {',
  12139. ' };',
  12140. ' this.GetInt = function () {',
  12141. ' var Result = 0;',
  12142. ' Result = this.Fx;',
  12143. ' return Result;',
  12144. ' };',
  12145. ' this.SetInt = function (Value) {',
  12146. ' if (Value === this.Fy) return;',
  12147. ' this.Fy = Value;',
  12148. ' };',
  12149. ' this.DoIt = function () {',
  12150. ' this.Fy = this.Fx + 1;',
  12151. ' this.Fy = this.Fx + 1;',
  12152. ' this.SetInt(this.GetInt() + 1);',
  12153. ' this.SetInt(this.GetInt() + 1);',
  12154. ' };',
  12155. '});',
  12156. 'this.Obj = null;'
  12157. ]),
  12158. LinesToStr([ // $mod.$main
  12159. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12160. 'if ($mod.Obj.GetInt() === 2);',
  12161. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12162. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12163. ]));
  12164. end;
  12165. procedure TTestModule.TestClass_Property_ClassMethod;
  12166. begin
  12167. StartProgram(false);
  12168. Add([
  12169. 'type',
  12170. ' TObject = class',
  12171. ' class var Fx: longint;',
  12172. ' class var Fy: longint;',
  12173. ' class function GetInt: longint;',
  12174. ' class procedure SetInt(Value: longint);',
  12175. ' end;',
  12176. ' TBird = class',
  12177. ' class procedure DoIt;',
  12178. ' class property IntA: longint read Fx write Fy;',
  12179. ' class property IntB: longint read GetInt write SetInt;',
  12180. ' end;',
  12181. 'class function tobject.getint: longint;',
  12182. 'begin',
  12183. ' result:=fx;',
  12184. 'end;',
  12185. 'class procedure tobject.setint(value: longint);',
  12186. 'begin',
  12187. 'end;',
  12188. 'class procedure tbird.doit;',
  12189. 'begin',
  12190. ' FX:=3;',
  12191. ' IntA:=IntA+1;',
  12192. ' Self.IntA:=Self.IntA+1;',
  12193. ' IntB:=IntB+1;',
  12194. ' Self.IntB:=Self.IntB+1;',
  12195. ' with Self do begin',
  12196. ' FX:=11;',
  12197. ' IntA:=IntA+12;',
  12198. ' IntB:=IntB+13;',
  12199. ' end;',
  12200. 'end;',
  12201. 'var Obj: tbird;',
  12202. 'begin',
  12203. ' tbird.fx:=tbird.fx+1;',
  12204. ' tbird.inta:=tbird.inta+1;',
  12205. ' if tbird.intb=2 then;',
  12206. ' tbird.intb:=tbird.intb+2;',
  12207. ' tbird.setint(tbird.inta);',
  12208. ' obj.inta:=obj.inta+1;',
  12209. ' if obj.intb=2 then;',
  12210. ' obj.intb:=obj.intb+2;',
  12211. ' obj.setint(obj.inta);',
  12212. ' with Tbird do begin',
  12213. ' FX:=FY+1;',
  12214. ' inta:=inta+2;',
  12215. ' intb:=intb+3;',
  12216. ' end;',
  12217. ' with Obj do begin',
  12218. ' FX:=FY+1;',
  12219. ' inta:=inta+2;',
  12220. ' intb:=intb+3;',
  12221. ' end;',
  12222. '']);
  12223. ConvertProgram;
  12224. CheckSource('TestClass_Property_ClassMethod',
  12225. LinesToStr([ // statements
  12226. 'rtl.createClass($mod, "TObject", null, function () {',
  12227. ' this.Fx = 0;',
  12228. ' this.Fy = 0;',
  12229. ' this.$init = function () {',
  12230. ' };',
  12231. ' this.$final = function () {',
  12232. ' };',
  12233. ' this.GetInt = function () {',
  12234. ' var Result = 0;',
  12235. ' Result = this.Fx;',
  12236. ' return Result;',
  12237. ' };',
  12238. ' this.SetInt = function (Value) {',
  12239. ' };',
  12240. '});',
  12241. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12242. ' this.DoIt = function () {',
  12243. ' $mod.TObject.Fx = 3;',
  12244. ' $mod.TObject.Fy = this.Fx + 1;',
  12245. ' $mod.TObject.Fy = this.Fx + 1;',
  12246. ' this.SetInt(this.GetInt() + 1);',
  12247. ' this.SetInt(this.GetInt() + 1);',
  12248. ' $mod.TObject.Fx = 11;',
  12249. ' $mod.TObject.Fy = this.Fx + 12;',
  12250. ' this.SetInt(this.GetInt() + 13);',
  12251. ' };',
  12252. '});',
  12253. 'this.Obj = null;'
  12254. ]),
  12255. LinesToStr([ // $mod.$main
  12256. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12257. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12258. 'if ($mod.TBird.GetInt() === 2);',
  12259. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12260. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12261. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12262. 'if ($mod.Obj.$class.GetInt() === 2);',
  12263. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12264. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12265. 'var $with1 = $mod.TBird;',
  12266. '$mod.TObject.Fx = $with1.Fy + 1;',
  12267. '$mod.TObject.Fy = $with1.Fx + 2;',
  12268. '$with1.SetInt($with1.GetInt() + 3);',
  12269. 'var $with2 = $mod.Obj;',
  12270. '$mod.TObject.Fx = $with2.Fy + 1;',
  12271. '$mod.TObject.Fy = $with2.Fx + 2;',
  12272. '$with2.$class.SetInt($with2.$class.GetInt() + 3);',
  12273. '']));
  12274. end;
  12275. procedure TTestModule.TestClass_Property_Indexed;
  12276. begin
  12277. StartProgram(false);
  12278. Add('type');
  12279. Add(' TObject = class');
  12280. Add(' FItems: array of longint;');
  12281. Add(' function GetItems(Index: longint): longint;');
  12282. Add(' procedure SetItems(Index: longint; Value: longint);');
  12283. Add(' procedure DoIt;');
  12284. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  12285. Add(' end;');
  12286. Add('function tobject.getitems(index: longint): longint;');
  12287. Add('begin');
  12288. Add(' Result:=fitems[index];');
  12289. Add('end;');
  12290. Add('procedure tobject.setitems(index: longint; value: longint);');
  12291. Add('begin');
  12292. Add(' fitems[index]:=value;');
  12293. Add('end;');
  12294. Add('procedure tobject.doit;');
  12295. Add('begin');
  12296. Add(' items[1]:=2;');
  12297. Add(' items[3]:=items[4];');
  12298. Add(' self.items[5]:=self.items[6];');
  12299. Add(' items[items[7]]:=items[items[8]];');
  12300. Add('end;');
  12301. Add('var Obj: tobject;');
  12302. Add('begin');
  12303. Add(' obj.Items[11]:=obj.Items[12];');
  12304. ConvertProgram;
  12305. CheckSource('TestClass_Property_Indexed',
  12306. LinesToStr([ // statements
  12307. 'rtl.createClass($mod, "TObject", null, function () {',
  12308. ' this.$init = function () {',
  12309. ' this.FItems = [];',
  12310. ' };',
  12311. ' this.$final = function () {',
  12312. ' this.FItems = undefined;',
  12313. ' };',
  12314. ' this.GetItems = function (Index) {',
  12315. ' var Result = 0;',
  12316. ' Result = this.FItems[Index];',
  12317. ' return Result;',
  12318. ' };',
  12319. ' this.SetItems = function (Index, Value) {',
  12320. ' this.FItems[Index] = Value;',
  12321. ' };',
  12322. ' this.DoIt = function () {',
  12323. ' this.SetItems(1, 2);',
  12324. ' this.SetItems(3,this.GetItems(4));',
  12325. ' this.SetItems(5,this.GetItems(6));',
  12326. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12327. ' };',
  12328. '});',
  12329. 'this.Obj = null;'
  12330. ]),
  12331. LinesToStr([ // $mod.$main
  12332. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12333. ]));
  12334. end;
  12335. procedure TTestModule.TestClass_Property_IndexSpec;
  12336. begin
  12337. StartProgram(false);
  12338. Add([
  12339. 'type',
  12340. ' TEnum = (red, blue);',
  12341. ' TObject = class',
  12342. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12343. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12344. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12345. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12346. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12347. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12348. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12349. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12350. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12351. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12352. ' end;',
  12353. 'procedure DoIt(b: boolean); begin end;',
  12354. 'var',
  12355. ' o: TObject;',
  12356. 'begin',
  12357. ' o.B1:=o.B1;',
  12358. ' o.B2:=o.B2;',
  12359. ' o.B3:=o.B3;',
  12360. ' o.I1[''a'']:=o.I1[''b''];',
  12361. ' doit(o.b1);',
  12362. ' doit(o.b2);',
  12363. ' doit(o.i1[''c'']);',
  12364. '']);
  12365. ConvertProgram;
  12366. CheckSource('TestClass_Property_IndexSpec',
  12367. LinesToStr([ // statements
  12368. 'this.TEnum = {',
  12369. ' "0": "red",',
  12370. ' red: 0,',
  12371. ' "1": "blue",',
  12372. ' blue: 1',
  12373. '};',
  12374. 'rtl.createClass($mod, "TObject", null, function () {',
  12375. ' this.$init = function () {',
  12376. ' };',
  12377. ' this.$final = function () {',
  12378. ' };',
  12379. '});',
  12380. 'this.DoIt = function (b) {',
  12381. '};',
  12382. 'this.o = null;',
  12383. '']),
  12384. LinesToStr([ // $mod.$main
  12385. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12386. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12387. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12388. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12389. '$mod.DoIt($mod.o.GetIntBool(1));',
  12390. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12391. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12392. '']));
  12393. end;
  12394. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12395. begin
  12396. StartProgram(false);
  12397. Add('type');
  12398. Add(' TArray = array of longint;');
  12399. Add(' TObject = class');
  12400. Add(' FItems: TArray;');
  12401. Add(' function GetItems: tarray;');
  12402. Add(' procedure SetItems(Value: tarray);');
  12403. Add(' property Items: tarray read getitems write setitems;');
  12404. Add(' end;');
  12405. Add('function tobject.getitems: tarray;');
  12406. Add('begin');
  12407. Add(' Result:=fitems;');
  12408. Add('end;');
  12409. Add('procedure tobject.setitems(value: tarray);');
  12410. Add('begin');
  12411. Add(' fitems:=value;');
  12412. Add(' fitems:=nil;');
  12413. Add(' Items:=nil;');
  12414. Add(' Items:=Items;');
  12415. Add(' Items[1]:=2;');
  12416. Add(' fitems[3]:=Items[4];');
  12417. Add(' Items[5]:=Items[6];');
  12418. Add(' Self.Items[7]:=8;');
  12419. Add(' Self.Items[9]:=Self.Items[10];');
  12420. Add(' Items[Items[11]]:=Items[Items[12]];');
  12421. Add('end;');
  12422. Add('var Obj: tobject;');
  12423. Add('begin');
  12424. Add(' obj.items:=nil;');
  12425. Add(' obj.items:=obj.items;');
  12426. Add(' obj.items[11]:=obj.items[12];');
  12427. ConvertProgram;
  12428. CheckSource('TestClass_PropertyOfTypeArray',
  12429. LinesToStr([ // statements
  12430. 'rtl.createClass($mod, "TObject", null, function () {',
  12431. ' this.$init = function () {',
  12432. ' this.FItems = [];',
  12433. ' };',
  12434. ' this.$final = function () {',
  12435. ' this.FItems = undefined;',
  12436. ' };',
  12437. ' this.GetItems = function () {',
  12438. ' var Result = [];',
  12439. ' Result = this.FItems;',
  12440. ' return Result;',
  12441. ' };',
  12442. ' this.SetItems = function (Value) {',
  12443. ' this.FItems = Value;',
  12444. ' this.FItems = [];',
  12445. ' this.SetItems([]);',
  12446. ' this.SetItems(this.GetItems());',
  12447. ' this.GetItems()[1] = 2;',
  12448. ' this.FItems[3] = this.GetItems()[4];',
  12449. ' this.GetItems()[5] = this.GetItems()[6];',
  12450. ' this.GetItems()[7] = 8;',
  12451. ' this.GetItems()[9] = this.GetItems()[10];',
  12452. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12453. ' };',
  12454. '});',
  12455. 'this.Obj = null;'
  12456. ]),
  12457. LinesToStr([ // $mod.$main
  12458. '$mod.Obj.SetItems([]);',
  12459. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12460. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12461. ]));
  12462. end;
  12463. procedure TTestModule.TestClass_PropertyDefault;
  12464. begin
  12465. StartProgram(false);
  12466. Add([
  12467. 'type',
  12468. ' TArray = array of longint;',
  12469. ' TObject = class',
  12470. ' FItems: TArray;',
  12471. ' function GetItems(Index: longint): longint;',
  12472. ' procedure SetItems(Index, Value: longint);',
  12473. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12474. ' end;',
  12475. 'function tobject.getitems(index: longint): longint;',
  12476. 'begin',
  12477. 'end;',
  12478. 'procedure tobject.setitems(index, value: longint);',
  12479. 'begin',
  12480. ' Self[1]:=2;',
  12481. ' Self[3]:=Self[index];',
  12482. ' Self[index]:=Self[Self[value]];',
  12483. ' Self[Self[4]]:=value;',
  12484. 'end;',
  12485. 'var Obj: tobject;',
  12486. 'begin',
  12487. ' obj[11]:=12;',
  12488. ' obj[13]:=obj[14];',
  12489. ' obj[obj[15]]:=obj[obj[15]];',
  12490. ' TObject(obj)[16]:=TObject(obj)[17];']);
  12491. ConvertProgram;
  12492. CheckSource('TestClass_PropertyDefault',
  12493. LinesToStr([ // statements
  12494. 'rtl.createClass($mod, "TObject", null, function () {',
  12495. ' this.$init = function () {',
  12496. ' this.FItems = [];',
  12497. ' };',
  12498. ' this.$final = function () {',
  12499. ' this.FItems = undefined;',
  12500. ' };',
  12501. ' this.GetItems = function (Index) {',
  12502. ' var Result = 0;',
  12503. ' return Result;',
  12504. ' };',
  12505. ' this.SetItems = function (Index, Value) {',
  12506. ' this.SetItems(1, 2);',
  12507. ' this.SetItems(3, this.GetItems(Index));',
  12508. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  12509. ' this.SetItems(this.GetItems(4), Value);',
  12510. ' };',
  12511. '});',
  12512. 'this.Obj = null;'
  12513. ]),
  12514. LinesToStr([ // $mod.$main
  12515. '$mod.Obj.SetItems(11, 12);',
  12516. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  12517. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  12518. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  12519. '']));
  12520. end;
  12521. procedure TTestModule.TestClass_PropertyDefault2;
  12522. begin
  12523. StartProgram(false);
  12524. Add([
  12525. 'type',
  12526. ' TObject = class end;',
  12527. ' TAlphaList = class',
  12528. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  12529. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  12530. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  12531. ' end;',
  12532. ' TBetaList = class',
  12533. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  12534. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  12535. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  12536. ' end;',
  12537. ' TBird = class',
  12538. ' procedure DoIt;',
  12539. ' end;',
  12540. 'procedure TBird.DoIt;',
  12541. 'var',
  12542. ' List: TAlphaList;',
  12543. 'begin',
  12544. ' if TBetaList(List[true])[3]=nil then ;',
  12545. ' TBetaList(List[false])[5]:=nil;',
  12546. 'end;',
  12547. 'var',
  12548. ' List: TAlphaList;',
  12549. 'begin',
  12550. ' if TBetaList(List[true])[3]=nil then ;',
  12551. ' TBetaList(List[false])[5]:=nil;',
  12552. '']);
  12553. ConvertProgram;
  12554. CheckSource('TestClass_PropertyDefault2',
  12555. LinesToStr([ // statements
  12556. 'rtl.createClass($mod, "TObject", null, function () {',
  12557. ' this.$init = function () {',
  12558. ' };',
  12559. ' this.$final = function () {',
  12560. ' };',
  12561. '});',
  12562. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  12563. '});',
  12564. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  12565. '});',
  12566. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12567. ' this.DoIt = function () {',
  12568. ' var List = null;',
  12569. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  12570. ' List.GetAlphas(false).SetBetas(5, null);',
  12571. ' };',
  12572. '});',
  12573. 'this.List = null;',
  12574. '']),
  12575. LinesToStr([ // $mod.$main
  12576. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  12577. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  12578. '']));
  12579. end;
  12580. procedure TTestModule.TestClass_PropertyOverride;
  12581. begin
  12582. StartProgram(false);
  12583. Add('type');
  12584. Add(' integer = longint;');
  12585. Add(' TObject = class');
  12586. Add(' FItem: integer;');
  12587. Add(' function GetItem: integer; external name ''GetItem'';');
  12588. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  12589. Add(' property Item: integer read getitem write setitem;');
  12590. Add(' end;');
  12591. Add(' TCar = class');
  12592. Add(' FBag: integer;');
  12593. Add(' function GetBag: integer; external name ''GetBag'';');
  12594. Add(' property Item read getbag;');
  12595. Add(' end;');
  12596. Add('var');
  12597. Add(' Obj: tobject;');
  12598. Add(' Car: tcar;');
  12599. Add('begin');
  12600. Add(' Obj.Item:=Obj.Item;');
  12601. Add(' Car.Item:=Car.Item;');
  12602. ConvertProgram;
  12603. CheckSource('TestClass_PropertyOverride',
  12604. LinesToStr([ // statements
  12605. 'rtl.createClass($mod, "TObject", null, function () {',
  12606. ' this.$init = function () {',
  12607. ' this.FItem = 0;',
  12608. ' };',
  12609. ' this.$final = function () {',
  12610. ' };',
  12611. '});',
  12612. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12613. ' this.$init = function () {',
  12614. ' $mod.TObject.$init.call(this);',
  12615. ' this.FBag = 0;',
  12616. ' };',
  12617. '});',
  12618. 'this.Obj = null;',
  12619. 'this.Car = null;',
  12620. '']),
  12621. LinesToStr([ // $mod.$main
  12622. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  12623. '$mod.Car.SetItem($mod.Car.GetBag());',
  12624. '']));
  12625. end;
  12626. procedure TTestModule.TestClass_PropertyIncVisibility;
  12627. begin
  12628. AddModuleWithIntfImplSrc('unit1.pp',
  12629. LinesToStr([
  12630. 'type',
  12631. ' TNumber = longint;',
  12632. ' TInteger = longint;',
  12633. ' TObject = class',
  12634. ' private',
  12635. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  12636. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  12637. ' protected',
  12638. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  12639. ' end;']),
  12640. LinesToStr([
  12641. '']));
  12642. StartProgram(true);
  12643. Add([
  12644. 'uses unit1;',
  12645. 'type',
  12646. ' TBird = class',
  12647. ' public',
  12648. ' property Items;',
  12649. ' end;',
  12650. 'procedure DoIt(i: TInteger);',
  12651. 'begin',
  12652. 'end;',
  12653. 'var b: TBird;',
  12654. 'begin',
  12655. ' b.Items[1]:=2;',
  12656. ' b.Items[3]:=b.Items[4];',
  12657. ' DoIt(b.Items[5]);',
  12658. '']);
  12659. ConvertProgram;
  12660. CheckSource('TestClass_PropertyIncVisibility',
  12661. LinesToStr([ // statements
  12662. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  12663. '});',
  12664. 'this.DoIt = function (i) {',
  12665. '};',
  12666. 'this.b = null;'
  12667. ]),
  12668. LinesToStr([ // $mod.$main
  12669. '$mod.b.SetItems(1, 2);',
  12670. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  12671. '$mod.DoIt($mod.b.GetItems(5));'
  12672. ]));
  12673. end;
  12674. procedure TTestModule.TestClass_Assigned;
  12675. begin
  12676. StartProgram(false);
  12677. Add('type');
  12678. Add(' TObject = class');
  12679. Add(' end;');
  12680. Add('var');
  12681. Add(' Obj: tobject;');
  12682. Add(' b: boolean;');
  12683. Add('begin');
  12684. Add(' if Assigned(obj) then ;');
  12685. Add(' b:=Assigned(obj) or false;');
  12686. ConvertProgram;
  12687. CheckSource('TestClass_Assigned',
  12688. LinesToStr([ // statements
  12689. 'rtl.createClass($mod, "TObject", null, function () {',
  12690. ' this.$init = function () {',
  12691. ' };',
  12692. ' this.$final = function () {',
  12693. ' };',
  12694. '});',
  12695. 'this.Obj = null;',
  12696. 'this.b = false;'
  12697. ]),
  12698. LinesToStr([ // $mod.$main
  12699. 'if ($mod.Obj != null);',
  12700. '$mod.b = ($mod.Obj != null) || false;'
  12701. ]));
  12702. end;
  12703. procedure TTestModule.TestClass_WithClassDoCreate;
  12704. begin
  12705. StartProgram(false);
  12706. Add('type');
  12707. Add(' TObject = class');
  12708. Add(' aBool: boolean;');
  12709. Add(' Arr: array of boolean;');
  12710. Add(' constructor Create;');
  12711. Add(' end;');
  12712. Add('constructor TObject.Create; begin end;');
  12713. Add('var');
  12714. Add(' Obj: tobject;');
  12715. Add(' b: boolean;');
  12716. Add('begin');
  12717. Add(' with tobject.create do begin');
  12718. Add(' b:=abool;');
  12719. Add(' abool:=b;');
  12720. Add(' b:=arr[1];');
  12721. Add(' arr[2]:=b;');
  12722. Add(' end;');
  12723. Add(' with tobject do');
  12724. Add(' obj:=create;');
  12725. Add(' with obj do begin');
  12726. Add(' create;');
  12727. Add(' b:=abool;');
  12728. Add(' abool:=b;');
  12729. Add(' b:=arr[3];');
  12730. Add(' arr[4]:=b;');
  12731. Add(' end;');
  12732. ConvertProgram;
  12733. CheckSource('TestClass_WithClassDoCreate',
  12734. LinesToStr([ // statements
  12735. 'rtl.createClass($mod, "TObject", null, function () {',
  12736. ' this.$init = function () {',
  12737. ' this.aBool = false;',
  12738. ' this.Arr = [];',
  12739. ' };',
  12740. ' this.$final = function () {',
  12741. ' this.Arr = undefined;',
  12742. ' };',
  12743. ' this.Create = function () {',
  12744. ' return this;',
  12745. ' };',
  12746. '});',
  12747. 'this.Obj = null;',
  12748. 'this.b = false;'
  12749. ]),
  12750. LinesToStr([ // $mod.$main
  12751. 'var $with1 = $mod.TObject.$create("Create");',
  12752. '$mod.b = $with1.aBool;',
  12753. '$with1.aBool = $mod.b;',
  12754. '$mod.b = $with1.Arr[1];',
  12755. '$with1.Arr[2] = $mod.b;',
  12756. 'var $with2 = $mod.TObject;',
  12757. '$mod.Obj = $with2.$create("Create");',
  12758. 'var $with3 = $mod.Obj;',
  12759. '$with3.Create();',
  12760. '$mod.b = $with3.aBool;',
  12761. '$with3.aBool = $mod.b;',
  12762. '$mod.b = $with3.Arr[3];',
  12763. '$with3.Arr[4] = $mod.b;',
  12764. '']));
  12765. end;
  12766. procedure TTestModule.TestClass_WithClassInstDoProperty;
  12767. begin
  12768. StartProgram(false);
  12769. Add('type');
  12770. Add(' TObject = class');
  12771. Add(' FInt: longint;');
  12772. Add(' constructor Create;');
  12773. Add(' function GetSize: longint;');
  12774. Add(' procedure SetSize(Value: longint);');
  12775. Add(' property Int: longint read FInt write FInt;');
  12776. Add(' property Size: longint read GetSize write SetSize;');
  12777. Add(' end;');
  12778. Add('constructor TObject.Create; begin end;');
  12779. Add('function TObject.GetSize: longint; begin; end;');
  12780. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12781. Add('var');
  12782. Add(' Obj: tobject;');
  12783. Add(' i: longint;');
  12784. Add('begin');
  12785. Add(' with TObject.Create do begin');
  12786. Add(' i:=int;');
  12787. Add(' int:=i;');
  12788. Add(' i:=size;');
  12789. Add(' size:=i;');
  12790. Add(' end;');
  12791. Add(' with obj do begin');
  12792. Add(' i:=int;');
  12793. Add(' int:=i;');
  12794. Add(' i:=size;');
  12795. Add(' size:=i;');
  12796. Add(' end;');
  12797. ConvertProgram;
  12798. CheckSource('TestClass_WithClassInstDoProperty',
  12799. LinesToStr([ // statements
  12800. 'rtl.createClass($mod, "TObject", null, function () {',
  12801. ' this.$init = function () {',
  12802. ' this.FInt = 0;',
  12803. ' };',
  12804. ' this.$final = function () {',
  12805. ' };',
  12806. ' this.Create = function () {',
  12807. ' return this;',
  12808. ' };',
  12809. ' this.GetSize = function () {',
  12810. ' var Result = 0;',
  12811. ' return Result;',
  12812. ' };',
  12813. ' this.SetSize = function (Value) {',
  12814. ' };',
  12815. '});',
  12816. 'this.Obj = null;',
  12817. 'this.i = 0;'
  12818. ]),
  12819. LinesToStr([ // $mod.$main
  12820. 'var $with1 = $mod.TObject.$create("Create");',
  12821. '$mod.i = $with1.FInt;',
  12822. '$with1.FInt = $mod.i;',
  12823. '$mod.i = $with1.GetSize();',
  12824. '$with1.SetSize($mod.i);',
  12825. 'var $with2 = $mod.Obj;',
  12826. '$mod.i = $with2.FInt;',
  12827. '$with2.FInt = $mod.i;',
  12828. '$mod.i = $with2.GetSize();',
  12829. '$with2.SetSize($mod.i);',
  12830. '']));
  12831. end;
  12832. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  12833. begin
  12834. StartProgram(false);
  12835. Add('type');
  12836. Add(' TObject = class');
  12837. Add(' constructor Create;');
  12838. Add(' function GetItems(Index: longint): longint;');
  12839. Add(' procedure SetItems(Index, Value: longint);');
  12840. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  12841. Add(' end;');
  12842. Add('constructor TObject.Create; begin end;');
  12843. Add('function tobject.getitems(index: longint): longint; begin; end;');
  12844. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  12845. Add('var');
  12846. Add(' Obj: tobject;');
  12847. Add(' i: longint;');
  12848. Add('begin');
  12849. Add(' with TObject.Create do begin');
  12850. Add(' i:=Items[1];');
  12851. Add(' Items[2]:=i;');
  12852. Add(' end;');
  12853. Add(' with obj do begin');
  12854. Add(' i:=Items[3];');
  12855. Add(' Items[4]:=i;');
  12856. Add(' end;');
  12857. ConvertProgram;
  12858. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  12859. LinesToStr([ // statements
  12860. 'rtl.createClass($mod, "TObject", null, function () {',
  12861. ' this.$init = function () {',
  12862. ' };',
  12863. ' this.$final = function () {',
  12864. ' };',
  12865. ' this.Create = function () {',
  12866. ' return this;',
  12867. ' };',
  12868. ' this.GetItems = function (Index) {',
  12869. ' var Result = 0;',
  12870. ' return Result;',
  12871. ' };',
  12872. ' this.SetItems = function (Index, Value) {',
  12873. ' };',
  12874. '});',
  12875. 'this.Obj = null;',
  12876. 'this.i = 0;'
  12877. ]),
  12878. LinesToStr([ // $mod.$main
  12879. 'var $with1 = $mod.TObject.$create("Create");',
  12880. '$mod.i = $with1.GetItems(1);',
  12881. '$with1.SetItems(2, $mod.i);',
  12882. 'var $with2 = $mod.Obj;',
  12883. '$mod.i = $with2.GetItems(3);',
  12884. '$with2.SetItems(4, $mod.i);',
  12885. '']));
  12886. end;
  12887. procedure TTestModule.TestClass_WithClassInstDoFunc;
  12888. begin
  12889. StartProgram(false);
  12890. Add('type');
  12891. Add(' TObject = class');
  12892. Add(' constructor Create;');
  12893. Add(' function GetSize: longint;');
  12894. Add(' procedure SetSize(Value: longint);');
  12895. Add(' end;');
  12896. Add('constructor TObject.Create; begin end;');
  12897. Add('function TObject.GetSize: longint; begin; end;');
  12898. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12899. Add('var');
  12900. Add(' Obj: tobject;');
  12901. Add(' i: longint;');
  12902. Add('begin');
  12903. Add(' with TObject.Create do begin');
  12904. Add(' i:=GetSize;');
  12905. Add(' i:=GetSize();');
  12906. Add(' SetSize(i);');
  12907. Add(' end;');
  12908. Add(' with obj do begin');
  12909. Add(' i:=GetSize;');
  12910. Add(' i:=GetSize();');
  12911. Add(' SetSize(i);');
  12912. Add(' end;');
  12913. ConvertProgram;
  12914. CheckSource('TestClass_WithClassInstDoFunc',
  12915. LinesToStr([ // statements
  12916. 'rtl.createClass($mod, "TObject", null, function () {',
  12917. ' this.$init = function () {',
  12918. ' };',
  12919. ' this.$final = function () {',
  12920. ' };',
  12921. ' this.Create = function () {',
  12922. ' return this;',
  12923. ' };',
  12924. ' this.GetSize = function () {',
  12925. ' var Result = 0;',
  12926. ' return Result;',
  12927. ' };',
  12928. ' this.SetSize = function (Value) {',
  12929. ' };',
  12930. '});',
  12931. 'this.Obj = null;',
  12932. 'this.i = 0;'
  12933. ]),
  12934. LinesToStr([ // $mod.$main
  12935. 'var $with1 = $mod.TObject.$create("Create");',
  12936. '$mod.i = $with1.GetSize();',
  12937. '$mod.i = $with1.GetSize();',
  12938. '$with1.SetSize($mod.i);',
  12939. 'var $with2 = $mod.Obj;',
  12940. '$mod.i = $with2.GetSize();',
  12941. '$mod.i = $with2.GetSize();',
  12942. '$with2.SetSize($mod.i);',
  12943. '']));
  12944. end;
  12945. procedure TTestModule.TestClass_TypeCast;
  12946. begin
  12947. StartProgram(false);
  12948. Add('type');
  12949. Add(' TObject = class');
  12950. Add(' Next: TObject;');
  12951. Add(' constructor Create;');
  12952. Add(' end;');
  12953. Add(' TControl = class(TObject)');
  12954. Add(' Arr: array of TObject;');
  12955. Add(' function GetIt(vI: longint = 0): TObject;');
  12956. Add(' end;');
  12957. Add('constructor tobject.create; begin end;');
  12958. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  12959. Add('var');
  12960. Add(' Obj: tobject;');
  12961. Add('begin');
  12962. Add(' obj:=tcontrol(obj).next;');
  12963. Add(' tcontrol(obj):=nil;');
  12964. Add(' obj:=tcontrol(obj);');
  12965. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  12966. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  12967. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  12968. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  12969. Add(' obj:=tcontrol(nil);');
  12970. ConvertProgram;
  12971. CheckSource('TestClass_TypeCast',
  12972. LinesToStr([ // statements
  12973. 'rtl.createClass($mod, "TObject", null, function () {',
  12974. ' this.$init = function () {',
  12975. ' this.Next = null;',
  12976. ' };',
  12977. ' this.$final = function () {',
  12978. ' this.Next = undefined;',
  12979. ' };',
  12980. ' this.Create = function () {',
  12981. ' return this;',
  12982. ' };',
  12983. '});',
  12984. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  12985. ' this.$init = function () {',
  12986. ' $mod.TObject.$init.call(this);',
  12987. ' this.Arr = [];',
  12988. ' };',
  12989. ' this.$final = function () {',
  12990. ' this.Arr = undefined;',
  12991. ' $mod.TObject.$final.call(this);',
  12992. ' };',
  12993. ' this.GetIt = function (vI) {',
  12994. ' var Result = null;',
  12995. ' return Result;',
  12996. ' };',
  12997. '});',
  12998. 'this.Obj = null;'
  12999. ]),
  13000. LinesToStr([ // $mod.$main
  13001. '$mod.Obj = $mod.Obj.Next;',
  13002. '$mod.Obj = null;',
  13003. '$mod.Obj = $mod.Obj;',
  13004. '$mod.Obj = $mod.Obj.GetIt(0);',
  13005. '$mod.Obj = $mod.Obj.GetIt(0);',
  13006. '$mod.Obj = $mod.Obj.GetIt(1);',
  13007. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13008. '$mod.Obj = null;',
  13009. '']));
  13010. end;
  13011. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13012. begin
  13013. StartProgram(false);
  13014. Add('type');
  13015. Add(' TObject = class end;');
  13016. Add('procedure ProcA(var A);');
  13017. Add('begin');
  13018. Add(' TObject(A):=nil;');
  13019. Add(' TObject(A):=TObject(A);');
  13020. Add(' if TObject(A)=nil then ;');
  13021. Add(' if nil=TObject(A) then ;');
  13022. Add('end;');
  13023. Add('procedure ProcB(out A);');
  13024. Add('begin');
  13025. Add(' TObject(A):=nil;');
  13026. Add(' TObject(A):=TObject(A);');
  13027. Add(' if TObject(A)=nil then ;');
  13028. Add(' if nil=TObject(A) then ;');
  13029. Add('end;');
  13030. Add('procedure ProcC(const A);');
  13031. Add('begin');
  13032. Add(' if TObject(A)=nil then ;');
  13033. Add(' if nil=TObject(A) then ;');
  13034. Add('end;');
  13035. Add('var o: TObject;');
  13036. Add('begin');
  13037. Add(' ProcA(o);');
  13038. Add(' ProcB(o);');
  13039. Add(' ProcC(o);');
  13040. ConvertProgram;
  13041. CheckSource('TestClass_TypeCastUntypedParam',
  13042. LinesToStr([ // statements
  13043. 'rtl.createClass($mod, "TObject", null, function () {',
  13044. ' this.$init = function () {',
  13045. ' };',
  13046. ' this.$final = function () {',
  13047. ' };',
  13048. '});',
  13049. 'this.ProcA = function (A) {',
  13050. ' A.set(null);',
  13051. ' A.set(A.get());',
  13052. ' if (A.get() === null);',
  13053. ' if (null === A.get());',
  13054. '};',
  13055. 'this.ProcB = function (A) {',
  13056. ' A.set(null);',
  13057. ' A.set(A.get());',
  13058. ' if (A.get() === null);',
  13059. ' if (null === A.get());',
  13060. '};',
  13061. 'this.ProcC = function (A) {',
  13062. ' if (A === null);',
  13063. ' if (null === A);',
  13064. '};',
  13065. 'this.o = null;',
  13066. '']),
  13067. LinesToStr([ // $mod.$main
  13068. '$mod.ProcA({',
  13069. ' p: $mod,',
  13070. ' get: function () {',
  13071. ' return this.p.o;',
  13072. ' },',
  13073. ' set: function (v) {',
  13074. ' this.p.o = v;',
  13075. ' }',
  13076. '});',
  13077. '$mod.ProcB({',
  13078. ' p: $mod,',
  13079. ' get: function () {',
  13080. ' return this.p.o;',
  13081. ' },',
  13082. ' set: function (v) {',
  13083. ' this.p.o = v;',
  13084. ' }',
  13085. '});',
  13086. '$mod.ProcC($mod.o);',
  13087. '']));
  13088. end;
  13089. procedure TTestModule.TestClass_Overloads;
  13090. begin
  13091. StartProgram(false);
  13092. Add('type');
  13093. Add(' TObject = class');
  13094. Add(' procedure DoIt;');
  13095. Add(' procedure DoIt(vI: longint);');
  13096. Add(' end;');
  13097. Add('procedure TObject.DoIt;');
  13098. Add('begin');
  13099. Add(' DoIt;');
  13100. Add(' DoIt(1);');
  13101. Add('end;');
  13102. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13103. Add('begin');
  13104. ConvertProgram;
  13105. CheckSource('TestClass_Overloads',
  13106. LinesToStr([ // statements
  13107. 'rtl.createClass($mod, "TObject", null, function () {',
  13108. ' this.$init = function () {',
  13109. ' };',
  13110. ' this.$final = function () {',
  13111. ' };',
  13112. ' this.DoIt = function () {',
  13113. ' this.DoIt();',
  13114. ' this.DoIt$1(1);',
  13115. ' };',
  13116. ' this.DoIt$1 = function (vI) {',
  13117. ' };',
  13118. '});',
  13119. '']),
  13120. LinesToStr([ // $mod.$main
  13121. '']));
  13122. end;
  13123. procedure TTestModule.TestClass_OverloadsAncestor;
  13124. begin
  13125. StartProgram(false);
  13126. Add('type');
  13127. Add(' TObject = class;');
  13128. Add(' TObject = class');
  13129. Add(' procedure DoIt(vA: longint);');
  13130. Add(' procedure DoIt(vA, vB: longint);');
  13131. Add(' end;');
  13132. Add(' TCar = class;');
  13133. Add(' TCar = class');
  13134. Add(' procedure DoIt(vA: longint);');
  13135. Add(' procedure DoIt(vA, vB: longint);');
  13136. Add(' end;');
  13137. Add('procedure tobject.doit(va: longint);');
  13138. Add('begin');
  13139. Add(' doit(1);');
  13140. Add(' doit(1,2);');
  13141. Add('end;');
  13142. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13143. Add('procedure tcar.doit(va: longint);');
  13144. Add('begin');
  13145. Add(' doit(1);');
  13146. Add(' doit(1,2);');
  13147. Add(' inherited doit(1);');
  13148. Add(' inherited doit(1,2);');
  13149. Add('end;');
  13150. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13151. Add('begin');
  13152. ConvertProgram;
  13153. CheckSource('TestClass_OverloadsAncestor',
  13154. LinesToStr([ // statements
  13155. 'rtl.createClass($mod, "TObject", null, function () {',
  13156. ' this.$init = function () {',
  13157. ' };',
  13158. ' this.$final = function () {',
  13159. ' };',
  13160. ' this.DoIt = function (vA) {',
  13161. ' this.DoIt(1);',
  13162. ' this.DoIt$1(1,2);',
  13163. ' };',
  13164. ' this.DoIt$1 = function (vA, vB) {',
  13165. ' };',
  13166. '});',
  13167. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13168. ' this.DoIt$2 = function (vA) {',
  13169. ' this.DoIt$2(1);',
  13170. ' this.DoIt$3(1, 2);',
  13171. ' $mod.TObject.DoIt.call(this, 1);',
  13172. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13173. ' };',
  13174. ' this.DoIt$3 = function (vA, vB) {',
  13175. ' };',
  13176. '});',
  13177. '']),
  13178. LinesToStr([ // $mod.$main
  13179. '']));
  13180. end;
  13181. procedure TTestModule.TestClass_OverloadConstructor;
  13182. begin
  13183. StartProgram(false);
  13184. Add('type');
  13185. Add(' TObject = class');
  13186. Add(' constructor Create(vA: longint);');
  13187. Add(' constructor Create(vA, vB: longint);');
  13188. Add(' end;');
  13189. Add(' TCar = class');
  13190. Add(' constructor Create(vA: longint);');
  13191. Add(' constructor Create(vA, vB: longint);');
  13192. Add(' end;');
  13193. Add('constructor tobject.create(va: longint);');
  13194. Add('begin');
  13195. Add(' create(1);');
  13196. Add(' create(1,2);');
  13197. Add('end;');
  13198. Add('constructor tobject.create(va, vb: longint); begin end;');
  13199. Add('constructor tcar.create(va: longint);');
  13200. Add('begin');
  13201. Add(' create(1);');
  13202. Add(' create(1,2);');
  13203. Add(' inherited create(1);');
  13204. Add(' inherited create(1,2);');
  13205. Add('end;');
  13206. Add('constructor tcar.create(va, vb: longint); begin end;');
  13207. Add('begin');
  13208. Add(' tobject.create(1);');
  13209. Add(' tobject.create(1,2);');
  13210. Add(' tcar.create(1);');
  13211. Add(' tcar.create(1,2);');
  13212. ConvertProgram;
  13213. CheckSource('TestClass_OverloadConstructor',
  13214. LinesToStr([ // statements
  13215. 'rtl.createClass($mod, "TObject", null, function () {',
  13216. ' this.$init = function () {',
  13217. ' };',
  13218. ' this.$final = function () {',
  13219. ' };',
  13220. ' this.Create = function (vA) {',
  13221. ' this.Create(1);',
  13222. ' this.Create$1(1,2);',
  13223. ' return this;',
  13224. ' };',
  13225. ' this.Create$1 = function (vA, vB) {',
  13226. ' return this;',
  13227. ' };',
  13228. '});',
  13229. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13230. ' this.Create$2 = function (vA) {',
  13231. ' this.Create$2(1);',
  13232. ' this.Create$3(1, 2);',
  13233. ' $mod.TObject.Create.call(this, 1);',
  13234. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13235. ' return this;',
  13236. ' };',
  13237. ' this.Create$3 = function (vA, vB) {',
  13238. ' return this;',
  13239. ' };',
  13240. '});',
  13241. '']),
  13242. LinesToStr([ // $mod.$main
  13243. '$mod.TObject.$create("Create", [1]);',
  13244. '$mod.TObject.$create("Create$1", [1, 2]);',
  13245. '$mod.TCar.$create("Create$2", [1]);',
  13246. '$mod.TCar.$create("Create$3", [1, 2]);',
  13247. '']));
  13248. end;
  13249. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13250. begin
  13251. StartProgram(false);
  13252. Add([
  13253. '{$mode delphi}',
  13254. 'type',
  13255. ' TObject = class end;',
  13256. ' TBird = class',
  13257. ' function {#a}GetValue: longint; overload; virtual;',
  13258. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  13259. ' end;',
  13260. ' TEagle = class(TBird)',
  13261. ' function {#c}GetValue: longint; overload; override;',
  13262. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  13263. ' end;',
  13264. 'function TBird.GetValue: longint;',
  13265. 'begin',
  13266. ' if 3={@a}GetValue then ;',
  13267. ' if 4={@b}GetValue(5) then ;',
  13268. 'end;',
  13269. 'function TBird.GetValue(AValue: longint): longint;',
  13270. 'begin',
  13271. 'end;',
  13272. 'function TEagle.GetValue: longint;',
  13273. 'begin',
  13274. ' if 13={@c}GetValue then ;',
  13275. ' if 14={@d}GetValue(15) then ;',
  13276. ' if 15=inherited {@a}GetValue then ;',
  13277. ' if 16=inherited {@b}GetValue(17) then ;',
  13278. 'end;',
  13279. 'function TEagle.GetValue(AValue: longint): longint;',
  13280. 'begin',
  13281. 'end;',
  13282. 'var',
  13283. ' e: TEagle;',
  13284. 'begin',
  13285. ' if 23=e.{@c}GetValue then ;',
  13286. ' if 24=e.{@d}GetValue(25) then ;']);
  13287. ConvertProgram;
  13288. CheckSource('TestClass_OverloadDelphiOverride',
  13289. LinesToStr([ // statements
  13290. 'rtl.createClass($mod, "TObject", null, function () {',
  13291. ' this.$init = function () {',
  13292. ' };',
  13293. ' this.$final = function () {',
  13294. ' };',
  13295. '});',
  13296. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13297. ' this.GetValue = function () {',
  13298. ' var Result = 0;',
  13299. ' if (3 === this.GetValue()) ;',
  13300. ' if (4 === this.GetValue$1(5)) ;',
  13301. ' return Result;',
  13302. ' };',
  13303. ' this.GetValue$1 = function (AValue) {',
  13304. ' var Result = 0;',
  13305. ' return Result;',
  13306. ' };',
  13307. '});',
  13308. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13309. ' this.GetValue = function () {',
  13310. ' var Result = 0;',
  13311. ' if (13 === this.GetValue()) ;',
  13312. ' if (14 === this.GetValue$1(15)) ;',
  13313. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13314. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13315. ' return Result;',
  13316. ' };',
  13317. ' this.GetValue$1 = function (AValue) {',
  13318. ' var Result = 0;',
  13319. ' return Result;',
  13320. ' };',
  13321. '});',
  13322. 'this.e = null;',
  13323. '']),
  13324. LinesToStr([ // $mod.$main
  13325. 'if (23 === $mod.e.GetValue()) ;',
  13326. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13327. '']));
  13328. end;
  13329. procedure TTestModule.TestClass_ReintroducedVar;
  13330. begin
  13331. StartProgram(false);
  13332. Add('type');
  13333. Add(' TObject = class');
  13334. Add(' strict private');
  13335. Add(' Some: longint;');
  13336. Add(' end;');
  13337. Add(' TMobile = class');
  13338. Add(' strict private');
  13339. Add(' Some: string;');
  13340. Add(' end;');
  13341. Add(' TCar = class(tmobile)');
  13342. Add(' procedure Some;');
  13343. Add(' procedure Some(vA: longint);');
  13344. Add(' end;');
  13345. Add('procedure tcar.some;');
  13346. Add('begin');
  13347. Add(' Some;');
  13348. Add(' Some(1);');
  13349. Add('end;');
  13350. Add('procedure tcar.some(va: longint); begin end;');
  13351. Add('begin');
  13352. ConvertProgram;
  13353. CheckSource('TestClass_ReintroducedVar',
  13354. LinesToStr([ // statements
  13355. 'rtl.createClass($mod, "TObject", null, function () {',
  13356. ' this.$init = function () {',
  13357. ' this.Some = 0;',
  13358. ' };',
  13359. ' this.$final = function () {',
  13360. ' };',
  13361. '});',
  13362. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13363. ' this.$init = function () {',
  13364. ' $mod.TObject.$init.call(this);',
  13365. ' this.Some$1 = "";',
  13366. ' };',
  13367. '});',
  13368. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13369. ' this.Some$2 = function () {',
  13370. ' this.Some$2();',
  13371. ' this.Some$3(1);',
  13372. ' };',
  13373. ' this.Some$3 = function (vA) {',
  13374. ' };',
  13375. '});',
  13376. '']),
  13377. LinesToStr([ // $mod.$main
  13378. '']));
  13379. end;
  13380. procedure TTestModule.TestClass_RaiseDescendant;
  13381. begin
  13382. StartProgram(false);
  13383. Add([
  13384. 'type',
  13385. ' TObject = class',
  13386. ' constructor Create(Msg: string);',
  13387. ' end;',
  13388. ' Exception = class',
  13389. ' end;',
  13390. ' EConvertError = class(Exception)',
  13391. ' end;',
  13392. 'constructor TObject.Create(Msg: string); begin end;',
  13393. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  13394. 'begin',
  13395. ' raise Exception.Create(''Bar1'');',
  13396. ' raise EConvertError.Create(''Bar2'');',
  13397. ' raise AssertConv(''Bar2'');',
  13398. ' raise AssertConv;',
  13399. '']);
  13400. ConvertProgram;
  13401. CheckSource('TestClass_RaiseDescendant',
  13402. LinesToStr([ // statements
  13403. 'rtl.createClass($mod, "TObject", null, function () {',
  13404. ' this.$init = function () {',
  13405. ' };',
  13406. ' this.$final = function () {',
  13407. ' };',
  13408. ' this.Create = function (Msg) {',
  13409. ' return this;',
  13410. ' };',
  13411. '});',
  13412. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  13413. '});',
  13414. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  13415. '});',
  13416. 'this.AssertConv = function (Msg) {',
  13417. ' var Result = null;',
  13418. ' return Result;',
  13419. '};',
  13420. '']),
  13421. LinesToStr([ // $mod.$main
  13422. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  13423. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  13424. 'throw $mod.AssertConv("Bar2");',
  13425. 'throw $mod.AssertConv("def");',
  13426. '']));
  13427. end;
  13428. procedure TTestModule.TestClass_ExternalMethod;
  13429. begin
  13430. AddModuleWithIntfImplSrc('unit2.pas',
  13431. LinesToStr([
  13432. 'type',
  13433. ' TObject = class',
  13434. ' public',
  13435. ' procedure Intern; external name ''$DoIntern'';',
  13436. ' end;',
  13437. '']),
  13438. LinesToStr([
  13439. '']));
  13440. StartUnit(true);
  13441. Add('interface');
  13442. Add('uses unit2;');
  13443. Add('type');
  13444. Add(' TCar = class(TObject)');
  13445. Add(' public');
  13446. Add(' procedure Intern2; external name ''$DoIntern2'';');
  13447. Add(' procedure DoIt;');
  13448. Add(' end;');
  13449. Add('implementation');
  13450. Add('procedure tcar.doit;');
  13451. Add('begin');
  13452. Add(' Intern;');
  13453. Add(' Intern();');
  13454. Add(' Intern2;');
  13455. Add(' Intern2();');
  13456. Add('end;');
  13457. Add('var Obj: TCar;');
  13458. Add('begin');
  13459. Add(' obj.intern;');
  13460. Add(' obj.intern();');
  13461. Add(' obj.intern2;');
  13462. Add(' obj.intern2();');
  13463. Add(' obj.doit;');
  13464. Add(' obj.doit();');
  13465. Add(' with obj do begin');
  13466. Add(' Intern;');
  13467. Add(' Intern();');
  13468. Add(' Intern2;');
  13469. Add(' Intern2();');
  13470. Add(' end;');
  13471. ConvertUnit;
  13472. CheckSource('TestClass_ExternalMethod',
  13473. LinesToStr([
  13474. 'var $impl = $mod.$impl;',
  13475. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13476. ' this.DoIt = function () {',
  13477. ' this.$DoIntern();',
  13478. ' this.$DoIntern();',
  13479. ' this.$DoIntern2();',
  13480. ' this.$DoIntern2();',
  13481. ' };',
  13482. ' });',
  13483. '']),
  13484. LinesToStr([ // this.$init
  13485. '$impl.Obj.$DoIntern();',
  13486. '$impl.Obj.$DoIntern();',
  13487. '$impl.Obj.$DoIntern2();',
  13488. '$impl.Obj.$DoIntern2();',
  13489. '$impl.Obj.DoIt();',
  13490. '$impl.Obj.DoIt();',
  13491. 'var $with1 = $impl.Obj;',
  13492. '$with1.$DoIntern();',
  13493. '$with1.$DoIntern();',
  13494. '$with1.$DoIntern2();',
  13495. '$with1.$DoIntern2();',
  13496. '']),
  13497. LinesToStr([ // implementation
  13498. '$impl.Obj = null;',
  13499. '']) );
  13500. end;
  13501. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  13502. begin
  13503. StartProgram(false);
  13504. Add('type');
  13505. Add(' TObject = class');
  13506. Add(' procedure DoIt; virtual; external name ''Foo'';');
  13507. Add(' end;');
  13508. Add('begin');
  13509. SetExpectedPasResolverError('Virtual method name must match external',
  13510. nVirtualMethodNameMustMatchExternal);
  13511. ConvertProgram;
  13512. end;
  13513. procedure TTestModule.TestClass_ExternalOverrideFail;
  13514. begin
  13515. StartProgram(false);
  13516. Add('type');
  13517. Add(' TObject = class');
  13518. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  13519. Add(' end;');
  13520. Add(' TCar = class');
  13521. Add(' procedure DoIt; override; external name ''DoIt'';');
  13522. Add(' end;');
  13523. Add('begin');
  13524. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  13525. nInvalidXModifierY);
  13526. ConvertProgram;
  13527. end;
  13528. procedure TTestModule.TestClass_ExternalVar;
  13529. begin
  13530. AddModuleWithIntfImplSrc('unit2.pas',
  13531. LinesToStr([
  13532. '{$modeswitch externalclass}',
  13533. 'type',
  13534. ' TObject = class',
  13535. ' public',
  13536. ' Intern: longint external name ''$Intern'';',
  13537. ' Bracket: longint external name ''["A B"]'';',
  13538. ' end;',
  13539. '']),
  13540. LinesToStr([
  13541. '']));
  13542. StartUnit(true);
  13543. Add([
  13544. 'interface',
  13545. 'uses unit2;',
  13546. '{$modeswitch externalclass}',
  13547. 'type',
  13548. ' TCar = class(tobject)',
  13549. ' public',
  13550. ' Intern2: longint external name ''$Intern2'';',
  13551. ' procedure DoIt;',
  13552. ' end;',
  13553. 'implementation',
  13554. 'procedure tcar.doit;',
  13555. 'begin',
  13556. ' Intern:=Intern+1;',
  13557. ' Intern2:=Intern2+2;',
  13558. ' Bracket:=Bracket+3;',
  13559. 'end;',
  13560. 'var Obj: TCar;',
  13561. 'begin',
  13562. ' obj.intern:=obj.intern+1;',
  13563. ' obj.intern2:=obj.intern2+2;',
  13564. ' obj.Bracket:=obj.Bracket+3;',
  13565. ' with obj do begin',
  13566. ' intern:=intern+1;',
  13567. ' intern2:=intern2+2;',
  13568. ' Bracket:=Bracket+3;',
  13569. ' end;']);
  13570. ConvertUnit;
  13571. CheckSource('TestClass_ExternalVar',
  13572. LinesToStr([
  13573. 'var $impl = $mod.$impl;',
  13574. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  13575. ' this.DoIt = function () {',
  13576. ' this.$Intern = this.$Intern + 1;',
  13577. ' this.$Intern2 = this.$Intern2 + 2;',
  13578. ' this["A B"] = this["A B"] + 3;',
  13579. ' };',
  13580. ' });',
  13581. '']),
  13582. LinesToStr([
  13583. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  13584. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  13585. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  13586. 'var $with1 = $impl.Obj;',
  13587. '$with1.$Intern = $with1.$Intern + 1;',
  13588. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  13589. '$with1["A B"] = $with1["A B"] + 3;',
  13590. '']),
  13591. LinesToStr([ // implementation
  13592. '$impl.Obj = null;',
  13593. '']));
  13594. end;
  13595. procedure TTestModule.TestClass_Const;
  13596. begin
  13597. StartProgram(false);
  13598. Add('type');
  13599. Add(' integer = longint;');
  13600. Add(' TClass = class of TObject;');
  13601. Add(' TObject = class');
  13602. Add(' public');
  13603. Add(' const cI: integer = 3;');
  13604. Add(' procedure DoIt;');
  13605. Add(' class procedure DoMore;');
  13606. Add(' end;');
  13607. Add('procedure tobject.doit;');
  13608. Add('begin');
  13609. Add(' if cI=4 then;');
  13610. Add(' if 5=cI then;');
  13611. Add(' if Self.cI=6 then;');
  13612. Add(' if 7=Self.cI then;');
  13613. Add(' with Self do begin');
  13614. Add(' if cI=11 then;');
  13615. Add(' if 12=cI then;');
  13616. Add(' end;');
  13617. Add('end;');
  13618. Add('class procedure tobject.domore;');
  13619. Add('begin');
  13620. Add(' if cI=8 then;');
  13621. Add(' if Self.cI=9 then;');
  13622. Add(' if 10=cI then;');
  13623. Add(' if 11=Self.cI then;');
  13624. Add(' with Self do begin');
  13625. Add(' if cI=13 then;');
  13626. Add(' if 14=cI then;');
  13627. Add(' end;');
  13628. Add('end;');
  13629. Add('var');
  13630. Add(' Obj: TObject;');
  13631. Add(' Cla: TClass;');
  13632. Add('begin');
  13633. Add(' if TObject.cI=21 then ;');
  13634. Add(' if Obj.cI=22 then ;');
  13635. Add(' if Cla.cI=23 then ;');
  13636. Add(' with obj do if ci=24 then;');
  13637. Add(' with TObject do if ci=25 then;');
  13638. Add(' with Cla do if ci=26 then;');
  13639. ConvertProgram;
  13640. CheckSource('TestClass_Const',
  13641. LinesToStr([
  13642. 'rtl.createClass($mod, "TObject", null, function () {',
  13643. ' this.cI = 3;',
  13644. ' this.$init = function () {',
  13645. ' };',
  13646. ' this.$final = function () {',
  13647. ' };',
  13648. ' this.DoIt = function () {',
  13649. ' if (this.cI === 4) ;',
  13650. ' if (5 === this.cI) ;',
  13651. ' if (this.cI === 6) ;',
  13652. ' if (7 === this.cI) ;',
  13653. ' if (this.cI === 11) ;',
  13654. ' if (12 === this.cI) ;',
  13655. ' };',
  13656. ' this.DoMore = function () {',
  13657. ' if (this.cI === 8) ;',
  13658. ' if (this.cI === 9) ;',
  13659. ' if (10 === this.cI) ;',
  13660. ' if (11 === this.cI) ;',
  13661. ' if (this.cI === 13) ;',
  13662. ' if (14 === this.cI) ;',
  13663. ' };',
  13664. '});',
  13665. 'this.Obj = null;',
  13666. 'this.Cla = null;',
  13667. '']),
  13668. LinesToStr([
  13669. 'if ($mod.TObject.cI === 21) ;',
  13670. 'if ($mod.Obj.cI === 22) ;',
  13671. 'if ($mod.Cla.cI === 23) ;',
  13672. 'var $with1 = $mod.Obj;',
  13673. 'if ($with1.cI === 24) ;',
  13674. 'var $with2 = $mod.TObject;',
  13675. 'if ($with2.cI === 25) ;',
  13676. 'var $with3 = $mod.Cla;',
  13677. 'if ($with3.cI === 26) ;',
  13678. '']));
  13679. end;
  13680. procedure TTestModule.TestClass_LocalConstDuplicate;
  13681. begin
  13682. StartProgram(false);
  13683. Add([
  13684. 'type',
  13685. ' TObject = class',
  13686. ' const cI: longint = 3;',
  13687. ' procedure Fly;',
  13688. ' procedure Run;',
  13689. ' end;',
  13690. ' TBird = class',
  13691. ' procedure Go;',
  13692. ' end;',
  13693. 'procedure tobject.fly;',
  13694. 'const cI: word = 4;',
  13695. 'begin',
  13696. ' if cI=Self.cI then ;',
  13697. 'end;',
  13698. 'procedure tobject.run;',
  13699. 'const cI: word = 5;',
  13700. 'begin',
  13701. ' if cI=Self.cI then ;',
  13702. 'end;',
  13703. 'procedure tbird.go;',
  13704. 'const cI: word = 6;',
  13705. 'begin',
  13706. ' if cI=Self.cI then ;',
  13707. 'end;',
  13708. 'begin',
  13709. '']);
  13710. ConvertProgram;
  13711. CheckSource('TestClass_LocalConstDuplicate',
  13712. LinesToStr([
  13713. 'rtl.createClass($mod, "TObject", null, function () {',
  13714. ' this.cI = 3;',
  13715. ' this.$init = function () {',
  13716. ' };',
  13717. ' this.$final = function () {',
  13718. ' };',
  13719. ' var cI$1 = 4;',
  13720. ' this.Fly = function () {',
  13721. ' if (cI$1 === this.cI) ;',
  13722. ' };',
  13723. ' var cI$2 = 5;',
  13724. ' this.Run = function () {',
  13725. ' if (cI$2 === this.cI) ;',
  13726. ' };',
  13727. '});',
  13728. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13729. ' var cI$3 = 6;',
  13730. ' this.Go = function () {',
  13731. ' if (cI$3 === this.cI) ;',
  13732. ' };',
  13733. '});',
  13734. '']),
  13735. LinesToStr([
  13736. '']));
  13737. end;
  13738. procedure TTestModule.TestClass_LocalVarSelfFail;
  13739. begin
  13740. StartProgram(false);
  13741. Add([
  13742. 'type',
  13743. ' TObject = class',
  13744. ' constructor Create;',
  13745. ' end;',
  13746. 'constructor tobject.create;',
  13747. 'var self: longint;',
  13748. 'begin',
  13749. 'end',
  13750. 'begin',
  13751. '']);
  13752. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  13753. ConvertProgram;
  13754. end;
  13755. procedure TTestModule.TestClass_ArgSelfFail;
  13756. begin
  13757. StartProgram(false);
  13758. Add([
  13759. 'type',
  13760. ' TObject = class',
  13761. ' procedure DoIt(Self: longint);',
  13762. ' end;',
  13763. 'procedure tobject.doit(self: longint);',
  13764. 'begin',
  13765. 'end',
  13766. 'begin',
  13767. '']);
  13768. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  13769. ConvertProgram;
  13770. end;
  13771. procedure TTestModule.TestClass_NestedProcSelf;
  13772. begin
  13773. StartProgram(false);
  13774. Add([
  13775. 'type',
  13776. ' TObject = class',
  13777. ' Key: longint;',
  13778. ' class var State: longint;',
  13779. ' procedure DoIt;',
  13780. ' function GetSize: longint; virtual; abstract;',
  13781. ' procedure SetSize(Value: longint); virtual; abstract;',
  13782. ' property Size: longint read GetSize write SetSize;',
  13783. ' end;',
  13784. 'procedure tobject.doit;',
  13785. ' procedure Sub;',
  13786. ' begin',
  13787. ' key:=key+2;',
  13788. ' self.key:=self.key+3;',
  13789. ' state:=state+4;',
  13790. ' self.state:=self.state+5;',
  13791. ' tobject.state:=tobject.state+6;',
  13792. ' size:=size+7;',
  13793. ' self.size:=self.size+8;',
  13794. ' end;',
  13795. 'begin',
  13796. ' sub;',
  13797. ' key:=key+12;',
  13798. ' self.key:=self.key+13;',
  13799. ' state:=state+14;',
  13800. ' self.state:=self.state+15;',
  13801. ' tobject.state:=tobject.state+16;',
  13802. ' size:=size+17;',
  13803. ' self.size:=self.size+18;',
  13804. 'end;',
  13805. 'begin',
  13806. '']);
  13807. ConvertProgram;
  13808. CheckSource('TestClass_NestedProcSelf',
  13809. LinesToStr([ // statements
  13810. 'rtl.createClass($mod, "TObject", null, function () {',
  13811. ' this.State = 0;',
  13812. ' this.$init = function () {',
  13813. ' this.Key = 0;',
  13814. ' };',
  13815. ' this.$final = function () {',
  13816. ' };',
  13817. ' this.DoIt = function () {',
  13818. ' var $Self = this;',
  13819. ' function Sub() {',
  13820. ' $Self.Key = $Self.Key + 2;',
  13821. ' $Self.Key = $Self.Key + 3;',
  13822. ' $mod.TObject.State = $Self.State + 4;',
  13823. ' $mod.TObject.State = $Self.State + 5;',
  13824. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13825. ' $Self.SetSize($Self.GetSize() + 7);',
  13826. ' $Self.SetSize($Self.GetSize() + 8);',
  13827. ' };',
  13828. ' Sub();',
  13829. ' $Self.Key = $Self.Key + 12;',
  13830. ' $Self.Key = $Self.Key + 13;',
  13831. ' $mod.TObject.State = $Self.State + 14;',
  13832. ' $mod.TObject.State = $Self.State + 15;',
  13833. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13834. ' $Self.SetSize($Self.GetSize() + 17);',
  13835. ' $Self.SetSize($Self.GetSize() + 18);',
  13836. ' };',
  13837. '});',
  13838. '']),
  13839. LinesToStr([ // $mod.$main
  13840. '']));
  13841. end;
  13842. procedure TTestModule.TestClass_NestedProcSelf2;
  13843. begin
  13844. StartProgram(false);
  13845. Add([
  13846. 'type',
  13847. ' TObject = class',
  13848. ' Key: longint;',
  13849. ' class var State: longint;',
  13850. ' function GetSize: longint; virtual; abstract;',
  13851. ' procedure SetSize(Value: longint); virtual; abstract;',
  13852. ' property Size: longint read GetSize write SetSize;',
  13853. ' end;',
  13854. ' TBird = class',
  13855. ' procedure DoIt;',
  13856. ' end;',
  13857. 'procedure tbird.doit;',
  13858. ' procedure Sub;',
  13859. ' begin',
  13860. ' key:=key+2;',
  13861. ' self.key:=self.key+3;',
  13862. ' state:=state+4;',
  13863. ' self.state:=self.state+5;',
  13864. ' tobject.state:=tobject.state+6;',
  13865. ' size:=size+7;',
  13866. ' self.size:=self.size+8;',
  13867. ' end;',
  13868. 'begin',
  13869. ' sub;',
  13870. ' key:=key+12;',
  13871. ' self.key:=self.key+13;',
  13872. ' state:=state+14;',
  13873. ' self.state:=self.state+15;',
  13874. ' tobject.state:=tobject.state+16;',
  13875. ' size:=size+17;',
  13876. ' self.size:=self.size+18;',
  13877. 'end;',
  13878. 'begin',
  13879. '']);
  13880. ConvertProgram;
  13881. CheckSource('TestClass_NestedProcSelf2',
  13882. LinesToStr([ // statements
  13883. 'rtl.createClass($mod, "TObject", null, function () {',
  13884. ' this.State = 0;',
  13885. ' this.$init = function () {',
  13886. ' this.Key = 0;',
  13887. ' };',
  13888. ' this.$final = function () {',
  13889. ' };',
  13890. '});',
  13891. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13892. ' this.DoIt = function () {',
  13893. ' var $Self = this;',
  13894. ' function Sub() {',
  13895. ' $Self.Key = $Self.Key + 2;',
  13896. ' $Self.Key = $Self.Key + 3;',
  13897. ' $mod.TObject.State = $Self.State + 4;',
  13898. ' $mod.TObject.State = $Self.State + 5;',
  13899. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13900. ' $Self.SetSize($Self.GetSize() + 7);',
  13901. ' $Self.SetSize($Self.GetSize() + 8);',
  13902. ' };',
  13903. ' Sub();',
  13904. ' $Self.Key = $Self.Key + 12;',
  13905. ' $Self.Key = $Self.Key + 13;',
  13906. ' $mod.TObject.State = $Self.State + 14;',
  13907. ' $mod.TObject.State = $Self.State + 15;',
  13908. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13909. ' $Self.SetSize($Self.GetSize() + 17);',
  13910. ' $Self.SetSize($Self.GetSize() + 18);',
  13911. ' };',
  13912. '});',
  13913. '']),
  13914. LinesToStr([ // $mod.$main
  13915. '']));
  13916. end;
  13917. procedure TTestModule.TestClass_NestedProcClassSelf;
  13918. begin
  13919. StartProgram(false);
  13920. Add([
  13921. 'type',
  13922. ' TObject = class',
  13923. ' class var State: longint;',
  13924. ' class procedure DoIt;',
  13925. ' class function GetSize: longint; virtual; abstract;',
  13926. ' class procedure SetSize(Value: longint); virtual; abstract;',
  13927. ' class property Size: longint read GetSize write SetSize;',
  13928. ' end;',
  13929. 'class procedure tobject.doit;',
  13930. ' procedure Sub;',
  13931. ' begin',
  13932. ' state:=state+2;',
  13933. ' self.state:=self.state+3;',
  13934. ' tobject.state:=tobject.state+4;',
  13935. ' size:=size+5;',
  13936. ' self.size:=self.size+6;',
  13937. ' tobject.size:=tobject.size+7;',
  13938. ' end;',
  13939. 'begin',
  13940. ' sub;',
  13941. ' state:=state+12;',
  13942. ' self.state:=self.state+13;',
  13943. ' tobject.state:=tobject.state+14;',
  13944. ' size:=size+15;',
  13945. ' self.size:=self.size+16;',
  13946. ' tobject.size:=tobject.size+17;',
  13947. 'end;',
  13948. 'begin',
  13949. '']);
  13950. ConvertProgram;
  13951. CheckSource('TestClass_NestedProcClassSelf',
  13952. LinesToStr([ // statements
  13953. 'rtl.createClass($mod, "TObject", null, function () {',
  13954. ' this.State = 0;',
  13955. ' this.$init = function () {',
  13956. ' };',
  13957. ' this.$final = function () {',
  13958. ' };',
  13959. ' this.DoIt = function () {',
  13960. ' var $Self = this;',
  13961. ' function Sub() {',
  13962. ' $mod.TObject.State = $Self.State + 2;',
  13963. ' $mod.TObject.State = $Self.State + 3;',
  13964. ' $mod.TObject.State = $mod.TObject.State + 4;',
  13965. ' $Self.SetSize($Self.GetSize() + 5);',
  13966. ' $Self.SetSize($Self.GetSize() + 6);',
  13967. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  13968. ' };',
  13969. ' Sub();',
  13970. ' $mod.TObject.State = $Self.State + 12;',
  13971. ' $mod.TObject.State = $Self.State + 13;',
  13972. ' $mod.TObject.State = $mod.TObject.State + 14;',
  13973. ' $Self.SetSize($Self.GetSize() + 15);',
  13974. ' $Self.SetSize($Self.GetSize() + 16);',
  13975. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  13976. ' };',
  13977. '});',
  13978. '']),
  13979. LinesToStr([ // $mod.$main
  13980. '']));
  13981. end;
  13982. procedure TTestModule.TestClass_NestedProcCallInherited;
  13983. begin
  13984. StartProgram(false);
  13985. Add([
  13986. 'type',
  13987. ' TObject = class',
  13988. ' function DoIt(k: boolean): longint; virtual;',
  13989. ' end;',
  13990. ' TBird = class',
  13991. ' function DoIt(k: boolean): longint; override;',
  13992. ' end;',
  13993. 'function tobject.doit(k: boolean): longint;',
  13994. 'begin',
  13995. 'end;',
  13996. 'function tbird.doit(k: boolean): longint;',
  13997. ' procedure Sub;',
  13998. ' begin',
  13999. ' inherited DoIt(true);',
  14000. //' if inherited DoIt(false)=4 then ;',
  14001. ' end;',
  14002. 'begin',
  14003. ' Sub;',
  14004. ' inherited;',
  14005. ' inherited DoIt(true);',
  14006. //' if inherited DoIt(false)=14 then ;',
  14007. 'end;',
  14008. 'begin',
  14009. '']);
  14010. ConvertProgram;
  14011. CheckSource('TestClass_NestedProcCallInherited',
  14012. LinesToStr([ // statements
  14013. 'rtl.createClass($mod, "TObject", null, function () {',
  14014. ' this.$init = function () {',
  14015. ' };',
  14016. ' this.$final = function () {',
  14017. ' };',
  14018. ' this.DoIt = function (k) {',
  14019. ' var Result = 0;',
  14020. ' return Result;',
  14021. ' };',
  14022. '});',
  14023. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14024. ' this.DoIt = function (k) {',
  14025. ' var $Self = this;',
  14026. ' var Result = 0;',
  14027. ' function Sub() {',
  14028. ' $mod.TObject.DoIt.call($Self, true);',
  14029. ' };',
  14030. ' Sub();',
  14031. ' $mod.TObject.DoIt.apply($Self, arguments);',
  14032. ' $mod.TObject.DoIt.call($Self, true);',
  14033. ' return Result;',
  14034. ' };',
  14035. '});',
  14036. '']),
  14037. LinesToStr([ // $mod.$main
  14038. '']));
  14039. end;
  14040. procedure TTestModule.TestClass_TObjectFree;
  14041. begin
  14042. StartProgram(false);
  14043. Add([
  14044. 'type',
  14045. ' TObject = class',
  14046. ' Obj: tobject;',
  14047. ' procedure Free;',
  14048. ' procedure Release;',
  14049. ' end;',
  14050. 'procedure tobject.free;',
  14051. 'begin',
  14052. 'end;',
  14053. 'procedure tobject.release;',
  14054. 'begin',
  14055. ' free;',
  14056. ' if true then free;',
  14057. 'end;',
  14058. 'function DoIt(o: tobject): tobject;',
  14059. 'var l: tobject;',
  14060. 'begin',
  14061. ' o.free;',
  14062. ' o.free();',
  14063. ' l.free;',
  14064. ' l.free();',
  14065. ' o.obj.free;',
  14066. ' o.obj.free();',
  14067. ' with o do obj.free;',
  14068. ' with o do obj.free();',
  14069. ' result.Free;',
  14070. ' result.Free();',
  14071. 'end;',
  14072. 'var o: tobject;',
  14073. ' a: array of tobject;',
  14074. 'begin',
  14075. ' o.free;',
  14076. ' o.obj.free;',
  14077. ' a[1+2].free;',
  14078. '']);
  14079. ConvertProgram;
  14080. CheckSource('TestClass_TObjectFree',
  14081. LinesToStr([ // statements
  14082. 'rtl.createClass($mod, "TObject", null, function () {',
  14083. ' this.$init = function () {',
  14084. ' this.Obj = null;',
  14085. ' };',
  14086. ' this.$final = function () {',
  14087. ' this.Obj = undefined;',
  14088. ' };',
  14089. ' this.Free = function () {',
  14090. ' };',
  14091. ' this.Release = function () {',
  14092. ' this.Free();',
  14093. ' if (true) this.Free();',
  14094. ' };',
  14095. '});',
  14096. 'this.DoIt = function (o) {',
  14097. ' var Result = null;',
  14098. ' var l = null;',
  14099. ' o = rtl.freeLoc(o);',
  14100. ' o = rtl.freeLoc(o);',
  14101. ' l = rtl.freeLoc(l);',
  14102. ' l = rtl.freeLoc(l);',
  14103. ' rtl.free(o, "Obj");',
  14104. ' rtl.free(o, "Obj");',
  14105. ' rtl.free(o, "Obj");',
  14106. ' rtl.free(o, "Obj");',
  14107. ' Result = rtl.freeLoc(Result);',
  14108. ' Result = rtl.freeLoc(Result);',
  14109. ' return Result;',
  14110. '};',
  14111. 'this.o = null;',
  14112. 'this.a = [];',
  14113. '']),
  14114. LinesToStr([ // $mod.$main
  14115. 'rtl.free($mod, "o");',
  14116. 'rtl.free($mod.o, "Obj");',
  14117. 'rtl.free($mod.a, 1 + 2);',
  14118. '']));
  14119. end;
  14120. procedure TTestModule.TestClass_TObjectFree_VarArg;
  14121. begin
  14122. StartProgram(false);
  14123. Add([
  14124. 'type',
  14125. ' TObject = class',
  14126. ' Obj: tobject;',
  14127. ' procedure Free;',
  14128. ' end;',
  14129. 'procedure tobject.free;',
  14130. 'begin',
  14131. 'end;',
  14132. 'procedure DoIt(var o: tobject);',
  14133. 'begin',
  14134. ' o.free;',
  14135. ' o.free();',
  14136. 'end;',
  14137. 'begin',
  14138. '']);
  14139. ConvertProgram;
  14140. CheckSource('TestClass_TObjectFree_VarArg',
  14141. LinesToStr([ // statements
  14142. 'rtl.createClass($mod, "TObject", null, function () {',
  14143. ' this.$init = function () {',
  14144. ' this.Obj = null;',
  14145. ' };',
  14146. ' this.$final = function () {',
  14147. ' this.Obj = undefined;',
  14148. ' };',
  14149. ' this.Free = function () {',
  14150. ' };',
  14151. '});',
  14152. 'this.DoIt = function (o) {',
  14153. ' o.set(rtl.freeLoc(o.get()));',
  14154. ' o.set(rtl.freeLoc(o.get()));',
  14155. '};',
  14156. '']),
  14157. LinesToStr([ // $mod.$main
  14158. '']));
  14159. end;
  14160. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  14161. begin
  14162. StartProgram(false);
  14163. Add([
  14164. 'type',
  14165. ' TObject = class',
  14166. ' constructor Create;',
  14167. ' procedure Free;',
  14168. ' end;',
  14169. 'constructor TObject.Create; begin end;',
  14170. 'procedure tobject.free; begin end;',
  14171. 'begin',
  14172. ' with tobject.create do free;',
  14173. '']);
  14174. ConvertProgram;
  14175. CheckSource('TestClass_TObjectFreeNewInstance',
  14176. LinesToStr([ // statements
  14177. 'rtl.createClass($mod, "TObject", null, function () {',
  14178. ' this.$init = function () {',
  14179. ' };',
  14180. ' this.$final = function () {',
  14181. ' };',
  14182. ' this.Create = function () {',
  14183. ' return this;',
  14184. ' };',
  14185. ' this.Free = function () {',
  14186. ' };',
  14187. '});',
  14188. '']),
  14189. LinesToStr([ // $mod.$main
  14190. 'var $with1 = $mod.TObject.$create("Create");',
  14191. '$with1=rtl.freeLoc($with1);',
  14192. '']));
  14193. end;
  14194. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  14195. begin
  14196. StartProgram(false);
  14197. Add([
  14198. 'type',
  14199. ' TObject = class',
  14200. ' destructor Destroy;',
  14201. ' procedure Free;',
  14202. ' end;',
  14203. 'destructor TObject.Destroy; begin end;',
  14204. 'procedure tobject.free; begin end;',
  14205. 'var o: tobject;',
  14206. 'begin',
  14207. ' o.free;',
  14208. '']);
  14209. Converter.UseLowerCase:=true;
  14210. ConvertProgram;
  14211. CheckSource('TestClass_TObjectFreeLowerCase',
  14212. LinesToStr([ // statements
  14213. 'rtl.createClass($mod, "tobject", null, function () {',
  14214. ' this.$init = function () {',
  14215. ' };',
  14216. ' this.$final = function () {',
  14217. ' };',
  14218. ' rtl.tObjectDestroy = "destroy";',
  14219. ' this.destroy = function () {',
  14220. ' };',
  14221. ' this.free = function () {',
  14222. ' };',
  14223. '});',
  14224. 'this.o = null;',
  14225. '']),
  14226. LinesToStr([ // $mod.$main
  14227. 'rtl.free($mod, "o");',
  14228. '']));
  14229. end;
  14230. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  14231. begin
  14232. StartProgram(false);
  14233. Add([
  14234. 'type',
  14235. ' TObject = class',
  14236. ' procedure Free;',
  14237. ' function GetObj: tobject; virtual; abstract;',
  14238. ' end;',
  14239. 'procedure tobject.free;',
  14240. 'begin',
  14241. 'end;',
  14242. 'var o: tobject;',
  14243. 'begin',
  14244. ' o.getobj.free;',
  14245. '']);
  14246. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14247. ConvertProgram;
  14248. end;
  14249. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  14250. begin
  14251. StartProgram(false);
  14252. Add([
  14253. 'type',
  14254. ' TObject = class',
  14255. ' procedure Free;',
  14256. ' FObj: TObject;',
  14257. ' property Obj: tobject read FObj write FObj;',
  14258. ' end;',
  14259. 'procedure tobject.free;',
  14260. 'begin',
  14261. 'end;',
  14262. 'var o: tobject;',
  14263. 'begin',
  14264. ' o.obj.free;',
  14265. '']);
  14266. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14267. ConvertProgram;
  14268. end;
  14269. procedure TTestModule.TestClass_ForIn;
  14270. begin
  14271. StartProgram(false);
  14272. Add([
  14273. 'type',
  14274. ' TObject = class end;',
  14275. ' TItem = TObject;',
  14276. ' TEnumerator = class',
  14277. ' FCurrent: TItem;',
  14278. ' property Current: TItem read FCurrent;',
  14279. ' function MoveNext: boolean;',
  14280. ' end;',
  14281. ' TBird = class',
  14282. ' function GetEnumerator: TEnumerator;',
  14283. ' end;',
  14284. 'function TEnumerator.MoveNext: boolean;',
  14285. 'begin',
  14286. 'end;',
  14287. 'function TBird.GetEnumerator: TEnumerator;',
  14288. 'begin',
  14289. 'end;',
  14290. 'var',
  14291. ' b: TBird;',
  14292. ' i, i2: TItem;',
  14293. 'begin',
  14294. ' for i in b do i2:=i;']);
  14295. ConvertProgram;
  14296. CheckSource('TestClass_ForIn',
  14297. LinesToStr([ // statements
  14298. 'rtl.createClass($mod, "TObject", null, function () {',
  14299. ' this.$init = function () {',
  14300. ' };',
  14301. ' this.$final = function () {',
  14302. ' };',
  14303. '});',
  14304. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  14305. ' this.$init = function () {',
  14306. ' $mod.TObject.$init.call(this);',
  14307. ' this.FCurrent = null;',
  14308. ' };',
  14309. ' this.$final = function () {',
  14310. ' this.FCurrent = undefined;',
  14311. ' $mod.TObject.$final.call(this);',
  14312. ' };',
  14313. ' this.MoveNext = function () {',
  14314. ' var Result = false;',
  14315. ' return Result;',
  14316. ' };',
  14317. '});',
  14318. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14319. ' this.GetEnumerator = function () {',
  14320. ' var Result = null;',
  14321. ' return Result;',
  14322. ' };',
  14323. '});',
  14324. 'this.b = null;',
  14325. 'this.i = null;',
  14326. 'this.i2 = null;'
  14327. ]),
  14328. LinesToStr([ // $mod.$main
  14329. 'var $in1 = $mod.b.GetEnumerator();',
  14330. 'try {',
  14331. ' while ($in1.MoveNext()){',
  14332. ' $mod.i = $in1.FCurrent;',
  14333. ' $mod.i2 = $mod.i;',
  14334. ' }',
  14335. '} finally {',
  14336. ' $in1 = rtl.freeLoc($in1)',
  14337. '};',
  14338. '']));
  14339. end;
  14340. procedure TTestModule.TestClass_DispatchMessage;
  14341. begin
  14342. StartProgram(false);
  14343. Add([
  14344. 'type',
  14345. ' TObject = class',
  14346. ' {$DispatchField DispInt}',
  14347. ' procedure Dispatch(var Msg); virtual; abstract;',
  14348. ' {$DispatchStrField DispStr}',
  14349. ' procedure DispatchStr(var Msg); virtual; abstract;',
  14350. ' end;',
  14351. ' THopMsg = record',
  14352. ' DispInt: longint;',
  14353. ' end;',
  14354. ' TPutMsg = record',
  14355. ' DispStr: string;',
  14356. ' end;',
  14357. ' TBird = class',
  14358. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  14359. ' procedure Run; overload; virtual; abstract;',
  14360. ' procedure Run(var Msg); overload; message ''Fast'';',
  14361. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  14362. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  14363. ' end;',
  14364. 'procedure TBird.Run(var Msg);',
  14365. 'begin',
  14366. 'end;',
  14367. 'begin',
  14368. '']);
  14369. ConvertProgram;
  14370. CheckSource('TestClass_Message',
  14371. LinesToStr([ // statements
  14372. 'rtl.createClass($mod, "TObject", null, function () {',
  14373. ' this.$init = function () {',
  14374. ' };',
  14375. ' this.$final = function () {',
  14376. ' };',
  14377. '});',
  14378. 'rtl.recNewT($mod, "THopMsg", function () {',
  14379. ' this.DispInt = 0;',
  14380. ' this.$eq = function (b) {',
  14381. ' return this.DispInt === b.DispInt;',
  14382. ' };',
  14383. ' this.$assign = function (s) {',
  14384. ' this.DispInt = s.DispInt;',
  14385. ' return this;',
  14386. ' };',
  14387. '});',
  14388. 'rtl.recNewT($mod, "TPutMsg", function () {',
  14389. ' this.DispStr = "";',
  14390. ' this.$eq = function (b) {',
  14391. ' return this.DispStr === b.DispStr;',
  14392. ' };',
  14393. ' this.$assign = function (s) {',
  14394. ' this.DispStr = s.DispStr;',
  14395. ' return this;',
  14396. ' };',
  14397. '});',
  14398. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14399. ' this.Run$1 = function (Msg) {',
  14400. ' };',
  14401. ' this.$msgint = {',
  14402. ' "2": "Fly",',
  14403. ' "3": "Hop"',
  14404. ' };',
  14405. ' this.$msgstr = {',
  14406. ' Fast: "Run$1",',
  14407. ' foo: "Put"',
  14408. ' };',
  14409. '});',
  14410. '']),
  14411. LinesToStr([ // $mod.$main
  14412. '']));
  14413. end;
  14414. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  14415. begin
  14416. StartProgram(false);
  14417. Add([
  14418. 'type',
  14419. ' TObject = class',
  14420. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  14421. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  14422. ' end;',
  14423. 'begin',
  14424. '']);
  14425. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  14426. ConvertProgram;
  14427. end;
  14428. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  14429. begin
  14430. StartProgram(false);
  14431. Add([
  14432. 'type',
  14433. ' TObject = class',
  14434. ' {$dispatchfield Msg}',
  14435. ' procedure Dispatch(var Msg); virtual; abstract;',
  14436. ' end;',
  14437. ' TFlyMsg = record',
  14438. ' FlyId: longint;',
  14439. ' end;',
  14440. ' TBird = class',
  14441. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  14442. ' end;',
  14443. 'begin',
  14444. '']);
  14445. ConvertProgram;
  14446. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  14447. end;
  14448. procedure TTestModule.TestClassOf_Create;
  14449. begin
  14450. StartProgram(false);
  14451. Add('type');
  14452. Add(' TObject = class');
  14453. Add(' constructor Create;');
  14454. Add(' end;');
  14455. Add(' TClass = class of TObject;');
  14456. Add('constructor tobject.create; begin end;');
  14457. Add('var');
  14458. Add(' Obj: tobject;');
  14459. Add(' C: tclass;');
  14460. Add('begin');
  14461. Add(' obj:=C.create;');
  14462. Add(' with c do obj:=create;');
  14463. ConvertProgram;
  14464. CheckSource('TestClassOf_Create',
  14465. LinesToStr([ // statements
  14466. 'rtl.createClass($mod, "TObject", null, function () {',
  14467. ' this.$init = function () {',
  14468. ' };',
  14469. ' this.$final = function () {',
  14470. ' };',
  14471. ' this.Create = function () {',
  14472. ' return this;',
  14473. ' };',
  14474. '});',
  14475. 'this.Obj = null;',
  14476. 'this.C = null;'
  14477. ]),
  14478. LinesToStr([ // $mod.$main
  14479. '$mod.Obj = $mod.C.$create("Create");',
  14480. 'var $with1 = $mod.C;',
  14481. '$mod.Obj = $with1.$create("Create");',
  14482. '']));
  14483. end;
  14484. procedure TTestModule.TestClassOf_Call;
  14485. begin
  14486. StartProgram(false);
  14487. Add('type');
  14488. Add(' TObject = class');
  14489. Add(' class procedure DoIt;');
  14490. Add(' end;');
  14491. Add(' TClass = class of TObject;');
  14492. Add('class procedure tobject.doit; begin end;');
  14493. Add('var');
  14494. Add(' C: tclass;');
  14495. Add('begin');
  14496. Add(' c.doit;');
  14497. Add(' with c do doit;');
  14498. ConvertProgram;
  14499. CheckSource('TestClassOf_Call',
  14500. LinesToStr([ // statements
  14501. 'rtl.createClass($mod, "TObject", null, function () {',
  14502. ' this.$init = function () {',
  14503. ' };',
  14504. ' this.$final = function () {',
  14505. ' };',
  14506. ' this.DoIt = function () {',
  14507. ' };',
  14508. '});',
  14509. 'this.C = null;'
  14510. ]),
  14511. LinesToStr([ // $mod.$main
  14512. '$mod.C.DoIt();',
  14513. 'var $with1 = $mod.C;',
  14514. '$with1.DoIt();',
  14515. '']));
  14516. end;
  14517. procedure TTestModule.TestClassOf_Assign;
  14518. begin
  14519. StartProgram(false);
  14520. Add('type');
  14521. Add(' TClass = class of TObject;');
  14522. Add(' TObject = class');
  14523. Add(' ClassType: TClass; ');
  14524. Add(' end;');
  14525. Add('var');
  14526. Add(' Obj: tobject;');
  14527. Add(' C: tclass;');
  14528. Add('begin');
  14529. Add(' c:=nil;');
  14530. Add(' c:=obj.classtype;');
  14531. ConvertProgram;
  14532. CheckSource('TestClassOf_Assign',
  14533. LinesToStr([ // statements
  14534. 'rtl.createClass($mod, "TObject", null, function () {',
  14535. ' this.$init = function () {',
  14536. ' this.ClassType = null;',
  14537. ' };',
  14538. ' this.$final = function () {',
  14539. ' this.ClassType = undefined;',
  14540. ' };',
  14541. '});',
  14542. 'this.Obj = null;',
  14543. 'this.C = null;'
  14544. ]),
  14545. LinesToStr([ // $mod.$main
  14546. '$mod.C = null;',
  14547. '$mod.C = $mod.Obj.ClassType;',
  14548. '']));
  14549. end;
  14550. procedure TTestModule.TestClassOf_Is;
  14551. begin
  14552. StartProgram(false);
  14553. Add('type');
  14554. Add(' TClass = class of TObject;');
  14555. Add(' TObject = class');
  14556. Add(' end;');
  14557. Add(' TCar = class');
  14558. Add(' end;');
  14559. Add(' TCars = class of TCar;');
  14560. Add('var');
  14561. Add(' Obj: tobject;');
  14562. Add(' C: tclass;');
  14563. Add(' Cars: tcars;');
  14564. Add('begin');
  14565. Add(' if c is tcar then ;');
  14566. Add(' if c is tcars then ;');
  14567. ConvertProgram;
  14568. CheckSource('TestClassOf_Is',
  14569. LinesToStr([ // statements
  14570. 'rtl.createClass($mod, "TObject", null, function () {',
  14571. ' this.$init = function () {',
  14572. ' };',
  14573. ' this.$final = function () {',
  14574. ' };',
  14575. '});',
  14576. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  14577. '});',
  14578. 'this.Obj = null;',
  14579. 'this.C = null;',
  14580. 'this.Cars = null;'
  14581. ]),
  14582. LinesToStr([ // $mod.$main
  14583. 'if(rtl.is($mod.C,$mod.TCar));',
  14584. 'if(rtl.is($mod.C,$mod.TCar));',
  14585. '']));
  14586. end;
  14587. procedure TTestModule.TestClassOf_Compare;
  14588. begin
  14589. StartProgram(false);
  14590. Add('type');
  14591. Add(' TClass = class of TObject;');
  14592. Add(' TObject = class');
  14593. Add(' ClassType: TClass; ');
  14594. Add(' end;');
  14595. Add('var');
  14596. Add(' b: boolean;');
  14597. Add(' Obj: tobject;');
  14598. Add(' C: tclass;');
  14599. Add('begin');
  14600. Add(' b:=c=nil;');
  14601. Add(' b:=nil=c;');
  14602. Add(' b:=c=obj.classtype;');
  14603. Add(' b:=obj.classtype=c;');
  14604. Add(' b:=c=TObject;');
  14605. Add(' b:=TObject=c;');
  14606. Add(' b:=c<>nil;');
  14607. Add(' b:=nil<>c;');
  14608. Add(' b:=c<>obj.classtype;');
  14609. Add(' b:=obj.classtype<>c;');
  14610. Add(' b:=c<>TObject;');
  14611. Add(' b:=TObject<>c;');
  14612. ConvertProgram;
  14613. CheckSource('TestClassOf_Compare',
  14614. LinesToStr([ // statements
  14615. 'rtl.createClass($mod, "TObject", null, function () {',
  14616. ' this.$init = function () {',
  14617. ' this.ClassType = null;',
  14618. ' };',
  14619. ' this.$final = function () {',
  14620. ' this.ClassType = undefined;',
  14621. ' };',
  14622. '});',
  14623. 'this.b = false;',
  14624. 'this.Obj = null;',
  14625. 'this.C = null;'
  14626. ]),
  14627. LinesToStr([ // $mod.$main
  14628. '$mod.b = $mod.C === null;',
  14629. '$mod.b = null === $mod.C;',
  14630. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  14631. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  14632. '$mod.b = $mod.C === $mod.TObject;',
  14633. '$mod.b = $mod.TObject === $mod.C;',
  14634. '$mod.b = $mod.C !== null;',
  14635. '$mod.b = null !== $mod.C;',
  14636. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  14637. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  14638. '$mod.b = $mod.C !== $mod.TObject;',
  14639. '$mod.b = $mod.TObject !== $mod.C;',
  14640. '']));
  14641. end;
  14642. procedure TTestModule.TestClassOf_ClassVar;
  14643. begin
  14644. StartProgram(false);
  14645. Add('type');
  14646. Add(' TObject = class');
  14647. Add(' class var id: longint;');
  14648. Add(' end;');
  14649. Add(' TClass = class of TObject;');
  14650. Add('var');
  14651. Add(' C: tclass;');
  14652. Add('begin');
  14653. Add(' C.id:=C.id;');
  14654. ConvertProgram;
  14655. CheckSource('TestClassOf_ClassVar',
  14656. LinesToStr([ // statements
  14657. 'rtl.createClass($mod, "TObject", null, function () {',
  14658. ' this.id = 0;',
  14659. ' this.$init = function () {',
  14660. ' };',
  14661. ' this.$final = function () {',
  14662. ' };',
  14663. '});',
  14664. 'this.C = null;'
  14665. ]),
  14666. LinesToStr([ // $mod.$main
  14667. '$mod.TObject.id = $mod.C.id;',
  14668. '']));
  14669. end;
  14670. procedure TTestModule.TestClassOf_ClassMethod;
  14671. begin
  14672. StartProgram(false);
  14673. Add('type');
  14674. Add(' TObject = class');
  14675. Add(' class function DoIt(i: longint = 0): longint;');
  14676. Add(' end;');
  14677. Add(' TClass = class of TObject;');
  14678. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  14679. Add('var');
  14680. Add(' i: longint;');
  14681. Add(' C: tclass;');
  14682. Add('begin');
  14683. Add(' C.DoIt;');
  14684. Add(' C.DoIt();');
  14685. Add(' i:=C.DoIt;');
  14686. Add(' i:=C.DoIt();');
  14687. ConvertProgram;
  14688. CheckSource('TestClassOf_ClassMethod',
  14689. LinesToStr([ // statements
  14690. 'rtl.createClass($mod, "TObject", null, function () {',
  14691. ' this.$init = function () {',
  14692. ' };',
  14693. ' this.$final = function () {',
  14694. ' };',
  14695. ' this.DoIt = function (i) {',
  14696. ' var Result = 0;',
  14697. ' return Result;',
  14698. ' };',
  14699. '});',
  14700. 'this.i = 0;',
  14701. 'this.C = null;'
  14702. ]),
  14703. LinesToStr([ // $mod.$main
  14704. '$mod.C.DoIt(0);',
  14705. '$mod.C.DoIt(0);',
  14706. '$mod.i = $mod.C.DoIt(0);',
  14707. '$mod.i = $mod.C.DoIt(0);',
  14708. '']));
  14709. end;
  14710. procedure TTestModule.TestClassOf_ClassProperty;
  14711. begin
  14712. StartProgram(false);
  14713. Add([
  14714. 'type',
  14715. ' TObject = class',
  14716. ' class var FA: longint;',
  14717. ' class function GetA: longint;',
  14718. ' class procedure SetA(Value: longint);',
  14719. ' class property pA: longint read fa write fa;',
  14720. ' class property pB: longint read geta write seta;',
  14721. ' end;',
  14722. ' TObjectClass = class of tobject;',
  14723. 'class function tobject.geta: longint; begin end;',
  14724. 'class procedure tobject.seta(value: longint); begin end;',
  14725. 'var',
  14726. ' b: boolean;',
  14727. ' Obj: tobject;',
  14728. ' Cla: tobjectclass;',
  14729. 'begin',
  14730. ' obj.pa:=obj.pa;',
  14731. ' obj.pb:=obj.pb;',
  14732. ' b:=obj.pa=4;',
  14733. ' b:=obj.pb=obj.pb;',
  14734. ' b:=5=obj.pa;',
  14735. ' cla.pa:=6;',
  14736. ' cla.pa:=cla.pa;',
  14737. ' cla.pb:=cla.pb;',
  14738. ' b:=cla.pa=7;',
  14739. ' b:=cla.pb=cla.pb;',
  14740. ' b:=8=cla.pa;',
  14741. ' tobject.pa:=9;',
  14742. ' tobject.pb:=tobject.pb;',
  14743. ' b:=tobject.pa=10;',
  14744. ' b:=11=tobject.pa;',
  14745. '']);
  14746. ConvertProgram;
  14747. CheckSource('TestClassOf_ClassProperty',
  14748. LinesToStr([ // statements
  14749. 'rtl.createClass($mod, "TObject", null, function () {',
  14750. ' this.FA = 0;',
  14751. ' this.$init = function () {',
  14752. ' };',
  14753. ' this.$final = function () {',
  14754. ' };',
  14755. ' this.GetA = function () {',
  14756. ' var Result = 0;',
  14757. ' return Result;',
  14758. ' };',
  14759. ' this.SetA = function (Value) {',
  14760. ' };',
  14761. '});',
  14762. 'this.b = false;',
  14763. 'this.Obj = null;',
  14764. 'this.Cla = null;'
  14765. ]),
  14766. LinesToStr([ // $mod.$main
  14767. '$mod.TObject.FA = $mod.Obj.FA;',
  14768. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  14769. '$mod.b = $mod.Obj.FA === 4;',
  14770. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  14771. '$mod.b = 5 === $mod.Obj.FA;',
  14772. '$mod.TObject.FA = 6;',
  14773. '$mod.TObject.FA = $mod.Cla.FA;',
  14774. '$mod.Cla.SetA($mod.Cla.GetA());',
  14775. '$mod.b = $mod.Cla.FA === 7;',
  14776. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  14777. '$mod.b = 8 === $mod.Cla.FA;',
  14778. '$mod.TObject.FA = 9;',
  14779. '$mod.TObject.SetA($mod.TObject.GetA());',
  14780. '$mod.b = $mod.TObject.FA === 10;',
  14781. '$mod.b = 11 === $mod.TObject.FA;',
  14782. '']));
  14783. end;
  14784. procedure TTestModule.TestClassOf_ClassMethodSelf;
  14785. begin
  14786. StartProgram(false);
  14787. Add('type');
  14788. Add(' TObject = class');
  14789. Add(' class var GlobalId: longint;');
  14790. Add(' class procedure ProcA;');
  14791. Add(' end;');
  14792. Add('class procedure tobject.proca;');
  14793. Add('var b: boolean;');
  14794. Add('begin');
  14795. Add(' b:=self=nil;');
  14796. Add(' b:=self.globalid=3;');
  14797. Add(' b:=4=self.globalid;');
  14798. Add(' self.globalid:=5;');
  14799. Add(' self.proca;');
  14800. Add('end;');
  14801. Add('begin');
  14802. ConvertProgram;
  14803. CheckSource('TestClassOf_ClassMethodSelf',
  14804. LinesToStr([ // statements
  14805. 'rtl.createClass($mod, "TObject", null, function () {',
  14806. ' this.GlobalId = 0;',
  14807. ' this.$init = function () {',
  14808. ' };',
  14809. ' this.$final = function () {',
  14810. ' };',
  14811. ' this.ProcA = function () {',
  14812. ' var b = false;',
  14813. ' b = this === null;',
  14814. ' b = this.GlobalId === 3;',
  14815. ' b = 4 === this.GlobalId;',
  14816. ' $mod.TObject.GlobalId = 5;',
  14817. ' this.ProcA();',
  14818. ' };',
  14819. '});'
  14820. ]),
  14821. LinesToStr([ // $mod.$main
  14822. '']));
  14823. end;
  14824. procedure TTestModule.TestClassOf_TypeCast;
  14825. begin
  14826. StartProgram(false);
  14827. Add('type');
  14828. Add(' TObject = class');
  14829. Add(' class procedure {#TObject_DoIt}DoIt;');
  14830. Add(' end;');
  14831. Add(' TClass = class of TObject;');
  14832. Add(' TMobile = class');
  14833. Add(' class procedure {#TMobile_DoIt}DoIt;');
  14834. Add(' end;');
  14835. Add(' TMobileClass = class of TMobile;');
  14836. Add(' TCar = class(TMobile)');
  14837. Add(' class procedure {#TCar_DoIt}DoIt;');
  14838. Add(' end;');
  14839. Add(' TCarClass = class of TCar;');
  14840. Add('class procedure TObject.DoIt;');
  14841. Add('begin');
  14842. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14843. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14844. Add('end;');
  14845. Add('class procedure TMobile.DoIt;');
  14846. Add('begin');
  14847. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  14848. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  14849. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  14850. Add('end;');
  14851. Add('class procedure TCar.DoIt; begin end;');
  14852. Add('var');
  14853. Add(' ObjC: TClass;');
  14854. Add(' MobileC: TMobileClass;');
  14855. Add(' CarC: TCarClass;');
  14856. Add('begin');
  14857. Add(' ObjC.{@TObject_DoIt}DoIt;');
  14858. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  14859. Add(' CarC.{@TCar_DoIt}DoIt;');
  14860. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  14861. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  14862. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  14863. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  14864. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  14865. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  14866. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  14867. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  14868. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  14869. ConvertProgram;
  14870. CheckSource('TestClassOf_TypeCast',
  14871. LinesToStr([ // statements
  14872. 'rtl.createClass($mod, "TObject", null, function () {',
  14873. ' this.$init = function () {',
  14874. ' };',
  14875. ' this.$final = function () {',
  14876. ' };',
  14877. ' this.DoIt = function () {',
  14878. ' this.DoIt();',
  14879. ' this.DoIt$1();',
  14880. ' };',
  14881. '});',
  14882. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  14883. ' this.DoIt$1 = function () {',
  14884. ' this.DoIt();',
  14885. ' this.DoIt$1();',
  14886. ' this.DoIt$2();',
  14887. ' };',
  14888. '});',
  14889. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  14890. ' this.DoIt$2 = function () {',
  14891. ' };',
  14892. '});',
  14893. 'this.ObjC = null;',
  14894. 'this.MobileC = null;',
  14895. 'this.CarC = null;',
  14896. '']),
  14897. LinesToStr([ // $mod.$main
  14898. '$mod.ObjC.DoIt();',
  14899. '$mod.MobileC.DoIt$1();',
  14900. '$mod.CarC.DoIt$2();',
  14901. '$mod.ObjC.DoIt();',
  14902. '$mod.ObjC.DoIt$1();',
  14903. '$mod.ObjC.DoIt$2();',
  14904. '$mod.MobileC.DoIt();',
  14905. '$mod.MobileC.DoIt$1();',
  14906. '$mod.MobileC.DoIt$2();',
  14907. '$mod.CarC.DoIt();',
  14908. '$mod.CarC.DoIt$1();',
  14909. '$mod.CarC.DoIt$2();',
  14910. '']));
  14911. end;
  14912. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  14913. begin
  14914. StartProgram(false);
  14915. Add('type');
  14916. Add(' TObject = class');
  14917. Add(' function CurNow: longint; ');
  14918. Add(' class function Now: longint; ');
  14919. Add(' end;');
  14920. Add('function TObject.CurNow: longint; begin end;');
  14921. Add('class function TObject.Now: longint; begin end;');
  14922. Add('var');
  14923. Add(' Obj: tobject;');
  14924. Add(' vI: longint;');
  14925. Add('begin');
  14926. Add(' obj.curnow;');
  14927. Add(' vi:=obj.curnow;');
  14928. Add(' tobject.now;');
  14929. Add(' vi:=tobject.now;');
  14930. ConvertProgram;
  14931. CheckSource('TestClassOf_ImplicitFunctionCall',
  14932. LinesToStr([ // statements
  14933. 'rtl.createClass($mod, "TObject", null, function () {',
  14934. ' this.$init = function () {',
  14935. ' };',
  14936. ' this.$final = function () {',
  14937. ' };',
  14938. ' this.CurNow = function () {',
  14939. ' var Result = 0;',
  14940. ' return Result;',
  14941. ' };',
  14942. ' this.Now = function () {',
  14943. ' var Result = 0;',
  14944. ' return Result;',
  14945. ' };',
  14946. '});',
  14947. 'this.Obj = null;',
  14948. 'this.vI = 0;',
  14949. '']),
  14950. LinesToStr([ // $mod.$main
  14951. '$mod.Obj.CurNow();',
  14952. '$mod.vI = $mod.Obj.CurNow();',
  14953. '$mod.TObject.Now();',
  14954. '$mod.vI = $mod.TObject.Now();',
  14955. '']));
  14956. end;
  14957. procedure TTestModule.TestClassOf_Const;
  14958. begin
  14959. StartProgram(false);
  14960. Add([
  14961. 'type',
  14962. ' TObject = class',
  14963. ' end;',
  14964. ' TBird = TObject;',
  14965. ' TBirds = class of TBird;',
  14966. ' TEagles = TBirds;',
  14967. ' THawk = class(TBird);',
  14968. 'const',
  14969. ' Hawk: TEagles = THawk;',
  14970. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  14971. ' TBird,',
  14972. ' THawk',
  14973. ' );',
  14974. 'begin']);
  14975. ConvertProgram;
  14976. CheckSource('TestClassOf_Const',
  14977. LinesToStr([ // statements
  14978. 'rtl.createClass($mod, "TObject", null, function () {',
  14979. ' this.$init = function () {',
  14980. ' };',
  14981. ' this.$final = function () {',
  14982. ' };',
  14983. '});',
  14984. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  14985. '});',
  14986. 'this.Hawk = $mod.THawk;',
  14987. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  14988. '']),
  14989. LinesToStr([ // $mod.$main
  14990. '']));
  14991. end;
  14992. procedure TTestModule.TestNestedClass_Alias;
  14993. begin
  14994. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14995. StartProgram(false);
  14996. Add([
  14997. 'type',
  14998. ' TObject = class',
  14999. ' type TNested = type longint;',
  15000. ' end;',
  15001. 'type TAlias = type tobject.tnested;',
  15002. 'var i: tobject.tnested = 3;',
  15003. 'var j: TAlias = 4;',
  15004. 'begin',
  15005. ' if typeinfo(TAlias)=nil then ;',
  15006. ' if typeinfo(tobject.tnested)=nil then ;',
  15007. '']);
  15008. ConvertProgram;
  15009. CheckSource('TestNestedClass_Alias',
  15010. LinesToStr([ // statements
  15011. 'rtl.createClass($mod, "TObject", null, function () {',
  15012. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  15013. ' this.$init = function () {',
  15014. ' };',
  15015. ' this.$final = function () {',
  15016. ' };',
  15017. '});',
  15018. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  15019. 'this.i = 3;',
  15020. 'this.j = 4;',
  15021. '']),
  15022. LinesToStr([ // $mod.$main
  15023. 'if ($mod.$rtti["TAlias"] === null) ;',
  15024. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  15025. '']));
  15026. end;
  15027. procedure TTestModule.TestNestedClass_Record;
  15028. begin
  15029. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15030. StartProgram(false);
  15031. Add([
  15032. 'type',
  15033. ' TObject = class',
  15034. ' type TPoint = record',
  15035. ' x,y: byte;',
  15036. ' end;',
  15037. ' procedure DoIt(t: TPoint);',
  15038. ' end;',
  15039. 'procedure tobject.DoIt(t: TPoint);',
  15040. 'var p: TPoint;',
  15041. 'begin',
  15042. ' t.x:=t.y;',
  15043. ' p:=t;',
  15044. 'end;',
  15045. 'var',
  15046. ' p: tobject.tpoint = (x:2; y:4);',
  15047. ' o: TObject;',
  15048. 'begin',
  15049. ' p:=p;',
  15050. ' o.doit(p);',
  15051. '']);
  15052. ConvertProgram;
  15053. CheckSource('TestNestedClass_Record',
  15054. LinesToStr([ // statements
  15055. 'rtl.createClass($mod, "TObject", null, function () {',
  15056. ' rtl.recNewT(this, "TPoint", function () {',
  15057. ' this.x = 0;',
  15058. ' this.y = 0;',
  15059. ' this.$eq = function (b) {',
  15060. ' return (this.x === b.x) && (this.y === b.y);',
  15061. ' };',
  15062. ' this.$assign = function (s) {',
  15063. ' this.x = s.x;',
  15064. ' this.y = s.y;',
  15065. ' return this;',
  15066. ' };',
  15067. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  15068. ' $r.addField("x", rtl.byte);',
  15069. ' $r.addField("y", rtl.byte);',
  15070. ' });',
  15071. ' this.$init = function () {',
  15072. ' };',
  15073. ' this.$final = function () {',
  15074. ' };',
  15075. ' this.DoIt = function (t) {',
  15076. ' var p = this.TPoint.$new();',
  15077. ' t.x = t.y;',
  15078. ' p.$assign(t);',
  15079. ' };',
  15080. '});',
  15081. 'this.p = $mod.TObject.TPoint.$clone({',
  15082. ' x: 2,',
  15083. ' y: 4',
  15084. '});',
  15085. 'this.o = null;',
  15086. '']),
  15087. LinesToStr([ // $mod.$main
  15088. '$mod.p.$assign($mod.p);',
  15089. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  15090. '']));
  15091. end;
  15092. procedure TTestModule.TestNestedClass_Class;
  15093. begin
  15094. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15095. StartProgram(false);
  15096. Add([
  15097. 'type',
  15098. ' TObject = class end;',
  15099. ' TBird = class',
  15100. ' type TLeg = class',
  15101. ' FId: longint;',
  15102. ' constructor Create;',
  15103. ' function Create(i: longint): TLeg;',
  15104. ' end;',
  15105. ' function DoIt(b: TBird): Tleg;',
  15106. ' end;',
  15107. 'constructor tbird.tleg.create;',
  15108. 'begin',
  15109. ' FId:=3;',
  15110. 'end;',
  15111. 'function tbird.tleg.Create(i: longint): TLeg;',
  15112. 'begin',
  15113. ' Create;',
  15114. ' Result:=TLeg.Create;',
  15115. ' Result:=TBird.TLeg.Create;',
  15116. ' Result:=Create(3);',
  15117. ' FId:=i;',
  15118. 'end;',
  15119. 'function tbird.DoIt(b: tbird): tleg;',
  15120. 'begin',
  15121. ' Result.Create;',
  15122. ' Result:=TLeg.Create;',
  15123. ' Result:=TBird.TLeg.Create;',
  15124. ' Result:=Result.Create(3);',
  15125. 'end;',
  15126. 'var',
  15127. ' b: Tbird.tleg;',
  15128. 'begin',
  15129. ' b.Create;',
  15130. ' b:=TBird.TLeg.Create;',
  15131. ' b:=b.Create(3);',
  15132. '']);
  15133. ConvertProgram;
  15134. CheckSource('TestNestedClass_Class',
  15135. LinesToStr([ // statements
  15136. 'rtl.createClass($mod, "TObject", null, function () {',
  15137. ' this.$init = function () {',
  15138. ' };',
  15139. ' this.$final = function () {',
  15140. ' };',
  15141. '});',
  15142. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15143. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  15144. ' this.$init = function () {',
  15145. ' $mod.TObject.$init.call(this);',
  15146. ' this.FId = 0;',
  15147. ' };',
  15148. ' this.Create = function () {',
  15149. ' this.FId = 3;',
  15150. ' return this;',
  15151. ' };',
  15152. ' this.Create$1 = function (i) {',
  15153. ' var Result = null;',
  15154. ' this.Create();',
  15155. ' Result = $mod.TBird.TLeg.$create("Create");',
  15156. ' Result = $mod.TBird.TLeg.$create("Create");',
  15157. ' Result = this.Create$1(3);',
  15158. ' this.FId = i;',
  15159. ' return Result;',
  15160. ' };',
  15161. ' });',
  15162. ' this.DoIt = function (b) {',
  15163. ' var Result = null;',
  15164. ' Result.Create();',
  15165. ' Result = this.TLeg.$create("Create");',
  15166. ' Result = $mod.TBird.TLeg.$create("Create");',
  15167. ' Result = Result.Create$1(3);',
  15168. ' return Result;',
  15169. ' };',
  15170. '});',
  15171. 'this.b = null;',
  15172. '']),
  15173. LinesToStr([ // $mod.$main
  15174. '$mod.b.Create();',
  15175. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  15176. '$mod.b = $mod.b.Create$1(3);',
  15177. '']));
  15178. end;
  15179. procedure TTestModule.TestExternalClass_Var;
  15180. begin
  15181. StartProgram(false);
  15182. Add([
  15183. '{$modeswitch externalclass}',
  15184. 'type',
  15185. ' TExtA = class external name ''ExtObj''',
  15186. ' Id: longint external name ''$Id'';',
  15187. ' B: longint;',
  15188. ' end;',
  15189. 'var Obj: TExtA;',
  15190. 'begin',
  15191. ' obj.id:=obj.id+1;',
  15192. ' obj.B:=obj.B+1;']);
  15193. ConvertProgram;
  15194. CheckSource('TestExternalClass_Var',
  15195. LinesToStr([ // statements
  15196. 'this.Obj = null;',
  15197. '']),
  15198. LinesToStr([ // $mod.$main
  15199. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  15200. '$mod.Obj.B = $mod.Obj.B + 1;',
  15201. '']));
  15202. end;
  15203. procedure TTestModule.TestExternalClass_Const;
  15204. begin
  15205. StartProgram(false);
  15206. Add([
  15207. '{$modeswitch externalclass}',
  15208. 'type',
  15209. ' TExtA = class external name ''ExtObj''',
  15210. ' const Two: longint = 2;',
  15211. ' const Three = 3;',
  15212. ' const Id: longint;',
  15213. ' end;',
  15214. ' TExtB = class external name ''ExtB''',
  15215. ' A: TExtA;',
  15216. ' end;',
  15217. 'var',
  15218. ' A: texta;',
  15219. ' B: textb;',
  15220. ' i: longint;',
  15221. 'begin',
  15222. ' i:=a.two;',
  15223. ' i:=texta.two;',
  15224. ' i:=a.three;',
  15225. ' i:=texta.three;',
  15226. ' i:=a.id;',
  15227. ' i:=texta.id;',
  15228. '']);
  15229. ConvertProgram;
  15230. CheckSource('TestExternalClass_Const',
  15231. LinesToStr([ // statements
  15232. 'this.A = null;',
  15233. 'this.B = null;',
  15234. 'this.i = 0;',
  15235. '']),
  15236. LinesToStr([ // $mod.$main
  15237. '$mod.i = 2;',
  15238. '$mod.i = 2;',
  15239. '$mod.i = 3;',
  15240. '$mod.i = 3;',
  15241. '$mod.i = $mod.A.Id;',
  15242. '$mod.i = ExtObj.Id;',
  15243. '']));
  15244. end;
  15245. procedure TTestModule.TestExternalClass_Dollar;
  15246. begin
  15247. StartProgram(false);
  15248. Add([
  15249. '{$modeswitch externalclass}',
  15250. 'type',
  15251. ' TExtA = class external name ''$''',
  15252. ' Id: longint external name ''$'';',
  15253. ' function Bla(i: longint): longint; external name ''$'';',
  15254. ' end;',
  15255. 'function dollar(k: longint): longint; external name ''$'';',
  15256. 'var Obj: TExtA;',
  15257. 'begin',
  15258. ' dollar(1);',
  15259. ' obj.id:=obj.id+2;',
  15260. ' obj.Bla(3);',
  15261. '']);
  15262. ConvertProgram;
  15263. CheckSource('TestExternalClass_Dollar',
  15264. LinesToStr([ // statements
  15265. 'this.Obj = null;',
  15266. '']),
  15267. LinesToStr([ // $mod.$main
  15268. '$(1);',
  15269. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  15270. '$mod.Obj.$(3);',
  15271. '']));
  15272. end;
  15273. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  15274. begin
  15275. StartProgram(false);
  15276. Add('{$modeswitch externalclass}');
  15277. Add('type');
  15278. Add(' TExtA = class external name ''ExtA''');
  15279. Add(' Id: longint external name ''$Id'';');
  15280. Add(' end;');
  15281. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  15282. Add(' Id: longint;');
  15283. Add(' end;');
  15284. Add('begin');
  15285. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  15286. ConvertProgram;
  15287. end;
  15288. procedure TTestModule.TestExternalClass_Method;
  15289. begin
  15290. StartProgram(false);
  15291. Add('{$modeswitch externalclass}');
  15292. Add('type');
  15293. Add(' TExtA = class external name ''ExtObj''');
  15294. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  15295. Add(' procedure DoSome(Id: longint = 1);');
  15296. Add(' end;');
  15297. Add('var Obj: texta;');
  15298. Add('begin');
  15299. Add(' obj.doit;');
  15300. Add(' obj.doit();');
  15301. Add(' obj.doit(2);');
  15302. Add(' with obj do begin');
  15303. Add(' doit;');
  15304. Add(' doit();');
  15305. Add(' doit(3);');
  15306. Add(' end;');
  15307. ConvertProgram;
  15308. CheckSource('TestExternalClass_Method',
  15309. LinesToStr([ // statements
  15310. 'this.Obj = null;',
  15311. '']),
  15312. LinesToStr([ // $mod.$main
  15313. '$mod.Obj.$Execute(1);',
  15314. '$mod.Obj.$Execute(1);',
  15315. '$mod.Obj.$Execute(2);',
  15316. 'var $with1 = $mod.Obj;',
  15317. '$with1.$Execute(1);',
  15318. '$with1.$Execute(1);',
  15319. '$with1.$Execute(3);',
  15320. '']));
  15321. end;
  15322. procedure TTestModule.TestExternalClass_ClassMethod;
  15323. begin
  15324. StartProgram(false);
  15325. Add([
  15326. '{$modeswitch externalclass}',
  15327. 'type',
  15328. ' TExtA = class external name ''ExtObj''',
  15329. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15330. ' end;',
  15331. ' TExtB = TExtA;',
  15332. 'var p: Pointer;',
  15333. 'begin',
  15334. ' texta.doit;',
  15335. ' texta.doit();',
  15336. ' texta.doit(2);',
  15337. ' p:[email protected];',
  15338. ' with texta do begin',
  15339. ' doit;',
  15340. ' doit();',
  15341. ' doit(3);',
  15342. ' p:=@DoIt;',
  15343. ' end;',
  15344. ' textb.doit;',
  15345. ' textb.doit();',
  15346. ' textb.doit(4);',
  15347. ' with textb do begin',
  15348. ' doit;',
  15349. ' doit();',
  15350. ' doit(5);',
  15351. ' end;',
  15352. '']);
  15353. ConvertProgram;
  15354. CheckSource('TestExternalClass_ClassMethod',
  15355. LinesToStr([ // statements
  15356. 'this.p = null;',
  15357. '']),
  15358. LinesToStr([ // $mod.$main
  15359. 'ExtObj.$Execute(1);',
  15360. 'ExtObj.$Execute(1);',
  15361. 'ExtObj.$Execute(2);',
  15362. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15363. 'ExtObj.$Execute(1);',
  15364. 'ExtObj.$Execute(1);',
  15365. 'ExtObj.$Execute(3);',
  15366. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  15367. 'ExtObj.$Execute(1);',
  15368. 'ExtObj.$Execute(1);',
  15369. 'ExtObj.$Execute(4);',
  15370. 'ExtObj.$Execute(1);',
  15371. 'ExtObj.$Execute(1);',
  15372. 'ExtObj.$Execute(5);',
  15373. '']));
  15374. end;
  15375. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  15376. begin
  15377. StartProgram(false);
  15378. Add([
  15379. '{$modeswitch externalclass}',
  15380. 'type',
  15381. ' TExtA = class external name ''ExtObj''',
  15382. ' class procedure DoIt(Id: longint = 1); static;',
  15383. ' end;',
  15384. 'var p: Pointer;',
  15385. 'begin',
  15386. ' texta.doit;',
  15387. ' texta.doit();',
  15388. ' texta.doit(2);',
  15389. ' p:[email protected];',
  15390. ' with texta do begin',
  15391. ' doit;',
  15392. ' doit();',
  15393. ' doit(3);',
  15394. ' p:=@DoIt;',
  15395. ' end;',
  15396. '']);
  15397. ConvertProgram;
  15398. CheckSource('TestExternalClass_ClassMethodStatic',
  15399. LinesToStr([ // statements
  15400. 'this.p = null;',
  15401. '']),
  15402. LinesToStr([ // $mod.$main
  15403. 'ExtObj.DoIt(1);',
  15404. 'ExtObj.DoIt(1);',
  15405. 'ExtObj.DoIt(2);',
  15406. '$mod.p = ExtObj.DoIt;',
  15407. 'ExtObj.DoIt(1);',
  15408. 'ExtObj.DoIt(1);',
  15409. 'ExtObj.DoIt(3);',
  15410. '$mod.p = ExtObj.DoIt;',
  15411. '']));
  15412. end;
  15413. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  15414. begin
  15415. StartProgram(false);
  15416. Add([
  15417. '{$modeswitch externalclass}',
  15418. 'type',
  15419. ' TBird = class external name ''Array''',
  15420. ' end;',
  15421. 'function GetPtr: Pointer;',
  15422. 'begin',
  15423. 'end;',
  15424. 'procedure Write(const p);',
  15425. 'begin',
  15426. 'end;',
  15427. 'procedure WriteLn; varargs;',
  15428. 'begin',
  15429. 'end;',
  15430. 'begin',
  15431. ' if TBird(GetPtr)=nil then ;',
  15432. ' Write(GetPtr);',
  15433. ' WriteLn(GetPtr);',
  15434. ' Write(TBird(GetPtr));',
  15435. ' WriteLn(TBird(GetPtr));',
  15436. '']);
  15437. ConvertProgram;
  15438. CheckSource('TestFunctionResultInTypeCast',
  15439. LinesToStr([ // statements
  15440. 'this.GetPtr = function () {',
  15441. ' var Result = null;',
  15442. ' return Result;',
  15443. '};',
  15444. 'this.Write = function (p) {',
  15445. '};',
  15446. 'this.WriteLn = function () {',
  15447. '};',
  15448. '']),
  15449. LinesToStr([
  15450. 'if ($mod.GetPtr() === null) ;',
  15451. '$mod.Write($mod.GetPtr());',
  15452. '$mod.WriteLn($mod.GetPtr());',
  15453. '$mod.Write($mod.GetPtr());',
  15454. '$mod.WriteLn($mod.GetPtr());',
  15455. '']));
  15456. end;
  15457. procedure TTestModule.TestExternalClass_NonExternalOverride;
  15458. begin
  15459. StartProgram(false);
  15460. Add([
  15461. '{$modeswitch externalclass}',
  15462. 'type',
  15463. ' TExtA = class external name ''ExtObjA''',
  15464. ' procedure ProcA; virtual;',
  15465. ' procedure ProcB; virtual;',
  15466. ' end;',
  15467. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  15468. ' end;',
  15469. ' TExtC = class (TExtB)',
  15470. ' procedure ProcA; override;',
  15471. ' end;',
  15472. 'procedure TExtC.ProcA;',
  15473. 'begin',
  15474. ' ProcA;',
  15475. ' Self.ProcA;',
  15476. ' ProcB;',
  15477. ' Self.ProcB;',
  15478. 'end;',
  15479. 'var',
  15480. ' A: texta;',
  15481. ' B: textb;',
  15482. ' C: textc;',
  15483. 'begin',
  15484. ' a.proca;',
  15485. ' b.proca;',
  15486. ' c.proca;']);
  15487. ConvertProgram;
  15488. CheckSource('TestExternalClass_NonExternalOverride',
  15489. LinesToStr([ // statements
  15490. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  15491. ' this.$init = function () {',
  15492. ' };',
  15493. ' this.$final = function () {',
  15494. ' };',
  15495. ' this.ProcA = function () {',
  15496. ' this.ProcA();',
  15497. ' this.ProcA();',
  15498. ' this.ProcB();',
  15499. ' this.ProcB();',
  15500. ' };',
  15501. '});',
  15502. 'this.A = null;',
  15503. 'this.B = null;',
  15504. 'this.C = null;',
  15505. '']),
  15506. LinesToStr([ // $mod.$main
  15507. '$mod.A.ProcA();',
  15508. '$mod.B.ProcA();',
  15509. '$mod.C.ProcA();',
  15510. '']));
  15511. end;
  15512. procedure TTestModule.TestExternalClass_OverloadHint;
  15513. begin
  15514. StartProgram(false);
  15515. Add([
  15516. '{$modeswitch externalclass}',
  15517. 'type',
  15518. ' TExtA = class external name ''ExtObjA''',
  15519. ' procedure DoIt;',
  15520. ' procedure DoIt(i: longint);',
  15521. ' end;',
  15522. 'begin',
  15523. '']);
  15524. ConvertProgram;
  15525. CheckResolverUnexpectedHints(true);
  15526. CheckSource('TestExternalClass_OverloadHint',
  15527. LinesToStr([ // statements
  15528. '']),
  15529. LinesToStr([ // $mod.$main
  15530. '']));
  15531. end;
  15532. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  15533. begin
  15534. StartProgram(false);
  15535. Add([
  15536. '{$modeswitch externalclass}',
  15537. 'type',
  15538. ' JSwiper = class external name ''Swiper''',
  15539. ' constructor New;',
  15540. ' end;',
  15541. ' TObject = class',
  15542. ' private',
  15543. ' FSwiper: JSwiper;',
  15544. ' published',
  15545. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  15546. ' end;',
  15547. 'begin',
  15548. ' JSwiper.new;',
  15549. '']);
  15550. ConvertProgram;
  15551. CheckSource('TestExternalClass_SameNamePublishedProperty',
  15552. LinesToStr([ // statements
  15553. 'rtl.createClass($mod, "TObject", null, function () {',
  15554. ' this.$init = function () {',
  15555. ' this.FSwiper = null;',
  15556. ' };',
  15557. ' this.$final = function () {',
  15558. ' this.FSwiper = undefined;',
  15559. ' };',
  15560. ' var $r = this.$rtti;',
  15561. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  15562. '});',
  15563. '']),
  15564. LinesToStr([ // $mod.$main
  15565. 'new Swiper();',
  15566. '']));
  15567. end;
  15568. procedure TTestModule.TestExternalClass_Property;
  15569. begin
  15570. StartProgram(false);
  15571. Add([
  15572. '{$modeswitch externalclass}',
  15573. 'type',
  15574. ' TExtA = class external name ''ExtA''',
  15575. ' function getYear: longint;',
  15576. ' procedure setYear(Value: longint);',
  15577. ' property Year: longint read getyear write setyear;',
  15578. ' end;',
  15579. ' TExtB = class (TExtA)',
  15580. ' procedure OtherSetYear(Value: longint);',
  15581. ' property year write othersetyear;',
  15582. ' end;',
  15583. 'procedure textb.othersetyear(value: longint);',
  15584. 'begin',
  15585. ' setYear(Value+4);',
  15586. 'end;',
  15587. 'var',
  15588. ' A: texta;',
  15589. ' B: textb;',
  15590. 'begin',
  15591. ' a.year:=a.year+1;',
  15592. ' b.year:=b.year+2;']);
  15593. ConvertProgram;
  15594. CheckSource('TestExternalClass_NonExternalOverride',
  15595. LinesToStr([ // statements
  15596. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15597. ' this.$init = function () {',
  15598. ' };',
  15599. ' this.$final = function () {',
  15600. ' };',
  15601. ' this.OtherSetYear = function (Value) {',
  15602. ' this.setYear(Value+4);',
  15603. ' };',
  15604. '});',
  15605. 'this.A = null;',
  15606. 'this.B = null;',
  15607. '']),
  15608. LinesToStr([ // $mod.$main
  15609. '$mod.A.setYear($mod.A.getYear()+1);',
  15610. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  15611. '']));
  15612. end;
  15613. procedure TTestModule.TestExternalClass_PropertyDate;
  15614. begin
  15615. StartProgram(false);
  15616. Add([
  15617. '{$modeswitch externalclass}',
  15618. 'type',
  15619. ' TExtA = class external name ''ExtA''',
  15620. ' end;',
  15621. ' TExtB = class (TExtA)',
  15622. ' FDate: string;',
  15623. ' property Date: string read FDate write FDate;',
  15624. ' property ExtA: string read FDate write FDate;',
  15625. ' end;',
  15626. ' {$M+}',
  15627. ' TObject = class',
  15628. ' FDate: string;',
  15629. ' published',
  15630. ' property Date: string read FDate write FDate;',
  15631. ' property ExtA: string read FDate write FDate;',
  15632. ' end;',
  15633. 'var',
  15634. ' B: textb;',
  15635. ' o: TObject;',
  15636. 'begin',
  15637. ' b.date:=b.exta;',
  15638. ' o.date:=o.exta;']);
  15639. ConvertProgram;
  15640. CheckSource('TestExternalClass_PropertyDate',
  15641. LinesToStr([ // statements
  15642. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15643. ' this.$init = function () {',
  15644. ' this.FDate = "";',
  15645. ' };',
  15646. ' this.$final = function () {',
  15647. ' };',
  15648. '});',
  15649. 'rtl.createClass($mod, "TObject", null, function () {',
  15650. ' this.$init = function () {',
  15651. ' this.FDate = "";',
  15652. ' };',
  15653. ' this.$final = function () {',
  15654. ' };',
  15655. ' var $r = this.$rtti;',
  15656. ' $r.addField("FDate", rtl.string);',
  15657. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  15658. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  15659. '});',
  15660. 'this.B = null;',
  15661. 'this.o = null;',
  15662. '']),
  15663. LinesToStr([ // $mod.$main
  15664. '$mod.B.FDate = $mod.B.FDate;',
  15665. '$mod.o.FDate = $mod.o.FDate;',
  15666. '']));
  15667. end;
  15668. procedure TTestModule.TestExternalClass_ClassProperty;
  15669. begin
  15670. StartProgram(false);
  15671. Add('{$modeswitch externalclass}');
  15672. Add('type');
  15673. Add(' TExtA = class external name ''ExtA''');
  15674. Add(' class function getYear: longint;');
  15675. Add(' class procedure setYear(Value: longint);');
  15676. Add(' class property Year: longint read getyear write setyear;');
  15677. Add(' end;');
  15678. Add(' TExtB = class (TExtA)');
  15679. Add(' class function GetCentury: longint;');
  15680. Add(' class procedure SetCentury(Value: longint);');
  15681. Add(' class property Century: longint read getcentury write setcentury;');
  15682. Add(' end;');
  15683. Add('class function textb.getcentury: longint;');
  15684. Add('begin');
  15685. Add('end;');
  15686. Add('class procedure textb.setcentury(value: longint);');
  15687. Add('begin');
  15688. Add(' setyear(value+11);');
  15689. Add(' texta.year:=texta.year+12;');
  15690. Add(' year:=year+13;');
  15691. Add(' textb.century:=textb.century+14;');
  15692. Add(' century:=century+15;');
  15693. Add('end;');
  15694. Add('var');
  15695. Add(' A: texta;');
  15696. Add(' B: textb;');
  15697. Add('begin');
  15698. Add(' texta.year:=texta.year+1;');
  15699. Add(' textb.year:=textb.year+2;');
  15700. Add(' TextA.year:=TextA.year+3;');
  15701. Add(' b.year:=b.year+4;');
  15702. Add(' textb.century:=textb.century+5;');
  15703. Add(' b.century:=b.century+6;');
  15704. ConvertProgram;
  15705. CheckSource('TestExternalClass_ClassProperty',
  15706. LinesToStr([ // statements
  15707. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  15708. ' this.$init = function () {',
  15709. ' };',
  15710. ' this.$final = function () {',
  15711. ' };',
  15712. ' this.GetCentury = function () {',
  15713. ' var Result = 0;',
  15714. ' return Result;',
  15715. ' };',
  15716. ' this.SetCentury = function (Value) {',
  15717. ' this.setYear(Value + 11);',
  15718. ' ExtA.setYear(ExtA.getYear() + 12);',
  15719. ' this.setYear(this.getYear() + 13);',
  15720. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  15721. ' this.SetCentury(this.GetCentury() + 15);',
  15722. ' };',
  15723. '});',
  15724. 'this.A = null;',
  15725. 'this.B = null;',
  15726. '']),
  15727. LinesToStr([ // $mod.$main
  15728. 'ExtA.setYear(ExtA.getYear() + 1);',
  15729. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  15730. 'ExtA.setYear(ExtA.getYear() + 3);',
  15731. '$mod.B.setYear($mod.B.getYear() + 4);',
  15732. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  15733. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  15734. '']));
  15735. end;
  15736. procedure TTestModule.TestExternalClass_ClassOf;
  15737. begin
  15738. StartProgram(false);
  15739. Add('{$modeswitch externalclass}');
  15740. Add('type');
  15741. Add(' TExtA = class external name ''ExtA''');
  15742. Add(' procedure ProcA; virtual;');
  15743. Add(' procedure ProcB; virtual;');
  15744. Add(' end;');
  15745. Add(' TExtAClass = class of TExtA;');
  15746. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15747. Add(' end;');
  15748. Add(' TExtBClass = class of TExtB;');
  15749. Add(' TExtC = class (TExtB)');
  15750. Add(' procedure ProcA; override;');
  15751. Add(' end;');
  15752. Add(' TExtCClass = class of TExtC;');
  15753. Add('procedure TExtC.ProcA; begin end;');
  15754. Add('var');
  15755. Add(' A: texta; ClA: TExtAClass;');
  15756. Add(' B: textb; ClB: TExtBClass;');
  15757. Add(' C: textc; ClC: TExtCClass;');
  15758. Add('begin');
  15759. Add(' ClA:=texta;');
  15760. Add(' ClA:=textb;');
  15761. Add(' ClA:=textc;');
  15762. Add(' ClB:=textb;');
  15763. Add(' ClB:=textc;');
  15764. Add(' ClC:=textc;');
  15765. ConvertProgram;
  15766. CheckSource('TestExternalClass_ClassOf',
  15767. LinesToStr([ // statements
  15768. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15769. ' this.$init = function () {',
  15770. ' };',
  15771. ' this.$final = function () {',
  15772. ' };',
  15773. ' this.ProcA = function () {',
  15774. ' };',
  15775. '});',
  15776. 'this.A = null;',
  15777. 'this.ClA = null;',
  15778. 'this.B = null;',
  15779. 'this.ClB = null;',
  15780. 'this.C = null;',
  15781. 'this.ClC = null;',
  15782. '']),
  15783. LinesToStr([ // $mod.$main
  15784. '$mod.ClA = ExtA;',
  15785. '$mod.ClA = ExtB;',
  15786. '$mod.ClA = $mod.TExtC;',
  15787. '$mod.ClB = ExtB;',
  15788. '$mod.ClB = $mod.TExtC;',
  15789. '$mod.ClC = $mod.TExtC;',
  15790. '']));
  15791. end;
  15792. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  15793. begin
  15794. AddModuleWithIntfImplSrc('unit2.pas',
  15795. LinesToStr([
  15796. '{$modeswitch externalclass}',
  15797. 'type',
  15798. ' TExtA = class external name ''ExtA''',
  15799. ' class var Id: longint;',
  15800. ' end;',
  15801. '']),
  15802. '');
  15803. StartUnit(true);
  15804. Add('interface');
  15805. Add('uses unit2;');
  15806. Add('implementation');
  15807. Add('begin');
  15808. Add(' unit2.texta.id:=unit2.texta.id+1;');
  15809. ConvertUnit;
  15810. CheckSource('TestExternalClass_ClassOtherUnit',
  15811. LinesToStr([
  15812. '']),
  15813. LinesToStr([
  15814. 'ExtA.Id = ExtA.Id + 1;',
  15815. '']));
  15816. end;
  15817. procedure TTestModule.TestExternalClass_Is;
  15818. begin
  15819. StartProgram(false);
  15820. Add('{$modeswitch externalclass}');
  15821. Add('type');
  15822. Add(' TExtA = class external name ''ExtA''');
  15823. Add(' end;');
  15824. Add(' TExtAClass = class of TExtA;');
  15825. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15826. Add(' end;');
  15827. Add(' TExtBClass = class of TExtB;');
  15828. Add(' TExtC = class (TExtB)');
  15829. Add(' end;');
  15830. Add(' TExtCClass = class of TExtC;');
  15831. Add('var');
  15832. Add(' A: texta; ClA: TExtAClass;');
  15833. Add(' B: textb; ClB: TExtBClass;');
  15834. Add(' C: textc; ClC: TExtCClass;');
  15835. Add('begin');
  15836. Add(' if a is textb then ;');
  15837. Add(' if a is textc then ;');
  15838. Add(' if b is textc then ;');
  15839. Add(' if cla is textb then ;');
  15840. Add(' if cla is textc then ;');
  15841. Add(' if clb is textc then ;');
  15842. ConvertProgram;
  15843. CheckSource('TestExternalClass_Is',
  15844. LinesToStr([ // statements
  15845. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15846. ' this.$init = function () {',
  15847. ' };',
  15848. ' this.$final = function () {',
  15849. ' };',
  15850. '});',
  15851. 'this.A = null;',
  15852. 'this.ClA = null;',
  15853. 'this.B = null;',
  15854. 'this.ClB = null;',
  15855. 'this.C = null;',
  15856. 'this.ClC = null;',
  15857. '']),
  15858. LinesToStr([ // $mod.$main
  15859. 'if (rtl.isExt($mod.A, ExtB)) ;',
  15860. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  15861. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  15862. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  15863. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  15864. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  15865. '']));
  15866. end;
  15867. procedure TTestModule.TestExternalClass_As;
  15868. begin
  15869. StartProgram(false);
  15870. Add('{$modeswitch externalclass}');
  15871. Add('type');
  15872. Add(' TExtA = class external name ''ExtA''');
  15873. Add(' end;');
  15874. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  15875. Add(' end;');
  15876. Add(' TExtC = class (TExtB)');
  15877. Add(' end;');
  15878. Add('var');
  15879. Add(' A: texta;');
  15880. Add(' B: textb;');
  15881. Add(' C: textc;');
  15882. Add('begin');
  15883. Add(' b:=a as textb;');
  15884. Add(' c:=a as textc;');
  15885. Add(' c:=b as textc;');
  15886. ConvertProgram;
  15887. CheckSource('TestExternalClass_Is',
  15888. LinesToStr([ // statements
  15889. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  15890. ' this.$init = function () {',
  15891. ' };',
  15892. ' this.$final = function () {',
  15893. ' };',
  15894. '});',
  15895. 'this.A = null;',
  15896. 'this.B = null;',
  15897. 'this.C = null;',
  15898. '']),
  15899. LinesToStr([ // $mod.$main
  15900. '$mod.B = rtl.asExt($mod.A, ExtB);',
  15901. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  15902. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  15903. '']));
  15904. end;
  15905. procedure TTestModule.TestExternalClass_DestructorFail;
  15906. begin
  15907. StartProgram(false);
  15908. Add('{$modeswitch externalclass}');
  15909. Add('type');
  15910. Add(' TExtA = class external name ''ExtA''');
  15911. Add(' destructor Free;');
  15912. Add(' end;');
  15913. SetExpectedPasResolverError('Pascal element not supported: destructor',
  15914. nPasElementNotSupported);
  15915. ConvertProgram;
  15916. end;
  15917. procedure TTestModule.TestExternalClass_New;
  15918. begin
  15919. StartProgram(false);
  15920. Add([
  15921. '{$modeswitch externalclass}',
  15922. 'type',
  15923. ' TExtA = class external name ''ExtA''',
  15924. ' constructor New;',
  15925. ' constructor New(i: longint; j: longint = 2);',
  15926. ' end;',
  15927. 'var',
  15928. ' A: texta;',
  15929. 'begin',
  15930. ' a:=texta.new;',
  15931. ' a:=texta(texta.new);',
  15932. ' a:=texta.new();',
  15933. ' a:=texta.new(1);',
  15934. ' with texta do begin',
  15935. ' a:=new;',
  15936. ' a:=new();',
  15937. ' a:=new(2);',
  15938. ' end;',
  15939. ' a:=test1.texta.new;',
  15940. ' a:=test1.texta.new();',
  15941. ' a:=test1.texta.new(3);',
  15942. '']);
  15943. ConvertProgram;
  15944. CheckSource('TestExternalClass_New',
  15945. LinesToStr([ // statements
  15946. 'this.A = null;',
  15947. '']),
  15948. LinesToStr([ // $mod.$main
  15949. '$mod.A = new ExtA();',
  15950. '$mod.A = new ExtA();',
  15951. '$mod.A = new ExtA();',
  15952. '$mod.A = new ExtA(1,2);',
  15953. '$mod.A = new ExtA();',
  15954. '$mod.A = new ExtA();',
  15955. '$mod.A = new ExtA(2,2);',
  15956. '$mod.A = new ExtA();',
  15957. '$mod.A = new ExtA();',
  15958. '$mod.A = new ExtA(3,2);',
  15959. '']));
  15960. end;
  15961. procedure TTestModule.TestExternalClass_ClassOf_New;
  15962. begin
  15963. StartProgram(false);
  15964. Add('{$modeswitch externalclass}');
  15965. Add('type');
  15966. Add(' TExtAClass = class of TExtA;');
  15967. Add(' TExtA = class external name ''ExtA''');
  15968. Add(' C: TExtAClass;');
  15969. Add(' constructor New;');
  15970. Add(' end;');
  15971. Add('var');
  15972. Add(' A: texta;');
  15973. Add(' C: textaclass;');
  15974. Add('begin');
  15975. Add(' a:=c.new;');
  15976. Add(' a:=c.new();');
  15977. Add(' with C do begin');
  15978. Add(' a:=new;');
  15979. Add(' a:=new();');
  15980. Add(' end;');
  15981. Add(' a:=test1.c.new;');
  15982. Add(' a:=test1.c.new();');
  15983. Add(' a:=A.c.new();');
  15984. ConvertProgram;
  15985. CheckSource('TestExternalClass_ClassOf_New',
  15986. LinesToStr([ // statements
  15987. 'this.A = null;',
  15988. 'this.C = null;',
  15989. '']),
  15990. LinesToStr([ // $mod.$main
  15991. '$mod.A = new $mod.C();',
  15992. '$mod.A = new $mod.C();',
  15993. 'var $with1 = $mod.C;',
  15994. '$mod.A = new $with1();',
  15995. '$mod.A = new $with1();',
  15996. '$mod.A = new $mod.C();',
  15997. '$mod.A = new $mod.C();',
  15998. '$mod.A = new $mod.A.C();',
  15999. '']));
  16000. end;
  16001. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  16002. begin
  16003. StartProgram(false);
  16004. Add([
  16005. '{$modeswitch externalclass}',
  16006. 'type',
  16007. ' TExtAClass = class of TExtA;',
  16008. ' TExtA = class external name ''ExtA''',
  16009. ' constructor New;',
  16010. ' end;',
  16011. 'function GetCreator: TExtAClass;',
  16012. 'begin',
  16013. ' Result:=TExtA;',
  16014. 'end;',
  16015. 'var',
  16016. ' A: texta;',
  16017. 'begin',
  16018. ' a:=getcreator.new;',
  16019. ' a:=getcreator().new;',
  16020. ' a:=getcreator().new();',
  16021. ' a:=getcreator.new();',
  16022. ' with getcreator do begin',
  16023. ' a:=new;',
  16024. ' a:=new();',
  16025. ' end;']);
  16026. ConvertProgram;
  16027. CheckSource('TestExternalClass_FuncClassOf_New',
  16028. LinesToStr([ // statements
  16029. 'this.GetCreator = function () {',
  16030. ' var Result = null;',
  16031. ' Result = ExtA;',
  16032. ' return Result;',
  16033. '};',
  16034. 'this.A = null;',
  16035. '']),
  16036. LinesToStr([ // $mod.$main
  16037. '$mod.A = new ($mod.GetCreator())();',
  16038. '$mod.A = new ($mod.GetCreator())();',
  16039. '$mod.A = new ($mod.GetCreator())();',
  16040. '$mod.A = new ($mod.GetCreator())();',
  16041. 'var $with1 = $mod.GetCreator();',
  16042. '$mod.A = new $with1();',
  16043. '$mod.A = new $with1();',
  16044. '']));
  16045. end;
  16046. procedure TTestModule.TestExternalClass_New_PasClassFail;
  16047. begin
  16048. StartProgram(false);
  16049. Add([
  16050. '{$modeswitch externalclass}',
  16051. 'type',
  16052. ' TExtA = class external name ''ExtA''',
  16053. ' constructor New;',
  16054. ' end;',
  16055. ' TBird = class(TExtA)',
  16056. ' end;',
  16057. 'begin',
  16058. ' TBird.new;',
  16059. '']);
  16060. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16061. ConvertProgram;
  16062. end;
  16063. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  16064. begin
  16065. StartProgram(false);
  16066. Add([
  16067. '{$modeswitch externalclass}',
  16068. 'type',
  16069. ' TExtA = class external name ''ExtA''',
  16070. ' constructor New;',
  16071. ' end;',
  16072. ' TBird = class(TExtA)',
  16073. ' end;',
  16074. 'begin',
  16075. ' TBird.new();',
  16076. '']);
  16077. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16078. ConvertProgram;
  16079. end;
  16080. procedure TTestModule.TestExternalClass_NewExtName;
  16081. begin
  16082. StartProgram(false);
  16083. Add([
  16084. '{$modeswitch externalclass}',
  16085. 'type',
  16086. ' TExtA = class external name ''ExtA''',
  16087. ' constructor New; external name ''Other'';',
  16088. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  16089. ' end;',
  16090. 'var',
  16091. ' A: texta;',
  16092. 'begin',
  16093. ' a:=texta.new;',
  16094. ' a:=texta(texta.new);',
  16095. ' a:=texta.new();',
  16096. ' a:=texta.new(1);',
  16097. ' with texta do begin',
  16098. ' a:=new;',
  16099. ' a:=new();',
  16100. ' a:=new(2);',
  16101. ' end;',
  16102. ' a:=test1.texta.new;',
  16103. ' a:=test1.texta.new();',
  16104. ' a:=test1.texta.new(3);',
  16105. '']);
  16106. ConvertProgram;
  16107. CheckSource('TestExternalClass_NewExtName',
  16108. LinesToStr([ // statements
  16109. 'this.A = null;',
  16110. '']),
  16111. LinesToStr([ // $mod.$main
  16112. '$mod.A = new Other();',
  16113. '$mod.A = new Other();',
  16114. '$mod.A = new Other();',
  16115. '$mod.A = new A.B(1,2);',
  16116. '$mod.A = new Other();',
  16117. '$mod.A = new Other();',
  16118. '$mod.A = new A.B(2,2);',
  16119. '$mod.A = new Other();',
  16120. '$mod.A = new Other();',
  16121. '$mod.A = new A.B(3,2);',
  16122. '']));
  16123. end;
  16124. procedure TTestModule.TestExternalClass_Constructor;
  16125. begin
  16126. StartProgram(false);
  16127. Add([
  16128. '{$modeswitch externalclass}',
  16129. 'type',
  16130. ' TExtA = class external name ''ExtA''',
  16131. ' constructor Create;',
  16132. ' constructor Create(i: longint; j: longint = 2);',
  16133. ' end;',
  16134. 'var',
  16135. ' A: texta;',
  16136. 'begin',
  16137. ' a:=texta.create;',
  16138. ' a:=texta(texta.create);',
  16139. ' a:=texta.create();',
  16140. ' a:=texta.create(1);',
  16141. ' with texta do begin',
  16142. ' a:=create;',
  16143. ' a:=create();',
  16144. ' a:=create(2);',
  16145. ' end;',
  16146. ' a:=test1.texta.create;',
  16147. ' a:=test1.texta.create();',
  16148. ' a:=test1.texta.create(3);',
  16149. '']);
  16150. ConvertProgram;
  16151. CheckSource('TestExternalClass_Constructor',
  16152. LinesToStr([ // statements
  16153. 'this.A = null;',
  16154. '']),
  16155. LinesToStr([ // $mod.$main
  16156. '$mod.A = new ExtA.Create();',
  16157. '$mod.A = new ExtA.Create();',
  16158. '$mod.A = new ExtA.Create();',
  16159. '$mod.A = new ExtA.Create(1,2);',
  16160. '$mod.A = new ExtA.Create();',
  16161. '$mod.A = new ExtA.Create();',
  16162. '$mod.A = new ExtA.Create(2,2);',
  16163. '$mod.A = new ExtA.Create();',
  16164. '$mod.A = new ExtA.Create();',
  16165. '$mod.A = new ExtA.Create(3,2);',
  16166. '']));
  16167. end;
  16168. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  16169. begin
  16170. StartProgram(false);
  16171. Add([
  16172. '{$modeswitch externalclass}',
  16173. 'type',
  16174. ' TExtA = class external name ''ExtA''',
  16175. ' constructor Create; external name ''{}'';',
  16176. ' end;',
  16177. 'var',
  16178. ' A: texta;',
  16179. 'begin',
  16180. ' a:=texta.create;',
  16181. ' a:=texta(texta.create);',
  16182. ' a:=texta.create();',
  16183. ' with texta do begin',
  16184. ' a:=create;',
  16185. ' a:=create();',
  16186. ' end;',
  16187. ' a:=test1.texta.create;',
  16188. ' a:=test1.texta.create();',
  16189. '']);
  16190. ConvertProgram;
  16191. CheckSource('TestExternalClass_ConstructorBrackets',
  16192. LinesToStr([ // statements
  16193. 'this.A = null;',
  16194. '']),
  16195. LinesToStr([ // $mod.$main
  16196. '$mod.A = {};',
  16197. '$mod.A = {};',
  16198. '$mod.A = {};',
  16199. '$mod.A = {};',
  16200. '$mod.A = {};',
  16201. '$mod.A = {};',
  16202. '$mod.A = {};',
  16203. '']));
  16204. end;
  16205. procedure TTestModule.TestExternalClass_LocalConstSameName;
  16206. begin
  16207. StartProgram(false);
  16208. Add('{$modeswitch externalclass}');
  16209. Add('type');
  16210. Add(' TExtA = class external name ''ExtA''');
  16211. Add(' constructor New;');
  16212. Add(' end;');
  16213. Add('function DoIt: longint;');
  16214. Add('const ExtA: longint = 3;');
  16215. Add('begin');
  16216. Add(' Result:=ExtA;');
  16217. Add('end;');
  16218. Add('var');
  16219. Add(' A: texta;');
  16220. Add('begin');
  16221. Add(' a:=texta.new;');
  16222. ConvertProgram;
  16223. CheckSource('TestExternalClass_LocalConstSameName',
  16224. LinesToStr([ // statements
  16225. 'var ExtA$1 = 3;',
  16226. 'this.DoIt = function () {',
  16227. ' var Result = 0;',
  16228. ' Result = ExtA$1;',
  16229. ' return Result;',
  16230. '};',
  16231. 'this.A = null;',
  16232. '']),
  16233. LinesToStr([ // $mod.$main
  16234. '$mod.A = new ExtA();',
  16235. '']));
  16236. end;
  16237. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  16238. begin
  16239. StartProgram(false);
  16240. Add('{$modeswitch externalclass}');
  16241. Add('type');
  16242. Add(' TExtA = class external name ''ExtA''');
  16243. Add(' procedure DoIt;');
  16244. Add(' end;');
  16245. Add(' TMyA = class(TExtA)');
  16246. Add(' procedure DoIt;');
  16247. Add(' end;');
  16248. Add('procedure TMyA.DoIt; begin end;');
  16249. Add('begin');
  16250. ConvertProgram;
  16251. CheckSource('TestExternalClass_ReintroduceOverload',
  16252. LinesToStr([ // statements
  16253. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  16254. ' this.$init = function () {',
  16255. ' };',
  16256. ' this.$final = function () {',
  16257. ' };',
  16258. ' this.DoIt$1 = function () {',
  16259. ' };',
  16260. '});',
  16261. '']),
  16262. LinesToStr([ // $mod.$main
  16263. '']));
  16264. end;
  16265. procedure TTestModule.TestExternalClass_Inherited;
  16266. begin
  16267. StartProgram(false);
  16268. Add('{$modeswitch externalclass}');
  16269. Add('type');
  16270. Add(' TExtA = class external name ''ExtA''');
  16271. Add(' procedure DoIt(i: longint = 1); virtual;');
  16272. Add(' procedure DoSome(j: longint = 2);');
  16273. Add(' end;');
  16274. Add(' TExtB = class external name ''ExtB''(TExtA)');
  16275. Add(' end;');
  16276. Add(' TMyC = class(TExtB)');
  16277. Add(' procedure DoIt(i: longint = 1); override;');
  16278. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  16279. Add(' end;');
  16280. Add('procedure TMyC.DoIt(i: longint);');
  16281. Add('begin');
  16282. Add(' inherited;');
  16283. Add(' inherited DoIt;');
  16284. Add(' inherited DoIt();');
  16285. Add(' inherited DoIt(3);');
  16286. Add(' inherited DoSome;');
  16287. Add(' inherited DoSome();');
  16288. Add(' inherited DoSome(4);');
  16289. Add('end;');
  16290. Add('procedure TMyC.DoSome(j: longint);');
  16291. Add('begin');
  16292. Add(' inherited;');
  16293. Add('end;');
  16294. Add('begin');
  16295. ConvertProgram;
  16296. CheckSource('TestExternalClass_ReintroduceOverload',
  16297. LinesToStr([ // statements
  16298. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  16299. ' this.$init = function () {',
  16300. ' };',
  16301. ' this.$final = function () {',
  16302. ' };',
  16303. ' this.DoIt = function (i) {',
  16304. ' ExtB.DoIt.apply(this, arguments);',
  16305. ' ExtB.DoIt.call(this, 1);',
  16306. ' ExtB.DoIt.call(this, 1);',
  16307. ' ExtB.DoIt.call(this, 3);',
  16308. ' ExtB.DoSome.call(this, 2);',
  16309. ' ExtB.DoSome.call(this, 2);',
  16310. ' ExtB.DoSome.call(this, 4);',
  16311. ' };',
  16312. ' this.DoSome$1 = function (j) {',
  16313. ' ExtB.DoSome.apply(this, arguments);',
  16314. ' };',
  16315. '});',
  16316. '']),
  16317. LinesToStr([ // $mod.$main
  16318. '']));
  16319. end;
  16320. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  16321. begin
  16322. StartProgram(false);
  16323. Add('{$modeswitch externalclass}');
  16324. Add('type');
  16325. Add(' TObject = class');
  16326. Add(' end;');
  16327. Add(' TExtA = class external name ''ExtA''(TObject)');
  16328. Add(' end;');
  16329. Add('begin');
  16330. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  16331. ConvertProgram;
  16332. end;
  16333. procedure TTestModule.TestExternalClass_NewInstance;
  16334. begin
  16335. StartProgram(false);
  16336. Add('{$modeswitch externalclass}');
  16337. Add('type');
  16338. Add(' TExtA = class external name ''ExtA''');
  16339. Add(' end;');
  16340. Add(' TMyB = class(TExtA)');
  16341. Add(' protected');
  16342. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  16343. Add(' end;');
  16344. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16345. Add('begin end;');
  16346. Add('begin');
  16347. ConvertProgram;
  16348. CheckSource('TestExternalClass_NewInstance',
  16349. LinesToStr([ // statements
  16350. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  16351. ' this.$init = function () {',
  16352. ' };',
  16353. ' this.$final = function () {',
  16354. ' };',
  16355. ' this.NewInstance = function (fnname, paramarray) {',
  16356. ' var Result = null;',
  16357. ' return Result;',
  16358. ' };',
  16359. '});',
  16360. '']),
  16361. LinesToStr([ // $mod.$main
  16362. '']));
  16363. end;
  16364. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  16365. begin
  16366. StartProgram(false);
  16367. Add('{$modeswitch externalclass}');
  16368. Add('type');
  16369. Add(' TExtA = class external name ''ExtA''');
  16370. Add(' end;');
  16371. Add(' TMyB = class(TExtA)');
  16372. Add(' protected');
  16373. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  16374. Add(' end;');
  16375. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  16376. Add('begin end;');
  16377. Add('begin');
  16378. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  16379. ConvertProgram;
  16380. end;
  16381. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  16382. begin
  16383. StartProgram(false);
  16384. Add('{$modeswitch externalclass}');
  16385. Add('type');
  16386. Add(' TExtA = class external name ''ExtA''');
  16387. Add(' end;');
  16388. Add(' TMyB = class(TExtA)');
  16389. Add(' protected');
  16390. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  16391. Add(' end;');
  16392. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  16393. Add('begin end;');
  16394. Add('begin');
  16395. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  16396. nIncompatibleTypeArgNo);
  16397. ConvertProgram;
  16398. end;
  16399. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  16400. begin
  16401. StartProgram(false);
  16402. Add('{$modeswitch externalclass}');
  16403. Add('type');
  16404. Add(' TExtA = class external name ''ExtA''');
  16405. Add(' end;');
  16406. Add(' TMyB = class(TExtA)');
  16407. Add(' protected');
  16408. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  16409. Add(' end;');
  16410. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  16411. Add('begin end;');
  16412. Add('begin');
  16413. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  16414. nIncompatibleTypeArgNo);
  16415. ConvertProgram;
  16416. end;
  16417. procedure TTestModule.TestExternalClass_PascalProperty;
  16418. begin
  16419. StartProgram(false);
  16420. Add('{$modeswitch externalclass}');
  16421. Add('type');
  16422. Add(' TJSElement = class;');
  16423. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  16424. Add(' TJSElement = class external name ''ExtA''');
  16425. Add(' end;');
  16426. Add(' TControl = class(TJSElement)');
  16427. Add(' private');
  16428. Add(' FOnClick: TJSNotifyEvent;');
  16429. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  16430. Add(' procedure Click(Sender: TJSElement);');
  16431. Add(' end;');
  16432. Add('procedure TControl.Click(Sender: TJSElement);');
  16433. Add('begin');
  16434. Add(' OnClick(Self);');
  16435. Add('end;');
  16436. Add('var');
  16437. Add(' Ctrl: TControl;');
  16438. Add('begin');
  16439. Add(' Ctrl.OnClick:[email protected];');
  16440. Add(' Ctrl.OnClick(Ctrl);');
  16441. ConvertProgram;
  16442. CheckSource('TestExternalClass_PascalProperty',
  16443. LinesToStr([ // statements
  16444. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  16445. ' this.$init = function () {',
  16446. ' this.FOnClick = null;',
  16447. ' };',
  16448. ' this.$final = function () {',
  16449. ' this.FOnClick = undefined;',
  16450. ' };',
  16451. ' this.Click = function (Sender) {',
  16452. ' this.FOnClick(this);',
  16453. ' };',
  16454. '});',
  16455. 'this.Ctrl = null;',
  16456. '']),
  16457. LinesToStr([ // $mod.$main
  16458. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  16459. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  16460. '']));
  16461. end;
  16462. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  16463. begin
  16464. StartProgram(false);
  16465. Add([
  16466. '{$modeswitch externalclass}',
  16467. 'type',
  16468. ' IUnknown = interface end;',
  16469. ' TObject = class',
  16470. ' end;',
  16471. ' TChild = class',
  16472. ' end;',
  16473. ' TExtRootA = class external name ''ExtRootA''',
  16474. ' end;',
  16475. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  16476. ' end;',
  16477. ' TExtRootB = class external name ''ExtRootB''',
  16478. ' end;',
  16479. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  16480. ' end;',
  16481. 'var',
  16482. ' Obj: TObject;',
  16483. ' Child: TChild;',
  16484. ' RootA: TExtRootA;',
  16485. ' ChildA: TExtChildA;',
  16486. ' RootB: TExtRootB;',
  16487. ' ChildB: TExtChildB;',
  16488. ' i: IUnknown;',
  16489. 'begin',
  16490. ' obj:=tobject(roota);',
  16491. ' obj:=tobject(childa);',
  16492. ' child:=tchild(tobject(roota));',
  16493. ' roota:=textroota(obj);',
  16494. ' roota:=textroota(child);',
  16495. ' roota:=textroota(rootb);',
  16496. ' roota:=textroota(childb);',
  16497. ' childa:=textchilda(textroota(obj));',
  16498. ' roota:=TExtRootA(i)',
  16499. '']);
  16500. ConvertProgram;
  16501. CheckSource('TestExternalClass_TypeCastToRootClass',
  16502. LinesToStr([ // statements
  16503. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16504. 'rtl.createClass($mod, "TObject", null, function () {',
  16505. ' this.$init = function () {',
  16506. ' };',
  16507. ' this.$final = function () {',
  16508. ' };',
  16509. '});',
  16510. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16511. '});',
  16512. 'this.Obj = null;',
  16513. 'this.Child = null;',
  16514. 'this.RootA = null;',
  16515. 'this.ChildA = null;',
  16516. 'this.RootB = null;',
  16517. 'this.ChildB = null;',
  16518. 'this.i = null;',
  16519. '']),
  16520. LinesToStr([ // $mod.$main
  16521. '$mod.Obj = $mod.RootA;',
  16522. '$mod.Obj = $mod.ChildA;',
  16523. '$mod.Child = $mod.RootA;',
  16524. '$mod.RootA = $mod.Obj;',
  16525. '$mod.RootA = $mod.Child;',
  16526. '$mod.RootA = $mod.RootB;',
  16527. '$mod.RootA = $mod.ChildB;',
  16528. '$mod.ChildA = $mod.Obj;',
  16529. '$mod.RootA = $mod.i;',
  16530. '']));
  16531. end;
  16532. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  16533. begin
  16534. StartProgram(false);
  16535. Add([
  16536. '{$modeswitch externalclass}',
  16537. 'type',
  16538. ' IUnknown = interface end;',
  16539. ' IBird = interface(IUnknown) end;',
  16540. ' TClass = class of TObject;',
  16541. ' TObject = class',
  16542. ' end;',
  16543. ' TChild = class',
  16544. ' end;',
  16545. ' TJSObject = class external name ''Object''',
  16546. ' end;',
  16547. ' TRec = record end;',
  16548. 'var',
  16549. ' Obj: TObject;',
  16550. ' Child: TChild;',
  16551. ' i: IUnknown;',
  16552. ' Bird: IBird;',
  16553. ' j: TJSObject;',
  16554. ' r: TRec;',
  16555. ' c: TClass;',
  16556. 'begin',
  16557. ' j:=tjsobject(IUnknown);',
  16558. ' j:=tjsobject(IBird);',
  16559. ' j:=tjsobject(TObject);',
  16560. ' j:=tjsobject(TChild);',
  16561. ' j:=tjsobject(TRec);',
  16562. ' j:=tjsobject(Obj);',
  16563. ' j:=tjsobject(Child);',
  16564. ' j:=tjsobject(i);',
  16565. ' j:=tjsobject(Bird);',
  16566. ' j:=tjsobject(r);',
  16567. ' j:=tjsobject(c);',
  16568. '']);
  16569. ConvertProgram;
  16570. CheckSource('TestExternalClass_TypeCastToJSObject',
  16571. LinesToStr([ // statements
  16572. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16573. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  16574. 'rtl.createClass($mod, "TObject", null, function () {',
  16575. ' this.$init = function () {',
  16576. ' };',
  16577. ' this.$final = function () {',
  16578. ' };',
  16579. '});',
  16580. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  16581. '});',
  16582. 'rtl.recNewT($mod, "TRec", function () {',
  16583. ' this.$eq = function (b) {',
  16584. ' return true;',
  16585. ' };',
  16586. ' this.$assign = function (s) {',
  16587. ' return this;',
  16588. ' };',
  16589. '});',
  16590. 'this.Obj = null;',
  16591. 'this.Child = null;',
  16592. 'this.i = null;',
  16593. 'this.Bird = null;',
  16594. 'this.j = null;',
  16595. 'this.r = $mod.TRec.$new();',
  16596. 'this.c = null;',
  16597. '']),
  16598. LinesToStr([ // $mod.$main
  16599. '$mod.j = $mod.IUnknown;',
  16600. '$mod.j = $mod.IBird;',
  16601. '$mod.j = $mod.TObject;',
  16602. '$mod.j = $mod.TChild;',
  16603. '$mod.j = $mod.TRec;',
  16604. '$mod.j = $mod.Obj;',
  16605. '$mod.j = $mod.Child;',
  16606. '$mod.j = $mod.i;',
  16607. '$mod.j = $mod.Bird;',
  16608. '$mod.j = $mod.r;',
  16609. '$mod.j = $mod.c;',
  16610. '']));
  16611. end;
  16612. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  16613. begin
  16614. StartProgram(false);
  16615. Add('{$modeswitch externalclass}');
  16616. Add('type');
  16617. Add(' TJSString = class external name ''String''');
  16618. Add(' class function fromCharCode() : string; varargs;');
  16619. Add(' function anchor(const aName : string) : string;');
  16620. Add(' end;');
  16621. Add('var');
  16622. Add(' s: string;');
  16623. Add('begin');
  16624. Add(' s:=TJSString.fromCharCode(65,66);');
  16625. Add(' s:=TJSString(s).anchor(s);');
  16626. Add(' s:=TJSString(''foo'').anchor(s);');
  16627. ConvertProgram;
  16628. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  16629. LinesToStr([ // statements
  16630. 'this.s = "";',
  16631. '']),
  16632. LinesToStr([ // $mod.$main
  16633. '$mod.s = String.fromCharCode(65, 66);',
  16634. '$mod.s = $mod.s.anchor($mod.s);',
  16635. '$mod.s = "foo".anchor($mod.s);',
  16636. '']));
  16637. end;
  16638. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  16639. begin
  16640. StartProgram(false);
  16641. Add([
  16642. '{$modeswitch externalclass}',
  16643. 'type',
  16644. ' TJSObject = class external name ''Object'' end;',
  16645. ' TJSFunction = class external name ''Function''',
  16646. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  16647. ' function call(thisArg: TJSObject): JSValue; varargs;',
  16648. ' end;',
  16649. ' TObject = class',
  16650. ' procedure DoIt(i: longint);',
  16651. ' end;',
  16652. ' TFuncInt = function(o: TObject): longint;',
  16653. 'function GetIt(o: TObject): longint;',
  16654. ' procedure Sub; begin end;',
  16655. 'var',
  16656. ' f: TJSFunction;',
  16657. ' fi: TFuncInt;',
  16658. 'begin',
  16659. ' fi:=TFuncInt(f);',
  16660. ' f:=TJSFunction(fi);',
  16661. ' f:=TJSFunction(@GetIt);',
  16662. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  16663. ' f:=TJSFunction(@Sub);',
  16664. ' f:=TJSFunction(@o.doit);',
  16665. ' f:=TJSFunction(fi).bind(nil,4)',
  16666. 'end;',
  16667. 'procedure TObject.DoIt(i: longint);',
  16668. ' procedure Sub; begin end;',
  16669. 'var f: TJSFunction;',
  16670. 'begin',
  16671. ' f:=TJSFunction(@DoIt);',
  16672. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  16673. ' f:=TJSFunction(@Sub);',
  16674. ' f:=TJSFunction(@GetIt);',
  16675. 'end;',
  16676. 'begin']);
  16677. ConvertProgram;
  16678. CheckSource('TestExternalClass_TypeCastToJSFunction',
  16679. LinesToStr([ // statements
  16680. 'rtl.createClass($mod, "TObject", null, function () {',
  16681. ' this.$init = function () {',
  16682. ' };',
  16683. ' this.$final = function () {',
  16684. ' };',
  16685. ' this.DoIt = function (i) {',
  16686. ' var $Self = this;',
  16687. ' function Sub() {',
  16688. ' };',
  16689. ' var f = null;',
  16690. ' f = $Self.DoIt;',
  16691. ' f = $Self.DoIt.bind(null, 13);',
  16692. ' f = Sub;',
  16693. ' f = $mod.GetIt;',
  16694. ' };',
  16695. '});',
  16696. 'this.GetIt = function (o) {',
  16697. ' var Result = 0;',
  16698. ' function Sub() {',
  16699. ' };',
  16700. ' var f = null;',
  16701. ' var fi = null;',
  16702. ' fi = f;',
  16703. ' f = fi;',
  16704. ' f = $mod.GetIt;',
  16705. ' f = $mod.GetIt.bind(null, 3);',
  16706. ' f = Sub;',
  16707. ' f = $mod.TObject.DoIt;',
  16708. ' f = fi.bind(null, 4);',
  16709. ' return Result;',
  16710. '};',
  16711. '']),
  16712. LinesToStr([ // $mod.$main
  16713. '']));
  16714. end;
  16715. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  16716. begin
  16717. StartProgram(false);
  16718. Add([
  16719. '{$mode delphi}',
  16720. '{$modeswitch externalclass}',
  16721. 'type',
  16722. ' TJSObject = class external name ''Object'' end;',
  16723. ' TJSWindow = class external name ''Window''(TJSObject)',
  16724. ' procedure Open;',
  16725. ' end;',
  16726. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  16727. ' procedure Execute;',
  16728. ' end;',
  16729. 'procedure Fly;',
  16730. 'var',
  16731. ' w: TJSWindow;',
  16732. ' e: TJSEventTarget;',
  16733. 'begin',
  16734. ' w:=TJSWindow(e);',
  16735. ' e:=TJSEventTarget(w);',
  16736. 'end;',
  16737. 'begin']);
  16738. ConvertProgram;
  16739. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  16740. LinesToStr([ // statements
  16741. 'this.Fly = function () {',
  16742. ' var w = null;',
  16743. ' var e = null;',
  16744. ' w = e;',
  16745. ' e = w;',
  16746. '};',
  16747. '']),
  16748. LinesToStr([ // $mod.$main
  16749. '']));
  16750. end;
  16751. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  16752. begin
  16753. StartProgram(false);
  16754. Add('{$modeswitch externalclass}');
  16755. Add('type');
  16756. Add(' TJSString = class external name ''String''');
  16757. Add(' class function fromCharCode() : string; varargs;');
  16758. Add(' end;');
  16759. Add('var');
  16760. Add(' s: string;');
  16761. Add(' sObj: TJSString;');
  16762. Add('begin');
  16763. Add(' s:=sObj.fromCharCode(65,66);');
  16764. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  16765. nExternalClassInstanceCannotAccessStaticX);
  16766. ConvertProgram;
  16767. end;
  16768. procedure TTestModule.TestExternalClass_BracketAccessor;
  16769. begin
  16770. StartProgram(false);
  16771. Add([
  16772. '{$modeswitch externalclass}',
  16773. 'type',
  16774. ' TJSArray = class external name ''Array2''',
  16775. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16776. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16777. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16778. ' end;',
  16779. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  16780. 'begin end;',
  16781. 'var',
  16782. ' Arr: tjsarray;',
  16783. ' s: string;',
  16784. ' i: longint;',
  16785. ' v: jsvalue;',
  16786. 'begin',
  16787. ' v:=arr[0];',
  16788. ' v:=arr.items[1];',
  16789. ' arr[2]:=s;',
  16790. ' arr.items[3]:=s;',
  16791. ' arr[4]:=i;',
  16792. ' arr[5]:=arr[6];',
  16793. ' arr.items[7]:=arr.items[8];',
  16794. ' with arr do items[9]:=items[10];',
  16795. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  16796. ' with arr do begin',
  16797. ' v:=GetItems(14);',
  16798. ' setitems(15,16);',
  16799. ' end;',
  16800. ' v:=test1.arr.items[17];',
  16801. ' test1.arr.items[18]:=v;',
  16802. '']);
  16803. ConvertProgram;
  16804. CheckSource('TestExternalClass_BracketAccessor',
  16805. LinesToStr([ // statements
  16806. 'this.DoIt = function (vI, vJ, vK, vL) {',
  16807. '};',
  16808. 'this.Arr = null;',
  16809. 'this.s = "";',
  16810. 'this.i = 0;',
  16811. 'this.v = undefined;',
  16812. '']),
  16813. LinesToStr([ // $mod.$main
  16814. '$mod.v = $mod.Arr[0];',
  16815. '$mod.v = $mod.Arr[1];',
  16816. '$mod.Arr[2] = $mod.s;',
  16817. '$mod.Arr[3] = $mod.s;',
  16818. '$mod.Arr[4] = $mod.i;',
  16819. '$mod.Arr[5] = $mod.Arr[6];',
  16820. '$mod.Arr[7] = $mod.Arr[8];',
  16821. 'var $with1 = $mod.Arr;',
  16822. '$with1[9] = $with1[10];',
  16823. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  16824. ' a: 9,',
  16825. ' p: $mod.Arr,',
  16826. ' get: function () {',
  16827. ' return this.p[this.a];',
  16828. ' },',
  16829. ' set: function (v) {',
  16830. ' this.p[this.a] = v;',
  16831. ' }',
  16832. '}, {',
  16833. ' a: 10,',
  16834. ' p: $mod.Arr,',
  16835. ' get: function () {',
  16836. ' return this.p[this.a];',
  16837. ' },',
  16838. ' set: function (v) {',
  16839. ' this.p[this.a] = v;',
  16840. ' }',
  16841. '});',
  16842. 'var $with2 = $mod.Arr;',
  16843. '$mod.v = $with2[14];',
  16844. '$with2[15] = 16;',
  16845. '$mod.v = $mod.Arr[17];',
  16846. '$mod.Arr[18] = $mod.v;',
  16847. '']));
  16848. end;
  16849. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  16850. begin
  16851. StartProgram(false);
  16852. Add([
  16853. '{$modeswitch externalclass}',
  16854. 'type',
  16855. ' TJSArray = class external name ''Array2''',
  16856. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  16857. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  16858. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  16859. ' end;',
  16860. ' TMyArr = class(TJSArray)',
  16861. ' procedure DoIt;',
  16862. ' end;',
  16863. 'procedure tmyarr.DoIt;',
  16864. 'begin',
  16865. ' Items[1]:=Items[2];',
  16866. ' SetItems(3,getItems(4));',
  16867. 'end;',
  16868. 'var',
  16869. ' Arr: tmyarr;',
  16870. ' s: string;',
  16871. ' i: longint;',
  16872. ' v: jsvalue;',
  16873. 'begin',
  16874. ' v:=arr[0];',
  16875. ' v:=arr.items[1];',
  16876. ' arr[2]:=s;',
  16877. ' arr.items[3]:=s;',
  16878. ' arr[4]:=i;',
  16879. ' arr[5]:=arr[6];',
  16880. ' arr.items[7]:=arr.items[8];',
  16881. ' with arr do items[9]:=items[10];',
  16882. ' with arr do begin',
  16883. ' v:=GetItems(14);',
  16884. ' setitems(15,16);',
  16885. ' end;',
  16886. '']);
  16887. ConvertProgram;
  16888. CheckSource('TestExternalClass_BracketAccessor_Call',
  16889. LinesToStr([ // statements
  16890. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  16891. ' this.$init = function () {',
  16892. ' };',
  16893. ' this.$final = function () {',
  16894. ' };',
  16895. ' this.DoIt = function () {',
  16896. ' this[1] = this[2];',
  16897. ' this[3] = this[4];',
  16898. ' };',
  16899. '});',
  16900. 'this.Arr = null;',
  16901. 'this.s = "";',
  16902. 'this.i = 0;',
  16903. 'this.v = undefined;',
  16904. '']),
  16905. LinesToStr([ // $mod.$main
  16906. '$mod.v = $mod.Arr[0];',
  16907. '$mod.v = $mod.Arr[1];',
  16908. '$mod.Arr[2] = $mod.s;',
  16909. '$mod.Arr[3] = $mod.s;',
  16910. '$mod.Arr[4] = $mod.i;',
  16911. '$mod.Arr[5] = $mod.Arr[6];',
  16912. '$mod.Arr[7] = $mod.Arr[8];',
  16913. 'var $with1 = $mod.Arr;',
  16914. '$with1[9] = $with1[10];',
  16915. 'var $with2 = $mod.Arr;',
  16916. '$mod.v = $with2[14];',
  16917. '$with2[15] = 16;',
  16918. '']));
  16919. end;
  16920. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  16921. begin
  16922. StartProgram(false);
  16923. Add('{$modeswitch externalclass}');
  16924. Add('type');
  16925. Add(' TJSArray = class external name ''Array2''');
  16926. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  16927. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  16928. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  16929. Add(' end;');
  16930. Add('begin');
  16931. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  16932. nBracketAccessorOfExternalClassMustHaveOneParameter);
  16933. ConvertProgram;
  16934. end;
  16935. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  16936. begin
  16937. StartProgram(false);
  16938. Add('{$modeswitch externalclass}');
  16939. Add('type');
  16940. Add(' TJSArray = class external name ''Array2''');
  16941. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  16942. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  16943. Add(' end;');
  16944. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  16945. Add('begin end;');
  16946. Add('var');
  16947. Add(' Arr: tjsarray;');
  16948. Add(' v: jsvalue;');
  16949. Add('begin');
  16950. Add(' v:=arr[0];');
  16951. Add(' v:=arr.items[1];');
  16952. Add(' with arr do v:=items[2];');
  16953. Add(' doit(arr[3],arr[4]);');
  16954. ConvertProgram;
  16955. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  16956. LinesToStr([ // statements
  16957. 'this.DoIt = function (vI, vJ) {',
  16958. '};',
  16959. 'this.Arr = null;',
  16960. 'this.v = undefined;',
  16961. '']),
  16962. LinesToStr([ // $mod.$main
  16963. '$mod.v = $mod.Arr[0];',
  16964. '$mod.v = $mod.Arr[1];',
  16965. 'var $with1 = $mod.Arr;',
  16966. '$mod.v = $with1[2];',
  16967. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  16968. '']));
  16969. end;
  16970. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  16971. begin
  16972. StartProgram(false);
  16973. Add('{$modeswitch externalclass}');
  16974. Add('type');
  16975. Add(' TJSArray = class external name ''Array2''');
  16976. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  16977. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  16978. Add(' end;');
  16979. Add('var');
  16980. Add(' Arr: tjsarray;');
  16981. Add(' s: string;');
  16982. Add(' i: longint;');
  16983. Add(' v: jsvalue;');
  16984. Add('begin');
  16985. Add(' arr[2]:=s;');
  16986. Add(' arr.items[3]:=s;');
  16987. Add(' arr[4]:=i;');
  16988. Add(' with arr do items[5]:=i;');
  16989. ConvertProgram;
  16990. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  16991. LinesToStr([ // statements
  16992. 'this.Arr = null;',
  16993. 'this.s = "";',
  16994. 'this.i = 0;',
  16995. 'this.v = undefined;',
  16996. '']),
  16997. LinesToStr([ // $mod.$main
  16998. '$mod.Arr[2] = $mod.s;',
  16999. '$mod.Arr[3] = $mod.s;',
  17000. '$mod.Arr[4] = $mod.i;',
  17001. 'var $with1 = $mod.Arr;',
  17002. '$with1[5] = $mod.i;',
  17003. '']));
  17004. end;
  17005. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  17006. begin
  17007. StartProgram(false);
  17008. Add('{$modeswitch externalclass}');
  17009. Add('type');
  17010. Add(' TJSArray = class external name ''Array2''');
  17011. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17012. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17013. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  17014. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  17015. Add(' end;');
  17016. Add('var');
  17017. Add(' Arr: tjsarray;');
  17018. Add(' s: string;');
  17019. Add(' i: longint;');
  17020. Add(' v: jsvalue;');
  17021. Add('begin');
  17022. Add(' arr[2]:=s;');
  17023. Add(' arr.items[3]:=s;');
  17024. Add(' arr.numbers[4]:=i;');
  17025. Add(' with arr do items[5]:=i;');
  17026. Add(' with arr do numbers[6]:=i;');
  17027. ConvertProgram;
  17028. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  17029. LinesToStr([ // statements
  17030. 'this.Arr = null;',
  17031. 'this.s = "";',
  17032. 'this.i = 0;',
  17033. 'this.v = undefined;',
  17034. '']),
  17035. LinesToStr([ // $mod.$main
  17036. '$mod.Arr[2] = $mod.s;',
  17037. '$mod.Arr[3] = $mod.s;',
  17038. '$mod.Arr[4] = $mod.i;',
  17039. 'var $with1 = $mod.Arr;',
  17040. '$with1[5] = $mod.i;',
  17041. 'var $with2 = $mod.Arr;',
  17042. '$with2[6] = $mod.i;',
  17043. '']));
  17044. end;
  17045. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  17046. begin
  17047. StartProgram(false);
  17048. Add('{$modeswitch externalclass}');
  17049. Add('type');
  17050. Add(' TJSArray = class external name ''Array2''');
  17051. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17052. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17053. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  17054. Add(' end;');
  17055. Add('var');
  17056. Add(' Arr: tjsarray;');
  17057. Add(' i: longint;');
  17058. Add(' IntArr: array of longint;');
  17059. Add(' v: jsvalue;');
  17060. Add('begin');
  17061. Add(' v:=arr.items[i];');
  17062. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  17063. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  17064. ConvertProgram;
  17065. CheckSource('TestExternalClass_BracketAccessor_Index',
  17066. LinesToStr([ // statements
  17067. 'this.Arr = null;',
  17068. 'this.i = 0;',
  17069. 'this.IntArr = [];',
  17070. 'this.v = undefined;',
  17071. '']),
  17072. LinesToStr([ // $mod.$main
  17073. '$mod.v = $mod.Arr[$mod.i];',
  17074. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  17075. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  17076. '']));
  17077. end;
  17078. procedure TTestModule.TestExternalClass_ForInJSObject;
  17079. begin
  17080. StartProgram(false);
  17081. Add([
  17082. '{$modeswitch externalclass}',
  17083. 'type',
  17084. ' TJSObject = class external name ''Object''',
  17085. ' end;',
  17086. 'var',
  17087. ' o: TJSObject;',
  17088. ' key: string;',
  17089. 'begin',
  17090. ' for key in o do',
  17091. ' if key=''abc'' then ;',
  17092. '']);
  17093. ConvertProgram;
  17094. CheckSource('TestExternalClass_ForInJSObject',
  17095. LinesToStr([ // statements
  17096. 'this.o = null;',
  17097. 'this.key = "";',
  17098. '']),
  17099. LinesToStr([ // $mod.$main
  17100. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  17101. '']));
  17102. end;
  17103. procedure TTestModule.TestExternalClass_ForInJSArray;
  17104. begin
  17105. StartProgram(false);
  17106. Add([
  17107. '{$modeswitch externalclass}',
  17108. 'type',
  17109. ' TJSInt8Array = class external name ''Int8Array''',
  17110. ' private',
  17111. ' flength: NativeInt external name ''length'';',
  17112. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  17113. ' public',
  17114. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  17115. ' property Length: NativeInt read flength;',
  17116. ' end;',
  17117. 'var',
  17118. ' a: TJSInt8Array;',
  17119. ' value: shortint;',
  17120. 'begin',
  17121. ' for value in a do',
  17122. ' if value=3 then ;',
  17123. '']);
  17124. ConvertProgram;
  17125. CheckSource('TestExternalClass_ForInJSArray',
  17126. LinesToStr([ // statements
  17127. 'this.a = null;',
  17128. 'this.value = 0;',
  17129. '']),
  17130. LinesToStr([ // $mod.$main
  17131. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  17132. ' $mod.value = $in1[$l2];',
  17133. ' if ($mod.value === 3) ;',
  17134. '};',
  17135. '']));
  17136. end;
  17137. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  17138. begin
  17139. AddModuleWithIntfImplSrc('unit2.pas',
  17140. LinesToStr([
  17141. '{$modeswitch externalclass}',
  17142. 'type',
  17143. ' TJSBufferSource = class external name ''BufferSource''',
  17144. ' end;',
  17145. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  17146. '']),
  17147. '');
  17148. AddModuleWithIntfImplSrc('unit3.pas',
  17149. LinesToStr([
  17150. '{$modeswitch externalclass}',
  17151. 'type',
  17152. ' TJSBufferSource = class external name ''BufferSource''',
  17153. ' end;',
  17154. '']),
  17155. '');
  17156. StartUnit(true);
  17157. Add([
  17158. 'interface',
  17159. 'uses unit2, unit3;',
  17160. 'procedure DoSome(s: TJSBufferSource);',
  17161. 'implementation',
  17162. 'procedure DoSome(s: TJSBufferSource);',
  17163. 'begin',
  17164. ' DoIt(s);',
  17165. 'end;',
  17166. '']);
  17167. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  17168. nIncompatibleTypeArgNo);
  17169. ConvertUnit;
  17170. end;
  17171. procedure TTestModule.TestClassInterface_Corba;
  17172. begin
  17173. StartProgram(false);
  17174. Add([
  17175. '{$interfaces corba}',
  17176. 'type',
  17177. ' IUnknown = interface;',
  17178. ' IUnknown = interface',
  17179. ' [''{00000000-0000-0000-C000-000000000046}'']',
  17180. ' end;',
  17181. ' IInterface = IUnknown;',
  17182. ' IBird = interface(IInterface)',
  17183. ' function GetSize: longint;',
  17184. ' procedure SetSize(i: longint);',
  17185. ' property Size: longint read GetSize write SetSize;',
  17186. ' procedure DoIt(i: longint);',
  17187. ' end;',
  17188. ' TObject = class',
  17189. ' end;',
  17190. ' TBird = class(TObject,IBird)',
  17191. ' function GetSize: longint; virtual; abstract;',
  17192. ' procedure SetSize(i: longint); virtual; abstract;',
  17193. ' procedure DoIt(i: longint); virtual; abstract;',
  17194. ' end;',
  17195. 'var',
  17196. ' BirdIntf: IBird;',
  17197. 'begin',
  17198. ' BirdIntf.Size:=BirdIntf.Size;',
  17199. '']);
  17200. ConvertProgram;
  17201. CheckSource('TestClassInterface_Corba',
  17202. LinesToStr([ // statements
  17203. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  17204. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  17205. 'rtl.createClass($mod, "TObject", null, function () {',
  17206. ' this.$init = function () {',
  17207. ' };',
  17208. ' this.$final = function () {',
  17209. ' };',
  17210. '});',
  17211. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17212. ' rtl.addIntf(this, $mod.IBird);',
  17213. '});',
  17214. 'this.BirdIntf = null;',
  17215. '']),
  17216. LinesToStr([ // $mod.$main
  17217. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  17218. '']));
  17219. end;
  17220. procedure TTestModule.TestClassInterface_ProcExternalFail;
  17221. begin
  17222. StartProgram(false);
  17223. Add([
  17224. '{$interfaces corba}',
  17225. 'type',
  17226. ' IUnknown = interface',
  17227. ' procedure DoIt; external name ''foo'';',
  17228. ' end;',
  17229. 'begin']);
  17230. SetExpectedParserError(
  17231. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  17232. nParserNoFieldsAllowed);
  17233. ConvertProgram;
  17234. end;
  17235. procedure TTestModule.TestClassInterface_Overloads;
  17236. begin
  17237. StartProgram(false);
  17238. Add([
  17239. '{$interfaces corba}',
  17240. 'type',
  17241. ' integer = longint;',
  17242. ' IUnknown = interface',
  17243. ' procedure DoIt(i: integer);',
  17244. ' procedure DoIt(s: string);',
  17245. ' end;',
  17246. ' IBird = interface(IUnknown)',
  17247. ' procedure DoIt(b: boolean); overload;',
  17248. ' end;',
  17249. ' TObject = class',
  17250. ' end;',
  17251. ' TBird = class(TObject,IBird)',
  17252. ' procedure DoIt(o: TObject);',
  17253. ' procedure DoIt(s: string);',
  17254. ' procedure DoIt(i: integer);',
  17255. ' procedure DoIt(b: boolean);',
  17256. ' end;',
  17257. 'procedure TBird.DoIt(o: TObject); begin end;',
  17258. 'procedure TBird.DoIt(s: string); begin end;',
  17259. 'procedure TBird.DoIt(i: integer); begin end;',
  17260. 'procedure TBird.DoIt(b: boolean); begin end;',
  17261. 'var',
  17262. ' BirdIntf: IBird;',
  17263. 'begin',
  17264. ' BirdIntf.DoIt(3);',
  17265. ' BirdIntf.DoIt(''abc'');',
  17266. ' BirdIntf.DoIt(true);',
  17267. '']);
  17268. ConvertProgram;
  17269. CheckSource('TestClassInterface_Overloads',
  17270. LinesToStr([ // statements
  17271. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  17272. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  17273. 'rtl.createClass($mod, "TObject", null, function () {',
  17274. ' this.$init = function () {',
  17275. ' };',
  17276. ' this.$final = function () {',
  17277. ' };',
  17278. '});',
  17279. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17280. ' this.DoIt = function (o) {',
  17281. ' };',
  17282. ' this.DoIt$1 = function (s) {',
  17283. ' };',
  17284. ' this.DoIt$2 = function (i) {',
  17285. ' };',
  17286. ' this.DoIt$3 = function (b) {',
  17287. ' };',
  17288. ' rtl.addIntf(this, $mod.IBird, {',
  17289. ' DoIt$2: "DoIt$3",',
  17290. ' DoIt: "DoIt$2"',
  17291. ' });',
  17292. '});',
  17293. 'this.BirdIntf = null;',
  17294. '']),
  17295. LinesToStr([ // $mod.$main
  17296. '$mod.BirdIntf.DoIt(3);',
  17297. '$mod.BirdIntf.DoIt$1("abc");',
  17298. '$mod.BirdIntf.DoIt$2(true);',
  17299. '']));
  17300. end;
  17301. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  17302. begin
  17303. StartProgram(false);
  17304. Add([
  17305. '{$interfaces corba}',
  17306. 'type',
  17307. ' IBird = interface',
  17308. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17309. ' end;',
  17310. ' IDog = interface',
  17311. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17312. ' end;',
  17313. ' TObject = class(IBird,IDog)',
  17314. ' end;',
  17315. 'begin']);
  17316. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  17317. nDuplicateGUIDXInYZ);
  17318. ConvertProgram;
  17319. end;
  17320. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  17321. begin
  17322. StartProgram(false);
  17323. Add([
  17324. '{$interfaces corba}',
  17325. 'type',
  17326. ' IAnimal = interface',
  17327. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17328. ' end;',
  17329. ' IBird = interface(IAnimal)',
  17330. ' end;',
  17331. ' IHawk = interface(IBird)',
  17332. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  17333. ' end;',
  17334. 'begin']);
  17335. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  17336. nDuplicateGUIDXInYZ);
  17337. ConvertProgram;
  17338. end;
  17339. procedure TTestModule.TestClassInterface_AncestorImpl;
  17340. begin
  17341. StartProgram(false);
  17342. Add([
  17343. '{$interfaces corba}',
  17344. 'type',
  17345. ' integer = longint;',
  17346. ' IUnknown = interface',
  17347. ' procedure DoIt(i: integer);',
  17348. ' end;',
  17349. ' IBird = interface',
  17350. ' procedure Fly(i: integer);',
  17351. ' end;',
  17352. ' TObject = class(IUnknown)',
  17353. ' procedure DoIt(i: integer);',
  17354. ' end;',
  17355. ' TBird = class(IBird)',
  17356. ' procedure Fly(i: integer);',
  17357. ' end;',
  17358. 'procedure TObject.DoIt(i: integer); begin end;',
  17359. 'procedure TBird.Fly(i: integer); begin end;',
  17360. 'begin',
  17361. '']);
  17362. ConvertProgram;
  17363. CheckSource('TestClassInterface_AncestorIntf',
  17364. LinesToStr([ // statements
  17365. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  17366. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  17367. 'rtl.createClass($mod, "TObject", null, function () {',
  17368. ' this.$init = function () {',
  17369. ' };',
  17370. ' this.$final = function () {',
  17371. ' };',
  17372. ' this.DoIt = function (i) {',
  17373. ' };',
  17374. ' rtl.addIntf(this, $mod.IUnknown);',
  17375. '});',
  17376. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17377. ' this.Fly = function (i) {',
  17378. ' };',
  17379. ' rtl.addIntf(this, $mod.IBird);',
  17380. ' rtl.addIntf(this, $mod.IUnknown);',
  17381. '});',
  17382. '']),
  17383. LinesToStr([ // $mod.$main
  17384. '']));
  17385. end;
  17386. procedure TTestModule.TestClassInterface_ImplReintroduce;
  17387. begin
  17388. StartProgram(false);
  17389. Add([
  17390. '{$interfaces corba}',
  17391. 'type',
  17392. ' integer = longint;',
  17393. ' IBird = interface',
  17394. ' procedure DoIt(i: integer);',
  17395. ' end;',
  17396. ' TObject = class',
  17397. ' procedure DoIt(i: integer);',
  17398. ' end;',
  17399. ' TBird = class(IBird)',
  17400. ' procedure DoIt(i: integer); virtual; reintroduce;',
  17401. ' end;',
  17402. 'procedure TObject.DoIt(i: integer); begin end;',
  17403. 'procedure TBird.DoIt(i: integer); begin end;',
  17404. 'begin',
  17405. '']);
  17406. ConvertProgram;
  17407. CheckSource('TestClassInterface_ImplReintroduce',
  17408. LinesToStr([ // statements
  17409. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  17410. 'rtl.createClass($mod, "TObject", null, function () {',
  17411. ' this.$init = function () {',
  17412. ' };',
  17413. ' this.$final = function () {',
  17414. ' };',
  17415. ' this.DoIt = function (i) {',
  17416. ' };',
  17417. '});',
  17418. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17419. ' this.DoIt$1 = function (i) {',
  17420. ' };',
  17421. ' rtl.addIntf(this, $mod.IBird, {',
  17422. ' DoIt: "DoIt$1"',
  17423. ' });',
  17424. '});',
  17425. '']),
  17426. LinesToStr([ // $mod.$main
  17427. '']));
  17428. end;
  17429. procedure TTestModule.TestClassInterface_MethodResolution;
  17430. begin
  17431. StartProgram(false);
  17432. Add([
  17433. '{$interfaces corba}',
  17434. 'type',
  17435. ' IUnknown = interface',
  17436. ' procedure Walk(i: longint);',
  17437. ' end;',
  17438. ' IBird = interface(IUnknown)',
  17439. ' procedure Walk(b: boolean); overload;',
  17440. ' procedure Fly(s: string);',
  17441. ' end;',
  17442. ' TObject = class',
  17443. ' end;',
  17444. ' TBird = class(TObject,IBird)',
  17445. ' procedure IBird.Fly = Move;',
  17446. ' procedure IBird.Walk = Hop;',
  17447. ' procedure Hop(i: longint);',
  17448. ' procedure Move(s: string);',
  17449. ' procedure Hop(b: boolean);',
  17450. ' end;',
  17451. 'procedure TBird.Move(s: string); begin end;',
  17452. 'procedure TBird.Hop(i: longint); begin end;',
  17453. 'procedure TBird.Hop(b: boolean); begin end;',
  17454. 'var',
  17455. ' BirdIntf: IBird;',
  17456. 'begin',
  17457. ' BirdIntf.Walk(3);',
  17458. ' BirdIntf.Walk(true);',
  17459. ' BirdIntf.Fly(''abc'');',
  17460. '']);
  17461. ConvertProgram;
  17462. CheckSource('TestClassInterface_MethodResolution',
  17463. LinesToStr([ // statements
  17464. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  17465. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  17466. 'rtl.createClass($mod, "TObject", null, function () {',
  17467. ' this.$init = function () {',
  17468. ' };',
  17469. ' this.$final = function () {',
  17470. ' };',
  17471. '});',
  17472. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17473. ' this.Hop = function (i) {',
  17474. ' };',
  17475. ' this.Move = function (s) {',
  17476. ' };',
  17477. ' this.Hop$1 = function (b) {',
  17478. ' };',
  17479. ' rtl.addIntf(this, $mod.IBird, {',
  17480. ' Walk$1: "Hop$1",',
  17481. ' Fly: "Move",',
  17482. ' Walk: "Hop"',
  17483. ' });',
  17484. '});',
  17485. 'this.BirdIntf = null;',
  17486. '']),
  17487. LinesToStr([ // $mod.$main
  17488. '$mod.BirdIntf.Walk(3);',
  17489. '$mod.BirdIntf.Walk$1(true);',
  17490. '$mod.BirdIntf.Fly("abc");',
  17491. '']));
  17492. end;
  17493. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  17494. begin
  17495. StartProgram(false);
  17496. Add([
  17497. '{$interfaces com}',
  17498. 'type',
  17499. ' IUnknown = interface',
  17500. ' function _AddRef: longint;',
  17501. ' procedure Walk;',
  17502. ' end;',
  17503. ' IBird = interface end;',
  17504. ' IDog = interface end;',
  17505. ' TObject = class(IBird,IDog)',
  17506. ' function _AddRef: longint; virtual; abstract;',
  17507. ' procedure Walk; virtual; abstract;',
  17508. ' end;',
  17509. ' TBird = class(IUnknown)',
  17510. ' end;',
  17511. 'begin',
  17512. '']);
  17513. ConvertProgram;
  17514. CheckSource('TestClassInterface_COM_AncestorLess',
  17515. LinesToStr([ // statements
  17516. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  17517. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  17518. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  17519. 'rtl.createClass($mod, "TObject", null, function () {',
  17520. ' this.$init = function () {',
  17521. ' };',
  17522. ' this.$final = function () {',
  17523. ' };',
  17524. ' rtl.addIntf(this, $mod.IBird);',
  17525. ' rtl.addIntf(this, $mod.IDog);',
  17526. '});',
  17527. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17528. ' rtl.addIntf(this, $mod.IUnknown);',
  17529. ' rtl.addIntf(this, $mod.IBird);',
  17530. ' rtl.addIntf(this, $mod.IDog);',
  17531. '});',
  17532. '']),
  17533. LinesToStr([ // $mod.$main
  17534. '']));
  17535. end;
  17536. procedure TTestModule.TestClassInterface_MethodOverride;
  17537. begin
  17538. StartProgram(false);
  17539. Add([
  17540. '{$interfaces corba}',
  17541. 'type',
  17542. ' IUnknown = interface',
  17543. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  17544. ' procedure Go;',
  17545. ' end;',
  17546. ' TObject = class(IUnknown)',
  17547. ' procedure Go; virtual; abstract;',
  17548. ' end;',
  17549. ' TBird = class',
  17550. ' procedure Go; override;',
  17551. ' end;',
  17552. ' TCat = class(TObject)',
  17553. ' procedure Go; override;',
  17554. ' end;',
  17555. ' TDog = class(TObject, IUnknown)',
  17556. ' procedure Go; override;',
  17557. ' end;',
  17558. 'procedure TBird.Go; begin end;',
  17559. 'procedure TCat.Go; begin end;',
  17560. 'procedure TDog.Go; begin end;',
  17561. 'begin',
  17562. '']);
  17563. ConvertProgram;
  17564. CheckSource('TestClassInterface_MethodOverride',
  17565. LinesToStr([ // statements
  17566. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  17567. 'rtl.createClass($mod, "TObject", null, function () {',
  17568. ' this.$init = function () {',
  17569. ' };',
  17570. ' this.$final = function () {',
  17571. ' };',
  17572. ' rtl.addIntf(this, $mod.IUnknown);',
  17573. '});',
  17574. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17575. ' this.Go = function () {',
  17576. ' };',
  17577. ' rtl.addIntf(this, $mod.IUnknown);',
  17578. '});',
  17579. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  17580. ' this.Go = function () {',
  17581. ' };',
  17582. ' rtl.addIntf(this, $mod.IUnknown);',
  17583. '});',
  17584. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  17585. ' this.Go = function () {',
  17586. ' };',
  17587. ' rtl.addIntf(this, $mod.IUnknown);',
  17588. '});',
  17589. '']),
  17590. LinesToStr([ // $mod.$main
  17591. '']));
  17592. end;
  17593. procedure TTestModule.TestClassInterface_Corba_Delegation;
  17594. begin
  17595. StartProgram(false);
  17596. Add([
  17597. '{$interfaces corba}',
  17598. 'type',
  17599. ' IUnknown = interface',
  17600. ' end;',
  17601. ' IBird = interface(IUnknown)',
  17602. ' procedure Fly(s: string);',
  17603. ' end;',
  17604. ' IEagle = interface(IBird)',
  17605. ' end;',
  17606. ' IDove = interface(IBird)',
  17607. ' end;',
  17608. ' ISwallow = interface(IBird)',
  17609. ' end;',
  17610. ' TObject = class',
  17611. ' end;',
  17612. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17613. ' procedure Fly(s: string); virtual; abstract;',
  17614. ' end;',
  17615. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17616. ' FBirdIntf: IBird;',
  17617. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  17618. ' function GetEagleIntf: IEagle; virtual; abstract;',
  17619. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17620. ' FDoveObj: TBird;',
  17621. ' property DoveObj: TBird read FDoveObj implements IDove;',
  17622. ' function GetSwallowObj: TBird; virtual; abstract;',
  17623. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17624. ' end;',
  17625. 'begin',
  17626. '']);
  17627. ConvertProgram;
  17628. CheckSource('TestClassInterface_Delegation',
  17629. LinesToStr([ // statements
  17630. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17631. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17632. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17633. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17634. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17635. 'rtl.createClass($mod, "TObject", null, function () {',
  17636. ' this.$init = function () {',
  17637. ' };',
  17638. ' this.$final = function () {',
  17639. ' };',
  17640. '});',
  17641. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17642. ' rtl.addIntf(this, $mod.IBird);',
  17643. ' rtl.addIntf(this, $mod.IEagle);',
  17644. ' rtl.addIntf(this, $mod.IDove);',
  17645. ' rtl.addIntf(this, $mod.ISwallow);',
  17646. '});',
  17647. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17648. ' this.$init = function () {',
  17649. ' $mod.TObject.$init.call(this);',
  17650. ' this.FBirdIntf = null;',
  17651. ' this.FDoveObj = null;',
  17652. ' };',
  17653. ' this.$final = function () {',
  17654. ' this.FBirdIntf = undefined;',
  17655. ' this.FDoveObj = undefined;',
  17656. ' $mod.TObject.$final.call(this);',
  17657. ' };',
  17658. ' this.$intfmaps = {',
  17659. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17660. ' return this.FBirdIntf;',
  17661. ' },',
  17662. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17663. ' return this.GetEagleIntf();',
  17664. ' },',
  17665. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17666. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17667. ' },',
  17668. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17669. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17670. ' }',
  17671. ' };',
  17672. '});',
  17673. '']),
  17674. LinesToStr([ // $mod.$main
  17675. '']));
  17676. end;
  17677. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  17678. begin
  17679. StartProgram(false);
  17680. Add([
  17681. '{$interfaces corba}',
  17682. 'type',
  17683. ' IUnknown = interface',
  17684. ' end;',
  17685. ' IBird = interface(IUnknown)',
  17686. ' procedure Fly(s: string);',
  17687. ' end;',
  17688. ' IEagle = interface(IBird)',
  17689. ' end;',
  17690. ' IDove = interface(IBird)',
  17691. ' end;',
  17692. ' ISwallow = interface(IBird)',
  17693. ' end;',
  17694. ' TObject = class',
  17695. ' end;',
  17696. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17697. ' procedure Fly(s: string); virtual; abstract;',
  17698. ' end;',
  17699. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17700. ' private',
  17701. ' class var FBirdIntf: IBird;',
  17702. ' class var FDoveObj: TBird;',
  17703. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  17704. ' class function GetSwallowObj: TBird; virtual; abstract;',
  17705. ' protected',
  17706. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  17707. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17708. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  17709. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17710. ' end;',
  17711. 'begin',
  17712. '']);
  17713. ConvertProgram;
  17714. CheckSource('TestClassInterface_DelegationStatic',
  17715. LinesToStr([ // statements
  17716. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17717. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  17718. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  17719. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  17720. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  17721. 'rtl.createClass($mod, "TObject", null, function () {',
  17722. ' this.$init = function () {',
  17723. ' };',
  17724. ' this.$final = function () {',
  17725. ' };',
  17726. '});',
  17727. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17728. ' rtl.addIntf(this, $mod.IBird);',
  17729. ' rtl.addIntf(this, $mod.IEagle);',
  17730. ' rtl.addIntf(this, $mod.IDove);',
  17731. ' rtl.addIntf(this, $mod.ISwallow);',
  17732. '});',
  17733. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17734. ' this.FBirdIntf = null;',
  17735. ' this.FDoveObj = null;',
  17736. ' this.$intfmaps = {',
  17737. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  17738. ' return this.FBirdIntf;',
  17739. ' },',
  17740. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  17741. ' return this.GetEagleIntf();',
  17742. ' },',
  17743. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  17744. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  17745. ' },',
  17746. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  17747. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17748. ' }',
  17749. ' };',
  17750. '});',
  17751. '']),
  17752. LinesToStr([ // $mod.$main
  17753. '']));
  17754. end;
  17755. procedure TTestModule.TestClassInterface_Corba_Operators;
  17756. begin
  17757. StartProgram(false);
  17758. Add([
  17759. '{$interfaces corba}',
  17760. 'type',
  17761. ' IUnknown = interface',
  17762. ' end;',
  17763. ' IBird = interface(IUnknown)',
  17764. ' function GetItems(Index: longint): longint;',
  17765. ' procedure SetItems(Index: longint; Value: longint);',
  17766. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  17767. ' end;',
  17768. ' TObject = class',
  17769. ' end;',
  17770. ' TBird = class(TObject,IBird)',
  17771. ' function GetItems(Index: longint): longint; virtual; abstract;',
  17772. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  17773. ' end;',
  17774. 'var',
  17775. ' IntfVar: IBird = nil;',
  17776. ' IntfVar2: IBird;',
  17777. ' ObjVar: TBird;',
  17778. ' v: JSValue;',
  17779. 'begin',
  17780. ' IntfVar:=nil;',
  17781. ' IntfVar[3]:=IntfVar[4];',
  17782. ' if Assigned(IntfVar) then ;',
  17783. ' IntfVar:=IntfVar2;',
  17784. ' IntfVar:=ObjVar;',
  17785. ' if IntfVar=IntfVar2 then ;',
  17786. ' if IntfVar<>IntfVar2 then ;',
  17787. ' if IntfVar is IBird then ;',
  17788. ' if IntfVar is TBird then ;',
  17789. ' if ObjVar is IBird then ;',
  17790. ' IntfVar:=IntfVar2 as IBird;',
  17791. ' ObjVar:=IntfVar2 as TBird;',
  17792. ' IntfVar:=ObjVar as IBird;',
  17793. ' IntfVar:=IBird(IntfVar2);',
  17794. ' ObjVar:=TBird(IntfVar);',
  17795. ' IntfVar:=IBird(ObjVar);',
  17796. ' v:=IntfVar;',
  17797. ' IntfVar:=IBird(v);',
  17798. ' if v is IBird then ;',
  17799. ' v:=JSValue(IntfVar);',
  17800. ' v:=IBird;',
  17801. '']);
  17802. ConvertProgram;
  17803. CheckSource('TestClassInterface_Corba_Operators',
  17804. LinesToStr([ // statements
  17805. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17806. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  17807. 'rtl.createClass($mod, "TObject", null, function () {',
  17808. ' this.$init = function () {',
  17809. ' };',
  17810. ' this.$final = function () {',
  17811. ' };',
  17812. '});',
  17813. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17814. ' rtl.addIntf(this, $mod.IBird);',
  17815. '});',
  17816. 'this.IntfVar = null;',
  17817. 'this.IntfVar2 = null;',
  17818. 'this.ObjVar = null;',
  17819. 'this.v = undefined;',
  17820. '']),
  17821. LinesToStr([ // $mod.$main
  17822. '$mod.IntfVar = null;',
  17823. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  17824. 'if ($mod.IntfVar != null) ;',
  17825. '$mod.IntfVar = $mod.IntfVar2;',
  17826. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  17827. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  17828. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  17829. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  17830. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  17831. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  17832. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  17833. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  17834. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17835. '$mod.IntfVar = $mod.IntfVar2;',
  17836. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  17837. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  17838. '$mod.v = $mod.IntfVar;',
  17839. '$mod.IntfVar = rtl.getObject($mod.v);',
  17840. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  17841. '$mod.v = $mod.IntfVar;',
  17842. '$mod.v = $mod.IBird;',
  17843. '']));
  17844. end;
  17845. procedure TTestModule.TestClassInterface_Corba_Args;
  17846. begin
  17847. StartProgram(false);
  17848. Add([
  17849. '{$interfaces corba}',
  17850. 'type',
  17851. ' IUnknown = interface',
  17852. ' end;',
  17853. ' IBird = interface(IUnknown)',
  17854. ' end;',
  17855. ' TObject = class',
  17856. ' end;',
  17857. ' TBird = class(TObject,IBird)',
  17858. ' end;',
  17859. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  17860. 'begin',
  17861. ' DoIt(i,i,i);',
  17862. 'end;',
  17863. 'procedure Change(var i: IBird; out j: IBird);',
  17864. 'begin',
  17865. ' DoIt(i,i,i);',
  17866. ' Change(i,i);',
  17867. 'end;',
  17868. 'var',
  17869. ' i: IBird;',
  17870. ' o: TBird;',
  17871. 'begin',
  17872. ' DoIt(i,i,i);',
  17873. ' Change(i,i);',
  17874. ' DoIt(o,o,o);',
  17875. '']);
  17876. ConvertProgram;
  17877. CheckSource('TestClassInterface_Corba_Args',
  17878. LinesToStr([ // statements
  17879. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17880. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  17881. 'rtl.createClass($mod, "TObject", null, function () {',
  17882. ' this.$init = function () {',
  17883. ' };',
  17884. ' this.$final = function () {',
  17885. ' };',
  17886. '});',
  17887. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17888. ' rtl.addIntf(this, $mod.IBird);',
  17889. '});',
  17890. 'this.DoIt = function (u, i, j) {',
  17891. ' $mod.DoIt({',
  17892. ' get: function () {',
  17893. ' return i;',
  17894. ' },',
  17895. ' set: function (v) {',
  17896. ' i = v;',
  17897. ' }',
  17898. ' }, i, i);',
  17899. '};',
  17900. 'this.Change = function (i, j) {',
  17901. ' $mod.DoIt(i, i.get(), i.get());',
  17902. ' $mod.Change(i, i);',
  17903. '};',
  17904. 'this.i = null;',
  17905. 'this.o = null;',
  17906. '']),
  17907. LinesToStr([ // $mod.$main
  17908. '$mod.DoIt({',
  17909. ' p: $mod,',
  17910. ' get: function () {',
  17911. ' return this.p.i;',
  17912. ' },',
  17913. ' set: function (v) {',
  17914. ' this.p.i = v;',
  17915. ' }',
  17916. '}, $mod.i, $mod.i);',
  17917. '$mod.Change({',
  17918. ' p: $mod,',
  17919. ' get: function () {',
  17920. ' return this.p.i;',
  17921. ' },',
  17922. ' set: function (v) {',
  17923. ' this.p.i = v;',
  17924. ' }',
  17925. '}, {',
  17926. ' p: $mod,',
  17927. ' get: function () {',
  17928. ' return this.p.i;',
  17929. ' },',
  17930. ' set: function (v) {',
  17931. ' this.p.i = v;',
  17932. ' }',
  17933. '});',
  17934. '$mod.DoIt({',
  17935. ' p: $mod,',
  17936. ' get: function () {',
  17937. ' return this.p.o;',
  17938. ' },',
  17939. ' set: function (v) {',
  17940. ' this.p.o = v;',
  17941. ' }',
  17942. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  17943. '']));
  17944. end;
  17945. procedure TTestModule.TestClassInterface_Corba_ForIn;
  17946. begin
  17947. StartProgram(false);
  17948. Add([
  17949. '{$interfaces corba}',
  17950. 'type',
  17951. ' IUnknown = interface end;',
  17952. ' TObject = class',
  17953. ' Id: longint;',
  17954. ' end;',
  17955. ' IEnumerator = interface(IUnknown)',
  17956. ' function GetCurrent: TObject;',
  17957. ' function MoveNext: Boolean;',
  17958. ' property Current: TObject read GetCurrent;',
  17959. ' end;',
  17960. ' IEnumerable = interface(IUnknown)',
  17961. ' function GetEnumerator: IEnumerator;',
  17962. ' end;',
  17963. 'var',
  17964. ' o: TObject;',
  17965. ' i: IEnumerable;',
  17966. 'begin',
  17967. ' for o in i do o.Id:=3;',
  17968. '']);
  17969. ConvertProgram;
  17970. CheckSource('TestClassInterface_Corba_ForIn',
  17971. LinesToStr([ // statements
  17972. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17973. 'rtl.createClass($mod, "TObject", null, function () {',
  17974. ' this.$init = function () {',
  17975. ' this.Id = 0;',
  17976. ' };',
  17977. ' this.$final = function () {',
  17978. ' };',
  17979. '});',
  17980. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17981. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17982. 'this.o = null;',
  17983. 'this.i = null;',
  17984. '']),
  17985. LinesToStr([ // $mod.$main
  17986. 'var $in1 = $mod.i.GetEnumerator();',
  17987. 'while ($in1.MoveNext()) {',
  17988. ' $mod.o = $in1.GetCurrent();',
  17989. ' $mod.o.Id = 3;',
  17990. '};',
  17991. '']));
  17992. end;
  17993. procedure TTestModule.TestClassInterface_COM_AssignVar;
  17994. begin
  17995. StartProgram(false);
  17996. Add([
  17997. '{$interfaces com}',
  17998. 'type',
  17999. ' IUnknown = interface',
  18000. ' function _AddRef: longint;',
  18001. ' function _Release: longint;',
  18002. ' end;',
  18003. ' TObject = class(IUnknown)',
  18004. ' function _AddRef: longint; virtual; abstract;',
  18005. ' function _Release: longint; virtual; abstract;',
  18006. ' end;',
  18007. 'var',
  18008. ' i: IUnknown;',
  18009. 'procedure DoGlobal(o: TObject);',
  18010. 'begin',
  18011. ' i:=nil;',
  18012. ' i:=o;',
  18013. ' i:=i;',
  18014. 'end;',
  18015. 'procedure DoLocal(o: TObject);',
  18016. 'const k: IUnknown = nil;',
  18017. 'var j: IUnknown;',
  18018. 'begin',
  18019. ' k:=o;',
  18020. ' k:=i;',
  18021. ' j:=o;',
  18022. ' j:=i;',
  18023. 'end;',
  18024. 'var o: TObject;',
  18025. 'begin',
  18026. ' i:=nil;',
  18027. ' i:=o;',
  18028. '']);
  18029. ConvertProgram;
  18030. CheckSource('TestClassInterface_COM_AssignVar',
  18031. LinesToStr([ // statements
  18032. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18033. 'rtl.createClass($mod, "TObject", null, function () {',
  18034. ' this.$init = function () {',
  18035. ' };',
  18036. ' this.$final = function () {',
  18037. ' };',
  18038. ' rtl.addIntf(this, $mod.IUnknown);',
  18039. '});',
  18040. 'this.i = null;',
  18041. 'this.DoGlobal = function (o) {',
  18042. ' rtl.setIntfP($mod, "i", null);',
  18043. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  18044. ' rtl.setIntfP($mod, "i", $mod.i);',
  18045. '};',
  18046. 'var k = null;',
  18047. 'this.DoLocal = function (o) {',
  18048. ' var j = null;',
  18049. ' try{',
  18050. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18051. ' k = rtl.setIntfL(k, $mod.i);',
  18052. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18053. ' j = rtl.setIntfL(j, $mod.i);',
  18054. ' }finally{',
  18055. ' rtl._Release(j);',
  18056. ' };',
  18057. '};',
  18058. 'this.o = null;',
  18059. '']),
  18060. LinesToStr([ // $mod.$main
  18061. 'rtl.setIntfP($mod, "i", null);',
  18062. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  18063. '']));
  18064. end;
  18065. procedure TTestModule.TestClassInterface_COM_AssignArg;
  18066. begin
  18067. StartProgram(false);
  18068. Add([
  18069. '{$interfaces com}',
  18070. 'type',
  18071. ' IUnknown = interface',
  18072. ' function _AddRef: longint;',
  18073. ' function _Release: longint;',
  18074. ' end;',
  18075. ' TObject = class(IUnknown)',
  18076. ' function _AddRef: longint; virtual; abstract;',
  18077. ' function _Release: longint; virtual; abstract;',
  18078. ' end;',
  18079. 'procedure DoDefault(i, j: IUnknown);',
  18080. 'begin',
  18081. ' i:=nil;',
  18082. ' i:=j;',
  18083. 'end;',
  18084. 'begin',
  18085. '']);
  18086. ConvertProgram;
  18087. CheckSource('TestClassInterface_COM_AssignArg',
  18088. LinesToStr([ // statements
  18089. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18090. 'rtl.createClass($mod, "TObject", null, function () {',
  18091. ' this.$init = function () {',
  18092. ' };',
  18093. ' this.$final = function () {',
  18094. ' };',
  18095. ' rtl.addIntf(this, $mod.IUnknown);',
  18096. '});',
  18097. 'this.DoDefault = function (i, j) {',
  18098. ' rtl._AddRef(i);',
  18099. ' try {',
  18100. ' i = rtl.setIntfL(i, null);',
  18101. ' i = rtl.setIntfL(i, j);',
  18102. ' } finally {',
  18103. ' rtl._Release(i);',
  18104. ' };',
  18105. '};',
  18106. '']),
  18107. LinesToStr([ // $mod.$main
  18108. '']));
  18109. end;
  18110. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  18111. begin
  18112. StartProgram(false);
  18113. Add([
  18114. '{$interfaces com}',
  18115. 'type',
  18116. ' IUnknown = interface',
  18117. ' function _AddRef: longint;',
  18118. ' function _Release: longint;',
  18119. ' end;',
  18120. ' TObject = class(IUnknown)',
  18121. ' function _AddRef: longint; virtual; abstract;',
  18122. ' function _Release: longint; virtual; abstract;',
  18123. ' end;',
  18124. 'function DoDefault(i: IUnknown): IUnknown;',
  18125. 'begin',
  18126. ' Result:=i;',
  18127. ' if Result<>nil then exit;',
  18128. 'end;',
  18129. 'begin',
  18130. '']);
  18131. ConvertProgram;
  18132. CheckSource('TestClassInterface_COM_FunctionResult',
  18133. LinesToStr([ // statements
  18134. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18135. 'rtl.createClass($mod, "TObject", null, function () {',
  18136. ' this.$init = function () {',
  18137. ' };',
  18138. ' this.$final = function () {',
  18139. ' };',
  18140. ' rtl.addIntf(this, $mod.IUnknown);',
  18141. '});',
  18142. 'this.DoDefault = function (i) {',
  18143. ' var Result = null;',
  18144. ' var $ok = false;',
  18145. ' try {',
  18146. ' Result = rtl.setIntfL(Result, i);',
  18147. ' if(Result !== null){',
  18148. ' $ok = true;',
  18149. ' return Result;',
  18150. ' };',
  18151. ' $ok = true;',
  18152. ' } finally {',
  18153. ' if(!$ok) rtl._Release(Result);',
  18154. ' };',
  18155. ' return Result;',
  18156. '};',
  18157. '']),
  18158. LinesToStr([ // $mod.$main
  18159. '']));
  18160. end;
  18161. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  18162. begin
  18163. StartProgram(false);
  18164. Add([
  18165. '{$interfaces com}',
  18166. 'type',
  18167. ' IUnknown = interface',
  18168. ' function _AddRef: longint;',
  18169. ' function _Release: longint;',
  18170. ' end;',
  18171. ' TObject = class(IUnknown)',
  18172. ' function _AddRef: longint; virtual; abstract;',
  18173. ' function _Release: longint; virtual; abstract;',
  18174. ' function GetIntf: IUnknown; virtual;',
  18175. ' end;',
  18176. ' TMouse = class',
  18177. ' function GetIntf: IUnknown; override;',
  18178. ' end;',
  18179. 'function TObject.GetIntf: IUnknown; begin end;',
  18180. 'function TMouse.GetIntf: IUnknown;',
  18181. 'var i: IUnknown;',
  18182. 'begin',
  18183. ' inherited;',
  18184. ' inherited GetIntf;',
  18185. ' inherited GetIntf();',
  18186. ' Result:=inherited GetIntf;',
  18187. ' Result:=inherited GetIntf();',
  18188. ' i:=inherited GetIntf;',
  18189. ' i:=inherited GetIntf();',
  18190. 'end;',
  18191. 'begin',
  18192. '']);
  18193. ConvertProgram;
  18194. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  18195. LinesToStr([ // statements
  18196. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18197. 'rtl.createClass($mod, "TObject", null, function () {',
  18198. ' this.$init = function () {',
  18199. ' };',
  18200. ' this.$final = function () {',
  18201. ' };',
  18202. ' this.GetIntf = function () {',
  18203. ' var Result = null;',
  18204. ' return Result;',
  18205. ' };',
  18206. ' rtl.addIntf(this, $mod.IUnknown);',
  18207. '});',
  18208. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  18209. ' this.GetIntf = function () {',
  18210. ' var Result = null;',
  18211. ' var i = null;',
  18212. ' var $ir = rtl.createIntfRefs();',
  18213. ' var $ok = false;',
  18214. ' try {',
  18215. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  18216. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  18217. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  18218. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18219. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18220. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18221. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18222. ' $ok = true;',
  18223. ' } finally {',
  18224. ' $ir.free();',
  18225. ' rtl._Release(i);',
  18226. ' if (!$ok) rtl._Release(Result);',
  18227. ' };',
  18228. ' return Result;',
  18229. ' };',
  18230. ' rtl.addIntf(this, $mod.IUnknown);',
  18231. '});',
  18232. '']),
  18233. LinesToStr([ // $mod.$main
  18234. '']));
  18235. end;
  18236. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  18237. begin
  18238. StartProgram(false);
  18239. Add([
  18240. '{$interfaces com}',
  18241. 'type',
  18242. ' IUnknown = interface',
  18243. ' function _AddRef: longint;',
  18244. ' function _Release: longint;',
  18245. ' end;',
  18246. ' TObject = class(IUnknown)',
  18247. ' function _AddRef: longint; virtual; abstract;',
  18248. ' function _Release: longint; virtual; abstract;',
  18249. ' end;',
  18250. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  18251. 'begin',
  18252. ' if i is IUnknown then ;',
  18253. ' if o is IUnknown then ;',
  18254. ' if i is TObject then ;',
  18255. ' i:=j as IUnknown;',
  18256. ' i:=o as IUnknown;',
  18257. ' o:=j as TObject;',
  18258. ' i:=IUnknown(j);',
  18259. ' i:=IUnknown(o);',
  18260. ' o:=TObject(i);',
  18261. 'end;',
  18262. 'begin',
  18263. '']);
  18264. ConvertProgram;
  18265. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  18266. LinesToStr([ // statements
  18267. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18268. 'rtl.createClass($mod, "TObject", null, function () {',
  18269. ' this.$init = function () {',
  18270. ' };',
  18271. ' this.$final = function () {',
  18272. ' };',
  18273. ' rtl.addIntf(this, $mod.IUnknown);',
  18274. '});',
  18275. 'this.DoDefault = function (i, j, o) {',
  18276. ' rtl._AddRef(i);',
  18277. ' try {',
  18278. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  18279. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  18280. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  18281. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  18282. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18283. ' o = rtl.intfAsClass(j, $mod.TObject);',
  18284. ' i = rtl.setIntfL(i, j);',
  18285. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18286. ' o = rtl.intfToClass(i, $mod.TObject);',
  18287. ' } finally {',
  18288. ' rtl._Release(i);',
  18289. ' };',
  18290. '};',
  18291. '']),
  18292. LinesToStr([ // $mod.$main
  18293. '']));
  18294. end;
  18295. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  18296. begin
  18297. StartProgram(false);
  18298. Add([
  18299. '{$interfaces com}',
  18300. 'type',
  18301. ' IUnknown = interface',
  18302. ' function _AddRef: longint;',
  18303. ' function _Release: longint;',
  18304. ' end;',
  18305. ' TObject = class(IUnknown)',
  18306. ' function _AddRef: longint; virtual; abstract;',
  18307. ' function _Release: longint; virtual; abstract;',
  18308. ' end;',
  18309. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  18310. 'var o: TObject;',
  18311. 'begin',
  18312. ' DoIt(v,v,v,v);',
  18313. ' DoIt(o,o,k,k);',
  18314. 'end;',
  18315. 'procedure DoSome;',
  18316. 'var v: IUnknown;',
  18317. 'begin',
  18318. ' DoIt(v,v,v,v);',
  18319. 'end;',
  18320. 'var i: IUnknown;',
  18321. 'begin',
  18322. ' DoIt(i,i,i,i);',
  18323. '']);
  18324. ConvertProgram;
  18325. CheckSource('TestClassInterface_COM_PassAsArg',
  18326. LinesToStr([ // statements
  18327. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18328. 'rtl.createClass($mod, "TObject", null, function () {',
  18329. ' this.$init = function () {',
  18330. ' };',
  18331. ' this.$final = function () {',
  18332. ' };',
  18333. ' rtl.addIntf(this, $mod.IUnknown);',
  18334. '});',
  18335. 'this.DoIt = function (v, j, k, l) {',
  18336. ' var o = null;',
  18337. ' var $ir = rtl.createIntfRefs();',
  18338. ' rtl._AddRef(v);',
  18339. ' try {',
  18340. ' $mod.DoIt(v, v, {',
  18341. ' get: function () {',
  18342. ' return v;',
  18343. ' },',
  18344. ' set: function (w) {',
  18345. ' v = rtl.setIntfL(v, w);',
  18346. ' }',
  18347. ' }, {',
  18348. ' get: function () {',
  18349. ' return v;',
  18350. ' },',
  18351. ' set: function (w) {',
  18352. ' v = rtl.setIntfL(v, w);',
  18353. ' }',
  18354. ' });',
  18355. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  18356. ' } finally {',
  18357. ' $ir.free();',
  18358. ' rtl._Release(v);',
  18359. ' };',
  18360. '};',
  18361. 'this.DoSome = function () {',
  18362. ' var v = null;',
  18363. ' try {',
  18364. ' $mod.DoIt(v, v, {',
  18365. ' get: function () {',
  18366. ' return v;',
  18367. ' },',
  18368. ' set: function (w) {',
  18369. ' v = rtl.setIntfL(v, w);',
  18370. ' }',
  18371. ' }, {',
  18372. ' get: function () {',
  18373. ' return v;',
  18374. ' },',
  18375. ' set: function (w) {',
  18376. ' v = rtl.setIntfL(v, w);',
  18377. ' }',
  18378. ' });',
  18379. ' } finally {',
  18380. ' rtl._Release(v);',
  18381. ' };',
  18382. '};',
  18383. 'this.i = null;',
  18384. '']),
  18385. LinesToStr([ // $mod.$main
  18386. '$mod.DoIt($mod.i, $mod.i, {',
  18387. ' p: $mod,',
  18388. ' get: function () {',
  18389. ' return this.p.i;',
  18390. ' },',
  18391. ' set: function (v) {',
  18392. ' rtl.setIntfP(this.p, "i", v);',
  18393. ' }',
  18394. '}, {',
  18395. ' p: $mod,',
  18396. ' get: function () {',
  18397. ' return this.p.i;',
  18398. ' },',
  18399. ' set: function (v) {',
  18400. ' rtl.setIntfP(this.p, "i", v);',
  18401. ' }',
  18402. '});',
  18403. '']));
  18404. end;
  18405. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  18406. begin
  18407. StartProgram(false);
  18408. Add([
  18409. '{$interfaces com}',
  18410. 'type',
  18411. ' IUnknown = interface',
  18412. ' function _AddRef: longint;',
  18413. ' function _Release: longint;',
  18414. ' end;',
  18415. ' TObject = class(IUnknown)',
  18416. ' function _AddRef: longint; virtual; abstract;',
  18417. ' function _Release: longint; virtual; abstract;',
  18418. ' end;',
  18419. 'procedure DoIt(out i);',
  18420. 'begin end;',
  18421. 'procedure DoSome;',
  18422. 'var v: IUnknown;',
  18423. 'begin',
  18424. ' DoIt(v);',
  18425. 'end;',
  18426. 'function GetIt: IUnknown;',
  18427. 'begin',
  18428. ' DoIt(Result);',
  18429. 'end;',
  18430. 'var i: IUnknown;',
  18431. 'begin',
  18432. ' DoIt(i);',
  18433. '']);
  18434. ConvertProgram;
  18435. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  18436. LinesToStr([ // statements
  18437. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18438. 'rtl.createClass($mod, "TObject", null, function () {',
  18439. ' this.$init = function () {',
  18440. ' };',
  18441. ' this.$final = function () {',
  18442. ' };',
  18443. ' rtl.addIntf(this, $mod.IUnknown);',
  18444. '});',
  18445. 'this.DoIt = function (i) {',
  18446. '};',
  18447. 'this.DoSome = function () {',
  18448. ' var v = null;',
  18449. ' try {',
  18450. ' $mod.DoIt({',
  18451. ' get: function () {',
  18452. ' return v;',
  18453. ' },',
  18454. ' set: function (w) {',
  18455. ' v = w;',
  18456. ' }',
  18457. ' });',
  18458. ' } finally {',
  18459. ' rtl._Release(v);',
  18460. ' };',
  18461. '};',
  18462. 'this.GetIt = function () {',
  18463. ' var Result = null;',
  18464. ' var $ok = false;',
  18465. ' try {',
  18466. ' $mod.DoIt({',
  18467. ' get: function () {',
  18468. ' return Result;',
  18469. ' },',
  18470. ' set: function (v) {',
  18471. ' Result = v;',
  18472. ' }',
  18473. ' });',
  18474. ' $ok = true;',
  18475. ' } finally {',
  18476. ' if (!$ok) rtl._Release(Result);',
  18477. ' };',
  18478. ' return Result;',
  18479. '};',
  18480. 'this.i = null;',
  18481. '']),
  18482. LinesToStr([ // $mod.$main
  18483. 'try {',
  18484. ' $mod.DoIt({',
  18485. ' p: $mod,',
  18486. ' get: function () {',
  18487. ' return this.p.i;',
  18488. ' },',
  18489. ' set: function (v) {',
  18490. ' this.p.i = v;',
  18491. ' }',
  18492. ' });',
  18493. '} finally {',
  18494. ' rtl._Release($mod.i);',
  18495. '};',
  18496. '']));
  18497. end;
  18498. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  18499. begin
  18500. StartProgram(false);
  18501. Add([
  18502. '{$interfaces com}',
  18503. 'type',
  18504. ' IUnknown = interface',
  18505. ' function _AddRef: longint;',
  18506. ' function _Release: longint;',
  18507. ' end;',
  18508. ' TObject = class(IUnknown)',
  18509. ' function _AddRef: longint; virtual; abstract;',
  18510. ' function _Release: longint; virtual; abstract;',
  18511. ' end;',
  18512. 'function GetIt: IUnknown;',
  18513. 'begin',
  18514. 'end;',
  18515. 'procedure DoSome;',
  18516. 'var v: IUnknown;',
  18517. ' i: longint;',
  18518. 'begin',
  18519. ' v:=GetIt;',
  18520. ' v:=GetIt();',
  18521. ' GetIt()._AddRef;',
  18522. ' i:=GetIt()._AddRef;',
  18523. 'end;',
  18524. 'var v: IUnknown;',
  18525. ' i: longint;',
  18526. 'begin',
  18527. ' v:=GetIt;',
  18528. ' v:=GetIt();',
  18529. ' GetIt()._AddRef;',
  18530. ' i:=GetIt()._AddRef;',
  18531. '']);
  18532. ConvertProgram;
  18533. CheckSource('TestClassInterface_COM_FunctionInExpr',
  18534. LinesToStr([ // statements
  18535. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18536. 'rtl.createClass($mod, "TObject", null, function () {',
  18537. ' this.$init = function () {',
  18538. ' };',
  18539. ' this.$final = function () {',
  18540. ' };',
  18541. ' rtl.addIntf(this, $mod.IUnknown);',
  18542. '});',
  18543. 'this.GetIt = function () {',
  18544. ' var Result = null;',
  18545. ' return Result;',
  18546. '};',
  18547. 'this.DoSome = function () {',
  18548. ' var v = null;',
  18549. ' var i = 0;',
  18550. ' var $ir = rtl.createIntfRefs();',
  18551. ' try {',
  18552. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  18553. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  18554. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18555. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18556. ' } finally {',
  18557. ' $ir.free();',
  18558. ' rtl._Release(v);',
  18559. ' };',
  18560. '};',
  18561. 'this.v = null;',
  18562. 'this.i = 0;',
  18563. '']),
  18564. LinesToStr([ // $mod.$main
  18565. 'var $ir = rtl.createIntfRefs();',
  18566. 'try {',
  18567. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18568. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  18569. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  18570. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  18571. '} finally {',
  18572. ' $ir.free();',
  18573. '};',
  18574. '']));
  18575. end;
  18576. procedure TTestModule.TestClassInterface_COM_Property;
  18577. begin
  18578. StartProgram(false);
  18579. Add([
  18580. '{$interfaces com}',
  18581. 'type',
  18582. ' IUnknown = interface',
  18583. ' function _AddRef: longint;',
  18584. ' function _Release: longint;',
  18585. ' end;',
  18586. ' TObject = class(IUnknown)',
  18587. ' FAnt: IUnknown;',
  18588. ' function _AddRef: longint; virtual; abstract;',
  18589. ' function _Release: longint; virtual; abstract;',
  18590. ' function GetBird: IUnknown; virtual; abstract;',
  18591. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18592. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18593. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18594. ' property Ant: IUnknown read FAnt write FAnt;',
  18595. ' property Bird: IUnknown read GetBird write SetBird;',
  18596. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18597. ' end;',
  18598. 'procedure DoIt;',
  18599. 'var',
  18600. ' o: TObject;',
  18601. ' v: IUnknown;',
  18602. 'begin',
  18603. ' v:=o.Ant;',
  18604. ' o.Ant:=v;',
  18605. ' o.Ant:=o.Ant;',
  18606. ' v:=o.Bird;',
  18607. ' o.Bird:=v;',
  18608. ' o.Bird:=o.Bird;',
  18609. ' v:=o.Items[1];',
  18610. ' o.Items[2]:=v;',
  18611. ' o.Items[3]:=o.Items[4];',
  18612. ' v:=o[5];',
  18613. ' o[6]:=v;',
  18614. ' o[7]:=o[8];',
  18615. 'end;',
  18616. 'begin',
  18617. '']);
  18618. ConvertProgram;
  18619. CheckSource('TestClassInterface_COM_Property',
  18620. LinesToStr([ // statements
  18621. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18622. 'rtl.createClass($mod, "TObject", null, function () {',
  18623. ' this.$init = function () {',
  18624. ' this.FAnt = null;',
  18625. ' };',
  18626. ' this.$final = function () {',
  18627. ' this.FAnt = undefined;',
  18628. ' };',
  18629. ' rtl.addIntf(this, $mod.IUnknown);',
  18630. '});',
  18631. 'this.DoIt = function () {',
  18632. ' var o = null;',
  18633. ' var v = null;',
  18634. ' var $ir = rtl.createIntfRefs();',
  18635. ' try {',
  18636. ' v = rtl.setIntfL(v, o.FAnt);',
  18637. ' rtl.setIntfP(o, "FAnt", v);',
  18638. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  18639. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  18640. ' o.SetBird(v);',
  18641. ' o.SetBird($ir.ref(1, o.GetBird()));',
  18642. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  18643. ' o.SetItems(2, v);',
  18644. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  18645. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  18646. ' o.SetItems(6, v);',
  18647. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  18648. ' } finally {',
  18649. ' $ir.free();',
  18650. ' rtl._Release(v);',
  18651. ' };',
  18652. '};',
  18653. '']),
  18654. LinesToStr([ // $mod.$main
  18655. '']));
  18656. end;
  18657. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  18658. begin
  18659. StartProgram(false);
  18660. Add([
  18661. '{$interfaces com}',
  18662. 'type',
  18663. ' IUnknown = interface',
  18664. ' function _AddRef: longint;',
  18665. ' function _Release: longint;',
  18666. ' function GetBird: IUnknown;',
  18667. ' procedure SetBird(Value: IUnknown);',
  18668. ' function GetItems(Index: longint): IUnknown;',
  18669. ' procedure SetItems(Index: longint; Value: IUnknown);',
  18670. ' property Bird: IUnknown read GetBird write SetBird;',
  18671. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  18672. ' end;',
  18673. ' TObject = class(IUnknown)',
  18674. ' function _AddRef: longint; virtual; abstract;',
  18675. ' function _Release: longint; virtual; abstract;',
  18676. ' function GetBird: IUnknown; virtual; abstract;',
  18677. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  18678. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  18679. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  18680. ' end;',
  18681. 'procedure DoIt;',
  18682. 'var',
  18683. ' o: TObject;',
  18684. ' v: IUnknown;',
  18685. 'begin',
  18686. ' v:=v.Items[1];',
  18687. ' v.Items[2]:=v;',
  18688. ' v.Items[3]:=v.Items[4];',
  18689. ' v:=v[5];',
  18690. ' v[6]:=v;',
  18691. ' v[7]:=v[8];',
  18692. ' v[9].Bird.Bird:=v;',
  18693. ' v:=v.Bird[10].Bird',
  18694. 'end;',
  18695. 'begin',
  18696. '']);
  18697. ConvertProgram;
  18698. CheckSource('TestClassInterface_COM_IntfProperty',
  18699. LinesToStr([ // statements
  18700. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  18701. ' "_AddRef",',
  18702. ' "_Release",',
  18703. ' "GetBird",',
  18704. ' "SetBird",',
  18705. ' "GetItems",',
  18706. ' "SetItems"',
  18707. '], null);',
  18708. 'rtl.createClass($mod, "TObject", null, function () {',
  18709. ' this.$init = function () {',
  18710. ' };',
  18711. ' this.$final = function () {',
  18712. ' };',
  18713. ' rtl.addIntf(this, $mod.IUnknown);',
  18714. '});',
  18715. 'this.DoIt = function () {',
  18716. ' var o = null;',
  18717. ' var v = null;',
  18718. ' var $ir = rtl.createIntfRefs();',
  18719. ' try {',
  18720. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  18721. ' v.SetItems(2, v);',
  18722. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  18723. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  18724. ' v.SetItems(6, v);',
  18725. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  18726. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  18727. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  18728. ' } finally {',
  18729. ' $ir.free();',
  18730. ' rtl._Release(v);',
  18731. ' };',
  18732. '};',
  18733. '']),
  18734. LinesToStr([ // $mod.$main
  18735. '']));
  18736. end;
  18737. procedure TTestModule.TestClassInterface_COM_Delegation;
  18738. begin
  18739. StartProgram(false);
  18740. Add([
  18741. '{$interfaces com}',
  18742. 'type',
  18743. ' IUnknown = interface',
  18744. ' function _AddRef: longint;',
  18745. ' function _Release: longint;',
  18746. ' end;',
  18747. ' IBird = interface(IUnknown)',
  18748. ' procedure Fly(s: string);',
  18749. ' end;',
  18750. ' IEagle = interface(IBird) end;',
  18751. ' IDove = interface(IBird) end;',
  18752. ' ISwallow = interface(IBird) end;',
  18753. ' TObject = class',
  18754. ' end;',
  18755. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18756. ' function _AddRef: longint; virtual; abstract;',
  18757. ' function _Release: longint; virtual; abstract;',
  18758. ' procedure Fly(s: string); virtual; abstract;',
  18759. ' end;',
  18760. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18761. ' function _AddRef: longint; virtual; abstract;',
  18762. ' function _Release: longint; virtual; abstract;',
  18763. ' FBirdIntf: IBird;',
  18764. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18765. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18766. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18767. ' FDoveObj: TBird;',
  18768. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18769. ' function GetSwallowObj: TBird; virtual; abstract;',
  18770. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18771. ' end;',
  18772. 'begin',
  18773. '']);
  18774. ConvertProgram;
  18775. CheckSource('TestClassInterface_COM_Delegation',
  18776. LinesToStr([ // statements
  18777. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18778. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  18779. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  18780. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  18781. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  18782. 'rtl.createClass($mod, "TObject", null, function () {',
  18783. ' this.$init = function () {',
  18784. ' };',
  18785. ' this.$final = function () {',
  18786. ' };',
  18787. '});',
  18788. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18789. ' rtl.addIntf(this, $mod.IBird);',
  18790. ' rtl.addIntf(this, $mod.IEagle);',
  18791. ' rtl.addIntf(this, $mod.IDove);',
  18792. ' rtl.addIntf(this, $mod.ISwallow);',
  18793. '});',
  18794. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18795. ' this.$init = function () {',
  18796. ' $mod.TObject.$init.call(this);',
  18797. ' this.FBirdIntf = null;',
  18798. ' this.FDoveObj = null;',
  18799. ' };',
  18800. ' this.$final = function () {',
  18801. ' this.FBirdIntf = undefined;',
  18802. ' this.FDoveObj = undefined;',
  18803. ' $mod.TObject.$final.call(this);',
  18804. ' };',
  18805. ' this.$intfmaps = {',
  18806. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  18807. ' return rtl._AddRef(this.FBirdIntf);',
  18808. ' },',
  18809. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  18810. ' return this.GetEagleIntf();',
  18811. ' },',
  18812. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  18813. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  18814. ' },',
  18815. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  18816. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18817. ' }',
  18818. ' };',
  18819. '});',
  18820. '']),
  18821. LinesToStr([ // $mod.$main
  18822. '']));
  18823. end;
  18824. procedure TTestModule.TestClassInterface_COM_With;
  18825. begin
  18826. StartProgram(false);
  18827. Add([
  18828. '{$interfaces com}',
  18829. 'type',
  18830. ' IUnknown = interface',
  18831. ' function _AddRef: longint;',
  18832. ' function _Release: longint;',
  18833. ' function GetAnt: IUnknown;',
  18834. ' property Ant: IUnknown read GetAnt;',
  18835. ' end;',
  18836. ' TObject = class(IUnknown)',
  18837. ' function _AddRef: longint; virtual; abstract;',
  18838. ' function _Release: longint; virtual; abstract;',
  18839. ' function GetAnt: IUnknown; virtual; abstract;',
  18840. ' property Ant: IUnknown read GetAnt;',
  18841. ' end;',
  18842. 'procedure DoIt;',
  18843. 'var',
  18844. ' i: IUnknown;',
  18845. 'begin',
  18846. ' with i do ',
  18847. ' GetAnt;',
  18848. ' with i.Ant, Ant do ',
  18849. ' GetAnt;',
  18850. 'end;',
  18851. 'begin',
  18852. '']);
  18853. ConvertProgram;
  18854. CheckSource('TestClassInterface_COM_With',
  18855. LinesToStr([ // statements
  18856. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  18857. 'rtl.createClass($mod, "TObject", null, function () {',
  18858. ' this.$init = function () {',
  18859. ' };',
  18860. ' this.$final = function () {',
  18861. ' };',
  18862. ' rtl.addIntf(this, $mod.IUnknown);',
  18863. '});',
  18864. 'this.DoIt = function () {',
  18865. ' var i = null;',
  18866. ' var $ir = rtl.createIntfRefs();',
  18867. ' try {',
  18868. ' $ir.ref(1, i.GetAnt());',
  18869. ' var $with1 = $ir.ref(2, i.GetAnt());',
  18870. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  18871. ' $ir.ref(4, $with2.GetAnt());',
  18872. ' } finally {',
  18873. ' $ir.free();',
  18874. ' };',
  18875. '};',
  18876. '']),
  18877. LinesToStr([ // $mod.$main
  18878. '']));
  18879. end;
  18880. procedure TTestModule.TestClassInterface_COM_ForIn;
  18881. begin
  18882. StartProgram(false);
  18883. Add([
  18884. '{$interfaces com}',
  18885. 'type',
  18886. ' IUnknown = interface end;',
  18887. ' TObject = class',
  18888. ' Id: longint;',
  18889. ' end;',
  18890. ' IEnumerator = interface(IUnknown)',
  18891. ' function GetCurrent: TObject;',
  18892. ' function MoveNext: Boolean;',
  18893. ' property Current: TObject read GetCurrent;',
  18894. ' end;',
  18895. ' IEnumerable = interface(IUnknown)',
  18896. ' function GetEnumerator: IEnumerator;',
  18897. ' end;',
  18898. 'var',
  18899. ' o: TObject;',
  18900. ' i: IEnumerable;',
  18901. 'begin',
  18902. ' for o in i do o.Id:=3;',
  18903. '']);
  18904. ConvertProgram;
  18905. CheckSource('TestClassInterface_COM_ForIn',
  18906. LinesToStr([ // statements
  18907. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18908. 'rtl.createClass($mod, "TObject", null, function () {',
  18909. ' this.$init = function () {',
  18910. ' this.Id = 0;',
  18911. ' };',
  18912. ' this.$final = function () {',
  18913. ' };',
  18914. '});',
  18915. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18916. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18917. 'this.o = null;',
  18918. 'this.i = null;',
  18919. '']),
  18920. LinesToStr([ // $mod.$main
  18921. 'var $in1 = $mod.i.GetEnumerator();',
  18922. 'try {',
  18923. ' while ($in1.MoveNext()) {',
  18924. ' $mod.o = $in1.GetCurrent();',
  18925. ' $mod.o.Id = 3;',
  18926. ' }',
  18927. '} finally {',
  18928. ' rtl._Release($in1)',
  18929. '};',
  18930. '']));
  18931. end;
  18932. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  18933. begin
  18934. StartProgram(false);
  18935. Add([
  18936. '{$interfaces com}',
  18937. 'type',
  18938. ' IUnknown = interface',
  18939. ' function _AddRef: longint;',
  18940. ' function _Release: longint;',
  18941. ' end;',
  18942. ' TObject = class',
  18943. ' end;',
  18944. ' TArrOfIntf = array of IUnknown;',
  18945. 'begin',
  18946. '']);
  18947. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  18948. ConvertProgram;
  18949. end;
  18950. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  18951. begin
  18952. StartProgram(false);
  18953. Add([
  18954. '{$interfaces com}',
  18955. 'type',
  18956. ' IUnknown = interface',
  18957. ' function _AddRef: longint;',
  18958. ' function _Release: longint;',
  18959. ' end;',
  18960. ' TRec = record',
  18961. ' i: IUnknown;',
  18962. ' end;',
  18963. 'begin',
  18964. '']);
  18965. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  18966. ConvertProgram;
  18967. end;
  18968. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  18969. begin
  18970. StartUnit(false);
  18971. Add([
  18972. '{$interfaces com}',
  18973. 'interface',
  18974. 'implementation',
  18975. 'type',
  18976. ' IUnknown = interface',
  18977. ' function _AddRef: longint;',
  18978. ' end;',
  18979. ' TObject = class(IUnknown)',
  18980. ' function _AddRef: longint;',
  18981. ' end;',
  18982. 'function TObject._AddRef: longint; begin end;',
  18983. 'var i: IUnknown;',
  18984. ' o: TObject;',
  18985. 'initialization',
  18986. ' i:=nil;',
  18987. ' i:=i;',
  18988. ' i:=o;',
  18989. ' if (o as IUnknown)=nil then ;',
  18990. '']);
  18991. ConvertUnit;
  18992. CheckSource('TestClassInterface_COM_UnitInitialization',
  18993. LinesToStr([ // statements
  18994. 'var $impl = $mod.$impl;',
  18995. '']),
  18996. LinesToStr([ // this.$init
  18997. 'var $ir = rtl.createIntfRefs();',
  18998. 'try {',
  18999. ' rtl.setIntfP($impl, "i", null);',
  19000. ' rtl.setIntfP($impl, "i", $impl.i);',
  19001. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  19002. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  19003. '} finally {',
  19004. ' $ir.free();',
  19005. '};',
  19006. '']),
  19007. LinesToStr([ // implementation
  19008. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  19009. 'rtl.createClass($impl, "TObject", null, function () {',
  19010. ' this.$init = function () {',
  19011. ' };',
  19012. ' this.$final = function () {',
  19013. ' };',
  19014. ' this._AddRef = function () {',
  19015. ' var Result = 0;',
  19016. ' return Result;',
  19017. ' };',
  19018. ' rtl.addIntf(this, $impl.IUnknown);',
  19019. '});',
  19020. '$impl.i = null;',
  19021. '$impl.o = null;',
  19022. ''])
  19023. );
  19024. end;
  19025. procedure TTestModule.TestClassInterface_GUID;
  19026. begin
  19027. StartProgram(false);
  19028. Add([
  19029. '{$interfaces corba}',
  19030. 'type',
  19031. ' IUnknown = interface',
  19032. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19033. ' end;',
  19034. ' TObject = class end;',
  19035. ' TGUID = record D1, D2, D3, D4: word; end;',
  19036. ' TAliasGUID = TGUID;',
  19037. ' TGUIDString = type string;',
  19038. ' TAliasGUIDString = TGUIDString;',
  19039. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  19040. 'begin end;',
  19041. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  19042. 'begin end;',
  19043. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  19044. 'begin end;',
  19045. 'var',
  19046. ' i: IUnknown;',
  19047. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  19048. ' s: TAliasGUIDString;',
  19049. 'begin',
  19050. ' DoConstGUIDIt(IUnknown);',
  19051. ' DoDefGUID(IUnknown);',
  19052. ' DoStr(IUnknown);',
  19053. ' DoConstGUIDIt(i);',
  19054. ' DoDefGUID(i);',
  19055. ' DoStr(i);',
  19056. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19057. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19058. ' DoStr(g);',
  19059. ' g:=i;',
  19060. ' g:=IUnknown;',
  19061. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19062. ' s:=i;',
  19063. ' s:=IUnknown;',
  19064. ' s:=g;',
  19065. ' if g=i then ;',
  19066. ' if i=g then ;',
  19067. ' if g=IUnknown then ;',
  19068. ' if IUnknown=g then ;',
  19069. ' if s=i then ;',
  19070. ' if i=s then ;',
  19071. ' if s=IUnknown then ;',
  19072. ' if IUnknown=s then ;',
  19073. ' if s=g then ;',
  19074. ' if g=s then ;',
  19075. '']);
  19076. ConvertProgram;
  19077. CheckSource('TestClassInterface_GUID',
  19078. LinesToStr([ // statements
  19079. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19080. 'rtl.createClass($mod, "TObject", null, function () {',
  19081. ' this.$init = function () {',
  19082. ' };',
  19083. ' this.$final = function () {',
  19084. ' };',
  19085. '});',
  19086. 'rtl.recNewT($mod, "TGUID", function () {',
  19087. ' this.D1 = 0;',
  19088. ' this.D2 = 0;',
  19089. ' this.D3 = 0;',
  19090. ' this.D4 = 0;',
  19091. ' this.$eq = function (b) {',
  19092. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19093. ' };',
  19094. ' this.$assign = function (s) {',
  19095. ' this.D1 = s.D1;',
  19096. ' this.D2 = s.D2;',
  19097. ' this.D3 = s.D3;',
  19098. ' this.D4 = s.D4;',
  19099. ' return this;',
  19100. ' };',
  19101. '});',
  19102. 'this.DoConstGUIDIt = function (g) {',
  19103. '};',
  19104. 'this.DoDefGUID = function (g) {',
  19105. '};',
  19106. 'this.DoStr = function (s) {',
  19107. '};',
  19108. 'this.i = null;',
  19109. 'this.g = $mod.TGUID.$clone({',
  19110. ' D1: 0xD91C9AF4,',
  19111. ' D2: 0x3C93,',
  19112. ' D3: 0x420F,',
  19113. ' D4: [',
  19114. ' 0xA3,',
  19115. ' 0x03,',
  19116. ' 0xBF,',
  19117. ' 0x5B,',
  19118. ' 0xA8,',
  19119. ' 0x2B,',
  19120. ' 0xFD,',
  19121. ' 0x23',
  19122. ' ]',
  19123. '});',
  19124. 'this.s = "";',
  19125. '']),
  19126. LinesToStr([ // $mod.$main
  19127. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  19128. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  19129. '$mod.DoStr($mod.IUnknown.$guid);',
  19130. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  19131. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  19132. '$mod.DoStr($mod.i.$guid);',
  19133. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19134. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19135. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  19136. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  19137. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  19138. '$mod.g.$assign({',
  19139. ' D1: 0xD91C9AF4,',
  19140. ' D2: 0x3C93,',
  19141. ' D3: 0x420F,',
  19142. ' D4: [',
  19143. ' 0xA3,',
  19144. ' 0x03,',
  19145. ' 0xBF,',
  19146. ' 0x5B,',
  19147. ' 0xA8,',
  19148. ' 0x2B,',
  19149. ' 0xFD,',
  19150. ' 0x23',
  19151. ' ]',
  19152. '});',
  19153. '$mod.s = $mod.i.$guid;',
  19154. '$mod.s = $mod.IUnknown.$guid;',
  19155. '$mod.s = rtl.guidrToStr($mod.g);',
  19156. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19157. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19158. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19159. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19160. 'if ($mod.s === $mod.i.$guid) ;',
  19161. 'if ($mod.i.$guid === $mod.s) ;',
  19162. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  19163. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  19164. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19165. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19166. '']));
  19167. end;
  19168. procedure TTestModule.TestClassInterface_GUIDProperty;
  19169. begin
  19170. StartProgram(false);
  19171. Add([
  19172. '{$interfaces corba}',
  19173. 'type',
  19174. ' IUnknown = interface',
  19175. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19176. ' end;',
  19177. ' TGUID = record D1, D2, D3, D4: word; end;',
  19178. ' TAliasGUID = TGUID;',
  19179. ' TGUIDString = type string;',
  19180. ' TAliasGUIDString = TGUIDString;',
  19181. ' TObject = class',
  19182. ' function GetG: TAliasGUID; virtual; abstract;',
  19183. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  19184. ' function GetS: TAliasGUIDString; virtual; abstract;',
  19185. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  19186. ' property g: TAliasGUID read GetG write SetG;',
  19187. ' property s: TAliasGUIDString read GetS write SetS;',
  19188. ' end;',
  19189. 'var o: TObject;',
  19190. 'begin',
  19191. ' o.g:=IUnknown;',
  19192. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19193. ' o.s:=IUnknown;',
  19194. ' o.s:=o.g;',
  19195. '']);
  19196. ConvertProgram;
  19197. CheckSource('TestClassInterface_GUIDProperty',
  19198. LinesToStr([ // statements
  19199. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19200. 'rtl.recNewT($mod, "TGUID", function () {',
  19201. ' this.D1 = 0;',
  19202. ' this.D2 = 0;',
  19203. ' this.D3 = 0;',
  19204. ' this.D4 = 0;',
  19205. ' this.$eq = function (b) {',
  19206. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19207. ' };',
  19208. ' this.$assign = function (s) {',
  19209. ' this.D1 = s.D1;',
  19210. ' this.D2 = s.D2;',
  19211. ' this.D3 = s.D3;',
  19212. ' this.D4 = s.D4;',
  19213. ' return this;',
  19214. ' };',
  19215. '});',
  19216. 'rtl.createClass($mod, "TObject", null, function () {',
  19217. ' this.$init = function () {',
  19218. ' };',
  19219. ' this.$final = function () {',
  19220. ' };',
  19221. '});',
  19222. 'this.o = null;',
  19223. '']),
  19224. LinesToStr([ // $mod.$main
  19225. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  19226. '$mod.o.SetG({',
  19227. ' D1: 0xD91C9AF4,',
  19228. ' D2: 0x3C93,',
  19229. ' D3: 0x420F,',
  19230. ' D4: [',
  19231. ' 0xA3,',
  19232. ' 0x03,',
  19233. ' 0xBF,',
  19234. ' 0x5B,',
  19235. ' 0xA8,',
  19236. ' 0x2B,',
  19237. ' 0xFD,',
  19238. ' 0x23',
  19239. ' ]',
  19240. '});',
  19241. '$mod.o.SetS($mod.IUnknown.$guid);',
  19242. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  19243. '']));
  19244. end;
  19245. procedure TTestModule.TestClassHelper_ClassVar;
  19246. begin
  19247. StartProgram(false);
  19248. Add([
  19249. 'type',
  19250. ' TObject = class',
  19251. ' end;',
  19252. ' THelper = class helper for TObject',
  19253. ' const',
  19254. ' One = 1;',
  19255. ' Two: word = 2;',
  19256. ' class var',
  19257. ' Glob: word;',
  19258. ' function Foo(w: word): word;',
  19259. ' class function Bar(w: word): word;',
  19260. ' end;',
  19261. 'function THelper.foo(w: word): word;',
  19262. 'begin',
  19263. ' Result:=w;',
  19264. ' Two:=One+w;',
  19265. ' Glob:=Glob;',
  19266. ' Result:=Self.Glob;',
  19267. ' Self.Glob:=Self.Glob;',
  19268. ' with Self do Glob:=Glob;',
  19269. 'end;',
  19270. 'class function THelper.bar(w: word): word;',
  19271. 'begin',
  19272. ' Result:=w;',
  19273. ' Two:=One;',
  19274. ' Glob:=Glob;',
  19275. ' Self.Glob:=Self.Glob;',
  19276. ' with Self do Glob:=Glob;',
  19277. 'end;',
  19278. 'var o: TObject;',
  19279. 'begin',
  19280. ' tobject.two:=tobject.one;',
  19281. ' tobject.Glob:=tobject.Glob;',
  19282. ' with tobject do begin',
  19283. ' two:=one;',
  19284. ' Glob:=Glob;',
  19285. ' end;',
  19286. ' o.two:=o.one;',
  19287. ' o.Glob:=o.Glob;',
  19288. ' with o do begin',
  19289. ' two:=one;',
  19290. ' Glob:=Glob;',
  19291. ' end;',
  19292. '']);
  19293. ConvertProgram;
  19294. CheckSource('TestClassHelper_ClassVar',
  19295. LinesToStr([ // statements
  19296. 'rtl.createClass($mod, "TObject", null, function () {',
  19297. ' this.$init = function () {',
  19298. ' };',
  19299. ' this.$final = function () {',
  19300. ' };',
  19301. '});',
  19302. 'rtl.createHelper($mod, "THelper", null, function () {',
  19303. ' this.One = 1;',
  19304. ' this.Two = 2;',
  19305. ' this.Glob = 0;',
  19306. ' this.Foo = function (w) {',
  19307. ' var Result = 0;',
  19308. ' Result = w;',
  19309. ' $mod.THelper.Two = 1 + w;',
  19310. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19311. ' Result = $mod.THelper.Glob;',
  19312. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19313. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19314. ' return Result;',
  19315. ' };',
  19316. ' this.Bar = function (w) {',
  19317. ' var Result = 0;',
  19318. ' Result = w;',
  19319. ' $mod.THelper.Two = 1;',
  19320. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19321. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19322. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  19323. ' return Result;',
  19324. ' };',
  19325. '});',
  19326. 'this.o = null;',
  19327. '']),
  19328. LinesToStr([ // $mod.$main
  19329. '$mod.THelper.Two = 1;',
  19330. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19331. 'var $with1 = $mod.TObject;',
  19332. '$mod.THelper.Two = 1;',
  19333. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19334. '$mod.THelper.Two = 1;',
  19335. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19336. 'var $with2 = $mod.o;',
  19337. '$mod.THelper.Two = 1;',
  19338. '$mod.THelper.Glob = $mod.THelper.Glob;',
  19339. '']));
  19340. end;
  19341. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  19342. begin
  19343. StartProgram(false);
  19344. Add([
  19345. 'type',
  19346. ' TObject = class',
  19347. ' FSize: word;',
  19348. ' property Size: word read FSize write FSize;',
  19349. ' end;',
  19350. ' THelper = class helper for TObject',
  19351. ' function Foo(w: word = 1): word;',
  19352. ' end;',
  19353. 'function THelper.foo(w: word): word;',
  19354. 'begin',
  19355. ' Result:=Size;',
  19356. ' Size:=Size+2;',
  19357. ' Self.Size:=Self.Size+3;',
  19358. ' FSize:=FSize+4;',
  19359. ' Self.FSize:=Self.FSize+5;',
  19360. ' with Self do begin',
  19361. ' Size:=Size+6;',
  19362. ' FSize:=FSize+7;',
  19363. ' FSize:=FSize+8;',
  19364. ' end;',
  19365. 'end;',
  19366. 'begin',
  19367. '']);
  19368. ConvertProgram;
  19369. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  19370. LinesToStr([ // statements
  19371. 'rtl.createClass($mod, "TObject", null, function () {',
  19372. ' this.$init = function () {',
  19373. ' this.FSize = 0;',
  19374. ' };',
  19375. ' this.$final = function () {',
  19376. ' };',
  19377. '});',
  19378. 'rtl.createHelper($mod, "THelper", null, function () {',
  19379. ' this.Foo = function (w) {',
  19380. ' var Result = 0;',
  19381. ' Result = this.FSize;',
  19382. ' this.FSize = this.FSize + 2;',
  19383. ' this.FSize = this.FSize + 3;',
  19384. ' this.FSize = this.FSize + 4;',
  19385. ' this.FSize = this.FSize + 5;',
  19386. ' this.FSize = this.FSize + 6;',
  19387. ' this.FSize = this.FSize + 7;',
  19388. ' this.FSize = this.FSize + 8;',
  19389. ' return Result;',
  19390. ' };',
  19391. '});',
  19392. '']),
  19393. LinesToStr([ // $mod.$main
  19394. '']));
  19395. end;
  19396. procedure TTestModule.TestClassHelper_Method_Call;
  19397. begin
  19398. StartProgram(false);
  19399. Add([
  19400. 'type',
  19401. ' TObject = class',
  19402. ' procedure Run(w: word = 10);',
  19403. ' end;',
  19404. ' THelper = class helper for TObject',
  19405. ' function Foo(w: word = 1): word;',
  19406. ' end;',
  19407. 'procedure TObject.Run(w: word);',
  19408. 'begin',
  19409. ' Foo;',
  19410. ' Foo();',
  19411. ' Foo(2);',
  19412. ' Self.Foo;',
  19413. ' Self.Foo();',
  19414. ' Self.Foo(3);',
  19415. ' with Self do begin',
  19416. ' Foo;',
  19417. ' Foo();',
  19418. ' Foo(4);',
  19419. ' end;',
  19420. 'end;',
  19421. 'function THelper.foo(w: word): word;',
  19422. 'begin',
  19423. ' Run;',
  19424. ' Run();',
  19425. ' Run(11);',
  19426. ' Foo;',
  19427. ' Foo();',
  19428. ' Foo(12);',
  19429. ' Self.Foo;',
  19430. ' Self.Foo();',
  19431. ' Self.Foo(13);',
  19432. ' with Self do begin',
  19433. ' Foo;',
  19434. ' Foo();',
  19435. ' Foo(14);',
  19436. ' end;',
  19437. 'end;',
  19438. 'var Obj: TObject;',
  19439. 'begin',
  19440. ' obj.Foo;',
  19441. ' obj.Foo();',
  19442. ' obj.Foo(21);',
  19443. ' with obj do begin',
  19444. ' Foo;',
  19445. ' Foo();',
  19446. ' Foo(22);',
  19447. ' end;',
  19448. '']);
  19449. ConvertProgram;
  19450. CheckSource('TestClassHelper_Method_Call',
  19451. LinesToStr([ // statements
  19452. 'rtl.createClass($mod, "TObject", null, function () {',
  19453. ' this.$init = function () {',
  19454. ' };',
  19455. ' this.$final = function () {',
  19456. ' };',
  19457. ' this.Run = function (w) {',
  19458. ' $mod.THelper.Foo.call(this, 1);',
  19459. ' $mod.THelper.Foo.call(this, 1);',
  19460. ' $mod.THelper.Foo.call(this, 2);',
  19461. ' $mod.THelper.Foo.call(this, 1);',
  19462. ' $mod.THelper.Foo.call(this, 1);',
  19463. ' $mod.THelper.Foo.call(this, 3);',
  19464. ' $mod.THelper.Foo.call(this, 1);',
  19465. ' $mod.THelper.Foo.call(this, 1);',
  19466. ' $mod.THelper.Foo.call(this, 4);',
  19467. ' };',
  19468. '});',
  19469. 'rtl.createHelper($mod, "THelper", null, function () {',
  19470. ' this.Foo = function (w) {',
  19471. ' var Result = 0;',
  19472. ' this.Run(10);',
  19473. ' this.Run(10);',
  19474. ' this.Run(11);',
  19475. ' $mod.THelper.Foo.call(this, 1);',
  19476. ' $mod.THelper.Foo.call(this, 1);',
  19477. ' $mod.THelper.Foo.call(this, 12);',
  19478. ' $mod.THelper.Foo.call(this, 1);',
  19479. ' $mod.THelper.Foo.call(this, 1);',
  19480. ' $mod.THelper.Foo.call(this, 13);',
  19481. ' $mod.THelper.Foo.call(this, 1);',
  19482. ' $mod.THelper.Foo.call(this, 1);',
  19483. ' $mod.THelper.Foo.call(this, 14);',
  19484. ' return Result;',
  19485. ' };',
  19486. '});',
  19487. 'this.Obj = null;',
  19488. '']),
  19489. LinesToStr([ // $mod.$main
  19490. '$mod.THelper.Foo.call($mod.Obj, 1);',
  19491. '$mod.THelper.Foo.call($mod.Obj, 1);',
  19492. '$mod.THelper.Foo.call($mod.Obj, 21);',
  19493. 'var $with1 = $mod.Obj;',
  19494. '$mod.THelper.Foo.call($with1, 1);',
  19495. '$mod.THelper.Foo.call($with1, 1);',
  19496. '$mod.THelper.Foo.call($with1, 22);',
  19497. '']));
  19498. end;
  19499. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  19500. begin
  19501. StartProgram(false);
  19502. Add([
  19503. 'type',
  19504. ' TObject = class',
  19505. ' procedure Run(w: word = 10);',
  19506. ' end;',
  19507. ' THelper = class helper for TObject',
  19508. ' function Foo(w: word = 1): word;',
  19509. ' end;',
  19510. 'procedure TObject.Run(w: word);',
  19511. ' procedure Sub(Self: TObject);',
  19512. ' begin',
  19513. ' Foo;',
  19514. ' Foo();',
  19515. ' Self.Foo;',
  19516. ' Self.Foo();',
  19517. ' with Self do begin',
  19518. ' Foo;',
  19519. ' Foo();',
  19520. ' end;',
  19521. ' end;',
  19522. 'begin',
  19523. 'end;',
  19524. 'function THelper.foo(w: word): word;',
  19525. ' procedure Sub(Self: TObject);',
  19526. ' begin',
  19527. ' Run;',
  19528. ' Run();',
  19529. ' Foo;',
  19530. ' Foo();',
  19531. ' Self.Foo;',
  19532. ' Self.Foo();',
  19533. ' with Self do begin',
  19534. ' Foo;',
  19535. ' Foo();',
  19536. ' end;',
  19537. ' end;',
  19538. 'begin',
  19539. 'end;',
  19540. 'begin',
  19541. '']);
  19542. ConvertProgram;
  19543. CheckSource('TestClassHelper_Method_Nested_Call',
  19544. LinesToStr([ // statements
  19545. 'rtl.createClass($mod, "TObject", null, function () {',
  19546. ' this.$init = function () {',
  19547. ' };',
  19548. ' this.$final = function () {',
  19549. ' };',
  19550. ' this.Run = function (w) {',
  19551. ' var $Self = this;',
  19552. ' function Sub(Self) {',
  19553. ' $mod.THelper.Foo.call($Self, 1);',
  19554. ' $mod.THelper.Foo.call($Self, 1);',
  19555. ' $mod.THelper.Foo.call(Self, 1);',
  19556. ' $mod.THelper.Foo.call(Self, 1);',
  19557. ' $mod.THelper.Foo.call(Self, 1);',
  19558. ' $mod.THelper.Foo.call($Self, 1);',
  19559. ' };',
  19560. ' };',
  19561. '});',
  19562. 'rtl.createHelper($mod, "THelper", null, function () {',
  19563. ' this.Foo = function (w) {',
  19564. ' var $Self = this;',
  19565. ' var Result = 0;',
  19566. ' function Sub(Self) {',
  19567. ' $Self.Run(10);',
  19568. ' $Self.Run(10);',
  19569. ' $mod.THelper.Foo.call($Self, 1);',
  19570. ' $mod.THelper.Foo.call($Self, 1);',
  19571. ' $mod.THelper.Foo.call(Self, 1);',
  19572. ' $mod.THelper.Foo.call(Self, 1);',
  19573. ' $mod.THelper.Foo.call(Self, 1);',
  19574. ' $mod.THelper.Foo.call($Self, 1);',
  19575. ' };',
  19576. ' return Result;',
  19577. ' };',
  19578. '});',
  19579. '']),
  19580. LinesToStr([ // $mod.$main
  19581. '']));
  19582. end;
  19583. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  19584. begin
  19585. StartProgram(false);
  19586. Add([
  19587. 'type',
  19588. ' TObject = class',
  19589. ' class procedure Run(w: word = 10);',
  19590. ' end;',
  19591. ' THelper = class helper for TObject',
  19592. ' class function Foo(w: word = 1): word;',
  19593. ' end;',
  19594. 'class procedure TObject.Run(w: word);',
  19595. 'begin',
  19596. ' Foo;',
  19597. ' Foo();',
  19598. ' Self.Foo;',
  19599. ' Self.Foo();',
  19600. ' with Self do begin',
  19601. ' Foo;',
  19602. ' Foo();',
  19603. ' end;',
  19604. 'end;',
  19605. 'class function THelper.foo(w: word): word;',
  19606. 'begin',
  19607. ' Run;',
  19608. ' Run();',
  19609. ' Foo;',
  19610. ' Foo();',
  19611. ' Self.Foo;',
  19612. ' Self.Foo();',
  19613. ' with Self do begin',
  19614. ' Foo;',
  19615. ' Foo();',
  19616. ' end;',
  19617. 'end;',
  19618. 'var',
  19619. ' Obj: TObject;',
  19620. 'begin',
  19621. ' obj.Foo;',
  19622. ' obj.Foo();',
  19623. ' with obj do begin',
  19624. ' Foo;',
  19625. ' Foo();',
  19626. ' end;',
  19627. ' tobject.Foo;',
  19628. ' tobject.Foo();',
  19629. ' with tobject do begin',
  19630. ' Foo;',
  19631. ' Foo();',
  19632. ' end;',
  19633. '']);
  19634. ConvertProgram;
  19635. CheckSource('TestClassHelper_ClassMethod_Call',
  19636. LinesToStr([ // statements
  19637. 'rtl.createClass($mod, "TObject", null, function () {',
  19638. ' this.$init = function () {',
  19639. ' };',
  19640. ' this.$final = function () {',
  19641. ' };',
  19642. ' this.Run = function (w) {',
  19643. ' $mod.THelper.Foo.call(this, 1);',
  19644. ' $mod.THelper.Foo.call(this, 1);',
  19645. ' $mod.THelper.Foo.call(this, 1);',
  19646. ' $mod.THelper.Foo.call(this, 1);',
  19647. ' $mod.THelper.Foo.call(this, 1);',
  19648. ' $mod.THelper.Foo.call(this, 1);',
  19649. ' };',
  19650. '});',
  19651. 'rtl.createHelper($mod, "THelper", null, function () {',
  19652. ' this.Foo = function (w) {',
  19653. ' var Result = 0;',
  19654. ' this.Run(10);',
  19655. ' this.Run(10);',
  19656. ' $mod.THelper.Foo.call(this, 1);',
  19657. ' $mod.THelper.Foo.call(this, 1);',
  19658. ' $mod.THelper.Foo.call(this, 1);',
  19659. ' $mod.THelper.Foo.call(this, 1);',
  19660. ' $mod.THelper.Foo.call(this, 1);',
  19661. ' $mod.THelper.Foo.call(this, 1);',
  19662. ' return Result;',
  19663. ' };',
  19664. '});',
  19665. 'this.Obj = null;',
  19666. '']),
  19667. LinesToStr([ // $mod.$main
  19668. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19669. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  19670. 'var $with1 = $mod.Obj;',
  19671. '$mod.THelper.Foo.call($with1.$class, 1);',
  19672. '$mod.THelper.Foo.call($with1.$class, 1);',
  19673. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19674. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19675. 'var $with2 = $mod.TObject;',
  19676. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19677. '$mod.THelper.Foo.call($mod.TObject, 1);',
  19678. '']));
  19679. end;
  19680. procedure TTestModule.TestClassHelper_ClassOf;
  19681. begin
  19682. StartProgram(false);
  19683. Add([
  19684. 'type',
  19685. ' TObject = class',
  19686. ' end;',
  19687. ' TClass = class of TObject;',
  19688. ' THelper = class helper for TObject',
  19689. ' class function Foo(w: word = 1): word;',
  19690. ' end;',
  19691. 'class function THelper.foo(w: word): word;',
  19692. 'begin',
  19693. 'end;',
  19694. 'var',
  19695. ' c: TClass;',
  19696. 'begin',
  19697. ' c.Foo;',
  19698. ' c.Foo();',
  19699. ' with c do begin',
  19700. ' Foo;',
  19701. ' Foo();',
  19702. ' end;',
  19703. '']);
  19704. ConvertProgram;
  19705. CheckSource('TestClassHelper_ClassOf',
  19706. LinesToStr([ // statements
  19707. 'rtl.createClass($mod, "TObject", null, function () {',
  19708. ' this.$init = function () {',
  19709. ' };',
  19710. ' this.$final = function () {',
  19711. ' };',
  19712. '});',
  19713. 'rtl.createHelper($mod, "THelper", null, function () {',
  19714. ' this.Foo = function (w) {',
  19715. ' var Result = 0;',
  19716. ' return Result;',
  19717. ' };',
  19718. '});',
  19719. 'this.c = null;',
  19720. '']),
  19721. LinesToStr([ // $mod.$main
  19722. '$mod.THelper.Foo.call($mod.c, 1);',
  19723. '$mod.THelper.Foo.call($mod.c, 1);',
  19724. 'var $with1 = $mod.c;',
  19725. '$mod.THelper.Foo.call($with1, 1);',
  19726. '$mod.THelper.Foo.call($with1, 1);',
  19727. '']));
  19728. end;
  19729. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  19730. begin
  19731. StartProgram(false);
  19732. Add([
  19733. '{$mode objfpc}',
  19734. 'type',
  19735. ' TObject = class',
  19736. ' procedure DoIt;',
  19737. ' end;',
  19738. ' THelper = class helper for TObject',
  19739. ' procedure Fly(w: word = 1);',
  19740. ' class procedure Glide(w: word = 1);',
  19741. ' class procedure Run(w: word = 1); static;',
  19742. ' end;',
  19743. ' TFly = procedure(w: word) of object;',
  19744. ' TGlide = TFly;',
  19745. ' TRun = procedure(w: word);',
  19746. 'var',
  19747. ' f: TFly;',
  19748. ' g: TGlide;',
  19749. ' r: TRun;',
  19750. 'procedure TObject.DoIt;',
  19751. 'begin',
  19752. ' f:=@fly;',
  19753. ' g:=@glide;',
  19754. ' r:=@run;',
  19755. ' f:[email protected];',
  19756. ' g:[email protected];',
  19757. ' r:[email protected];',
  19758. ' with self do begin',
  19759. ' f:=@fly;',
  19760. ' g:=@glide;',
  19761. ' r:=@run;',
  19762. ' end;',
  19763. 'end;',
  19764. 'procedure THelper.fly(w: word);',
  19765. 'begin',
  19766. ' f:=@fly;',
  19767. ' g:=@glide;',
  19768. ' r:=@run;',
  19769. 'end;',
  19770. 'class procedure THelper.glide(w: word);',
  19771. 'begin',
  19772. ' g:=@glide;',
  19773. ' r:=@run;',
  19774. 'end;',
  19775. 'class procedure THelper.run(w: word);',
  19776. 'begin',
  19777. ' g:=@glide;',
  19778. ' r:=@run;',
  19779. 'end;',
  19780. 'var',
  19781. ' Obj: TObject;',
  19782. 'begin',
  19783. ' f:[email protected];',
  19784. ' g:[email protected];',
  19785. ' r:[email protected];',
  19786. ' with obj do begin',
  19787. ' f:=@fly;',
  19788. ' g:=@glide;',
  19789. ' r:=@run;',
  19790. ' end;',
  19791. ' g:[email protected];',
  19792. ' r:[email protected];',
  19793. ' with tobject do begin',
  19794. ' g:=@glide;',
  19795. ' r:=@run;',
  19796. ' end;',
  19797. '']);
  19798. ConvertProgram;
  19799. CheckSource('TestClassHelper_MethodRefObjFPC',
  19800. LinesToStr([ // statements
  19801. 'rtl.createClass($mod, "TObject", null, function () {',
  19802. ' this.$init = function () {',
  19803. ' };',
  19804. ' this.$final = function () {',
  19805. ' };',
  19806. ' this.DoIt = function () {',
  19807. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19808. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19809. ' $mod.r = $mod.THelper.Run;',
  19810. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19811. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19812. ' $mod.r = $mod.THelper.Run;',
  19813. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19814. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19815. ' $mod.r = $mod.THelper.Run;',
  19816. ' };',
  19817. '});',
  19818. 'rtl.createHelper($mod, "THelper", null, function () {',
  19819. ' this.Fly = function (w) {',
  19820. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  19821. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  19822. ' $mod.r = $mod.THelper.Run;',
  19823. ' };',
  19824. ' this.Glide = function (w) {',
  19825. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  19826. ' $mod.r = $mod.THelper.Run;',
  19827. ' };',
  19828. ' this.Run = function (w) {',
  19829. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  19830. ' $mod.r = $mod.THelper.Run;',
  19831. ' };',
  19832. '});',
  19833. 'this.f = null;',
  19834. 'this.g = null;',
  19835. 'this.r = null;',
  19836. 'this.Obj = null;',
  19837. '']),
  19838. LinesToStr([ // $mod.$main
  19839. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  19840. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  19841. '$mod.r = $mod.THelper.Run;',
  19842. 'var $with1 = $mod.Obj;',
  19843. '$mod.f = rtl.createCallback($with1, $mod.THelper.Fly);',
  19844. '$mod.g = rtl.createCallback($with1.$class, $mod.THelper.Glide);',
  19845. '$mod.r = $mod.THelper.Run;',
  19846. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  19847. '$mod.r = $mod.THelper.Run;',
  19848. 'var $with2 = $mod.TObject;',
  19849. '$mod.g = rtl.createCallback($with2, $mod.THelper.Glide);',
  19850. '$mod.r = $mod.THelper.Run;',
  19851. '']));
  19852. end;
  19853. procedure TTestModule.TestClassHelper_Constructor;
  19854. begin
  19855. StartProgram(false);
  19856. Add([
  19857. 'type',
  19858. ' TObject = class',
  19859. ' constructor Create;',
  19860. ' end;',
  19861. ' TClass = class of TObject;',
  19862. ' THelper = class helper for TObject',
  19863. ' constructor NewHlp(w: word);',
  19864. ' end;',
  19865. 'var',
  19866. ' obj: TObject;',
  19867. ' c: TClass;',
  19868. 'constructor TObject.Create;',
  19869. 'begin',
  19870. ' NewHlp(2);', // normal call
  19871. ' tobject.NewHlp(3);', // new instance
  19872. ' c.newhlp(4);', // new instance
  19873. 'end;',
  19874. 'constructor THelper.NewHlp(w: word);',
  19875. 'begin',
  19876. ' create;', // normal call
  19877. ' tobject.create;', // new instance
  19878. ' NewHlp(2);', // normal call
  19879. ' tobject.NewHlp(3);', // new instance
  19880. ' c.newhlp(4);', // new instance
  19881. 'end;',
  19882. 'begin',
  19883. ' obj.newhlp(2);', // normal call
  19884. ' with Obj do newhlp(12);', // normal call
  19885. ' tobject.newhlp(3);', // new instance
  19886. ' with tobject do newhlp(13);', // new instance
  19887. ' c.newhlp(4);', // new instance
  19888. ' with c do newhlp(14);', // new instance
  19889. '']);
  19890. ConvertProgram;
  19891. CheckSource('TestClassHelper_Constructor',
  19892. LinesToStr([ // statements
  19893. 'rtl.createClass($mod, "TObject", null, function () {',
  19894. ' this.$init = function () {',
  19895. ' };',
  19896. ' this.$final = function () {',
  19897. ' };',
  19898. ' this.Create = function () {',
  19899. ' $mod.THelper.NewHlp.call(this, 2);',
  19900. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19901. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19902. ' return this;',
  19903. ' };',
  19904. '});',
  19905. 'rtl.createHelper($mod, "THelper", null, function () {',
  19906. ' this.NewHlp = function (w) {',
  19907. ' this.Create();',
  19908. ' $mod.TObject.$create("Create");',
  19909. ' $mod.THelper.NewHlp.call(this, 2);',
  19910. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19911. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  19912. ' return this;',
  19913. ' };',
  19914. '});',
  19915. 'this.obj = null;',
  19916. 'this.c = null;',
  19917. '']),
  19918. LinesToStr([ // $mod.$main
  19919. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  19920. 'var $with1 = $mod.obj;',
  19921. '$mod.THelper.NewHlp.call($with1, 12);',
  19922. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  19923. 'var $with2 = $mod.TObject;',
  19924. '$with2.$create($mod.THelper.NewHlp, [13]);',
  19925. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  19926. 'var $with3 = $mod.c;',
  19927. '$with3.$create($mod.THelper.NewHlp, [14]);',
  19928. '']));
  19929. end;
  19930. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  19931. begin
  19932. StartProgram(false);
  19933. Add([
  19934. 'type',
  19935. ' TObject = class',
  19936. ' procedure Fly;',
  19937. ' end;',
  19938. ' TObjHelper = class helper for TObject',
  19939. ' procedure Fly;',
  19940. ' end;',
  19941. ' TBird = class',
  19942. ' procedure Fly;',
  19943. ' end;',
  19944. ' TBirdHelper = class helper for TBird',
  19945. ' procedure Fly;',
  19946. ' procedure Walk(w: word);',
  19947. ' end;',
  19948. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  19949. ' procedure Fly;',
  19950. ' procedure Walk(w: word);',
  19951. ' end;',
  19952. 'procedure Tobject.fly;',
  19953. 'begin',
  19954. ' inherited;', // ignore
  19955. 'end;',
  19956. 'procedure Tobjhelper.fly;',
  19957. 'begin',
  19958. ' {@TObject_Fly}inherited;',
  19959. ' inherited {@TObject_Fly}Fly;',
  19960. 'end;',
  19961. 'procedure Tbird.fly;',
  19962. 'begin',
  19963. ' {@TObjHelper_Fly}inherited;',
  19964. ' inherited {@TObjHelper_Fly}Fly;',
  19965. 'end;',
  19966. 'procedure Tbirdhelper.fly;',
  19967. 'begin',
  19968. ' {@TBird_Fly}inherited;',
  19969. ' inherited {@TBird_Fly}Fly;',
  19970. 'end;',
  19971. 'procedure Tbirdhelper.walk(w: word);',
  19972. 'begin',
  19973. 'end;',
  19974. 'procedure teagleHelper.fly;',
  19975. 'begin',
  19976. ' {@TBird_Fly}inherited;',
  19977. ' inherited {@TBird_Fly}Fly;',
  19978. 'end;',
  19979. 'procedure teagleHelper.walk(w: word);',
  19980. 'begin',
  19981. ' {@TBirdHelper_Walk}inherited;',
  19982. ' inherited {@TBirdHelper_Walk}Walk(3);',
  19983. 'end;',
  19984. 'begin',
  19985. '']);
  19986. ConvertProgram;
  19987. CheckSource('TestClassHelper_InheritedObjFPC',
  19988. LinesToStr([ // statements
  19989. 'rtl.createClass($mod, "TObject", null, function () {',
  19990. ' this.$init = function () {',
  19991. ' };',
  19992. ' this.$final = function () {',
  19993. ' };',
  19994. ' this.Fly = function () {',
  19995. ' };',
  19996. '});',
  19997. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  19998. ' this.Fly = function () {',
  19999. ' $mod.TObject.Fly.call(this);',
  20000. ' $mod.TObject.Fly.call(this);',
  20001. ' };',
  20002. '});',
  20003. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20004. ' this.Fly$1 = function () {',
  20005. ' $mod.TObjHelper.Fly.call(this);',
  20006. ' $mod.TObjHelper.Fly.call(this);',
  20007. ' };',
  20008. '});',
  20009. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20010. ' this.Fly = function () {',
  20011. ' $mod.TBird.Fly$1.call(this);',
  20012. ' $mod.TBird.Fly$1.call(this);',
  20013. ' };',
  20014. ' this.Walk = function (w) {',
  20015. ' };',
  20016. '});',
  20017. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  20018. ' this.Fly$1 = function () {',
  20019. ' $mod.TBird.Fly$1.call(this);',
  20020. ' $mod.TBird.Fly$1.call(this);',
  20021. ' };',
  20022. ' this.Walk$1 = function (w) {',
  20023. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  20024. ' $mod.TBirdHelper.Walk.call(this, 3);',
  20025. ' };',
  20026. '});',
  20027. '']),
  20028. LinesToStr([ // $mod.$main
  20029. '']));
  20030. end;
  20031. procedure TTestModule.TestClassHelper_Property;
  20032. begin
  20033. StartProgram(false);
  20034. Add([
  20035. 'type',
  20036. ' TObject = class',
  20037. ' FSize: word;',
  20038. ' function GetSpeed: word;',
  20039. ' procedure SetSpeed(Value: word);',
  20040. ' end;',
  20041. ' TObjHelper = class helper for TObject',
  20042. ' function GetLeft: word;',
  20043. ' procedure SetLeft(Value: word);',
  20044. ' property Size: word read FSize write FSize;',
  20045. ' property Speed: word read GetSpeed write SetSpeed;',
  20046. ' property Left: word read GetLeft write SetLeft;',
  20047. ' end;',
  20048. ' TBird = class',
  20049. ' property NotRight: word read GetLeft write SetLeft;',
  20050. ' procedure DoIt;',
  20051. ' end;',
  20052. 'var',
  20053. ' b: TBird;',
  20054. 'function Tobject.GetSpeed: word;',
  20055. 'begin',
  20056. ' Size:=Size+11;',
  20057. ' Speed:=Speed+12;',
  20058. ' Result:=Left+13;',
  20059. ' Left:=13;',
  20060. ' Left:=Left+13;',
  20061. ' Self.Size:=Self.Size+21;',
  20062. ' Self.Speed:=Self.Speed+22;',
  20063. ' Self.Left:=Self.Left+23;',
  20064. ' with Self do begin',
  20065. ' Size:=Size+31;',
  20066. ' Speed:=Speed+32;',
  20067. ' Left:=Left+33;',
  20068. ' end;',
  20069. 'end;',
  20070. 'procedure Tobject.SetSpeed(Value: word);',
  20071. 'begin',
  20072. 'end;',
  20073. 'function TObjHelper.GetLeft: word;',
  20074. 'begin',
  20075. ' Size:=Size+11;',
  20076. ' Speed:=Speed+12;',
  20077. ' Left:=Left+13;',
  20078. ' Self.Size:=Self.Size+21;',
  20079. ' Self.Speed:=Self.Speed+22;',
  20080. ' Self.Left:=Self.Left+23;',
  20081. ' with Self do begin',
  20082. ' Size:=Size+31;',
  20083. ' Speed:=Speed+32;',
  20084. ' Left:=Left+33;',
  20085. ' end;',
  20086. 'end;',
  20087. 'procedure TObjHelper.SetLeft(Value: word);',
  20088. 'begin',
  20089. 'end;',
  20090. 'procedure TBird.DoIt;',
  20091. 'begin',
  20092. ' NotRight:=NotRight+11;',
  20093. ' Self.NotRight:=Self.NotRight+21;',
  20094. ' with Self do begin',
  20095. ' NotRight:=NotRight+31;',
  20096. ' end;',
  20097. 'end;',
  20098. 'begin',
  20099. ' b.Size:=b.Size+11;',
  20100. ' b.Speed:=b.Speed+12;',
  20101. ' b.Left:=b.Left+13;',
  20102. ' b.NotRight:=b.NotRight+14;',
  20103. ' with b do begin',
  20104. ' Size:=Size+31;',
  20105. ' Speed:=Speed+32;',
  20106. ' Left:=Left+33;',
  20107. ' NotRight:=NotRight+34;',
  20108. ' end;',
  20109. '']);
  20110. ConvertProgram;
  20111. CheckSource('TestClassHelper_Property',
  20112. LinesToStr([ // statements
  20113. 'rtl.createClass($mod, "TObject", null, function () {',
  20114. ' this.$init = function () {',
  20115. ' this.FSize = 0;',
  20116. ' };',
  20117. ' this.$final = function () {',
  20118. ' };',
  20119. ' this.GetSpeed = function () {',
  20120. ' var Result = 0;',
  20121. ' this.FSize = this.FSize + 11;',
  20122. ' this.SetSpeed(this.GetSpeed() + 12);',
  20123. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  20124. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  20125. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20126. ' this.FSize = this.FSize + 21;',
  20127. ' this.SetSpeed(this.GetSpeed() + 22);',
  20128. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20129. ' this.FSize = this.FSize + 31;',
  20130. ' this.SetSpeed(this.GetSpeed() + 32);',
  20131. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20132. ' return Result;',
  20133. ' };',
  20134. ' this.SetSpeed = function (Value) {',
  20135. ' };',
  20136. '});',
  20137. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20138. ' this.GetLeft = function () {',
  20139. ' var Result = 0;',
  20140. ' this.FSize = this.FSize + 11;',
  20141. ' this.SetSpeed(this.GetSpeed() + 12);',
  20142. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20143. ' this.FSize = this.FSize + 21;',
  20144. ' this.SetSpeed(this.GetSpeed() + 22);',
  20145. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20146. ' this.FSize = this.FSize + 31;',
  20147. ' this.SetSpeed(this.GetSpeed() + 32);',
  20148. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20149. ' return Result;',
  20150. ' };',
  20151. ' this.SetLeft = function (Value) {',
  20152. ' };',
  20153. '});',
  20154. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20155. ' this.DoIt = function () {',
  20156. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20157. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20158. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20159. ' };',
  20160. '});',
  20161. 'this.b = null;',
  20162. '']),
  20163. LinesToStr([ // $mod.$main
  20164. '$mod.b.FSize = $mod.b.FSize + 11;',
  20165. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20166. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  20167. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  20168. 'var $with1 = $mod.b;',
  20169. '$with1.FSize = $with1.FSize + 31;',
  20170. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20171. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  20172. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  20173. '']));
  20174. end;
  20175. procedure TTestModule.TestClassHelper_Property_Array;
  20176. begin
  20177. StartProgram(false);
  20178. Add([
  20179. 'type',
  20180. ' TObject = class',
  20181. ' function GetSpeed(Index: boolean): word;',
  20182. ' procedure SetSpeed(Index: boolean; Value: word);',
  20183. ' end;',
  20184. ' TObjHelper = class helper for TObject',
  20185. ' function GetSize(Index: boolean): word;',
  20186. ' procedure SetSize(Index: boolean; Value: word);',
  20187. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  20188. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20189. ' end;',
  20190. ' TBird = class',
  20191. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  20192. ' procedure DoIt;',
  20193. ' end;',
  20194. 'var',
  20195. ' b: TBird;',
  20196. 'function Tobject.GetSpeed(Index: boolean): word;',
  20197. 'begin',
  20198. ' Result:=Size[false];',
  20199. ' Size[true]:=Size[false]+11;',
  20200. ' Speed[true]:=Speed[false]+12;',
  20201. ' Self.Size[true]:=Self.Size[false]+21;',
  20202. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20203. ' with Self do begin',
  20204. ' Size[true]:=Size[false]+31;',
  20205. ' Speed[true]:=Speed[false]+32;',
  20206. ' end;',
  20207. 'end;',
  20208. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20209. 'begin',
  20210. 'end;',
  20211. 'function TObjHelper.GetSize(Index: boolean): word;',
  20212. 'begin',
  20213. ' Size[true]:=Size[false]+11;',
  20214. ' Speed[true]:=Speed[false]+12;',
  20215. ' Self.Size[true]:=Self.Size[false]+21;',
  20216. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20217. ' with Self do begin',
  20218. ' Size[true]:=Size[false]+31;',
  20219. ' Speed[true]:=Speed[false]+32;',
  20220. ' end;',
  20221. 'end;',
  20222. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20223. 'begin',
  20224. 'end;',
  20225. 'procedure TBird.DoIt;',
  20226. 'begin',
  20227. ' Items[true]:=Items[false]+11;',
  20228. ' Self.Items[true]:=Self.Items[false]+21;',
  20229. ' with Self do Items[true]:=Items[false]+31;',
  20230. 'end;',
  20231. 'begin',
  20232. ' b.Size[true]:=b.Size[false]+11;',
  20233. ' b.Speed[true]:=b.Speed[false]+12;',
  20234. ' b.Items[true]:=b.Items[false]+13;',
  20235. ' with b do begin',
  20236. ' Size[true]:=Size[false]+21;',
  20237. ' Speed[true]:=Speed[false]+22;',
  20238. ' Items[true]:=Items[false]+23;',
  20239. ' end;',
  20240. '']);
  20241. ConvertProgram;
  20242. CheckSource('TestClassHelper_Property_Array',
  20243. LinesToStr([ // statements
  20244. 'rtl.createClass($mod, "TObject", null, function () {',
  20245. ' this.$init = function () {',
  20246. ' };',
  20247. ' this.$final = function () {',
  20248. ' };',
  20249. ' this.GetSpeed = function (Index) {',
  20250. ' var Result = 0;',
  20251. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  20252. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20253. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20254. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20255. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20256. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20257. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20258. ' return Result;',
  20259. ' };',
  20260. ' this.SetSpeed = function (Index, Value) {',
  20261. ' };',
  20262. '});',
  20263. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20264. ' this.GetSize = function (Index) {',
  20265. ' var Result = 0;',
  20266. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20267. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20268. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20269. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20270. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20271. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20272. ' return Result;',
  20273. ' };',
  20274. ' this.SetSize = function (Index, Value) {',
  20275. ' };',
  20276. '});',
  20277. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20278. ' this.DoIt = function () {',
  20279. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20280. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20281. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20282. ' };',
  20283. '});',
  20284. 'this.b = null;',
  20285. '']),
  20286. LinesToStr([ // $mod.$main
  20287. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  20288. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  20289. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  20290. 'var $with1 = $mod.b;',
  20291. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  20292. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  20293. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  20294. '']));
  20295. end;
  20296. procedure TTestModule.TestClassHelper_Property_Array_Default;
  20297. begin
  20298. StartProgram(false);
  20299. Add([
  20300. 'type',
  20301. ' TObject = class',
  20302. ' function GetSpeed(Index: boolean): word;',
  20303. ' procedure SetSpeed(Index: boolean; Value: word);',
  20304. ' end;',
  20305. ' TObjHelper = class helper for TObject',
  20306. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  20307. ' end;',
  20308. ' TBird = class',
  20309. ' end;',
  20310. ' TBirdHelper = class helper for TBird',
  20311. ' function GetSize(Index: word): boolean;',
  20312. ' procedure SetSize(Index: word; Value: boolean);',
  20313. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  20314. ' end;',
  20315. 'function Tobject.GetSpeed(Index: boolean): word;',
  20316. 'begin',
  20317. ' Self[true]:=Self[false]+1;',
  20318. 'end;',
  20319. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20320. 'begin',
  20321. 'end;',
  20322. 'function TBirdHelper.GetSize(Index: word): boolean;',
  20323. 'begin',
  20324. ' Self[1]:=not Self[2];',
  20325. 'end;',
  20326. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  20327. 'begin',
  20328. 'end;',
  20329. 'var',
  20330. ' o: TObject;',
  20331. ' b: TBird;',
  20332. 'begin',
  20333. ' o[true]:=o[false]+1;',
  20334. ' b[3]:=not b[4];',
  20335. '']);
  20336. ConvertProgram;
  20337. CheckSource('TestClassHelper_Property_Array_Default',
  20338. LinesToStr([ // statements
  20339. 'rtl.createClass($mod, "TObject", null, function () {',
  20340. ' this.$init = function () {',
  20341. ' };',
  20342. ' this.$final = function () {',
  20343. ' };',
  20344. ' this.GetSpeed = function (Index) {',
  20345. ' var Result = 0;',
  20346. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  20347. ' return Result;',
  20348. ' };',
  20349. ' this.SetSpeed = function (Index, Value) {',
  20350. ' };',
  20351. '});',
  20352. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20353. '});',
  20354. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20355. '});',
  20356. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20357. ' this.GetSize = function (Index) {',
  20358. ' var Result = false;',
  20359. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  20360. ' return Result;',
  20361. ' };',
  20362. ' this.SetSize = function (Index, Value) {',
  20363. ' };',
  20364. '});',
  20365. 'this.o = null;',
  20366. 'this.b = null;',
  20367. '']),
  20368. LinesToStr([ // $mod.$main
  20369. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  20370. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  20371. '']));
  20372. end;
  20373. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  20374. begin
  20375. StartProgram(false);
  20376. Add([
  20377. 'type',
  20378. ' TObject = class',
  20379. ' end;',
  20380. ' TObjHelper = class helper for TObject',
  20381. ' function GetItems(Index: word): TObject;',
  20382. ' procedure SetItems(Index: word; Value: TObject);',
  20383. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  20384. ' end;',
  20385. 'function Tobjhelper.GetItems(Index: word): TObject;',
  20386. 'begin',
  20387. ' Self[1][2]:=Self[3][4];',
  20388. 'end;',
  20389. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  20390. 'begin',
  20391. 'end;',
  20392. 'var',
  20393. ' o: TObject;',
  20394. 'begin',
  20395. ' o[1][2]:=o[3][4];',
  20396. '']);
  20397. ConvertProgram;
  20398. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  20399. LinesToStr([ // statements
  20400. 'rtl.createClass($mod, "TObject", null, function () {',
  20401. ' this.$init = function () {',
  20402. ' };',
  20403. ' this.$final = function () {',
  20404. ' };',
  20405. '});',
  20406. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20407. ' this.GetItems = function (Index) {',
  20408. ' var Result = null;',
  20409. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  20410. ' return Result;',
  20411. ' };',
  20412. ' this.SetItems = function (Index, Value) {',
  20413. ' };',
  20414. '});',
  20415. 'this.o = null;',
  20416. '']),
  20417. LinesToStr([ // $mod.$main
  20418. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  20419. '']));
  20420. end;
  20421. procedure TTestModule.TestClassHelper_ClassProperty;
  20422. begin
  20423. StartProgram(false);
  20424. Add([
  20425. 'type',
  20426. ' TObject = class',
  20427. ' class var FSize: word;',
  20428. ' class function GetSpeed: word;',
  20429. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  20430. ' end;',
  20431. ' TObjHelper = class helper for TObject',
  20432. ' class function GetLeft: word;',
  20433. ' class procedure SetLeft(Value: word);',
  20434. ' class property Size: word read FSize write FSize;',
  20435. ' class property Speed: word read GetSpeed write SetSpeed;',
  20436. ' class property Left: word read GetLeft write SetLeft;',
  20437. ' end;',
  20438. ' TBird = class',
  20439. ' class property NotRight: word read GetLeft write SetLeft;',
  20440. ' class procedure DoIt;',
  20441. ' end;',
  20442. ' TBirdClass = class of TBird;',
  20443. 'class function Tobject.GetSpeed: word;',
  20444. 'begin',
  20445. ' Size:=Size+11;',
  20446. ' Speed:=Speed+12;',
  20447. ' Left:=Left+13;',
  20448. ' Self.Size:=Self.Size+21;',
  20449. ' Self.Speed:=Self.Speed+22;',
  20450. ' Self.Left:=Self.Left+23;',
  20451. ' with Self do begin',
  20452. ' Size:=Size+31;',
  20453. ' Speed:=Speed+32;',
  20454. ' Left:=Left+33;',
  20455. ' end;',
  20456. 'end;',
  20457. 'class function TObjHelper.GetLeft: word;',
  20458. 'begin',
  20459. ' Size:=Size+11;',
  20460. ' Speed:=Speed+12;',
  20461. ' Left:=Left+13;',
  20462. ' Self.Size:=Self.Size+21;',
  20463. ' Self.Speed:=Self.Speed+22;',
  20464. ' Self.Left:=Self.Left+23;',
  20465. ' with Self do begin',
  20466. ' Size:=Size+31;',
  20467. ' Speed:=Speed+32;',
  20468. ' Left:=Left+33;',
  20469. ' end;',
  20470. 'end;',
  20471. 'class procedure TObjHelper.SetLeft(Value: word);',
  20472. 'begin',
  20473. 'end;',
  20474. 'class procedure TBird.DoIt;',
  20475. 'begin',
  20476. ' NotRight:=NotRight+11;',
  20477. ' Self.NotRight:=Self.NotRight+21;',
  20478. ' with Self do NotRight:=NotRight+31;',
  20479. 'end;',
  20480. 'var',
  20481. ' b: TBird;',
  20482. ' c: TBirdClass;',
  20483. 'begin',
  20484. ' b.Size:=b.Size+11;',
  20485. ' b.Speed:=b.Speed+12;',
  20486. ' b.Left:=b.Left+13;',
  20487. ' b.NotRight:=b.NotRight+14;',
  20488. ' with b do begin',
  20489. ' Size:=Size+31;',
  20490. ' Speed:=Speed+32;',
  20491. ' Left:=Left+33;',
  20492. ' NotRight:=NotRight+34;',
  20493. ' end;',
  20494. ' c.Size:=c.Size+11;',
  20495. ' c.Speed:=c.Speed+12;',
  20496. ' c.Left:=c.Left+13;',
  20497. ' c.NotRight:=c.NotRight+14;',
  20498. ' with c do begin',
  20499. ' Size:=Size+31;',
  20500. ' Speed:=Speed+32;',
  20501. ' Left:=Left+33;',
  20502. ' NotRight:=NotRight+34;',
  20503. ' end;',
  20504. ' tbird.Size:=tbird.Size+11;',
  20505. ' tbird.Speed:=tbird.Speed+12;',
  20506. ' tbird.Left:=tbird.Left+13;',
  20507. ' tbird.NotRight:=tbird.NotRight+14;',
  20508. ' with tbird do begin',
  20509. ' Size:=Size+31;',
  20510. ' Speed:=Speed+32;',
  20511. ' Left:=Left+33;',
  20512. ' NotRight:=NotRight+34;',
  20513. ' end;',
  20514. '']);
  20515. ConvertProgram;
  20516. CheckSource('TestClassHelper_ClassProperty',
  20517. LinesToStr([ // statements
  20518. 'rtl.createClass($mod, "TObject", null, function () {',
  20519. ' this.FSize = 0;',
  20520. ' this.$init = function () {',
  20521. ' };',
  20522. ' this.$final = function () {',
  20523. ' };',
  20524. ' this.GetSpeed = function () {',
  20525. ' var Result = 0;',
  20526. ' $mod.TObject.FSize = this.FSize + 11;',
  20527. ' this.SetSpeed(this.GetSpeed() + 12);',
  20528. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20529. ' $mod.TObject.FSize = this.FSize + 21;',
  20530. ' this.SetSpeed(this.GetSpeed() + 22);',
  20531. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20532. ' $mod.TObject.FSize = this.FSize + 31;',
  20533. ' this.SetSpeed(this.GetSpeed() + 32);',
  20534. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20535. ' return Result;',
  20536. ' };',
  20537. '});',
  20538. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20539. ' this.GetLeft = function () {',
  20540. ' var Result = 0;',
  20541. ' $mod.TObject.FSize = this.FSize + 11;',
  20542. ' this.SetSpeed(this.GetSpeed() + 12);',
  20543. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20544. ' $mod.TObject.FSize = this.FSize + 21;',
  20545. ' this.SetSpeed(this.GetSpeed() + 22);',
  20546. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20547. ' $mod.TObject.FSize = this.FSize + 31;',
  20548. ' this.SetSpeed(this.GetSpeed() + 32);',
  20549. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20550. ' return Result;',
  20551. ' };',
  20552. ' this.SetLeft = function (Value) {',
  20553. ' };',
  20554. '});',
  20555. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20556. ' this.DoIt = function () {',
  20557. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20558. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20559. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20560. ' };',
  20561. '});',
  20562. 'this.b = null;',
  20563. 'this.c = null;',
  20564. '']),
  20565. LinesToStr([ // $mod.$main
  20566. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  20567. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  20568. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  20569. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  20570. 'var $with1 = $mod.b;',
  20571. '$mod.TObject.FSize = $with1.FSize + 31;',
  20572. '$with1.$class.SetSpeed($with1.$class.GetSpeed() + 32);',
  20573. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 33);',
  20574. '$mod.TObjHelper.SetLeft.call($with1.$class, $mod.TObjHelper.GetLeft.call($with1.$class) + 34);',
  20575. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  20576. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20577. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  20578. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  20579. 'var $with2 = $mod.c;',
  20580. '$mod.TObject.FSize = $with2.FSize + 31;',
  20581. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20582. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 33);',
  20583. '$mod.TObjHelper.SetLeft.call($with2, $mod.TObjHelper.GetLeft.call($with2) + 34);',
  20584. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  20585. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20586. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  20587. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  20588. 'var $with3 = $mod.TBird;',
  20589. '$mod.TObject.FSize = $with3.FSize + 31;',
  20590. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20591. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  20592. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  20593. '']));
  20594. end;
  20595. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  20596. begin
  20597. StartProgram(false);
  20598. Add([
  20599. 'type',
  20600. ' TObject = class',
  20601. ' class function GetSpeed: word; static;',
  20602. ' class procedure SetSpeed(Value: word); static;',
  20603. ' end;',
  20604. ' TObjHelper = class helper for TObject',
  20605. ' class function GetLeft: word; static;',
  20606. ' class procedure SetLeft(Value: word); static;',
  20607. ' class property Speed: word read GetSpeed write SetSpeed;',
  20608. ' class property Left: word read GetLeft write SetLeft;',
  20609. ' end;',
  20610. ' TBird = class',
  20611. ' class property NotRight: word read GetLeft write SetLeft;',
  20612. ' class procedure DoIt; static;',
  20613. ' class procedure DoSome;',
  20614. ' end;',
  20615. ' TBirdClass = class of TBird;',
  20616. 'class function Tobject.GetSpeed: word;',
  20617. 'begin',
  20618. ' Speed:=Speed+12;',
  20619. ' Left:=Left+13;',
  20620. 'end;',
  20621. 'class procedure TObject.SetSpeed(Value: word);',
  20622. 'begin',
  20623. 'end;',
  20624. 'class function TObjHelper.GetLeft: word;',
  20625. 'begin',
  20626. ' Speed:=Speed+12;',
  20627. ' Left:=Left+13;',
  20628. 'end;',
  20629. 'class procedure TObjHelper.SetLeft(Value: word);',
  20630. 'begin',
  20631. 'end;',
  20632. 'class procedure TBird.DoIt;',
  20633. 'begin',
  20634. ' NotRight:=NotRight+11;',
  20635. 'end;',
  20636. 'class procedure TBird.DoSome;',
  20637. 'begin',
  20638. ' Speed:=Speed+12;',
  20639. ' Left:=Left+13;',
  20640. ' Self.Speed:=Self.Speed+22;',
  20641. ' Self.Left:=Self.Left+23;',
  20642. ' with Self do begin',
  20643. ' Speed:=Speed+32;',
  20644. ' Left:=Left+33;',
  20645. ' end;',
  20646. ' NotRight:=NotRight+11;',
  20647. ' Self.NotRight:=Self.NotRight+21;',
  20648. ' with Self do NotRight:=NotRight+31;',
  20649. 'end;',
  20650. 'var',
  20651. ' b: TBird;',
  20652. ' c: TBirdClass;',
  20653. 'begin',
  20654. ' b.Speed:=b.Speed+12;',
  20655. ' b.Left:=b.Left+13;',
  20656. ' b.NotRight:=b.NotRight+14;',
  20657. ' with b do begin',
  20658. ' Speed:=Speed+32;',
  20659. ' Left:=Left+33;',
  20660. ' NotRight:=NotRight+34;',
  20661. ' end;',
  20662. ' c.Speed:=c.Speed+12;',
  20663. ' c.Left:=c.Left+13;',
  20664. ' c.NotRight:=c.NotRight+14;',
  20665. ' with c do begin',
  20666. ' Speed:=Speed+32;',
  20667. ' Left:=Left+33;',
  20668. ' NotRight:=NotRight+34;',
  20669. ' end;',
  20670. ' tbird.Speed:=tbird.Speed+12;',
  20671. ' tbird.Left:=tbird.Left+13;',
  20672. ' tbird.NotRight:=tbird.NotRight+14;',
  20673. ' with tbird do begin',
  20674. ' Speed:=Speed+32;',
  20675. ' Left:=Left+33;',
  20676. ' NotRight:=NotRight+34;',
  20677. ' end;',
  20678. '']);
  20679. ConvertProgram;
  20680. CheckSource('TestClassHelper_ClassPropertyStatic',
  20681. LinesToStr([ // statements
  20682. 'rtl.createClass($mod, "TObject", null, function () {',
  20683. ' this.$init = function () {',
  20684. ' };',
  20685. ' this.$final = function () {',
  20686. ' };',
  20687. ' this.GetSpeed = function () {',
  20688. ' var Result = 0;',
  20689. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  20690. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20691. ' return Result;',
  20692. ' };',
  20693. ' this.SetSpeed = function (Value) {',
  20694. ' };',
  20695. '});',
  20696. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20697. ' this.GetLeft = function () {',
  20698. ' var Result = 0;',
  20699. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  20700. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20701. ' return Result;',
  20702. ' };',
  20703. ' this.SetLeft = function (Value) {',
  20704. ' };',
  20705. '});',
  20706. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20707. ' this.DoIt = function () {',
  20708. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20709. ' };',
  20710. ' this.DoSome = function () {',
  20711. ' this.SetSpeed(this.GetSpeed() + 12);',
  20712. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20713. ' this.SetSpeed(this.GetSpeed() + 22);',
  20714. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  20715. ' this.SetSpeed(this.GetSpeed() + 32);',
  20716. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20717. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  20718. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  20719. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  20720. ' };',
  20721. '});',
  20722. 'this.b = null;',
  20723. 'this.c = null;',
  20724. '']),
  20725. LinesToStr([ // $mod.$main
  20726. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20727. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20728. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20729. 'var $with1 = $mod.b;',
  20730. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  20731. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20732. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20733. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  20734. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20735. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20736. 'var $with2 = $mod.c;',
  20737. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  20738. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20739. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20740. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  20741. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  20742. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  20743. 'var $with3 = $mod.TBird;',
  20744. '$with3.SetSpeed($with3.GetSpeed() + 32);',
  20745. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  20746. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  20747. '']));
  20748. end;
  20749. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  20750. begin
  20751. StartProgram(false);
  20752. Add([
  20753. 'type',
  20754. ' TObject = class',
  20755. ' class function GetSpeed(Index: boolean): word;',
  20756. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  20757. ' end;',
  20758. ' TObjHelper = class helper for TObject',
  20759. ' class function GetSize(Index: boolean): word;',
  20760. ' class procedure SetSize(Index: boolean; Value: word);',
  20761. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  20762. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20763. ' end;',
  20764. ' TBird = class',
  20765. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  20766. ' class procedure DoIt;',
  20767. ' end;',
  20768. ' TBirdClass = class of TBird;',
  20769. 'class function Tobject.GetSpeed(Index: boolean): word;',
  20770. 'begin',
  20771. ' Size[true]:=Size[false]+11;',
  20772. ' Speed[true]:=Speed[false]+12;',
  20773. ' Self.Size[true]:=Self.Size[false]+21;',
  20774. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20775. ' with Self do begin',
  20776. ' Size[true]:=Size[false]+31;',
  20777. ' Speed[true]:=Speed[false]+32;',
  20778. ' end;',
  20779. 'end;',
  20780. 'class function TObjHelper.GetSize(Index: boolean): word;',
  20781. 'begin',
  20782. ' Size[true]:=Size[false]+11;',
  20783. ' Speed[true]:=Speed[false]+12;',
  20784. ' Self.Size[true]:=Self.Size[false]+21;',
  20785. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20786. ' with Self do begin',
  20787. ' Size[true]:=Size[false]+31;',
  20788. ' Speed[true]:=Speed[false]+32;',
  20789. ' end;',
  20790. 'end;',
  20791. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20792. 'begin',
  20793. 'end;',
  20794. 'class procedure TBird.DoIt;',
  20795. 'begin',
  20796. ' Items[true]:=Items[false]+11;',
  20797. ' Self.Items[true]:=Self.Items[false]+21;',
  20798. ' with Self do Items[true]:=Items[false]+31;',
  20799. 'end;',
  20800. 'var',
  20801. ' b: TBird;',
  20802. ' c: TBirdClass;',
  20803. 'begin',
  20804. ' b.Size[true]:=b.Size[false]+11;',
  20805. ' b.Speed[true]:=b.Speed[false]+12;',
  20806. ' b.Items[true]:=b.Items[false]+13;',
  20807. ' with b do begin',
  20808. ' Size[true]:=Size[false]+21;',
  20809. ' Speed[true]:=Speed[false]+22;',
  20810. ' Items[true]:=Items[false]+23;',
  20811. ' end;',
  20812. ' c.Size[true]:=c.Size[false]+11;',
  20813. ' c.Speed[true]:=c.Speed[false]+12;',
  20814. ' c.Items[true]:=c.Items[false]+13;',
  20815. ' with c do begin',
  20816. ' Size[true]:=Size[false]+21;',
  20817. ' Speed[true]:=Speed[false]+22;',
  20818. ' Items[true]:=Items[false]+23;',
  20819. ' end;',
  20820. ' TBird.Size[true]:=TBird.Size[false]+11;',
  20821. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  20822. ' TBird.Items[true]:=TBird.Items[false]+13;',
  20823. ' with TBird do begin',
  20824. ' Size[true]:=Size[false]+21;',
  20825. ' Speed[true]:=Speed[false]+22;',
  20826. ' Items[true]:=Items[false]+23;',
  20827. ' end;',
  20828. '']);
  20829. ConvertProgram;
  20830. CheckSource('TestClassHelper_ClassProperty_Array',
  20831. LinesToStr([ // statements
  20832. 'rtl.createClass($mod, "TObject", null, function () {',
  20833. ' this.$init = function () {',
  20834. ' };',
  20835. ' this.$final = function () {',
  20836. ' };',
  20837. ' this.GetSpeed = function (Index) {',
  20838. ' var Result = 0;',
  20839. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20840. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20841. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20842. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20843. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20844. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20845. ' return Result;',
  20846. ' };',
  20847. '});',
  20848. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20849. ' this.GetSize = function (Index) {',
  20850. ' var Result = 0;',
  20851. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20852. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20853. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20854. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20855. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20856. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  20857. ' return Result;',
  20858. ' };',
  20859. ' this.SetSize = function (Index, Value) {',
  20860. ' };',
  20861. '});',
  20862. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20863. ' this.DoIt = function () {',
  20864. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20865. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20866. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20867. ' };',
  20868. '});',
  20869. 'this.b = null;',
  20870. 'this.c = null;',
  20871. '']),
  20872. LinesToStr([ // $mod.$main
  20873. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  20874. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  20875. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  20876. 'var $with1 = $mod.b;',
  20877. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 21);',
  20878. '$with1.$class.SetSpeed(true, $with1.$class.GetSpeed(false) + 22);',
  20879. '$mod.TObjHelper.SetSize.call($with1.$class, true, $mod.TObjHelper.GetSize.call($with1.$class, false) + 23);',
  20880. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  20881. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  20882. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  20883. 'var $with2 = $mod.c;',
  20884. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 21);',
  20885. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  20886. '$mod.TObjHelper.SetSize.call($with2, true, $mod.TObjHelper.GetSize.call($with2, false) + 23);',
  20887. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  20888. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  20889. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  20890. 'var $with3 = $mod.TBird;',
  20891. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  20892. '$with3.SetSpeed(true, $with3.GetSpeed(false) + 22);',
  20893. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  20894. '']));
  20895. end;
  20896. procedure TTestModule.TestClassHelper_ForIn;
  20897. begin
  20898. StartProgram(false);
  20899. Add([
  20900. 'type',
  20901. ' TObject = class end;',
  20902. ' TItem = TObject;',
  20903. ' TEnumerator = class',
  20904. ' FCurrent: TItem;',
  20905. ' property Current: TItem read FCurrent;',
  20906. ' function MoveNext: boolean;',
  20907. ' end;',
  20908. ' TBird = class',
  20909. ' end;',
  20910. ' TBirdHelper = class helper for TBird',
  20911. ' function GetEnumerator: TEnumerator;',
  20912. ' end;',
  20913. 'function TEnumerator.MoveNext: boolean;',
  20914. 'begin',
  20915. 'end;',
  20916. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  20917. 'begin',
  20918. 'end;',
  20919. 'var',
  20920. ' b: TBird;',
  20921. ' i, i2: TItem;',
  20922. 'begin',
  20923. ' for i in b do i2:=i;']);
  20924. ConvertProgram;
  20925. CheckSource('TestClassHelper_ForIn',
  20926. LinesToStr([ // statements
  20927. 'rtl.createClass($mod, "TObject", null, function () {',
  20928. ' this.$init = function () {',
  20929. ' };',
  20930. ' this.$final = function () {',
  20931. ' };',
  20932. '});',
  20933. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  20934. ' this.$init = function () {',
  20935. ' $mod.TObject.$init.call(this);',
  20936. ' this.FCurrent = null;',
  20937. ' };',
  20938. ' this.$final = function () {',
  20939. ' this.FCurrent = undefined;',
  20940. ' $mod.TObject.$final.call(this);',
  20941. ' };',
  20942. ' this.MoveNext = function () {',
  20943. ' var Result = false;',
  20944. ' return Result;',
  20945. ' };',
  20946. '});',
  20947. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20948. '});',
  20949. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20950. ' this.GetEnumerator = function () {',
  20951. ' var Result = null;',
  20952. ' return Result;',
  20953. ' };',
  20954. '});',
  20955. 'this.b = null;',
  20956. 'this.i = null;',
  20957. 'this.i2 = null;'
  20958. ]),
  20959. LinesToStr([ // $mod.$main
  20960. 'var $in1 = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  20961. 'try {',
  20962. ' while ($in1.MoveNext()){',
  20963. ' $mod.i = $in1.FCurrent;',
  20964. ' $mod.i2 = $mod.i;',
  20965. ' }',
  20966. '} finally {',
  20967. ' $in1 = rtl.freeLoc($in1)',
  20968. '};',
  20969. '']));
  20970. end;
  20971. procedure TTestModule.TestClassHelper_PassProperty;
  20972. begin
  20973. StartProgram(false);
  20974. Add([
  20975. 'type',
  20976. ' TObject = class',
  20977. ' FField: TObject;',
  20978. ' property Field: TObject read FField write FField;',
  20979. ' end;',
  20980. ' THelper = class helper for TObject',
  20981. ' procedure Fly;',
  20982. ' class procedure Run;',
  20983. ' class procedure Jump; static;',
  20984. ' end;',
  20985. 'procedure THelper.Fly;',
  20986. 'begin',
  20987. ' Field.Fly;',
  20988. ' Field.Run;',
  20989. ' Field.Jump;',
  20990. ' with Field do begin',
  20991. ' Fly;',
  20992. ' Run;',
  20993. ' Jump;',
  20994. ' end;',
  20995. 'end;',
  20996. 'class procedure THelper.Run;',
  20997. 'begin',
  20998. 'end;',
  20999. 'class procedure THelper.Jump;',
  21000. 'begin',
  21001. 'end;',
  21002. 'var',
  21003. ' b: TObject;',
  21004. 'begin',
  21005. ' b.Field.Fly;',
  21006. ' b.Field.Run;',
  21007. ' b.Field.Jump;',
  21008. ' with b do begin',
  21009. ' Field.Run;',
  21010. ' Field.Fly;',
  21011. ' Field.Jump;',
  21012. ' end;',
  21013. ' with b.Field do begin',
  21014. ' Run;',
  21015. ' Fly;',
  21016. ' Jump;',
  21017. ' end;',
  21018. '']);
  21019. ConvertProgram;
  21020. CheckSource('TestClassHelper_PassProperty',
  21021. LinesToStr([ // statements
  21022. 'rtl.createClass($mod, "TObject", null, function () {',
  21023. ' this.$init = function () {',
  21024. ' this.FField = null;',
  21025. ' };',
  21026. ' this.$final = function () {',
  21027. ' this.FField = undefined;',
  21028. ' };',
  21029. '});',
  21030. 'rtl.createHelper($mod, "THelper", null, function () {',
  21031. ' this.Fly = function () {',
  21032. ' $mod.THelper.Fly.call(this.FField);',
  21033. ' $mod.THelper.Run.call(this.FField.$class);',
  21034. ' $mod.THelper.Jump();',
  21035. ' var $with1 = this.FField;',
  21036. ' $mod.THelper.Fly.call($with1);',
  21037. ' $mod.THelper.Run.call($with1.$class);',
  21038. ' $mod.THelper.Jump();',
  21039. ' };',
  21040. ' this.Run = function () {',
  21041. ' };',
  21042. ' this.Jump = function () {',
  21043. ' };',
  21044. '});',
  21045. 'this.b = null;',
  21046. '']),
  21047. LinesToStr([ // $mod.$main
  21048. '$mod.THelper.Fly.call($mod.b.FField);',
  21049. '$mod.THelper.Run.call($mod.b.FField.$class);',
  21050. '$mod.THelper.Jump();',
  21051. 'var $with1 = $mod.b;',
  21052. '$mod.THelper.Run.call($with1.FField.$class);',
  21053. '$mod.THelper.Fly.call($with1.FField);',
  21054. '$mod.THelper.Jump();',
  21055. 'var $with2 = $mod.b.FField;',
  21056. '$mod.THelper.Run.call($with2.$class);',
  21057. '$mod.THelper.Fly.call($with2);',
  21058. '$mod.THelper.Jump();',
  21059. '']));
  21060. end;
  21061. procedure TTestModule.TestExtClassHelper_ClassVar;
  21062. begin
  21063. StartProgram(false);
  21064. Add([
  21065. '{$modeswitch externalclass}',
  21066. 'type',
  21067. ' TExtA = class external name ''ExtObj''',
  21068. ' end;',
  21069. ' THelper = class helper for TExtA',
  21070. ' const',
  21071. ' One = 1;',
  21072. ' Two: word = 2;',
  21073. ' class var',
  21074. ' Glob: word;',
  21075. ' function Foo(w: word): word;',
  21076. ' class function Bar(w: word): word; static;',
  21077. ' end;',
  21078. 'function THelper.foo(w: word): word;',
  21079. 'begin',
  21080. ' Result:=w;',
  21081. ' Two:=One+w;',
  21082. ' Glob:=Glob;',
  21083. ' Result:=Self.Glob;',
  21084. ' Self.Glob:=Self.Glob;',
  21085. ' with Self do Glob:=Glob;',
  21086. 'end;',
  21087. 'class function THelper.bar(w: word): word;',
  21088. 'begin',
  21089. ' Result:=w;',
  21090. ' Two:=One;',
  21091. ' Glob:=Glob;',
  21092. 'end;',
  21093. 'var o: TExtA;',
  21094. 'begin',
  21095. ' texta.two:=texta.one;',
  21096. ' texta.Glob:=texta.Glob;',
  21097. ' with texta do begin',
  21098. ' two:=one;',
  21099. ' Glob:=Glob;',
  21100. ' end;',
  21101. ' o.two:=o.one;',
  21102. ' o.Glob:=o.Glob;',
  21103. ' with o do begin',
  21104. ' two:=one;',
  21105. ' Glob:=Glob;',
  21106. ' end;',
  21107. '']);
  21108. ConvertProgram;
  21109. CheckSource('TestExtClassHelper_ClassVar',
  21110. LinesToStr([ // statements
  21111. 'rtl.createHelper($mod, "THelper", null, function () {',
  21112. ' this.One = 1;',
  21113. ' this.Two = 2;',
  21114. ' this.Glob = 0;',
  21115. ' this.Foo = function (w) {',
  21116. ' var Result = 0;',
  21117. ' Result = w;',
  21118. ' $mod.THelper.Two = 1 + w;',
  21119. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21120. ' Result = $mod.THelper.Glob;',
  21121. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21122. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21123. ' return Result;',
  21124. ' };',
  21125. ' this.Bar = function (w) {',
  21126. ' var Result = 0;',
  21127. ' Result = w;',
  21128. ' $mod.THelper.Two = 1;',
  21129. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21130. ' return Result;',
  21131. ' };',
  21132. '});',
  21133. 'this.o = null;',
  21134. '']),
  21135. LinesToStr([ // $mod.$main
  21136. '$mod.THelper.Two = 1;',
  21137. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21138. '$mod.THelper.Two = 1;',
  21139. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21140. '$mod.THelper.Two = 1;',
  21141. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21142. 'var $with1 = $mod.o;',
  21143. '$mod.THelper.Two = 1;',
  21144. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21145. '']));
  21146. end;
  21147. procedure TTestModule.TestExtClassHelper_Method_Call;
  21148. begin
  21149. StartProgram(false);
  21150. Add([
  21151. '{$modeswitch externalclass}',
  21152. 'type',
  21153. ' TFly = function(w: word): word of object;',
  21154. ' TExtA = class external name ''ExtObj''',
  21155. ' procedure Run(w: word = 10);',
  21156. ' end;',
  21157. ' THelper = class helper for TExtA',
  21158. ' function Foo(w: word = 1): word;',
  21159. ' function Fly(w: word = 2): word; external name ''Fly'';',
  21160. ' end;',
  21161. 'var p: TFly;',
  21162. 'function THelper.foo(w: word): word;',
  21163. 'begin',
  21164. ' Run;',
  21165. ' Run();',
  21166. ' Run(11);',
  21167. ' Foo;',
  21168. ' Foo();',
  21169. ' Foo(12);',
  21170. ' Self.Foo;',
  21171. ' Self.Foo();',
  21172. ' Self.Foo(13);',
  21173. ' Fly;',
  21174. ' Fly();',
  21175. ' with Self do begin',
  21176. ' Foo;',
  21177. ' Foo();',
  21178. ' Foo(14);',
  21179. ' Fly;',
  21180. ' Fly();',
  21181. ' end;',
  21182. ' p:=@Fly;',
  21183. 'end;',
  21184. 'var Obj: TExtA;',
  21185. 'begin',
  21186. ' obj.Foo;',
  21187. ' obj.Foo();',
  21188. ' obj.Foo(21);',
  21189. ' obj.Fly;',
  21190. ' obj.Fly();',
  21191. ' with obj do begin',
  21192. ' Foo;',
  21193. ' Foo();',
  21194. ' Foo(22);',
  21195. ' Fly;',
  21196. ' Fly();',
  21197. ' end;',
  21198. ' p:[email protected];',
  21199. '']);
  21200. ConvertProgram;
  21201. CheckSource('TestExtClassHelper_Method_Call',
  21202. LinesToStr([ // statements
  21203. 'rtl.createHelper($mod, "THelper", null, function () {',
  21204. ' this.Foo = function (w) {',
  21205. ' var Result = 0;',
  21206. ' this.Run(10);',
  21207. ' this.Run(10);',
  21208. ' this.Run(11);',
  21209. ' $mod.THelper.Foo.call(this, 1);',
  21210. ' $mod.THelper.Foo.call(this, 1);',
  21211. ' $mod.THelper.Foo.call(this, 12);',
  21212. ' $mod.THelper.Foo.call(this, 1);',
  21213. ' $mod.THelper.Foo.call(this, 1);',
  21214. ' $mod.THelper.Foo.call(this, 13);',
  21215. ' this.Fly(2);',
  21216. ' this.Fly(2);',
  21217. ' $mod.THelper.Foo.call(this, 1);',
  21218. ' $mod.THelper.Foo.call(this, 1);',
  21219. ' $mod.THelper.Foo.call(this, 14);',
  21220. ' this.Fly(2);',
  21221. ' this.Fly(2);',
  21222. ' $mod.p = rtl.createCallback(this, "Fly");',
  21223. ' return Result;',
  21224. ' };',
  21225. '});',
  21226. 'this.p = null;',
  21227. 'this.Obj = null;',
  21228. '']),
  21229. LinesToStr([ // $mod.$main
  21230. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21231. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21232. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21233. '$mod.Obj.Fly(2);',
  21234. '$mod.Obj.Fly(2);',
  21235. 'var $with1 = $mod.Obj;',
  21236. '$mod.THelper.Foo.call($with1, 1);',
  21237. '$mod.THelper.Foo.call($with1, 1);',
  21238. '$mod.THelper.Foo.call($with1, 22);',
  21239. '$with1.Fly(2);',
  21240. '$with1.Fly(2);',
  21241. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  21242. '']));
  21243. end;
  21244. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  21245. begin
  21246. StartProgram(false);
  21247. Add([
  21248. '{$modeswitch externalclass}',
  21249. 'type',
  21250. ' TExtA = class external name ''ExtObj''',
  21251. ' procedure Run(w: word = 10);',
  21252. ' end;',
  21253. ' THelper = class helper for TExtA',
  21254. ' class procedure Fly;',
  21255. ' end;',
  21256. 'class procedure THelper.Fly;',
  21257. 'begin end;',
  21258. 'begin',
  21259. '']);
  21260. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  21261. nHelperClassMethodForExtClassMustBeStatic);
  21262. ConvertProgram;
  21263. end;
  21264. procedure TTestModule.TestRecordHelper_ClassVar;
  21265. begin
  21266. StartProgram(false);
  21267. Add([
  21268. 'type',
  21269. ' TRec = record',
  21270. ' end;',
  21271. ' THelper = record helper for TRec',
  21272. ' const',
  21273. ' One = 1;',
  21274. ' Two: word = 2;',
  21275. ' class var',
  21276. ' Glob: word;',
  21277. ' function Foo(w: word): word;',
  21278. ' class function Bar(w: word): word; static;',
  21279. ' end;',
  21280. 'function THelper.foo(w: word): word;',
  21281. 'begin',
  21282. ' Result:=w;',
  21283. ' Two:=One+w;',
  21284. ' Glob:=Glob;',
  21285. ' Result:=Self.Glob;',
  21286. ' Self.Glob:=Self.Glob;',
  21287. ' with Self do Glob:=Glob;',
  21288. ' Self:=Self;',
  21289. 'end;',
  21290. 'class function THelper.bar(w: word): word;',
  21291. 'begin',
  21292. ' Result:=w;',
  21293. ' Two:=One;',
  21294. ' Glob:=Glob;',
  21295. 'end;',
  21296. 'var r: TRec;',
  21297. 'begin',
  21298. ' trec.two:=trec.one;',
  21299. ' trec.Glob:=trec.Glob;',
  21300. ' with trec do begin',
  21301. ' two:=one;',
  21302. ' Glob:=Glob;',
  21303. ' end;',
  21304. ' r.two:=r.one;',
  21305. ' r.Glob:=r.Glob;',
  21306. ' with r do begin',
  21307. ' two:=one;',
  21308. ' Glob:=Glob;',
  21309. ' end;',
  21310. '']);
  21311. ConvertProgram;
  21312. CheckSource('TestRecordHelper_ClassVar',
  21313. LinesToStr([ // statements
  21314. 'rtl.recNewT($mod, "TRec", function () {',
  21315. ' this.$eq = function (b) {',
  21316. ' return true;',
  21317. ' };',
  21318. ' this.$assign = function (s) {',
  21319. ' return this;',
  21320. ' };',
  21321. '});',
  21322. 'rtl.createHelper($mod, "THelper", null, function () {',
  21323. ' this.One = 1;',
  21324. ' this.Two = 2;',
  21325. ' this.Glob = 0;',
  21326. ' this.Foo = function (w) {',
  21327. ' var Result = 0;',
  21328. ' Result = w;',
  21329. ' $mod.THelper.Two = 1 + w;',
  21330. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21331. ' Result = $mod.THelper.Glob;',
  21332. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21333. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21334. ' this.$assign(this);',
  21335. ' return Result;',
  21336. ' };',
  21337. ' this.Bar = function (w) {',
  21338. ' var Result = 0;',
  21339. ' Result = w;',
  21340. ' $mod.THelper.Two = 1;',
  21341. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21342. ' return Result;',
  21343. ' };',
  21344. '});',
  21345. 'this.r = $mod.TRec.$new();',
  21346. '']),
  21347. LinesToStr([ // $mod.$main
  21348. '$mod.THelper.Two = 1;',
  21349. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21350. 'var $with1 = $mod.TRec;',
  21351. '$mod.THelper.Two = 1;',
  21352. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21353. '$mod.THelper.Two = 1;',
  21354. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21355. 'var $with2 = $mod.r;',
  21356. '$mod.THelper.Two = 1;',
  21357. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21358. '']));
  21359. end;
  21360. procedure TTestModule.TestRecordHelper_Method_Call;
  21361. begin
  21362. StartProgram(false);
  21363. Add([
  21364. '{$modeswitch AdvancedRecords}',
  21365. 'type',
  21366. ' TRec = record',
  21367. ' procedure Run(w: word = 10);',
  21368. ' end;',
  21369. ' THelper = record helper for TRec',
  21370. ' function Foo(w: word = 1): word;',
  21371. ' end;',
  21372. 'procedure TRec.Run(w: word);',
  21373. 'begin',
  21374. ' Foo;',
  21375. ' Foo();',
  21376. ' Foo(2);',
  21377. ' Self.Foo;',
  21378. ' Self.Foo();',
  21379. ' Self.Foo(3);',
  21380. ' with Self do begin',
  21381. ' Foo;',
  21382. ' Foo();',
  21383. ' Foo(4);',
  21384. ' end;',
  21385. 'end;',
  21386. 'function THelper.foo(w: word): word;',
  21387. 'begin',
  21388. ' Run;',
  21389. ' Run();',
  21390. ' Run(11);',
  21391. ' Foo;',
  21392. ' Foo();',
  21393. ' Foo(12);',
  21394. ' Self.Foo;',
  21395. ' Self.Foo();',
  21396. ' Self.Foo(13);',
  21397. ' with Self do begin',
  21398. ' Foo;',
  21399. ' Foo();',
  21400. ' Foo(14);',
  21401. ' end;',
  21402. 'end;',
  21403. 'var Rec: TRec;',
  21404. 'begin',
  21405. ' Rec.Foo;',
  21406. ' Rec.Foo();',
  21407. ' Rec.Foo(21);',
  21408. ' with Rec do begin',
  21409. ' Foo;',
  21410. ' Foo();',
  21411. ' Foo(22);',
  21412. ' end;',
  21413. '']);
  21414. ConvertProgram;
  21415. CheckSource('TestRecordHelper_Method_Call',
  21416. LinesToStr([ // statements
  21417. 'rtl.recNewT($mod, "TRec", function () {',
  21418. ' this.$eq = function (b) {',
  21419. ' return true;',
  21420. ' };',
  21421. ' this.$assign = function (s) {',
  21422. ' return this;',
  21423. ' };',
  21424. ' this.Run = function (w) {',
  21425. ' $mod.THelper.Foo.call(this, 1);',
  21426. ' $mod.THelper.Foo.call(this, 1);',
  21427. ' $mod.THelper.Foo.call(this, 2);',
  21428. ' $mod.THelper.Foo.call(this, 1);',
  21429. ' $mod.THelper.Foo.call(this, 1);',
  21430. ' $mod.THelper.Foo.call(this, 3);',
  21431. ' $mod.THelper.Foo.call(this, 1);',
  21432. ' $mod.THelper.Foo.call(this, 1);',
  21433. ' $mod.THelper.Foo.call(this, 4);',
  21434. ' };',
  21435. '});',
  21436. 'rtl.createHelper($mod, "THelper", null, function () {',
  21437. ' this.Foo = function (w) {',
  21438. ' var Result = 0;',
  21439. ' this.Run(10);',
  21440. ' this.Run(10);',
  21441. ' this.Run(11);',
  21442. ' $mod.THelper.Foo.call(this, 1);',
  21443. ' $mod.THelper.Foo.call(this, 1);',
  21444. ' $mod.THelper.Foo.call(this, 12);',
  21445. ' $mod.THelper.Foo.call(this, 1);',
  21446. ' $mod.THelper.Foo.call(this, 1);',
  21447. ' $mod.THelper.Foo.call(this, 13);',
  21448. ' $mod.THelper.Foo.call(this, 1);',
  21449. ' $mod.THelper.Foo.call(this, 1);',
  21450. ' $mod.THelper.Foo.call(this, 14);',
  21451. ' return Result;',
  21452. ' };',
  21453. '});',
  21454. 'this.Rec = $mod.TRec.$new();',
  21455. '']),
  21456. LinesToStr([ // $mod.$main
  21457. '$mod.THelper.Foo.call($mod.Rec, 1);',
  21458. '$mod.THelper.Foo.call($mod.Rec, 1);',
  21459. '$mod.THelper.Foo.call($mod.Rec, 21);',
  21460. 'var $with1 = $mod.Rec;',
  21461. '$mod.THelper.Foo.call($with1, 1);',
  21462. '$mod.THelper.Foo.call($with1, 1);',
  21463. '$mod.THelper.Foo.call($with1, 22);',
  21464. '']));
  21465. end;
  21466. procedure TTestModule.TestRecordHelper_Constructor;
  21467. begin
  21468. StartProgram(false);
  21469. Add([
  21470. '{$modeswitch AdvancedRecords}',
  21471. 'type',
  21472. ' TRec = record',
  21473. ' constructor Create(w: word);',
  21474. ' end;',
  21475. ' THelper = record helper for TRec',
  21476. ' constructor NewHlp(w: word);',
  21477. ' end;',
  21478. 'var',
  21479. ' Rec: TRec;',
  21480. 'constructor TRec.Create(w: word);',
  21481. 'begin',
  21482. ' NewHlp(2);', // normal call
  21483. ' trec.NewHlp(3);', // new instance
  21484. 'end;',
  21485. 'constructor THelper.NewHlp(w: word);',
  21486. 'begin',
  21487. ' create(2);', // normal call
  21488. ' trec.create(3);', // new instance
  21489. ' NewHlp(4);', // normal call
  21490. ' trec.NewHlp(5);', // new instance
  21491. 'end;',
  21492. 'begin',
  21493. ' rec.newhlp(2);', // normal call
  21494. ' with rec do newhlp(12);', // normal call
  21495. ' trec.newhlp(3);', // new instance
  21496. ' with trec do newhlp(13);', // new instance
  21497. '']);
  21498. ConvertProgram;
  21499. CheckSource('TestRecordHelper_Constructor',
  21500. LinesToStr([ // statements
  21501. 'rtl.recNewT($mod, "TRec", function () {',
  21502. ' this.$eq = function (b) {',
  21503. ' return true;',
  21504. ' };',
  21505. ' this.$assign = function (s) {',
  21506. ' return this;',
  21507. ' };',
  21508. ' this.Create = function (w) {',
  21509. ' $mod.THelper.NewHlp.call(this, 2);',
  21510. ' $mod.THelper.$new("NewHlp", [3]);',
  21511. ' return this;',
  21512. ' };',
  21513. '}, true);',
  21514. 'rtl.createHelper($mod, "THelper", null, function () {',
  21515. ' this.NewHlp = function (w) {',
  21516. ' this.Create(2);',
  21517. ' $mod.TRec.$new().Create(3);',
  21518. ' $mod.THelper.NewHlp.call(this, 4);',
  21519. ' $mod.THelper.$new("NewHlp", [5]);',
  21520. ' return this;',
  21521. ' };',
  21522. ' this.$new = function (fn, args) {',
  21523. ' return this[fn].apply($mod.TRec.$new(), args);',
  21524. ' };',
  21525. '});',
  21526. 'this.Rec = $mod.TRec.$new();',
  21527. '']),
  21528. LinesToStr([ // $mod.$main
  21529. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  21530. 'var $with1 = $mod.Rec;',
  21531. '$mod.THelper.NewHlp.call($with1, 12);',
  21532. '$mod.THelper.$new("NewHlp", [3]);',
  21533. 'var $with2 = $mod.TRec;',
  21534. '$mod.THelper.$new("NewHlp", [13]);',
  21535. '']));
  21536. end;
  21537. procedure TTestModule.TestTypeHelper_ClassVar;
  21538. begin
  21539. StartProgram(false);
  21540. Add([
  21541. '{$modeswitch typehelpers}',
  21542. 'type',
  21543. ' THelper = type helper for byte',
  21544. ' const',
  21545. ' One = 1;',
  21546. ' Two: word = 2;',
  21547. ' class var',
  21548. ' Glob: word;',
  21549. ' function Foo(w: word): word;',
  21550. ' class function Bar(w: word): word; static;',
  21551. ' end;',
  21552. 'function THelper.foo(w: word): word;',
  21553. 'begin',
  21554. ' Result:=w;',
  21555. ' Two:=One+w;',
  21556. ' Glob:=Glob;',
  21557. ' Result:=Self.Glob;',
  21558. ' Self.Glob:=Self.Glob;',
  21559. ' with Self do Glob:=Glob;',
  21560. 'end;',
  21561. 'class function THelper.bar(w: word): word;',
  21562. 'begin',
  21563. ' Result:=w;',
  21564. ' Two:=One;',
  21565. ' Glob:=Glob;',
  21566. 'end;',
  21567. 'var b: byte;',
  21568. 'begin',
  21569. ' byte.two:=byte.one;',
  21570. ' byte.Glob:=byte.Glob;',
  21571. ' with byte do begin',
  21572. ' two:=one;',
  21573. ' Glob:=Glob;',
  21574. ' end;',
  21575. ' b.two:=b.one;',
  21576. ' b.Glob:=b.Glob;',
  21577. ' with b do begin',
  21578. ' two:=one;',
  21579. ' Glob:=Glob;',
  21580. ' end;',
  21581. '']);
  21582. ConvertProgram;
  21583. CheckSource('TestTypeHelper_ClassVar',
  21584. LinesToStr([ // statements
  21585. 'rtl.createHelper($mod, "THelper", null, function () {',
  21586. ' this.One = 1;',
  21587. ' this.Two = 2;',
  21588. ' this.Glob = 0;',
  21589. ' this.Foo = function (w) {',
  21590. ' var Result = 0;',
  21591. ' Result = w;',
  21592. ' $mod.THelper.Two = 1 + w;',
  21593. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21594. ' Result = $mod.THelper.Glob;',
  21595. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21596. ' var $with1 = this.get();',
  21597. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21598. ' return Result;',
  21599. ' };',
  21600. ' this.Bar = function (w) {',
  21601. ' var Result = 0;',
  21602. ' Result = w;',
  21603. ' $mod.THelper.Two = 1;',
  21604. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21605. ' return Result;',
  21606. ' };',
  21607. '});',
  21608. 'this.b = 0;',
  21609. '']),
  21610. LinesToStr([ // $mod.$main
  21611. '$mod.THelper.Two = 1;',
  21612. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21613. '$mod.THelper.Two = 1;',
  21614. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21615. '$mod.THelper.Two = 1;',
  21616. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21617. 'var $with1 = $mod.b;',
  21618. '$mod.THelper.Two = 1;',
  21619. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21620. '']));
  21621. end;
  21622. procedure TTestModule.TestTypeHelper_PassResultElement;
  21623. begin
  21624. StartProgram(false);
  21625. Add([
  21626. '{$modeswitch typehelpers}',
  21627. 'type',
  21628. ' THelper = type helper for word',
  21629. ' procedure DoIt(e: byte = 123);',
  21630. ' class procedure DoSome(e: byte = 456); static;',
  21631. ' end;',
  21632. 'procedure THelper.DoIt(e: byte);',
  21633. 'begin',
  21634. 'end;',
  21635. 'class procedure THelper.DoSome(e: byte);',
  21636. 'begin',
  21637. 'end;',
  21638. 'function Foo(w: word): word;',
  21639. 'begin',
  21640. ' Result.DoIt;',
  21641. ' Result.DoIt();',
  21642. ' Result.DoSome;',
  21643. ' Result.DoSome();',
  21644. ' with Result do begin',
  21645. ' DoIt;',
  21646. ' DoIt();',
  21647. ' DoSome;',
  21648. ' DoSome();',
  21649. ' end;',
  21650. 'end;',
  21651. 'begin',
  21652. '']);
  21653. ConvertProgram;
  21654. CheckSource('TestTypeHelper_PassResultElement',
  21655. LinesToStr([ // statements
  21656. 'rtl.createHelper($mod, "THelper", null, function () {',
  21657. ' this.DoIt = function (e) {',
  21658. ' };',
  21659. ' this.DoSome = function (e) {',
  21660. ' };',
  21661. '});',
  21662. 'this.Foo = function (w) {',
  21663. ' var Result = 0;',
  21664. ' $mod.THelper.DoIt.call({',
  21665. ' get: function () {',
  21666. ' return Result;',
  21667. ' },',
  21668. ' set: function (v) {',
  21669. ' Result = v;',
  21670. ' }',
  21671. ' }, 123);',
  21672. ' $mod.THelper.DoIt.call({',
  21673. ' get: function () {',
  21674. ' return Result;',
  21675. ' },',
  21676. ' set: function (v) {',
  21677. ' Result = v;',
  21678. ' }',
  21679. ' }, 123);',
  21680. ' $mod.THelper.DoSome(456);',
  21681. ' $mod.THelper.DoSome(456);',
  21682. ' $mod.THelper.DoIt.call({',
  21683. ' get: function () {',
  21684. ' return Result;',
  21685. ' },',
  21686. ' set: function (v) {',
  21687. ' Result = v;',
  21688. ' }',
  21689. ' }, 123);',
  21690. ' $mod.THelper.DoIt.call({',
  21691. ' get: function () {',
  21692. ' return Result;',
  21693. ' },',
  21694. ' set: function (v) {',
  21695. ' Result = v;',
  21696. ' }',
  21697. ' }, 123);',
  21698. ' $mod.THelper.DoSome(456);',
  21699. ' $mod.THelper.DoSome(456);',
  21700. ' return Result;',
  21701. '};',
  21702. '']),
  21703. LinesToStr([ // $mod.$main
  21704. '']));
  21705. end;
  21706. procedure TTestModule.TestTypeHelper_PassArgs;
  21707. begin
  21708. StartProgram(false);
  21709. Add([
  21710. '{$modeswitch typehelpers}',
  21711. 'type',
  21712. ' THelper = type helper for word',
  21713. ' procedure DoIt(e: byte = 123);',
  21714. ' end;',
  21715. 'procedure THelper.DoIt(e: byte);',
  21716. 'begin',
  21717. 'end;',
  21718. 'procedure FooDefault(a: word);',
  21719. 'begin',
  21720. ' a.DoIt;',
  21721. ' with a do DoIt;',
  21722. 'end;',
  21723. 'procedure FooConst(const a: word);',
  21724. 'begin',
  21725. ' a.DoIt;',
  21726. ' with a do DoIt;',
  21727. 'end;',
  21728. 'procedure FooVar(var a: word);',
  21729. 'begin',
  21730. ' a.DoIt;',
  21731. ' with a do DoIt;',
  21732. 'end;',
  21733. 'begin',
  21734. '']);
  21735. ConvertProgram;
  21736. CheckSource('TestTypeHelper_PassArgs',
  21737. LinesToStr([ // statements
  21738. 'rtl.createHelper($mod, "THelper", null, function () {',
  21739. ' this.DoIt = function (e) {',
  21740. ' };',
  21741. '});',
  21742. 'this.FooDefault = function (a) {',
  21743. ' $mod.THelper.DoIt.call({',
  21744. ' get: function () {',
  21745. ' return a;',
  21746. ' },',
  21747. ' set: function (v) {',
  21748. ' a = v;',
  21749. ' }',
  21750. ' }, 123);',
  21751. ' $mod.THelper.DoIt.call({',
  21752. ' get: function () {',
  21753. ' return a;',
  21754. ' },',
  21755. ' set: function (v) {',
  21756. ' a = v;',
  21757. ' }',
  21758. ' }, 123);',
  21759. '};',
  21760. 'this.FooConst = function (a) {',
  21761. ' $mod.THelper.DoIt.call({',
  21762. ' get: function () {',
  21763. ' return a;',
  21764. ' },',
  21765. ' set: function (v) {',
  21766. ' rtl.raiseE("EPropReadOnly");',
  21767. ' }',
  21768. ' }, 123);',
  21769. ' $mod.THelper.DoIt.call({',
  21770. ' get: function () {',
  21771. ' return a;',
  21772. ' },',
  21773. ' set: function () {',
  21774. ' rtl.raiseE("EPropReadOnly");',
  21775. ' }',
  21776. ' }, 123);',
  21777. '};',
  21778. 'this.FooVar = function (a) {',
  21779. ' $mod.THelper.DoIt.call(a, 123);',
  21780. ' var $with1 = a.get();',
  21781. ' $mod.THelper.DoIt.call(a, 123);',
  21782. '};',
  21783. '']),
  21784. LinesToStr([ // $mod.$main
  21785. '']));
  21786. end;
  21787. procedure TTestModule.TestTypeHelper_PassVarConst;
  21788. begin
  21789. StartProgram(false);
  21790. Add([
  21791. '{$modeswitch typehelpers}',
  21792. 'type',
  21793. ' THelper = type helper for word',
  21794. ' procedure DoIt(e: byte = 123);',
  21795. ' end;',
  21796. 'procedure THelper.DoIt(e: byte);',
  21797. 'begin',
  21798. 'end;',
  21799. 'var a: word;',
  21800. 'const c: word = 2;',
  21801. '{$writeableconst off}',
  21802. 'const r: word = 3;',
  21803. 'begin',
  21804. ' a.DoIt;',
  21805. ' with a do DoIt;',
  21806. ' c.DoIt;',
  21807. ' with c do DoIt;',
  21808. ' r.DoIt;',
  21809. ' with r do DoIt;',
  21810. '']);
  21811. ConvertProgram;
  21812. CheckSource('TestTypeHelper_PassVarConst',
  21813. LinesToStr([ // statements
  21814. 'rtl.createHelper($mod, "THelper", null, function () {',
  21815. ' this.DoIt = function (e) {',
  21816. ' };',
  21817. '});',
  21818. 'this.a = 0;',
  21819. 'this.c = 2;',
  21820. 'this.r = 3;',
  21821. '']),
  21822. LinesToStr([ // $mod.$main
  21823. '$mod.THelper.DoIt.call({',
  21824. ' p: $mod,',
  21825. ' get: function () {',
  21826. ' return this.p.a;',
  21827. ' },',
  21828. ' set: function (v) {',
  21829. ' this.p.a = v;',
  21830. ' }',
  21831. '}, 123);',
  21832. 'var $with1 = $mod.a;',
  21833. '$mod.THelper.DoIt.call({',
  21834. ' get: function () {',
  21835. ' return $with1;',
  21836. ' },',
  21837. ' set: function (v) {',
  21838. ' $with1 = v;',
  21839. ' }',
  21840. '}, 123);',
  21841. '$mod.THelper.DoIt.call({',
  21842. ' p: $mod,',
  21843. ' get: function () {',
  21844. ' return this.p.c;',
  21845. ' },',
  21846. ' set: function (v) {',
  21847. ' this.p.c = v;',
  21848. ' }',
  21849. '}, 123);',
  21850. 'var $with2 = $mod.c;',
  21851. '$mod.THelper.DoIt.call({',
  21852. ' get: function () {',
  21853. ' return $with2;',
  21854. ' },',
  21855. ' set: function (v) {',
  21856. ' $with2 = v;',
  21857. ' }',
  21858. '}, 123);',
  21859. '$mod.THelper.DoIt.call({',
  21860. ' get: function () {',
  21861. ' return 3;',
  21862. ' },',
  21863. ' set: function (v) {',
  21864. ' rtl.raiseE("EPropReadOnly");',
  21865. ' }',
  21866. '}, 123);',
  21867. 'var $with3 = 3;',
  21868. ' $mod.THelper.DoIt.call({',
  21869. ' get: function () {',
  21870. ' return $with3;',
  21871. ' },',
  21872. ' set: function () {',
  21873. ' rtl.raiseE("EPropReadOnly");',
  21874. ' }',
  21875. ' }, 123);',
  21876. '']));
  21877. end;
  21878. procedure TTestModule.TestTypeHelper_PassFuncResult;
  21879. begin
  21880. StartProgram(false);
  21881. Add([
  21882. '{$modeswitch typehelpers}',
  21883. 'type',
  21884. ' THelper = type helper for word',
  21885. ' procedure DoIt(e: byte = 123);',
  21886. ' end;',
  21887. 'procedure THelper.DoIt(e: byte);',
  21888. 'begin',
  21889. 'end;',
  21890. 'function Foo(b: byte = 1): word;',
  21891. 'begin',
  21892. 'end;',
  21893. 'begin',
  21894. ' Foo.DoIt;',
  21895. ' Foo().DoIt;',
  21896. ' with Foo do DoIt;',
  21897. ' with Foo() do DoIt;',
  21898. '']);
  21899. ConvertProgram;
  21900. CheckSource('TestTypeHelper_PassFuncResult',
  21901. LinesToStr([ // statements
  21902. 'rtl.createHelper($mod, "THelper", null, function () {',
  21903. ' this.DoIt = function (e) {',
  21904. ' };',
  21905. '});',
  21906. 'this.Foo = function (b) {',
  21907. ' var Result = 0;',
  21908. ' return Result;',
  21909. '};',
  21910. '']),
  21911. LinesToStr([ // $mod.$main
  21912. '$mod.THelper.DoIt.call({',
  21913. ' a: $mod.Foo(1),',
  21914. ' get: function () {',
  21915. ' return this.a;',
  21916. ' },',
  21917. ' set: function (v) {',
  21918. ' this.a = v;',
  21919. ' }',
  21920. '}, 123);',
  21921. '$mod.THelper.DoIt.call({',
  21922. ' a: $mod.Foo(1),',
  21923. ' get: function () {',
  21924. ' return this.a;',
  21925. ' },',
  21926. ' set: function (v) {',
  21927. ' this.a = v;',
  21928. ' }',
  21929. '}, 123);',
  21930. 'var $with1 = $mod.Foo(1);',
  21931. '$mod.THelper.DoIt.call({',
  21932. ' get: function () {',
  21933. ' return $with1;',
  21934. ' },',
  21935. ' set: function (v) {',
  21936. ' $with1 = v;',
  21937. ' }',
  21938. '}, 123);',
  21939. 'var $with2 = $mod.Foo(1);',
  21940. '$mod.THelper.DoIt.call({',
  21941. ' get: function () {',
  21942. ' return $with2;',
  21943. ' },',
  21944. ' set: function (v) {',
  21945. ' $with2 = v;',
  21946. ' }',
  21947. '}, 123);',
  21948. '']));
  21949. end;
  21950. procedure TTestModule.TestTypeHelper_PassPropertyField;
  21951. begin
  21952. StartProgram(false);
  21953. Add([
  21954. '{$modeswitch typehelpers}',
  21955. 'type',
  21956. ' TObject = class',
  21957. ' FField: word;',
  21958. ' procedure SetField(Value: word);',
  21959. ' property Field: word read FField write SetField;',
  21960. ' end;',
  21961. ' THelper = type helper for word',
  21962. ' procedure Fly;',
  21963. ' class procedure Run; static;',
  21964. ' end;',
  21965. 'procedure TObject.SetField(Value: word);',
  21966. 'begin',
  21967. ' Field.Fly;',
  21968. ' Field.Run;',
  21969. ' Self.Field.Fly;',
  21970. ' Self.Field.Run;',
  21971. ' with Self do begin',
  21972. ' Field.Fly;',
  21973. ' Field.Run;',
  21974. ' end;',
  21975. ' with Self.Field do begin',
  21976. ' Fly;',
  21977. ' Run;',
  21978. ' end;',
  21979. 'end;',
  21980. 'procedure THelper.Fly;',
  21981. 'begin',
  21982. 'end;',
  21983. 'class procedure THelper.Run;',
  21984. 'begin',
  21985. 'end;',
  21986. 'var',
  21987. ' o: TObject;',
  21988. 'begin',
  21989. ' o.Field.Fly;',
  21990. ' o.Field.Run;',
  21991. ' with o do begin',
  21992. ' Field.Fly;',
  21993. ' Field.Run;',
  21994. ' end;',
  21995. ' with o.Field do begin',
  21996. ' Fly;',
  21997. ' Run;',
  21998. ' end;',
  21999. '']);
  22000. ConvertProgram;
  22001. CheckSource('TestTypeHelper_PassPropertyField',
  22002. LinesToStr([ // statements
  22003. 'rtl.createClass($mod, "TObject", null, function () {',
  22004. ' this.$init = function () {',
  22005. ' this.FField = 0;',
  22006. ' };',
  22007. ' this.$final = function () {',
  22008. ' };',
  22009. ' this.SetField = function (Value) {',
  22010. ' $mod.THelper.Fly.call({',
  22011. ' p: this,',
  22012. ' get: function () {',
  22013. ' return this.p.FField;',
  22014. ' },',
  22015. ' set: function (v) {',
  22016. ' this.p.FField = v;',
  22017. ' }',
  22018. ' });',
  22019. ' $mod.THelper.Run();',
  22020. ' $mod.THelper.Fly.call({',
  22021. ' p: this,',
  22022. ' get: function () {',
  22023. ' return this.p.FField;',
  22024. ' },',
  22025. ' set: function (v) {',
  22026. ' this.p.FField = v;',
  22027. ' }',
  22028. ' });',
  22029. ' $mod.THelper.Run();',
  22030. ' $mod.THelper.Fly.call({',
  22031. ' p: this,',
  22032. ' get: function () {',
  22033. ' return this.p.FField;',
  22034. ' },',
  22035. ' set: function (v) {',
  22036. ' this.p.FField = v;',
  22037. ' }',
  22038. ' });',
  22039. ' $mod.THelper.Run();',
  22040. ' var $with1 = this.FField;',
  22041. ' $mod.THelper.Fly.call({',
  22042. ' get: function () {',
  22043. ' return $with1;',
  22044. ' },',
  22045. ' set: function (v) {',
  22046. ' $with1 = v;',
  22047. ' }',
  22048. ' });',
  22049. ' $mod.THelper.Run();',
  22050. ' };',
  22051. '});',
  22052. 'rtl.createHelper($mod, "THelper", null, function () {',
  22053. ' this.Fly = function () {',
  22054. ' };',
  22055. ' this.Run = function () {',
  22056. ' };',
  22057. '});',
  22058. 'this.o = null;',
  22059. '']),
  22060. LinesToStr([ // $mod.$main
  22061. '$mod.THelper.Fly.call({',
  22062. ' p: $mod.o,',
  22063. ' get: function () {',
  22064. ' return this.p.FField;',
  22065. ' },',
  22066. ' set: function (v) {',
  22067. ' this.p.FField = v;',
  22068. ' }',
  22069. '});',
  22070. '$mod.THelper.Run();',
  22071. 'var $with1 = $mod.o;',
  22072. '$mod.THelper.Fly.call({',
  22073. ' p: $with1,',
  22074. ' get: function () {',
  22075. ' return this.p.FField;',
  22076. ' },',
  22077. ' set: function (v) {',
  22078. ' this.p.FField = v;',
  22079. ' }',
  22080. '});',
  22081. '$mod.THelper.Run();',
  22082. 'var $with2 = $mod.o.FField;',
  22083. '$mod.THelper.Fly.call({',
  22084. ' get: function () {',
  22085. ' return $with2;',
  22086. ' },',
  22087. ' set: function (v) {',
  22088. ' $with2 = v;',
  22089. ' }',
  22090. '});',
  22091. '$mod.THelper.Run();',
  22092. '']));
  22093. end;
  22094. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  22095. begin
  22096. StartProgram(false);
  22097. Add([
  22098. '{$modeswitch typehelpers}',
  22099. 'type',
  22100. ' TObject = class',
  22101. ' FField: word;',
  22102. ' function GetField: word;',
  22103. ' property Field: word read GetField write FField;',
  22104. ' end;',
  22105. ' THelper = type helper for word',
  22106. ' procedure Fly;',
  22107. ' class procedure Run; static;',
  22108. ' end;',
  22109. 'function TObject.GetField: word;',
  22110. 'begin',
  22111. ' Field.Fly;',
  22112. ' Field.Run;',
  22113. ' Self.Field.Fly;',
  22114. ' Self.Field.Run;',
  22115. ' with Self do begin',
  22116. ' Field.Fly;',
  22117. ' Field.Run;',
  22118. ' end;',
  22119. ' with Self.Field do begin',
  22120. ' Fly;',
  22121. ' Run;',
  22122. ' end;',
  22123. 'end;',
  22124. 'procedure THelper.Fly;',
  22125. 'begin',
  22126. 'end;',
  22127. 'class procedure THelper.Run;',
  22128. 'begin',
  22129. 'end;',
  22130. 'var',
  22131. ' o: TObject;',
  22132. 'begin',
  22133. ' o.Field.Fly;',
  22134. ' o.Field.Run;',
  22135. ' with o do begin',
  22136. ' Field.Fly;',
  22137. ' Field.Run;',
  22138. ' end;',
  22139. ' with o.Field do begin',
  22140. ' Fly;',
  22141. ' Run;',
  22142. ' end;',
  22143. '']);
  22144. ConvertProgram;
  22145. CheckSource('TestTypeHelper_PassPropertyGetter',
  22146. LinesToStr([ // statements
  22147. 'rtl.createClass($mod, "TObject", null, function () {',
  22148. ' this.$init = function () {',
  22149. ' this.FField = 0;',
  22150. ' };',
  22151. ' this.$final = function () {',
  22152. ' };',
  22153. ' this.GetField = function () {',
  22154. ' var Result = 0;',
  22155. ' $mod.THelper.Fly.call({',
  22156. ' p: this.GetField(),',
  22157. ' get: function () {',
  22158. ' return this.p;',
  22159. ' },',
  22160. ' set: function (v) {',
  22161. ' this.p = v;',
  22162. ' }',
  22163. ' });',
  22164. ' $mod.THelper.Run();',
  22165. ' $mod.THelper.Fly.call({',
  22166. ' p: this.GetField(),',
  22167. ' get: function () {',
  22168. ' return this.p;',
  22169. ' },',
  22170. ' set: function (v) {',
  22171. ' this.p = v;',
  22172. ' }',
  22173. ' });',
  22174. ' $mod.THelper.Run();',
  22175. ' $mod.THelper.Fly.call({',
  22176. ' p: this.GetField(),',
  22177. ' get: function () {',
  22178. ' return this.p;',
  22179. ' },',
  22180. ' set: function (v) {',
  22181. ' this.p = v;',
  22182. ' }',
  22183. ' });',
  22184. ' $mod.THelper.Run();',
  22185. ' var $with1 = this.GetField();',
  22186. ' $mod.THelper.Fly.call({',
  22187. ' get: function () {',
  22188. ' return $with1;',
  22189. ' },',
  22190. ' set: function (v) {',
  22191. ' $with1 = v;',
  22192. ' }',
  22193. ' });',
  22194. ' $mod.THelper.Run();',
  22195. ' return Result;',
  22196. ' };',
  22197. '});',
  22198. 'rtl.createHelper($mod, "THelper", null, function () {',
  22199. ' this.Fly = function () {',
  22200. ' };',
  22201. ' this.Run = function () {',
  22202. ' };',
  22203. '});',
  22204. 'this.o = null;',
  22205. '']),
  22206. LinesToStr([ // $mod.$main
  22207. '$mod.THelper.Fly.call({',
  22208. ' p: $mod.o.GetField(),',
  22209. ' get: function () {',
  22210. ' return this.p;',
  22211. ' },',
  22212. ' set: function (v) {',
  22213. ' this.p = v;',
  22214. ' }',
  22215. '});',
  22216. '$mod.THelper.Run();',
  22217. 'var $with1 = $mod.o;',
  22218. '$mod.THelper.Fly.call({',
  22219. ' p: $with1.GetField(),',
  22220. ' get: function () {',
  22221. ' return this.p;',
  22222. ' },',
  22223. ' set: function (v) {',
  22224. ' this.p = v;',
  22225. ' }',
  22226. '});',
  22227. '$mod.THelper.Run();',
  22228. 'var $with2 = $mod.o.GetField();',
  22229. '$mod.THelper.Fly.call({',
  22230. ' get: function () {',
  22231. ' return $with2;',
  22232. ' },',
  22233. ' set: function (v) {',
  22234. ' $with2 = v;',
  22235. ' }',
  22236. '});',
  22237. '$mod.THelper.Run();',
  22238. '']));
  22239. end;
  22240. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  22241. begin
  22242. StartProgram(false);
  22243. Add([
  22244. '{$modeswitch typehelpers}',
  22245. 'type',
  22246. ' TObject = class',
  22247. ' class var FField: word;',
  22248. ' class procedure SetField(Value: word);',
  22249. ' class property Field: word read FField write SetField;',
  22250. ' end;',
  22251. ' THelper = type helper for word',
  22252. ' procedure Fly(n: byte);',
  22253. ' end;',
  22254. 'class procedure TObject.SetField(Value: word);',
  22255. 'begin',
  22256. ' Field.Fly(1);',
  22257. ' Self.Field.Fly(2);',
  22258. ' with Self do Field.Fly(3);',
  22259. ' with Self.Field do Fly(4);',
  22260. ' TObject.Field.Fly(5);',
  22261. ' with TObject do Field.Fly(6);',
  22262. ' with TObject.Field do Fly(7);',
  22263. 'end;',
  22264. 'procedure THelper.Fly(n: byte);',
  22265. 'begin',
  22266. 'end;',
  22267. 'var',
  22268. ' o: TObject;',
  22269. 'begin',
  22270. ' o.Field.Fly(11);',
  22271. ' with o do Field.Fly(12);',
  22272. ' with o.Field do Fly(13);',
  22273. ' TObject.Field.Fly(14);',
  22274. ' with TObject do Field.Fly(15);',
  22275. ' with TObject.Field do Fly(16);',
  22276. '']);
  22277. ConvertProgram;
  22278. CheckSource('TestTypeHelper_PassClassPropertyField',
  22279. LinesToStr([ // statements
  22280. 'rtl.createClass($mod, "TObject", null, function () {',
  22281. ' this.FField = 0;',
  22282. ' this.$init = function () {',
  22283. ' };',
  22284. ' this.$final = function () {',
  22285. ' };',
  22286. ' this.SetField = function (Value) {',
  22287. ' $mod.THelper.Fly.call({',
  22288. ' p: this,',
  22289. ' get: function () {',
  22290. ' return this.p.FField;',
  22291. ' },',
  22292. ' set: function (v) {',
  22293. ' $mod.TObject.FField = v;',
  22294. ' }',
  22295. ' }, 1);',
  22296. ' $mod.THelper.Fly.call({',
  22297. ' p: this,',
  22298. ' get: function () {',
  22299. ' return this.p.FField;',
  22300. ' },',
  22301. ' set: function (v) {',
  22302. ' $mod.TObject.FField = v;',
  22303. ' }',
  22304. ' }, 2);',
  22305. ' $mod.THelper.Fly.call({',
  22306. ' p: this,',
  22307. ' get: function () {',
  22308. ' return this.p.FField;',
  22309. ' },',
  22310. ' set: function (v) {',
  22311. ' $mod.TObject.FField = v;',
  22312. ' }',
  22313. ' }, 3);',
  22314. ' var $with1 = this.FField;',
  22315. ' $mod.THelper.Fly.call({',
  22316. ' get: function () {',
  22317. ' return $with1;',
  22318. ' },',
  22319. ' set: function (v) {',
  22320. ' $with1 = v;',
  22321. ' }',
  22322. ' }, 4);',
  22323. ' $mod.THelper.Fly.call({',
  22324. ' p: $mod.TObject,',
  22325. ' get: function () {',
  22326. ' return this.p.FField;',
  22327. ' },',
  22328. ' set: function (v) {',
  22329. ' $mod.TObject.FField = v;',
  22330. ' }',
  22331. ' }, 5);',
  22332. ' var $with2 = $mod.TObject;',
  22333. ' $mod.THelper.Fly.call({',
  22334. ' p: $with2,',
  22335. ' get: function () {',
  22336. ' return this.p.FField;',
  22337. ' },',
  22338. ' set: function (v) {',
  22339. ' $mod.TObject.FField = v;',
  22340. ' }',
  22341. ' }, 6);',
  22342. ' var $with3 = $mod.TObject.FField;',
  22343. ' $mod.THelper.Fly.call({',
  22344. ' get: function () {',
  22345. ' return $with3;',
  22346. ' },',
  22347. ' set: function (v) {',
  22348. ' $with3 = v;',
  22349. ' }',
  22350. ' }, 7);',
  22351. ' };',
  22352. '});',
  22353. 'rtl.createHelper($mod, "THelper", null, function () {',
  22354. ' this.Fly = function (n) {',
  22355. ' };',
  22356. '});',
  22357. 'this.o = null;',
  22358. '']),
  22359. LinesToStr([ // $mod.$main
  22360. '$mod.THelper.Fly.call({',
  22361. ' p: $mod.o,',
  22362. ' get: function () {',
  22363. ' return this.p.FField;',
  22364. ' },',
  22365. ' set: function (v) {',
  22366. ' $mod.TObject.FField = v;',
  22367. ' }',
  22368. '}, 11);',
  22369. 'var $with1 = $mod.o;',
  22370. '$mod.THelper.Fly.call({',
  22371. ' p: $with1,',
  22372. ' get: function () {',
  22373. ' return this.p.FField;',
  22374. ' },',
  22375. ' set: function (v) {',
  22376. ' $mod.TObject.FField = v;',
  22377. ' }',
  22378. '}, 12);',
  22379. 'var $with2 = $mod.o.FField;',
  22380. '$mod.THelper.Fly.call({',
  22381. ' get: function () {',
  22382. ' return $with2;',
  22383. ' },',
  22384. ' set: function (v) {',
  22385. ' $with2 = v;',
  22386. ' }',
  22387. '}, 13);',
  22388. '$mod.THelper.Fly.call({',
  22389. ' p: $mod.TObject,',
  22390. ' get: function () {',
  22391. ' return this.p.FField;',
  22392. ' },',
  22393. ' set: function (v) {',
  22394. ' $mod.TObject.FField = v;',
  22395. ' }',
  22396. '}, 14);',
  22397. 'var $with3 = $mod.TObject;',
  22398. '$mod.THelper.Fly.call({',
  22399. ' p: $with3,',
  22400. ' get: function () {',
  22401. ' return this.p.FField;',
  22402. ' },',
  22403. ' set: function (v) {',
  22404. ' $mod.TObject.FField = v;',
  22405. ' }',
  22406. '}, 15);',
  22407. 'var $with4 = $mod.TObject.FField;',
  22408. '$mod.THelper.Fly.call({',
  22409. ' get: function () {',
  22410. ' return $with4;',
  22411. ' },',
  22412. ' set: function (v) {',
  22413. ' $with4 = v;',
  22414. ' }',
  22415. '}, 16);',
  22416. '']));
  22417. end;
  22418. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  22419. begin
  22420. StartProgram(false);
  22421. Add([
  22422. '{$modeswitch typehelpers}',
  22423. 'type',
  22424. ' TObject = class',
  22425. ' class var FField: word;',
  22426. ' class function GetField: word; static;',
  22427. ' class property Field: word read GetField write FField;',
  22428. ' end;',
  22429. ' THelper = type helper for word',
  22430. ' procedure Fly(n: byte);',
  22431. ' end;',
  22432. 'class function TObject.GetField: word;',
  22433. 'begin',
  22434. ' Field.Fly(1);',
  22435. ' TObject.Field.Fly(5);',
  22436. ' with TObject do Field.Fly(6);',
  22437. ' with TObject.Field do Fly(7);',
  22438. 'end;',
  22439. 'procedure THelper.Fly(n: byte);',
  22440. 'begin',
  22441. 'end;',
  22442. 'var',
  22443. ' o: TObject;',
  22444. 'begin',
  22445. ' o.Field.Fly(11);',
  22446. ' with o do Field.Fly(12);',
  22447. ' with o.Field do Fly(13);',
  22448. '']);
  22449. ConvertProgram;
  22450. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  22451. LinesToStr([ // statements
  22452. 'rtl.createClass($mod, "TObject", null, function () {',
  22453. ' this.FField = 0;',
  22454. ' this.$init = function () {',
  22455. ' };',
  22456. ' this.$final = function () {',
  22457. ' };',
  22458. ' this.GetField = function () {',
  22459. ' var Result = 0;',
  22460. ' $mod.THelper.Fly.call({',
  22461. ' p: $mod.TObject.GetField(),',
  22462. ' get: function () {',
  22463. ' return this.p;',
  22464. ' },',
  22465. ' set: function (v) {',
  22466. ' this.p = v;',
  22467. ' }',
  22468. ' }, 1);',
  22469. ' $mod.THelper.Fly.call({',
  22470. ' p: $mod.TObject.GetField(),',
  22471. ' get: function () {',
  22472. ' return this.p;',
  22473. ' },',
  22474. ' set: function (v) {',
  22475. ' this.p = v;',
  22476. ' }',
  22477. ' }, 5);',
  22478. ' var $with1 = $mod.TObject;',
  22479. ' $mod.THelper.Fly.call({',
  22480. ' p: $with1.GetField(),',
  22481. ' get: function () {',
  22482. ' return this.p;',
  22483. ' },',
  22484. ' set: function (v) {',
  22485. ' this.p = v;',
  22486. ' }',
  22487. ' }, 6);',
  22488. ' var $with2 = $mod.TObject.GetField();',
  22489. ' $mod.THelper.Fly.call({',
  22490. ' get: function () {',
  22491. ' return $with2;',
  22492. ' },',
  22493. ' set: function (v) {',
  22494. ' $with2 = v;',
  22495. ' }',
  22496. ' }, 7);',
  22497. ' return Result;',
  22498. ' };',
  22499. '});',
  22500. 'rtl.createHelper($mod, "THelper", null, function () {',
  22501. ' this.Fly = function (n) {',
  22502. ' };',
  22503. '});',
  22504. 'this.o = null;',
  22505. '']),
  22506. LinesToStr([ // $mod.$main
  22507. '$mod.THelper.Fly.call({',
  22508. ' p: $mod.o.GetField(),',
  22509. ' get: function () {',
  22510. ' return this.p;',
  22511. ' },',
  22512. ' set: function (v) {',
  22513. ' this.p = v;',
  22514. ' }',
  22515. '}, 11);',
  22516. 'var $with1 = $mod.o;',
  22517. '$mod.THelper.Fly.call({',
  22518. ' p: $with1.GetField(),',
  22519. ' get: function () {',
  22520. ' return this.p;',
  22521. ' },',
  22522. ' set: function (v) {',
  22523. ' this.p = v;',
  22524. ' }',
  22525. '}, 12);',
  22526. 'var $with2 = $mod.o.GetField();',
  22527. '$mod.THelper.Fly.call({',
  22528. ' get: function () {',
  22529. ' return $with2;',
  22530. ' },',
  22531. ' set: function (v) {',
  22532. ' $with2 = v;',
  22533. ' }',
  22534. '}, 13);',
  22535. '']));
  22536. end;
  22537. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  22538. begin
  22539. StartProgram(false);
  22540. Add([
  22541. '{$modeswitch typehelpers}',
  22542. 'type',
  22543. ' TObject = class',
  22544. ' class var FField: word;',
  22545. ' class function GetField: word;',
  22546. ' class property Field: word read GetField write FField;',
  22547. ' end;',
  22548. ' TClass = class of TObject;',
  22549. ' THelper = type helper for word',
  22550. ' procedure Fly(n: byte);',
  22551. ' end;',
  22552. 'class function TObject.GetField: word;',
  22553. 'begin',
  22554. ' Field.Fly(1);',
  22555. ' Self.Field.Fly(5);',
  22556. ' with Self do Field.Fly(6);',
  22557. ' with Self.Field do Fly(7);',
  22558. 'end;',
  22559. 'procedure THelper.Fly(n: byte);',
  22560. 'begin',
  22561. 'end;',
  22562. 'var',
  22563. ' o: TObject;',
  22564. ' c: TClass;',
  22565. 'begin',
  22566. ' o.Field.Fly(11);',
  22567. ' with o do Field.Fly(12);',
  22568. ' with o.Field do Fly(13);',
  22569. ' c.Field.Fly(14);',
  22570. ' with c do Field.Fly(15);',
  22571. ' with c.Field do Fly(16);',
  22572. '']);
  22573. ConvertProgram;
  22574. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  22575. LinesToStr([ // statements
  22576. 'rtl.createClass($mod, "TObject", null, function () {',
  22577. ' this.FField = 0;',
  22578. ' this.$init = function () {',
  22579. ' };',
  22580. ' this.$final = function () {',
  22581. ' };',
  22582. ' this.GetField = function () {',
  22583. ' var Result = 0;',
  22584. ' $mod.THelper.Fly.call({',
  22585. ' p: this.GetField(),',
  22586. ' get: function () {',
  22587. ' return this.p;',
  22588. ' },',
  22589. ' set: function (v) {',
  22590. ' this.p = v;',
  22591. ' }',
  22592. ' }, 1);',
  22593. ' $mod.THelper.Fly.call({',
  22594. ' p: this.GetField(),',
  22595. ' get: function () {',
  22596. ' return this.p;',
  22597. ' },',
  22598. ' set: function (v) {',
  22599. ' this.p = v;',
  22600. ' }',
  22601. ' }, 5);',
  22602. ' $mod.THelper.Fly.call({',
  22603. ' p: this.GetField(),',
  22604. ' get: function () {',
  22605. ' return this.p;',
  22606. ' },',
  22607. ' set: function (v) {',
  22608. ' this.p = v;',
  22609. ' }',
  22610. ' }, 6);',
  22611. ' var $with1 = this.GetField();',
  22612. ' $mod.THelper.Fly.call({',
  22613. ' get: function () {',
  22614. ' return $with1;',
  22615. ' },',
  22616. ' set: function (v) {',
  22617. ' $with1 = v;',
  22618. ' }',
  22619. ' }, 7);',
  22620. ' return Result;',
  22621. ' };',
  22622. '});',
  22623. 'rtl.createHelper($mod, "THelper", null, function () {',
  22624. ' this.Fly = function (n) {',
  22625. ' };',
  22626. '});',
  22627. 'this.o = null;',
  22628. 'this.c = null;',
  22629. '']),
  22630. LinesToStr([ // $mod.$main
  22631. '$mod.THelper.Fly.call({',
  22632. ' p: $mod.o.$class.GetField(),',
  22633. ' get: function () {',
  22634. ' return this.p;',
  22635. ' },',
  22636. ' set: function (v) {',
  22637. ' this.p = v;',
  22638. ' }',
  22639. '}, 11);',
  22640. 'var $with1 = $mod.o;',
  22641. '$mod.THelper.Fly.call({',
  22642. ' p: $with1.$class.GetField(),',
  22643. ' get: function () {',
  22644. ' return this.p;',
  22645. ' },',
  22646. ' set: function (v) {',
  22647. ' this.p = v;',
  22648. ' }',
  22649. '}, 12);',
  22650. 'var $with2 = $mod.o.$class.GetField();',
  22651. '$mod.THelper.Fly.call({',
  22652. ' get: function () {',
  22653. ' return $with2;',
  22654. ' },',
  22655. ' set: function (v) {',
  22656. ' $with2 = v;',
  22657. ' }',
  22658. '}, 13);',
  22659. '$mod.THelper.Fly.call({',
  22660. ' p: $mod.c.GetField(),',
  22661. ' get: function () {',
  22662. ' return this.p;',
  22663. ' },',
  22664. ' set: function (v) {',
  22665. ' this.p = v;',
  22666. ' }',
  22667. '}, 14);',
  22668. 'var $with3 = $mod.c;',
  22669. '$mod.THelper.Fly.call({',
  22670. ' p: $with3.GetField(),',
  22671. ' get: function () {',
  22672. ' return this.p;',
  22673. ' },',
  22674. ' set: function (v) {',
  22675. ' this.p = v;',
  22676. ' }',
  22677. '}, 15);',
  22678. 'var $with4 = $mod.c.GetField();',
  22679. '$mod.THelper.Fly.call({',
  22680. ' get: function () {',
  22681. ' return $with4;',
  22682. ' },',
  22683. ' set: function (v) {',
  22684. ' $with4 = v;',
  22685. ' }',
  22686. '}, 16);',
  22687. '']));
  22688. end;
  22689. procedure TTestModule.TestTypeHelper_Property;
  22690. begin
  22691. StartProgram(false);
  22692. Add([
  22693. '{$modeswitch typehelpers}',
  22694. 'type',
  22695. ' THelper = type helper for word',
  22696. ' function GetSize: longint;',
  22697. ' procedure SetSize(Value: longint);',
  22698. ' property Size: longint read GetSize write SetSize;',
  22699. ' end;',
  22700. 'function THelper.GetSize: longint;',
  22701. 'begin',
  22702. ' Result:=Size+1;',
  22703. ' Size:=2;',
  22704. ' Result:=Self.Size+3;',
  22705. ' Self.Size:=4;',
  22706. ' with Self do begin',
  22707. ' Result:=Size+5;',
  22708. ' Size:=6;',
  22709. ' end;',
  22710. 'end;',
  22711. 'procedure THelper.SetSize(Value: longint);',
  22712. 'begin',
  22713. 'end;',
  22714. 'var w: word;',
  22715. 'begin',
  22716. ' w:=w.Size+7;',
  22717. ' w.Size:=w+8;',
  22718. ' with w do begin',
  22719. ' w:=Size+9;',
  22720. ' Size:=w+10;',
  22721. ' end;',
  22722. '']);
  22723. ConvertProgram;
  22724. CheckSource('TestTypeHelper_Property',
  22725. LinesToStr([ // statements
  22726. 'rtl.createHelper($mod, "THelper", null, function () {',
  22727. ' this.GetSize = function () {',
  22728. ' var Result = 0;',
  22729. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  22730. ' $mod.THelper.SetSize.call(this, 2);',
  22731. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  22732. ' $mod.THelper.SetSize.call(this, 4);',
  22733. ' var $with1 = this.get();',
  22734. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  22735. ' $mod.THelper.SetSize.call(this, 6);',
  22736. ' return Result;',
  22737. ' };',
  22738. ' this.SetSize = function (Value) {',
  22739. ' };',
  22740. '});',
  22741. 'this.w = 0;',
  22742. '']),
  22743. LinesToStr([ // $mod.$main
  22744. '$mod.w = $mod.THelper.GetSize.call({',
  22745. ' p: $mod,',
  22746. ' get: function () {',
  22747. ' return this.p.w;',
  22748. ' },',
  22749. ' set: function (v) {',
  22750. ' this.p.w = v;',
  22751. ' }',
  22752. '}) + 7;',
  22753. '$mod.THelper.SetSize.call({',
  22754. ' p: $mod,',
  22755. ' get: function () {',
  22756. ' return this.p.w;',
  22757. ' },',
  22758. ' set: function (v) {',
  22759. ' this.p.w = v;',
  22760. ' }',
  22761. '}, $mod.w + 8);',
  22762. 'var $with1 = $mod.w;',
  22763. '$mod.w = $mod.THelper.GetSize.call({',
  22764. ' get: function () {',
  22765. ' return $with1;',
  22766. ' },',
  22767. ' set: function (v) {',
  22768. ' $with1 = v;',
  22769. ' }',
  22770. '}) + 9;',
  22771. '$mod.THelper.SetSize.call({',
  22772. ' get: function () {',
  22773. ' return $with1;',
  22774. ' },',
  22775. ' set: function (v) {',
  22776. ' $with1 = v;',
  22777. ' }',
  22778. '}, $mod.w + 10);',
  22779. '']));
  22780. end;
  22781. procedure TTestModule.TestTypeHelper_Property_Array;
  22782. begin
  22783. StartProgram(false);
  22784. Add([
  22785. '{$modeswitch typehelpers}',
  22786. 'type',
  22787. ' THelper = type helper for word',
  22788. ' function GetItems(Index: byte): boolean;',
  22789. ' procedure SetItems(Index: byte; Value: boolean);',
  22790. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  22791. ' end;',
  22792. 'function THelper.GetItems(Index: byte): boolean;',
  22793. 'begin',
  22794. ' Result:=Items[1];',
  22795. ' Items[2]:=false;',
  22796. ' Result:=Self.Items[3];',
  22797. ' Self.Items[4]:=true;',
  22798. ' with Self do begin',
  22799. ' Result:=Items[5];',
  22800. ' Items[6]:=false;',
  22801. ' end;',
  22802. 'end;',
  22803. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  22804. 'begin',
  22805. 'end;',
  22806. 'var',
  22807. ' w: word;',
  22808. ' b: boolean;',
  22809. 'begin',
  22810. ' b:=w.Items[1];',
  22811. ' w.Items[2]:=b;',
  22812. ' with w do begin',
  22813. ' b:=Items[3];',
  22814. ' Items[4]:=b;',
  22815. ' end;',
  22816. '']);
  22817. ConvertProgram;
  22818. CheckSource('TestTypeHelper_Property_Array',
  22819. LinesToStr([ // statements
  22820. 'rtl.createHelper($mod, "THelper", null, function () {',
  22821. ' this.GetItems = function (Index) {',
  22822. ' var Result = false;',
  22823. ' Result = $mod.THelper.GetItems.call(this, 1);',
  22824. ' $mod.THelper.SetItems.call(this, 2, false);',
  22825. ' Result = $mod.THelper.GetItems.call(this, 3);',
  22826. ' $mod.THelper.SetItems.call(this, 4, true);',
  22827. ' var $with1 = this.get();',
  22828. ' Result = $mod.THelper.GetItems.call(this, 5);',
  22829. ' $mod.THelper.SetItems.call(this, 6, false);',
  22830. ' return Result;',
  22831. ' };',
  22832. ' this.SetItems = function (Index, Value) {',
  22833. ' };',
  22834. '});',
  22835. 'this.w = 0;',
  22836. 'this.b = false;',
  22837. '']),
  22838. LinesToStr([ // $mod.$main
  22839. '$mod.b = $mod.THelper.GetItems.call({',
  22840. ' p: $mod,',
  22841. ' get: function () {',
  22842. ' return this.p.w;',
  22843. ' },',
  22844. ' set: function (v) {',
  22845. ' this.p.w = v;',
  22846. ' }',
  22847. '}, 1);',
  22848. '$mod.THelper.SetItems.call({',
  22849. ' p: $mod,',
  22850. ' get: function () {',
  22851. ' return this.p.w;',
  22852. ' },',
  22853. ' set: function (v) {',
  22854. ' this.p.w = v;',
  22855. ' }',
  22856. '}, 2, $mod.b);',
  22857. 'var $with1 = $mod.w;',
  22858. '$mod.b = $mod.THelper.GetItems.call({',
  22859. ' get: function () {',
  22860. ' return $with1;',
  22861. ' },',
  22862. ' set: function (v) {',
  22863. ' $with1 = v;',
  22864. ' }',
  22865. '}, 3);',
  22866. '$mod.THelper.SetItems.call({',
  22867. ' get: function () {',
  22868. ' return $with1;',
  22869. ' },',
  22870. ' set: function (v) {',
  22871. ' $with1 = v;',
  22872. ' }',
  22873. '}, 4, $mod.b);',
  22874. '']));
  22875. end;
  22876. procedure TTestModule.TestTypeHelper_ClassProperty;
  22877. begin
  22878. StartProgram(false);
  22879. Add([
  22880. '{$modeswitch typehelpers}',
  22881. 'type',
  22882. ' THelper = type helper for word',
  22883. ' class function GetSize: longint; static;',
  22884. ' class procedure SetSize(Value: longint); static;',
  22885. ' class property Size: longint read GetSize write SetSize;',
  22886. ' end;',
  22887. 'class function THelper.GetSize: longint;',
  22888. 'begin',
  22889. ' Result:=Size+1;',
  22890. ' Size:=2;',
  22891. 'end;',
  22892. 'class procedure THelper.SetSize(Value: longint);',
  22893. 'begin',
  22894. 'end;',
  22895. 'begin',
  22896. '']);
  22897. ConvertProgram;
  22898. CheckSource('TestTypeHelper_ClassProperty',
  22899. LinesToStr([ // statements
  22900. 'rtl.createHelper($mod, "THelper", null, function () {',
  22901. ' this.GetSize = function () {',
  22902. ' var Result = 0;',
  22903. ' Result = $mod.THelper.GetSize() + 1;',
  22904. ' $mod.THelper.SetSize(2);',
  22905. ' return Result;',
  22906. ' };',
  22907. ' this.SetSize = function (Value) {',
  22908. ' };',
  22909. '});',
  22910. '']),
  22911. LinesToStr([ // $mod.$main
  22912. '']));
  22913. end;
  22914. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  22915. begin
  22916. StartProgram(false);
  22917. Add([
  22918. '{$modeswitch typehelpers}',
  22919. 'type',
  22920. ' THelper = type helper for word',
  22921. ' class function GetItems(Index: byte): boolean; static;',
  22922. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  22923. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  22924. ' end;',
  22925. 'class function THelper.GetItems(Index: byte): boolean;',
  22926. 'begin',
  22927. ' Result:=Items[1];',
  22928. ' Items[2]:=false;',
  22929. 'end;',
  22930. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  22931. 'begin',
  22932. 'end;',
  22933. 'var',
  22934. ' w: word;',
  22935. ' b: boolean;',
  22936. 'begin',
  22937. ' b:=w.Items[1];',
  22938. ' w.Items[2]:=b;',
  22939. ' with w do begin',
  22940. ' b:=Items[3];',
  22941. ' Items[4]:=b;',
  22942. ' end;',
  22943. '']);
  22944. ConvertProgram;
  22945. CheckSource('TestTypeHelper_ClassProperty_Array',
  22946. LinesToStr([ // statements
  22947. 'rtl.createHelper($mod, "THelper", null, function () {',
  22948. ' this.GetItems = function (Index) {',
  22949. ' var Result = false;',
  22950. ' Result = $mod.THelper.GetItems(1);',
  22951. ' $mod.THelper.SetItems(2, false);',
  22952. ' return Result;',
  22953. ' };',
  22954. ' this.SetItems = function (Index, Value) {',
  22955. ' };',
  22956. '});',
  22957. 'this.w = 0;',
  22958. 'this.b = false;',
  22959. '']),
  22960. LinesToStr([ // $mod.$main
  22961. '$mod.b = $mod.THelper.GetItems(1);',
  22962. '$mod.THelper.SetItems(2, $mod.b);',
  22963. 'var $with1 = $mod.w;',
  22964. '$mod.b = $mod.THelper.GetItems(3);',
  22965. '$mod.THelper.SetItems(4, $mod.b);',
  22966. '']));
  22967. end;
  22968. procedure TTestModule.TestTypeHelper_ClassMethod;
  22969. begin
  22970. StartProgram(false);
  22971. Add([
  22972. '{$modeswitch typehelpers}',
  22973. 'type',
  22974. ' THelper = type helper for word',
  22975. ' class procedure DoStatic; static;',
  22976. ' end;',
  22977. 'class procedure THelper.DoStatic;',
  22978. 'begin',
  22979. ' DoStatic;',
  22980. ' DoStatic();',
  22981. 'end;',
  22982. 'var w: word;',
  22983. 'begin',
  22984. ' w.DoStatic;',
  22985. ' w.DoStatic();',
  22986. '']);
  22987. ConvertProgram;
  22988. CheckSource('TestTypeHelper_ClassMethod',
  22989. LinesToStr([ // statements
  22990. 'rtl.createHelper($mod, "THelper", null, function () {',
  22991. ' this.DoStatic = function () {',
  22992. ' $mod.THelper.DoStatic();',
  22993. ' $mod.THelper.DoStatic();',
  22994. ' };',
  22995. '});',
  22996. 'this.w = 0;',
  22997. '']),
  22998. LinesToStr([ // $mod.$main
  22999. '$mod.THelper.DoStatic();',
  23000. '$mod.THelper.DoStatic();',
  23001. '']));
  23002. end;
  23003. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  23004. begin
  23005. StartProgram(false);
  23006. Add([
  23007. '{$modeswitch typehelpers}',
  23008. 'type',
  23009. ' THelper = type helper for word',
  23010. ' procedure Run; external name ''Run'';',
  23011. ' end;',
  23012. 'var w: word;',
  23013. 'begin',
  23014. ' w.Run;',
  23015. '']);
  23016. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  23017. ConvertProgram;
  23018. end;
  23019. procedure TTestModule.TestTypeHelper_Constructor;
  23020. begin
  23021. StartProgram(false);
  23022. Add([
  23023. '{$modeswitch typehelpers}',
  23024. 'type',
  23025. ' THelper = type helper for word',
  23026. ' constructor Init(e: longint);',
  23027. ' end;',
  23028. 'constructor THelper.Init(e: longint);',
  23029. 'begin',
  23030. ' Self:=e;',
  23031. ' Init(e+1);',
  23032. 'end;',
  23033. 'var w: word;',
  23034. 'begin',
  23035. ' w:=word.Init(2);',
  23036. ' w:=w.Init(3);',
  23037. ' with word do w:=Init(4);',
  23038. ' with w do w:=Init(5);',
  23039. '']);
  23040. ConvertProgram;
  23041. CheckSource('TestTypeHelper_Constructor',
  23042. LinesToStr([ // statements
  23043. 'rtl.createHelper($mod, "THelper", null, function () {',
  23044. ' this.Init = function (e) {',
  23045. ' this.set(e);',
  23046. ' $mod.THelper.Init.call(this, e + 1);',
  23047. ' return this.get();',
  23048. ' };',
  23049. ' this.$new = function (fn, args) {',
  23050. ' return this[fn].apply({',
  23051. ' p: 0,',
  23052. ' get: function () {',
  23053. ' return this.p;',
  23054. ' },',
  23055. ' set: function (v) {',
  23056. ' this.p = v;',
  23057. ' }',
  23058. ' }, args);',
  23059. ' };',
  23060. '});',
  23061. 'this.w = 0;',
  23062. '']),
  23063. LinesToStr([ // $mod.$main
  23064. '$mod.w = $mod.THelper.$new("Init", [2]);',
  23065. '$mod.w = $mod.THelper.Init.call({',
  23066. ' p: $mod,',
  23067. ' get: function () {',
  23068. ' return this.p.w;',
  23069. ' },',
  23070. ' set: function (v) {',
  23071. ' this.p.w = v;',
  23072. ' }',
  23073. '}, 3);',
  23074. '$mod.w = $mod.THelper.$new("Init", [4]);',
  23075. 'var $with1 = $mod.w;',
  23076. '$mod.w = $mod.THelper.Init.call({',
  23077. ' get: function () {',
  23078. ' return $with1;',
  23079. ' },',
  23080. ' set: function (v) {',
  23081. ' $with1 = v;',
  23082. ' }',
  23083. '}, 5);',
  23084. '']));
  23085. end;
  23086. procedure TTestModule.TestTypeHelper_Word;
  23087. begin
  23088. StartProgram(false);
  23089. Add([
  23090. '{$modeswitch typehelpers}',
  23091. 'type',
  23092. ' THelper = type helper for word',
  23093. ' procedure DoIt(e: byte = 123);',
  23094. ' end;',
  23095. 'procedure THelper.DoIt(e: byte);',
  23096. 'begin',
  23097. ' Self:=e;',
  23098. ' Self:=Self+1;',
  23099. ' with Self do Doit;',
  23100. 'end;',
  23101. 'begin',
  23102. ' word(3).DoIt;',
  23103. '']);
  23104. ConvertProgram;
  23105. CheckSource('TestTypeHelper_Word',
  23106. LinesToStr([ // statements
  23107. 'rtl.createHelper($mod, "THelper", null, function () {',
  23108. ' this.DoIt = function (e) {',
  23109. ' this.set(e);',
  23110. ' this.set(this.get() + 1);',
  23111. ' var $with1 = this.get();',
  23112. ' $mod.THelper.DoIt.call(this, 123);',
  23113. ' };',
  23114. '});',
  23115. '']),
  23116. LinesToStr([ // $mod.$main
  23117. '$mod.THelper.DoIt.call({',
  23118. ' get: function () {',
  23119. ' return 3;',
  23120. ' },',
  23121. ' set: function (v) {',
  23122. ' rtl.raiseE("EPropReadOnly");',
  23123. ' }',
  23124. '}, 123);',
  23125. '']));
  23126. end;
  23127. procedure TTestModule.TestTypeHelper_Double;
  23128. begin
  23129. StartProgram(false);
  23130. Add([
  23131. '{$modeswitch typehelpers}',
  23132. 'type',
  23133. ' Float = type double;',
  23134. ' THelper = type helper for Float',
  23135. ' const NPI = 3.141592;',
  23136. ' function ToStr: String;',
  23137. ' end;',
  23138. 'function THelper.ToStr: String;',
  23139. 'begin',
  23140. 'end;',
  23141. 'procedure DoIt(s: string);',
  23142. 'begin',
  23143. 'end;',
  23144. 'var f: Float;',
  23145. 'begin',
  23146. ' DoIt(f.toStr);',
  23147. ' DoIt(f.toStr());',
  23148. ' (f*f).toStr;',
  23149. ' DoIt((f*f).toStr);',
  23150. '']);
  23151. ConvertProgram;
  23152. CheckSource('TestTypeHelper_Double',
  23153. LinesToStr([ // statements
  23154. 'rtl.createHelper($mod, "THelper", null, function () {',
  23155. ' this.NPI = 3.141592;',
  23156. ' this.ToStr = function () {',
  23157. ' var Result = "";',
  23158. ' return Result;',
  23159. ' };',
  23160. '});',
  23161. 'this.DoIt = function (s) {',
  23162. '};',
  23163. 'this.f = 0.0;',
  23164. '']),
  23165. LinesToStr([ // $mod.$main
  23166. '$mod.DoIt($mod.THelper.ToStr.call({',
  23167. ' p: $mod,',
  23168. ' get: function () {',
  23169. ' return this.p.f;',
  23170. ' },',
  23171. ' set: function (v) {',
  23172. ' this.p.f = v;',
  23173. ' }',
  23174. '}));',
  23175. '$mod.DoIt($mod.THelper.ToStr.call({',
  23176. ' p: $mod,',
  23177. ' get: function () {',
  23178. ' return this.p.f;',
  23179. ' },',
  23180. ' set: function (v) {',
  23181. ' this.p.f = v;',
  23182. ' }',
  23183. '}));',
  23184. '$mod.THelper.ToStr.call({',
  23185. ' a: $mod.f * $mod.f,',
  23186. ' get: function () {',
  23187. ' return this.a;',
  23188. ' },',
  23189. ' set: function (v) {',
  23190. ' rtl.raiseE("EPropReadOnly");',
  23191. ' }',
  23192. '});',
  23193. '$mod.DoIt($mod.THelper.ToStr.call({',
  23194. ' a: $mod.f * $mod.f,',
  23195. ' get: function () {',
  23196. ' return this.a;',
  23197. ' },',
  23198. ' set: function (v) {',
  23199. ' rtl.raiseE("EPropReadOnly");',
  23200. ' }',
  23201. '}));',
  23202. '']));
  23203. end;
  23204. procedure TTestModule.TestTypeHelper_StringChar;
  23205. begin
  23206. StartProgram(false);
  23207. Add([
  23208. '{$modeswitch typehelpers}',
  23209. 'type',
  23210. ' TStringHelper = type helper for string',
  23211. ' procedure DoIt(e: byte = 123);',
  23212. ' end;',
  23213. ' TCharHelper = type helper for char',
  23214. ' procedure Fly;',
  23215. ' end;',
  23216. 'procedure TStringHelper.DoIt(e: byte);',
  23217. 'begin',
  23218. ' Self[1]:=''c'';',
  23219. ' Self[2]:=Self[3];',
  23220. 'end;',
  23221. 'procedure TCharHelper.Fly;',
  23222. 'begin',
  23223. ' Self:=''c'';',
  23224. 'end;',
  23225. 'begin',
  23226. ' ''abc''.DoIt;',
  23227. ' ''xyz''.DoIt();',
  23228. ' ''c''.Fly();',
  23229. '']);
  23230. ConvertProgram;
  23231. CheckSource('TestTypeHelper_StringChar',
  23232. LinesToStr([ // statements
  23233. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  23234. ' this.DoIt = function (e) {',
  23235. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  23236. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  23237. ' };',
  23238. '});',
  23239. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  23240. ' this.Fly = function () {',
  23241. ' this.set("c");',
  23242. ' };',
  23243. '});',
  23244. '']),
  23245. LinesToStr([ // $mod.$main
  23246. '$mod.TStringHelper.DoIt.call({',
  23247. ' get: function () {',
  23248. ' return "abc";',
  23249. ' },',
  23250. ' set: function (v) {',
  23251. ' rtl.raiseE("EPropReadOnly");',
  23252. ' }',
  23253. '}, 123);',
  23254. '$mod.TStringHelper.DoIt.call({',
  23255. ' get: function () {',
  23256. ' return "xyz";',
  23257. ' },',
  23258. ' set: function (v) {',
  23259. ' rtl.raiseE("EPropReadOnly");',
  23260. ' }',
  23261. '}, 123);',
  23262. '$mod.TCharHelper.Fly.call({',
  23263. ' get: function () {',
  23264. ' return "c";',
  23265. ' },',
  23266. ' set: function (v) {',
  23267. ' rtl.raiseE("EPropReadOnly");',
  23268. ' }',
  23269. '});',
  23270. '']));
  23271. end;
  23272. procedure TTestModule.TestTypeHelper_JSValue;
  23273. begin
  23274. StartProgram(false);
  23275. Add([
  23276. '{$modeswitch typehelpers}',
  23277. 'type',
  23278. ' TExtValue = type jsvalue;',
  23279. ' THelper = type helper for TExtValue',
  23280. ' function ToStr: String;',
  23281. ' end;',
  23282. 'function THelper.ToStr: String;',
  23283. 'begin',
  23284. 'end;',
  23285. 'var',
  23286. ' s: string;',
  23287. ' v: TExtValue;',
  23288. 'begin',
  23289. ' s:=v.toStr;',
  23290. ' s:=v.toStr();',
  23291. ' TExtValue(s).toStr;',
  23292. '']);
  23293. ConvertProgram;
  23294. CheckSource('TestTypeHelper_JSValue',
  23295. LinesToStr([ // statements
  23296. 'rtl.createHelper($mod, "THelper", null, function () {',
  23297. ' this.ToStr = function () {',
  23298. ' var Result = "";',
  23299. ' return Result;',
  23300. ' };',
  23301. '});',
  23302. 'this.s = "";',
  23303. 'this.v = undefined;',
  23304. '']),
  23305. LinesToStr([ // $mod.$main
  23306. '$mod.s = $mod.THelper.ToStr.call({',
  23307. ' p: $mod,',
  23308. ' get: function () {',
  23309. ' return this.p.v;',
  23310. ' },',
  23311. ' set: function (v) {',
  23312. ' this.p.v = v;',
  23313. ' }',
  23314. '});',
  23315. '$mod.s = $mod.THelper.ToStr.call({',
  23316. ' p: $mod,',
  23317. ' get: function () {',
  23318. ' return this.p.v;',
  23319. ' },',
  23320. ' set: function (v) {',
  23321. ' this.p.v = v;',
  23322. ' }',
  23323. '});',
  23324. '$mod.THelper.ToStr.call({',
  23325. ' p: $mod,',
  23326. ' get: function () {',
  23327. ' return this.p.s;',
  23328. ' },',
  23329. ' set: function (v) {',
  23330. ' rtl.raiseE("EPropReadOnly");',
  23331. ' }',
  23332. '});',
  23333. '']));
  23334. end;
  23335. procedure TTestModule.TestTypeHelper_Array;
  23336. begin
  23337. StartProgram(false);
  23338. Add([
  23339. '{$modeswitch typehelpers}',
  23340. 'type',
  23341. ' TArrOfBool = array of boolean;',
  23342. ' TArrOfJS = array of jsvalue;',
  23343. ' THelper = type helper for TArrOfBool',
  23344. ' procedure DoIt(e: byte = 123);',
  23345. ' end;',
  23346. 'procedure THelper.DoIt(e: byte);',
  23347. 'begin',
  23348. ' Self[1]:=true;',
  23349. ' Self[2]:=not Self[3];',
  23350. ' SetLength(Self,4);',
  23351. 'end;',
  23352. 'var',
  23353. ' b: TArrOfBool;',
  23354. ' j: TArrOfJS;',
  23355. 'begin',
  23356. ' b.DoIt;',
  23357. ' TArrOfBool(j).DoIt();',
  23358. '']);
  23359. ConvertProgram;
  23360. CheckSource('TestTypeHelper_Array',
  23361. LinesToStr([ // statements
  23362. 'rtl.createHelper($mod, "THelper", null, function () {',
  23363. ' this.DoIt = function (e) {',
  23364. ' this.get()[1] = true;',
  23365. ' this.get()[2] = !this.get()[3];',
  23366. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  23367. ' };',
  23368. '});',
  23369. 'this.b = [];',
  23370. 'this.j = [];',
  23371. '']),
  23372. LinesToStr([ // $mod.$main
  23373. '$mod.THelper.DoIt.call({',
  23374. ' p: $mod,',
  23375. ' get: function () {',
  23376. ' return this.p.b;',
  23377. ' },',
  23378. ' set: function (v) {',
  23379. ' this.p.b = v;',
  23380. ' }',
  23381. '}, 123);',
  23382. '$mod.THelper.DoIt.call({',
  23383. ' p: $mod,',
  23384. ' get: function () {',
  23385. ' return this.p.j;',
  23386. ' },',
  23387. ' set: function (v) {',
  23388. ' this.p.j = v;',
  23389. ' }',
  23390. '}, 123);',
  23391. '']));
  23392. end;
  23393. procedure TTestModule.TestTypeHelper_EnumType;
  23394. begin
  23395. StartProgram(false);
  23396. Add([
  23397. '{$modeswitch typehelpers}',
  23398. 'type',
  23399. ' TEnum = (red,blue);',
  23400. ' THelper = type helper for TEnum',
  23401. ' procedure DoIt(e: byte = 123);',
  23402. ' class procedure Swing(w: word); static;',
  23403. ' end;',
  23404. 'procedure THelper.DoIt(e: byte);',
  23405. 'begin',
  23406. ' Self:=red;',
  23407. ' Self:=succ(Self);',
  23408. ' with Self do Doit;',
  23409. 'end;',
  23410. 'class procedure THelper.Swing(w: word);',
  23411. 'begin',
  23412. 'end;',
  23413. 'var e: TEnum;',
  23414. 'begin',
  23415. ' e.DoIt;',
  23416. ' red.DoIt;',
  23417. ' TEnum.blue.DoIt;',
  23418. ' TEnum(1).DoIt;',
  23419. ' TEnum.Swing(3);',
  23420. '']);
  23421. ConvertProgram;
  23422. CheckSource('TestTypeHelper_EnumType',
  23423. LinesToStr([ // statements
  23424. 'this.TEnum = {',
  23425. ' "0": "red",',
  23426. ' red: 0,',
  23427. ' "1": "blue",',
  23428. ' blue: 1',
  23429. '};',
  23430. 'rtl.createHelper($mod, "THelper", null, function () {',
  23431. ' this.DoIt = function (e) {',
  23432. ' this.set($mod.TEnum.red);',
  23433. ' this.set(this.get() + 1);',
  23434. ' var $with1 = this.get();',
  23435. ' $mod.THelper.DoIt.call(this, 123);',
  23436. ' };',
  23437. ' this.Swing = function (w) {',
  23438. ' };',
  23439. '});',
  23440. 'this.e = 0;',
  23441. '']),
  23442. LinesToStr([ // $mod.$main
  23443. '$mod.THelper.DoIt.call({',
  23444. ' p: $mod,',
  23445. ' get: function () {',
  23446. ' return this.p.e;',
  23447. ' },',
  23448. ' set: function (v) {',
  23449. ' this.p.e = v;',
  23450. ' }',
  23451. '}, 123);',
  23452. '$mod.THelper.DoIt.call({',
  23453. ' p: $mod.TEnum,',
  23454. ' get: function () {',
  23455. ' return this.p.red;',
  23456. ' },',
  23457. ' set: function (v) {',
  23458. ' rtl.raiseE("EPropReadOnly");',
  23459. ' }',
  23460. '}, 123);',
  23461. '$mod.THelper.DoIt.call({',
  23462. ' p: $mod.TEnum,',
  23463. ' get: function () {',
  23464. ' return this.p.blue;',
  23465. ' },',
  23466. ' set: function (v) {',
  23467. ' rtl.raiseE("EPropReadOnly");',
  23468. ' }',
  23469. '}, 123);',
  23470. '$mod.THelper.DoIt.call({',
  23471. ' get: function () {',
  23472. ' return 1;',
  23473. ' },',
  23474. ' set: function (v) {',
  23475. ' rtl.raiseE("EPropReadOnly");',
  23476. ' }',
  23477. '}, 123);',
  23478. '$mod.THelper.Swing(3);',
  23479. '']));
  23480. end;
  23481. procedure TTestModule.TestTypeHelper_SetType;
  23482. begin
  23483. StartProgram(false);
  23484. Add([
  23485. '{$modeswitch typehelpers}',
  23486. 'type',
  23487. ' TEnum = (red,blue);',
  23488. ' TSetOfEnum = set of TEnum;',
  23489. ' THelper = type helper for TSetOfEnum',
  23490. ' procedure DoIt(e: byte = 123);',
  23491. ' constructor Init(e: TEnum);',
  23492. ' constructor InitEmpty;',
  23493. ' end;',
  23494. 'procedure THelper.DoIt(e: byte);',
  23495. 'begin',
  23496. ' Self:=[];',
  23497. ' Self:=[red];',
  23498. ' Include(Self,blue);',
  23499. 'end;',
  23500. 'constructor THelper.Init(e: TEnum);',
  23501. 'begin',
  23502. ' Self:=[];',
  23503. ' Self:=[e];',
  23504. ' Include(Self,blue);',
  23505. 'end;',
  23506. 'constructor THelper.InitEmpty;',
  23507. 'begin',
  23508. 'end;',
  23509. 'var s: TSetOfEnum;',
  23510. 'begin',
  23511. ' s.DoIt;',
  23512. //' [red].DoIt;',
  23513. //' with s do DoIt;',
  23514. //' with [red,blue] do DoIt;',
  23515. ' s:=TSetOfEnum.Init(blue);',
  23516. ' s:=s.Init(blue);',
  23517. '']);
  23518. ConvertProgram;
  23519. CheckSource('TestTypeHelper_SetType',
  23520. LinesToStr([ // statements
  23521. 'this.TEnum = {',
  23522. ' "0": "red",',
  23523. ' red: 0,',
  23524. ' "1": "blue",',
  23525. ' blue: 1',
  23526. '};',
  23527. 'rtl.createHelper($mod, "THelper", null, function () {',
  23528. ' this.DoIt = function (e) {',
  23529. ' this.set({});',
  23530. ' this.set(rtl.createSet($mod.TEnum.red));',
  23531. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  23532. ' };',
  23533. ' this.Init = function (e) {',
  23534. ' this.set({});',
  23535. ' this.set(rtl.createSet(e));',
  23536. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  23537. ' return this.get();',
  23538. ' };',
  23539. ' this.InitEmpty = function () {',
  23540. ' return this.get();',
  23541. ' };',
  23542. ' this.$new = function (fn, args) {',
  23543. ' return this[fn].apply({',
  23544. ' p: {},',
  23545. ' get: function () {',
  23546. ' return this.p;',
  23547. ' },',
  23548. ' set: function (v) {',
  23549. ' this.p = v;',
  23550. ' }',
  23551. ' }, args);',
  23552. ' };',
  23553. '});',
  23554. 'this.s = {};',
  23555. '']),
  23556. LinesToStr([ // $mod.$main
  23557. '$mod.THelper.DoIt.call({',
  23558. ' p: $mod,',
  23559. ' get: function () {',
  23560. ' return this.p.s;',
  23561. ' },',
  23562. ' set: function (v) {',
  23563. ' this.p.s = v;',
  23564. ' }',
  23565. '}, 123);',
  23566. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  23567. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  23568. ' p: $mod,',
  23569. ' get: function () {',
  23570. ' return this.p.s;',
  23571. ' },',
  23572. ' set: function (v) {',
  23573. ' this.p.s = v;',
  23574. ' }',
  23575. '}, $mod.TEnum.blue));',
  23576. '']));
  23577. end;
  23578. procedure TTestModule.TestTypeHelper_InterfaceType;
  23579. begin
  23580. StartProgram(false);
  23581. Add([
  23582. '{$interfaces com}',
  23583. '{$modeswitch typehelpers}',
  23584. 'type',
  23585. ' IUnknown = interface',
  23586. ' function _AddRef: longint;',
  23587. ' function _Release: longint;',
  23588. ' end;',
  23589. ' TObject = class(IUnknown)',
  23590. ' function _AddRef: longint; virtual; abstract;',
  23591. ' function _Release: longint; virtual; abstract;',
  23592. ' end;',
  23593. ' THelper = type helper for IUnknown',
  23594. ' procedure Fly(e: byte = 123);',
  23595. ' class procedure Run; static;',
  23596. ' end;',
  23597. 'var',
  23598. ' i: IUnknown;',
  23599. ' o: TObject;',
  23600. 'procedure THelper.Fly(e: byte);',
  23601. 'begin',
  23602. ' i:=Self;',
  23603. ' o:=Self as TObject;',
  23604. ' Self:=nil;',
  23605. ' Self:=i;',
  23606. ' Self:=o;',
  23607. ' with Self do begin',
  23608. ' Fly;',
  23609. ' Fly();',
  23610. ' end;',
  23611. 'end;',
  23612. 'class procedure THelper.Run;',
  23613. 'var l: IUnknown;',
  23614. 'begin',
  23615. ' l.Fly;',
  23616. ' l.Fly();',
  23617. 'end;',
  23618. 'begin',
  23619. ' i.Fly;',
  23620. ' i.Fly();',
  23621. ' i.Run;',
  23622. ' i.Run();',
  23623. ' IUnknown.Run;',
  23624. ' IUnknown.Run();',
  23625. '']);
  23626. ConvertProgram;
  23627. CheckSource('TestTypeHelper_InterfaceType',
  23628. LinesToStr([ // statements
  23629. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  23630. 'rtl.createClass($mod, "TObject", null, function () {',
  23631. ' this.$init = function () {',
  23632. ' };',
  23633. ' this.$final = function () {',
  23634. ' };',
  23635. ' rtl.addIntf(this, $mod.IUnknown);',
  23636. '});',
  23637. 'rtl.createHelper($mod, "THelper", null, function () {',
  23638. ' this.Fly = function (e) {',
  23639. ' var $ir = rtl.createIntfRefs();',
  23640. ' try {',
  23641. ' rtl.setIntfP($mod, "i", this.get());',
  23642. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  23643. ' this.set(null);',
  23644. ' this.set($mod.i);',
  23645. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  23646. ' var $with1 = this.get();',
  23647. ' $mod.THelper.Fly.call(this, 123);',
  23648. ' $mod.THelper.Fly.call(this, 123);',
  23649. ' } finally {',
  23650. ' $ir.free();',
  23651. ' };',
  23652. ' };',
  23653. ' this.Run = function () {',
  23654. ' var l = null;',
  23655. ' try {',
  23656. ' $mod.THelper.Fly.call({',
  23657. ' get: function () {',
  23658. ' return l;',
  23659. ' },',
  23660. ' set: function (v) {',
  23661. ' l = rtl.setIntfL(l, v);',
  23662. ' }',
  23663. ' }, 123);',
  23664. ' $mod.THelper.Fly.call({',
  23665. ' get: function () {',
  23666. ' return l;',
  23667. ' },',
  23668. ' set: function (v) {',
  23669. ' l = rtl.setIntfL(l, v);',
  23670. ' }',
  23671. ' }, 123);',
  23672. ' } finally {',
  23673. ' rtl._Release(l);',
  23674. ' };',
  23675. ' };',
  23676. '});',
  23677. 'this.i = null;',
  23678. 'this.o = null;',
  23679. '']),
  23680. LinesToStr([ // $mod.$main
  23681. '$mod.THelper.Fly.call({',
  23682. ' p: $mod,',
  23683. ' get: function () {',
  23684. ' return this.p.i;',
  23685. ' },',
  23686. ' set: function (v) {',
  23687. ' rtl.setIntfP(this.p, "i", v);',
  23688. ' }',
  23689. '}, 123);',
  23690. '$mod.THelper.Fly.call({',
  23691. ' p: $mod,',
  23692. ' get: function () {',
  23693. ' return this.p.i;',
  23694. ' },',
  23695. ' set: function (v) {',
  23696. ' rtl.setIntfP(this.p, "i", v);',
  23697. ' }',
  23698. '}, 123);',
  23699. '$mod.THelper.Run();',
  23700. '$mod.THelper.Run();',
  23701. '$mod.THelper.Run();',
  23702. '$mod.THelper.Run();',
  23703. '']));
  23704. end;
  23705. procedure TTestModule.TestProcType;
  23706. begin
  23707. StartProgram(false);
  23708. Add([
  23709. 'type',
  23710. ' TProcInt = procedure(vI: longint = 1);',
  23711. 'procedure DoIt(vJ: longint);',
  23712. 'begin end;',
  23713. 'var',
  23714. ' b: boolean;',
  23715. ' vP, vQ: tprocint;',
  23716. 'begin',
  23717. ' vp:=nil;',
  23718. ' vp:=vp;',
  23719. ' vp:=@doit;',
  23720. ' vp;',
  23721. ' vp();',
  23722. ' vp(2);',
  23723. ' b:=vp=nil;',
  23724. ' b:=nil=vp;',
  23725. ' b:=vp=vq;',
  23726. ' b:=vp=@doit;',
  23727. ' b:=@doit=vp;',
  23728. ' b:=vp<>nil;',
  23729. ' b:=nil<>vp;',
  23730. ' b:=vp<>vq;',
  23731. ' b:=vp<>@doit;',
  23732. ' b:=@doit<>vp;',
  23733. ' b:=Assigned(vp);',
  23734. ' if Assigned(vp) then ;']);
  23735. ConvertProgram;
  23736. CheckSource('TestProcType',
  23737. LinesToStr([ // statements
  23738. 'this.DoIt = function(vJ) {',
  23739. '};',
  23740. 'this.b = false;',
  23741. 'this.vP = null;',
  23742. 'this.vQ = null;'
  23743. ]),
  23744. LinesToStr([ // $mod.$main
  23745. '$mod.vP = null;',
  23746. '$mod.vP = $mod.vP;',
  23747. '$mod.vP = $mod.DoIt;',
  23748. '$mod.vP(1);',
  23749. '$mod.vP(1);',
  23750. '$mod.vP(2);',
  23751. '$mod.b = $mod.vP === null;',
  23752. '$mod.b = null === $mod.vP;',
  23753. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  23754. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23755. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23756. '$mod.b = $mod.vP !== null;',
  23757. '$mod.b = null !== $mod.vP;',
  23758. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  23759. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23760. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23761. '$mod.b = $mod.vP != null;',
  23762. 'if ($mod.vP != null) ;',
  23763. '']));
  23764. end;
  23765. procedure TTestModule.TestProcType_Arg;
  23766. begin
  23767. StartProgram(false);
  23768. Add([
  23769. 'type',
  23770. ' TProcInt = procedure(vI: longint = 1);',
  23771. 'procedure DoIt(vJ: longint); begin end;',
  23772. 'procedure DoSome(vP, vQ: TProcInt);',
  23773. 'var',
  23774. ' b: boolean;',
  23775. 'begin',
  23776. ' vp:=nil;',
  23777. ' vp:=vp;',
  23778. ' vp:=@doit;',
  23779. ' vp;',
  23780. ' vp();',
  23781. ' vp(2);',
  23782. ' b:=vp=nil;',
  23783. ' b:=nil=vp;',
  23784. ' b:=vp=vq;',
  23785. ' b:=vp=@doit;',
  23786. ' b:=@doit=vp;',
  23787. ' b:=vp<>nil;',
  23788. ' b:=nil<>vp;',
  23789. ' b:=vp<>vq;',
  23790. ' b:=vp<>@doit;',
  23791. ' b:=@doit<>vp;',
  23792. ' b:=Assigned(vp);',
  23793. ' if Assigned(vp) then ;',
  23794. 'end;',
  23795. 'begin',
  23796. ' DoSome(@DoIt,nil);']);
  23797. ConvertProgram;
  23798. CheckSource('TestProcType_Arg',
  23799. LinesToStr([ // statements
  23800. 'this.DoIt = function(vJ) {',
  23801. '};',
  23802. 'this.DoSome = function(vP, vQ) {',
  23803. ' var b = false;',
  23804. ' vP = null;',
  23805. ' vP = vP;',
  23806. ' vP = $mod.DoIt;',
  23807. ' vP(1);',
  23808. ' vP(1);',
  23809. ' vP(2);',
  23810. ' b = vP === null;',
  23811. ' b = null === vP;',
  23812. ' b = rtl.eqCallback(vP,vQ);',
  23813. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  23814. ' b = rtl.eqCallback($mod.DoIt, vP);',
  23815. ' b = vP !== null;',
  23816. ' b = null !== vP;',
  23817. ' b = !rtl.eqCallback(vP, vQ);',
  23818. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  23819. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  23820. ' b = vP != null;',
  23821. ' if (vP != null) ;',
  23822. '};',
  23823. '']),
  23824. LinesToStr([ // $mod.$main
  23825. '$mod.DoSome($mod.DoIt,null);',
  23826. '']));
  23827. end;
  23828. procedure TTestModule.TestProcType_FunctionFPC;
  23829. begin
  23830. StartProgram(false);
  23831. Add('type');
  23832. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23833. Add('function DoIt(vI: longint): longint;');
  23834. Add('begin end;');
  23835. Add('var');
  23836. Add(' b: boolean;');
  23837. Add(' vP, vQ: tfuncint;');
  23838. Add('begin');
  23839. Add(' vp:=nil;');
  23840. Add(' vp:=vp;');
  23841. Add(' vp:=@doit;'); // ok in fpc and delphi
  23842. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23843. Add(' vp;'); // ok in fpc and delphi
  23844. Add(' vp();');
  23845. Add(' vp(2);');
  23846. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23847. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23848. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23849. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23850. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23851. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23852. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23853. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23854. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23855. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23856. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23857. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23858. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23859. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23860. Add(' b:=Assigned(vp);');
  23861. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23862. Add(' doit(vp());'); // ok in fpc and delphi
  23863. Add(' doit(vp(2));'); // ok in fpc and delphi
  23864. ConvertProgram;
  23865. CheckSource('TestProcType_FunctionFPC',
  23866. LinesToStr([ // statements
  23867. 'this.DoIt = function(vI) {',
  23868. ' var Result = 0;',
  23869. ' return Result;',
  23870. '};',
  23871. 'this.b = false;',
  23872. 'this.vP = null;',
  23873. 'this.vQ = null;'
  23874. ]),
  23875. LinesToStr([ // $mod.$main
  23876. '$mod.vP = null;',
  23877. '$mod.vP = $mod.vP;',
  23878. '$mod.vP = $mod.DoIt;',
  23879. '$mod.vP(1);',
  23880. '$mod.vP(1);',
  23881. '$mod.vP(2);',
  23882. '$mod.b = $mod.vP === null;',
  23883. '$mod.b = null === $mod.vP;',
  23884. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  23885. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  23886. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  23887. '$mod.b = 4 === $mod.vP(1);',
  23888. '$mod.b = $mod.vP !== null;',
  23889. '$mod.b = null !== $mod.vP;',
  23890. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  23891. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  23892. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  23893. '$mod.b = 6 !== $mod.vP(1);',
  23894. '$mod.b = $mod.vP != null;',
  23895. '$mod.DoIt($mod.vP(1));',
  23896. '$mod.DoIt($mod.vP(2));',
  23897. '']));
  23898. end;
  23899. procedure TTestModule.TestProcType_FunctionDelphi;
  23900. begin
  23901. StartProgram(false);
  23902. Add('{$mode Delphi}');
  23903. Add('type');
  23904. Add(' TFuncInt = function(vA: longint = 1): longint;');
  23905. Add('function DoIt(vI: longint): longint;');
  23906. Add('begin end;');
  23907. Add('var');
  23908. Add(' b: boolean;');
  23909. Add(' vP, vQ: tfuncint;');
  23910. Add('begin');
  23911. Add(' vp:=nil;');
  23912. Add(' vp:=vp;');
  23913. Add(' vp:=@doit;'); // ok in fpc and delphi
  23914. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23915. Add(' vp;'); // ok in fpc and delphi
  23916. Add(' vp();');
  23917. Add(' vp(2);');
  23918. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23919. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23920. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23921. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23922. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23923. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  23924. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  23925. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  23926. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  23927. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  23928. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  23929. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  23930. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  23931. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  23932. Add(' b:=Assigned(vp);');
  23933. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  23934. Add(' doit(vp());'); // ok in fpc and delphi
  23935. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  23936. ConvertProgram;
  23937. CheckSource('TestProcType_FunctionDelphi',
  23938. LinesToStr([ // statements
  23939. 'this.DoIt = function(vI) {',
  23940. ' var Result = 0;',
  23941. ' return Result;',
  23942. '};',
  23943. 'this.b = false;',
  23944. 'this.vP = null;',
  23945. 'this.vQ = null;'
  23946. ]),
  23947. LinesToStr([ // $mod.$main
  23948. '$mod.vP = null;',
  23949. '$mod.vP = $mod.vP;',
  23950. '$mod.vP = $mod.DoIt;',
  23951. '$mod.vP = $mod.DoIt;',
  23952. '$mod.vP(1);',
  23953. '$mod.vP(1);',
  23954. '$mod.vP(2);',
  23955. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  23956. '$mod.b = $mod.vP(1) === 3;',
  23957. '$mod.b = 4 === $mod.vP(1);',
  23958. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  23959. '$mod.b = $mod.vP(1) !== 5;',
  23960. '$mod.b = 6 !== $mod.vP(1);',
  23961. '$mod.b = $mod.vP != null;',
  23962. '$mod.DoIt($mod.vP(1));',
  23963. '$mod.DoIt($mod.vP(1));',
  23964. '$mod.DoIt($mod.vP(2));',
  23965. '']));
  23966. end;
  23967. procedure TTestModule.TestProcType_ProcedureDelphi;
  23968. begin
  23969. StartProgram(false);
  23970. Add('{$mode Delphi}');
  23971. Add('type');
  23972. Add(' TProc = procedure;');
  23973. Add('procedure DoIt;');
  23974. Add('begin end;');
  23975. Add('var');
  23976. Add(' b: boolean;');
  23977. Add(' vP, vQ: tproc;');
  23978. Add('begin');
  23979. Add(' vp:=nil;');
  23980. Add(' vp:=vp;');
  23981. Add(' vp:=vq;');
  23982. 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
  23983. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  23984. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  23985. Add(' vp;'); // ok in fpc and delphi
  23986. Add(' vp();');
  23987. // equal
  23988. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  23989. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  23990. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  23991. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23992. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  23993. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  23994. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  23995. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  23996. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  23997. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  23998. // unequal
  23999. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24000. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  24001. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24002. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  24003. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24004. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  24005. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24006. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  24007. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24008. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  24009. Add(' b:=Assigned(vp);');
  24010. ConvertProgram;
  24011. CheckSource('TestProcType_ProcedureDelphi',
  24012. LinesToStr([ // statements
  24013. 'this.DoIt = function() {',
  24014. '};',
  24015. 'this.b = false;',
  24016. 'this.vP = null;',
  24017. 'this.vQ = null;'
  24018. ]),
  24019. LinesToStr([ // $mod.$main
  24020. '$mod.vP = null;',
  24021. '$mod.vP = $mod.vP;',
  24022. '$mod.vP = $mod.vQ;',
  24023. '$mod.vP = $mod.DoIt;',
  24024. '$mod.vP = $mod.DoIt;',
  24025. '$mod.vP();',
  24026. '$mod.vP();',
  24027. '$mod.b = $mod.vP === null;',
  24028. '$mod.b = null === $mod.vP;',
  24029. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  24030. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24031. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24032. '$mod.b = $mod.vP !== null;',
  24033. '$mod.b = null !== $mod.vP;',
  24034. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  24035. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24036. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24037. '$mod.b = $mod.vP != null;',
  24038. '']));
  24039. end;
  24040. procedure TTestModule.TestProcType_AsParam;
  24041. begin
  24042. StartProgram(false);
  24043. Add('type');
  24044. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24045. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  24046. Add('var vJ: tfuncint;');
  24047. Add('begin');
  24048. Add(' vg:=vg;');
  24049. Add(' vj:=vh;');
  24050. Add(' vi:=vi;');
  24051. Add(' doit(vg,vg,vg);');
  24052. Add(' doit(vh,vh,vj);');
  24053. Add(' doit(vi,vi,vi);');
  24054. Add(' doit(vj,vj,vj);');
  24055. Add('end;');
  24056. Add('var i: tfuncint;');
  24057. Add('begin');
  24058. Add(' doit(i,i,i);');
  24059. ConvertProgram;
  24060. CheckSource('TestProcType_AsParam',
  24061. LinesToStr([ // statements
  24062. 'this.DoIt = function (vG,vH,vI) {',
  24063. ' var vJ = null;',
  24064. ' vG = vG;',
  24065. ' vJ = vH;',
  24066. ' vI.set(vI.get());',
  24067. ' $mod.DoIt(vG, vG, {',
  24068. ' get: function () {',
  24069. ' return vG;',
  24070. ' },',
  24071. ' set: function (v) {',
  24072. ' vG = v;',
  24073. ' }',
  24074. ' });',
  24075. ' $mod.DoIt(vH, vH, {',
  24076. ' get: function () {',
  24077. ' return vJ;',
  24078. ' },',
  24079. ' set: function (v) {',
  24080. ' vJ = v;',
  24081. ' }',
  24082. ' });',
  24083. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  24084. ' $mod.DoIt(vJ, vJ, {',
  24085. ' get: function () {',
  24086. ' return vJ;',
  24087. ' },',
  24088. ' set: function (v) {',
  24089. ' vJ = v;',
  24090. ' }',
  24091. ' });',
  24092. '};',
  24093. 'this.i = null;'
  24094. ]),
  24095. LinesToStr([
  24096. '$mod.DoIt($mod.i,$mod.i,{',
  24097. ' p: $mod,',
  24098. ' get: function () {',
  24099. ' return this.p.i;',
  24100. ' },',
  24101. ' set: function (v) {',
  24102. ' this.p.i = v;',
  24103. ' }',
  24104. '});'
  24105. ]));
  24106. end;
  24107. procedure TTestModule.TestProcType_MethodFPC;
  24108. begin
  24109. StartProgram(false);
  24110. Add('type');
  24111. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24112. Add(' TObject = class');
  24113. Add(' function DoIt(vA: longint = 1): longint;');
  24114. Add(' end;');
  24115. Add('function TObject.DoIt(vA: longint = 1): longint;');
  24116. Add('begin');
  24117. Add('end;');
  24118. Add('var');
  24119. Add(' Obj: TObject;');
  24120. Add(' vP: tfuncint;');
  24121. Add(' b: boolean;');
  24122. Add('begin');
  24123. Add(' vp:[email protected];'); // ok in fpc and delphi
  24124. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  24125. Add(' vp;'); // ok in fpc and delphi
  24126. Add(' vp();');
  24127. Add(' vp(2);');
  24128. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  24129. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  24130. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  24131. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  24132. ConvertProgram;
  24133. CheckSource('TestProcType_MethodFPC',
  24134. LinesToStr([ // statements
  24135. 'rtl.createClass($mod, "TObject", null, function () {',
  24136. ' this.$init = function () {',
  24137. ' };',
  24138. ' this.$final = function () {',
  24139. ' };',
  24140. ' this.DoIt = function (vA) {',
  24141. ' var Result = 0;',
  24142. ' return Result;',
  24143. ' };',
  24144. '});',
  24145. 'this.Obj = null;',
  24146. 'this.vP = null;',
  24147. 'this.b = false;'
  24148. ]),
  24149. LinesToStr([
  24150. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24151. '$mod.vP(1);',
  24152. '$mod.vP(1);',
  24153. '$mod.vP(2);',
  24154. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  24155. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  24156. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  24157. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  24158. '']));
  24159. end;
  24160. procedure TTestModule.TestProcType_MethodDelphi;
  24161. begin
  24162. StartProgram(false);
  24163. Add('{$mode delphi}');
  24164. Add('type');
  24165. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24166. Add(' TObject = class');
  24167. Add(' function DoIt(vA: longint = 1): longint;');
  24168. Add(' end;');
  24169. Add('function TObject.DoIt(vA: longint = 1): longint;');
  24170. Add('begin');
  24171. Add('end;');
  24172. Add('var');
  24173. Add(' Obj: TObject;');
  24174. Add(' vP: tfuncint;');
  24175. Add(' b: boolean;');
  24176. Add('begin');
  24177. Add(' vp:[email protected];'); // ok in fpc and delphi
  24178. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  24179. Add(' vp;'); // ok in fpc and delphi
  24180. Add(' vp();');
  24181. Add(' vp(2);');
  24182. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  24183. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  24184. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  24185. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  24186. ConvertProgram;
  24187. CheckSource('TestProcType_MethodDelphi',
  24188. LinesToStr([ // statements
  24189. 'rtl.createClass($mod, "TObject", null, function () {',
  24190. ' this.$init = function () {',
  24191. ' };',
  24192. ' this.$final = function () {',
  24193. ' };',
  24194. ' this.DoIt = function (vA) {',
  24195. ' var Result = 0;',
  24196. ' return Result;',
  24197. ' };',
  24198. '});',
  24199. 'this.Obj = null;',
  24200. 'this.vP = null;',
  24201. 'this.b = false;'
  24202. ]),
  24203. LinesToStr([
  24204. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24205. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  24206. '$mod.vP(1);',
  24207. '$mod.vP(1);',
  24208. '$mod.vP(2);',
  24209. '']));
  24210. end;
  24211. procedure TTestModule.TestProcType_PropertyFPC;
  24212. begin
  24213. StartProgram(false);
  24214. Add('type');
  24215. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24216. Add(' TObject = class');
  24217. Add(' FOnFoo: TFuncInt;');
  24218. Add(' function DoIt(vA: longint = 1): longint;');
  24219. Add(' function GetFoo: TFuncInt;');
  24220. Add(' procedure SetFoo(const Value: TFuncInt);');
  24221. Add(' function GetEvents(Index: longint): TFuncInt;');
  24222. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  24223. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24224. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24225. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  24226. Add(' end;');
  24227. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24228. Add('function tobject.getfoo: tfuncint; begin end;');
  24229. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24230. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  24231. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  24232. Add('var');
  24233. Add(' Obj: TObject;');
  24234. Add(' vP: tfuncint;');
  24235. Add(' b: boolean;');
  24236. Add('begin');
  24237. Add(' obj.onfoo:=nil;');
  24238. Add(' obj.onbar:=nil;');
  24239. Add(' obj.events[1]:=nil;');
  24240. Add(' obj.onfoo:=obj.onfoo;');
  24241. Add(' obj.onbar:=obj.onbar;');
  24242. Add(' obj.events[2]:=obj.events[3];');
  24243. Add(' obj.onfoo:[email protected];');
  24244. Add(' obj.onbar:[email protected];');
  24245. Add(' obj.events[4]:[email protected];');
  24246. //Add(' obj.onfoo:=obj.doit;'); // delphi
  24247. //Add(' obj.onbar:=obj.doit;'); // delphi
  24248. //Add(' obj.events[4]:=obj.doit;'); // delphi
  24249. Add(' obj.onfoo;');
  24250. Add(' obj.onbar;');
  24251. //Add(' obj.events[5];'); ToDo in pasresolver
  24252. Add(' obj.onfoo();');
  24253. Add(' obj.onbar();');
  24254. Add(' obj.events[6]();');
  24255. Add(' b:=obj.onfoo=nil;');
  24256. Add(' b:=obj.onbar=nil;');
  24257. Add(' b:=obj.events[7]=nil;');
  24258. Add(' b:=obj.onfoo<>nil;');
  24259. Add(' b:=obj.onbar<>nil;');
  24260. Add(' b:=obj.events[8]<>nil;');
  24261. Add(' b:=obj.onfoo=vp;');
  24262. Add(' b:=obj.onbar=vp;');
  24263. Add(' b:=obj.events[9]=vp;');
  24264. Add(' b:=obj.onfoo=obj.onfoo;');
  24265. Add(' b:=obj.onbar=obj.onfoo;');
  24266. Add(' b:=obj.events[10]=obj.onfoo;');
  24267. Add(' b:=obj.onfoo<>obj.onfoo;');
  24268. Add(' b:=obj.onbar<>obj.onfoo;');
  24269. Add(' b:=obj.events[11]<>obj.onfoo;');
  24270. Add(' b:[email protected];');
  24271. Add(' b:[email protected];');
  24272. Add(' b:=obj.events[12][email protected];');
  24273. Add(' b:=obj.onfoo<>@obj.doit;');
  24274. Add(' b:=obj.onbar<>@obj.doit;');
  24275. Add(' b:=obj.events[12]<>@obj.doit;');
  24276. Add(' b:=Assigned(obj.onfoo);');
  24277. Add(' b:=Assigned(obj.onbar);');
  24278. Add(' b:=Assigned(obj.events[13]);');
  24279. ConvertProgram;
  24280. CheckSource('TestProcType_PropertyFPC',
  24281. LinesToStr([ // statements
  24282. 'rtl.createClass($mod, "TObject", null, function () {',
  24283. ' this.$init = function () {',
  24284. ' this.FOnFoo = null;',
  24285. ' };',
  24286. ' this.$final = function () {',
  24287. ' this.FOnFoo = undefined;',
  24288. ' };',
  24289. ' this.DoIt = function (vA) {',
  24290. ' var Result = 0;',
  24291. ' return Result;',
  24292. ' };',
  24293. 'this.GetFoo = function () {',
  24294. ' var Result = null;',
  24295. ' return Result;',
  24296. '};',
  24297. 'this.SetFoo = function (Value) {',
  24298. '};',
  24299. 'this.GetEvents = function (Index) {',
  24300. ' var Result = null;',
  24301. ' return Result;',
  24302. '};',
  24303. 'this.SetEvents = function (Index, Value) {',
  24304. '};',
  24305. '});',
  24306. 'this.Obj = null;',
  24307. 'this.vP = null;',
  24308. 'this.b = false;'
  24309. ]),
  24310. LinesToStr([
  24311. '$mod.Obj.FOnFoo = null;',
  24312. '$mod.Obj.SetFoo(null);',
  24313. '$mod.Obj.SetEvents(1, null);',
  24314. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  24315. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  24316. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  24317. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24318. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24319. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24320. '$mod.Obj.FOnFoo(1);',
  24321. '$mod.Obj.GetFoo();',
  24322. '$mod.Obj.FOnFoo(1);',
  24323. '$mod.Obj.GetFoo()(1);',
  24324. '$mod.Obj.GetEvents(6)(1);',
  24325. '$mod.b = $mod.Obj.FOnFoo === null;',
  24326. '$mod.b = $mod.Obj.GetFoo() === null;',
  24327. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  24328. '$mod.b = $mod.Obj.FOnFoo !== null;',
  24329. '$mod.b = $mod.Obj.GetFoo() !== null;',
  24330. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  24331. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  24332. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  24333. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  24334. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  24335. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  24336. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  24337. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  24338. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  24339. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  24340. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  24341. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  24342. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  24343. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  24344. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  24345. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  24346. '$mod.b = $mod.Obj.FOnFoo != null;',
  24347. '$mod.b = $mod.Obj.GetFoo() != null;',
  24348. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  24349. '']));
  24350. end;
  24351. procedure TTestModule.TestProcType_PropertyDelphi;
  24352. begin
  24353. StartProgram(false);
  24354. Add('{$mode delphi}');
  24355. Add('type');
  24356. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24357. Add(' TObject = class');
  24358. Add(' FOnFoo: TFuncInt;');
  24359. Add(' function DoIt(vA: longint = 1): longint;');
  24360. Add(' function GetFoo: TFuncInt;');
  24361. Add(' procedure SetFoo(const Value: TFuncInt);');
  24362. Add(' function GetEvents(Index: longint): TFuncInt;');
  24363. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  24364. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24365. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24366. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  24367. Add(' end;');
  24368. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24369. Add('function tobject.getfoo: tfuncint; begin end;');
  24370. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24371. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  24372. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  24373. Add('var');
  24374. Add(' Obj: TObject;');
  24375. Add(' vP: tfuncint;');
  24376. Add(' b: boolean;');
  24377. Add('begin');
  24378. Add(' obj.onfoo:=nil;');
  24379. Add(' obj.onbar:=nil;');
  24380. Add(' obj.events[1]:=nil;');
  24381. Add(' obj.onfoo:=obj.onfoo;');
  24382. Add(' obj.onbar:=obj.onbar;');
  24383. Add(' obj.events[2]:=obj.events[3];');
  24384. Add(' obj.onfoo:[email protected];');
  24385. Add(' obj.onbar:[email protected];');
  24386. Add(' obj.events[4]:[email protected];');
  24387. Add(' obj.onfoo:=obj.doit;'); // delphi
  24388. Add(' obj.onbar:=obj.doit;'); // delphi
  24389. Add(' obj.events[4]:=obj.doit;'); // delphi
  24390. Add(' obj.onfoo;');
  24391. Add(' obj.onbar;');
  24392. //Add(' obj.events[5];'); ToDo in pasresolver
  24393. Add(' obj.onfoo();');
  24394. Add(' obj.onbar();');
  24395. Add(' obj.events[6]();');
  24396. //Add(' b:=obj.onfoo=nil;'); // fpc
  24397. //Add(' b:=obj.onbar=nil;'); // fpc
  24398. //Add(' b:=obj.events[7]=nil;'); // fpc
  24399. //Add(' b:=obj.onfoo<>nil;'); // fpc
  24400. //Add(' b:=obj.onbar<>nil;'); // fpc
  24401. //Add(' b:=obj.events[8]<>nil;'); // fpc
  24402. Add(' b:=obj.onfoo=vp;');
  24403. Add(' b:=obj.onbar=vp;');
  24404. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  24405. Add(' b:=obj.onfoo=obj.onfoo;');
  24406. Add(' b:=obj.onbar=obj.onfoo;');
  24407. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  24408. Add(' b:=obj.onfoo<>obj.onfoo;');
  24409. Add(' b:=obj.onbar<>obj.onfoo;');
  24410. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  24411. //Add(' b:[email protected];'); // fpc
  24412. //Add(' b:[email protected];'); // fpc
  24413. //Add(' b:=obj.events[12][email protected];'); // fpc
  24414. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  24415. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  24416. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  24417. Add(' b:=Assigned(obj.onfoo);');
  24418. Add(' b:=Assigned(obj.onbar);');
  24419. Add(' b:=Assigned(obj.events[13]);');
  24420. ConvertProgram;
  24421. CheckSource('TestProcType_PropertyDelphi',
  24422. LinesToStr([ // statements
  24423. 'rtl.createClass($mod, "TObject", null, function () {',
  24424. ' this.$init = function () {',
  24425. ' this.FOnFoo = null;',
  24426. ' };',
  24427. ' this.$final = function () {',
  24428. ' this.FOnFoo = undefined;',
  24429. ' };',
  24430. ' this.DoIt = function (vA) {',
  24431. ' var Result = 0;',
  24432. ' return Result;',
  24433. ' };',
  24434. 'this.GetFoo = function () {',
  24435. ' var Result = null;',
  24436. ' return Result;',
  24437. '};',
  24438. 'this.SetFoo = function (Value) {',
  24439. '};',
  24440. 'this.GetEvents = function (Index) {',
  24441. ' var Result = null;',
  24442. ' return Result;',
  24443. '};',
  24444. 'this.SetEvents = function (Index, Value) {',
  24445. '};',
  24446. '});',
  24447. 'this.Obj = null;',
  24448. 'this.vP = null;',
  24449. 'this.b = false;'
  24450. ]),
  24451. LinesToStr([
  24452. '$mod.Obj.FOnFoo = null;',
  24453. '$mod.Obj.SetFoo(null);',
  24454. '$mod.Obj.SetEvents(1, null);',
  24455. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  24456. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  24457. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  24458. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24459. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24460. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24461. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  24462. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  24463. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  24464. '$mod.Obj.FOnFoo(1);',
  24465. '$mod.Obj.GetFoo();',
  24466. '$mod.Obj.FOnFoo(1);',
  24467. '$mod.Obj.GetFoo()(1);',
  24468. '$mod.Obj.GetEvents(6)(1);',
  24469. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  24470. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  24471. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  24472. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  24473. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  24474. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  24475. '$mod.b = $mod.Obj.FOnFoo != null;',
  24476. '$mod.b = $mod.Obj.GetFoo() != null;',
  24477. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  24478. '']));
  24479. end;
  24480. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  24481. begin
  24482. StartProgram(false);
  24483. Add('type');
  24484. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  24485. Add(' TObject = class');
  24486. Add(' FOnFoo: TFuncInt;');
  24487. Add(' function DoIt(vA: longint = 1): longint;');
  24488. Add(' function GetFoo: TFuncInt;');
  24489. Add(' procedure SetFoo(const Value: TFuncInt);');
  24490. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  24491. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  24492. Add(' end;');
  24493. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  24494. Add('function tobject.getfoo: tfuncint; begin end;');
  24495. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  24496. Add('var');
  24497. Add(' Obj: TObject;');
  24498. Add(' vP: tfuncint;');
  24499. Add(' b: boolean;');
  24500. Add('begin');
  24501. Add('with obj do begin');
  24502. Add(' fonfoo:=nil;');
  24503. Add(' onfoo:=nil;');
  24504. Add(' onbar:=nil;');
  24505. Add(' fonfoo:=fonfoo;');
  24506. Add(' onfoo:=onfoo;');
  24507. Add(' onbar:=onbar;');
  24508. Add(' fonfoo:=@doit;');
  24509. Add(' onfoo:=@doit;');
  24510. Add(' onbar:=@doit;');
  24511. //Add(' fonfoo:=doit;'); // delphi
  24512. //Add(' onfoo:=doit;'); // delphi
  24513. //Add(' onbar:=doit;'); // delphi
  24514. Add(' fonfoo;');
  24515. Add(' onfoo;');
  24516. Add(' onbar;');
  24517. Add(' fonfoo();');
  24518. Add(' onfoo();');
  24519. Add(' onbar();');
  24520. Add(' b:=fonfoo=nil;');
  24521. Add(' b:=onfoo=nil;');
  24522. Add(' b:=onbar=nil;');
  24523. Add(' b:=fonfoo<>nil;');
  24524. Add(' b:=onfoo<>nil;');
  24525. Add(' b:=onbar<>nil;');
  24526. Add(' b:=fonfoo=vp;');
  24527. Add(' b:=onfoo=vp;');
  24528. Add(' b:=onbar=vp;');
  24529. Add(' b:=fonfoo=fonfoo;');
  24530. Add(' b:=onfoo=onfoo;');
  24531. Add(' b:=onbar=onfoo;');
  24532. Add(' b:=fonfoo<>fonfoo;');
  24533. Add(' b:=onfoo<>onfoo;');
  24534. Add(' b:=onbar<>onfoo;');
  24535. Add(' b:=fonfoo=@doit;');
  24536. Add(' b:=onfoo=@doit;');
  24537. Add(' b:=onbar=@doit;');
  24538. Add(' b:=fonfoo<>@doit;');
  24539. Add(' b:=onfoo<>@doit;');
  24540. Add(' b:=onbar<>@doit;');
  24541. Add(' b:=Assigned(fonfoo);');
  24542. Add(' b:=Assigned(onfoo);');
  24543. Add(' b:=Assigned(onbar);');
  24544. Add('end;');
  24545. ConvertProgram;
  24546. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  24547. LinesToStr([ // statements
  24548. 'rtl.createClass($mod, "TObject", null, function () {',
  24549. ' this.$init = function () {',
  24550. ' this.FOnFoo = null;',
  24551. ' };',
  24552. ' this.$final = function () {',
  24553. ' this.FOnFoo = undefined;',
  24554. ' };',
  24555. ' this.DoIt = function (vA) {',
  24556. ' var Result = 0;',
  24557. ' return Result;',
  24558. ' };',
  24559. ' this.GetFoo = function () {',
  24560. ' var Result = null;',
  24561. ' return Result;',
  24562. ' };',
  24563. ' this.SetFoo = function (Value) {',
  24564. ' };',
  24565. '});',
  24566. 'this.Obj = null;',
  24567. 'this.vP = null;',
  24568. 'this.b = false;'
  24569. ]),
  24570. LinesToStr([
  24571. 'var $with1 = $mod.Obj;',
  24572. '$with1.FOnFoo = null;',
  24573. '$with1.FOnFoo = null;',
  24574. '$with1.SetFoo(null);',
  24575. '$with1.FOnFoo = $with1.FOnFoo;',
  24576. '$with1.FOnFoo = $with1.FOnFoo;',
  24577. '$with1.SetFoo($with1.GetFoo());',
  24578. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  24579. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  24580. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  24581. '$with1.FOnFoo(1);',
  24582. '$with1.FOnFoo(1);',
  24583. '$with1.GetFoo();',
  24584. '$with1.FOnFoo(1);',
  24585. '$with1.FOnFoo(1);',
  24586. '$with1.GetFoo()(1);',
  24587. '$mod.b = $with1.FOnFoo === null;',
  24588. '$mod.b = $with1.FOnFoo === null;',
  24589. '$mod.b = $with1.GetFoo() === null;',
  24590. '$mod.b = $with1.FOnFoo !== null;',
  24591. '$mod.b = $with1.FOnFoo !== null;',
  24592. '$mod.b = $with1.GetFoo() !== null;',
  24593. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  24594. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  24595. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  24596. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24597. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24598. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  24599. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24600. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  24601. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  24602. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24603. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24604. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  24605. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24606. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  24607. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  24608. '$mod.b = $with1.FOnFoo != null;',
  24609. '$mod.b = $with1.FOnFoo != null;',
  24610. '$mod.b = $with1.GetFoo() != null;',
  24611. '']));
  24612. end;
  24613. procedure TTestModule.TestProcType_Nested;
  24614. begin
  24615. StartProgram(false);
  24616. Add([
  24617. 'type',
  24618. ' TProcInt = procedure(vI: longint = 1);',
  24619. 'procedure DoIt(vJ: longint);',
  24620. 'var aProc: TProcInt;',
  24621. ' b: boolean;',
  24622. ' procedure Sub(vK: longint);',
  24623. ' var aSub: TProcInt;',
  24624. ' procedure SubSub(vK: longint);',
  24625. ' var aSubSub: TProcInt;',
  24626. ' begin;',
  24627. ' aProc:=@DoIt;',
  24628. ' aSub:=@DoIt;',
  24629. ' aSubSub:=@DoIt;',
  24630. ' aProc:=@Sub;',
  24631. ' aSub:=@Sub;',
  24632. ' aSubSub:=@Sub;',
  24633. ' aProc:=@SubSub;',
  24634. ' aSub:=@SubSub;',
  24635. ' aSubSub:=@SubSub;',
  24636. ' end;',
  24637. ' begin;',
  24638. ' end;',
  24639. 'begin;',
  24640. ' aProc:=@Sub;',
  24641. ' b:=aProc=@Sub;',
  24642. ' b:=@Sub=aProc;',
  24643. 'end;',
  24644. 'begin',
  24645. '']);
  24646. ConvertProgram;
  24647. CheckSource('TestProcType_Nested',
  24648. LinesToStr([ // statements
  24649. 'this.DoIt = function (vJ) {',
  24650. ' var aProc = null;',
  24651. ' var b = false;',
  24652. ' function Sub(vK) {',
  24653. ' var aSub = null;',
  24654. ' function SubSub(vK) {',
  24655. ' var aSubSub = null;',
  24656. ' aProc = $mod.DoIt;',
  24657. ' aSub = $mod.DoIt;',
  24658. ' aSubSub = $mod.DoIt;',
  24659. ' aProc = Sub;',
  24660. ' aSub = Sub;',
  24661. ' aSubSub = Sub;',
  24662. ' aProc = SubSub;',
  24663. ' aSub = SubSub;',
  24664. ' aSubSub = SubSub;',
  24665. ' };',
  24666. ' };',
  24667. ' aProc = Sub;',
  24668. ' b = rtl.eqCallback(aProc, Sub);',
  24669. ' b = rtl.eqCallback(Sub, aProc);',
  24670. '};',
  24671. '']),
  24672. LinesToStr([ // $mod.$main
  24673. '']));
  24674. end;
  24675. procedure TTestModule.TestProcType_NestedOfObject;
  24676. begin
  24677. StartProgram(false);
  24678. Add([
  24679. 'type',
  24680. ' TProcInt = procedure(vI: longint = 1) of object;',
  24681. ' TObject = class',
  24682. ' procedure DoIt(vJ: longint);',
  24683. ' end;',
  24684. 'procedure TObject.DoIt(vJ: longint);',
  24685. 'var aProc: TProcInt;',
  24686. ' b: boolean;',
  24687. ' procedure Sub(vK: longint);',
  24688. ' var aSub: TProcInt;',
  24689. ' procedure SubSub(vK: longint);',
  24690. ' var aSubSub: TProcInt;',
  24691. ' begin;',
  24692. ' aProc:=@DoIt;',
  24693. ' aSub:=@DoIt;',
  24694. ' aSubSub:=@DoIt;',
  24695. ' aProc:=@Sub;',
  24696. ' aSub:=@Sub;',
  24697. ' aSubSub:=@Sub;',
  24698. ' aProc:=@SubSub;',
  24699. ' aSub:=@SubSub;',
  24700. ' aSubSub:=@SubSub;',
  24701. ' end;',
  24702. ' begin;',
  24703. ' end;',
  24704. 'begin;',
  24705. ' aProc:=@Sub;',
  24706. ' b:=aProc=@Sub;',
  24707. ' b:=@Sub=aProc;',
  24708. 'end;',
  24709. 'begin',
  24710. '']);
  24711. ConvertProgram;
  24712. CheckSource('TestProcType_Nested',
  24713. LinesToStr([ // statements
  24714. 'rtl.createClass($mod, "TObject", null, function () {',
  24715. ' this.$init = function () {',
  24716. ' };',
  24717. ' this.$final = function () {',
  24718. ' };',
  24719. ' this.DoIt = function (vJ) {',
  24720. ' var $Self = this;',
  24721. ' var aProc = null;',
  24722. ' var b = false;',
  24723. ' function Sub(vK) {',
  24724. ' var aSub = null;',
  24725. ' function SubSub(vK) {',
  24726. ' var aSubSub = null;',
  24727. ' aProc = rtl.createCallback($Self, "DoIt");',
  24728. ' aSub = rtl.createCallback($Self, "DoIt");',
  24729. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  24730. ' aProc = Sub;',
  24731. ' aSub = Sub;',
  24732. ' aSubSub = Sub;',
  24733. ' aProc = SubSub;',
  24734. ' aSub = SubSub;',
  24735. ' aSubSub = SubSub;',
  24736. ' };',
  24737. ' };',
  24738. ' aProc = Sub;',
  24739. ' b = rtl.eqCallback(aProc, Sub);',
  24740. ' b = rtl.eqCallback(Sub, aProc);',
  24741. ' };',
  24742. '});',
  24743. '']),
  24744. LinesToStr([ // $mod.$main
  24745. '']));
  24746. end;
  24747. procedure TTestModule.TestProcType_ReferenceToProc;
  24748. begin
  24749. StartProgram(false);
  24750. Add([
  24751. 'type',
  24752. ' TProcRef = reference to procedure(i: longint = 0);',
  24753. ' TFuncRef = reference to function(i: longint = 0): longint;',
  24754. 'var',
  24755. ' p: TProcRef;',
  24756. ' f: TFuncRef;',
  24757. 'procedure DoIt(i: longint);',
  24758. 'begin',
  24759. 'end;',
  24760. 'function GetIt(i: longint): longint;',
  24761. 'begin',
  24762. ' p:=@DoIt;',
  24763. ' f:=@GetIt;',
  24764. ' f;',
  24765. ' f();',
  24766. ' f(1);',
  24767. 'end;',
  24768. 'begin',
  24769. ' p:=@DoIt;',
  24770. ' f:=@GetIt;',
  24771. ' f;',
  24772. ' f();',
  24773. ' f(1);',
  24774. ' p:=TProcRef(f);',
  24775. '']);
  24776. ConvertProgram;
  24777. CheckSource('TestProcType_ReferenceToProc',
  24778. LinesToStr([ // statements
  24779. 'this.p = null;',
  24780. 'this.f = null;',
  24781. 'this.DoIt = function (i) {',
  24782. '};',
  24783. 'this.GetIt = function (i) {',
  24784. ' var Result = 0;',
  24785. ' $mod.p = $mod.DoIt;',
  24786. ' $mod.f = $mod.GetIt;',
  24787. ' $mod.f(0);',
  24788. ' $mod.f(0);',
  24789. ' $mod.f(1);',
  24790. ' return Result;',
  24791. '};',
  24792. '']),
  24793. LinesToStr([ // $mod.$main
  24794. '$mod.p = $mod.DoIt;',
  24795. '$mod.f = $mod.GetIt;',
  24796. '$mod.f(0);',
  24797. '$mod.f(0);',
  24798. '$mod.f(1);',
  24799. '$mod.p = $mod.f;',
  24800. '']));
  24801. end;
  24802. procedure TTestModule.TestProcType_ReferenceToMethod;
  24803. begin
  24804. StartProgram(false);
  24805. Add([
  24806. 'type',
  24807. ' TFuncRef = reference to function(i: longint = 5): longint;',
  24808. ' TObject = class',
  24809. ' function Grow(s: longint): longint;',
  24810. ' end;',
  24811. 'var',
  24812. ' f: tfuncref;',
  24813. 'function tobject.grow(s: longint): longint;',
  24814. ' function GrowSub(i: longint): longint;',
  24815. ' begin',
  24816. ' f:=@grow;',
  24817. ' f:=@growsub;',
  24818. ' end;',
  24819. 'begin',
  24820. ' f:=@grow;',
  24821. ' f:=@growsub;',
  24822. 'end;',
  24823. 'begin',
  24824. '']);
  24825. ConvertProgram;
  24826. CheckSource('TestProcType_ReferenceToMethod',
  24827. LinesToStr([ // statements
  24828. 'rtl.createClass($mod, "TObject", null, function () {',
  24829. ' this.$init = function () {',
  24830. ' };',
  24831. ' this.$final = function () {',
  24832. ' };',
  24833. ' this.Grow = function (s) {',
  24834. ' var $Self = this;',
  24835. ' var Result = 0;',
  24836. ' function GrowSub(i) {',
  24837. ' var Result = 0;',
  24838. ' $mod.f = rtl.createCallback($Self, "Grow");',
  24839. ' $mod.f = GrowSub;',
  24840. ' return Result;',
  24841. ' };',
  24842. ' $mod.f = rtl.createCallback($Self, "Grow");',
  24843. ' $mod.f = GrowSub;',
  24844. ' return Result;',
  24845. ' };',
  24846. '});',
  24847. 'this.f = null;',
  24848. '']),
  24849. LinesToStr([ // $mod.$main
  24850. '']));
  24851. end;
  24852. procedure TTestModule.TestProcType_Typecast;
  24853. begin
  24854. StartProgram(false);
  24855. Add([
  24856. 'type',
  24857. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  24858. ' TEvent = procedure of object;',
  24859. ' TGetter = function:longint of object;',
  24860. ' TProcA = procedure(i: longint);',
  24861. ' TFuncB = function(i, j: longint): longint;',
  24862. 'procedure DoIt(); varargs; begin end;',
  24863. 'var',
  24864. ' Notify: tnotifyevent;',
  24865. ' Event: tevent;',
  24866. ' Getter: tgetter;',
  24867. ' ProcA: tproca;',
  24868. ' FuncB: tfuncb;',
  24869. ' p: pointer;',
  24870. 'begin',
  24871. ' notify:=tnotifyevent(event);',
  24872. ' event:=tevent(event);',
  24873. ' event:=tevent(notify);',
  24874. ' event:=tevent(getter);',
  24875. ' event:=tevent(proca);',
  24876. ' proca:=tproca(funcb);',
  24877. ' funcb:=tfuncb(funcb);',
  24878. ' funcb:=tfuncb(proca);',
  24879. ' funcb:=tfuncb(getter);',
  24880. ' proca:=tproca(p);',
  24881. ' funcb:=tfuncb(p);',
  24882. ' getter:=tgetter(p);',
  24883. ' p:=pointer(notify);',
  24884. ' p:=notify;',
  24885. ' p:=pointer(proca);',
  24886. ' p:=proca;',
  24887. ' p:=pointer(funcb);',
  24888. ' p:=funcb;',
  24889. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  24890. '']);
  24891. ConvertProgram;
  24892. CheckSource('TestProcType_Typecast',
  24893. LinesToStr([ // statements
  24894. 'this.DoIt = function () {',
  24895. '};',
  24896. 'this.Notify = null;',
  24897. 'this.Event = null;',
  24898. 'this.Getter = null;',
  24899. 'this.ProcA = null;',
  24900. 'this.FuncB = null;',
  24901. 'this.p = null;',
  24902. '']),
  24903. LinesToStr([ // $mod.$main
  24904. '$mod.Notify = $mod.Event;',
  24905. '$mod.Event = $mod.Event;',
  24906. '$mod.Event = $mod.Notify;',
  24907. '$mod.Event = $mod.Getter;',
  24908. '$mod.Event = $mod.ProcA;',
  24909. '$mod.ProcA = $mod.FuncB;',
  24910. '$mod.FuncB = $mod.FuncB;',
  24911. '$mod.FuncB = $mod.ProcA;',
  24912. '$mod.FuncB = $mod.Getter;',
  24913. '$mod.ProcA = $mod.p;',
  24914. '$mod.FuncB = $mod.p;',
  24915. '$mod.Getter = $mod.p;',
  24916. '$mod.p = $mod.Notify;',
  24917. '$mod.p = $mod.Notify;',
  24918. '$mod.p = $mod.ProcA;',
  24919. '$mod.p = $mod.ProcA;',
  24920. '$mod.p = $mod.FuncB;',
  24921. '$mod.p = $mod.FuncB;',
  24922. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  24923. '']));
  24924. end;
  24925. procedure TTestModule.TestProcType_PassProcToUntyped;
  24926. begin
  24927. StartProgram(false);
  24928. Add([
  24929. 'type',
  24930. ' TEvent = procedure of object;',
  24931. ' TFunc = function: longint;',
  24932. 'procedure DoIt(); varargs; begin end;',
  24933. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  24934. 'var',
  24935. ' Event: tevent;',
  24936. ' Func: TFunc;',
  24937. 'begin',
  24938. ' doit(event,func);',
  24939. ' dosome(event,event,event);',
  24940. ' dosome(func,func,func);',
  24941. '']);
  24942. ConvertProgram;
  24943. CheckSource('TestProcType_PassProcToUntyped',
  24944. LinesToStr([ // statements
  24945. 'this.DoIt = function () {',
  24946. '};',
  24947. 'this.DoSome = function (a, b, p) {',
  24948. '};',
  24949. 'this.Event = null;',
  24950. 'this.Func = null;',
  24951. '']),
  24952. LinesToStr([ // $mod.$main
  24953. '$mod.DoIt($mod.Event, $mod.Func);',
  24954. '$mod.DoSome($mod.Event, {',
  24955. ' p: $mod,',
  24956. ' get: function () {',
  24957. ' return this.p.Event;',
  24958. ' },',
  24959. ' set: function (v) {',
  24960. ' this.p.Event = v;',
  24961. ' }',
  24962. '}, $mod.Event);',
  24963. '$mod.DoSome($mod.Func, {',
  24964. ' p: $mod,',
  24965. ' get: function () {',
  24966. ' return this.p.Func;',
  24967. ' },',
  24968. ' set: function (v) {',
  24969. ' this.p.Func = v;',
  24970. ' }',
  24971. '}, $mod.Func);',
  24972. '']));
  24973. end;
  24974. procedure TTestModule.TestProcType_PassProcToArray;
  24975. begin
  24976. StartProgram(false);
  24977. Add([
  24978. 'type',
  24979. ' TFunc = function: longint;',
  24980. ' TArrFunc = array of TFunc;',
  24981. 'procedure DoIt(Arr: TArrFunc); begin end;',
  24982. 'function GetIt: longint; begin end;',
  24983. 'var',
  24984. ' Func: tfunc;',
  24985. 'begin',
  24986. ' doit([]);',
  24987. ' doit([@GetIt]);',
  24988. ' doit([Func]);',
  24989. '']);
  24990. ConvertProgram;
  24991. CheckSource('TestProcType_PassProcToArray',
  24992. LinesToStr([ // statements
  24993. 'this.DoIt = function (Arr) {',
  24994. '};',
  24995. 'this.GetIt = function () {',
  24996. ' var Result = 0;',
  24997. ' return Result;',
  24998. '};',
  24999. 'this.Func = null;',
  25000. '']),
  25001. LinesToStr([ // $mod.$main
  25002. '$mod.DoIt([]);',
  25003. '$mod.DoIt([$mod.GetIt]);',
  25004. '$mod.DoIt([$mod.Func]);',
  25005. '']));
  25006. end;
  25007. procedure TTestModule.TestPointer;
  25008. begin
  25009. StartProgram(false);
  25010. Add(['type',
  25011. ' TObject = class end;',
  25012. ' TClass = class of TObject;',
  25013. ' TArrInt = array of longint;',
  25014. 'const',
  25015. ' n = nil;',
  25016. 'var',
  25017. ' v: jsvalue;',
  25018. ' Obj: tobject;',
  25019. ' C: tclass;',
  25020. ' a: tarrint;',
  25021. ' p: Pointer = nil;',
  25022. ' s: string;',
  25023. 'begin',
  25024. ' p:=p;',
  25025. ' p:=nil;',
  25026. ' if p=nil then;',
  25027. ' if nil=p then;',
  25028. ' if Assigned(p) then;',
  25029. ' p:=Pointer(v);',
  25030. ' p:=obj;',
  25031. ' p:=c;',
  25032. ' p:=a;',
  25033. ' p:=tobject;',
  25034. ' obj:=TObject(p);',
  25035. ' c:=TClass(p);',
  25036. ' a:=TArrInt(p);',
  25037. ' p:=n;',
  25038. ' p:=Pointer(a);',
  25039. ' p:=pointer(s);',
  25040. ' s:=string(p);',
  25041. '']);
  25042. ConvertProgram;
  25043. CheckSource('TestPointer',
  25044. LinesToStr([ // statements
  25045. 'rtl.createClass($mod, "TObject", null, function () {',
  25046. ' this.$init = function () {',
  25047. ' };',
  25048. ' this.$final = function () {',
  25049. ' };',
  25050. '});',
  25051. 'this.n = null;',
  25052. 'this.v = undefined;',
  25053. 'this.Obj = null;',
  25054. 'this.C = null;',
  25055. 'this.a = [];',
  25056. 'this.p = null;',
  25057. 'this.s = "";',
  25058. '']),
  25059. LinesToStr([ // $mod.$main
  25060. '$mod.p = $mod.p;',
  25061. '$mod.p = null;',
  25062. 'if ($mod.p === null) ;',
  25063. 'if (null === $mod.p) ;',
  25064. 'if ($mod.p != null) ;',
  25065. '$mod.p = $mod.v;',
  25066. '$mod.p = $mod.Obj;',
  25067. '$mod.p = $mod.C;',
  25068. '$mod.p = $mod.a;',
  25069. '$mod.p = $mod.TObject;',
  25070. '$mod.Obj = $mod.p;',
  25071. '$mod.C = $mod.p;',
  25072. '$mod.a = $mod.p;',
  25073. '$mod.p = null;',
  25074. '$mod.p = $mod.a;',
  25075. '$mod.p = $mod.s;',
  25076. '$mod.s = $mod.p;',
  25077. '']));
  25078. end;
  25079. procedure TTestModule.TestPointer_Proc;
  25080. begin
  25081. StartProgram(false);
  25082. Add('type');
  25083. Add(' TObject = class');
  25084. Add(' procedure DoIt; virtual; abstract;');
  25085. Add(' end;');
  25086. Add('procedure DoSome; begin end;');
  25087. Add('var');
  25088. Add(' o: TObject;');
  25089. Add(' p: Pointer;');
  25090. Add('begin');
  25091. Add(' p:=@DoSome;');
  25092. Add(' p:[email protected];');
  25093. ConvertProgram;
  25094. CheckSource('TestPointer_Proc',
  25095. LinesToStr([ // statements
  25096. 'rtl.createClass($mod, "TObject", null, function () {',
  25097. ' this.$init = function () {',
  25098. ' };',
  25099. ' this.$final = function () {',
  25100. ' };',
  25101. '});',
  25102. 'this.DoSome = function () {',
  25103. '};',
  25104. 'this.o = null;',
  25105. 'this.p = null;',
  25106. '']),
  25107. LinesToStr([ // $mod.$main
  25108. '$mod.p = $mod.DoSome;',
  25109. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  25110. '']));
  25111. end;
  25112. procedure TTestModule.TestPointer_AssignRecordFail;
  25113. begin
  25114. StartProgram(false);
  25115. Add('type');
  25116. Add(' TRec = record end;');
  25117. Add('var');
  25118. Add(' p: Pointer;');
  25119. Add(' r: TRec;');
  25120. Add('begin');
  25121. Add(' p:=r;');
  25122. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  25123. nIncompatibleTypesGotExpected);
  25124. ConvertProgram;
  25125. end;
  25126. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  25127. begin
  25128. StartProgram(false);
  25129. Add('type');
  25130. Add(' TArr = array[boolean] of longint;');
  25131. Add('var');
  25132. Add(' p: Pointer;');
  25133. Add(' a: TArr;');
  25134. Add('begin');
  25135. Add(' p:=a;');
  25136. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  25137. nIncompatibleTypesGotExpected);
  25138. ConvertProgram;
  25139. end;
  25140. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  25141. begin
  25142. StartProgram(false);
  25143. Add([
  25144. 'procedure DoIt(args: array of jsvalue); begin end;',
  25145. 'procedure DoAll; varargs; begin end;',
  25146. 'var',
  25147. ' v: jsvalue;',
  25148. 'begin',
  25149. ' DoIt([pointer(v)]);',
  25150. ' DoAll(pointer(v));',
  25151. '']);
  25152. ConvertProgram;
  25153. CheckSource('TestPointer_TypeCastJSValueToPointer',
  25154. LinesToStr([ // statements
  25155. 'this.DoIt = function (args) {',
  25156. '};',
  25157. 'this.DoAll = function () {',
  25158. '};',
  25159. 'this.v = undefined;',
  25160. '']),
  25161. LinesToStr([ // $mod.$main
  25162. '$mod.DoIt([$mod.v]);',
  25163. '$mod.DoAll($mod.v);',
  25164. '']));
  25165. end;
  25166. procedure TTestModule.TestPointer_NonRecordFail;
  25167. begin
  25168. StartProgram(false);
  25169. Add([
  25170. 'type',
  25171. ' p = ^longint;',
  25172. 'begin',
  25173. '']);
  25174. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  25175. ConvertProgram;
  25176. end;
  25177. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  25178. begin
  25179. StartProgram(false);
  25180. Add([
  25181. 'procedure DoIt(p: ^longint); begin end;',
  25182. 'begin',
  25183. '']);
  25184. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25185. ConvertProgram;
  25186. end;
  25187. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  25188. begin
  25189. StartProgram(false);
  25190. Add([
  25191. 'var p: ^longint;',
  25192. 'begin',
  25193. '']);
  25194. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25195. ConvertProgram;
  25196. end;
  25197. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  25198. begin
  25199. StartProgram(false);
  25200. Add([
  25201. 'function DoIt: ^longint; begin end;',
  25202. 'begin',
  25203. '']);
  25204. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  25205. ConvertProgram;
  25206. end;
  25207. procedure TTestModule.TestPointer_AddrOperatorFail;
  25208. begin
  25209. StartProgram(false);
  25210. Add([
  25211. 'var i: longint;',
  25212. 'begin',
  25213. ' if @i=nil then ;',
  25214. '']);
  25215. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  25216. ConvertProgram;
  25217. end;
  25218. procedure TTestModule.TestPointer_ArrayParamsFail;
  25219. begin
  25220. StartProgram(false);
  25221. Add([
  25222. 'var',
  25223. ' p: Pointer;',
  25224. 'begin',
  25225. ' p:=p[1];',
  25226. '']);
  25227. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  25228. ConvertProgram;
  25229. end;
  25230. procedure TTestModule.TestPointer_PointerAddFail;
  25231. begin
  25232. StartProgram(false);
  25233. Add([
  25234. 'var',
  25235. ' p: Pointer;',
  25236. 'begin',
  25237. ' p:=p+1;',
  25238. '']);
  25239. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  25240. ConvertProgram;
  25241. end;
  25242. procedure TTestModule.TestPointer_IncPointerFail;
  25243. begin
  25244. StartProgram(false);
  25245. Add([
  25246. 'var',
  25247. ' p: Pointer;',
  25248. 'begin',
  25249. ' inc(p,1);',
  25250. '']);
  25251. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  25252. nIncompatibleTypeArgNo);
  25253. ConvertProgram;
  25254. end;
  25255. procedure TTestModule.TestPointer_Record;
  25256. begin
  25257. StartProgram(false);
  25258. Add([
  25259. 'type',
  25260. ' TRec = record x: longint; end;',
  25261. ' PRec = ^TRec;',
  25262. 'var',
  25263. ' r: TRec;',
  25264. ' p: PRec;',
  25265. ' q: ^TRec;',
  25266. ' Ptr: pointer;',
  25267. 'begin',
  25268. ' new(p);',
  25269. ' p:=@r;',
  25270. ' r:=p^;',
  25271. ' r.x:=p^.x;',
  25272. ' p^.x:=r.x;',
  25273. ' if p^.x=3 then ;',
  25274. ' if 4=p^.x then ;',
  25275. ' dispose(p);',
  25276. ' new(q);',
  25277. ' dispose(q);',
  25278. ' Ptr:=p;',
  25279. ' p:=PRec(ptr);',
  25280. '']);
  25281. ConvertProgram;
  25282. CheckSource('TestPointer_Record',
  25283. LinesToStr([ // statements
  25284. 'rtl.recNewT($mod, "TRec", function () {',
  25285. ' this.x = 0;',
  25286. ' this.$eq = function (b) {',
  25287. ' return this.x === b.x;',
  25288. ' };',
  25289. ' this.$assign = function (s) {',
  25290. ' this.x = s.x;',
  25291. ' return this;',
  25292. ' };',
  25293. '});',
  25294. 'this.r = $mod.TRec.$new();',
  25295. 'this.p = null;',
  25296. 'this.q = null;',
  25297. 'this.Ptr = null;',
  25298. '']),
  25299. LinesToStr([ // $mod.$main
  25300. '$mod.p = $mod.TRec.$new();',
  25301. '$mod.p = $mod.r;',
  25302. '$mod.r.$assign($mod.p);',
  25303. '$mod.r.x = $mod.p.x;',
  25304. '$mod.p.x = $mod.r.x;',
  25305. 'if ($mod.p.x === 3) ;',
  25306. 'if (4 === $mod.p.x) ;',
  25307. '$mod.p = null;',
  25308. '$mod.q = $mod.TRec.$new();',
  25309. '$mod.q = null;',
  25310. '$mod.Ptr = $mod.p;',
  25311. '$mod.p = $mod.Ptr;',
  25312. '']));
  25313. end;
  25314. procedure TTestModule.TestPointer_RecordArg;
  25315. begin
  25316. StartProgram(false);
  25317. Add([
  25318. '{$modeswitch autoderef}',
  25319. 'type',
  25320. ' TRec = record x: longint; end;',
  25321. ' PRec = ^TRec;',
  25322. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  25323. 'begin',
  25324. ' a.x:=a.x;',
  25325. ' a^.x:=a^.x;',
  25326. ' with a^ do',
  25327. ' x:=x;',
  25328. 'end;',
  25329. 'function GetIt(p: PRec): PRec;',
  25330. 'begin',
  25331. ' p.x:=p.x;',
  25332. ' p^.x:=p^.x;',
  25333. ' with p^ do',
  25334. ' x:=x;',
  25335. 'end;',
  25336. 'var',
  25337. ' r: TRec;',
  25338. ' p: PRec;',
  25339. 'begin',
  25340. ' p:=GetIt(p);',
  25341. ' p^:=GetIt(@r)^;',
  25342. ' DoIt(p,p,p);',
  25343. ' DoIt(@r,p,p);',
  25344. '']);
  25345. ConvertProgram;
  25346. CheckSource('TestPointer_Record',
  25347. LinesToStr([ // statements
  25348. 'rtl.recNewT($mod, "TRec", function () {',
  25349. ' this.x = 0;',
  25350. ' this.$eq = function (b) {',
  25351. ' return this.x === b.x;',
  25352. ' };',
  25353. ' this.$assign = function (s) {',
  25354. ' this.x = s.x;',
  25355. ' return this;',
  25356. ' };',
  25357. '});',
  25358. 'this.DoIt = function (a, b, c) {',
  25359. ' var Result = $mod.TRec.$new();',
  25360. ' a.x = a.x;',
  25361. ' a.x = a.x;',
  25362. ' a.x = a.x;',
  25363. ' return Result;',
  25364. '};',
  25365. 'this.GetIt = function (p) {',
  25366. ' var Result = null;',
  25367. ' p.x = p.x;',
  25368. ' p.x = p.x;',
  25369. ' p.x = p.x;',
  25370. ' return Result;',
  25371. '};',
  25372. 'this.r = $mod.TRec.$new();',
  25373. 'this.p = null;',
  25374. '']),
  25375. LinesToStr([ // $mod.$main
  25376. '$mod.p = $mod.GetIt($mod.p);',
  25377. '$mod.p.$assign($mod.GetIt($mod.r));',
  25378. '$mod.DoIt($mod.p, {',
  25379. ' p: $mod,',
  25380. ' get: function () {',
  25381. ' return this.p.p;',
  25382. ' },',
  25383. ' set: function (v) {',
  25384. ' this.p.p = v;',
  25385. ' }',
  25386. '}, {',
  25387. ' p: $mod,',
  25388. ' get: function () {',
  25389. ' return this.p.p;',
  25390. ' },',
  25391. ' set: function (v) {',
  25392. ' this.p.p = v;',
  25393. ' }',
  25394. '});',
  25395. '$mod.DoIt($mod.r, {',
  25396. ' p: $mod,',
  25397. ' get: function () {',
  25398. ' return this.p.p;',
  25399. ' },',
  25400. ' set: function (v) {',
  25401. ' this.p.p = v;',
  25402. ' }',
  25403. '}, {',
  25404. ' p: $mod,',
  25405. ' get: function () {',
  25406. ' return this.p.p;',
  25407. ' },',
  25408. ' set: function (v) {',
  25409. ' this.p.p = v;',
  25410. ' }',
  25411. '});',
  25412. '']));
  25413. end;
  25414. procedure TTestModule.TestJSValue_AssignToJSValue;
  25415. begin
  25416. StartProgram(false);
  25417. Add('var');
  25418. Add(' v: jsvalue;');
  25419. Add(' i: longint;');
  25420. Add(' s: string;');
  25421. Add(' b: boolean;');
  25422. Add(' d: double;');
  25423. Add(' p: pointer;');
  25424. Add('begin');
  25425. Add(' v:=v;');
  25426. Add(' v:=1;');
  25427. Add(' v:=i;');
  25428. Add(' v:='''';');
  25429. Add(' v:=''c'';');
  25430. Add(' v:=''foo'';');
  25431. Add(' v:=s;');
  25432. Add(' v:=false;');
  25433. Add(' v:=true;');
  25434. Add(' v:=b;');
  25435. Add(' v:=0.1;');
  25436. Add(' v:=d;');
  25437. Add(' v:=nil;');
  25438. Add(' v:=p;');
  25439. ConvertProgram;
  25440. CheckSource('TestJSValue_AssignToJSValue',
  25441. LinesToStr([ // statements
  25442. 'this.v = undefined;',
  25443. 'this.i = 0;',
  25444. 'this.s = "";',
  25445. 'this.b = false;',
  25446. 'this.d = 0.0;',
  25447. 'this.p = null;',
  25448. '']),
  25449. LinesToStr([ // $mod.$main
  25450. '$mod.v = $mod.v;',
  25451. '$mod.v = 1;',
  25452. '$mod.v = $mod.i;',
  25453. '$mod.v = "";',
  25454. '$mod.v = "c";',
  25455. '$mod.v = "foo";',
  25456. '$mod.v = $mod.s;',
  25457. '$mod.v = false;',
  25458. '$mod.v = true;',
  25459. '$mod.v = $mod.b;',
  25460. '$mod.v = 0.1;',
  25461. '$mod.v = $mod.d;',
  25462. '$mod.v = null;',
  25463. '$mod.v = $mod.p;',
  25464. '']));
  25465. end;
  25466. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  25467. begin
  25468. StartProgram(false);
  25469. Add('type');
  25470. Add(' integer = longint;');
  25471. Add(' TYesNo = boolean;');
  25472. Add(' TFloat = double;');
  25473. Add(' TCaption = string;');
  25474. Add(' TChar = char;');
  25475. Add('var');
  25476. Add(' v: jsvalue;');
  25477. Add(' i: integer;');
  25478. Add(' s: TCaption;');
  25479. Add(' b: TYesNo;');
  25480. Add(' d: TFloat;');
  25481. Add(' c: char;');
  25482. Add('begin');
  25483. Add(' i:=longint(v);');
  25484. Add(' i:=integer(v);');
  25485. Add(' s:=string(v);');
  25486. Add(' s:=TCaption(v);');
  25487. Add(' b:=boolean(v);');
  25488. Add(' b:=TYesNo(v);');
  25489. Add(' d:=double(v);');
  25490. Add(' d:=TFloat(v);');
  25491. Add(' c:=char(v);');
  25492. Add(' c:=TChar(v);');
  25493. ConvertProgram;
  25494. CheckSource('TestJSValue_TypeCastToBaseType',
  25495. LinesToStr([ // statements
  25496. 'this.v = undefined;',
  25497. 'this.i = 0;',
  25498. 'this.s = "";',
  25499. 'this.b = false;',
  25500. 'this.d = 0.0;',
  25501. 'this.c = "";',
  25502. '']),
  25503. LinesToStr([ // $mod.$main
  25504. '$mod.i = Math.floor($mod.v);',
  25505. '$mod.i = Math.floor($mod.v);',
  25506. '$mod.s = "" + $mod.v;',
  25507. '$mod.s = "" + $mod.v;',
  25508. '$mod.b = !($mod.v == false);',
  25509. '$mod.b = !($mod.v == false);',
  25510. '$mod.d = rtl.getNumber($mod.v);',
  25511. '$mod.d = rtl.getNumber($mod.v);',
  25512. '$mod.c = rtl.getChar($mod.v);',
  25513. '$mod.c = rtl.getChar($mod.v);',
  25514. '']));
  25515. end;
  25516. procedure TTestModule.TestJSValue_TypecastToJSValue;
  25517. begin
  25518. StartProgram(false);
  25519. Add([
  25520. 'type',
  25521. ' TArr = array of word;',
  25522. ' TRec = record end;',
  25523. ' TSet = set of boolean;',
  25524. 'procedure Fly(v: jsvalue);',
  25525. 'begin',
  25526. 'end;',
  25527. 'var',
  25528. ' a: TArr;',
  25529. ' r: TRec;',
  25530. ' s: TSet;',
  25531. 'begin',
  25532. ' Fly(jsvalue(a));',
  25533. ' Fly(jsvalue(r));',
  25534. ' Fly(jsvalue(s));',
  25535. '']);
  25536. ConvertProgram;
  25537. CheckSource('TestJSValue_TypecastToJSValue',
  25538. LinesToStr([ // statements
  25539. 'rtl.recNewT($mod, "TRec", function () {',
  25540. ' this.$eq = function (b) {',
  25541. ' return true;',
  25542. ' };',
  25543. ' this.$assign = function (s) {',
  25544. ' return this;',
  25545. ' };',
  25546. '});',
  25547. 'this.Fly = function (v) {',
  25548. '};',
  25549. 'this.a = [];',
  25550. 'this.r = $mod.TRec.$new();',
  25551. 'this.s = {};',
  25552. '']),
  25553. LinesToStr([ // $mod.$main
  25554. '$mod.Fly($mod.a);',
  25555. '$mod.Fly($mod.r);',
  25556. '$mod.Fly($mod.s);',
  25557. '']));
  25558. end;
  25559. procedure TTestModule.TestJSValue_Equal;
  25560. begin
  25561. StartProgram(false);
  25562. Add('type');
  25563. Add(' integer = longint;');
  25564. Add(' TYesNo = boolean;');
  25565. Add(' TFloat = double;');
  25566. Add(' TCaption = string;');
  25567. Add(' TChar = char;');
  25568. Add(' TMulti = JSValue;');
  25569. Add('var');
  25570. Add(' v: jsvalue;');
  25571. Add(' i: integer;');
  25572. Add(' s: TCaption;');
  25573. Add(' b: TYesNo;');
  25574. Add(' d: TFloat;');
  25575. Add(' c: char;');
  25576. Add(' m: TMulti;');
  25577. Add('begin');
  25578. Add(' b:=v=v;');
  25579. Add(' b:=v<>v;');
  25580. Add(' b:=v=1;');
  25581. Add(' b:=v<>1;');
  25582. Add(' b:=2=v;');
  25583. Add(' b:=2<>v;');
  25584. Add(' b:=v=i;');
  25585. Add(' b:=i=v;');
  25586. Add(' b:=v=nil;');
  25587. Add(' b:=nil=v;');
  25588. Add(' b:=v=false;');
  25589. Add(' b:=true=v;');
  25590. Add(' b:=v=b;');
  25591. Add(' b:=b=v;');
  25592. Add(' b:=v=s;');
  25593. Add(' b:=s=v;');
  25594. Add(' b:=v=''foo'';');
  25595. Add(' b:=''''=v;');
  25596. Add(' b:=v=d;');
  25597. Add(' b:=d=v;');
  25598. Add(' b:=v=3.4;');
  25599. Add(' b:=5.6=v;');
  25600. Add(' b:=v=c;');
  25601. Add(' b:=c=v;');
  25602. Add(' b:=m=m;');
  25603. Add(' b:=v=m;');
  25604. Add(' b:=m=v;');
  25605. ConvertProgram;
  25606. CheckSource('TestJSValue_Equal',
  25607. LinesToStr([ // statements
  25608. 'this.v = undefined;',
  25609. 'this.i = 0;',
  25610. 'this.s = "";',
  25611. 'this.b = false;',
  25612. 'this.d = 0.0;',
  25613. 'this.c = "";',
  25614. 'this.m = undefined;',
  25615. '']),
  25616. LinesToStr([ // $mod.$main
  25617. '$mod.b = $mod.v == $mod.v;',
  25618. '$mod.b = $mod.v != $mod.v;',
  25619. '$mod.b = $mod.v == 1;',
  25620. '$mod.b = $mod.v != 1;',
  25621. '$mod.b = 2 == $mod.v;',
  25622. '$mod.b = 2 != $mod.v;',
  25623. '$mod.b = $mod.v == $mod.i;',
  25624. '$mod.b = $mod.i == $mod.v;',
  25625. '$mod.b = $mod.v == null;',
  25626. '$mod.b = null == $mod.v;',
  25627. '$mod.b = $mod.v == false;',
  25628. '$mod.b = true == $mod.v;',
  25629. '$mod.b = $mod.v == $mod.b;',
  25630. '$mod.b = $mod.b == $mod.v;',
  25631. '$mod.b = $mod.v == $mod.s;',
  25632. '$mod.b = $mod.s == $mod.v;',
  25633. '$mod.b = $mod.v == "foo";',
  25634. '$mod.b = "" == $mod.v;',
  25635. '$mod.b = $mod.v == $mod.d;',
  25636. '$mod.b = $mod.d == $mod.v;',
  25637. '$mod.b = $mod.v == 3.4;',
  25638. '$mod.b = 5.6 == $mod.v;',
  25639. '$mod.b = $mod.v == $mod.c;',
  25640. '$mod.b = $mod.c == $mod.v;',
  25641. '$mod.b = $mod.m == $mod.m;',
  25642. '$mod.b = $mod.v == $mod.m;',
  25643. '$mod.b = $mod.m == $mod.v;',
  25644. '']));
  25645. end;
  25646. procedure TTestModule.TestJSValue_If;
  25647. begin
  25648. StartProgram(false);
  25649. Add([
  25650. 'procedure Fly(var u);',
  25651. 'begin',
  25652. ' if jsvalue(u) then ;',
  25653. 'end;',
  25654. 'var',
  25655. ' v: jsvalue;',
  25656. 'begin',
  25657. ' if v then ;',
  25658. ' while v do ;',
  25659. ' repeat until v;',
  25660. '']);
  25661. ConvertProgram;
  25662. CheckSource('TestJSValue_If',
  25663. LinesToStr([ // statements
  25664. 'this.Fly = function (u) {',
  25665. ' if (u.get()) ;',
  25666. '};',
  25667. 'this.v = undefined;',
  25668. '']),
  25669. LinesToStr([ // $mod.$main
  25670. 'if ($mod.v) ;',
  25671. 'while($mod.v){',
  25672. '};',
  25673. 'do{',
  25674. '} while(!$mod.v);',
  25675. '']));
  25676. end;
  25677. procedure TTestModule.TestJSValue_Not;
  25678. begin
  25679. StartProgram(false);
  25680. Add([
  25681. 'var',
  25682. ' v: jsvalue;',
  25683. ' b: boolean;',
  25684. 'begin',
  25685. ' b:=not v;',
  25686. ' if not v then ;',
  25687. ' while not v do ;',
  25688. ' repeat until not v;',
  25689. '']);
  25690. ConvertProgram;
  25691. CheckSource('TestJSValue_If',
  25692. LinesToStr([ // statements
  25693. 'this.v = undefined;',
  25694. 'this.b = false;',
  25695. '']),
  25696. LinesToStr([ // $mod.$main
  25697. '$mod.b=!$mod.v;',
  25698. 'if (!$mod.v) ;',
  25699. 'while(!$mod.v){',
  25700. '};',
  25701. 'do{',
  25702. '} while($mod.v);',
  25703. '']));
  25704. end;
  25705. procedure TTestModule.TestJSValue_Enum;
  25706. begin
  25707. StartProgram(false);
  25708. Add('type');
  25709. Add(' TColor = (red, blue);');
  25710. Add(' TRedBlue = TColor;');
  25711. Add('var');
  25712. Add(' v: jsvalue;');
  25713. Add(' e: TColor;');
  25714. Add('begin');
  25715. Add(' v:=e;');
  25716. Add(' v:=TColor(e);');
  25717. Add(' v:=TRedBlue(e);');
  25718. Add(' e:=TColor(v);');
  25719. Add(' e:=TRedBlue(v);');
  25720. ConvertProgram;
  25721. CheckSource('TestJSValue_Enum',
  25722. LinesToStr([ // statements
  25723. 'this.TColor = {',
  25724. ' "0": "red",',
  25725. ' red: 0,',
  25726. ' "1": "blue",',
  25727. ' blue: 1',
  25728. '};',
  25729. 'this.v = undefined;',
  25730. 'this.e = 0;',
  25731. '']),
  25732. LinesToStr([ // $mod.$main
  25733. '$mod.v = $mod.e;',
  25734. '$mod.v = $mod.e;',
  25735. '$mod.v = $mod.e;',
  25736. '$mod.e = $mod.v;',
  25737. '$mod.e = $mod.v;',
  25738. '']));
  25739. end;
  25740. procedure TTestModule.TestJSValue_ClassInstance;
  25741. begin
  25742. StartProgram(false);
  25743. Add([
  25744. 'type',
  25745. ' TObject = class',
  25746. ' end;',
  25747. ' TBirdObject = TObject;',
  25748. 'var',
  25749. ' v: jsvalue;',
  25750. ' o: TObject;',
  25751. 'begin',
  25752. ' v:=o;',
  25753. ' v:=TObject(o);',
  25754. ' v:=TBirdObject(o);',
  25755. ' o:=TObject(v);',
  25756. ' o:=TBirdObject(v);',
  25757. ' if v is TObject then ;',
  25758. '']);
  25759. ConvertProgram;
  25760. CheckSource('TestJSValue_ClassInstance',
  25761. LinesToStr([ // statements
  25762. 'rtl.createClass($mod, "TObject", null, function () {',
  25763. ' this.$init = function () {',
  25764. ' };',
  25765. ' this.$final = function () {',
  25766. ' };',
  25767. '});',
  25768. 'this.v = undefined;',
  25769. 'this.o = null;',
  25770. '']),
  25771. LinesToStr([ // $mod.$main
  25772. '$mod.v = $mod.o;',
  25773. '$mod.v = $mod.o;',
  25774. '$mod.v = $mod.o;',
  25775. '$mod.o = rtl.getObject($mod.v);',
  25776. '$mod.o = rtl.getObject($mod.v);',
  25777. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  25778. '']));
  25779. end;
  25780. procedure TTestModule.TestJSValue_ClassOf;
  25781. begin
  25782. StartProgram(false);
  25783. Add([
  25784. 'type',
  25785. ' TClass = class of TObject;',
  25786. ' TObject = class',
  25787. ' end;',
  25788. ' TBirds = class of TBird;',
  25789. ' TBird = class(TObject) end;',
  25790. 'var',
  25791. ' v: jsvalue;',
  25792. ' c: TClass;',
  25793. 'begin',
  25794. ' v:=c;',
  25795. ' v:=TObject;',
  25796. ' v:=TClass(c);',
  25797. ' v:=TBirds(c);',
  25798. ' c:=TClass(v);',
  25799. ' c:=TBirds(v);',
  25800. ' if v is TClass then ;',
  25801. '']);
  25802. ConvertProgram;
  25803. CheckSource('TestJSValue_ClassOf',
  25804. LinesToStr([ // statements
  25805. 'rtl.createClass($mod, "TObject", null, function () {',
  25806. ' this.$init = function () {',
  25807. ' };',
  25808. ' this.$final = function () {',
  25809. ' };',
  25810. '});',
  25811. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  25812. '});',
  25813. 'this.v = undefined;',
  25814. 'this.c = null;',
  25815. '']),
  25816. LinesToStr([ // $mod.$main
  25817. '$mod.v = $mod.c;',
  25818. '$mod.v = $mod.TObject;',
  25819. '$mod.v = $mod.c;',
  25820. '$mod.v = $mod.c;',
  25821. '$mod.c = rtl.getObject($mod.v);',
  25822. '$mod.c = rtl.getObject($mod.v);',
  25823. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  25824. '']));
  25825. end;
  25826. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  25827. begin
  25828. StartProgram(false);
  25829. Add([
  25830. 'type',
  25831. ' integer = longint;',
  25832. ' TArray = array of JSValue;',
  25833. ' TArrgh = tarray;',
  25834. ' TArrInt = array of integer;',
  25835. 'var',
  25836. ' v: jsvalue;',
  25837. ' TheArray: tarray = (1,''2'');',
  25838. ' Arr: tarrgh;',
  25839. ' i: integer;',
  25840. ' ArrInt: tarrint;',
  25841. 'begin',
  25842. ' arr:=thearray;',
  25843. ' thearray:=arr;',
  25844. ' setlength(arr,2);',
  25845. ' setlength(thearray,3);',
  25846. ' arr[4]:=v;',
  25847. ' arr[5]:=length(thearray);',
  25848. ' arr[6]:=nil;',
  25849. ' arr[7]:=thearray[8];',
  25850. ' arr[low(arr)]:=high(thearray);',
  25851. ' arr:=arrint;',
  25852. ' arrInt:=tarrint(arr);',
  25853. ' if TheArray = nil then ;',
  25854. ' if nil = TheArray then ;',
  25855. ' if TheArray <> nil then ;',
  25856. ' if nil <> TheArray then ;',
  25857. '']);
  25858. ConvertProgram;
  25859. CheckSource('TestJSValue_ArrayOfJSValue',
  25860. LinesToStr([ // statements
  25861. 'this.v = undefined;',
  25862. 'this.TheArray = [1, "2"];',
  25863. 'this.Arr = [];',
  25864. 'this.i = 0;',
  25865. 'this.ArrInt = [];',
  25866. '']),
  25867. LinesToStr([ // $mod.$main
  25868. '$mod.Arr = $mod.TheArray;',
  25869. '$mod.TheArray = $mod.Arr;',
  25870. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  25871. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  25872. '$mod.Arr[4] = $mod.v;',
  25873. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  25874. '$mod.Arr[6] = null;',
  25875. '$mod.Arr[7] = $mod.TheArray[8];',
  25876. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  25877. '$mod.Arr = $mod.ArrInt;',
  25878. '$mod.ArrInt = $mod.Arr;',
  25879. 'if (rtl.length($mod.TheArray) === 0) ;',
  25880. 'if (rtl.length($mod.TheArray) === 0) ;',
  25881. 'if (rtl.length($mod.TheArray) > 0) ;',
  25882. 'if (rtl.length($mod.TheArray) > 0) ;',
  25883. '']));
  25884. end;
  25885. procedure TTestModule.TestJSValue_ArrayLit;
  25886. begin
  25887. StartProgram(false);
  25888. Add([
  25889. 'type',
  25890. ' TFlag = (big,small);',
  25891. ' TArray = array of JSValue;',
  25892. ' TObject = class end;',
  25893. ' TClass = class of TObject;',
  25894. 'var',
  25895. ' v: jsvalue;',
  25896. ' a: TArray;',
  25897. ' o: TObject;',
  25898. 'begin',
  25899. ' a:=[];',
  25900. ' a:=[1];',
  25901. ' a:=[1,2];',
  25902. ' a:=[big];',
  25903. ' a:=[1,big];',
  25904. ' a:=[o,nil];',
  25905. '']);
  25906. ConvertProgram;
  25907. CheckSource('TestJSValue_ArrayLit',
  25908. LinesToStr([ // statements
  25909. 'this.TFlag = {',
  25910. ' "0": "big",',
  25911. ' big: 0,',
  25912. ' "1": "small",',
  25913. ' small: 1',
  25914. '};',
  25915. 'rtl.createClass($mod, "TObject", null, function () {',
  25916. ' this.$init = function () {',
  25917. ' };',
  25918. ' this.$final = function () {',
  25919. ' };',
  25920. '});',
  25921. 'this.v = undefined;',
  25922. 'this.a = [];',
  25923. 'this.o = null;',
  25924. '']),
  25925. LinesToStr([ // $mod.$main
  25926. '$mod.a = [];',
  25927. '$mod.a = [1];',
  25928. '$mod.a = [1, 2];',
  25929. '$mod.a = [$mod.TFlag.big];',
  25930. '$mod.a = [1, $mod.TFlag.big];',
  25931. '$mod.a = [$mod.o, null];',
  25932. '']));
  25933. end;
  25934. procedure TTestModule.TestJSValue_Params;
  25935. begin
  25936. StartProgram(false);
  25937. Add('type');
  25938. Add(' integer = longint;');
  25939. Add(' TYesNo = boolean;');
  25940. Add(' TFloat = double;');
  25941. Add(' TCaption = string;');
  25942. Add(' TChar = char;');
  25943. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  25944. Add('var');
  25945. Add(' l: jsvalue;');
  25946. Add('begin');
  25947. Add(' a:=a;');
  25948. Add(' l:=b;');
  25949. Add(' c:=c;');
  25950. Add(' d:=d;');
  25951. Add(' Result:=l;');
  25952. Add('end;');
  25953. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  25954. Add('var');
  25955. Add(' v: jsvalue;');
  25956. Add(' i: integer;');
  25957. Add(' b: TYesNo;');
  25958. Add(' d: TFloat;');
  25959. Add(' s: TCaption;');
  25960. Add(' c: TChar;');
  25961. Add('begin');
  25962. Add(' v:=doit(v,v,v,v);');
  25963. Add(' i:=integer(dosome(i,i));');
  25964. Add(' b:=TYesNo(dosome(b,b));');
  25965. Add(' d:=TFloat(dosome(d,d));');
  25966. Add(' s:=TCaption(dosome(s,s));');
  25967. Add(' c:=TChar(dosome(c,c));');
  25968. ConvertProgram;
  25969. CheckSource('TestJSValue_Params',
  25970. LinesToStr([ // statements
  25971. 'this.DoIt = function (a, b, c, d) {',
  25972. ' var Result = undefined;',
  25973. ' var l = undefined;',
  25974. ' a = a;',
  25975. ' l = b;',
  25976. ' c.set(c.get());',
  25977. ' d.set(d.get());',
  25978. ' Result = l;',
  25979. ' return Result;',
  25980. '};',
  25981. 'this.DoSome = function (a, b) {',
  25982. ' var Result = undefined;',
  25983. ' return Result;',
  25984. '};',
  25985. 'this.v = undefined;',
  25986. 'this.i = 0;',
  25987. 'this.b = false;',
  25988. 'this.d = 0.0;',
  25989. 'this.s = "";',
  25990. 'this.c = "";',
  25991. '']),
  25992. LinesToStr([ // $mod.$main
  25993. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  25994. ' p: $mod,',
  25995. ' get: function () {',
  25996. ' return this.p.v;',
  25997. ' },',
  25998. ' set: function (v) {',
  25999. ' this.p.v = v;',
  26000. ' }',
  26001. '}, {',
  26002. ' p: $mod,',
  26003. ' get: function () {',
  26004. ' return this.p.v;',
  26005. ' },',
  26006. ' set: function (v) {',
  26007. ' this.p.v = v;',
  26008. ' }',
  26009. '});',
  26010. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  26011. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  26012. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  26013. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  26014. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  26015. '']));
  26016. end;
  26017. procedure TTestModule.TestJSValue_UntypedParam;
  26018. begin
  26019. StartProgram(false);
  26020. Add('function DoIt(const a; var b; out c): jsvalue;');
  26021. Add('begin');
  26022. Add(' Result:=a;');
  26023. Add(' Result:=b;');
  26024. Add(' Result:=c;');
  26025. Add(' b:=Result;');
  26026. Add(' c:=Result;');
  26027. Add('end;');
  26028. Add('var i: longint;');
  26029. Add('begin');
  26030. Add(' doit(i,i,i);');
  26031. ConvertProgram;
  26032. CheckSource('TestJSValue_UntypedParam',
  26033. LinesToStr([ // statements
  26034. 'this.DoIt = function (a, b, c) {',
  26035. ' var Result = undefined;',
  26036. ' Result = a;',
  26037. ' Result = b.get();',
  26038. ' Result = c.get();',
  26039. ' b.set(Result);',
  26040. ' c.set(Result);',
  26041. ' return Result;',
  26042. '};',
  26043. 'this.i = 0;',
  26044. '']),
  26045. LinesToStr([ // $mod.$main
  26046. '$mod.DoIt($mod.i, {',
  26047. ' p: $mod,',
  26048. ' get: function () {',
  26049. ' return this.p.i;',
  26050. ' },',
  26051. ' set: function (v) {',
  26052. ' this.p.i = v;',
  26053. ' }',
  26054. '}, {',
  26055. ' p: $mod,',
  26056. ' get: function () {',
  26057. ' return this.p.i;',
  26058. ' },',
  26059. ' set: function (v) {',
  26060. ' this.p.i = v;',
  26061. ' }',
  26062. '});',
  26063. '']));
  26064. end;
  26065. procedure TTestModule.TestJSValue_FuncResultType;
  26066. begin
  26067. StartProgram(false);
  26068. Add('type');
  26069. Add(' integer = longint;');
  26070. Add(' TJSValueArray = array of JSValue;');
  26071. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  26072. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  26073. Add('begin');
  26074. Add(' while Compare(P,aList[0])>0 do ;');
  26075. Add('end;');
  26076. Add('var');
  26077. Add(' Compare: TListSortCompare;');
  26078. Add(' V: JSValue;');
  26079. Add(' i: integer;');
  26080. Add('begin');
  26081. Add(' if Compare(V,V)>0 then ;');
  26082. Add(' if Compare(i,i)>1 then ;');
  26083. Add(' if Compare(nil,false)>2 then ;');
  26084. Add(' if Compare(1,true)>3 then ;');
  26085. ConvertProgram;
  26086. CheckSource('TestJSValue_UntypedParam',
  26087. LinesToStr([ // statements
  26088. 'this.Sort = function (P, aList, Compare) {',
  26089. ' while (Compare(P, aList[0]) > 0) {',
  26090. ' };',
  26091. '};',
  26092. 'this.Compare = null;',
  26093. 'this.V = undefined;',
  26094. 'this.i = 0;',
  26095. '']),
  26096. LinesToStr([ // $mod.$main
  26097. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  26098. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  26099. 'if ($mod.Compare(null, false) > 2) ;',
  26100. 'if ($mod.Compare(1, true) > 3) ;',
  26101. '']));
  26102. end;
  26103. procedure TTestModule.TestJSValue_ProcType_Assign;
  26104. begin
  26105. StartProgram(false);
  26106. Add('type');
  26107. Add(' integer = longint;');
  26108. Add(' TObject = class');
  26109. Add(' class function GetGlob: integer;');
  26110. Add(' function Getter: integer;');
  26111. Add(' end;');
  26112. Add('class function TObject.GetGlob: integer;');
  26113. Add('var v1: jsvalue;');
  26114. Add('begin');
  26115. Add(' v1:=@GetGlob;');
  26116. Add(' v1:[email protected];');
  26117. Add('end;');
  26118. Add('function TObject.Getter: integer;');
  26119. Add('var v2: jsvalue;');
  26120. Add('begin');
  26121. Add(' v2:=@Getter;');
  26122. Add(' v2:[email protected];');
  26123. Add(' v2:=@GetGlob;');
  26124. Add(' v2:[email protected];');
  26125. Add('end;');
  26126. Add('function GetIt(i: integer): integer;');
  26127. Add('var v3: jsvalue;');
  26128. Add('begin');
  26129. Add(' v3:=@GetIt;');
  26130. Add('end;');
  26131. Add('var');
  26132. Add(' V: JSValue;');
  26133. Add(' o: TObject;');
  26134. Add('begin');
  26135. Add(' v:=@GetIt;');
  26136. Add(' v:[email protected];');
  26137. Add(' v:[email protected];');
  26138. ConvertProgram;
  26139. CheckSource('TestJSValue_ProcType_Assign',
  26140. LinesToStr([ // statements
  26141. 'rtl.createClass($mod, "TObject", null, function () {',
  26142. ' this.$init = function () {',
  26143. ' };',
  26144. ' this.$final = function () {',
  26145. ' };',
  26146. ' this.GetGlob = function () {',
  26147. ' var Result = 0;',
  26148. ' var v1 = undefined;',
  26149. ' v1 = rtl.createCallback(this, "GetGlob");',
  26150. ' v1 = rtl.createCallback(this, "GetGlob");',
  26151. ' return Result;',
  26152. ' };',
  26153. ' this.Getter = function () {',
  26154. ' var Result = 0;',
  26155. ' var v2 = undefined;',
  26156. ' v2 = rtl.createCallback(this, "Getter");',
  26157. ' v2 = rtl.createCallback(this, "Getter");',
  26158. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  26159. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  26160. ' return Result;',
  26161. ' };',
  26162. '});',
  26163. 'this.GetIt = function (i) {',
  26164. ' var Result = 0;',
  26165. ' var v3 = undefined;',
  26166. ' v3 = $mod.GetIt;',
  26167. ' return Result;',
  26168. '};',
  26169. 'this.V = undefined;',
  26170. 'this.o = null;',
  26171. '']),
  26172. LinesToStr([ // $mod.$main
  26173. '$mod.V = $mod.GetIt;',
  26174. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  26175. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  26176. '']));
  26177. end;
  26178. procedure TTestModule.TestJSValue_ProcType_Equal;
  26179. begin
  26180. StartProgram(false);
  26181. Add('type');
  26182. Add(' integer = longint;');
  26183. Add(' TObject = class');
  26184. Add(' class function GetGlob: integer;');
  26185. Add(' function Getter: integer;');
  26186. Add(' end;');
  26187. Add('class function TObject.GetGlob: integer;');
  26188. Add('var v1: jsvalue;');
  26189. Add('begin');
  26190. Add(' if v1=@GetGlob then;');
  26191. Add(' if [email protected] then ;');
  26192. Add('end;');
  26193. Add('function TObject.Getter: integer;');
  26194. Add('var v2: jsvalue;');
  26195. Add('begin');
  26196. Add(' if v2=@Getter then;');
  26197. Add(' if [email protected] then ;');
  26198. Add(' if v2=@GetGlob then;');
  26199. Add(' if [email protected] then;');
  26200. Add('end;');
  26201. Add('function GetIt(i: integer): integer;');
  26202. Add('var v3: jsvalue;');
  26203. Add('begin');
  26204. Add(' if v3=@GetIt then;');
  26205. Add('end;');
  26206. Add('var');
  26207. Add(' V: JSValue;');
  26208. Add(' o: TObject;');
  26209. Add('begin');
  26210. Add(' if v=@GetIt then;');
  26211. Add(' if [email protected] then;');
  26212. Add(' if [email protected] then;');
  26213. Add(' if @GetIt=v then;');
  26214. Add(' if @o.Getter=v then;');
  26215. Add(' if @o.GetGlob=v then;');
  26216. ConvertProgram;
  26217. CheckSource('TestJSValue_ProcType_Equal',
  26218. LinesToStr([ // statements
  26219. 'rtl.createClass($mod, "TObject", null, function () {',
  26220. ' this.$init = function () {',
  26221. ' };',
  26222. ' this.$final = function () {',
  26223. ' };',
  26224. ' this.GetGlob = function () {',
  26225. ' var Result = 0;',
  26226. ' var v1 = undefined;',
  26227. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  26228. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  26229. ' return Result;',
  26230. ' };',
  26231. ' this.Getter = function () {',
  26232. ' var Result = 0;',
  26233. ' var v2 = undefined;',
  26234. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  26235. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  26236. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  26237. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  26238. ' return Result;',
  26239. ' };',
  26240. '});',
  26241. 'this.GetIt = function (i) {',
  26242. ' var Result = 0;',
  26243. ' var v3 = undefined;',
  26244. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  26245. ' return Result;',
  26246. '};',
  26247. 'this.V = undefined;',
  26248. 'this.o = null;',
  26249. '']),
  26250. LinesToStr([ // $mod.$main
  26251. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  26252. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  26253. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  26254. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  26255. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  26256. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  26257. '']));
  26258. end;
  26259. procedure TTestModule.TestJSValue_ProcType_Param;
  26260. begin
  26261. StartProgram(false);
  26262. Add([
  26263. 'type',
  26264. ' variant = jsvalue;',
  26265. ' TArrVariant = array of variant;',
  26266. ' TArrVar2 = TArrVariant;',
  26267. ' TFuncInt = function: longint;',
  26268. 'function GetIt: longint;',
  26269. 'begin',
  26270. 'end;',
  26271. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  26272. 'var v: variant;',
  26273. 'begin',
  26274. ' v:=arr[1];',
  26275. 'end;',
  26276. 'var s: string;',
  26277. 'begin',
  26278. ' DoIt(GetIt,[]);',
  26279. ' DoIt(@GetIt,[]);',
  26280. ' DoIt(1,[s,GetIt]);',
  26281. ' DoIt(1,[s,@GetIt]);',
  26282. '']);
  26283. ConvertProgram;
  26284. CheckSource('TestJSValue_ProcType_Param',
  26285. LinesToStr([ // statements
  26286. 'this.GetIt = function () {',
  26287. ' var Result = 0;',
  26288. ' return Result;',
  26289. '};',
  26290. 'this.DoIt = function (p, Arr) {',
  26291. ' var v = undefined;',
  26292. ' v = Arr[1];',
  26293. '};',
  26294. 'this.s = "";',
  26295. '']),
  26296. LinesToStr([ // $mod.$main
  26297. '$mod.DoIt($mod.GetIt(), []);',
  26298. '$mod.DoIt($mod.GetIt, []);',
  26299. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  26300. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  26301. '']));
  26302. end;
  26303. procedure TTestModule.TestJSValue_AssignToPointerFail;
  26304. begin
  26305. StartProgram(false);
  26306. Add([
  26307. 'var',
  26308. ' v: JSValue;',
  26309. ' p: Pointer;',
  26310. 'begin',
  26311. ' p:=v;',
  26312. '']);
  26313. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  26314. nIncompatibleTypesGotExpected);
  26315. ConvertProgram;
  26316. end;
  26317. procedure TTestModule.TestJSValue_OverloadDouble;
  26318. begin
  26319. StartProgram(false);
  26320. Add([
  26321. 'type',
  26322. ' integer = longint;',
  26323. ' tdatetime = double;',
  26324. 'procedure DoIt(d: double); begin end;',
  26325. 'procedure DoIt(v: jsvalue); begin end;',
  26326. 'var',
  26327. ' d: double;',
  26328. ' dt: tdatetime;',
  26329. ' i: integer;',
  26330. ' b: byte;',
  26331. ' shi: shortint;',
  26332. ' w: word;',
  26333. ' smi: smallint;',
  26334. ' lw: longword;',
  26335. ' li: longint;',
  26336. ' ni: nativeint;',
  26337. ' nu: nativeuint;',
  26338. 'begin',
  26339. ' DoIt(d);',
  26340. ' DoIt(dt);',
  26341. ' DoIt(i);',
  26342. ' DoIt(b);',
  26343. ' DoIt(shi);',
  26344. ' DoIt(w);',
  26345. ' DoIt(smi);',
  26346. ' DoIt(lw);',
  26347. ' DoIt(li);',
  26348. ' DoIt(ni);',
  26349. ' DoIt(nu);',
  26350. '']);
  26351. ConvertProgram;
  26352. CheckSource('TestJSValue_OverloadDouble',
  26353. LinesToStr([ // statements
  26354. 'this.DoIt = function (d) {',
  26355. '};',
  26356. 'this.DoIt$1 = function (v) {',
  26357. '};',
  26358. 'this.d = 0.0;',
  26359. 'this.dt = 0.0;',
  26360. 'this.i = 0;',
  26361. 'this.b = 0;',
  26362. 'this.shi = 0;',
  26363. 'this.w = 0;',
  26364. 'this.smi = 0;',
  26365. 'this.lw = 0;',
  26366. 'this.li = 0;',
  26367. 'this.ni = 0;',
  26368. 'this.nu = 0;',
  26369. '']),
  26370. LinesToStr([ // $mod.$main
  26371. '$mod.DoIt($mod.d);',
  26372. '$mod.DoIt($mod.dt);',
  26373. '$mod.DoIt$1($mod.i);',
  26374. '$mod.DoIt$1($mod.b);',
  26375. '$mod.DoIt$1($mod.shi);',
  26376. '$mod.DoIt$1($mod.w);',
  26377. '$mod.DoIt$1($mod.smi);',
  26378. '$mod.DoIt$1($mod.lw);',
  26379. '$mod.DoIt$1($mod.li);',
  26380. '$mod.DoIt$1($mod.ni);',
  26381. '$mod.DoIt$1($mod.nu);',
  26382. '']));
  26383. end;
  26384. procedure TTestModule.TestJSValue_OverloadNativeInt;
  26385. begin
  26386. StartProgram(false);
  26387. Add([
  26388. 'type',
  26389. ' integer = longint;',
  26390. ' int53 = nativeint;',
  26391. ' tdatetime = double;',
  26392. 'procedure DoIt(n: nativeint); begin end;',
  26393. 'procedure DoIt(v: jsvalue); begin end;',
  26394. 'var',
  26395. ' d: double;',
  26396. ' dt: tdatetime;',
  26397. ' i: integer;',
  26398. ' b: byte;',
  26399. ' shi: shortint;',
  26400. ' w: word;',
  26401. ' smi: smallint;',
  26402. ' lw: longword;',
  26403. ' li: longint;',
  26404. ' ni: nativeint;',
  26405. ' nu: nativeuint;',
  26406. 'begin',
  26407. ' DoIt(d);',
  26408. ' DoIt(dt);',
  26409. ' DoIt(i);',
  26410. ' DoIt(b);',
  26411. ' DoIt(shi);',
  26412. ' DoIt(w);',
  26413. ' DoIt(smi);',
  26414. ' DoIt(lw);',
  26415. ' DoIt(li);',
  26416. ' DoIt(ni);',
  26417. ' DoIt(nu);',
  26418. '']);
  26419. ConvertProgram;
  26420. CheckSource('TestJSValue_OverloadNativeInt',
  26421. LinesToStr([ // statements
  26422. 'this.DoIt = function (n) {',
  26423. '};',
  26424. 'this.DoIt$1 = function (v) {',
  26425. '};',
  26426. 'this.d = 0.0;',
  26427. 'this.dt = 0.0;',
  26428. 'this.i = 0;',
  26429. 'this.b = 0;',
  26430. 'this.shi = 0;',
  26431. 'this.w = 0;',
  26432. 'this.smi = 0;',
  26433. 'this.lw = 0;',
  26434. 'this.li = 0;',
  26435. 'this.ni = 0;',
  26436. 'this.nu = 0;',
  26437. '']),
  26438. LinesToStr([ // $mod.$main
  26439. '$mod.DoIt$1($mod.d);',
  26440. '$mod.DoIt$1($mod.dt);',
  26441. '$mod.DoIt($mod.i);',
  26442. '$mod.DoIt($mod.b);',
  26443. '$mod.DoIt($mod.shi);',
  26444. '$mod.DoIt($mod.w);',
  26445. '$mod.DoIt($mod.smi);',
  26446. '$mod.DoIt($mod.lw);',
  26447. '$mod.DoIt($mod.li);',
  26448. '$mod.DoIt($mod.ni);',
  26449. '$mod.DoIt($mod.nu);',
  26450. '']));
  26451. end;
  26452. procedure TTestModule.TestJSValue_OverloadWord;
  26453. begin
  26454. StartProgram(false);
  26455. Add([
  26456. 'type',
  26457. ' integer = longint;',
  26458. ' int53 = nativeint;',
  26459. ' tdatetime = double;',
  26460. 'procedure DoIt(w: word); begin end;',
  26461. 'procedure DoIt(v: jsvalue); begin end;',
  26462. 'var',
  26463. ' d: double;',
  26464. ' dt: tdatetime;',
  26465. ' i: integer;',
  26466. ' b: byte;',
  26467. ' shi: shortint;',
  26468. ' w: word;',
  26469. ' smi: smallint;',
  26470. ' lw: longword;',
  26471. ' li: longint;',
  26472. ' ni: nativeint;',
  26473. ' nu: nativeuint;',
  26474. 'begin',
  26475. ' DoIt(d);',
  26476. ' DoIt(dt);',
  26477. ' DoIt(i);',
  26478. ' DoIt(b);',
  26479. ' DoIt(shi);',
  26480. ' DoIt(w);',
  26481. ' DoIt(smi);',
  26482. ' DoIt(lw);',
  26483. ' DoIt(li);',
  26484. ' DoIt(ni);',
  26485. ' DoIt(nu);',
  26486. '']);
  26487. ConvertProgram;
  26488. CheckSource('TestJSValue_OverloadWord',
  26489. LinesToStr([ // statements
  26490. 'this.DoIt = function (w) {',
  26491. '};',
  26492. 'this.DoIt$1 = function (v) {',
  26493. '};',
  26494. 'this.d = 0.0;',
  26495. 'this.dt = 0.0;',
  26496. 'this.i = 0;',
  26497. 'this.b = 0;',
  26498. 'this.shi = 0;',
  26499. 'this.w = 0;',
  26500. 'this.smi = 0;',
  26501. 'this.lw = 0;',
  26502. 'this.li = 0;',
  26503. 'this.ni = 0;',
  26504. 'this.nu = 0;',
  26505. '']),
  26506. LinesToStr([ // $mod.$main
  26507. '$mod.DoIt$1($mod.d);',
  26508. '$mod.DoIt$1($mod.dt);',
  26509. '$mod.DoIt$1($mod.i);',
  26510. '$mod.DoIt($mod.b);',
  26511. '$mod.DoIt($mod.shi);',
  26512. '$mod.DoIt($mod.w);',
  26513. '$mod.DoIt$1($mod.smi);',
  26514. '$mod.DoIt$1($mod.lw);',
  26515. '$mod.DoIt$1($mod.li);',
  26516. '$mod.DoIt$1($mod.ni);',
  26517. '$mod.DoIt$1($mod.nu);',
  26518. '']));
  26519. end;
  26520. procedure TTestModule.TestJSValue_OverloadString;
  26521. begin
  26522. StartProgram(false);
  26523. Add([
  26524. 'type',
  26525. ' uni = string;',
  26526. ' WChar = char;',
  26527. 'procedure DoIt(s: string); begin end;',
  26528. 'procedure DoIt(v: jsvalue); begin end;',
  26529. 'var',
  26530. ' s: string;',
  26531. ' c: char;',
  26532. ' u: uni;',
  26533. 'begin',
  26534. ' DoIt(s);',
  26535. ' DoIt(c);',
  26536. ' DoIt(u);',
  26537. '']);
  26538. ConvertProgram;
  26539. CheckSource('TestJSValue_OverloadString',
  26540. LinesToStr([ // statements
  26541. 'this.DoIt = function (s) {',
  26542. '};',
  26543. 'this.DoIt$1 = function (v) {',
  26544. '};',
  26545. 'this.s = "";',
  26546. 'this.c = "";',
  26547. 'this.u = "";',
  26548. '']),
  26549. LinesToStr([ // $mod.$main
  26550. '$mod.DoIt($mod.s);',
  26551. '$mod.DoIt($mod.c);',
  26552. '$mod.DoIt($mod.u);',
  26553. '']));
  26554. end;
  26555. procedure TTestModule.TestJSValue_OverloadChar;
  26556. begin
  26557. StartProgram(false);
  26558. Add([
  26559. 'type',
  26560. ' uni = string;',
  26561. ' WChar = char;',
  26562. 'procedure DoIt(c: char); begin end;',
  26563. 'procedure DoIt(v: jsvalue); begin end;',
  26564. 'var',
  26565. ' s: string;',
  26566. ' c: char;',
  26567. ' u: uni;',
  26568. 'begin',
  26569. ' DoIt(s);',
  26570. ' DoIt(c);',
  26571. ' DoIt(u);',
  26572. '']);
  26573. ConvertProgram;
  26574. CheckSource('TestJSValue_OverloadChar',
  26575. LinesToStr([ // statements
  26576. 'this.DoIt = function (c) {',
  26577. '};',
  26578. 'this.DoIt$1 = function (v) {',
  26579. '};',
  26580. 'this.s = "";',
  26581. 'this.c = "";',
  26582. 'this.u = "";',
  26583. '']),
  26584. LinesToStr([ // $mod.$main
  26585. '$mod.DoIt$1($mod.s);',
  26586. '$mod.DoIt($mod.c);',
  26587. '$mod.DoIt$1($mod.u);',
  26588. '']));
  26589. end;
  26590. procedure TTestModule.TestJSValue_OverloadPointer;
  26591. begin
  26592. StartProgram(false);
  26593. Add([
  26594. 'type',
  26595. ' TObject = class end;',
  26596. 'procedure DoIt(p: pointer); begin end;',
  26597. 'procedure DoIt(v: jsvalue); begin end;',
  26598. 'var',
  26599. ' o: TObject;',
  26600. 'begin',
  26601. ' DoIt(o);',
  26602. '']);
  26603. ConvertProgram;
  26604. CheckSource('TestJSValue_OverloadPointer',
  26605. LinesToStr([ // statements
  26606. 'rtl.createClass($mod, "TObject", null, function () {',
  26607. ' this.$init = function () {',
  26608. ' };',
  26609. ' this.$final = function () {',
  26610. ' };',
  26611. '});',
  26612. 'this.DoIt = function (p) {',
  26613. '};',
  26614. 'this.DoIt$1 = function (v) {',
  26615. '};',
  26616. 'this.o = null;',
  26617. '']),
  26618. LinesToStr([ // $mod.$main
  26619. '$mod.DoIt($mod.o);',
  26620. '']));
  26621. end;
  26622. procedure TTestModule.TestJSValue_ForIn;
  26623. begin
  26624. StartProgram(false);
  26625. Add([
  26626. 'var',
  26627. ' v: JSValue;',
  26628. ' key: string;',
  26629. 'begin',
  26630. ' for key in v do begin',
  26631. ' if key=''abc'' then ;',
  26632. ' end;',
  26633. '']);
  26634. ConvertProgram;
  26635. CheckSource('TestJSValue_ForIn',
  26636. LinesToStr([ // statements
  26637. 'this.v = undefined;',
  26638. 'this.key = "";',
  26639. '']),
  26640. LinesToStr([ // $mod.$main
  26641. 'for ($mod.key in $mod.v) {',
  26642. ' if ($mod.key === "abc") ;',
  26643. '};',
  26644. '']));
  26645. end;
  26646. procedure TTestModule.TestRTTI_IntRange;
  26647. begin
  26648. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26649. StartProgram(true,[supTypeInfo]);
  26650. Add([
  26651. '{$modeswitch externalclass}',
  26652. 'type',
  26653. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  26654. ' TColor = type TGraphicsColor;',
  26655. 'var',
  26656. ' p: TTypeInfo;',
  26657. 'begin',
  26658. ' p:=typeinfo(TGraphicsColor);',
  26659. ' p:=typeinfo(TColor);',
  26660. '']);
  26661. ConvertProgram;
  26662. CheckSource('TestRTTI_IntRange',
  26663. LinesToStr([ // statements
  26664. '$mod.$rtti.$Int("TGraphicsColor", {',
  26665. ' minvalue: -2147483648,',
  26666. ' maxvalue: 2147483647,',
  26667. ' ordtype: 4',
  26668. '});',
  26669. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  26670. 'this.p = null;',
  26671. '']),
  26672. LinesToStr([ // $mod.$main
  26673. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  26674. '$mod.p = $mod.$rtti["TColor"];',
  26675. '']));
  26676. end;
  26677. procedure TTestModule.TestRTTI_Double;
  26678. begin
  26679. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26680. StartProgram(true,[supTypeInfo]);
  26681. Add([
  26682. '{$modeswitch externalclass}',
  26683. 'type',
  26684. ' TFloat = type double;',
  26685. 'var',
  26686. ' p: TTypeInfo;',
  26687. 'begin',
  26688. ' p:=typeinfo(double);',
  26689. ' p:=typeinfo(TFloat);',
  26690. '']);
  26691. ConvertProgram;
  26692. CheckSource('TestRTTI_Double',
  26693. LinesToStr([ // statements
  26694. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  26695. 'this.p = null;',
  26696. '']),
  26697. LinesToStr([ // $mod.$main
  26698. '$mod.p = rtl.double;',
  26699. '$mod.p = $mod.$rtti["TFloat"];',
  26700. '']));
  26701. end;
  26702. procedure TTestModule.TestRTTI_ProcType;
  26703. begin
  26704. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26705. StartProgram(false);
  26706. Add('type');
  26707. Add(' TProcA = procedure;');
  26708. Add(' TMethodB = procedure of object;');
  26709. Add(' TProcC = procedure; varargs;');
  26710. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  26711. Add(' TProcE = function: nativeint;');
  26712. Add(' TProcF = function(const p: TProcA): nativeuint;');
  26713. Add('var p: pointer;');
  26714. Add('begin');
  26715. Add(' p:=typeinfo(tproca);');
  26716. ConvertProgram;
  26717. CheckSource('TestRTTI_ProcType',
  26718. LinesToStr([ // statements
  26719. '$mod.$rtti.$ProcVar("TProcA", {',
  26720. ' procsig: rtl.newTIProcSig(null)',
  26721. '});',
  26722. '$mod.$rtti.$MethodVar("TMethodB", {',
  26723. ' procsig: rtl.newTIProcSig(null),',
  26724. ' methodkind: 0',
  26725. '});',
  26726. '$mod.$rtti.$ProcVar("TProcC", {',
  26727. ' procsig: rtl.newTIProcSig(null, 2)',
  26728. '});',
  26729. '$mod.$rtti.$ProcVar("TProcD", {',
  26730. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  26731. '});',
  26732. '$mod.$rtti.$ProcVar("TProcE", {',
  26733. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  26734. '});',
  26735. '$mod.$rtti.$ProcVar("TProcF", {',
  26736. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  26737. '});',
  26738. 'this.p = null;',
  26739. '']),
  26740. LinesToStr([ // $mod.$main
  26741. '$mod.p = $mod.$rtti["TProcA"];',
  26742. '']));
  26743. end;
  26744. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  26745. begin
  26746. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26747. AddModuleWithIntfImplSrc('unit2.pas',
  26748. LinesToStr([
  26749. 'type',
  26750. ' TObject = class end;'
  26751. ]),
  26752. '');
  26753. StartUnit(true);
  26754. Add('interface');
  26755. Add('uses unit2;');
  26756. Add('type');
  26757. Add(' TProcA = function(o: tobject): tobject;');
  26758. Add('implementation');
  26759. Add('type');
  26760. Add(' TProcB = function(o: tobject): tobject;');
  26761. Add('var p: Pointer;');
  26762. Add('initialization');
  26763. Add(' p:=typeinfo(tproca);');
  26764. Add(' p:=typeinfo(tprocb);');
  26765. ConvertUnit;
  26766. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  26767. LinesToStr([ // statements
  26768. 'var $impl = $mod.$impl;',
  26769. '$mod.$rtti.$ProcVar("TProcA", {',
  26770. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  26771. '});',
  26772. '']),
  26773. LinesToStr([ // this.$init
  26774. '$impl.p = $mod.$rtti["TProcA"];',
  26775. '$impl.p = $mod.$rtti["TProcB"];',
  26776. '']),
  26777. LinesToStr([ // implementation
  26778. '$mod.$rtti.$ProcVar("TProcB", {',
  26779. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  26780. '});',
  26781. '$impl.p = null;',
  26782. '']) );
  26783. end;
  26784. procedure TTestModule.TestRTTI_EnumAndSetType;
  26785. begin
  26786. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26787. StartProgram(false);
  26788. Add('type');
  26789. Add(' TFlag = (light,dark);');
  26790. Add(' TFlags = set of TFlag;');
  26791. Add(' TProc = function(f: TFlags): TFlag;');
  26792. Add('var p: pointer;');
  26793. Add('begin');
  26794. Add(' p:=typeinfo(tflag);');
  26795. Add(' p:=typeinfo(tflags);');
  26796. ConvertProgram;
  26797. CheckSource('TestRTTI_EnumAndType',
  26798. LinesToStr([ // statements
  26799. 'this.TFlag = {',
  26800. ' "0": "light",',
  26801. ' light: 0,',
  26802. ' "1": "dark",',
  26803. ' dark: 1',
  26804. '};',
  26805. '$mod.$rtti.$Enum("TFlag", {',
  26806. ' minvalue: 0,',
  26807. ' maxvalue: 1,',
  26808. ' ordtype: 1,',
  26809. ' enumtype: this.TFlag',
  26810. '});',
  26811. '$mod.$rtti.$Set("TFlags", {',
  26812. ' comptype: $mod.$rtti["TFlag"]',
  26813. '});',
  26814. '$mod.$rtti.$ProcVar("TProc", {',
  26815. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  26816. '});',
  26817. 'this.p = null;',
  26818. '']),
  26819. LinesToStr([ // $mod.$main
  26820. '$mod.p = $mod.$rtti["TFlag"];',
  26821. '$mod.p = $mod.$rtti["TFlags"];',
  26822. '']));
  26823. end;
  26824. procedure TTestModule.TestRTTI_EnumRange;
  26825. begin
  26826. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26827. StartProgram(false);
  26828. Add([
  26829. 'type',
  26830. ' TCol = (red,green,blue);',
  26831. ' TColRg = green..blue;',
  26832. ' TSetOfColRg = set of TColRg;',
  26833. 'var p: pointer;',
  26834. 'begin',
  26835. ' p:=typeinfo(tcolrg);',
  26836. ' p:=typeinfo(tsetofcolrg);',
  26837. '']);
  26838. ConvertProgram;
  26839. end;
  26840. procedure TTestModule.TestRTTI_AnonymousEnumType;
  26841. begin
  26842. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26843. StartProgram(false);
  26844. Add('type');
  26845. Add(' TFlags = set of (red, green);');
  26846. Add('var');
  26847. Add(' f: TFlags;');
  26848. Add('begin');
  26849. Add(' Include(f,red);');
  26850. ConvertProgram;
  26851. CheckSource('TestRTTI_AnonymousEnumType',
  26852. LinesToStr([ // statements
  26853. 'this.TFlags$a = {',
  26854. ' "0": "red",',
  26855. ' red: 0,',
  26856. ' "1": "green",',
  26857. ' green: 1',
  26858. '};',
  26859. '$mod.$rtti.$Enum("TFlags$a", {',
  26860. ' minvalue: 0,',
  26861. ' maxvalue: 1,',
  26862. ' ordtype: 1,',
  26863. ' enumtype: this.TFlags$a',
  26864. '});',
  26865. '$mod.$rtti.$Set("TFlags", {',
  26866. ' comptype: $mod.$rtti["TFlags$a"]',
  26867. '});',
  26868. 'this.f = {};',
  26869. '']),
  26870. LinesToStr([
  26871. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  26872. '']));
  26873. end;
  26874. procedure TTestModule.TestRTTI_StaticArray;
  26875. begin
  26876. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26877. StartProgram(false);
  26878. Add('type');
  26879. Add(' TFlag = (light,dark);');
  26880. Add(' TFlagNames = array[TFlag] of string;');
  26881. Add(' TBoolNames = array[boolean] of string;');
  26882. Add(' TByteArray = array[1..32768] of byte;');
  26883. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  26884. Add('var p: pointer;');
  26885. Add('begin');
  26886. Add(' p:=typeinfo(TFlagNames);');
  26887. Add(' p:=typeinfo(TBoolNames);');
  26888. ConvertProgram;
  26889. CheckSource('TestRTTI_StaticArray',
  26890. LinesToStr([ // statements
  26891. 'this.TFlag = {',
  26892. ' "0": "light",',
  26893. ' light: 0,',
  26894. ' "1": "dark",',
  26895. ' dark: 1',
  26896. '};',
  26897. '$mod.$rtti.$Enum("TFlag", {',
  26898. ' minvalue: 0,',
  26899. ' maxvalue: 1,',
  26900. ' ordtype: 1,',
  26901. ' enumtype: this.TFlag',
  26902. '});',
  26903. '$mod.$rtti.$StaticArray("TFlagNames", {',
  26904. ' dims: [2],',
  26905. ' eltype: rtl.string',
  26906. '});',
  26907. '$mod.$rtti.$StaticArray("TBoolNames", {',
  26908. ' dims: [2],',
  26909. ' eltype: rtl.string',
  26910. '});',
  26911. '$mod.$rtti.$StaticArray("TByteArray", {',
  26912. ' dims: [32768],',
  26913. ' eltype: rtl.byte',
  26914. '});',
  26915. '$mod.$rtti.$ProcVar("TProc", {',
  26916. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  26917. '});',
  26918. 'this.p = null;',
  26919. '']),
  26920. LinesToStr([ // $mod.$main
  26921. '$mod.p = $mod.$rtti["TFlagNames"];',
  26922. '$mod.p = $mod.$rtti["TBoolNames"];',
  26923. '']));
  26924. end;
  26925. procedure TTestModule.TestRTTI_DynArray;
  26926. begin
  26927. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26928. StartProgram(false);
  26929. Add('type');
  26930. Add(' TArrStr = array of string;');
  26931. Add(' TArr2Dim = array of tarrstr;');
  26932. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  26933. Add('var p: pointer;');
  26934. Add('begin');
  26935. Add(' p:=typeinfo(tarrstr);');
  26936. Add(' p:=typeinfo(tarr2dim);');
  26937. ConvertProgram;
  26938. CheckSource('TestRTTI_DynArray',
  26939. LinesToStr([ // statements
  26940. '$mod.$rtti.$DynArray("TArrStr", {',
  26941. ' eltype: rtl.string',
  26942. '});',
  26943. '$mod.$rtti.$DynArray("TArr2Dim", {',
  26944. ' eltype: $mod.$rtti["TArrStr"]',
  26945. '});',
  26946. '$mod.$rtti.$ProcVar("TProc", {',
  26947. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  26948. '});',
  26949. 'this.p = null;',
  26950. '']),
  26951. LinesToStr([ // $mod.$main
  26952. '$mod.p = $mod.$rtti["TArrStr"];',
  26953. '$mod.p = $mod.$rtti["TArr2Dim"];',
  26954. '']));
  26955. end;
  26956. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  26957. begin
  26958. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26959. StartProgram(false);
  26960. Add('type');
  26961. Add(' TArr = array of array of longint;');
  26962. Add('var a: TArr;');
  26963. Add('begin');
  26964. ConvertProgram;
  26965. CheckSource('TestRTTI_ArrayNestedAnonymous',
  26966. LinesToStr([ // statements
  26967. '$mod.$rtti.$DynArray("TArr$a", {',
  26968. ' eltype: rtl.longint',
  26969. '});',
  26970. '$mod.$rtti.$DynArray("TArr", {',
  26971. ' eltype: $mod.$rtti["TArr$a"]',
  26972. '});',
  26973. 'this.a = [];',
  26974. '']),
  26975. LinesToStr([ // $mod.$main
  26976. ]));
  26977. end;
  26978. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  26979. begin
  26980. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26981. StartProgram(false);
  26982. Add('type');
  26983. Add(' TObject = class');
  26984. Add(' published');
  26985. Add(' procedure Proc; virtual; abstract;');
  26986. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  26987. Add(' end;');
  26988. Add('begin');
  26989. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  26990. nDuplicatePublishedMethodXAtY);
  26991. ConvertProgram;
  26992. end;
  26993. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  26994. begin
  26995. Converter.Options:=Converter.Options-[coNoTypeInfo];
  26996. StartProgram(false);
  26997. Add('type');
  26998. Add(' TObject = class');
  26999. Add(' published');
  27000. Add(' procedure Proc; external name ''foo'';');
  27001. Add(' end;');
  27002. Add('begin');
  27003. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  27004. nPublishedNameMustMatchExternal);
  27005. ConvertProgram;
  27006. end;
  27007. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  27008. begin
  27009. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27010. StartProgram(false);
  27011. Add('type');
  27012. Add(' TObject = class');
  27013. Add(' class var FA: longint;');
  27014. Add(' published');
  27015. Add(' class property A: longint read FA;');
  27016. Add(' end;');
  27017. Add('begin');
  27018. SetExpectedPasResolverError('Invalid published property modifier "class"',
  27019. nInvalidXModifierY);
  27020. ConvertProgram;
  27021. end;
  27022. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  27023. begin
  27024. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27025. StartProgram(false);
  27026. Add('type');
  27027. Add(' TObject = class');
  27028. Add(' published');
  27029. Add(' class var FA: longint;');
  27030. Add(' end;');
  27031. Add('begin');
  27032. SetExpectedPasResolverError(sSymbolCannotBePublished,
  27033. nSymbolCannotBePublished);
  27034. ConvertProgram;
  27035. end;
  27036. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  27037. begin
  27038. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27039. StartProgram(false);
  27040. Add('{$modeswitch externalclass}');
  27041. Add('type');
  27042. Add(' TObject = class');
  27043. Add(' published');
  27044. Add(' V: longint; external name ''foo'';');
  27045. Add(' end;');
  27046. Add('begin');
  27047. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  27048. nPublishedNameMustMatchExternal);
  27049. ConvertProgram;
  27050. end;
  27051. procedure TTestModule.TestRTTI_Class_Field;
  27052. begin
  27053. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27054. StartProgram(false);
  27055. Add('{$modeswitch externalclass}');
  27056. Add('type');
  27057. Add(' TObject = class');
  27058. Add(' private');
  27059. Add(' FPropA: string;');
  27060. Add(' published');
  27061. Add(' VarLI: longint;');
  27062. Add(' VarC: char;');
  27063. Add(' VarS: string;');
  27064. Add(' VarD: double;');
  27065. Add(' VarB: boolean;');
  27066. Add(' VarLW: longword;');
  27067. Add(' VarSmI: smallint;');
  27068. Add(' VarW: word;');
  27069. Add(' VarShI: shortint;');
  27070. Add(' VarBy: byte;');
  27071. Add(' VarExt: longint external name ''VarExt'';');
  27072. Add(' end;');
  27073. Add('var p: pointer;');
  27074. Add(' Obj: tobject;');
  27075. Add('begin');
  27076. Add(' p:=typeinfo(tobject);');
  27077. Add(' p:=typeinfo(p);');
  27078. Add(' p:=typeinfo(obj);');
  27079. ConvertProgram;
  27080. CheckSource('TestRTTI_Class_Field',
  27081. LinesToStr([ // statements
  27082. 'rtl.createClass($mod, "TObject", null, function () {',
  27083. ' this.$init = function () {',
  27084. ' this.FPropA = "";',
  27085. ' this.VarLI = 0;',
  27086. ' this.VarC = "";',
  27087. ' this.VarS = "";',
  27088. ' this.VarD = 0.0;',
  27089. ' this.VarB = false;',
  27090. ' this.VarLW = 0;',
  27091. ' this.VarSmI = 0;',
  27092. ' this.VarW = 0;',
  27093. ' this.VarShI = 0;',
  27094. ' this.VarBy = 0;',
  27095. ' };',
  27096. ' this.$final = function () {',
  27097. ' };',
  27098. ' var $r = this.$rtti;',
  27099. ' $r.addField("VarLI", rtl.longint);',
  27100. ' $r.addField("VarC", rtl.char);',
  27101. ' $r.addField("VarS", rtl.string);',
  27102. ' $r.addField("VarD", rtl.double);',
  27103. ' $r.addField("VarB", rtl.boolean);',
  27104. ' $r.addField("VarLW", rtl.longword);',
  27105. ' $r.addField("VarSmI", rtl.smallint);',
  27106. ' $r.addField("VarW", rtl.word);',
  27107. ' $r.addField("VarShI", rtl.shortint);',
  27108. ' $r.addField("VarBy", rtl.byte);',
  27109. ' $r.addField("VarExt", rtl.longint);',
  27110. '});',
  27111. 'this.p = null;',
  27112. 'this.Obj = null;',
  27113. '']),
  27114. LinesToStr([ // $mod.$main
  27115. '$mod.p = $mod.$rtti["TObject"];',
  27116. '$mod.p = rtl.pointer;',
  27117. '$mod.p = $mod.Obj.$rtti;',
  27118. '']));
  27119. end;
  27120. procedure TTestModule.TestRTTI_Class_Method;
  27121. begin
  27122. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27123. StartProgram(false);
  27124. Add('type');
  27125. Add(' TObject = class');
  27126. Add(' private');
  27127. Add(' procedure Internal; external name ''$intern'';');
  27128. Add(' published');
  27129. Add(' procedure Click; virtual; abstract;');
  27130. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  27131. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  27132. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  27133. Add(' end;');
  27134. Add('begin');
  27135. ConvertProgram;
  27136. CheckSource('TestRTTI_Class_Method',
  27137. LinesToStr([ // statements
  27138. 'rtl.createClass($mod, "TObject", null, function () {',
  27139. ' this.$init = function () {',
  27140. ' };',
  27141. ' this.$final = function () {',
  27142. ' };',
  27143. ' var $r = this.$rtti;',
  27144. ' $r.addMethod("Click", 0, null);',
  27145. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  27146. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  27147. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  27148. ' flags: 2',
  27149. ' });',
  27150. '});',
  27151. '']),
  27152. LinesToStr([ // $mod.$main
  27153. '']));
  27154. end;
  27155. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  27156. begin
  27157. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27158. StartProgram(false);
  27159. Add('type');
  27160. Add(' TObject = class');
  27161. Add(' published');
  27162. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  27163. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  27164. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  27165. Add(' end;');
  27166. Add('begin');
  27167. ConvertProgram;
  27168. CheckSource('TestRTTI_Class_MethodOpenArray',
  27169. LinesToStr([ // statements
  27170. 'rtl.createClass($mod, "TObject", null, function () {',
  27171. ' this.$init = function () {',
  27172. ' };',
  27173. ' this.$final = function () {',
  27174. ' };',
  27175. ' var $r = this.$rtti;',
  27176. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  27177. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  27178. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  27179. '});',
  27180. '']),
  27181. LinesToStr([ // $mod.$main
  27182. '']));
  27183. end;
  27184. procedure TTestModule.TestRTTI_Class_Property;
  27185. begin
  27186. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27187. StartProgram(false);
  27188. Add('{$modeswitch externalclass}');
  27189. Add('type');
  27190. Add(' TObject = class');
  27191. Add(' private');
  27192. Add(' FColor: longint;');
  27193. Add(' FColorStored: boolean;');
  27194. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  27195. Add(' function GetColor: longint; virtual; abstract;');
  27196. Add(' function GetColorStored: boolean; virtual; abstract;');
  27197. Add(' FExtSize: longint external name ''$extSize'';');
  27198. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  27199. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  27200. Add(' function GetExtSize: longint; external name ''$getSize'';');
  27201. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  27202. Add(' published');
  27203. Add(' property ColorA: longint read FColor;');
  27204. Add(' property ColorB: longint write FColor;');
  27205. Add(' property ColorC: longint read GetColor write SetColor;');
  27206. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  27207. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  27208. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  27209. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  27210. Add(' end;');
  27211. Add('begin');
  27212. ConvertProgram;
  27213. CheckSource('TestRTTI_Class_Property',
  27214. LinesToStr([ // statements
  27215. 'rtl.createClass($mod, "TObject", null, function () {',
  27216. ' this.$init = function () {',
  27217. ' this.FColor = 0;',
  27218. ' this.FColorStored = false;',
  27219. ' };',
  27220. ' this.$final = function () {',
  27221. ' };',
  27222. ' var $r = this.$rtti;',
  27223. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  27224. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  27225. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  27226. ' $r.addProperty(',
  27227. ' "ColorD",',
  27228. ' 8,',
  27229. ' rtl.longint,',
  27230. ' "FColor",',
  27231. ' "FColor",',
  27232. ' {',
  27233. ' stored: "FColorStored"',
  27234. ' }',
  27235. ' );',
  27236. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  27237. ' $r.addProperty(',
  27238. ' "ExtSizeB",',
  27239. ' 11,',
  27240. ' rtl.longint,',
  27241. ' "$getSize",',
  27242. ' "$setSize",',
  27243. ' {',
  27244. ' stored: "$extSizeStored"',
  27245. ' }',
  27246. ' );',
  27247. ' $r.addProperty(',
  27248. ' "ExtSizeC",',
  27249. ' 12,',
  27250. ' rtl.longint,',
  27251. ' "$extSize",',
  27252. ' "$extSize",',
  27253. ' {',
  27254. ' stored: "$getExtSizeStored"',
  27255. ' }',
  27256. ' );',
  27257. '});',
  27258. '']),
  27259. LinesToStr([ // $mod.$main
  27260. '']));
  27261. end;
  27262. procedure TTestModule.TestRTTI_Class_PropertyParams;
  27263. begin
  27264. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27265. StartProgram(false);
  27266. Add('{$modeswitch externalclass}');
  27267. Add('type');
  27268. Add(' integer = longint;');
  27269. Add(' TObject = class');
  27270. Add(' private');
  27271. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  27272. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  27273. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  27274. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  27275. Add(' published');
  27276. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  27277. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  27278. Add(' end;');
  27279. Add('begin');
  27280. ConvertProgram;
  27281. CheckSource('TestRTTI_Class_PropertyParams',
  27282. LinesToStr([ // statements
  27283. 'rtl.createClass($mod, "TObject", null, function () {',
  27284. ' this.$init = function () {',
  27285. ' };',
  27286. ' this.$final = function () {',
  27287. ' };',
  27288. ' var $r = this.$rtti;',
  27289. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  27290. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  27291. '});',
  27292. '']),
  27293. LinesToStr([ // $mod.$main
  27294. '']));
  27295. end;
  27296. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  27297. begin
  27298. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27299. AddModuleWithIntfImplSrc('unit1.pas',
  27300. 'type TColor = -5..5;',
  27301. '');
  27302. StartProgram(true);
  27303. Add([
  27304. 'uses unit1;',
  27305. 'type',
  27306. ' TColorAlias = TColor;',
  27307. ' TColorTypeAlias = type TColor;',
  27308. ' TObject = class',
  27309. ' private',
  27310. ' fColor: TColor;',
  27311. ' fAlias: TColorAlias;',
  27312. ' fTypeAlias: TColorTypeAlias;',
  27313. ' published',
  27314. ' property Color: TColor read fcolor;',
  27315. ' property Alias: TColorAlias read falias;',
  27316. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  27317. ' end;',
  27318. 'begin',
  27319. '']);
  27320. ConvertProgram;
  27321. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  27322. LinesToStr([ // statements
  27323. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  27324. 'rtl.createClass($mod, "TObject", null, function () {',
  27325. ' this.$init = function () {',
  27326. ' this.fColor = 0;',
  27327. ' this.fAlias = 0;',
  27328. ' this.fTypeAlias = 0;',
  27329. ' };',
  27330. ' this.$final = function () {',
  27331. ' };',
  27332. ' var $r = this.$rtti;',
  27333. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  27334. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  27335. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  27336. '});',
  27337. '']),
  27338. LinesToStr([ // $mod.$main
  27339. '']));
  27340. end;
  27341. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  27342. begin
  27343. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27344. StartProgram(false);
  27345. Add([
  27346. '{$modeswitch omitrtti}',
  27347. 'type',
  27348. ' TObject = class',
  27349. ' private',
  27350. ' FA: byte;',
  27351. ' published',
  27352. ' property A: byte read FA write FA;',
  27353. ' end;',
  27354. 'begin']);
  27355. ConvertProgram;
  27356. CheckSource('TestRTTI_Class_OmitRTTI',
  27357. LinesToStr([ // statements
  27358. 'rtl.createClass($mod, "TObject", null, function () {',
  27359. ' this.$init = function () {',
  27360. ' this.FA = 0;',
  27361. ' };',
  27362. ' this.$final = function () {',
  27363. ' };',
  27364. '});',
  27365. '']),
  27366. LinesToStr([ // $mod.$main
  27367. '']));
  27368. end;
  27369. procedure TTestModule.TestRTTI_IndexModifier;
  27370. begin
  27371. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27372. StartProgram(false);
  27373. Add([
  27374. 'type',
  27375. ' TEnum = (red, blue);',
  27376. ' TObject = class',
  27377. ' FB: boolean;',
  27378. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  27379. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  27380. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  27381. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  27382. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  27383. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  27384. ' published',
  27385. ' property B1: boolean index 1 read FB write SetIntBool;',
  27386. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  27387. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  27388. ' end;',
  27389. 'begin']);
  27390. ConvertProgram;
  27391. CheckSource('TestRTTI_IndexModifier',
  27392. LinesToStr([ // statements
  27393. 'this.TEnum = {',
  27394. ' "0": "red",',
  27395. ' red: 0,',
  27396. ' "1": "blue",',
  27397. ' blue: 1',
  27398. '};',
  27399. '$mod.$rtti.$Enum("TEnum", {',
  27400. ' minvalue: 0,',
  27401. ' maxvalue: 1,',
  27402. ' ordtype: 1,',
  27403. ' enumtype: this.TEnum',
  27404. '});',
  27405. 'rtl.createClass($mod, "TObject", null, function () {',
  27406. ' this.$init = function () {',
  27407. ' this.FB = false;',
  27408. ' };',
  27409. ' this.$final = function () {',
  27410. ' };',
  27411. ' var $r = this.$rtti;',
  27412. ' $r.addProperty(',
  27413. ' "B1",',
  27414. ' 18,',
  27415. ' rtl.boolean,',
  27416. ' "FB",',
  27417. ' "SetIntBool",',
  27418. ' {',
  27419. ' index: 1',
  27420. ' }',
  27421. ' );',
  27422. ' $r.addProperty(',
  27423. ' "B2",',
  27424. ' 17,',
  27425. ' rtl.boolean,',
  27426. ' "GetEnumBool",',
  27427. ' "FB",',
  27428. ' {',
  27429. ' index: $mod.TEnum.blue',
  27430. ' }',
  27431. ' );',
  27432. ' $r.addProperty(',
  27433. ' "I1",',
  27434. ' 19,',
  27435. ' rtl.boolean,',
  27436. ' "GetStrIntBool",',
  27437. ' "SetStrIntBool",',
  27438. ' {',
  27439. ' index: 2',
  27440. ' }',
  27441. ' );',
  27442. '});',
  27443. '']),
  27444. LinesToStr([ // $mod.$main
  27445. '']));
  27446. end;
  27447. procedure TTestModule.TestRTTI_StoredModifier;
  27448. begin
  27449. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27450. StartProgram(false);
  27451. Add([
  27452. 'const',
  27453. ' ConstB = true;',
  27454. 'type',
  27455. ' TObject = class',
  27456. ' private',
  27457. ' FB: boolean;',
  27458. ' function IsBStored: boolean; virtual; abstract;',
  27459. ' published',
  27460. ' property BoolA: boolean read FB stored true;',
  27461. ' property BoolB: boolean read FB stored false;',
  27462. ' property BoolC: boolean read FB stored FB;',
  27463. ' property BoolD: boolean read FB stored ConstB;',
  27464. ' property BoolE: boolean read FB stored IsBStored;',
  27465. ' end;',
  27466. 'begin']);
  27467. ConvertProgram;
  27468. CheckSource('TestRTTI_StoredModifier',
  27469. LinesToStr([ // statements
  27470. 'this.ConstB = true;',
  27471. 'rtl.createClass($mod, "TObject", null, function () {',
  27472. ' this.$init = function () {',
  27473. ' this.FB = false;',
  27474. ' };',
  27475. ' this.$final = function () {',
  27476. ' };',
  27477. ' var $r = this.$rtti;',
  27478. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  27479. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  27480. ' $r.addProperty(',
  27481. ' "BoolC",',
  27482. ' 8,',
  27483. ' rtl.boolean,',
  27484. ' "FB",',
  27485. ' "",',
  27486. ' {',
  27487. ' stored: "FB"',
  27488. ' }',
  27489. ' );',
  27490. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  27491. ' $r.addProperty(',
  27492. ' "BoolE",',
  27493. ' 12,',
  27494. ' rtl.boolean,',
  27495. ' "FB",',
  27496. ' "",',
  27497. ' {',
  27498. ' stored: "IsBStored"',
  27499. ' }',
  27500. ' );',
  27501. '});',
  27502. '']),
  27503. LinesToStr([ // $mod.$main
  27504. '']));
  27505. end;
  27506. procedure TTestModule.TestRTTI_DefaultValue;
  27507. begin
  27508. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27509. StartProgram(false);
  27510. Add([
  27511. 'type',
  27512. ' TEnum = (red, blue);',
  27513. 'const',
  27514. ' CB = true or false;',
  27515. ' CI = 1+2;',
  27516. 'type',
  27517. ' TObject = class',
  27518. ' FB: boolean;',
  27519. ' FI: longint;',
  27520. ' FE: TEnum;',
  27521. ' published',
  27522. ' property B1: boolean read FB default true;',
  27523. ' property B2: boolean read FB default CB;',
  27524. ' property B3: boolean read FB default test1.cb;',
  27525. ' property I1: longint read FI default 2;',
  27526. ' property I2: longint read FI default CI;',
  27527. ' property E1: TEnum read FE default red;',
  27528. ' property E2: TEnum read FE default TEnum.blue;',
  27529. ' end;',
  27530. 'begin']);
  27531. ConvertProgram;
  27532. CheckSource('TestRTTI_DefaultValue',
  27533. LinesToStr([ // statements
  27534. 'this.TEnum = {',
  27535. ' "0": "red",',
  27536. ' red: 0,',
  27537. ' "1": "blue",',
  27538. ' blue: 1',
  27539. '};',
  27540. '$mod.$rtti.$Enum("TEnum", {',
  27541. ' minvalue: 0,',
  27542. ' maxvalue: 1,',
  27543. ' ordtype: 1,',
  27544. ' enumtype: this.TEnum',
  27545. '});',
  27546. 'this.CB = true || false;',
  27547. 'this.CI = 1 + 2;',
  27548. 'rtl.createClass($mod, "TObject", null, function () {',
  27549. ' this.$init = function () {',
  27550. ' this.FB = false;',
  27551. ' this.FI = 0;',
  27552. ' this.FE = 0;',
  27553. ' };',
  27554. ' this.$final = function () {',
  27555. ' };',
  27556. ' var $r = this.$rtti;',
  27557. ' $r.addProperty(',
  27558. ' "B1",',
  27559. ' 0,',
  27560. ' rtl.boolean,',
  27561. ' "FB",',
  27562. ' "",',
  27563. ' {',
  27564. ' Default: true',
  27565. ' }',
  27566. ' );',
  27567. ' $r.addProperty(',
  27568. ' "B2",',
  27569. ' 0,',
  27570. ' rtl.boolean,',
  27571. ' "FB",',
  27572. ' "",',
  27573. ' {',
  27574. ' Default: true',
  27575. ' }',
  27576. ' );',
  27577. ' $r.addProperty(',
  27578. ' "B3",',
  27579. ' 0,',
  27580. ' rtl.boolean,',
  27581. ' "FB",',
  27582. ' "",',
  27583. ' {',
  27584. ' Default: true',
  27585. ' }',
  27586. ' );',
  27587. ' $r.addProperty(',
  27588. ' "I1",',
  27589. ' 0,',
  27590. ' rtl.longint,',
  27591. ' "FI",',
  27592. ' "",',
  27593. ' {',
  27594. ' Default: 2',
  27595. ' }',
  27596. ' );',
  27597. ' $r.addProperty(',
  27598. ' "I2",',
  27599. ' 0,',
  27600. ' rtl.longint,',
  27601. ' "FI",',
  27602. ' "",',
  27603. ' {',
  27604. ' Default: 3',
  27605. ' }',
  27606. ' );',
  27607. ' $r.addProperty(',
  27608. ' "E1",',
  27609. ' 0,',
  27610. ' $mod.$rtti["TEnum"],',
  27611. ' "FE",',
  27612. ' "",',
  27613. ' {',
  27614. ' Default: $mod.TEnum.red',
  27615. ' }',
  27616. ' );',
  27617. ' $r.addProperty(',
  27618. ' "E2",',
  27619. ' 0,',
  27620. ' $mod.$rtti["TEnum"],',
  27621. ' "FE",',
  27622. ' "",',
  27623. ' {',
  27624. ' Default: $mod.TEnum.blue',
  27625. ' }',
  27626. ' );',
  27627. '});',
  27628. '']),
  27629. LinesToStr([ // $mod.$main
  27630. '']));
  27631. end;
  27632. procedure TTestModule.TestRTTI_DefaultValueSet;
  27633. begin
  27634. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27635. StartProgram(false);
  27636. Add([
  27637. 'type',
  27638. ' TEnum = (red, blue);',
  27639. ' TSet = set of TEnum;',
  27640. 'const',
  27641. ' CSet = [red,blue];',
  27642. 'type',
  27643. ' TObject = class',
  27644. ' FSet: TSet;',
  27645. ' published',
  27646. ' property Set1: TSet read FSet default [];',
  27647. ' property Set2: TSet read FSet default [red];',
  27648. ' property Set3: TSet read FSet default [red,blue];',
  27649. ' property Set4: TSet read FSet default CSet;',
  27650. ' end;',
  27651. 'begin']);
  27652. ConvertProgram;
  27653. CheckSource('TestRTTI_DefaultValueSet',
  27654. LinesToStr([ // statements
  27655. 'this.TEnum = {',
  27656. ' "0": "red",',
  27657. ' red: 0,',
  27658. ' "1": "blue",',
  27659. ' blue: 1',
  27660. '};',
  27661. '$mod.$rtti.$Enum("TEnum", {',
  27662. ' minvalue: 0,',
  27663. ' maxvalue: 1,',
  27664. ' ordtype: 1,',
  27665. ' enumtype: this.TEnum',
  27666. '});',
  27667. '$mod.$rtti.$Set("TSet", {',
  27668. ' comptype: $mod.$rtti["TEnum"]',
  27669. '});',
  27670. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  27671. 'rtl.createClass($mod, "TObject", null, function () {',
  27672. ' this.$init = function () {',
  27673. ' this.FSet = {};',
  27674. ' };',
  27675. ' this.$final = function () {',
  27676. ' this.FSet = undefined;',
  27677. ' };',
  27678. ' var $r = this.$rtti;',
  27679. ' $r.addProperty(',
  27680. ' "Set1",',
  27681. ' 0,',
  27682. ' $mod.$rtti["TSet"],',
  27683. ' "FSet",',
  27684. ' "",',
  27685. ' {',
  27686. ' Default: {}',
  27687. ' }',
  27688. ' );',
  27689. ' $r.addProperty(',
  27690. ' "Set2",',
  27691. ' 0,',
  27692. ' $mod.$rtti["TSet"],',
  27693. ' "FSet",',
  27694. ' "",',
  27695. ' {',
  27696. ' Default: rtl.createSet($mod.TEnum.red)',
  27697. ' }',
  27698. ' );',
  27699. ' $r.addProperty(',
  27700. ' "Set3",',
  27701. ' 0,',
  27702. ' $mod.$rtti["TSet"],',
  27703. ' "FSet",',
  27704. ' "",',
  27705. ' {',
  27706. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  27707. ' }',
  27708. ' );',
  27709. ' $r.addProperty(',
  27710. ' "Set4",',
  27711. ' 0,',
  27712. ' $mod.$rtti["TSet"],',
  27713. ' "FSet",',
  27714. ' "",',
  27715. ' {',
  27716. ' Default: $mod.CSet',
  27717. ' }',
  27718. ' );',
  27719. '});',
  27720. '']),
  27721. LinesToStr([ // $mod.$main
  27722. '']));
  27723. end;
  27724. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  27725. begin
  27726. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27727. StartProgram(false);
  27728. Add([
  27729. 'type',
  27730. ' TRg = -1..1;',
  27731. 'const',
  27732. ' l = low(TRg);',
  27733. ' h = high(TRg);',
  27734. 'type',
  27735. ' TObject = class',
  27736. ' FV: TRg;',
  27737. ' published',
  27738. ' property V1: TRg read FV default -1;',
  27739. ' end;',
  27740. 'begin']);
  27741. ConvertProgram;
  27742. CheckSource('TestRTTI_DefaultValueRangeType',
  27743. LinesToStr([ // statements
  27744. '$mod.$rtti.$Int("TRg", {',
  27745. ' minvalue: -1,',
  27746. ' maxvalue: 1,',
  27747. ' ordtype: 0',
  27748. '});',
  27749. 'this.l = -1;',
  27750. 'this.h = 1;',
  27751. 'rtl.createClass($mod, "TObject", null, function () {',
  27752. ' this.$init = function () {',
  27753. ' this.FV = 0;',
  27754. ' };',
  27755. ' this.$final = function () {',
  27756. ' };',
  27757. ' var $r = this.$rtti;',
  27758. ' $r.addProperty(',
  27759. ' "V1",',
  27760. ' 0,',
  27761. ' $mod.$rtti["TRg"],',
  27762. ' "FV",',
  27763. ' "",',
  27764. ' {',
  27765. ' Default: -1',
  27766. ' }',
  27767. ' );',
  27768. '});',
  27769. '']),
  27770. LinesToStr([ // $mod.$main
  27771. '']));
  27772. end;
  27773. procedure TTestModule.TestRTTI_DefaultValueInherit;
  27774. begin
  27775. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27776. StartProgram(false);
  27777. Add([
  27778. 'type',
  27779. ' TObject = class',
  27780. ' FA, FB: byte;',
  27781. ' property A: byte read FA default 1;',
  27782. ' property B: byte read FB default 2;',
  27783. ' end;',
  27784. ' TBird = class',
  27785. ' published',
  27786. ' property A;',
  27787. ' property B nodefault;',
  27788. ' end;',
  27789. 'begin']);
  27790. ConvertProgram;
  27791. CheckSource('TestRTTI_DefaultValueInherit',
  27792. LinesToStr([ // statements
  27793. 'rtl.createClass($mod, "TObject", null, function () {',
  27794. ' this.$init = function () {',
  27795. ' this.FA = 0;',
  27796. ' this.FB = 0;',
  27797. ' };',
  27798. ' this.$final = function () {',
  27799. ' };',
  27800. '});',
  27801. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  27802. ' var $r = this.$rtti;',
  27803. ' $r.addProperty(',
  27804. ' "A",',
  27805. ' 0,',
  27806. ' rtl.byte,',
  27807. ' "FA",',
  27808. ' "",',
  27809. ' {',
  27810. ' Default: 1',
  27811. ' }',
  27812. ' );',
  27813. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  27814. '});',
  27815. '']),
  27816. LinesToStr([ // $mod.$main
  27817. '']));
  27818. end;
  27819. procedure TTestModule.TestRTTI_OverrideMethod;
  27820. begin
  27821. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27822. StartProgram(false);
  27823. Add('type');
  27824. Add(' TObject = class');
  27825. Add(' published');
  27826. Add(' procedure DoIt; virtual; abstract;');
  27827. Add(' end;');
  27828. Add(' TSky = class');
  27829. Add(' published');
  27830. Add(' procedure DoIt; override;');
  27831. Add(' end;');
  27832. Add('procedure TSky.DoIt; begin end;');
  27833. Add('begin');
  27834. ConvertProgram;
  27835. CheckSource('TestRTTI_OverrideMethod',
  27836. LinesToStr([ // statements
  27837. 'rtl.createClass($mod, "TObject", null, function () {',
  27838. ' this.$init = function () {',
  27839. ' };',
  27840. ' this.$final = function () {',
  27841. ' };',
  27842. ' var $r = this.$rtti;',
  27843. ' $r.addMethod("DoIt", 0, null);',
  27844. '});',
  27845. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27846. ' this.DoIt = function () {',
  27847. ' };',
  27848. '});',
  27849. '']),
  27850. LinesToStr([ // $mod.$main
  27851. '']));
  27852. end;
  27853. procedure TTestModule.TestRTTI_ReintroduceMethod;
  27854. begin
  27855. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27856. StartProgram(false);
  27857. Add([
  27858. 'type',
  27859. ' TObject = class',
  27860. ' published',
  27861. ' procedure DoIt;',
  27862. ' end;',
  27863. ' TSky = class',
  27864. ' published',
  27865. ' procedure DoIt; reintroduce;',
  27866. ' end;',
  27867. 'procedure TObject.DoIt; begin end;',
  27868. 'procedure TSky.DoIt;',
  27869. 'begin',
  27870. ' inherited DoIt;',
  27871. 'end;',
  27872. 'begin']);
  27873. ConvertProgram;
  27874. CheckSource('TestRTTI_ReintroduceMethod',
  27875. LinesToStr([ // statements
  27876. 'rtl.createClass($mod, "TObject", null, function () {',
  27877. ' this.$init = function () {',
  27878. ' };',
  27879. ' this.$final = function () {',
  27880. ' };',
  27881. ' this.DoIt = function () {',
  27882. ' };',
  27883. ' var $r = this.$rtti;',
  27884. ' $r.addMethod("DoIt", 0, null);',
  27885. '});',
  27886. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27887. ' this.DoIt = function () {',
  27888. ' $mod.TObject.DoIt.call(this);',
  27889. ' };',
  27890. ' var $r = this.$rtti;',
  27891. ' $r.addMethod("DoIt", 0, null);',
  27892. '});',
  27893. '']),
  27894. LinesToStr([ // $mod.$main
  27895. '']));
  27896. end;
  27897. procedure TTestModule.TestRTTI_OverloadProperty;
  27898. begin
  27899. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27900. StartProgram(false);
  27901. Add('type');
  27902. Add(' TObject = class');
  27903. Add(' protected');
  27904. Add(' FFlag: longint;');
  27905. Add(' published');
  27906. Add(' property Flag: longint read fflag;');
  27907. Add(' end;');
  27908. Add(' TSky = class');
  27909. Add(' published');
  27910. Add(' property FLAG: longint write fflag;');
  27911. Add(' end;');
  27912. Add('begin');
  27913. ConvertProgram;
  27914. CheckSource('TestRTTI_OverrideMethod',
  27915. LinesToStr([ // statements
  27916. 'rtl.createClass($mod, "TObject", null, function () {',
  27917. ' this.$init = function () {',
  27918. ' this.FFlag = 0;',
  27919. ' };',
  27920. ' this.$final = function () {',
  27921. ' };',
  27922. ' var $r = this.$rtti;',
  27923. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  27924. '});',
  27925. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  27926. ' var $r = this.$rtti;',
  27927. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  27928. '});',
  27929. '']),
  27930. LinesToStr([ // $mod.$main
  27931. '']));
  27932. end;
  27933. procedure TTestModule.TestRTTI_ClassForward;
  27934. begin
  27935. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27936. StartProgram(false);
  27937. Add('type');
  27938. Add(' TObject = class end;');
  27939. Add(' tbridge = class;');
  27940. Add(' TProc = function: tbridge;');
  27941. Add(' TOger = class');
  27942. Add(' published');
  27943. Add(' FBridge: tbridge;');
  27944. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  27945. Add(' property Bridge: tbridge read fbridge write setbridge;');
  27946. Add(' end;');
  27947. Add(' TBridge = class');
  27948. Add(' FOger: toger;');
  27949. Add(' end;');
  27950. Add('var p: Pointer;');
  27951. Add(' b: tbridge;');
  27952. Add('begin');
  27953. Add(' p:=typeinfo(tbridge);');
  27954. Add(' p:=typeinfo(b);');
  27955. ConvertProgram;
  27956. CheckSource('TestRTTI_ClassForward',
  27957. LinesToStr([ // statements
  27958. 'rtl.createClass($mod, "TObject", null, function () {',
  27959. ' this.$init = function () {',
  27960. ' };',
  27961. ' this.$final = function () {',
  27962. ' };',
  27963. '});',
  27964. '$mod.$rtti.$Class("TBridge");',
  27965. '$mod.$rtti.$ProcVar("TProc", {',
  27966. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  27967. '});',
  27968. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  27969. ' this.$init = function () {',
  27970. ' $mod.TObject.$init.call(this);',
  27971. ' this.FBridge = null;',
  27972. ' };',
  27973. ' this.$final = function () {',
  27974. ' this.FBridge = undefined;',
  27975. ' $mod.TObject.$final.call(this);',
  27976. ' };',
  27977. ' var $r = this.$rtti;',
  27978. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  27979. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  27980. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  27981. '});',
  27982. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  27983. ' this.$init = function () {',
  27984. ' $mod.TObject.$init.call(this);',
  27985. ' this.FOger = null;',
  27986. ' };',
  27987. ' this.$final = function () {',
  27988. ' this.FOger = undefined;',
  27989. ' $mod.TObject.$final.call(this);',
  27990. ' };',
  27991. '});',
  27992. 'this.p = null;',
  27993. 'this.b = null;',
  27994. '']),
  27995. LinesToStr([ // $mod.$main
  27996. '$mod.p = $mod.$rtti["TBridge"];',
  27997. '$mod.p = $mod.b.$rtti;',
  27998. '']));
  27999. end;
  28000. procedure TTestModule.TestRTTI_ClassOf;
  28001. begin
  28002. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28003. StartProgram(false);
  28004. Add('type');
  28005. Add(' TClass = class of tobject;');
  28006. Add(' TProcA = function: TClass;');
  28007. Add(' TObject = class');
  28008. Add(' published');
  28009. Add(' C: tclass;');
  28010. Add(' end;');
  28011. Add(' tfox = class;');
  28012. Add(' TBird = class end;');
  28013. Add(' TBirds = class of tbird;');
  28014. Add(' TFox = class end;');
  28015. Add(' TFoxes = class of tfox;');
  28016. Add(' TCows = class of TCow;');
  28017. Add(' TCow = class;');
  28018. Add(' TCow = class end;');
  28019. Add('begin');
  28020. ConvertProgram;
  28021. CheckSource('TestRTTI_ClassOf',
  28022. LinesToStr([ // statements
  28023. '$mod.$rtti.$Class("TObject");',
  28024. '$mod.$rtti.$ClassRef("TClass", {',
  28025. ' instancetype: $mod.$rtti["TObject"]',
  28026. '});',
  28027. '$mod.$rtti.$ProcVar("TProcA", {',
  28028. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  28029. '});',
  28030. 'rtl.createClass($mod, "TObject", null, function () {',
  28031. ' this.$init = function () {',
  28032. ' this.C = null;',
  28033. ' };',
  28034. ' this.$final = function () {',
  28035. ' this.C = undefined;',
  28036. ' };',
  28037. ' var $r = this.$rtti;',
  28038. ' $r.addField("C", $mod.$rtti["TClass"]);',
  28039. '});',
  28040. '$mod.$rtti.$Class("TFox");',
  28041. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28042. '});',
  28043. '$mod.$rtti.$ClassRef("TBirds", {',
  28044. ' instancetype: $mod.$rtti["TBird"]',
  28045. '});',
  28046. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  28047. '});',
  28048. '$mod.$rtti.$ClassRef("TFoxes", {',
  28049. ' instancetype: $mod.$rtti["TFox"]',
  28050. '});',
  28051. '$mod.$rtti.$Class("TCow");',
  28052. '$mod.$rtti.$ClassRef("TCows", {',
  28053. ' instancetype: $mod.$rtti["TCow"]',
  28054. '});',
  28055. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  28056. '});',
  28057. '']),
  28058. LinesToStr([ // $mod.$main
  28059. '']));
  28060. end;
  28061. procedure TTestModule.TestRTTI_Record;
  28062. begin
  28063. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28064. StartProgram(false);
  28065. Add('type');
  28066. Add(' integer = longint;');
  28067. Add(' TPoint = record');
  28068. Add(' x,y: integer;');
  28069. Add(' end;');
  28070. Add('var p: pointer;');
  28071. Add(' r: tpoint;');
  28072. Add('begin');
  28073. Add(' p:=typeinfo(tpoint);');
  28074. Add(' p:=typeinfo(r);');
  28075. Add(' p:=typeinfo(r.x);');
  28076. ConvertProgram;
  28077. CheckSource('TestRTTI_Record',
  28078. LinesToStr([ // statements
  28079. 'rtl.recNewT($mod, "TPoint", function () {',
  28080. ' this.x = 0;',
  28081. ' this.y = 0;',
  28082. ' this.$eq = function (b) {',
  28083. ' return (this.x === b.x) && (this.y === b.y);',
  28084. ' };',
  28085. ' this.$assign = function (s) {',
  28086. ' this.x = s.x;',
  28087. ' this.y = s.y;',
  28088. ' return this;',
  28089. ' };',
  28090. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  28091. ' $r.addField("x", rtl.longint);',
  28092. ' $r.addField("y", rtl.longint);',
  28093. '});',
  28094. 'this.p = null;',
  28095. 'this.r = $mod.TPoint.$new();',
  28096. '']),
  28097. LinesToStr([ // $mod.$main
  28098. '$mod.p = $mod.$rtti["TPoint"];',
  28099. '$mod.p = $mod.$rtti["TPoint"];',
  28100. '$mod.p = rtl.longint;',
  28101. '']));
  28102. end;
  28103. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  28104. begin
  28105. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28106. StartProgram(false);
  28107. Add('type');
  28108. Add(' TFloatRec = record');
  28109. Add(' d: array of char;');
  28110. // Add(' i: array of array of longint;');
  28111. Add(' end;');
  28112. Add('var p: pointer;');
  28113. Add(' r: tfloatrec;');
  28114. Add('begin');
  28115. Add(' p:=typeinfo(tfloatrec);');
  28116. Add(' p:=typeinfo(r);');
  28117. Add(' p:=typeinfo(r.d);');
  28118. ConvertProgram;
  28119. CheckSource('TestRTTI_Record',
  28120. LinesToStr([ // statements
  28121. 'rtl.recNewT($mod, "TFloatRec", function () {',
  28122. ' this.d = [];',
  28123. ' this.$eq = function (b) {',
  28124. ' return this.d === b.d;',
  28125. ' };',
  28126. ' this.$assign = function (s) {',
  28127. ' this.d = s.d;',
  28128. ' return this;',
  28129. ' };',
  28130. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  28131. ' eltype: rtl.char',
  28132. ' });',
  28133. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  28134. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  28135. '});',
  28136. 'this.p = null;',
  28137. 'this.r = $mod.TFloatRec.$new();',
  28138. '']),
  28139. LinesToStr([ // $mod.$main
  28140. '$mod.p = $mod.$rtti["TFloatRec"];',
  28141. '$mod.p = $mod.$rtti["TFloatRec"];',
  28142. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  28143. '']));
  28144. end;
  28145. procedure TTestModule.TestRTTI_LocalTypes;
  28146. begin
  28147. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28148. StartProgram(false);
  28149. Add([
  28150. 'procedure DoIt;',
  28151. 'type',
  28152. ' integer = longint;',
  28153. ' TPoint = record',
  28154. ' x,y: integer;',
  28155. ' end;',
  28156. 'var p: TPoint;',
  28157. 'begin',
  28158. 'end;',
  28159. 'begin']);
  28160. ConvertProgram;
  28161. CheckSource('TestRTTI_LocalTypes',
  28162. LinesToStr([ // statements
  28163. 'var TPoint = rtl.recNewT(null, "", function () {',
  28164. ' this.x = 0;',
  28165. ' this.y = 0;',
  28166. ' this.$eq = function (b) {',
  28167. ' return (this.x === b.x) && (this.y === b.y);',
  28168. ' };',
  28169. ' this.$assign = function (s) {',
  28170. ' this.x = s.x;',
  28171. ' this.y = s.y;',
  28172. ' return this;',
  28173. ' };',
  28174. '});',
  28175. 'this.DoIt = function () {',
  28176. ' var p = TPoint.$new();',
  28177. '};',
  28178. '']),
  28179. LinesToStr([ // $mod.$main
  28180. '']));
  28181. end;
  28182. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  28183. begin
  28184. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28185. StartProgram(false);
  28186. Add([
  28187. 'type',
  28188. ' TCaption = string;',
  28189. ' TYesNo = boolean;',
  28190. ' TLetter = char;',
  28191. ' TFloat = double;',
  28192. ' TPtr = pointer;',
  28193. ' TShortInt = shortint;',
  28194. ' TByte = byte;',
  28195. ' TSmallInt = smallint;',
  28196. ' TWord = word;',
  28197. ' TInt32 = longint;',
  28198. ' TDWord = longword;',
  28199. ' TValue = jsvalue;',
  28200. 'var p: TPtr;',
  28201. 'begin',
  28202. ' p:=typeinfo(string);',
  28203. ' p:=typeinfo(tcaption);',
  28204. ' p:=typeinfo(boolean);',
  28205. ' p:=typeinfo(tyesno);',
  28206. ' p:=typeinfo(char);',
  28207. ' p:=typeinfo(tletter);',
  28208. ' p:=typeinfo(double);',
  28209. ' p:=typeinfo(tfloat);',
  28210. ' p:=typeinfo(pointer);',
  28211. ' p:=typeinfo(tptr);',
  28212. ' p:=typeinfo(shortint);',
  28213. ' p:=typeinfo(tshortint);',
  28214. ' p:=typeinfo(byte);',
  28215. ' p:=typeinfo(tbyte);',
  28216. ' p:=typeinfo(smallint);',
  28217. ' p:=typeinfo(tsmallint);',
  28218. ' p:=typeinfo(word);',
  28219. ' p:=typeinfo(tword);',
  28220. ' p:=typeinfo(longword);',
  28221. ' p:=typeinfo(tdword);',
  28222. ' p:=typeinfo(jsvalue);',
  28223. ' p:=typeinfo(tvalue);',
  28224. '']);
  28225. ConvertProgram;
  28226. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  28227. LinesToStr([ // statements
  28228. 'this.p = null;',
  28229. '']),
  28230. LinesToStr([ // $mod.$main
  28231. '$mod.p = rtl.string;',
  28232. '$mod.p = rtl.string;',
  28233. '$mod.p = rtl.boolean;',
  28234. '$mod.p = rtl.boolean;',
  28235. '$mod.p = rtl.char;',
  28236. '$mod.p = rtl.char;',
  28237. '$mod.p = rtl.double;',
  28238. '$mod.p = rtl.double;',
  28239. '$mod.p = rtl.pointer;',
  28240. '$mod.p = rtl.pointer;',
  28241. '$mod.p = rtl.shortint;',
  28242. '$mod.p = rtl.shortint;',
  28243. '$mod.p = rtl.byte;',
  28244. '$mod.p = rtl.byte;',
  28245. '$mod.p = rtl.smallint;',
  28246. '$mod.p = rtl.smallint;',
  28247. '$mod.p = rtl.word;',
  28248. '$mod.p = rtl.word;',
  28249. '$mod.p = rtl.longword;',
  28250. '$mod.p = rtl.longword;',
  28251. '$mod.p = rtl.jsvalue;',
  28252. '$mod.p = rtl.jsvalue;',
  28253. '']));
  28254. end;
  28255. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  28256. begin
  28257. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28258. StartProgram(false);
  28259. Add([
  28260. 'type',
  28261. ' TCaption = type string;',
  28262. ' TYesNo = type boolean;',
  28263. ' TLetter = type char;',
  28264. ' TFloat = type double;',
  28265. ' TPtr = type pointer;',
  28266. ' TShortInt = type shortint;',
  28267. ' TByte = type byte;',
  28268. ' TSmallInt = type smallint;',
  28269. ' TWord = type word;',
  28270. ' TInt32 = type longint;',
  28271. ' TDWord = type longword;',
  28272. ' TValue = type jsvalue;',
  28273. ' TAliasValue = type TValue;',
  28274. 'var',
  28275. ' p: TPtr;',
  28276. ' a: TAliasValue;',
  28277. 'begin',
  28278. ' p:=typeinfo(tcaption);',
  28279. ' p:=typeinfo(tyesno);',
  28280. ' p:=typeinfo(tletter);',
  28281. ' p:=typeinfo(tfloat);',
  28282. ' p:=typeinfo(tptr);',
  28283. ' p:=typeinfo(tshortint);',
  28284. ' p:=typeinfo(tbyte);',
  28285. ' p:=typeinfo(tsmallint);',
  28286. ' p:=typeinfo(tword);',
  28287. ' p:=typeinfo(tdword);',
  28288. ' p:=typeinfo(tvalue);',
  28289. ' p:=typeinfo(taliasvalue);',
  28290. ' p:=typeinfo(a);',
  28291. '']);
  28292. ConvertProgram;
  28293. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  28294. LinesToStr([ // statements
  28295. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  28296. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  28297. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  28298. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  28299. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  28300. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  28301. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  28302. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  28303. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  28304. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  28305. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  28306. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  28307. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  28308. 'this.p = null;',
  28309. 'this.a = undefined;',
  28310. '']),
  28311. LinesToStr([ // $mod.$main
  28312. '$mod.p = $mod.$rtti["TCaption"];',
  28313. '$mod.p = $mod.$rtti["TYesNo"];',
  28314. '$mod.p = $mod.$rtti["TLetter"];',
  28315. '$mod.p = $mod.$rtti["TFloat"];',
  28316. '$mod.p = $mod.$rtti["TPtr"];',
  28317. '$mod.p = $mod.$rtti["TShortInt"];',
  28318. '$mod.p = $mod.$rtti["TByte"];',
  28319. '$mod.p = $mod.$rtti["TSmallInt"];',
  28320. '$mod.p = $mod.$rtti["TWord"];',
  28321. '$mod.p = $mod.$rtti["TDWord"];',
  28322. '$mod.p = $mod.$rtti["TValue"];',
  28323. '$mod.p = $mod.$rtti["TAliasValue"];',
  28324. '$mod.p = $mod.$rtti["TAliasValue"];',
  28325. '']));
  28326. end;
  28327. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  28328. begin
  28329. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28330. StartProgram(false);
  28331. Add('procedure DoIt;');
  28332. Add('type');
  28333. Add(' integer = longint;');
  28334. Add(' TPoint = record');
  28335. Add(' x,y: integer;');
  28336. Add(' end;');
  28337. Add('var p: pointer;');
  28338. Add('begin');
  28339. Add(' p:=typeinfo(tpoint);');
  28340. Add('end;');
  28341. Add('begin');
  28342. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  28343. ConvertProgram;
  28344. end;
  28345. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  28346. begin
  28347. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28348. StartProgram(true,[supTypeInfo]);
  28349. Add([
  28350. '{$modeswitch externalclass}',
  28351. 'type',
  28352. ' TFlag = (up,down);',
  28353. ' TFlags = set of TFlag;',
  28354. 'var',
  28355. ' ti: TTypeInfo;',
  28356. ' tiInt: TTypeInfoInteger;',
  28357. ' tiEnum: TTypeInfoEnum;',
  28358. ' tiSet: TTypeInfoSet;',
  28359. 'begin',
  28360. ' ti:=typeinfo(string);',
  28361. ' ti:=typeinfo(boolean);',
  28362. ' ti:=typeinfo(char);',
  28363. ' ti:=typeinfo(double);',
  28364. ' tiInt:=typeinfo(shortint);',
  28365. ' tiInt:=typeinfo(byte);',
  28366. ' tiInt:=typeinfo(smallint);',
  28367. ' tiInt:=typeinfo(word);',
  28368. ' tiInt:=typeinfo(longint);',
  28369. ' tiInt:=typeinfo(longword);',
  28370. ' ti:=typeinfo(jsvalue);',
  28371. ' tiEnum:=typeinfo(tflag);',
  28372. ' tiSet:=typeinfo(tflags);']);
  28373. ConvertProgram;
  28374. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  28375. LinesToStr([ // statements
  28376. 'this.TFlag = {',
  28377. ' "0": "up",',
  28378. ' up: 0,',
  28379. ' "1": "down",',
  28380. ' down: 1',
  28381. '};',
  28382. '$mod.$rtti.$Enum("TFlag", {',
  28383. ' minvalue: 0,',
  28384. ' maxvalue: 1,',
  28385. ' ordtype: 1,',
  28386. ' enumtype: this.TFlag',
  28387. '});',
  28388. '$mod.$rtti.$Set("TFlags", {',
  28389. ' comptype: $mod.$rtti["TFlag"]',
  28390. '});',
  28391. 'this.ti = null;',
  28392. 'this.tiInt = null;',
  28393. 'this.tiEnum = null;',
  28394. 'this.tiSet = null;',
  28395. '']),
  28396. LinesToStr([ // $mod.$main
  28397. '$mod.ti = rtl.string;',
  28398. '$mod.ti = rtl.boolean;',
  28399. '$mod.ti = rtl.char;',
  28400. '$mod.ti = rtl.double;',
  28401. '$mod.tiInt = rtl.shortint;',
  28402. '$mod.tiInt = rtl.byte;',
  28403. '$mod.tiInt = rtl.smallint;',
  28404. '$mod.tiInt = rtl.word;',
  28405. '$mod.tiInt = rtl.longint;',
  28406. '$mod.tiInt = rtl.longword;',
  28407. '$mod.ti = rtl.jsvalue;',
  28408. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  28409. '$mod.tiSet = $mod.$rtti["TFlags"];',
  28410. '']));
  28411. end;
  28412. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  28413. begin
  28414. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28415. StartProgram(true,[supTypeInfo]);
  28416. Add('{$modeswitch externalclass}');
  28417. Add('type');
  28418. Add(' TStaticArr = array[boolean] of string;');
  28419. Add(' TDynArr = array of string;');
  28420. Add(' TProc = procedure;');
  28421. Add(' TMethod = procedure of object;');
  28422. Add('var');
  28423. Add(' StaticArray: TStaticArr;');
  28424. Add(' tiStaticArray: TTypeInfoStaticArray;');
  28425. Add(' DynArray: TDynArr;');
  28426. Add(' tiDynArray: TTypeInfoDynArray;');
  28427. Add(' ProcVar: TProc;');
  28428. Add(' tiProcVar: TTypeInfoProcVar;');
  28429. Add(' MethodVar: TMethod;');
  28430. Add(' tiMethodVar: TTypeInfoMethodVar;');
  28431. Add('begin');
  28432. Add(' tiStaticArray:=typeinfo(StaticArray);');
  28433. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  28434. Add(' tiDynArray:=typeinfo(DynArray);');
  28435. Add(' tiDynArray:=typeinfo(TDynArr);');
  28436. Add(' tiProcVar:=typeinfo(ProcVar);');
  28437. Add(' tiProcVar:=typeinfo(TProc);');
  28438. Add(' tiMethodVar:=typeinfo(MethodVar);');
  28439. Add(' tiMethodVar:=typeinfo(TMethod);');
  28440. ConvertProgram;
  28441. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  28442. LinesToStr([ // statements
  28443. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  28444. ' dims: [2],',
  28445. ' eltype: rtl.string',
  28446. '});',
  28447. '$mod.$rtti.$DynArray("TDynArr", {',
  28448. ' eltype: rtl.string',
  28449. '});',
  28450. '$mod.$rtti.$ProcVar("TProc", {',
  28451. ' procsig: rtl.newTIProcSig(null)',
  28452. '});',
  28453. '$mod.$rtti.$MethodVar("TMethod", {',
  28454. ' procsig: rtl.newTIProcSig(null),',
  28455. ' methodkind: 0',
  28456. '});',
  28457. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  28458. 'this.tiStaticArray = null;',
  28459. 'this.DynArray = [];',
  28460. 'this.tiDynArray = null;',
  28461. 'this.ProcVar = null;',
  28462. 'this.tiProcVar = null;',
  28463. 'this.MethodVar = null;',
  28464. 'this.tiMethodVar = null;',
  28465. '']),
  28466. LinesToStr([ // $mod.$main
  28467. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  28468. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  28469. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  28470. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  28471. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  28472. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  28473. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  28474. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  28475. '']));
  28476. end;
  28477. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  28478. begin
  28479. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28480. StartProgram(true,[supTypeInfo]);
  28481. Add('{$modeswitch externalclass}');
  28482. Add('type');
  28483. Add(' TRec = record end;');
  28484. // ToDo: ^PRec
  28485. Add(' TObject = class end;');
  28486. Add(' TClass = class of tobject;');
  28487. Add('var');
  28488. Add(' Rec: trec;');
  28489. Add(' tiRecord: ttypeinforecord;');
  28490. Add(' Obj: tobject;');
  28491. Add(' tiClass: ttypeinfoclass;');
  28492. Add(' aClass: tclass;');
  28493. Add(' tiClassRef: ttypeinfoclassref;');
  28494. // ToDo: ^PRec
  28495. Add(' tiPointer: ttypeinfopointer;');
  28496. Add('begin');
  28497. Add(' tirecord:=typeinfo(trec);');
  28498. Add(' tirecord:=typeinfo(trec);');
  28499. Add(' ticlass:=typeinfo(obj);');
  28500. Add(' ticlass:=typeinfo(tobject);');
  28501. Add(' ticlass:=typeinfo(aclass);');
  28502. Add(' ticlassref:=typeinfo(tclass);');
  28503. ConvertProgram;
  28504. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  28505. LinesToStr([ // statements
  28506. 'rtl.recNewT($mod, "TRec", function () {',
  28507. ' this.$eq = function (b) {',
  28508. ' return true;',
  28509. ' };',
  28510. ' this.$assign = function (s) {',
  28511. ' return this;',
  28512. ' };',
  28513. ' $mod.$rtti.$Record("TRec", {});',
  28514. '});',
  28515. 'rtl.createClass($mod, "TObject", null, function () {',
  28516. ' this.$init = function () {',
  28517. ' };',
  28518. ' this.$final = function () {',
  28519. ' };',
  28520. '});',
  28521. '$mod.$rtti.$ClassRef("TClass", {',
  28522. ' instancetype: $mod.$rtti["TObject"]',
  28523. '});',
  28524. 'this.Rec = $mod.TRec.$new();',
  28525. 'this.tiRecord = null;',
  28526. 'this.Obj = null;',
  28527. 'this.tiClass = null;',
  28528. 'this.aClass = null;',
  28529. 'this.tiClassRef = null;',
  28530. 'this.tiPointer = null;',
  28531. '']),
  28532. LinesToStr([ // $mod.$main
  28533. '$mod.tiRecord = $mod.$rtti["TRec"];',
  28534. '$mod.tiRecord = $mod.$rtti["TRec"];',
  28535. '$mod.tiClass = $mod.Obj.$rtti;',
  28536. '$mod.tiClass = $mod.$rtti["TObject"];',
  28537. '$mod.tiClass = $mod.aClass.$rtti;',
  28538. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  28539. '']));
  28540. end;
  28541. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  28542. begin
  28543. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28544. StartProgram(true,[supTypeInfo]);
  28545. Add([
  28546. '{$modeswitch externalclass}',
  28547. 'type',
  28548. ' TClass = class of tobject;',
  28549. ' TObject = class',
  28550. ' function MyClass: TClass;',
  28551. ' class function ClassType: TClass;',
  28552. ' end;',
  28553. 'function TObject.MyClass: TClass;',
  28554. 'var t: TTypeInfoClass;',
  28555. 'begin',
  28556. ' t:=TypeInfo(Self);',
  28557. ' t:=TypeInfo(Result);',
  28558. ' t:=TypeInfo(TObject);',
  28559. 'end;',
  28560. 'class function TObject.ClassType: TClass;',
  28561. 'var t: TTypeInfoClass;',
  28562. 'begin',
  28563. ' t:=TypeInfo(Self);',
  28564. ' t:=TypeInfo(Result);',
  28565. 'end;',
  28566. 'var',
  28567. ' Obj: TObject;',
  28568. ' t: TTypeInfoClass;',
  28569. 'begin',
  28570. ' t:=TypeInfo(TObject.ClassType);',
  28571. ' t:=TypeInfo(Obj.ClassType);',
  28572. ' t:=TypeInfo(Obj.MyClass);',
  28573. '']);
  28574. ConvertProgram;
  28575. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  28576. LinesToStr([ // statements
  28577. '$mod.$rtti.$Class("TObject");',
  28578. '$mod.$rtti.$ClassRef("TClass", {',
  28579. ' instancetype: $mod.$rtti["TObject"]',
  28580. '});',
  28581. 'rtl.createClass($mod, "TObject", null, function () {',
  28582. ' this.$init = function () {',
  28583. ' };',
  28584. ' this.$final = function () {',
  28585. ' };',
  28586. ' this.MyClass = function () {',
  28587. ' var Result = null;',
  28588. ' var t = null;',
  28589. ' t = this.$rtti;',
  28590. ' t = Result.$rtti;',
  28591. ' t = $mod.$rtti["TObject"];',
  28592. ' return Result;',
  28593. ' };',
  28594. ' this.ClassType = function () {',
  28595. ' var Result = null;',
  28596. ' var t = null;',
  28597. ' t = this.$rtti;',
  28598. ' t = Result.$rtti;',
  28599. ' return Result;',
  28600. ' };',
  28601. '});',
  28602. 'this.Obj = null;',
  28603. 'this.t = null;',
  28604. '']),
  28605. LinesToStr([ // $mod.$main
  28606. '$mod.t = $mod.TObject.ClassType().$rtti;',
  28607. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  28608. '$mod.t = $mod.Obj.MyClass().$rtti;',
  28609. '']));
  28610. end;
  28611. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  28612. begin
  28613. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28614. AddModuleWithIntfImplSrc('typinfo.pas',
  28615. LinesToStr([
  28616. '{$modeswitch externalclass}',
  28617. 'type',
  28618. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  28619. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  28620. '']),
  28621. '');
  28622. AddModuleWithIntfImplSrc('unit2.pas',
  28623. LinesToStr([
  28624. 'uses typinfo;',
  28625. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  28626. 'procedure DoPtr(p: PTypeInfo);',
  28627. 'procedure DoInfo(t: TTypeInfo);',
  28628. 'procedure DoInt(t: TTypeInfoInteger);',
  28629. '']),
  28630. LinesToStr([
  28631. 'procedure DoPtr(p: PTypeInfo);',
  28632. 'begin end;',
  28633. 'procedure DoInfo(t: TTypeInfo);',
  28634. 'begin end;',
  28635. 'procedure DoInt(t: TTypeInfoInteger);',
  28636. 'begin end;',
  28637. '']));
  28638. StartUnit(true);
  28639. Add([
  28640. 'interface',
  28641. 'uses unit2;', // does not use unit typinfo
  28642. 'implementation',
  28643. 'var',
  28644. ' i: byte;',
  28645. ' p: pointer;',
  28646. ' t: PTypeInfo;',
  28647. 'initialization',
  28648. ' p:=typeinfo(i);',
  28649. ' t:=typeinfo(i);',
  28650. ' if p=t then ;',
  28651. ' if p=typeinfo(i) then ;',
  28652. ' if typeinfo(i)=p then ;',
  28653. ' if t=typeinfo(i) then ;',
  28654. ' if typeinfo(i)=t then ;',
  28655. ' DoPtr(p);',
  28656. ' DoPtr(t);',
  28657. ' DoPtr(typeinfo(i));',
  28658. ' DoInfo(p);',
  28659. ' DoInfo(t);',
  28660. ' DoInfo(typeinfo(i));',
  28661. ' DoInt(typeinfo(i));',
  28662. '']);
  28663. ConvertUnit;
  28664. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  28665. LinesToStr([ // statements
  28666. 'var $impl = $mod.$impl;',
  28667. '']),
  28668. LinesToStr([ // this.$init
  28669. '$impl.p = rtl.byte;',
  28670. '$impl.t = rtl.byte;',
  28671. 'if ($impl.p === $impl.t) ;',
  28672. 'if ($impl.p === rtl.byte) ;',
  28673. 'if (rtl.byte === $impl.p) ;',
  28674. 'if ($impl.t === rtl.byte) ;',
  28675. 'if (rtl.byte === $impl.t) ;',
  28676. 'pas.unit2.DoPtr($impl.p);',
  28677. 'pas.unit2.DoPtr($impl.t);',
  28678. 'pas.unit2.DoPtr(rtl.byte);',
  28679. 'pas.unit2.DoInfo($impl.p);',
  28680. 'pas.unit2.DoInfo($impl.t);',
  28681. 'pas.unit2.DoInfo(rtl.byte);',
  28682. 'pas.unit2.DoInt(rtl.byte);',
  28683. '']),
  28684. LinesToStr([ // implementation
  28685. '$impl.i = 0;',
  28686. '$impl.p = null;',
  28687. '$impl.t = null;',
  28688. '']) );
  28689. end;
  28690. procedure TTestModule.TestRTTI_Interface_Corba;
  28691. begin
  28692. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28693. StartProgram(true,[supTypeInfo]);
  28694. Add([
  28695. '{$interfaces corba}',
  28696. '{$modeswitch externalclass}',
  28697. 'type',
  28698. ' IUnknown = interface',
  28699. ' end;',
  28700. ' IBird = interface',
  28701. ' function GetItem: longint;',
  28702. ' procedure SetItem(Value: longint);',
  28703. ' property Item: longint read GetItem write SetItem;',
  28704. ' end;',
  28705. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  28706. 'var',
  28707. ' i: IBird;',
  28708. ' t: TTypeInfoInterface;',
  28709. 'begin',
  28710. ' t:=TypeInfo(IBird);',
  28711. ' t:=TypeInfo(i);',
  28712. ' DoIt(t);',
  28713. ' DoIt(TypeInfo(IBird));',
  28714. '']);
  28715. ConvertProgram;
  28716. CheckSource('TestRTTI_Interface_Corba',
  28717. LinesToStr([ // statements
  28718. 'rtl.createInterface(',
  28719. ' $mod,',
  28720. ' "IUnknown",',
  28721. ' "{B92D5841-758A-322B-B800-000000000000}",',
  28722. ' [],',
  28723. ' null,',
  28724. ' function () {',
  28725. ' }',
  28726. ');',
  28727. 'rtl.createInterface(',
  28728. ' $mod,',
  28729. ' "IBird",',
  28730. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  28731. ' ["GetItem", "SetItem"],',
  28732. ' null,',
  28733. ' function () {',
  28734. ' var $r = this.$rtti;',
  28735. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28736. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  28737. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  28738. ' }',
  28739. ');',
  28740. 'this.DoIt = function (t) {',
  28741. '}; ',
  28742. 'this.i = null;',
  28743. 'this.t = null;',
  28744. '']),
  28745. LinesToStr([ // $mod.$main
  28746. '$mod.t = $mod.$rtti["IBird"];',
  28747. '$mod.t = $mod.i.$rtti;',
  28748. '$mod.DoIt($mod.t);',
  28749. '$mod.DoIt($mod.$rtti["IBird"]);',
  28750. '']));
  28751. end;
  28752. procedure TTestModule.TestRTTI_Interface_COM;
  28753. begin
  28754. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28755. StartProgram(true,[supTypeInfo]);
  28756. Add([
  28757. '{$interfaces com}',
  28758. '{$modeswitch externalclass}',
  28759. 'type',
  28760. ' TGuid = record end;',
  28761. ' integer = longint;',
  28762. ' IUnknown = interface',
  28763. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  28764. ' function _AddRef: Integer;',
  28765. ' function _Release: Integer;',
  28766. ' end;',
  28767. ' IBird = interface',
  28768. ' function GetItem: longint;',
  28769. ' procedure SetItem(Value: longint);',
  28770. ' property Item: longint read GetItem write SetItem;',
  28771. ' end;',
  28772. 'var',
  28773. ' i: IBird;',
  28774. ' t: TTypeInfoInterface;',
  28775. 'begin',
  28776. ' t:=TypeInfo(IBird);',
  28777. ' t:=TypeInfo(i);',
  28778. '']);
  28779. ConvertProgram;
  28780. CheckSource('TestRTTI_Interface_COM',
  28781. LinesToStr([ // statements
  28782. 'rtl.recNewT($mod, "TGuid", function () {',
  28783. ' this.$eq = function (b) {',
  28784. ' return true;',
  28785. ' };',
  28786. ' this.$assign = function (s) {',
  28787. ' return this;',
  28788. ' };',
  28789. ' $mod.$rtti.$Record("TGuid", {});',
  28790. '});',
  28791. 'rtl.createInterface(',
  28792. ' $mod,',
  28793. ' "IUnknown",',
  28794. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  28795. ' ["QueryInterface", "_AddRef", "_Release"],',
  28796. ' null,',
  28797. ' function () {',
  28798. ' this.$kind = "com";',
  28799. ' var $r = this.$rtti;',
  28800. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  28801. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  28802. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  28803. ' }',
  28804. ');',
  28805. 'rtl.createInterface(',
  28806. ' $mod,',
  28807. ' "IBird",',
  28808. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  28809. ' ["GetItem", "SetItem"],',
  28810. ' $mod.IUnknown,',
  28811. ' function () {',
  28812. ' var $r = this.$rtti;',
  28813. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28814. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  28815. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  28816. ' }',
  28817. ');',
  28818. 'this.i = null;',
  28819. 'this.t = null;',
  28820. '']),
  28821. LinesToStr([ // $mod.$main
  28822. '$mod.t = $mod.$rtti["IBird"];',
  28823. '$mod.t = $mod.i.$rtti;',
  28824. '']));
  28825. end;
  28826. procedure TTestModule.TestRTTI_ClassHelper;
  28827. begin
  28828. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28829. StartProgram(true,[supTypeInfo]);
  28830. Add([
  28831. '{$interfaces com}',
  28832. '{$modeswitch externalclass}',
  28833. 'type',
  28834. ' TObject = class',
  28835. ' end;',
  28836. ' THelper = class helper for TObject',
  28837. ' published',
  28838. ' function GetItem: longint;',
  28839. ' property Item: longint read GetItem;',
  28840. ' end;',
  28841. 'function THelper.GetItem: longint;',
  28842. 'begin',
  28843. 'end;',
  28844. 'var',
  28845. ' t: TTypeInfoHelper;',
  28846. 'begin',
  28847. ' t:=TypeInfo(THelper);',
  28848. '']);
  28849. ConvertProgram;
  28850. CheckSource('TestRTTI_ClassHelper',
  28851. LinesToStr([ // statements
  28852. 'rtl.createClass($mod, "TObject", null, function () {',
  28853. ' this.$init = function () {',
  28854. ' };',
  28855. ' this.$final = function () {',
  28856. ' };',
  28857. '});',
  28858. 'rtl.createHelper($mod, "THelper", null, function () {',
  28859. ' this.GetItem = function () {',
  28860. ' var Result = 0;',
  28861. ' return Result;',
  28862. ' };',
  28863. ' var $r = this.$rtti;',
  28864. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  28865. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  28866. '});',
  28867. 'this.t = null;',
  28868. '']),
  28869. LinesToStr([ // $mod.$main
  28870. '$mod.t = $mod.$rtti["THelper"];',
  28871. '']));
  28872. end;
  28873. procedure TTestModule.TestRTTI_ExternalClass;
  28874. begin
  28875. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28876. StartProgram(true,[supTypeInfo]);
  28877. Add([
  28878. '{$modeswitch externalclass}',
  28879. 'type',
  28880. ' TJSObject = class external name ''Object''',
  28881. ' end;',
  28882. ' TJSArray = class external name ''Array'' (TJSObject)',
  28883. ' end;',
  28884. 'var',
  28885. ' p: Pointer;',
  28886. ' tc: TTypeInfoExtClass;',
  28887. 'begin',
  28888. ' p:=typeinfo(TJSArray);']);
  28889. ConvertProgram;
  28890. CheckSource('TestRTTI_ExternalClass',
  28891. LinesToStr([ // statements
  28892. '$mod.$rtti.$ExtClass("TJSObject", {',
  28893. ' jsclass: "Object"',
  28894. '});',
  28895. '$mod.$rtti.$ExtClass("TJSArray", {',
  28896. ' ancestor: $mod.$rtti["TJSObject"],',
  28897. ' jsclass: "Array"',
  28898. '});',
  28899. 'this.p = null;',
  28900. 'this.tc = null;',
  28901. '']),
  28902. LinesToStr([ // $mod.$main
  28903. '$mod.p = $mod.$rtti["TJSArray"];',
  28904. '']));
  28905. end;
  28906. procedure TTestModule.TestResourcestringProgram;
  28907. begin
  28908. StartProgram(false);
  28909. Add([
  28910. 'const Bar = ''bar'';',
  28911. 'resourcestring',
  28912. ' Red = ''red'';',
  28913. ' Foobar = ''fOo''+bar;',
  28914. 'var s: string;',
  28915. ' c: char;',
  28916. 'begin',
  28917. ' s:=red;',
  28918. ' s:=test1.red;',
  28919. ' c:=red[1];',
  28920. ' c:=test1.red[2];',
  28921. ' if red=foobar then ;',
  28922. ' if red[3]=red[4] then ;']);
  28923. ConvertProgram;
  28924. CheckSource('TestResourcestringProgram',
  28925. LinesToStr([ // statements
  28926. 'this.Bar = "bar";',
  28927. 'this.s = "";',
  28928. 'this.c = "";',
  28929. '$mod.$resourcestrings = {',
  28930. ' Red: {',
  28931. ' org: "red"',
  28932. ' },',
  28933. ' Foobar: {',
  28934. ' org: "fOobar"',
  28935. ' }',
  28936. '};',
  28937. '']),
  28938. LinesToStr([ // $mod.$main
  28939. '$mod.s = rtl.getResStr(pas.program, "Red");',
  28940. '$mod.s = rtl.getResStr(pas.program, "Red");',
  28941. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  28942. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  28943. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  28944. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  28945. '']));
  28946. end;
  28947. procedure TTestModule.TestResourcestringUnit;
  28948. begin
  28949. StartUnit(false);
  28950. Add([
  28951. 'interface',
  28952. 'const Red = ''rEd'';',
  28953. 'resourcestring',
  28954. ' Blue = ''blue'';',
  28955. ' NotRed = ''not''+Red;',
  28956. 'var s: string;',
  28957. 'implementation',
  28958. 'resourcestring',
  28959. ' ImplGreen = ''green'';',
  28960. 'initialization',
  28961. ' s:=blue+ImplGreen;',
  28962. ' s:=test1.blue+test1.implgreen;',
  28963. ' s:=blue[1]+implgreen[2];']);
  28964. ConvertUnit;
  28965. CheckSource('TestResourcestringUnit',
  28966. LinesToStr([ // statements
  28967. 'this.Red = "rEd";',
  28968. 'this.s = "";',
  28969. '$mod.$resourcestrings = {',
  28970. ' Blue: {',
  28971. ' org: "blue"',
  28972. ' },',
  28973. ' NotRed: {',
  28974. ' org: "notrEd"',
  28975. ' },',
  28976. ' ImplGreen: {',
  28977. ' org: "green"',
  28978. ' }',
  28979. '};',
  28980. '']),
  28981. LinesToStr([ // $mod.$main
  28982. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28983. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  28984. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  28985. '']));
  28986. end;
  28987. procedure TTestModule.TestResourcestringImplementation;
  28988. begin
  28989. StartUnit(false);
  28990. Add([
  28991. 'interface',
  28992. 'implementation',
  28993. 'resourcestring',
  28994. ' ImplRed = ''red'';']);
  28995. ConvertUnit;
  28996. CheckSource('TestResourcestringImplementation',
  28997. LinesToStr([ // intf statements
  28998. 'var $impl = $mod.$impl;']),
  28999. LinesToStr([ // $mod.$init
  29000. '']),
  29001. LinesToStr([ // impl statements
  29002. '$mod.$resourcestrings = {',
  29003. ' ImplRed: {',
  29004. ' org: "red"',
  29005. ' }',
  29006. '};',
  29007. '']));
  29008. end;
  29009. procedure TTestModule.TestAttributes_Members;
  29010. begin
  29011. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29012. StartProgram(false);
  29013. Add([
  29014. '{$modeswitch PrefixedAttributes}',
  29015. 'type',
  29016. ' TObject = class',
  29017. ' constructor Create;',
  29018. ' end;',
  29019. ' TCustomAttribute = class',
  29020. ' constructor Create(Id: word);',
  29021. ' end;',
  29022. ' [Missing]',
  29023. ' TBird = class',
  29024. ' published',
  29025. ' [Tcustom]',
  29026. ' FField: word;',
  29027. ' [tcustom(14)]',
  29028. ' property Size: word read FField;',
  29029. ' [Tcustom(15)]',
  29030. ' procedure Fly; virtual; abstract;',
  29031. ' end;',
  29032. ' TRec = record',
  29033. ' [Tcustom,tcustom(14)]',
  29034. ' Size: word;',
  29035. ' end;',
  29036. 'constructor TObject.Create; begin end;',
  29037. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  29038. 'begin',
  29039. '']);
  29040. ConvertProgram;
  29041. CheckSource('TestAttributes_Members',
  29042. LinesToStr([ // statements
  29043. 'rtl.createClass($mod, "TObject", null, function () {',
  29044. ' this.$init = function () {',
  29045. ' };',
  29046. ' this.$final = function () {',
  29047. ' };',
  29048. ' this.Create = function () {',
  29049. ' return this;',
  29050. ' };',
  29051. '});',
  29052. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  29053. ' this.Create$1 = function (Id) {',
  29054. ' return this;',
  29055. ' };',
  29056. '});',
  29057. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  29058. ' this.$init = function () {',
  29059. ' $mod.TObject.$init.call(this);',
  29060. ' this.FField = 0;',
  29061. ' };',
  29062. ' var $r = this.$rtti;',
  29063. ' $r.addField("FField", rtl.word, {',
  29064. ' attr: [$mod.TCustomAttribute, "Create"]',
  29065. ' });',
  29066. ' $r.addProperty(',
  29067. ' "Size",',
  29068. ' 0,',
  29069. ' rtl.word,',
  29070. ' "FField",',
  29071. ' "",',
  29072. ' {',
  29073. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  29074. ' }',
  29075. ' );',
  29076. ' $r.addMethod("Fly", 0, null, null, {',
  29077. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  29078. ' });',
  29079. '});',
  29080. 'rtl.recNewT($mod, "TRec", function () {',
  29081. ' this.Size = 0;',
  29082. ' this.$eq = function (b) {',
  29083. ' return this.Size === b.Size;',
  29084. ' };',
  29085. ' this.$assign = function (s) {',
  29086. ' this.Size = s.Size;',
  29087. ' return this;',
  29088. ' };',
  29089. ' var $r = $mod.$rtti.$Record("TRec", {});',
  29090. ' $r.addField("Size", rtl.word, {',
  29091. ' attr: [',
  29092. ' $mod.TCustomAttribute,',
  29093. ' "Create",',
  29094. ' $mod.TCustomAttribute,',
  29095. ' "Create$1",',
  29096. ' [14]',
  29097. ' ]',
  29098. ' });',
  29099. '});',
  29100. '']),
  29101. LinesToStr([ // $mod.$main
  29102. '']));
  29103. end;
  29104. procedure TTestModule.TestAttributes_Types;
  29105. begin
  29106. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29107. StartProgram(false);
  29108. Add([
  29109. '{$modeswitch PrefixedAttributes}',
  29110. 'type',
  29111. ' TObject = class',
  29112. ' constructor Create(Id: word);',
  29113. ' end;',
  29114. ' TCustomAttribute = class',
  29115. ' end;',
  29116. ' [TCustom(1)]',
  29117. ' TMyClass = class',
  29118. ' end;',
  29119. ' [TCustom(2)]',
  29120. ' TRec = record',
  29121. ' end;',
  29122. ' [TCustom(3)]',
  29123. ' TInt = type word;',
  29124. 'constructor TObject.Create(Id: word);',
  29125. 'begin',
  29126. 'end;',
  29127. 'var p: pointer;',
  29128. 'begin',
  29129. ' p:=typeinfo(TMyClass);',
  29130. ' p:=typeinfo(TRec);',
  29131. ' p:=typeinfo(TInt);',
  29132. '']);
  29133. ConvertProgram;
  29134. CheckSource('TestAttributes_Types',
  29135. LinesToStr([ // statements
  29136. 'rtl.createClass($mod, "TObject", null, function () {',
  29137. ' this.$init = function () {',
  29138. ' };',
  29139. ' this.$final = function () {',
  29140. ' };',
  29141. ' this.Create = function (Id) {',
  29142. ' return this;',
  29143. ' };',
  29144. '});',
  29145. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  29146. '});',
  29147. 'rtl.createClass($mod, "TMyClass", $mod.TObject, function () {',
  29148. ' var $r = this.$rtti;',
  29149. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  29150. '});',
  29151. 'rtl.recNewT($mod, "TRec", function () {',
  29152. ' this.$eq = function (b) {',
  29153. ' return true;',
  29154. ' };',
  29155. ' this.$assign = function (s) {',
  29156. ' return this;',
  29157. ' };',
  29158. ' $mod.$rtti.$Record("TRec", {',
  29159. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  29160. ' });',
  29161. '});',
  29162. '$mod.$rtti.$inherited("TInt", rtl.word, {',
  29163. ' attr: [$mod.TCustomAttribute, "Create", [3]]',
  29164. '});',
  29165. 'this.p = null;',
  29166. '']),
  29167. LinesToStr([ // $mod.$main
  29168. '$mod.p = $mod.$rtti["TMyClass"];',
  29169. '$mod.p = $mod.$rtti["TRec"];',
  29170. '$mod.p = $mod.$rtti["TInt"];',
  29171. '']));
  29172. end;
  29173. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  29174. begin
  29175. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29176. StartProgram(false);
  29177. Add([
  29178. '{$modeswitch PrefixedAttributes}',
  29179. 'type',
  29180. ' TObject = class',
  29181. ' constructor Create;',
  29182. ' end;',
  29183. ' TCustomAttribute = class',
  29184. ' end;',
  29185. ' THelper = class helper for TCustomAttribute',
  29186. ' constructor Create(Id: word);',
  29187. ' end;',
  29188. ' [TCustom(3)]',
  29189. ' TMyInt = word;',
  29190. 'constructor TObject.Create; begin end;',
  29191. 'constructor THelper.Create(Id: word); begin end;',
  29192. 'begin',
  29193. ' if typeinfo(TMyInt)=nil then ;']);
  29194. ConvertProgram;
  29195. end;
  29196. procedure TTestModule.TestAssert;
  29197. begin
  29198. StartProgram(false);
  29199. Add([
  29200. 'procedure DoIt;',
  29201. 'var',
  29202. ' b: boolean;',
  29203. ' s: string;',
  29204. 'begin',
  29205. ' {$Assertions on}',
  29206. ' Assert(b);',
  29207. 'end;',
  29208. 'begin',
  29209. ' DoIt;',
  29210. '']);
  29211. ConvertProgram;
  29212. CheckSource('TestAssert',
  29213. LinesToStr([ // statements
  29214. 'this.DoIt = function () {',
  29215. ' var b = false;',
  29216. ' var s = "";',
  29217. ' if (!b) throw "assert failed";',
  29218. '};',
  29219. '']),
  29220. LinesToStr([ // $mod.$main
  29221. '$mod.DoIt();',
  29222. '']));
  29223. end;
  29224. procedure TTestModule.TestAssert_SysUtils;
  29225. begin
  29226. AddModuleWithIntfImplSrc('SysUtils.pas',
  29227. LinesToStr([
  29228. 'type',
  29229. ' TObject = class',
  29230. ' constructor Create;',
  29231. ' end;',
  29232. ' EAssertionFailed = class',
  29233. ' constructor Create(s: string);',
  29234. ' end;',
  29235. '']),
  29236. LinesToStr([
  29237. 'constructor TObject.Create;',
  29238. 'begin end;',
  29239. 'constructor EAssertionFailed.Create(s: string);',
  29240. 'begin end;',
  29241. '']) );
  29242. StartProgram(true);
  29243. Add([
  29244. 'uses sysutils;',
  29245. 'procedure DoIt;',
  29246. 'var',
  29247. ' b: boolean;',
  29248. ' s: string;',
  29249. 'begin',
  29250. ' {$Assertions on}',
  29251. ' Assert(b);',
  29252. ' Assert(b,''msg'');',
  29253. 'end;',
  29254. 'begin',
  29255. ' DoIt;',
  29256. '']);
  29257. ConvertProgram;
  29258. CheckSource('TestAssert_SysUtils',
  29259. LinesToStr([ // statements
  29260. 'this.DoIt = function () {',
  29261. ' var b = false;',
  29262. ' var s = "";',
  29263. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  29264. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  29265. '};',
  29266. '']),
  29267. LinesToStr([ // $mod.$main
  29268. '$mod.DoIt();',
  29269. '']));
  29270. end;
  29271. procedure TTestModule.TestObjectChecks;
  29272. begin
  29273. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  29274. StartProgram(false);
  29275. Add([
  29276. 'type',
  29277. ' TObject = class',
  29278. ' procedure DoIt;',
  29279. ' end;',
  29280. ' TClass = class of tobject;',
  29281. ' TBird = class',
  29282. ' end;',
  29283. ' TBirdClass = class of TBird;',
  29284. 'var',
  29285. ' o : TObject;',
  29286. ' c: TClass;',
  29287. ' b: TBird;',
  29288. ' bc: TBirdClass;',
  29289. 'procedure TObject.DoIt;',
  29290. 'begin',
  29291. ' b:=TBird(o);',
  29292. 'end;',
  29293. 'begin',
  29294. ' o.DoIt;',
  29295. ' b:=TBird(o);',
  29296. ' bc:=TBirdClass(c);',
  29297. '']);
  29298. ConvertProgram;
  29299. CheckSource('TestCheckMethodCall',
  29300. LinesToStr([ // statements
  29301. 'rtl.createClass($mod, "TObject", null, function () {',
  29302. ' this.$init = function () {',
  29303. ' };',
  29304. ' this.$final = function () {',
  29305. ' };',
  29306. ' this.DoIt = function () {',
  29307. ' rtl.checkMethodCall(this,$mod.TObject);',
  29308. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  29309. ' };',
  29310. '});',
  29311. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  29312. '});',
  29313. 'this.o = null;',
  29314. 'this.c = null;',
  29315. 'this.b = null;',
  29316. 'this.bc = null;',
  29317. '']),
  29318. LinesToStr([ // $mod.$main
  29319. '$mod.o.DoIt();',
  29320. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  29321. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  29322. '']));
  29323. end;
  29324. procedure TTestModule.TestOverflowChecks_Int;
  29325. begin
  29326. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  29327. StartProgram(false);
  29328. Add([
  29329. 'procedure DoIt;',
  29330. 'var',
  29331. ' b: byte;',
  29332. ' n: nativeint;',
  29333. ' u: nativeuint;',
  29334. ' c: currency;',
  29335. 'begin',
  29336. ' n:=n+n;',
  29337. ' n:=n-n;',
  29338. ' n:=n+b;',
  29339. ' n:=b-n;',
  29340. ' n:=n*n;',
  29341. ' n:=n*u;',
  29342. ' c:=c+b;',
  29343. ' c:=b+c;',
  29344. ' c:=c*b;',
  29345. ' c:=b*c;',
  29346. 'end;',
  29347. 'begin',
  29348. '']);
  29349. ConvertProgram;
  29350. CheckSource('TestOverflowChecks_Int',
  29351. LinesToStr([ // statements
  29352. 'this.DoIt = function () {',
  29353. ' var b = 0;',
  29354. ' var n = 0;',
  29355. ' var u = 0;',
  29356. ' var c = 0;',
  29357. ' n = rtl.oc(n + n);',
  29358. ' n = rtl.oc(n - n);',
  29359. ' n = rtl.oc(n + b);',
  29360. ' n = rtl.oc(b - n);',
  29361. ' n = rtl.oc(n * n);',
  29362. ' n = rtl.oc(n * u);',
  29363. ' c = rtl.oc(c + (b * 10000));',
  29364. ' c = rtl.oc((b * 10000) + c);',
  29365. ' c = rtl.oc(c * b);',
  29366. ' c = rtl.oc(b * c);',
  29367. '};',
  29368. '']),
  29369. LinesToStr([ // $mod.$main
  29370. '']));
  29371. end;
  29372. procedure TTestModule.TestRangeChecks_AssignInt;
  29373. begin
  29374. Scanner.Options:=Scanner.Options+[po_CAssignments];
  29375. StartProgram(false);
  29376. Add([
  29377. '{$R+}',
  29378. 'var',
  29379. ' b: byte = 2;',
  29380. ' w: word = 3;',
  29381. 'procedure DoIt(p: byte);',
  29382. 'begin',
  29383. ' b:=w;',
  29384. ' b+=w;',
  29385. ' b:=1;',
  29386. 'end;',
  29387. '{$R-}',
  29388. 'procedure DoSome;',
  29389. 'begin',
  29390. ' DoIt(w);',
  29391. ' b:=w;',
  29392. ' b:=2;',
  29393. 'end;',
  29394. 'begin',
  29395. '{$R+}',
  29396. '']);
  29397. ConvertProgram;
  29398. CheckSource('TestRangeChecks_AssignInt',
  29399. LinesToStr([ // statements
  29400. 'this.b = 2;',
  29401. 'this.w = 3;',
  29402. 'this.DoIt = function (p) {',
  29403. ' rtl.rc(p, 0, 255);',
  29404. ' $mod.b = rtl.rc($mod.w,0,255);',
  29405. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  29406. ' $mod.b = 1;',
  29407. '};',
  29408. 'this.DoSome = function () {',
  29409. ' $mod.DoIt($mod.w);',
  29410. ' $mod.b = $mod.w;',
  29411. ' $mod.b = 2;',
  29412. '};',
  29413. '']),
  29414. LinesToStr([ // $mod.$main
  29415. '']));
  29416. end;
  29417. procedure TTestModule.TestRangeChecks_AssignIntRange;
  29418. begin
  29419. Scanner.Options:=Scanner.Options+[po_CAssignments];
  29420. StartProgram(false);
  29421. Add([
  29422. '{$R+}',
  29423. 'type Ten = 1..10;',
  29424. 'var',
  29425. ' b: Ten = 2;',
  29426. ' w: Ten = 3;',
  29427. 'procedure DoIt(p: Ten);',
  29428. 'begin',
  29429. ' b:=w;',
  29430. ' b+=w;',
  29431. ' b:=1;',
  29432. 'end;',
  29433. '{$R-}',
  29434. 'procedure DoSome;',
  29435. 'begin',
  29436. ' DoIt(w);',
  29437. ' b:=w;',
  29438. ' b:=2;',
  29439. 'end;',
  29440. 'begin',
  29441. '{$R+}',
  29442. '']);
  29443. ConvertProgram;
  29444. CheckSource('TestRangeChecks_AssignIntRange',
  29445. LinesToStr([ // statements
  29446. 'this.b = 2;',
  29447. 'this.w = 3;',
  29448. 'this.DoIt = function (p) {',
  29449. ' rtl.rc(p, 1, 10);',
  29450. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  29451. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  29452. ' $mod.b = 1;',
  29453. '};',
  29454. 'this.DoSome = function () {',
  29455. ' $mod.DoIt($mod.w);',
  29456. ' $mod.b = $mod.w;',
  29457. ' $mod.b = 2;',
  29458. '};',
  29459. '']),
  29460. LinesToStr([ // $mod.$main
  29461. '']));
  29462. end;
  29463. procedure TTestModule.TestRangeChecks_AssignEnum;
  29464. begin
  29465. StartProgram(false);
  29466. Add([
  29467. '{$R+}',
  29468. 'type TEnum = (red,green);',
  29469. 'var',
  29470. ' e: TEnum = red;',
  29471. 'procedure DoIt(p: TEnum);',
  29472. 'begin',
  29473. ' e:=p;',
  29474. ' p:=TEnum(0);',
  29475. ' p:=succ(e);',
  29476. 'end;',
  29477. '{$R-}',
  29478. 'procedure DoSome;',
  29479. 'begin',
  29480. ' DoIt(e);',
  29481. ' e:=TEnum(1);',
  29482. ' e:=pred(e);',
  29483. 'end;',
  29484. 'begin',
  29485. '{$R+}',
  29486. '']);
  29487. ConvertProgram;
  29488. CheckSource('TestRangeChecks_AssignEnum',
  29489. LinesToStr([ // statements
  29490. 'this.TEnum = {',
  29491. ' "0": "red",',
  29492. ' red: 0,',
  29493. ' "1": "green",',
  29494. ' green: 1',
  29495. '};',
  29496. 'this.e = $mod.TEnum.red;',
  29497. 'this.DoIt = function (p) {',
  29498. ' rtl.rc(p, 0, 1);',
  29499. ' $mod.e = rtl.rc(p, 0, 1);',
  29500. ' p = 0;',
  29501. ' p = rtl.rc($mod.e + 1, 0, 1);',
  29502. '};',
  29503. 'this.DoSome = function () {',
  29504. ' $mod.DoIt($mod.e);',
  29505. ' $mod.e = 1;',
  29506. ' $mod.e = $mod.e - 1;',
  29507. '};',
  29508. '']),
  29509. LinesToStr([ // $mod.$main
  29510. '']));
  29511. end;
  29512. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  29513. begin
  29514. StartProgram(false);
  29515. Add([
  29516. '{$R+}',
  29517. 'type',
  29518. ' TEnum = (red,green);',
  29519. ' TEnumRg = red..green;',
  29520. 'var',
  29521. ' e: TEnumRg = red;',
  29522. 'procedure DoIt(p: TEnumRg);',
  29523. 'begin',
  29524. ' e:=p;',
  29525. ' p:=TEnumRg(0);',
  29526. ' p:=succ(e);',
  29527. 'end;',
  29528. '{$R-}',
  29529. 'procedure DoSome;',
  29530. 'begin',
  29531. ' DoIt(e);',
  29532. ' e:=TEnum(1);',
  29533. ' e:=pred(e);',
  29534. 'end;',
  29535. 'begin',
  29536. '{$R+}',
  29537. '']);
  29538. ConvertProgram;
  29539. CheckSource('TestRangeChecks_AssignEnumRange',
  29540. LinesToStr([ // statements
  29541. 'this.TEnum = {',
  29542. ' "0": "red",',
  29543. ' red: 0,',
  29544. ' "1": "green",',
  29545. ' green: 1',
  29546. '};',
  29547. 'this.e = $mod.TEnum.red;',
  29548. 'this.DoIt = function (p) {',
  29549. ' rtl.rc(p, 0, 1);',
  29550. ' $mod.e = rtl.rc(p, 0, 1);',
  29551. ' p = 0;',
  29552. ' p = rtl.rc($mod.e + 1, 0, 1);',
  29553. '};',
  29554. 'this.DoSome = function () {',
  29555. ' $mod.DoIt($mod.e);',
  29556. ' $mod.e = 1;',
  29557. ' $mod.e = $mod.e - 1;',
  29558. '};',
  29559. '']),
  29560. LinesToStr([ // $mod.$main
  29561. '']));
  29562. end;
  29563. procedure TTestModule.TestRangeChecks_AssignChar;
  29564. begin
  29565. StartProgram(false);
  29566. Add([
  29567. '{$R+}',
  29568. 'type',
  29569. ' TLetter = char;',
  29570. 'var',
  29571. ' b: TLetter = ''2'';',
  29572. ' w: TLetter = ''3'';',
  29573. 'procedure DoIt(p: TLetter);',
  29574. 'begin',
  29575. ' b:=w;',
  29576. ' b:=''1'';',
  29577. 'end;',
  29578. '{$R-}',
  29579. 'procedure DoSome;',
  29580. 'begin',
  29581. ' DoIt(w);',
  29582. ' b:=w;',
  29583. ' b:=''2'';',
  29584. 'end;',
  29585. 'begin',
  29586. '{$R+}',
  29587. '']);
  29588. ConvertProgram;
  29589. CheckSource('TestRangeChecks_AssignChar',
  29590. LinesToStr([ // statements
  29591. 'this.b = "2";',
  29592. 'this.w = "3";',
  29593. 'this.DoIt = function (p) {',
  29594. ' rtl.rcc(p, 0, 65535);',
  29595. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  29596. ' $mod.b = "1";',
  29597. '};',
  29598. 'this.DoSome = function () {',
  29599. ' $mod.DoIt($mod.w);',
  29600. ' $mod.b = $mod.w;',
  29601. ' $mod.b = "2";',
  29602. '};',
  29603. '']),
  29604. LinesToStr([ // $mod.$main
  29605. '']));
  29606. end;
  29607. procedure TTestModule.TestRangeChecks_AssignCharRange;
  29608. begin
  29609. StartProgram(false);
  29610. Add([
  29611. '{$R+}',
  29612. 'type TDigit = ''0''..''9'';',
  29613. 'var',
  29614. ' b: TDigit = ''2'';',
  29615. ' w: TDigit = ''3'';',
  29616. 'procedure DoIt(p: TDigit);',
  29617. 'begin',
  29618. ' b:=w;',
  29619. ' b:=''1'';',
  29620. 'end;',
  29621. '{$R-}',
  29622. 'procedure DoSome;',
  29623. 'begin',
  29624. ' DoIt(w);',
  29625. ' b:=w;',
  29626. ' b:=''2'';',
  29627. 'end;',
  29628. 'begin',
  29629. '{$R+}',
  29630. '']);
  29631. ConvertProgram;
  29632. CheckSource('TestRangeChecks_AssignCharRange',
  29633. LinesToStr([ // statements
  29634. 'this.b = "2";',
  29635. 'this.w = "3";',
  29636. 'this.DoIt = function (p) {',
  29637. ' rtl.rcc(p, 48, 57);',
  29638. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  29639. ' $mod.b = "1";',
  29640. '};',
  29641. 'this.DoSome = function () {',
  29642. ' $mod.DoIt($mod.w);',
  29643. ' $mod.b = $mod.w;',
  29644. ' $mod.b = "2";',
  29645. '};',
  29646. '']),
  29647. LinesToStr([ // $mod.$main
  29648. '']));
  29649. end;
  29650. procedure TTestModule.TestRangeChecks_ArrayIndex;
  29651. begin
  29652. StartProgram(false);
  29653. Add([
  29654. '{$R+}',
  29655. 'type',
  29656. ' Ten = 1..10;',
  29657. ' TArr = array of Ten;',
  29658. ' TArrArr = array of TArr;',
  29659. ' TArrByte = array[byte] of Ten;',
  29660. ' TArrChar = array[''0''..''9''] of Ten;',
  29661. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  29662. ' TObject = class',
  29663. ' A: TArr;',
  29664. ' end;',
  29665. 'procedure DoIt;',
  29666. 'var',
  29667. ' Arr: TArr;',
  29668. ' ArrArr: TArrArr;',
  29669. ' ArrByte: TArrByte;',
  29670. ' ArrChar: TArrChar;',
  29671. ' ArrByteChar: TArrByteChar;',
  29672. ' i: Ten;',
  29673. ' c: char;',
  29674. ' o: tobject;',
  29675. 'begin',
  29676. ' i:=Arr[1];',
  29677. ' i:=ArrByteChar[1,''2''];',
  29678. ' Arr[1]:=Arr[1];',
  29679. ' Arr[i]:=Arr[i];',
  29680. ' ArrByte[3]:=ArrByte[3];',
  29681. ' ArrByte[i]:=ArrByte[i];',
  29682. ' ArrChar[''5'']:=ArrChar[''5''];',
  29683. ' ArrChar[c]:=ArrChar[c];',
  29684. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  29685. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  29686. ' o.a[i]:=o.a[i];',
  29687. 'end;',
  29688. 'begin',
  29689. '']);
  29690. ConvertProgram;
  29691. CheckSource('TestRangeChecks_ArrayIndex',
  29692. LinesToStr([ // statements
  29693. 'rtl.createClass($mod, "TObject", null, function () {',
  29694. ' this.$init = function () {',
  29695. ' this.A = [];',
  29696. ' };',
  29697. ' this.$final = function () {',
  29698. ' this.A = undefined;',
  29699. ' };',
  29700. '});',
  29701. 'this.DoIt = function () {',
  29702. ' var Arr = [];',
  29703. ' var ArrArr = [];',
  29704. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  29705. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  29706. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  29707. ' var i = 0;',
  29708. ' var c = "";',
  29709. ' var o = null;',
  29710. ' i = rtl.rc(Arr[1], 1, 10);',
  29711. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  29712. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  29713. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  29714. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  29715. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  29716. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  29717. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  29718. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  29719. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  29720. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  29721. '};',
  29722. '']),
  29723. LinesToStr([ // $mod.$main
  29724. '']));
  29725. end;
  29726. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  29727. begin
  29728. StartProgram(false);
  29729. Add([
  29730. '{$R+}',
  29731. 'type',
  29732. ' Ten = 1..10;',
  29733. ' TRec = record x: Ten end;',
  29734. ' TArr = array of TRec;',
  29735. ' TArrArr = array of TArr;',
  29736. ' TObject = class',
  29737. ' A: TArr;',
  29738. ' end;',
  29739. 'procedure DoIt;',
  29740. 'var',
  29741. ' Arr: TArr;',
  29742. ' ArrArr: TArrArr;',
  29743. ' i: Ten;',
  29744. ' o: tobject;',
  29745. 'begin',
  29746. ' Arr[1]:=Arr[1];',
  29747. ' Arr[i]:=Arr[i+1];',
  29748. ' o.a[i]:=o.a[i+2];',
  29749. 'end;',
  29750. 'begin',
  29751. '']);
  29752. ConvertProgram;
  29753. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  29754. LinesToStr([ // statements
  29755. 'rtl.recNewT($mod, "TRec", function () {',
  29756. ' this.x = 0;',
  29757. ' this.$eq = function (b) {',
  29758. ' return this.x === b.x;',
  29759. ' };',
  29760. ' this.$assign = function (s) {',
  29761. ' this.x = s.x;',
  29762. ' return this;',
  29763. ' };',
  29764. '});',
  29765. 'rtl.createClass($mod, "TObject", null, function () {',
  29766. ' this.$init = function () {',
  29767. ' this.A = [];',
  29768. ' };',
  29769. ' this.$final = function () {',
  29770. ' this.A = undefined;',
  29771. ' };',
  29772. '});',
  29773. 'this.DoIt = function () {',
  29774. ' var Arr = [];',
  29775. ' var ArrArr = [];',
  29776. ' var i = 0;',
  29777. ' var o = null;',
  29778. ' Arr[1].$assign(Arr[1]);',
  29779. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  29780. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  29781. '};',
  29782. '']),
  29783. LinesToStr([ // $mod.$main
  29784. '']));
  29785. end;
  29786. procedure TTestModule.TestRangeChecks_StringIndex;
  29787. begin
  29788. StartProgram(false);
  29789. Add([
  29790. 'type',
  29791. ' TObject = class',
  29792. ' S: string;',
  29793. ' end;',
  29794. '{$R+}',
  29795. 'procedure DoIt(var h: string);',
  29796. 'var',
  29797. ' s: string;',
  29798. ' i: longint;',
  29799. ' c: char;',
  29800. ' o: tobject;',
  29801. 'begin',
  29802. ' c:=s[1];',
  29803. ' s[i]:=s[i];',
  29804. ' h[i]:=h[i];',
  29805. ' c:=o.s[i];',
  29806. ' o.s[i]:=c;',
  29807. 'end;',
  29808. 'begin',
  29809. '']);
  29810. ConvertProgram;
  29811. CheckSource('TestRangeChecks_StringIndex',
  29812. LinesToStr([ // statements
  29813. 'rtl.createClass($mod, "TObject", null, function () {',
  29814. ' this.$init = function () {',
  29815. ' this.S = "";',
  29816. ' };',
  29817. ' this.$final = function () {',
  29818. ' };',
  29819. '});',
  29820. 'this.DoIt = function (h) {',
  29821. ' var s = "";',
  29822. ' var i = 0;',
  29823. ' var c = "";',
  29824. ' var o = null;',
  29825. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  29826. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  29827. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  29828. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  29829. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  29830. '};',
  29831. '']),
  29832. LinesToStr([ // $mod.$main
  29833. '']));
  29834. end;
  29835. procedure TTestModule.TestRangeChecks_TypecastInt;
  29836. begin
  29837. StartProgram(false);
  29838. Add([
  29839. '{$R+}',
  29840. 'var',
  29841. ' i: nativeint;',
  29842. ' b: byte;',
  29843. ' sh: shortint;',
  29844. ' w: word;',
  29845. ' sm: smallint;',
  29846. ' lw: longword;',
  29847. ' li: longint;',
  29848. 'begin',
  29849. ' b:=12+byte(i);',
  29850. ' sh:=12+shortint(i);',
  29851. ' w:=12+word(i);',
  29852. ' sm:=12+smallint(i);',
  29853. ' lw:=12+longword(i);',
  29854. ' li:=12+longint(i);',
  29855. '']);
  29856. ConvertProgram;
  29857. CheckSource('TestRangeChecks_TypecastInt',
  29858. LinesToStr([
  29859. 'this.i = 0;',
  29860. 'this.b = 0;',
  29861. 'this.sh = 0;',
  29862. 'this.w = 0;',
  29863. 'this.sm = 0;',
  29864. 'this.lw = 0;',
  29865. 'this.li = 0;',
  29866. '']),
  29867. LinesToStr([
  29868. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  29869. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  29870. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  29871. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  29872. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  29873. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  29874. '']));
  29875. end;
  29876. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  29877. begin
  29878. Scanner.Options:=Scanner.Options+[po_CAssignments];
  29879. StartProgram(false);
  29880. Add([
  29881. '{$modeswitch typehelpers}',
  29882. '{$R+}',
  29883. 'type',
  29884. ' TObject = class',
  29885. ' FSize: byte;',
  29886. ' property Size: byte read FSize;',
  29887. ' end;',
  29888. ' THelper = type helper for byte',
  29889. ' procedure SetIt(w: word);',
  29890. ' end;',
  29891. 'procedure THelper.SetIt(w: word);',
  29892. 'begin',
  29893. ' Self:=w;',
  29894. 'end;',
  29895. 'function GetIt: byte;',
  29896. 'begin',
  29897. ' Result.SetIt(2);',
  29898. 'end;',
  29899. 'var',
  29900. ' b: byte = 3;',
  29901. ' o: TObject;',
  29902. 'begin',
  29903. ' b.SetIt(14);',
  29904. ' with b do SetIt(15);',
  29905. ' o.Size.SetIt(16);',
  29906. '']);
  29907. ConvertProgram;
  29908. CheckSource('TestRangeChecks_AssignInt',
  29909. LinesToStr([ // statements
  29910. 'rtl.createClass($mod, "TObject", null, function () {',
  29911. ' this.$init = function () {',
  29912. ' this.FSize = 0;',
  29913. ' };',
  29914. ' this.$final = function () {',
  29915. ' };',
  29916. '});',
  29917. 'rtl.createHelper($mod, "THelper", null, function () {',
  29918. ' this.SetIt = function (w) {',
  29919. ' rtl.rc(w, 0, 65535);',
  29920. ' this.set(w);',
  29921. ' };',
  29922. '});',
  29923. 'this.GetIt = function () {',
  29924. ' var Result = 0;',
  29925. ' $mod.THelper.SetIt.call({',
  29926. ' get: function () {',
  29927. ' return Result;',
  29928. ' },',
  29929. ' set: function (v) {',
  29930. ' rtl.rc(v, 0, 255);',
  29931. ' Result = v;',
  29932. ' }',
  29933. ' }, 2);',
  29934. ' return Result;',
  29935. '};',
  29936. 'this.b = 3;',
  29937. 'this.o = null;',
  29938. '']),
  29939. LinesToStr([ // $mod.$main
  29940. '$mod.THelper.SetIt.call({',
  29941. ' p: $mod,',
  29942. ' get: function () {',
  29943. ' return this.p.b;',
  29944. ' },',
  29945. ' set: function (v) {',
  29946. ' rtl.rc(v, 0, 255);',
  29947. ' this.p.b = v;',
  29948. ' }',
  29949. '}, 14);',
  29950. 'var $with1 = $mod.b;',
  29951. '$mod.THelper.SetIt.call({',
  29952. ' get: function () {',
  29953. ' return $with1;',
  29954. ' },',
  29955. ' set: function (v) {',
  29956. ' rtl.rc(v, 0, 255);',
  29957. ' $with1 = v;',
  29958. ' }',
  29959. '}, 15);',
  29960. '$mod.THelper.SetIt.call({',
  29961. ' p: $mod.o,',
  29962. ' get: function () {',
  29963. ' return this.p.FSize;',
  29964. ' },',
  29965. ' set: function (v) {',
  29966. ' rtl.rc(v, 0, 255);',
  29967. ' this.p.FSize = v;',
  29968. ' }',
  29969. '}, 16);',
  29970. '']));
  29971. end;
  29972. Initialization
  29973. RegisterTests([TTestModule]);
  29974. end.