tcmodules.pas 921 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924
  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, TestPasUtils,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. const
  34. SrcMarker: array[TSrcMarkerKind] of char = (
  35. '#', // mkLabel
  36. '@', // mkResolverReference
  37. '=' // mkDirectReference
  38. );
  39. type
  40. PSrcMarker = ^TSrcMarker;
  41. TSrcMarker = record
  42. Kind: TSrcMarkerKind;
  43. Filename: string;
  44. Row: integer;
  45. StartCol, EndCol: integer; // token start, end column
  46. Identifier: string;
  47. Next: PSrcMarker;
  48. end;
  49. TSystemUnitPart = (
  50. supTObject,
  51. supTVarRec,
  52. supTypeInfo,
  53. supTInterfacedObject,
  54. supWriteln
  55. );
  56. TSystemUnitParts = set of TSystemUnitPart;
  57. { TTestHintMessage }
  58. TTestHintMessage = class
  59. public
  60. Id: int64;
  61. MsgType: TMessageType;
  62. MsgNumber: integer;
  63. Msg: string;
  64. SourcePos: TPasSourcePos;
  65. end;
  66. TTestResolverReferenceData = record
  67. Filename: string;
  68. Row: integer;
  69. StartCol: integer;
  70. EndCol: integer;
  71. Found: TFPList; // list of TPasElement at this token
  72. end;
  73. PTestResolverReferenceData = ^TTestResolverReferenceData;
  74. { TTestPasParser }
  75. TTestPasParser = Class(TPasParser)
  76. end;
  77. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  78. { TTestEnginePasResolver }
  79. TTestEnginePasResolver = class(TPas2JsResolver)
  80. private
  81. FFilename: string;
  82. FModule: TPasModule;
  83. FOnFindUnit: TOnFindUnit;
  84. FParser: TTestPasParser;
  85. FStreamResolver: TStreamResolver;
  86. FScanner: TPas2jsPasScanner;
  87. FSource: string;
  88. procedure SetModule(const AValue: TPasModule);
  89. public
  90. destructor Destroy; override;
  91. function CreateElement(AClass: TPTreeElement; const AName: String;
  92. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  93. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  94. overload; override;
  95. function FindUnit(const AName, InFilename: String; NameExpr,
  96. InFileExpr: TPasExpr): TPasModule; override;
  97. procedure UsedInterfacesFinished(Section: TPasSection); override;
  98. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  99. property Filename: string read FFilename write FFilename;
  100. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  101. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  102. property Parser: TTestPasParser read FParser write FParser;
  103. property Source: string read FSource write FSource;
  104. property Module: TPasModule read FModule write SetModule;
  105. end;
  106. { TCustomTestModule }
  107. TCustomTestModule = Class(TTestCase)
  108. private
  109. FWithTypeInfo: boolean;
  110. FSource: TStringList;
  111. FSkipTests: boolean;
  112. FScanner: TPas2jsPasScanner;
  113. FResolvers: TObjectList;// list of TTestEnginePasResolver
  114. FPasProgram: TPasProgram;
  115. FPasLibrary: TPasLibrary;
  116. FParser: TTestPasParser;
  117. FModule: TPasModule;
  118. FJSSource: TStringList;
  119. FJSRegModuleCall: TJSCallExpression;
  120. FJSModuleSrc: TJSSourceElements;
  121. FJSModuleCallArgs: TJSArguments;
  122. FJSModule: TJSSourceElements;
  123. FJSInterfaceUses: TJSArrayLiteral;
  124. FJSInitBody: TJSFunctionBody;
  125. FJSImplentationUses: TJSArrayLiteral;
  126. FJSImplementationUses: TJSArrayLiteral;
  127. FHub: TPas2JSResolverHub;
  128. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  129. FHintMsgs: TObjectList; // list of TTestHintMessage
  130. FFirstPasStatement: TPasImplBlock;
  131. FFileResolver: TStreamResolver;
  132. FFilename: string;
  133. FExpectedErrorNumber: integer;
  134. FExpectedErrorMsg: string;
  135. FExpectedErrorClass: ExceptClass;
  136. FEngine: TTestEnginePasResolver;
  137. FConverter: TPasToJSConverter;
  138. {$IFDEF EnablePasTreeGlobalRefCount}
  139. FElementRefCountAtSetup: int64;
  140. {$ENDIF}
  141. procedure FreeSrcMarkers;
  142. function GetResolverCount: integer;
  143. function GetResolvers(Index: integer): TTestEnginePasResolver;
  144. function GetMsgCount: integer;
  145. function GetMsgs(Index: integer): TTestHintMessage;
  146. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  147. procedure OnParserLog(Sender: TObject; const Msg: String);
  148. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  149. procedure OnScannerLog(Sender: TObject; const Msg: String);
  150. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  151. procedure OnFindReference(El: TPasElement; FindData: pointer);
  152. procedure SetWithTypeInfo(const AValue: boolean);
  153. protected
  154. procedure SetUp; override;
  155. function CreateConverter: TPasToJSConverter; virtual;
  156. function LoadUnit(const aUnitName: String): TPasModule;
  157. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  158. procedure TearDown; override;
  159. Procedure Add(Line: string); virtual;
  160. Procedure Add(const Lines: array of string);
  161. Procedure StartParsing; virtual;
  162. procedure ParseModuleQueue; virtual;
  163. procedure ParseModule; virtual;
  164. procedure ParseProgram; virtual;
  165. procedure ParseLibrary; virtual;
  166. procedure ParseUnit; virtual;
  167. protected
  168. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  169. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  172. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  173. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  174. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  175. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  178. procedure ConvertModule; virtual;
  179. procedure ConvertProgram; virtual;
  180. procedure ConvertLibrary; virtual;
  181. procedure ConvertUnit; virtual;
  182. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  183. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  184. function GetDottedIdentifier(El: TJSElement): string;
  185. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  186. ImplStatements: string = ''); virtual;
  187. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  188. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  189. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  190. procedure CheckReferenceDirectives; virtual;
  191. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  192. Msg: string; Marker: PSrcMarker = nil); virtual;
  193. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  194. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  197. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  198. function IsErrorExpected(E: Exception): boolean;
  199. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  200. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  201. procedure HandleScannerError(E: EScannerError);
  202. procedure HandleParserError(E: EParserError);
  203. procedure HandlePasResolveError(E: EPasResolve);
  204. procedure HandlePas2JSError(E: EPas2JS);
  205. procedure HandleException(E: Exception);
  206. procedure FailException(E: Exception);
  207. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  208. function IndexOfResolver(const Filename: string): integer;
  209. function GetResolver(const Filename: string): TTestEnginePasResolver;
  210. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  211. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  212. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  213. function FindSrcLabel(const Identifier: string): PSrcMarker;
  214. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  215. function GetDefaultNamespace: string;
  216. property PasProgram: TPasProgram Read FPasProgram;
  217. property PasLibrary: TPasLibrary Read FPasLibrary;
  218. property ResolverEngine: TTestEnginePasResolver read FEngine;
  219. property Filename: string read FFilename;
  220. Property Module: TPasModule Read FModule;
  221. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  222. property Converter: TPasToJSConverter read FConverter;
  223. property JSSource: TStringList read FJSSource;
  224. property JSModule: TJSSourceElements read FJSModule;
  225. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  226. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  227. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  228. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  229. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  230. property JSInitBody: TJSFunctionBody read FJSInitBody;
  231. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  232. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  233. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  234. property SkipTests: boolean read FSkipTests write FSkipTests;
  235. public
  236. constructor Create; override;
  237. destructor Destroy; override;
  238. property Hub: TPas2JSResolverHub read FHub;
  239. property Source: TStringList read FSource;
  240. property FileResolver: TStreamResolver read FFileResolver;
  241. property Scanner: TPas2jsPasScanner read FScanner;
  242. property Parser: TTestPasParser read FParser;
  243. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  244. property ResolverCount: integer read GetResolverCount;
  245. property MsgCount: integer read GetMsgCount;
  246. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  247. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  248. end;
  249. { TTestModule }
  250. TTestModule = class(TCustomTestModule)
  251. Published
  252. Procedure TestReservedWords;
  253. // program, units, includes
  254. Procedure TestEmptyProgram;
  255. Procedure TestEmptyProgramUseStrict;
  256. Procedure TestEmptyUnit;
  257. Procedure TestEmptyUnitUseStrict;
  258. Procedure TestDottedUnitNames;
  259. Procedure TestDottedUnitNameImpl;
  260. Procedure TestDottedUnitExpr;
  261. Procedure Test_ModeFPCFail;
  262. Procedure Test_ModeSwitchCBlocksFail;
  263. Procedure TestUnit_UseSystem;
  264. Procedure TestUnit_Intf1Impl2Intf1;
  265. Procedure TestIncludeVersion;
  266. // vars/const
  267. Procedure TestVarInt;
  268. Procedure TestVarBaseTypes;
  269. Procedure TestBaseTypeSingleFail;
  270. Procedure TestBaseTypeExtendedFail;
  271. Procedure TestConstBaseTypes;
  272. Procedure TestUnitImplVars;
  273. Procedure TestUnitImplConsts;
  274. Procedure TestUnitImplRecord;
  275. Procedure TestRenameJSNameConflict;
  276. Procedure TestLocalConst;
  277. Procedure TestVarExternal;
  278. Procedure TestVarExternalOtherUnit;
  279. Procedure TestVarAbsoluteFail;
  280. Procedure TestConstExternal;
  281. // numbers
  282. Procedure TestDouble;
  283. Procedure TestInteger;
  284. Procedure TestIntegerRange;
  285. Procedure TestIntegerTypecasts;
  286. Procedure TestInteger_BitwiseShrNativeInt;
  287. Procedure TestInteger_BitwiseShlNativeInt;
  288. Procedure TestInteger_SystemFunc;
  289. Procedure TestInteger_AssignOutsideConst;
  290. Procedure TestCurrency;
  291. Procedure TestForBoolDo;
  292. Procedure TestForIntDo;
  293. Procedure TestForIntInDo;
  294. // strings
  295. Procedure TestCharConst;
  296. Procedure TestChar_Compare;
  297. Procedure TestChar_BuiltInProcs;
  298. Procedure TestStringConst;
  299. Procedure TestStringConst_InvalidUTF16;
  300. Procedure TestStringConstSurrogate;
  301. Procedure TestString_Length;
  302. Procedure TestString_Compare;
  303. Procedure TestString_SetLength;
  304. Procedure TestString_CharAt;
  305. Procedure TestStringHMinusFail;
  306. Procedure TestStr;
  307. Procedure TestBaseType_AnsiStringFail;
  308. Procedure TestBaseType_WideStringFail;
  309. Procedure TestBaseType_ShortStringFail;
  310. Procedure TestBaseType_RawByteStringFail;
  311. Procedure TestTypeShortstring_Fail;
  312. Procedure TestCharSet_Custom;
  313. Procedure TestWideChar;
  314. Procedure TestForCharDo;
  315. Procedure TestForCharInDo;
  316. // alias types
  317. Procedure TestAliasTypeRef;
  318. Procedure TestTypeCast_BaseTypes;
  319. Procedure TestTypeCast_AliasBaseTypes;
  320. // functions
  321. Procedure TestEmptyProc;
  322. Procedure TestProcOneParam;
  323. Procedure TestFunctionWithoutParams;
  324. Procedure TestProcedureWithoutParams;
  325. Procedure TestPrgProcVar;
  326. Procedure TestProcTwoArgs;
  327. Procedure TestProc_DefaultValue;
  328. Procedure TestUnitProcVar;
  329. Procedure TestImplProc;
  330. Procedure TestFunctionResult;
  331. Procedure TestNestedProc;
  332. Procedure TestNestedProc_ResultString;
  333. Procedure TestForwardProc;
  334. Procedure TestNestedForwardProc;
  335. Procedure TestAssignFunctionResult;
  336. Procedure TestFunctionResultInCondition;
  337. Procedure TestFunctionResultInForLoop;
  338. Procedure TestFunctionResultInTypeCast;
  339. Procedure TestExit;
  340. Procedure TestExit_ResultInFinally;
  341. Procedure TestBreak;
  342. Procedure TestBreakAsVar;
  343. Procedure TestContinue;
  344. Procedure TestProc_External;
  345. Procedure TestProc_ExternalOtherUnit;
  346. Procedure TestProc_Asm;
  347. Procedure TestProc_AsmSubBlock;
  348. Procedure TestProc_Assembler;
  349. Procedure TestProc_VarParam;
  350. Procedure TestProc_VarParamString;
  351. Procedure TestProc_VarParamV;
  352. Procedure TestProc_Overload;
  353. Procedure TestProc_OverloadForward;
  354. Procedure TestProc_OverloadIntfImpl;
  355. Procedure TestProc_OverloadNested;
  356. Procedure TestProc_OverloadNestedForward;
  357. Procedure TestProc_OverloadUnitCycle;
  358. Procedure TestProc_Varargs;
  359. Procedure TestProc_ConstOrder;
  360. Procedure TestProc_DuplicateConst;
  361. Procedure TestProc_LocalVarAbsolute;
  362. Procedure TestProc_LocalVarInit;
  363. Procedure TestProc_ReservedWords;
  364. Procedure TestProc_ConstRefWord;
  365. // anonymous functions
  366. Procedure TestAnonymousProc_Assign_ObjFPC;
  367. Procedure TestAnonymousProc_Assign_Delphi;
  368. Procedure TestAnonymousProc_Arg;
  369. Procedure TestAnonymousProc_Typecast;
  370. Procedure TestAnonymousProc_With;
  371. Procedure TestAnonymousProc_ExceptOn;
  372. Procedure TestAnonymousProc_Nested;
  373. Procedure TestAnonymousProc_NestedAssignResult;
  374. Procedure TestAnonymousProc_Class;
  375. Procedure TestAnonymousProc_ForLoop;
  376. Procedure TestAnonymousProc_AsmDelphi;
  377. // enums, sets
  378. Procedure TestEnum_Name;
  379. Procedure TestEnum_Number;
  380. Procedure TestEnum_ConstFail;
  381. Procedure TestEnum_Functions;
  382. Procedure TestEnumRg_Functions;
  383. Procedure TestEnum_AsParams;
  384. Procedure TestEnumRange_Array;
  385. Procedure TestEnum_ForIn;
  386. Procedure TestEnum_ScopedNumber;
  387. Procedure TestEnum_InFunction;
  388. Procedure TestEnum_Name_Anonymous_Unit;
  389. Procedure TestSet_Enum;
  390. Procedure TestSet_Operators;
  391. Procedure TestSet_Operator_In;
  392. Procedure TestSet_Functions;
  393. Procedure TestSet_PassAsArgClone;
  394. Procedure TestSet_AsParams;
  395. Procedure TestSet_Property;
  396. Procedure TestSet_EnumConst;
  397. Procedure TestSet_IntConst;
  398. Procedure TestSet_IntRange;
  399. Procedure TestSet_AnonymousEnumType;
  400. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  401. Procedure TestSet_ConstEnum;
  402. Procedure TestSet_ConstChar;
  403. Procedure TestSet_ConstInt;
  404. Procedure TestSet_InFunction;
  405. Procedure TestSet_ForIn;
  406. // statements
  407. Procedure TestNestBegin;
  408. Procedure TestIncDec;
  409. Procedure TestLoHiFpcMode;
  410. Procedure TestLoHiDelphiMode;
  411. Procedure TestAssignments;
  412. Procedure TestArithmeticOperators1;
  413. Procedure TestMultiAdd;
  414. Procedure TestLogicalOperators;
  415. Procedure TestBitwiseOperators;
  416. Procedure TestBitwiseOperatorsLongword;
  417. Procedure TestFunctionInt;
  418. Procedure TestFunctionString;
  419. Procedure TestIfThen;
  420. Procedure TestForLoop;
  421. Procedure TestForLoopInsideFunction;
  422. Procedure TestForLoop_ReadVarAfter;
  423. Procedure TestForLoop_Nested;
  424. Procedure TestRepeatUntil;
  425. Procedure TestAsmBlock;
  426. Procedure TestAsmPas_Impl; // ToDo
  427. Procedure TestTryFinally;
  428. Procedure TestTryExcept;
  429. Procedure TestTryExcept_ReservedWords;
  430. Procedure TestIfThenRaiseElse;
  431. Procedure TestCaseOf;
  432. Procedure TestCaseOf_UseSwitch;
  433. Procedure TestCaseOfNoElse;
  434. Procedure TestCaseOfNoElse_UseSwitch;
  435. Procedure TestCaseOfRange;
  436. Procedure TestCaseOfString;
  437. Procedure TestCaseOfChar;
  438. Procedure TestCaseOfExternalClassConst;
  439. Procedure TestDebugger;
  440. // arrays
  441. Procedure TestArray_Dynamic;
  442. Procedure TestArray_Dynamic_Nil;
  443. Procedure TestArray_DynMultiDimensional;
  444. Procedure TestArray_DynamicAssign;
  445. Procedure TestArray_StaticInt;
  446. Procedure TestArray_StaticBool;
  447. Procedure TestArray_StaticChar;
  448. Procedure TestArray_StaticMultiDim;
  449. Procedure TestArray_StaticInFunction;
  450. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  451. Procedure TestArrayOfRecord;
  452. Procedure TestArray_StaticRecord;
  453. Procedure TestArrayOfSet;
  454. Procedure TestArray_DynAsParam;
  455. Procedure TestArray_StaticAsParam;
  456. Procedure TestArrayElement_AsParams;
  457. Procedure TestArrayElementFromFuncResult_AsParams;
  458. Procedure TestArrayEnumTypeRange;
  459. Procedure TestArray_SetLengthOutArg;
  460. Procedure TestArray_SetLengthProperty;
  461. Procedure TestArray_SetLengthMultiDim;
  462. Procedure TestArray_SetLengthDynOfStatic;
  463. Procedure TestArray_OpenArrayOfString;
  464. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  465. Procedure TestArray_ConstRef;
  466. Procedure TestArray_Concat;
  467. Procedure TestArray_Copy;
  468. Procedure TestArray_InsertDelete;
  469. Procedure TestArray_DynArrayConstObjFPC;
  470. Procedure TestArray_DynArrayConstDelphi;
  471. Procedure TestArray_ArrayLitAsParam;
  472. Procedure TestArray_ArrayLitMultiDimAsParam;
  473. Procedure TestArray_ArrayLitStaticAsParam;
  474. Procedure TestArray_ForInArrOfString;
  475. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  476. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  477. Procedure TestArrayOfConst_TVarRec;
  478. Procedure TestArrayOfConst_PassBaseTypes;
  479. Procedure TestArrayOfConst_PassObj;
  480. // record
  481. Procedure TestRecord_Empty;
  482. Procedure TestRecord_Var;
  483. Procedure TestRecord_VarExternal;
  484. Procedure TestRecord_WithDo;
  485. Procedure TestRecord_Assign;
  486. Procedure TestRecord_AsParams;
  487. Procedure TestRecord_ConstRef;
  488. Procedure TestRecordElement_AsParams;
  489. Procedure TestRecordElementFromFuncResult_AsParams;
  490. Procedure TestRecordElementFromWith_AsParams;
  491. Procedure TestRecord_Equal;
  492. Procedure TestRecord_JSValue;
  493. Procedure TestRecord_VariantFail;
  494. Procedure TestRecord_FieldArray;
  495. Procedure TestRecord_Const;
  496. Procedure TestRecord_TypecastFail;
  497. Procedure TestRecord_InFunction;
  498. Procedure TestRecord_AnonymousFail;
  499. // advanced record
  500. Procedure TestAdvRecord_Function;
  501. Procedure TestAdvRecord_Property;
  502. Procedure TestAdvRecord_PropertyDefault;
  503. Procedure TestAdvRecord_Property_ClassMethod;
  504. Procedure TestAdvRecord_Const;
  505. Procedure TestAdvRecord_ExternalField;
  506. Procedure TestAdvRecord_SubRecord;
  507. Procedure TestAdvRecord_SubClass;
  508. Procedure TestAdvRecord_SubInterfaceFail;
  509. Procedure TestAdvRecord_Constructor;
  510. Procedure TestAdvRecord_ClassConstructor_Program;
  511. Procedure TestAdvRecord_ClassConstructor_Unit;
  512. // classes
  513. Procedure TestClass_TObjectDefaultConstructor;
  514. Procedure TestClass_TObjectConstructorWithParams;
  515. Procedure TestClass_TObjectConstructorWithDefaultParam;
  516. Procedure TestClass_Var;
  517. Procedure TestClass_Method;
  518. Procedure TestClass_Implementation;
  519. Procedure TestClass_Inheritance;
  520. Procedure TestClass_TypeAlias;
  521. Procedure TestClass_AbstractMethod;
  522. Procedure TestClass_CallInherited_ProcNoParams;
  523. Procedure TestClass_CallInherited_WithParams;
  524. Procedure TestClasS_CallInheritedConstructor;
  525. Procedure TestClass_ClassVar_Assign;
  526. Procedure TestClass_CallClassMethod;
  527. Procedure TestClass_CallClassMethodStatic;
  528. Procedure TestClass_Property;
  529. Procedure TestClass_Property_ClassMethod;
  530. Procedure TestClass_Property_ClassMethodStatic;
  531. Procedure TestClass_Property_Indexed;
  532. Procedure TestClass_Property_IndexSpec;
  533. Procedure TestClass_PropertyOfTypeArray;
  534. Procedure TestClass_PropertyDefault;
  535. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  536. //Procedure TestClass_PropertyDefault;
  537. Procedure TestClass_PropertyOverride;
  538. Procedure TestClass_PropertyIncVisibility;
  539. Procedure TestClass_Assigned;
  540. Procedure TestClass_WithClassDoCreate;
  541. Procedure TestClass_WithClassInstDoProperty;
  542. Procedure TestClass_WithClassInstDoPropertyWithParams;
  543. Procedure TestClass_WithClassInstDoFunc;
  544. Procedure TestClass_TypeCast;
  545. Procedure TestClass_TypeCastUntypedParam;
  546. Procedure TestClass_Overloads;
  547. Procedure TestClass_OverloadsAncestor;
  548. Procedure TestClass_OverloadConstructor;
  549. Procedure TestClass_OverloadDelphiOverride;
  550. Procedure TestClass_ReintroduceVarDelphi;
  551. Procedure TestClass_ReintroducedVar;
  552. Procedure TestClass_RaiseDescendant;
  553. Procedure TestClass_ExternalMethod;
  554. Procedure TestClass_ExternalVirtualNameMismatchFail;
  555. Procedure TestClass_ExternalOverrideFail;
  556. Procedure TestClass_ExternalVar;
  557. Procedure TestClass_Const;
  558. Procedure TestClass_ConstEnum;
  559. Procedure TestClass_LocalConstDuplicate_Prg;
  560. Procedure TestClass_LocalConstDuplicate_Unit;
  561. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  562. Procedure TestClass_LocalVarSelfFail;
  563. Procedure TestClass_ArgSelfFail;
  564. Procedure TestClass_NestedProcSelf;
  565. Procedure TestClass_NestedProcSelf2;
  566. Procedure TestClass_NestedProcClassSelf;
  567. Procedure TestClass_NestedProcCallInherited;
  568. Procedure TestClass_TObjectFree;
  569. Procedure TestClass_TObjectFree_VarArg;
  570. Procedure TestClass_TObjectFreeNewInstance;
  571. Procedure TestClass_TObjectFreeLowerCase;
  572. Procedure TestClass_TObjectFreeFunctionFail;
  573. Procedure TestClass_TObjectFreePropertyFail;
  574. Procedure TestClass_ForIn;
  575. Procedure TestClass_DispatchMessage;
  576. Procedure TestClass_Message_DuplicateIntFail;
  577. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  578. // class of
  579. Procedure TestClassOf_Create;
  580. Procedure TestClassOf_Call;
  581. Procedure TestClassOf_Assign;
  582. Procedure TestClassOf_Is;
  583. Procedure TestClassOf_Compare;
  584. Procedure TestClassOf_ClassVar;
  585. Procedure TestClassOf_ClassMethod;
  586. Procedure TestClassOf_ClassProperty;
  587. Procedure TestClassOf_ClassMethodSelf;
  588. Procedure TestClassOf_TypeCast;
  589. Procedure TestClassOf_ImplicitFunctionCall;
  590. Procedure TestClassOf_Const;
  591. // nested class
  592. Procedure TestNestedClass_Alias;
  593. Procedure TestNestedClass_Record;
  594. Procedure TestNestedClass_Class;
  595. // external class
  596. Procedure TestExternalClass_Var;
  597. Procedure TestExternalClass_Const;
  598. Procedure TestExternalClass_Dollar;
  599. Procedure TestExternalClass_DuplicateVarFail;
  600. Procedure TestExternalClass_Method;
  601. Procedure TestExternalClass_ClassMethod;
  602. Procedure TestExternalClass_ClassMethodStatic;
  603. Procedure TestExternalClass_FunctionResultInTypeCast;
  604. Procedure TestExternalClass_NonExternalOverride;
  605. Procedure TestExternalClass_OverloadHint;
  606. Procedure TestExternalClass_SameNamePublishedProperty;
  607. Procedure TestExternalClass_Property;
  608. Procedure TestExternalClass_PropertyDate;
  609. Procedure TestExternalClass_ClassProperty;
  610. Procedure TestExternalClass_ClassOf;
  611. Procedure TestExternalClass_ClassOtherUnit;
  612. Procedure TestExternalClass_Is;
  613. Procedure TestExternalClass_As;
  614. Procedure TestExternalClass_DestructorFail;
  615. Procedure TestExternalClass_New;
  616. Procedure TestExternalClass_ClassOf_New;
  617. Procedure TestExternalClass_FuncClassOf_New;
  618. Procedure TestExternalClass_New_PasClassFail;
  619. Procedure TestExternalClass_New_PasClassBracketsFail;
  620. Procedure TestExternalClass_NewExtName;
  621. Procedure TestExternalClass_Constructor;
  622. Procedure TestExternalClass_ConstructorBrackets;
  623. Procedure TestExternalClass_LocalConstSameName;
  624. Procedure TestExternalClass_ReintroduceOverload;
  625. Procedure TestExternalClass_Inherited;
  626. Procedure TestExternalClass_PascalAncestorFail;
  627. Procedure TestExternalClass_NewInstance;
  628. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  629. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  630. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  631. Procedure TestExternalClass_JSFunctionPasDescendant;
  632. Procedure TestExternalClass_PascalProperty;
  633. Procedure TestExternalClass_TypeCastToRootClass;
  634. Procedure TestExternalClass_TypeCastToJSObject;
  635. Procedure TestExternalClass_TypeCastStringToExternalString;
  636. Procedure TestExternalClass_TypeCastToJSFunction;
  637. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  638. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  639. Procedure TestExternalClass_BracketAccessor;
  640. Procedure TestExternalClass_BracketAccessor_Call;
  641. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  642. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  643. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  644. Procedure TestExternalClass_BracketAccessor_MultiType;
  645. Procedure TestExternalClass_BracketAccessor_Index;
  646. Procedure TestExternalClass_ForInJSObject;
  647. Procedure TestExternalClass_ForInJSArray;
  648. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  649. Procedure TestExternalClass_NestedConstructor;
  650. // class interfaces
  651. Procedure TestClassInterface_Corba;
  652. Procedure TestClassInterface_ProcExternalFail;
  653. Procedure TestClassInterface_Overloads;
  654. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  655. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  656. Procedure TestClassInterface_AncestorImpl;
  657. Procedure TestClassInterface_ImplReintroduce;
  658. Procedure TestClassInterface_MethodResolution;
  659. Procedure TestClassInterface_AncestorMoreInterfaces;
  660. Procedure TestClassInterface_MethodOverride;
  661. Procedure TestClassInterface_Corba_Delegation;
  662. Procedure TestClassInterface_Corba_DelegationStatic;
  663. Procedure TestClassInterface_Corba_Operators;
  664. Procedure TestClassInterface_Corba_Args;
  665. Procedure TestClassInterface_Corba_ForIn;
  666. Procedure TestClassInterface_COM_AssignVar;
  667. Procedure TestClassInterface_COM_AssignArg;
  668. Procedure TestClassInterface_COM_FunctionResult;
  669. Procedure TestClassInterface_COM_InheritedFuncResult;
  670. Procedure TestClassInterface_COM_IsAsTypeCasts;
  671. Procedure TestClassInterface_COM_PassAsArg;
  672. Procedure TestClassInterface_COM_PassToUntypedParam;
  673. Procedure TestClassInterface_COM_FunctionInExpr;
  674. Procedure TestClassInterface_COM_Property;
  675. Procedure TestClassInterface_COM_IntfProperty;
  676. Procedure TestClassInterface_COM_Delegation;
  677. Procedure TestClassInterface_COM_With;
  678. Procedure TestClassInterface_COM_ForIn;
  679. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  680. Procedure TestClassInterface_COM_RecordIntfFail;
  681. Procedure TestClassInterface_COM_UnitInitialization;
  682. Procedure TestClassInterface_GUID;
  683. Procedure TestClassInterface_GUIDProperty;
  684. // helpers
  685. Procedure TestClassHelper_ClassVar;
  686. Procedure TestClassHelper_Method_AccessInstanceFields;
  687. Procedure TestClassHelper_Method_Call;
  688. Procedure TestClassHelper_Method_Nested_Call;
  689. Procedure TestClassHelper_ClassMethod_Call;
  690. Procedure TestClassHelper_ClassOf;
  691. Procedure TestClassHelper_MethodRefObjFPC;
  692. Procedure TestClassHelper_Constructor;
  693. Procedure TestClassHelper_InheritedObjFPC;
  694. Procedure TestClassHelper_Property;
  695. Procedure TestClassHelper_Property_Array;
  696. Procedure TestClassHelper_Property_Array_Default;
  697. Procedure TestClassHelper_Property_Array_DefaultDefault;
  698. Procedure TestClassHelper_ClassProperty;
  699. Procedure TestClassHelper_ClassPropertyStatic;
  700. Procedure TestClassHelper_ClassProperty_Array;
  701. Procedure TestClassHelper_ForIn;
  702. Procedure TestClassHelper_PassProperty;
  703. Procedure TestExtClassHelper_ClassVar;
  704. Procedure TestExtClassHelper_Method_Call;
  705. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  706. Procedure TestRecordHelper_ClassVar;
  707. Procedure TestRecordHelper_Method_Call;
  708. Procedure TestRecordHelper_Constructor;
  709. Procedure TestTypeHelper_ClassVar;
  710. Procedure TestTypeHelper_PassResultElement;
  711. Procedure TestTypeHelper_PassArgs;
  712. Procedure TestTypeHelper_PassVarConst;
  713. Procedure TestTypeHelper_PassFuncResult;
  714. Procedure TestTypeHelper_PassPropertyField;
  715. Procedure TestTypeHelper_PassPropertyGetter;
  716. Procedure TestTypeHelper_PassClassPropertyField;
  717. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  718. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  719. Procedure TestTypeHelper_Property;
  720. Procedure TestTypeHelper_Property_Array;
  721. Procedure TestTypeHelper_ClassProperty;
  722. Procedure TestTypeHelper_ClassProperty_Array;
  723. Procedure TestTypeHelper_ClassMethod;
  724. Procedure TestTypeHelper_ExtClassMethodFail;
  725. Procedure TestTypeHelper_Constructor;
  726. Procedure TestTypeHelper_Word;
  727. Procedure TestTypeHelper_Boolean;
  728. Procedure TestTypeHelper_WordBool;
  729. Procedure TestTypeHelper_Double;
  730. Procedure TestTypeHelper_NativeInt;
  731. Procedure TestTypeHelper_StringChar;
  732. Procedure TestTypeHelper_JSValue;
  733. Procedure TestTypeHelper_Array;
  734. Procedure TestTypeHelper_EnumType;
  735. Procedure TestTypeHelper_SetType;
  736. Procedure TestTypeHelper_InterfaceType;
  737. Procedure TestTypeHelper_NestedSelf;
  738. // proc types
  739. Procedure TestProcType;
  740. Procedure TestProcType_Arg;
  741. Procedure TestProcType_FunctionFPC;
  742. Procedure TestProcType_FunctionDelphi;
  743. Procedure TestProcType_ProcedureDelphi;
  744. Procedure TestProcType_AsParam;
  745. Procedure TestProcType_MethodFPC;
  746. Procedure TestProcType_MethodDelphi;
  747. Procedure TestProcType_PropertyFPC;
  748. Procedure TestProcType_PropertyDelphi;
  749. Procedure TestProcType_WithClassInstDoPropertyFPC;
  750. Procedure TestProcType_Nested;
  751. Procedure TestProcType_NestedOfObject;
  752. Procedure TestProcType_ReferenceToProc;
  753. Procedure TestProcType_ReferenceToMethod;
  754. Procedure TestProcType_Typecast;
  755. Procedure TestProcType_PassProcToUntyped;
  756. Procedure TestProcType_PassProcToArray;
  757. Procedure TestProcType_SafeCallObjFPC;
  758. Procedure TestProcType_SafeCallDelphi;
  759. // pointer
  760. Procedure TestPointer;
  761. Procedure TestPointer_Proc;
  762. Procedure TestPointer_AssignRecordFail;
  763. Procedure TestPointer_AssignStaticArrayFail;
  764. Procedure TestPointer_TypeCastJSValueToPointer;
  765. Procedure TestPointer_NonRecordFail;
  766. Procedure TestPointer_AnonymousArgTypeFail;
  767. Procedure TestPointer_AnonymousVarTypeFail;
  768. Procedure TestPointer_AnonymousResultTypeFail;
  769. Procedure TestPointer_AddrOperatorFail;
  770. Procedure TestPointer_ArrayParamsFail;
  771. Procedure TestPointer_PointerAddFail;
  772. Procedure TestPointer_IncPointerFail;
  773. Procedure TestPointer_Record;
  774. Procedure TestPointer_RecordArg;
  775. // jsvalue
  776. Procedure TestJSValue_AssignToJSValue;
  777. Procedure TestJSValue_TypeCastToBaseType;
  778. Procedure TestJSValue_TypecastToJSValue;
  779. Procedure TestJSValue_Equal;
  780. Procedure TestJSValue_If;
  781. Procedure TestJSValue_Not;
  782. Procedure TestJSValue_Enum;
  783. Procedure TestJSValue_ClassInstance;
  784. Procedure TestJSValue_ClassOf;
  785. Procedure TestJSValue_ArrayOfJSValue;
  786. Procedure TestJSValue_ArrayLit;
  787. Procedure TestJSValue_Params;
  788. Procedure TestJSValue_UntypedParam;
  789. Procedure TestJSValue_FuncResultType;
  790. Procedure TestJSValue_ProcType_Assign;
  791. Procedure TestJSValue_ProcType_Equal;
  792. Procedure TestJSValue_ProcType_Param;
  793. Procedure TestJSValue_AssignToPointerFail;
  794. Procedure TestJSValue_OverloadDouble;
  795. Procedure TestJSValue_OverloadNativeInt;
  796. Procedure TestJSValue_OverloadWord;
  797. Procedure TestJSValue_OverloadString;
  798. Procedure TestJSValue_OverloadChar;
  799. Procedure TestJSValue_OverloadPointer;
  800. Procedure TestJSValue_ForIn;
  801. // RTTI
  802. Procedure TestRTTI_IntRange;
  803. Procedure TestRTTI_Double;
  804. Procedure TestRTTI_ProcType;
  805. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  806. Procedure TestRTTI_EnumAndSetType;
  807. Procedure TestRTTI_EnumRange;
  808. Procedure TestRTTI_AnonymousEnumType;
  809. Procedure TestRTTI_StaticArray;
  810. Procedure TestRTTI_DynArray;
  811. Procedure TestRTTI_ArrayNestedAnonymous;
  812. Procedure TestRTTI_PublishedMethodOverloadFail;
  813. Procedure TestRTTI_PublishedMethodHideNoHint;
  814. Procedure TestRTTI_PublishedMethodExternalFail;
  815. Procedure TestRTTI_PublishedClassPropertyFail;
  816. Procedure TestRTTI_PublishedClassFieldFail;
  817. Procedure TestRTTI_PublishedFieldExternalFail;
  818. Procedure TestRTTI_Class_Field;
  819. Procedure TestRTTI_Class_Method;
  820. Procedure TestRTTI_Class_MethodArgFlags;
  821. Procedure TestRTTI_Class_Property;
  822. Procedure TestRTTI_Class_PropertyParams;
  823. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  824. Procedure TestRTTI_Class_OmitRTTI;
  825. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  826. Procedure TestRTTI_IndexModifier;
  827. Procedure TestRTTI_StoredModifier;
  828. Procedure TestRTTI_DefaultValue;
  829. Procedure TestRTTI_DefaultValueSet;
  830. Procedure TestRTTI_DefaultValueRangeType;
  831. Procedure TestRTTI_DefaultValueInherit;
  832. Procedure TestRTTI_OverrideMethod;
  833. Procedure TestRTTI_ReintroduceMethod;
  834. Procedure TestRTTI_OverloadProperty;
  835. // ToDo: array argument
  836. Procedure TestRTTI_ClassForward;
  837. Procedure TestRTTI_ClassOf;
  838. Procedure TestRTTI_Record;
  839. Procedure TestRTTI_RecordAnonymousArray;
  840. Procedure TestRTTI_Record_ClassVarType;
  841. Procedure TestRTTI_LocalTypes;
  842. Procedure TestRTTI_TypeInfo_BaseTypes;
  843. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  844. Procedure TestRTTI_TypeInfo_LocalFail;
  845. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  846. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  847. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  848. Procedure TestRTTI_TypeInfo_FunctionClassType;
  849. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  850. Procedure TestRTTI_Interface_Corba;
  851. Procedure TestRTTI_Interface_COM;
  852. Procedure TestRTTI_ClassHelper;
  853. Procedure TestRTTI_ExternalClass;
  854. Procedure TestRTTI_Unit;
  855. // Resourcestring
  856. Procedure TestResourcestringProgram;
  857. Procedure TestResourcestringUnit;
  858. Procedure TestResourcestringImplementation;
  859. // Attributes
  860. Procedure TestAttributes_Members;
  861. Procedure TestAttributes_Types;
  862. Procedure TestAttributes_HelperConstructor_Fail;
  863. Procedure TestAttributes_InterfacesList;
  864. // Assertions, checks
  865. procedure TestAssert;
  866. procedure TestAssert_SysUtils;
  867. procedure TestObjectChecks;
  868. procedure TestOverflowChecks_Int;
  869. procedure TestRangeChecks_AssignInt;
  870. procedure TestRangeChecks_AssignIntRange;
  871. procedure TestRangeChecks_AssignEnum;
  872. procedure TestRangeChecks_AssignEnumRange;
  873. procedure TestRangeChecks_AssignChar;
  874. procedure TestRangeChecks_AssignCharRange;
  875. procedure TestRangeChecks_ArrayIndex;
  876. procedure TestRangeChecks_ArrayOfRecIndex;
  877. procedure TestRangeChecks_StringIndex;
  878. procedure TestRangeChecks_TypecastInt;
  879. procedure TestRangeChecks_TypeHelperInt;
  880. // Async/AWait
  881. Procedure TestAsync_Proc;
  882. Procedure TestAsync_CallResultIsPromise;
  883. Procedure TestAsync_ConstructorFail;
  884. Procedure TestAsync_PropertyGetterFail;
  885. Procedure TestAwait_NonPromiseWithTypeFail;
  886. Procedure TestAwait_AsyncCallTypeMismatch;
  887. Procedure TestAWait_OutsideAsyncFail;
  888. Procedure TestAWait_IntegerFail;
  889. Procedure TestAWait_ExternalClassPromise;
  890. Procedure TestAWait_JSValue;
  891. Procedure TestAWait_Result;
  892. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  893. Procedure TestAsync_AnonymousProc;
  894. Procedure TestAsync_ProcType;
  895. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  896. Procedure TestAsync_Inherited;
  897. Procedure TestAsync_ClassInterface;
  898. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  899. Procedure TestAWait_ClassAs;
  900. // Library
  901. Procedure TestLibrary_Empty;
  902. Procedure TestLibrary_ExportFunc;
  903. Procedure TestLibrary_ExportFuncOverloadedFail;
  904. Procedure TestLibrary_Export_Index_Fail;
  905. Procedure TestLibrary_ExportVar;
  906. Procedure TestLibrary_ExportUnitFunc;
  907. // ToDo: test delayed specialization init
  908. // ToDo: shortrefoptimization
  909. end;
  910. function LinesToStr(Args: array of const): string;
  911. function ExtractFileUnitName(aFilename: string): string;
  912. function JSToStr(El: TJSElement): string;
  913. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  914. implementation
  915. function LinesToStr(Args: array of const): string;
  916. var
  917. s: String;
  918. i: Integer;
  919. begin
  920. s:='';
  921. for i:=Low(Args) to High(Args) do
  922. case Args[i].VType of
  923. vtChar: s += Args[i].VChar+LineEnding;
  924. vtString: s += Args[i].VString^+LineEnding;
  925. vtPChar: s += Args[i].VPChar+LineEnding;
  926. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  927. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  928. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  929. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  930. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  931. end;
  932. Result:=s;
  933. end;
  934. function ExtractFileUnitName(aFilename: string): string;
  935. var
  936. p: Integer;
  937. begin
  938. Result:=ExtractFileName(aFilename);
  939. if Result='' then exit;
  940. for p:=length(Result) downto 1 do
  941. case Result[p] of
  942. '/','\': exit;
  943. '.':
  944. begin
  945. Delete(Result,p,length(Result));
  946. exit;
  947. end;
  948. end;
  949. end;
  950. function JSToStr(El: TJSElement): string;
  951. var
  952. aWriter: TBufferWriter;
  953. aJSWriter: TJSWriter;
  954. begin
  955. aJSWriter:=nil;
  956. aWriter:=TBufferWriter.Create(1000);
  957. try
  958. aJSWriter:=TJSWriter.Create(aWriter);
  959. aJSWriter.IndentSize:=2;
  960. aJSWriter.WriteJS(El);
  961. Result:=aWriter.AsString;
  962. finally
  963. aJSWriter.Free;
  964. aWriter.Free;
  965. end;
  966. end;
  967. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  968. // search diff, ignore changes in spaces
  969. const
  970. SpaceChars = [#9,#10,#13,' '];
  971. var
  972. ExpectedP, ActualP: PChar;
  973. function FindLineEnd(p: PChar): PChar;
  974. begin
  975. Result:=p;
  976. while not (Result^ in [#0,#10,#13]) do inc(Result);
  977. end;
  978. function FindLineStart(p, MinP: PChar): PChar;
  979. begin
  980. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  981. Result:=p;
  982. end;
  983. procedure SkipLineEnd(var p: PChar);
  984. begin
  985. if p^ in [#10,#13] then
  986. begin
  987. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  988. inc(p,2)
  989. else
  990. inc(p);
  991. end;
  992. end;
  993. function HasSpecialChar(s: string): boolean;
  994. var
  995. i: Integer;
  996. begin
  997. for i:=1 to length(s) do
  998. if s[i] in [#0..#31,#127..#255] then
  999. exit(true);
  1000. Result:=false;
  1001. end;
  1002. function HashSpecialChars(s: string): string;
  1003. var
  1004. i: Integer;
  1005. begin
  1006. Result:='';
  1007. for i:=1 to length(s) do
  1008. if s[i] in [#0..#31,#127..#255] then
  1009. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1010. else
  1011. Result:=Result+s[i];
  1012. end;
  1013. procedure DiffFound;
  1014. var
  1015. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1016. ExpLine, ActLine: String;
  1017. i, LineNo, DiffLineNo: Integer;
  1018. begin
  1019. writeln('Diff found "',Msg,'". Lines:');
  1020. // write correct lines
  1021. p:=PChar(Expected);
  1022. LineNo:=0;
  1023. DiffLineNo:=0;
  1024. repeat
  1025. StartPos:=p;
  1026. while not (p^ in [#0,#10,#13]) do inc(p);
  1027. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1028. SkipLineEnd(p);
  1029. inc(LineNo);
  1030. if (p<=ExpectedP) and (p^<>#0) then
  1031. begin
  1032. writeln('= ',ExpLine);
  1033. end else begin
  1034. // diff line
  1035. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1036. // write actual line
  1037. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1038. ActLineEndP:=FindLineEnd(ActualP);
  1039. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1040. writeln('- ',ActLine);
  1041. if HasSpecialChar(ActLine) then
  1042. writeln('- ',HashSpecialChars(ActLine));
  1043. // write expected line
  1044. writeln('+ ',ExpLine);
  1045. if HasSpecialChar(ExpLine) then
  1046. writeln('- ',HashSpecialChars(ExpLine));
  1047. // write empty line with pointer ^
  1048. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1049. writeln('^');
  1050. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1051. CheckSrcDiff:=false;
  1052. // write up to three following actual lines to get some context
  1053. for i:=1 to 3 do begin
  1054. ActLineStartP:=ActLineEndP;
  1055. SkipLineEnd(ActLineStartP);
  1056. if ActLineStartP^=#0 then break;
  1057. ActLineEndP:=FindLineEnd(ActLineStartP);
  1058. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1059. writeln('~ ',ActLine);
  1060. end;
  1061. exit;
  1062. end;
  1063. until p^=#0;
  1064. writeln('DiffFound Actual:-----------------------');
  1065. writeln(Actual);
  1066. writeln('DiffFound Expected:---------------------');
  1067. writeln(Expected);
  1068. writeln('DiffFound ------------------------------');
  1069. Msg:='diff found, but lines are the same, internal error';
  1070. CheckSrcDiff:=false;
  1071. end;
  1072. var
  1073. IsSpaceNeeded: Boolean;
  1074. LastChar, Quote: Char;
  1075. begin
  1076. Result:=true;
  1077. Msg:='';
  1078. if Expected='' then Expected:=' ';
  1079. if Actual='' then Actual:=' ';
  1080. ExpectedP:=PChar(Expected);
  1081. ActualP:=PChar(Actual);
  1082. repeat
  1083. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1084. case ExpectedP^ of
  1085. #0:
  1086. begin
  1087. // check that rest of Actual has only spaces
  1088. while ActualP^ in SpaceChars do inc(ActualP);
  1089. if ActualP^<>#0 then
  1090. begin
  1091. DiffFound;
  1092. exit;
  1093. end;
  1094. exit(true);
  1095. end;
  1096. ' ',#9,#10,#13:
  1097. begin
  1098. // skip space in Expected
  1099. IsSpaceNeeded:=false;
  1100. if ExpectedP>PChar(Expected) then
  1101. LastChar:=ExpectedP[-1]
  1102. else
  1103. LastChar:=#0;
  1104. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1105. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1106. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1107. IsSpaceNeeded:=true;
  1108. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1109. begin
  1110. DiffFound;
  1111. exit;
  1112. end;
  1113. while ActualP^ in SpaceChars do inc(ActualP);
  1114. end;
  1115. '''','"':
  1116. begin
  1117. while ActualP^ in SpaceChars do inc(ActualP);
  1118. if ExpectedP^<>ActualP^ then
  1119. begin
  1120. DiffFound;
  1121. exit;
  1122. end;
  1123. Quote:=ExpectedP^;
  1124. repeat
  1125. inc(ExpectedP);
  1126. inc(ActualP);
  1127. if ExpectedP^<>ActualP^ then
  1128. begin
  1129. DiffFound;
  1130. exit;
  1131. end;
  1132. if (ExpectedP^ in [#0,#10,#13]) then
  1133. break
  1134. else if (ExpectedP^=Quote) then
  1135. begin
  1136. inc(ExpectedP);
  1137. inc(ActualP);
  1138. break;
  1139. end;
  1140. until false;
  1141. end;
  1142. else
  1143. while ActualP^ in SpaceChars do inc(ActualP);
  1144. if ExpectedP^<>ActualP^ then
  1145. begin
  1146. DiffFound;
  1147. exit;
  1148. end;
  1149. inc(ExpectedP);
  1150. inc(ActualP);
  1151. end;
  1152. until false;
  1153. end;
  1154. { TTestEnginePasResolver }
  1155. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1156. begin
  1157. if FModule=AValue then Exit;
  1158. FModule:=AValue;
  1159. end;
  1160. destructor TTestEnginePasResolver.Destroy;
  1161. begin
  1162. FreeAndNil(FStreamResolver);
  1163. FreeAndNil(FParser);
  1164. FreeAndNil(FScanner);
  1165. FreeAndNil(FStreamResolver);
  1166. Module:=nil;
  1167. inherited Destroy;
  1168. end;
  1169. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1170. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1171. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1172. begin
  1173. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1174. TypeParams);
  1175. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1176. Module:=TPasModule(Result);
  1177. end;
  1178. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1179. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1180. begin
  1181. Result:=nil;
  1182. if InFilename<>'' then
  1183. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1184. if Assigned(OnFindUnit) then
  1185. Result:=OnFindUnit(AName);
  1186. if NameExpr=nil then ;
  1187. end;
  1188. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1189. begin
  1190. // do not parse recursively
  1191. // parse via the queue
  1192. if Section=nil then ;
  1193. end;
  1194. { TCustomTestModule }
  1195. procedure TCustomTestModule.FreeSrcMarkers;
  1196. var
  1197. aMarker, Last: PSrcMarker;
  1198. begin
  1199. aMarker:=FirstSrcMarker;
  1200. while aMarker<>nil do
  1201. begin
  1202. Last:=aMarker;
  1203. aMarker:=aMarker^.Next;
  1204. Dispose(Last);
  1205. end;
  1206. FirstSrcMarker:=nil;
  1207. LastSrcMarker:=nil;
  1208. end;
  1209. function TCustomTestModule.GetResolverCount: integer;
  1210. begin
  1211. Result:=FResolvers.Count;
  1212. end;
  1213. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1214. begin
  1215. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1216. end;
  1217. function TCustomTestModule.GetMsgCount: integer;
  1218. begin
  1219. Result:=FHintMsgs.Count;
  1220. end;
  1221. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1222. begin
  1223. Result:=TTestHintMessage(FHintMsgs[Index]);
  1224. end;
  1225. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1226. ): TPasModule;
  1227. var
  1228. DefNamespace: String;
  1229. begin
  1230. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1231. if (Pos('.',aUnitName)<1) then
  1232. begin
  1233. DefNamespace:=GetDefaultNamespace;
  1234. if DefNamespace<>'' then
  1235. begin
  1236. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1237. if Result<>nil then exit;
  1238. end;
  1239. end;
  1240. Result:=LoadUnit(aUnitName);
  1241. if Result<>nil then exit;
  1242. {$IFDEF VerbosePas2JS}
  1243. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1244. {$ENDIF}
  1245. Fail('can''t find unit "'+aUnitName+'"');
  1246. end;
  1247. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1248. var
  1249. aParser: TPasParser;
  1250. Item: TTestHintMessage;
  1251. begin
  1252. aParser:=Sender as TPasParser;
  1253. Item:=TTestHintMessage.Create;
  1254. Item.Id:=aParser.LastMsgNumber;
  1255. Item.MsgType:=aParser.LastMsgType;
  1256. Item.MsgNumber:=aParser.LastMsgNumber;
  1257. Item.Msg:=Msg;
  1258. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1259. {$IFDEF VerbosePas2JS}
  1260. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1261. {$ENDIF}
  1262. FHintMsgs.Add(Item);
  1263. end;
  1264. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1265. );
  1266. var
  1267. aResolver: TTestEnginePasResolver;
  1268. Item: TTestHintMessage;
  1269. begin
  1270. aResolver:=Sender as TTestEnginePasResolver;
  1271. Item:=TTestHintMessage.Create;
  1272. Item.Id:=aResolver.LastMsgId;
  1273. Item.MsgType:=aResolver.LastMsgType;
  1274. Item.MsgNumber:=aResolver.LastMsgNumber;
  1275. Item.Msg:=Msg;
  1276. Item.SourcePos:=aResolver.LastSourcePos;
  1277. {$IFDEF VerbosePas2JS}
  1278. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1279. {$ENDIF}
  1280. FHintMsgs.Add(Item);
  1281. end;
  1282. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1283. var
  1284. Item: TTestHintMessage;
  1285. aScanner: TPas2jsPasScanner;
  1286. begin
  1287. aScanner:=Sender as TPas2jsPasScanner;
  1288. Item:=TTestHintMessage.Create;
  1289. Item.Id:=aScanner.LastMsgNumber;
  1290. Item.MsgType:=aScanner.LastMsgType;
  1291. Item.MsgNumber:=aScanner.LastMsgNumber;
  1292. Item.Msg:=Msg;
  1293. Item.SourcePos:=aScanner.CurSourcePos;
  1294. {$IFDEF VerbosePas2JS}
  1295. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1296. {$ENDIF}
  1297. FHintMsgs.Add(Item);
  1298. end;
  1299. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1300. var
  1301. SubEl: TPasElement;
  1302. i: Integer;
  1303. procedure E(Msg: string);
  1304. var
  1305. s: String;
  1306. begin
  1307. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1308. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1309. writeln('ERROR: ',s);
  1310. Fail(s);
  1311. end;
  1312. begin
  1313. if arg=nil then ;
  1314. if El=nil then exit;
  1315. if El.Parent=El then
  1316. E('El.Parent=El='+GetObjName(El));
  1317. if El is TBinaryExpr then
  1318. begin
  1319. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1320. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1321. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1322. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1323. end
  1324. else if El is TParamsExpr then
  1325. begin
  1326. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1327. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1328. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1329. if TParamsExpr(El).Params[i].Parent<>El then
  1330. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1331. end
  1332. else if El is TProcedureExpr then
  1333. begin
  1334. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1335. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1336. end
  1337. else if El is TPasDeclarations then
  1338. begin
  1339. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1340. begin
  1341. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1342. if SubEl.Parent<>El then
  1343. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1344. end;
  1345. end
  1346. else if El is TPasImplBlock then
  1347. begin
  1348. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1349. begin
  1350. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1351. if SubEl.Parent<>El then
  1352. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1353. end;
  1354. end
  1355. else if El is TPasImplWithDo then
  1356. begin
  1357. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1358. begin
  1359. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1360. if SubEl.Parent<>El then
  1361. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1362. end;
  1363. end
  1364. else if El is TPasProcedure then
  1365. begin
  1366. if TPasProcedure(El).ProcType.Parent<>El then
  1367. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1368. end
  1369. else if El is TPasProcedureType then
  1370. begin
  1371. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1372. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1373. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1374. end;
  1375. end;
  1376. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1377. var
  1378. Data: PTestResolverReferenceData absolute FindData;
  1379. Line, Col: integer;
  1380. begin
  1381. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1382. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1383. if (Data^.Filename=El.SourceFilename)
  1384. and (Data^.Row=Line)
  1385. and (Data^.StartCol<=Col)
  1386. and (Data^.EndCol>=Col)
  1387. then
  1388. Data^.Found.Add(El);
  1389. end;
  1390. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1391. begin
  1392. if FWithTypeInfo=AValue then Exit;
  1393. FWithTypeInfo:=AValue;
  1394. if AValue then
  1395. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1396. else
  1397. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1398. end;
  1399. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1400. var
  1401. i: Integer;
  1402. CurEngine: TTestEnginePasResolver;
  1403. CurUnitName: String;
  1404. begin
  1405. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1406. Result:=nil;
  1407. if (Module.ClassType=TPasModule)
  1408. and (CompareText(Module.Name,aUnitName)=0) then
  1409. exit(Module);
  1410. for i:=0 to ResolverCount-1 do
  1411. begin
  1412. CurEngine:=Resolvers[i];
  1413. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1414. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1415. if CompareText(aUnitName,CurUnitName)=0 then
  1416. begin
  1417. Result:=CurEngine.Module;
  1418. if Result<>nil then exit;
  1419. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1420. FileResolver.FindSourceFile(aUnitName);
  1421. CurEngine.StreamResolver:=TStreamResolver.Create;
  1422. CurEngine.StreamResolver.OwnsStreams:=True;
  1423. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1424. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1425. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1426. InitScanner(CurEngine.Scanner);
  1427. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1428. CurEngine.Parser.Options:=po_tcmodules;
  1429. if CompareText(CurUnitName,'System')=0 then
  1430. CurEngine.Parser.ImplicitUses.Clear;
  1431. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1432. try
  1433. CurEngine.Parser.NextToken;
  1434. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1435. except
  1436. on E: Exception do
  1437. HandleException(E);
  1438. end;
  1439. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1440. Result:=CurEngine.Module;
  1441. exit;
  1442. end;
  1443. end;
  1444. end;
  1445. procedure TCustomTestModule.SetUp;
  1446. begin
  1447. {$IFDEF EnablePasTreeGlobalRefCount}
  1448. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1449. {$ENDIF}
  1450. if FResolvers<>nil then
  1451. begin
  1452. writeln('TCustomTestModule.SetUp FModules<>nil');
  1453. Halt;
  1454. end;
  1455. inherited SetUp;
  1456. FSkipTests:=false;
  1457. FWithTypeInfo:=false;
  1458. FSource:=TStringList.Create;
  1459. FHub:=TPas2JSResolverHub.Create(Self);
  1460. FResolvers:=TObjectList.Create(true);
  1461. FFilename:='test1.pp';
  1462. FFileResolver:=TStreamResolver.Create;
  1463. FFileResolver.OwnsStreams:=True;
  1464. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1465. InitScanner(FScanner);
  1466. FEngine:=AddModule(Filename);
  1467. FEngine.Scanner:=FScanner;
  1468. FScanner.Resolver:=FEngine;
  1469. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1470. FParser.OnLog:=@OnParserLog;
  1471. FEngine.Parser:=FParser;
  1472. Parser.Options:=po_tcmodules;
  1473. FModule:=Nil;
  1474. FConverter:=CreateConverter;
  1475. FExpectedErrorClass:=nil;
  1476. end;
  1477. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1478. var
  1479. Options: TPasToJsConverterOptions;
  1480. begin
  1481. Result:=TPasToJSConverter.Create;
  1482. Options:=co_tcmodules;
  1483. if WithTypeInfo then
  1484. Exclude(Options,coNoTypeInfo)
  1485. else
  1486. Include(Options,coNoTypeInfo);
  1487. Result.Options:=Options;
  1488. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1489. end;
  1490. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1491. begin
  1492. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1493. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1494. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1495. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1496. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1497. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1498. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1499. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1500. aScanner.OnLog:=@OnScannerLog;
  1501. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1502. end;
  1503. procedure TCustomTestModule.TearDown;
  1504. {$IFDEF CheckPasTreeRefCount}
  1505. var
  1506. El: TPasElement;
  1507. {$ENDIF}
  1508. var
  1509. i: Integer;
  1510. CurModule: TPasModule;
  1511. begin
  1512. FreeSrcMarkers;
  1513. FHintMsgs.Clear;
  1514. FHintMsgsGood.Clear;
  1515. FSkipTests:=false;
  1516. FWithTypeInfo:=false;
  1517. FJSRegModuleCall:=nil;
  1518. FJSModuleCallArgs:=nil;
  1519. FJSImplentationUses:=nil;
  1520. FJSInterfaceUses:=nil;
  1521. FJSModuleSrc:=nil;
  1522. FJSInitBody:=nil;
  1523. FreeAndNil(FJSSource);
  1524. FreeAndNil(FJSModule);
  1525. FreeAndNil(FConverter);
  1526. ResolverEngine.Clear;
  1527. FreeAndNil(FSource);
  1528. FreeAndNil(FFileResolver);
  1529. if FResolvers<>nil then
  1530. begin
  1531. for i:=0 to FResolvers.Count-1 do
  1532. begin
  1533. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1534. if CurModule=nil then continue;
  1535. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1536. CurModule.ReleaseUsedUnits;
  1537. end;
  1538. if FModule<>nil then
  1539. FModule.ReleaseUsedUnits;
  1540. for i:=0 to FResolvers.Count-1 do
  1541. begin
  1542. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1543. if CurModule=nil then continue;
  1544. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1545. end;
  1546. FreeAndNil(FResolvers);
  1547. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1548. FEngine:=nil;
  1549. end;
  1550. FreeAndNil(FHub);
  1551. inherited TearDown;
  1552. {$IFDEF EnablePasTreeGlobalRefCount}
  1553. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1554. begin
  1555. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1556. {$IFDEF CheckPasTreeRefCount}
  1557. El:=TPasElement.FirstRefEl;
  1558. while El<>nil do
  1559. begin
  1560. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1561. for i:=0 to El.RefIds.Count-1 do
  1562. writeln(' ',El.RefIds[i]);
  1563. El:=El.NextRefEl;
  1564. end;
  1565. {$ENDIF}
  1566. Halt;
  1567. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1568. end;
  1569. {$ENDIF}
  1570. end;
  1571. procedure TCustomTestModule.Add(Line: string);
  1572. begin
  1573. Source.Add(Line);
  1574. end;
  1575. procedure TCustomTestModule.Add(const Lines: array of string);
  1576. var
  1577. i: Integer;
  1578. begin
  1579. for i:=low(Lines) to high(Lines) do
  1580. Add(Lines[i]);
  1581. end;
  1582. procedure TCustomTestModule.StartParsing;
  1583. var
  1584. Src: String;
  1585. begin
  1586. Src:=Source.Text;
  1587. FEngine.Source:=Src;
  1588. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1589. Scanner.OpenFile(FileName);
  1590. Writeln('// Test : ',Self.TestName);
  1591. Writeln(Src);
  1592. end;
  1593. procedure TCustomTestModule.ParseModuleQueue;
  1594. var
  1595. i: Integer;
  1596. CurResolver: TTestEnginePasResolver;
  1597. Found: Boolean;
  1598. Section: TPasSection;
  1599. begin
  1600. // parse til exception or all Resolvers finished
  1601. while not SkipTests do
  1602. begin
  1603. Found:=false;
  1604. for i:=0 to ResolverCount-1 do
  1605. begin
  1606. CurResolver:=Resolvers[i];
  1607. if CurResolver.CurrentParser=nil then continue;
  1608. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1609. continue;
  1610. CurResolver.Parser.ParseContinue;
  1611. Found:=true;
  1612. break;
  1613. end;
  1614. if not Found then break;
  1615. end;
  1616. for i:=0 to ResolverCount-1 do
  1617. begin
  1618. CurResolver:=Resolvers[i];
  1619. if CurResolver.Parser=nil then
  1620. begin
  1621. if CurResolver.CurrentParser<>nil then
  1622. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1623. continue;
  1624. end;
  1625. if CurResolver.Parser.CurModule<>nil then
  1626. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1627. end;
  1628. end;
  1629. procedure TCustomTestModule.ParseModule;
  1630. begin
  1631. if SkipTests then exit;
  1632. FFirstPasStatement:=nil;
  1633. try
  1634. StartParsing;
  1635. Parser.ParseMain(FModule);
  1636. ParseModuleQueue;
  1637. except
  1638. on E: Exception do
  1639. HandleException(E);
  1640. end;
  1641. if SkipTests then exit;
  1642. AssertNotNull('Module resulted in Module',Module);
  1643. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1644. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1645. end;
  1646. procedure TCustomTestModule.ParseProgram;
  1647. begin
  1648. if SkipTests then exit;
  1649. ParseModule;
  1650. if SkipTests then exit;
  1651. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1652. FPasProgram:=TPasProgram(Module);
  1653. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1654. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1655. if (PasProgram.InitializationSection.Elements.Count>0) then
  1656. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1657. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1658. end;
  1659. procedure TCustomTestModule.ParseLibrary;
  1660. var
  1661. Init: TInitializationSection;
  1662. begin
  1663. if SkipTests then exit;
  1664. ParseModule;
  1665. if SkipTests then exit;
  1666. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1667. FPasLibrary:=TPasLibrary(Module);
  1668. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1669. Init:=PasLibrary.InitializationSection;
  1670. if (Init<>nil) and (Init.Elements.Count>0) then
  1671. if TObject(Init.Elements[0]) is TPasImplBlock then
  1672. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1673. end;
  1674. procedure TCustomTestModule.ParseUnit;
  1675. begin
  1676. if SkipTests then exit;
  1677. ParseModule;
  1678. if SkipTests then exit;
  1679. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1680. AssertNotNull('Has interface section',Module.InterfaceSection);
  1681. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1682. if (Module.InitializationSection<>nil)
  1683. and (Module.InitializationSection.Elements.Count>0)
  1684. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1685. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1686. end;
  1687. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1688. ): TTestEnginePasResolver;
  1689. var
  1690. i: Integer;
  1691. begin
  1692. for i:=0 to ResolverCount-1 do
  1693. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1694. exit(Resolvers[i]);
  1695. Result:=nil;
  1696. end;
  1697. function TCustomTestModule.AddModule(aFilename: string
  1698. ): TTestEnginePasResolver;
  1699. begin
  1700. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1701. if FindModuleWithFilename(aFilename)<>nil then
  1702. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1703. Result:=TTestEnginePasResolver.Create;
  1704. Result.Filename:=aFilename;
  1705. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1706. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1707. Result.OnLog:=@OnPasResolverLog;
  1708. Result.Hub:=Hub;
  1709. FResolvers.Add(Result);
  1710. end;
  1711. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1712. ): TTestEnginePasResolver;
  1713. begin
  1714. Result:=AddModule(aFilename);
  1715. Result.Source:=Src;
  1716. end;
  1717. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1718. ImplementationSrc: string): TTestEnginePasResolver;
  1719. var
  1720. Src: String;
  1721. begin
  1722. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1723. Src+=LineEnding;
  1724. Src+='interface'+LineEnding;
  1725. Src+=LineEnding;
  1726. Src+=InterfaceSrc;
  1727. Src+='implementation'+LineEnding;
  1728. Src+=LineEnding;
  1729. Src+=ImplementationSrc;
  1730. Src+='end.'+LineEnding;
  1731. Result:=AddModuleWithSrc(aFilename,Src);
  1732. end;
  1733. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1734. var
  1735. Intf, Impl: TStringList;
  1736. begin
  1737. Intf:=TStringList.Create;
  1738. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1739. // unit interface
  1740. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1741. Intf.Add('{$modeswitch externalclass}');
  1742. Intf.Add('type');
  1743. Intf.Add(' integer=longint;');
  1744. Intf.Add(' sizeint=nativeint;');
  1745. //'const',
  1746. //' LineEnding = #10;',
  1747. //' DirectorySeparator = ''/'';',
  1748. //' DriveSeparator = '''';',
  1749. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1750. //' AllowDriveSeparators : set of char = [];',
  1751. if supTObject in Parts then
  1752. Intf.AddStrings([
  1753. 'type',
  1754. ' TClass = class of TObject;',
  1755. ' TObject = class',
  1756. ' constructor Create;',
  1757. ' destructor Destroy; virtual;',
  1758. ' class function ClassType: TClass; assembler;',
  1759. ' class function ClassName: String; assembler;',
  1760. ' class function ClassNameIs(const Name: string): boolean;',
  1761. ' class function ClassParent: TClass; assembler;',
  1762. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1763. ' class function UnitName: String; assembler;',
  1764. ' procedure AfterConstruction; virtual;',
  1765. ' procedure BeforeDestruction;virtual;',
  1766. ' function Equals(Obj: TObject): boolean; virtual;',
  1767. ' function ToString: String; virtual;',
  1768. ' end;']);
  1769. if supTInterfacedObject in Parts then
  1770. Intf.AddStrings([
  1771. ' {$Interfaces COM}',
  1772. ' IUnknown = interface',
  1773. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1774. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1775. ' function _AddRef: Integer;',
  1776. ' function _Release: Integer;',
  1777. ' end;',
  1778. ' IInterface = IUnknown;',
  1779. ' TInterfacedObject = class(TObject,IUnknown)',
  1780. ' protected',
  1781. ' fRefCount: Integer;',
  1782. ' { implement methods of IUnknown }',
  1783. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1784. ' function _AddRef: Integer; virtual;',
  1785. ' function _Release: Integer; virtual;',
  1786. ' end;',
  1787. ' TInterfacedClass = class of TInterfacedObject;',
  1788. '',
  1789. '']);
  1790. if supTVarRec in Parts then
  1791. Intf.AddStrings([
  1792. 'const',
  1793. ' vtInteger = 0;',
  1794. ' vtBoolean = 1;',
  1795. ' vtJSValue = 19;',
  1796. 'type',
  1797. ' PVarRec = ^TVarRec;',
  1798. ' TVarRec = record',
  1799. ' VType : byte;',
  1800. ' VJSValue: JSValue;',
  1801. ' vInteger: longint external name ''VJSValue'';',
  1802. ' vBoolean: boolean external name ''VJSValue'';',
  1803. ' end;',
  1804. ' TVarRecArray = array of TVarRec;',
  1805. 'function VarRecs: TVarRecArray; varargs;',
  1806. '']);
  1807. if supTypeInfo in Parts then
  1808. begin
  1809. Intf.AddStrings([
  1810. 'type',
  1811. ' TTypeKind = (',
  1812. ' tkUnknown, // 0',
  1813. ' tkInteger, // 1',
  1814. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1815. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1816. ' tkEnumeration, // 4',
  1817. ' tkSet, // 5',
  1818. ' tkDouble, // 6',
  1819. ' tkBool, // 7',
  1820. ' tkProcVar, // 8 function or procedure',
  1821. ' tkMethod, // 9 proc var of object',
  1822. ' tkArray, // 10 static array',
  1823. ' tkDynArray, // 11',
  1824. ' tkRecord, // 12',
  1825. ' tkClass, // 13',
  1826. ' tkClassRef, // 14',
  1827. ' tkPointer, // 15',
  1828. ' tkJSValue, // 16',
  1829. ' tkRefToProcVar, // 17 variable of procedure type',
  1830. ' tkInterface, // 18',
  1831. ' //tkObject,',
  1832. ' //tkSString,tkLString,tkAString,tkWString,',
  1833. ' //tkVariant,',
  1834. ' //tkWChar,',
  1835. ' //tkInt64,',
  1836. ' //tkQWord,',
  1837. ' //tkInterfaceRaw,',
  1838. ' //tkUString,tkUChar,',
  1839. ' tkHelper, // 19',
  1840. ' //tkFile,',
  1841. ' tkExtClass // 20',
  1842. ' );',
  1843. ' TTypeKinds = set of TTypeKind;',
  1844. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1845. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1846. ' end;',
  1847. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1848. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1849. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1850. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1851. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1852. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1853. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1854. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1855. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1856. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1857. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1858. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1859. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1860. '']);
  1861. end;
  1862. if supWriteln in Parts then
  1863. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1864. Intf.Add('var');
  1865. Intf.Add(' ExitCode: Longint = 0;');
  1866. // unit implementation
  1867. Impl:=TStringList.Create;
  1868. if supTObject in Parts then
  1869. Impl.AddStrings([
  1870. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1871. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1872. 'asm',
  1873. 'end;',
  1874. 'constructor TObject.Create; begin end;',
  1875. 'destructor TObject.Destroy; begin end;',
  1876. 'class function TObject.ClassType: TClass; assembler;',
  1877. 'asm',
  1878. 'end;',
  1879. 'class function TObject.ClassName: String; assembler;',
  1880. 'asm',
  1881. 'end;',
  1882. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1883. 'begin',
  1884. ' Result:=SameText(Name,ClassName);',
  1885. 'end;',
  1886. 'class function TObject.ClassParent: TClass; assembler;',
  1887. 'asm',
  1888. 'end;',
  1889. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1890. 'asm',
  1891. 'end;',
  1892. 'class function TObject.UnitName: String; assembler;',
  1893. 'asm',
  1894. 'end;',
  1895. 'procedure TObject.AfterConstruction; begin end;',
  1896. 'procedure TObject.BeforeDestruction; begin end;',
  1897. 'function TObject.Equals(Obj: TObject): boolean;',
  1898. 'begin',
  1899. ' Result:=Obj=Self;',
  1900. 'end;',
  1901. 'function TObject.ToString: String;',
  1902. 'begin',
  1903. ' Result:=ClassName;',
  1904. 'end;'
  1905. ]);
  1906. if supTInterfacedObject in Parts then
  1907. Impl.AddStrings([
  1908. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1909. //'begin',
  1910. //'end;',
  1911. 'function TInterfacedObject._AddRef: Integer;',
  1912. 'begin',
  1913. 'end;',
  1914. 'function TInterfacedObject._Release: Integer;',
  1915. 'begin',
  1916. 'end;',
  1917. '']);
  1918. if supTVarRec in Parts then
  1919. Impl.AddStrings([
  1920. 'function VarRecs: TVarRecArray; varargs;',
  1921. 'var',
  1922. ' v: PVarRec;',
  1923. 'begin',
  1924. ' v^.VType:=1;',
  1925. ' v^.VJSValue:=2;',
  1926. 'end;',
  1927. '']);
  1928. try
  1929. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1930. finally
  1931. Intf.Free;
  1932. Impl.Free;
  1933. end;
  1934. end;
  1935. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1936. SystemUnitParts: TSystemUnitParts);
  1937. begin
  1938. if NeedSystemUnit then
  1939. AddSystemUnit(SystemUnitParts)
  1940. else
  1941. Parser.ImplicitUses.Clear;
  1942. Add('program '+ExtractFileUnitName(Filename)+';');
  1943. Add('');
  1944. end;
  1945. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1946. SystemUnitParts: TSystemUnitParts);
  1947. begin
  1948. if NeedSystemUnit then
  1949. AddSystemUnit(SystemUnitParts)
  1950. else
  1951. Parser.ImplicitUses.Clear;
  1952. Add('library '+ExtractFileUnitName(Filename)+';');
  1953. Add('');
  1954. end;
  1955. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1956. SystemUnitParts: TSystemUnitParts);
  1957. begin
  1958. if NeedSystemUnit then
  1959. AddSystemUnit(SystemUnitParts)
  1960. else
  1961. Parser.ImplicitUses.Clear;
  1962. Add('unit Test1;');
  1963. Add('');
  1964. end;
  1965. procedure TCustomTestModule.ConvertModule;
  1966. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1967. out UsesLit: TJSArrayLiteral);
  1968. var
  1969. i: Integer;
  1970. Item: TJSElement;
  1971. Lit: TJSLiteral;
  1972. begin
  1973. UsesLit:=nil;
  1974. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1975. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1976. exit; // null is ok
  1977. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1978. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1979. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1980. begin
  1981. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1982. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1983. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1984. Lit:=TJSLiteral(Item);
  1985. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1986. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1987. end;
  1988. end;
  1989. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1990. out Src: TJSSourceElements);
  1991. var
  1992. FunDecl: TJSFunctionDeclarationStatement;
  1993. FunDef: TJSFuncDef;
  1994. FunBody: TJSFunctionBody;
  1995. begin
  1996. Src:=nil;
  1997. AssertNotNull(ParamName,Arg.Expr);
  1998. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1999. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2000. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2001. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2002. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2003. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2004. AssertNotNull(ParamName+' body',FunDef.Body);
  2005. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2006. FunBody:=FunDef.Body as TJSFunctionBody;
  2007. AssertNotNull(ParamName+' body.A',FunBody.A);
  2008. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2009. Src:=FunBody.A as TJSSourceElements;
  2010. end;
  2011. var
  2012. ModuleNameExpr: TJSLiteral;
  2013. InitFunction: TJSFunctionDeclarationStatement;
  2014. InitAssign: TJSSimpleAssignStatement;
  2015. InitName: String;
  2016. LastNode, FirstNode: TJSElement;
  2017. Arg: TJSArrayLiteralElement;
  2018. IsProg, IsLib: Boolean;
  2019. begin
  2020. if SkipTests then exit;
  2021. IsProg:=false;
  2022. IsLib:=false;
  2023. if Module is TPasProgram then
  2024. IsProg:=true
  2025. else if Module is TPasLibrary then
  2026. IsLib:=true;
  2027. try
  2028. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2029. except
  2030. on E: Exception do
  2031. HandleException(E);
  2032. end;
  2033. if SkipTests then exit;
  2034. if ExpectedErrorClass<>nil then
  2035. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2036. FJSSource:=TStringList.Create;
  2037. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2038. {$IFDEF VerbosePas2JS}
  2039. writeln('TTestModule.ConvertModule JS:');
  2040. write(FJSSource.Text);
  2041. {$ENDIF}
  2042. // rtl.module(...
  2043. if JSModule.Statements.Count<1 then
  2044. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2045. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2046. AssertNotNull('register module call',FirstNode);
  2047. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2048. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2049. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2050. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2051. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2052. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2053. // parameter 'unitname'
  2054. if JSModuleCallArgs.Elements.Count<1 then
  2055. Fail('rtl.module first param unit missing');
  2056. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2057. AssertNotNull('module name param',Arg.Expr);
  2058. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2059. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2060. if IsProg then
  2061. begin
  2062. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2063. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2064. end
  2065. else if IsLib then
  2066. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2067. else
  2068. begin
  2069. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2070. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2071. end;
  2072. // main uses section
  2073. if JSModuleCallArgs.Elements.Count<2 then
  2074. Fail('rtl.module second param main uses missing');
  2075. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2076. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2077. // program/library/interface function()
  2078. if JSModuleCallArgs.Elements.Count<3 then
  2079. Fail('rtl.module third param intf-function missing');
  2080. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2081. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2082. // search for $mod.$init or $mod.$main - the last statement
  2083. if IsProg or IsLib then
  2084. begin
  2085. InitName:='$main';
  2086. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2087. end
  2088. else
  2089. InitName:='$init';
  2090. InitAssign:=nil;
  2091. InitFunction:=nil;
  2092. FJSInitBody:=nil;
  2093. if JSModuleSrc.Statements.Count>0 then
  2094. begin
  2095. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2096. if LastNode is TJSSimpleAssignStatement then
  2097. begin
  2098. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2099. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2100. begin
  2101. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2102. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2103. end
  2104. else if IsProg or IsLib then
  2105. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2106. end;
  2107. end;
  2108. // optional: implementation uses section
  2109. if JSModuleCallArgs.Elements.Count<4 then
  2110. exit;
  2111. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2112. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2113. end;
  2114. procedure TCustomTestModule.ConvertProgram;
  2115. begin
  2116. Add('end.');
  2117. ParseProgram;
  2118. ConvertModule;
  2119. end;
  2120. procedure TCustomTestModule.ConvertLibrary;
  2121. begin
  2122. Add('end.');
  2123. ParseLibrary;
  2124. ConvertModule;
  2125. end;
  2126. procedure TCustomTestModule.ConvertUnit;
  2127. begin
  2128. Add('end.');
  2129. ParseUnit;
  2130. ConvertModule;
  2131. end;
  2132. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2133. begin
  2134. Result:=tcmodules.JSToStr(El);
  2135. end;
  2136. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2137. DottedName: string);
  2138. begin
  2139. if DottedName='' then
  2140. begin
  2141. AssertNull(Msg,El);
  2142. end
  2143. else
  2144. begin
  2145. AssertNotNull(Msg,El);
  2146. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2147. end;
  2148. end;
  2149. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2150. begin
  2151. if El=nil then
  2152. Result:=''
  2153. else if El is TJSPrimaryExpressionIdent then
  2154. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2155. else if El is TJSDotMemberExpression then
  2156. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2157. else
  2158. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2159. end;
  2160. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2161. InitStatements: string; ImplStatements: string);
  2162. var
  2163. ActualSrc, ExpectedSrc, InitName: String;
  2164. IsProg, IsLib: Boolean;
  2165. begin
  2166. ActualSrc:=JSToStr(JSModuleSrc);
  2167. if coUseStrict in Converter.Options then
  2168. ExpectedSrc:='"use strict";'+LineEnding
  2169. else
  2170. ExpectedSrc:='';
  2171. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2172. ExpectedSrc:=ExpectedSrc+Statements;
  2173. // unit implementation
  2174. if (Trim(ImplStatements)<>'') then
  2175. ExpectedSrc:=ExpectedSrc+LineEnding
  2176. +'$mod.$implcode = function () {'+LineEnding
  2177. +ImplStatements
  2178. +'};'+LineEnding;
  2179. // program main or unit initialization
  2180. IsProg:=false;
  2181. IsLib:=false;
  2182. if Module is TPasProgram then
  2183. IsProg:=true
  2184. else if Module is TPasLibrary then
  2185. IsLib:=true;
  2186. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2187. begin
  2188. if IsProg or IsLib then
  2189. InitName:='$main'
  2190. else
  2191. InitName:='$init';
  2192. ExpectedSrc:=ExpectedSrc+LineEnding
  2193. +'$mod.'+InitName+' = function () {'+LineEnding
  2194. +InitStatements
  2195. +'};'+LineEnding;
  2196. end;
  2197. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2198. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2199. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2200. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2201. end;
  2202. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2203. var
  2204. ActualSrc: String;
  2205. begin
  2206. ActualSrc:=JSToStr(JSModule);
  2207. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2208. end;
  2209. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2210. // search diff, ignore changes in spaces
  2211. var
  2212. s: string;
  2213. begin
  2214. if CheckSrcDiff(Expected,Actual,s) then exit;
  2215. Fail(Msg+': '+s);
  2216. end;
  2217. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2218. var
  2219. aResolver: TTestEnginePasResolver;
  2220. aConverter: TPasToJSConverter;
  2221. aJSModule: TJSSourceElements;
  2222. ActualSrc: String;
  2223. begin
  2224. aResolver:=GetResolver(Filename);
  2225. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2226. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2227. {$IFDEF VerbosePas2JS}
  2228. writeln('CheckUnit '+Filename+' converting ...');
  2229. {$ENDIF}
  2230. aConverter:=CreateConverter;
  2231. aJSModule:=nil;
  2232. try
  2233. try
  2234. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2235. except
  2236. on E: Exception do
  2237. HandleException(E);
  2238. end;
  2239. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2240. {$IFDEF VerbosePas2JS}
  2241. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2242. write(aResolver.Source);
  2243. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2244. write(ActualSrc);
  2245. {$ENDIF}
  2246. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2247. finally
  2248. aJSModule.Free;
  2249. aConverter.Free;
  2250. end;
  2251. end;
  2252. procedure TCustomTestModule.CheckReferenceDirectives;
  2253. var
  2254. CurFilename: string;
  2255. LineNumber: Integer;
  2256. SrcLine: String;
  2257. CommentStartP, CommentEndP: PChar;
  2258. procedure RaiseError(Msg: string; p: PChar);
  2259. begin
  2260. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2261. end;
  2262. procedure AddMarker(Marker: PSrcMarker);
  2263. begin
  2264. if LastSrcMarker<>nil then
  2265. LastSrcMarker^.Next:=Marker
  2266. else
  2267. FirstSrcMarker:=Marker;
  2268. LastSrcMarker:=Marker;
  2269. end;
  2270. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2271. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2272. begin
  2273. New(Result);
  2274. Result^.Kind:=Kind;
  2275. Result^.Filename:=aFilename;
  2276. Result^.Row:=aLine;
  2277. Result^.StartCol:=aStartCol;
  2278. Result^.EndCol:=aEndCol;
  2279. Result^.Identifier:=Identifier;
  2280. Result^.Next:=nil;
  2281. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2282. AddMarker(Result);
  2283. end;
  2284. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2285. const Identifier: string): PSrcMarker;
  2286. var
  2287. TokenStart, p: PChar;
  2288. begin
  2289. p:=CommentEndP;
  2290. ReadNextPascalToken(p,TokenStart,false,false);
  2291. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2292. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2293. end;
  2294. function ReadIdentifier(var p: PChar): string;
  2295. var
  2296. StartP: PChar;
  2297. begin
  2298. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2299. RaiseError('identifier expected',p);
  2300. StartP:=p;
  2301. inc(p);
  2302. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2303. Result:='';
  2304. SetLength(Result,p-StartP);
  2305. Move(StartP^,Result[1],length(Result));
  2306. end;
  2307. procedure AddLabel;
  2308. var
  2309. Identifier: String;
  2310. p: PChar;
  2311. begin
  2312. p:=CommentStartP+2;
  2313. Identifier:=ReadIdentifier(p);
  2314. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2315. if FindSrcLabel(Identifier)<>nil then
  2316. RaiseError('duplicate label "'+Identifier+'"',p);
  2317. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2318. end;
  2319. procedure AddResolverReference;
  2320. var
  2321. Identifier: String;
  2322. p: PChar;
  2323. begin
  2324. p:=CommentStartP+2;
  2325. Identifier:=ReadIdentifier(p);
  2326. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2327. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2328. end;
  2329. procedure AddDirectReference;
  2330. var
  2331. Identifier: String;
  2332. p: PChar;
  2333. begin
  2334. p:=CommentStartP+2;
  2335. Identifier:=ReadIdentifier(p);
  2336. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2337. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2338. end;
  2339. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2340. var
  2341. p: PChar;
  2342. IsDirective: Boolean;
  2343. begin
  2344. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2345. CurFilename:=aFilename;
  2346. // parse code, find all labels
  2347. LineNumber:=0;
  2348. while LineNumber<SrcLines.Count do
  2349. begin
  2350. inc(LineNumber);
  2351. SrcLine:=SrcLines[LineNumber-1];
  2352. if SrcLine='' then continue;
  2353. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2354. p:=PChar(SrcLine);
  2355. repeat
  2356. case p^ of
  2357. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2358. '{':
  2359. begin
  2360. CommentStartP:=p;
  2361. inc(p);
  2362. IsDirective:=p^ in ['#','@','='];
  2363. // skip to end of comment
  2364. repeat
  2365. case p^ of
  2366. #0:
  2367. if (p-PChar(SrcLine)=length(SrcLine)) then
  2368. begin
  2369. // multi line comment
  2370. if IsDirective then
  2371. RaiseError('directive missing closing bracket',CommentStartP);
  2372. repeat
  2373. inc(LineNumber);
  2374. if LineNumber>SrcLines.Count then exit;
  2375. SrcLine:=SrcLines[LineNumber-1];
  2376. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2377. until SrcLine<>'';
  2378. p:=PChar(SrcLine);
  2379. continue;
  2380. end;
  2381. '}':
  2382. begin
  2383. inc(p);
  2384. break;
  2385. end;
  2386. end;
  2387. inc(p);
  2388. until false;
  2389. CommentEndP:=p;
  2390. case CommentStartP[1] of
  2391. '#': AddLabel;
  2392. '@': AddResolverReference;
  2393. '=': AddDirectReference;
  2394. end;
  2395. p:=CommentEndP;
  2396. continue;
  2397. end;
  2398. '/':
  2399. if p[1]='/' then
  2400. break; // rest of line is comment -> skip
  2401. end;
  2402. inc(p);
  2403. until false;
  2404. end;
  2405. end;
  2406. procedure CheckResolverReference(aMarker: PSrcMarker);
  2407. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2408. var
  2409. aLabel: PSrcMarker;
  2410. ReferenceElements, LabelElements: TFPList;
  2411. i, j, aLine, aCol: Integer;
  2412. El, Ref, LabelEl: TPasElement;
  2413. begin
  2414. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2415. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2416. if aLabel=nil then
  2417. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2418. LabelElements:=nil;
  2419. ReferenceElements:=nil;
  2420. try
  2421. LabelElements:=FindElementsAt(aLabel);
  2422. ReferenceElements:=FindElementsAt(aMarker);
  2423. for i:=0 to ReferenceElements.Count-1 do
  2424. begin
  2425. El:=TPasElement(ReferenceElements[i]);
  2426. Ref:=nil;
  2427. if El.CustomData is TResolvedReference then
  2428. Ref:=TResolvedReference(El.CustomData).Declaration
  2429. else if El.CustomData is TPasPropertyScope then
  2430. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2431. else if El.CustomData is TPasSpecializeTypeData then
  2432. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2433. if Ref<>nil then
  2434. for j:=0 to LabelElements.Count-1 do
  2435. begin
  2436. LabelEl:=TPasElement(LabelElements[j]);
  2437. if Ref=LabelEl then
  2438. exit; // success
  2439. end;
  2440. end;
  2441. // failure write candidates
  2442. for i:=0 to ReferenceElements.Count-1 do
  2443. begin
  2444. El:=TPasElement(ReferenceElements[i]);
  2445. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2446. write(' El=',GetObjName(El));
  2447. if EL is TPrimitiveExpr then
  2448. begin
  2449. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2450. end;
  2451. Ref:=nil;
  2452. if El.CustomData is TResolvedReference then
  2453. Ref:=TResolvedReference(El.CustomData).Declaration
  2454. else if El.CustomData is TPasPropertyScope then
  2455. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2456. else if El.CustomData is TPasSpecializeTypeData then
  2457. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2458. if Ref<>nil then
  2459. begin
  2460. write(' Decl=',GetObjName(Ref));
  2461. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2462. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2463. end
  2464. else
  2465. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2466. writeln;
  2467. end;
  2468. for i:=0 to LabelElements.Count-1 do
  2469. begin
  2470. El:=TPasElement(LabelElements[i]);
  2471. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2472. write(' El=',GetObjName(El));
  2473. writeln;
  2474. end;
  2475. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2476. finally
  2477. LabelElements.Free;
  2478. ReferenceElements.Free;
  2479. end;
  2480. end;
  2481. procedure CheckDirectReference(aMarker: PSrcMarker);
  2482. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2483. var
  2484. aLabel: PSrcMarker;
  2485. ReferenceElements, LabelElements: TFPList;
  2486. i, LabelLine, LabelCol, j: Integer;
  2487. El, LabelEl: TPasElement;
  2488. DeclEl, TypeEl: TPasType;
  2489. begin
  2490. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2491. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2492. if aLabel=nil then
  2493. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2494. LabelElements:=nil;
  2495. ReferenceElements:=nil;
  2496. try
  2497. //writeln('CheckDirectReference finding elements at label ...');
  2498. LabelElements:=FindElementsAt(aLabel);
  2499. //writeln('CheckDirectReference finding elements at reference ...');
  2500. ReferenceElements:=FindElementsAt(aMarker);
  2501. for i:=0 to ReferenceElements.Count-1 do
  2502. begin
  2503. El:=TPasElement(ReferenceElements[i]);
  2504. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2505. if El.ClassType=TPasVariable then
  2506. begin
  2507. if TPasVariable(El).VarType=nil then
  2508. begin
  2509. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2510. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2511. end;
  2512. TypeEl:=TPasVariable(El).VarType;
  2513. for j:=0 to LabelElements.Count-1 do
  2514. begin
  2515. LabelEl:=TPasElement(LabelElements[j]);
  2516. if TypeEl=LabelEl then
  2517. exit; // success
  2518. end;
  2519. end
  2520. else if El is TPasAliasType then
  2521. begin
  2522. DeclEl:=TPasAliasType(El).DestType;
  2523. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2524. if (aLabel^.Filename=DeclEl.SourceFilename)
  2525. and (integer(aLabel^.Row)=LabelLine)
  2526. and (aLabel^.StartCol<=LabelCol)
  2527. and (aLabel^.EndCol>=LabelCol) then
  2528. exit; // success
  2529. end
  2530. else if El.ClassType=TPasArgument then
  2531. begin
  2532. TypeEl:=TPasArgument(El).ArgType;
  2533. for j:=0 to LabelElements.Count-1 do
  2534. begin
  2535. LabelEl:=TPasElement(LabelElements[j]);
  2536. if TypeEl=LabelEl then
  2537. exit; // success
  2538. end;
  2539. end;
  2540. end;
  2541. // failed -> show candidates
  2542. writeln('CheckDirectReference failed: Labels:');
  2543. for j:=0 to LabelElements.Count-1 do
  2544. begin
  2545. LabelEl:=TPasElement(LabelElements[j]);
  2546. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2547. end;
  2548. writeln('CheckDirectReference failed: References:');
  2549. for i:=0 to ReferenceElements.Count-1 do
  2550. begin
  2551. El:=TPasElement(ReferenceElements[i]);
  2552. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2553. //if EL is TPasVariable then
  2554. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2555. end;
  2556. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2557. finally
  2558. LabelElements.Free;
  2559. ReferenceElements.Free;
  2560. end;
  2561. end;
  2562. var
  2563. aMarker: PSrcMarker;
  2564. i: Integer;
  2565. SrcLines: TStringList;
  2566. begin
  2567. Module.ForEachCall(@OnCheckElementParent,nil);
  2568. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2569. // find all markers
  2570. for i:=0 to FileResolver.Streams.Count-1 do
  2571. begin
  2572. GetSrc(i,SrcLines,CurFilename);
  2573. ParseCode(SrcLines,CurFilename);
  2574. SrcLines.Free;
  2575. end;
  2576. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2577. // check references
  2578. aMarker:=FirstSrcMarker;
  2579. while aMarker<>nil do
  2580. begin
  2581. case aMarker^.Kind of
  2582. mkResolverReference: CheckResolverReference(aMarker);
  2583. mkDirectReference: CheckDirectReference(aMarker);
  2584. end;
  2585. aMarker:=aMarker^.Next;
  2586. end;
  2587. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2588. end;
  2589. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2590. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2591. var
  2592. i: Integer;
  2593. Item: TTestHintMessage;
  2594. Expected,Actual: string;
  2595. begin
  2596. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2597. for i:=0 to MsgCount-1 do
  2598. begin
  2599. Item:=Msgs[i];
  2600. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2601. if (Marker<>nil) then
  2602. begin
  2603. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2604. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2605. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2606. end;
  2607. // found
  2608. FHintMsgsGood.Add(Item);
  2609. str(Item.MsgType,Actual);
  2610. str(MsgType,Expected);
  2611. AssertEquals('MsgType',Expected,Actual);
  2612. exit;
  2613. end;
  2614. // needed message missing -> show emitted messages
  2615. WriteSources('',0,0);
  2616. for i:=0 to MsgCount-1 do
  2617. begin
  2618. Item:=Msgs[i];
  2619. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2620. ' ('+IntToStr(Item.MsgNumber),')');
  2621. if Marker<>nil then
  2622. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2623. writeln(' {',Item.Msg,'}');
  2624. end;
  2625. str(MsgType,Expected);
  2626. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2627. if Marker<>nil then
  2628. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2629. Actual:=Actual+' '+Msg;
  2630. Fail(Actual);
  2631. end;
  2632. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2633. );
  2634. var
  2635. i: Integer;
  2636. s, Txt: String;
  2637. Msg: TTestHintMessage;
  2638. begin
  2639. for i:=0 to MsgCount-1 do
  2640. begin
  2641. Msg:=Msgs[i];
  2642. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2643. s:='';
  2644. str(Msg.MsgType,s);
  2645. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2646. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2647. if WithSourcePos then
  2648. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2649. Txt:=Txt+' {'+Msg.Msg+'}';
  2650. Fail(Txt);
  2651. end;
  2652. end;
  2653. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2654. MsgNumber: integer);
  2655. begin
  2656. ExpectedErrorClass:=EScannerError;
  2657. ExpectedErrorMsg:=Msg;
  2658. ExpectedErrorNumber:=MsgNumber;
  2659. end;
  2660. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2661. MsgNumber: integer);
  2662. begin
  2663. ExpectedErrorClass:=EParserError;
  2664. ExpectedErrorMsg:=Msg;
  2665. ExpectedErrorNumber:=MsgNumber;
  2666. end;
  2667. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2668. MsgNumber: integer);
  2669. begin
  2670. ExpectedErrorClass:=EPasResolve;
  2671. ExpectedErrorMsg:=Msg;
  2672. ExpectedErrorNumber:=MsgNumber;
  2673. end;
  2674. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2675. MsgNumber: integer);
  2676. begin
  2677. ExpectedErrorClass:=EPas2JS;
  2678. ExpectedErrorMsg:=Msg;
  2679. ExpectedErrorNumber:=MsgNumber;
  2680. end;
  2681. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2682. var
  2683. MsgNumber: Integer;
  2684. Msg: String;
  2685. begin
  2686. Result:=false;
  2687. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2688. Msg:=E.Message;
  2689. if E is EPas2JS then
  2690. MsgNumber:=EPas2JS(E).MsgNumber
  2691. else if E is EPasResolve then
  2692. MsgNumber:=EPasResolve(E).MsgNumber
  2693. else if E is EParserError then
  2694. MsgNumber:=Parser.LastMsgNumber
  2695. else if E is EScannerError then
  2696. begin
  2697. MsgNumber:=Scanner.LastMsgNumber;
  2698. Msg:=Scanner.LastMsg;
  2699. end
  2700. else
  2701. MsgNumber:=0;
  2702. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2703. if Result then
  2704. SkipTests:=true;
  2705. end;
  2706. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2707. const aFilename: string; aRow, aCol: integer);
  2708. var
  2709. s: String;
  2710. begin
  2711. WriteSources(aFilename,aRow,aCol);
  2712. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2713. writeln('ERROR: ',s);
  2714. Fail(s);
  2715. end;
  2716. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2717. aMarker: PSrcMarker);
  2718. begin
  2719. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2720. end;
  2721. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2722. begin
  2723. if IsErrorExpected(E) then exit;
  2724. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2725. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2726. +' '+Scanner.CurFilename
  2727. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2728. FailException(E);
  2729. end;
  2730. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2731. begin
  2732. if IsErrorExpected(E) then exit;
  2733. WriteSources(E.Filename,E.Row,E.Column);
  2734. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2735. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2736. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2737. );
  2738. FailException(E);
  2739. end;
  2740. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2741. var
  2742. P: TPasSourcePos;
  2743. begin
  2744. if IsErrorExpected(E) then exit;
  2745. P:=E.SourcePos;
  2746. WriteSources(P.FileName,P.Row,P.Column);
  2747. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2748. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2749. FailException(E);
  2750. end;
  2751. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2752. var
  2753. Row, Col: integer;
  2754. begin
  2755. if IsErrorExpected(E) then exit;
  2756. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2757. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2758. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2759. +' '+E.PasElement.SourceFilename
  2760. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2761. FailException(E);
  2762. end;
  2763. procedure TCustomTestModule.HandleException(E: Exception);
  2764. begin
  2765. if E is EScannerError then
  2766. HandleScannerError(EScannerError(E))
  2767. else if E is EParserError then
  2768. HandleParserError(EParserError(E))
  2769. else if E is EPasResolve then
  2770. HandlePasResolveError(EPasResolve(E))
  2771. else if E is EPas2JS then
  2772. HandlePas2JSError(EPas2JS(E))
  2773. else
  2774. begin
  2775. if IsErrorExpected(E) then exit;
  2776. if not (E is EAssertionFailedError) then
  2777. begin
  2778. WriteSources('',0,0);
  2779. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2780. end;
  2781. FailException(E);
  2782. end;
  2783. end;
  2784. procedure TCustomTestModule.FailException(E: Exception);
  2785. var
  2786. MsgNumber: Integer;
  2787. begin
  2788. if ExpectedErrorClass<>nil then
  2789. begin
  2790. if FExpectedErrorClass=E.ClassType then
  2791. begin
  2792. if E is EPas2JS then
  2793. MsgNumber:=EPas2JS(E).MsgNumber
  2794. else if E is EPasResolve then
  2795. MsgNumber:=EPasResolve(E).MsgNumber
  2796. else if E is EParserError then
  2797. MsgNumber:=Parser.LastMsgNumber
  2798. else if E is EScannerError then
  2799. MsgNumber:=Scanner.LastMsgNumber
  2800. else
  2801. MsgNumber:=0;
  2802. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2803. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2804. ExpectedErrorNumber,MsgNumber);
  2805. end else begin
  2806. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2807. end;
  2808. end;
  2809. Fail(E.Message);
  2810. end;
  2811. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2812. aCol: integer);
  2813. var
  2814. IsSrc: Boolean;
  2815. i, j: Integer;
  2816. SrcLines: TStringList;
  2817. Line: string;
  2818. aModule: TTestEnginePasResolver;
  2819. begin
  2820. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2821. for i:=0 to ResolverCount-1 do
  2822. begin
  2823. aModule:=Resolvers[i];
  2824. SrcLines:=TStringList.Create;
  2825. try
  2826. SrcLines.Text:=aModule.Source;
  2827. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2828. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2829. for j:=1 to SrcLines.Count do
  2830. begin
  2831. Line:=SrcLines[j-1];
  2832. if IsSrc and (j=aRow) then
  2833. begin
  2834. write('*');
  2835. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2836. end;
  2837. writeln(Format('%:4d: ',[j]),Line);
  2838. end;
  2839. finally
  2840. SrcLines.Free;
  2841. end;
  2842. end;
  2843. end;
  2844. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2845. var
  2846. i: Integer;
  2847. begin
  2848. for i:=0 to ResolverCount-1 do
  2849. if Filename=Resolvers[i].Filename then exit(i);
  2850. Result:=-1;
  2851. end;
  2852. function TCustomTestModule.GetResolver(const Filename: string
  2853. ): TTestEnginePasResolver;
  2854. var
  2855. i: Integer;
  2856. begin
  2857. i:=IndexOfResolver(Filename);
  2858. if i<0 then exit(nil);
  2859. Result:=Resolvers[i];
  2860. end;
  2861. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2862. out aFilename: string);
  2863. var
  2864. aStream: TStream;
  2865. begin
  2866. SrcLines:=TStringList.Create;
  2867. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2868. aStream.Position:=0;
  2869. SrcLines.LoadFromStream(aStream);
  2870. aFilename:=FileResolver.Streams[Index];
  2871. end;
  2872. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2873. aEndCol: integer): TFPList;
  2874. var
  2875. ok: Boolean;
  2876. FoundRefs: TTestResolverReferenceData;
  2877. i: Integer;
  2878. CurResolver: TTestEnginePasResolver;
  2879. begin
  2880. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2881. FoundRefs:=Default(TTestResolverReferenceData);
  2882. FoundRefs.Filename:=aFilename;
  2883. FoundRefs.Row:=aLine;
  2884. FoundRefs.StartCol:=aStartCol;
  2885. FoundRefs.EndCol:=aEndCol;
  2886. FoundRefs.Found:=TFPList.Create;
  2887. ok:=false;
  2888. try
  2889. // find all markers
  2890. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2891. for i:=0 to ResolverCount-1 do
  2892. begin
  2893. CurResolver:=Resolvers[i];
  2894. if CurResolver.Module=Module then continue;
  2895. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2896. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2897. end;
  2898. ok:=true;
  2899. finally
  2900. if not ok then
  2901. FreeAndNil(FoundRefs.Found);
  2902. end;
  2903. Result:=FoundRefs.Found;
  2904. FoundRefs.Found:=nil;
  2905. end;
  2906. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2907. ErrorOnNoElements: boolean): TFPList;
  2908. begin
  2909. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2910. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2911. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2912. end;
  2913. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2914. begin
  2915. Result:=FirstSrcMarker;
  2916. while Result<>nil do
  2917. begin
  2918. if (Result^.Kind=mkLabel)
  2919. and (CompareText(Result^.Identifier,Identifier)=0) then
  2920. exit;
  2921. Result:=Result^.Next;
  2922. end;
  2923. end;
  2924. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2925. ErrorOnNoElements: boolean): TFPList;
  2926. var
  2927. SrcLabel: PSrcMarker;
  2928. begin
  2929. SrcLabel:=FindSrcLabel(Identifier);
  2930. if SrcLabel=nil then
  2931. Fail('missing label "'+Identifier+'"');
  2932. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2933. end;
  2934. function TCustomTestModule.GetDefaultNamespace: string;
  2935. var
  2936. C: TClass;
  2937. begin
  2938. Result:='';
  2939. if FModule=nil then exit;
  2940. C:=FModule.ClassType;
  2941. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2942. Result:=ResolverEngine.DefaultNameSpace;
  2943. end;
  2944. constructor TCustomTestModule.Create;
  2945. begin
  2946. inherited Create;
  2947. FHintMsgs:=TObjectList.Create(true);
  2948. FHintMsgsGood:=TFPList.Create;
  2949. end;
  2950. destructor TCustomTestModule.Destroy;
  2951. begin
  2952. FreeAndNil(FHintMsgs);
  2953. FreeAndNil(FHintMsgsGood);
  2954. inherited Destroy;
  2955. end;
  2956. { TTestModule }
  2957. procedure TTestModule.TestReservedWords;
  2958. var
  2959. i: integer;
  2960. begin
  2961. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2962. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2963. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2964. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2965. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2966. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2967. end;
  2968. procedure TTestModule.TestEmptyProgram;
  2969. begin
  2970. StartProgram(false);
  2971. Add('begin');
  2972. ConvertProgram;
  2973. CheckSource('TestEmptyProgram','','');
  2974. end;
  2975. procedure TTestModule.TestEmptyProgramUseStrict;
  2976. begin
  2977. Converter.Options:=Converter.Options+[coUseStrict];
  2978. StartProgram(false);
  2979. Add('begin');
  2980. ConvertProgram;
  2981. CheckSource('TestEmptyProgramUseStrict','','');
  2982. end;
  2983. procedure TTestModule.TestEmptyUnit;
  2984. begin
  2985. StartUnit(false);
  2986. Add('interface');
  2987. Add('implementation');
  2988. ConvertUnit;
  2989. CheckSource('TestEmptyUnit',
  2990. LinesToStr([
  2991. ]),
  2992. '');
  2993. end;
  2994. procedure TTestModule.TestEmptyUnitUseStrict;
  2995. begin
  2996. Converter.Options:=Converter.Options+[coUseStrict];
  2997. StartUnit(false);
  2998. Add('interface');
  2999. Add('implementation');
  3000. ConvertUnit;
  3001. CheckSource('TestEmptyUnitUseStrict',
  3002. LinesToStr([
  3003. ''
  3004. ]),
  3005. '');
  3006. end;
  3007. procedure TTestModule.TestDottedUnitNames;
  3008. begin
  3009. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3010. LinesToStr([
  3011. 'var iV: longint;'
  3012. ]),
  3013. '');
  3014. FFilename:='ns1.test1.pp';
  3015. StartProgram(true);
  3016. Add('uses unIt2;');
  3017. Add('var');
  3018. Add(' i: longint;');
  3019. Add('begin');
  3020. Add(' i:=iv;');
  3021. Add(' i:=uNit2.iv;');
  3022. Add(' i:=Ns1.TEst1.i;');
  3023. ConvertProgram;
  3024. CheckSource('TestDottedUnitNames',
  3025. LinesToStr([
  3026. 'this.i = 0;',
  3027. '']),
  3028. LinesToStr([ // this.$init
  3029. '$mod.i = pas["NS1.Unit2"].iV;',
  3030. '$mod.i = pas["NS1.Unit2"].iV;',
  3031. '$mod.i = $mod.i;',
  3032. '']) );
  3033. end;
  3034. procedure TTestModule.TestDottedUnitNameImpl;
  3035. begin
  3036. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3037. LinesToStr([
  3038. 'type',
  3039. ' TObject = class end;',
  3040. ' TTestA = class',
  3041. ' end;'
  3042. ]),
  3043. LinesToStr(['uses TEST.UnitB;'])
  3044. );
  3045. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3046. LinesToStr([
  3047. 'uses TEST.UnitA;',
  3048. 'type TTestB = class(TTestA);'
  3049. ]),
  3050. ''
  3051. );
  3052. StartProgram(true);
  3053. Add('uses TEST.UnitA;');
  3054. Add('begin');
  3055. ConvertProgram;
  3056. CheckSource('TestDottedUnitNameImpl',
  3057. LinesToStr([
  3058. '']),
  3059. LinesToStr([ // this.$init
  3060. '']) );
  3061. CheckUnit('TEST.UnitA.pas',
  3062. LinesToStr([
  3063. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3064. ' var $mod = this;',
  3065. ' rtl.createClass(this, "TObject", null, function () {',
  3066. ' this.$init = function () {',
  3067. ' };',
  3068. ' this.$final = function () {',
  3069. ' };',
  3070. ' });',
  3071. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3072. ' });',
  3073. '}, ["TEST.UnitB"]);'
  3074. ]));
  3075. CheckUnit('TEST.UnitB.pas',
  3076. LinesToStr([
  3077. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3078. ' var $mod = this;',
  3079. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3080. ' });',
  3081. '});'
  3082. ]));
  3083. end;
  3084. procedure TTestModule.TestDottedUnitExpr;
  3085. begin
  3086. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3087. LinesToStr([
  3088. 'procedure DoIt;'
  3089. ]),
  3090. 'procedure DoIt; begin end;');
  3091. FFilename:='Ns1.SubNs1.Test1.pp';
  3092. StartProgram(true);
  3093. Add('uses Ns2.sUbnS2.unIt2;');
  3094. Add('var');
  3095. Add(' i: longint;');
  3096. Add('begin');
  3097. Add(' ns2.subns2.unit2.doit;');
  3098. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3099. ConvertProgram;
  3100. CheckSource('TestDottedUnitExpr',
  3101. LinesToStr([
  3102. 'this.i = 0;',
  3103. '']),
  3104. LinesToStr([ // this.$init
  3105. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3106. '$mod.i = $mod.i;',
  3107. '']) );
  3108. end;
  3109. procedure TTestModule.Test_ModeFPCFail;
  3110. begin
  3111. StartProgram(false);
  3112. Add('{$mode FPC}');
  3113. Add('begin');
  3114. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3115. ConvertProgram;
  3116. end;
  3117. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3118. begin
  3119. StartProgram(false);
  3120. Add('{$modeswitch cblocks-}');
  3121. Add('begin');
  3122. ConvertProgram;
  3123. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3124. CheckResolverUnexpectedHints();
  3125. end;
  3126. procedure TTestModule.TestUnit_UseSystem;
  3127. begin
  3128. StartUnit(true);
  3129. Add([
  3130. 'interface',
  3131. 'var i: integer;',
  3132. 'implementation']);
  3133. ConvertUnit;
  3134. CheckSource('TestUnit_UseSystem',
  3135. LinesToStr([
  3136. 'this.i = 0;',
  3137. '']),
  3138. LinesToStr([
  3139. '']) );
  3140. end;
  3141. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3142. begin
  3143. AddModuleWithIntfImplSrc('unit1.pp',
  3144. LinesToStr([
  3145. 'type number = longint;']),
  3146. LinesToStr([
  3147. 'uses test1;',
  3148. 'procedure DoIt;',
  3149. 'begin',
  3150. ' i:=3;',
  3151. 'end;']));
  3152. StartUnit(true);
  3153. Add([
  3154. 'interface',
  3155. 'uses unit1;',
  3156. 'var i: number;',
  3157. 'implementation']);
  3158. ConvertUnit;
  3159. CheckSource('TestUnit_Intf1Impl2Intf1',
  3160. LinesToStr([
  3161. 'this.i = 0;',
  3162. '']),
  3163. LinesToStr([
  3164. '']) );
  3165. end;
  3166. procedure TTestModule.TestIncludeVersion;
  3167. begin
  3168. StartProgram(false);
  3169. Add([
  3170. 'var',
  3171. ' s: string;',
  3172. ' i: word;',
  3173. 'begin',
  3174. ' s:={$I %line%};',
  3175. ' i:={$I %linenum%};',
  3176. ' s:={$I %currentroutine%};',
  3177. ' s:={$I %pas2jsversion%};',
  3178. ' s:={$I %pas2jstarget%};',
  3179. ' s:={$I %pas2jstargetos%};',
  3180. ' s:={$I %pas2jstargetcpu%};',
  3181. ' s:={$I %file%};',
  3182. '']);
  3183. ConvertProgram;
  3184. CheckSource('TestIncludeVersion',
  3185. LinesToStr([
  3186. 'this.s="";',
  3187. 'this.i = 0;']),
  3188. LinesToStr([
  3189. '$mod.s = "7";',
  3190. '$mod.i = 8;',
  3191. '$mod.s = "<anonymous>";',
  3192. '$mod.s = "Comp.Ver.tcmodules";',
  3193. '$mod.s = "Browser";',
  3194. '$mod.s = "Browser";',
  3195. '$mod.s = "ECMAScript5";',
  3196. '$mod.s = "test1.pp";',
  3197. '']));
  3198. end;
  3199. procedure TTestModule.TestVarInt;
  3200. begin
  3201. StartProgram(false);
  3202. Add('var MyI: longint;');
  3203. Add('begin');
  3204. ConvertProgram;
  3205. CheckSource('TestVarInt','this.MyI=0;','');
  3206. end;
  3207. procedure TTestModule.TestVarBaseTypes;
  3208. begin
  3209. StartProgram(false);
  3210. Add('var');
  3211. Add(' i: longint;');
  3212. Add(' s: string;');
  3213. Add(' c: char;');
  3214. Add(' b: boolean;');
  3215. Add(' d: double;');
  3216. Add(' i2: longint = 3;');
  3217. Add(' s2: string = ''foo'';');
  3218. Add(' c2: char = ''4'';');
  3219. Add(' b2: boolean = true;');
  3220. Add(' d2: double = 5.6;');
  3221. Add(' i3: longint = $707;');
  3222. Add(' i4: nativeint = 9007199254740991;');
  3223. Add(' i5: nativeint = -9007199254740991-1;');
  3224. Add(' i6: nativeint = $fffffffffffff;');
  3225. Add(' i7: nativeint = -$fffffffffffff-1;');
  3226. Add(' i8: byte = 00;');
  3227. Add(' u8: nativeuint = $fffffffffffff;');
  3228. Add(' u9: nativeuint = $0000000000000;');
  3229. Add(' u10: nativeuint = $00ff00;');
  3230. Add('begin');
  3231. ConvertProgram;
  3232. CheckSource('TestVarBaseTypes',
  3233. LinesToStr([
  3234. 'this.i = 0;',
  3235. 'this.s = "";',
  3236. 'this.c = "";',
  3237. 'this.b = false;',
  3238. 'this.d = 0.0;',
  3239. 'this.i2 = 3;',
  3240. 'this.s2 = "foo";',
  3241. 'this.c2 = "4";',
  3242. 'this.b2 = true;',
  3243. 'this.d2 = 5.6;',
  3244. 'this.i3 = 0x707;',
  3245. 'this.i4 = 9007199254740991;',
  3246. 'this.i5 = -9007199254740991-1;',
  3247. 'this.i6 = 0xfffffffffffff;',
  3248. 'this.i7 =-0xfffffffffffff-1;',
  3249. 'this.i8 = 0;',
  3250. 'this.u8 = 0xfffffffffffff;',
  3251. 'this.u9 = 0x0;',
  3252. 'this.u10 = 0xff00;'
  3253. ]),
  3254. '');
  3255. end;
  3256. procedure TTestModule.TestBaseTypeSingleFail;
  3257. begin
  3258. StartProgram(false);
  3259. Add('var s: single;');
  3260. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3261. ConvertProgram;
  3262. end;
  3263. procedure TTestModule.TestBaseTypeExtendedFail;
  3264. begin
  3265. StartProgram(false);
  3266. Add('var e: extended;');
  3267. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3268. ConvertProgram;
  3269. end;
  3270. procedure TTestModule.TestConstBaseTypes;
  3271. begin
  3272. StartProgram(false);
  3273. Add('const');
  3274. Add(' i: longint = 3;');
  3275. Add(' s: string = ''foo'';');
  3276. Add(' c: char = ''4'';');
  3277. Add(' b: boolean = true;');
  3278. Add(' d: double = 5.6;');
  3279. Add(' e = low(word);');
  3280. Add(' f = high(word);');
  3281. Add('begin');
  3282. ConvertProgram;
  3283. CheckSource('TestVarBaseTypes',
  3284. LinesToStr([
  3285. 'this.i=3;',
  3286. 'this.s="foo";',
  3287. 'this.c="4";',
  3288. 'this.b=true;',
  3289. 'this.d=5.6;',
  3290. 'this.e = 0;',
  3291. 'this.f = 65535;'
  3292. ]),
  3293. '');
  3294. end;
  3295. procedure TTestModule.TestAliasTypeRef;
  3296. begin
  3297. StartProgram(false);
  3298. Add('type');
  3299. Add(' a=longint;');
  3300. Add(' b=a;');
  3301. Add('var');
  3302. Add(' c: A;');
  3303. Add(' d: B;');
  3304. Add('begin');
  3305. ConvertProgram;
  3306. CheckSource('TestAliasTypeRef',
  3307. LinesToStr([ // statements
  3308. 'this.c = 0;',
  3309. 'this.d = 0;'
  3310. ]),
  3311. LinesToStr([ // this.$main
  3312. ''
  3313. ]));
  3314. end;
  3315. procedure TTestModule.TestTypeCast_BaseTypes;
  3316. begin
  3317. StartProgram(false);
  3318. Add([
  3319. 'var',
  3320. ' i: longint;',
  3321. ' b: boolean;',
  3322. ' d: double;',
  3323. ' s: string;',
  3324. ' c: char;',
  3325. 'begin',
  3326. ' i:=longint(i);',
  3327. ' i:=longint(b);',
  3328. ' b:=boolean(b);',
  3329. ' b:=boolean(i);',
  3330. ' d:=double(d);',
  3331. ' d:=double(i);',
  3332. ' s:=string(s);',
  3333. ' s:=string(c);',
  3334. ' c:=char(c);',
  3335. ' c:=char(i);',
  3336. ' c:=char(65);',
  3337. ' c:=char(#10);',
  3338. ' c:=char(#$E000);',
  3339. '']);
  3340. ConvertProgram;
  3341. CheckSource('TestAliasTypeRef',
  3342. LinesToStr([ // statements
  3343. 'this.i = 0;',
  3344. 'this.b = false;',
  3345. 'this.d = 0.0;',
  3346. 'this.s = "";',
  3347. 'this.c = "";',
  3348. '']),
  3349. LinesToStr([ // this.$main
  3350. '$mod.i = $mod.i;',
  3351. '$mod.i = ($mod.b ? 1 : 0);',
  3352. '$mod.b = $mod.b;',
  3353. '$mod.b = $mod.i != 0;',
  3354. '$mod.d = $mod.d;',
  3355. '$mod.d = $mod.i;',
  3356. '$mod.s = $mod.s;',
  3357. '$mod.s = $mod.c;',
  3358. '$mod.c = $mod.c;',
  3359. '$mod.c = String.fromCharCode($mod.i);',
  3360. '$mod.c = "A";',
  3361. '$mod.c = "\n";',
  3362. '$mod.c = "";',
  3363. '']));
  3364. end;
  3365. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3366. begin
  3367. StartProgram(false);
  3368. Add('type');
  3369. Add(' integer = longint;');
  3370. Add(' TYesNo = boolean;');
  3371. Add(' TFloat = double;');
  3372. Add(' TCaption = string;');
  3373. Add(' TChar = char;');
  3374. Add('var');
  3375. Add(' i: integer;');
  3376. Add(' b: TYesNo;');
  3377. Add(' d: TFloat;');
  3378. Add(' s: TCaption;');
  3379. Add(' c: TChar;');
  3380. Add('begin');
  3381. Add(' i:=integer(i);');
  3382. Add(' i:=integer(b);');
  3383. Add(' b:=TYesNo(b);');
  3384. Add(' b:=TYesNo(i);');
  3385. Add(' d:=TFloat(d);');
  3386. Add(' d:=TFloat(i);');
  3387. Add(' s:=TCaption(s);');
  3388. Add(' s:=TCaption(c);');
  3389. Add(' c:=TChar(c);');
  3390. ConvertProgram;
  3391. CheckSource('TestAliasTypeRef',
  3392. LinesToStr([ // statements
  3393. 'this.i = 0;',
  3394. 'this.b = false;',
  3395. 'this.d = 0.0;',
  3396. 'this.s = "";',
  3397. 'this.c = "";',
  3398. '']),
  3399. LinesToStr([ // this.$main
  3400. '$mod.i = $mod.i;',
  3401. '$mod.i = ($mod.b ? 1 : 0);',
  3402. '$mod.b = $mod.b;',
  3403. '$mod.b = $mod.i != 0;',
  3404. '$mod.d = $mod.d;',
  3405. '$mod.d = $mod.i;',
  3406. '$mod.s = $mod.s;',
  3407. '$mod.s = $mod.c;',
  3408. '$mod.c = $mod.c;',
  3409. '']));
  3410. end;
  3411. procedure TTestModule.TestEmptyProc;
  3412. begin
  3413. StartProgram(false);
  3414. Add('procedure Test;');
  3415. Add('begin');
  3416. Add('end;');
  3417. Add('begin');
  3418. ConvertProgram;
  3419. CheckSource('TestEmptyProc',
  3420. LinesToStr([ // statements
  3421. 'this.Test = function () {',
  3422. '};'
  3423. ]),
  3424. LinesToStr([ // this.$main
  3425. ''
  3426. ]));
  3427. end;
  3428. procedure TTestModule.TestProcOneParam;
  3429. begin
  3430. StartProgram(false);
  3431. Add('procedure ProcA(i: longint);');
  3432. Add('begin');
  3433. Add('end;');
  3434. Add('begin');
  3435. Add(' PROCA(3);');
  3436. ConvertProgram;
  3437. CheckSource('TestProcOneParam',
  3438. LinesToStr([ // statements
  3439. 'this.ProcA = function (i) {',
  3440. '};'
  3441. ]),
  3442. LinesToStr([ // this.$main
  3443. '$mod.ProcA(3);'
  3444. ]));
  3445. end;
  3446. procedure TTestModule.TestFunctionWithoutParams;
  3447. begin
  3448. StartProgram(false);
  3449. Add('function FuncA: longint;');
  3450. Add('begin');
  3451. Add('end;');
  3452. Add('var i: longint;');
  3453. Add('begin');
  3454. Add(' I:=FUNCA();');
  3455. Add(' I:=FUNCA;');
  3456. Add(' FUNCA();');
  3457. Add(' FUNCA;');
  3458. ConvertProgram;
  3459. CheckSource('TestProcWithoutParams',
  3460. LinesToStr([ // statements
  3461. 'this.FuncA = function () {',
  3462. ' var Result = 0;',
  3463. ' return Result;',
  3464. '};',
  3465. 'this.i=0;'
  3466. ]),
  3467. LinesToStr([ // this.$main
  3468. '$mod.i=$mod.FuncA();',
  3469. '$mod.i=$mod.FuncA();',
  3470. '$mod.FuncA();',
  3471. '$mod.FuncA();'
  3472. ]));
  3473. end;
  3474. procedure TTestModule.TestProcedureWithoutParams;
  3475. begin
  3476. StartProgram(false);
  3477. Add('procedure ProcA;');
  3478. Add('begin');
  3479. Add('end;');
  3480. Add('begin');
  3481. Add(' PROCA();');
  3482. Add(' PROCA;');
  3483. ConvertProgram;
  3484. CheckSource('TestProcWithoutParams',
  3485. LinesToStr([ // statements
  3486. 'this.ProcA = function () {',
  3487. '};'
  3488. ]),
  3489. LinesToStr([ // this.$main
  3490. '$mod.ProcA();',
  3491. '$mod.ProcA();'
  3492. ]));
  3493. end;
  3494. procedure TTestModule.TestIncDec;
  3495. begin
  3496. StartProgram(false);
  3497. Add([
  3498. 'procedure DoIt(var i: longint);',
  3499. 'begin',
  3500. ' inc(i);',
  3501. ' inc(i,2);',
  3502. 'end;',
  3503. 'var',
  3504. ' Bar: longint;',
  3505. 'begin',
  3506. ' inc(bar);',
  3507. ' inc(bar,2);',
  3508. ' dec(bar);',
  3509. ' dec(bar,3);',
  3510. '']);
  3511. ConvertProgram;
  3512. CheckSource('TestIncDec',
  3513. LinesToStr([ // statements
  3514. 'this.DoIt = function (i) {',
  3515. ' i.set(i.get()+1);',
  3516. ' i.set(i.get()+2);',
  3517. '};',
  3518. 'this.Bar = 0;'
  3519. ]),
  3520. LinesToStr([ // this.$main
  3521. '$mod.Bar+=1;',
  3522. '$mod.Bar+=2;',
  3523. '$mod.Bar-=1;',
  3524. '$mod.Bar-=3;'
  3525. ]));
  3526. end;
  3527. procedure TTestModule.TestLoHiFpcMode;
  3528. begin
  3529. StartProgram(false);
  3530. Add([
  3531. '{$mode objfpc}',
  3532. 'const',
  3533. ' LoByte1 = Lo(Word($1234));',
  3534. ' HiByte1 = Hi(Word($1234));',
  3535. ' LoByte2 = Lo(SmallInt($1234));',
  3536. ' HiByte2 = Hi(SmallInt($1234));',
  3537. ' LoWord1 = Lo($1234CDEF);',
  3538. ' HiWord1 = Hi($1234CDEF);',
  3539. ' LoWord2 = Lo(-$1234CDEF);',
  3540. ' HiWord2 = Hi(-$1234CDEF);',
  3541. ' lo4:byte=lo(byte($34));',
  3542. ' hi4:byte=hi(byte($34));',
  3543. ' lo5:byte=lo(shortint(-$34));',
  3544. ' hi5:byte=hi(shortint(-$34));',
  3545. ' lo6:longword=lo($123456789ABCD);',
  3546. ' hi6:longword=hi($123456789ABCD);',
  3547. ' lo7:longword=lo(-$123456789ABCD);',
  3548. ' hi7:longword=hi(-$123456789ABCD);',
  3549. 'var',
  3550. ' b: Byte;',
  3551. ' ss: shortint;',
  3552. ' w: Word;',
  3553. ' si: SmallInt;',
  3554. ' lw: LongWord;',
  3555. ' li: LongInt;',
  3556. ' b2: Byte;',
  3557. ' ni: nativeint;',
  3558. 'begin',
  3559. ' w := $1234;',
  3560. ' ss := -$12;',
  3561. ' b := lo(ss);',
  3562. ' b := HI(ss);',
  3563. ' b := lo(w);',
  3564. ' b := HI(w);',
  3565. ' b2 := lo(b);',
  3566. ' b2 := hi(b);',
  3567. ' lw := $1234CDEF;',
  3568. ' w := lo(lw);',
  3569. ' w := hi(lw);',
  3570. ' ni := $123456789ABCD;',
  3571. ' lw := lo(ni);',
  3572. ' lw := hi(ni);',
  3573. '']);
  3574. ConvertProgram;
  3575. CheckSource('TestLoHiFpcMode',
  3576. LinesToStr([ // statements
  3577. 'this.LoByte1 = 0x1234 & 0xFF;',
  3578. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3579. 'this.LoByte2 = 0x1234 & 0xFF;',
  3580. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3581. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3582. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3583. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3584. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3585. 'this.lo4 = 0x34 & 0xF;',
  3586. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3587. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3588. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3589. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3590. 'this.hi6 = 74565 >>> 0;',
  3591. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3592. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3593. 'this.b = 0;',
  3594. 'this.ss = 0;',
  3595. 'this.w = 0;',
  3596. 'this.si = 0;',
  3597. 'this.lw = 0;',
  3598. 'this.li = 0;',
  3599. 'this.b2 = 0;',
  3600. 'this.ni = 0;',
  3601. '']),
  3602. LinesToStr([ // this.$main
  3603. '$mod.w = 0x1234;',
  3604. '$mod.ss = -0x12;',
  3605. '$mod.b = $mod.ss & 0xFF;',
  3606. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3607. '$mod.b = $mod.w & 0xFF;',
  3608. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3609. '$mod.b2 = $mod.b & 0xF;',
  3610. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3611. '$mod.lw = 0x1234CDEF;',
  3612. '$mod.w = $mod.lw & 0xFFFF;',
  3613. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3614. '$mod.ni = 0x123456789ABCD;',
  3615. '$mod.lw = $mod.ni >>> 0;',
  3616. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3617. '']));
  3618. end;
  3619. procedure TTestModule.TestLoHiDelphiMode;
  3620. begin
  3621. StartProgram(false);
  3622. Add([
  3623. '{$mode delphi}',
  3624. 'const',
  3625. ' LoByte1 = Lo(Word($1234));',
  3626. ' HiByte1 = Hi(Word($1234));',
  3627. ' LoByte2 = Lo(SmallInt($1234));',
  3628. ' HiByte2 = Hi(SmallInt($1234));',
  3629. ' LoByte3 = Lo($1234CDEF);',
  3630. ' HiByte3 = Hi($1234CDEF);',
  3631. ' LoByte4 = Lo(-$1234CDEF);',
  3632. ' HiByte4 = Hi(-$1234CDEF);',
  3633. 'var',
  3634. ' b: Byte;',
  3635. ' w: Word;',
  3636. ' si: SmallInt;',
  3637. ' lw: LongWord;',
  3638. ' li: LongInt;',
  3639. 'begin',
  3640. ' w := $1234;',
  3641. ' b := lo(w);',
  3642. ' b := HI(w);',
  3643. ' lw := $1234CDEF;',
  3644. ' b := lo(lw);',
  3645. ' b := hi(lw);',
  3646. '']);
  3647. ConvertProgram;
  3648. CheckSource('TestLoHiDelphiMode',
  3649. LinesToStr([ // statements
  3650. 'this.LoByte1 = 0x1234 & 0xFF;',
  3651. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3652. 'this.LoByte2 = 0x1234 & 0xFF;',
  3653. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3654. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3655. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3656. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3657. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3658. 'this.b = 0;',
  3659. 'this.w = 0;',
  3660. 'this.si = 0;',
  3661. 'this.lw = 0;',
  3662. 'this.li = 0;'
  3663. ]),
  3664. LinesToStr([ // this.$main
  3665. '$mod.w = 0x1234;',
  3666. '$mod.b = $mod.w & 0xFF;',
  3667. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3668. '$mod.lw = 0x1234CDEF;',
  3669. '$mod.b = $mod.lw & 0xFF;',
  3670. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3671. ]));
  3672. end;
  3673. procedure TTestModule.TestAssignments;
  3674. begin
  3675. StartProgram(false);
  3676. Parser.Options:=Parser.Options+[po_cassignments];
  3677. Add('var');
  3678. Add(' Bar:longint;');
  3679. Add('begin');
  3680. Add(' bar:=3;');
  3681. Add(' bar+=4;');
  3682. Add(' bar-=5;');
  3683. Add(' bar*=6;');
  3684. ConvertProgram;
  3685. CheckSource('TestAssignments',
  3686. LinesToStr([ // statements
  3687. 'this.Bar = 0;'
  3688. ]),
  3689. LinesToStr([ // this.$main
  3690. '$mod.Bar=3;',
  3691. '$mod.Bar+=4;',
  3692. '$mod.Bar-=5;',
  3693. '$mod.Bar*=6;'
  3694. ]));
  3695. end;
  3696. procedure TTestModule.TestArithmeticOperators1;
  3697. begin
  3698. StartProgram(false);
  3699. Add('var');
  3700. Add(' vA,vB,vC:longint;');
  3701. Add('begin');
  3702. Add(' va:=1;');
  3703. Add(' vb:=va+va;');
  3704. Add(' vb:=va div vb;');
  3705. Add(' vb:=va mod vb;');
  3706. Add(' vb:=va+va*vb+va div vb;');
  3707. Add(' vc:=-va;');
  3708. Add(' va:=va-vb;');
  3709. Add(' vb:=va;');
  3710. Add(' if va<vb then vc:=va else vc:=vb;');
  3711. ConvertProgram;
  3712. CheckSource('TestArithmeticOperators1',
  3713. LinesToStr([ // statements
  3714. 'this.vA = 0;',
  3715. 'this.vB = 0;',
  3716. 'this.vC = 0;'
  3717. ]),
  3718. LinesToStr([ // this.$main
  3719. '$mod.vA = 1;',
  3720. '$mod.vB = $mod.vA + $mod.vA;',
  3721. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3722. '$mod.vB = $mod.vA % $mod.vB;',
  3723. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3724. '$mod.vC = -$mod.vA;',
  3725. '$mod.vA = $mod.vA - $mod.vB;',
  3726. '$mod.vB = $mod.vA;',
  3727. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3728. ]));
  3729. end;
  3730. procedure TTestModule.TestMultiAdd;
  3731. begin
  3732. StartProgram(false);
  3733. Add([
  3734. 'function Fly: string; external name ''fly'';',
  3735. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3736. 'var',
  3737. ' Date: double;',
  3738. 'begin',
  3739. ' Result:=(Year>0) and (Year<10000) and',
  3740. ' (Month >= 1) and (Month<=12) and',
  3741. ' (Day>0) and (Day<=31);',
  3742. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3743. 'end;',
  3744. 'var s: string;',
  3745. 'begin',
  3746. ' s:=''a''+''b''+''c''+''d'';',
  3747. ' s:=s+Fly+''e'';',
  3748. ' s:=Fly+Fly+Fly;',
  3749. '']);
  3750. ConvertProgram;
  3751. CheckSource('TestMultiAdd',
  3752. LinesToStr([ // statements
  3753. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3754. ' var Result = false;',
  3755. ' var date = 0.0;',
  3756. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3757. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3758. ' return Result;',
  3759. '};',
  3760. 'this.s = "";',
  3761. '']),
  3762. LinesToStr([ // this.$main
  3763. '$mod.s = "a" + "b" + "c" + "d";',
  3764. '$mod.s = $mod.s + fly() + "e";',
  3765. '$mod.s = fly() + fly() + fly();',
  3766. '']));
  3767. end;
  3768. procedure TTestModule.TestLogicalOperators;
  3769. begin
  3770. StartProgram(false);
  3771. Add('var');
  3772. Add(' vA,vB,vC:boolean;');
  3773. Add('begin');
  3774. Add(' va:=vb and vc;');
  3775. Add(' va:=vb or vc;');
  3776. Add(' va:=vb xor vc;');
  3777. Add(' va:=true and vc;');
  3778. Add(' va:=(vb and vc) or (va and vb);');
  3779. Add(' va:=not vb;');
  3780. ConvertProgram;
  3781. CheckSource('TestLogicalOperators',
  3782. LinesToStr([ // statements
  3783. 'this.vA = false;',
  3784. 'this.vB = false;',
  3785. 'this.vC = false;'
  3786. ]),
  3787. LinesToStr([ // this.$main
  3788. '$mod.vA = $mod.vB && $mod.vC;',
  3789. '$mod.vA = $mod.vB || $mod.vC;',
  3790. '$mod.vA = $mod.vB ^ $mod.vC;',
  3791. '$mod.vA = true && $mod.vC;',
  3792. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3793. '$mod.vA = !$mod.vB;'
  3794. ]));
  3795. end;
  3796. procedure TTestModule.TestBitwiseOperators;
  3797. begin
  3798. StartProgram(false);
  3799. Add([
  3800. 'var',
  3801. ' vA,vB,vC:longint;',
  3802. ' X,Y,Z: nativeint;',
  3803. 'begin',
  3804. ' va:=vb and vc;',
  3805. ' va:=vb or vc;',
  3806. ' va:=vb xor vc;',
  3807. ' va:=vb shl vc;',
  3808. ' va:=vb shr vc;',
  3809. ' va:=3 and vc;',
  3810. ' va:=(vb and vc) or (va and vb);',
  3811. ' va:=not vb;',
  3812. ' X:=Y and Z;',
  3813. ' X:=Y and va;',
  3814. ' X:=Y or Z;',
  3815. ' X:=Y or va;',
  3816. ' X:=Y xor Z;',
  3817. ' X:=Y xor va;',
  3818. '']);
  3819. ConvertProgram;
  3820. CheckSource('TestBitwiseOperators',
  3821. LinesToStr([ // statements
  3822. 'this.vA = 0;',
  3823. 'this.vB = 0;',
  3824. 'this.vC = 0;',
  3825. 'this.X = 0;',
  3826. 'this.Y = 0;',
  3827. 'this.Z = 0;',
  3828. '']),
  3829. LinesToStr([ // this.$main
  3830. '$mod.vA = $mod.vB & $mod.vC;',
  3831. '$mod.vA = $mod.vB | $mod.vC;',
  3832. '$mod.vA = $mod.vB ^ $mod.vC;',
  3833. '$mod.vA = $mod.vB << $mod.vC;',
  3834. '$mod.vA = $mod.vB >>> $mod.vC;',
  3835. '$mod.vA = 3 & $mod.vC;',
  3836. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3837. '$mod.vA = ~$mod.vB;',
  3838. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3839. '$mod.X = $mod.Y & $mod.vA;',
  3840. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3841. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3842. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3843. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3844. '']));
  3845. end;
  3846. procedure TTestModule.TestBitwiseOperatorsLongword;
  3847. begin
  3848. StartProgram(false);
  3849. Add([
  3850. 'var',
  3851. ' a,b,c:longword;',
  3852. ' i: longint;',
  3853. 'begin',
  3854. ' a:=$12345678;',
  3855. ' b:=$EDCBA987;',
  3856. ' c:=not a;',
  3857. ' c:=a and b;',
  3858. ' c:=a and $ffff0000;',
  3859. ' c:=a or b;',
  3860. ' c:=a or $ff00ff00;',
  3861. ' c:=a xor b;',
  3862. ' c:=a xor $f0f0f0f0;',
  3863. ' c:=a shl 1;',
  3864. ' c:=a shl 16;',
  3865. ' c:=a shl 24;',
  3866. ' c:=a shl b;',
  3867. ' c:=a shr 1;',
  3868. ' c:=a shr 16;',
  3869. ' c:=a shr 24;',
  3870. ' c:=a shr b;',
  3871. ' c:=(b and c) or (a and b);',
  3872. ' c:=i and a;',
  3873. ' c:=i or a;',
  3874. ' c:=i xor a;',
  3875. '']);
  3876. ConvertProgram;
  3877. CheckSource('TestBitwiseOperatorsLongword',
  3878. LinesToStr([ // statements
  3879. 'this.a = 0;',
  3880. 'this.b = 0;',
  3881. 'this.c = 0;',
  3882. 'this.i = 0;',
  3883. '']),
  3884. LinesToStr([ // this.$main
  3885. '$mod.a = 0x12345678;',
  3886. '$mod.b = 0xEDCBA987;',
  3887. '$mod.c = rtl.lw(~$mod.a);',
  3888. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3889. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3890. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3891. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3892. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3893. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3894. '$mod.c = rtl.lw($mod.a << 1);',
  3895. '$mod.c = rtl.lw($mod.a << 16);',
  3896. '$mod.c = rtl.lw($mod.a << 24);',
  3897. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3898. '$mod.c = rtl.lw($mod.a >>> 1);',
  3899. '$mod.c = rtl.lw($mod.a >>> 16);',
  3900. '$mod.c = rtl.lw($mod.a >>> 24);',
  3901. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3902. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3903. '$mod.c = $mod.i & $mod.a;',
  3904. '$mod.c = $mod.i | $mod.a;',
  3905. '$mod.c = $mod.i ^ $mod.a;',
  3906. '']));
  3907. end;
  3908. procedure TTestModule.TestPrgProcVar;
  3909. begin
  3910. StartProgram(false);
  3911. Add('procedure Proc1;');
  3912. Add('type');
  3913. Add(' t1=longint;');
  3914. Add('var');
  3915. Add(' vA:t1;');
  3916. Add('begin');
  3917. Add('end;');
  3918. Add('begin');
  3919. ConvertProgram;
  3920. CheckSource('TestPrgProcVar',
  3921. LinesToStr([ // statements
  3922. 'this.Proc1 = function () {',
  3923. ' var vA=0;',
  3924. '};'
  3925. ]),
  3926. LinesToStr([ // this.$main
  3927. ''
  3928. ]));
  3929. end;
  3930. procedure TTestModule.TestUnitProcVar;
  3931. begin
  3932. StartUnit(false);
  3933. Add('interface');
  3934. Add('');
  3935. Add('type tA=string; // unit scope');
  3936. Add('procedure Proc1;');
  3937. Add('');
  3938. Add('implementation');
  3939. Add('');
  3940. Add('procedure Proc1;');
  3941. Add('type tA=longint; // local proc scope');
  3942. Add('var v1:tA; // using local tA');
  3943. Add('begin');
  3944. Add('end;');
  3945. Add('var v2:tA; // using interface tA');
  3946. ConvertUnit;
  3947. CheckSource('TestUnitProcVar',
  3948. LinesToStr([ // statements
  3949. 'var $impl = $mod.$impl;',
  3950. 'this.Proc1 = function () {',
  3951. ' var v1 = 0;',
  3952. '};',
  3953. '']),
  3954. // this.$init
  3955. '',
  3956. // implementation
  3957. LinesToStr([
  3958. '$impl.v2 = "";',
  3959. '']));
  3960. end;
  3961. procedure TTestModule.TestImplProc;
  3962. begin
  3963. StartUnit(false);
  3964. Add('interface');
  3965. Add('');
  3966. Add('procedure Proc1;');
  3967. Add('');
  3968. Add('implementation');
  3969. Add('');
  3970. Add('procedure Proc1; begin end;');
  3971. Add('procedure Proc2; begin end;');
  3972. Add('initialization');
  3973. Add(' Proc1;');
  3974. Add(' Proc2;');
  3975. ConvertUnit;
  3976. CheckSource('TestImplProc',
  3977. LinesToStr([ // statements
  3978. 'var $impl = $mod.$impl;',
  3979. 'this.Proc1 = function () {',
  3980. '};',
  3981. '']),
  3982. LinesToStr([ // this.$init
  3983. '$mod.Proc1();',
  3984. '$impl.Proc2();',
  3985. '']),
  3986. LinesToStr([ // implementation
  3987. '$impl.Proc2 = function () {',
  3988. '};',
  3989. ''])
  3990. );
  3991. end;
  3992. procedure TTestModule.TestFunctionResult;
  3993. begin
  3994. StartProgram(false);
  3995. Add('function Func1: longint;');
  3996. Add('begin');
  3997. Add(' Result:=3;');
  3998. Add(' Func1:=4;');
  3999. Add('end;');
  4000. Add('begin');
  4001. ConvertProgram;
  4002. CheckSource('TestFunctionResult',
  4003. LinesToStr([ // statements
  4004. 'this.Func1 = function () {',
  4005. ' var Result = 0;',
  4006. ' Result = 3;',
  4007. ' Result = 4;',
  4008. ' return Result;',
  4009. '};'
  4010. ]),
  4011. '');
  4012. end;
  4013. procedure TTestModule.TestNestedProc;
  4014. begin
  4015. StartProgram(false);
  4016. Add([
  4017. 'var vInUnit: longint;',
  4018. 'function DoIt(pA,pD: longint): longint;',
  4019. 'var',
  4020. ' vB: longint;',
  4021. ' vC: longint;',
  4022. ' function Nesty(pA: longint): longint; ',
  4023. ' var vB: longint;',
  4024. ' begin',
  4025. ' Result:=pa+vb+vc+pd+vInUnit;',
  4026. ' nesty:=3;',
  4027. ' doit:=4;',
  4028. ' exit;',
  4029. ' end;',
  4030. 'begin',
  4031. ' Result:=pa+vb+vc;',
  4032. ' doit:=6;',
  4033. ' exit;',
  4034. 'end;',
  4035. 'begin']);
  4036. ConvertProgram;
  4037. CheckSource('TestNestedProc',
  4038. LinesToStr([ // statements
  4039. 'this.vInUnit = 0;',
  4040. 'this.DoIt = function (pA, pD) {',
  4041. ' var Result = 0;',
  4042. ' var vB = 0;',
  4043. ' var vC = 0;',
  4044. ' function Nesty(pA) {',
  4045. ' var Result$1 = 0;',
  4046. ' var vB = 0;',
  4047. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4048. ' Result$1 = 3;',
  4049. ' Result = 4;',
  4050. ' return Result$1;',
  4051. ' return Result$1;',
  4052. ' };',
  4053. ' Result = pA + vB + vC;',
  4054. ' Result = 6;',
  4055. ' return Result;',
  4056. ' return Result;',
  4057. '};'
  4058. ]),
  4059. '');
  4060. end;
  4061. procedure TTestModule.TestNestedProc_ResultString;
  4062. begin
  4063. StartProgram(false);
  4064. Add([
  4065. 'function DoIt: string;',
  4066. ' function Nesty: string; ',
  4067. ' begin',
  4068. ' nesty:=#65#66;',
  4069. ' nesty[1]:=#67;',
  4070. ' doit:=#68;',
  4071. ' doit[2]:=#69;',
  4072. ' end;',
  4073. 'begin',
  4074. ' doit:=#70;',
  4075. ' doit[3]:=#71;',
  4076. 'end;',
  4077. 'begin']);
  4078. ConvertProgram;
  4079. CheckSource('TestNestedProc_ResultString',
  4080. LinesToStr([ // statements
  4081. 'this.DoIt = function () {',
  4082. ' var Result = "";',
  4083. ' function Nesty() {',
  4084. ' var Result$1 = "";',
  4085. ' Result$1 = "AB";',
  4086. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4087. ' Result = "D";',
  4088. ' Result = rtl.setCharAt(Result, 1, "E");',
  4089. ' return Result$1;',
  4090. ' };',
  4091. ' Result = "F";',
  4092. ' Result = rtl.setCharAt(Result, 2, "G");',
  4093. ' return Result;',
  4094. '};'
  4095. ]),
  4096. '');
  4097. end;
  4098. procedure TTestModule.TestForwardProc;
  4099. begin
  4100. StartProgram(false);
  4101. Add('procedure FuncA(Bar: longint); forward;');
  4102. Add('procedure FuncB(Bar: longint);');
  4103. Add('begin');
  4104. Add(' funca(bar);');
  4105. Add('end;');
  4106. Add('procedure funca(bar: longint);');
  4107. Add('begin');
  4108. Add(' if bar=3 then ;');
  4109. Add('end;');
  4110. Add('begin');
  4111. Add(' funca(4);');
  4112. Add(' funcb(5);');
  4113. ConvertProgram;
  4114. CheckSource('TestForwardProc',
  4115. LinesToStr([ // statements'
  4116. 'this.FuncB = function (Bar) {',
  4117. ' $mod.FuncA(Bar);',
  4118. '};',
  4119. 'this.FuncA = function (Bar) {',
  4120. ' if (Bar === 3);',
  4121. '};'
  4122. ]),
  4123. LinesToStr([
  4124. '$mod.FuncA(4);',
  4125. '$mod.FuncB(5);'
  4126. ])
  4127. );
  4128. end;
  4129. procedure TTestModule.TestNestedForwardProc;
  4130. begin
  4131. StartProgram(false);
  4132. Add('procedure FuncA;');
  4133. Add(' procedure FuncB(i: longint); forward;');
  4134. Add(' procedure FuncC(i: longint);');
  4135. Add(' begin');
  4136. Add(' funcb(i);');
  4137. Add(' end;');
  4138. Add(' procedure FuncB(i: longint);');
  4139. Add(' begin');
  4140. Add(' if i=3 then ;');
  4141. Add(' end;');
  4142. Add('begin');
  4143. Add(' funcc(4)');
  4144. Add('end;');
  4145. Add('begin');
  4146. Add(' funca;');
  4147. ConvertProgram;
  4148. CheckSource('TestNestedForwardProc',
  4149. LinesToStr([ // statements'
  4150. 'this.FuncA = function () {',
  4151. ' function FuncC(i) {',
  4152. ' FuncB(i);',
  4153. ' };',
  4154. ' function FuncB(i) {',
  4155. ' if (i === 3);',
  4156. ' };',
  4157. ' FuncC(4);',
  4158. '};'
  4159. ]),
  4160. LinesToStr([
  4161. '$mod.FuncA();'
  4162. ])
  4163. );
  4164. end;
  4165. procedure TTestModule.TestAssignFunctionResult;
  4166. begin
  4167. StartProgram(false);
  4168. Add('function Func1: longint;');
  4169. Add('begin');
  4170. Add('end;');
  4171. Add('var i: longint;');
  4172. Add('begin');
  4173. Add(' i:=func1();');
  4174. Add(' i:=func1()+func1();');
  4175. ConvertProgram;
  4176. CheckSource('TestAssignFunctionResult',
  4177. LinesToStr([ // statements
  4178. 'this.Func1 = function () {',
  4179. ' var Result = 0;',
  4180. ' return Result;',
  4181. '};',
  4182. 'this.i = 0;'
  4183. ]),
  4184. LinesToStr([
  4185. '$mod.i = $mod.Func1();',
  4186. '$mod.i = $mod.Func1() + $mod.Func1();'
  4187. ]));
  4188. end;
  4189. procedure TTestModule.TestFunctionResultInCondition;
  4190. begin
  4191. StartProgram(false);
  4192. Add('function Func1: longint;');
  4193. Add('begin');
  4194. Add('end;');
  4195. Add('function Func2: boolean;');
  4196. Add('begin');
  4197. Add('end;');
  4198. Add('var i: longint;');
  4199. Add('begin');
  4200. Add(' if func2 then ;');
  4201. Add(' if i=func1() then ;');
  4202. Add(' if i=func1 then ;');
  4203. ConvertProgram;
  4204. CheckSource('TestFunctionResultInCondition',
  4205. LinesToStr([ // statements
  4206. 'this.Func1 = function () {',
  4207. ' var Result = 0;',
  4208. ' return Result;',
  4209. '};',
  4210. 'this.Func2 = function () {',
  4211. ' var Result = false;',
  4212. ' return Result;',
  4213. '};',
  4214. 'this.i = 0;'
  4215. ]),
  4216. LinesToStr([
  4217. 'if ($mod.Func2());',
  4218. 'if ($mod.i === $mod.Func1());',
  4219. 'if ($mod.i === $mod.Func1());'
  4220. ]));
  4221. end;
  4222. procedure TTestModule.TestFunctionResultInForLoop;
  4223. begin
  4224. StartProgram(false);
  4225. Add([
  4226. 'function Func1(a: array of longint): longint;',
  4227. 'begin',
  4228. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4229. ' for Result in a do if a[Result]=0 then exit;',
  4230. 'end;',
  4231. 'begin',
  4232. ' Func1([1,2,3])']);
  4233. ConvertProgram;
  4234. CheckSource('TestFunctionResultInForLoop',
  4235. LinesToStr([ // statements
  4236. 'this.Func1 = function (a) {',
  4237. ' var Result = 0;',
  4238. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4239. ' Result = $l;',
  4240. ' if (a[Result] === 0) return Result;',
  4241. ' };',
  4242. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4243. ' Result = $in[$l1];',
  4244. ' if (a[Result] === 0) return Result;',
  4245. ' };',
  4246. ' return Result;',
  4247. '};',
  4248. '']),
  4249. LinesToStr([
  4250. '$mod.Func1([1, 2, 3]);'
  4251. ]));
  4252. end;
  4253. procedure TTestModule.TestFunctionResultInTypeCast;
  4254. begin
  4255. StartProgram(false);
  4256. Add([
  4257. 'function GetInt: longint;',
  4258. 'begin',
  4259. 'end;',
  4260. 'begin',
  4261. ' if Byte(GetInt)=0 then ;',
  4262. '']);
  4263. ConvertProgram;
  4264. CheckSource('TestFunctionResultInTypeCast',
  4265. LinesToStr([ // statements
  4266. 'this.GetInt = function () {',
  4267. ' var Result = 0;',
  4268. ' return Result;',
  4269. '};',
  4270. '']),
  4271. LinesToStr([
  4272. 'if (($mod.GetInt() & 255) === 0) ;'
  4273. ]));
  4274. end;
  4275. procedure TTestModule.TestExit;
  4276. begin
  4277. StartProgram(false);
  4278. Add('procedure ProcA;');
  4279. Add('begin');
  4280. Add(' exit;');
  4281. Add('end;');
  4282. Add('function FuncB: longint;');
  4283. Add('begin');
  4284. Add(' exit;');
  4285. Add(' exit(3);');
  4286. Add('end;');
  4287. Add('function FuncC: string;');
  4288. Add('begin');
  4289. Add(' exit;');
  4290. Add(' exit(''a'');');
  4291. Add(' exit(''abc'');');
  4292. Add('end;');
  4293. Add('begin');
  4294. Add(' exit;');
  4295. Add(' exit(1);');
  4296. ConvertProgram;
  4297. CheckSource('TestExit',
  4298. LinesToStr([ // statements
  4299. 'this.ProcA = function () {',
  4300. ' return;',
  4301. '};',
  4302. 'this.FuncB = function () {',
  4303. ' var Result = 0;',
  4304. ' return Result;',
  4305. ' return 3;',
  4306. ' return Result;',
  4307. '};',
  4308. 'this.FuncC = function () {',
  4309. ' var Result = "";',
  4310. ' return Result;',
  4311. ' return "a";',
  4312. ' return "abc";',
  4313. ' return Result;',
  4314. '};'
  4315. ]),
  4316. LinesToStr([
  4317. 'return;',
  4318. 'return 1;',
  4319. '']));
  4320. end;
  4321. procedure TTestModule.TestExit_ResultInFinally;
  4322. begin
  4323. StartProgram(false);
  4324. Add([
  4325. 'function Run: word;',
  4326. 'begin',
  4327. ' try',
  4328. ' exit(3);', // no Result in finally -> use return 3
  4329. ' finally',
  4330. ' end;',
  4331. 'end;',
  4332. 'function Fly: word;',
  4333. 'begin',
  4334. ' try',
  4335. ' exit(3);',
  4336. ' finally',
  4337. ' if Result>0 then ;',
  4338. ' end;',
  4339. 'end;',
  4340. 'function Jump: word;',
  4341. 'begin',
  4342. ' try',
  4343. ' try',
  4344. ' exit(4);',
  4345. ' finally',
  4346. ' end;',
  4347. ' finally',
  4348. ' if Result>0 then ;',
  4349. ' end;',
  4350. 'end;',
  4351. 'begin',
  4352. '']);
  4353. ConvertProgram;
  4354. CheckSource('TestExit_ResultInFinally',
  4355. LinesToStr([ // statements
  4356. 'this.Run = function () {',
  4357. ' var Result = 0;',
  4358. ' try {',
  4359. ' return 3;',
  4360. ' } finally {',
  4361. ' };',
  4362. ' return Result;',
  4363. '};',
  4364. 'this.Fly = function () {',
  4365. ' var Result = 0;',
  4366. ' try {',
  4367. ' Result = 3;',
  4368. ' return Result;',
  4369. ' } finally {',
  4370. ' if (Result > 0) ;',
  4371. ' };',
  4372. ' return Result;',
  4373. '};',
  4374. 'this.Jump = function () {',
  4375. ' var Result = 0;',
  4376. ' try {',
  4377. ' try {',
  4378. ' Result = 4;',
  4379. ' return Result;',
  4380. ' } finally {',
  4381. ' };',
  4382. ' } finally {',
  4383. ' if (Result > 0) ;',
  4384. ' };',
  4385. ' return Result;',
  4386. '};',
  4387. '']),
  4388. LinesToStr([
  4389. '']));
  4390. end;
  4391. procedure TTestModule.TestBreak;
  4392. begin
  4393. StartProgram(false);
  4394. Add([
  4395. 'var',
  4396. ' i: longint;',
  4397. 'begin',
  4398. ' repeat',
  4399. ' break;',
  4400. ' until true;',
  4401. ' while true do',
  4402. ' break;',
  4403. ' for i:=1 to 2 do',
  4404. ' break;']);
  4405. ConvertProgram;
  4406. CheckSource('TestBreak',
  4407. LinesToStr([ // statements
  4408. 'this.i = 0;'
  4409. ]),
  4410. LinesToStr([
  4411. 'do {',
  4412. ' break;',
  4413. '} while (!true);',
  4414. 'while (true) break;',
  4415. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4416. '']));
  4417. end;
  4418. procedure TTestModule.TestBreakAsVar;
  4419. begin
  4420. StartProgram(false);
  4421. Add([
  4422. 'procedure DoIt(break: boolean);',
  4423. 'begin',
  4424. ' if break then ;',
  4425. 'end;',
  4426. 'var',
  4427. ' break: boolean;',
  4428. 'begin',
  4429. ' if break then ;']);
  4430. ConvertProgram;
  4431. CheckSource('TestBreakAsVar',
  4432. LinesToStr([ // statements
  4433. 'this.DoIt = function (Break) {',
  4434. ' if (Break) ;',
  4435. '};',
  4436. 'this.Break = false;',
  4437. '']),
  4438. LinesToStr([
  4439. 'if($mod.Break) ;',
  4440. '']));
  4441. end;
  4442. procedure TTestModule.TestContinue;
  4443. begin
  4444. StartProgram(false);
  4445. Add('var i: longint;');
  4446. Add('begin');
  4447. Add(' repeat');
  4448. Add(' continue;');
  4449. Add(' until true;');
  4450. Add(' while true do');
  4451. Add(' continue;');
  4452. Add(' for i:=1 to 2 do');
  4453. Add(' continue;');
  4454. ConvertProgram;
  4455. CheckSource('TestContinue',
  4456. LinesToStr([ // statements
  4457. 'this.i = 0;'
  4458. ]),
  4459. LinesToStr([
  4460. 'do {',
  4461. ' continue;',
  4462. '} while (!true);',
  4463. 'while (true) continue;',
  4464. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4465. '']));
  4466. end;
  4467. procedure TTestModule.TestProc_External;
  4468. begin
  4469. StartProgram(false);
  4470. Add('procedure Foo; external name ''console.log'';');
  4471. Add('function Bar: longint; external name ''get.item'';');
  4472. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4473. Add('var');
  4474. Add(' i: longint;');
  4475. Add('begin');
  4476. Add(' Foo;');
  4477. Add(' i:=Bar;');
  4478. Add(' i:=Bla(''abc'');');
  4479. ConvertProgram;
  4480. CheckSource('TestProc_External',
  4481. LinesToStr([ // statements
  4482. 'this.i = 0;'
  4483. ]),
  4484. LinesToStr([
  4485. 'console.log();',
  4486. '$mod.i = get.item();',
  4487. '$mod.i = apply.something("abc");'
  4488. ]));
  4489. end;
  4490. procedure TTestModule.TestProc_ExternalOtherUnit;
  4491. begin
  4492. AddModuleWithIntfImplSrc('unit2.pas',
  4493. LinesToStr([
  4494. 'procedure Now; external name ''Date.now'';',
  4495. 'procedure DoIt;'
  4496. ]),
  4497. 'procedure doit; begin end;');
  4498. StartUnit(true);
  4499. Add('interface');
  4500. Add('uses unit2;');
  4501. Add('implementation');
  4502. Add('begin');
  4503. Add(' now;');
  4504. Add(' now();');
  4505. Add(' uNit2.now;');
  4506. Add(' uNit2.now();');
  4507. Add(' doit;');
  4508. Add(' uNit2.doit;');
  4509. ConvertUnit;
  4510. CheckSource('TestProc_ExternalOtherUnit',
  4511. LinesToStr([
  4512. '']),
  4513. LinesToStr([
  4514. 'Date.now();',
  4515. 'Date.now();',
  4516. 'Date.now();',
  4517. 'Date.now();',
  4518. 'pas.unit2.DoIt();',
  4519. 'pas.unit2.DoIt();',
  4520. '']));
  4521. end;
  4522. procedure TTestModule.TestProc_Asm;
  4523. begin
  4524. StartProgram(false);
  4525. Add([
  4526. '{$mode delphi}',
  4527. 'function DoIt: longint;',
  4528. 'begin;',
  4529. ' asm',
  4530. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4531. ' end;',
  4532. ' asm console.log(); end;',
  4533. ' asm',
  4534. ' s = "'' ";',
  4535. ' s = ''" '';',
  4536. ' s = s + "world" + "''";',
  4537. ' // end',
  4538. ' s = ''end'';',
  4539. ' s = "end";',
  4540. ' s = "foo\"bar";',
  4541. ' s = ''a\''b'';',
  4542. ' s = `${expr}\`-"-''-`;',
  4543. ' s = `multi',
  4544. 'line`;',
  4545. ' end;',
  4546. 'end;',
  4547. 'procedure Fly;',
  4548. 'asm',
  4549. ' return;',
  4550. 'end;',
  4551. 'begin']);
  4552. ConvertProgram;
  4553. CheckSource('TestProc_Asm',
  4554. LinesToStr([ // statements
  4555. 'this.DoIt = function () {',
  4556. ' var Result = 0;',
  4557. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4558. ' console.log();',
  4559. ' s = "'' ";',
  4560. ' s = ''" '';',
  4561. ' s = s + "world" + "''";',
  4562. ' // end',
  4563. ' s = ''end'';',
  4564. ' s = "end";',
  4565. ' s = "foo\"bar";',
  4566. ' s = ''a\''b'';',
  4567. ' s = `${expr}\`-"-''-`;',
  4568. ' s = `multi',
  4569. 'line`;',
  4570. ' return Result;',
  4571. '};',
  4572. 'this.Fly = function () {',
  4573. ' return;',
  4574. '};',
  4575. '']),
  4576. LinesToStr([
  4577. ''
  4578. ]));
  4579. end;
  4580. procedure TTestModule.TestProc_AsmSubBlock;
  4581. begin
  4582. StartProgram(true,[supTObject]);
  4583. Add([
  4584. '{$mode delphi}',
  4585. 'type',
  4586. ' TBird = class end;',
  4587. 'procedure Run(w: word);',
  4588. 'begin;',
  4589. ' if true then asm console.log(); end;',
  4590. ' if w>3 then asm',
  4591. ' var a = w+1;',
  4592. ' w = a+3;',
  4593. ' end;',
  4594. ' while (w>7) do asm',
  4595. ' w+=3; w*=2;',
  4596. ' end;',
  4597. ' try',
  4598. ' except',
  4599. ' on E: TBird do',
  4600. ' asm console.log(E); end;',
  4601. ' on E: TObject do',
  4602. ' asm var i=3; i--; end;',
  4603. ' else asm Fly; High; end;',
  4604. ' end;',
  4605. 'end;',
  4606. 'begin']);
  4607. ConvertProgram;
  4608. CheckSource('TestProc_AsmSubBlock',
  4609. LinesToStr([ // statements
  4610. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4611. '});',
  4612. 'this.Run = function (w) {',
  4613. ' if (true) console.log();',
  4614. ' if (w > 3) {',
  4615. ' var a = w+1;',
  4616. ' w = a+3;',
  4617. ' };',
  4618. ' while (w > 7) {',
  4619. ' w+=3; w*=2;',
  4620. ' };',
  4621. ' try {} catch ($e) {',
  4622. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4623. ' var E = $e;',
  4624. ' console.log(E);',
  4625. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4626. ' var E = $e;',
  4627. ' var i=3; i--;',
  4628. ' } else {',
  4629. ' Fly; High;',
  4630. ' }',
  4631. ' };',
  4632. '};',
  4633. '']),
  4634. LinesToStr([
  4635. ''
  4636. ]));
  4637. end;
  4638. procedure TTestModule.TestProc_Assembler;
  4639. begin
  4640. StartProgram(false);
  4641. Add('function DoIt: longint; assembler;');
  4642. Add('asm');
  4643. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4644. Add('end;');
  4645. Add('begin');
  4646. ConvertProgram;
  4647. CheckSource('TestProc_Assembler',
  4648. LinesToStr([ // statements
  4649. 'this.DoIt = function () {',
  4650. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4651. '};'
  4652. ]),
  4653. LinesToStr([
  4654. ''
  4655. ]));
  4656. end;
  4657. procedure TTestModule.TestProc_VarParam;
  4658. begin
  4659. StartProgram(false);
  4660. Add('type integer = longint;');
  4661. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4662. Add('var vJ: integer;');
  4663. Add('begin');
  4664. Add(' vg:=vg+1;');
  4665. Add(' vj:=vh+2;');
  4666. Add(' vi:=vi+3;');
  4667. Add(' doit(vg,vg,vg);');
  4668. Add(' doit(vh,vh,vj);');
  4669. Add(' doit(vi,vi,vi);');
  4670. Add(' doit(vj,vj,vj);');
  4671. Add('end;');
  4672. Add('var i: integer;');
  4673. Add('begin');
  4674. Add(' doit(i,i,i);');
  4675. ConvertProgram;
  4676. CheckSource('TestProc_VarParam',
  4677. LinesToStr([ // statements
  4678. 'this.DoIt = function (vG,vH,vI) {',
  4679. ' var vJ = 0;',
  4680. ' vG = vG + 1;',
  4681. ' vJ = vH + 2;',
  4682. ' vI.set(vI.get()+3);',
  4683. ' $mod.DoIt(vG, vG, {',
  4684. ' get: function () {',
  4685. ' return vG;',
  4686. ' },',
  4687. ' set: function (v) {',
  4688. ' vG = v;',
  4689. ' }',
  4690. ' });',
  4691. ' $mod.DoIt(vH, vH, {',
  4692. ' get: function () {',
  4693. ' return vJ;',
  4694. ' },',
  4695. ' set: function (v) {',
  4696. ' vJ = v;',
  4697. ' }',
  4698. ' });',
  4699. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4700. ' $mod.DoIt(vJ, vJ, {',
  4701. ' get: function () {',
  4702. ' return vJ;',
  4703. ' },',
  4704. ' set: function (v) {',
  4705. ' vJ = v;',
  4706. ' }',
  4707. ' });',
  4708. '};',
  4709. 'this.i = 0;'
  4710. ]),
  4711. LinesToStr([
  4712. '$mod.DoIt($mod.i,$mod.i,{',
  4713. ' p: $mod,',
  4714. ' get: function () {',
  4715. ' return this.p.i;',
  4716. ' },',
  4717. ' set: function (v) {',
  4718. ' this.p.i = v;',
  4719. ' }',
  4720. '});'
  4721. ]));
  4722. end;
  4723. procedure TTestModule.TestProc_VarParamString;
  4724. begin
  4725. StartProgram(false);
  4726. Add(['type TCaption = string;',
  4727. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4728. 'var c: char;',
  4729. 'begin',
  4730. ' va[1]:=c;',
  4731. ' vb[2]:=c;',
  4732. ' vc[3]:=c;',
  4733. 'end;',
  4734. 'begin']);
  4735. ConvertProgram;
  4736. CheckSource('TestProc_VarParamString',
  4737. LinesToStr([ // statements
  4738. 'this.DoIt = function (vA,vB,vC) {',
  4739. ' var c = "";',
  4740. ' vA = rtl.setCharAt(vA, 0, c);',
  4741. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4742. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4743. '};',
  4744. '']),
  4745. LinesToStr([
  4746. ]));
  4747. end;
  4748. procedure TTestModule.TestProc_VarParamV;
  4749. begin
  4750. StartProgram(false);
  4751. Add([
  4752. 'procedure Inc2(var i: longint);',
  4753. 'begin',
  4754. ' i:=i+2;',
  4755. 'end;',
  4756. 'procedure DoIt(v: longint);',
  4757. 'var p: array of longint;',
  4758. 'begin',
  4759. ' Inc2(v);',
  4760. ' Inc2(p[v]);',
  4761. 'end;',
  4762. 'begin']);
  4763. ConvertProgram;
  4764. CheckSource('TestProc_VarParamV',
  4765. LinesToStr([ // statements
  4766. 'this.Inc2 = function (i) {',
  4767. ' i.set(i.get()+2);',
  4768. '};',
  4769. 'this.DoIt = function (v) {',
  4770. ' var p = [];',
  4771. ' $mod.Inc2({get: function () {',
  4772. ' return v;',
  4773. ' }, set: function (w) {',
  4774. ' v = w;',
  4775. ' }});',
  4776. ' $mod.Inc2({',
  4777. ' a: v,',
  4778. ' p: p,',
  4779. ' get: function () {',
  4780. ' return this.p[this.a];',
  4781. ' },',
  4782. ' set: function (v) {',
  4783. ' this.p[this.a] = v;',
  4784. ' }',
  4785. ' });',
  4786. '};',
  4787. '']),
  4788. LinesToStr([
  4789. '']));
  4790. end;
  4791. procedure TTestModule.TestProc_Overload;
  4792. begin
  4793. StartProgram(false);
  4794. Add('procedure DoIt(vI: longint); begin end;');
  4795. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4796. Add('procedure DoIt(vD: double); begin end;');
  4797. Add('begin');
  4798. Add(' DoIt(1);');
  4799. Add(' DoIt(2,3);');
  4800. Add(' DoIt(4.5);');
  4801. ConvertProgram;
  4802. CheckSource('TestProcedureOverload',
  4803. LinesToStr([ // statements
  4804. 'this.DoIt = function (vI) {',
  4805. '};',
  4806. 'this.DoIt$1 = function (vI, vJ) {',
  4807. '};',
  4808. 'this.DoIt$2 = function (vD) {',
  4809. '};',
  4810. '']),
  4811. LinesToStr([
  4812. '$mod.DoIt(1);',
  4813. '$mod.DoIt$1(2, 3);',
  4814. '$mod.DoIt$2(4.5);',
  4815. '']));
  4816. end;
  4817. procedure TTestModule.TestProc_OverloadForward;
  4818. begin
  4819. StartProgram(false);
  4820. Add('procedure DoIt(vI: longint); forward;');
  4821. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4822. Add('procedure doit(vi: longint); begin end;');
  4823. Add('begin');
  4824. Add(' doit(1);');
  4825. Add(' doit(2,3);');
  4826. ConvertProgram;
  4827. CheckSource('TestProcedureOverloadForward',
  4828. LinesToStr([ // statements
  4829. 'this.DoIt$1 = function (vI, vJ) {',
  4830. '};',
  4831. 'this.DoIt = function (vI) {',
  4832. '};',
  4833. '']),
  4834. LinesToStr([
  4835. '$mod.DoIt(1);',
  4836. '$mod.DoIt$1(2, 3);',
  4837. '']));
  4838. end;
  4839. procedure TTestModule.TestProc_OverloadIntfImpl;
  4840. begin
  4841. StartUnit(false);
  4842. Add('interface');
  4843. Add('procedure DoIt(vI: longint);');
  4844. Add('procedure DoIt(vI, vJ: longint);');
  4845. Add('implementation');
  4846. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4847. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4848. Add('procedure DoIt(vi: longint); begin end;');
  4849. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4850. Add('procedure DoIt(vi, vj: longint); begin end;');
  4851. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4852. Add('begin');
  4853. Add(' doit(1);');
  4854. Add(' doit(2,3);');
  4855. Add(' doit(4,5,6);');
  4856. Add(' doit(7,8,9,10);');
  4857. Add(' doit(11,12,13,14,15);');
  4858. ConvertUnit;
  4859. CheckSource('TestProcedureOverloadUnit',
  4860. LinesToStr([ // statements
  4861. 'var $impl = $mod.$impl;',
  4862. 'this.DoIt = function (vI) {',
  4863. '};',
  4864. 'this.DoIt$1 = function (vI, vJ) {',
  4865. '};',
  4866. '']),
  4867. LinesToStr([ // this.$init
  4868. '$mod.DoIt(1);',
  4869. '$mod.DoIt$1(2, 3);',
  4870. '$impl.DoIt$3(4,5,6);',
  4871. '$impl.DoIt$4(7,8,9,10);',
  4872. '$impl.DoIt$2(11,12,13,14,15);',
  4873. '']),
  4874. LinesToStr([ // implementation
  4875. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4876. '};',
  4877. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4878. '};',
  4879. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4880. '};',
  4881. '']));
  4882. end;
  4883. procedure TTestModule.TestProc_OverloadNested;
  4884. begin
  4885. StartProgram(false);
  4886. Add([
  4887. 'procedure doit(vA: longint);',
  4888. ' procedure DoIt(vA, vB: longint); overload;',
  4889. ' begin',
  4890. ' doit(1);',
  4891. ' doit(1,2);',
  4892. ' end;',
  4893. ' procedure doit(vA, vB, vC: longint);',
  4894. ' begin',
  4895. ' doit(1);',
  4896. ' doit(1,2);',
  4897. ' doit(1,2,3);',
  4898. ' end;',
  4899. 'begin',
  4900. ' doit(1);',
  4901. ' doit(1,2);',
  4902. ' doit(1,2,3);',
  4903. 'end;',
  4904. 'begin // main',
  4905. ' doit(1);']);
  4906. ConvertProgram;
  4907. CheckSource('TestProcedureOverloadNested',
  4908. LinesToStr([ // statements
  4909. 'this.doit = function (vA) {',
  4910. ' function DoIt$1(vA, vB) {',
  4911. ' $mod.doit(1);',
  4912. ' DoIt$1(1, 2);',
  4913. ' };',
  4914. ' function doit$2(vA, vB, vC) {',
  4915. ' $mod.doit(1);',
  4916. ' DoIt$1(1, 2);',
  4917. ' doit$2(1, 2, 3);',
  4918. ' };',
  4919. ' $mod.doit(1);',
  4920. ' DoIt$1(1, 2);',
  4921. ' doit$2(1, 2, 3);',
  4922. '};',
  4923. '']),
  4924. LinesToStr([
  4925. '$mod.doit(1);',
  4926. '']));
  4927. end;
  4928. procedure TTestModule.TestProc_OverloadNestedForward;
  4929. begin
  4930. StartProgram(false);
  4931. Add([
  4932. 'procedure DoIt(vA: longint); overload; forward;',
  4933. 'procedure DoIt(vB, vC: longint); overload;',
  4934. 'begin // 2 param overload',
  4935. ' doit(1);',
  4936. ' doit(1,2);',
  4937. 'end;',
  4938. 'procedure doit(vA: longint);',
  4939. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4940. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4941. ' begin // 4 param overload',
  4942. ' doit(1);',
  4943. ' doit(1,2);',
  4944. ' doit(1,2,3);',
  4945. ' doit(1,2,3,4);',
  4946. ' end;',
  4947. ' procedure doit(vA, vB, vC: longint);',
  4948. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4949. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4950. ' begin // 6 param overload',
  4951. ' doit(1);',
  4952. ' doit(1,2);',
  4953. ' doit(1,2,3);',
  4954. ' doit(1,2,3,4);',
  4955. ' doit(1,2,3,4,5);',
  4956. ' doit(1,2,3,4,5,6);',
  4957. ' end;',
  4958. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4959. ' begin // 5 param overload',
  4960. ' doit(1);',
  4961. ' doit(1,2);',
  4962. ' doit(1,2,3);',
  4963. ' doit(1,2,3,4);',
  4964. ' doit(1,2,3,4,5);',
  4965. ' doit(1,2,3,4,5,6);',
  4966. ' end;',
  4967. ' begin // 3 param overload',
  4968. ' doit(1);',
  4969. ' doit(1,2);',
  4970. ' doit(1,2,3);',
  4971. ' doit(1,2,3,4);',
  4972. ' doit(1,2,3,4,5);',
  4973. ' doit(1,2,3,4,5,6);',
  4974. ' end;',
  4975. 'begin // 1 param overload',
  4976. ' doit(1);',
  4977. ' doit(1,2);',
  4978. ' doit(1,2,3);',
  4979. ' doit(1,2,3,4);',
  4980. 'end;',
  4981. 'begin // main',
  4982. ' doit(1);',
  4983. ' doit(1,2);']);
  4984. ConvertProgram;
  4985. CheckSource('TestProc_OverloadNestedForward',
  4986. LinesToStr([ // statements
  4987. 'this.DoIt$1 = function (vB, vC) {',
  4988. ' $mod.DoIt(1);',
  4989. ' $mod.DoIt$1(1, 2);',
  4990. '};',
  4991. 'this.DoIt = function (vA) {',
  4992. ' function DoIt$3(vA, vB, vC, vD) {',
  4993. ' $mod.DoIt(1);',
  4994. ' $mod.DoIt$1(1, 2);',
  4995. ' DoIt$2(1, 2, 3);',
  4996. ' DoIt$3(1, 2, 3, 4);',
  4997. ' };',
  4998. ' function DoIt$2(vA, vB, vC) {',
  4999. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5000. ' $mod.DoIt(1);',
  5001. ' $mod.DoIt$1(1, 2);',
  5002. ' DoIt$2(1, 2, 3);',
  5003. ' DoIt$3(1, 2, 3, 4);',
  5004. ' DoIt$4(1, 2, 3, 4, 5);',
  5005. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5006. ' };',
  5007. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5008. ' $mod.DoIt(1);',
  5009. ' $mod.DoIt$1(1, 2);',
  5010. ' DoIt$2(1, 2, 3);',
  5011. ' DoIt$3(1, 2, 3, 4);',
  5012. ' DoIt$4(1, 2, 3, 4, 5);',
  5013. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5014. ' };',
  5015. ' $mod.DoIt(1);',
  5016. ' $mod.DoIt$1(1, 2);',
  5017. ' DoIt$2(1, 2, 3);',
  5018. ' DoIt$3(1, 2, 3, 4);',
  5019. ' DoIt$4(1, 2, 3, 4, 5);',
  5020. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5021. ' };',
  5022. ' $mod.DoIt(1);',
  5023. ' $mod.DoIt$1(1, 2);',
  5024. ' DoIt$2(1, 2, 3);',
  5025. ' DoIt$3(1, 2, 3, 4);',
  5026. '};',
  5027. '']),
  5028. LinesToStr([
  5029. '$mod.DoIt(1);',
  5030. '$mod.DoIt$1(1, 2);',
  5031. '']));
  5032. end;
  5033. procedure TTestModule.TestProc_OverloadUnitCycle;
  5034. begin
  5035. AddModuleWithIntfImplSrc('Unit2.pas',
  5036. LinesToStr([
  5037. 'type',
  5038. ' TObject = class',
  5039. ' procedure DoIt(b: boolean); virtual; abstract;',
  5040. ' procedure DoIt(i: longint); virtual; abstract;',
  5041. ' end;',
  5042. '']),
  5043. 'uses test1;');
  5044. StartUnit(true);
  5045. Add([
  5046. 'interface',
  5047. 'uses unit2;',
  5048. 'type',
  5049. ' TEagle = class(TObject)',
  5050. ' procedure DoIt(b: boolean); override;',
  5051. ' procedure DoIt(i: longint); override;',
  5052. ' end;',
  5053. 'implementation',
  5054. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5055. 'procedure TEagle.DoIt(i: longint); begin end;',
  5056. '']);
  5057. ConvertUnit;
  5058. CheckSource('TestProc_OverloadUnitCycle',
  5059. LinesToStr([ // statements
  5060. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5061. ' this.DoIt = function (b) {',
  5062. ' };',
  5063. ' this.DoIt$1 = function (i) {',
  5064. ' };',
  5065. '});',
  5066. '']),
  5067. '',
  5068. LinesToStr([
  5069. '']));
  5070. end;
  5071. procedure TTestModule.TestProc_Varargs;
  5072. begin
  5073. StartProgram(false);
  5074. Add([
  5075. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5076. 'procedure ProcB; varargs; external name ''ProcB'';',
  5077. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5078. 'function GetIt: longint; begin end;',
  5079. 'begin',
  5080. ' ProcA(1);',
  5081. ' ProcA(1,2);',
  5082. ' ProcA(1,2.0);',
  5083. ' ProcA(1,2,3);',
  5084. ' ProcA(1,''2'');',
  5085. ' ProcA(2,'''');',
  5086. ' ProcA(3,false);',
  5087. ' ProcB;',
  5088. ' ProcB();',
  5089. ' ProcB(4);',
  5090. ' ProcB(''foo'');',
  5091. ' ProcC;',
  5092. ' ProcC();',
  5093. ' ProcC(4);',
  5094. ' ProcC(5,''foo'');',
  5095. ' ProcB(GetIt);',
  5096. ' ProcB(GetIt());',
  5097. ' ProcB(GetIt,GetIt());']);
  5098. ConvertProgram;
  5099. CheckSource('TestProc_Varargs',
  5100. LinesToStr([ // statements
  5101. 'this.GetIt = function () {',
  5102. ' var Result = 0;',
  5103. ' return Result;',
  5104. '};',
  5105. '']),
  5106. LinesToStr([
  5107. 'ProcA(1);',
  5108. 'ProcA(1, 2);',
  5109. 'ProcA(1, 2.0);',
  5110. 'ProcA(1, 2, 3);',
  5111. 'ProcA(1, "2");',
  5112. 'ProcA(2, "");',
  5113. 'ProcA(3, false);',
  5114. 'ProcB();',
  5115. 'ProcB();',
  5116. 'ProcB(4);',
  5117. 'ProcB("foo");',
  5118. 'ProcC(17);',
  5119. 'ProcC(17);',
  5120. 'ProcC(4);',
  5121. 'ProcC(5, "foo");',
  5122. 'ProcB($mod.GetIt());',
  5123. 'ProcB($mod.GetIt());',
  5124. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5125. '']));
  5126. end;
  5127. procedure TTestModule.TestProc_ConstOrder;
  5128. begin
  5129. StartProgram(false);
  5130. Add([
  5131. 'const A = 3;',
  5132. 'const B = A+1;',
  5133. 'procedure DoIt;',
  5134. 'const C = A+1;',
  5135. 'const D = B+1;',
  5136. 'const E = D+C+B+A;',
  5137. 'begin',
  5138. 'end;',
  5139. 'begin'
  5140. ]);
  5141. ConvertProgram;
  5142. CheckSource('TestProc_ConstOrder',
  5143. LinesToStr([ // statements
  5144. 'this.A = 3;',
  5145. 'this.B = 3 + 1;',
  5146. 'var C = 3 + 1;',
  5147. 'var D = 4 + 1;',
  5148. 'var E = 5 + 4 + 4 + 3;',
  5149. 'this.DoIt = function () {',
  5150. '};',
  5151. '']),
  5152. LinesToStr([
  5153. ''
  5154. ]));
  5155. end;
  5156. procedure TTestModule.TestProc_DuplicateConst;
  5157. begin
  5158. StartProgram(false);
  5159. Add([
  5160. 'const A = 1;',
  5161. 'procedure DoIt;',
  5162. 'const A = 2;',
  5163. ' procedure SubIt;',
  5164. ' const A = 21;',
  5165. ' begin',
  5166. ' end;',
  5167. 'begin',
  5168. 'end;',
  5169. 'procedure DoSome;',
  5170. 'const A = 3;',
  5171. 'begin',
  5172. 'end;',
  5173. 'begin'
  5174. ]);
  5175. ConvertProgram;
  5176. CheckSource('TestProc_DuplicateConst',
  5177. LinesToStr([ // statements
  5178. 'this.A = 1;',
  5179. 'var A$1 = 2;',
  5180. 'var A$2 = 21;',
  5181. 'this.DoIt = function () {',
  5182. ' function SubIt() {',
  5183. ' };',
  5184. '};',
  5185. 'var A$3 = 3;',
  5186. 'this.DoSome = function () {',
  5187. '};',
  5188. '']),
  5189. LinesToStr([
  5190. ''
  5191. ]));
  5192. end;
  5193. procedure TTestModule.TestProc_LocalVarAbsolute;
  5194. begin
  5195. StartProgram(false);
  5196. Add([
  5197. 'type',
  5198. ' TObject = class',
  5199. ' Index: longint;',
  5200. ' procedure DoAbs(Item: pointer);',
  5201. ' end;',
  5202. 'procedure TObject.DoAbs(Item: pointer);',
  5203. 'var',
  5204. ' o: TObject absolute Item;',
  5205. 'begin',
  5206. ' if o.Index<o.Index then o.Index:=o.Index;',
  5207. 'end;',
  5208. 'procedure DoIt(i: longint; p: pointer);',
  5209. 'var',
  5210. ' d: double absolute i;',
  5211. ' s: string absolute d;',
  5212. ' oi: TObject absolute i;',
  5213. ' op: TObject absolute p;',
  5214. 'begin',
  5215. ' if d=d then d:=d;',
  5216. ' if s=s then s:=s;',
  5217. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5218. ' if op.Index=op.Index then op.Index:=op.Index;',
  5219. 'end;',
  5220. 'begin']);
  5221. ConvertProgram;
  5222. CheckSource('TestProc_LocalVarAbsolute',
  5223. LinesToStr([ // statements
  5224. 'rtl.createClass(this, "TObject", null, function () {',
  5225. ' this.$init = function () {',
  5226. ' this.Index = 0;',
  5227. ' };',
  5228. ' this.$final = function () {',
  5229. ' };',
  5230. ' this.DoAbs = function (Item) {',
  5231. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5232. ' };',
  5233. '});',
  5234. 'this.DoIt = function (i, p) {',
  5235. ' if (i === i) i = i;',
  5236. ' if (i === i) i = i;',
  5237. ' if (i.Index < i.Index) i.Index = i.Index;',
  5238. ' if (p.Index === p.Index) p.Index = p.Index;',
  5239. '};'
  5240. ]),
  5241. LinesToStr([
  5242. ]));
  5243. end;
  5244. procedure TTestModule.TestProc_LocalVarInit;
  5245. begin
  5246. StartProgram(false);
  5247. Add([
  5248. 'type TBytes = array of byte;',
  5249. 'procedure DoIt;',
  5250. 'const c = 4;',
  5251. 'var',
  5252. ' b: byte = 1;',
  5253. ' w: word = 2+c;',
  5254. ' p: pointer = nil;',
  5255. ' Buffer: TBytes = nil;',
  5256. 'begin',
  5257. 'end;',
  5258. 'begin']);
  5259. ConvertProgram;
  5260. CheckSource('TestProc_LocalVarInit',
  5261. LinesToStr([ // statements
  5262. 'var c = 4;',
  5263. 'this.DoIt = function () {',
  5264. ' var b = 1;',
  5265. ' var w = 2 + 4;',
  5266. ' var p = null;',
  5267. ' var Buffer = [];',
  5268. '};',
  5269. '']),
  5270. LinesToStr([
  5271. ]));
  5272. end;
  5273. procedure TTestModule.TestProc_ReservedWords;
  5274. begin
  5275. StartProgram(false);
  5276. Add([
  5277. 'procedure Date(ArrayBuffer: longint);',
  5278. 'const',
  5279. ' NaN: longint = 3;',
  5280. 'var',
  5281. ' &Boolean: longint;',
  5282. ' procedure Error(ArrayBuffer: longint);',
  5283. ' begin',
  5284. ' end;',
  5285. 'begin',
  5286. ' Nan:=&bOolean;',
  5287. 'end;',
  5288. 'begin',
  5289. ' Date(1);']);
  5290. ConvertProgram;
  5291. CheckSource('TestProc_ReservedWords',
  5292. LinesToStr([ // statements
  5293. 'var naN = 3;',
  5294. 'this.Date = function (arrayBuffer) {',
  5295. ' var boolean = 0;',
  5296. ' function error(arrayBuffer) {',
  5297. ' };',
  5298. ' naN = boolean;',
  5299. '};',
  5300. '']),
  5301. LinesToStr([
  5302. ' $mod.Date(1);'
  5303. ]));
  5304. end;
  5305. procedure TTestModule.TestProc_ConstRefWord;
  5306. begin
  5307. StartProgram(false);
  5308. Add([
  5309. 'procedure Run(constref w: word);',
  5310. 'var l: word;',
  5311. 'begin',
  5312. ' l:=w;',
  5313. ' Run(w);',
  5314. ' Run(l);',
  5315. 'end;',
  5316. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5317. 'begin',
  5318. ' Run(a);',
  5319. ' Run(b);',
  5320. ' Run(c);',
  5321. ' Run(d);',
  5322. ' Run(e);',
  5323. 'end;',
  5324. 'begin',
  5325. ' Run(1);']);
  5326. ConvertProgram;
  5327. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5328. CheckSource('TestProc_ConstRefWord',
  5329. LinesToStr([ // statements
  5330. 'this.Run = function (w) {',
  5331. ' var l = 0;',
  5332. ' l = w;',
  5333. ' $mod.Run(w);',
  5334. ' $mod.Run(l);',
  5335. '};',
  5336. 'this.Fly = function (a, b, c, d, e) {',
  5337. ' $mod.Run(a);',
  5338. ' $mod.Run(b.get());',
  5339. ' $mod.Run(c.get());',
  5340. ' $mod.Run(d);',
  5341. ' $mod.Run(e);',
  5342. '};',
  5343. '']),
  5344. LinesToStr([
  5345. '$mod.Run(1);'
  5346. ]));
  5347. end;
  5348. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5349. begin
  5350. StartProgram(false);
  5351. Add([
  5352. '{$mode objfpc}',
  5353. 'type',
  5354. ' TFunc = reference to function(x: word): word;',
  5355. 'var Func: TFunc;',
  5356. 'procedure DoIt(a: word);',
  5357. 'begin',
  5358. ' Func:=function(b:word): word',
  5359. ' begin',
  5360. ' Result:=a+b;',
  5361. ' exit(b);',
  5362. ' exit(Result);',
  5363. ' end;',// test semicolon
  5364. ' a:=3;',
  5365. 'end;',
  5366. 'begin',
  5367. ' Func:=function(c:word):word begin',
  5368. ' Result:=3+c;',
  5369. ' exit(c);',
  5370. ' exit(Result);',
  5371. ' end;']);
  5372. ConvertProgram;
  5373. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5374. LinesToStr([ // statements
  5375. 'this.Func = null;',
  5376. 'this.DoIt = function (a) {',
  5377. ' $mod.Func = function (b) {',
  5378. ' var Result = 0;',
  5379. ' Result = a + b;',
  5380. ' return b;',
  5381. ' return Result;',
  5382. ' return Result;',
  5383. ' };',
  5384. ' a = 3;',
  5385. '};',
  5386. '']),
  5387. LinesToStr([
  5388. '$mod.Func = function (c) {',
  5389. ' var Result = 0;',
  5390. ' Result = 3 + c;',
  5391. ' return c;',
  5392. ' return Result;',
  5393. ' return Result;',
  5394. '};',
  5395. '']));
  5396. end;
  5397. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5398. begin
  5399. StartProgram(false);
  5400. Add([
  5401. '{$mode delphi}',
  5402. 'type',
  5403. ' TProc = reference to procedure(x: word);',
  5404. 'procedure DoIt(a: word);',
  5405. 'var Proc: TProc;',
  5406. 'begin',
  5407. ' Proc:=procedure(b:word) begin end;',
  5408. 'end;',
  5409. 'var Proc: TProc;',
  5410. 'begin',
  5411. ' Proc:=procedure(c:word) begin end;',
  5412. '']);
  5413. ConvertProgram;
  5414. CheckSource('TestAnonymousProc_Assign_Delphi',
  5415. LinesToStr([ // statements
  5416. 'this.DoIt = function (a) {',
  5417. ' var Proc = null;',
  5418. ' Proc = function (b) {',
  5419. ' };',
  5420. '};',
  5421. 'this.Proc = null;',
  5422. '']),
  5423. LinesToStr([
  5424. '$mod.Proc = function (c) {',
  5425. '};',
  5426. '']));
  5427. end;
  5428. procedure TTestModule.TestAnonymousProc_Arg;
  5429. begin
  5430. StartProgram(false);
  5431. Add([
  5432. 'type',
  5433. ' TProc = reference to procedure;',
  5434. ' TFunc = reference to function(x: word): word;',
  5435. 'procedure DoMore(f,g: TProc);',
  5436. 'begin',
  5437. 'end;',
  5438. 'procedure DoOdd(v: jsvalue);',
  5439. 'begin',
  5440. 'end;',
  5441. 'procedure DoIt(f: TFunc);',
  5442. 'begin',
  5443. ' DoIt(function(b:word): word',
  5444. ' begin',
  5445. ' Result:=1+b;',
  5446. ' end);',
  5447. ' DoMore(procedure begin end, procedure begin end);',
  5448. ' DoOdd(procedure begin end);',
  5449. 'end;',
  5450. 'begin',
  5451. ' DoMore(procedure begin end,',
  5452. ' procedure assembler asm',
  5453. ' console.log("c");',
  5454. ' end);',
  5455. '']);
  5456. ConvertProgram;
  5457. CheckSource('TestAnonymousProc_Arg',
  5458. LinesToStr([ // statements
  5459. 'this.DoMore = function (f, g) {',
  5460. '};',
  5461. 'this.DoOdd = function (v) {',
  5462. '};',
  5463. 'this.DoIt = function (f) {',
  5464. ' $mod.DoIt(function (b) {',
  5465. ' var Result = 0;',
  5466. ' Result = 1 + b;',
  5467. ' return Result;',
  5468. ' });',
  5469. ' $mod.DoMore(function () {',
  5470. ' }, function () {',
  5471. ' });',
  5472. ' $mod.DoOdd(function () {',
  5473. ' });',
  5474. '};',
  5475. '']),
  5476. LinesToStr([
  5477. '$mod.DoMore(function () {',
  5478. '}, function () {',
  5479. ' console.log("c");',
  5480. '});',
  5481. '']));
  5482. end;
  5483. procedure TTestModule.TestAnonymousProc_Typecast;
  5484. begin
  5485. StartProgram(false);
  5486. Add([
  5487. 'type',
  5488. ' TProc = reference to procedure(w: word);',
  5489. ' TArr = array of word;',
  5490. ' TFuncArr = reference to function: TArr;',
  5491. 'procedure DoIt(p: TProc);',
  5492. 'var',
  5493. ' w: word;',
  5494. ' a: TArr;',
  5495. 'begin',
  5496. ' p:=TProc(procedure(b: smallint) begin end);',
  5497. ' a:=TFuncArr(function: TArr begin end)();',
  5498. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5499. 'end;',
  5500. 'begin']);
  5501. ConvertProgram;
  5502. CheckSource('TestAnonymousProc_Typecast',
  5503. LinesToStr([ // statements
  5504. 'this.DoIt = function (p) {',
  5505. ' var w = 0;',
  5506. ' var a = [];',
  5507. ' p = function (b) {',
  5508. ' };',
  5509. ' a = function () {',
  5510. ' var Result = [];',
  5511. ' return Result;',
  5512. ' }();',
  5513. ' w = function () {',
  5514. ' var Result = [];',
  5515. ' return Result;',
  5516. ' }()[3];',
  5517. '};',
  5518. '']),
  5519. LinesToStr([
  5520. '']));
  5521. end;
  5522. procedure TTestModule.TestAnonymousProc_With;
  5523. begin
  5524. StartProgram(false);
  5525. Add([
  5526. 'type',
  5527. ' TProc = reference to procedure(w: word);',
  5528. ' TObject = class',
  5529. ' b: boolean;',
  5530. ' end;',
  5531. 'var',
  5532. ' p: TProc;',
  5533. ' bird: TObject;',
  5534. 'begin',
  5535. ' with bird do',
  5536. ' p:=procedure(w: word)',
  5537. ' begin',
  5538. ' b:=w>2;',
  5539. ' end;',
  5540. '']);
  5541. ConvertProgram;
  5542. CheckSource('TestAnonymousProc_With',
  5543. LinesToStr([ // statements
  5544. 'rtl.createClass(this, "TObject", null, function () {',
  5545. ' this.$init = function () {',
  5546. ' this.b = false;',
  5547. ' };',
  5548. ' this.$final = function () {',
  5549. ' };',
  5550. '});',
  5551. 'this.p = null;',
  5552. 'this.bird = null;',
  5553. '']),
  5554. LinesToStr([
  5555. 'var $with = $mod.bird;',
  5556. '$mod.p = function (w) {',
  5557. ' $with.b = w > 2;',
  5558. '};',
  5559. '']));
  5560. end;
  5561. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5562. begin
  5563. StartProgram(false);
  5564. Add([
  5565. 'type',
  5566. ' TProc = reference to procedure;',
  5567. ' TObject = class',
  5568. ' b: boolean;',
  5569. ' end;',
  5570. 'procedure DoIt;',
  5571. 'var',
  5572. ' p: TProc;',
  5573. 'begin',
  5574. ' try',
  5575. ' except',
  5576. ' on E: TObject do',
  5577. ' p:=procedure',
  5578. ' begin',
  5579. ' E.b:=true;',
  5580. ' end;',
  5581. ' end;',
  5582. 'end;',
  5583. 'begin']);
  5584. ConvertProgram;
  5585. CheckSource('TestAnonymousProc_ExceptOn',
  5586. LinesToStr([ // statements
  5587. 'rtl.createClass(this, "TObject", null, function () {',
  5588. ' this.$init = function () {',
  5589. ' this.b = false;',
  5590. ' };',
  5591. ' this.$final = function () {',
  5592. ' };',
  5593. '});',
  5594. 'this.DoIt = function () {',
  5595. ' var p = null;',
  5596. ' try {} catch ($e) {',
  5597. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5598. ' var E = $e;',
  5599. ' p = function () {',
  5600. ' E.b = true;',
  5601. ' };',
  5602. ' } else throw $e',
  5603. ' };',
  5604. '};',
  5605. '']),
  5606. LinesToStr([
  5607. '']));
  5608. end;
  5609. procedure TTestModule.TestAnonymousProc_Nested;
  5610. begin
  5611. StartProgram(false);
  5612. Add([
  5613. 'type',
  5614. ' TProc = reference to procedure;',
  5615. ' TObject = class',
  5616. ' i: byte;',
  5617. ' procedure DoIt;',
  5618. ' end;',
  5619. 'procedure TObject.DoIt;',
  5620. 'var',
  5621. ' p: TProc;',
  5622. ' procedure Sub;',
  5623. ' begin',
  5624. ' p:=procedure',
  5625. ' begin',
  5626. ' i:=3;',
  5627. ' Self.i:=4;',
  5628. ' p:=procedure',
  5629. ' procedure SubSub;',
  5630. ' begin',
  5631. ' i:=13;',
  5632. ' Self.i:=14;',
  5633. ' end;',
  5634. ' begin',
  5635. ' i:=13;',
  5636. ' Self.i:=14;',
  5637. ' end;',
  5638. ' end;',
  5639. ' end;',
  5640. 'begin',
  5641. 'end;',
  5642. 'begin']);
  5643. ConvertProgram;
  5644. CheckSource('TestAnonymousProc_Nested',
  5645. LinesToStr([ // statements
  5646. 'rtl.createClass(this, "TObject", null, function () {',
  5647. ' this.$init = function () {',
  5648. ' this.i = 0;',
  5649. ' };',
  5650. ' this.$final = function () {',
  5651. ' };',
  5652. ' this.DoIt = function () {',
  5653. ' var $Self = this;',
  5654. ' var p = null;',
  5655. ' function Sub() {',
  5656. ' p = function () {',
  5657. ' $Self.i = 3;',
  5658. ' $Self.i = 4;',
  5659. ' p = function () {',
  5660. ' function SubSub() {',
  5661. ' $Self.i = 13;',
  5662. ' $Self.i = 14;',
  5663. ' };',
  5664. ' $Self.i = 13;',
  5665. ' $Self.i = 14;',
  5666. ' };',
  5667. ' };',
  5668. ' };',
  5669. ' };',
  5670. '});',
  5671. '']),
  5672. LinesToStr([
  5673. '']));
  5674. end;
  5675. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5676. begin
  5677. StartProgram(false);
  5678. Add([
  5679. 'type',
  5680. ' TProc = reference to procedure;',
  5681. 'function DoIt: TProc;',
  5682. ' function Sub: TProc;',
  5683. ' begin',
  5684. ' Result:=procedure',
  5685. ' begin',
  5686. ' Sub:=procedure',
  5687. ' procedure SubSub;',
  5688. ' begin',
  5689. ' Result:=nil;',
  5690. ' Sub:=nil;',
  5691. ' DoIt:=nil;',
  5692. ' end;',
  5693. ' begin',
  5694. ' Result:=nil;',
  5695. ' Sub:=nil;',
  5696. ' DoIt:=nil;',
  5697. ' end;',
  5698. ' end;',
  5699. ' end;',
  5700. 'begin',
  5701. 'end;',
  5702. 'begin']);
  5703. ConvertProgram;
  5704. CheckSource('TestAnonymousProc_NestedAssignResult',
  5705. LinesToStr([ // statements
  5706. 'this.DoIt = function () {',
  5707. ' var Result = null;',
  5708. ' function Sub() {',
  5709. ' var Result$1 = null;',
  5710. ' Result$1 = function () {',
  5711. ' Result$1 = function () {',
  5712. ' function SubSub() {',
  5713. ' Result$1 = null;',
  5714. ' Result$1 = null;',
  5715. ' Result = null;',
  5716. ' };',
  5717. ' Result$1 = null;',
  5718. ' Result$1 = null;',
  5719. ' Result = null;',
  5720. ' };',
  5721. ' };',
  5722. ' return Result$1;',
  5723. ' };',
  5724. ' return Result;',
  5725. '};',
  5726. '']),
  5727. LinesToStr([
  5728. '']));
  5729. end;
  5730. procedure TTestModule.TestAnonymousProc_Class;
  5731. begin
  5732. StartProgram(false);
  5733. Add([
  5734. 'type',
  5735. ' TProc = reference to procedure;',
  5736. ' TEvent = procedure of object;',
  5737. ' TObject = class',
  5738. ' Size: word;',
  5739. ' function GetIt: TProc;',
  5740. ' procedure DoIt; virtual; abstract;',
  5741. ' end;',
  5742. 'function TObject.GetIt: TProc;',
  5743. 'begin',
  5744. ' Result:=procedure',
  5745. ' var p: TEvent;',
  5746. ' begin',
  5747. ' Size:=Size;',
  5748. ' Size:=Self.Size;',
  5749. ' p:=@DoIt;',
  5750. ' p:[email protected];',
  5751. ' end;',
  5752. 'end;',
  5753. 'begin']);
  5754. ConvertProgram;
  5755. CheckSource('TestAnonymousProc_Class',
  5756. LinesToStr([ // statements
  5757. 'rtl.createClass(this, "TObject", null, function () {',
  5758. ' this.$init = function () {',
  5759. ' this.Size = 0;',
  5760. ' };',
  5761. ' this.$final = function () {',
  5762. ' };',
  5763. ' this.GetIt = function () {',
  5764. ' var $Self = this;',
  5765. ' var Result = null;',
  5766. ' Result = function () {',
  5767. ' var p = null;',
  5768. ' $Self.Size = $Self.Size;',
  5769. ' $Self.Size = $Self.Size;',
  5770. ' p = rtl.createCallback($Self, "DoIt");',
  5771. ' p = rtl.createCallback($Self, "DoIt");',
  5772. ' };',
  5773. ' return Result;',
  5774. ' };',
  5775. '});',
  5776. '']),
  5777. LinesToStr([
  5778. '']));
  5779. end;
  5780. procedure TTestModule.TestAnonymousProc_ForLoop;
  5781. begin
  5782. StartProgram(false);
  5783. Add([
  5784. 'type TProc = reference to procedure;',
  5785. 'procedure Foo(p: TProc);',
  5786. 'begin',
  5787. 'end;',
  5788. 'procedure DoIt;',
  5789. 'var i: word;',
  5790. ' a: word;',
  5791. 'begin',
  5792. ' for i:=1 to 10 do begin',
  5793. ' Foo(procedure begin a:=3; end);',
  5794. ' end;',
  5795. 'end;',
  5796. 'begin',
  5797. ' DoIt;']);
  5798. ConvertProgram;
  5799. CheckSource('TestAnonymousProc_ForLoop',
  5800. LinesToStr([ // statements
  5801. 'this.Foo = function (p) {',
  5802. '};',
  5803. 'this.DoIt = function () {',
  5804. ' var i = 0;',
  5805. ' var a = 0;',
  5806. ' for (i = 1; i <= 10; i++) {',
  5807. ' $mod.Foo(function () {',
  5808. ' a = 3;',
  5809. ' });',
  5810. ' };',
  5811. '};',
  5812. '']),
  5813. LinesToStr([
  5814. '$mod.DoIt();'
  5815. ]));
  5816. end;
  5817. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5818. begin
  5819. StartProgram(false);
  5820. Add([
  5821. '{$mode delphi}',
  5822. 'type',
  5823. ' TProc = reference to procedure;',
  5824. ' TFunc = reference to function(x: word): word;',
  5825. 'procedure Run;',
  5826. 'asm',
  5827. 'end;',
  5828. 'procedure Walk(p: TProc; f: TFunc);',
  5829. 'begin',
  5830. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5831. 'end;',
  5832. 'begin',
  5833. ' Walk(procedure',
  5834. ' asm',
  5835. ' console.log("a");',
  5836. ' end,',
  5837. ' function(x: word): word asm',
  5838. ' console.log("c");',
  5839. ' end);',
  5840. '']);
  5841. ConvertProgram;
  5842. CheckSource('TestAnonymousProc_AsmDelphi',
  5843. LinesToStr([ // statements
  5844. 'this.Run = function () {',
  5845. '};',
  5846. 'this.Walk = function (p, f) {',
  5847. ' $mod.Walk(function () {',
  5848. ' }, function (b) {',
  5849. ' return 1+b;',
  5850. ' });',
  5851. '};',
  5852. '']),
  5853. LinesToStr([
  5854. '$mod.Walk(function () {',
  5855. ' console.log("a");',
  5856. '}, function (x) {',
  5857. ' console.log("c");',
  5858. '});',
  5859. '']));
  5860. end;
  5861. procedure TTestModule.TestEnum_Name;
  5862. begin
  5863. StartProgram(false);
  5864. Add('type TMyEnum = (Red, Green, Blue);');
  5865. Add('var e: TMyEnum;');
  5866. Add('var f: TMyEnum = Blue;');
  5867. Add('begin');
  5868. Add(' e:=green;');
  5869. Add(' e:=default(TMyEnum);');
  5870. ConvertProgram;
  5871. CheckSource('TestEnum_Name',
  5872. LinesToStr([ // statements
  5873. 'this.TMyEnum = {',
  5874. ' "0":"Red",',
  5875. ' Red:0,',
  5876. ' "1":"Green",',
  5877. ' Green:1,',
  5878. ' "2":"Blue",',
  5879. ' Blue:2',
  5880. ' };',
  5881. 'this.e = 0;',
  5882. 'this.f = this.TMyEnum.Blue;'
  5883. ]),
  5884. LinesToStr([
  5885. '$mod.e=$mod.TMyEnum.Green;',
  5886. '$mod.e=$mod.TMyEnum.Red;'
  5887. ]));
  5888. end;
  5889. procedure TTestModule.TestEnum_Number;
  5890. begin
  5891. Converter.Options:=Converter.Options+[coEnumNumbers];
  5892. StartProgram(false);
  5893. Add('type TMyEnum = (Red, Green);');
  5894. Add('var');
  5895. Add(' e: TMyEnum;');
  5896. Add(' f: TMyEnum = Green;');
  5897. Add(' i: longint;');
  5898. Add('begin');
  5899. Add(' e:=green;');
  5900. Add(' i:=longint(e);');
  5901. ConvertProgram;
  5902. CheckSource('TestEnumNumber',
  5903. LinesToStr([ // statements
  5904. 'this.TMyEnum = {',
  5905. ' "0":"Red",',
  5906. ' Red:0,',
  5907. ' "1":"Green",',
  5908. ' Green:1',
  5909. ' };',
  5910. 'this.e = 0;',
  5911. 'this.f = 1;',
  5912. 'this.i = 0;'
  5913. ]),
  5914. LinesToStr([
  5915. '$mod.e=1;',
  5916. '$mod.i=$mod.e;'
  5917. ]));
  5918. end;
  5919. procedure TTestModule.TestEnum_ConstFail;
  5920. begin
  5921. StartProgram(false);
  5922. Add([
  5923. 'type TMyEnum = (Red = 100, Green = 101);',
  5924. 'var',
  5925. ' e: TMyEnum;',
  5926. ' f: TMyEnum = Green;',
  5927. 'begin',
  5928. ' e:=green;']);
  5929. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5930. ConvertProgram;
  5931. end;
  5932. procedure TTestModule.TestEnum_Functions;
  5933. begin
  5934. StartProgram(false);
  5935. Add([
  5936. 'type TMyEnum = (Red, Green);',
  5937. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5938. 'var',
  5939. ' v: longint;',
  5940. ' s: string;',
  5941. 'begin',
  5942. ' val(s,e,v);',
  5943. ' val(s,e,i);',
  5944. 'end;',
  5945. 'var',
  5946. ' e: TMyEnum;',
  5947. ' i: longint;',
  5948. ' s: string;',
  5949. ' b: boolean;',
  5950. 'begin',
  5951. ' i:=ord(red);',
  5952. ' i:=ord(green);',
  5953. ' i:=ord(e);',
  5954. ' i:=ord(b);',
  5955. ' e:=low(tmyenum);',
  5956. ' e:=low(e);',
  5957. ' b:=low(boolean);',
  5958. ' e:=high(tmyenum);',
  5959. ' e:=high(e);',
  5960. ' b:=high(boolean);',
  5961. ' e:=pred(green);',
  5962. ' e:=pred(e);',
  5963. ' b:=pred(b);',
  5964. ' e:=succ(red);',
  5965. ' e:=succ(e);',
  5966. ' b:=succ(b);',
  5967. ' e:=tmyenum(1);',
  5968. ' e:=tmyenum(i);',
  5969. ' s:=str(e);',
  5970. ' str(e,s);',
  5971. ' str(red,s);',
  5972. ' s:=str(e:3);',
  5973. ' writestr(s,e:3,red);',
  5974. ' val(s,e,i);',
  5975. ' i:=longint(e);']);
  5976. ConvertProgram;
  5977. CheckSource('TestEnum_Functions',
  5978. LinesToStr([ // statements
  5979. 'this.TMyEnum = {',
  5980. ' "0":"Red",',
  5981. ' Red:0,',
  5982. ' "1":"Green",',
  5983. ' Green:1',
  5984. ' };',
  5985. 'this.DoIt = function (e, i) {',
  5986. ' var v = 0;',
  5987. ' var s = "";',
  5988. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5989. ' v = w;',
  5990. ' }));',
  5991. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5992. '};',
  5993. 'this.e = 0;',
  5994. 'this.i = 0;',
  5995. 'this.s = "";',
  5996. 'this.b = false;',
  5997. '']),
  5998. LinesToStr([
  5999. '$mod.i=$mod.TMyEnum.Red;',
  6000. '$mod.i=$mod.TMyEnum.Green;',
  6001. '$mod.i=$mod.e;',
  6002. '$mod.i=$mod.b+0;',
  6003. '$mod.e=$mod.TMyEnum.Red;',
  6004. '$mod.e=$mod.TMyEnum.Red;',
  6005. '$mod.b=false;',
  6006. '$mod.e=$mod.TMyEnum.Green;',
  6007. '$mod.e=$mod.TMyEnum.Green;',
  6008. '$mod.b=true;',
  6009. '$mod.e=$mod.TMyEnum.Green-1;',
  6010. '$mod.e=$mod.e-1;',
  6011. '$mod.b=false;',
  6012. '$mod.e=$mod.TMyEnum.Red+1;',
  6013. '$mod.e=$mod.e+1;',
  6014. '$mod.b=true;',
  6015. '$mod.e=1;',
  6016. '$mod.e=$mod.i;',
  6017. '$mod.s = $mod.TMyEnum[$mod.e];',
  6018. '$mod.s = $mod.TMyEnum[$mod.e];',
  6019. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6020. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6021. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6022. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6023. ' $mod.i = v;',
  6024. '});',
  6025. '$mod.i=$mod.e;',
  6026. '']));
  6027. end;
  6028. procedure TTestModule.TestEnumRg_Functions;
  6029. begin
  6030. StartProgram(false);
  6031. Add([
  6032. 'type',
  6033. ' TEnum = (Red, Green, Blue);',
  6034. ' TEnumRg = Green..Blue;',
  6035. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6036. 'var',
  6037. ' v: longint;',
  6038. ' s: string;',
  6039. 'begin',
  6040. ' val(s,e,v);',
  6041. ' val(s,e,i);',
  6042. 'end;',
  6043. 'var',
  6044. ' e: TEnumRg;',
  6045. ' i: longint;',
  6046. ' s: string;',
  6047. 'begin',
  6048. ' i:=ord(green);',
  6049. ' i:=ord(e);',
  6050. ' e:=low(tenumrg);',
  6051. ' e:=low(e);',
  6052. ' e:=high(tenumrg);',
  6053. ' e:=high(e);',
  6054. ' e:=pred(blue);',
  6055. ' e:=pred(e);',
  6056. ' e:=succ(green);',
  6057. ' e:=succ(e);',
  6058. ' e:=tenumrg(1);',
  6059. ' e:=tenumrg(i);',
  6060. ' s:=str(e);',
  6061. ' str(e,s);',
  6062. ' str(red,s);',
  6063. ' s:=str(e:3);',
  6064. ' writestr(s,e:3,blue);',
  6065. ' val(s,e,i);',
  6066. ' i:=longint(e);']);
  6067. ConvertProgram;
  6068. CheckSource('TestEnumRg_Functions',
  6069. LinesToStr([ // statements
  6070. 'this.TEnum = {',
  6071. ' "0":"Red",',
  6072. ' Red:0,',
  6073. ' "1":"Green",',
  6074. ' Green:1,',
  6075. ' "2":"Blue",',
  6076. ' Blue:2',
  6077. ' };',
  6078. 'this.DoIt = function (e, i) {',
  6079. ' var v = 0;',
  6080. ' var s = "";',
  6081. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6082. ' v = w;',
  6083. ' }));',
  6084. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6085. '};',
  6086. 'this.e = this.TEnum.Green;',
  6087. 'this.i = 0;',
  6088. 'this.s = "";',
  6089. '']),
  6090. LinesToStr([
  6091. '$mod.i=$mod.TEnum.Green;',
  6092. '$mod.i=$mod.e;',
  6093. '$mod.e=$mod.TEnum.Green;',
  6094. '$mod.e=$mod.TEnum.Green;',
  6095. '$mod.e=$mod.TEnum.Blue;',
  6096. '$mod.e=$mod.TEnum.Blue;',
  6097. '$mod.e=$mod.TEnum.Blue-1;',
  6098. '$mod.e=$mod.e-1;',
  6099. '$mod.e=$mod.TEnum.Green+1;',
  6100. '$mod.e=$mod.e+1;',
  6101. '$mod.e=1;',
  6102. '$mod.e=$mod.i;',
  6103. '$mod.s = $mod.TEnum[$mod.e];',
  6104. '$mod.s = $mod.TEnum[$mod.e];',
  6105. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6106. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6107. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6108. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6109. ' $mod.i = v;',
  6110. '});',
  6111. '$mod.i=$mod.e;',
  6112. '']));
  6113. end;
  6114. procedure TTestModule.TestEnum_AsParams;
  6115. begin
  6116. StartProgram(false);
  6117. Add('type TEnum = (Red,Blue);');
  6118. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6119. Add('var vJ: TEnum;');
  6120. Add('begin');
  6121. Add(' vg:=vg;');
  6122. Add(' vj:=vh;');
  6123. Add(' vi:=vi;');
  6124. Add(' doit(vg,vg,vg);');
  6125. Add(' doit(vh,vh,vj);');
  6126. Add(' doit(vi,vi,vi);');
  6127. Add(' doit(vj,vj,vj);');
  6128. Add('end;');
  6129. Add('var i: TEnum;');
  6130. Add('begin');
  6131. Add(' doit(i,i,i);');
  6132. ConvertProgram;
  6133. CheckSource('TestEnum_AsParams',
  6134. LinesToStr([ // statements
  6135. 'this.TEnum = {',
  6136. ' "0": "Red",',
  6137. ' Red: 0,',
  6138. ' "1": "Blue",',
  6139. ' Blue: 1',
  6140. '};',
  6141. 'this.DoIt = function (vG,vH,vI) {',
  6142. ' var vJ = 0;',
  6143. ' vG = vG;',
  6144. ' vJ = vH;',
  6145. ' vI.set(vI.get());',
  6146. ' $mod.DoIt(vG, vG, {',
  6147. ' get: function () {',
  6148. ' return vG;',
  6149. ' },',
  6150. ' set: function (v) {',
  6151. ' vG = v;',
  6152. ' }',
  6153. ' });',
  6154. ' $mod.DoIt(vH, vH, {',
  6155. ' get: function () {',
  6156. ' return vJ;',
  6157. ' },',
  6158. ' set: function (v) {',
  6159. ' vJ = v;',
  6160. ' }',
  6161. ' });',
  6162. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6163. ' $mod.DoIt(vJ, vJ, {',
  6164. ' get: function () {',
  6165. ' return vJ;',
  6166. ' },',
  6167. ' set: function (v) {',
  6168. ' vJ = v;',
  6169. ' }',
  6170. ' });',
  6171. '};',
  6172. 'this.i = 0;'
  6173. ]),
  6174. LinesToStr([
  6175. '$mod.DoIt($mod.i,$mod.i,{',
  6176. ' p: $mod,',
  6177. ' get: function () {',
  6178. ' return this.p.i;',
  6179. ' },',
  6180. ' set: function (v) {',
  6181. ' this.p.i = v;',
  6182. ' }',
  6183. '});'
  6184. ]));
  6185. end;
  6186. procedure TTestModule.TestEnumRange_Array;
  6187. begin
  6188. StartProgram(false);
  6189. Add([
  6190. 'type',
  6191. ' TEnum = (Red, Green, Blue);',
  6192. ' TEnumRg = green..blue;',
  6193. ' TArr = array[TEnumRg] of byte;',
  6194. ' TArr2 = array[green..blue] of byte;',
  6195. 'var',
  6196. ' a: TArr;',
  6197. ' b: TArr = (3,4);',
  6198. ' c: TArr2 = (5,6);',
  6199. 'begin',
  6200. ' a[green] := b[blue];',
  6201. ' c[green] := c[blue];',
  6202. '']);
  6203. ConvertProgram;
  6204. CheckSource('TestEnumRange_Array',
  6205. LinesToStr([ // statements
  6206. 'this.TEnum = {',
  6207. ' "0": "Red",',
  6208. ' Red: 0,',
  6209. ' "1": "Green",',
  6210. ' Green: 1,',
  6211. ' "2": "Blue",',
  6212. ' Blue: 2',
  6213. '};',
  6214. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6215. 'this.b = [3, 4];',
  6216. 'this.c = [5, 6];',
  6217. '']),
  6218. LinesToStr([
  6219. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6220. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6221. '']));
  6222. end;
  6223. procedure TTestModule.TestEnum_ForIn;
  6224. begin
  6225. StartProgram(false);
  6226. Add([
  6227. 'type',
  6228. ' TEnum = (Red, Green, Blue);',
  6229. ' TEnumRg = green..blue;',
  6230. ' TArr = array[TEnum] of byte;',
  6231. ' TArrRg = array[TEnumRg] of byte;',
  6232. 'var',
  6233. ' e: TEnum;',
  6234. ' a1: TArr = (3,4,5);',
  6235. ' a2: TArrRg = (11,12);',
  6236. ' b: byte;',
  6237. 'begin',
  6238. ' for e in TEnum do ;',
  6239. ' for e in TEnumRg do ;',
  6240. ' for e in TArr do ;',
  6241. ' for e in TArrRg do ;',
  6242. ' for b in a1 do ;',
  6243. ' for b in a2 do ;',
  6244. '']);
  6245. ConvertProgram;
  6246. CheckSource('TestEnum_ForIn',
  6247. LinesToStr([ // statements
  6248. 'this.TEnum = {',
  6249. ' "0": "Red",',
  6250. ' Red: 0,',
  6251. ' "1": "Green",',
  6252. ' Green: 1,',
  6253. ' "2": "Blue",',
  6254. ' Blue: 2',
  6255. '};',
  6256. 'this.e = 0;',
  6257. 'this.a1 = [3, 4, 5];',
  6258. 'this.a2 = [11, 12];',
  6259. 'this.b = 0;',
  6260. '']),
  6261. LinesToStr([
  6262. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6263. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6264. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6265. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6266. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6267. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6268. '']));
  6269. end;
  6270. procedure TTestModule.TestEnum_ScopedNumber;
  6271. begin
  6272. Converter.Options:=Converter.Options+[coEnumNumbers];
  6273. StartProgram(false);
  6274. Add([
  6275. 'type',
  6276. ' TEnum = (Red, Green);',
  6277. 'var',
  6278. ' e: TEnum;',
  6279. 'begin',
  6280. ' e:=TEnum.Green;',
  6281. '']);
  6282. ConvertProgram;
  6283. CheckSource('TestEnum_ScopedNumber',
  6284. LinesToStr([ // statements
  6285. 'this.TEnum = {',
  6286. ' "0": "Red",',
  6287. ' Red: 0,',
  6288. ' "1": "Green",',
  6289. ' Green: 1',
  6290. '};',
  6291. 'this.e = 0;',
  6292. '']),
  6293. LinesToStr([
  6294. '$mod.e = 1;']));
  6295. end;
  6296. procedure TTestModule.TestEnum_InFunction;
  6297. begin
  6298. StartProgram(false);
  6299. Add([
  6300. 'const TEnum = 3;',
  6301. 'procedure DoIt;',
  6302. 'type',
  6303. ' TEnum = (Red, Green, Blue);',
  6304. ' procedure Sub;',
  6305. ' type',
  6306. ' TEnumSub = (Left, Right);',
  6307. ' var',
  6308. ' es: TEnumSub;',
  6309. ' begin',
  6310. ' es:=Left;',
  6311. ' end;',
  6312. 'var',
  6313. ' e, e2: TEnum;',
  6314. 'begin',
  6315. ' if e in [red,blue] then e2:=e;',
  6316. 'end;',
  6317. 'begin']);
  6318. ConvertProgram;
  6319. CheckSource('TestEnum_InFunction',
  6320. LinesToStr([ // statements
  6321. 'this.TEnum = 3;',
  6322. 'var TEnum$1 = {',
  6323. ' "0":"Red",',
  6324. ' Red:0,',
  6325. ' "1":"Green",',
  6326. ' Green:1,',
  6327. ' "2":"Blue",',
  6328. ' Blue:2',
  6329. ' };',
  6330. 'var TEnumSub = {',
  6331. ' "0": "Left",',
  6332. ' Left: 0,',
  6333. ' "1": "Right",',
  6334. ' Right: 1',
  6335. '};',
  6336. 'this.DoIt = function () {',
  6337. ' function Sub() {',
  6338. ' var es = 0;',
  6339. ' es = TEnumSub.Left;',
  6340. ' };',
  6341. ' var e = 0;',
  6342. ' var e2 = 0;',
  6343. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6344. '};',
  6345. '']),
  6346. LinesToStr([
  6347. '']));
  6348. end;
  6349. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6350. begin
  6351. StartUnit(true);
  6352. Add([
  6353. 'interface',
  6354. 'var color: (red, green);',
  6355. 'implementation',
  6356. 'initialization',
  6357. ' color:=green;',
  6358. '']);
  6359. ConvertUnit;
  6360. CheckSource('TestEnum_Name_Anonymous_Unit',
  6361. LinesToStr([
  6362. 'this.color$a = {',
  6363. ' "0": "red",',
  6364. ' red: 0,',
  6365. ' "1": "green",',
  6366. ' green: 1',
  6367. '};',
  6368. 'this.color = 0;',
  6369. '']),
  6370. LinesToStr([ // this.$init
  6371. '$mod.color = $mod.color$a.green;',
  6372. '']),
  6373. LinesToStr([ // implementation
  6374. '']) );
  6375. end;
  6376. procedure TTestModule.TestSet_Enum;
  6377. begin
  6378. StartProgram(false);
  6379. Add([
  6380. 'type',
  6381. ' TColor = (Red, Green, Blue);',
  6382. ' TColors = set of TColor;',
  6383. 'var',
  6384. ' c: TColor;',
  6385. ' s: TColors;',
  6386. ' t: TColors = [];',
  6387. ' u: TColors = [Red];',
  6388. 'begin',
  6389. ' s:=[];',
  6390. ' s:=[Green];',
  6391. ' s:=[Green,Blue];',
  6392. ' s:=[Red..Blue];',
  6393. ' s:=[Red,Green..Blue];',
  6394. ' s:=[Red,c];',
  6395. ' s:=t;',
  6396. ' s:=default(TColors);',
  6397. '']);
  6398. ConvertProgram;
  6399. CheckSource('TestSet',
  6400. LinesToStr([ // statements
  6401. 'this.TColor = {',
  6402. ' "0":"Red",',
  6403. ' Red:0,',
  6404. ' "1":"Green",',
  6405. ' Green:1,',
  6406. ' "2":"Blue",',
  6407. ' Blue:2',
  6408. ' };',
  6409. 'this.c = 0;',
  6410. 'this.s = {};',
  6411. 'this.t = {};',
  6412. 'this.u = rtl.createSet(this.TColor.Red);'
  6413. ]),
  6414. LinesToStr([
  6415. '$mod.s={};',
  6416. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6417. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6418. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6419. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6420. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6421. '$mod.s=rtl.refSet($mod.t);',
  6422. '$mod.s={};',
  6423. '']));
  6424. end;
  6425. procedure TTestModule.TestSet_Operators;
  6426. begin
  6427. StartProgram(false);
  6428. Add('type');
  6429. Add(' TColor = (Red, Green, Blue);');
  6430. Add(' TColors = set of tcolor;');
  6431. Add('var');
  6432. Add(' vC: TColor;');
  6433. Add(' vS: TColors;');
  6434. Add(' vT: TColors;');
  6435. Add(' vU: TColors;');
  6436. Add(' B: boolean;');
  6437. Add('begin');
  6438. Add(' include(vs,green);');
  6439. Add(' exclude(vs,vc);');
  6440. Add(' vs:=vt+vu;');
  6441. Add(' vs:=vt+[red];');
  6442. Add(' vs:=[red]+vt;');
  6443. Add(' vs:=[red]+[green];');
  6444. Add(' vs:=vt-vu;');
  6445. Add(' vs:=vt-[red];');
  6446. Add(' vs:=[red]-vt;');
  6447. Add(' vs:=[red]-[green];');
  6448. Add(' vs:=vt*vu;');
  6449. Add(' vs:=vt*[red];');
  6450. Add(' vs:=[red]*vt;');
  6451. Add(' vs:=[red]*[green];');
  6452. Add(' vs:=vt><vu;');
  6453. Add(' vs:=vt><[red];');
  6454. Add(' vs:=[red]><vt;');
  6455. Add(' vs:=[red]><[green];');
  6456. Add(' b:=vt=vu;');
  6457. Add(' b:=vt=[red];');
  6458. Add(' b:=[red]=vt;');
  6459. Add(' b:=[red]=[green];');
  6460. Add(' b:=vt<>vu;');
  6461. Add(' b:=vt<>[red];');
  6462. Add(' b:=[red]<>vt;');
  6463. Add(' b:=[red]<>[green];');
  6464. Add(' b:=vt<=vu;');
  6465. Add(' b:=vt<=[red];');
  6466. Add(' b:=[red]<=vt;');
  6467. Add(' b:=[red]<=[green];');
  6468. Add(' b:=vt>=vu;');
  6469. Add(' b:=vt>=[red];');
  6470. Add(' b:=[red]>=vt;');
  6471. Add(' b:=[red]>=[green];');
  6472. ConvertProgram;
  6473. CheckSource('TestSet_Operators',
  6474. LinesToStr([ // statements
  6475. 'this.TColor = {',
  6476. ' "0":"Red",',
  6477. ' Red:0,',
  6478. ' "1":"Green",',
  6479. ' Green:1,',
  6480. ' "2":"Blue",',
  6481. ' Blue:2',
  6482. ' };',
  6483. 'this.vC = 0;',
  6484. 'this.vS = {};',
  6485. 'this.vT = {};',
  6486. 'this.vU = {};',
  6487. 'this.B = false;'
  6488. ]),
  6489. LinesToStr([
  6490. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6491. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6492. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6493. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6494. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6495. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6496. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6497. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6498. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6499. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6500. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6501. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6502. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6503. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6504. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6505. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6506. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6507. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6508. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6509. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6510. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6511. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6512. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6513. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6514. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6515. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6516. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6517. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6518. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6519. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6520. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6521. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6522. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6523. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6524. '']));
  6525. end;
  6526. procedure TTestModule.TestSet_Operator_In;
  6527. begin
  6528. StartProgram(false);
  6529. Add([
  6530. 'type',
  6531. ' TColor = (Red, Green, Blue);',
  6532. ' TColors = set of tcolor;',
  6533. ' TColorRg = green..blue;',
  6534. 'var',
  6535. ' vC: tcolor;',
  6536. ' vT: tcolors;',
  6537. ' B: boolean;',
  6538. ' rg: TColorRg;',
  6539. 'begin',
  6540. ' b:=red in vt;',
  6541. ' b:=vc in vt;',
  6542. ' b:=green in [red..blue];',
  6543. ' b:=vc in [red..blue];',
  6544. ' ',
  6545. ' if red in vt then ;',
  6546. ' while vC in vt do ;',
  6547. ' repeat',
  6548. ' until vC in vt;',
  6549. ' if rg in [green..blue] then ;',
  6550. '']);
  6551. ConvertProgram;
  6552. CheckSource('TestSet_Operator_In',
  6553. LinesToStr([ // statements
  6554. 'this.TColor = {',
  6555. ' "0":"Red",',
  6556. ' Red:0,',
  6557. ' "1":"Green",',
  6558. ' Green:1,',
  6559. ' "2":"Blue",',
  6560. ' Blue:2',
  6561. ' };',
  6562. 'this.vC = 0;',
  6563. 'this.vT = {};',
  6564. 'this.B = false;',
  6565. 'this.rg = this.TColor.Green;',
  6566. '']),
  6567. LinesToStr([
  6568. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6569. '$mod.B = $mod.vC in $mod.vT;',
  6570. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6571. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6572. 'if ($mod.TColor.Red in $mod.vT) ;',
  6573. 'while ($mod.vC in $mod.vT) {',
  6574. '};',
  6575. 'do {',
  6576. '} while (!($mod.vC in $mod.vT));',
  6577. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6578. '']));
  6579. end;
  6580. procedure TTestModule.TestSet_Functions;
  6581. begin
  6582. StartProgram(false);
  6583. Add('type');
  6584. Add(' TMyEnum = (Red, Green);');
  6585. Add(' TMyEnums = set of TMyEnum;');
  6586. Add('var');
  6587. Add(' e: TMyEnum;');
  6588. Add(' s: TMyEnums;');
  6589. Add('begin');
  6590. Add(' e:=Low(TMyEnums);');
  6591. Add(' e:=Low(s);');
  6592. Add(' e:=High(TMyEnums);');
  6593. Add(' e:=High(s);');
  6594. ConvertProgram;
  6595. CheckSource('TestSetFunctions',
  6596. LinesToStr([ // statements
  6597. 'this.TMyEnum = {',
  6598. ' "0":"Red",',
  6599. ' Red:0,',
  6600. ' "1":"Green",',
  6601. ' Green:1',
  6602. ' };',
  6603. 'this.e = 0;',
  6604. 'this.s = {};'
  6605. ]),
  6606. LinesToStr([
  6607. '$mod.e=$mod.TMyEnum.Red;',
  6608. '$mod.e=$mod.TMyEnum.Red;',
  6609. '$mod.e=$mod.TMyEnum.Green;',
  6610. '$mod.e=$mod.TMyEnum.Green;',
  6611. '']));
  6612. end;
  6613. procedure TTestModule.TestSet_PassAsArgClone;
  6614. begin
  6615. StartProgram(false);
  6616. Add('type');
  6617. Add(' TMyEnum = (Red, Green);');
  6618. Add(' TMyEnums = set of TMyEnum;');
  6619. Add('procedure DoDefault(s: tmyenums); begin end;');
  6620. Add('procedure DoConst(const s: tmyenums); begin end;');
  6621. Add('var');
  6622. Add(' aSet: tmyenums;');
  6623. Add('begin');
  6624. Add(' dodefault(aset);');
  6625. Add(' doconst(aset);');
  6626. ConvertProgram;
  6627. CheckSource('TestSetFunctions',
  6628. LinesToStr([ // statements
  6629. 'this.TMyEnum = {',
  6630. ' "0":"Red",',
  6631. ' Red:0,',
  6632. ' "1":"Green",',
  6633. ' Green:1',
  6634. ' };',
  6635. 'this.DoDefault = function (s) {',
  6636. '};',
  6637. 'this.DoConst = function (s) {',
  6638. '};',
  6639. 'this.aSet = {};'
  6640. ]),
  6641. LinesToStr([
  6642. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6643. '$mod.DoConst($mod.aSet);',
  6644. '']));
  6645. end;
  6646. procedure TTestModule.TestSet_AsParams;
  6647. begin
  6648. StartProgram(false);
  6649. Add([
  6650. 'type TEnum = (Red,Blue);',
  6651. 'type TEnums = set of TEnum;',
  6652. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6653. 'var vJ: TEnums;',
  6654. 'begin',
  6655. ' Include(vg,red);',
  6656. ' Include(result,blue);',
  6657. ' vg:=vg;',
  6658. ' vj:=vh;',
  6659. ' vi:=vi;',
  6660. ' doit(vg,vg,vg);',
  6661. ' doit(vh,vh,vj);',
  6662. ' doit(vi,vi,vi);',
  6663. ' doit(vj,vj,vj);',
  6664. 'end;',
  6665. 'var i: TEnums;',
  6666. 'begin',
  6667. ' doit(i,i,i);']);
  6668. ConvertProgram;
  6669. CheckSource('TestSet_AsParams',
  6670. LinesToStr([ // statements
  6671. 'this.TEnum = {',
  6672. ' "0": "Red",',
  6673. ' Red: 0,',
  6674. ' "1": "Blue",',
  6675. ' Blue: 1',
  6676. '};',
  6677. 'this.DoIt = function (vG,vH,vI) {',
  6678. ' var Result = {};',
  6679. ' var vJ = {};',
  6680. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6681. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6682. ' vG = rtl.refSet(vG);',
  6683. ' vJ = rtl.refSet(vH);',
  6684. ' vI.set(rtl.refSet(vI.get()));',
  6685. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6686. ' get: function () {',
  6687. ' return vG;',
  6688. ' },',
  6689. ' set: function (v) {',
  6690. ' vG = v;',
  6691. ' }',
  6692. ' });',
  6693. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6694. ' get: function () {',
  6695. ' return vJ;',
  6696. ' },',
  6697. ' set: function (v) {',
  6698. ' vJ = v;',
  6699. ' }',
  6700. ' });',
  6701. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6702. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6703. ' get: function () {',
  6704. ' return vJ;',
  6705. ' },',
  6706. ' set: function (v) {',
  6707. ' vJ = v;',
  6708. ' }',
  6709. ' });',
  6710. ' return Result;',
  6711. '};',
  6712. 'this.i = {};'
  6713. ]),
  6714. LinesToStr([
  6715. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6716. ' p: $mod,',
  6717. ' get: function () {',
  6718. ' return this.p.i;',
  6719. ' },',
  6720. ' set: function (v) {',
  6721. ' this.p.i = v;',
  6722. ' }',
  6723. '});'
  6724. ]));
  6725. end;
  6726. procedure TTestModule.TestSet_Property;
  6727. begin
  6728. StartProgram(false);
  6729. Add('type');
  6730. Add(' TEnum = (Red,Blue);');
  6731. Add(' TEnums = set of TEnum;');
  6732. Add(' TObject = class');
  6733. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6734. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6735. Add(' property Colors: TEnums read GetColors write SetColors;');
  6736. Add(' end;');
  6737. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6738. Add('begin end;');
  6739. Add('var Obj: TObject;');
  6740. Add('begin');
  6741. Add(' Include(Obj.Colors,Red);');
  6742. Add(' Exclude(Obj.Colors,Red);');
  6743. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6744. ConvertProgram;
  6745. CheckSource('TestSet_Property',
  6746. LinesToStr([ // statements
  6747. 'this.TEnum = {',
  6748. ' "0": "Red",',
  6749. ' Red: 0,',
  6750. ' "1": "Blue",',
  6751. ' Blue: 1',
  6752. '};',
  6753. 'rtl.createClass(this, "TObject", null, function () {',
  6754. ' this.$init = function () {',
  6755. ' };',
  6756. ' this.$final = function () {',
  6757. ' };',
  6758. '});',
  6759. 'this.DoIt = function (i, j, k, l) {',
  6760. '};',
  6761. 'this.Obj = null;',
  6762. '']),
  6763. LinesToStr([
  6764. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6765. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6766. '']));
  6767. end;
  6768. procedure TTestModule.TestSet_EnumConst;
  6769. begin
  6770. StartProgram(false);
  6771. Add([
  6772. 'type',
  6773. ' TEnum = (Red,Blue);',
  6774. ' TEnums = set of TEnum;',
  6775. 'const',
  6776. ' Orange = red;',
  6777. 'var',
  6778. ' Enum: tenum;',
  6779. ' Enums: tenums;',
  6780. 'begin',
  6781. ' Include(enums,orange);',
  6782. ' Exclude(enums,orange);',
  6783. ' if orange in enums then;',
  6784. ' if orange in [orange,red] then;']);
  6785. ConvertProgram;
  6786. CheckSource('TestSet_EnumConst',
  6787. LinesToStr([ // statements
  6788. 'this.TEnum = {',
  6789. ' "0": "Red",',
  6790. ' Red: 0,',
  6791. ' "1": "Blue",',
  6792. ' Blue: 1',
  6793. '};',
  6794. 'this.Orange = this.TEnum.Red;',
  6795. 'this.Enum = 0;',
  6796. 'this.Enums = {};',
  6797. '']),
  6798. LinesToStr([
  6799. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6800. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6801. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6802. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6803. '']));
  6804. end;
  6805. procedure TTestModule.TestSet_IntConst;
  6806. begin
  6807. StartProgram(false);
  6808. Add([
  6809. 'type',
  6810. ' TEnums = set of Byte;',
  6811. 'const',
  6812. ' Orange = 0;',
  6813. 'var',
  6814. ' Enum: byte;',
  6815. ' Enums: tenums;',
  6816. 'begin',
  6817. ' Enums:=[];',
  6818. ' Enums:=[0];',
  6819. ' Enums:=[1..2];',
  6820. //' Include(enums,orange);',
  6821. //' Exclude(enums,orange);',
  6822. ' if orange in enums then;',
  6823. ' if orange in [orange,1] then;']);
  6824. ConvertProgram;
  6825. CheckSource('TestSet_IntConst',
  6826. LinesToStr([ // statements
  6827. 'this.Orange = 0;',
  6828. 'this.Enum = 0;',
  6829. 'this.Enums = {};',
  6830. '']),
  6831. LinesToStr([
  6832. '$mod.Enums = {};',
  6833. '$mod.Enums = rtl.createSet(0);',
  6834. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6835. 'if (0 in $mod.Enums) ;',
  6836. 'if (0 in rtl.createSet(0, 1)) ;',
  6837. '']));
  6838. end;
  6839. procedure TTestModule.TestSet_IntRange;
  6840. begin
  6841. StartProgram(false);
  6842. Add([
  6843. 'type',
  6844. ' TRange = 1..3;',
  6845. ' TEnums = set of TRange;',
  6846. 'const',
  6847. ' Orange = 2;',
  6848. 'var',
  6849. ' Enum: byte;',
  6850. ' Enums: TEnums;',
  6851. 'begin',
  6852. ' Enums:=[];',
  6853. ' Enums:=[1];',
  6854. ' Enums:=[2..3];',
  6855. ' Include(enums,orange);',
  6856. ' Exclude(enums,orange);',
  6857. ' if orange in enums then;',
  6858. ' if orange in [orange,1] then;']);
  6859. ConvertProgram;
  6860. CheckSource('TestSet_IntRange',
  6861. LinesToStr([ // statements
  6862. 'this.Orange = 2;',
  6863. 'this.Enum = 0;',
  6864. 'this.Enums = {};',
  6865. '']),
  6866. LinesToStr([
  6867. '$mod.Enums = {};',
  6868. '$mod.Enums = rtl.createSet(1);',
  6869. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6870. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6871. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6872. 'if (2 in $mod.Enums) ;',
  6873. 'if (2 in rtl.createSet(2, 1)) ;',
  6874. '']));
  6875. end;
  6876. procedure TTestModule.TestSet_AnonymousEnumType;
  6877. begin
  6878. StartProgram(false);
  6879. Add('type');
  6880. Add(' TFlags = set of (red, green);');
  6881. Add('const');
  6882. Add(' favorite = red;');
  6883. Add('var');
  6884. Add(' f: TFlags;');
  6885. Add(' i: longint;');
  6886. Add('begin');
  6887. Add(' Include(f,red);');
  6888. Add(' Include(f,favorite);');
  6889. Add(' i:=ord(red);');
  6890. Add(' i:=ord(favorite);');
  6891. Add(' i:=ord(low(TFlags));');
  6892. Add(' i:=ord(low(f));');
  6893. Add(' i:=ord(low(favorite));');
  6894. Add(' i:=ord(high(TFlags));');
  6895. Add(' i:=ord(high(f));');
  6896. Add(' i:=ord(high(favorite));');
  6897. Add(' f:=[green,favorite];');
  6898. ConvertProgram;
  6899. CheckSource('TestSet_AnonymousEnumType',
  6900. LinesToStr([ // statements
  6901. 'this.TFlags$a = {',
  6902. ' "0": "red",',
  6903. ' red: 0,',
  6904. ' "1": "green",',
  6905. ' green: 1',
  6906. '};',
  6907. 'this.favorite = this.TFlags$a.red;',
  6908. 'this.f = {};',
  6909. 'this.i = 0;',
  6910. '']),
  6911. LinesToStr([
  6912. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6913. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6914. '$mod.i = $mod.TFlags$a.red;',
  6915. '$mod.i = $mod.TFlags$a.red;',
  6916. '$mod.i = $mod.TFlags$a.red;',
  6917. '$mod.i = $mod.TFlags$a.red;',
  6918. '$mod.i = $mod.TFlags$a.red;',
  6919. '$mod.i = $mod.TFlags$a.green;',
  6920. '$mod.i = $mod.TFlags$a.green;',
  6921. '$mod.i = $mod.TFlags$a.green;',
  6922. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6923. '']));
  6924. end;
  6925. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6926. begin
  6927. exit;
  6928. StartProgram(false);
  6929. Add([
  6930. 'type',
  6931. ' TAtoZ = ''A''..''Z'';',
  6932. ' TSetOfAZ = set of TAtoZ;',
  6933. 'var',
  6934. ' c: char;',
  6935. ' a: TAtoZ;',
  6936. ' s: TSetOfAZ = [''P'',''A''];',
  6937. ' i: longint;',
  6938. 'begin',
  6939. ' Include(s,''S'');',
  6940. ' Include(s,c);',
  6941. ' Include(s,a);',
  6942. ' c:=low(TAtoZ);',
  6943. ' i:=ord(low(TAtoZ));',
  6944. ' a:=high(TAtoZ);',
  6945. ' a:=high(TSetOfAtoZ);',
  6946. ' s:=[a,c,''M''];',
  6947. '']);
  6948. ConvertProgram;
  6949. CheckSource('TestSet_AnonymousEnumTypeChar',
  6950. LinesToStr([ // statements
  6951. '']),
  6952. LinesToStr([
  6953. '']));
  6954. end;
  6955. procedure TTestModule.TestSet_ConstEnum;
  6956. begin
  6957. StartProgram(false);
  6958. Add([
  6959. 'type',
  6960. ' TEnum = (red,blue,green);',
  6961. ' TEnums = set of TEnum;',
  6962. 'const',
  6963. ' teAny = [low(TEnum)..high(TEnum)];',
  6964. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6965. 'var',
  6966. ' e: TEnum;',
  6967. ' s: TEnums;',
  6968. 'begin',
  6969. ' if blue in teAny then;',
  6970. ' if blue in teAny+[e] then;',
  6971. ' if blue in teAny+teRedBlue then;',
  6972. ' if e in [red,blue] then;',
  6973. ' s:=teAny;',
  6974. ' s:=teAny+[e];',
  6975. ' s:=[e]+teAny;',
  6976. ' s:=teAny+teRedBlue;',
  6977. ' s:=teAny+teRedBlue+[e];',
  6978. '']);
  6979. ConvertProgram;
  6980. CheckSource('TestSet_ConstEnum',
  6981. LinesToStr([ // statements
  6982. 'this.TEnum = {',
  6983. ' "0": "red",',
  6984. ' red: 0,',
  6985. ' "1": "blue",',
  6986. ' blue: 1,',
  6987. ' "2": "green",',
  6988. ' green: 2',
  6989. '};',
  6990. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6991. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6992. 'this.e = 0;',
  6993. 'this.s = {};',
  6994. '']),
  6995. LinesToStr([
  6996. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6997. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6998. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6999. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7000. '$mod.s = rtl.refSet($mod.teAny);',
  7001. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7002. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7003. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7004. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7005. '']));
  7006. end;
  7007. procedure TTestModule.TestSet_ConstChar;
  7008. begin
  7009. StartProgram(false);
  7010. Add([
  7011. 'const',
  7012. ' LowChars = [''a''..''z''];',
  7013. ' Chars = LowChars+[''A''..''Z''];',
  7014. ' sc = [''А'', ''Я''];',
  7015. 'var',
  7016. ' c: char;',
  7017. ' s: string;',
  7018. 'begin',
  7019. ' if c in lowchars then ;',
  7020. ' if ''a'' in lowchars then ;',
  7021. ' if s[1] in lowchars then ;',
  7022. ' if c in chars then ;',
  7023. ' if c in [''a''..''z'',''_''] then ;',
  7024. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7025. ' if ''Я'' in sc then ;',
  7026. ' if 3=ord('' '') then ;',
  7027. '']);
  7028. ConvertProgram;
  7029. CheckSource('TestSet_ConstChar',
  7030. LinesToStr([ // statements
  7031. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7032. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7033. 'this.sc = rtl.createSet(1040, 1071);',
  7034. 'this.c = "";',
  7035. 'this.s = "";',
  7036. '']),
  7037. LinesToStr([
  7038. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7039. 'if (97 in $mod.LowChars) ;',
  7040. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7041. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7042. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7043. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7044. 'if (1071 in $mod.sc) ;',
  7045. 'if (3 === 32) ;',
  7046. '']));
  7047. end;
  7048. procedure TTestModule.TestSet_ConstInt;
  7049. begin
  7050. StartProgram(false);
  7051. Add([
  7052. 'const',
  7053. ' Months = [1..12];',
  7054. ' Mirror = [-12..-1]+Months;',
  7055. 'var',
  7056. ' i: smallint;',
  7057. 'begin',
  7058. ' if 3 in Months then;',
  7059. ' if i in Months+[i] then;',
  7060. ' if i in Months+Mirror then;',
  7061. ' if i in [4..6,8] then;',
  7062. '']);
  7063. ConvertProgram;
  7064. CheckSource('TestSet_ConstInt',
  7065. LinesToStr([ // statements
  7066. 'this.Months = rtl.createSet(null, 1, 12);',
  7067. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7068. 'this.i = 0;',
  7069. '']),
  7070. LinesToStr([
  7071. 'if (3 in $mod.Months) ;',
  7072. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7073. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7074. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7075. '']));
  7076. end;
  7077. procedure TTestModule.TestSet_InFunction;
  7078. begin
  7079. StartProgram(false);
  7080. Add([
  7081. 'const',
  7082. ' TEnum = 3;',
  7083. ' TSetOfEnum = 4;',
  7084. ' TSetOfAno = 5;',
  7085. 'procedure DoIt;',
  7086. 'type',
  7087. ' TEnum = (red, blue);',
  7088. ' TSetOfEnum = set of TEnum;',
  7089. ' TSetOfAno = set of (up,down);',
  7090. 'var',
  7091. ' e: TEnum;',
  7092. ' se: TSetOfEnum;',
  7093. ' sa: TSetOfAno;',
  7094. 'begin',
  7095. ' se:=[e];',
  7096. ' sa:=[up];',
  7097. 'end;',
  7098. 'begin',
  7099. '']);
  7100. ConvertProgram;
  7101. CheckSource('TestSet_InFunction',
  7102. LinesToStr([ // statements
  7103. 'this.TEnum = 3;',
  7104. 'this.TSetOfEnum = 4;',
  7105. 'this.TSetOfAno = 5;',
  7106. 'var TEnum$1 = {',
  7107. ' "0": "red",',
  7108. ' red: 0,',
  7109. ' "1": "blue",',
  7110. ' blue: 1',
  7111. '};',
  7112. 'var TSetOfAno$a = {',
  7113. ' "0": "up",',
  7114. ' up: 0,',
  7115. ' "1": "down",',
  7116. ' down: 1',
  7117. '};',
  7118. 'this.DoIt = function () {',
  7119. ' var e = 0;',
  7120. ' var se = {};',
  7121. ' var sa = {};',
  7122. ' se = rtl.createSet(e);',
  7123. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7124. '};',
  7125. '']),
  7126. LinesToStr([
  7127. '']));
  7128. end;
  7129. procedure TTestModule.TestSet_ForIn;
  7130. begin
  7131. StartProgram(false);
  7132. Add([
  7133. 'type',
  7134. ' TEnum = (Red, Green, Blue);',
  7135. ' TEnumRg = green..blue;',
  7136. ' TSetOfEnum = set of TEnum;',
  7137. ' TSetOfEnumRg = set of TEnumRg;',
  7138. 'var',
  7139. ' e, e2: TEnum;',
  7140. ' er: TEnum;',
  7141. ' s: TSetOfEnum;',
  7142. 'begin',
  7143. ' for e in TSetOfEnum do ;',
  7144. ' for e in TSetOfEnumRg do ;',
  7145. ' for e in [] do e2:=e;',
  7146. ' for e in [red..green] do e2:=e;',
  7147. ' for e in [green,blue] do e2:=e;',
  7148. ' for e in [red,blue] do e2:=e;',
  7149. ' for e in s do e2:=e;',
  7150. ' for er in TSetOfEnumRg do ;',
  7151. '']);
  7152. ConvertProgram;
  7153. CheckSource('TestSet_ForIn',
  7154. LinesToStr([ // statements
  7155. 'this.TEnum = {',
  7156. ' "0":"Red",',
  7157. ' Red:0,',
  7158. ' "1":"Green",',
  7159. ' Green:1,',
  7160. ' "2":"Blue",',
  7161. ' Blue:2',
  7162. ' };',
  7163. 'this.e = 0;',
  7164. 'this.e2 = 0;',
  7165. 'this.er = 0;',
  7166. 'this.s = {};',
  7167. '']),
  7168. LinesToStr([
  7169. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7170. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7171. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7172. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7173. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7174. 'for (var $l in $mod.s){',
  7175. ' $mod.e = +$l;',
  7176. ' $mod.e2 = $mod.e;',
  7177. '};',
  7178. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7179. '']));
  7180. end;
  7181. procedure TTestModule.TestNestBegin;
  7182. begin
  7183. StartProgram(false);
  7184. Add('begin');
  7185. Add(' begin');
  7186. Add(' begin');
  7187. Add(' end;');
  7188. Add(' begin');
  7189. Add(' if true then ;');
  7190. Add(' end;');
  7191. Add(' end;');
  7192. ConvertProgram;
  7193. CheckSource('TestNestBegin',
  7194. '',
  7195. 'if (true) ;');
  7196. end;
  7197. procedure TTestModule.TestUnitImplVars;
  7198. begin
  7199. StartUnit(false);
  7200. Add('interface');
  7201. Add('implementation');
  7202. Add('var');
  7203. Add(' V1:longint;');
  7204. Add(' V2:longint = 3;');
  7205. Add(' V3:string = ''abc'';');
  7206. ConvertUnit;
  7207. CheckSource('TestUnitImplVars',
  7208. LinesToStr([ // statements
  7209. 'var $impl = $mod.$impl;',
  7210. '']),
  7211. '', // this.$init
  7212. LinesToStr([ // implementation
  7213. '$impl.V1 = 0;',
  7214. '$impl.V2 = 3;',
  7215. '$impl.V3 = "abc";',
  7216. '']) );
  7217. end;
  7218. procedure TTestModule.TestUnitImplConsts;
  7219. begin
  7220. StartUnit(false);
  7221. Add('interface');
  7222. Add('implementation');
  7223. Add('const');
  7224. Add(' v1 = 3;');
  7225. Add(' v2:longint = 4;');
  7226. Add(' v3:string = ''abc'';');
  7227. ConvertUnit;
  7228. CheckSource('TestUnitImplConsts',
  7229. LinesToStr([ // statements
  7230. 'var $impl = $mod.$impl;',
  7231. '']),
  7232. '', // this.$init
  7233. LinesToStr([ // implementation
  7234. '$impl.v1 = 3;',
  7235. '$impl.v2 = 4;',
  7236. '$impl.v3 = "abc";',
  7237. '']) );
  7238. end;
  7239. procedure TTestModule.TestUnitImplRecord;
  7240. begin
  7241. StartUnit(false);
  7242. Add('interface');
  7243. Add('implementation');
  7244. Add('type');
  7245. Add(' TMyRecord = record');
  7246. Add(' i: longint;');
  7247. Add(' end;');
  7248. Add('var aRec: TMyRecord;');
  7249. Add('initialization');
  7250. Add(' arec.i:=3;');
  7251. ConvertUnit;
  7252. CheckSource('TestUnitImplRecord',
  7253. LinesToStr([ // statements
  7254. 'var $impl = $mod.$impl;',
  7255. '']),
  7256. // this.$init
  7257. '$impl.aRec.i = 3;',
  7258. LinesToStr([ // implementation
  7259. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7260. ' this.i = 0;',
  7261. ' this.$eq = function (b) {',
  7262. ' return this.i === b.i;',
  7263. ' };',
  7264. ' this.$assign = function (s) {',
  7265. ' this.i = s.i;',
  7266. ' return this;',
  7267. ' };',
  7268. '});',
  7269. '$impl.aRec = $impl.TMyRecord.$new();',
  7270. '']) );
  7271. end;
  7272. procedure TTestModule.TestRenameJSNameConflict;
  7273. begin
  7274. StartProgram(false);
  7275. Add('var apply: longint;');
  7276. Add('var bind: longint;');
  7277. Add('var call: longint;');
  7278. Add('begin');
  7279. ConvertProgram;
  7280. CheckSource('TestRenameJSNameConflict',
  7281. LinesToStr([ // statements
  7282. 'this.Apply = 0;',
  7283. 'this.Bind = 0;',
  7284. 'this.Call = 0;'
  7285. ]),
  7286. LinesToStr([ // this.$main
  7287. ''
  7288. ]));
  7289. end;
  7290. procedure TTestModule.TestLocalConst;
  7291. begin
  7292. StartProgram(false);
  7293. Add('procedure DoIt;');
  7294. Add('const');
  7295. Add(' cA: longint = 1;');
  7296. Add(' cB = 2;');
  7297. Add(' procedure Sub;');
  7298. Add(' const');
  7299. Add(' csA = 3;');
  7300. Add(' cB: double = 4;');
  7301. Add(' begin');
  7302. Add(' cb:=cb+csa;');
  7303. Add(' ca:=ca+csa+5;');
  7304. Add(' end;');
  7305. Add('begin');
  7306. Add(' ca:=ca+cb+6;');
  7307. Add('end;');
  7308. Add('begin');
  7309. ConvertProgram;
  7310. CheckSource('TestLocalConst',
  7311. LinesToStr([
  7312. 'var cA = 1;',
  7313. 'var cB = 2;',
  7314. 'var csA = 3;',
  7315. 'var cB$1 = 4;',
  7316. 'this.DoIt = function () {',
  7317. ' function Sub() {',
  7318. ' cB$1 = cB$1 + 3;',
  7319. ' cA = cA + 3 + 5;',
  7320. ' };',
  7321. ' cA = cA + 2 + 6;',
  7322. '};'
  7323. ]),
  7324. LinesToStr([
  7325. ]));
  7326. end;
  7327. procedure TTestModule.TestVarExternal;
  7328. begin
  7329. StartProgram(false);
  7330. Add('var');
  7331. Add(' NaN: double; external name ''Global.NaN'';');
  7332. Add(' d: double;');
  7333. Add('begin');
  7334. Add(' d:=NaN;');
  7335. ConvertProgram;
  7336. CheckSource('TestVarExternal',
  7337. LinesToStr([
  7338. 'this.d = 0.0;'
  7339. ]),
  7340. LinesToStr([
  7341. '$mod.d = Global.NaN;'
  7342. ]));
  7343. end;
  7344. procedure TTestModule.TestVarExternalOtherUnit;
  7345. begin
  7346. AddModuleWithIntfImplSrc('unit2.pas',
  7347. LinesToStr([
  7348. 'var NaN: double; external name ''Global.NaN'';',
  7349. 'var iV: longint;'
  7350. ]),
  7351. '');
  7352. StartUnit(true);
  7353. Add('interface');
  7354. Add('uses unit2;');
  7355. Add('implementation');
  7356. Add('var');
  7357. Add(' d: double;');
  7358. Add(' i: longint; external name ''$i'';');
  7359. Add('begin');
  7360. Add(' d:=nan;');
  7361. Add(' d:=uNit2.nan;');
  7362. Add(' d:=test1.d;');
  7363. Add(' i:=iv;');
  7364. Add(' i:=uNit2.iv;');
  7365. Add(' i:=test1.i;');
  7366. ConvertUnit;
  7367. CheckSource('TestVarExternalOtherUnit',
  7368. LinesToStr([
  7369. 'var $impl = $mod.$impl;',
  7370. '']),
  7371. LinesToStr([ // this.$init
  7372. '$impl.d = Global.NaN;',
  7373. '$impl.d = Global.NaN;',
  7374. '$impl.d = $impl.d;',
  7375. '$i = pas.unit2.iV;',
  7376. '$i = pas.unit2.iV;',
  7377. '$i = $i;',
  7378. '']),
  7379. LinesToStr([ // implementation
  7380. '$impl.d = 0.0;',
  7381. '']) );
  7382. end;
  7383. procedure TTestModule.TestVarAbsoluteFail;
  7384. begin
  7385. StartProgram(false);
  7386. Add([
  7387. 'var',
  7388. ' a: longint;',
  7389. ' b: longword absolute a;',
  7390. 'begin']);
  7391. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7392. ConvertProgram;
  7393. end;
  7394. procedure TTestModule.TestConstExternal;
  7395. begin
  7396. StartProgram(false);
  7397. Add([
  7398. 'const',
  7399. ' PI: double; external name ''Global.PI'';',
  7400. ' Tau = 2*pi;',
  7401. 'var d: double;',
  7402. 'begin',
  7403. ' d:=pi;',
  7404. ' d:=tau+pi;']);
  7405. ConvertProgram;
  7406. CheckSource('TestConstExternal',
  7407. LinesToStr([
  7408. 'this.Tau = 2*Global.PI;',
  7409. 'this.d = 0.0;'
  7410. ]),
  7411. LinesToStr([
  7412. '$mod.d = Global.PI;',
  7413. '$mod.d = $mod.Tau + Global.PI;'
  7414. ]));
  7415. end;
  7416. procedure TTestModule.TestDouble;
  7417. begin
  7418. StartProgram(false);
  7419. Add([
  7420. 'type',
  7421. ' TDateTime = double;',
  7422. 'const',
  7423. ' a = TDateTime(2.7);',
  7424. ' b = a + TDateTime(1.7);',
  7425. ' c = 0.9 + 0.1;',
  7426. ' f0_1 = 0.1;',
  7427. ' f0_3 = 0.3;',
  7428. ' fn0_1 = -0.1;',
  7429. ' fn0_3 = -0.3;',
  7430. ' fn0_003 = -0.003;',
  7431. ' fn0_123456789 = -0.123456789;',
  7432. ' fn300_0 = -300.0;',
  7433. ' fn123456_0 = -123456.0;',
  7434. ' fn1234567_8 = -1234567.8;',
  7435. ' fn12345678_9 = -12345678.9;',
  7436. ' f1_0En12 = 1E-12;',
  7437. ' fn1_0En12 = -1E-12;',
  7438. ' maxdouble = 1.7e+308;',
  7439. ' mindouble = -1.7e+308;',
  7440. ' MinSafeIntDouble = -$1fffffffffffff;',
  7441. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7442. ' MaxSafeIntDouble = $1fffffffffffff;',
  7443. ' DZeroResolution = 1E-12;',
  7444. ' Minus1 = -1E-12;',
  7445. ' EPS = 1E-9;',
  7446. ' DELTA = 0.001;',
  7447. ' Big = 129.789E+100;',
  7448. ' Test0_15 = 0.15;',
  7449. ' Test999 = 2.9999999999999;',
  7450. ' Test111999 = 211199999999999000.0;',
  7451. ' TestMinus111999 = -211199999999999000.0;',
  7452. ' Inf = 1.0 / 0.0;',
  7453. ' NegInf = -1.0 / 0.0;',
  7454. 'procedure Run(d: double); external name ''Run'';',
  7455. 'var',
  7456. ' d: double = b;',
  7457. 'begin',
  7458. ' d:=1.0;',
  7459. ' d:=1.0/3.0;',
  7460. ' d:=1.0/(3-2-1);',
  7461. ' d:=1/3;',
  7462. ' d:=5.0E-324;',
  7463. ' d:=1.7E308;',
  7464. ' d:=001.00E00;',
  7465. ' d:=002.00E001;',
  7466. ' d:=003.000E000;',
  7467. ' d:=-004.00E-00;',
  7468. ' d:=-005.00E-001;',
  7469. ' d:=10**3;',
  7470. ' d:=10 mod 3;',
  7471. ' d:=10 div 3;',
  7472. ' d:=c;',
  7473. ' d:=f0_1;',
  7474. ' d:=f0_3;',
  7475. ' d:=fn0_1;',
  7476. ' d:=fn0_3;',
  7477. ' d:=fn0_003;',
  7478. ' d:=fn0_123456789;',
  7479. ' d:=fn300_0;',
  7480. ' d:=fn123456_0;',
  7481. ' d:=fn1234567_8;',
  7482. ' d:=fn12345678_9;',
  7483. ' d:=f1_0En12;',
  7484. ' d:=fn1_0En12;',
  7485. ' d:=maxdouble;',
  7486. ' d:=mindouble;',
  7487. ' d:=MinSafeIntDouble;',
  7488. ' d:=double(MinSafeIntDouble);',
  7489. ' d:=MinSafeIntDouble2;',
  7490. ' d:=double(MinSafeIntDouble2);',
  7491. ' d:=MaxSafeIntDouble;',
  7492. ' d:=default(double);',
  7493. ' Run(Inf);',
  7494. ' Run(NegInf);',
  7495. '']);
  7496. ConvertProgram;
  7497. CheckSource('TestDouble',
  7498. LinesToStr([
  7499. 'this.a = 2.7;',
  7500. 'this.b = 2.7 + 1.7;',
  7501. 'this.c = 0.9 + 0.1;',
  7502. 'this.f0_1 = 0.1;',
  7503. 'this.f0_3 = 0.3;',
  7504. 'this.fn0_1 = -0.1;',
  7505. 'this.fn0_3 = -0.3;',
  7506. 'this.fn0_003 = -0.003;',
  7507. 'this.fn0_123456789 = -0.123456789;',
  7508. 'this.fn300_0 = -300.0;',
  7509. 'this.fn123456_0 = -123456.0;',
  7510. 'this.fn1234567_8 = -1234567.8;',
  7511. 'this.fn12345678_9 = -12345678.9;',
  7512. 'this.f1_0En12 = 1E-12;',
  7513. 'this.fn1_0En12 = -1E-12;',
  7514. 'this.maxdouble = 1.7e+308;',
  7515. 'this.mindouble = -1.7e+308;',
  7516. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7517. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7518. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7519. 'this.DZeroResolution = 1E-12;',
  7520. 'this.Minus1 = -1E-12;',
  7521. 'this.EPS = 1E-9;',
  7522. 'this.DELTA = 0.001;',
  7523. 'this.Big = 129.789E+100;',
  7524. 'this.Test0_15 = 0.15;',
  7525. 'this.Test999 = 2.9999999999999;',
  7526. 'this.Test111999 = 211199999999999000.0;',
  7527. 'this.TestMinus111999 = -211199999999999000.0;',
  7528. 'this.Inf = 1.0 / 0.0;',
  7529. 'this.NegInf = -1.0 / 0.0;',
  7530. 'this.d = 4.4;',
  7531. '']),
  7532. LinesToStr([
  7533. '$mod.d = 1.0;',
  7534. '$mod.d = 1.0 / 3.0;',
  7535. '$mod.d = 1.0 / (3 - 2 - 1);',
  7536. '$mod.d = 1 / 3;',
  7537. '$mod.d = 5.0E-324;',
  7538. '$mod.d = 1.7E308;',
  7539. '$mod.d = 1.00E0;',
  7540. '$mod.d = 2.00E1;',
  7541. '$mod.d = 3.000E0;',
  7542. '$mod.d = -4.00E-0;',
  7543. '$mod.d = -5.00E-1;',
  7544. '$mod.d = Math.pow(10, 3);',
  7545. '$mod.d = 10 % 3;',
  7546. '$mod.d = rtl.trunc(10 / 3);',
  7547. '$mod.d = 1;',
  7548. '$mod.d = 0.1;',
  7549. '$mod.d = 0.3;',
  7550. '$mod.d = -0.1;',
  7551. '$mod.d = -0.3;',
  7552. '$mod.d = -0.003;',
  7553. '$mod.d = -0.123456789;',
  7554. '$mod.d = -300;',
  7555. '$mod.d = -123456;',
  7556. '$mod.d = -1234567.8;',
  7557. '$mod.d = -1.23456789E7;',
  7558. '$mod.d = 1E-12;',
  7559. '$mod.d = -1E-12;',
  7560. '$mod.d = 1.7E308;',
  7561. '$mod.d = -1.7E308;',
  7562. '$mod.d = -9007199254740991;',
  7563. '$mod.d = -9007199254740991;',
  7564. '$mod.d = -9.007199254740992E15;',
  7565. '$mod.d = -9.007199254740992E15;',
  7566. '$mod.d = 9007199254740991;',
  7567. '$mod.d = 0.0;',
  7568. 'Run(1 / 0);',
  7569. 'Run(-1 / 0);',
  7570. '']));
  7571. end;
  7572. procedure TTestModule.TestInteger;
  7573. begin
  7574. StartProgram(false);
  7575. Add([
  7576. 'const',
  7577. ' MinInt = low(NativeInt);',
  7578. ' MaxInt = high(NativeInt);',
  7579. 'type',
  7580. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7581. 'const',
  7582. ' a = low(TMyInt)+High(TMyInt);',
  7583. 'var',
  7584. ' i: TMyInt;',
  7585. 'begin',
  7586. ' i:=-MinInt;',
  7587. ' i:=default(TMyInt);',
  7588. ' i:=low(i)+high(i);',
  7589. '']);
  7590. ConvertProgram;
  7591. CheckSource('TestIntegerRange',
  7592. LinesToStr([
  7593. 'this.MinInt = -9007199254740991;',
  7594. 'this.MaxInt = 9007199254740991;',
  7595. 'this.a = -9007199254740991 + 9007199254740991;',
  7596. 'this.i = 0;',
  7597. '']),
  7598. LinesToStr([
  7599. '$mod.i = - -9007199254740991;',
  7600. '$mod.i = -9007199254740991;',
  7601. '$mod.i = -9007199254740991 + 9007199254740991;',
  7602. '']));
  7603. end;
  7604. procedure TTestModule.TestIntegerRange;
  7605. begin
  7606. StartProgram(false);
  7607. Add([
  7608. 'const',
  7609. ' MinInt = -1;',
  7610. ' MaxInt = +1;',
  7611. 'type',
  7612. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7613. ' TInt2 = 1..3;',
  7614. 'const',
  7615. ' a = low(TMyInt)+High(TMyInt);',
  7616. ' b = low(TInt2)+High(TInt2);',
  7617. ' s1 = [1];',
  7618. ' s2 = [1,2];',
  7619. ' s3 = [1..3];',
  7620. ' s4 = [low(shortint)..high(shortint)];',
  7621. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7622. ' s6 = 1 in s2;',
  7623. 'var',
  7624. ' i: TMyInt;',
  7625. ' i2: TInt2;',
  7626. 'begin',
  7627. ' i:=i2;',
  7628. ' i:=default(TMyInt);',
  7629. ' if i=i2 then ;',
  7630. ' i:=ord(i2);',
  7631. '']);
  7632. ConvertProgram;
  7633. CheckSource('TestIntegerRange',
  7634. LinesToStr([
  7635. 'this.MinInt = -1;',
  7636. 'this.MaxInt = +1;',
  7637. 'this.a = -1 + 1;',
  7638. 'this.b = 1 + 3;',
  7639. 'this.s1 = rtl.createSet(1);',
  7640. 'this.s2 = rtl.createSet(1, 2);',
  7641. 'this.s3 = rtl.createSet(null, 1, 3);',
  7642. 'this.s4 = rtl.createSet(null, -128, 127);',
  7643. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7644. 'this.s6 = 1 in this.s2;',
  7645. 'this.i = 0;',
  7646. 'this.i2 = 0;',
  7647. '']),
  7648. LinesToStr([
  7649. '$mod.i = $mod.i2;',
  7650. '$mod.i = -1;',
  7651. 'if ($mod.i === $mod.i2) ;',
  7652. '$mod.i = $mod.i2;',
  7653. '']));
  7654. end;
  7655. procedure TTestModule.TestIntegerTypecasts;
  7656. begin
  7657. StartProgram(false);
  7658. Add([
  7659. 'var',
  7660. ' i: nativeint;',
  7661. ' b: byte;',
  7662. ' sh: shortint;',
  7663. ' w: word;',
  7664. ' sm: smallint;',
  7665. ' lw: longword;',
  7666. ' li: longint;',
  7667. 'begin',
  7668. ' b:=byte(i);',
  7669. ' sh:=shortint(i);',
  7670. ' w:=word(i);',
  7671. ' sm:=smallint(i);',
  7672. ' lw:=longword(i);',
  7673. ' li:=longint(i);',
  7674. '']);
  7675. ConvertProgram;
  7676. CheckSource('TestIntegerTypecasts',
  7677. LinesToStr([
  7678. 'this.i = 0;',
  7679. 'this.b = 0;',
  7680. 'this.sh = 0;',
  7681. 'this.w = 0;',
  7682. 'this.sm = 0;',
  7683. 'this.lw = 0;',
  7684. 'this.li = 0;',
  7685. '']),
  7686. LinesToStr([
  7687. '$mod.b = $mod.i & 255;',
  7688. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7689. '$mod.w = $mod.i & 65535;',
  7690. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7691. '$mod.lw = $mod.i >>> 0;',
  7692. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7693. '']));
  7694. end;
  7695. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7696. begin
  7697. StartProgram(false);
  7698. Add([
  7699. 'var',
  7700. ' i,j: nativeint;',
  7701. 'begin',
  7702. ' i:=i shr 0;',
  7703. ' i:=i shr 1;',
  7704. ' i:=i shr 3;',
  7705. ' i:=i shr 54;',
  7706. ' i:=j shr i;',
  7707. '']);
  7708. ConvertProgram;
  7709. CheckResolverUnexpectedHints;
  7710. CheckSource('TestInteger_BitwiseShrNativeInt',
  7711. LinesToStr([
  7712. 'this.i = 0;',
  7713. 'this.j = 0;',
  7714. '']),
  7715. LinesToStr([
  7716. '$mod.i = $mod.i;',
  7717. '$mod.i = Math.floor($mod.i / 2);',
  7718. '$mod.i = Math.floor($mod.i / 8);',
  7719. '$mod.i = 0;',
  7720. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7721. '']));
  7722. end;
  7723. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7724. begin
  7725. StartProgram(false);
  7726. Add([
  7727. 'var',
  7728. ' i: nativeint;',
  7729. 'begin',
  7730. ' i:=i shl 0;',
  7731. ' i:=i shl 54;',
  7732. ' i:=123456789012 shl 1;',
  7733. ' i:=i shl 1;',
  7734. '']);
  7735. ConvertProgram;
  7736. CheckResolverUnexpectedHints;
  7737. CheckSource('TestInteger_BitwiseShrNativeInt',
  7738. LinesToStr([
  7739. 'this.i = 0;',
  7740. '']),
  7741. LinesToStr([
  7742. '$mod.i = $mod.i;',
  7743. '$mod.i = 0;',
  7744. '$mod.i = 246913578024;',
  7745. '$mod.i = rtl.shl($mod.i, 1);',
  7746. '']));
  7747. end;
  7748. procedure TTestModule.TestInteger_SystemFunc;
  7749. begin
  7750. StartProgram(true);
  7751. Add([
  7752. 'var',
  7753. ' i: byte;',
  7754. ' s: string;',
  7755. 'begin',
  7756. ' system.inc(i);',
  7757. ' system.str(i,s);',
  7758. ' s:=system.str(i);',
  7759. ' i:=system.low(i);',
  7760. ' i:=system.high(i);',
  7761. ' i:=system.pred(i);',
  7762. ' i:=system.succ(i);',
  7763. ' i:=system.ord(i);',
  7764. '']);
  7765. ConvertProgram;
  7766. CheckResolverUnexpectedHints;
  7767. CheckSource('TestInteger_SystemFunc',
  7768. LinesToStr([
  7769. 'this.i = 0;',
  7770. 'this.s = "";',
  7771. '']),
  7772. LinesToStr([
  7773. '$mod.i += 1;',
  7774. '$mod.s = "" + $mod.i;',
  7775. '$mod.s = "" + $mod.i;',
  7776. '$mod.i = 0;',
  7777. '$mod.i = 255;',
  7778. '$mod.i = $mod.i - 1;',
  7779. '$mod.i = $mod.i + 1;',
  7780. '$mod.i = $mod.i;',
  7781. '']));
  7782. end;
  7783. procedure TTestModule.TestInteger_AssignOutsideConst;
  7784. begin
  7785. StartProgram(false);
  7786. Add([
  7787. 'const',
  7788. ' MinInt = low(longint);',
  7789. ' MaxInt = high(longint);',
  7790. 'type',
  7791. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7792. 'var',
  7793. ' i: TMyInt;',
  7794. ' aByte: byte;',
  7795. ' aShortInt: shortint;',
  7796. ' aWord: word;',
  7797. ' aSmallInt: smallint;',
  7798. ' aLongWord: longword;',
  7799. ' aLongInt: longint;',
  7800. ' aNativeInt: nativeint;',
  7801. ' aNativeUInt: nativeuint;',
  7802. 'begin',
  7803. ' aByte:=$FF;',
  7804. ' aByte:=$100;',
  7805. ' aByte:=-1;',
  7806. ' aByte:=-127;',
  7807. ' aByte:=-128;',
  7808. ' aByte:=-254;',
  7809. ' aByte:=-255;',
  7810. ' aByte:=-256;',
  7811. ' aShortInt:=127;',
  7812. ' aShortInt:=128;',
  7813. ' aShortInt:=-128;',
  7814. ' aShortInt:=-129;',
  7815. ' aWord:=$ffff;',
  7816. ' aWord:=$10000;',
  7817. ' aWord:=-1;',
  7818. ' aWord:=-$ffff;',
  7819. ' aWord:=-$10000;',
  7820. ' aWord:=-$10001;',
  7821. ' aSmallInt:=$7fff;',
  7822. ' aSmallInt:=$8000;',
  7823. ' aSmallInt:=-$8000;',
  7824. ' aSmallInt:=-$8001;',
  7825. ' aLongWord:=$ffffffff;',
  7826. ' aLongWord:=$100000000;',
  7827. ' aLongWord:=-1;',
  7828. ' aLongWord:=-$ffffffff;',
  7829. ' aNativeInt:=$1fffffffffffff;',
  7830. ' aNativeInt:=-$1fffffffffffff;',
  7831. ' aNativeUInt:=$1fffffffffffff;',
  7832. ' aNativeUInt:=-$1fffffffffffff;',
  7833. '']);
  7834. ConvertProgram;
  7835. CheckSource('TestInteger_AssignOutsideConst',
  7836. LinesToStr([
  7837. 'this.MinInt = -2147483648;',
  7838. 'this.MaxInt = 2147483647;',
  7839. 'this.i = 0;',
  7840. 'this.aByte = 0;',
  7841. 'this.aShortInt = 0;',
  7842. 'this.aWord = 0;',
  7843. 'this.aSmallInt = 0;',
  7844. 'this.aLongWord = 0;',
  7845. 'this.aLongInt = 0;',
  7846. 'this.aNativeInt = 0;',
  7847. 'this.aNativeUInt = 0;',
  7848. '']),
  7849. LinesToStr([
  7850. '$mod.aByte = 0xFF;',
  7851. '$mod.aByte = 0;',
  7852. '$mod.aByte = 255;',
  7853. '$mod.aByte = 129;',
  7854. '$mod.aByte = 128;',
  7855. '$mod.aByte = 2;',
  7856. '$mod.aByte = 1;',
  7857. '$mod.aByte = 0;',
  7858. '$mod.aShortInt = 127;',
  7859. '$mod.aShortInt = -128;',
  7860. '$mod.aShortInt = -128;',
  7861. '$mod.aShortInt = 127;',
  7862. '$mod.aWord = 0xffff;',
  7863. '$mod.aWord = 0;',
  7864. '$mod.aWord = 65535;',
  7865. '$mod.aWord = 1;',
  7866. '$mod.aWord = 0;',
  7867. '$mod.aWord = 65535;',
  7868. '$mod.aSmallInt = 0x7fff;',
  7869. '$mod.aSmallInt = -32768;',
  7870. '$mod.aSmallInt = -0x8000;',
  7871. '$mod.aSmallInt = 32767;',
  7872. '$mod.aLongWord = 0xffffffff;',
  7873. '$mod.aLongWord = 0;',
  7874. '$mod.aLongWord = 4294967295;',
  7875. '$mod.aLongWord = 1;',
  7876. '$mod.aNativeInt = 0x1fffffffffffff;',
  7877. '$mod.aNativeInt = -0x1fffffffffffff;',
  7878. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7879. '$mod.aNativeUInt = 1;',
  7880. '']));
  7881. end;
  7882. procedure TTestModule.TestCurrency;
  7883. begin
  7884. StartProgram(false);
  7885. Add([
  7886. 'type',
  7887. ' TCoin = currency;',
  7888. 'const',
  7889. ' a = TCoin(2.7);',
  7890. ' b = a + TCoin(1.7);',
  7891. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7892. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7893. 'var',
  7894. ' c: TCoin = b;',
  7895. ' i: nativeint;',
  7896. ' d: double;',
  7897. ' j: jsvalue;',
  7898. 'function DoIt(c: currency): currency; begin end;',
  7899. 'function GetIt(d: double): double; begin end;',
  7900. 'procedure Write(v: jsvalue); begin end;',
  7901. 'begin',
  7902. ' c:=1.0;',
  7903. ' c:=0.1;',
  7904. ' c:=1.0/3.0;',
  7905. ' c:=1/3;',
  7906. ' c:=a;',
  7907. ' d:=c;',
  7908. ' c:=d;',
  7909. ' c:=currency(c);',
  7910. ' c:=currency(d);',
  7911. ' d:=double(c);',
  7912. ' c:=i;',
  7913. ' c:=currency(i);',
  7914. //' i:=c;', not allowed
  7915. ' i:=nativeint(c);',
  7916. ' c:=c+a;',
  7917. ' c:=-c-a;',
  7918. ' c:=d+c;',
  7919. ' c:=c+d;',
  7920. ' c:=d-c;',
  7921. ' c:=c-d;',
  7922. ' c:=c*a;',
  7923. ' c:=a*c;',
  7924. ' c:=d*c;',
  7925. ' c:=c*d;',
  7926. ' c:=c/a;',
  7927. ' c:=a/c;',
  7928. ' c:=d/c;',
  7929. ' c:=c/d;',
  7930. ' c:=c**a;',
  7931. ' c:=a**c;',
  7932. ' c:=d**c;',
  7933. ' c:=c**d;',
  7934. ' if c=c then ;',
  7935. ' if c=a then ;',
  7936. ' if a=c then ;',
  7937. ' if d=c then ;',
  7938. ' if c=d then ;',
  7939. ' c:=DoIt(c);',
  7940. ' c:=DoIt(i);',
  7941. ' c:=DoIt(d);',
  7942. ' c:=GetIt(c);',
  7943. ' j:=c;',
  7944. ' Write(c);',
  7945. ' c:=default(currency);',
  7946. ' j:=str(c);',
  7947. ' j:=str(c:0:3);',
  7948. '']);
  7949. ConvertProgram;
  7950. CheckSource('TestCurrency',
  7951. LinesToStr([
  7952. 'this.a = 27000;',
  7953. 'this.b = this.a + 17000;',
  7954. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7955. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7956. 'this.c = this.b;',
  7957. 'this.i = 0;',
  7958. 'this.d = 0.0;',
  7959. 'this.j = undefined;',
  7960. 'this.DoIt = function (c) {',
  7961. ' var Result = 0;',
  7962. ' return Result;',
  7963. '};',
  7964. 'this.GetIt = function (d) {',
  7965. ' var Result = 0.0;',
  7966. ' return Result;',
  7967. '};',
  7968. 'this.Write = function (v) {',
  7969. '};',
  7970. '']),
  7971. LinesToStr([
  7972. '$mod.c = 10000;',
  7973. '$mod.c = 1000;',
  7974. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7975. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7976. '$mod.c = $mod.a;',
  7977. '$mod.d = $mod.c / 10000;',
  7978. '$mod.c = rtl.trunc($mod.d * 10000);',
  7979. '$mod.c = $mod.c;',
  7980. '$mod.c = $mod.d * 10000;',
  7981. '$mod.d = $mod.c / 10000;',
  7982. '$mod.c = $mod.i * 10000;',
  7983. '$mod.c = $mod.i * 10000;',
  7984. '$mod.i = rtl.trunc($mod.c / 10000);',
  7985. '$mod.c = $mod.c + $mod.a;',
  7986. '$mod.c = -$mod.c - $mod.a;',
  7987. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7988. '$mod.c = $mod.c + ($mod.d * 10000);',
  7989. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7990. '$mod.c = $mod.c - ($mod.d * 10000);',
  7991. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7992. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7993. '$mod.c = $mod.d * $mod.c;',
  7994. '$mod.c = $mod.c * $mod.d;',
  7995. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7996. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7997. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7998. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7999. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8000. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8001. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8002. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8003. 'if ($mod.c === $mod.c) ;',
  8004. 'if ($mod.c === $mod.a) ;',
  8005. 'if ($mod.a === $mod.c) ;',
  8006. 'if (($mod.d * 10000) === $mod.c) ;',
  8007. 'if ($mod.c === ($mod.d * 10000)) ;',
  8008. '$mod.c = $mod.DoIt($mod.c);',
  8009. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8010. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8011. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8012. '$mod.j = $mod.c / 10000;',
  8013. '$mod.Write($mod.c / 10000);',
  8014. '$mod.c = 0;',
  8015. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8016. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8017. '']));
  8018. end;
  8019. procedure TTestModule.TestForBoolDo;
  8020. begin
  8021. StartProgram(false);
  8022. Add([
  8023. 'var b: boolean;',
  8024. 'begin',
  8025. ' for b:=false to true do ;',
  8026. ' for b:=b downto false do ;',
  8027. ' for b in boolean do ;',
  8028. '']);
  8029. ConvertProgram;
  8030. CheckSource('TestForBoolDo',
  8031. LinesToStr([ // statements
  8032. 'this.b = false;']),
  8033. LinesToStr([ // this.$main
  8034. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8035. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8036. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8037. '']));
  8038. end;
  8039. procedure TTestModule.TestForIntDo;
  8040. begin
  8041. StartProgram(false);
  8042. Add([
  8043. 'var i: longint;',
  8044. 'begin',
  8045. ' for i:=3 to 5 do ;',
  8046. ' for i:=i downto 2 do ;',
  8047. ' for i in byte do ;',
  8048. '']);
  8049. ConvertProgram;
  8050. CheckSource('TestForIntDo',
  8051. LinesToStr([ // statements
  8052. 'this.i = 0;']),
  8053. LinesToStr([ // this.$main
  8054. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8055. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8056. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8057. '']));
  8058. end;
  8059. procedure TTestModule.TestForIntInDo;
  8060. begin
  8061. StartProgram(false);
  8062. Add([
  8063. 'type',
  8064. ' TSetOfInt = set of byte;',
  8065. ' TIntRg = 3..7;',
  8066. ' TSetOfIntRg = set of TIntRg;',
  8067. 'var',
  8068. ' i,i2: longint;',
  8069. ' a1: array of byte;',
  8070. ' a2: array[1..3] of byte;',
  8071. ' soi: TSetOfInt;',
  8072. ' soir: TSetOfIntRg;',
  8073. ' ir: TIntRg;',
  8074. 'begin',
  8075. ' for i in byte do ;',
  8076. ' for i in a1 do ;',
  8077. ' for i in a2 do ;',
  8078. ' for i in [11..13] do ;',
  8079. ' for i in TSetOfInt do ;',
  8080. ' for i in TIntRg do ;',
  8081. ' for i in soi do i2:=i;',
  8082. ' for i in TSetOfIntRg do ;',
  8083. ' for i in soir do ;',
  8084. ' for ir in TIntRg do ;',
  8085. ' for ir in TSetOfIntRg do ;',
  8086. ' for ir in soir do ;',
  8087. '']);
  8088. ConvertProgram;
  8089. CheckSource('TestForIntInDo',
  8090. LinesToStr([ // statements
  8091. 'this.i = 0;',
  8092. 'this.i2 = 0;',
  8093. 'this.a1 = [];',
  8094. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8095. 'this.soi = {};',
  8096. 'this.soir = {};',
  8097. 'this.ir = 0;',
  8098. '']),
  8099. LinesToStr([ // this.$main
  8100. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8101. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8102. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8103. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8104. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8105. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8106. 'for (var $l6 in $mod.soi) {',
  8107. ' $mod.i = +$l6;',
  8108. ' $mod.i2 = $mod.i;',
  8109. '};',
  8110. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8111. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8112. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8113. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8114. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8115. '']));
  8116. end;
  8117. procedure TTestModule.TestCharConst;
  8118. begin
  8119. StartProgram(false);
  8120. Add([
  8121. 'const',
  8122. ' a = #$00F3;',
  8123. ' c: char = ''1'';',
  8124. ' wc: widechar = ''ä'';',
  8125. 'begin',
  8126. ' c:=#0;',
  8127. ' c:=#1;',
  8128. ' c:=#9;',
  8129. ' c:=#10;',
  8130. ' c:=#13;',
  8131. ' c:=#31;',
  8132. ' c:=#32;',
  8133. ' c:=#$A;',
  8134. ' c:=#$0A;',
  8135. ' c:=#$b;',
  8136. ' c:=#$0b;',
  8137. ' c:=^A;',
  8138. ' c:=''"'';',
  8139. ' c:=default(char);',
  8140. ' c:=#$00E4;', // ä
  8141. ' c:=''ä'';',
  8142. ' c:=#$E4;', // ä
  8143. ' c:=#$D800;', // invalid UTF-16
  8144. ' c:=#$DFFF;', // invalid UTF-16
  8145. ' c:=#$FFFF;', // last UCS-2
  8146. ' c:=high(c);', // last UCS-2
  8147. ' c:=#269;',
  8148. '']);
  8149. ConvertProgram;
  8150. CheckSource('TestCharConst',
  8151. LinesToStr([
  8152. 'this.a="ó";',
  8153. 'this.c="1";',
  8154. 'this.wc="ä";'
  8155. ]),
  8156. LinesToStr([
  8157. '$mod.c="\x00";',
  8158. '$mod.c="\x01";',
  8159. '$mod.c="\t";',
  8160. '$mod.c="\n";',
  8161. '$mod.c="\r";',
  8162. '$mod.c="\x1F";',
  8163. '$mod.c=" ";',
  8164. '$mod.c="\n";',
  8165. '$mod.c="\n";',
  8166. '$mod.c="\x0B";',
  8167. '$mod.c="\x0B";',
  8168. '$mod.c="\x01";',
  8169. '$mod.c=''"'';',
  8170. '$mod.c="\x00";',
  8171. '$mod.c = "ä";',
  8172. '$mod.c = "ä";',
  8173. '$mod.c = "ä";',
  8174. '$mod.c="\uD800";',
  8175. '$mod.c="\uDFFF";',
  8176. '$mod.c="\uFFFF";',
  8177. '$mod.c="\uFFFF";',
  8178. '$mod.c = "č";',
  8179. '']));
  8180. end;
  8181. procedure TTestModule.TestChar_Compare;
  8182. begin
  8183. StartProgram(false);
  8184. Add('var');
  8185. Add(' c: char;');
  8186. Add(' b: boolean;');
  8187. Add('begin');
  8188. Add(' b:=c=''1'';');
  8189. Add(' b:=''2''=c;');
  8190. Add(' b:=''3''=''4'';');
  8191. Add(' b:=c<>''5'';');
  8192. Add(' b:=''6''<>c;');
  8193. Add(' b:=c>''7'';');
  8194. Add(' b:=''8''>c;');
  8195. Add(' b:=c>=''9'';');
  8196. Add(' b:=''A''>=c;');
  8197. Add(' b:=c<''B'';');
  8198. Add(' b:=''C''<c;');
  8199. Add(' b:=c<=''D'';');
  8200. Add(' b:=''E''<=c;');
  8201. ConvertProgram;
  8202. CheckSource('TestChar_Compare',
  8203. LinesToStr([
  8204. 'this.c="";',
  8205. 'this.b = false;'
  8206. ]),
  8207. LinesToStr([
  8208. '$mod.b = $mod.c === "1";',
  8209. '$mod.b = "2" === $mod.c;',
  8210. '$mod.b = "3" === "4";',
  8211. '$mod.b = $mod.c !== "5";',
  8212. '$mod.b = "6" !== $mod.c;',
  8213. '$mod.b = $mod.c > "7";',
  8214. '$mod.b = "8" > $mod.c;',
  8215. '$mod.b = $mod.c >= "9";',
  8216. '$mod.b = "A" >= $mod.c;',
  8217. '$mod.b = $mod.c < "B";',
  8218. '$mod.b = "C" < $mod.c;',
  8219. '$mod.b = $mod.c <= "D";',
  8220. '$mod.b = "E" <= $mod.c;',
  8221. '']));
  8222. end;
  8223. procedure TTestModule.TestChar_BuiltInProcs;
  8224. begin
  8225. StartProgram(false);
  8226. Add([
  8227. 'var',
  8228. ' c: char;',
  8229. ' i: longint;',
  8230. ' s: string;',
  8231. 'begin',
  8232. ' i:=ord(c);',
  8233. ' i:=ord(s[i]);',
  8234. ' c:=chr(i);',
  8235. ' c:=pred(c);',
  8236. ' c:=succ(c);',
  8237. ' c:=low(c);',
  8238. ' c:=high(c);',
  8239. ' i:=byte(c);',
  8240. ' i:=word(c);',
  8241. ' i:=longint(c);',
  8242. '']);
  8243. ConvertProgram;
  8244. CheckSource('TestChar_BuiltInProcs',
  8245. LinesToStr([
  8246. 'this.c = "";',
  8247. 'this.i = 0;',
  8248. 'this.s = "";'
  8249. ]),
  8250. LinesToStr([
  8251. '$mod.i = $mod.c.charCodeAt();',
  8252. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8253. '$mod.c = String.fromCharCode($mod.i);',
  8254. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8255. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8256. '$mod.c = "\x00";',
  8257. '$mod.c = "\uFFFF";',
  8258. '$mod.i = $mod.c.charCodeAt() & 255;',
  8259. '$mod.i = $mod.c.charCodeAt();',
  8260. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8261. '']));
  8262. end;
  8263. procedure TTestModule.TestStringConst;
  8264. begin
  8265. StartProgram(false);
  8266. Add([
  8267. '{$H+}',
  8268. 'const',
  8269. ' a = #$00F3#$017C;', // first <256, then >=256
  8270. ' b = string(''a'');',
  8271. ' c = string(''ä'');',
  8272. ' d = UnicodeString(''b'');',
  8273. ' e = UnicodeString(''ö'');',
  8274. ' f = low(a)+high(b);',
  8275. ' g: word = low(a);',
  8276. 'var',
  8277. ' s: string = ''abc'';',
  8278. ' i: longint;',
  8279. 'begin',
  8280. ' s:='''';',
  8281. ' s:=#13#10;',
  8282. ' s:=#9''foo'';',
  8283. ' s:=#$A9;',
  8284. ' s:=''foo''#13''bar'';',
  8285. ' s:=''"'';',
  8286. ' s:=''"''''"'';',
  8287. ' s:=#$20AC;', // euro
  8288. ' s:=#$10437;', // outside BMP
  8289. ' s:=''abc''#$20AC;', // ascii,#
  8290. ' s:=''ä''#$20AC;', // non ascii,#
  8291. ' s:=#$20AC''abc'';', // #, ascii
  8292. ' s:=#$20AC''ä'';', // #, non ascii
  8293. ' s:=default(string);',
  8294. ' s:=concat(s);',
  8295. ' s:=concat(s,''a'',s);',
  8296. ' s:=#250#269;',
  8297. ' i:=low(s)+high(a);',
  8298. //' s:=#$2F804;',
  8299. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8300. '']);
  8301. ConvertProgram;
  8302. CheckSource('TestStringConst',
  8303. LinesToStr([
  8304. 'this.a = "óż";',
  8305. 'this.b = "a";',
  8306. 'this.c = "ä";',
  8307. 'this.d = "b";',
  8308. 'this.e = "ö";',
  8309. 'this.f = 1 + this.b.length;',
  8310. 'this.g = 1;',
  8311. 'this.s="abc";',
  8312. 'this.i = 0;',
  8313. '']),
  8314. LinesToStr([
  8315. '$mod.s="";',
  8316. '$mod.s="\r\n";',
  8317. '$mod.s="\tfoo";',
  8318. '$mod.s="©";',
  8319. '$mod.s="foo\rbar";',
  8320. '$mod.s=''"'';',
  8321. '$mod.s=''"\''"'';',
  8322. '$mod.s="€";',
  8323. '$mod.s="'#$F0#$90#$90#$B7'";',
  8324. '$mod.s = "abc€";',
  8325. '$mod.s = "ä€";',
  8326. '$mod.s = "€abc";',
  8327. '$mod.s = "ۊ";',
  8328. '$mod.s="";',
  8329. '$mod.s = $mod.s;',
  8330. '$mod.s = $mod.s.concat("a", $mod.s);',
  8331. '$mod.s = "úč";',
  8332. '$mod.i = 1 + $mod.a.length;',
  8333. '']));
  8334. end;
  8335. procedure TTestModule.TestStringConst_InvalidUTF16;
  8336. begin
  8337. StartProgram(false);
  8338. Add([
  8339. 'const',
  8340. ' a: char = #$D87E;',
  8341. ' b: string = #$D87E;',
  8342. ' c: string = #$D87E#43;',
  8343. 'begin',
  8344. ' c:=''abc''#$D87E;',
  8345. ' c:=#0#1#2;',
  8346. ' c:=#127;',
  8347. ' c:=#128;',
  8348. ' c:=#255;',
  8349. ' c:=#256;',
  8350. '']);
  8351. ConvertProgram;
  8352. CheckSource('TestStringConst',
  8353. LinesToStr([
  8354. 'this.a = "\uD87E";',
  8355. 'this.b = "\uD87E";',
  8356. 'this.c = "\uD87E+";',
  8357. '']),
  8358. LinesToStr([
  8359. '$mod.c = "abc\uD87E";',
  8360. '$mod.c = "\x00\x01\x02";',
  8361. '$mod.c = "'#127'";',
  8362. '$mod.c = "'#$c2#$80'";',
  8363. '$mod.c = "'#$c3#$BF'";',
  8364. '$mod.c = "'#$c4#$80'";',
  8365. '']));
  8366. end;
  8367. procedure TTestModule.TestStringConstSurrogate;
  8368. begin
  8369. StartProgram(false);
  8370. Add([
  8371. 'var',
  8372. ' s: string;',
  8373. 'begin',
  8374. ' s:=''😊'';', // 1F60A
  8375. '']);
  8376. ConvertProgram;
  8377. CheckSource('TestStringConstSurrogate',
  8378. LinesToStr([
  8379. 'this.s="";'
  8380. ]),
  8381. LinesToStr([
  8382. '$mod.s="😊";'
  8383. ]));
  8384. end;
  8385. procedure TTestModule.TestString_Length;
  8386. begin
  8387. StartProgram(false);
  8388. Add('const c = ''foo'';');
  8389. Add('var');
  8390. Add(' s: string;');
  8391. Add(' i: longint;');
  8392. Add('begin');
  8393. Add(' i:=length(s);');
  8394. Add(' i:=length(s+s);');
  8395. Add(' i:=length(''abc'');');
  8396. Add(' i:=length(c);');
  8397. ConvertProgram;
  8398. CheckSource('TestString_Length',
  8399. LinesToStr([
  8400. 'this.c = "foo";',
  8401. 'this.s = "";',
  8402. 'this.i = 0;',
  8403. '']),
  8404. LinesToStr([
  8405. '$mod.i = $mod.s.length;',
  8406. '$mod.i = ($mod.s+$mod.s).length;',
  8407. '$mod.i = "abc".length;',
  8408. '$mod.i = $mod.c.length;',
  8409. '']));
  8410. end;
  8411. procedure TTestModule.TestString_Compare;
  8412. begin
  8413. StartProgram(false);
  8414. Add('var');
  8415. Add(' s, t: string;');
  8416. Add(' b: boolean;');
  8417. Add('begin');
  8418. Add(' b:=s=t;');
  8419. Add(' b:=s<>t;');
  8420. Add(' b:=s>t;');
  8421. Add(' b:=s>=t;');
  8422. Add(' b:=s<t;');
  8423. Add(' b:=s<=t;');
  8424. ConvertProgram;
  8425. CheckSource('TestString_Compare',
  8426. LinesToStr([ // statements
  8427. 'this.s = "";',
  8428. 'this.t = "";',
  8429. 'this.b =false;'
  8430. ]),
  8431. LinesToStr([ // this.$main
  8432. '$mod.b = $mod.s === $mod.t;',
  8433. '$mod.b = $mod.s !== $mod.t;',
  8434. '$mod.b = $mod.s > $mod.t;',
  8435. '$mod.b = $mod.s >= $mod.t;',
  8436. '$mod.b = $mod.s < $mod.t;',
  8437. '$mod.b = $mod.s <= $mod.t;',
  8438. '']));
  8439. end;
  8440. procedure TTestModule.TestString_SetLength;
  8441. begin
  8442. StartProgram(false);
  8443. Add([
  8444. 'procedure Fly(var s: string);',
  8445. 'begin',
  8446. ' SetLength(s,1);',
  8447. 'end;',
  8448. 'procedure Run(var s: unicodestring);',
  8449. 'begin',
  8450. ' SetLength(s,2);',
  8451. 'end;',
  8452. 'var s: string;',
  8453. ' u: unicodestring;',
  8454. 'begin',
  8455. ' SetLength(s,3);',
  8456. ' SetLength(u,4);',
  8457. '']);
  8458. ConvertProgram;
  8459. CheckSource('TestString_SetLength',
  8460. LinesToStr([ // statements
  8461. 'this.Fly = function (s) {',
  8462. ' s.set(rtl.strSetLength(s.get(), 1));',
  8463. '};',
  8464. 'this.Run = function (s) {',
  8465. ' s.set(rtl.strSetLength(s.get(), 2));',
  8466. '};',
  8467. 'this.s = "";',
  8468. 'this.u = "";',
  8469. '']),
  8470. LinesToStr([ // this.$main
  8471. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8472. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8473. ]));
  8474. end;
  8475. procedure TTestModule.TestString_CharAt;
  8476. begin
  8477. StartProgram(false);
  8478. Add([
  8479. 'var',
  8480. ' s: string;',
  8481. ' c: char;',
  8482. ' b: boolean;',
  8483. 'begin',
  8484. ' b:= s[1] = c;',
  8485. ' b:= c = s[1];',
  8486. ' b:= c <> s[1];',
  8487. ' b:= c > s[1];',
  8488. ' b:= c >= s[1];',
  8489. ' b:= c < s[2];',
  8490. ' b:= c <= s[1];',
  8491. ' s[1] := c;',
  8492. ' s[2+3] := c;']);
  8493. ConvertProgram;
  8494. CheckSource('TestString_CharAt',
  8495. LinesToStr([ // statements
  8496. 'this.s = "";',
  8497. 'this.c = "";',
  8498. 'this.b = false;'
  8499. ]),
  8500. LinesToStr([ // this.$main
  8501. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8502. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8503. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8504. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8505. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8506. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8507. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8508. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8509. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8510. '']));
  8511. end;
  8512. procedure TTestModule.TestStringHMinusFail;
  8513. begin
  8514. StartProgram(false);
  8515. Add([
  8516. '{$H-}',
  8517. 'var s: string;',
  8518. 'begin']);
  8519. ConvertProgram;
  8520. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8521. end;
  8522. procedure TTestModule.TestStr;
  8523. begin
  8524. StartProgram(false);
  8525. Add('var');
  8526. Add(' b: boolean;');
  8527. Add(' i: longint;');
  8528. Add(' d: double;');
  8529. Add(' s: string;');
  8530. Add('begin');
  8531. Add(' str(b,s);');
  8532. Add(' str(i,s);');
  8533. Add(' str(d,s);');
  8534. Add(' str(i:3,s);');
  8535. Add(' str(d:3:2,s);');
  8536. Add(' Str(12.456:12:1,s);');
  8537. Add(' Str(12.456:12,s);');
  8538. Add(' s:=str(b);');
  8539. Add(' s:=str(i);');
  8540. Add(' s:=str(d);');
  8541. Add(' s:=str(i,i);');
  8542. Add(' s:=str(i:3);');
  8543. Add(' s:=str(d:3:2);');
  8544. Add(' s:=str(i:4,i);');
  8545. Add(' s:=str(i,i:5);');
  8546. Add(' s:=str(i:4,i:5);');
  8547. Add(' s:=str(s,s);');
  8548. Add(' s:=str(s,''foo'');');
  8549. ConvertProgram;
  8550. CheckSource('TestStr',
  8551. LinesToStr([ // statements
  8552. 'this.b = false;',
  8553. 'this.i = 0;',
  8554. 'this.d = 0.0;',
  8555. 'this.s = "";',
  8556. '']),
  8557. LinesToStr([ // this.$main
  8558. '$mod.s = ""+$mod.b;',
  8559. '$mod.s = ""+$mod.i;',
  8560. '$mod.s = rtl.floatToStr($mod.d);',
  8561. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8562. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8563. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8564. '$mod.s = rtl.floatToStr(12.456,12);',
  8565. '$mod.s = ""+$mod.b;',
  8566. '$mod.s = ""+$mod.i;',
  8567. '$mod.s = rtl.floatToStr($mod.d);',
  8568. '$mod.s = ""+$mod.i+$mod.i;',
  8569. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8570. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8571. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8572. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8573. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8574. '$mod.s = $mod.s + $mod.s;',
  8575. '$mod.s = $mod.s + "foo";',
  8576. '']));
  8577. end;
  8578. procedure TTestModule.TestBaseType_AnsiStringFail;
  8579. begin
  8580. StartProgram(false);
  8581. Add('var s: AnsiString');
  8582. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8583. ConvertProgram;
  8584. end;
  8585. procedure TTestModule.TestBaseType_WideStringFail;
  8586. begin
  8587. StartProgram(false);
  8588. Add('var s: WideString');
  8589. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8590. ConvertProgram;
  8591. end;
  8592. procedure TTestModule.TestBaseType_ShortStringFail;
  8593. begin
  8594. StartProgram(false);
  8595. Add('var s: ShortString');
  8596. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8597. ConvertProgram;
  8598. end;
  8599. procedure TTestModule.TestBaseType_RawByteStringFail;
  8600. begin
  8601. StartProgram(false);
  8602. Add('var s: RawByteString');
  8603. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8604. ConvertProgram;
  8605. end;
  8606. procedure TTestModule.TestTypeShortstring_Fail;
  8607. begin
  8608. StartProgram(false);
  8609. Add('type t = string[12];');
  8610. Add('var s: t;');
  8611. Add('begin');
  8612. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8613. ConvertProgram;
  8614. end;
  8615. procedure TTestModule.TestCharSet_Custom;
  8616. begin
  8617. StartProgram(false);
  8618. Add([
  8619. 'type',
  8620. ' TCharRg = ''a''..''z'';',
  8621. ' TSetOfCharRg = set of TCharRg;',
  8622. ' TCharRg2 = ''m''..''p'';',
  8623. 'const',
  8624. ' crg: TCharRg = ''b'';',
  8625. 'var',
  8626. ' c: char;',
  8627. ' crg2: TCharRg2;',
  8628. ' s: TSetOfCharRg;',
  8629. 'begin',
  8630. ' c:=crg;',
  8631. ' crg:=c;',
  8632. ' crg2:=crg;',
  8633. ' if c=crg then ;',
  8634. ' if crg=c then ;',
  8635. ' if crg=crg2 then ;',
  8636. ' if c in s then ;',
  8637. ' if crg2 in s then ;',
  8638. ' c:=default(TCharRg);',
  8639. '']);
  8640. ConvertProgram;
  8641. CheckSource('TestCharSet_Custom',
  8642. LinesToStr([ // statements
  8643. 'this.crg = "b";',
  8644. 'this.c = "";',
  8645. 'this.crg2 = "m";',
  8646. 'this.s = {};',
  8647. '']),
  8648. LinesToStr([ // this.$main
  8649. '$mod.c = $mod.crg;',
  8650. '$mod.crg = $mod.c;',
  8651. '$mod.crg2 = $mod.crg;',
  8652. 'if ($mod.c === $mod.crg) ;',
  8653. 'if ($mod.crg === $mod.c) ;',
  8654. 'if ($mod.crg === $mod.crg2) ;',
  8655. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8656. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8657. '$mod.c = "a";',
  8658. '']));
  8659. end;
  8660. procedure TTestModule.TestWideChar;
  8661. begin
  8662. StartProgram(false);
  8663. Add([
  8664. 'procedure Fly(var c: char);',
  8665. 'begin',
  8666. 'end;',
  8667. 'procedure Run(var c: widechar);',
  8668. 'begin',
  8669. 'end;',
  8670. 'var',
  8671. ' c: char;',
  8672. ' wc: widechar;',
  8673. ' w: word;',
  8674. 'begin',
  8675. ' Fly(wc);',
  8676. ' Run(c);',
  8677. ' wc:=WideChar(w);',
  8678. ' w:=ord(wc);',
  8679. '']);
  8680. ConvertProgram;
  8681. CheckSource('TestWideChar_VarArg',
  8682. LinesToStr([ // statements
  8683. 'this.Fly = function (c) {',
  8684. '};',
  8685. 'this.Run = function (c) {',
  8686. '};',
  8687. 'this.c = "";',
  8688. 'this.wc = "";',
  8689. 'this.w = 0;',
  8690. '']),
  8691. LinesToStr([ // this.$main
  8692. '$mod.Fly({',
  8693. ' p: $mod,',
  8694. ' get: function () {',
  8695. ' return this.p.wc;',
  8696. ' },',
  8697. ' set: function (v) {',
  8698. ' this.p.wc = v;',
  8699. ' }',
  8700. '});',
  8701. '$mod.Run({',
  8702. ' p: $mod,',
  8703. ' get: function () {',
  8704. ' return this.p.c;',
  8705. ' },',
  8706. ' set: function (v) {',
  8707. ' this.p.c = v;',
  8708. ' }',
  8709. '});',
  8710. '$mod.wc = String.fromCharCode($mod.w);',
  8711. '$mod.w = $mod.wc.charCodeAt();',
  8712. '',
  8713. '']));
  8714. end;
  8715. procedure TTestModule.TestForCharDo;
  8716. begin
  8717. StartProgram(false);
  8718. Add([
  8719. 'var c: char;',
  8720. 'begin',
  8721. ' for c:=''a'' to ''c'' do ;',
  8722. ' for c:=c downto ''a'' do ;',
  8723. ' for c:=''Б'' to ''Я'' do ;',
  8724. '']);
  8725. ConvertProgram;
  8726. CheckSource('TestForCharDo',
  8727. LinesToStr([ // statements
  8728. 'this.c = "";']),
  8729. LinesToStr([ // this.$main
  8730. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8731. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8732. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8733. '']));
  8734. end;
  8735. procedure TTestModule.TestForCharInDo;
  8736. begin
  8737. StartProgram(false);
  8738. Add([
  8739. 'type',
  8740. ' TSetOfChar = set of char;',
  8741. ' TCharRg = ''a''..''z'';',
  8742. ' TSetOfCharRg = set of TCharRg;',
  8743. 'const Foo = ''foo'';',
  8744. 'var',
  8745. ' c,c2: char;',
  8746. ' s: string;',
  8747. ' a1: array of char;',
  8748. ' a2: array[1..3] of char;',
  8749. ' soc: TSetOfChar;',
  8750. ' socr: TSetOfCharRg;',
  8751. ' cr: TCharRg;',
  8752. 'begin',
  8753. ' for c in foo do ;',
  8754. ' for c in s do ;',
  8755. ' for c in char do ;',
  8756. ' for c in a1 do ;',
  8757. ' for c in a2 do ;',
  8758. ' for c in [''1''..''3''] do ;',
  8759. ' for c in TSetOfChar do ;',
  8760. ' for c in TCharRg do ;',
  8761. ' for c in soc do c2:=c;',
  8762. ' for c in TSetOfCharRg do ;',
  8763. ' for c in socr do ;',
  8764. ' for cr in TCharRg do ;',
  8765. ' for cr in TSetOfCharRg do ;',
  8766. ' for cr in socr do ;',
  8767. '']);
  8768. ConvertProgram;
  8769. CheckSource('TestForCharInDo',
  8770. LinesToStr([ // statements
  8771. 'this.Foo = "foo";',
  8772. 'this.c = "";',
  8773. 'this.c2 = "";',
  8774. 'this.s = "";',
  8775. 'this.a1 = [];',
  8776. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8777. 'this.soc = {};',
  8778. 'this.socr = {};',
  8779. 'this.cr = "a";',
  8780. '']),
  8781. LinesToStr([ // this.$main
  8782. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8783. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8784. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8785. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8786. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8787. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8788. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8789. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8790. 'for (var $l8 in $mod.soc) {',
  8791. ' $mod.c = String.fromCharCode($l8);',
  8792. ' $mod.c2 = $mod.c;',
  8793. '};',
  8794. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8795. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8796. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8797. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8798. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8799. '']));
  8800. end;
  8801. procedure TTestModule.TestProcTwoArgs;
  8802. begin
  8803. StartProgram(false);
  8804. Add('procedure Test(a,b: longint);');
  8805. Add('begin');
  8806. Add('end;');
  8807. Add('begin');
  8808. ConvertProgram;
  8809. CheckSource('TestProcTwoArgs',
  8810. LinesToStr([ // statements
  8811. 'this.Test = function (a,b) {',
  8812. '};'
  8813. ]),
  8814. LinesToStr([ // this.$main
  8815. ''
  8816. ]));
  8817. end;
  8818. procedure TTestModule.TestProc_DefaultValue;
  8819. begin
  8820. StartProgram(false);
  8821. Add('procedure p1(i: longint = 1);');
  8822. Add('begin');
  8823. Add('end;');
  8824. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8825. Add('begin');
  8826. Add('end;');
  8827. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8828. Add('begin');
  8829. Add('end;');
  8830. Add('begin');
  8831. Add(' p1;');
  8832. Add(' p1();');
  8833. Add(' p1(11);');
  8834. Add(' p2;');
  8835. Add(' p2();');
  8836. Add(' p2(12);');
  8837. Add(' p2(13,''b'');');
  8838. Add(' p3();');
  8839. ConvertProgram;
  8840. CheckSource('TestProc_DefaultValue',
  8841. LinesToStr([ // statements
  8842. 'this.p1 = function (i) {',
  8843. '};',
  8844. 'this.p2 = function (i,c) {',
  8845. '};',
  8846. 'this.p3 = function (d,b,s) {',
  8847. '};'
  8848. ]),
  8849. LinesToStr([ // this.$main
  8850. ' $mod.p1(1);',
  8851. ' $mod.p1(1);',
  8852. ' $mod.p1(11);',
  8853. ' $mod.p2(1,"a");',
  8854. ' $mod.p2(1,"a");',
  8855. ' $mod.p2(12,"a");',
  8856. ' $mod.p2(13,"b");',
  8857. ' $mod.p3(1.0,false,"abc");'
  8858. ]));
  8859. end;
  8860. procedure TTestModule.TestFunctionInt;
  8861. begin
  8862. StartProgram(false);
  8863. Add('function MyTest(Bar: longint): longint;');
  8864. Add('begin');
  8865. Add(' Result:=2*bar');
  8866. Add('end;');
  8867. Add('begin');
  8868. ConvertProgram;
  8869. CheckSource('TestFunctionInt',
  8870. LinesToStr([ // statements
  8871. 'this.MyTest = function (Bar) {',
  8872. ' var Result = 0;',
  8873. ' Result = 2*Bar;',
  8874. ' return Result;',
  8875. '};'
  8876. ]),
  8877. LinesToStr([ // this.$main
  8878. ''
  8879. ]));
  8880. end;
  8881. procedure TTestModule.TestFunctionString;
  8882. begin
  8883. StartProgram(false);
  8884. Add('function Test(Bar: string): string;');
  8885. Add('begin');
  8886. Add(' Result:=bar+BAR');
  8887. Add('end;');
  8888. Add('begin');
  8889. ConvertProgram;
  8890. CheckSource('TestFunctionString',
  8891. LinesToStr([ // statements
  8892. 'this.Test = function (Bar) {',
  8893. ' var Result = "";',
  8894. ' Result = Bar+Bar;',
  8895. ' return Result;',
  8896. '};'
  8897. ]),
  8898. LinesToStr([ // this.$main
  8899. ''
  8900. ]));
  8901. end;
  8902. procedure TTestModule.TestIfThen;
  8903. begin
  8904. StartProgram(false);
  8905. Add([
  8906. 'var b: boolean;',
  8907. 'begin',
  8908. ' if b then ;',
  8909. ' if b then else ;']);
  8910. ConvertProgram;
  8911. CheckSource('TestIfThen',
  8912. LinesToStr([ // statements
  8913. 'this.b = false;',
  8914. '']),
  8915. LinesToStr([ // this.$main
  8916. 'if ($mod.b) ;',
  8917. 'if ($mod.b) ;',
  8918. '']));
  8919. end;
  8920. procedure TTestModule.TestForLoop;
  8921. begin
  8922. StartProgram(false);
  8923. Add('var');
  8924. Add(' vI, vJ, vN: longint;');
  8925. Add('begin');
  8926. Add(' VJ:=0;');
  8927. Add(' VN:=3;');
  8928. Add(' for VI:=1 to VN do');
  8929. Add(' begin');
  8930. Add(' VJ:=VJ+VI;');
  8931. Add(' end;');
  8932. ConvertProgram;
  8933. CheckSource('TestForLoop',
  8934. LinesToStr([ // statements
  8935. 'this.vI = 0;',
  8936. 'this.vJ = 0;',
  8937. 'this.vN = 0;'
  8938. ]),
  8939. LinesToStr([ // this.$main
  8940. ' $mod.vJ = 0;',
  8941. ' $mod.vN = 3;',
  8942. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8943. ' $mod.vI = $l;',
  8944. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8945. ' };',
  8946. '']));
  8947. end;
  8948. procedure TTestModule.TestForLoopInsideFunction;
  8949. begin
  8950. StartProgram(false);
  8951. Add('function SumNumbers(Count: longint): longint;');
  8952. Add('var');
  8953. Add(' vI, vJ: longint;');
  8954. Add('begin');
  8955. Add(' vj:=0;');
  8956. Add(' for vi:=1 to count do');
  8957. Add(' begin');
  8958. Add(' vj:=vj+vi;');
  8959. Add(' end;');
  8960. Add('end;');
  8961. Add('begin');
  8962. Add(' sumnumbers(3);');
  8963. ConvertProgram;
  8964. CheckSource('TestForLoopInsideFunction',
  8965. LinesToStr([ // statements
  8966. 'this.SumNumbers = function (Count) {',
  8967. ' var Result = 0;',
  8968. ' var vI = 0;',
  8969. ' var vJ = 0;',
  8970. ' vJ = 0;',
  8971. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8972. ' vI = $l;',
  8973. ' vJ = vJ + vI;',
  8974. ' };',
  8975. ' return Result;',
  8976. '};'
  8977. ]),
  8978. LinesToStr([ // $mod.$main
  8979. ' $mod.SumNumbers(3);'
  8980. ]));
  8981. end;
  8982. procedure TTestModule.TestForLoop_ReadVarAfter;
  8983. begin
  8984. StartProgram(false);
  8985. Add('var');
  8986. Add(' vI: longint;');
  8987. Add('begin');
  8988. Add(' for vi:=1 to 2 do ;');
  8989. Add(' if vi=3 then ;');
  8990. ConvertProgram;
  8991. CheckSource('TestForLoop',
  8992. LinesToStr([ // statements
  8993. 'this.vI = 0;'
  8994. ]),
  8995. LinesToStr([ // this.$main
  8996. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8997. ' if ($mod.vI===3) ;'
  8998. ]));
  8999. end;
  9000. procedure TTestModule.TestForLoop_Nested;
  9001. begin
  9002. StartProgram(false);
  9003. Add('function SumNumbers(Count: longint): longint;');
  9004. Add('var');
  9005. Add(' vI, vJ, vK: longint;');
  9006. Add('begin');
  9007. Add(' VK:=0;');
  9008. Add(' for VI:=1 to count do');
  9009. Add(' begin');
  9010. Add(' for vj:=1 to vi do');
  9011. Add(' begin');
  9012. Add(' vk:=VK+VI;');
  9013. Add(' end;');
  9014. Add(' end;');
  9015. Add('end;');
  9016. Add('begin');
  9017. Add(' sumnumbers(3);');
  9018. ConvertProgram;
  9019. CheckSource('TestForLoopInFunction',
  9020. LinesToStr([ // statements
  9021. 'this.SumNumbers = function (Count) {',
  9022. ' var Result = 0;',
  9023. ' var vI = 0;',
  9024. ' var vJ = 0;',
  9025. ' var vK = 0;',
  9026. ' vK = 0;',
  9027. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9028. ' vI = $l;',
  9029. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9030. ' vJ = $l1;',
  9031. ' vK = vK + vI;',
  9032. ' };',
  9033. ' };',
  9034. ' return Result;',
  9035. '};'
  9036. ]),
  9037. LinesToStr([ // $mod.$main
  9038. ' $mod.SumNumbers(3);'
  9039. ]));
  9040. end;
  9041. procedure TTestModule.TestRepeatUntil;
  9042. begin
  9043. StartProgram(false);
  9044. Add('var');
  9045. Add(' vI, vJ, vN: longint;');
  9046. Add('begin');
  9047. Add(' vn:=3;');
  9048. Add(' vj:=0;');
  9049. Add(' VI:=0;');
  9050. Add(' repeat');
  9051. Add(' VI:=vi+1;');
  9052. Add(' vj:=VJ+vI;');
  9053. Add(' until vi>=vn');
  9054. ConvertProgram;
  9055. CheckSource('TestRepeatUntil',
  9056. LinesToStr([ // statements
  9057. 'this.vI = 0;',
  9058. 'this.vJ = 0;',
  9059. 'this.vN = 0;'
  9060. ]),
  9061. LinesToStr([ // $mod.$main
  9062. ' $mod.vN = 3;',
  9063. ' $mod.vJ = 0;',
  9064. ' $mod.vI = 0;',
  9065. ' do{',
  9066. ' $mod.vI = $mod.vI + 1;',
  9067. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9068. ' }while(!($mod.vI>=$mod.vN));'
  9069. ]));
  9070. end;
  9071. procedure TTestModule.TestAsmBlock;
  9072. begin
  9073. StartProgram(false);
  9074. Add([
  9075. 'var',
  9076. ' vI: longint;',
  9077. 'begin',
  9078. ' vi:=1;',
  9079. ' asm',
  9080. ' if (vI===1) {',
  9081. ' vI=2;',
  9082. //' console.log(''end;'');', ToDo
  9083. ' }',
  9084. ' if (vI===2){ vI=3; }',
  9085. ' end;',
  9086. ' VI:=4;']);
  9087. ConvertProgram;
  9088. CheckSource('TestAsmBlock',
  9089. LinesToStr([ // statements
  9090. 'this.vI = 0;'
  9091. ]),
  9092. LinesToStr([ // $mod.$main
  9093. '$mod.vI = 1;',
  9094. 'if (vI===1) {',
  9095. ' vI=2;',
  9096. '}',
  9097. 'if (vI===2){ vI=3; }',
  9098. ';',
  9099. '$mod.vI = 4;'
  9100. ]));
  9101. end;
  9102. procedure TTestModule.TestAsmPas_Impl;
  9103. begin
  9104. StartUnit(false);
  9105. Add('interface');
  9106. Add('const cIntf: longint = 1;');
  9107. Add('var vIntf: longint;');
  9108. Add('implementation');
  9109. Add('const cImpl: longint = 2;');
  9110. Add('var vImpl: longint;');
  9111. Add('procedure DoIt;');
  9112. Add('const cLoc: longint = 3;');
  9113. Add('var vLoc: longint;');
  9114. Add('begin;');
  9115. Add(' asm');
  9116. //Add(' pas(vIntf)=pas(cIntf);');
  9117. //Add(' pas(vImpl)=pas(cImpl);');
  9118. //Add(' pas(vLoc)=pas(cLoc);');
  9119. Add(' end;');
  9120. Add('end;');
  9121. ConvertUnit;
  9122. CheckSource('TestAsmPas_Impl',
  9123. LinesToStr([
  9124. 'var $impl = $mod.$impl;',
  9125. 'this.cIntf = 1;',
  9126. 'this.vIntf = 0;',
  9127. '']),
  9128. '', // this.$init
  9129. LinesToStr([ // implementation
  9130. '$impl.cImpl = 2;',
  9131. '$impl.vImpl = 0;',
  9132. 'var cLoc = 3;',
  9133. '$impl.DoIt = function () {',
  9134. ' var vLoc = 0;',
  9135. '};',
  9136. '']) );
  9137. end;
  9138. procedure TTestModule.TestTryFinally;
  9139. begin
  9140. StartProgram(false);
  9141. Add('var i: longint;');
  9142. Add('begin');
  9143. Add(' try');
  9144. Add(' i:=0; i:=2 div i;');
  9145. Add(' finally');
  9146. Add(' i:=3');
  9147. Add(' end;');
  9148. ConvertProgram;
  9149. CheckSource('TestTryFinally',
  9150. LinesToStr([ // statements
  9151. 'this.i = 0;'
  9152. ]),
  9153. LinesToStr([ // $mod.$main
  9154. 'try {',
  9155. ' $mod.i = 0;',
  9156. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9157. '} finally {',
  9158. ' $mod.i = 3;',
  9159. '};'
  9160. ]));
  9161. end;
  9162. procedure TTestModule.TestTryExcept;
  9163. begin
  9164. StartProgram(false);
  9165. Add([
  9166. 'type',
  9167. ' TObject = class end;',
  9168. ' Exception = class Msg: string; end;',
  9169. ' EInvalidCast = class(Exception) end;',
  9170. 'var vI: longint;',
  9171. 'begin',
  9172. ' try',
  9173. ' vi:=1;',
  9174. ' except',
  9175. ' vi:=2',
  9176. ' end;',
  9177. ' try',
  9178. ' vi:=3;',
  9179. ' except',
  9180. ' raise;',
  9181. ' end;',
  9182. ' try',
  9183. ' VI:=4;',
  9184. ' except',
  9185. ' on einvalidcast do',
  9186. ' raise;',
  9187. ' on E: exception do',
  9188. ' if e.msg='''' then',
  9189. ' raise e;',
  9190. ' else',
  9191. ' vi:=5',
  9192. ' end;',
  9193. ' try',
  9194. ' VI:=6;',
  9195. ' except',
  9196. ' on einvalidcast do ;',
  9197. ' end;',
  9198. '']);
  9199. ConvertProgram;
  9200. CheckSource('TestTryExcept',
  9201. LinesToStr([ // statements
  9202. 'rtl.createClass(this, "TObject", null, function () {',
  9203. ' this.$init = function () {',
  9204. ' };',
  9205. ' this.$final = function () {',
  9206. ' };',
  9207. '});',
  9208. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9209. ' this.$init = function () {',
  9210. ' $mod.TObject.$init.call(this);',
  9211. ' this.Msg = "";',
  9212. ' };',
  9213. '});',
  9214. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9215. '});',
  9216. 'this.vI = 0;'
  9217. ]),
  9218. LinesToStr([ // $mod.$main
  9219. 'try {',
  9220. ' $mod.vI = 1;',
  9221. '} catch ($e) {',
  9222. ' $mod.vI = 2;',
  9223. '};',
  9224. 'try {',
  9225. ' $mod.vI = 3;',
  9226. '} catch ($e) {',
  9227. ' throw $e;',
  9228. '};',
  9229. 'try {',
  9230. ' $mod.vI = 4;',
  9231. '} catch ($e) {',
  9232. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9233. ' throw $e',
  9234. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9235. ' var E = $e;',
  9236. ' if (E.Msg === "") throw E;',
  9237. ' } else {',
  9238. ' $mod.vI = 5;',
  9239. ' }',
  9240. '};',
  9241. 'try {',
  9242. ' $mod.vI = 6;',
  9243. '} catch ($e) {',
  9244. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9245. ' } else throw $e',
  9246. '};',
  9247. '']));
  9248. end;
  9249. procedure TTestModule.TestTryExcept_ReservedWords;
  9250. begin
  9251. StartProgram(false);
  9252. Add([
  9253. 'type',
  9254. ' TObject = class end;',
  9255. ' Exception = class',
  9256. ' Symbol: string;',
  9257. ' end;',
  9258. 'var &try: longint;',
  9259. 'begin',
  9260. ' try',
  9261. ' &try:=4;',
  9262. ' except',
  9263. ' on Error: exception do',
  9264. ' if errOR.symBol='''' then',
  9265. ' raise ERRor;',
  9266. ' end;',
  9267. '']);
  9268. ConvertProgram;
  9269. CheckSource('TestTryExcept_ReservedWords',
  9270. LinesToStr([ // statements
  9271. 'rtl.createClass(this, "TObject", null, function () {',
  9272. ' this.$init = function () {',
  9273. ' };',
  9274. ' this.$final = function () {',
  9275. ' };',
  9276. '});',
  9277. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9278. ' this.$init = function () {',
  9279. ' $mod.TObject.$init.call(this);',
  9280. ' this.Symbol = "";',
  9281. ' };',
  9282. '});',
  9283. 'this.Try = 0;',
  9284. '']),
  9285. LinesToStr([ // $mod.$main
  9286. 'try {',
  9287. ' $mod.Try = 4;',
  9288. '} catch ($e) {',
  9289. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9290. ' var error = $e;',
  9291. ' if (error.Symbol === "") throw error;',
  9292. ' } else throw $e',
  9293. '};',
  9294. '']));
  9295. end;
  9296. procedure TTestModule.TestIfThenRaiseElse;
  9297. begin
  9298. StartProgram(false);
  9299. Add([
  9300. 'type',
  9301. ' TObject = class',
  9302. ' constructor Create;',
  9303. ' end;',
  9304. 'constructor TObject.Create;',
  9305. 'begin',
  9306. 'end;',
  9307. 'var b: boolean;',
  9308. 'begin',
  9309. ' if b then',
  9310. ' raise TObject.Create',
  9311. ' else',
  9312. ' b:=false;',
  9313. '']);
  9314. ConvertProgram;
  9315. CheckSource('TestIfThenRaiseElse',
  9316. LinesToStr([ // statements
  9317. 'rtl.createClass(this, "TObject", null, function () {',
  9318. ' this.$init = function () {',
  9319. ' };',
  9320. ' this.$final = function () {',
  9321. ' };',
  9322. ' this.Create = function () {',
  9323. ' return this;',
  9324. ' };',
  9325. '});',
  9326. 'this.b = false;',
  9327. '']),
  9328. LinesToStr([ // $mod.$main
  9329. 'if ($mod.b) {',
  9330. ' throw $mod.TObject.$create("Create")}',
  9331. ' else $mod.b = false;',
  9332. '']));
  9333. end;
  9334. procedure TTestModule.TestCaseOf;
  9335. begin
  9336. StartProgram(false);
  9337. Add([
  9338. 'const e: longint; external name ''$e'';',
  9339. 'var vI: longint;',
  9340. 'begin',
  9341. ' case vi of',
  9342. ' 1: ;',
  9343. ' 2: vi:=3;',
  9344. ' e: ;',
  9345. ' else',
  9346. ' VI:=4',
  9347. ' end;']);
  9348. ConvertProgram;
  9349. CheckSource('TestCaseOf',
  9350. LinesToStr([ // statements
  9351. 'this.vI = 0;'
  9352. ]),
  9353. LinesToStr([ // $mod.$main
  9354. 'var $tmp = $mod.vI;',
  9355. 'if ($tmp === 1) {}',
  9356. 'else if ($tmp === 2) {',
  9357. ' $mod.vI = 3}',
  9358. ' else if ($tmp === $e) {}',
  9359. 'else {',
  9360. ' $mod.vI = 4;',
  9361. '};'
  9362. ]));
  9363. end;
  9364. procedure TTestModule.TestCaseOf_UseSwitch;
  9365. begin
  9366. StartProgram(false);
  9367. Converter.UseSwitchStatement:=true;
  9368. Add('var Vi: longint;');
  9369. Add('begin');
  9370. Add(' case vi of');
  9371. Add(' 1: ;');
  9372. Add(' 2: VI:=3;');
  9373. Add(' else');
  9374. Add(' vi:=4');
  9375. Add(' end;');
  9376. ConvertProgram;
  9377. CheckSource('TestCaseOf_UseSwitch',
  9378. LinesToStr([ // statements
  9379. 'this.Vi = 0;'
  9380. ]),
  9381. LinesToStr([ // $mod.$main
  9382. 'switch ($mod.Vi) {',
  9383. 'case 1:',
  9384. ' break;',
  9385. 'case 2:',
  9386. ' $mod.Vi = 3;',
  9387. ' break;',
  9388. 'default:',
  9389. ' $mod.Vi = 4;',
  9390. '};'
  9391. ]));
  9392. end;
  9393. procedure TTestModule.TestCaseOfNoElse;
  9394. begin
  9395. StartProgram(false);
  9396. Add('var Vi: longint;');
  9397. Add('begin');
  9398. Add(' case vi of');
  9399. Add(' 1: begin vi:=2; VI:=3; end;');
  9400. Add(' end;');
  9401. ConvertProgram;
  9402. CheckSource('TestCaseOfNoElse',
  9403. LinesToStr([ // statements
  9404. 'this.Vi = 0;'
  9405. ]),
  9406. LinesToStr([ // $mod.$main
  9407. 'var $tmp = $mod.Vi;',
  9408. 'if ($tmp === 1) {',
  9409. ' $mod.Vi = 2;',
  9410. ' $mod.Vi = 3;',
  9411. '};'
  9412. ]));
  9413. end;
  9414. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9415. begin
  9416. StartProgram(false);
  9417. Converter.UseSwitchStatement:=true;
  9418. Add('var vI: longint;');
  9419. Add('begin');
  9420. Add(' case vi of');
  9421. Add(' 1: begin VI:=2; vi:=3; end;');
  9422. Add(' end;');
  9423. ConvertProgram;
  9424. CheckSource('TestCaseOfNoElse_UseSwitch',
  9425. LinesToStr([ // statements
  9426. 'this.vI = 0;'
  9427. ]),
  9428. LinesToStr([ // $mod.$main
  9429. 'switch ($mod.vI) {',
  9430. 'case 1:',
  9431. ' $mod.vI = 2;',
  9432. ' $mod.vI = 3;',
  9433. ' break;',
  9434. '};'
  9435. ]));
  9436. end;
  9437. procedure TTestModule.TestCaseOfRange;
  9438. begin
  9439. StartProgram(false);
  9440. Add('var vI: longint;');
  9441. Add('begin');
  9442. Add(' case vi of');
  9443. Add(' 1..3: vi:=14;');
  9444. Add(' 4,5: vi:=16;');
  9445. Add(' 6..7,9..10: ;');
  9446. Add(' else ;');
  9447. Add(' end;');
  9448. ConvertProgram;
  9449. CheckSource('TestCaseOfRange',
  9450. LinesToStr([ // statements
  9451. 'this.vI = 0;'
  9452. ]),
  9453. LinesToStr([ // $mod.$main
  9454. 'var $tmp = $mod.vI;',
  9455. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9456. ' $mod.vI = 14',
  9457. '} else if (($tmp === 4) || ($tmp === 5)){',
  9458. ' $mod.vI = 16',
  9459. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9460. ]));
  9461. end;
  9462. procedure TTestModule.TestCaseOfString;
  9463. begin
  9464. StartProgram(false);
  9465. Add([
  9466. 'var s,h: string;',
  9467. 'begin',
  9468. ' case s of',
  9469. ' ''foo'': s:=h;',
  9470. ' ''a''..''z'': h:=s;',
  9471. ' ''ў'', ''ё'': ;',
  9472. ' ''Б''..''Я'': ;',
  9473. ' end;',
  9474. '']);
  9475. ConvertProgram;
  9476. CheckSource('TestCaseOfString',
  9477. LinesToStr([ // statements
  9478. 'this.s = "";',
  9479. 'this.h = "";',
  9480. '']),
  9481. LinesToStr([ // $mod.$main
  9482. 'var $tmp = $mod.s;',
  9483. 'if ($tmp === "foo") {',
  9484. ' $mod.s = $mod.h}',
  9485. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9486. ' $mod.h = $mod.s}',
  9487. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9488. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9489. '']));
  9490. end;
  9491. procedure TTestModule.TestCaseOfChar;
  9492. begin
  9493. StartProgram(false);
  9494. Add([
  9495. 'var s,h: char;',
  9496. 'begin',
  9497. ' case s of',
  9498. ' ''a''..''z'': h:=s;',
  9499. ' ''ä'': ;',
  9500. ' ''ў'', ''ё'': ;',
  9501. ' ''Б''..''Я'': ;',
  9502. ' end;',
  9503. '']);
  9504. ConvertProgram;
  9505. CheckSource('TestCaseOfString',
  9506. LinesToStr([ // statements
  9507. 'this.s = "";',
  9508. 'this.h = "";',
  9509. '']),
  9510. LinesToStr([ // $mod.$main
  9511. 'var $tmp = $mod.s;',
  9512. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9513. ' $mod.h = $mod.s}',
  9514. ' else if ($tmp === "ä") {}',
  9515. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9516. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9517. '']));
  9518. end;
  9519. procedure TTestModule.TestCaseOfExternalClassConst;
  9520. begin
  9521. StartProgram(false);
  9522. Add([
  9523. '{$modeswitch externalclass}',
  9524. 'type',
  9525. ' TBird = class external name ''Bird''',
  9526. ' const e: longint;',
  9527. ' end;',
  9528. 'var vI: longint;',
  9529. 'begin',
  9530. ' case vi of',
  9531. ' 1: vi:=3;',
  9532. ' TBird.e: ;',
  9533. ' end;']);
  9534. ConvertProgram;
  9535. CheckSource('TestCaseOfExternalClassConst',
  9536. LinesToStr([ // statements
  9537. 'this.vI = 0;'
  9538. ]),
  9539. LinesToStr([ // $mod.$main
  9540. 'var $tmp = $mod.vI;',
  9541. 'if ($tmp === 1) {',
  9542. ' $mod.vI = 3}',
  9543. ' else if ($tmp === Bird.e) ;'
  9544. ]));
  9545. end;
  9546. procedure TTestModule.TestDebugger;
  9547. begin
  9548. StartProgram(false);
  9549. Add([
  9550. 'procedure DoIt;',
  9551. 'begin',
  9552. ' deBugger;',
  9553. ' DeBugger();',
  9554. 'end;',
  9555. 'begin',
  9556. ' Debugger;']);
  9557. ConvertProgram;
  9558. CheckSource('TestDebugger',
  9559. LinesToStr([ // statements
  9560. 'this.DoIt = function () {',
  9561. ' debugger;',
  9562. ' debugger;',
  9563. '};',
  9564. '']),
  9565. LinesToStr([ // $mod.$main
  9566. 'debugger;',
  9567. '']));
  9568. end;
  9569. procedure TTestModule.TestArray_Dynamic;
  9570. begin
  9571. StartProgram(false);
  9572. Add([
  9573. 'type',
  9574. ' TArrayInt = array of longint;',
  9575. 'var',
  9576. ' Arr: TArrayInt;',
  9577. ' i: longint;',
  9578. ' b: boolean;',
  9579. 'begin',
  9580. ' SetLength(arr,3);',
  9581. ' arr[0]:=4;',
  9582. ' arr[1]:=length(arr)+arr[0];',
  9583. ' arr[i]:=5;',
  9584. ' arr[arr[i]]:=arr[6];',
  9585. ' i:=low(arr);',
  9586. ' i:=high(arr);',
  9587. ' b:=Assigned(arr);',
  9588. ' Arr:=default(TArrayInt);']);
  9589. ConvertProgram;
  9590. CheckSource('TestArray_Dynamic',
  9591. LinesToStr([ // statements
  9592. 'this.Arr = [];',
  9593. 'this.i = 0;',
  9594. 'this.b = false;'
  9595. ]),
  9596. LinesToStr([ // $mod.$main
  9597. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9598. '$mod.Arr[0] = 4;',
  9599. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9600. '$mod.Arr[$mod.i] = 5;',
  9601. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9602. '$mod.i = 0;',
  9603. '$mod.i = rtl.length($mod.Arr) - 1;',
  9604. '$mod.b = rtl.length($mod.Arr) > 0;',
  9605. '$mod.Arr = [];',
  9606. '']));
  9607. end;
  9608. procedure TTestModule.TestArray_Dynamic_Nil;
  9609. begin
  9610. StartProgram(false);
  9611. Add('type');
  9612. Add(' TArrayInt = array of longint;');
  9613. Add('var');
  9614. Add(' Arr: TArrayInt;');
  9615. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9616. Add('begin');
  9617. Add(' arr:=nil;');
  9618. Add(' if arr=nil then;');
  9619. Add(' if nil=arr then;');
  9620. Add(' if arr<>nil then;');
  9621. Add(' if nil<>arr then;');
  9622. Add(' DoIt(nil,nil);');
  9623. ConvertProgram;
  9624. CheckSource('TestArray_Dynamic',
  9625. LinesToStr([ // statements
  9626. 'this.Arr = [];',
  9627. 'this.DoIt = function(i,j){',
  9628. '};'
  9629. ]),
  9630. LinesToStr([ // $mod.$main
  9631. '$mod.Arr = [];',
  9632. 'if (rtl.length($mod.Arr) === 0) ;',
  9633. 'if (rtl.length($mod.Arr) === 0) ;',
  9634. 'if (rtl.length($mod.Arr) > 0) ;',
  9635. 'if (rtl.length($mod.Arr) > 0) ;',
  9636. '$mod.DoIt([],[]);',
  9637. '']));
  9638. end;
  9639. procedure TTestModule.TestArray_DynMultiDimensional;
  9640. begin
  9641. StartProgram(false);
  9642. Add([
  9643. 'type',
  9644. ' TArrayInt = array of longint;',
  9645. ' TArrayArrayInt = array of TArrayInt;',
  9646. 'var',
  9647. ' Arr: TArrayInt;',
  9648. ' Arr2: TArrayArrayInt;',
  9649. ' i: longint;',
  9650. 'begin',
  9651. ' arr2:=nil;',
  9652. ' if arr2=nil then;',
  9653. ' if nil=arr2 then;',
  9654. ' i:=low(arr2);',
  9655. ' i:=low(arr2[1]);',
  9656. ' i:=high(arr2);',
  9657. ' i:=high(arr2[2]);',
  9658. ' arr2[3]:=arr;',
  9659. ' arr2[4][5]:=i;',
  9660. ' i:=arr2[6][7];',
  9661. ' arr2[8,9]:=i;',
  9662. ' i:=arr2[10,11];',
  9663. ' SetLength(arr2,14);',
  9664. ' SetLength(arr2[15],16);']);
  9665. ConvertProgram;
  9666. CheckSource('TestArray_Dynamic',
  9667. LinesToStr([ // statements
  9668. 'this.Arr = [];',
  9669. 'this.Arr2 = [];',
  9670. 'this.i = 0;'
  9671. ]),
  9672. LinesToStr([ // $mod.$main
  9673. '$mod.Arr2 = [];',
  9674. 'if (rtl.length($mod.Arr2) === 0) ;',
  9675. 'if (rtl.length($mod.Arr2) === 0) ;',
  9676. '$mod.i = 0;',
  9677. '$mod.i = 0;',
  9678. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9679. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9680. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9681. '$mod.Arr2[4][5] = $mod.i;',
  9682. '$mod.i = $mod.Arr2[6][7];',
  9683. '$mod.Arr2[8][9] = $mod.i;',
  9684. '$mod.i = $mod.Arr2[10][11];',
  9685. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9686. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9687. '']));
  9688. end;
  9689. procedure TTestModule.TestArray_DynamicAssign;
  9690. begin
  9691. StartProgram(false);
  9692. Add([
  9693. 'type',
  9694. ' TArrayInt = array of longint;',
  9695. ' TArrayArrayInt = array of TArrayInt;',
  9696. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9697. 'begin',
  9698. 'end;',
  9699. 'procedure Fly(var a: TArrayInt);',
  9700. 'begin',
  9701. 'end;',
  9702. 'var',
  9703. ' Arr: TArrayInt;',
  9704. ' Arr2: TArrayArrayInt;',
  9705. 'begin',
  9706. ' arr:=nil;',
  9707. ' arr2:=nil;',
  9708. ' arr2[1]:=nil;',
  9709. ' arr2[2]:=arr;',
  9710. ' Run(arr,arr,arr);',
  9711. ' Fly(arr);',
  9712. ' Run(arr2[4],arr2[5],arr2[6]);',
  9713. ' Fly(arr2[7]);',
  9714. '']);
  9715. ConvertProgram;
  9716. CheckSource('TestArray_DynamicAssign',
  9717. LinesToStr([ // statements
  9718. 'this.Run = function (a, b, c) {',
  9719. '};',
  9720. 'this.Fly = function (a) {',
  9721. '};',
  9722. 'this.Arr = [];',
  9723. 'this.Arr2 = [];',
  9724. '']),
  9725. LinesToStr([ // $mod.$main
  9726. '$mod.Arr = [];',
  9727. '$mod.Arr2 = [];',
  9728. '$mod.Arr2[1] = [];',
  9729. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9730. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9731. '$mod.Fly({',
  9732. ' p: $mod,',
  9733. ' get: function () {',
  9734. ' return this.p.Arr;',
  9735. ' },',
  9736. ' set: function (v) {',
  9737. ' this.p.Arr = v;',
  9738. ' }',
  9739. '});',
  9740. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9741. '$mod.Fly({',
  9742. ' a: 7,',
  9743. ' p: $mod.Arr2,',
  9744. ' get: function () {',
  9745. ' return this.p[this.a];',
  9746. ' },',
  9747. ' set: function (v) {',
  9748. ' this.p[this.a] = v;',
  9749. ' }',
  9750. '});',
  9751. '']));
  9752. end;
  9753. procedure TTestModule.TestArray_StaticInt;
  9754. begin
  9755. StartProgram(false);
  9756. Add('type');
  9757. Add(' TArrayInt = array[2..4] of longint;');
  9758. Add('var');
  9759. Add(' Arr: TArrayInt;');
  9760. Add(' Arr2: TArrayInt = (5,6,7);');
  9761. Add(' i: longint;');
  9762. Add(' b: boolean;');
  9763. Add('begin');
  9764. Add(' arr[2]:=4;');
  9765. Add(' arr[3]:=arr[2]+arr[3];');
  9766. Add(' arr[i]:=5;');
  9767. Add(' arr[arr[i]]:=arr[high(arr)];');
  9768. Add(' i:=low(arr);');
  9769. Add(' i:=high(arr);');
  9770. Add(' b:=arr[2]=arr[3];');
  9771. Add(' arr:=default(TArrayInt);');
  9772. ConvertProgram;
  9773. CheckSource('TestArray_StaticInt',
  9774. LinesToStr([ // statements
  9775. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9776. 'this.Arr2 = [5, 6, 7];',
  9777. 'this.i = 0;',
  9778. 'this.b = false;'
  9779. ]),
  9780. LinesToStr([ // $mod.$main
  9781. '$mod.Arr[0] = 4;',
  9782. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9783. '$mod.Arr[$mod.i-2] = 5;',
  9784. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9785. '$mod.i = 2;',
  9786. '$mod.i = 4;',
  9787. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9788. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9789. '']));
  9790. end;
  9791. procedure TTestModule.TestArray_StaticBool;
  9792. begin
  9793. StartProgram(false);
  9794. Add('type');
  9795. Add(' TBools = array[boolean] of boolean;');
  9796. Add(' TBool2 = array[true..true] of boolean;');
  9797. Add('var');
  9798. Add(' Arr: TBools;');
  9799. Add(' Arr2: TBool2;');
  9800. Add(' Arr3: TBools = (true,false);');
  9801. Add(' b: boolean;');
  9802. Add('begin');
  9803. Add(' b:=low(arr);');
  9804. Add(' b:=high(arr);');
  9805. Add(' arr[true]:=false;');
  9806. Add(' arr[false]:=arr[b] or arr[true];');
  9807. Add(' arr[b]:=true;');
  9808. Add(' arr[arr[b]]:=arr[high(arr)];');
  9809. Add(' b:=arr[false]=arr[true];');
  9810. Add(' b:=low(arr2);');
  9811. Add(' b:=high(arr2);');
  9812. Add(' arr2[true]:=true;');
  9813. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9814. Add(' arr2[b]:=false;');
  9815. ConvertProgram;
  9816. CheckSource('TestArray_StaticBool',
  9817. LinesToStr([ // statements
  9818. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9819. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9820. 'this.Arr3 = [true, false];',
  9821. 'this.b = false;'
  9822. ]),
  9823. LinesToStr([ // $mod.$main
  9824. '$mod.b = false;',
  9825. '$mod.b = true;',
  9826. '$mod.Arr[1] = false;',
  9827. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9828. '$mod.Arr[+$mod.b] = true;',
  9829. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9830. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9831. '$mod.b = true;',
  9832. '$mod.b = true;',
  9833. '$mod.Arr2[0] = true;',
  9834. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9835. '$mod.Arr2[1-$mod.b] = false;',
  9836. '']));
  9837. end;
  9838. procedure TTestModule.TestArray_StaticChar;
  9839. begin
  9840. StartProgram(false);
  9841. Add([
  9842. 'type',
  9843. ' TChars = array[char] of char;',
  9844. ' TChars2 = array[''a''..''z''] of char;',
  9845. 'var',
  9846. ' Arr: TChars;',
  9847. ' Arr2: TChars2;',
  9848. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9849. ' Arr4: array[11..13] of char = ''pas'';',
  9850. ' Arr5: array[21..22] of char = ''äö'';',
  9851. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9852. ' c: char;',
  9853. ' b: boolean;',
  9854. 'begin',
  9855. ' c:=low(arr);',
  9856. ' c:=high(arr);',
  9857. ' arr[''B'']:=''a'';',
  9858. ' arr[''D'']:=arr[c];',
  9859. ' arr[c]:=arr[''d''];',
  9860. ' arr[arr[c]]:=arr[high(arr)];',
  9861. ' b:=arr[low(arr)]=arr[''e''];',
  9862. ' c:=low(arr2);',
  9863. ' c:=high(arr2);',
  9864. ' arr2[''b'']:=''f'';',
  9865. ' arr2[''a'']:=arr2[c];',
  9866. ' arr2[c]:=arr2[''g''];']);
  9867. ConvertProgram;
  9868. CheckSource('TestArray_StaticChar',
  9869. LinesToStr([ // statements
  9870. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9871. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9872. 'this.Arr3 = ["p", "a", "s"];',
  9873. 'this.Arr4 = ["p", "a", "s"];',
  9874. 'this.Arr5 = ["ä", "ö"];',
  9875. 'this.Arr6 = ["ä", "ö"];',
  9876. 'this.c = "";',
  9877. 'this.b = false;',
  9878. '']),
  9879. LinesToStr([ // $mod.$main
  9880. '$mod.c = "\x00";',
  9881. '$mod.c = "\uFFFF";',
  9882. '$mod.Arr[66] = "a";',
  9883. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9884. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9885. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9886. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9887. '$mod.c = "a";',
  9888. '$mod.c = "z";',
  9889. '$mod.Arr2[1] = "f";',
  9890. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9891. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9892. '']));
  9893. end;
  9894. procedure TTestModule.TestArray_StaticMultiDim;
  9895. begin
  9896. StartProgram(false);
  9897. Add([
  9898. 'type',
  9899. ' TArrayInt = array[1..3] of longint;',
  9900. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9901. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  9902. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  9903. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  9904. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  9905. 'var',
  9906. ' Arr: TArrayInt;',
  9907. ' Arr2: TArrayArrayInt;',
  9908. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9909. ' Arr4: TArrayArrayInt;',
  9910. ' ArrDim2: TArrayDim2Int;',
  9911. ' ArrDim3: TArrayDim3Int;',
  9912. ' ArrDim4: TArrayDim4Int;',
  9913. ' i: longint;',
  9914. 'begin',
  9915. ' i:=low(arr);',
  9916. ' i:=low(arr2);',
  9917. ' i:=low(arr2[5]);',
  9918. ' i:=high(arr);',
  9919. ' i:=high(arr2);',
  9920. ' i:=high(arr2[6]);',
  9921. ' arr2[5]:=arr;',
  9922. ' arr2[6][2]:=i;',
  9923. ' i:=arr2[6][3];',
  9924. ' arr2[6,3]:=i;',
  9925. ' i:=arr2[5,2];',
  9926. ' arr2:=arr2;',// clone multi dim static array
  9927. ' arr3:=arr3;',// clone anonymous multi dim static array
  9928. ' arr4:=arr4;',
  9929. ' Arr:=Arr;',
  9930. ' ArrDim2:=ArrDim2;',
  9931. ' ArrDim3:=ArrDim3;',
  9932. ' ArrDim4:=ArrDim4;',
  9933. '']);
  9934. ConvertProgram;
  9935. CheckSource('TestArray_StaticMultiDim',
  9936. LinesToStr([ // statements
  9937. 'this.TArrayArrayInt$clone = function (a) {',
  9938. ' var b = [];',
  9939. ' b.length = 2;',
  9940. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9941. ' return b;',
  9942. '};',
  9943. 'this.TArrayArrayArrayInt$clone = function (a) {',
  9944. ' var b = [];',
  9945. ' b.length = 2;',
  9946. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  9947. ' return b;',
  9948. '};',
  9949. 'this.TArrayDim2Int$clone = function (a) {',
  9950. ' var b = [];',
  9951. ' b.length = 2;',
  9952. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9953. ' return b;',
  9954. '};',
  9955. 'this.TArrayDim3Int$clone = function (a) {',
  9956. ' var b = [];',
  9957. ' b.length = 2;',
  9958. ' for (var c = 0; c < 2; c++) {',
  9959. ' var d = b[c] = [];',
  9960. ' d.length = 3;',
  9961. ' var e = a[c];',
  9962. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  9963. ' };',
  9964. ' return b;',
  9965. '};',
  9966. 'this.TArrayDim4Int$clone = function (a) {',
  9967. ' var b = [];',
  9968. ' b.length = 2;',
  9969. ' for (var c = 0; c < 2; c++) {',
  9970. ' var d = b[c] = [];',
  9971. ' d.length = 3;',
  9972. ' var e = a[c];',
  9973. ' for (var f = 0; f < 3; f++) {',
  9974. ' var g = d[f] = [];',
  9975. ' g.length = 4;',
  9976. ' var h = e[f];',
  9977. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  9978. ' };',
  9979. ' };',
  9980. ' return b;',
  9981. '};',
  9982. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9983. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9984. 'this.Arr3$a$clone = function (a) {',
  9985. ' var b = [];',
  9986. ' b.length = 2;',
  9987. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9988. ' return b;',
  9989. '};',
  9990. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9991. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  9992. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  9993. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  9994. 'this.ArrDim4 = rtl.arraySetLength(',
  9995. ' null,',
  9996. ' 0,',
  9997. ' 2,',
  9998. ' 3,',
  9999. ' 4,',
  10000. ' 5',
  10001. ');',
  10002. 'this.i = 0;'
  10003. ]),
  10004. LinesToStr([ // $mod.$main
  10005. '$mod.i = 1;',
  10006. '$mod.i = 5;',
  10007. '$mod.i = 1;',
  10008. '$mod.i = 3;',
  10009. '$mod.i = 6;',
  10010. '$mod.i = 3;',
  10011. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10012. '$mod.Arr2[1][1] = $mod.i;',
  10013. '$mod.i = $mod.Arr2[1][2];',
  10014. '$mod.Arr2[1][2] = $mod.i;',
  10015. '$mod.i = $mod.Arr2[0][1];',
  10016. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10017. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10018. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10019. '$mod.Arr = $mod.Arr.slice(0);',
  10020. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10021. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10022. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10023. '']));
  10024. end;
  10025. procedure TTestModule.TestArray_StaticInFunction;
  10026. begin
  10027. StartProgram(false);
  10028. Add([
  10029. 'const TArrayInt = 3;',
  10030. 'const TArrayArrayInt = 4;',
  10031. 'procedure DoIt;',
  10032. 'type',
  10033. ' TArrayInt = array[1..3] of longint;',
  10034. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10035. 'var',
  10036. ' Arr: TArrayInt;',
  10037. ' Arr2: TArrayArrayInt;',
  10038. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10039. ' i: longint;',
  10040. 'begin',
  10041. ' arr2[5]:=arr;',
  10042. ' arr2:=arr2;',// clone multi dim static array
  10043. ' arr3:=arr3;',// clone multi dim anonymous static array
  10044. 'end;',
  10045. 'begin',
  10046. '']);
  10047. ConvertProgram;
  10048. CheckSource('TestArray_StaticInFunction',
  10049. LinesToStr([ // statements
  10050. 'this.TArrayInt = 3;',
  10051. 'this.TArrayArrayInt = 4;',
  10052. 'var TArrayArrayInt$1$clone = function (a) {',
  10053. ' var b = [];',
  10054. ' b.length = 2;',
  10055. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10056. ' return b;',
  10057. '};',
  10058. 'var Arr3$a$clone = function (a) {',
  10059. ' var b = [];',
  10060. ' b.length = 2;',
  10061. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10062. ' return b;',
  10063. '};',
  10064. 'this.DoIt = function () {',
  10065. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10066. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10067. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10068. ' var i = 0;',
  10069. ' Arr2[0] = Arr.slice(0);',
  10070. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10071. ' Arr3 = Arr3$a$clone(Arr3);',
  10072. '};',
  10073. '']),
  10074. LinesToStr([ // $mod.$main
  10075. '']));
  10076. end;
  10077. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10078. begin
  10079. StartProgram(false);
  10080. Add([
  10081. 'type',
  10082. ' TArrayInt = array[1..3,1..2] of longint;',
  10083. 'var',
  10084. ' a,b: TArrayInt;',
  10085. 'begin',
  10086. ' if a=b then ;',
  10087. '']);
  10088. SetExpectedPasResolverError('compare static array is not supported',
  10089. nXIsNotSupported);
  10090. ConvertProgram;
  10091. end;
  10092. procedure TTestModule.TestArrayOfRecord;
  10093. begin
  10094. StartProgram(false);
  10095. Add([
  10096. 'type',
  10097. ' TRec = record',
  10098. ' Int: longint;',
  10099. ' end;',
  10100. ' TArrayRec = array of TRec;',
  10101. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10102. 'begin',
  10103. 'end;',
  10104. 'var',
  10105. ' Arr: TArrayRec;',
  10106. ' r: TRec;',
  10107. ' i: longint;',
  10108. 'begin',
  10109. ' SetLength(arr,3);',
  10110. ' arr[0].int:=4;',
  10111. ' arr[1].int:=length(arr)+arr[2].int;',
  10112. ' arr[arr[i].int].int:=arr[5].int;',
  10113. ' arr[7]:=r;',
  10114. ' r:=arr[8];',
  10115. ' i:=low(arr);',
  10116. ' i:=high(arr);',
  10117. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10118. ConvertProgram;
  10119. CheckSource('TestArrayOfRecord',
  10120. LinesToStr([ // statements
  10121. 'rtl.recNewT(this, "TRec", function () {',
  10122. ' this.Int = 0;',
  10123. ' this.$eq = function (b) {',
  10124. ' return this.Int === b.Int;',
  10125. ' };',
  10126. ' this.$assign = function (s) {',
  10127. ' this.Int = s.Int;',
  10128. ' return this;',
  10129. ' };',
  10130. '});',
  10131. 'this.DoIt = function (vd, vc, vv) {',
  10132. '};',
  10133. 'this.Arr = [];',
  10134. 'this.r = this.TRec.$new();',
  10135. 'this.i = 0;'
  10136. ]),
  10137. LinesToStr([ // $mod.$main
  10138. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10139. '$mod.Arr[0].Int = 4;',
  10140. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10141. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10142. '$mod.Arr[7].$assign($mod.r);',
  10143. '$mod.r.$assign($mod.Arr[8]);',
  10144. '$mod.i = 0;',
  10145. '$mod.i = rtl.length($mod.Arr)-1;',
  10146. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10147. '']));
  10148. end;
  10149. procedure TTestModule.TestArray_StaticRecord;
  10150. begin
  10151. StartProgram(false);
  10152. Add([
  10153. 'type',
  10154. ' TRec = record',
  10155. ' Int: longint;',
  10156. ' end;',
  10157. ' TArrayRec = array[1..2] of TRec;',
  10158. 'var',
  10159. ' Arr: TArrayRec;',
  10160. 'begin',
  10161. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10162. '']);
  10163. ConvertProgram;
  10164. CheckSource('TestArray_StaticRecord',
  10165. LinesToStr([ // statements
  10166. 'rtl.recNewT(this, "TRec", function () {',
  10167. ' this.Int = 0;',
  10168. ' this.$eq = function (b) {',
  10169. ' return this.Int === b.Int;',
  10170. ' };',
  10171. ' this.$assign = function (s) {',
  10172. ' this.Int = s.Int;',
  10173. ' return this;',
  10174. ' };',
  10175. '});',
  10176. 'this.TArrayRec$clone = function (a) {',
  10177. ' var b = [];',
  10178. ' b.length = 2;',
  10179. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10180. ' return b;',
  10181. '};',
  10182. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10183. '']),
  10184. LinesToStr([ // $mod.$main
  10185. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10186. end;
  10187. procedure TTestModule.TestArrayOfSet;
  10188. begin
  10189. StartProgram(false);
  10190. Add([
  10191. 'type',
  10192. ' TFlag = (big,small);',
  10193. ' TSetOfFlag = set of tflag;',
  10194. ' TArrayFlag = array of TSetOfFlag;',
  10195. 'procedure DoIt(const a: Tarrayflag);',
  10196. 'begin',
  10197. 'end;',
  10198. 'var',
  10199. ' f: TFlag;',
  10200. ' s: TSetOfFlag;',
  10201. ' Arr: TArrayFlag;',
  10202. ' i: longint;',
  10203. 'begin',
  10204. ' SetLength(arr,3);',
  10205. ' arr[0]:=s;',
  10206. ' arr[1]:=[big];',
  10207. ' arr[2]:=[big]+s;',
  10208. ' arr[3]:=s+[big];',
  10209. ' arr[4]:=arr[5];',
  10210. ' s:=arr[6];',
  10211. ' i:=low(arr);',
  10212. ' i:=high(arr);',
  10213. ' DoIt(arr);',
  10214. ' DoIt([s]);',
  10215. ' DoIt([[],s]);',
  10216. ' DoIt([s,[]]);',
  10217. '']);
  10218. ConvertProgram;
  10219. CheckSource('TestArrayOfSet',
  10220. LinesToStr([ // statements
  10221. 'this.TFlag = {',
  10222. ' "0": "big",',
  10223. ' big: 0,',
  10224. ' "1": "small",',
  10225. ' small: 1',
  10226. '};',
  10227. 'this.DoIt = function (a) {',
  10228. '};',
  10229. 'this.f = 0;',
  10230. 'this.s = {};',
  10231. 'this.Arr = [];',
  10232. 'this.i = 0;',
  10233. '']),
  10234. LinesToStr([ // $mod.$main
  10235. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10236. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10237. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10238. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10239. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10240. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10241. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10242. '$mod.i = 0;',
  10243. '$mod.i = rtl.length($mod.Arr) - 1;',
  10244. '$mod.DoIt($mod.Arr);',
  10245. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10246. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10247. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10248. '']));
  10249. end;
  10250. procedure TTestModule.TestArray_DynAsParam;
  10251. begin
  10252. StartProgram(false);
  10253. Add([
  10254. 'type integer = longint;',
  10255. 'type TArrInt = array of integer;',
  10256. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10257. 'var vJ: TArrInt;',
  10258. 'begin',
  10259. ' vg:=vg;',
  10260. ' vj:=vh;',
  10261. ' vi:=vi;',
  10262. ' doit(vg,vg,vg);',
  10263. ' doit(vh,vh,vj);',
  10264. ' doit(vi,vi,vi);',
  10265. ' doit(vj,vj,vj);',
  10266. 'end;',
  10267. 'var i: TArrInt;',
  10268. 'begin',
  10269. ' doit(i,i,i);']);
  10270. ConvertProgram;
  10271. CheckSource('TestArray_DynAsParams',
  10272. LinesToStr([ // statements
  10273. 'this.DoIt = function (vG,vH,vI) {',
  10274. ' var vJ = [];',
  10275. ' vG = rtl.arrayRef(vG);',
  10276. ' vJ = rtl.arrayRef(vH);',
  10277. ' vI.set(rtl.arrayRef(vI.get()));',
  10278. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10279. ' get: function () {',
  10280. ' return vG;',
  10281. ' },',
  10282. ' set: function (v) {',
  10283. ' vG = v;',
  10284. ' }',
  10285. ' });',
  10286. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10287. ' get: function () {',
  10288. ' return vJ;',
  10289. ' },',
  10290. ' set: function (v) {',
  10291. ' vJ = v;',
  10292. ' }',
  10293. ' });',
  10294. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10295. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10296. ' get: function () {',
  10297. ' return vJ;',
  10298. ' },',
  10299. ' set: function (v) {',
  10300. ' vJ = v;',
  10301. ' }',
  10302. ' });',
  10303. '};',
  10304. 'this.i = [];'
  10305. ]),
  10306. LinesToStr([
  10307. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10308. ' p: $mod,',
  10309. ' get: function () {',
  10310. ' return this.p.i;',
  10311. ' },',
  10312. ' set: function (v) {',
  10313. ' this.p.i = v;',
  10314. ' }',
  10315. '});'
  10316. ]));
  10317. end;
  10318. procedure TTestModule.TestArray_StaticAsParam;
  10319. begin
  10320. StartProgram(false);
  10321. Add([
  10322. 'type integer = longint;',
  10323. 'type TArrInt = array[1..2] of integer;',
  10324. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10325. 'var vJ: TArrInt;',
  10326. 'begin',
  10327. ' vg:=vg;',
  10328. ' vj:=vh;',
  10329. ' vi:=vi;',
  10330. ' doit(vg,vg,vg);',
  10331. ' doit(vh,vh,vj);',
  10332. ' doit(vi,vi,vi);',
  10333. ' doit(vj,vj,vj);',
  10334. 'end;',
  10335. 'var i: TArrInt;',
  10336. 'begin',
  10337. ' doit(i,i,i);']);
  10338. ConvertProgram;
  10339. CheckSource('TestArray_StaticAsParams',
  10340. LinesToStr([ // statements
  10341. 'this.DoIt = function (vG,vH,vI) {',
  10342. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10343. ' vG = vG.slice(0);',
  10344. ' vJ = vH.slice(0);',
  10345. ' vI.set(vI.get().slice(0));',
  10346. ' $mod.DoIt(vG.slice(0), vG, {',
  10347. ' get: function () {',
  10348. ' return vG;',
  10349. ' },',
  10350. ' set: function (v) {',
  10351. ' vG = v;',
  10352. ' }',
  10353. ' });',
  10354. ' $mod.DoIt(vH.slice(0), vH, {',
  10355. ' get: function () {',
  10356. ' return vJ;',
  10357. ' },',
  10358. ' set: function (v) {',
  10359. ' vJ = v;',
  10360. ' }',
  10361. ' });',
  10362. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10363. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10364. ' get: function () {',
  10365. ' return vJ;',
  10366. ' },',
  10367. ' set: function (v) {',
  10368. ' vJ = v;',
  10369. ' }',
  10370. ' });',
  10371. '};',
  10372. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10373. ]),
  10374. LinesToStr([
  10375. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10376. ' p: $mod,',
  10377. ' get: function () {',
  10378. ' return this.p.i;',
  10379. ' },',
  10380. ' set: function (v) {',
  10381. ' this.p.i = v;',
  10382. ' }',
  10383. '});'
  10384. ]));
  10385. end;
  10386. procedure TTestModule.TestArrayElement_AsParams;
  10387. begin
  10388. StartProgram(false);
  10389. Add('type integer = longint;');
  10390. Add('type TArrayInt = array of integer;');
  10391. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10392. Add('var vJ: tarrayint;');
  10393. Add('begin');
  10394. Add(' vi:=vi;');
  10395. Add(' doit(vi,vi,vi);');
  10396. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10397. Add('end;');
  10398. Add('var a: TArrayInt;');
  10399. Add('begin');
  10400. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10401. ConvertProgram;
  10402. CheckSource('TestArrayElement_AsParams',
  10403. LinesToStr([ // statements
  10404. 'this.DoIt = function (vG,vH,vI) {',
  10405. ' var vJ = [];',
  10406. ' vI.set(vI.get());',
  10407. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10408. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10409. ' a:1+3,',
  10410. ' p:vJ,',
  10411. ' get: function () {',
  10412. ' return this.p[this.a];',
  10413. ' },',
  10414. ' set: function (v) {',
  10415. ' this.p[this.a] = v;',
  10416. ' }',
  10417. ' });',
  10418. '};',
  10419. 'this.a = [];'
  10420. ]),
  10421. LinesToStr([
  10422. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10423. ' a: 1+6,',
  10424. ' p: $mod.a,',
  10425. ' get: function () {',
  10426. ' return this.p[this.a];',
  10427. ' },',
  10428. ' set: function (v) {',
  10429. ' this.p[this.a] = v;',
  10430. ' }',
  10431. '});'
  10432. ]));
  10433. end;
  10434. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10435. begin
  10436. StartProgram(false);
  10437. Add('type Integer = longint;');
  10438. Add('type TArrayInt = array of integer;');
  10439. Add('function GetArr(vB: integer = 0): tarrayint;');
  10440. Add('begin');
  10441. Add('end;');
  10442. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10443. Add('begin');
  10444. Add('end;');
  10445. Add('begin');
  10446. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10447. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10448. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10449. ConvertProgram;
  10450. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10451. LinesToStr([ // statements
  10452. 'this.GetArr = function (vB) {',
  10453. ' var Result = [];',
  10454. ' return Result;',
  10455. '};',
  10456. 'this.DoIt = function (vG,vH,vI) {',
  10457. '};'
  10458. ]),
  10459. LinesToStr([
  10460. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10461. ' a: 1+3,',
  10462. ' p: $mod.GetArr(0),',
  10463. ' get: function () {',
  10464. ' return this.p[this.a];',
  10465. ' },',
  10466. ' set: function (v) {',
  10467. ' this.p[this.a] = v;',
  10468. ' }',
  10469. '});',
  10470. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10471. ' a: 2+3,',
  10472. ' p: $mod.GetArr(0),',
  10473. ' get: function () {',
  10474. ' return this.p[this.a];',
  10475. ' },',
  10476. ' set: function (v) {',
  10477. ' this.p[this.a] = v;',
  10478. ' }',
  10479. '});',
  10480. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10481. ' a: 3+3,',
  10482. ' p: $mod.GetArr(9),',
  10483. ' get: function () {',
  10484. ' return this.p[this.a];',
  10485. ' },',
  10486. ' set: function (v) {',
  10487. ' this.p[this.a] = v;',
  10488. ' }',
  10489. '});',
  10490. '']));
  10491. end;
  10492. procedure TTestModule.TestArrayEnumTypeRange;
  10493. begin
  10494. StartProgram(false);
  10495. Add([
  10496. 'type',
  10497. ' TEnum = (red,blue);',
  10498. ' TEnumArray = array[TEnum] of longint;',
  10499. 'var',
  10500. ' e: TEnum;',
  10501. ' i: longint;',
  10502. ' a: TEnumArray;',
  10503. ' numbers: TEnumArray = (1,2);',
  10504. ' names: array[TEnum] of string = (''red'',''blue'');',
  10505. 'begin',
  10506. ' e:=low(a);',
  10507. ' e:=high(a);',
  10508. ' i:=a[red];',
  10509. ' a[e]:=a[e];']);
  10510. ConvertProgram;
  10511. CheckSource('TestArrayEnumTypeRange',
  10512. LinesToStr([ // statements
  10513. ' this.TEnum = {',
  10514. ' "0": "red",',
  10515. ' red: 0,',
  10516. ' "1": "blue",',
  10517. ' blue: 1',
  10518. '};',
  10519. 'this.e = 0;',
  10520. 'this.i = 0;',
  10521. 'this.a = rtl.arraySetLength(null,0,2);',
  10522. 'this.numbers = [1, 2];',
  10523. 'this.names = ["red", "blue"];',
  10524. '']),
  10525. LinesToStr([ // $mod.$main
  10526. '$mod.e = $mod.TEnum.red;',
  10527. '$mod.e = $mod.TEnum.blue;',
  10528. '$mod.i = $mod.a[$mod.TEnum.red];',
  10529. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10530. '']));
  10531. end;
  10532. procedure TTestModule.TestArray_SetLengthOutArg;
  10533. begin
  10534. StartProgram(false);
  10535. Add([
  10536. 'type TArrInt = array of longint;',
  10537. 'procedure DoIt(out a: TArrInt);',
  10538. 'begin',
  10539. ' SetLength(a,2);',
  10540. 'end;',
  10541. 'begin',
  10542. '']);
  10543. ConvertProgram;
  10544. CheckSource('TestArray_SetLengthOutArg',
  10545. LinesToStr([ // statements
  10546. 'this.DoIt = function (a) {',
  10547. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10548. '};',
  10549. '']),
  10550. LinesToStr([
  10551. '']));
  10552. end;
  10553. procedure TTestModule.TestArray_SetLengthProperty;
  10554. begin
  10555. StartProgram(false);
  10556. Add('type');
  10557. Add(' TArrInt = array of longint;');
  10558. Add(' TObject = class');
  10559. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10560. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10561. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10562. Add(' end;');
  10563. Add('var Obj: TObject;');
  10564. Add('begin');
  10565. Add(' SetLength(Obj.Colors,2);');
  10566. ConvertProgram;
  10567. CheckSource('TestArray_SetLengthProperty',
  10568. LinesToStr([ // statements
  10569. 'rtl.createClass(this, "TObject", null, function () {',
  10570. ' this.$init = function () {',
  10571. ' };',
  10572. ' this.$final = function () {',
  10573. ' };',
  10574. '});',
  10575. 'this.Obj = null;',
  10576. '']),
  10577. LinesToStr([
  10578. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10579. '']));
  10580. end;
  10581. procedure TTestModule.TestArray_SetLengthMultiDim;
  10582. begin
  10583. StartProgram(false);
  10584. Add([
  10585. 'type',
  10586. ' TArrArrInt = array of array of longint;',
  10587. ' TArrStaInt = array of array[1..2] of longint;',
  10588. 'var',
  10589. ' a: TArrArrInt;',
  10590. ' b: TArrStaInt;',
  10591. 'begin',
  10592. ' SetLength(a,2);',
  10593. ' SetLength(a,3,4);',
  10594. ' SetLength(b,5);',
  10595. '']);
  10596. ConvertProgram;
  10597. CheckSource('TestArray_SetLengthMultiDim',
  10598. LinesToStr([ // statements
  10599. 'this.a = [];',
  10600. 'this.b = [];',
  10601. '']),
  10602. LinesToStr([
  10603. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10604. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10605. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10606. '']));
  10607. end;
  10608. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10609. begin
  10610. StartProgram(false);
  10611. Add([
  10612. 'type',
  10613. ' TStaArr1 = array[1..3] of boolean;',
  10614. //' TStaArr2 = array[5..6] of TStaArr1;',
  10615. ' TDynArr1StaArr1 = array of TStaArr1;',
  10616. //' TDynArr1StaArr2 = array of TStaArr2;',
  10617. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10618. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10619. 'var',
  10620. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10621. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10622. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10623. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10624. 'begin',
  10625. ' SetLength(DynArr1StaArr1,11);',
  10626. ' SetLength(DynArr2StaArr1,12);',
  10627. ' SetLength(DynArr2StaArr1[13],14);',
  10628. ' SetLength(DynArr2StaArr1,15,16);',
  10629. //' SetLength(DynArr1StaArr2,21);',
  10630. //' SetLength(DynArr2StaArr2,22);',
  10631. //' SetLength(DynArr2StaArr2[23],24);',
  10632. //' SetLength(DynArr2StaArr2,25,26);',
  10633. '']);
  10634. ConvertProgram;
  10635. CheckSource('TestArray_DynOfStatic',
  10636. LinesToStr([ // statements
  10637. 'this.DynArr1StaArr1 = [];',
  10638. 'this.DynArr2StaArr1 = [];',
  10639. '']),
  10640. LinesToStr([ // $mod.$main
  10641. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10642. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10643. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10644. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10645. ' $mod.DynArr2StaArr1,',
  10646. ' false,',
  10647. ' 15,',
  10648. ' 16,',
  10649. ' "s",',
  10650. ' 3',
  10651. ');',
  10652. '']));
  10653. end;
  10654. procedure TTestModule.TestArray_OpenArrayOfString;
  10655. begin
  10656. StartProgram(false);
  10657. Add('procedure DoIt(const a: array of String);');
  10658. Add('var');
  10659. Add(' i: longint;');
  10660. Add(' s: string;');
  10661. Add('begin');
  10662. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10663. Add('end;');
  10664. Add('var s: string;');
  10665. Add('begin');
  10666. Add(' DoIt([]);');
  10667. Add(' DoIt([s,''foo'','''',s+s]);');
  10668. ConvertProgram;
  10669. CheckSource('TestArray_OpenArrayOfString',
  10670. LinesToStr([ // statements
  10671. 'this.DoIt = function (a) {',
  10672. ' var i = 0;',
  10673. ' var s = "";',
  10674. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10675. ' i = $l;',
  10676. ' s = a[rtl.length(a) - i - 1];',
  10677. ' };',
  10678. '};',
  10679. 'this.s = "";',
  10680. '']),
  10681. LinesToStr([
  10682. '$mod.DoIt([]);',
  10683. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10684. '']));
  10685. end;
  10686. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10687. begin
  10688. StartProgram(false);
  10689. Add([
  10690. 'type TArr = array of char;',
  10691. 'var',
  10692. ' c: char;',
  10693. ' s: string;',
  10694. ' a: TArr;',
  10695. 'procedure Run(const a: array of char);',
  10696. 'begin',
  10697. ' Run(c);',
  10698. ' Run(s);',
  10699. 'end;',
  10700. 'begin',
  10701. ' a:=c;',
  10702. ' a:=s;',
  10703. ' a:=#13;',
  10704. ' a:=''Foo'';',
  10705. ' Run(c);',
  10706. ' Run(s);',
  10707. '']);
  10708. ConvertProgram;
  10709. CheckSource('TestArray_ArrayOfCharAssignString',
  10710. LinesToStr([ // statements
  10711. 'this.c = "";',
  10712. 'this.s = "";',
  10713. 'this.a = [];',
  10714. 'this.Run = function (a) {',
  10715. ' $mod.Run($mod.c.split(""));',
  10716. ' $mod.Run($mod.s.split(""));',
  10717. '};',
  10718. '']),
  10719. LinesToStr([
  10720. '$mod.a = $mod.c.split("");',
  10721. '$mod.a = $mod.s.split("");',
  10722. '$mod.a = "\r".split("");',
  10723. '$mod.a = "Foo".split("");',
  10724. '$mod.Run($mod.c.split(""));',
  10725. '$mod.Run($mod.s.split(""));',
  10726. '']));
  10727. end;
  10728. procedure TTestModule.TestArray_ConstRef;
  10729. begin
  10730. StartProgram(false);
  10731. Add([
  10732. 'type TArr = array of word;',
  10733. 'procedure Run(constref a: TArr);',
  10734. 'begin',
  10735. 'end;',
  10736. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10737. 'var l: TArr;',
  10738. 'begin',
  10739. ' Run(l);',
  10740. ' Run(a);',
  10741. ' Run(b);',
  10742. ' Run(c);',
  10743. ' Run(d);',
  10744. ' Run(e);',
  10745. 'end;',
  10746. 'begin',
  10747. '']);
  10748. ConvertProgram;
  10749. CheckResolverUnexpectedHints();
  10750. CheckSource('TestArray_ConstRef',
  10751. LinesToStr([ // statements
  10752. 'this.Run = function (a) {',
  10753. '};',
  10754. 'this.Fly = function (a, b, c, d, e) {',
  10755. ' var l = [];',
  10756. ' $mod.Run(l);',
  10757. ' $mod.Run(a);',
  10758. ' $mod.Run(b.get());',
  10759. ' $mod.Run(c.get());',
  10760. ' $mod.Run(d);',
  10761. ' $mod.Run(e);',
  10762. '};',
  10763. '']),
  10764. LinesToStr([
  10765. '']));
  10766. end;
  10767. procedure TTestModule.TestArray_Concat;
  10768. begin
  10769. StartProgram(false);
  10770. Add([
  10771. 'type',
  10772. ' integer = longint;',
  10773. ' TFlag = (big,small);',
  10774. ' TFlags = set of TFlag;',
  10775. ' TRec = record',
  10776. ' i: integer;',
  10777. ' end;',
  10778. ' TArrInt = array of integer;',
  10779. ' TArrRec = array of TRec;',
  10780. ' TArrFlag = array of TFlag;',
  10781. ' TArrSet = array of TFlags;',
  10782. ' TArrJSValue = array of jsvalue;',
  10783. 'var',
  10784. ' ArrInt: tarrint;',
  10785. ' ArrRec: tarrrec;',
  10786. ' ArrFlag: tarrflag;',
  10787. ' ArrSet: tarrset;',
  10788. ' ArrJSValue: tarrjsvalue;',
  10789. 'begin',
  10790. ' arrint:=concat(arrint);',
  10791. ' arrint:=concat(arrint,arrint);',
  10792. ' arrint:=concat(arrint,arrint,arrint);',
  10793. ' arrrec:=concat(arrrec);',
  10794. ' arrrec:=concat(arrrec,arrrec);',
  10795. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  10796. ' arrset:=concat(arrset);',
  10797. ' arrset:=concat(arrset,arrset);',
  10798. ' arrset:=concat(arrset,arrset,arrset);',
  10799. ' arrjsvalue:=concat(arrjsvalue);',
  10800. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  10801. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  10802. ' arrint:=concat([1],arrint);',
  10803. ' arrflag:=concat([big]);',
  10804. ' arrflag:=concat([big],arrflag);',
  10805. ' arrflag:=concat(arrflag,[small]);',
  10806. '']);
  10807. ConvertProgram;
  10808. CheckSource('TestArray_Concat',
  10809. LinesToStr([ // statements
  10810. 'this.TFlag = {',
  10811. ' "0": "big",',
  10812. ' big: 0,',
  10813. ' "1": "small",',
  10814. ' small: 1',
  10815. '};',
  10816. 'rtl.recNewT(this, "TRec", function () {',
  10817. ' this.i = 0;',
  10818. ' this.$eq = function (b) {',
  10819. ' return this.i === b.i;',
  10820. ' };',
  10821. ' this.$assign = function (s) {',
  10822. ' this.i = s.i;',
  10823. ' return this;',
  10824. ' };',
  10825. '});',
  10826. 'this.ArrInt = [];',
  10827. 'this.ArrRec = [];',
  10828. 'this.ArrFlag = [];',
  10829. 'this.ArrSet = [];',
  10830. 'this.ArrJSValue = [];',
  10831. '']),
  10832. LinesToStr([ // $mod.$main
  10833. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10834. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10835. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10836. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10837. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10838. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10839. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10840. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10841. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10842. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10843. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10844. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10845. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10846. '$mod.ArrFlag = [$mod.TFlag.big];',
  10847. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10848. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10849. '']));
  10850. end;
  10851. procedure TTestModule.TestArray_Copy;
  10852. begin
  10853. StartProgram(false);
  10854. Add([
  10855. 'type',
  10856. ' integer = longint;',
  10857. ' TFlag = (big,small);',
  10858. ' TFlags = set of TFlag;',
  10859. ' TRec = record',
  10860. ' i: integer;',
  10861. ' end;',
  10862. ' TArrInt = array of integer;',
  10863. ' TArrRec = array of TRec;',
  10864. ' TArrSet = array of TFlags;',
  10865. ' TArrJSValue = array of jsvalue;',
  10866. 'var',
  10867. ' ArrInt: tarrint;',
  10868. ' ArrRec: tarrrec;',
  10869. ' ArrSet: tarrset;',
  10870. ' ArrJSValue: tarrjsvalue;',
  10871. 'begin',
  10872. ' arrint:=copy(arrint);',
  10873. ' arrint:=copy(arrint,2);',
  10874. ' arrint:=copy(arrint,3,4);',
  10875. ' arrint:=copy([1,1],1,2);',
  10876. ' arrrec:=copy(arrrec);',
  10877. ' arrrec:=copy(arrrec,5);',
  10878. ' arrrec:=copy(arrrec,6,7);',
  10879. ' arrset:=copy(arrset);',
  10880. ' arrset:=copy(arrset,8);',
  10881. ' arrset:=copy(arrset,9,10);',
  10882. ' arrjsvalue:=copy(arrjsvalue);',
  10883. ' arrjsvalue:=copy(arrjsvalue,11);',
  10884. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10885. ' ']);
  10886. ConvertProgram;
  10887. CheckSource('TestArray_Copy',
  10888. LinesToStr([ // statements
  10889. 'this.TFlag = {',
  10890. ' "0": "big",',
  10891. ' big: 0,',
  10892. ' "1": "small",',
  10893. ' small: 1',
  10894. '};',
  10895. 'rtl.recNewT(this, "TRec", function () {',
  10896. ' this.i = 0;',
  10897. ' this.$eq = function (b) {',
  10898. ' return this.i === b.i;',
  10899. ' };',
  10900. ' this.$assign = function (s) {',
  10901. ' this.i = s.i;',
  10902. ' return this;',
  10903. ' };',
  10904. '});',
  10905. 'this.ArrInt = [];',
  10906. 'this.ArrRec = [];',
  10907. 'this.ArrSet = [];',
  10908. 'this.ArrJSValue = [];',
  10909. '']),
  10910. LinesToStr([ // $mod.$main
  10911. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10912. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10913. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10914. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10915. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10916. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10917. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10918. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10919. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10920. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10921. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10922. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10923. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10924. '']));
  10925. end;
  10926. procedure TTestModule.TestArray_InsertDelete;
  10927. begin
  10928. StartProgram(false);
  10929. Add([
  10930. 'type',
  10931. ' integer = longint;',
  10932. ' TFlag = (big,small);',
  10933. ' TFlags = set of TFlag;',
  10934. ' TRec = record',
  10935. ' i: integer;',
  10936. ' end;',
  10937. ' TArrInt = array of integer;',
  10938. ' TArrRec = array of TRec;',
  10939. ' TArrSet = array of TFlags;',
  10940. ' TArrJSValue = array of jsvalue;',
  10941. ' TArrArrInt = array of TArrInt;',
  10942. 'var',
  10943. ' ArrInt: tarrint;',
  10944. ' ArrRec: tarrrec;',
  10945. ' ArrSet: tarrset;',
  10946. ' ArrJSValue: tarrjsvalue;',
  10947. ' ArrArrInt: TArrArrInt;',
  10948. 'begin',
  10949. ' Insert(1,arrint,2);',
  10950. ' Insert(arrint[3],arrint,4);',
  10951. ' Insert(arrrec[5],arrrec,6);',
  10952. ' Insert(arrset[7],arrset,7);',
  10953. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10954. ' Insert(10,arrjsvalue,11);',
  10955. ' Insert([23],arrarrint,22);',
  10956. ' Delete(arrint,12,13);',
  10957. ' Delete(arrrec,14,15);',
  10958. ' Delete(arrset,17,18);',
  10959. ' Delete(arrjsvalue,19,10);']);
  10960. ConvertProgram;
  10961. CheckSource('TestArray_InsertDelete',
  10962. LinesToStr([ // statements
  10963. 'this.TFlag = {',
  10964. ' "0": "big",',
  10965. ' big: 0,',
  10966. ' "1": "small",',
  10967. ' small: 1',
  10968. '};',
  10969. 'rtl.recNewT(this, "TRec", function () {',
  10970. ' this.i = 0;',
  10971. ' this.$eq = function (b) {',
  10972. ' return this.i === b.i;',
  10973. ' };',
  10974. ' this.$assign = function (s) {',
  10975. ' this.i = s.i;',
  10976. ' return this;',
  10977. ' };',
  10978. '});',
  10979. 'this.ArrInt = [];',
  10980. 'this.ArrRec = [];',
  10981. 'this.ArrSet = [];',
  10982. 'this.ArrJSValue = [];',
  10983. 'this.ArrArrInt = [];',
  10984. '']),
  10985. LinesToStr([ // $mod.$main
  10986. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10987. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10988. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10989. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10990. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10991. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10992. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10993. '$mod.ArrInt.splice(12, 13);',
  10994. '$mod.ArrRec.splice(14, 15);',
  10995. '$mod.ArrSet.splice(17, 18);',
  10996. '$mod.ArrJSValue.splice(19, 10);',
  10997. '']));
  10998. end;
  10999. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11000. begin
  11001. Parser.Options:=Parser.Options+[po_cassignments];
  11002. StartProgram(false);
  11003. Add([
  11004. '{$modeswitch arrayoperators}',
  11005. 'type',
  11006. ' integer = longint;',
  11007. ' TArrInt = array of integer;',
  11008. ' TArrStr = array of string;',
  11009. 'const',
  11010. ' Ints: TArrInt = (1,2,3);',
  11011. ' Aliases: TarrStr = (''foo'',''b'');',
  11012. ' OneInt: TArrInt = (7);',
  11013. ' OneStr: array of integer = (7);',
  11014. ' Chars: array of char = ''aoc'';',
  11015. ' Names: array of string = (''a'',''foo'');',
  11016. ' NameCount = low(Names)+high(Names)+length(Names);',
  11017. 'var i: integer;',
  11018. 'begin',
  11019. ' Ints:=[];',
  11020. ' Ints:=[1,1];',
  11021. ' Ints:=[1]+[2];',
  11022. ' Ints:=[2];',
  11023. ' Ints:=[]+ints;',
  11024. ' Ints:=Ints+[];',
  11025. ' Ints:=Ints+OneInt;',
  11026. ' Ints:=Ints+[1,1];',
  11027. ' Ints:=[i,i]+Ints;',
  11028. ' Ints:=[1]+[i]+[3];',
  11029. '']);
  11030. ConvertProgram;
  11031. CheckSource('TestArray_DynArrayConstObjFPC',
  11032. LinesToStr([ // statements
  11033. 'this.Ints = [1, 2, 3];',
  11034. 'this.Aliases = ["foo", "b"];',
  11035. 'this.OneInt = [7];',
  11036. 'this.OneStr = [7];',
  11037. 'this.Chars = ["a", "o", "c"];',
  11038. 'this.Names = ["a", "foo"];',
  11039. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11040. 'this.i = 0;',
  11041. '']),
  11042. LinesToStr([ // $mod.$main
  11043. '$mod.Ints = [];',
  11044. '$mod.Ints = [1, 1];',
  11045. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11046. '$mod.Ints = [2];',
  11047. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11048. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  11049. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11050. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  11051. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11052. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11053. '']));
  11054. end;
  11055. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11056. begin
  11057. StartProgram(false);
  11058. // Note: const c = [1,1]; defines a set!
  11059. Add([
  11060. '{$mode delphi}',
  11061. 'type',
  11062. ' integer = longint;',
  11063. ' TArrInt = array of integer;',
  11064. ' TArrStr = array of string;',
  11065. 'const',
  11066. ' Ints: TArrInt = [1,1,2];',
  11067. ' Aliases: TarrStr = [''foo'',''b''];',
  11068. ' OneInt: TArrInt = [7];',
  11069. ' OneStr: array of integer = [7]+[8];',
  11070. ' Chars: array of char = ''aoc'';',
  11071. ' Names: array of string = [''a'',''a''];',
  11072. ' NameCount = low(Names)+high(Names)+length(Names);',
  11073. 'begin',
  11074. '']);
  11075. ConvertProgram;
  11076. CheckSource('TestArray_DynArrayConstDelphi',
  11077. LinesToStr([ // statements
  11078. 'this.Ints = [1, 1, 2];',
  11079. 'this.Aliases = ["foo", "b"];',
  11080. 'this.OneInt = [7];',
  11081. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11082. 'this.Chars = ["a", "o", "c"];',
  11083. 'this.Names = ["a", "a"];',
  11084. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11085. '']),
  11086. LinesToStr([ // $mod.$main
  11087. '']));
  11088. end;
  11089. procedure TTestModule.TestArray_ArrayLitAsParam;
  11090. begin
  11091. StartProgram(false);
  11092. Add([
  11093. '{$modeswitch arrayoperators}',
  11094. 'type',
  11095. ' integer = longint;',
  11096. ' TArrInt = array of integer;',
  11097. ' TArrSet = array of (red,green,blue);',
  11098. 'procedure DoOpenInt(const a: array of integer); forward;',
  11099. 'procedure DoInt(const a: TArrInt);',
  11100. 'begin',
  11101. ' DoInt(a+[1]);',
  11102. ' DoInt([1]+a);',
  11103. ' DoOpenInt(a);',
  11104. ' DoOpenInt(a+[1]);',
  11105. ' DoOpenInt([1]+a);',
  11106. 'end;',
  11107. 'procedure DoOpenInt(const a: array of integer);',
  11108. 'begin',
  11109. ' DoOpenInt(a+[1]);',
  11110. ' DoOpenInt([1]+a);',
  11111. ' DoInt(a);',
  11112. ' DoInt(a+[1]);',
  11113. ' DoInt([1]+a);',
  11114. 'end;',
  11115. 'procedure DoSet(const a: TArrSet);',
  11116. 'begin',
  11117. ' DoSet(a+[red]);',
  11118. ' DoSet([blue]+a);',
  11119. 'end;',
  11120. 'var',
  11121. ' i: TArrInt;',
  11122. ' s: TArrSet;',
  11123. 'begin',
  11124. ' DoInt([1]);',
  11125. ' DoInt([1]+[2]);',
  11126. ' DoInt(i+[1]);',
  11127. ' DoInt([1]+i);',
  11128. ' DoOpenInt([1]);',
  11129. ' DoOpenInt([1]+[2]);',
  11130. ' DoOpenInt(i+[1]);',
  11131. ' DoOpenInt([1]+i);',
  11132. ' DoSet([red]);',
  11133. ' DoSet([blue]+[green]);',
  11134. ' DoSet(s+[blue]);',
  11135. ' DoSet([red]+s);',
  11136. '']);
  11137. ConvertProgram;
  11138. CheckSource('TestArray_ArrayLitAsParam',
  11139. LinesToStr([ // statements
  11140. 'this.TArrSet$a = {',
  11141. ' "0": "red",',
  11142. ' red: 0,',
  11143. ' "1": "green",',
  11144. ' green: 1,',
  11145. ' "2": "blue",',
  11146. ' blue: 2',
  11147. '};',
  11148. 'this.DoInt = function (a) {',
  11149. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11150. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11151. ' $mod.DoOpenInt(a);',
  11152. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11153. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11154. '};',
  11155. 'this.DoOpenInt = function (a) {',
  11156. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11157. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11158. ' $mod.DoInt(a);',
  11159. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11160. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11161. '};',
  11162. 'this.DoSet = function (a) {',
  11163. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11164. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11165. '};',
  11166. 'this.i = [];',
  11167. 'this.s = [];',
  11168. '']),
  11169. LinesToStr([ // $mod.$main
  11170. '$mod.DoInt([1]);',
  11171. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11172. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11173. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11174. '$mod.DoOpenInt([1]);',
  11175. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11176. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11177. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11178. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11179. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11180. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11181. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11182. '']));
  11183. end;
  11184. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11185. begin
  11186. StartProgram(false);
  11187. Add([
  11188. '{$modeswitch arrayoperators}',
  11189. 'type',
  11190. ' integer = longint;',
  11191. ' TArrInt = array of integer;',
  11192. ' TArrArrInt = array of TArrInt;',
  11193. 'procedure DoInt(const a: TArrArrInt);',
  11194. 'begin',
  11195. ' DoInt(a+[[1]]);',
  11196. ' DoInt([[1]]+a);',
  11197. ' DoInt(a);',
  11198. 'end;',
  11199. 'var',
  11200. ' i: TArrInt;',
  11201. ' a: TArrArrInt;',
  11202. 'begin',
  11203. ' a:=[[1]];',
  11204. ' a:=[i];',
  11205. ' a:=a+[i];',
  11206. ' a:=[i]+a;',
  11207. ' a:=[[1]+i];',
  11208. ' a:=[[1]+[2]];',
  11209. ' a:=[i+[2]];',
  11210. ' DoInt([[1]]);',
  11211. ' DoInt([[1]+[2],[3,4],[5]]);',
  11212. ' DoInt([i+[1]]+a);',
  11213. ' DoInt([i]+a);',
  11214. '']);
  11215. ConvertProgram;
  11216. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11217. LinesToStr([ // statements
  11218. 'this.DoInt = function (a) {',
  11219. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11220. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11221. ' $mod.DoInt(a);',
  11222. '};',
  11223. 'this.i = [];',
  11224. 'this.a = [];',
  11225. '']),
  11226. LinesToStr([ // $mod.$main
  11227. '$mod.a = [[1]];',
  11228. '$mod.a = [$mod.i];',
  11229. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  11230. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11231. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11232. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11233. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11234. '$mod.DoInt([[1]]);',
  11235. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11236. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11237. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11238. '']));
  11239. end;
  11240. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11241. begin
  11242. StartProgram(false);
  11243. Add([
  11244. '{$modeswitch arrayoperators}',
  11245. 'type',
  11246. ' integer = longint;',
  11247. ' TArrInt = array[1..2] of integer;',
  11248. ' TArrArrInt = array of TArrInt;',
  11249. 'procedure DoInt(const a: TArrArrInt);',
  11250. 'begin',
  11251. ' DoInt(a+[[1,2]]);',
  11252. ' DoInt([[1,2]]+a);',
  11253. ' DoInt(a);',
  11254. 'end;',
  11255. 'var',
  11256. ' i: TArrInt;',
  11257. ' a: TArrArrInt;',
  11258. 'begin',
  11259. ' a:=[[1,1]];',
  11260. ' a:=[i];',
  11261. ' a:=a+[i];',
  11262. ' a:=[i]+a;',
  11263. ' DoInt([[1,1]]);',
  11264. ' DoInt([[1,2],[3,4]]);',
  11265. '']);
  11266. ConvertProgram;
  11267. CheckSource('TestArray_ArrayLitStaticAsParam',
  11268. LinesToStr([ // statements
  11269. 'this.DoInt = function (a) {',
  11270. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  11271. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  11272. ' $mod.DoInt(a);',
  11273. '};',
  11274. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11275. 'this.a = [];',
  11276. '']),
  11277. LinesToStr([ // $mod.$main
  11278. '$mod.a = [[1, 1]];',
  11279. '$mod.a = [$mod.i.slice(0)];',
  11280. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  11281. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  11282. '$mod.DoInt([[1, 1]]);',
  11283. '$mod.DoInt([[1, 2], [3, 4]]);',
  11284. '']));
  11285. end;
  11286. procedure TTestModule.TestArray_ForInArrOfString;
  11287. begin
  11288. StartProgram(false);
  11289. Add([
  11290. 'type',
  11291. 'type',
  11292. ' TMonthNameArray = array [1..12] of string;',
  11293. ' TMonthNames = TMonthNameArray;',
  11294. ' TObject = class',
  11295. ' private',
  11296. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11297. ' public',
  11298. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11299. ' end;',
  11300. 'var',
  11301. ' f: TObject;',
  11302. ' Month: string;',
  11303. ' Names: array of string = (''a'',''foo'',''bar'');',
  11304. ' i: longint;',
  11305. 'begin',
  11306. ' for Month in f.LongMonthNames do ;',
  11307. ' for Month in Names do ;',
  11308. ' for i:=low(Names) to high(Names) do ;',
  11309. '']);
  11310. ConvertProgram;
  11311. CheckSource('TestArray_ForInArrOfString',
  11312. LinesToStr([ // statements
  11313. 'rtl.createClass(this, "TObject", null, function () {',
  11314. ' this.$init = function () {',
  11315. ' };',
  11316. ' this.$final = function () {',
  11317. ' };',
  11318. '});',
  11319. 'this.f = null;',
  11320. 'this.Month = "";',
  11321. 'this.Names = ["a", "foo", "bar"];',
  11322. 'this.i = 0;',
  11323. '']),
  11324. LinesToStr([ // $mod.$main
  11325. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11326. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11327. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11328. '']));
  11329. end;
  11330. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11331. begin
  11332. StartProgram(false);
  11333. Add([
  11334. '{$modeswitch externalclass}',
  11335. 'type',
  11336. ' TJSObject = class external name ''Object''',
  11337. ' end;',
  11338. ' TJSArray = class external name ''Array''',
  11339. ' class function isArray(Value: JSValue) : boolean;',
  11340. ' function concat() : TJSArray; varargs;',
  11341. ' end;',
  11342. 'var',
  11343. ' aObj: TJSArray;',
  11344. ' a: array of longint;',
  11345. ' o: TJSObject;',
  11346. 'begin',
  11347. ' if TJSArray.isArray(65) then ;',
  11348. ' aObj:=TJSArray(a).concat(a);',
  11349. ' o:=TJSObject(a);']);
  11350. ConvertProgram;
  11351. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11352. LinesToStr([ // statements
  11353. 'this.aObj = null;',
  11354. 'this.a = [];',
  11355. 'this.o = null;',
  11356. '']),
  11357. LinesToStr([ // $mod.$main
  11358. 'if (Array.isArray(65)) ;',
  11359. '$mod.aObj = $mod.a.concat($mod.a);',
  11360. '$mod.o = $mod.a;',
  11361. '']));
  11362. end;
  11363. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11364. begin
  11365. StartProgram(false);
  11366. Add([
  11367. '{$modeswitch externalclass}',
  11368. 'type',
  11369. ' TArrStr = array of string;',
  11370. ' TJSArray = class external name ''Array''',
  11371. ' end;',
  11372. ' TJSObject = class external name ''Object''',
  11373. ' end;',
  11374. 'var',
  11375. ' aObj: TJSArray;',
  11376. ' a: TArrStr;',
  11377. ' jo: TJSObject;',
  11378. 'begin',
  11379. ' a:=TArrStr(aObj);',
  11380. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11381. ' a:=TarrStr(jo);',
  11382. '']);
  11383. ConvertProgram;
  11384. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11385. LinesToStr([ // statements
  11386. 'this.aObj = null;',
  11387. 'this.a = [];',
  11388. 'this.jo = null;',
  11389. '']),
  11390. LinesToStr([ // $mod.$main
  11391. '$mod.a = $mod.aObj;',
  11392. '$mod.aObj[1] = $mod.aObj[2];',
  11393. '$mod.a = $mod.jo;',
  11394. '']));
  11395. end;
  11396. procedure TTestModule.TestArrayOfConst_TVarRec;
  11397. begin
  11398. StartProgram(true,[supTVarRec]);
  11399. Add([
  11400. 'procedure Say(args: array of const);',
  11401. 'var',
  11402. ' i: longint;',
  11403. ' v: TVarRec;',
  11404. 'begin',
  11405. ' for i:=low(args) to high(args) do begin',
  11406. ' v:=args[i];',
  11407. ' case v.vtype of',
  11408. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11409. ' end;',
  11410. ' end;',
  11411. ' for v in args do ;',
  11412. ' args:=nil;',
  11413. ' SetLength(args,2);',
  11414. 'end;',
  11415. 'begin']);
  11416. ConvertProgram;
  11417. CheckSource('TestArrayOfConst_TVarRec',
  11418. LinesToStr([ // statements
  11419. 'this.Say = function (args) {',
  11420. ' var i = 0;',
  11421. ' var v = pas.system.TVarRec.$new();',
  11422. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11423. ' i = $l;',
  11424. ' v.$assign(args[i]);',
  11425. ' var $tmp = v.VType;',
  11426. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11427. ' };',
  11428. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11429. ' args = [];',
  11430. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11431. '};',
  11432. '']),
  11433. LinesToStr([ // $mod.$main
  11434. ]));
  11435. end;
  11436. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11437. begin
  11438. StartProgram(true,[supTVarRec]);
  11439. Add([
  11440. 'procedure Say(args: array of const);',
  11441. 'begin',
  11442. ' Say(args);',
  11443. 'end;',
  11444. 'var',
  11445. ' p: Pointer;',
  11446. ' j: jsvalue;',
  11447. ' c: currency;',
  11448. 'begin',
  11449. ' Say([]);',
  11450. ' Say([1]);',
  11451. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11452. '']);
  11453. ConvertProgram;
  11454. CheckSource('TestArrayOfConst_PassBaseTypes',
  11455. LinesToStr([ // statements
  11456. 'this.Say = function (args) {',
  11457. ' $mod.Say(args);',
  11458. '};',
  11459. 'this.p = null;',
  11460. 'this.j = undefined;',
  11461. 'this.c = 0;',
  11462. '']),
  11463. LinesToStr([ // $mod.$main
  11464. '$mod.Say([]);',
  11465. '$mod.Say(pas.system.VarRecs(0, 1));',
  11466. '$mod.Say(pas.system.VarRecs(',
  11467. ' 9,',
  11468. ' "c",',
  11469. ' 18,',
  11470. ' "foo",',
  11471. ' 5,',
  11472. ' null,',
  11473. ' 1,',
  11474. ' true,',
  11475. ' 3,',
  11476. ' 1.3,',
  11477. ' 5,',
  11478. ' $mod.p,',
  11479. ' 20,',
  11480. ' $mod.j,',
  11481. ' 12,',
  11482. ' $mod.c',
  11483. ' ));',
  11484. '']));
  11485. end;
  11486. procedure TTestModule.TestArrayOfConst_PassObj;
  11487. begin
  11488. StartProgram(true,[supTVarRec]);
  11489. Add([
  11490. '{$interfaces corba}',
  11491. 'type',
  11492. ' TObject = class',
  11493. ' end;',
  11494. ' TClass = class of TObject;',
  11495. ' IUnknown = interface',
  11496. ' end;',
  11497. 'procedure Say(args: array of const);',
  11498. 'begin',
  11499. 'end;',
  11500. 'var',
  11501. ' o: TObject;',
  11502. ' c: TClass;',
  11503. ' i: IUnknown;',
  11504. 'begin',
  11505. ' Say([o,c,TObject]);',
  11506. ' Say([nil,i]);',
  11507. '']);
  11508. ConvertProgram;
  11509. CheckSource('TestArrayOfConst_PassObj',
  11510. LinesToStr([ // statements
  11511. 'rtl.createClass(this, "TObject", null, function () {',
  11512. ' this.$init = function () {',
  11513. ' };',
  11514. ' this.$final = function () {',
  11515. ' };',
  11516. '});',
  11517. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11518. 'this.Say = function (args) {',
  11519. '};',
  11520. 'this.o = null;',
  11521. 'this.c = null;',
  11522. 'this.i = null;',
  11523. '']),
  11524. LinesToStr([ // $mod.$main
  11525. '$mod.Say(pas.system.VarRecs(',
  11526. ' 7,',
  11527. ' $mod.o,',
  11528. ' 8,',
  11529. ' $mod.c,',
  11530. ' 8,',
  11531. ' $mod.TObject',
  11532. '));',
  11533. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11534. '']));
  11535. end;
  11536. procedure TTestModule.TestRecord_Empty;
  11537. begin
  11538. StartProgram(false);
  11539. Add([
  11540. 'type',
  11541. ' TRecA = record',
  11542. ' end;',
  11543. 'var a,b: TRecA;',
  11544. 'begin',
  11545. ' if a=b then ;']);
  11546. ConvertProgram;
  11547. CheckSource('TestRecord_Empty',
  11548. LinesToStr([ // statements
  11549. 'rtl.recNewT(this, "TRecA", function () {',
  11550. ' this.$eq = function (b) {',
  11551. ' return true;',
  11552. ' };',
  11553. ' this.$assign = function (s) {',
  11554. ' return this;',
  11555. ' };',
  11556. '});',
  11557. 'this.a = this.TRecA.$new();',
  11558. 'this.b = this.TRecA.$new();',
  11559. '']),
  11560. LinesToStr([ // $mod.$main
  11561. 'if ($mod.a.$eq($mod.b)) ;'
  11562. ]));
  11563. end;
  11564. procedure TTestModule.TestRecord_Var;
  11565. begin
  11566. StartProgram(false);
  11567. Add('type');
  11568. Add(' TRecA = record');
  11569. Add(' Bold: longint;');
  11570. Add(' end;');
  11571. Add('var Rec: TRecA;');
  11572. Add('begin');
  11573. Add(' rec.bold:=123');
  11574. ConvertProgram;
  11575. CheckSource('TestRecord_Var',
  11576. LinesToStr([ // statements
  11577. 'rtl.recNewT(this, "TRecA", function () {',
  11578. ' this.Bold = 0;',
  11579. ' this.$eq = function (b) {',
  11580. ' return this.Bold === b.Bold;',
  11581. ' };',
  11582. ' this.$assign = function (s) {',
  11583. ' this.Bold = s.Bold;',
  11584. ' return this;',
  11585. ' };',
  11586. '});',
  11587. 'this.Rec = this.TRecA.$new();',
  11588. '']),
  11589. LinesToStr([ // $mod.$main
  11590. '$mod.Rec.Bold = 123;'
  11591. ]));
  11592. end;
  11593. procedure TTestModule.TestRecord_VarExternal;
  11594. begin
  11595. StartProgram(false);
  11596. Add([
  11597. '{$modeswitch externalclass}',
  11598. 'type',
  11599. ' TRecA = record',
  11600. ' i: byte;',
  11601. ' length_: longint external name ''length'';',
  11602. ' end;',
  11603. 'var Rec: TRecA;',
  11604. 'begin',
  11605. ' rec.length_ := rec.length_',
  11606. '']);
  11607. ConvertProgram;
  11608. CheckSource('TestRecord_VarExternal',
  11609. LinesToStr([ // statements
  11610. 'rtl.recNewT(this, "TRecA", function () {',
  11611. ' this.i = 0;',
  11612. ' this.$eq = function (b) {',
  11613. ' return (this.i === b.i) && (this.length === b.length);',
  11614. ' };',
  11615. ' this.$assign = function (s) {',
  11616. ' this.i = s.i;',
  11617. ' this.length = s.length;',
  11618. ' return this;',
  11619. ' };',
  11620. '});',
  11621. 'this.Rec = this.TRecA.$new();',
  11622. '']),
  11623. LinesToStr([ // $mod.$main
  11624. '$mod.Rec.length = $mod.Rec.length;'
  11625. ]));
  11626. end;
  11627. procedure TTestModule.TestRecord_WithDo;
  11628. begin
  11629. StartProgram(false);
  11630. Add('type');
  11631. Add(' TRec = record');
  11632. Add(' vI: longint;');
  11633. Add(' end;');
  11634. Add('var');
  11635. Add(' Int: longint;');
  11636. Add(' r: TRec;');
  11637. Add('begin');
  11638. Add(' with r do');
  11639. Add(' int:=vi;');
  11640. Add(' with r do begin');
  11641. Add(' int:=vi;');
  11642. Add(' vi:=int;');
  11643. Add(' end;');
  11644. ConvertProgram;
  11645. CheckSource('TestWithRecordDo',
  11646. LinesToStr([ // statements
  11647. 'rtl.recNewT(this, "TRec", function () {',
  11648. ' this.vI = 0;',
  11649. ' this.$eq = function (b) {',
  11650. ' return this.vI === b.vI;',
  11651. ' };',
  11652. ' this.$assign = function (s) {',
  11653. ' this.vI = s.vI;',
  11654. ' return this;',
  11655. ' };',
  11656. '});',
  11657. 'this.Int = 0;',
  11658. 'this.r = this.TRec.$new();',
  11659. '']),
  11660. LinesToStr([ // $mod.$main
  11661. 'var $with = $mod.r;',
  11662. '$mod.Int = $with.vI;',
  11663. 'var $with1 = $mod.r;',
  11664. '$mod.Int = $with1.vI;',
  11665. '$with1.vI = $mod.Int;'
  11666. ]));
  11667. end;
  11668. procedure TTestModule.TestRecord_Assign;
  11669. begin
  11670. StartProgram(false);
  11671. Add([
  11672. 'type',
  11673. ' TEnum = (red,green);',
  11674. ' TEnums = set of TEnum;',
  11675. ' TSmallRec = record',
  11676. ' N: longint;',
  11677. ' end;',
  11678. ' TBigRec = record',
  11679. ' Int: longint;',
  11680. ' D: double;',
  11681. ' Arr: array of longint;',
  11682. ' Arr2: array[1..2] of longint;',
  11683. ' Small: TSmallRec;',
  11684. ' Enums: TEnums;',
  11685. ' end;',
  11686. 'var',
  11687. ' r, s: TBigRec;',
  11688. 'begin',
  11689. ' r:=s;',
  11690. ' r:=default(TBigRec);',
  11691. ' r:=default(s);',
  11692. '']);
  11693. ConvertProgram;
  11694. CheckSource('TestRecord_Assign',
  11695. LinesToStr([ // statements
  11696. 'this.TEnum = {',
  11697. ' "0": "red",',
  11698. ' red: 0,',
  11699. ' "1": "green",',
  11700. ' green: 1',
  11701. '};',
  11702. 'rtl.recNewT(this, "TSmallRec", function () {',
  11703. ' this.N = 0;',
  11704. ' this.$eq = function (b) {',
  11705. ' return this.N === b.N;',
  11706. ' };',
  11707. ' this.$assign = function (s) {',
  11708. ' this.N = s.N;',
  11709. ' return this;',
  11710. ' };',
  11711. '});',
  11712. 'rtl.recNewT(this, "TBigRec", function () {',
  11713. ' this.Int = 0;',
  11714. ' this.D = 0.0;',
  11715. ' this.$new = function () {',
  11716. ' var r = Object.create(this);',
  11717. ' r.Arr = [];',
  11718. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  11719. ' r.Small = $mod.TSmallRec.$new();',
  11720. ' r.Enums = {};',
  11721. ' return r;',
  11722. ' };',
  11723. ' this.$eq = function (b) {',
  11724. ' 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);',
  11725. ' };',
  11726. ' this.$assign = function (s) {',
  11727. ' this.Int = s.Int;',
  11728. ' this.D = s.D;',
  11729. ' this.Arr = rtl.arrayRef(s.Arr);',
  11730. ' this.Arr2 = s.Arr2.slice(0);',
  11731. ' this.Small.$assign(s.Small);',
  11732. ' this.Enums = rtl.refSet(s.Enums);',
  11733. ' return this;',
  11734. ' };',
  11735. '});',
  11736. 'this.r = this.TBigRec.$new();',
  11737. 'this.s = this.TBigRec.$new();',
  11738. '']),
  11739. LinesToStr([ // $mod.$main
  11740. '$mod.r.$assign($mod.s);',
  11741. '$mod.r.$assign($mod.TBigRec.$new());',
  11742. '$mod.r.$assign($mod.TBigRec.$new());',
  11743. '']));
  11744. end;
  11745. procedure TTestModule.TestRecord_AsParams;
  11746. begin
  11747. StartProgram(false);
  11748. Add([
  11749. 'type',
  11750. ' integer = longint;',
  11751. ' TRecord = record',
  11752. ' i: integer;',
  11753. ' end;',
  11754. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  11755. 'var vL: TRecord;',
  11756. 'begin',
  11757. ' vd:=vd;',
  11758. ' vd.i:=vd.i;',
  11759. ' vl:=vc;',
  11760. ' vv:=vv;',
  11761. ' vv.i:=vv.i;',
  11762. ' U:=vl;',
  11763. ' U:=vd;',
  11764. ' U:=vc;',
  11765. ' U:=vv;',
  11766. ' vl:=TRecord(U);',
  11767. ' vd:=TRecord(U);',
  11768. ' vv:=TRecord(U);',
  11769. ' doit(vd,vd,vd,vd);',
  11770. ' doit(vc,vc,vl,vl);',
  11771. ' doit(vv,vv,vv,vv);',
  11772. ' doit(vl,vl,vl,vl);',
  11773. ' TRecord(U).i:=3;',
  11774. 'end;',
  11775. 'var i: TRecord;',
  11776. 'begin',
  11777. ' doit(i,i,i,i);',
  11778. '']);
  11779. ConvertProgram;
  11780. CheckSource('TestRecord_AsParams',
  11781. LinesToStr([ // statements
  11782. 'rtl.recNewT(this, "TRecord", function () {',
  11783. ' this.i = 0;',
  11784. ' this.$eq = function (b) {',
  11785. ' return this.i === b.i;',
  11786. ' };',
  11787. ' this.$assign = function (s) {',
  11788. ' this.i = s.i;',
  11789. ' return this;',
  11790. ' };',
  11791. '});',
  11792. 'this.DoIt = function (vD, vC, vV, U) {',
  11793. ' var vL = $mod.TRecord.$new();',
  11794. ' vD.$assign(vD);',
  11795. ' vD.i = vD.i;',
  11796. ' vL.$assign(vC);',
  11797. ' vV.$assign(vV);',
  11798. ' vV.i = vV.i;',
  11799. ' U.$assign(vL);',
  11800. ' U.$assign(vD);',
  11801. ' U.$assign(vC);',
  11802. ' U.$assign(vV);',
  11803. ' vL.$assign(U);',
  11804. ' vD.$assign(U);',
  11805. ' vV.$assign(U);',
  11806. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  11807. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  11808. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  11809. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  11810. ' U.i = 3;',
  11811. '};',
  11812. 'this.i = this.TRecord.$new();'
  11813. ]),
  11814. LinesToStr([
  11815. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11816. '']));
  11817. end;
  11818. procedure TTestModule.TestRecord_ConstRef;
  11819. begin
  11820. StartProgram(false);
  11821. Add([
  11822. 'type TRec = record i: word; end;',
  11823. 'procedure Run(constref a: TRec);',
  11824. 'begin',
  11825. 'end;',
  11826. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11827. 'var l: TRec;',
  11828. 'begin',
  11829. ' Run(l);',
  11830. ' Run(a);',
  11831. ' Run(b);',
  11832. ' Run(c);',
  11833. ' Run(d);',
  11834. ' Run(e);',
  11835. 'end;',
  11836. 'begin',
  11837. '']);
  11838. ConvertProgram;
  11839. CheckResolverUnexpectedHints();
  11840. CheckSource('TestRecord_ConstRef',
  11841. LinesToStr([ // statements
  11842. 'rtl.recNewT(this, "TRec", function () {',
  11843. ' this.i = 0;',
  11844. ' this.$eq = function (b) {',
  11845. ' return this.i === b.i;',
  11846. ' };',
  11847. ' this.$assign = function (s) {',
  11848. ' this.i = s.i;',
  11849. ' return this;',
  11850. ' };',
  11851. '});',
  11852. 'this.Run = function (a) {',
  11853. '};',
  11854. 'this.Fly = function (a, b, c, d, e) {',
  11855. ' var l = $mod.TRec.$new();',
  11856. ' $mod.Run(l);',
  11857. ' $mod.Run(a);',
  11858. ' $mod.Run(b);',
  11859. ' $mod.Run(c);',
  11860. ' $mod.Run(d);',
  11861. ' $mod.Run(e);',
  11862. '};',
  11863. '']),
  11864. LinesToStr([
  11865. '']));
  11866. end;
  11867. procedure TTestModule.TestRecordElement_AsParams;
  11868. begin
  11869. StartProgram(false);
  11870. Add('type');
  11871. Add(' integer = longint;');
  11872. Add(' TRecord = record');
  11873. Add(' i: integer;');
  11874. Add(' end;');
  11875. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11876. Add('var vJ: TRecord;');
  11877. Add('begin');
  11878. Add(' doit(vj.i,vj.i,vj.i);');
  11879. Add('end;');
  11880. Add('var r: TRecord;');
  11881. Add('begin');
  11882. Add(' doit(r.i,r.i,r.i);');
  11883. ConvertProgram;
  11884. CheckSource('TestRecordElement_AsParams',
  11885. LinesToStr([ // statements
  11886. 'rtl.recNewT(this, "TRecord", function () {',
  11887. ' this.i = 0;',
  11888. ' this.$eq = function (b) {',
  11889. ' return this.i === b.i;',
  11890. ' };',
  11891. ' this.$assign = function (s) {',
  11892. ' this.i = s.i;',
  11893. ' return this;',
  11894. ' };',
  11895. '});',
  11896. 'this.DoIt = function (vG,vH,vI) {',
  11897. ' var vJ = $mod.TRecord.$new();',
  11898. ' $mod.DoIt(vJ.i, vJ.i, {',
  11899. ' p: vJ,',
  11900. ' get: function () {',
  11901. ' return this.p.i;',
  11902. ' },',
  11903. ' set: function (v) {',
  11904. ' this.p.i = v;',
  11905. ' }',
  11906. ' });',
  11907. '};',
  11908. 'this.r = this.TRecord.$new();'
  11909. ]),
  11910. LinesToStr([
  11911. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11912. ' p: $mod.r,',
  11913. ' get: function () {',
  11914. ' return this.p.i;',
  11915. ' },',
  11916. ' set: function (v) {',
  11917. ' this.p.i = v;',
  11918. ' }',
  11919. '});'
  11920. ]));
  11921. end;
  11922. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11923. begin
  11924. StartProgram(false);
  11925. Add('type');
  11926. Add(' integer = longint;');
  11927. Add(' TRecord = record');
  11928. Add(' i: integer;');
  11929. Add(' end;');
  11930. Add('function GetRec(vB: integer = 0): TRecord;');
  11931. Add('begin');
  11932. Add('end;');
  11933. Add('procedure DoIt(vG: integer; const vH: integer);');
  11934. Add('begin');
  11935. Add('end;');
  11936. Add('begin');
  11937. Add(' doit(getrec.i,getrec.i);');
  11938. Add(' doit(getrec().i,getrec().i);');
  11939. Add(' doit(getrec(1).i,getrec(2).i);');
  11940. ConvertProgram;
  11941. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11942. LinesToStr([ // statements
  11943. 'rtl.recNewT(this, "TRecord", function () {',
  11944. ' this.i = 0;',
  11945. ' this.$eq = function (b) {',
  11946. ' return this.i === b.i;',
  11947. ' };',
  11948. ' this.$assign = function (s) {',
  11949. ' this.i = s.i;',
  11950. ' return this;',
  11951. ' };',
  11952. '});',
  11953. 'this.GetRec = function (vB) {',
  11954. ' var Result = $mod.TRecord.$new();',
  11955. ' return Result;',
  11956. '};',
  11957. 'this.DoIt = function (vG, vH) {',
  11958. '};',
  11959. '']),
  11960. LinesToStr([
  11961. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11962. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11963. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11964. '']));
  11965. end;
  11966. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11967. begin
  11968. StartProgram(false);
  11969. Add('type');
  11970. Add(' integer = longint;');
  11971. Add(' TRecord = record');
  11972. Add(' i: integer;');
  11973. Add(' end;');
  11974. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11975. Add('begin');
  11976. Add('end;');
  11977. Add('var r: trecord;');
  11978. Add('begin');
  11979. Add(' with r do ');
  11980. Add(' doit(i,i,i);');
  11981. ConvertProgram;
  11982. CheckSource('TestRecordElementFromWith_AsParams',
  11983. LinesToStr([ // statements
  11984. 'rtl.recNewT(this, "TRecord", function () {',
  11985. ' this.i = 0;',
  11986. ' this.$eq = function (b) {',
  11987. ' return this.i === b.i;',
  11988. ' };',
  11989. ' this.$assign = function (s) {',
  11990. ' this.i = s.i;',
  11991. ' return this;',
  11992. ' };',
  11993. '});',
  11994. 'this.DoIt = function (vG,vH,vI) {',
  11995. '};',
  11996. 'this.r = this.TRecord.$new();'
  11997. ]),
  11998. LinesToStr([
  11999. 'var $with = $mod.r;',
  12000. '$mod.DoIt($with.i,$with.i,{',
  12001. ' p: $with,',
  12002. ' get: function () {',
  12003. ' return this.p.i;',
  12004. ' },',
  12005. ' set: function (v) {',
  12006. ' this.p.i = v;',
  12007. ' }',
  12008. '});',
  12009. '']));
  12010. end;
  12011. procedure TTestModule.TestRecord_Equal;
  12012. begin
  12013. StartProgram(false);
  12014. Add('type');
  12015. Add(' integer = longint;');
  12016. Add(' TFlag = (red,blue);');
  12017. Add(' TFlags = set of TFlag;');
  12018. Add(' TProc = procedure;');
  12019. Add(' TRecord = record');
  12020. Add(' i: integer;');
  12021. Add(' Event: TProc;');
  12022. Add(' f: TFlags;');
  12023. Add(' end;');
  12024. Add(' TNested = record');
  12025. Add(' r: TRecord;');
  12026. Add(' end;');
  12027. Add('var');
  12028. Add(' b: boolean;');
  12029. Add(' r,s: trecord;');
  12030. Add('begin');
  12031. Add(' b:=r=s;');
  12032. Add(' b:=r<>s;');
  12033. ConvertProgram;
  12034. CheckSource('TestRecord_Equal',
  12035. LinesToStr([ // statements
  12036. 'this.TFlag = {',
  12037. ' "0": "red",',
  12038. ' red: 0,',
  12039. ' "1": "blue",',
  12040. ' blue: 1',
  12041. '};',
  12042. 'rtl.recNewT(this, "TRecord", function () {',
  12043. ' this.i = 0;',
  12044. ' this.Event = null;',
  12045. ' this.$new = function () {',
  12046. ' var r = Object.create(this);',
  12047. ' r.f = {};',
  12048. ' return r;',
  12049. ' };',
  12050. ' this.$eq = function (b) {',
  12051. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12052. ' };',
  12053. ' this.$assign = function (s) {',
  12054. ' this.i = s.i;',
  12055. ' this.Event = s.Event;',
  12056. ' this.f = rtl.refSet(s.f);',
  12057. ' return this;',
  12058. ' };',
  12059. '});',
  12060. 'rtl.recNewT(this, "TNested", function () {',
  12061. ' this.$new = function () {',
  12062. ' var r = Object.create(this);',
  12063. ' r.r = $mod.TRecord.$new();',
  12064. ' return r;',
  12065. ' };',
  12066. ' this.$eq = function (b) {',
  12067. ' return this.r.$eq(b.r);',
  12068. ' };',
  12069. ' this.$assign = function (s) {',
  12070. ' this.r.$assign(s.r);',
  12071. ' return this;',
  12072. ' };',
  12073. '});',
  12074. 'this.b = false;',
  12075. 'this.r = this.TRecord.$new();',
  12076. 'this.s = this.TRecord.$new();',
  12077. '']),
  12078. LinesToStr([
  12079. '$mod.b = $mod.r.$eq($mod.s);',
  12080. '$mod.b = !$mod.r.$eq($mod.s);',
  12081. '']));
  12082. end;
  12083. procedure TTestModule.TestRecord_JSValue;
  12084. begin
  12085. StartProgram(false);
  12086. Add([
  12087. 'type',
  12088. ' TRecord = record',
  12089. ' i: longint;',
  12090. ' end;',
  12091. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12092. 'begin',
  12093. 'end;',
  12094. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12095. 'begin',
  12096. ' if jsvalue(d) then ;',
  12097. ' if jsvalue(c) then ;',
  12098. ' if jsvalue(v) then ;',
  12099. 'end;',
  12100. 'var',
  12101. ' Jv: jsvalue;',
  12102. ' Rec: trecord;',
  12103. 'begin',
  12104. ' rec:=trecord(jv);',
  12105. ' jv:=rec;',
  12106. ' Fly(rec,rec);',
  12107. ' Fly(@rec,@rec);',
  12108. ' if jsvalue(Rec) then ;',
  12109. ' Run(trecord(jv),trecord(jv),rec);',
  12110. '']);
  12111. ConvertProgram;
  12112. CheckSource('TestRecord_JSValue',
  12113. LinesToStr([ // statements
  12114. 'rtl.recNewT(this, "TRecord", function () {',
  12115. ' this.i = 0;',
  12116. ' this.$eq = function (b) {',
  12117. ' return this.i === b.i;',
  12118. ' };',
  12119. ' this.$assign = function (s) {',
  12120. ' this.i = s.i;',
  12121. ' return this;',
  12122. ' };',
  12123. '});',
  12124. 'this.Fly = function (d, c) {',
  12125. '};',
  12126. 'this.Run = function (d, c, v) {',
  12127. ' if (d) ;',
  12128. ' if (c) ;',
  12129. ' if (v) ;',
  12130. '};',
  12131. 'this.Jv = undefined;',
  12132. 'this.Rec = this.TRecord.$new();',
  12133. '']),
  12134. LinesToStr([
  12135. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12136. '$mod.Jv = $mod.Rec;',
  12137. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12138. '$mod.Fly($mod.Rec, $mod.Rec);',
  12139. 'if ($mod.Rec) ;',
  12140. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12141. '']));
  12142. end;
  12143. procedure TTestModule.TestRecord_VariantFail;
  12144. begin
  12145. StartProgram(false);
  12146. Add([
  12147. 'type',
  12148. ' TRec = record',
  12149. ' case word of',
  12150. ' 0: (b0, b1: Byte);',
  12151. ' 1: (i: word);',
  12152. ' end;',
  12153. 'begin']);
  12154. SetExpectedPasResolverError('variant record is not supported',
  12155. nXIsNotSupported);
  12156. ConvertProgram;
  12157. end;
  12158. procedure TTestModule.TestRecord_FieldArray;
  12159. begin
  12160. StartProgram(false);
  12161. Add([
  12162. 'type',
  12163. ' TArrInt = array[3..4] of longint;',
  12164. ' TArrArrInt = array[3..4] of longint;',
  12165. ' TRec = record',
  12166. ' a: array of longint;',
  12167. ' s: array[1..2] of longint;',
  12168. ' m: array[1..2,3..4] of longint;',
  12169. ' o: TArrArrInt;',
  12170. ' end;',
  12171. 'begin']);
  12172. ConvertProgram;
  12173. CheckSource('TestRecord_FieldArray',
  12174. LinesToStr([ // statements
  12175. 'rtl.recNewT(this, "TRec", function () {',
  12176. ' this.m$a$clone = function (a) {',
  12177. ' var b = [];',
  12178. ' b.length = 2;',
  12179. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12180. ' return b;',
  12181. ' };',
  12182. ' this.$new = function () {',
  12183. ' var r = Object.create(this);',
  12184. ' r.a = [];',
  12185. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12186. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12187. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12188. ' return r;',
  12189. ' };',
  12190. ' this.$eq = function (b) {',
  12191. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12192. ' };',
  12193. ' this.$assign = function (s) {',
  12194. ' this.a = rtl.arrayRef(s.a);',
  12195. ' this.s = s.s.slice(0);',
  12196. ' this.m = this.m$a$clone(s.m);',
  12197. ' this.o = s.o.slice(0);',
  12198. ' return this;',
  12199. ' };',
  12200. '});',
  12201. '']),
  12202. LinesToStr([ // $mod.$main
  12203. '']));
  12204. end;
  12205. procedure TTestModule.TestRecord_Const;
  12206. begin
  12207. StartProgram(false);
  12208. Add([
  12209. 'type',
  12210. ' TArrInt = array[3..4] of longint;',
  12211. ' TPoint = record x,y: longint; end;',
  12212. ' TRec = record',
  12213. ' i: longint;',
  12214. ' a: array of longint;',
  12215. ' s: array[1..2] of longint;',
  12216. ' m: array[1..2,3..4] of longint;',
  12217. ' p: TPoint;',
  12218. ' end;',
  12219. ' TPoints = array of TPoint;',
  12220. 'const',
  12221. ' r: TRec = (',
  12222. ' i:1;',
  12223. ' a:(2,3);',
  12224. ' s:(4,5);',
  12225. ' m:( (11,12), (13,14) );',
  12226. ' p: (x:21; y:22)',
  12227. ' );',
  12228. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12229. 'begin']);
  12230. ConvertProgram;
  12231. CheckSource('TestRecord_Const',
  12232. LinesToStr([ // statements
  12233. 'rtl.recNewT(this, "TPoint", function () {',
  12234. ' this.x = 0;',
  12235. ' this.y = 0;',
  12236. ' this.$eq = function (b) {',
  12237. ' return (this.x === b.x) && (this.y === b.y);',
  12238. ' };',
  12239. ' this.$assign = function (s) {',
  12240. ' this.x = s.x;',
  12241. ' this.y = s.y;',
  12242. ' return this;',
  12243. ' };',
  12244. '});',
  12245. 'rtl.recNewT(this, "TRec", function () {',
  12246. ' this.i = 0;',
  12247. ' this.m$a$clone = function (a) {',
  12248. ' var b = [];',
  12249. ' b.length = 2;',
  12250. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12251. ' return b;',
  12252. ' };',
  12253. ' this.$new = function () {',
  12254. ' var r = Object.create(this);',
  12255. ' r.a = [];',
  12256. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12257. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12258. ' r.p = $mod.TPoint.$new();',
  12259. ' return r;',
  12260. ' };',
  12261. ' this.$eq = function (b) {',
  12262. ' 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);',
  12263. ' };',
  12264. ' this.$assign = function (s) {',
  12265. ' this.i = s.i;',
  12266. ' this.a = rtl.arrayRef(s.a);',
  12267. ' this.s = s.s.slice(0);',
  12268. ' this.m = this.m$a$clone(s.m);',
  12269. ' this.p.$assign(s.p);',
  12270. ' return this;',
  12271. ' };',
  12272. '});',
  12273. 'this.r = this.TRec.$clone({',
  12274. ' i: 1,',
  12275. ' a: [2, 3],',
  12276. ' s: [4, 5],',
  12277. ' m: [[11, 12], [13, 14]],',
  12278. ' p: this.TPoint.$clone({',
  12279. ' x: 21,',
  12280. ' y: 22',
  12281. ' })',
  12282. '});',
  12283. 'this.p = [this.TPoint.$clone({',
  12284. ' x: 1,',
  12285. ' y: 2',
  12286. '}), this.TPoint.$clone({',
  12287. ' x: 3,',
  12288. ' y: 4',
  12289. '})];',
  12290. '']),
  12291. LinesToStr([ // $mod.$main
  12292. '']));
  12293. end;
  12294. procedure TTestModule.TestRecord_TypecastFail;
  12295. begin
  12296. StartProgram(false);
  12297. Add([
  12298. 'type',
  12299. ' TPoint = record x,y: longint; end;',
  12300. ' TRec = record l: longint end;',
  12301. 'var p: TPoint;',
  12302. 'begin',
  12303. ' if TRec(p).l=2 then ;']);
  12304. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12305. nIllegalTypeConversionTo);
  12306. ConvertProgram;
  12307. end;
  12308. procedure TTestModule.TestRecord_InFunction;
  12309. begin
  12310. StartProgram(false);
  12311. Add([
  12312. 'var TPoint: longint = 3;',
  12313. 'procedure DoIt;',
  12314. 'type',
  12315. ' TPoint = record x,y: longint; end;',
  12316. ' TPoints = array of TPoint;',
  12317. 'var',
  12318. ' r: TPoint;',
  12319. ' p: TPoints;',
  12320. 'begin',
  12321. ' SetLength(p,2);',
  12322. 'end;',
  12323. 'begin']);
  12324. ConvertProgram;
  12325. CheckSource('TestRecord_InFunction',
  12326. LinesToStr([ // statements
  12327. 'this.TPoint = 3;',
  12328. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12329. ' this.x = 0;',
  12330. ' this.y = 0;',
  12331. ' this.$eq = function (b) {',
  12332. ' return (this.x === b.x) && (this.y === b.y);',
  12333. ' };',
  12334. ' this.$assign = function (s) {',
  12335. ' this.x = s.x;',
  12336. ' this.y = s.y;',
  12337. ' return this;',
  12338. ' };',
  12339. '});',
  12340. 'this.DoIt = function () {',
  12341. ' var r = TPoint$1.$new();',
  12342. ' var p = [];',
  12343. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12344. '};',
  12345. '']),
  12346. LinesToStr([ // $mod.$main
  12347. '']));
  12348. end;
  12349. procedure TTestModule.TestRecord_AnonymousFail;
  12350. begin
  12351. StartProgram(false);
  12352. Add([
  12353. 'var',
  12354. ' r: record x: word end;',
  12355. 'begin']);
  12356. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  12357. nNotYetImplemented);
  12358. ConvertProgram;
  12359. end;
  12360. procedure TTestModule.TestAdvRecord_Function;
  12361. begin
  12362. StartProgram(false);
  12363. Parser.Options:=Parser.Options+[po_cassignments];
  12364. Add([
  12365. '{$modeswitch AdvancedRecords}',
  12366. 'type',
  12367. ' TPoint = record',
  12368. ' x,y: word;',
  12369. ' function Add(const apt: TPoint): TPoint;',
  12370. ' end;',
  12371. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12372. 'begin',
  12373. ' Result:=Self;',
  12374. ' Result.x+=apt.x;',
  12375. ' Result.y:=Result.y+apt.y;',
  12376. ' Self:=apt;',
  12377. 'end;',
  12378. 'var p,q: TPoint;',
  12379. 'begin',
  12380. ' p.add(q);',
  12381. ' p:=default(TPoint);',
  12382. ' p:=q;',
  12383. '']);
  12384. ConvertProgram;
  12385. CheckSource('TestAdvRecord_Function',
  12386. LinesToStr([ // statements
  12387. 'rtl.recNewT(this, "TPoint", function () {',
  12388. ' this.x = 0;',
  12389. ' this.y = 0;',
  12390. ' this.$eq = function (b) {',
  12391. ' return (this.x === b.x) && (this.y === b.y);',
  12392. ' };',
  12393. ' this.$assign = function (s) {',
  12394. ' this.x = s.x;',
  12395. ' this.y = s.y;',
  12396. ' return this;',
  12397. ' };',
  12398. ' this.Add = function (apt) {',
  12399. ' var Result = $mod.TPoint.$new();',
  12400. ' Result.$assign(this);',
  12401. ' Result.x += apt.x;',
  12402. ' Result.y = Result.y + apt.y;',
  12403. ' this.$assign(apt);',
  12404. ' return Result;',
  12405. ' };',
  12406. '});',
  12407. 'this.p = this.TPoint.$new();',
  12408. 'this.q = this.TPoint.$new();',
  12409. '']),
  12410. LinesToStr([ // $mod.$main
  12411. '$mod.p.Add($mod.q);',
  12412. '$mod.p.$assign($mod.TPoint.$new());',
  12413. '$mod.p.$assign($mod.q);',
  12414. '']));
  12415. end;
  12416. procedure TTestModule.TestAdvRecord_Property;
  12417. begin
  12418. StartProgram(false);
  12419. Add([
  12420. '{$modeswitch AdvancedRecords}',
  12421. 'type',
  12422. ' TPoint = record',
  12423. ' x,y: word;',
  12424. ' strict private',
  12425. ' function GetSize: longword;',
  12426. ' procedure SetSize(Value: longword);',
  12427. ' public',
  12428. ' property Size: longword read GetSize write SetSize;',
  12429. ' property Left: word read x write y;',
  12430. ' end;',
  12431. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  12432. 'function TPoint.GetSize: longword;',
  12433. 'begin',
  12434. ' x:=y;',
  12435. ' Size:=Size;',
  12436. ' Left:=Left;',
  12437. 'end;',
  12438. 'procedure TPoint.SetSize(Value: longword);',
  12439. 'begin',
  12440. 'end;',
  12441. 'var p,q: TPoint;',
  12442. 'begin',
  12443. ' p.Size:=q.Size;',
  12444. ' p.Left:=q.Left;',
  12445. '']);
  12446. ConvertProgram;
  12447. CheckSource('TestAdvRecord_Property',
  12448. LinesToStr([ // statements
  12449. 'rtl.recNewT(this, "TPoint", function () {',
  12450. ' this.x = 0;',
  12451. ' this.y = 0;',
  12452. ' this.$eq = function (b) {',
  12453. ' return (this.x === b.x) && (this.y === b.y);',
  12454. ' };',
  12455. ' this.$assign = function (s) {',
  12456. ' this.x = s.x;',
  12457. ' this.y = s.y;',
  12458. ' return this;',
  12459. ' };',
  12460. ' this.GetSize = function () {',
  12461. ' var Result = 0;',
  12462. ' this.x = this.y;',
  12463. ' this.SetSize(this.GetSize());',
  12464. ' this.y = this.x;',
  12465. ' return Result;',
  12466. ' };',
  12467. ' this.SetSize = function (Value) {',
  12468. ' };',
  12469. '});',
  12470. 'this.SetSize = function (Value) {',
  12471. '};',
  12472. 'this.p = this.TPoint.$new();',
  12473. 'this.q = this.TPoint.$new();',
  12474. '']),
  12475. LinesToStr([ // $mod.$main
  12476. '$mod.p.SetSize($mod.q.GetSize());',
  12477. '$mod.p.y = $mod.q.x;',
  12478. '']));
  12479. end;
  12480. procedure TTestModule.TestAdvRecord_PropertyDefault;
  12481. begin
  12482. StartProgram(false);
  12483. Add([
  12484. '{$modeswitch AdvancedRecords}',
  12485. 'type',
  12486. ' TPoint = record',
  12487. ' strict private',
  12488. ' function GetItems(Index: word): word;',
  12489. ' procedure SetItems(Index: word; Value: word);',
  12490. ' public',
  12491. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  12492. ' end;',
  12493. 'function TPoint.GetItems(Index: word): word;',
  12494. 'begin',
  12495. ' Items[index]:=Items[index];',
  12496. ' self.Items[index]:=self.Items[index];',
  12497. 'end;',
  12498. 'procedure TPoint.SetItems(Index: word; Value: word);',
  12499. 'begin',
  12500. 'end;',
  12501. 'var p: TPoint;',
  12502. 'begin',
  12503. ' p[1]:=p[2];',
  12504. ' p.Items[3]:=p.Items[4];',
  12505. '']);
  12506. ConvertProgram;
  12507. CheckSource('TestAdvRecord_PropertyDefault',
  12508. LinesToStr([ // statements
  12509. 'rtl.recNewT(this, "TPoint", function () {',
  12510. ' this.$eq = function (b) {',
  12511. ' return true;',
  12512. ' };',
  12513. ' this.$assign = function (s) {',
  12514. ' return this;',
  12515. ' };',
  12516. ' this.GetItems = function (Index) {',
  12517. ' var Result = 0;',
  12518. ' this.SetItems(Index, this.GetItems(Index));',
  12519. ' this.SetItems(Index, this.GetItems(Index));',
  12520. ' return Result;',
  12521. ' };',
  12522. ' this.SetItems = function (Index, Value) {',
  12523. ' };',
  12524. '});',
  12525. 'this.p = this.TPoint.$new();',
  12526. '']),
  12527. LinesToStr([ // $mod.$main
  12528. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  12529. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  12530. '']));
  12531. end;
  12532. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  12533. begin
  12534. StartProgram(false);
  12535. Add([
  12536. '{$modeswitch AdvancedRecords}',
  12537. 'type',
  12538. ' TRec = record',
  12539. ' class var',
  12540. ' Fx: longint;',
  12541. ' Fy: longint;',
  12542. ' class function GetInt: longint; static;',
  12543. ' class procedure SetInt(Value: longint); static;',
  12544. ' class procedure DoIt; static;',
  12545. ' class property IntA: longint read Fx write Fy;',
  12546. ' class property IntB: longint read GetInt write SetInt;',
  12547. ' end;',
  12548. 'class function trec.getint: longint;',
  12549. 'begin',
  12550. ' result:=fx;',
  12551. 'end;',
  12552. 'class procedure trec.setint(value: longint);',
  12553. 'begin',
  12554. 'end;',
  12555. 'class procedure trec.doit;',
  12556. 'begin',
  12557. ' IntA:=IntA+1;',
  12558. ' IntB:=IntB+1;',
  12559. 'end;',
  12560. 'var r: trec;',
  12561. 'begin',
  12562. ' trec.inta:=trec.inta+1;',
  12563. ' if trec.intb=2 then;',
  12564. ' trec.intb:=trec.intb+2;',
  12565. ' trec.setint(trec.inta);',
  12566. ' r.inta:=r.inta+1;',
  12567. ' if r.intb=2 then;',
  12568. ' r.intb:=r.intb+2;',
  12569. ' r.setint(r.inta);']);
  12570. ConvertProgram;
  12571. CheckSource('TestAdvRecord_Property_ClassMethod',
  12572. LinesToStr([ // statements
  12573. 'rtl.recNewT(this, "TRec", function () {',
  12574. ' this.Fx = 0;',
  12575. ' this.Fy = 0;',
  12576. ' this.$eq = function (b) {',
  12577. ' return true;',
  12578. ' };',
  12579. ' this.$assign = function (s) {',
  12580. ' return this;',
  12581. ' };',
  12582. ' this.GetInt = function () {',
  12583. ' var Result = 0;',
  12584. ' Result = $mod.TRec.Fx;',
  12585. ' return Result;',
  12586. ' };',
  12587. ' this.SetInt = function (Value) {',
  12588. ' };',
  12589. ' this.DoIt = function () {',
  12590. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12591. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  12592. ' };',
  12593. '}, true);',
  12594. 'this.r = this.TRec.$new();',
  12595. '']),
  12596. LinesToStr([ // $mod.$main
  12597. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12598. 'if ($mod.TRec.GetInt() === 2) ;',
  12599. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12600. '$mod.TRec.SetInt($mod.TRec.Fx);',
  12601. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  12602. 'if ($mod.TRec.GetInt() === 2) ;',
  12603. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12604. '$mod.TRec.SetInt($mod.r.Fx);',
  12605. '']));
  12606. end;
  12607. procedure TTestModule.TestAdvRecord_Const;
  12608. begin
  12609. StartProgram(false);
  12610. Add([
  12611. '{$modeswitch AdvancedRecords}',
  12612. 'type',
  12613. ' TArrInt = array[3..4] of longint;',
  12614. ' TPoint = record',
  12615. ' x,y: longint;',
  12616. ' class var Count: nativeint;',
  12617. ' end;',
  12618. ' TRec = record',
  12619. ' i: longint;',
  12620. ' a: array of longint;',
  12621. ' s: array[1..2] of longint;',
  12622. ' m: array[1..2,3..4] of longint;',
  12623. ' p: TPoint;',
  12624. ' end;',
  12625. ' TPoints = array of TPoint;',
  12626. 'const',
  12627. ' r: TRec = (',
  12628. ' i:1;',
  12629. ' a:(2,3);',
  12630. ' s:(4,5);',
  12631. ' m:( (11,12), (13,14) );',
  12632. ' p: (x:21)',
  12633. ' );',
  12634. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12635. 'begin']);
  12636. ConvertProgram;
  12637. CheckSource('TestAdvRecord_Const',
  12638. LinesToStr([ // statements
  12639. 'rtl.recNewT(this, "TPoint", function () {',
  12640. ' this.x = 0;',
  12641. ' this.y = 0;',
  12642. ' this.Count = 0;',
  12643. ' this.$eq = function (b) {',
  12644. ' return (this.x === b.x) && (this.y === b.y);',
  12645. ' };',
  12646. ' this.$assign = function (s) {',
  12647. ' this.x = s.x;',
  12648. ' this.y = s.y;',
  12649. ' return this;',
  12650. ' };',
  12651. '}, true);',
  12652. 'rtl.recNewT(this, "TRec", function () {',
  12653. ' this.i = 0;',
  12654. ' this.m$a$clone = function (a) {',
  12655. ' var b = [];',
  12656. ' b.length = 2;',
  12657. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12658. ' return b;',
  12659. ' };',
  12660. ' this.$new = function () {',
  12661. ' var r = Object.create(this);',
  12662. ' r.a = [];',
  12663. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12664. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12665. ' r.p = $mod.TPoint.$new();',
  12666. ' return r;',
  12667. ' };',
  12668. ' this.$eq = function (b) {',
  12669. ' 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);',
  12670. ' };',
  12671. ' this.$assign = function (s) {',
  12672. ' this.i = s.i;',
  12673. ' this.a = rtl.arrayRef(s.a);',
  12674. ' this.s = s.s.slice(0);',
  12675. ' this.m = this.m$a$clone(s.m);',
  12676. ' this.p.$assign(s.p);',
  12677. ' return this;',
  12678. ' };',
  12679. '});',
  12680. 'this.r = this.TRec.$clone({',
  12681. ' i: 1,',
  12682. ' a: [2, 3],',
  12683. ' s: [4, 5],',
  12684. ' m: [[11, 12], [13, 14]],',
  12685. ' p: this.TPoint.$clone({',
  12686. ' x: 21,',
  12687. ' y: 0',
  12688. ' })',
  12689. '});',
  12690. 'this.p = [this.TPoint.$clone({',
  12691. ' x: 1,',
  12692. ' y: 2',
  12693. '}), this.TPoint.$clone({',
  12694. ' x: 3,',
  12695. ' y: 4',
  12696. '})];',
  12697. '']),
  12698. LinesToStr([ // $mod.$main
  12699. '']));
  12700. end;
  12701. procedure TTestModule.TestAdvRecord_ExternalField;
  12702. begin
  12703. StartProgram(false);
  12704. Add([
  12705. '{$modeswitch AdvancedRecords}',
  12706. '{$modeswitch externalclass}',
  12707. 'type',
  12708. ' TCar = record',
  12709. ' public',
  12710. ' Intern: longint external name ''$Intern'';',
  12711. ' Intern2: longint external name ''$Intern2'';',
  12712. ' Bracket: longint external name ''["A B"]'';',
  12713. ' procedure DoIt;',
  12714. ' end;',
  12715. 'procedure tcar.doit;',
  12716. 'begin',
  12717. ' Intern:=Intern+1;',
  12718. ' Intern2:=Intern2+2;',
  12719. ' Bracket:=Bracket+3;',
  12720. 'end;',
  12721. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  12722. 'begin',
  12723. ' Rec.intern:=Rec.intern+1;',
  12724. ' Rec.intern2:=Rec.intern2+2;',
  12725. ' Rec.Bracket:=Rec.Bracket+3;',
  12726. ' with Rec do begin',
  12727. ' intern:=intern+1;',
  12728. ' intern2:=intern2+2;',
  12729. ' Bracket:=Bracket+3;',
  12730. ' end;']);
  12731. ConvertProgram;
  12732. CheckSource('TestAdvRecord_ExternalField',
  12733. LinesToStr([ // statements
  12734. 'rtl.recNewT(this, "TCar", function () {',
  12735. ' this.$eq = function (b) {',
  12736. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  12737. ' };',
  12738. ' this.$assign = function (s) {',
  12739. ' this.$Intern = s.$Intern;',
  12740. ' this.$Intern2 = s.$Intern2;',
  12741. ' this["A B"] = s["A B"];',
  12742. ' return this;',
  12743. ' };',
  12744. ' this.DoIt = function () {',
  12745. ' this.$Intern = this.$Intern + 1;',
  12746. ' this.$Intern2 = this.$Intern2 + 2;',
  12747. ' this["A B"] = this["A B"] + 3;',
  12748. ' };',
  12749. '});',
  12750. 'this.Rec = this.TCar.$clone({',
  12751. ' $Intern: 11,',
  12752. ' $Intern2: 12,',
  12753. ' "A B": 13',
  12754. '});',
  12755. '']),
  12756. LinesToStr([ // $mod.$main
  12757. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  12758. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  12759. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  12760. 'var $with = $mod.Rec;',
  12761. '$with.$Intern = $with.$Intern + 1;',
  12762. '$with.$Intern2 = $with.$Intern2 + 2;',
  12763. '$with["A B"] = $with["A B"] + 3;',
  12764. '']));
  12765. end;
  12766. procedure TTestModule.TestAdvRecord_SubRecord;
  12767. begin
  12768. StartProgram(false);
  12769. Add([
  12770. '{$modeswitch AdvancedRecords}',
  12771. 'type',
  12772. ' TRec = record',
  12773. ' type',
  12774. ' TPoint = record',
  12775. ' x,y: longint;',
  12776. ' class var Count: nativeint;',
  12777. ' procedure DoIt;',
  12778. ' class procedure DoThat; static;',
  12779. ' end;',
  12780. ' var',
  12781. ' i: longint;',
  12782. ' p: TPoint;',
  12783. ' procedure DoSome;',
  12784. ' end;',
  12785. 'const',
  12786. ' r: TRec = (',
  12787. ' i:1;',
  12788. ' p: (x:21;y:22)',
  12789. ' );',
  12790. 'procedure TRec.DoSome;',
  12791. 'begin',
  12792. ' p.x:=p.y+1;',
  12793. ' p.Count:=p.Count+2;',
  12794. 'end;',
  12795. 'procedure TRec.TPoint.DoIt;',
  12796. 'begin',
  12797. ' Count:=Count+3;',
  12798. 'end;',
  12799. 'class procedure TRec.TPoint.DoThat;',
  12800. 'begin',
  12801. ' Count:=Count+4;',
  12802. 'end;',
  12803. 'begin']);
  12804. ConvertProgram;
  12805. CheckSource('TestAdvRecord_SubRecord',
  12806. LinesToStr([ // statements
  12807. 'rtl.recNewT(this, "TRec", function () {',
  12808. ' rtl.recNewT(this, "TPoint", function () {',
  12809. ' this.x = 0;',
  12810. ' this.y = 0;',
  12811. ' this.Count = 0;',
  12812. ' this.$eq = function (b) {',
  12813. ' return (this.x === b.x) && (this.y === b.y);',
  12814. ' };',
  12815. ' this.$assign = function (s) {',
  12816. ' this.x = s.x;',
  12817. ' this.y = s.y;',
  12818. ' return this;',
  12819. ' };',
  12820. ' this.DoIt = function () {',
  12821. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  12822. ' };',
  12823. ' this.DoThat = function () {',
  12824. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  12825. ' };',
  12826. ' }, true);',
  12827. ' this.i = 0;',
  12828. ' this.$new = function () {',
  12829. ' var r = Object.create(this);',
  12830. ' r.p = this.TPoint.$new();',
  12831. ' return r;',
  12832. ' };',
  12833. ' this.$eq = function (b) {',
  12834. ' return (this.i === b.i) && this.p.$eq(b.p);',
  12835. ' };',
  12836. ' this.$assign = function (s) {',
  12837. ' this.i = s.i;',
  12838. ' this.p.$assign(s.p);',
  12839. ' return this;',
  12840. ' };',
  12841. ' this.DoSome = function () {',
  12842. ' this.p.x = this.p.y + 1;',
  12843. ' this.TPoint.Count = this.p.Count + 2;',
  12844. ' };',
  12845. '}, true);',
  12846. 'this.r = this.TRec.$clone({',
  12847. ' i: 1,',
  12848. ' p: this.TRec.TPoint.$clone({',
  12849. ' x: 21,',
  12850. ' y: 22',
  12851. ' })',
  12852. '});',
  12853. '']),
  12854. LinesToStr([ // $mod.$main
  12855. '']));
  12856. end;
  12857. procedure TTestModule.TestAdvRecord_SubClass;
  12858. begin
  12859. StartProgram(false);
  12860. Add([
  12861. '{$modeswitch AdvancedRecords}',
  12862. 'type',
  12863. ' TObject = class end;',
  12864. ' TPoint = record',
  12865. ' type',
  12866. ' TBird = class',
  12867. ' procedure DoIt;',
  12868. ' class procedure Glob;',
  12869. ' end;',
  12870. ' procedure DoIt(b: TBird);',
  12871. ' end;',
  12872. 'procedure TPoint.TBird.DoIt;',
  12873. 'begin',
  12874. ' doit;',
  12875. ' self.doit;',
  12876. ' glob;',
  12877. ' self.glob;',
  12878. 'end;',
  12879. 'class procedure TPoint.TBird.Glob;',
  12880. 'begin',
  12881. ' glob;',
  12882. ' self.glob;',
  12883. 'end;',
  12884. 'procedure TPoint.DoIt(b: TBird);',
  12885. 'begin',
  12886. ' b.doit;',
  12887. ' b.glob;',
  12888. ' TBird.glob;',
  12889. 'end;',
  12890. 'begin',
  12891. '']);
  12892. ConvertProgram;
  12893. CheckSource('TestAdvRecord_SubClass',
  12894. LinesToStr([ // statements
  12895. 'rtl.createClass(this, "TObject", null, function () {',
  12896. ' this.$init = function () {',
  12897. ' };',
  12898. ' this.$final = function () {',
  12899. ' };',
  12900. '});',
  12901. 'rtl.recNewT(this, "TPoint", function () {',
  12902. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12903. ' this.DoIt = function () {',
  12904. ' this.DoIt();',
  12905. ' this.DoIt();',
  12906. ' this.$class.Glob();',
  12907. ' this.$class.Glob();',
  12908. ' };',
  12909. ' this.Glob = function () {',
  12910. ' this.Glob();',
  12911. ' this.Glob();',
  12912. ' };',
  12913. ' }, "TPoint.TBird");',
  12914. ' this.$eq = function (b) {',
  12915. ' return true;',
  12916. ' };',
  12917. ' this.$assign = function (s) {',
  12918. ' return this;',
  12919. ' };',
  12920. ' this.DoIt = function (b) {',
  12921. ' b.DoIt();',
  12922. ' b.$class.Glob();',
  12923. ' this.TBird.Glob();',
  12924. ' };',
  12925. '}, true);',
  12926. '']),
  12927. LinesToStr([ // $mod.$main
  12928. '']));
  12929. end;
  12930. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12931. begin
  12932. StartProgram(false);
  12933. Add([
  12934. '{$modeswitch AdvancedRecords}',
  12935. 'type',
  12936. ' IUnknown = interface end;',
  12937. ' TPoint = record',
  12938. ' type IBird = interface end;',
  12939. ' end;',
  12940. 'begin',
  12941. '']);
  12942. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12943. nNotYetImplemented);
  12944. ParseProgram;
  12945. end;
  12946. procedure TTestModule.TestAdvRecord_Constructor;
  12947. begin
  12948. StartProgram(false);
  12949. Add([
  12950. '{$modeswitch AdvancedRecords}',
  12951. 'type',
  12952. ' TPoint = record',
  12953. ' x,y: longint;',
  12954. ' class procedure Run(w: longint = 13); static;',
  12955. ' constructor Create(ax: longint; ay: longint = -1);',
  12956. ' end;',
  12957. 'class procedure tpoint.run(w: longint);',
  12958. 'begin',
  12959. ' run;',
  12960. ' run();',
  12961. 'end;',
  12962. 'constructor tpoint.create(ax,ay: longint);',
  12963. 'begin',
  12964. ' x:=ax;',
  12965. ' self.y:=ay;',
  12966. ' run;',
  12967. ' run(ax);',
  12968. 'end;',
  12969. 'var r: TPoint;',
  12970. 'begin',
  12971. ' r:=TPoint.Create(1,2);',
  12972. ' with TPoint do r:=Create(1,2);',
  12973. ' r.Create(3);',
  12974. ' r:=r.Create(4);',
  12975. '']);
  12976. ConvertProgram;
  12977. CheckSource('TestAdvRecord_Constructor',
  12978. LinesToStr([ // statements
  12979. 'rtl.recNewT(this, "TPoint", function () {',
  12980. ' this.x = 0;',
  12981. ' this.y = 0;',
  12982. ' this.$eq = function (b) {',
  12983. ' return (this.x === b.x) && (this.y === b.y);',
  12984. ' };',
  12985. ' this.$assign = function (s) {',
  12986. ' this.x = s.x;',
  12987. ' this.y = s.y;',
  12988. ' return this;',
  12989. ' };',
  12990. ' this.Run = function (w) {',
  12991. ' $mod.TPoint.Run(13);',
  12992. ' $mod.TPoint.Run(13);',
  12993. ' };',
  12994. ' this.Create = function (ax, ay) {',
  12995. ' this.x = ax;',
  12996. ' this.y = ay;',
  12997. ' this.Run(13);',
  12998. ' this.Run(ax);',
  12999. ' return this;',
  13000. ' };',
  13001. '});',
  13002. 'this.r = this.TPoint.$new();',
  13003. '']),
  13004. LinesToStr([ // $mod.$main
  13005. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13006. 'var $with = $mod.TPoint;',
  13007. '$mod.r.$assign($with.$new().Create(1, 2));',
  13008. '$mod.r.Create(3, -1);',
  13009. '$mod.r.$assign($mod.r.Create(4, -1));',
  13010. '']));
  13011. end;
  13012. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13013. begin
  13014. StartProgram(false);
  13015. Add([
  13016. '{$modeswitch AdvancedRecords}',
  13017. 'type',
  13018. ' TPoint = record',
  13019. ' class var x: longint;',
  13020. ' class procedure Fly; static;',
  13021. ' class constructor Init;',
  13022. ' end;',
  13023. 'var count: word;',
  13024. 'class procedure Tpoint.Fly;',
  13025. 'begin',
  13026. 'end;',
  13027. 'class constructor tpoint.init;',
  13028. 'begin',
  13029. ' count:=count+1;',
  13030. ' x:=x+3;',
  13031. ' tpoint.x:=tpoint.x+4;',
  13032. ' fly;',
  13033. ' tpoint.fly;',
  13034. 'end;',
  13035. 'var r: TPoint;',
  13036. 'begin',
  13037. ' r.x:=r.x+10;',
  13038. ' r.Fly;',
  13039. ' r.Fly();',
  13040. '']);
  13041. ConvertProgram;
  13042. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13043. LinesToStr([ // statements
  13044. 'rtl.recNewT(this, "TPoint", function () {',
  13045. ' this.x = 0;',
  13046. ' this.$eq = function (b) {',
  13047. ' return true;',
  13048. ' };',
  13049. ' this.$assign = function (s) {',
  13050. ' return this;',
  13051. ' };',
  13052. ' this.Fly = function () {',
  13053. ' };',
  13054. '}, true);',
  13055. 'this.count = 0;',
  13056. 'this.r = this.TPoint.$new();',
  13057. '']),
  13058. LinesToStr([ // $mod.$main
  13059. '(function () {',
  13060. ' $mod.count = $mod.count + 1;',
  13061. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13062. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13063. ' $mod.TPoint.Fly();',
  13064. ' $mod.TPoint.Fly();',
  13065. '})();',
  13066. '$mod.TPoint.x = $mod.r.x + 10;',
  13067. '$mod.TPoint.Fly();',
  13068. '$mod.TPoint.Fly();',
  13069. '']));
  13070. end;
  13071. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13072. begin
  13073. StartUnit(false);
  13074. Add([
  13075. 'interface',
  13076. '{$modeswitch AdvancedRecords}',
  13077. 'type',
  13078. ' TPoint = record',
  13079. ' class var x: longint;',
  13080. ' class procedure Fly; static;',
  13081. ' class constructor Init;',
  13082. ' end;',
  13083. 'implementation',
  13084. 'var count: word;',
  13085. 'class procedure Tpoint.Fly;',
  13086. 'begin',
  13087. 'end;',
  13088. 'class constructor tpoint.init;',
  13089. 'begin',
  13090. ' count:=count+1;',
  13091. ' x:=3;',
  13092. ' tpoint.x:=4;',
  13093. ' fly;',
  13094. ' tpoint.fly;',
  13095. 'end;',
  13096. '']);
  13097. ConvertUnit;
  13098. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13099. LinesToStr([ // statements
  13100. 'var $impl = $mod.$impl;',
  13101. 'rtl.recNewT(this, "TPoint", function () {',
  13102. ' this.x = 0;',
  13103. ' this.$eq = function (b) {',
  13104. ' return true;',
  13105. ' };',
  13106. ' this.$assign = function (s) {',
  13107. ' return this;',
  13108. ' };',
  13109. ' this.Fly = function () {',
  13110. ' };',
  13111. '}, true);',
  13112. '']),
  13113. LinesToStr([ // $mod.$init
  13114. '(function () {',
  13115. ' $impl.count = $impl.count + 1;',
  13116. ' $mod.TPoint.x = 3;',
  13117. ' $mod.TPoint.x = 4;',
  13118. ' $mod.TPoint.Fly();',
  13119. ' $mod.TPoint.Fly();',
  13120. '})();',
  13121. '']),
  13122. LinesToStr([ // $mod.$main
  13123. '$impl.count = 0;',
  13124. '']));
  13125. end;
  13126. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13127. begin
  13128. StartProgram(false);
  13129. Add(['type',
  13130. ' TObject = class',
  13131. ' public',
  13132. ' constructor Create;',
  13133. ' destructor Destroy;',
  13134. ' end;',
  13135. ' TBird = TObject;',
  13136. 'constructor tobject.create;',
  13137. 'begin end;',
  13138. 'destructor tobject.destroy;',
  13139. 'begin end;',
  13140. 'var Obj: tobject;',
  13141. 'begin',
  13142. ' obj:=tobject.create;',
  13143. ' obj:=tobject.create();',
  13144. ' obj:=tbird.create;',
  13145. ' obj:=tbird.create();',
  13146. ' obj:=obj.create();',
  13147. ' obj.destroy;',
  13148. '']);
  13149. ConvertProgram;
  13150. CheckSource('TestClass_TObjectDefaultConstructor',
  13151. LinesToStr([ // statements
  13152. 'rtl.createClass(this,"TObject",null,function(){',
  13153. ' this.$init = function () {',
  13154. ' };',
  13155. ' this.$final = function () {',
  13156. ' };',
  13157. ' this.Create = function(){',
  13158. ' return this;',
  13159. ' };',
  13160. ' this.Destroy = function(){',
  13161. ' };',
  13162. '});',
  13163. 'this.Obj = null;'
  13164. ]),
  13165. LinesToStr([ // $mod.$main
  13166. '$mod.Obj = $mod.TObject.$create("Create");',
  13167. '$mod.Obj = $mod.TObject.$create("Create");',
  13168. '$mod.Obj = $mod.TObject.$create("Create");',
  13169. '$mod.Obj = $mod.TObject.$create("Create");',
  13170. '$mod.Obj = $mod.Obj.Create();',
  13171. '$mod.Obj.$destroy("Destroy");',
  13172. '']));
  13173. end;
  13174. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13175. begin
  13176. StartProgram(false);
  13177. Add('type');
  13178. Add(' TObject = class');
  13179. Add(' public');
  13180. Add(' constructor Create(Par: longint);');
  13181. Add(' end;');
  13182. Add('constructor tobject.create(par: longint);');
  13183. Add('begin end;');
  13184. Add('var Obj: tobject;');
  13185. Add('begin');
  13186. Add(' obj:=tobject.create(3);');
  13187. ConvertProgram;
  13188. CheckSource('TestClass_TObjectConstructorWithParams',
  13189. LinesToStr([ // statements
  13190. 'rtl.createClass(this,"TObject",null,function(){',
  13191. ' this.$init = function () {',
  13192. ' };',
  13193. ' this.$final = function () {',
  13194. ' };',
  13195. ' this.Create = function(Par){',
  13196. ' return this;',
  13197. ' };',
  13198. '});',
  13199. 'this.Obj = null;'
  13200. ]),
  13201. LinesToStr([ // $mod.$main
  13202. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13203. ]));
  13204. end;
  13205. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13206. begin
  13207. StartProgram(false);
  13208. Add('type');
  13209. Add(' TObject = class');
  13210. Add(' public');
  13211. Add(' constructor Create;');
  13212. Add(' end;');
  13213. Add(' TTest = class(TObject)');
  13214. Add(' public');
  13215. Add(' constructor Create(const Par: longint = 1);');
  13216. Add(' end;');
  13217. Add('constructor tobject.create;');
  13218. Add('begin end;');
  13219. Add('constructor ttest.create(const par: longint);');
  13220. Add('begin end;');
  13221. Add('var t: ttest;');
  13222. Add('begin');
  13223. Add(' t:=ttest.create;');
  13224. Add(' t:=ttest.create(2);');
  13225. ConvertProgram;
  13226. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13227. LinesToStr([ // statements
  13228. 'rtl.createClass(this,"TObject",null,function(){',
  13229. ' this.$init = function () {',
  13230. ' };',
  13231. ' this.$final = function () {',
  13232. ' };',
  13233. ' this.Create = function(){',
  13234. ' return this;',
  13235. ' };',
  13236. '});',
  13237. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13238. ' this.Create$1 = function (Par) {',
  13239. ' return this;',
  13240. ' };',
  13241. '});',
  13242. 'this.t = null;'
  13243. ]),
  13244. LinesToStr([ // $mod.$main
  13245. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13246. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13247. ]));
  13248. end;
  13249. procedure TTestModule.TestClass_Var;
  13250. begin
  13251. StartProgram(false);
  13252. Add([
  13253. 'type',
  13254. ' TObject = class',
  13255. ' public',
  13256. ' vI: longint;',
  13257. ' constructor Create(Par: longint);',
  13258. ' end;',
  13259. 'constructor tobject.create(par: longint);',
  13260. 'begin',
  13261. ' vi:=par+3',
  13262. 'end;',
  13263. 'var Obj: tobject;',
  13264. 'begin',
  13265. ' obj:=tobject.create(4);',
  13266. ' obj.vi:=obj.VI+5;']);
  13267. ConvertProgram;
  13268. CheckSource('TestClass_Var',
  13269. LinesToStr([ // statements
  13270. 'rtl.createClass(this,"TObject",null,function(){',
  13271. ' this.$init = function () {',
  13272. ' this.vI = 0;',
  13273. ' };',
  13274. ' this.$final = function () {',
  13275. ' };',
  13276. ' this.Create = function(Par){',
  13277. ' this.vI = Par+3;',
  13278. ' return this;',
  13279. ' };',
  13280. '});',
  13281. 'this.Obj = null;'
  13282. ]),
  13283. LinesToStr([ // $mod.$main
  13284. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13285. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13286. ]));
  13287. end;
  13288. procedure TTestModule.TestClass_Method;
  13289. begin
  13290. StartProgram(false);
  13291. Add('type');
  13292. Add(' TObject = class');
  13293. Add(' public');
  13294. Add(' vI: longint;');
  13295. Add(' Sub: TObject;');
  13296. Add(' constructor Create;');
  13297. Add(' function GetIt(Par: longint): tobject;');
  13298. Add(' end;');
  13299. Add('constructor tobject.create; begin end;');
  13300. Add('function tobject.getit(par: longint): tobject;');
  13301. Add('begin');
  13302. Add(' Self.vi:=par+3;');
  13303. Add(' Result:=self.sub;');
  13304. Add('end;');
  13305. Add('var Obj: tobject;');
  13306. Add('begin');
  13307. Add(' obj:=tobject.create;');
  13308. Add(' obj.getit(4);');
  13309. Add(' obj.sub.sub:=nil;');
  13310. Add(' obj.sub.getit(5);');
  13311. Add(' obj.sub.getit(6).SUB:=nil;');
  13312. Add(' obj.sub.getit(7).GETIT(8);');
  13313. Add(' obj.sub.getit(9).SuB.getit(10);');
  13314. ConvertProgram;
  13315. CheckSource('TestClass_Method',
  13316. LinesToStr([ // statements
  13317. 'rtl.createClass(this,"TObject",null,function(){',
  13318. ' this.$init = function () {',
  13319. ' this.vI = 0;',
  13320. ' this.Sub = null;',
  13321. ' };',
  13322. ' this.$final = function () {',
  13323. ' this.Sub = undefined;',
  13324. ' };',
  13325. ' this.Create = function(){',
  13326. ' return this;',
  13327. ' };',
  13328. ' this.GetIt = function(Par){',
  13329. ' var Result = null;',
  13330. ' this.vI = Par + 3;',
  13331. ' Result = this.Sub;',
  13332. ' return Result;',
  13333. ' };',
  13334. '});',
  13335. 'this.Obj = null;'
  13336. ]),
  13337. LinesToStr([ // $mod.$main
  13338. '$mod.Obj = $mod.TObject.$create("Create");',
  13339. '$mod.Obj.GetIt(4);',
  13340. '$mod.Obj.Sub.Sub=null;',
  13341. '$mod.Obj.Sub.GetIt(5);',
  13342. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13343. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13344. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13345. ]));
  13346. end;
  13347. procedure TTestModule.TestClass_Implementation;
  13348. begin
  13349. StartUnit(false);
  13350. Add([
  13351. 'interface',
  13352. 'type',
  13353. ' TObject = class',
  13354. ' constructor Create;',
  13355. ' end;',
  13356. 'implementation',
  13357. 'type',
  13358. ' TIntClass = class',
  13359. ' constructor Create; reintroduce;',
  13360. ' class procedure DoGlob;',
  13361. ' end;',
  13362. 'constructor tintclass.create;',
  13363. 'begin',
  13364. ' inherited;',
  13365. ' inherited create;',
  13366. ' doglob;',
  13367. 'end;',
  13368. 'class procedure tintclass.doglob;',
  13369. 'begin',
  13370. 'end;',
  13371. 'constructor tobject.create;',
  13372. 'var',
  13373. ' iC: tintclass;',
  13374. 'begin',
  13375. ' ic:=tintclass.create;',
  13376. ' tintclass.doglob;',
  13377. ' ic.doglob;',
  13378. 'end;',
  13379. 'initialization',
  13380. ' tintclass.doglob;',
  13381. '']);
  13382. ConvertUnit;
  13383. CheckSource('TestClass_Implementation',
  13384. LinesToStr([ // statements
  13385. 'var $impl = $mod.$impl;',
  13386. 'rtl.createClass(this, "TObject", null, function () {',
  13387. ' this.$init = function () {',
  13388. ' };',
  13389. ' this.$final = function () {',
  13390. ' };',
  13391. ' this.Create = function () {',
  13392. ' var iC = null;',
  13393. ' iC = $impl.TIntClass.$create("Create$1");',
  13394. ' $impl.TIntClass.DoGlob();',
  13395. ' iC.$class.DoGlob();',
  13396. ' return this;',
  13397. ' };',
  13398. '});',
  13399. '']),
  13400. LinesToStr([ // $mod.$main
  13401. '$impl.TIntClass.DoGlob();',
  13402. '']),
  13403. LinesToStr([
  13404. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13405. ' this.Create$1 = function () {',
  13406. ' $mod.TObject.Create.call(this);',
  13407. ' $mod.TObject.Create.call(this);',
  13408. ' this.$class.DoGlob();',
  13409. ' return this;',
  13410. ' };',
  13411. ' this.DoGlob = function () {',
  13412. ' };',
  13413. '});',
  13414. '']));
  13415. end;
  13416. procedure TTestModule.TestClass_Inheritance;
  13417. begin
  13418. StartProgram(false);
  13419. Add('type');
  13420. Add(' TObject = class');
  13421. Add(' public');
  13422. Add(' constructor Create;');
  13423. Add(' end;');
  13424. Add(' TClassA = class');
  13425. Add(' end;');
  13426. Add(' TClassB = class(TObject)');
  13427. Add(' procedure ProcB;');
  13428. Add(' end;');
  13429. Add('constructor tobject.create; begin end;');
  13430. Add('procedure tclassb.procb; begin end;');
  13431. Add('var');
  13432. Add(' oO: TObject;');
  13433. Add(' oA: TClassA;');
  13434. Add(' oB: TClassB;');
  13435. Add('begin');
  13436. Add(' oO:=tobject.Create;');
  13437. Add(' oA:=tclassa.Create;');
  13438. Add(' ob:=tclassb.Create;');
  13439. Add(' if oo is tclassa then ;');
  13440. Add(' ob:=oo as tclassb;');
  13441. Add(' (oo as tclassb).procb;');
  13442. ConvertProgram;
  13443. CheckSource('TestClass_Inheritance',
  13444. LinesToStr([ // statements
  13445. 'rtl.createClass(this,"TObject",null,function(){',
  13446. ' this.$init = function () {',
  13447. ' };',
  13448. ' this.$final = function () {',
  13449. ' };',
  13450. ' this.Create = function () {',
  13451. ' return this;',
  13452. ' };',
  13453. '});',
  13454. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  13455. '});',
  13456. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  13457. ' this.ProcB = function () {',
  13458. ' };',
  13459. '});',
  13460. 'this.oO = null;',
  13461. 'this.oA = null;',
  13462. 'this.oB = null;'
  13463. ]),
  13464. LinesToStr([ // $mod.$main
  13465. '$mod.oO = $mod.TObject.$create("Create");',
  13466. '$mod.oA = $mod.TClassA.$create("Create");',
  13467. '$mod.oB = $mod.TClassB.$create("Create");',
  13468. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  13469. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  13470. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  13471. ]));
  13472. end;
  13473. procedure TTestModule.TestClass_TypeAlias;
  13474. begin
  13475. StartProgram(false);
  13476. Add([
  13477. '{$interfaces corba}',
  13478. 'type',
  13479. ' IObject = interface',
  13480. ' end;',
  13481. ' IBird = type IObject;',
  13482. ' TObject = class',
  13483. ' end;',
  13484. ' TBird = type TObject;',
  13485. 'var',
  13486. ' oObj: TObject;',
  13487. ' oBird: TBird;',
  13488. ' IntfObj: IObject;',
  13489. ' IntfBird: IBird;',
  13490. 'begin',
  13491. ' oObj:=oBird;',
  13492. '']);
  13493. ConvertProgram;
  13494. CheckSource('TestClass_TypeAlias',
  13495. LinesToStr([ // statements
  13496. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  13497. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  13498. 'rtl.createClass(this, "TObject", null, function () {',
  13499. ' this.$init = function () {',
  13500. ' };',
  13501. ' this.$final = function () {',
  13502. ' };',
  13503. '});',
  13504. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13505. '});',
  13506. 'this.oObj = null;',
  13507. 'this.oBird = null;',
  13508. 'this.IntfObj = null;',
  13509. 'this.IntfBird = null;',
  13510. '']),
  13511. LinesToStr([ // $mod.$main
  13512. '$mod.oObj = $mod.oBird;',
  13513. '']));
  13514. end;
  13515. procedure TTestModule.TestClass_AbstractMethod;
  13516. begin
  13517. StartProgram(false);
  13518. Add('type');
  13519. Add(' TObject = class');
  13520. Add(' public');
  13521. Add(' procedure DoIt; virtual; abstract;');
  13522. Add(' end;');
  13523. Add('begin');
  13524. ConvertProgram;
  13525. CheckSource('TestClass_AbstractMethod',
  13526. LinesToStr([ // statements
  13527. 'rtl.createClass(this,"TObject",null,function(){',
  13528. ' this.$init = function () {',
  13529. ' };',
  13530. ' this.$final = function () {',
  13531. ' };',
  13532. '});'
  13533. ]),
  13534. LinesToStr([ // this.$main
  13535. ''
  13536. ]));
  13537. end;
  13538. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  13539. begin
  13540. StartProgram(false);
  13541. Add([
  13542. 'type',
  13543. ' TObject = class',
  13544. ' procedure DoAbstract; virtual; abstract;',
  13545. ' procedure DoVirtual; virtual;',
  13546. ' procedure DoIt;',
  13547. ' end;',
  13548. ' TA = class',
  13549. ' procedure doabstract; override;',
  13550. ' procedure dovirtual; override;',
  13551. ' procedure DoSome;',
  13552. ' end;',
  13553. 'procedure tobject.dovirtual;',
  13554. 'begin',
  13555. ' inherited; // call non existing ancestor -> ignore silently',
  13556. 'end;',
  13557. 'procedure tobject.doit;',
  13558. 'begin',
  13559. 'end;',
  13560. 'procedure ta.doabstract;',
  13561. 'begin',
  13562. ' inherited dovirtual; // call TObject.DoVirtual',
  13563. 'end;',
  13564. 'procedure ta.dovirtual;',
  13565. 'begin',
  13566. ' inherited; // call TObject.DoVirtual',
  13567. ' inherited dovirtual; // call TObject.DoVirtual',
  13568. ' inherited dovirtual(); // call TObject.DoVirtual',
  13569. ' doit;',
  13570. ' doit();',
  13571. 'end;',
  13572. 'procedure ta.dosome;',
  13573. 'begin',
  13574. ' inherited; // call non existing ancestor method -> silently ignore',
  13575. 'end;',
  13576. 'begin']);
  13577. ConvertProgram;
  13578. CheckSource('TestClass_CallInherited_ProcNoParams',
  13579. LinesToStr([ // statements
  13580. 'rtl.createClass(this,"TObject",null,function(){',
  13581. ' this.$init = function () {',
  13582. ' };',
  13583. ' this.$final = function () {',
  13584. ' };',
  13585. ' this.DoVirtual = function () {',
  13586. ' };',
  13587. ' this.DoIt = function () {',
  13588. ' };',
  13589. '});',
  13590. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13591. ' this.DoAbstract = function () {',
  13592. ' $mod.TObject.DoVirtual.call(this);',
  13593. ' };',
  13594. ' this.DoVirtual = function () {',
  13595. ' $mod.TObject.DoVirtual.call(this);',
  13596. ' $mod.TObject.DoVirtual.call(this);',
  13597. ' $mod.TObject.DoVirtual.call(this);',
  13598. ' this.DoIt();',
  13599. ' this.DoIt();',
  13600. ' };',
  13601. ' this.DoSome = function () {',
  13602. ' };',
  13603. '});'
  13604. ]),
  13605. LinesToStr([ // this.$main
  13606. ''
  13607. ]));
  13608. end;
  13609. procedure TTestModule.TestClass_CallInherited_WithParams;
  13610. begin
  13611. StartProgram(false);
  13612. Add([
  13613. 'type',
  13614. ' TObject = class',
  13615. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  13616. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  13617. ' procedure DoIt(pA: longint; pB: longint = 0);',
  13618. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  13619. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13620. ' end;',
  13621. ' TClassA = class',
  13622. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  13623. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  13624. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13625. ' end;',
  13626. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  13627. 'begin',
  13628. 'end;',
  13629. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  13630. 'begin',
  13631. 'end;',
  13632. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  13633. 'begin',
  13634. 'end;',
  13635. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  13636. 'begin',
  13637. 'end;',
  13638. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  13639. 'begin',
  13640. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13641. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13642. 'end;',
  13643. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  13644. 'begin',
  13645. ' inherited; // call TObject.DoVirtual(pA,pB)',
  13646. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13647. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13648. ' doit(pa,pb);',
  13649. ' doit(pa);',
  13650. ' doit2(pa);',
  13651. ' doit2;',
  13652. 'end;',
  13653. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  13654. 'begin',
  13655. ' pa:=inherited;',
  13656. 'end;',
  13657. 'begin']);
  13658. ConvertProgram;
  13659. CheckSource('TestClass_CallInherited_WithParams',
  13660. LinesToStr([ // statements
  13661. 'rtl.createClass(this,"TObject",null,function(){',
  13662. ' this.$init = function () {',
  13663. ' };',
  13664. ' this.$final = function () {',
  13665. ' };',
  13666. ' this.DoVirtual = function (pA,pB) {',
  13667. ' };',
  13668. ' this.DoIt = function (pA,pB) {',
  13669. ' };',
  13670. ' this.DoIt2 = function (pA,pB) {',
  13671. ' };',
  13672. ' this.GetIt = function (pA, pB) {',
  13673. ' var Result = 0;',
  13674. ' return Result;',
  13675. ' };',
  13676. '});',
  13677. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  13678. ' this.DoAbstract = function (pA,pB) {',
  13679. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13680. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13681. ' };',
  13682. ' this.DoVirtual = function (pA,pB) {',
  13683. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  13684. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13685. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13686. ' this.DoIt(pA,pB);',
  13687. ' this.DoIt(pA,0);',
  13688. ' this.DoIt2(pA,2);',
  13689. ' this.DoIt2(1,2);',
  13690. ' };',
  13691. ' this.GetIt$1 = function (pA, pB) {',
  13692. ' var Result = 0;',
  13693. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  13694. ' return Result;',
  13695. ' };',
  13696. '});'
  13697. ]),
  13698. LinesToStr([ // this.$main
  13699. ''
  13700. ]));
  13701. end;
  13702. procedure TTestModule.TestClasS_CallInheritedConstructor;
  13703. begin
  13704. StartProgram(false);
  13705. Add('type');
  13706. Add(' TObject = class');
  13707. Add(' constructor Create; virtual;');
  13708. Add(' constructor CreateWithB(b: boolean);');
  13709. Add(' end;');
  13710. Add(' TA = class');
  13711. Add(' constructor Create; override;');
  13712. Add(' constructor CreateWithC(c: char);');
  13713. Add(' procedure DoIt;');
  13714. Add(' class function DoSome: TObject;');
  13715. Add(' end;');
  13716. Add('constructor tobject.create;');
  13717. Add('begin');
  13718. Add(' inherited; // call non existing ancestor -> ignore silently');
  13719. Add('end;');
  13720. Add('constructor tobject.createwithb(b: boolean);');
  13721. Add('begin');
  13722. Add(' inherited; // call non existing ancestor -> ignore silently');
  13723. Add(' create; // normal call');
  13724. Add('end;');
  13725. Add('constructor ta.create;');
  13726. Add('begin');
  13727. Add(' inherited; // normal call TObject.Create');
  13728. Add(' inherited create; // normal call TObject.Create');
  13729. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  13730. Add('end;');
  13731. Add('constructor ta.createwithc(c: char);');
  13732. Add('begin');
  13733. Add(' inherited create; // call TObject.Create');
  13734. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  13735. Add(' doit;');
  13736. Add(' doit();');
  13737. Add(' dosome;');
  13738. Add('end;');
  13739. Add('procedure ta.doit;');
  13740. Add('begin');
  13741. Add(' create; // normal call');
  13742. Add(' createwithb(false); // normal call');
  13743. Add(' createwithc(''c''); // normal call');
  13744. Add('end;');
  13745. Add('class function ta.dosome: TObject;');
  13746. Add('begin');
  13747. Add(' Result:=create; // constructor');
  13748. Add(' Result:=createwithb(true); // constructor');
  13749. Add(' Result:=createwithc(''c''); // constructor');
  13750. Add('end;');
  13751. Add('begin');
  13752. ConvertProgram;
  13753. CheckSource('TestClass_CallInheritedConstructor',
  13754. LinesToStr([ // statements
  13755. 'rtl.createClass(this,"TObject",null,function(){',
  13756. ' this.$init = function () {',
  13757. ' };',
  13758. ' this.$final = function () {',
  13759. ' };',
  13760. ' this.Create = function () {',
  13761. ' return this;',
  13762. ' };',
  13763. ' this.CreateWithB = function (b) {',
  13764. ' this.Create();',
  13765. ' return this;',
  13766. ' };',
  13767. '});',
  13768. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13769. ' this.Create = function () {',
  13770. ' $mod.TObject.Create.call(this);',
  13771. ' $mod.TObject.Create.call(this);',
  13772. ' $mod.TObject.CreateWithB.call(this, false);',
  13773. ' return this;',
  13774. ' };',
  13775. ' this.CreateWithC = function (c) {',
  13776. ' $mod.TObject.Create.call(this);',
  13777. ' $mod.TObject.CreateWithB.call(this, true);',
  13778. ' this.DoIt();',
  13779. ' this.DoIt();',
  13780. ' this.$class.DoSome();',
  13781. ' return this;',
  13782. ' };',
  13783. ' this.DoIt = function () {',
  13784. ' this.Create();',
  13785. ' this.CreateWithB(false);',
  13786. ' this.CreateWithC("c");',
  13787. ' };',
  13788. ' this.DoSome = function () {',
  13789. ' var Result = null;',
  13790. ' Result = this.$create("Create");',
  13791. ' Result = this.$create("CreateWithB", [true]);',
  13792. ' Result = this.$create("CreateWithC", ["c"]);',
  13793. ' return Result;',
  13794. ' };',
  13795. '});'
  13796. ]),
  13797. LinesToStr([ // this.$main
  13798. ''
  13799. ]));
  13800. end;
  13801. procedure TTestModule.TestClass_ClassVar_Assign;
  13802. begin
  13803. StartProgram(false);
  13804. Add([
  13805. 'type',
  13806. ' TObject = class',
  13807. ' public',
  13808. ' class var vI: longint;',
  13809. ' class var Sub: TObject;',
  13810. ' constructor Create;',
  13811. ' class function GetIt(var Par: longint): tobject;',
  13812. ' end;',
  13813. 'constructor tobject.create;',
  13814. 'begin',
  13815. ' vi:=vi+1;',
  13816. ' Self.vi:=Self.vi+1;',
  13817. ' inc(vi);',
  13818. 'end;',
  13819. 'class function tobject.getit(var par: longint): tobject;',
  13820. 'begin',
  13821. ' vi:=vi+3;',
  13822. ' Self.vi:=Self.vi+4;',
  13823. ' inc(vi);',
  13824. ' Result:=self.sub;',
  13825. ' GetIt(vi);',
  13826. 'end;',
  13827. 'var Obj: tobject;',
  13828. 'begin',
  13829. ' obj:=tobject.create;',
  13830. ' tobject.vi:=3;',
  13831. ' if tobject.vi=4 then ;',
  13832. ' tobject.sub:=nil;',
  13833. ' obj.sub:=nil;',
  13834. ' obj.sub.sub:=nil;']);
  13835. ConvertProgram;
  13836. CheckSource('TestClass_ClassVar_Assign',
  13837. LinesToStr([ // statements
  13838. 'rtl.createClass(this,"TObject",null,function(){',
  13839. ' this.vI = 0;',
  13840. ' this.Sub = null;',
  13841. ' this.$init = function () {',
  13842. ' };',
  13843. ' this.$final = function () {',
  13844. ' };',
  13845. ' this.Create = function(){',
  13846. ' $mod.TObject.vI = this.vI+1;',
  13847. ' $mod.TObject.vI = this.vI+1;',
  13848. ' $mod.TObject.vI += 1;',
  13849. ' return this;',
  13850. ' };',
  13851. ' this.GetIt = function(Par){',
  13852. ' var Result = null;',
  13853. ' $mod.TObject.vI = this.vI + 3;',
  13854. ' $mod.TObject.vI = this.vI + 4;',
  13855. ' $mod.TObject.vI += 1;',
  13856. ' Result = this.Sub;',
  13857. ' this.GetIt({',
  13858. ' p: $mod.TObject,',
  13859. ' get: function () {',
  13860. ' return this.p.vI;',
  13861. ' },',
  13862. ' set: function (v) {',
  13863. ' this.p.vI = v;',
  13864. ' }',
  13865. ' });',
  13866. ' return Result;',
  13867. ' };',
  13868. '});',
  13869. 'this.Obj = null;'
  13870. ]),
  13871. LinesToStr([ // $mod.$main
  13872. '$mod.Obj = $mod.TObject.$create("Create");',
  13873. '$mod.TObject.vI = 3;',
  13874. 'if ($mod.TObject.vI === 4);',
  13875. '$mod.TObject.Sub=null;',
  13876. '$mod.TObject.Sub=null;',
  13877. '$mod.TObject.Sub=null;',
  13878. '']));
  13879. end;
  13880. procedure TTestModule.TestClass_CallClassMethod;
  13881. begin
  13882. StartProgram(false);
  13883. Add('type');
  13884. Add(' TObject = class');
  13885. Add(' public');
  13886. Add(' class var vI: longint;');
  13887. Add(' class var Sub: TObject;');
  13888. Add(' constructor Create;');
  13889. Add(' function GetMore(Par: longint): longint;');
  13890. Add(' class function GetIt(Par: longint): tobject;');
  13891. Add(' end;');
  13892. Add('constructor tobject.create;');
  13893. Add('begin');
  13894. Add(' sub:=getit(3);');
  13895. Add(' vi:=getmore(4);');
  13896. Add(' sub:=Self.getit(5);');
  13897. Add(' vi:=Self.getmore(6);');
  13898. Add('end;');
  13899. Add('function tobject.getmore(par: longint): longint;');
  13900. Add('begin');
  13901. Add(' sub:=getit(11);');
  13902. Add(' vi:=getmore(12);');
  13903. Add(' sub:=self.getit(13);');
  13904. Add(' vi:=self.getmore(14);');
  13905. Add('end;');
  13906. Add('class function tobject.getit(par: longint): tobject;');
  13907. Add('begin');
  13908. Add(' sub:=getit(21);');
  13909. Add(' vi:=sub.getmore(22);');
  13910. Add(' sub:=self.getit(23);');
  13911. Add(' vi:=self.sub.getmore(24);');
  13912. Add('end;');
  13913. Add('var Obj: tobject;');
  13914. Add('begin');
  13915. Add(' obj:=tobject.create;');
  13916. Add(' tobject.getit(5);');
  13917. Add(' obj.getit(6);');
  13918. Add(' obj.sub.getit(7);');
  13919. Add(' obj.sub.getit(8).SUB:=nil;');
  13920. Add(' obj.sub.getit(9).GETIT(10);');
  13921. Add(' obj.sub.getit(11).SuB.getit(12);');
  13922. ConvertProgram;
  13923. CheckSource('TestClass_CallClassMethod',
  13924. LinesToStr([ // statements
  13925. 'rtl.createClass(this,"TObject",null,function(){',
  13926. ' this.vI = 0;',
  13927. ' this.Sub = null;',
  13928. ' this.$init = function () {',
  13929. ' };',
  13930. ' this.$final = function () {',
  13931. ' };',
  13932. ' this.Create = function(){',
  13933. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13934. ' $mod.TObject.vI = this.GetMore(4);',
  13935. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13936. ' $mod.TObject.vI = this.GetMore(6);',
  13937. ' return this;',
  13938. ' };',
  13939. ' this.GetMore = function(Par){',
  13940. ' var Result = 0;',
  13941. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13942. ' $mod.TObject.vI = this.GetMore(12);',
  13943. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13944. ' $mod.TObject.vI = this.GetMore(14);',
  13945. ' return Result;',
  13946. ' };',
  13947. ' this.GetIt = function(Par){',
  13948. ' var Result = null;',
  13949. ' $mod.TObject.Sub = this.GetIt(21);',
  13950. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13951. ' $mod.TObject.Sub = this.GetIt(23);',
  13952. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13953. ' return Result;',
  13954. ' };',
  13955. '});',
  13956. 'this.Obj = null;'
  13957. ]),
  13958. LinesToStr([ // $mod.$main
  13959. '$mod.Obj = $mod.TObject.$create("Create");',
  13960. '$mod.TObject.GetIt(5);',
  13961. '$mod.Obj.$class.GetIt(6);',
  13962. '$mod.Obj.Sub.$class.GetIt(7);',
  13963. '$mod.TObject.Sub=null;',
  13964. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13965. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13966. '']));
  13967. end;
  13968. procedure TTestModule.TestClass_CallClassMethodStatic;
  13969. begin
  13970. StartProgram(false);
  13971. Add([
  13972. 'type',
  13973. ' TObject = class',
  13974. ' public',
  13975. ' class function Fly: tobject; static;',
  13976. ' end;',
  13977. 'class function tobject.Fly: tobject;',
  13978. 'begin',
  13979. ' Result.Fly;',
  13980. ' Result.Fly();',
  13981. ' Fly;',
  13982. ' Fly();',
  13983. ' Fly.Fly;',
  13984. ' Fly.Fly();',
  13985. 'end;',
  13986. 'var Obj: tobject;',
  13987. 'begin',
  13988. ' obj.Fly;',
  13989. ' obj.Fly();',
  13990. ' with obj do begin',
  13991. ' Fly;',
  13992. ' Fly();',
  13993. ' end;',
  13994. '']);
  13995. ConvertProgram;
  13996. CheckSource('TestClass_CallClassMethodStatic',
  13997. LinesToStr([ // statements
  13998. 'rtl.createClass(this, "TObject", null, function () {',
  13999. ' this.$init = function () {',
  14000. ' };',
  14001. ' this.$final = function () {',
  14002. ' };',
  14003. ' this.Fly = function () {',
  14004. ' var Result = null;',
  14005. ' $mod.TObject.Fly();',
  14006. ' $mod.TObject.Fly();',
  14007. ' $mod.TObject.Fly();',
  14008. ' $mod.TObject.Fly();',
  14009. ' $mod.TObject.Fly();',
  14010. ' $mod.TObject.Fly();',
  14011. ' return Result;',
  14012. ' };',
  14013. '});',
  14014. 'this.Obj = null;'
  14015. ]),
  14016. LinesToStr([ // $mod.$main
  14017. '$mod.TObject.Fly();',
  14018. '$mod.TObject.Fly();',
  14019. 'var $with = $mod.Obj;',
  14020. '$with.Fly();',
  14021. '$with.Fly();',
  14022. '']));
  14023. end;
  14024. procedure TTestModule.TestClass_Property;
  14025. begin
  14026. StartProgram(false);
  14027. Add('type');
  14028. Add(' TObject = class');
  14029. Add(' Fx: longint;');
  14030. Add(' Fy: longint;');
  14031. Add(' function GetInt: longint;');
  14032. Add(' procedure SetInt(Value: longint);');
  14033. Add(' procedure DoIt;');
  14034. Add(' property IntA: longint read Fx write Fy;');
  14035. Add(' property IntB: longint read GetInt write SetInt;');
  14036. Add(' end;');
  14037. Add('function tobject.getint: longint;');
  14038. Add('begin');
  14039. Add(' result:=fx;');
  14040. Add('end;');
  14041. Add('procedure tobject.setint(value: longint);');
  14042. Add('begin');
  14043. Add(' if value=fy then exit;');
  14044. Add(' fy:=value;');
  14045. Add('end;');
  14046. Add('procedure tobject.doit;');
  14047. Add('begin');
  14048. Add(' IntA:=IntA+1;');
  14049. Add(' Self.IntA:=Self.IntA+1;');
  14050. Add(' IntB:=IntB+1;');
  14051. Add(' Self.IntB:=Self.IntB+1;');
  14052. Add('end;');
  14053. Add('var Obj: tobject;');
  14054. Add('begin');
  14055. Add(' obj.inta:=obj.inta+1;');
  14056. Add(' if obj.intb=2 then;');
  14057. Add(' obj.intb:=obj.intb+2;');
  14058. Add(' obj.setint(obj.inta);');
  14059. ConvertProgram;
  14060. CheckSource('TestClass_Property',
  14061. LinesToStr([ // statements
  14062. 'rtl.createClass(this, "TObject", null, function () {',
  14063. ' this.$init = function () {',
  14064. ' this.Fx = 0;',
  14065. ' this.Fy = 0;',
  14066. ' };',
  14067. ' this.$final = function () {',
  14068. ' };',
  14069. ' this.GetInt = function () {',
  14070. ' var Result = 0;',
  14071. ' Result = this.Fx;',
  14072. ' return Result;',
  14073. ' };',
  14074. ' this.SetInt = function (Value) {',
  14075. ' if (Value === this.Fy) return;',
  14076. ' this.Fy = Value;',
  14077. ' };',
  14078. ' this.DoIt = function () {',
  14079. ' this.Fy = this.Fx + 1;',
  14080. ' this.Fy = this.Fx + 1;',
  14081. ' this.SetInt(this.GetInt() + 1);',
  14082. ' this.SetInt(this.GetInt() + 1);',
  14083. ' };',
  14084. '});',
  14085. 'this.Obj = null;'
  14086. ]),
  14087. LinesToStr([ // $mod.$main
  14088. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14089. 'if ($mod.Obj.GetInt() === 2);',
  14090. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14091. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14092. ]));
  14093. end;
  14094. procedure TTestModule.TestClass_Property_ClassMethod;
  14095. begin
  14096. StartProgram(false);
  14097. Add([
  14098. 'type',
  14099. ' TObject = class',
  14100. ' class var Fx: longint;',
  14101. ' class var Fy: longint;',
  14102. ' class function GetInt: longint;',
  14103. ' class procedure SetInt(Value: longint);',
  14104. ' end;',
  14105. ' TBird = class',
  14106. ' class procedure DoIt;',
  14107. ' class property IntA: longint read Fx write Fy;',
  14108. ' class property IntB: longint read GetInt write SetInt;',
  14109. ' end;',
  14110. 'class function tobject.getint: longint;',
  14111. 'begin',
  14112. ' result:=fx;',
  14113. 'end;',
  14114. 'class procedure tobject.setint(value: longint);',
  14115. 'begin',
  14116. 'end;',
  14117. 'class procedure tbird.doit;',
  14118. 'begin',
  14119. ' FX:=3;',
  14120. ' IntA:=IntA+1;',
  14121. ' Self.IntA:=Self.IntA+1;',
  14122. ' IntB:=IntB+1;',
  14123. ' Self.IntB:=Self.IntB+1;',
  14124. ' with Self do begin',
  14125. ' FX:=11;',
  14126. ' IntA:=IntA+12;',
  14127. ' IntB:=IntB+13;',
  14128. ' end;',
  14129. 'end;',
  14130. 'var Obj: tbird;',
  14131. 'begin',
  14132. ' tbird.fx:=tbird.fx+1;',
  14133. ' tbird.inta:=tbird.inta+1;',
  14134. ' if tbird.intb=2 then;',
  14135. ' tbird.intb:=tbird.intb+2;',
  14136. ' tbird.setint(tbird.inta);',
  14137. ' obj.inta:=obj.inta+1;',
  14138. ' if obj.intb=2 then;',
  14139. ' obj.intb:=obj.intb+2;',
  14140. ' obj.setint(obj.inta);',
  14141. ' with Tbird do begin',
  14142. ' FX:=FY+1;',
  14143. ' inta:=inta+2;',
  14144. ' intb:=intb+3;',
  14145. ' end;',
  14146. ' with Obj do begin',
  14147. ' FX:=FY+1;',
  14148. ' inta:=inta+2;',
  14149. ' intb:=intb+3;',
  14150. ' end;',
  14151. '']);
  14152. ConvertProgram;
  14153. CheckSource('TestClass_Property_ClassMethod',
  14154. LinesToStr([ // statements
  14155. 'rtl.createClass(this, "TObject", null, function () {',
  14156. ' this.Fx = 0;',
  14157. ' this.Fy = 0;',
  14158. ' this.$init = function () {',
  14159. ' };',
  14160. ' this.$final = function () {',
  14161. ' };',
  14162. ' this.GetInt = function () {',
  14163. ' var Result = 0;',
  14164. ' Result = this.Fx;',
  14165. ' return Result;',
  14166. ' };',
  14167. ' this.SetInt = function (Value) {',
  14168. ' };',
  14169. '});',
  14170. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14171. ' this.DoIt = function () {',
  14172. ' $mod.TObject.Fx = 3;',
  14173. ' $mod.TObject.Fy = this.Fx + 1;',
  14174. ' $mod.TObject.Fy = this.Fx + 1;',
  14175. ' this.SetInt(this.GetInt() + 1);',
  14176. ' this.SetInt(this.GetInt() + 1);',
  14177. ' $mod.TObject.Fx = 11;',
  14178. ' $mod.TObject.Fy = this.Fx + 12;',
  14179. ' this.SetInt(this.GetInt() + 13);',
  14180. ' };',
  14181. '});',
  14182. 'this.Obj = null;'
  14183. ]),
  14184. LinesToStr([ // $mod.$main
  14185. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14186. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14187. 'if ($mod.TBird.GetInt() === 2);',
  14188. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14189. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14190. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14191. 'if ($mod.Obj.$class.GetInt() === 2);',
  14192. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14193. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14194. 'var $with = $mod.TBird;',
  14195. '$mod.TObject.Fx = $with.Fy + 1;',
  14196. '$mod.TObject.Fy = $with.Fx + 2;',
  14197. '$with.SetInt($with.GetInt() + 3);',
  14198. 'var $with1 = $mod.Obj;',
  14199. '$mod.TObject.Fx = $with1.Fy + 1;',
  14200. '$mod.TObject.Fy = $with1.Fx + 2;',
  14201. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14202. '']));
  14203. end;
  14204. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14205. begin
  14206. StartProgram(false);
  14207. Add([
  14208. 'type',
  14209. ' TObject = class',
  14210. ' class function GetInt: longint; static;',
  14211. ' class procedure SetInt(Value: longint); static;',
  14212. ' class function GetItems(Index: word): longint; static;',
  14213. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14214. ' end;',
  14215. ' TBird = class',
  14216. ' class procedure Fly;',
  14217. ' class property IntA: longint read GetInt write SetInt;',
  14218. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14219. ' end;',
  14220. 'class function tobject.getint: longint;',
  14221. 'begin',
  14222. 'end;',
  14223. 'class procedure tobject.setint(value: longint);',
  14224. 'begin',
  14225. 'end;',
  14226. 'class function tobject.GetItems(Index: word): longint;',
  14227. 'begin',
  14228. 'end;',
  14229. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14230. 'begin',
  14231. 'end;',
  14232. 'class procedure tbird.fly;',
  14233. 'var w: longint;',
  14234. 'begin',
  14235. ' inta:=inta+51;',
  14236. ' w:=items[52];',
  14237. ' items[53]:=54;',
  14238. 'end;',
  14239. 'var Obj: tbird;',
  14240. ' i: longint;',
  14241. 'begin',
  14242. ' tbird.inta:=tbird.inta+1;',
  14243. ' i:=tbird.items[2];',
  14244. ' tbird.items[3]:=4;',
  14245. ' obj.inta:=obj.inta+11;',
  14246. ' i:=obj.items[12];',
  14247. ' obj.items[13]:=14;',
  14248. ' with Tbird do begin',
  14249. ' inta:=inta+21;',
  14250. ' i:=items[22];',
  14251. ' items[23]:=24;',
  14252. ' end;',
  14253. ' with Obj do begin',
  14254. ' inta:=inta+31;',
  14255. ' i:=items[32];',
  14256. ' items[33]:=34;',
  14257. ' end;',
  14258. '']);
  14259. ConvertProgram;
  14260. CheckSource('TestClass_Property_ClassMethod',
  14261. LinesToStr([ // statements
  14262. 'rtl.createClass(this, "TObject", null, function () {',
  14263. ' this.$init = function () {',
  14264. ' };',
  14265. ' this.$final = function () {',
  14266. ' };',
  14267. ' this.GetInt = function () {',
  14268. ' var Result = 0;',
  14269. ' return Result;',
  14270. ' };',
  14271. ' this.SetInt = function (Value) {',
  14272. ' };',
  14273. ' this.GetItems = function (Index) {',
  14274. ' var Result = 0;',
  14275. ' return Result;',
  14276. ' };',
  14277. ' this.SetItems = function (Index, Value) {',
  14278. ' };',
  14279. '});',
  14280. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14281. ' this.Fly = function () {',
  14282. ' var w = 0;',
  14283. ' this.SetInt(this.GetInt() + 51);',
  14284. ' w = this.GetItems(52);',
  14285. ' this.SetItems(53, 54);',
  14286. ' };',
  14287. '});',
  14288. 'this.Obj = null;',
  14289. 'this.i = 0;',
  14290. '']),
  14291. LinesToStr([ // $mod.$main
  14292. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14293. '$mod.i = $mod.TObject.GetItems(2);',
  14294. '$mod.TObject.SetItems(3, 4);',
  14295. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14296. '$mod.i = $mod.TObject.GetItems(12);',
  14297. '$mod.TObject.SetItems(13, 14);',
  14298. 'var $with = $mod.TBird;',
  14299. '$with.SetInt($with.GetInt() + 21);',
  14300. '$mod.i = $with.GetItems(22);',
  14301. '$with.SetItems(23, 24);',
  14302. 'var $with1 = $mod.Obj;',
  14303. '$with1.SetInt($with1.GetInt() + 31);',
  14304. '$mod.i = $with1.GetItems(32);',
  14305. '$with1.SetItems(33, 34);',
  14306. '']));
  14307. end;
  14308. procedure TTestModule.TestClass_Property_Indexed;
  14309. begin
  14310. StartProgram(false);
  14311. Add([
  14312. 'type',
  14313. ' TObject = class',
  14314. ' FItems: array of longint;',
  14315. ' function GetItems(Index: longint): longint;',
  14316. ' procedure SetItems(Index: longint; Value: longint);',
  14317. ' procedure DoIt;',
  14318. ' property Items[Index: longint]: longint read getitems write setitems;',
  14319. ' end;',
  14320. 'function tobject.getitems(index: longint): longint;',
  14321. 'begin',
  14322. ' Result:=fitems[index];',
  14323. 'end;',
  14324. 'procedure tobject.setitems(index: longint; value: longint);',
  14325. 'begin',
  14326. ' fitems[index]:=value;',
  14327. 'end;',
  14328. 'procedure tobject.doit;',
  14329. 'begin',
  14330. ' items[1]:=2;',
  14331. ' items[3]:=items[4];',
  14332. ' self.items[5]:=self.items[6];',
  14333. ' items[items[7]]:=items[items[8]];',
  14334. 'end;',
  14335. 'var Obj: tobject;',
  14336. 'begin',
  14337. ' obj.Items[11]:=obj.Items[12];',
  14338. '']);
  14339. ConvertProgram;
  14340. CheckSource('TestClass_Property_Indexed',
  14341. LinesToStr([ // statements
  14342. 'rtl.createClass(this, "TObject", null, function () {',
  14343. ' this.$init = function () {',
  14344. ' this.FItems = [];',
  14345. ' };',
  14346. ' this.$final = function () {',
  14347. ' this.FItems = undefined;',
  14348. ' };',
  14349. ' this.GetItems = function (Index) {',
  14350. ' var Result = 0;',
  14351. ' Result = this.FItems[Index];',
  14352. ' return Result;',
  14353. ' };',
  14354. ' this.SetItems = function (Index, Value) {',
  14355. ' this.FItems[Index] = Value;',
  14356. ' };',
  14357. ' this.DoIt = function () {',
  14358. ' this.SetItems(1, 2);',
  14359. ' this.SetItems(3,this.GetItems(4));',
  14360. ' this.SetItems(5,this.GetItems(6));',
  14361. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14362. ' };',
  14363. '});',
  14364. 'this.Obj = null;'
  14365. ]),
  14366. LinesToStr([ // $mod.$main
  14367. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14368. ]));
  14369. end;
  14370. procedure TTestModule.TestClass_Property_IndexSpec;
  14371. begin
  14372. StartProgram(false);
  14373. Add([
  14374. 'type',
  14375. ' TEnum = (red, blue);',
  14376. ' TObject = class',
  14377. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14378. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14379. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14380. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14381. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14382. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14383. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14384. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14385. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14386. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14387. ' end;',
  14388. 'procedure DoIt(b: boolean); begin end;',
  14389. 'var',
  14390. ' o: TObject;',
  14391. 'begin',
  14392. ' o.B1:=o.B1;',
  14393. ' o.B2:=o.B2;',
  14394. ' o.B3:=o.B3;',
  14395. ' o.I1[''a'']:=o.I1[''b''];',
  14396. ' doit(o.b1);',
  14397. ' doit(o.b2);',
  14398. ' doit(o.i1[''c'']);',
  14399. '']);
  14400. ConvertProgram;
  14401. CheckSource('TestClass_Property_IndexSpec',
  14402. LinesToStr([ // statements
  14403. 'this.TEnum = {',
  14404. ' "0": "red",',
  14405. ' red: 0,',
  14406. ' "1": "blue",',
  14407. ' blue: 1',
  14408. '};',
  14409. 'rtl.createClass(this, "TObject", null, function () {',
  14410. ' this.$init = function () {',
  14411. ' };',
  14412. ' this.$final = function () {',
  14413. ' };',
  14414. '});',
  14415. 'this.DoIt = function (b) {',
  14416. '};',
  14417. 'this.o = null;',
  14418. '']),
  14419. LinesToStr([ // $mod.$main
  14420. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  14421. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  14422. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  14423. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  14424. '$mod.DoIt($mod.o.GetIntBool(1));',
  14425. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  14426. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  14427. '']));
  14428. end;
  14429. procedure TTestModule.TestClass_PropertyOfTypeArray;
  14430. begin
  14431. StartProgram(false);
  14432. Add('type');
  14433. Add(' TArray = array of longint;');
  14434. Add(' TObject = class');
  14435. Add(' FItems: TArray;');
  14436. Add(' function GetItems: tarray;');
  14437. Add(' procedure SetItems(Value: tarray);');
  14438. Add(' property Items: tarray read getitems write setitems;');
  14439. Add(' procedure SetNumbers(const Value: tarray);');
  14440. Add(' property Numbers: tarray write setnumbers;');
  14441. Add(' end;');
  14442. Add('function tobject.getitems: tarray;');
  14443. Add('begin');
  14444. Add(' Result:=fitems;');
  14445. Add('end;');
  14446. Add('procedure tobject.setitems(value: tarray);');
  14447. Add('begin');
  14448. Add(' fitems:=value;');
  14449. Add(' fitems:=nil;');
  14450. Add(' Items:=nil;');
  14451. Add(' Items:=Items;');
  14452. Add(' Items[1]:=2;');
  14453. Add(' fitems[3]:=Items[4];');
  14454. Add(' Items[5]:=Items[6];');
  14455. Add(' Self.Items[7]:=8;');
  14456. Add(' Self.Items[9]:=Self.Items[10];');
  14457. Add(' Items[Items[11]]:=Items[Items[12]];');
  14458. Add('end;');
  14459. Add('procedure tobject.SetNumbers(const Value: tarray);');
  14460. Add('begin;');
  14461. Add(' Numbers:=nil;');
  14462. Add(' Numbers:=Value;');
  14463. Add(' Self.Numbers:=Value;');
  14464. Add('end;');
  14465. Add('var Obj: tobject;');
  14466. Add('begin');
  14467. Add(' obj.items:=nil;');
  14468. Add(' obj.items:=obj.items;');
  14469. Add(' obj.items[11]:=obj.items[12];');
  14470. ConvertProgram;
  14471. CheckSource('TestClass_PropertyOfTypeArray',
  14472. LinesToStr([ // statements
  14473. 'rtl.createClass(this, "TObject", null, function () {',
  14474. ' this.$init = function () {',
  14475. ' this.FItems = [];',
  14476. ' };',
  14477. ' this.$final = function () {',
  14478. ' this.FItems = undefined;',
  14479. ' };',
  14480. ' this.GetItems = function () {',
  14481. ' var Result = [];',
  14482. ' Result = rtl.arrayRef(this.FItems);',
  14483. ' return Result;',
  14484. ' };',
  14485. ' this.SetItems = function (Value) {',
  14486. ' this.FItems = rtl.arrayRef(Value);',
  14487. ' this.FItems = [];',
  14488. ' this.SetItems([]);',
  14489. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  14490. ' this.GetItems()[1] = 2;',
  14491. ' this.FItems[3] = this.GetItems()[4];',
  14492. ' this.GetItems()[5] = this.GetItems()[6];',
  14493. ' this.GetItems()[7] = 8;',
  14494. ' this.GetItems()[9] = this.GetItems()[10];',
  14495. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  14496. ' };',
  14497. ' this.SetNumbers = function (Value) {',
  14498. ' this.SetNumbers([]);',
  14499. ' this.SetNumbers(Value);',
  14500. ' this.SetNumbers(Value);',
  14501. ' };',
  14502. '});',
  14503. 'this.Obj = null;'
  14504. ]),
  14505. LinesToStr([ // $mod.$main
  14506. '$mod.Obj.SetItems([]);',
  14507. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  14508. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  14509. ]));
  14510. end;
  14511. procedure TTestModule.TestClass_PropertyDefault;
  14512. begin
  14513. StartProgram(false);
  14514. Add([
  14515. 'type',
  14516. ' TArray = array of longint;',
  14517. ' TObject = class',
  14518. ' end;',
  14519. ' TBird = class',
  14520. ' FItems: TArray;',
  14521. ' function GetItems(Index: longint): longint;',
  14522. ' procedure SetItems(Index, Value: longint);',
  14523. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  14524. ' end;',
  14525. 'function TBird.getitems(index: longint): longint;',
  14526. 'begin',
  14527. 'end;',
  14528. 'procedure TBird.setitems(index, value: longint);',
  14529. 'begin',
  14530. ' Self[1]:=2;',
  14531. ' Self[3]:=Self[index];',
  14532. ' Self[index]:=Self[Self[value]];',
  14533. ' Self[Self[4]]:=value;',
  14534. 'end;',
  14535. 'var',
  14536. ' Bird: TBird;',
  14537. ' Obj: TObject;',
  14538. 'begin',
  14539. ' bird[11]:=12;',
  14540. ' bird[13]:=bird[14];',
  14541. ' bird[Bird[15]]:=bird[Bird[15]];',
  14542. ' TBird(obj)[16]:=TBird(obj)[17];',
  14543. ' (obj as tbird)[18]:=19;',
  14544. '']);
  14545. ConvertProgram;
  14546. CheckSource('TestClass_PropertyDefault',
  14547. LinesToStr([ // statements
  14548. 'rtl.createClass(this, "TObject", null, function () {',
  14549. ' this.$init = function () {',
  14550. ' };',
  14551. ' this.$final = function () {',
  14552. ' };',
  14553. '});',
  14554. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14555. ' this.$init = function () {',
  14556. ' $mod.TObject.$init.call(this);',
  14557. ' this.FItems = [];',
  14558. ' };',
  14559. ' this.$final = function () {',
  14560. ' this.FItems = undefined;',
  14561. ' $mod.TObject.$final.call(this);',
  14562. ' };',
  14563. ' this.GetItems = function (Index) {',
  14564. ' var Result = 0;',
  14565. ' return Result;',
  14566. ' };',
  14567. ' this.SetItems = function (Index, Value) {',
  14568. ' this.SetItems(1, 2);',
  14569. ' this.SetItems(3, this.GetItems(Index));',
  14570. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  14571. ' this.SetItems(this.GetItems(4), Value);',
  14572. ' };',
  14573. '});',
  14574. 'this.Bird = null;',
  14575. 'this.Obj = null;',
  14576. '']),
  14577. LinesToStr([ // $mod.$main
  14578. '$mod.Bird.SetItems(11, 12);',
  14579. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  14580. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  14581. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  14582. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  14583. '']));
  14584. end;
  14585. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  14586. begin
  14587. StartProgram(false);
  14588. Add([
  14589. 'type',
  14590. ' TObject = class end;',
  14591. ' TAlphaList = class',
  14592. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  14593. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  14594. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  14595. ' end;',
  14596. ' TBetaList = class',
  14597. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  14598. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  14599. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  14600. ' end;',
  14601. ' TBird = class',
  14602. ' procedure DoIt;',
  14603. ' end;',
  14604. 'procedure TBird.DoIt;',
  14605. 'var',
  14606. ' List: TAlphaList;',
  14607. 'begin',
  14608. ' if TBetaList(List[true])[3]=nil then ;',
  14609. ' TBetaList(List[false])[5]:=nil;',
  14610. 'end;',
  14611. 'var',
  14612. ' List: TAlphaList;',
  14613. 'begin',
  14614. ' if TBetaList(List[true])[3]=nil then ;',
  14615. ' TBetaList(List[false])[5]:=nil;',
  14616. '']);
  14617. ConvertProgram;
  14618. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  14619. LinesToStr([ // statements
  14620. 'rtl.createClass(this, "TObject", null, function () {',
  14621. ' this.$init = function () {',
  14622. ' };',
  14623. ' this.$final = function () {',
  14624. ' };',
  14625. '});',
  14626. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  14627. '});',
  14628. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  14629. '});',
  14630. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14631. ' this.DoIt = function () {',
  14632. ' var List = null;',
  14633. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  14634. ' List.GetAlphas(false).SetBetas(5, null);',
  14635. ' };',
  14636. '});',
  14637. 'this.List = null;',
  14638. '']),
  14639. LinesToStr([ // $mod.$main
  14640. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  14641. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  14642. '']));
  14643. end;
  14644. procedure TTestModule.TestClass_PropertyOverride;
  14645. begin
  14646. StartProgram(false);
  14647. Add('type');
  14648. Add(' integer = longint;');
  14649. Add(' TObject = class');
  14650. Add(' FItem: integer;');
  14651. Add(' function GetItem: integer; external name ''GetItem'';');
  14652. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  14653. Add(' property Item: integer read getitem write setitem;');
  14654. Add(' end;');
  14655. Add(' TCar = class');
  14656. Add(' FBag: integer;');
  14657. Add(' function GetBag: integer; external name ''GetBag'';');
  14658. Add(' property Item read getbag;');
  14659. Add(' end;');
  14660. Add('var');
  14661. Add(' Obj: tobject;');
  14662. Add(' Car: tcar;');
  14663. Add('begin');
  14664. Add(' Obj.Item:=Obj.Item;');
  14665. Add(' Car.Item:=Car.Item;');
  14666. ConvertProgram;
  14667. CheckSource('TestClass_PropertyOverride',
  14668. LinesToStr([ // statements
  14669. 'rtl.createClass(this, "TObject", null, function () {',
  14670. ' this.$init = function () {',
  14671. ' this.FItem = 0;',
  14672. ' };',
  14673. ' this.$final = function () {',
  14674. ' };',
  14675. '});',
  14676. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14677. ' this.$init = function () {',
  14678. ' $mod.TObject.$init.call(this);',
  14679. ' this.FBag = 0;',
  14680. ' };',
  14681. '});',
  14682. 'this.Obj = null;',
  14683. 'this.Car = null;',
  14684. '']),
  14685. LinesToStr([ // $mod.$main
  14686. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  14687. '$mod.Car.SetItem($mod.Car.GetBag());',
  14688. '']));
  14689. end;
  14690. procedure TTestModule.TestClass_PropertyIncVisibility;
  14691. begin
  14692. AddModuleWithIntfImplSrc('unit1.pp',
  14693. LinesToStr([
  14694. 'type',
  14695. ' TNumber = longint;',
  14696. ' TInteger = longint;',
  14697. ' TObject = class',
  14698. ' private',
  14699. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  14700. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  14701. ' protected',
  14702. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  14703. ' end;']),
  14704. LinesToStr([
  14705. '']));
  14706. StartProgram(true);
  14707. Add([
  14708. 'uses unit1;',
  14709. 'type',
  14710. ' TBird = class',
  14711. ' public',
  14712. ' property Items;',
  14713. ' end;',
  14714. 'procedure DoIt(i: TInteger);',
  14715. 'begin',
  14716. 'end;',
  14717. 'var b: TBird;',
  14718. 'begin',
  14719. ' b.Items[1]:=2;',
  14720. ' b.Items[3]:=b.Items[4];',
  14721. ' DoIt(b.Items[5]);',
  14722. '']);
  14723. ConvertProgram;
  14724. CheckSource('TestClass_PropertyIncVisibility',
  14725. LinesToStr([ // statements
  14726. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  14727. '});',
  14728. 'this.DoIt = function (i) {',
  14729. '};',
  14730. 'this.b = null;'
  14731. ]),
  14732. LinesToStr([ // $mod.$main
  14733. '$mod.b.SetItems(1, 2);',
  14734. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  14735. '$mod.DoIt($mod.b.GetItems(5));'
  14736. ]));
  14737. end;
  14738. procedure TTestModule.TestClass_Assigned;
  14739. begin
  14740. StartProgram(false);
  14741. Add('type');
  14742. Add(' TObject = class');
  14743. Add(' end;');
  14744. Add('var');
  14745. Add(' Obj: tobject;');
  14746. Add(' b: boolean;');
  14747. Add('begin');
  14748. Add(' if Assigned(obj) then ;');
  14749. Add(' b:=Assigned(obj) or false;');
  14750. ConvertProgram;
  14751. CheckSource('TestClass_Assigned',
  14752. LinesToStr([ // statements
  14753. 'rtl.createClass(this, "TObject", null, function () {',
  14754. ' this.$init = function () {',
  14755. ' };',
  14756. ' this.$final = function () {',
  14757. ' };',
  14758. '});',
  14759. 'this.Obj = null;',
  14760. 'this.b = false;'
  14761. ]),
  14762. LinesToStr([ // $mod.$main
  14763. 'if ($mod.Obj != null);',
  14764. '$mod.b = ($mod.Obj != null) || false;'
  14765. ]));
  14766. end;
  14767. procedure TTestModule.TestClass_WithClassDoCreate;
  14768. begin
  14769. StartProgram(false);
  14770. Add('type');
  14771. Add(' TObject = class');
  14772. Add(' aBool: boolean;');
  14773. Add(' Arr: array of boolean;');
  14774. Add(' constructor Create;');
  14775. Add(' end;');
  14776. Add('constructor TObject.Create; begin end;');
  14777. Add('var');
  14778. Add(' Obj: tobject;');
  14779. Add(' b: boolean;');
  14780. Add('begin');
  14781. Add(' with tobject.create do begin');
  14782. Add(' b:=abool;');
  14783. Add(' abool:=b;');
  14784. Add(' b:=arr[1];');
  14785. Add(' arr[2]:=b;');
  14786. Add(' end;');
  14787. Add(' with tobject do');
  14788. Add(' obj:=create;');
  14789. Add(' with obj do begin');
  14790. Add(' create;');
  14791. Add(' b:=abool;');
  14792. Add(' abool:=b;');
  14793. Add(' b:=arr[3];');
  14794. Add(' arr[4]:=b;');
  14795. Add(' end;');
  14796. ConvertProgram;
  14797. CheckSource('TestClass_WithClassDoCreate',
  14798. LinesToStr([ // statements
  14799. 'rtl.createClass(this, "TObject", null, function () {',
  14800. ' this.$init = function () {',
  14801. ' this.aBool = false;',
  14802. ' this.Arr = [];',
  14803. ' };',
  14804. ' this.$final = function () {',
  14805. ' this.Arr = undefined;',
  14806. ' };',
  14807. ' this.Create = function () {',
  14808. ' return this;',
  14809. ' };',
  14810. '});',
  14811. 'this.Obj = null;',
  14812. 'this.b = false;'
  14813. ]),
  14814. LinesToStr([ // $mod.$main
  14815. 'var $with = $mod.TObject.$create("Create");',
  14816. '$mod.b = $with.aBool;',
  14817. '$with.aBool = $mod.b;',
  14818. '$mod.b = $with.Arr[1];',
  14819. '$with.Arr[2] = $mod.b;',
  14820. 'var $with1 = $mod.TObject;',
  14821. '$mod.Obj = $with1.$create("Create");',
  14822. 'var $with2 = $mod.Obj;',
  14823. '$with2.Create();',
  14824. '$mod.b = $with2.aBool;',
  14825. '$with2.aBool = $mod.b;',
  14826. '$mod.b = $with2.Arr[3];',
  14827. '$with2.Arr[4] = $mod.b;',
  14828. '']));
  14829. end;
  14830. procedure TTestModule.TestClass_WithClassInstDoProperty;
  14831. begin
  14832. StartProgram(false);
  14833. Add('type');
  14834. Add(' TObject = class');
  14835. Add(' FInt: longint;');
  14836. Add(' constructor Create;');
  14837. Add(' function GetSize: longint;');
  14838. Add(' procedure SetSize(Value: longint);');
  14839. Add(' property Int: longint read FInt write FInt;');
  14840. Add(' property Size: longint read GetSize write SetSize;');
  14841. Add(' end;');
  14842. Add('constructor TObject.Create; begin end;');
  14843. Add('function TObject.GetSize: longint; begin; end;');
  14844. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14845. Add('var');
  14846. Add(' Obj: tobject;');
  14847. Add(' i: longint;');
  14848. Add('begin');
  14849. Add(' with TObject.Create do begin');
  14850. Add(' i:=int;');
  14851. Add(' int:=i;');
  14852. Add(' i:=size;');
  14853. Add(' size:=i;');
  14854. Add(' end;');
  14855. Add(' with obj do begin');
  14856. Add(' i:=int;');
  14857. Add(' int:=i;');
  14858. Add(' i:=size;');
  14859. Add(' size:=i;');
  14860. Add(' end;');
  14861. ConvertProgram;
  14862. CheckSource('TestClass_WithClassInstDoProperty',
  14863. LinesToStr([ // statements
  14864. 'rtl.createClass(this, "TObject", null, function () {',
  14865. ' this.$init = function () {',
  14866. ' this.FInt = 0;',
  14867. ' };',
  14868. ' this.$final = function () {',
  14869. ' };',
  14870. ' this.Create = function () {',
  14871. ' return this;',
  14872. ' };',
  14873. ' this.GetSize = function () {',
  14874. ' var Result = 0;',
  14875. ' return Result;',
  14876. ' };',
  14877. ' this.SetSize = function (Value) {',
  14878. ' };',
  14879. '});',
  14880. 'this.Obj = null;',
  14881. 'this.i = 0;'
  14882. ]),
  14883. LinesToStr([ // $mod.$main
  14884. 'var $with = $mod.TObject.$create("Create");',
  14885. '$mod.i = $with.FInt;',
  14886. '$with.FInt = $mod.i;',
  14887. '$mod.i = $with.GetSize();',
  14888. '$with.SetSize($mod.i);',
  14889. 'var $with1 = $mod.Obj;',
  14890. '$mod.i = $with1.FInt;',
  14891. '$with1.FInt = $mod.i;',
  14892. '$mod.i = $with1.GetSize();',
  14893. '$with1.SetSize($mod.i);',
  14894. '']));
  14895. end;
  14896. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  14897. begin
  14898. StartProgram(false);
  14899. Add('type');
  14900. Add(' TObject = class');
  14901. Add(' constructor Create;');
  14902. Add(' function GetItems(Index: longint): longint;');
  14903. Add(' procedure SetItems(Index, Value: longint);');
  14904. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  14905. Add(' end;');
  14906. Add('constructor TObject.Create; begin end;');
  14907. Add('function tobject.getitems(index: longint): longint; begin; end;');
  14908. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  14909. Add('var');
  14910. Add(' Obj: tobject;');
  14911. Add(' i: longint;');
  14912. Add('begin');
  14913. Add(' with TObject.Create do begin');
  14914. Add(' i:=Items[1];');
  14915. Add(' Items[2]:=i;');
  14916. Add(' end;');
  14917. Add(' with obj do begin');
  14918. Add(' i:=Items[3];');
  14919. Add(' Items[4]:=i;');
  14920. Add(' end;');
  14921. ConvertProgram;
  14922. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  14923. LinesToStr([ // statements
  14924. 'rtl.createClass(this, "TObject", null, function () {',
  14925. ' this.$init = function () {',
  14926. ' };',
  14927. ' this.$final = function () {',
  14928. ' };',
  14929. ' this.Create = function () {',
  14930. ' return this;',
  14931. ' };',
  14932. ' this.GetItems = function (Index) {',
  14933. ' var Result = 0;',
  14934. ' return Result;',
  14935. ' };',
  14936. ' this.SetItems = function (Index, Value) {',
  14937. ' };',
  14938. '});',
  14939. 'this.Obj = null;',
  14940. 'this.i = 0;'
  14941. ]),
  14942. LinesToStr([ // $mod.$main
  14943. 'var $with = $mod.TObject.$create("Create");',
  14944. '$mod.i = $with.GetItems(1);',
  14945. '$with.SetItems(2, $mod.i);',
  14946. 'var $with1 = $mod.Obj;',
  14947. '$mod.i = $with1.GetItems(3);',
  14948. '$with1.SetItems(4, $mod.i);',
  14949. '']));
  14950. end;
  14951. procedure TTestModule.TestClass_WithClassInstDoFunc;
  14952. begin
  14953. StartProgram(false);
  14954. Add('type');
  14955. Add(' TObject = class');
  14956. Add(' constructor Create;');
  14957. Add(' function GetSize: longint;');
  14958. Add(' procedure SetSize(Value: longint);');
  14959. Add(' end;');
  14960. Add('constructor TObject.Create; begin end;');
  14961. Add('function TObject.GetSize: longint; begin; end;');
  14962. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14963. Add('var');
  14964. Add(' Obj: tobject;');
  14965. Add(' i: longint;');
  14966. Add('begin');
  14967. Add(' with TObject.Create do begin');
  14968. Add(' i:=GetSize;');
  14969. Add(' i:=GetSize();');
  14970. Add(' SetSize(i);');
  14971. Add(' end;');
  14972. Add(' with obj do begin');
  14973. Add(' i:=GetSize;');
  14974. Add(' i:=GetSize();');
  14975. Add(' SetSize(i);');
  14976. Add(' end;');
  14977. ConvertProgram;
  14978. CheckSource('TestClass_WithClassInstDoFunc',
  14979. LinesToStr([ // statements
  14980. 'rtl.createClass(this, "TObject", null, function () {',
  14981. ' this.$init = function () {',
  14982. ' };',
  14983. ' this.$final = function () {',
  14984. ' };',
  14985. ' this.Create = function () {',
  14986. ' return this;',
  14987. ' };',
  14988. ' this.GetSize = function () {',
  14989. ' var Result = 0;',
  14990. ' return Result;',
  14991. ' };',
  14992. ' this.SetSize = function (Value) {',
  14993. ' };',
  14994. '});',
  14995. 'this.Obj = null;',
  14996. 'this.i = 0;'
  14997. ]),
  14998. LinesToStr([ // $mod.$main
  14999. 'var $with = $mod.TObject.$create("Create");',
  15000. '$mod.i = $with.GetSize();',
  15001. '$mod.i = $with.GetSize();',
  15002. '$with.SetSize($mod.i);',
  15003. 'var $with1 = $mod.Obj;',
  15004. '$mod.i = $with1.GetSize();',
  15005. '$mod.i = $with1.GetSize();',
  15006. '$with1.SetSize($mod.i);',
  15007. '']));
  15008. end;
  15009. procedure TTestModule.TestClass_TypeCast;
  15010. begin
  15011. StartProgram(false);
  15012. Add('type');
  15013. Add(' TObject = class');
  15014. Add(' Next: TObject;');
  15015. Add(' constructor Create;');
  15016. Add(' end;');
  15017. Add(' TControl = class(TObject)');
  15018. Add(' Arr: array of TObject;');
  15019. Add(' function GetIt(vI: longint = 0): TObject;');
  15020. Add(' end;');
  15021. Add('constructor tobject.create; begin end;');
  15022. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15023. Add('var');
  15024. Add(' Obj: tobject;');
  15025. Add('begin');
  15026. Add(' obj:=tcontrol(obj).next;');
  15027. Add(' tcontrol(obj):=nil;');
  15028. Add(' obj:=tcontrol(obj);');
  15029. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15030. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15031. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15032. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15033. Add(' obj:=tcontrol(nil);');
  15034. ConvertProgram;
  15035. CheckSource('TestClass_TypeCast',
  15036. LinesToStr([ // statements
  15037. 'rtl.createClass(this, "TObject", null, function () {',
  15038. ' this.$init = function () {',
  15039. ' this.Next = null;',
  15040. ' };',
  15041. ' this.$final = function () {',
  15042. ' this.Next = undefined;',
  15043. ' };',
  15044. ' this.Create = function () {',
  15045. ' return this;',
  15046. ' };',
  15047. '});',
  15048. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15049. ' this.$init = function () {',
  15050. ' $mod.TObject.$init.call(this);',
  15051. ' this.Arr = [];',
  15052. ' };',
  15053. ' this.$final = function () {',
  15054. ' this.Arr = undefined;',
  15055. ' $mod.TObject.$final.call(this);',
  15056. ' };',
  15057. ' this.GetIt = function (vI) {',
  15058. ' var Result = null;',
  15059. ' return Result;',
  15060. ' };',
  15061. '});',
  15062. 'this.Obj = null;'
  15063. ]),
  15064. LinesToStr([ // $mod.$main
  15065. '$mod.Obj = $mod.Obj.Next;',
  15066. '$mod.Obj = null;',
  15067. '$mod.Obj = $mod.Obj;',
  15068. '$mod.Obj = $mod.Obj.GetIt(0);',
  15069. '$mod.Obj = $mod.Obj.GetIt(0);',
  15070. '$mod.Obj = $mod.Obj.GetIt(1);',
  15071. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15072. '$mod.Obj = null;',
  15073. '']));
  15074. end;
  15075. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15076. begin
  15077. StartProgram(false);
  15078. Add('type');
  15079. Add(' TObject = class end;');
  15080. Add('procedure ProcA(var A);');
  15081. Add('begin');
  15082. Add(' TObject(A):=nil;');
  15083. Add(' TObject(A):=TObject(A);');
  15084. Add(' if TObject(A)=nil then ;');
  15085. Add(' if nil=TObject(A) then ;');
  15086. Add('end;');
  15087. Add('procedure ProcB(out A);');
  15088. Add('begin');
  15089. Add(' TObject(A):=nil;');
  15090. Add(' TObject(A):=TObject(A);');
  15091. Add(' if TObject(A)=nil then ;');
  15092. Add(' if nil=TObject(A) then ;');
  15093. Add('end;');
  15094. Add('procedure ProcC(const A);');
  15095. Add('begin');
  15096. Add(' if TObject(A)=nil then ;');
  15097. Add(' if nil=TObject(A) then ;');
  15098. Add('end;');
  15099. Add('var o: TObject;');
  15100. Add('begin');
  15101. Add(' ProcA(o);');
  15102. Add(' ProcB(o);');
  15103. Add(' ProcC(o);');
  15104. ConvertProgram;
  15105. CheckSource('TestClass_TypeCastUntypedParam',
  15106. LinesToStr([ // statements
  15107. 'rtl.createClass(this, "TObject", null, function () {',
  15108. ' this.$init = function () {',
  15109. ' };',
  15110. ' this.$final = function () {',
  15111. ' };',
  15112. '});',
  15113. 'this.ProcA = function (A) {',
  15114. ' A.set(null);',
  15115. ' A.set(A.get());',
  15116. ' if (A.get() === null);',
  15117. ' if (null === A.get());',
  15118. '};',
  15119. 'this.ProcB = function (A) {',
  15120. ' A.set(null);',
  15121. ' A.set(A.get());',
  15122. ' if (A.get() === null);',
  15123. ' if (null === A.get());',
  15124. '};',
  15125. 'this.ProcC = function (A) {',
  15126. ' if (A === null);',
  15127. ' if (null === A);',
  15128. '};',
  15129. 'this.o = null;',
  15130. '']),
  15131. LinesToStr([ // $mod.$main
  15132. '$mod.ProcA({',
  15133. ' p: $mod,',
  15134. ' get: function () {',
  15135. ' return this.p.o;',
  15136. ' },',
  15137. ' set: function (v) {',
  15138. ' this.p.o = v;',
  15139. ' }',
  15140. '});',
  15141. '$mod.ProcB({',
  15142. ' p: $mod,',
  15143. ' get: function () {',
  15144. ' return this.p.o;',
  15145. ' },',
  15146. ' set: function (v) {',
  15147. ' this.p.o = v;',
  15148. ' }',
  15149. '});',
  15150. '$mod.ProcC($mod.o);',
  15151. '']));
  15152. end;
  15153. procedure TTestModule.TestClass_Overloads;
  15154. begin
  15155. StartProgram(false);
  15156. Add('type');
  15157. Add(' TObject = class');
  15158. Add(' procedure DoIt;');
  15159. Add(' procedure DoIt(vI: longint);');
  15160. Add(' end;');
  15161. Add('procedure TObject.DoIt;');
  15162. Add('begin');
  15163. Add(' DoIt;');
  15164. Add(' DoIt(1);');
  15165. Add('end;');
  15166. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15167. Add('begin');
  15168. ConvertProgram;
  15169. CheckSource('TestClass_Overloads',
  15170. LinesToStr([ // statements
  15171. 'rtl.createClass(this, "TObject", null, function () {',
  15172. ' this.$init = function () {',
  15173. ' };',
  15174. ' this.$final = function () {',
  15175. ' };',
  15176. ' this.DoIt = function () {',
  15177. ' this.DoIt();',
  15178. ' this.DoIt$1(1);',
  15179. ' };',
  15180. ' this.DoIt$1 = function (vI) {',
  15181. ' };',
  15182. '});',
  15183. '']),
  15184. LinesToStr([ // $mod.$main
  15185. '']));
  15186. end;
  15187. procedure TTestModule.TestClass_OverloadsAncestor;
  15188. begin
  15189. StartProgram(false);
  15190. Add('type');
  15191. Add(' TObject = class;');
  15192. Add(' TObject = class');
  15193. Add(' procedure DoIt(vA: longint);');
  15194. Add(' procedure DoIt(vA, vB: longint);');
  15195. Add(' end;');
  15196. Add(' TCar = class;');
  15197. Add(' TCar = class');
  15198. Add(' procedure DoIt(vA: longint);');
  15199. Add(' procedure DoIt(vA, vB: longint);');
  15200. Add(' end;');
  15201. Add('procedure tobject.doit(va: longint);');
  15202. Add('begin');
  15203. Add(' doit(1);');
  15204. Add(' doit(1,2);');
  15205. Add('end;');
  15206. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15207. Add('procedure tcar.doit(va: longint);');
  15208. Add('begin');
  15209. Add(' doit(1);');
  15210. Add(' doit(1,2);');
  15211. Add(' inherited doit(1);');
  15212. Add(' inherited doit(1,2);');
  15213. Add('end;');
  15214. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15215. Add('begin');
  15216. ConvertProgram;
  15217. CheckSource('TestClass_OverloadsAncestor',
  15218. LinesToStr([ // statements
  15219. 'rtl.createClass(this, "TObject", null, function () {',
  15220. ' this.$init = function () {',
  15221. ' };',
  15222. ' this.$final = function () {',
  15223. ' };',
  15224. ' this.DoIt = function (vA) {',
  15225. ' this.DoIt(1);',
  15226. ' this.DoIt$1(1,2);',
  15227. ' };',
  15228. ' this.DoIt$1 = function (vA, vB) {',
  15229. ' };',
  15230. '});',
  15231. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15232. ' this.DoIt$2 = function (vA) {',
  15233. ' this.DoIt$2(1);',
  15234. ' this.DoIt$3(1, 2);',
  15235. ' $mod.TObject.DoIt.call(this, 1);',
  15236. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15237. ' };',
  15238. ' this.DoIt$3 = function (vA, vB) {',
  15239. ' };',
  15240. '});',
  15241. '']),
  15242. LinesToStr([ // $mod.$main
  15243. '']));
  15244. end;
  15245. procedure TTestModule.TestClass_OverloadConstructor;
  15246. begin
  15247. StartProgram(false);
  15248. Add('type');
  15249. Add(' TObject = class');
  15250. Add(' constructor Create(vA: longint);');
  15251. Add(' constructor Create(vA, vB: longint);');
  15252. Add(' end;');
  15253. Add(' TCar = class');
  15254. Add(' constructor Create(vA: longint);');
  15255. Add(' constructor Create(vA, vB: longint);');
  15256. Add(' end;');
  15257. Add('constructor tobject.create(va: longint);');
  15258. Add('begin');
  15259. Add(' create(1);');
  15260. Add(' create(1,2);');
  15261. Add('end;');
  15262. Add('constructor tobject.create(va, vb: longint); begin end;');
  15263. Add('constructor tcar.create(va: longint);');
  15264. Add('begin');
  15265. Add(' create(1);');
  15266. Add(' create(1,2);');
  15267. Add(' inherited create(1);');
  15268. Add(' inherited create(1,2);');
  15269. Add('end;');
  15270. Add('constructor tcar.create(va, vb: longint); begin end;');
  15271. Add('begin');
  15272. Add(' tobject.create(1);');
  15273. Add(' tobject.create(1,2);');
  15274. Add(' tcar.create(1);');
  15275. Add(' tcar.create(1,2);');
  15276. ConvertProgram;
  15277. CheckSource('TestClass_OverloadConstructor',
  15278. LinesToStr([ // statements
  15279. 'rtl.createClass(this, "TObject", null, function () {',
  15280. ' this.$init = function () {',
  15281. ' };',
  15282. ' this.$final = function () {',
  15283. ' };',
  15284. ' this.Create = function (vA) {',
  15285. ' this.Create(1);',
  15286. ' this.Create$1(1,2);',
  15287. ' return this;',
  15288. ' };',
  15289. ' this.Create$1 = function (vA, vB) {',
  15290. ' return this;',
  15291. ' };',
  15292. '});',
  15293. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15294. ' this.Create$2 = function (vA) {',
  15295. ' this.Create$2(1);',
  15296. ' this.Create$3(1, 2);',
  15297. ' $mod.TObject.Create.call(this, 1);',
  15298. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15299. ' return this;',
  15300. ' };',
  15301. ' this.Create$3 = function (vA, vB) {',
  15302. ' return this;',
  15303. ' };',
  15304. '});',
  15305. '']),
  15306. LinesToStr([ // $mod.$main
  15307. '$mod.TObject.$create("Create", [1]);',
  15308. '$mod.TObject.$create("Create$1", [1, 2]);',
  15309. '$mod.TCar.$create("Create$2", [1]);',
  15310. '$mod.TCar.$create("Create$3", [1, 2]);',
  15311. '']));
  15312. end;
  15313. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15314. begin
  15315. StartProgram(false);
  15316. Add([
  15317. '{$mode delphi}',
  15318. 'type',
  15319. ' TObject = class end;',
  15320. ' TBird = class',
  15321. ' function {#a}GetValue: longint; overload; virtual;',
  15322. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15323. ' end;',
  15324. ' TEagle = class(TBird)',
  15325. ' function {#c}GetValue: longint; overload; override;',
  15326. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15327. ' end;',
  15328. 'function TBird.GetValue: longint;',
  15329. 'begin',
  15330. ' if 3={@a}GetValue then ;',
  15331. ' if 4={@b}GetValue(5) then ;',
  15332. 'end;',
  15333. 'function TBird.GetValue(AValue: longint): longint;',
  15334. 'begin',
  15335. 'end;',
  15336. 'function TEagle.GetValue: longint;',
  15337. 'begin',
  15338. ' if 13={@c}GetValue then ;',
  15339. ' if 14={@d}GetValue(15) then ;',
  15340. ' if 15=inherited {@a}GetValue then ;',
  15341. ' if 16=inherited {@b}GetValue(17) then ;',
  15342. 'end;',
  15343. 'function TEagle.GetValue(AValue: longint): longint;',
  15344. 'begin',
  15345. 'end;',
  15346. 'var',
  15347. ' e: TEagle;',
  15348. 'begin',
  15349. ' if 23=e.{@c}GetValue then ;',
  15350. ' if 24=e.{@d}GetValue(25) then ;']);
  15351. ConvertProgram;
  15352. CheckSource('TestClass_OverloadDelphiOverride',
  15353. LinesToStr([ // statements
  15354. 'rtl.createClass(this, "TObject", null, function () {',
  15355. ' this.$init = function () {',
  15356. ' };',
  15357. ' this.$final = function () {',
  15358. ' };',
  15359. '});',
  15360. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15361. ' this.GetValue = function () {',
  15362. ' var Result = 0;',
  15363. ' if (3 === this.GetValue()) ;',
  15364. ' if (4 === this.GetValue$1(5)) ;',
  15365. ' return Result;',
  15366. ' };',
  15367. ' this.GetValue$1 = function (AValue) {',
  15368. ' var Result = 0;',
  15369. ' return Result;',
  15370. ' };',
  15371. '});',
  15372. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15373. ' this.GetValue = function () {',
  15374. ' var Result = 0;',
  15375. ' if (13 === this.GetValue()) ;',
  15376. ' if (14 === this.GetValue$1(15)) ;',
  15377. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15378. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15379. ' return Result;',
  15380. ' };',
  15381. ' this.GetValue$1 = function (AValue) {',
  15382. ' var Result = 0;',
  15383. ' return Result;',
  15384. ' };',
  15385. '});',
  15386. 'this.e = null;',
  15387. '']),
  15388. LinesToStr([ // $mod.$main
  15389. 'if (23 === $mod.e.GetValue()) ;',
  15390. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15391. '']));
  15392. end;
  15393. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15394. begin
  15395. StartProgram(false);
  15396. Add([
  15397. '{$mode delphi}',
  15398. 'type',
  15399. ' TObject = class end;',
  15400. ' TAnimal = class',
  15401. ' public',
  15402. ' {#animal_a}A: longint;',
  15403. ' function {#animal_b}B: longint;',
  15404. ' end;',
  15405. ' TBird = class(TAnimal)',
  15406. ' public',
  15407. ' {#bird_a}A: double;',
  15408. ' {#bird_b}B: boolean;',
  15409. ' end;',
  15410. ' TEagle = class(TBird)',
  15411. ' public',
  15412. ' function {#eagle_a}A: boolean;',
  15413. ' {#eagle_b}B: double;',
  15414. ' end;',
  15415. 'function TAnimal.B: longint;',
  15416. 'begin',
  15417. 'end;',
  15418. 'function TEagle.A: boolean;',
  15419. 'begin',
  15420. ' {@eagle_b}B:=3.3;',
  15421. ' {@eagle_a}A();',
  15422. ' TBird(Self).{@bird_b}B:=true;',
  15423. ' TAnimal(Self).{@animal_a}A:=17;',
  15424. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  15425. 'end;',
  15426. 'var',
  15427. ' e: TEagle;',
  15428. 'begin',
  15429. ' e.{@eagle_b}B:=5.3;',
  15430. ' if e.{@eagle_a}A then ;',
  15431. '']);
  15432. ConvertProgram;
  15433. CheckSource('TestClass_ReintroduceVarDelphi',
  15434. LinesToStr([ // statements
  15435. 'rtl.createClass(this, "TObject", null, function () {',
  15436. ' this.$init = function () {',
  15437. ' };',
  15438. ' this.$final = function () {',
  15439. ' };',
  15440. '});',
  15441. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  15442. ' this.$init = function () {',
  15443. ' $mod.TObject.$init.call(this);',
  15444. ' this.A = 0;',
  15445. ' };',
  15446. ' this.B = function () {',
  15447. ' var Result = 0;',
  15448. ' return Result;',
  15449. ' };',
  15450. '});',
  15451. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  15452. ' this.$init = function () {',
  15453. ' $mod.TAnimal.$init.call(this);',
  15454. ' this.A$1 = 0.0;',
  15455. ' this.B$1 = false;',
  15456. ' };',
  15457. '});',
  15458. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15459. ' this.$init = function () {',
  15460. ' $mod.TBird.$init.call(this);',
  15461. ' this.B$2 = 0.0;',
  15462. ' };',
  15463. ' this.A$2 = function () {',
  15464. ' var Result = false;',
  15465. ' this.B$2 = 3.3;',
  15466. ' this.A$2();',
  15467. ' this.B$1 = true;',
  15468. ' this.A = 17;',
  15469. ' this.B$1 = this.A$1 > 1;',
  15470. ' return Result;',
  15471. ' };',
  15472. '});',
  15473. 'this.e = null;',
  15474. '']),
  15475. LinesToStr([ // $mod.$main
  15476. '$mod.e.B$2 = 5.3;',
  15477. 'if ($mod.e.A$2()) ;',
  15478. '']));
  15479. end;
  15480. procedure TTestModule.TestClass_ReintroducedVar;
  15481. begin
  15482. StartProgram(false);
  15483. Add('type');
  15484. Add(' TObject = class');
  15485. Add(' strict private');
  15486. Add(' Some: longint;');
  15487. Add(' end;');
  15488. Add(' TMobile = class');
  15489. Add(' strict private');
  15490. Add(' Some: string;');
  15491. Add(' end;');
  15492. Add(' TCar = class(tmobile)');
  15493. Add(' procedure Some;');
  15494. Add(' procedure Some(vA: longint);');
  15495. Add(' end;');
  15496. Add('procedure tcar.some;');
  15497. Add('begin');
  15498. Add(' Some;');
  15499. Add(' Some(1);');
  15500. Add('end;');
  15501. Add('procedure tcar.some(va: longint); begin end;');
  15502. Add('begin');
  15503. ConvertProgram;
  15504. CheckSource('TestClass_ReintroducedVar',
  15505. LinesToStr([ // statements
  15506. 'rtl.createClass(this, "TObject", null, function () {',
  15507. ' this.$init = function () {',
  15508. ' this.Some = 0;',
  15509. ' };',
  15510. ' this.$final = function () {',
  15511. ' };',
  15512. '});',
  15513. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  15514. ' this.$init = function () {',
  15515. ' $mod.TObject.$init.call(this);',
  15516. ' this.Some$1 = "";',
  15517. ' };',
  15518. '});',
  15519. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  15520. ' this.Some$2 = function () {',
  15521. ' this.Some$2();',
  15522. ' this.Some$3(1);',
  15523. ' };',
  15524. ' this.Some$3 = function (vA) {',
  15525. ' };',
  15526. '});',
  15527. '']),
  15528. LinesToStr([ // $mod.$main
  15529. '']));
  15530. end;
  15531. procedure TTestModule.TestClass_RaiseDescendant;
  15532. begin
  15533. StartProgram(false);
  15534. Add([
  15535. 'type',
  15536. ' TObject = class',
  15537. ' constructor Create(Msg: string);',
  15538. ' end;',
  15539. ' Exception = class',
  15540. ' end;',
  15541. ' EConvertError = class(Exception)',
  15542. ' end;',
  15543. 'constructor TObject.Create(Msg: string); begin end;',
  15544. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  15545. 'begin',
  15546. ' raise Exception.Create(''Bar1'');',
  15547. ' raise EConvertError.Create(''Bar2'');',
  15548. ' raise AssertConv(''Bar2'');',
  15549. ' raise AssertConv;',
  15550. '']);
  15551. ConvertProgram;
  15552. CheckSource('TestClass_RaiseDescendant',
  15553. LinesToStr([ // statements
  15554. 'rtl.createClass(this, "TObject", null, function () {',
  15555. ' this.$init = function () {',
  15556. ' };',
  15557. ' this.$final = function () {',
  15558. ' };',
  15559. ' this.Create = function (Msg) {',
  15560. ' return this;',
  15561. ' };',
  15562. '});',
  15563. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  15564. '});',
  15565. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  15566. '});',
  15567. 'this.AssertConv = function (Msg) {',
  15568. ' var Result = null;',
  15569. ' return Result;',
  15570. '};',
  15571. '']),
  15572. LinesToStr([ // $mod.$main
  15573. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  15574. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  15575. 'throw $mod.AssertConv("Bar2");',
  15576. 'throw $mod.AssertConv("def");',
  15577. '']));
  15578. end;
  15579. procedure TTestModule.TestClass_ExternalMethod;
  15580. begin
  15581. AddModuleWithIntfImplSrc('unit2.pas',
  15582. LinesToStr([
  15583. 'type',
  15584. ' TObject = class',
  15585. ' public',
  15586. ' procedure Intern; external name ''$DoIntern'';',
  15587. ' end;',
  15588. '']),
  15589. LinesToStr([
  15590. '']));
  15591. StartUnit(true);
  15592. Add('interface');
  15593. Add('uses unit2;');
  15594. Add('type');
  15595. Add(' TCar = class(TObject)');
  15596. Add(' public');
  15597. Add(' procedure Intern2; external name ''$DoIntern2'';');
  15598. Add(' procedure DoIt;');
  15599. Add(' end;');
  15600. Add('implementation');
  15601. Add('procedure tcar.doit;');
  15602. Add('begin');
  15603. Add(' Intern;');
  15604. Add(' Intern();');
  15605. Add(' Intern2;');
  15606. Add(' Intern2();');
  15607. Add('end;');
  15608. Add('var Obj: TCar;');
  15609. Add('begin');
  15610. Add(' obj.intern;');
  15611. Add(' obj.intern();');
  15612. Add(' obj.intern2;');
  15613. Add(' obj.intern2();');
  15614. Add(' obj.doit;');
  15615. Add(' obj.doit();');
  15616. Add(' with obj do begin');
  15617. Add(' Intern;');
  15618. Add(' Intern();');
  15619. Add(' Intern2;');
  15620. Add(' Intern2();');
  15621. Add(' end;');
  15622. ConvertUnit;
  15623. CheckSource('TestClass_ExternalMethod',
  15624. LinesToStr([
  15625. 'var $impl = $mod.$impl;',
  15626. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15627. ' this.DoIt = function () {',
  15628. ' this.$DoIntern();',
  15629. ' this.$DoIntern();',
  15630. ' this.$DoIntern2();',
  15631. ' this.$DoIntern2();',
  15632. ' };',
  15633. ' });',
  15634. '']),
  15635. LinesToStr([ // this.$init
  15636. '$impl.Obj.$DoIntern();',
  15637. '$impl.Obj.$DoIntern();',
  15638. '$impl.Obj.$DoIntern2();',
  15639. '$impl.Obj.$DoIntern2();',
  15640. '$impl.Obj.DoIt();',
  15641. '$impl.Obj.DoIt();',
  15642. 'var $with = $impl.Obj;',
  15643. '$with.$DoIntern();',
  15644. '$with.$DoIntern();',
  15645. '$with.$DoIntern2();',
  15646. '$with.$DoIntern2();',
  15647. '']),
  15648. LinesToStr([ // implementation
  15649. '$impl.Obj = null;',
  15650. '']) );
  15651. end;
  15652. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  15653. begin
  15654. StartProgram(false);
  15655. Add('type');
  15656. Add(' TObject = class');
  15657. Add(' procedure DoIt; virtual; external name ''Foo'';');
  15658. Add(' end;');
  15659. Add('begin');
  15660. SetExpectedPasResolverError('Virtual method name must match external',
  15661. nVirtualMethodNameMustMatchExternal);
  15662. ConvertProgram;
  15663. end;
  15664. procedure TTestModule.TestClass_ExternalOverrideFail;
  15665. begin
  15666. StartProgram(false);
  15667. Add('type');
  15668. Add(' TObject = class');
  15669. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  15670. Add(' end;');
  15671. Add(' TCar = class');
  15672. Add(' procedure DoIt; override; external name ''DoIt'';');
  15673. Add(' end;');
  15674. Add('begin');
  15675. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  15676. nInvalidXModifierY);
  15677. ConvertProgram;
  15678. end;
  15679. procedure TTestModule.TestClass_ExternalVar;
  15680. begin
  15681. AddModuleWithIntfImplSrc('unit2.pas',
  15682. LinesToStr([
  15683. '{$modeswitch externalclass}',
  15684. 'type',
  15685. ' TObject = class',
  15686. ' public',
  15687. ' Intern: longint external name ''$Intern'';',
  15688. ' Bracket: longint external name ''["A B"]'';',
  15689. ' end;',
  15690. '']),
  15691. LinesToStr([
  15692. '']));
  15693. StartUnit(true);
  15694. Add([
  15695. 'interface',
  15696. 'uses unit2;',
  15697. '{$modeswitch externalclass}',
  15698. 'type',
  15699. ' TCar = class(tobject)',
  15700. ' public',
  15701. ' Intern2: longint external name ''$Intern2'';',
  15702. ' procedure DoIt;',
  15703. ' end;',
  15704. 'implementation',
  15705. 'procedure tcar.doit;',
  15706. 'begin',
  15707. ' Intern:=Intern+1;',
  15708. ' Intern2:=Intern2+2;',
  15709. ' Bracket:=Bracket+3;',
  15710. 'end;',
  15711. 'var Obj: TCar;',
  15712. 'begin',
  15713. ' obj.intern:=obj.intern+1;',
  15714. ' obj.intern2:=obj.intern2+2;',
  15715. ' obj.Bracket:=obj.Bracket+3;',
  15716. ' with obj do begin',
  15717. ' intern:=intern+1;',
  15718. ' intern2:=intern2+2;',
  15719. ' Bracket:=Bracket+3;',
  15720. ' end;']);
  15721. ConvertUnit;
  15722. CheckSource('TestClass_ExternalVar',
  15723. LinesToStr([
  15724. 'var $impl = $mod.$impl;',
  15725. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15726. ' this.DoIt = function () {',
  15727. ' this.$Intern = this.$Intern + 1;',
  15728. ' this.$Intern2 = this.$Intern2 + 2;',
  15729. ' this["A B"] = this["A B"] + 3;',
  15730. ' };',
  15731. ' });',
  15732. '']),
  15733. LinesToStr([
  15734. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  15735. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  15736. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  15737. 'var $with = $impl.Obj;',
  15738. '$with.$Intern = $with.$Intern + 1;',
  15739. '$with.$Intern2 = $with.$Intern2 + 2;',
  15740. '$with["A B"] = $with["A B"] + 3;',
  15741. '']),
  15742. LinesToStr([ // implementation
  15743. '$impl.Obj = null;',
  15744. '']));
  15745. end;
  15746. procedure TTestModule.TestClass_Const;
  15747. begin
  15748. StartProgram(false);
  15749. Add([
  15750. 'type',
  15751. ' integer = longint;',
  15752. ' TClass = class of TObject;',
  15753. ' TObject = class',
  15754. ' public',
  15755. ' const cI: integer = 3;',
  15756. ' procedure DoIt;',
  15757. ' class procedure DoMore;',
  15758. ' end;',
  15759. 'procedure tobject.doit;',
  15760. 'begin',
  15761. ' if cI=4 then;',
  15762. ' if 5=cI then;',
  15763. ' if Self.cI=6 then;',
  15764. ' if 7=Self.cI then;',
  15765. ' with Self do begin',
  15766. ' if cI=11 then;',
  15767. ' if 12=cI then;',
  15768. ' end;',
  15769. 'end;',
  15770. 'class procedure tobject.domore;',
  15771. 'begin',
  15772. ' if cI=8 then;',
  15773. ' if Self.cI=9 then;',
  15774. ' if 10=cI then;',
  15775. ' if 11=Self.cI then;',
  15776. ' with Self do begin',
  15777. ' if cI=13 then;',
  15778. ' if 14=cI then;',
  15779. ' end;',
  15780. 'end;',
  15781. 'var',
  15782. ' Obj: TObject;',
  15783. ' Cla: TClass;',
  15784. 'begin',
  15785. ' if TObject.cI=21 then ;',
  15786. ' if Obj.cI=22 then ;',
  15787. ' if Cla.cI=23 then ;',
  15788. ' with obj do if ci=24 then;',
  15789. ' with TObject do if ci=25 then;',
  15790. ' with Cla do if ci=26 then;']);
  15791. ConvertProgram;
  15792. CheckSource('TestClass_Const',
  15793. LinesToStr([
  15794. 'rtl.createClass(this, "TObject", null, function () {',
  15795. ' this.cI = 3;',
  15796. ' this.$init = function () {',
  15797. ' };',
  15798. ' this.$final = function () {',
  15799. ' };',
  15800. ' this.DoIt = function () {',
  15801. ' if (this.cI === 4) ;',
  15802. ' if (5 === this.cI) ;',
  15803. ' if (this.cI === 6) ;',
  15804. ' if (7 === this.cI) ;',
  15805. ' if (this.cI === 11) ;',
  15806. ' if (12 === this.cI) ;',
  15807. ' };',
  15808. ' this.DoMore = function () {',
  15809. ' if (this.cI === 8) ;',
  15810. ' if (this.cI === 9) ;',
  15811. ' if (10 === this.cI) ;',
  15812. ' if (11 === this.cI) ;',
  15813. ' if (this.cI === 13) ;',
  15814. ' if (14 === this.cI) ;',
  15815. ' };',
  15816. '});',
  15817. 'this.Obj = null;',
  15818. 'this.Cla = null;',
  15819. '']),
  15820. LinesToStr([
  15821. 'if ($mod.TObject.cI === 21) ;',
  15822. 'if ($mod.Obj.cI === 22) ;',
  15823. 'if ($mod.Cla.cI === 23) ;',
  15824. 'var $with = $mod.Obj;',
  15825. 'if ($with.cI === 24) ;',
  15826. 'var $with1 = $mod.TObject;',
  15827. 'if ($with1.cI === 25) ;',
  15828. 'var $with2 = $mod.Cla;',
  15829. 'if ($with2.cI === 26) ;',
  15830. '']));
  15831. end;
  15832. procedure TTestModule.TestClass_ConstEnum;
  15833. begin
  15834. StartProgram(false);
  15835. Add([
  15836. 'type',
  15837. ' TEnum = (red,blue);',
  15838. ' TObject = class',
  15839. ' end;',
  15840. ' TAnimal = class',
  15841. ' public',
  15842. ' type TSubEnum = (light,dark);',
  15843. ' const a = high(TEnum);',
  15844. ' const b = high(TSubEnum);',
  15845. ' end;',
  15846. ' TBird = class(TAnimal)',
  15847. ' public',
  15848. ' const c = high(TEnum);',
  15849. ' const d = high(TSubEnum);',
  15850. ' end;',
  15851. ' TAnt = class',
  15852. ' public',
  15853. ' const e = high(TEnum);',
  15854. ' const f = high(TBird.TSubEnum);',
  15855. ' end;',
  15856. 'begin',
  15857. '']);
  15858. ConvertProgram;
  15859. CheckSource('TestClass_ConstEnum',
  15860. LinesToStr([
  15861. 'this.TEnum = {',
  15862. ' "0": "red",',
  15863. ' red: 0,',
  15864. ' "1": "blue",',
  15865. ' blue: 1',
  15866. '};',
  15867. 'rtl.createClass(this, "TObject", null, function () {',
  15868. ' this.$init = function () {',
  15869. ' };',
  15870. ' this.$final = function () {',
  15871. ' };',
  15872. '});',
  15873. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  15874. ' this.TSubEnum = {',
  15875. ' "0": "light",',
  15876. ' light: 0,',
  15877. ' "1": "dark",',
  15878. ' dark: 1',
  15879. ' };',
  15880. ' this.a = $mod.TEnum.blue;',
  15881. ' this.b = this.TSubEnum.dark;',
  15882. '});',
  15883. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  15884. ' this.c = $mod.TEnum.blue;',
  15885. ' this.d = this.TSubEnum.dark;',
  15886. '});',
  15887. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  15888. ' this.e = $mod.TEnum.blue;',
  15889. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  15890. '});',
  15891. '']),
  15892. LinesToStr([
  15893. '']));
  15894. end;
  15895. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  15896. begin
  15897. StartProgram(false);
  15898. Add([
  15899. 'type',
  15900. ' TObject = class',
  15901. ' const cI: longint = 3;',
  15902. ' procedure Fly;',
  15903. ' procedure Run;',
  15904. ' end;',
  15905. ' TBird = class',
  15906. ' procedure Go;',
  15907. ' end;',
  15908. 'procedure tobject.fly;',
  15909. 'const cI: word = 4;',
  15910. 'begin',
  15911. ' if cI=Self.cI then ;',
  15912. 'end;',
  15913. 'procedure tobject.run;',
  15914. 'const cI: word = 5;',
  15915. 'begin',
  15916. ' if cI=Self.cI then ;',
  15917. 'end;',
  15918. 'procedure tbird.go;',
  15919. 'const cI: word = 6;',
  15920. 'begin',
  15921. ' if cI=Self.cI then ;',
  15922. 'end;',
  15923. 'begin',
  15924. '']);
  15925. ConvertProgram;
  15926. CheckSource('TestClass_LocalConstDuplicate_Prg',
  15927. LinesToStr([
  15928. 'rtl.createClass(this, "TObject", null, function () {',
  15929. ' this.cI = 3;',
  15930. ' this.$init = function () {',
  15931. ' };',
  15932. ' this.$final = function () {',
  15933. ' };',
  15934. ' var cI$1 = 4;',
  15935. ' this.Fly = function () {',
  15936. ' if (cI$1 === this.cI) ;',
  15937. ' };',
  15938. ' var cI$2 = 5;',
  15939. ' this.Run = function () {',
  15940. ' if (cI$2 === this.cI) ;',
  15941. ' };',
  15942. '});',
  15943. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15944. ' var cI$3 = 6;',
  15945. ' this.Go = function () {',
  15946. ' if (cI$3 === this.cI) ;',
  15947. ' };',
  15948. '});',
  15949. '']),
  15950. LinesToStr([
  15951. '']));
  15952. end;
  15953. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  15954. begin
  15955. StartUnit(false);
  15956. Add([
  15957. 'interface',
  15958. 'type',
  15959. ' TObject = class',
  15960. ' const cI: longint = 3;',
  15961. ' procedure Fly;',
  15962. ' procedure Run;',
  15963. ' end;',
  15964. ' TBird = class',
  15965. ' procedure Go;',
  15966. ' end;',
  15967. 'implementation',
  15968. 'procedure tobject.fly;',
  15969. 'const cI: word = 4;',
  15970. 'begin',
  15971. ' if cI=Self.cI then ;',
  15972. 'end;',
  15973. 'procedure tobject.run;',
  15974. 'const cI: word = 5;',
  15975. 'begin',
  15976. ' if cI=Self.cI then ;',
  15977. 'end;',
  15978. 'procedure tbird.go;',
  15979. 'const cI: word = 6;',
  15980. 'begin',
  15981. ' if cI=Self.cI then ;',
  15982. 'end;',
  15983. '']);
  15984. ConvertUnit;
  15985. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15986. LinesToStr([
  15987. 'rtl.createClass(this, "TObject", null, function () {',
  15988. ' this.cI = 3;',
  15989. ' this.$init = function () {',
  15990. ' };',
  15991. ' this.$final = function () {',
  15992. ' };',
  15993. ' var cI$1 = 4;',
  15994. ' this.Fly = function () {',
  15995. ' if (cI$1 === this.cI) ;',
  15996. ' };',
  15997. ' var cI$2 = 5;',
  15998. ' this.Run = function () {',
  15999. ' if (cI$2 === this.cI) ;',
  16000. ' };',
  16001. '});',
  16002. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16003. ' var cI$3 = 6;',
  16004. ' this.Go = function () {',
  16005. ' if (cI$3 === this.cI) ;',
  16006. ' };',
  16007. '});',
  16008. '']),
  16009. '',
  16010. '');
  16011. end;
  16012. procedure TTestModule.TestClass_LocalVarSelfFail;
  16013. begin
  16014. StartProgram(false);
  16015. Add([
  16016. 'type',
  16017. ' TObject = class',
  16018. ' constructor Create;',
  16019. ' end;',
  16020. 'constructor tobject.create;',
  16021. 'var self: longint;',
  16022. 'begin',
  16023. 'end',
  16024. 'begin',
  16025. '']);
  16026. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16027. ConvertProgram;
  16028. end;
  16029. procedure TTestModule.TestClass_ArgSelfFail;
  16030. begin
  16031. StartProgram(false);
  16032. Add([
  16033. 'type',
  16034. ' TObject = class',
  16035. ' procedure DoIt(Self: longint);',
  16036. ' end;',
  16037. 'procedure tobject.doit(self: longint);',
  16038. 'begin',
  16039. 'end',
  16040. 'begin',
  16041. '']);
  16042. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16043. ConvertProgram;
  16044. end;
  16045. procedure TTestModule.TestClass_NestedProcSelf;
  16046. begin
  16047. StartProgram(false);
  16048. Add([
  16049. 'type',
  16050. ' TObject = class',
  16051. ' Key: longint;',
  16052. ' class var State: longint;',
  16053. ' procedure DoIt;',
  16054. ' function GetSize: longint; virtual; abstract;',
  16055. ' procedure SetSize(Value: longint); virtual; abstract;',
  16056. ' property Size: longint read GetSize write SetSize;',
  16057. ' end;',
  16058. 'procedure tobject.doit;',
  16059. ' procedure Sub;',
  16060. ' begin',
  16061. ' key:=key+2;',
  16062. ' self.key:=self.key+3;',
  16063. ' state:=state+4;',
  16064. ' self.state:=self.state+5;',
  16065. ' tobject.state:=tobject.state+6;',
  16066. ' size:=size+7;',
  16067. ' self.size:=self.size+8;',
  16068. ' end;',
  16069. 'begin',
  16070. ' sub;',
  16071. ' key:=key+12;',
  16072. ' self.key:=self.key+13;',
  16073. ' state:=state+14;',
  16074. ' self.state:=self.state+15;',
  16075. ' tobject.state:=tobject.state+16;',
  16076. ' size:=size+17;',
  16077. ' self.size:=self.size+18;',
  16078. 'end;',
  16079. 'begin',
  16080. '']);
  16081. ConvertProgram;
  16082. CheckSource('TestClass_NestedProcSelf',
  16083. LinesToStr([ // statements
  16084. 'rtl.createClass(this, "TObject", null, function () {',
  16085. ' this.State = 0;',
  16086. ' this.$init = function () {',
  16087. ' this.Key = 0;',
  16088. ' };',
  16089. ' this.$final = function () {',
  16090. ' };',
  16091. ' this.DoIt = function () {',
  16092. ' var $Self = this;',
  16093. ' function Sub() {',
  16094. ' $Self.Key = $Self.Key + 2;',
  16095. ' $Self.Key = $Self.Key + 3;',
  16096. ' $mod.TObject.State = $Self.State + 4;',
  16097. ' $mod.TObject.State = $Self.State + 5;',
  16098. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16099. ' $Self.SetSize($Self.GetSize() + 7);',
  16100. ' $Self.SetSize($Self.GetSize() + 8);',
  16101. ' };',
  16102. ' Sub();',
  16103. ' this.Key = this.Key + 12;',
  16104. ' $Self.Key = $Self.Key + 13;',
  16105. ' $mod.TObject.State = this.State + 14;',
  16106. ' $mod.TObject.State = $Self.State + 15;',
  16107. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16108. ' this.SetSize(this.GetSize() + 17);',
  16109. ' $Self.SetSize($Self.GetSize() + 18);',
  16110. ' };',
  16111. '});',
  16112. '']),
  16113. LinesToStr([ // $mod.$main
  16114. '']));
  16115. end;
  16116. procedure TTestModule.TestClass_NestedProcSelf2;
  16117. begin
  16118. StartProgram(false);
  16119. Add([
  16120. 'type',
  16121. ' TObject = class',
  16122. ' Key: longint;',
  16123. ' class var State: longint;',
  16124. ' function GetSize: longint; virtual; abstract;',
  16125. ' procedure SetSize(Value: longint); virtual; abstract;',
  16126. ' property Size: longint read GetSize write SetSize;',
  16127. ' end;',
  16128. ' TBird = class',
  16129. ' procedure DoIt;',
  16130. ' end;',
  16131. 'procedure tbird.doit;',
  16132. ' procedure Sub;',
  16133. ' begin',
  16134. ' key:=key+2;',
  16135. ' self.key:=self.key+3;',
  16136. ' state:=state+4;',
  16137. ' self.state:=self.state+5;',
  16138. ' tobject.state:=tobject.state+6;',
  16139. ' size:=size+7;',
  16140. ' self.size:=self.size+8;',
  16141. ' end;',
  16142. 'begin',
  16143. ' sub;',
  16144. ' key:=key+12;',
  16145. ' self.key:=self.key+13;',
  16146. ' state:=state+14;',
  16147. ' self.state:=self.state+15;',
  16148. ' tobject.state:=tobject.state+16;',
  16149. ' size:=size+17;',
  16150. ' self.size:=self.size+18;',
  16151. 'end;',
  16152. 'begin',
  16153. '']);
  16154. ConvertProgram;
  16155. CheckSource('TestClass_NestedProcSelf2',
  16156. LinesToStr([ // statements
  16157. 'rtl.createClass(this, "TObject", null, function () {',
  16158. ' this.State = 0;',
  16159. ' this.$init = function () {',
  16160. ' this.Key = 0;',
  16161. ' };',
  16162. ' this.$final = function () {',
  16163. ' };',
  16164. '});',
  16165. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16166. ' this.DoIt = function () {',
  16167. ' var $Self = this;',
  16168. ' function Sub() {',
  16169. ' $Self.Key = $Self.Key + 2;',
  16170. ' $Self.Key = $Self.Key + 3;',
  16171. ' $mod.TObject.State = $Self.State + 4;',
  16172. ' $mod.TObject.State = $Self.State + 5;',
  16173. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16174. ' $Self.SetSize($Self.GetSize() + 7);',
  16175. ' $Self.SetSize($Self.GetSize() + 8);',
  16176. ' };',
  16177. ' Sub();',
  16178. ' this.Key = this.Key + 12;',
  16179. ' $Self.Key = $Self.Key + 13;',
  16180. ' $mod.TObject.State = this.State + 14;',
  16181. ' $mod.TObject.State = $Self.State + 15;',
  16182. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16183. ' this.SetSize(this.GetSize() + 17);',
  16184. ' $Self.SetSize($Self.GetSize() + 18);',
  16185. ' };',
  16186. '});',
  16187. '']),
  16188. LinesToStr([ // $mod.$main
  16189. '']));
  16190. end;
  16191. procedure TTestModule.TestClass_NestedProcClassSelf;
  16192. begin
  16193. StartProgram(false);
  16194. Add([
  16195. 'type',
  16196. ' TObject = class',
  16197. ' class var State: longint;',
  16198. ' class procedure DoIt;',
  16199. ' class function GetSize: longint; virtual; abstract;',
  16200. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16201. ' class property Size: longint read GetSize write SetSize;',
  16202. ' end;',
  16203. 'class procedure tobject.doit;',
  16204. ' procedure Sub;',
  16205. ' begin',
  16206. ' state:=state+2;',
  16207. ' self.state:=self.state+3;',
  16208. ' tobject.state:=tobject.state+4;',
  16209. ' size:=size+5;',
  16210. ' self.size:=self.size+6;',
  16211. ' tobject.size:=tobject.size+7;',
  16212. ' end;',
  16213. 'begin',
  16214. ' sub;',
  16215. ' state:=state+12;',
  16216. ' self.state:=self.state+13;',
  16217. ' tobject.state:=tobject.state+14;',
  16218. ' size:=size+15;',
  16219. ' self.size:=self.size+16;',
  16220. ' tobject.size:=tobject.size+17;',
  16221. 'end;',
  16222. 'begin',
  16223. '']);
  16224. ConvertProgram;
  16225. CheckSource('TestClass_NestedProcClassSelf',
  16226. LinesToStr([ // statements
  16227. 'rtl.createClass(this, "TObject", null, function () {',
  16228. ' this.State = 0;',
  16229. ' this.$init = function () {',
  16230. ' };',
  16231. ' this.$final = function () {',
  16232. ' };',
  16233. ' this.DoIt = function () {',
  16234. ' var $Self = this;',
  16235. ' function Sub() {',
  16236. ' $mod.TObject.State = $Self.State + 2;',
  16237. ' $mod.TObject.State = $Self.State + 3;',
  16238. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16239. ' $Self.SetSize($Self.GetSize() + 5);',
  16240. ' $Self.SetSize($Self.GetSize() + 6);',
  16241. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16242. ' };',
  16243. ' Sub();',
  16244. ' $mod.TObject.State = this.State + 12;',
  16245. ' $mod.TObject.State = $Self.State + 13;',
  16246. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16247. ' this.SetSize(this.GetSize() + 15);',
  16248. ' $Self.SetSize($Self.GetSize() + 16);',
  16249. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16250. ' };',
  16251. '});',
  16252. '']),
  16253. LinesToStr([ // $mod.$main
  16254. '']));
  16255. end;
  16256. procedure TTestModule.TestClass_NestedProcCallInherited;
  16257. begin
  16258. StartProgram(false);
  16259. Add([
  16260. 'type',
  16261. ' TObject = class',
  16262. ' function DoIt(k: boolean): longint; virtual;',
  16263. ' end;',
  16264. ' TBird = class',
  16265. ' function DoIt(k: boolean): longint; override;',
  16266. ' end;',
  16267. 'function tobject.doit(k: boolean): longint;',
  16268. 'begin',
  16269. 'end;',
  16270. 'function tbird.doit(k: boolean): longint;',
  16271. ' procedure Sub;',
  16272. ' begin',
  16273. ' inherited DoIt(true);',
  16274. //' if inherited DoIt(false)=4 then ;',
  16275. ' end;',
  16276. 'begin',
  16277. ' Sub;',
  16278. ' inherited;',
  16279. ' inherited DoIt(true);',
  16280. //' if inherited DoIt(false)=14 then ;',
  16281. 'end;',
  16282. 'begin',
  16283. '']);
  16284. ConvertProgram;
  16285. CheckSource('TestClass_NestedProcCallInherited',
  16286. LinesToStr([ // statements
  16287. 'rtl.createClass(this, "TObject", null, function () {',
  16288. ' this.$init = function () {',
  16289. ' };',
  16290. ' this.$final = function () {',
  16291. ' };',
  16292. ' this.DoIt = function (k) {',
  16293. ' var Result = 0;',
  16294. ' return Result;',
  16295. ' };',
  16296. '});',
  16297. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16298. ' this.DoIt = function (k) {',
  16299. ' var $Self = this;',
  16300. ' var Result = 0;',
  16301. ' function Sub() {',
  16302. ' $mod.TObject.DoIt.call($Self, true);',
  16303. ' };',
  16304. ' Sub();',
  16305. ' $mod.TObject.DoIt.apply(this, arguments);',
  16306. ' $mod.TObject.DoIt.call(this, true);',
  16307. ' return Result;',
  16308. ' };',
  16309. '});',
  16310. '']),
  16311. LinesToStr([ // $mod.$main
  16312. '']));
  16313. end;
  16314. procedure TTestModule.TestClass_TObjectFree;
  16315. begin
  16316. StartProgram(false);
  16317. Add([
  16318. 'type',
  16319. ' TObject = class',
  16320. ' Obj: tobject;',
  16321. ' procedure Free;',
  16322. ' procedure Release;',
  16323. ' end;',
  16324. 'procedure tobject.free;',
  16325. 'begin',
  16326. 'end;',
  16327. 'procedure tobject.release;',
  16328. 'begin',
  16329. ' free;',
  16330. ' if true then free;',
  16331. 'end;',
  16332. 'function DoIt(o: tobject): tobject;',
  16333. 'var l: tobject;',
  16334. 'begin',
  16335. ' o.free;',
  16336. ' o.free();',
  16337. ' l.free;',
  16338. ' l.free();',
  16339. ' o.obj.free;',
  16340. ' o.obj.free();',
  16341. ' with o do obj.free;',
  16342. ' with o do obj.free();',
  16343. ' result.Free;',
  16344. ' result.Free();',
  16345. 'end;',
  16346. 'var o: tobject;',
  16347. ' a: array of tobject;',
  16348. 'begin',
  16349. ' o.free;',
  16350. ' o.obj.free;',
  16351. ' a[1+2].free;',
  16352. '']);
  16353. ConvertProgram;
  16354. CheckSource('TestClass_TObjectFree',
  16355. LinesToStr([ // statements
  16356. 'rtl.createClass(this, "TObject", null, function () {',
  16357. ' this.$init = function () {',
  16358. ' this.Obj = null;',
  16359. ' };',
  16360. ' this.$final = function () {',
  16361. ' this.Obj = undefined;',
  16362. ' };',
  16363. ' this.Free = function () {',
  16364. ' };',
  16365. ' this.Release = function () {',
  16366. ' this.Free();',
  16367. ' if (true) this.Free();',
  16368. ' };',
  16369. '});',
  16370. 'this.DoIt = function (o) {',
  16371. ' var Result = null;',
  16372. ' var l = null;',
  16373. ' o = rtl.freeLoc(o);',
  16374. ' o = rtl.freeLoc(o);',
  16375. ' l = rtl.freeLoc(l);',
  16376. ' l = rtl.freeLoc(l);',
  16377. ' rtl.free(o, "Obj");',
  16378. ' rtl.free(o, "Obj");',
  16379. ' rtl.free(o, "Obj");',
  16380. ' rtl.free(o, "Obj");',
  16381. ' Result = rtl.freeLoc(Result);',
  16382. ' Result = rtl.freeLoc(Result);',
  16383. ' return Result;',
  16384. '};',
  16385. 'this.o = null;',
  16386. 'this.a = [];',
  16387. '']),
  16388. LinesToStr([ // $mod.$main
  16389. 'rtl.free($mod, "o");',
  16390. 'rtl.free($mod.o, "Obj");',
  16391. 'rtl.free($mod.a, 1 + 2);',
  16392. '']));
  16393. end;
  16394. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16395. begin
  16396. StartProgram(false);
  16397. Add([
  16398. 'type',
  16399. ' TObject = class',
  16400. ' Obj: tobject;',
  16401. ' procedure Free;',
  16402. ' end;',
  16403. 'procedure tobject.free;',
  16404. 'begin',
  16405. 'end;',
  16406. 'procedure DoIt(var o: tobject);',
  16407. 'begin',
  16408. ' o.free;',
  16409. ' o.free();',
  16410. 'end;',
  16411. 'begin',
  16412. '']);
  16413. ConvertProgram;
  16414. CheckSource('TestClass_TObjectFree_VarArg',
  16415. LinesToStr([ // statements
  16416. 'rtl.createClass(this, "TObject", null, function () {',
  16417. ' this.$init = function () {',
  16418. ' this.Obj = null;',
  16419. ' };',
  16420. ' this.$final = function () {',
  16421. ' this.Obj = undefined;',
  16422. ' };',
  16423. ' this.Free = function () {',
  16424. ' };',
  16425. '});',
  16426. 'this.DoIt = function (o) {',
  16427. ' o.set(rtl.freeLoc(o.get()));',
  16428. ' o.set(rtl.freeLoc(o.get()));',
  16429. '};',
  16430. '']),
  16431. LinesToStr([ // $mod.$main
  16432. '']));
  16433. end;
  16434. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  16435. begin
  16436. StartProgram(false);
  16437. Add([
  16438. 'type',
  16439. ' TObject = class',
  16440. ' constructor Create;',
  16441. ' procedure Free;',
  16442. ' end;',
  16443. 'constructor TObject.Create; begin end;',
  16444. 'procedure tobject.free; begin end;',
  16445. 'begin',
  16446. ' with tobject.create do free;',
  16447. '']);
  16448. ConvertProgram;
  16449. CheckSource('TestClass_TObjectFreeNewInstance',
  16450. LinesToStr([ // statements
  16451. 'rtl.createClass(this, "TObject", null, function () {',
  16452. ' this.$init = function () {',
  16453. ' };',
  16454. ' this.$final = function () {',
  16455. ' };',
  16456. ' this.Create = function () {',
  16457. ' return this;',
  16458. ' };',
  16459. ' this.Free = function () {',
  16460. ' };',
  16461. '});',
  16462. '']),
  16463. LinesToStr([ // $mod.$main
  16464. 'var $with = $mod.TObject.$create("Create");',
  16465. '$with=rtl.freeLoc($with);',
  16466. '']));
  16467. end;
  16468. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  16469. begin
  16470. StartProgram(false);
  16471. Add([
  16472. 'type',
  16473. ' TObject = class',
  16474. ' destructor Destroy;',
  16475. ' procedure Free;',
  16476. ' end;',
  16477. 'destructor TObject.Destroy; begin end;',
  16478. 'procedure tobject.free; begin end;',
  16479. 'var o: tobject;',
  16480. 'begin',
  16481. ' o.free;',
  16482. '']);
  16483. Converter.UseLowerCase:=true;
  16484. ConvertProgram;
  16485. CheckSource('TestClass_TObjectFreeLowerCase',
  16486. LinesToStr([ // statements
  16487. 'rtl.createClass(this, "tobject", null, function () {',
  16488. ' this.$init = function () {',
  16489. ' };',
  16490. ' this.$final = function () {',
  16491. ' };',
  16492. ' rtl.tObjectDestroy = "destroy";',
  16493. ' this.destroy = function () {',
  16494. ' };',
  16495. ' this.free = function () {',
  16496. ' };',
  16497. '});',
  16498. 'this.o = null;',
  16499. '']),
  16500. LinesToStr([ // $mod.$main
  16501. 'rtl.free($mod, "o");',
  16502. '']));
  16503. end;
  16504. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  16505. begin
  16506. StartProgram(false);
  16507. Add([
  16508. 'type',
  16509. ' TObject = class',
  16510. ' procedure Free;',
  16511. ' function GetObj: tobject; virtual; abstract;',
  16512. ' end;',
  16513. 'procedure tobject.free;',
  16514. 'begin',
  16515. 'end;',
  16516. 'var o: tobject;',
  16517. 'begin',
  16518. ' o.getobj.free;',
  16519. '']);
  16520. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  16521. ConvertProgram;
  16522. end;
  16523. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  16524. begin
  16525. StartProgram(false);
  16526. Add([
  16527. 'type',
  16528. ' TObject = class',
  16529. ' procedure Free;',
  16530. ' FObj: TObject;',
  16531. ' property Obj: tobject read FObj write FObj;',
  16532. ' end;',
  16533. 'procedure tobject.free;',
  16534. 'begin',
  16535. 'end;',
  16536. 'var o: tobject;',
  16537. 'begin',
  16538. ' o.obj.free;',
  16539. '']);
  16540. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  16541. ConvertProgram;
  16542. end;
  16543. procedure TTestModule.TestClass_ForIn;
  16544. begin
  16545. StartProgram(false);
  16546. Add([
  16547. 'type',
  16548. ' TObject = class end;',
  16549. ' TItem = TObject;',
  16550. ' TEnumerator = class',
  16551. ' FCurrent: TItem;',
  16552. ' property Current: TItem read FCurrent;',
  16553. ' function MoveNext: boolean;',
  16554. ' end;',
  16555. ' TBird = class',
  16556. ' function GetEnumerator: TEnumerator;',
  16557. ' end;',
  16558. 'function TEnumerator.MoveNext: boolean;',
  16559. 'begin',
  16560. 'end;',
  16561. 'function TBird.GetEnumerator: TEnumerator;',
  16562. 'begin',
  16563. 'end;',
  16564. 'var',
  16565. ' b: TBird;',
  16566. ' i, i2: TItem;',
  16567. 'begin',
  16568. ' for i in b do i2:=i;']);
  16569. ConvertProgram;
  16570. CheckSource('TestClass_ForIn',
  16571. LinesToStr([ // statements
  16572. 'rtl.createClass(this, "TObject", null, function () {',
  16573. ' this.$init = function () {',
  16574. ' };',
  16575. ' this.$final = function () {',
  16576. ' };',
  16577. '});',
  16578. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  16579. ' this.$init = function () {',
  16580. ' $mod.TObject.$init.call(this);',
  16581. ' this.FCurrent = null;',
  16582. ' };',
  16583. ' this.$final = function () {',
  16584. ' this.FCurrent = undefined;',
  16585. ' $mod.TObject.$final.call(this);',
  16586. ' };',
  16587. ' this.MoveNext = function () {',
  16588. ' var Result = false;',
  16589. ' return Result;',
  16590. ' };',
  16591. '});',
  16592. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16593. ' this.GetEnumerator = function () {',
  16594. ' var Result = null;',
  16595. ' return Result;',
  16596. ' };',
  16597. '});',
  16598. 'this.b = null;',
  16599. 'this.i = null;',
  16600. 'this.i2 = null;'
  16601. ]),
  16602. LinesToStr([ // $mod.$main
  16603. 'var $in = $mod.b.GetEnumerator();',
  16604. 'try {',
  16605. ' while ($in.MoveNext()){',
  16606. ' $mod.i = $in.FCurrent;',
  16607. ' $mod.i2 = $mod.i;',
  16608. ' }',
  16609. '} finally {',
  16610. ' $in = rtl.freeLoc($in)',
  16611. '};',
  16612. '']));
  16613. end;
  16614. procedure TTestModule.TestClass_DispatchMessage;
  16615. begin
  16616. StartProgram(false);
  16617. Add([
  16618. 'type',
  16619. ' TObject = class',
  16620. ' {$DispatchField DispInt}',
  16621. ' procedure Dispatch(var Msg); virtual; abstract;',
  16622. ' {$DispatchStrField DispStr}',
  16623. ' procedure DispatchStr(var Msg); virtual; abstract;',
  16624. ' end;',
  16625. ' THopMsg = record',
  16626. ' DispInt: longint;',
  16627. ' end;',
  16628. ' TPutMsg = record',
  16629. ' DispStr: string;',
  16630. ' end;',
  16631. ' TBird = class',
  16632. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  16633. ' procedure Run; overload; virtual; abstract;',
  16634. ' procedure Run(var Msg); overload; message ''Fast'';',
  16635. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  16636. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  16637. ' end;',
  16638. 'procedure TBird.Run(var Msg);',
  16639. 'begin',
  16640. 'end;',
  16641. 'begin',
  16642. '']);
  16643. ConvertProgram;
  16644. CheckSource('TestClass_Message',
  16645. LinesToStr([ // statements
  16646. 'rtl.createClass(this, "TObject", null, function () {',
  16647. ' this.$init = function () {',
  16648. ' };',
  16649. ' this.$final = function () {',
  16650. ' };',
  16651. '});',
  16652. 'rtl.recNewT(this, "THopMsg", function () {',
  16653. ' this.DispInt = 0;',
  16654. ' this.$eq = function (b) {',
  16655. ' return this.DispInt === b.DispInt;',
  16656. ' };',
  16657. ' this.$assign = function (s) {',
  16658. ' this.DispInt = s.DispInt;',
  16659. ' return this;',
  16660. ' };',
  16661. '});',
  16662. 'rtl.recNewT(this, "TPutMsg", function () {',
  16663. ' this.DispStr = "";',
  16664. ' this.$eq = function (b) {',
  16665. ' return this.DispStr === b.DispStr;',
  16666. ' };',
  16667. ' this.$assign = function (s) {',
  16668. ' this.DispStr = s.DispStr;',
  16669. ' return this;',
  16670. ' };',
  16671. '});',
  16672. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16673. ' this.Run$1 = function (Msg) {',
  16674. ' };',
  16675. ' this.$msgint = {',
  16676. ' "2": "Fly",',
  16677. ' "3": "Hop"',
  16678. ' };',
  16679. ' this.$msgstr = {',
  16680. ' Fast: "Run$1",',
  16681. ' foo: "Put"',
  16682. ' };',
  16683. '});',
  16684. '']),
  16685. LinesToStr([ // $mod.$main
  16686. '']));
  16687. end;
  16688. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  16689. begin
  16690. StartProgram(false);
  16691. Add([
  16692. 'type',
  16693. ' TObject = class',
  16694. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  16695. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  16696. ' end;',
  16697. 'begin',
  16698. '']);
  16699. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  16700. ConvertProgram;
  16701. end;
  16702. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  16703. begin
  16704. StartProgram(false);
  16705. Add([
  16706. 'type',
  16707. ' TObject = class',
  16708. ' {$dispatchfield Msg}',
  16709. ' procedure Dispatch(var Msg); virtual; abstract;',
  16710. ' end;',
  16711. ' TFlyMsg = record',
  16712. ' FlyId: longint;',
  16713. ' end;',
  16714. ' TBird = class',
  16715. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  16716. ' end;',
  16717. 'begin',
  16718. '']);
  16719. ConvertProgram;
  16720. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  16721. end;
  16722. procedure TTestModule.TestClassOf_Create;
  16723. begin
  16724. StartProgram(false);
  16725. Add('type');
  16726. Add(' TObject = class');
  16727. Add(' constructor Create;');
  16728. Add(' end;');
  16729. Add(' TClass = class of TObject;');
  16730. Add('constructor tobject.create; begin end;');
  16731. Add('var');
  16732. Add(' Obj: tobject;');
  16733. Add(' C: tclass;');
  16734. Add('begin');
  16735. Add(' obj:=C.create;');
  16736. Add(' with c do obj:=create;');
  16737. ConvertProgram;
  16738. CheckSource('TestClassOf_Create',
  16739. LinesToStr([ // statements
  16740. 'rtl.createClass(this, "TObject", null, function () {',
  16741. ' this.$init = function () {',
  16742. ' };',
  16743. ' this.$final = function () {',
  16744. ' };',
  16745. ' this.Create = function () {',
  16746. ' return this;',
  16747. ' };',
  16748. '});',
  16749. 'this.Obj = null;',
  16750. 'this.C = null;'
  16751. ]),
  16752. LinesToStr([ // $mod.$main
  16753. '$mod.Obj = $mod.C.$create("Create");',
  16754. 'var $with = $mod.C;',
  16755. '$mod.Obj = $with.$create("Create");',
  16756. '']));
  16757. end;
  16758. procedure TTestModule.TestClassOf_Call;
  16759. begin
  16760. StartProgram(false);
  16761. Add('type');
  16762. Add(' TObject = class');
  16763. Add(' class procedure DoIt;');
  16764. Add(' end;');
  16765. Add(' TClass = class of TObject;');
  16766. Add('class procedure tobject.doit; begin end;');
  16767. Add('var');
  16768. Add(' C: tclass;');
  16769. Add('begin');
  16770. Add(' c.doit;');
  16771. Add(' with c do doit;');
  16772. ConvertProgram;
  16773. CheckSource('TestClassOf_Call',
  16774. LinesToStr([ // statements
  16775. 'rtl.createClass(this, "TObject", null, function () {',
  16776. ' this.$init = function () {',
  16777. ' };',
  16778. ' this.$final = function () {',
  16779. ' };',
  16780. ' this.DoIt = function () {',
  16781. ' };',
  16782. '});',
  16783. 'this.C = null;'
  16784. ]),
  16785. LinesToStr([ // $mod.$main
  16786. '$mod.C.DoIt();',
  16787. 'var $with = $mod.C;',
  16788. '$with.DoIt();',
  16789. '']));
  16790. end;
  16791. procedure TTestModule.TestClassOf_Assign;
  16792. begin
  16793. StartProgram(false);
  16794. Add('type');
  16795. Add(' TClass = class of TObject;');
  16796. Add(' TObject = class');
  16797. Add(' ClassType: TClass; ');
  16798. Add(' end;');
  16799. Add('var');
  16800. Add(' Obj: tobject;');
  16801. Add(' C: tclass;');
  16802. Add('begin');
  16803. Add(' c:=nil;');
  16804. Add(' c:=obj.classtype;');
  16805. ConvertProgram;
  16806. CheckSource('TestClassOf_Assign',
  16807. LinesToStr([ // statements
  16808. 'rtl.createClass(this, "TObject", null, function () {',
  16809. ' this.$init = function () {',
  16810. ' this.ClassType = null;',
  16811. ' };',
  16812. ' this.$final = function () {',
  16813. ' this.ClassType = undefined;',
  16814. ' };',
  16815. '});',
  16816. 'this.Obj = null;',
  16817. 'this.C = null;'
  16818. ]),
  16819. LinesToStr([ // $mod.$main
  16820. '$mod.C = null;',
  16821. '$mod.C = $mod.Obj.ClassType;',
  16822. '']));
  16823. end;
  16824. procedure TTestModule.TestClassOf_Is;
  16825. begin
  16826. StartProgram(false);
  16827. Add('type');
  16828. Add(' TClass = class of TObject;');
  16829. Add(' TObject = class');
  16830. Add(' end;');
  16831. Add(' TCar = class');
  16832. Add(' end;');
  16833. Add(' TCars = class of TCar;');
  16834. Add('var');
  16835. Add(' Obj: tobject;');
  16836. Add(' C: tclass;');
  16837. Add(' Cars: tcars;');
  16838. Add('begin');
  16839. Add(' if c is tcar then ;');
  16840. Add(' if c is tcars then ;');
  16841. ConvertProgram;
  16842. CheckSource('TestClassOf_Is',
  16843. LinesToStr([ // statements
  16844. 'rtl.createClass(this, "TObject", null, function () {',
  16845. ' this.$init = function () {',
  16846. ' };',
  16847. ' this.$final = function () {',
  16848. ' };',
  16849. '});',
  16850. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  16851. '});',
  16852. 'this.Obj = null;',
  16853. 'this.C = null;',
  16854. 'this.Cars = null;'
  16855. ]),
  16856. LinesToStr([ // $mod.$main
  16857. 'if(rtl.is($mod.C,$mod.TCar));',
  16858. 'if(rtl.is($mod.C,$mod.TCar));',
  16859. '']));
  16860. end;
  16861. procedure TTestModule.TestClassOf_Compare;
  16862. begin
  16863. StartProgram(false);
  16864. Add('type');
  16865. Add(' TClass = class of TObject;');
  16866. Add(' TObject = class');
  16867. Add(' ClassType: TClass; ');
  16868. Add(' end;');
  16869. Add('var');
  16870. Add(' b: boolean;');
  16871. Add(' Obj: tobject;');
  16872. Add(' C: tclass;');
  16873. Add('begin');
  16874. Add(' b:=c=nil;');
  16875. Add(' b:=nil=c;');
  16876. Add(' b:=c=obj.classtype;');
  16877. Add(' b:=obj.classtype=c;');
  16878. Add(' b:=c=TObject;');
  16879. Add(' b:=TObject=c;');
  16880. Add(' b:=c<>nil;');
  16881. Add(' b:=nil<>c;');
  16882. Add(' b:=c<>obj.classtype;');
  16883. Add(' b:=obj.classtype<>c;');
  16884. Add(' b:=c<>TObject;');
  16885. Add(' b:=TObject<>c;');
  16886. ConvertProgram;
  16887. CheckSource('TestClassOf_Compare',
  16888. LinesToStr([ // statements
  16889. 'rtl.createClass(this, "TObject", null, function () {',
  16890. ' this.$init = function () {',
  16891. ' this.ClassType = null;',
  16892. ' };',
  16893. ' this.$final = function () {',
  16894. ' this.ClassType = undefined;',
  16895. ' };',
  16896. '});',
  16897. 'this.b = false;',
  16898. 'this.Obj = null;',
  16899. 'this.C = null;'
  16900. ]),
  16901. LinesToStr([ // $mod.$main
  16902. '$mod.b = $mod.C === null;',
  16903. '$mod.b = null === $mod.C;',
  16904. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  16905. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  16906. '$mod.b = $mod.C === $mod.TObject;',
  16907. '$mod.b = $mod.TObject === $mod.C;',
  16908. '$mod.b = $mod.C !== null;',
  16909. '$mod.b = null !== $mod.C;',
  16910. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  16911. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  16912. '$mod.b = $mod.C !== $mod.TObject;',
  16913. '$mod.b = $mod.TObject !== $mod.C;',
  16914. '']));
  16915. end;
  16916. procedure TTestModule.TestClassOf_ClassVar;
  16917. begin
  16918. StartProgram(false);
  16919. Add('type');
  16920. Add(' TObject = class');
  16921. Add(' class var id: longint;');
  16922. Add(' end;');
  16923. Add(' TClass = class of TObject;');
  16924. Add('var');
  16925. Add(' C: tclass;');
  16926. Add('begin');
  16927. Add(' C.id:=C.id;');
  16928. ConvertProgram;
  16929. CheckSource('TestClassOf_ClassVar',
  16930. LinesToStr([ // statements
  16931. 'rtl.createClass(this, "TObject", null, function () {',
  16932. ' this.id = 0;',
  16933. ' this.$init = function () {',
  16934. ' };',
  16935. ' this.$final = function () {',
  16936. ' };',
  16937. '});',
  16938. 'this.C = null;'
  16939. ]),
  16940. LinesToStr([ // $mod.$main
  16941. '$mod.TObject.id = $mod.C.id;',
  16942. '']));
  16943. end;
  16944. procedure TTestModule.TestClassOf_ClassMethod;
  16945. begin
  16946. StartProgram(false);
  16947. Add('type');
  16948. Add(' TObject = class');
  16949. Add(' class function DoIt(i: longint = 0): longint;');
  16950. Add(' end;');
  16951. Add(' TClass = class of TObject;');
  16952. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  16953. Add('var');
  16954. Add(' i: longint;');
  16955. Add(' C: tclass;');
  16956. Add('begin');
  16957. Add(' C.DoIt;');
  16958. Add(' C.DoIt();');
  16959. Add(' i:=C.DoIt;');
  16960. Add(' i:=C.DoIt();');
  16961. ConvertProgram;
  16962. CheckSource('TestClassOf_ClassMethod',
  16963. LinesToStr([ // statements
  16964. 'rtl.createClass(this, "TObject", null, function () {',
  16965. ' this.$init = function () {',
  16966. ' };',
  16967. ' this.$final = function () {',
  16968. ' };',
  16969. ' this.DoIt = function (i) {',
  16970. ' var Result = 0;',
  16971. ' return Result;',
  16972. ' };',
  16973. '});',
  16974. 'this.i = 0;',
  16975. 'this.C = null;'
  16976. ]),
  16977. LinesToStr([ // $mod.$main
  16978. '$mod.C.DoIt(0);',
  16979. '$mod.C.DoIt(0);',
  16980. '$mod.i = $mod.C.DoIt(0);',
  16981. '$mod.i = $mod.C.DoIt(0);',
  16982. '']));
  16983. end;
  16984. procedure TTestModule.TestClassOf_ClassProperty;
  16985. begin
  16986. StartProgram(false);
  16987. Add([
  16988. 'type',
  16989. ' TObject = class',
  16990. ' class var FA: longint;',
  16991. ' class function GetA: longint;',
  16992. ' class procedure SetA(Value: longint);',
  16993. ' class property pA: longint read fa write fa;',
  16994. ' class property pB: longint read geta write seta;',
  16995. ' end;',
  16996. ' TObjectClass = class of tobject;',
  16997. 'class function tobject.geta: longint; begin end;',
  16998. 'class procedure tobject.seta(value: longint); begin end;',
  16999. 'var',
  17000. ' b: boolean;',
  17001. ' Obj: tobject;',
  17002. ' Cla: tobjectclass;',
  17003. 'begin',
  17004. ' obj.pa:=obj.pa;',
  17005. ' obj.pb:=obj.pb;',
  17006. ' b:=obj.pa=4;',
  17007. ' b:=obj.pb=obj.pb;',
  17008. ' b:=5=obj.pa;',
  17009. ' cla.pa:=6;',
  17010. ' cla.pa:=cla.pa;',
  17011. ' cla.pb:=cla.pb;',
  17012. ' b:=cla.pa=7;',
  17013. ' b:=cla.pb=cla.pb;',
  17014. ' b:=8=cla.pa;',
  17015. ' tobject.pa:=9;',
  17016. ' tobject.pb:=tobject.pb;',
  17017. ' b:=tobject.pa=10;',
  17018. ' b:=11=tobject.pa;',
  17019. '']);
  17020. ConvertProgram;
  17021. CheckSource('TestClassOf_ClassProperty',
  17022. LinesToStr([ // statements
  17023. 'rtl.createClass(this, "TObject", null, function () {',
  17024. ' this.FA = 0;',
  17025. ' this.$init = function () {',
  17026. ' };',
  17027. ' this.$final = function () {',
  17028. ' };',
  17029. ' this.GetA = function () {',
  17030. ' var Result = 0;',
  17031. ' return Result;',
  17032. ' };',
  17033. ' this.SetA = function (Value) {',
  17034. ' };',
  17035. '});',
  17036. 'this.b = false;',
  17037. 'this.Obj = null;',
  17038. 'this.Cla = null;'
  17039. ]),
  17040. LinesToStr([ // $mod.$main
  17041. '$mod.TObject.FA = $mod.Obj.FA;',
  17042. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17043. '$mod.b = $mod.Obj.FA === 4;',
  17044. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17045. '$mod.b = 5 === $mod.Obj.FA;',
  17046. '$mod.TObject.FA = 6;',
  17047. '$mod.TObject.FA = $mod.Cla.FA;',
  17048. '$mod.Cla.SetA($mod.Cla.GetA());',
  17049. '$mod.b = $mod.Cla.FA === 7;',
  17050. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17051. '$mod.b = 8 === $mod.Cla.FA;',
  17052. '$mod.TObject.FA = 9;',
  17053. '$mod.TObject.SetA($mod.TObject.GetA());',
  17054. '$mod.b = $mod.TObject.FA === 10;',
  17055. '$mod.b = 11 === $mod.TObject.FA;',
  17056. '']));
  17057. end;
  17058. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17059. begin
  17060. StartProgram(false);
  17061. Add('type');
  17062. Add(' TObject = class');
  17063. Add(' class var GlobalId: longint;');
  17064. Add(' class procedure ProcA;');
  17065. Add(' end;');
  17066. Add('class procedure tobject.proca;');
  17067. Add('var b: boolean;');
  17068. Add('begin');
  17069. Add(' b:=self=nil;');
  17070. Add(' b:=self.globalid=3;');
  17071. Add(' b:=4=self.globalid;');
  17072. Add(' self.globalid:=5;');
  17073. Add(' self.proca;');
  17074. Add('end;');
  17075. Add('begin');
  17076. ConvertProgram;
  17077. CheckSource('TestClassOf_ClassMethodSelf',
  17078. LinesToStr([ // statements
  17079. 'rtl.createClass(this, "TObject", null, function () {',
  17080. ' this.GlobalId = 0;',
  17081. ' this.$init = function () {',
  17082. ' };',
  17083. ' this.$final = function () {',
  17084. ' };',
  17085. ' this.ProcA = function () {',
  17086. ' var b = false;',
  17087. ' b = this === null;',
  17088. ' b = this.GlobalId === 3;',
  17089. ' b = 4 === this.GlobalId;',
  17090. ' $mod.TObject.GlobalId = 5;',
  17091. ' this.ProcA();',
  17092. ' };',
  17093. '});'
  17094. ]),
  17095. LinesToStr([ // $mod.$main
  17096. '']));
  17097. end;
  17098. procedure TTestModule.TestClassOf_TypeCast;
  17099. begin
  17100. StartProgram(false);
  17101. Add('type');
  17102. Add(' TObject = class');
  17103. Add(' class procedure {#TObject_DoIt}DoIt;');
  17104. Add(' end;');
  17105. Add(' TClass = class of TObject;');
  17106. Add(' TMobile = class');
  17107. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17108. Add(' end;');
  17109. Add(' TMobileClass = class of TMobile;');
  17110. Add(' TCar = class(TMobile)');
  17111. Add(' class procedure {#TCar_DoIt}DoIt;');
  17112. Add(' end;');
  17113. Add(' TCarClass = class of TCar;');
  17114. Add('class procedure TObject.DoIt;');
  17115. Add('begin');
  17116. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17117. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17118. Add('end;');
  17119. Add('class procedure TMobile.DoIt;');
  17120. Add('begin');
  17121. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17122. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17123. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17124. Add('end;');
  17125. Add('class procedure TCar.DoIt; begin end;');
  17126. Add('var');
  17127. Add(' ObjC: TClass;');
  17128. Add(' MobileC: TMobileClass;');
  17129. Add(' CarC: TCarClass;');
  17130. Add('begin');
  17131. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17132. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17133. Add(' CarC.{@TCar_DoIt}DoIt;');
  17134. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17135. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17136. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17137. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17138. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17139. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17140. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17141. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17142. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17143. ConvertProgram;
  17144. CheckSource('TestClassOf_TypeCast',
  17145. LinesToStr([ // statements
  17146. 'rtl.createClass(this, "TObject", null, function () {',
  17147. ' this.$init = function () {',
  17148. ' };',
  17149. ' this.$final = function () {',
  17150. ' };',
  17151. ' this.DoIt = function () {',
  17152. ' this.DoIt();',
  17153. ' this.DoIt$1();',
  17154. ' };',
  17155. '});',
  17156. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17157. ' this.DoIt$1 = function () {',
  17158. ' this.DoIt();',
  17159. ' this.DoIt$1();',
  17160. ' this.DoIt$2();',
  17161. ' };',
  17162. '});',
  17163. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17164. ' this.DoIt$2 = function () {',
  17165. ' };',
  17166. '});',
  17167. 'this.ObjC = null;',
  17168. 'this.MobileC = null;',
  17169. 'this.CarC = null;',
  17170. '']),
  17171. LinesToStr([ // $mod.$main
  17172. '$mod.ObjC.DoIt();',
  17173. '$mod.MobileC.DoIt$1();',
  17174. '$mod.CarC.DoIt$2();',
  17175. '$mod.ObjC.DoIt();',
  17176. '$mod.ObjC.DoIt$1();',
  17177. '$mod.ObjC.DoIt$2();',
  17178. '$mod.MobileC.DoIt();',
  17179. '$mod.MobileC.DoIt$1();',
  17180. '$mod.MobileC.DoIt$2();',
  17181. '$mod.CarC.DoIt();',
  17182. '$mod.CarC.DoIt$1();',
  17183. '$mod.CarC.DoIt$2();',
  17184. '']));
  17185. end;
  17186. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17187. begin
  17188. StartProgram(false);
  17189. Add('type');
  17190. Add(' TObject = class');
  17191. Add(' function CurNow: longint; ');
  17192. Add(' class function Now: longint; ');
  17193. Add(' end;');
  17194. Add('function TObject.CurNow: longint; begin end;');
  17195. Add('class function TObject.Now: longint; begin end;');
  17196. Add('var');
  17197. Add(' Obj: tobject;');
  17198. Add(' vI: longint;');
  17199. Add('begin');
  17200. Add(' obj.curnow;');
  17201. Add(' vi:=obj.curnow;');
  17202. Add(' tobject.now;');
  17203. Add(' vi:=tobject.now;');
  17204. ConvertProgram;
  17205. CheckSource('TestClassOf_ImplicitFunctionCall',
  17206. LinesToStr([ // statements
  17207. 'rtl.createClass(this, "TObject", null, function () {',
  17208. ' this.$init = function () {',
  17209. ' };',
  17210. ' this.$final = function () {',
  17211. ' };',
  17212. ' this.CurNow = function () {',
  17213. ' var Result = 0;',
  17214. ' return Result;',
  17215. ' };',
  17216. ' this.Now = function () {',
  17217. ' var Result = 0;',
  17218. ' return Result;',
  17219. ' };',
  17220. '});',
  17221. 'this.Obj = null;',
  17222. 'this.vI = 0;',
  17223. '']),
  17224. LinesToStr([ // $mod.$main
  17225. '$mod.Obj.CurNow();',
  17226. '$mod.vI = $mod.Obj.CurNow();',
  17227. '$mod.TObject.Now();',
  17228. '$mod.vI = $mod.TObject.Now();',
  17229. '']));
  17230. end;
  17231. procedure TTestModule.TestClassOf_Const;
  17232. begin
  17233. StartProgram(false);
  17234. Add([
  17235. 'type',
  17236. ' TObject = class',
  17237. ' end;',
  17238. ' TBird = TObject;',
  17239. ' TBirds = class of TBird;',
  17240. ' TEagles = TBirds;',
  17241. ' THawk = class(TBird);',
  17242. 'const',
  17243. ' Hawk: TEagles = THawk;',
  17244. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17245. ' TBird,',
  17246. ' THawk',
  17247. ' );',
  17248. 'begin']);
  17249. ConvertProgram;
  17250. CheckSource('TestClassOf_Const',
  17251. LinesToStr([ // statements
  17252. 'rtl.createClass(this, "TObject", null, function () {',
  17253. ' this.$init = function () {',
  17254. ' };',
  17255. ' this.$final = function () {',
  17256. ' };',
  17257. '});',
  17258. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17259. '});',
  17260. 'this.Hawk = this.THawk;',
  17261. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17262. '']),
  17263. LinesToStr([ // $mod.$main
  17264. '']));
  17265. end;
  17266. procedure TTestModule.TestNestedClass_Alias;
  17267. begin
  17268. WithTypeInfo:=true;
  17269. StartProgram(false);
  17270. Add([
  17271. 'type',
  17272. ' TObject = class',
  17273. ' type TNested = type longint;',
  17274. ' end;',
  17275. 'type TAlias = type tobject.tnested;',
  17276. 'var i: tobject.tnested = 3;',
  17277. 'var j: TAlias = 4;',
  17278. 'begin',
  17279. ' if typeinfo(TAlias)=nil then ;',
  17280. ' if typeinfo(tobject.tnested)=nil then ;',
  17281. '']);
  17282. ConvertProgram;
  17283. CheckSource('TestNestedClass_Alias',
  17284. LinesToStr([ // statements
  17285. 'rtl.createClass(this, "TObject", null, function () {',
  17286. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17287. ' this.$init = function () {',
  17288. ' };',
  17289. ' this.$final = function () {',
  17290. ' };',
  17291. '});',
  17292. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17293. 'this.i = 3;',
  17294. 'this.j = 4;',
  17295. '']),
  17296. LinesToStr([ // $mod.$main
  17297. 'if ($mod.$rtti["TAlias"] === null) ;',
  17298. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17299. '']));
  17300. end;
  17301. procedure TTestModule.TestNestedClass_Record;
  17302. begin
  17303. WithTypeInfo:=true;
  17304. StartProgram(false);
  17305. Add([
  17306. 'type',
  17307. ' TObject = class',
  17308. ' type TPoint = record',
  17309. ' x,y: byte;',
  17310. ' end;',
  17311. ' procedure DoIt(t: TPoint);',
  17312. ' end;',
  17313. 'procedure tobject.DoIt(t: TPoint);',
  17314. 'var p: TPoint;',
  17315. 'begin',
  17316. ' t.x:=t.y;',
  17317. ' p:=t;',
  17318. 'end;',
  17319. 'var',
  17320. ' p: tobject.tpoint = (x:2; y:4);',
  17321. ' o: TObject;',
  17322. 'begin',
  17323. ' p:=p;',
  17324. ' o.doit(p);',
  17325. '']);
  17326. ConvertProgram;
  17327. CheckSource('TestNestedClass_Record',
  17328. LinesToStr([ // statements
  17329. 'rtl.createClass(this, "TObject", null, function () {',
  17330. ' rtl.recNewT(this, "TPoint", function () {',
  17331. ' this.x = 0;',
  17332. ' this.y = 0;',
  17333. ' this.$eq = function (b) {',
  17334. ' return (this.x === b.x) && (this.y === b.y);',
  17335. ' };',
  17336. ' this.$assign = function (s) {',
  17337. ' this.x = s.x;',
  17338. ' this.y = s.y;',
  17339. ' return this;',
  17340. ' };',
  17341. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17342. ' $r.addField("x", rtl.byte);',
  17343. ' $r.addField("y", rtl.byte);',
  17344. ' });',
  17345. ' this.$init = function () {',
  17346. ' };',
  17347. ' this.$final = function () {',
  17348. ' };',
  17349. ' this.DoIt = function (t) {',
  17350. ' var p = this.TPoint.$new();',
  17351. ' t.x = t.y;',
  17352. ' p.$assign(t);',
  17353. ' };',
  17354. '});',
  17355. 'this.p = this.TObject.TPoint.$clone({',
  17356. ' x: 2,',
  17357. ' y: 4',
  17358. '});',
  17359. 'this.o = null;',
  17360. '']),
  17361. LinesToStr([ // $mod.$main
  17362. '$mod.p.$assign($mod.p);',
  17363. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17364. '']));
  17365. end;
  17366. procedure TTestModule.TestNestedClass_Class;
  17367. begin
  17368. WithTypeInfo:=true;
  17369. StartProgram(false);
  17370. Add([
  17371. 'type',
  17372. ' TObject = class end;',
  17373. ' TBird = class',
  17374. ' type TLeg = class',
  17375. ' FId: longint;',
  17376. ' constructor Create;',
  17377. ' function Create(i: longint): TLeg;',
  17378. ' end;',
  17379. ' function DoIt(b: TBird): Tleg;',
  17380. ' end;',
  17381. 'constructor tbird.tleg.create;',
  17382. 'begin',
  17383. ' FId:=3;',
  17384. 'end;',
  17385. 'function tbird.tleg.Create(i: longint): TLeg;',
  17386. 'begin',
  17387. ' Create;',
  17388. ' Result:=TLeg.Create;',
  17389. ' Result:=TBird.TLeg.Create;',
  17390. ' Result:=Create(3);',
  17391. ' FId:=i;',
  17392. 'end;',
  17393. 'function tbird.DoIt(b: tbird): tleg;',
  17394. 'begin',
  17395. ' Result.Create;',
  17396. ' Result:=TLeg.Create;',
  17397. ' Result:=TBird.TLeg.Create;',
  17398. ' Result:=Result.Create(3);',
  17399. 'end;',
  17400. 'var',
  17401. ' b: Tbird.tleg;',
  17402. 'begin',
  17403. ' b.Create;',
  17404. ' b:=TBird.TLeg.Create;',
  17405. ' b:=b.Create(3);',
  17406. '']);
  17407. ConvertProgram;
  17408. CheckSource('TestNestedClass_Class',
  17409. LinesToStr([ // statements
  17410. 'rtl.createClass(this, "TObject", null, function () {',
  17411. ' this.$init = function () {',
  17412. ' };',
  17413. ' this.$final = function () {',
  17414. ' };',
  17415. '});',
  17416. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17417. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  17418. ' this.$init = function () {',
  17419. ' $mod.TObject.$init.call(this);',
  17420. ' this.FId = 0;',
  17421. ' };',
  17422. ' this.Create = function () {',
  17423. ' this.FId = 3;',
  17424. ' return this;',
  17425. ' };',
  17426. ' this.Create$1 = function (i) {',
  17427. ' var Result = null;',
  17428. ' this.Create();',
  17429. ' Result = $mod.TBird.TLeg.$create("Create");',
  17430. ' Result = $mod.TBird.TLeg.$create("Create");',
  17431. ' Result = this.Create$1(3);',
  17432. ' this.FId = i;',
  17433. ' return Result;',
  17434. ' };',
  17435. ' }, "TBird.TLeg");',
  17436. ' this.DoIt = function (b) {',
  17437. ' var Result = null;',
  17438. ' Result.Create();',
  17439. ' Result = this.TLeg.$create("Create");',
  17440. ' Result = $mod.TBird.TLeg.$create("Create");',
  17441. ' Result = Result.Create$1(3);',
  17442. ' return Result;',
  17443. ' };',
  17444. '});',
  17445. 'this.b = null;',
  17446. '']),
  17447. LinesToStr([ // $mod.$main
  17448. '$mod.b.Create();',
  17449. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  17450. '$mod.b = $mod.b.Create$1(3);',
  17451. '']));
  17452. end;
  17453. procedure TTestModule.TestExternalClass_Var;
  17454. begin
  17455. StartProgram(false);
  17456. Add([
  17457. '{$modeswitch externalclass}',
  17458. 'type',
  17459. ' TExtA = class external name ''ExtObj''',
  17460. ' Id: longint external name ''$Id'';',
  17461. ' B: longint;',
  17462. ' end;',
  17463. 'var Obj: TExtA;',
  17464. 'begin',
  17465. ' obj.id:=obj.id+1;',
  17466. ' obj.B:=obj.B+1;']);
  17467. ConvertProgram;
  17468. CheckSource('TestExternalClass_Var',
  17469. LinesToStr([ // statements
  17470. 'this.Obj = null;',
  17471. '']),
  17472. LinesToStr([ // $mod.$main
  17473. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  17474. '$mod.Obj.B = $mod.Obj.B + 1;',
  17475. '']));
  17476. end;
  17477. procedure TTestModule.TestExternalClass_Const;
  17478. begin
  17479. StartProgram(false);
  17480. Add([
  17481. '{$modeswitch externalclass}',
  17482. 'type',
  17483. ' TExtA = class external name ''ExtObj''',
  17484. ' const Two: longint = 2;',
  17485. ' const Three = 3;',
  17486. ' const Id: longint;',
  17487. ' end;',
  17488. ' TExtB = class external name ''ExtB''',
  17489. ' A: TExtA;',
  17490. ' end;',
  17491. 'var',
  17492. ' A: texta;',
  17493. ' B: textb;',
  17494. ' i: longint;',
  17495. 'begin',
  17496. ' i:=a.two;',
  17497. ' i:=texta.two;',
  17498. ' i:=a.three;',
  17499. ' i:=texta.three;',
  17500. ' i:=a.id;',
  17501. ' i:=texta.id;',
  17502. '']);
  17503. ConvertProgram;
  17504. CheckSource('TestExternalClass_Const',
  17505. LinesToStr([ // statements
  17506. 'this.A = null;',
  17507. 'this.B = null;',
  17508. 'this.i = 0;',
  17509. '']),
  17510. LinesToStr([ // $mod.$main
  17511. '$mod.i = 2;',
  17512. '$mod.i = 2;',
  17513. '$mod.i = 3;',
  17514. '$mod.i = 3;',
  17515. '$mod.i = $mod.A.Id;',
  17516. '$mod.i = ExtObj.Id;',
  17517. '']));
  17518. end;
  17519. procedure TTestModule.TestExternalClass_Dollar;
  17520. begin
  17521. StartProgram(false);
  17522. Add([
  17523. '{$modeswitch externalclass}',
  17524. 'type',
  17525. ' TExtA = class external name ''$''',
  17526. ' Id: longint external name ''$'';',
  17527. ' function Bla(i: longint): longint; external name ''$'';',
  17528. ' end;',
  17529. 'function dollar(k: longint): longint; external name ''$'';',
  17530. 'var Obj: TExtA;',
  17531. 'begin',
  17532. ' dollar(1);',
  17533. ' obj.id:=obj.id+2;',
  17534. ' obj.Bla(3);',
  17535. '']);
  17536. ConvertProgram;
  17537. CheckSource('TestExternalClass_Dollar',
  17538. LinesToStr([ // statements
  17539. 'this.Obj = null;',
  17540. '']),
  17541. LinesToStr([ // $mod.$main
  17542. '$(1);',
  17543. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  17544. '$mod.Obj.$(3);',
  17545. '']));
  17546. end;
  17547. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  17548. begin
  17549. StartProgram(false);
  17550. Add('{$modeswitch externalclass}');
  17551. Add('type');
  17552. Add(' TExtA = class external name ''ExtA''');
  17553. Add(' Id: longint external name ''$Id'';');
  17554. Add(' end;');
  17555. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  17556. Add(' Id: longint;');
  17557. Add(' end;');
  17558. Add('begin');
  17559. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  17560. ConvertProgram;
  17561. end;
  17562. procedure TTestModule.TestExternalClass_Method;
  17563. begin
  17564. StartProgram(false);
  17565. Add(['{$modeswitch externalclass}',
  17566. 'type',
  17567. ' TExtA = class external name ''ExtObj''',
  17568. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17569. ' procedure DoSome(Id: longint = 1);',
  17570. ' end;',
  17571. 'var Obj: texta;',
  17572. 'begin',
  17573. ' obj.doit;',
  17574. ' obj.doit();',
  17575. ' obj.doit(2);',
  17576. ' with obj do begin',
  17577. ' doit;',
  17578. ' doit();',
  17579. ' doit(3);',
  17580. ' end;']);
  17581. ConvertProgram;
  17582. CheckSource('TestExternalClass_Method',
  17583. LinesToStr([ // statements
  17584. 'this.Obj = null;',
  17585. '']),
  17586. LinesToStr([ // $mod.$main
  17587. '$mod.Obj.$Execute(1);',
  17588. '$mod.Obj.$Execute(1);',
  17589. '$mod.Obj.$Execute(2);',
  17590. 'var $with = $mod.Obj;',
  17591. '$with.$Execute(1);',
  17592. '$with.$Execute(1);',
  17593. '$with.$Execute(3);',
  17594. '']));
  17595. end;
  17596. procedure TTestModule.TestExternalClass_ClassMethod;
  17597. begin
  17598. StartProgram(false);
  17599. Add([
  17600. '{$modeswitch externalclass}',
  17601. 'type',
  17602. ' TExtA = class external name ''ExtObj''',
  17603. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17604. ' end;',
  17605. ' TExtB = TExtA;',
  17606. 'var p: Pointer;',
  17607. 'begin',
  17608. ' texta.doit;',
  17609. ' texta.doit();',
  17610. ' texta.doit(2);',
  17611. ' p:[email protected];',
  17612. ' with texta do begin',
  17613. ' doit;',
  17614. ' doit();',
  17615. ' doit(3);',
  17616. ' p:=@DoIt;',
  17617. ' end;',
  17618. ' textb.doit;',
  17619. ' textb.doit();',
  17620. ' textb.doit(4);',
  17621. ' with textb do begin',
  17622. ' doit;',
  17623. ' doit();',
  17624. ' doit(5);',
  17625. ' end;',
  17626. '']);
  17627. ConvertProgram;
  17628. CheckSource('TestExternalClass_ClassMethod',
  17629. LinesToStr([ // statements
  17630. 'this.p = null;',
  17631. '']),
  17632. LinesToStr([ // $mod.$main
  17633. 'ExtObj.$Execute(1);',
  17634. 'ExtObj.$Execute(1);',
  17635. 'ExtObj.$Execute(2);',
  17636. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17637. 'ExtObj.$Execute(1);',
  17638. 'ExtObj.$Execute(1);',
  17639. 'ExtObj.$Execute(3);',
  17640. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17641. 'ExtObj.$Execute(1);',
  17642. 'ExtObj.$Execute(1);',
  17643. 'ExtObj.$Execute(4);',
  17644. 'ExtObj.$Execute(1);',
  17645. 'ExtObj.$Execute(1);',
  17646. 'ExtObj.$Execute(5);',
  17647. '']));
  17648. end;
  17649. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  17650. begin
  17651. StartProgram(false);
  17652. Add([
  17653. '{$modeswitch externalclass}',
  17654. 'type',
  17655. ' TExtA = class external name ''ExtObj''',
  17656. ' class procedure DoIt(Id: longint = 1); static;',
  17657. ' end;',
  17658. 'var p: Pointer;',
  17659. 'begin',
  17660. ' texta.doit;',
  17661. ' texta.doit();',
  17662. ' texta.doit(2);',
  17663. ' p:[email protected];',
  17664. ' with texta do begin',
  17665. ' doit;',
  17666. ' doit();',
  17667. ' doit(3);',
  17668. ' p:=@DoIt;',
  17669. ' end;',
  17670. '']);
  17671. ConvertProgram;
  17672. CheckSource('TestExternalClass_ClassMethodStatic',
  17673. LinesToStr([ // statements
  17674. 'this.p = null;',
  17675. '']),
  17676. LinesToStr([ // $mod.$main
  17677. 'ExtObj.DoIt(1);',
  17678. 'ExtObj.DoIt(1);',
  17679. 'ExtObj.DoIt(2);',
  17680. '$mod.p = ExtObj.DoIt;',
  17681. 'ExtObj.DoIt(1);',
  17682. 'ExtObj.DoIt(1);',
  17683. 'ExtObj.DoIt(3);',
  17684. '$mod.p = ExtObj.DoIt;',
  17685. '']));
  17686. end;
  17687. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  17688. begin
  17689. StartProgram(false);
  17690. Add([
  17691. '{$modeswitch externalclass}',
  17692. 'type',
  17693. ' TBird = class external name ''Array''',
  17694. ' end;',
  17695. 'function GetPtr: Pointer;',
  17696. 'begin',
  17697. 'end;',
  17698. 'procedure Write(const p);',
  17699. 'begin',
  17700. 'end;',
  17701. 'procedure WriteLn; varargs;',
  17702. 'begin',
  17703. 'end;',
  17704. 'begin',
  17705. ' if TBird(GetPtr)=nil then ;',
  17706. ' Write(GetPtr);',
  17707. ' WriteLn(GetPtr);',
  17708. ' Write(TBird(GetPtr));',
  17709. ' WriteLn(TBird(GetPtr));',
  17710. '']);
  17711. ConvertProgram;
  17712. CheckSource('TestFunctionResultInTypeCast',
  17713. LinesToStr([ // statements
  17714. 'this.GetPtr = function () {',
  17715. ' var Result = null;',
  17716. ' return Result;',
  17717. '};',
  17718. 'this.Write = function (p) {',
  17719. '};',
  17720. 'this.WriteLn = function () {',
  17721. '};',
  17722. '']),
  17723. LinesToStr([
  17724. 'if ($mod.GetPtr() === null) ;',
  17725. '$mod.Write($mod.GetPtr());',
  17726. '$mod.WriteLn($mod.GetPtr());',
  17727. '$mod.Write($mod.GetPtr());',
  17728. '$mod.WriteLn($mod.GetPtr());',
  17729. '']));
  17730. end;
  17731. procedure TTestModule.TestExternalClass_NonExternalOverride;
  17732. begin
  17733. StartProgram(false);
  17734. Add([
  17735. '{$modeswitch externalclass}',
  17736. 'type',
  17737. ' TExtA = class external name ''ExtObjA''',
  17738. ' procedure ProcA; virtual;',
  17739. ' procedure ProcB; virtual;',
  17740. ' end;',
  17741. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  17742. ' end;',
  17743. ' TExtC = class (TExtB)',
  17744. ' procedure ProcA; override;',
  17745. ' end;',
  17746. 'procedure TExtC.ProcA;',
  17747. 'begin',
  17748. ' ProcA;',
  17749. ' Self.ProcA;',
  17750. ' ProcB;',
  17751. ' Self.ProcB;',
  17752. 'end;',
  17753. 'var',
  17754. ' A: texta;',
  17755. ' B: textb;',
  17756. ' C: textc;',
  17757. 'begin',
  17758. ' a.proca;',
  17759. ' b.proca;',
  17760. ' c.proca;']);
  17761. ConvertProgram;
  17762. CheckSource('TestExternalClass_NonExternalOverride',
  17763. LinesToStr([ // statements
  17764. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  17765. ' this.$init = function () {',
  17766. ' };',
  17767. ' this.$final = function () {',
  17768. ' };',
  17769. ' this.ProcA = function () {',
  17770. ' this.ProcA();',
  17771. ' this.ProcA();',
  17772. ' this.ProcB();',
  17773. ' this.ProcB();',
  17774. ' };',
  17775. '});',
  17776. 'this.A = null;',
  17777. 'this.B = null;',
  17778. 'this.C = null;',
  17779. '']),
  17780. LinesToStr([ // $mod.$main
  17781. '$mod.A.ProcA();',
  17782. '$mod.B.ProcA();',
  17783. '$mod.C.ProcA();',
  17784. '']));
  17785. end;
  17786. procedure TTestModule.TestExternalClass_OverloadHint;
  17787. begin
  17788. StartProgram(false);
  17789. Add([
  17790. '{$modeswitch externalclass}',
  17791. 'type',
  17792. ' TExtA = class external name ''ExtObjA''',
  17793. ' procedure DoIt;',
  17794. ' procedure DoIt(i: longint);',
  17795. ' end;',
  17796. 'begin',
  17797. '']);
  17798. ConvertProgram;
  17799. CheckResolverUnexpectedHints(true);
  17800. CheckSource('TestExternalClass_OverloadHint',
  17801. LinesToStr([ // statements
  17802. '']),
  17803. LinesToStr([ // $mod.$main
  17804. '']));
  17805. end;
  17806. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  17807. begin
  17808. WithTypeInfo:=true;
  17809. StartProgram(false);
  17810. Add([
  17811. '{$modeswitch externalclass}',
  17812. 'type',
  17813. ' JSwiper = class external name ''Swiper''',
  17814. ' constructor New;',
  17815. ' end;',
  17816. ' TObject = class',
  17817. ' private',
  17818. ' FSwiper: JSwiper;',
  17819. ' published',
  17820. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  17821. ' end;',
  17822. 'begin',
  17823. ' JSwiper.new;',
  17824. '']);
  17825. ConvertProgram;
  17826. CheckSource('TestExternalClass_SameNamePublishedProperty',
  17827. LinesToStr([ // statements
  17828. 'this.$rtti.$ExtClass("JSwiper", {',
  17829. ' jsclass: "Swiper"',
  17830. '});',
  17831. 'rtl.createClass(this, "TObject", null, function () {',
  17832. ' this.$init = function () {',
  17833. ' this.FSwiper = null;',
  17834. ' };',
  17835. ' this.$final = function () {',
  17836. ' this.FSwiper = undefined;',
  17837. ' };',
  17838. ' var $r = this.$rtti;',
  17839. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  17840. '});',
  17841. '']),
  17842. LinesToStr([ // $mod.$main
  17843. 'new Swiper();',
  17844. '']));
  17845. end;
  17846. procedure TTestModule.TestExternalClass_Property;
  17847. begin
  17848. StartProgram(false);
  17849. Add([
  17850. '{$modeswitch externalclass}',
  17851. 'type',
  17852. ' TExtA = class external name ''ExtA''',
  17853. ' function getYear: longint;',
  17854. ' procedure setYear(Value: longint);',
  17855. ' property Year: longint read getyear write setyear;',
  17856. ' end;',
  17857. ' TExtB = class (TExtA)',
  17858. ' procedure OtherSetYear(Value: longint);',
  17859. ' property year write othersetyear;',
  17860. ' end;',
  17861. 'procedure textb.othersetyear(value: longint);',
  17862. 'begin',
  17863. ' setYear(Value+4);',
  17864. 'end;',
  17865. 'var',
  17866. ' A: texta;',
  17867. ' B: textb;',
  17868. 'begin',
  17869. ' a.year:=a.year+1;',
  17870. ' b.year:=b.year+2;']);
  17871. ConvertProgram;
  17872. CheckSource('TestExternalClass_NonExternalOverride',
  17873. LinesToStr([ // statements
  17874. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17875. ' this.$init = function () {',
  17876. ' };',
  17877. ' this.$final = function () {',
  17878. ' };',
  17879. ' this.OtherSetYear = function (Value) {',
  17880. ' this.setYear(Value+4);',
  17881. ' };',
  17882. '});',
  17883. 'this.A = null;',
  17884. 'this.B = null;',
  17885. '']),
  17886. LinesToStr([ // $mod.$main
  17887. '$mod.A.setYear($mod.A.getYear()+1);',
  17888. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  17889. '']));
  17890. end;
  17891. procedure TTestModule.TestExternalClass_PropertyDate;
  17892. begin
  17893. StartProgram(false);
  17894. Add([
  17895. '{$modeswitch externalclass}',
  17896. 'type',
  17897. ' TExtA = class external name ''ExtA''',
  17898. ' end;',
  17899. ' TExtB = class (TExtA)',
  17900. ' FDate: string;',
  17901. ' property Date: string read FDate write FDate;',
  17902. ' property ExtA: string read FDate write FDate;',
  17903. ' end;',
  17904. ' {$M+}',
  17905. ' TObject = class',
  17906. ' FDate: string;',
  17907. ' published',
  17908. ' property Date: string read FDate write FDate;',
  17909. ' property ExtA: string read FDate write FDate;',
  17910. ' end;',
  17911. 'var',
  17912. ' B: textb;',
  17913. ' o: TObject;',
  17914. 'begin',
  17915. ' b.date:=b.exta;',
  17916. ' o.date:=o.exta;']);
  17917. ConvertProgram;
  17918. CheckSource('TestExternalClass_PropertyDate',
  17919. LinesToStr([ // statements
  17920. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17921. ' this.$init = function () {',
  17922. ' this.FDate = "";',
  17923. ' };',
  17924. ' this.$final = function () {',
  17925. ' };',
  17926. '});',
  17927. 'rtl.createClass(this, "TObject", null, function () {',
  17928. ' this.$init = function () {',
  17929. ' this.FDate = "";',
  17930. ' };',
  17931. ' this.$final = function () {',
  17932. ' };',
  17933. ' var $r = this.$rtti;',
  17934. ' $r.addField("FDate", rtl.string);',
  17935. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  17936. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  17937. '});',
  17938. 'this.B = null;',
  17939. 'this.o = null;',
  17940. '']),
  17941. LinesToStr([ // $mod.$main
  17942. '$mod.B.FDate = $mod.B.FDate;',
  17943. '$mod.o.FDate = $mod.o.FDate;',
  17944. '']));
  17945. end;
  17946. procedure TTestModule.TestExternalClass_ClassProperty;
  17947. begin
  17948. StartProgram(false);
  17949. Add('{$modeswitch externalclass}');
  17950. Add('type');
  17951. Add(' TExtA = class external name ''ExtA''');
  17952. Add(' class function getYear: longint;');
  17953. Add(' class procedure setYear(Value: longint);');
  17954. Add(' class property Year: longint read getyear write setyear;');
  17955. Add(' end;');
  17956. Add(' TExtB = class (TExtA)');
  17957. Add(' class function GetCentury: longint;');
  17958. Add(' class procedure SetCentury(Value: longint);');
  17959. Add(' class property Century: longint read getcentury write setcentury;');
  17960. Add(' end;');
  17961. Add('class function textb.getcentury: longint;');
  17962. Add('begin');
  17963. Add('end;');
  17964. Add('class procedure textb.setcentury(value: longint);');
  17965. Add('begin');
  17966. Add(' setyear(value+11);');
  17967. Add(' texta.year:=texta.year+12;');
  17968. Add(' year:=year+13;');
  17969. Add(' textb.century:=textb.century+14;');
  17970. Add(' century:=century+15;');
  17971. Add('end;');
  17972. Add('var');
  17973. Add(' A: texta;');
  17974. Add(' B: textb;');
  17975. Add('begin');
  17976. Add(' texta.year:=texta.year+1;');
  17977. Add(' textb.year:=textb.year+2;');
  17978. Add(' TextA.year:=TextA.year+3;');
  17979. Add(' b.year:=b.year+4;');
  17980. Add(' textb.century:=textb.century+5;');
  17981. Add(' b.century:=b.century+6;');
  17982. ConvertProgram;
  17983. CheckSource('TestExternalClass_ClassProperty',
  17984. LinesToStr([ // statements
  17985. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17986. ' this.$init = function () {',
  17987. ' };',
  17988. ' this.$final = function () {',
  17989. ' };',
  17990. ' this.GetCentury = function () {',
  17991. ' var Result = 0;',
  17992. ' return Result;',
  17993. ' };',
  17994. ' this.SetCentury = function (Value) {',
  17995. ' this.setYear(Value + 11);',
  17996. ' ExtA.setYear(ExtA.getYear() + 12);',
  17997. ' this.setYear(this.getYear() + 13);',
  17998. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17999. ' this.SetCentury(this.GetCentury() + 15);',
  18000. ' };',
  18001. '});',
  18002. 'this.A = null;',
  18003. 'this.B = null;',
  18004. '']),
  18005. LinesToStr([ // $mod.$main
  18006. 'ExtA.setYear(ExtA.getYear() + 1);',
  18007. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18008. 'ExtA.setYear(ExtA.getYear() + 3);',
  18009. '$mod.B.setYear($mod.B.getYear() + 4);',
  18010. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18011. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18012. '']));
  18013. end;
  18014. procedure TTestModule.TestExternalClass_ClassOf;
  18015. begin
  18016. StartProgram(false);
  18017. Add('{$modeswitch externalclass}');
  18018. Add('type');
  18019. Add(' TExtA = class external name ''ExtA''');
  18020. Add(' procedure ProcA; virtual;');
  18021. Add(' procedure ProcB; virtual;');
  18022. Add(' end;');
  18023. Add(' TExtAClass = class of TExtA;');
  18024. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18025. Add(' end;');
  18026. Add(' TExtBClass = class of TExtB;');
  18027. Add(' TExtC = class (TExtB)');
  18028. Add(' procedure ProcA; override;');
  18029. Add(' end;');
  18030. Add(' TExtCClass = class of TExtC;');
  18031. Add('procedure TExtC.ProcA; begin end;');
  18032. Add('var');
  18033. Add(' A: texta; ClA: TExtAClass;');
  18034. Add(' B: textb; ClB: TExtBClass;');
  18035. Add(' C: textc; ClC: TExtCClass;');
  18036. Add('begin');
  18037. Add(' ClA:=texta;');
  18038. Add(' ClA:=textb;');
  18039. Add(' ClA:=textc;');
  18040. Add(' ClB:=textb;');
  18041. Add(' ClB:=textc;');
  18042. Add(' ClC:=textc;');
  18043. ConvertProgram;
  18044. CheckSource('TestExternalClass_ClassOf',
  18045. LinesToStr([ // statements
  18046. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18047. ' this.$init = function () {',
  18048. ' };',
  18049. ' this.$final = function () {',
  18050. ' };',
  18051. ' this.ProcA = function () {',
  18052. ' };',
  18053. '});',
  18054. 'this.A = null;',
  18055. 'this.ClA = null;',
  18056. 'this.B = null;',
  18057. 'this.ClB = null;',
  18058. 'this.C = null;',
  18059. 'this.ClC = null;',
  18060. '']),
  18061. LinesToStr([ // $mod.$main
  18062. '$mod.ClA = ExtA;',
  18063. '$mod.ClA = ExtB;',
  18064. '$mod.ClA = $mod.TExtC;',
  18065. '$mod.ClB = ExtB;',
  18066. '$mod.ClB = $mod.TExtC;',
  18067. '$mod.ClC = $mod.TExtC;',
  18068. '']));
  18069. end;
  18070. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18071. begin
  18072. AddModuleWithIntfImplSrc('unit2.pas',
  18073. LinesToStr([
  18074. '{$modeswitch externalclass}',
  18075. 'type',
  18076. ' TExtA = class external name ''ExtA''',
  18077. ' class var Id: longint;',
  18078. ' end;',
  18079. '']),
  18080. '');
  18081. StartUnit(true);
  18082. Add('interface');
  18083. Add('uses unit2;');
  18084. Add('implementation');
  18085. Add('begin');
  18086. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18087. ConvertUnit;
  18088. CheckSource('TestExternalClass_ClassOtherUnit',
  18089. LinesToStr([
  18090. '']),
  18091. LinesToStr([
  18092. 'ExtA.Id = ExtA.Id + 1;',
  18093. '']));
  18094. end;
  18095. procedure TTestModule.TestExternalClass_Is;
  18096. begin
  18097. StartProgram(false);
  18098. Add([
  18099. '{$modeswitch externalclass}',
  18100. 'type',
  18101. ' TExtA = class external name ''ExtA''',
  18102. ' end;',
  18103. ' TExtAClass = class of TExtA;',
  18104. ' TExtB = class external name ''ExtB'' (TExtA)',
  18105. ' end;',
  18106. ' TExtBClass = class of TExtB;',
  18107. ' TExtC = class (TExtB)',
  18108. ' end;',
  18109. ' TExtCClass = class of TExtC;',
  18110. 'var',
  18111. ' A: texta; ClA: TExtAClass;',
  18112. ' B: textb; ClB: TExtBClass;',
  18113. ' C: textc; ClC: TExtCClass;',
  18114. 'begin',
  18115. ' if a is textb then ;',
  18116. ' if a is textc then ;',
  18117. ' if b is textc then ;',
  18118. ' if cla is textb then ;',
  18119. ' if cla is textc then ;',
  18120. ' if clb is textc then ;',
  18121. ' try',
  18122. ' except',
  18123. ' on TExtA do ;',
  18124. ' on e: TExtB do ;',
  18125. ' end;',
  18126. '']);
  18127. ConvertProgram;
  18128. CheckSource('TestExternalClass_Is',
  18129. LinesToStr([ // statements
  18130. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18131. ' this.$init = function () {',
  18132. ' };',
  18133. ' this.$final = function () {',
  18134. ' };',
  18135. '});',
  18136. 'this.A = null;',
  18137. 'this.ClA = null;',
  18138. 'this.B = null;',
  18139. 'this.ClB = null;',
  18140. 'this.C = null;',
  18141. 'this.ClC = null;',
  18142. '']),
  18143. LinesToStr([ // $mod.$main
  18144. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18145. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18146. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18147. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18148. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18149. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18150. 'try {} catch ($e) {',
  18151. ' if (rtl.isExt($e,ExtA)) {}',
  18152. ' else if (rtl.isExt($e,ExtB)) {',
  18153. ' var e = $e;',
  18154. ' } else throw $e',
  18155. '};',
  18156. '']));
  18157. end;
  18158. procedure TTestModule.TestExternalClass_As;
  18159. begin
  18160. StartProgram(false);
  18161. Add('{$modeswitch externalclass}');
  18162. Add('type');
  18163. Add(' TExtA = class external name ''ExtA''');
  18164. Add(' end;');
  18165. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18166. Add(' end;');
  18167. Add(' TExtC = class (TExtB)');
  18168. Add(' end;');
  18169. Add('var');
  18170. Add(' A: texta;');
  18171. Add(' B: textb;');
  18172. Add(' C: textc;');
  18173. Add('begin');
  18174. Add(' b:=a as textb;');
  18175. Add(' c:=a as textc;');
  18176. Add(' c:=b as textc;');
  18177. ConvertProgram;
  18178. CheckSource('TestExternalClass_Is',
  18179. LinesToStr([ // statements
  18180. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18181. ' this.$init = function () {',
  18182. ' };',
  18183. ' this.$final = function () {',
  18184. ' };',
  18185. '});',
  18186. 'this.A = null;',
  18187. 'this.B = null;',
  18188. 'this.C = null;',
  18189. '']),
  18190. LinesToStr([ // $mod.$main
  18191. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18192. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18193. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18194. '']));
  18195. end;
  18196. procedure TTestModule.TestExternalClass_DestructorFail;
  18197. begin
  18198. StartProgram(false);
  18199. Add('{$modeswitch externalclass}');
  18200. Add('type');
  18201. Add(' TExtA = class external name ''ExtA''');
  18202. Add(' destructor Free;');
  18203. Add(' end;');
  18204. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18205. nPasElementNotSupported);
  18206. ConvertProgram;
  18207. end;
  18208. procedure TTestModule.TestExternalClass_New;
  18209. begin
  18210. StartProgram(false);
  18211. Add([
  18212. '{$modeswitch externalclass}',
  18213. 'type',
  18214. ' TExtA = class external name ''ExtA''',
  18215. ' constructor New;',
  18216. ' constructor New(i: longint; j: longint = 2);',
  18217. ' end;',
  18218. 'var',
  18219. ' A: texta;',
  18220. 'begin',
  18221. ' a:=texta.new;',
  18222. ' a:=texta(texta.new);',
  18223. ' a:=texta.new();',
  18224. ' a:=texta.new(1);',
  18225. ' with texta do begin',
  18226. ' a:=new;',
  18227. ' a:=new();',
  18228. ' a:=new(2);',
  18229. ' end;',
  18230. ' a:=test1.texta.new;',
  18231. ' a:=test1.texta.new();',
  18232. ' a:=test1.texta.new(3);',
  18233. '']);
  18234. ConvertProgram;
  18235. CheckSource('TestExternalClass_New',
  18236. LinesToStr([ // statements
  18237. 'this.A = null;',
  18238. '']),
  18239. LinesToStr([ // $mod.$main
  18240. '$mod.A = new ExtA();',
  18241. '$mod.A = new ExtA();',
  18242. '$mod.A = new ExtA();',
  18243. '$mod.A = new ExtA(1,2);',
  18244. '$mod.A = new ExtA();',
  18245. '$mod.A = new ExtA();',
  18246. '$mod.A = new ExtA(2,2);',
  18247. '$mod.A = new ExtA();',
  18248. '$mod.A = new ExtA();',
  18249. '$mod.A = new ExtA(3,2);',
  18250. '']));
  18251. end;
  18252. procedure TTestModule.TestExternalClass_ClassOf_New;
  18253. begin
  18254. StartProgram(false);
  18255. Add('{$modeswitch externalclass}');
  18256. Add('type');
  18257. Add(' TExtAClass = class of TExtA;');
  18258. Add(' TExtA = class external name ''ExtA''');
  18259. Add(' C: TExtAClass;');
  18260. Add(' constructor New;');
  18261. Add(' end;');
  18262. Add('var');
  18263. Add(' A: texta;');
  18264. Add(' C: textaclass;');
  18265. Add('begin');
  18266. Add(' a:=c.new;');
  18267. Add(' a:=c.new();');
  18268. Add(' with C do begin');
  18269. Add(' a:=new;');
  18270. Add(' a:=new();');
  18271. Add(' end;');
  18272. Add(' a:=test1.c.new;');
  18273. Add(' a:=test1.c.new();');
  18274. Add(' a:=A.c.new();');
  18275. ConvertProgram;
  18276. CheckSource('TestExternalClass_ClassOf_New',
  18277. LinesToStr([ // statements
  18278. 'this.A = null;',
  18279. 'this.C = null;',
  18280. '']),
  18281. LinesToStr([ // $mod.$main
  18282. '$mod.A = new $mod.C();',
  18283. '$mod.A = new $mod.C();',
  18284. 'var $with = $mod.C;',
  18285. '$mod.A = new $with();',
  18286. '$mod.A = new $with();',
  18287. '$mod.A = new $mod.C();',
  18288. '$mod.A = new $mod.C();',
  18289. '$mod.A = new $mod.A.C();',
  18290. '']));
  18291. end;
  18292. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18293. begin
  18294. StartProgram(false);
  18295. Add([
  18296. '{$modeswitch externalclass}',
  18297. 'type',
  18298. ' TExtAClass = class of TExtA;',
  18299. ' TExtA = class external name ''ExtA''',
  18300. ' constructor New;',
  18301. ' end;',
  18302. 'function GetCreator: TExtAClass;',
  18303. 'begin',
  18304. ' Result:=TExtA;',
  18305. 'end;',
  18306. 'var',
  18307. ' A: texta;',
  18308. 'begin',
  18309. ' a:=getcreator.new;',
  18310. ' a:=getcreator().new;',
  18311. ' a:=getcreator().new();',
  18312. ' a:=getcreator.new();',
  18313. ' with getcreator do begin',
  18314. ' a:=new;',
  18315. ' a:=new();',
  18316. ' end;']);
  18317. ConvertProgram;
  18318. CheckSource('TestExternalClass_FuncClassOf_New',
  18319. LinesToStr([ // statements
  18320. 'this.GetCreator = function () {',
  18321. ' var Result = null;',
  18322. ' Result = ExtA;',
  18323. ' return Result;',
  18324. '};',
  18325. 'this.A = null;',
  18326. '']),
  18327. LinesToStr([ // $mod.$main
  18328. '$mod.A = new ($mod.GetCreator())();',
  18329. '$mod.A = new ($mod.GetCreator())();',
  18330. '$mod.A = new ($mod.GetCreator())();',
  18331. '$mod.A = new ($mod.GetCreator())();',
  18332. 'var $with = $mod.GetCreator();',
  18333. '$mod.A = new $with();',
  18334. '$mod.A = new $with();',
  18335. '']));
  18336. end;
  18337. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18338. begin
  18339. StartProgram(false);
  18340. Add([
  18341. '{$modeswitch externalclass}',
  18342. 'type',
  18343. ' TExtA = class external name ''ExtA''',
  18344. ' constructor New;',
  18345. ' end;',
  18346. ' TBird = class(TExtA)',
  18347. ' end;',
  18348. 'begin',
  18349. ' TBird.new;',
  18350. '']);
  18351. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18352. ConvertProgram;
  18353. end;
  18354. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  18355. begin
  18356. StartProgram(false);
  18357. Add([
  18358. '{$modeswitch externalclass}',
  18359. 'type',
  18360. ' TExtA = class external name ''ExtA''',
  18361. ' constructor New;',
  18362. ' end;',
  18363. ' TBird = class(TExtA)',
  18364. ' end;',
  18365. 'begin',
  18366. ' TBird.new();',
  18367. '']);
  18368. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18369. ConvertProgram;
  18370. end;
  18371. procedure TTestModule.TestExternalClass_NewExtName;
  18372. begin
  18373. StartProgram(false);
  18374. Add([
  18375. '{$modeswitch externalclass}',
  18376. 'type',
  18377. ' TExtA = class external name ''ExtA''',
  18378. ' constructor New; external name ''Other'';',
  18379. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  18380. ' end;',
  18381. 'var',
  18382. ' A: texta;',
  18383. 'begin',
  18384. ' a:=texta.new;',
  18385. ' a:=texta(texta.new);',
  18386. ' a:=texta.new();',
  18387. ' a:=texta.new(1);',
  18388. ' with texta do begin',
  18389. ' a:=new;',
  18390. ' a:=new();',
  18391. ' a:=new(2);',
  18392. ' end;',
  18393. ' a:=test1.texta.new;',
  18394. ' a:=test1.texta.new();',
  18395. ' a:=test1.texta.new(3);',
  18396. '']);
  18397. ConvertProgram;
  18398. CheckSource('TestExternalClass_NewExtName',
  18399. LinesToStr([ // statements
  18400. 'this.A = null;',
  18401. '']),
  18402. LinesToStr([ // $mod.$main
  18403. '$mod.A = new Other();',
  18404. '$mod.A = new Other();',
  18405. '$mod.A = new Other();',
  18406. '$mod.A = new A.B(1,2);',
  18407. '$mod.A = new Other();',
  18408. '$mod.A = new Other();',
  18409. '$mod.A = new A.B(2,2);',
  18410. '$mod.A = new Other();',
  18411. '$mod.A = new Other();',
  18412. '$mod.A = new A.B(3,2);',
  18413. '']));
  18414. end;
  18415. procedure TTestModule.TestExternalClass_Constructor;
  18416. begin
  18417. StartProgram(false);
  18418. Add([
  18419. '{$modeswitch externalclass}',
  18420. 'type',
  18421. ' TExtA = class external name ''ExtA''',
  18422. ' public type',
  18423. ' TExtB = class external name ''ExtB''',
  18424. ' public type',
  18425. ' TExtC = class external name ''ExtC''',
  18426. ' constructor New;',
  18427. ' constructor New(i: word);',
  18428. ' end;',
  18429. ' end;',
  18430. ' constructor Create;',
  18431. ' constructor Create(i: longint; j: longint = 2);',
  18432. ' end;',
  18433. 'var',
  18434. ' A: texta;',
  18435. ' C: texta.textb.textc;',
  18436. 'begin',
  18437. ' a:=texta.create;',
  18438. ' a:=texta(texta.create);',
  18439. ' a:=texta.create();',
  18440. ' a:=texta.create(1);',
  18441. ' with texta do begin',
  18442. ' a:=create;',
  18443. ' a:=create();',
  18444. ' a:=create(2);',
  18445. ' end;',
  18446. ' a:=test1.texta.create;',
  18447. ' a:=test1.texta.create();',
  18448. ' a:=test1.texta.create(3);',
  18449. ' c:=texta.textb.textc.new;',
  18450. ' c:=texta.textb.textc.new();',
  18451. ' c:=texta.textb.textc.new(4);',
  18452. '']);
  18453. ConvertProgram;
  18454. CheckSource('TestExternalClass_Constructor',
  18455. LinesToStr([ // statements
  18456. 'this.A = null;',
  18457. 'this.C = null;',
  18458. '']),
  18459. LinesToStr([ // $mod.$main
  18460. '$mod.A = new ExtA.Create();',
  18461. '$mod.A = new ExtA.Create();',
  18462. '$mod.A = new ExtA.Create();',
  18463. '$mod.A = new ExtA.Create(1,2);',
  18464. '$mod.A = new ExtA.Create();',
  18465. '$mod.A = new ExtA.Create();',
  18466. '$mod.A = new ExtA.Create(2,2);',
  18467. '$mod.A = new ExtA.Create();',
  18468. '$mod.A = new ExtA.Create();',
  18469. '$mod.A = new ExtA.Create(3,2);',
  18470. '$mod.C = new ExtA.ExtB.ExtC();',
  18471. '$mod.C = new ExtA.ExtB.ExtC();',
  18472. '$mod.C = new ExtA.ExtB.ExtC(4);',
  18473. '']));
  18474. end;
  18475. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  18476. begin
  18477. StartProgram(false);
  18478. Add([
  18479. '{$modeswitch externalclass}',
  18480. 'type',
  18481. ' TExtA = class external name ''ExtA''',
  18482. ' constructor Create; external name ''{}'';',
  18483. ' end;',
  18484. 'var',
  18485. ' A: texta;',
  18486. 'begin',
  18487. ' a:=texta.create;',
  18488. ' a:=texta(texta.create);',
  18489. ' a:=texta.create();',
  18490. ' with texta do begin',
  18491. ' a:=create;',
  18492. ' a:=create();',
  18493. ' end;',
  18494. ' a:=test1.texta.create;',
  18495. ' a:=test1.texta.create();',
  18496. '']);
  18497. ConvertProgram;
  18498. CheckSource('TestExternalClass_ConstructorBrackets',
  18499. LinesToStr([ // statements
  18500. 'this.A = null;',
  18501. '']),
  18502. LinesToStr([ // $mod.$main
  18503. '$mod.A = {};',
  18504. '$mod.A = {};',
  18505. '$mod.A = {};',
  18506. '$mod.A = {};',
  18507. '$mod.A = {};',
  18508. '$mod.A = {};',
  18509. '$mod.A = {};',
  18510. '']));
  18511. end;
  18512. procedure TTestModule.TestExternalClass_LocalConstSameName;
  18513. begin
  18514. StartProgram(false);
  18515. Add('{$modeswitch externalclass}');
  18516. Add('type');
  18517. Add(' TExtA = class external name ''ExtA''');
  18518. Add(' constructor New;');
  18519. Add(' end;');
  18520. Add('function DoIt: longint;');
  18521. Add('const ExtA: longint = 3;');
  18522. Add('begin');
  18523. Add(' Result:=ExtA;');
  18524. Add('end;');
  18525. Add('var');
  18526. Add(' A: texta;');
  18527. Add('begin');
  18528. Add(' a:=texta.new;');
  18529. ConvertProgram;
  18530. CheckSource('TestExternalClass_LocalConstSameName',
  18531. LinesToStr([ // statements
  18532. 'var ExtA$1 = 3;',
  18533. 'this.DoIt = function () {',
  18534. ' var Result = 0;',
  18535. ' Result = ExtA$1;',
  18536. ' return Result;',
  18537. '};',
  18538. 'this.A = null;',
  18539. '']),
  18540. LinesToStr([ // $mod.$main
  18541. '$mod.A = new ExtA();',
  18542. '']));
  18543. end;
  18544. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  18545. begin
  18546. StartProgram(false);
  18547. Add('{$modeswitch externalclass}');
  18548. Add('type');
  18549. Add(' TExtA = class external name ''ExtA''');
  18550. Add(' procedure DoIt;');
  18551. Add(' end;');
  18552. Add(' TMyA = class(TExtA)');
  18553. Add(' procedure DoIt;');
  18554. Add(' end;');
  18555. Add('procedure TMyA.DoIt; begin end;');
  18556. Add('begin');
  18557. ConvertProgram;
  18558. CheckSource('TestExternalClass_ReintroduceOverload',
  18559. LinesToStr([ // statements
  18560. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  18561. ' this.$init = function () {',
  18562. ' };',
  18563. ' this.$final = function () {',
  18564. ' };',
  18565. ' this.DoIt$1 = function () {',
  18566. ' };',
  18567. '});',
  18568. '']),
  18569. LinesToStr([ // $mod.$main
  18570. '']));
  18571. end;
  18572. procedure TTestModule.TestExternalClass_Inherited;
  18573. begin
  18574. StartProgram(false);
  18575. Add('{$modeswitch externalclass}');
  18576. Add('type');
  18577. Add(' TExtA = class external name ''ExtA''');
  18578. Add(' procedure DoIt(i: longint = 1); virtual;');
  18579. Add(' procedure DoSome(j: longint = 2);');
  18580. Add(' end;');
  18581. Add(' TExtB = class external name ''ExtB''(TExtA)');
  18582. Add(' end;');
  18583. Add(' TMyC = class(TExtB)');
  18584. Add(' procedure DoIt(i: longint = 1); override;');
  18585. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  18586. Add(' end;');
  18587. Add('procedure TMyC.DoIt(i: longint);');
  18588. Add('begin');
  18589. Add(' inherited;');
  18590. Add(' inherited DoIt;');
  18591. Add(' inherited DoIt();');
  18592. Add(' inherited DoIt(3);');
  18593. Add(' inherited DoSome;');
  18594. Add(' inherited DoSome();');
  18595. Add(' inherited DoSome(4);');
  18596. Add('end;');
  18597. Add('procedure TMyC.DoSome(j: longint);');
  18598. Add('begin');
  18599. Add(' inherited;');
  18600. Add('end;');
  18601. Add('begin');
  18602. ConvertProgram;
  18603. CheckSource('TestExternalClass_ReintroduceOverload',
  18604. LinesToStr([ // statements
  18605. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  18606. ' this.$init = function () {',
  18607. ' };',
  18608. ' this.$final = function () {',
  18609. ' };',
  18610. ' this.DoIt = function (i) {',
  18611. ' ExtB.DoIt.apply(this, arguments);',
  18612. ' ExtB.DoIt.call(this, 1);',
  18613. ' ExtB.DoIt.call(this, 1);',
  18614. ' ExtB.DoIt.call(this, 3);',
  18615. ' ExtB.DoSome.call(this, 2);',
  18616. ' ExtB.DoSome.call(this, 2);',
  18617. ' ExtB.DoSome.call(this, 4);',
  18618. ' };',
  18619. ' this.DoSome$1 = function (j) {',
  18620. ' ExtB.DoSome.apply(this, arguments);',
  18621. ' };',
  18622. '});',
  18623. '']),
  18624. LinesToStr([ // $mod.$main
  18625. '']));
  18626. end;
  18627. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  18628. begin
  18629. StartProgram(false);
  18630. Add('{$modeswitch externalclass}');
  18631. Add('type');
  18632. Add(' TObject = class');
  18633. Add(' end;');
  18634. Add(' TExtA = class external name ''ExtA''(TObject)');
  18635. Add(' end;');
  18636. Add('begin');
  18637. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  18638. ConvertProgram;
  18639. end;
  18640. procedure TTestModule.TestExternalClass_NewInstance;
  18641. begin
  18642. StartProgram(false);
  18643. Add('{$modeswitch externalclass}');
  18644. Add('type');
  18645. Add(' TExtA = class external name ''ExtA''');
  18646. Add(' end;');
  18647. Add(' TMyB = class(TExtA)');
  18648. Add(' protected');
  18649. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  18650. Add(' end;');
  18651. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18652. Add('begin end;');
  18653. Add('begin');
  18654. ConvertProgram;
  18655. CheckSource('TestExternalClass_NewInstance',
  18656. LinesToStr([ // statements
  18657. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  18658. ' this.$init = function () {',
  18659. ' };',
  18660. ' this.$final = function () {',
  18661. ' };',
  18662. ' this.NewInstance = function (fnname, paramarray) {',
  18663. ' var Result = null;',
  18664. ' return Result;',
  18665. ' };',
  18666. '});',
  18667. '']),
  18668. LinesToStr([ // $mod.$main
  18669. '']));
  18670. end;
  18671. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  18672. begin
  18673. StartProgram(false);
  18674. Add('{$modeswitch externalclass}');
  18675. Add('type');
  18676. Add(' TExtA = class external name ''ExtA''');
  18677. Add(' end;');
  18678. Add(' TMyB = class(TExtA)');
  18679. Add(' protected');
  18680. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  18681. Add(' end;');
  18682. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18683. Add('begin end;');
  18684. Add('begin');
  18685. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  18686. ConvertProgram;
  18687. end;
  18688. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  18689. begin
  18690. StartProgram(false);
  18691. Add('{$modeswitch externalclass}');
  18692. Add('type');
  18693. Add(' TExtA = class external name ''ExtA''');
  18694. Add(' end;');
  18695. Add(' TMyB = class(TExtA)');
  18696. Add(' protected');
  18697. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  18698. Add(' end;');
  18699. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  18700. Add('begin end;');
  18701. Add('begin');
  18702. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  18703. nIncompatibleTypeArgNo);
  18704. ConvertProgram;
  18705. end;
  18706. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  18707. begin
  18708. StartProgram(false);
  18709. Add('{$modeswitch externalclass}');
  18710. Add('type');
  18711. Add(' TExtA = class external name ''ExtA''');
  18712. Add(' end;');
  18713. Add(' TMyB = class(TExtA)');
  18714. Add(' protected');
  18715. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  18716. Add(' end;');
  18717. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  18718. Add('begin end;');
  18719. Add('begin');
  18720. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  18721. nIncompatibleTypeArgNo);
  18722. ConvertProgram;
  18723. end;
  18724. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  18725. begin
  18726. StartProgram(false);
  18727. Add([
  18728. '{$modeswitch externalclass}',
  18729. 'type',
  18730. ' TJSFunction = class external name ''Function''',
  18731. ' end;',
  18732. ' TExtA = class external name ''ExtA''(TJSFunction)',
  18733. ' constructor New(w: word);',
  18734. ' end;',
  18735. ' TBird = class (TExtA)',
  18736. ' public',
  18737. ' Size: word;',
  18738. ' class var Legs: word;',
  18739. ' constructor Create(a: word);',
  18740. ' end;',
  18741. ' TEagle = class (TBird)',
  18742. ' public',
  18743. ' constructor Create(b: word); reintroduce;',
  18744. ' end;',
  18745. 'constructor TBird.Create(a: word);',
  18746. 'begin',
  18747. ' inherited;', // silently ignored
  18748. ' inherited New(a);', // this.$func(a)
  18749. 'end;',
  18750. 'constructor TEagle.Create(b: word);',
  18751. 'begin',
  18752. ' inherited Create(b);',
  18753. 'end;',
  18754. 'var',
  18755. ' Bird: TBird;',
  18756. ' Eagle: TEagle;',
  18757. 'begin',
  18758. ' Bird:=TBird.Create(3);',
  18759. ' Eagle:=TEagle.Create(4);',
  18760. ' Bird.Size:=Bird.Size+5;',
  18761. ' Bird.Legs:=Bird.Legs+6;',
  18762. ' Eagle.Size:=Eagle.Size+5;',
  18763. ' Eagle.Legs:=Eagle.Legs+6;',
  18764. '']);
  18765. ConvertProgram;
  18766. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  18767. LinesToStr([ // statements
  18768. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  18769. ' this.Legs = 0;',
  18770. ' this.$init = function () {',
  18771. ' this.Size = 0;',
  18772. ' };',
  18773. ' this.$final = function () {',
  18774. ' };',
  18775. ' this.Create = function (a) {',
  18776. ' this.$ancestorfunc(a);',
  18777. ' return this;',
  18778. ' };',
  18779. '});',
  18780. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  18781. ' this.Create$1 = function (b) {',
  18782. ' $mod.TBird.Create.call(this, b);',
  18783. ' return this;',
  18784. ' };',
  18785. '});',
  18786. 'this.Bird = null;',
  18787. 'this.Eagle = null;',
  18788. '']),
  18789. LinesToStr([ // $mod.$main
  18790. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  18791. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  18792. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  18793. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  18794. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  18795. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  18796. '']));
  18797. end;
  18798. procedure TTestModule.TestExternalClass_PascalProperty;
  18799. begin
  18800. StartProgram(false);
  18801. Add('{$modeswitch externalclass}');
  18802. Add('type');
  18803. Add(' TJSElement = class;');
  18804. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  18805. Add(' TJSElement = class external name ''ExtA''');
  18806. Add(' end;');
  18807. Add(' TControl = class(TJSElement)');
  18808. Add(' private');
  18809. Add(' FOnClick: TJSNotifyEvent;');
  18810. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  18811. Add(' procedure Click(Sender: TJSElement);');
  18812. Add(' end;');
  18813. Add('procedure TControl.Click(Sender: TJSElement);');
  18814. Add('begin');
  18815. Add(' OnClick(Self);');
  18816. Add('end;');
  18817. Add('var');
  18818. Add(' Ctrl: TControl;');
  18819. Add('begin');
  18820. Add(' Ctrl.OnClick:[email protected];');
  18821. Add(' Ctrl.OnClick(Ctrl);');
  18822. ConvertProgram;
  18823. CheckSource('TestExternalClass_PascalProperty',
  18824. LinesToStr([ // statements
  18825. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  18826. ' this.$init = function () {',
  18827. ' this.FOnClick = null;',
  18828. ' };',
  18829. ' this.$final = function () {',
  18830. ' this.FOnClick = undefined;',
  18831. ' };',
  18832. ' this.Click = function (Sender) {',
  18833. ' this.FOnClick(this);',
  18834. ' };',
  18835. '});',
  18836. 'this.Ctrl = null;',
  18837. '']),
  18838. LinesToStr([ // $mod.$main
  18839. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  18840. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  18841. '']));
  18842. end;
  18843. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  18844. begin
  18845. StartProgram(false);
  18846. Add([
  18847. '{$modeswitch externalclass}',
  18848. 'type',
  18849. ' IUnknown = interface end;',
  18850. ' TObject = class',
  18851. ' end;',
  18852. ' TChild = class',
  18853. ' end;',
  18854. ' TExtRootA = class external name ''ExtRootA''',
  18855. ' end;',
  18856. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  18857. ' end;',
  18858. ' TExtRootB = class external name ''ExtRootB''',
  18859. ' end;',
  18860. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  18861. ' end;',
  18862. ' TExtString = class external name ''String''',
  18863. ' function charAt(aIndex : NativeInt) : string;',
  18864. ' end;',
  18865. 'var',
  18866. ' Obj: TObject;',
  18867. ' Child: TChild;',
  18868. ' RootA: TExtRootA;',
  18869. ' ChildA: TExtChildA;',
  18870. ' RootB: TExtRootB;',
  18871. ' ChildB: TExtChildB;',
  18872. ' i: IUnknown;',
  18873. ' s: string;',
  18874. ' v: jsvalue;',
  18875. 'begin',
  18876. ' obj:=tobject(roota);',
  18877. ' obj:=tobject(childa);',
  18878. ' child:=tchild(tobject(roota));',
  18879. ' roota:=textroota(obj);',
  18880. ' roota:=textroota(child);',
  18881. ' roota:=textroota(rootb);',
  18882. ' roota:=textroota(childb);',
  18883. ' childa:=textchilda(textroota(obj));',
  18884. ' roota:=TExtRootA(i);',
  18885. ' s:=TExtString(s).charAt(7);',
  18886. ' s:=TExtString(v).charAt(8);',
  18887. '']);
  18888. ConvertProgram;
  18889. CheckSource('TestExternalClass_TypeCastToRootClass',
  18890. LinesToStr([ // statements
  18891. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18892. 'rtl.createClass(this, "TObject", null, function () {',
  18893. ' this.$init = function () {',
  18894. ' };',
  18895. ' this.$final = function () {',
  18896. ' };',
  18897. '});',
  18898. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18899. '});',
  18900. 'this.Obj = null;',
  18901. 'this.Child = null;',
  18902. 'this.RootA = null;',
  18903. 'this.ChildA = null;',
  18904. 'this.RootB = null;',
  18905. 'this.ChildB = null;',
  18906. 'this.i = null;',
  18907. 'this.s = "";',
  18908. 'this.v = undefined;',
  18909. '']),
  18910. LinesToStr([ // $mod.$main
  18911. '$mod.Obj = $mod.RootA;',
  18912. '$mod.Obj = $mod.ChildA;',
  18913. '$mod.Child = $mod.RootA;',
  18914. '$mod.RootA = $mod.Obj;',
  18915. '$mod.RootA = $mod.Child;',
  18916. '$mod.RootA = $mod.RootB;',
  18917. '$mod.RootA = $mod.ChildB;',
  18918. '$mod.ChildA = $mod.Obj;',
  18919. '$mod.RootA = $mod.i;',
  18920. '$mod.s = $mod.s.charAt(7);',
  18921. '$mod.s = $mod.v.charAt(8);',
  18922. '']));
  18923. end;
  18924. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  18925. begin
  18926. StartProgram(false);
  18927. Add([
  18928. '{$modeswitch externalclass}',
  18929. 'type',
  18930. ' IUnknown = interface end;',
  18931. ' IBird = interface(IUnknown) end;',
  18932. ' TClass = class of TObject;',
  18933. ' TObject = class',
  18934. ' end;',
  18935. ' TChild = class',
  18936. ' end;',
  18937. ' TJSObject = class external name ''Object''',
  18938. ' end;',
  18939. ' TRec = record end;',
  18940. 'var',
  18941. ' Obj: TObject;',
  18942. ' Child: TChild;',
  18943. ' i: IUnknown;',
  18944. ' Bird: IBird;',
  18945. ' j: TJSObject;',
  18946. ' r: TRec;',
  18947. ' c: TClass;',
  18948. 'begin',
  18949. ' j:=tjsobject(IUnknown);',
  18950. ' j:=tjsobject(IBird);',
  18951. ' j:=tjsobject(TObject);',
  18952. ' j:=tjsobject(TChild);',
  18953. ' j:=tjsobject(TRec);',
  18954. ' j:=tjsobject(Obj);',
  18955. ' j:=tjsobject(Child);',
  18956. ' j:=tjsobject(i);',
  18957. ' j:=tjsobject(Bird);',
  18958. ' j:=tjsobject(r);',
  18959. ' j:=tjsobject(c);',
  18960. '']);
  18961. ConvertProgram;
  18962. CheckSource('TestExternalClass_TypeCastToJSObject',
  18963. LinesToStr([ // statements
  18964. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18965. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18966. 'rtl.createClass(this, "TObject", null, function () {',
  18967. ' this.$init = function () {',
  18968. ' };',
  18969. ' this.$final = function () {',
  18970. ' };',
  18971. '});',
  18972. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18973. '});',
  18974. 'rtl.recNewT(this, "TRec", function () {',
  18975. ' this.$eq = function (b) {',
  18976. ' return true;',
  18977. ' };',
  18978. ' this.$assign = function (s) {',
  18979. ' return this;',
  18980. ' };',
  18981. '});',
  18982. 'this.Obj = null;',
  18983. 'this.Child = null;',
  18984. 'this.i = null;',
  18985. 'this.Bird = null;',
  18986. 'this.j = null;',
  18987. 'this.r = this.TRec.$new();',
  18988. 'this.c = null;',
  18989. '']),
  18990. LinesToStr([ // $mod.$main
  18991. '$mod.j = $mod.IUnknown;',
  18992. '$mod.j = $mod.IBird;',
  18993. '$mod.j = $mod.TObject;',
  18994. '$mod.j = $mod.TChild;',
  18995. '$mod.j = $mod.TRec;',
  18996. '$mod.j = $mod.Obj;',
  18997. '$mod.j = $mod.Child;',
  18998. '$mod.j = $mod.i;',
  18999. '$mod.j = $mod.Bird;',
  19000. '$mod.j = $mod.r;',
  19001. '$mod.j = $mod.c;',
  19002. '']));
  19003. end;
  19004. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19005. begin
  19006. StartProgram(false);
  19007. Add('{$modeswitch externalclass}');
  19008. Add('type');
  19009. Add(' TJSString = class external name ''String''');
  19010. Add(' class function fromCharCode() : string; varargs;');
  19011. Add(' function anchor(const aName : string) : string;');
  19012. Add(' end;');
  19013. Add('var');
  19014. Add(' s: string;');
  19015. Add('begin');
  19016. Add(' s:=TJSString.fromCharCode(65,66);');
  19017. Add(' s:=TJSString(s).anchor(s);');
  19018. Add(' s:=TJSString(''foo'').anchor(s);');
  19019. ConvertProgram;
  19020. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19021. LinesToStr([ // statements
  19022. 'this.s = "";',
  19023. '']),
  19024. LinesToStr([ // $mod.$main
  19025. '$mod.s = String.fromCharCode(65, 66);',
  19026. '$mod.s = $mod.s.anchor($mod.s);',
  19027. '$mod.s = "foo".anchor($mod.s);',
  19028. '']));
  19029. end;
  19030. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19031. begin
  19032. StartProgram(false);
  19033. Add([
  19034. '{$modeswitch externalclass}',
  19035. 'type',
  19036. ' TJSObject = class external name ''Object'' end;',
  19037. ' TJSFunction = class external name ''Function''',
  19038. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19039. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19040. ' end;',
  19041. ' TObject = class',
  19042. ' procedure DoIt(i: longint);',
  19043. ' end;',
  19044. ' TFuncInt = function(o: TObject): longint;',
  19045. 'function GetIt(o: TObject): longint;',
  19046. ' procedure Sub; begin end;',
  19047. 'var',
  19048. ' f: TJSFunction;',
  19049. ' fi: TFuncInt;',
  19050. 'begin',
  19051. ' fi:=TFuncInt(f);',
  19052. ' f:=TJSFunction(fi);',
  19053. ' f:=TJSFunction(@GetIt);',
  19054. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19055. ' f:=TJSFunction(@Sub);',
  19056. ' f:=TJSFunction(@o.doit);',
  19057. ' f:=TJSFunction(fi).bind(nil,4)',
  19058. 'end;',
  19059. 'procedure TObject.DoIt(i: longint);',
  19060. ' procedure Sub; begin end;',
  19061. 'var f: TJSFunction;',
  19062. 'begin',
  19063. ' f:=TJSFunction(@DoIt);',
  19064. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19065. ' f:=TJSFunction(@Sub);',
  19066. ' f:=TJSFunction(@GetIt);',
  19067. 'end;',
  19068. 'begin']);
  19069. ConvertProgram;
  19070. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19071. LinesToStr([ // statements
  19072. 'rtl.createClass(this, "TObject", null, function () {',
  19073. ' this.$init = function () {',
  19074. ' };',
  19075. ' this.$final = function () {',
  19076. ' };',
  19077. ' this.DoIt = function (i) {',
  19078. ' var $Self = this;',
  19079. ' function Sub() {',
  19080. ' };',
  19081. ' var f = null;',
  19082. ' f = this.DoIt;',
  19083. ' f = this.DoIt.bind(null, 13);',
  19084. ' f = Sub;',
  19085. ' f = $mod.GetIt;',
  19086. ' };',
  19087. '});',
  19088. 'this.GetIt = function (o) {',
  19089. ' var Result = 0;',
  19090. ' function Sub() {',
  19091. ' };',
  19092. ' var f = null;',
  19093. ' var fi = null;',
  19094. ' fi = f;',
  19095. ' f = fi;',
  19096. ' f = $mod.GetIt;',
  19097. ' f = $mod.GetIt.bind(null, 3);',
  19098. ' f = Sub;',
  19099. ' f = $mod.TObject.DoIt;',
  19100. ' f = fi.bind(null, 4);',
  19101. ' return Result;',
  19102. '};',
  19103. '']),
  19104. LinesToStr([ // $mod.$main
  19105. '']));
  19106. end;
  19107. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19108. begin
  19109. StartProgram(false);
  19110. Add([
  19111. '{$mode delphi}',
  19112. '{$modeswitch externalclass}',
  19113. 'type',
  19114. ' TJSObject = class external name ''Object'' end;',
  19115. ' TJSWindow = class external name ''Window''(TJSObject)',
  19116. ' procedure Open;',
  19117. ' end;',
  19118. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19119. ' procedure Execute;',
  19120. ' end;',
  19121. 'procedure Fly;',
  19122. 'var',
  19123. ' w: TJSWindow;',
  19124. ' e: TJSEventTarget;',
  19125. 'begin',
  19126. ' w:=TJSWindow(e);',
  19127. ' e:=TJSEventTarget(w);',
  19128. 'end;',
  19129. 'begin']);
  19130. ConvertProgram;
  19131. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19132. LinesToStr([ // statements
  19133. 'this.Fly = function () {',
  19134. ' var w = null;',
  19135. ' var e = null;',
  19136. ' w = e;',
  19137. ' e = w;',
  19138. '};',
  19139. '']),
  19140. LinesToStr([ // $mod.$main
  19141. '']));
  19142. end;
  19143. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19144. begin
  19145. StartProgram(false);
  19146. Add('{$modeswitch externalclass}');
  19147. Add('type');
  19148. Add(' TJSString = class external name ''String''');
  19149. Add(' class function fromCharCode() : string; varargs;');
  19150. Add(' end;');
  19151. Add('var');
  19152. Add(' s: string;');
  19153. Add(' sObj: TJSString;');
  19154. Add('begin');
  19155. Add(' s:=sObj.fromCharCode(65,66);');
  19156. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19157. nExternalClassInstanceCannotAccessStaticX);
  19158. ConvertProgram;
  19159. end;
  19160. procedure TTestModule.TestExternalClass_BracketAccessor;
  19161. begin
  19162. StartProgram(false);
  19163. Add([
  19164. '{$modeswitch externalclass}',
  19165. 'type',
  19166. ' TJSArray = class external name ''Array2''',
  19167. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19168. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19169. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19170. ' end;',
  19171. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19172. 'begin end;',
  19173. 'var',
  19174. ' Arr: tjsarray;',
  19175. ' s: string;',
  19176. ' i: longint;',
  19177. ' v: jsvalue;',
  19178. 'begin',
  19179. ' v:=arr[0];',
  19180. ' v:=arr.items[1];',
  19181. ' arr[2]:=s;',
  19182. ' arr.items[3]:=s;',
  19183. ' arr[4]:=i;',
  19184. ' arr[5]:=arr[6];',
  19185. ' arr.items[7]:=arr.items[8];',
  19186. ' with arr do items[9]:=items[10];',
  19187. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19188. ' with arr do begin',
  19189. ' v:=GetItems(14);',
  19190. ' setitems(15,16);',
  19191. ' end;',
  19192. ' v:=test1.arr.items[17];',
  19193. ' test1.arr.items[18]:=v;',
  19194. '']);
  19195. ConvertProgram;
  19196. CheckSource('TestExternalClass_BracketAccessor',
  19197. LinesToStr([ // statements
  19198. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19199. '};',
  19200. 'this.Arr = null;',
  19201. 'this.s = "";',
  19202. 'this.i = 0;',
  19203. 'this.v = undefined;',
  19204. '']),
  19205. LinesToStr([ // $mod.$main
  19206. '$mod.v = $mod.Arr[0];',
  19207. '$mod.v = $mod.Arr[1];',
  19208. '$mod.Arr[2] = $mod.s;',
  19209. '$mod.Arr[3] = $mod.s;',
  19210. '$mod.Arr[4] = $mod.i;',
  19211. '$mod.Arr[5] = $mod.Arr[6];',
  19212. '$mod.Arr[7] = $mod.Arr[8];',
  19213. 'var $with = $mod.Arr;',
  19214. '$with[9] = $with[10];',
  19215. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19216. ' a: 9,',
  19217. ' p: $mod.Arr,',
  19218. ' get: function () {',
  19219. ' return this.p[this.a];',
  19220. ' },',
  19221. ' set: function (v) {',
  19222. ' this.p[this.a] = v;',
  19223. ' }',
  19224. '}, {',
  19225. ' a: 10,',
  19226. ' p: $mod.Arr,',
  19227. ' get: function () {',
  19228. ' return this.p[this.a];',
  19229. ' },',
  19230. ' set: function (v) {',
  19231. ' this.p[this.a] = v;',
  19232. ' }',
  19233. '});',
  19234. 'var $with1 = $mod.Arr;',
  19235. '$mod.v = $with1[14];',
  19236. '$with1[15] = 16;',
  19237. '$mod.v = $mod.Arr[17];',
  19238. '$mod.Arr[18] = $mod.v;',
  19239. '']));
  19240. end;
  19241. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19242. begin
  19243. StartProgram(false);
  19244. Add([
  19245. '{$modeswitch externalclass}',
  19246. 'type',
  19247. ' TJSArray = class external name ''Array2''',
  19248. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19249. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19250. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19251. ' end;',
  19252. ' TMyArr = class(TJSArray)',
  19253. ' procedure DoIt;',
  19254. ' end;',
  19255. 'procedure tmyarr.DoIt;',
  19256. 'begin',
  19257. ' Items[1]:=Items[2];',
  19258. ' SetItems(3,getItems(4));',
  19259. 'end;',
  19260. 'var',
  19261. ' Arr: tmyarr;',
  19262. ' s: string;',
  19263. ' i: longint;',
  19264. ' v: jsvalue;',
  19265. 'begin',
  19266. ' v:=arr[0];',
  19267. ' v:=arr.items[1];',
  19268. ' arr[2]:=s;',
  19269. ' arr.items[3]:=s;',
  19270. ' arr[4]:=i;',
  19271. ' arr[5]:=arr[6];',
  19272. ' arr.items[7]:=arr.items[8];',
  19273. ' with arr do items[9]:=items[10];',
  19274. ' with arr do begin',
  19275. ' v:=GetItems(14);',
  19276. ' setitems(15,16);',
  19277. ' end;',
  19278. '']);
  19279. ConvertProgram;
  19280. CheckSource('TestExternalClass_BracketAccessor_Call',
  19281. LinesToStr([ // statements
  19282. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19283. ' this.$init = function () {',
  19284. ' };',
  19285. ' this.$final = function () {',
  19286. ' };',
  19287. ' this.DoIt = function () {',
  19288. ' this[1] = this[2];',
  19289. ' this[3] = this[4];',
  19290. ' };',
  19291. '});',
  19292. 'this.Arr = null;',
  19293. 'this.s = "";',
  19294. 'this.i = 0;',
  19295. 'this.v = undefined;',
  19296. '']),
  19297. LinesToStr([ // $mod.$main
  19298. '$mod.v = $mod.Arr[0];',
  19299. '$mod.v = $mod.Arr[1];',
  19300. '$mod.Arr[2] = $mod.s;',
  19301. '$mod.Arr[3] = $mod.s;',
  19302. '$mod.Arr[4] = $mod.i;',
  19303. '$mod.Arr[5] = $mod.Arr[6];',
  19304. '$mod.Arr[7] = $mod.Arr[8];',
  19305. 'var $with = $mod.Arr;',
  19306. '$with[9] = $with[10];',
  19307. 'var $with1 = $mod.Arr;',
  19308. '$mod.v = $with1[14];',
  19309. '$with1[15] = 16;',
  19310. '']));
  19311. end;
  19312. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19313. begin
  19314. StartProgram(false);
  19315. Add('{$modeswitch externalclass}');
  19316. Add('type');
  19317. Add(' TJSArray = class external name ''Array2''');
  19318. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19319. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19320. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19321. Add(' end;');
  19322. Add('begin');
  19323. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19324. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19325. ConvertProgram;
  19326. end;
  19327. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19328. begin
  19329. StartProgram(false);
  19330. Add('{$modeswitch externalclass}');
  19331. Add('type');
  19332. Add(' TJSArray = class external name ''Array2''');
  19333. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19334. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19335. Add(' end;');
  19336. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19337. Add('begin end;');
  19338. Add('var');
  19339. Add(' Arr: tjsarray;');
  19340. Add(' v: jsvalue;');
  19341. Add('begin');
  19342. Add(' v:=arr[0];');
  19343. Add(' v:=arr.items[1];');
  19344. Add(' with arr do v:=items[2];');
  19345. Add(' doit(arr[3],arr[4]);');
  19346. ConvertProgram;
  19347. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19348. LinesToStr([ // statements
  19349. 'this.DoIt = function (vI, vJ) {',
  19350. '};',
  19351. 'this.Arr = null;',
  19352. 'this.v = undefined;',
  19353. '']),
  19354. LinesToStr([ // $mod.$main
  19355. '$mod.v = $mod.Arr[0];',
  19356. '$mod.v = $mod.Arr[1];',
  19357. 'var $with = $mod.Arr;',
  19358. '$mod.v = $with[2];',
  19359. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  19360. '']));
  19361. end;
  19362. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  19363. begin
  19364. StartProgram(false);
  19365. Add('{$modeswitch externalclass}');
  19366. Add('type');
  19367. Add(' TJSArray = class external name ''Array2''');
  19368. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19369. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  19370. Add(' end;');
  19371. Add('var');
  19372. Add(' Arr: tjsarray;');
  19373. Add(' s: string;');
  19374. Add(' i: longint;');
  19375. Add(' v: jsvalue;');
  19376. Add('begin');
  19377. Add(' arr[2]:=s;');
  19378. Add(' arr.items[3]:=s;');
  19379. Add(' arr[4]:=i;');
  19380. Add(' with arr do items[5]:=i;');
  19381. ConvertProgram;
  19382. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  19383. LinesToStr([ // statements
  19384. 'this.Arr = null;',
  19385. 'this.s = "";',
  19386. 'this.i = 0;',
  19387. 'this.v = undefined;',
  19388. '']),
  19389. LinesToStr([ // $mod.$main
  19390. '$mod.Arr[2] = $mod.s;',
  19391. '$mod.Arr[3] = $mod.s;',
  19392. '$mod.Arr[4] = $mod.i;',
  19393. 'var $with = $mod.Arr;',
  19394. '$with[5] = $mod.i;',
  19395. '']));
  19396. end;
  19397. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  19398. begin
  19399. StartProgram(false);
  19400. Add('{$modeswitch externalclass}');
  19401. Add('type');
  19402. Add(' TJSArray = class external name ''Array2''');
  19403. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19404. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  19405. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  19406. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  19407. Add(' end;');
  19408. Add('var');
  19409. Add(' Arr: tjsarray;');
  19410. Add(' s: string;');
  19411. Add(' i: longint;');
  19412. Add(' v: jsvalue;');
  19413. Add('begin');
  19414. Add(' arr[2]:=s;');
  19415. Add(' arr.items[3]:=s;');
  19416. Add(' arr.numbers[4]:=i;');
  19417. Add(' with arr do items[5]:=i;');
  19418. Add(' with arr do numbers[6]:=i;');
  19419. ConvertProgram;
  19420. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  19421. LinesToStr([ // statements
  19422. 'this.Arr = null;',
  19423. 'this.s = "";',
  19424. 'this.i = 0;',
  19425. 'this.v = undefined;',
  19426. '']),
  19427. LinesToStr([ // $mod.$main
  19428. '$mod.Arr[2] = $mod.s;',
  19429. '$mod.Arr[3] = $mod.s;',
  19430. '$mod.Arr[4] = $mod.i;',
  19431. 'var $with = $mod.Arr;',
  19432. '$with[5] = $mod.i;',
  19433. 'var $with1 = $mod.Arr;',
  19434. '$with1[6] = $mod.i;',
  19435. '']));
  19436. end;
  19437. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  19438. begin
  19439. StartProgram(false);
  19440. Add('{$modeswitch externalclass}');
  19441. Add('type');
  19442. Add(' TJSArray = class external name ''Array2''');
  19443. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19444. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19445. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  19446. Add(' end;');
  19447. Add('var');
  19448. Add(' Arr: tjsarray;');
  19449. Add(' i: longint;');
  19450. Add(' IntArr: array of longint;');
  19451. Add(' v: jsvalue;');
  19452. Add('begin');
  19453. Add(' v:=arr.items[i];');
  19454. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  19455. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  19456. ConvertProgram;
  19457. CheckSource('TestExternalClass_BracketAccessor_Index',
  19458. LinesToStr([ // statements
  19459. 'this.Arr = null;',
  19460. 'this.i = 0;',
  19461. 'this.IntArr = [];',
  19462. 'this.v = undefined;',
  19463. '']),
  19464. LinesToStr([ // $mod.$main
  19465. '$mod.v = $mod.Arr[$mod.i];',
  19466. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  19467. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  19468. '']));
  19469. end;
  19470. procedure TTestModule.TestExternalClass_ForInJSObject;
  19471. begin
  19472. StartProgram(false);
  19473. Add([
  19474. '{$modeswitch externalclass}',
  19475. 'type',
  19476. ' TJSObject = class external name ''Object''',
  19477. ' end;',
  19478. 'var',
  19479. ' o: TJSObject;',
  19480. ' key: string;',
  19481. 'begin',
  19482. ' for key in o do',
  19483. ' if key=''abc'' then ;',
  19484. '']);
  19485. ConvertProgram;
  19486. CheckSource('TestExternalClass_ForInJSObject',
  19487. LinesToStr([ // statements
  19488. 'this.o = null;',
  19489. 'this.key = "";',
  19490. '']),
  19491. LinesToStr([ // $mod.$main
  19492. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  19493. '']));
  19494. end;
  19495. procedure TTestModule.TestExternalClass_ForInJSArray;
  19496. begin
  19497. StartProgram(false);
  19498. Add([
  19499. '{$modeswitch externalclass}',
  19500. 'type',
  19501. ' TJSInt8Array = class external name ''Int8Array''',
  19502. ' private',
  19503. ' flength: NativeInt external name ''length'';',
  19504. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  19505. ' public',
  19506. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  19507. ' property Length: NativeInt read flength;',
  19508. ' end;',
  19509. 'var',
  19510. ' a: TJSInt8Array;',
  19511. ' value: shortint;',
  19512. 'begin',
  19513. ' for value in a do',
  19514. ' if value=3 then ;',
  19515. '']);
  19516. ConvertProgram;
  19517. CheckSource('TestExternalClass_ForInJSArray',
  19518. LinesToStr([ // statements
  19519. 'this.a = null;',
  19520. 'this.value = 0;',
  19521. '']),
  19522. LinesToStr([ // $mod.$main
  19523. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  19524. ' $mod.value = $in[$l];',
  19525. ' if ($mod.value === 3) ;',
  19526. '};',
  19527. '']));
  19528. end;
  19529. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  19530. begin
  19531. AddModuleWithIntfImplSrc('unit2.pas',
  19532. LinesToStr([
  19533. '{$modeswitch externalclass}',
  19534. 'type',
  19535. ' TJSBufferSource = class external name ''BufferSource''',
  19536. ' end;',
  19537. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  19538. '']),
  19539. '');
  19540. AddModuleWithIntfImplSrc('unit3.pas',
  19541. LinesToStr([
  19542. '{$modeswitch externalclass}',
  19543. 'type',
  19544. ' TJSBufferSource = class external name ''BufferSource''',
  19545. ' end;',
  19546. '']),
  19547. '');
  19548. StartUnit(true);
  19549. Add([
  19550. 'interface',
  19551. 'uses unit2, unit3;',
  19552. 'procedure DoSome(s: TJSBufferSource);',
  19553. 'implementation',
  19554. 'procedure DoSome(s: TJSBufferSource);',
  19555. 'begin',
  19556. ' DoIt(s);',
  19557. 'end;',
  19558. '']);
  19559. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  19560. nIncompatibleTypeArgNo);
  19561. ConvertUnit;
  19562. end;
  19563. procedure TTestModule.TestExternalClass_NestedConstructor;
  19564. begin
  19565. StartProgram(false);
  19566. Add([
  19567. '{$modeswitch externalclass}',
  19568. 'type',
  19569. ' TJSObject = class external name ''Object''',
  19570. ' type TBird = class external name ''Bird''',
  19571. ' type TWing = class external name ''Wing''',
  19572. ' constructor New;',
  19573. ' constructor Create(w: word = 3);',
  19574. ' end;',
  19575. ' end;',
  19576. ' end;',
  19577. 'var',
  19578. ' w: TJSObject.TBird.TWing;',
  19579. 'begin',
  19580. ' w:=tjsobject.tbird.twing.new;',
  19581. ' w:=tjsobject.tbird.twing.new();',
  19582. ' w:=tjsobject.tbird.twing.create;',
  19583. ' w:=tjsobject.tbird.twing.create(4);',
  19584. ' with tjsobject do begin',
  19585. ' w:=tbird.twing.new;',
  19586. ' w:=tbird.twing.new();',
  19587. ' w:=tbird.twing.create;',
  19588. ' w:=tbird.twing.create(11);',
  19589. ' end;',
  19590. ' with tjsobject.tbird do begin',
  19591. ' w:=twing.new;',
  19592. ' w:=twing.new();',
  19593. ' w:=twing.create;',
  19594. ' w:=twing.create(21);',
  19595. ' end;',
  19596. ' with tjsobject.tbird.twing do begin',
  19597. ' w:=new;',
  19598. ' w:=new();',
  19599. ' w:=create;',
  19600. ' w:=create(31);',
  19601. ' end;',
  19602. '']);
  19603. ConvertProgram;
  19604. CheckSource('TestExternalClass_NestedConstructor',
  19605. LinesToStr([ // statements
  19606. 'this.w = null;',
  19607. '']),
  19608. LinesToStr([ // $mod.$main
  19609. '$mod.w = new Object.Bird.Wing();',
  19610. '$mod.w = new Object.Bird.Wing();',
  19611. '$mod.w = new Object.Bird.Wing.Create();',
  19612. '$mod.w = new Object.Bird.Wing.Create(4);',
  19613. '$mod.w = new Object.Bird.Wing();',
  19614. '$mod.w = new Object.Bird.Wing();',
  19615. '$mod.w = new Object.Bird.Wing.Create();',
  19616. '$mod.w = new Object.Bird.Wing.Create(11);',
  19617. 'var $with = Object.Bird;',
  19618. '$mod.w = new Object.Bird.Wing();',
  19619. '$mod.w = new Object.Bird.Wing();',
  19620. '$mod.w = new Object.Bird.Wing.Create();',
  19621. '$mod.w = new Object.Bird.Wing.Create(21);',
  19622. 'var $with1 = Object.Bird.Wing;',
  19623. '$mod.w = new $with1();',
  19624. '$mod.w = new $with1();',
  19625. '$mod.w = new Object.Bird.Wing.Create();',
  19626. '$mod.w = new Object.Bird.Wing.Create(31);',
  19627. '']));
  19628. end;
  19629. procedure TTestModule.TestClassInterface_Corba;
  19630. begin
  19631. StartProgram(false);
  19632. Add([
  19633. '{$interfaces corba}',
  19634. 'type',
  19635. ' IUnknown = interface;',
  19636. ' IUnknown = interface',
  19637. ' [''{00000000-0000-0000-C000-000000000046}'']',
  19638. ' end;',
  19639. ' IInterface = IUnknown;',
  19640. ' IBird = interface(IInterface)',
  19641. ' function GetSize: longint;',
  19642. ' procedure SetSize(i: longint);',
  19643. ' property Size: longint read GetSize write SetSize;',
  19644. ' procedure DoIt(i: longint);',
  19645. ' end;',
  19646. ' TObject = class',
  19647. ' end;',
  19648. ' TBird = class(TObject,IBird)',
  19649. ' function GetSize: longint; virtual; abstract;',
  19650. ' procedure SetSize(i: longint); virtual; abstract;',
  19651. ' procedure DoIt(i: longint); virtual; abstract;',
  19652. ' end;',
  19653. 'var',
  19654. ' BirdIntf: IBird;',
  19655. 'begin',
  19656. ' BirdIntf.Size:=BirdIntf.Size;',
  19657. '']);
  19658. ConvertProgram;
  19659. CheckSource('TestClassInterface_Corba',
  19660. LinesToStr([ // statements
  19661. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  19662. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  19663. 'rtl.createClass(this, "TObject", null, function () {',
  19664. ' this.$init = function () {',
  19665. ' };',
  19666. ' this.$final = function () {',
  19667. ' };',
  19668. '});',
  19669. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19670. ' rtl.addIntf(this, $mod.IBird);',
  19671. '});',
  19672. 'this.BirdIntf = null;',
  19673. '']),
  19674. LinesToStr([ // $mod.$main
  19675. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  19676. '']));
  19677. end;
  19678. procedure TTestModule.TestClassInterface_ProcExternalFail;
  19679. begin
  19680. StartProgram(false);
  19681. Add([
  19682. '{$interfaces corba}',
  19683. 'type',
  19684. ' IUnknown = interface',
  19685. ' procedure DoIt; external name ''foo'';',
  19686. ' end;',
  19687. 'begin']);
  19688. SetExpectedParserError(
  19689. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  19690. nParserNoFieldsAllowed);
  19691. ConvertProgram;
  19692. end;
  19693. procedure TTestModule.TestClassInterface_Overloads;
  19694. begin
  19695. StartProgram(false);
  19696. Add([
  19697. '{$interfaces corba}',
  19698. 'type',
  19699. ' integer = longint;',
  19700. ' IUnknown = interface',
  19701. ' procedure DoIt(i: integer);',
  19702. ' procedure DoIt(s: string);',
  19703. ' end;',
  19704. ' IBird = interface(IUnknown)',
  19705. ' procedure DoIt(b: boolean); overload;',
  19706. ' end;',
  19707. ' TObject = class',
  19708. ' end;',
  19709. ' TBird = class(TObject,IBird)',
  19710. ' procedure DoIt(o: TObject);',
  19711. ' procedure DoIt(s: string);',
  19712. ' procedure DoIt(i: integer);',
  19713. ' procedure DoIt(b: boolean);',
  19714. ' end;',
  19715. 'procedure TBird.DoIt(o: TObject); begin end;',
  19716. 'procedure TBird.DoIt(s: string); begin end;',
  19717. 'procedure TBird.DoIt(i: integer); begin end;',
  19718. 'procedure TBird.DoIt(b: boolean); begin end;',
  19719. 'var',
  19720. ' BirdIntf: IBird;',
  19721. 'begin',
  19722. ' BirdIntf.DoIt(3);',
  19723. ' BirdIntf.DoIt(''abc'');',
  19724. ' BirdIntf.DoIt(true);',
  19725. '']);
  19726. ConvertProgram;
  19727. CheckSource('TestClassInterface_Overloads',
  19728. LinesToStr([ // statements
  19729. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  19730. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  19731. 'rtl.createClass(this, "TObject", null, function () {',
  19732. ' this.$init = function () {',
  19733. ' };',
  19734. ' this.$final = function () {',
  19735. ' };',
  19736. '});',
  19737. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19738. ' this.DoIt = function (o) {',
  19739. ' };',
  19740. ' this.DoIt$1 = function (s) {',
  19741. ' };',
  19742. ' this.DoIt$2 = function (i) {',
  19743. ' };',
  19744. ' this.DoIt$3 = function (b) {',
  19745. ' };',
  19746. ' rtl.addIntf(this, $mod.IBird, {',
  19747. ' DoIt$2: "DoIt$3",',
  19748. ' DoIt: "DoIt$2"',
  19749. ' });',
  19750. '});',
  19751. 'this.BirdIntf = null;',
  19752. '']),
  19753. LinesToStr([ // $mod.$main
  19754. '$mod.BirdIntf.DoIt(3);',
  19755. '$mod.BirdIntf.DoIt$1("abc");',
  19756. '$mod.BirdIntf.DoIt$2(true);',
  19757. '']));
  19758. end;
  19759. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  19760. begin
  19761. StartProgram(false);
  19762. Add([
  19763. '{$interfaces corba}',
  19764. 'type',
  19765. ' IBird = interface',
  19766. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19767. ' end;',
  19768. ' IDog = interface',
  19769. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19770. ' end;',
  19771. ' TObject = class(IBird,IDog)',
  19772. ' end;',
  19773. 'begin']);
  19774. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  19775. nDuplicateGUIDXInYZ);
  19776. ConvertProgram;
  19777. end;
  19778. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  19779. begin
  19780. StartProgram(false);
  19781. Add([
  19782. '{$interfaces corba}',
  19783. 'type',
  19784. ' IAnimal = interface',
  19785. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19786. ' end;',
  19787. ' IBird = interface(IAnimal)',
  19788. ' end;',
  19789. ' IHawk = interface(IBird)',
  19790. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19791. ' end;',
  19792. 'begin']);
  19793. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  19794. nDuplicateGUIDXInYZ);
  19795. ConvertProgram;
  19796. end;
  19797. procedure TTestModule.TestClassInterface_AncestorImpl;
  19798. begin
  19799. StartProgram(false);
  19800. Add([
  19801. '{$interfaces corba}',
  19802. 'type',
  19803. ' integer = longint;',
  19804. ' IUnknown = interface',
  19805. ' procedure DoIt(i: integer);',
  19806. ' end;',
  19807. ' IBird = interface',
  19808. ' procedure Fly(i: integer);',
  19809. ' end;',
  19810. ' TObject = class(IUnknown)',
  19811. ' procedure DoIt(i: integer);',
  19812. ' end;',
  19813. ' TBird = class(IBird)',
  19814. ' procedure Fly(i: integer);',
  19815. ' end;',
  19816. 'procedure TObject.DoIt(i: integer); begin end;',
  19817. 'procedure TBird.Fly(i: integer); begin end;',
  19818. 'begin',
  19819. '']);
  19820. ConvertProgram;
  19821. CheckSource('TestClassInterface_AncestorIntf',
  19822. LinesToStr([ // statements
  19823. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  19824. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  19825. 'rtl.createClass(this, "TObject", null, function () {',
  19826. ' this.$init = function () {',
  19827. ' };',
  19828. ' this.$final = function () {',
  19829. ' };',
  19830. ' this.DoIt = function (i) {',
  19831. ' };',
  19832. ' rtl.addIntf(this, $mod.IUnknown);',
  19833. '});',
  19834. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19835. ' this.Fly = function (i) {',
  19836. ' };',
  19837. ' rtl.addIntf(this, $mod.IBird);',
  19838. ' rtl.addIntf(this, $mod.IUnknown);',
  19839. '});',
  19840. '']),
  19841. LinesToStr([ // $mod.$main
  19842. '']));
  19843. end;
  19844. procedure TTestModule.TestClassInterface_ImplReintroduce;
  19845. begin
  19846. StartProgram(false);
  19847. Add([
  19848. '{$interfaces corba}',
  19849. 'type',
  19850. ' integer = longint;',
  19851. ' IBird = interface',
  19852. ' procedure DoIt(i: integer);',
  19853. ' end;',
  19854. ' TObject = class',
  19855. ' procedure DoIt(i: integer);',
  19856. ' end;',
  19857. ' TBird = class(IBird)',
  19858. ' procedure DoIt(i: integer); virtual; reintroduce;',
  19859. ' end;',
  19860. 'procedure TObject.DoIt(i: integer); begin end;',
  19861. 'procedure TBird.DoIt(i: integer); begin end;',
  19862. 'begin',
  19863. '']);
  19864. ConvertProgram;
  19865. CheckSource('TestClassInterface_ImplReintroduce',
  19866. LinesToStr([ // statements
  19867. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  19868. 'rtl.createClass(this, "TObject", null, function () {',
  19869. ' this.$init = function () {',
  19870. ' };',
  19871. ' this.$final = function () {',
  19872. ' };',
  19873. ' this.DoIt = function (i) {',
  19874. ' };',
  19875. '});',
  19876. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19877. ' this.DoIt$1 = function (i) {',
  19878. ' };',
  19879. ' rtl.addIntf(this, $mod.IBird, {',
  19880. ' DoIt: "DoIt$1"',
  19881. ' });',
  19882. '});',
  19883. '']),
  19884. LinesToStr([ // $mod.$main
  19885. '']));
  19886. end;
  19887. procedure TTestModule.TestClassInterface_MethodResolution;
  19888. begin
  19889. StartProgram(false);
  19890. Add([
  19891. '{$interfaces corba}',
  19892. 'type',
  19893. ' IUnknown = interface',
  19894. ' procedure Walk(i: longint);',
  19895. ' end;',
  19896. ' IBird = interface(IUnknown)',
  19897. ' procedure Walk(b: boolean); overload;',
  19898. ' procedure Fly(s: string);',
  19899. ' end;',
  19900. ' TObject = class',
  19901. ' end;',
  19902. ' TBird = class(TObject,IBird)',
  19903. ' procedure IBird.Fly = Move;',
  19904. ' procedure IBird.Walk = Hop;',
  19905. ' procedure Hop(i: longint);',
  19906. ' procedure Move(s: string);',
  19907. ' procedure Hop(b: boolean);',
  19908. ' end;',
  19909. 'procedure TBird.Move(s: string); begin end;',
  19910. 'procedure TBird.Hop(i: longint); begin end;',
  19911. 'procedure TBird.Hop(b: boolean); begin end;',
  19912. 'var',
  19913. ' BirdIntf: IBird;',
  19914. 'begin',
  19915. ' BirdIntf.Walk(3);',
  19916. ' BirdIntf.Walk(true);',
  19917. ' BirdIntf.Fly(''abc'');',
  19918. '']);
  19919. ConvertProgram;
  19920. CheckSource('TestClassInterface_MethodResolution',
  19921. LinesToStr([ // statements
  19922. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  19923. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  19924. 'rtl.createClass(this, "TObject", null, function () {',
  19925. ' this.$init = function () {',
  19926. ' };',
  19927. ' this.$final = function () {',
  19928. ' };',
  19929. '});',
  19930. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19931. ' this.Hop = function (i) {',
  19932. ' };',
  19933. ' this.Move = function (s) {',
  19934. ' };',
  19935. ' this.Hop$1 = function (b) {',
  19936. ' };',
  19937. ' rtl.addIntf(this, $mod.IBird, {',
  19938. ' Walk$1: "Hop$1",',
  19939. ' Fly: "Move",',
  19940. ' Walk: "Hop"',
  19941. ' });',
  19942. '});',
  19943. 'this.BirdIntf = null;',
  19944. '']),
  19945. LinesToStr([ // $mod.$main
  19946. '$mod.BirdIntf.Walk(3);',
  19947. '$mod.BirdIntf.Walk$1(true);',
  19948. '$mod.BirdIntf.Fly("abc");',
  19949. '']));
  19950. end;
  19951. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  19952. begin
  19953. StartProgram(false);
  19954. Add([
  19955. '{$interfaces com}',
  19956. 'type',
  19957. ' IUnknown = interface',
  19958. ' function _AddRef: longint;',
  19959. ' procedure Walk;',
  19960. ' end;',
  19961. ' IBird = interface end;',
  19962. ' IDog = interface end;',
  19963. ' TObject = class(IBird,IDog)',
  19964. ' function _AddRef: longint; virtual; abstract;',
  19965. ' procedure Walk; virtual; abstract;',
  19966. ' end;',
  19967. ' TBird = class(IUnknown)',
  19968. ' end;',
  19969. 'begin',
  19970. '']);
  19971. ConvertProgram;
  19972. CheckSource('TestClassInterface_COM_AncestorLess',
  19973. LinesToStr([ // statements
  19974. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  19975. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  19976. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  19977. 'rtl.createClass(this, "TObject", null, function () {',
  19978. ' this.$init = function () {',
  19979. ' };',
  19980. ' this.$final = function () {',
  19981. ' };',
  19982. ' rtl.addIntf(this, $mod.IBird);',
  19983. ' rtl.addIntf(this, $mod.IDog);',
  19984. '});',
  19985. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19986. ' rtl.addIntf(this, $mod.IUnknown);',
  19987. ' rtl.addIntf(this, $mod.IBird);',
  19988. ' rtl.addIntf(this, $mod.IDog);',
  19989. '});',
  19990. '']),
  19991. LinesToStr([ // $mod.$main
  19992. '']));
  19993. end;
  19994. procedure TTestModule.TestClassInterface_MethodOverride;
  19995. begin
  19996. StartProgram(false);
  19997. Add([
  19998. '{$interfaces corba}',
  19999. 'type',
  20000. ' IUnknown = interface',
  20001. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20002. ' procedure Go;',
  20003. ' end;',
  20004. ' TObject = class(IUnknown)',
  20005. ' procedure Go; virtual; abstract;',
  20006. ' end;',
  20007. ' TBird = class',
  20008. ' procedure Go; override;',
  20009. ' end;',
  20010. ' TCat = class(TObject)',
  20011. ' procedure Go; override;',
  20012. ' end;',
  20013. ' TDog = class(TObject, IUnknown)',
  20014. ' procedure Go; override;',
  20015. ' end;',
  20016. 'procedure TBird.Go; begin end;',
  20017. 'procedure TCat.Go; begin end;',
  20018. 'procedure TDog.Go; begin end;',
  20019. 'begin',
  20020. '']);
  20021. ConvertProgram;
  20022. CheckSource('TestClassInterface_MethodOverride',
  20023. LinesToStr([ // statements
  20024. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20025. 'rtl.createClass(this, "TObject", null, function () {',
  20026. ' this.$init = function () {',
  20027. ' };',
  20028. ' this.$final = function () {',
  20029. ' };',
  20030. ' rtl.addIntf(this, $mod.IUnknown);',
  20031. '});',
  20032. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20033. ' this.Go = function () {',
  20034. ' };',
  20035. ' rtl.addIntf(this, $mod.IUnknown);',
  20036. '});',
  20037. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20038. ' this.Go = function () {',
  20039. ' };',
  20040. ' rtl.addIntf(this, $mod.IUnknown);',
  20041. '});',
  20042. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20043. ' this.Go = function () {',
  20044. ' };',
  20045. ' rtl.addIntf(this, $mod.IUnknown);',
  20046. '});',
  20047. '']),
  20048. LinesToStr([ // $mod.$main
  20049. '']));
  20050. end;
  20051. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20052. begin
  20053. StartProgram(false);
  20054. Add([
  20055. '{$interfaces corba}',
  20056. 'type',
  20057. ' IUnknown = interface',
  20058. ' end;',
  20059. ' IBird = interface(IUnknown)',
  20060. ' procedure Fly(s: string);',
  20061. ' end;',
  20062. ' IEagle = interface(IBird)',
  20063. ' end;',
  20064. ' IDove = interface(IBird)',
  20065. ' end;',
  20066. ' ISwallow = interface(IBird)',
  20067. ' end;',
  20068. ' TObject = class',
  20069. ' end;',
  20070. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20071. ' procedure Fly(s: string); virtual; abstract;',
  20072. ' end;',
  20073. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20074. ' FBirdIntf: IBird;',
  20075. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20076. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20077. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20078. ' FDoveObj: TBird;',
  20079. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20080. ' function GetSwallowObj: TBird; virtual; abstract;',
  20081. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20082. ' end;',
  20083. 'begin',
  20084. '']);
  20085. ConvertProgram;
  20086. CheckSource('TestClassInterface_Corba_Delegation',
  20087. LinesToStr([ // statements
  20088. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20089. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20090. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20091. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20092. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20093. 'rtl.createClass(this, "TObject", null, function () {',
  20094. ' this.$init = function () {',
  20095. ' };',
  20096. ' this.$final = function () {',
  20097. ' };',
  20098. '});',
  20099. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20100. ' rtl.addIntf(this, $mod.IBird);',
  20101. ' rtl.addIntf(this, $mod.IEagle);',
  20102. ' rtl.addIntf(this, $mod.IDove);',
  20103. ' rtl.addIntf(this, $mod.ISwallow);',
  20104. '});',
  20105. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20106. ' this.$init = function () {',
  20107. ' $mod.TObject.$init.call(this);',
  20108. ' this.FBirdIntf = null;',
  20109. ' this.FDoveObj = null;',
  20110. ' };',
  20111. ' this.$final = function () {',
  20112. ' this.FBirdIntf = undefined;',
  20113. ' this.FDoveObj = undefined;',
  20114. ' $mod.TObject.$final.call(this);',
  20115. ' };',
  20116. ' this.$intfmaps = {',
  20117. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20118. ' return this.FBirdIntf;',
  20119. ' },',
  20120. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20121. ' return this.GetEagleIntf();',
  20122. ' },',
  20123. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20124. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20125. ' },',
  20126. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20127. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20128. ' }',
  20129. ' };',
  20130. '});',
  20131. '']),
  20132. LinesToStr([ // $mod.$main
  20133. '']));
  20134. end;
  20135. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20136. begin
  20137. StartProgram(false);
  20138. Add([
  20139. '{$interfaces corba}',
  20140. 'type',
  20141. ' IUnknown = interface',
  20142. ' end;',
  20143. ' IBird = interface(IUnknown)',
  20144. ' procedure Fly(s: string);',
  20145. ' end;',
  20146. ' IEagle = interface(IBird)',
  20147. ' end;',
  20148. ' IDove = interface(IBird)',
  20149. ' end;',
  20150. ' ISwallow = interface(IBird)',
  20151. ' end;',
  20152. ' TObject = class',
  20153. ' end;',
  20154. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20155. ' procedure Fly(s: string); virtual; abstract;',
  20156. ' end;',
  20157. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20158. ' private',
  20159. ' class var FBirdIntf: IBird;',
  20160. ' class var FDoveObj: TBird;',
  20161. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20162. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20163. ' protected',
  20164. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20165. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20166. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20167. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20168. ' end;',
  20169. 'begin',
  20170. '']);
  20171. ConvertProgram;
  20172. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20173. LinesToStr([ // statements
  20174. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20175. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20176. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20177. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20178. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20179. 'rtl.createClass(this, "TObject", null, function () {',
  20180. ' this.$init = function () {',
  20181. ' };',
  20182. ' this.$final = function () {',
  20183. ' };',
  20184. '});',
  20185. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20186. ' rtl.addIntf(this, $mod.IBird);',
  20187. ' rtl.addIntf(this, $mod.IEagle);',
  20188. ' rtl.addIntf(this, $mod.IDove);',
  20189. ' rtl.addIntf(this, $mod.ISwallow);',
  20190. '});',
  20191. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20192. ' this.FBirdIntf = null;',
  20193. ' this.FDoveObj = null;',
  20194. ' this.$intfmaps = {',
  20195. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20196. ' return this.FBirdIntf;',
  20197. ' },',
  20198. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20199. ' return this.GetEagleIntf();',
  20200. ' },',
  20201. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20202. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20203. ' },',
  20204. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20205. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20206. ' }',
  20207. ' };',
  20208. '});',
  20209. '']),
  20210. LinesToStr([ // $mod.$main
  20211. '']));
  20212. end;
  20213. procedure TTestModule.TestClassInterface_Corba_Operators;
  20214. begin
  20215. StartProgram(false);
  20216. Add([
  20217. '{$interfaces corba}',
  20218. 'type',
  20219. ' IUnknown = interface',
  20220. ' end;',
  20221. ' IBird = interface(IUnknown)',
  20222. ' function GetItems(Index: longint): longint;',
  20223. ' procedure SetItems(Index: longint; Value: longint);',
  20224. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20225. ' end;',
  20226. ' TObject = class',
  20227. ' end;',
  20228. ' TBird = class(TObject,IBird)',
  20229. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20230. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20231. ' end;',
  20232. 'var',
  20233. ' IntfVar: IBird = nil;',
  20234. ' IntfVar2: IBird;',
  20235. ' ObjVar: TBird;',
  20236. ' v: JSValue;',
  20237. 'begin',
  20238. ' IntfVar:=nil;',
  20239. ' IntfVar[3]:=IntfVar[4];',
  20240. ' if Assigned(IntfVar) then ;',
  20241. ' IntfVar:=IntfVar2;',
  20242. ' IntfVar:=ObjVar;',
  20243. ' if IntfVar=IntfVar2 then ;',
  20244. ' if IntfVar<>IntfVar2 then ;',
  20245. ' if IntfVar is IBird then ;',
  20246. ' if IntfVar is TBird then ;',
  20247. ' if ObjVar is IBird then ;',
  20248. ' IntfVar:=IntfVar2 as IBird;',
  20249. ' ObjVar:=IntfVar2 as TBird;',
  20250. ' IntfVar:=ObjVar as IBird;',
  20251. ' IntfVar:=IBird(IntfVar2);',
  20252. ' ObjVar:=TBird(IntfVar);',
  20253. ' IntfVar:=IBird(ObjVar);',
  20254. ' v:=IntfVar;',
  20255. ' IntfVar:=IBird(v);',
  20256. ' if v is IBird then ;',
  20257. ' v:=JSValue(IntfVar);',
  20258. ' v:=IBird;',
  20259. '']);
  20260. ConvertProgram;
  20261. CheckSource('TestClassInterface_Corba_Operators',
  20262. LinesToStr([ // statements
  20263. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20264. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20265. 'rtl.createClass(this, "TObject", null, function () {',
  20266. ' this.$init = function () {',
  20267. ' };',
  20268. ' this.$final = function () {',
  20269. ' };',
  20270. '});',
  20271. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20272. ' rtl.addIntf(this, $mod.IBird);',
  20273. '});',
  20274. 'this.IntfVar = null;',
  20275. 'this.IntfVar2 = null;',
  20276. 'this.ObjVar = null;',
  20277. 'this.v = undefined;',
  20278. '']),
  20279. LinesToStr([ // $mod.$main
  20280. '$mod.IntfVar = null;',
  20281. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20282. 'if ($mod.IntfVar != null) ;',
  20283. '$mod.IntfVar = $mod.IntfVar2;',
  20284. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20285. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20286. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20287. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20288. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20289. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20290. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20291. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20292. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20293. '$mod.IntfVar = $mod.IntfVar2;',
  20294. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20295. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20296. '$mod.v = $mod.IntfVar;',
  20297. '$mod.IntfVar = rtl.getObject($mod.v);',
  20298. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20299. '$mod.v = $mod.IntfVar;',
  20300. '$mod.v = $mod.IBird;',
  20301. '']));
  20302. end;
  20303. procedure TTestModule.TestClassInterface_Corba_Args;
  20304. begin
  20305. StartProgram(false);
  20306. Add([
  20307. '{$interfaces corba}',
  20308. 'type',
  20309. ' IUnknown = interface',
  20310. ' end;',
  20311. ' IBird = interface(IUnknown)',
  20312. ' end;',
  20313. ' TObject = class',
  20314. ' end;',
  20315. ' TBird = class(TObject,IBird)',
  20316. ' end;',
  20317. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20318. 'begin',
  20319. ' DoIt(i,i,i);',
  20320. 'end;',
  20321. 'procedure Change(var i: IBird; out j: IBird);',
  20322. 'begin',
  20323. ' DoIt(i,i,i);',
  20324. ' Change(i,i);',
  20325. 'end;',
  20326. 'var',
  20327. ' i: IBird;',
  20328. ' o: TBird;',
  20329. 'begin',
  20330. ' DoIt(i,i,i);',
  20331. ' Change(i,i);',
  20332. ' DoIt(o,o,o);',
  20333. '']);
  20334. ConvertProgram;
  20335. CheckSource('TestClassInterface_Corba_Args',
  20336. LinesToStr([ // statements
  20337. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20338. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20339. 'rtl.createClass(this, "TObject", null, function () {',
  20340. ' this.$init = function () {',
  20341. ' };',
  20342. ' this.$final = function () {',
  20343. ' };',
  20344. '});',
  20345. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20346. ' rtl.addIntf(this, $mod.IBird);',
  20347. '});',
  20348. 'this.DoIt = function (u, i, j) {',
  20349. ' $mod.DoIt({',
  20350. ' get: function () {',
  20351. ' return i;',
  20352. ' },',
  20353. ' set: function (v) {',
  20354. ' i = v;',
  20355. ' }',
  20356. ' }, i, i);',
  20357. '};',
  20358. 'this.Change = function (i, j) {',
  20359. ' $mod.DoIt(i, i.get(), i.get());',
  20360. ' $mod.Change(i, i);',
  20361. '};',
  20362. 'this.i = null;',
  20363. 'this.o = null;',
  20364. '']),
  20365. LinesToStr([ // $mod.$main
  20366. '$mod.DoIt({',
  20367. ' p: $mod,',
  20368. ' get: function () {',
  20369. ' return this.p.i;',
  20370. ' },',
  20371. ' set: function (v) {',
  20372. ' this.p.i = v;',
  20373. ' }',
  20374. '}, $mod.i, $mod.i);',
  20375. '$mod.Change({',
  20376. ' p: $mod,',
  20377. ' get: function () {',
  20378. ' return this.p.i;',
  20379. ' },',
  20380. ' set: function (v) {',
  20381. ' this.p.i = v;',
  20382. ' }',
  20383. '}, {',
  20384. ' p: $mod,',
  20385. ' get: function () {',
  20386. ' return this.p.i;',
  20387. ' },',
  20388. ' set: function (v) {',
  20389. ' this.p.i = v;',
  20390. ' }',
  20391. '});',
  20392. '$mod.DoIt({',
  20393. ' p: $mod,',
  20394. ' get: function () {',
  20395. ' return this.p.o;',
  20396. ' },',
  20397. ' set: function (v) {',
  20398. ' this.p.o = v;',
  20399. ' }',
  20400. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  20401. '']));
  20402. end;
  20403. procedure TTestModule.TestClassInterface_Corba_ForIn;
  20404. begin
  20405. StartProgram(false);
  20406. Add([
  20407. '{$interfaces corba}',
  20408. 'type',
  20409. ' IUnknown = interface end;',
  20410. ' TObject = class',
  20411. ' Id: longint;',
  20412. ' end;',
  20413. ' IEnumerator = interface(IUnknown)',
  20414. ' function GetCurrent: TObject;',
  20415. ' function MoveNext: Boolean;',
  20416. ' property Current: TObject read GetCurrent;',
  20417. ' end;',
  20418. ' IEnumerable = interface(IUnknown)',
  20419. ' function GetEnumerator: IEnumerator;',
  20420. ' end;',
  20421. 'var',
  20422. ' o: TObject;',
  20423. ' i: IEnumerable;',
  20424. 'begin',
  20425. ' for o in i do o.Id:=3;',
  20426. '']);
  20427. ConvertProgram;
  20428. CheckSource('TestClassInterface_Corba_ForIn',
  20429. LinesToStr([ // statements
  20430. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20431. 'rtl.createClass(this, "TObject", null, function () {',
  20432. ' this.$init = function () {',
  20433. ' this.Id = 0;',
  20434. ' };',
  20435. ' this.$final = function () {',
  20436. ' };',
  20437. '});',
  20438. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20439. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20440. 'this.o = null;',
  20441. 'this.i = null;',
  20442. '']),
  20443. LinesToStr([ // $mod.$main
  20444. 'var $in = $mod.i.GetEnumerator();',
  20445. 'while ($in.MoveNext()) {',
  20446. ' $mod.o = $in.GetCurrent();',
  20447. ' $mod.o.Id = 3;',
  20448. '};',
  20449. '']));
  20450. end;
  20451. procedure TTestModule.TestClassInterface_COM_AssignVar;
  20452. begin
  20453. StartProgram(false);
  20454. Add([
  20455. '{$interfaces com}',
  20456. 'type',
  20457. ' IUnknown = interface',
  20458. ' function _AddRef: longint;',
  20459. ' function _Release: longint;',
  20460. ' end;',
  20461. ' TObject = class(IUnknown)',
  20462. ' function _AddRef: longint; virtual; abstract;',
  20463. ' function _Release: longint; virtual; abstract;',
  20464. ' end;',
  20465. 'var',
  20466. ' i: IUnknown;',
  20467. 'procedure DoGlobal(o: TObject);',
  20468. 'begin',
  20469. ' i:=nil;',
  20470. ' i:=o;',
  20471. ' i:=i;',
  20472. 'end;',
  20473. 'procedure DoLocal(o: TObject);',
  20474. 'const k: IUnknown = nil;',
  20475. 'var j: IUnknown;',
  20476. 'begin',
  20477. ' k:=o;',
  20478. ' k:=i;',
  20479. ' j:=o;',
  20480. ' j:=i;',
  20481. 'end;',
  20482. 'var o: TObject;',
  20483. 'begin',
  20484. ' i:=nil;',
  20485. ' i:=o;',
  20486. '']);
  20487. ConvertProgram;
  20488. CheckSource('TestClassInterface_COM_AssignVar',
  20489. LinesToStr([ // statements
  20490. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20491. 'rtl.createClass(this, "TObject", null, function () {',
  20492. ' this.$init = function () {',
  20493. ' };',
  20494. ' this.$final = function () {',
  20495. ' };',
  20496. ' rtl.addIntf(this, $mod.IUnknown);',
  20497. '});',
  20498. 'this.i = null;',
  20499. 'this.DoGlobal = function (o) {',
  20500. ' rtl.setIntfP($mod, "i", null);',
  20501. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  20502. ' rtl.setIntfP($mod, "i", $mod.i);',
  20503. '};',
  20504. 'var k = null;',
  20505. 'this.DoLocal = function (o) {',
  20506. ' var j = null;',
  20507. ' try{',
  20508. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20509. ' k = rtl.setIntfL(k, $mod.i);',
  20510. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20511. ' j = rtl.setIntfL(j, $mod.i);',
  20512. ' }finally{',
  20513. ' rtl._Release(j);',
  20514. ' };',
  20515. '};',
  20516. 'this.o = null;',
  20517. '']),
  20518. LinesToStr([ // $mod.$main
  20519. 'rtl.setIntfP($mod, "i", null);',
  20520. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  20521. '']));
  20522. end;
  20523. procedure TTestModule.TestClassInterface_COM_AssignArg;
  20524. begin
  20525. StartProgram(false);
  20526. Add([
  20527. '{$interfaces com}',
  20528. 'type',
  20529. ' IUnknown = interface',
  20530. ' function _AddRef: longint;',
  20531. ' function _Release: longint;',
  20532. ' end;',
  20533. ' TObject = class(IUnknown)',
  20534. ' function _AddRef: longint; virtual; abstract;',
  20535. ' function _Release: longint; virtual; abstract;',
  20536. ' end;',
  20537. 'procedure DoDefault(i, j: IUnknown);',
  20538. 'begin',
  20539. ' i:=nil;',
  20540. ' i:=j;',
  20541. 'end;',
  20542. 'begin',
  20543. '']);
  20544. ConvertProgram;
  20545. CheckSource('TestClassInterface_COM_AssignArg',
  20546. LinesToStr([ // statements
  20547. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20548. 'rtl.createClass(this, "TObject", null, function () {',
  20549. ' this.$init = function () {',
  20550. ' };',
  20551. ' this.$final = function () {',
  20552. ' };',
  20553. ' rtl.addIntf(this, $mod.IUnknown);',
  20554. '});',
  20555. 'this.DoDefault = function (i, j) {',
  20556. ' rtl._AddRef(i);',
  20557. ' try {',
  20558. ' i = rtl.setIntfL(i, null);',
  20559. ' i = rtl.setIntfL(i, j);',
  20560. ' } finally {',
  20561. ' rtl._Release(i);',
  20562. ' };',
  20563. '};',
  20564. '']),
  20565. LinesToStr([ // $mod.$main
  20566. '']));
  20567. end;
  20568. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  20569. begin
  20570. StartProgram(false);
  20571. Add([
  20572. '{$interfaces com}',
  20573. 'type',
  20574. ' IUnknown = interface',
  20575. ' function _AddRef: longint;',
  20576. ' function _Release: longint;',
  20577. ' end;',
  20578. ' TObject = class(IUnknown)',
  20579. ' function _AddRef: longint; virtual; abstract;',
  20580. ' function _Release: longint; virtual; abstract;',
  20581. ' end;',
  20582. 'function DoDefault(i: IUnknown): IUnknown;',
  20583. 'begin',
  20584. ' Result:=i;',
  20585. ' if Result<>nil then exit;',
  20586. 'end;',
  20587. 'begin',
  20588. '']);
  20589. ConvertProgram;
  20590. CheckSource('TestClassInterface_COM_FunctionResult',
  20591. LinesToStr([ // statements
  20592. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20593. 'rtl.createClass(this, "TObject", null, function () {',
  20594. ' this.$init = function () {',
  20595. ' };',
  20596. ' this.$final = function () {',
  20597. ' };',
  20598. ' rtl.addIntf(this, $mod.IUnknown);',
  20599. '});',
  20600. 'this.DoDefault = function (i) {',
  20601. ' var Result = null;',
  20602. ' var $ok = false;',
  20603. ' try {',
  20604. ' Result = rtl.setIntfL(Result, i);',
  20605. ' if(Result !== null){',
  20606. ' $ok = true;',
  20607. ' return Result;',
  20608. ' };',
  20609. ' $ok = true;',
  20610. ' } finally {',
  20611. ' if(!$ok) rtl._Release(Result);',
  20612. ' };',
  20613. ' return Result;',
  20614. '};',
  20615. '']),
  20616. LinesToStr([ // $mod.$main
  20617. '']));
  20618. end;
  20619. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  20620. begin
  20621. StartProgram(false);
  20622. Add([
  20623. '{$interfaces com}',
  20624. 'type',
  20625. ' IUnknown = interface',
  20626. ' function _AddRef: longint;',
  20627. ' function _Release: longint;',
  20628. ' end;',
  20629. ' TObject = class(IUnknown)',
  20630. ' function _AddRef: longint; virtual; abstract;',
  20631. ' function _Release: longint; virtual; abstract;',
  20632. ' function GetIntf: IUnknown; virtual;',
  20633. ' end;',
  20634. ' TMouse = class',
  20635. ' function GetIntf: IUnknown; override;',
  20636. ' end;',
  20637. 'function TObject.GetIntf: IUnknown; begin end;',
  20638. 'function TMouse.GetIntf: IUnknown;',
  20639. 'var i: IUnknown;',
  20640. 'begin',
  20641. ' inherited;',
  20642. ' inherited GetIntf;',
  20643. ' inherited GetIntf();',
  20644. ' Result:=inherited GetIntf;',
  20645. ' Result:=inherited GetIntf();',
  20646. ' i:=inherited GetIntf;',
  20647. ' i:=inherited GetIntf();',
  20648. 'end;',
  20649. 'begin',
  20650. '']);
  20651. ConvertProgram;
  20652. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  20653. LinesToStr([ // statements
  20654. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20655. 'rtl.createClass(this, "TObject", null, function () {',
  20656. ' this.$init = function () {',
  20657. ' };',
  20658. ' this.$final = function () {',
  20659. ' };',
  20660. ' this.GetIntf = function () {',
  20661. ' var Result = null;',
  20662. ' return Result;',
  20663. ' };',
  20664. ' rtl.addIntf(this, $mod.IUnknown);',
  20665. '});',
  20666. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  20667. ' this.GetIntf = function () {',
  20668. ' var Result = null;',
  20669. ' var i = null;',
  20670. ' var $ir = rtl.createIntfRefs();',
  20671. ' var $ok = false;',
  20672. ' try {',
  20673. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  20674. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  20675. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  20676. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20677. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20678. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20679. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20680. ' $ok = true;',
  20681. ' } finally {',
  20682. ' $ir.free();',
  20683. ' rtl._Release(i);',
  20684. ' if (!$ok) rtl._Release(Result);',
  20685. ' };',
  20686. ' return Result;',
  20687. ' };',
  20688. ' rtl.addIntf(this, $mod.IUnknown);',
  20689. '});',
  20690. '']),
  20691. LinesToStr([ // $mod.$main
  20692. '']));
  20693. end;
  20694. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  20695. begin
  20696. StartProgram(false);
  20697. Add([
  20698. '{$interfaces com}',
  20699. 'type',
  20700. ' IUnknown = interface',
  20701. ' function _AddRef: longint;',
  20702. ' function _Release: longint;',
  20703. ' end;',
  20704. ' TObject = class(IUnknown)',
  20705. ' function _AddRef: longint; virtual; abstract;',
  20706. ' function _Release: longint; virtual; abstract;',
  20707. ' end;',
  20708. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  20709. 'begin',
  20710. ' if i is IUnknown then ;',
  20711. ' if o is IUnknown then ;',
  20712. ' if i is TObject then ;',
  20713. ' i:=j as IUnknown;',
  20714. ' i:=o as IUnknown;',
  20715. ' o:=j as TObject;',
  20716. ' i:=IUnknown(j);',
  20717. ' i:=IUnknown(o);',
  20718. ' o:=TObject(i);',
  20719. 'end;',
  20720. 'begin',
  20721. '']);
  20722. ConvertProgram;
  20723. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  20724. LinesToStr([ // statements
  20725. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20726. 'rtl.createClass(this, "TObject", null, function () {',
  20727. ' this.$init = function () {',
  20728. ' };',
  20729. ' this.$final = function () {',
  20730. ' };',
  20731. ' rtl.addIntf(this, $mod.IUnknown);',
  20732. '});',
  20733. 'this.DoDefault = function (i, j, o) {',
  20734. ' rtl._AddRef(i);',
  20735. ' try {',
  20736. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  20737. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  20738. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  20739. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  20740. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20741. ' o = rtl.intfAsClass(j, $mod.TObject);',
  20742. ' i = rtl.setIntfL(i, j);',
  20743. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20744. ' o = rtl.intfToClass(i, $mod.TObject);',
  20745. ' } finally {',
  20746. ' rtl._Release(i);',
  20747. ' };',
  20748. '};',
  20749. '']),
  20750. LinesToStr([ // $mod.$main
  20751. '']));
  20752. end;
  20753. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  20754. begin
  20755. StartProgram(false);
  20756. Add([
  20757. '{$interfaces com}',
  20758. 'type',
  20759. ' IUnknown = interface',
  20760. ' function _AddRef: longint;',
  20761. ' function _Release: longint;',
  20762. ' end;',
  20763. ' TObject = class(IUnknown)',
  20764. ' function _AddRef: longint; virtual; abstract;',
  20765. ' function _Release: longint; virtual; abstract;',
  20766. ' end;',
  20767. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  20768. 'var o: TObject;',
  20769. 'begin',
  20770. ' DoIt(v,v,v,v);',
  20771. ' DoIt(o,o,k,k);',
  20772. 'end;',
  20773. 'procedure DoSome;',
  20774. 'var v: IUnknown;',
  20775. 'begin',
  20776. ' DoIt(v,v,v,v);',
  20777. 'end;',
  20778. 'var i: IUnknown;',
  20779. 'begin',
  20780. ' DoIt(i,i,i,i);',
  20781. '']);
  20782. ConvertProgram;
  20783. CheckSource('TestClassInterface_COM_PassAsArg',
  20784. LinesToStr([ // statements
  20785. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20786. 'rtl.createClass(this, "TObject", null, function () {',
  20787. ' this.$init = function () {',
  20788. ' };',
  20789. ' this.$final = function () {',
  20790. ' };',
  20791. ' rtl.addIntf(this, $mod.IUnknown);',
  20792. '});',
  20793. 'this.DoIt = function (v, j, k, l) {',
  20794. ' var o = null;',
  20795. ' var $ir = rtl.createIntfRefs();',
  20796. ' rtl._AddRef(v);',
  20797. ' try {',
  20798. ' $mod.DoIt(v, v, {',
  20799. ' get: function () {',
  20800. ' return v;',
  20801. ' },',
  20802. ' set: function (w) {',
  20803. ' v = rtl.setIntfL(v, w);',
  20804. ' }',
  20805. ' }, {',
  20806. ' get: function () {',
  20807. ' return v;',
  20808. ' },',
  20809. ' set: function (w) {',
  20810. ' v = rtl.setIntfL(v, w);',
  20811. ' }',
  20812. ' });',
  20813. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  20814. ' } finally {',
  20815. ' $ir.free();',
  20816. ' rtl._Release(v);',
  20817. ' };',
  20818. '};',
  20819. 'this.DoSome = function () {',
  20820. ' var v = null;',
  20821. ' try {',
  20822. ' $mod.DoIt(v, v, {',
  20823. ' get: function () {',
  20824. ' return v;',
  20825. ' },',
  20826. ' set: function (w) {',
  20827. ' v = rtl.setIntfL(v, w);',
  20828. ' }',
  20829. ' }, {',
  20830. ' get: function () {',
  20831. ' return v;',
  20832. ' },',
  20833. ' set: function (w) {',
  20834. ' v = rtl.setIntfL(v, w);',
  20835. ' }',
  20836. ' });',
  20837. ' } finally {',
  20838. ' rtl._Release(v);',
  20839. ' };',
  20840. '};',
  20841. 'this.i = null;',
  20842. '']),
  20843. LinesToStr([ // $mod.$main
  20844. '$mod.DoIt($mod.i, $mod.i, {',
  20845. ' p: $mod,',
  20846. ' get: function () {',
  20847. ' return this.p.i;',
  20848. ' },',
  20849. ' set: function (v) {',
  20850. ' rtl.setIntfP(this.p, "i", v);',
  20851. ' }',
  20852. '}, {',
  20853. ' p: $mod,',
  20854. ' get: function () {',
  20855. ' return this.p.i;',
  20856. ' },',
  20857. ' set: function (v) {',
  20858. ' rtl.setIntfP(this.p, "i", v);',
  20859. ' }',
  20860. '});',
  20861. '']));
  20862. end;
  20863. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  20864. begin
  20865. StartProgram(false);
  20866. Add([
  20867. '{$interfaces com}',
  20868. 'type',
  20869. ' IUnknown = interface',
  20870. ' function _AddRef: longint;',
  20871. ' function _Release: longint;',
  20872. ' end;',
  20873. ' TObject = class(IUnknown)',
  20874. ' function _AddRef: longint; virtual; abstract;',
  20875. ' function _Release: longint; virtual; abstract;',
  20876. ' end;',
  20877. 'procedure DoIt(out i);',
  20878. 'begin end;',
  20879. 'procedure DoSome;',
  20880. 'var v: IUnknown;',
  20881. 'begin',
  20882. ' DoIt(v);',
  20883. 'end;',
  20884. 'function GetIt: IUnknown;',
  20885. 'begin',
  20886. ' DoIt(Result);',
  20887. 'end;',
  20888. 'var i: IUnknown;',
  20889. 'begin',
  20890. ' DoIt(i);',
  20891. '']);
  20892. ConvertProgram;
  20893. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  20894. LinesToStr([ // statements
  20895. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20896. 'rtl.createClass(this, "TObject", null, function () {',
  20897. ' this.$init = function () {',
  20898. ' };',
  20899. ' this.$final = function () {',
  20900. ' };',
  20901. ' rtl.addIntf(this, $mod.IUnknown);',
  20902. '});',
  20903. 'this.DoIt = function (i) {',
  20904. '};',
  20905. 'this.DoSome = function () {',
  20906. ' var v = null;',
  20907. ' try {',
  20908. ' $mod.DoIt({',
  20909. ' get: function () {',
  20910. ' return v;',
  20911. ' },',
  20912. ' set: function (w) {',
  20913. ' v = w;',
  20914. ' }',
  20915. ' });',
  20916. ' } finally {',
  20917. ' rtl._Release(v);',
  20918. ' };',
  20919. '};',
  20920. 'this.GetIt = function () {',
  20921. ' var Result = null;',
  20922. ' var $ok = false;',
  20923. ' try {',
  20924. ' $mod.DoIt({',
  20925. ' get: function () {',
  20926. ' return Result;',
  20927. ' },',
  20928. ' set: function (v) {',
  20929. ' Result = v;',
  20930. ' }',
  20931. ' });',
  20932. ' $ok = true;',
  20933. ' } finally {',
  20934. ' if (!$ok) rtl._Release(Result);',
  20935. ' };',
  20936. ' return Result;',
  20937. '};',
  20938. 'this.i = null;',
  20939. '']),
  20940. LinesToStr([ // $mod.$main
  20941. 'try {',
  20942. ' $mod.DoIt({',
  20943. ' p: $mod,',
  20944. ' get: function () {',
  20945. ' return this.p.i;',
  20946. ' },',
  20947. ' set: function (v) {',
  20948. ' this.p.i = v;',
  20949. ' }',
  20950. ' });',
  20951. '} finally {',
  20952. ' rtl._Release($mod.i);',
  20953. '};',
  20954. '']));
  20955. end;
  20956. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  20957. begin
  20958. StartProgram(false);
  20959. Add([
  20960. '{$interfaces com}',
  20961. 'type',
  20962. ' IUnknown = interface',
  20963. ' function _AddRef: longint;',
  20964. ' function _Release: longint;',
  20965. ' end;',
  20966. ' TObject = class(IUnknown)',
  20967. ' function _AddRef: longint; virtual; abstract;',
  20968. ' function _Release: longint; virtual; abstract;',
  20969. ' end;',
  20970. 'function GetIt: IUnknown;',
  20971. 'begin',
  20972. 'end;',
  20973. 'procedure DoSome;',
  20974. 'var v: IUnknown;',
  20975. ' i: longint;',
  20976. 'begin',
  20977. ' v:=GetIt;',
  20978. ' v:=GetIt();',
  20979. ' GetIt()._AddRef;',
  20980. ' i:=GetIt()._AddRef;',
  20981. 'end;',
  20982. 'var v: IUnknown;',
  20983. ' i: longint;',
  20984. 'begin',
  20985. ' v:=GetIt;',
  20986. ' v:=GetIt();',
  20987. ' GetIt()._AddRef;',
  20988. ' i:=GetIt()._AddRef;',
  20989. '']);
  20990. ConvertProgram;
  20991. CheckSource('TestClassInterface_COM_FunctionInExpr',
  20992. LinesToStr([ // statements
  20993. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20994. 'rtl.createClass(this, "TObject", null, function () {',
  20995. ' this.$init = function () {',
  20996. ' };',
  20997. ' this.$final = function () {',
  20998. ' };',
  20999. ' rtl.addIntf(this, $mod.IUnknown);',
  21000. '});',
  21001. 'this.GetIt = function () {',
  21002. ' var Result = null;',
  21003. ' return Result;',
  21004. '};',
  21005. 'this.DoSome = function () {',
  21006. ' var v = null;',
  21007. ' var i = 0;',
  21008. ' var $ir = rtl.createIntfRefs();',
  21009. ' try {',
  21010. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21011. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21012. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21013. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21014. ' } finally {',
  21015. ' $ir.free();',
  21016. ' rtl._Release(v);',
  21017. ' };',
  21018. '};',
  21019. 'this.v = null;',
  21020. 'this.i = 0;',
  21021. '']),
  21022. LinesToStr([ // $mod.$main
  21023. 'var $ir = rtl.createIntfRefs();',
  21024. 'try {',
  21025. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21026. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21027. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21028. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21029. '} finally {',
  21030. ' $ir.free();',
  21031. '};',
  21032. '']));
  21033. end;
  21034. procedure TTestModule.TestClassInterface_COM_Property;
  21035. begin
  21036. StartProgram(false);
  21037. Add([
  21038. '{$interfaces com}',
  21039. 'type',
  21040. ' IUnknown = interface',
  21041. ' function _AddRef: longint;',
  21042. ' function _Release: longint;',
  21043. ' end;',
  21044. ' TObject = class(IUnknown)',
  21045. ' FAnt: IUnknown;',
  21046. ' function _AddRef: longint; virtual; abstract;',
  21047. ' function _Release: longint; virtual; abstract;',
  21048. ' function GetBird: IUnknown; virtual; abstract;',
  21049. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21050. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21051. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21052. ' property Ant: IUnknown read FAnt write FAnt;',
  21053. ' property Bird: IUnknown read GetBird write SetBird;',
  21054. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21055. ' end;',
  21056. 'procedure DoIt;',
  21057. 'var',
  21058. ' o: TObject;',
  21059. ' v: IUnknown;',
  21060. 'begin',
  21061. ' v:=o.Ant;',
  21062. ' o.Ant:=v;',
  21063. ' o.Ant:=o.Ant;',
  21064. ' v:=o.Bird;',
  21065. ' o.Bird:=v;',
  21066. ' o.Bird:=o.Bird;',
  21067. ' v:=o.Items[1];',
  21068. ' o.Items[2]:=v;',
  21069. ' o.Items[3]:=o.Items[4];',
  21070. ' v:=o[5];',
  21071. ' o[6]:=v;',
  21072. ' o[7]:=o[8];',
  21073. 'end;',
  21074. 'begin',
  21075. '']);
  21076. ConvertProgram;
  21077. CheckSource('TestClassInterface_COM_Property',
  21078. LinesToStr([ // statements
  21079. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21080. 'rtl.createClass(this, "TObject", null, function () {',
  21081. ' this.$init = function () {',
  21082. ' this.FAnt = null;',
  21083. ' };',
  21084. ' this.$final = function () {',
  21085. ' rtl.setIntfP(this, "FAnt", null);',
  21086. ' };',
  21087. ' rtl.addIntf(this, $mod.IUnknown);',
  21088. '});',
  21089. 'this.DoIt = function () {',
  21090. ' var o = null;',
  21091. ' var v = null;',
  21092. ' var $ir = rtl.createIntfRefs();',
  21093. ' try {',
  21094. ' v = rtl.setIntfL(v, o.FAnt);',
  21095. ' rtl.setIntfP(o, "FAnt", v);',
  21096. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21097. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21098. ' o.SetBird(v);',
  21099. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21100. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21101. ' o.SetItems(2, v);',
  21102. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21103. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21104. ' o.SetItems(6, v);',
  21105. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21106. ' } finally {',
  21107. ' $ir.free();',
  21108. ' rtl._Release(v);',
  21109. ' };',
  21110. '};',
  21111. '']),
  21112. LinesToStr([ // $mod.$main
  21113. '']));
  21114. end;
  21115. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21116. begin
  21117. StartProgram(false);
  21118. Add([
  21119. '{$interfaces com}',
  21120. 'type',
  21121. ' IUnknown = interface',
  21122. ' function _AddRef: longint;',
  21123. ' function _Release: longint;',
  21124. ' function GetBird: IUnknown;',
  21125. ' procedure SetBird(Value: IUnknown);',
  21126. ' function GetItems(Index: longint): IUnknown;',
  21127. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21128. ' property Bird: IUnknown read GetBird write SetBird;',
  21129. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21130. ' end;',
  21131. ' TObject = class(IUnknown)',
  21132. ' function _AddRef: longint; virtual; abstract;',
  21133. ' function _Release: longint; virtual; abstract;',
  21134. ' function GetBird: IUnknown; virtual; abstract;',
  21135. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21136. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21137. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21138. ' end;',
  21139. 'procedure DoIt;',
  21140. 'var',
  21141. ' o: TObject;',
  21142. ' v: IUnknown;',
  21143. 'begin',
  21144. ' v:=v.Items[1];',
  21145. ' v.Items[2]:=v;',
  21146. ' v.Items[3]:=v.Items[4];',
  21147. ' v:=v[5];',
  21148. ' v[6]:=v;',
  21149. ' v[7]:=v[8];',
  21150. ' v[9].Bird.Bird:=v;',
  21151. ' v:=v.Bird[10].Bird',
  21152. 'end;',
  21153. 'begin',
  21154. '']);
  21155. ConvertProgram;
  21156. CheckSource('TestClassInterface_COM_IntfProperty',
  21157. LinesToStr([ // statements
  21158. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21159. ' "_AddRef",',
  21160. ' "_Release",',
  21161. ' "GetBird",',
  21162. ' "SetBird",',
  21163. ' "GetItems",',
  21164. ' "SetItems"',
  21165. '], null);',
  21166. 'rtl.createClass(this, "TObject", null, function () {',
  21167. ' this.$init = function () {',
  21168. ' };',
  21169. ' this.$final = function () {',
  21170. ' };',
  21171. ' rtl.addIntf(this, $mod.IUnknown);',
  21172. '});',
  21173. 'this.DoIt = function () {',
  21174. ' var o = null;',
  21175. ' var v = null;',
  21176. ' var $ir = rtl.createIntfRefs();',
  21177. ' try {',
  21178. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21179. ' v.SetItems(2, v);',
  21180. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21181. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21182. ' v.SetItems(6, v);',
  21183. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21184. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21185. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21186. ' } finally {',
  21187. ' $ir.free();',
  21188. ' rtl._Release(v);',
  21189. ' };',
  21190. '};',
  21191. '']),
  21192. LinesToStr([ // $mod.$main
  21193. '']));
  21194. end;
  21195. procedure TTestModule.TestClassInterface_COM_Delegation;
  21196. begin
  21197. StartProgram(false);
  21198. Add([
  21199. '{$interfaces com}',
  21200. 'type',
  21201. ' IUnknown = interface',
  21202. ' function _AddRef: longint;',
  21203. ' function _Release: longint;',
  21204. ' end;',
  21205. ' IBird = interface(IUnknown)',
  21206. ' procedure Fly(s: string);',
  21207. ' end;',
  21208. ' IEagle = interface(IBird) end;',
  21209. ' IDove = interface(IBird) end;',
  21210. ' ISwallow = interface(IBird) end;',
  21211. ' TObject = class',
  21212. ' end;',
  21213. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21214. ' function _AddRef: longint; virtual; abstract;',
  21215. ' function _Release: longint; virtual; abstract;',
  21216. ' procedure Fly(s: string); virtual; abstract;',
  21217. ' end;',
  21218. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21219. ' function _AddRef: longint; virtual; abstract;',
  21220. ' function _Release: longint; virtual; abstract;',
  21221. ' FBirdIntf: IBird;',
  21222. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21223. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21224. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21225. ' FDoveObj: TBird;',
  21226. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21227. ' function GetSwallowObj: TBird; virtual; abstract;',
  21228. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21229. ' end;',
  21230. 'begin',
  21231. '']);
  21232. ConvertProgram;
  21233. CheckSource('TestClassInterface_COM_Delegation',
  21234. LinesToStr([ // statements
  21235. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21236. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21237. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21238. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21239. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21240. 'rtl.createClass(this, "TObject", null, function () {',
  21241. ' this.$init = function () {',
  21242. ' };',
  21243. ' this.$final = function () {',
  21244. ' };',
  21245. '});',
  21246. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21247. ' rtl.addIntf(this, $mod.IBird);',
  21248. ' rtl.addIntf(this, $mod.IEagle);',
  21249. ' rtl.addIntf(this, $mod.IDove);',
  21250. ' rtl.addIntf(this, $mod.ISwallow);',
  21251. '});',
  21252. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21253. ' this.$init = function () {',
  21254. ' $mod.TObject.$init.call(this);',
  21255. ' this.FBirdIntf = null;',
  21256. ' this.FDoveObj = null;',
  21257. ' };',
  21258. ' this.$final = function () {',
  21259. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21260. ' this.FDoveObj = undefined;',
  21261. ' $mod.TObject.$final.call(this);',
  21262. ' };',
  21263. ' this.$intfmaps = {',
  21264. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21265. ' return rtl._AddRef(this.FBirdIntf);',
  21266. ' },',
  21267. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21268. ' return this.GetEagleIntf();',
  21269. ' },',
  21270. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21271. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21272. ' },',
  21273. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21274. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21275. ' }',
  21276. ' };',
  21277. '});',
  21278. '']),
  21279. LinesToStr([ // $mod.$main
  21280. '']));
  21281. end;
  21282. procedure TTestModule.TestClassInterface_COM_With;
  21283. begin
  21284. StartProgram(false);
  21285. Add([
  21286. '{$interfaces com}',
  21287. 'type',
  21288. ' IUnknown = interface',
  21289. ' function _AddRef: longint;',
  21290. ' function _Release: longint;',
  21291. ' function GetAnt: IUnknown;',
  21292. ' property Ant: IUnknown read GetAnt;',
  21293. ' end;',
  21294. ' TObject = class(IUnknown)',
  21295. ' function _AddRef: longint; virtual; abstract;',
  21296. ' function _Release: longint; virtual; abstract;',
  21297. ' function GetAnt: IUnknown; virtual; abstract;',
  21298. ' property Ant: IUnknown read GetAnt;',
  21299. ' end;',
  21300. 'procedure DoIt;',
  21301. 'var',
  21302. ' i: IUnknown;',
  21303. 'begin',
  21304. ' with i do ',
  21305. ' GetAnt;',
  21306. ' with i.Ant, Ant do ',
  21307. ' GetAnt;',
  21308. 'end;',
  21309. 'begin',
  21310. '']);
  21311. ConvertProgram;
  21312. CheckSource('TestClassInterface_COM_With',
  21313. LinesToStr([ // statements
  21314. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  21315. 'rtl.createClass(this, "TObject", null, function () {',
  21316. ' this.$init = function () {',
  21317. ' };',
  21318. ' this.$final = function () {',
  21319. ' };',
  21320. ' rtl.addIntf(this, $mod.IUnknown);',
  21321. '});',
  21322. 'this.DoIt = function () {',
  21323. ' var i = null;',
  21324. ' var $ir = rtl.createIntfRefs();',
  21325. ' try {',
  21326. ' $ir.ref(1, i.GetAnt());',
  21327. ' var $with = $ir.ref(2, i.GetAnt());',
  21328. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  21329. ' $ir.ref(4, $with1.GetAnt());',
  21330. ' } finally {',
  21331. ' $ir.free();',
  21332. ' };',
  21333. '};',
  21334. '']),
  21335. LinesToStr([ // $mod.$main
  21336. '']));
  21337. end;
  21338. procedure TTestModule.TestClassInterface_COM_ForIn;
  21339. begin
  21340. StartProgram(false);
  21341. Add([
  21342. '{$interfaces com}',
  21343. 'type',
  21344. ' IUnknown = interface end;',
  21345. ' TObject = class',
  21346. ' Id: longint;',
  21347. ' end;',
  21348. ' IEnumerator = interface(IUnknown)',
  21349. ' function GetCurrent: TObject;',
  21350. ' function MoveNext: Boolean;',
  21351. ' property Current: TObject read GetCurrent;',
  21352. ' end;',
  21353. ' IEnumerable = interface(IUnknown)',
  21354. ' function GetEnumerator: IEnumerator;',
  21355. ' end;',
  21356. 'var',
  21357. ' o: TObject;',
  21358. ' i: IEnumerable;',
  21359. 'begin',
  21360. ' for o in i do o.Id:=3;',
  21361. '']);
  21362. ConvertProgram;
  21363. CheckSource('TestClassInterface_COM_ForIn',
  21364. LinesToStr([ // statements
  21365. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21366. 'rtl.createClass(this, "TObject", null, function () {',
  21367. ' this.$init = function () {',
  21368. ' this.Id = 0;',
  21369. ' };',
  21370. ' this.$final = function () {',
  21371. ' };',
  21372. '});',
  21373. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21374. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21375. 'this.o = null;',
  21376. 'this.i = null;',
  21377. '']),
  21378. LinesToStr([ // $mod.$main
  21379. 'var $in = $mod.i.GetEnumerator();',
  21380. 'try {',
  21381. ' while ($in.MoveNext()) {',
  21382. ' $mod.o = $in.GetCurrent();',
  21383. ' $mod.o.Id = 3;',
  21384. ' }',
  21385. '} finally {',
  21386. ' rtl._Release($in)',
  21387. '};',
  21388. '']));
  21389. end;
  21390. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  21391. begin
  21392. StartProgram(false);
  21393. Add([
  21394. '{$interfaces com}',
  21395. 'type',
  21396. ' IUnknown = interface',
  21397. ' function _AddRef: longint;',
  21398. ' function _Release: longint;',
  21399. ' end;',
  21400. ' TObject = class',
  21401. ' end;',
  21402. ' TArrOfIntf = array of IUnknown;',
  21403. 'begin',
  21404. '']);
  21405. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  21406. ConvertProgram;
  21407. end;
  21408. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  21409. begin
  21410. StartProgram(false);
  21411. Add([
  21412. '{$interfaces com}',
  21413. 'type',
  21414. ' IUnknown = interface',
  21415. ' function _AddRef: longint;',
  21416. ' function _Release: longint;',
  21417. ' end;',
  21418. ' TRec = record',
  21419. ' i: IUnknown;',
  21420. ' end;',
  21421. 'begin',
  21422. '']);
  21423. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  21424. ConvertProgram;
  21425. end;
  21426. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  21427. begin
  21428. StartUnit(false);
  21429. Add([
  21430. '{$interfaces com}',
  21431. 'interface',
  21432. 'implementation',
  21433. 'type',
  21434. ' IUnknown = interface',
  21435. ' function _AddRef: longint;',
  21436. ' end;',
  21437. ' TObject = class(IUnknown)',
  21438. ' function _AddRef: longint;',
  21439. ' end;',
  21440. 'function TObject._AddRef: longint; begin end;',
  21441. 'var i: IUnknown;',
  21442. ' o: TObject;',
  21443. 'initialization',
  21444. ' i:=nil;',
  21445. ' i:=i;',
  21446. ' i:=o;',
  21447. ' if (o as IUnknown)=nil then ;',
  21448. '']);
  21449. ConvertUnit;
  21450. CheckSource('TestClassInterface_COM_UnitInitialization',
  21451. LinesToStr([ // statements
  21452. 'var $impl = $mod.$impl;',
  21453. '']),
  21454. LinesToStr([ // this.$init
  21455. 'var $ir = rtl.createIntfRefs();',
  21456. 'try {',
  21457. ' rtl.setIntfP($impl, "i", null);',
  21458. ' rtl.setIntfP($impl, "i", $impl.i);',
  21459. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  21460. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  21461. '} finally {',
  21462. ' $ir.free();',
  21463. '};',
  21464. '']),
  21465. LinesToStr([ // implementation
  21466. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  21467. 'rtl.createClass($impl, "TObject", null, function () {',
  21468. ' this.$init = function () {',
  21469. ' };',
  21470. ' this.$final = function () {',
  21471. ' };',
  21472. ' this._AddRef = function () {',
  21473. ' var Result = 0;',
  21474. ' return Result;',
  21475. ' };',
  21476. ' rtl.addIntf(this, $impl.IUnknown);',
  21477. '});',
  21478. '$impl.i = null;',
  21479. '$impl.o = null;',
  21480. ''])
  21481. );
  21482. end;
  21483. procedure TTestModule.TestClassInterface_GUID;
  21484. begin
  21485. StartProgram(false);
  21486. Add([
  21487. '{$interfaces corba}',
  21488. 'type',
  21489. ' IUnknown = interface',
  21490. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  21491. ' end;',
  21492. ' TObject = class end;',
  21493. ' TGUID = record D1, D2, D3, D4: word; end;',
  21494. ' TAliasGUID = TGUID;',
  21495. ' TGUIDString = type string;',
  21496. ' TAliasGUIDString = TGUIDString;',
  21497. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  21498. 'begin end;',
  21499. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  21500. 'begin end;',
  21501. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  21502. 'begin end;',
  21503. 'var',
  21504. ' i: IUnknown;',
  21505. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  21506. ' s: TAliasGUIDString;',
  21507. 'begin',
  21508. ' DoConstGUIDIt(IUnknown);',
  21509. ' DoDefGUID(IUnknown);',
  21510. ' DoStr(IUnknown);',
  21511. ' DoConstGUIDIt(i);',
  21512. ' DoDefGUID(i);',
  21513. ' DoStr(i);',
  21514. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  21515. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  21516. ' DoStr(g);',
  21517. ' g:=i;',
  21518. ' g:=IUnknown;',
  21519. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  21520. ' s:=i;',
  21521. ' s:=IUnknown;',
  21522. ' s:=g;',
  21523. ' if g=i then ;',
  21524. ' if i=g then ;',
  21525. ' if g=IUnknown then ;',
  21526. ' if IUnknown=g then ;',
  21527. ' if s=i then ;',
  21528. ' if i=s then ;',
  21529. ' if s=IUnknown then ;',
  21530. ' if IUnknown=s then ;',
  21531. ' if s=g then ;',
  21532. ' if g=s then ;',
  21533. '']);
  21534. ConvertProgram;
  21535. CheckSource('TestClassInterface_GUID',
  21536. LinesToStr([ // statements
  21537. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  21538. 'rtl.createClass(this, "TObject", null, function () {',
  21539. ' this.$init = function () {',
  21540. ' };',
  21541. ' this.$final = function () {',
  21542. ' };',
  21543. '});',
  21544. 'rtl.recNewT(this, "TGUID", function () {',
  21545. ' this.D1 = 0;',
  21546. ' this.D2 = 0;',
  21547. ' this.D3 = 0;',
  21548. ' this.D4 = 0;',
  21549. ' this.$eq = function (b) {',
  21550. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  21551. ' };',
  21552. ' this.$assign = function (s) {',
  21553. ' this.D1 = s.D1;',
  21554. ' this.D2 = s.D2;',
  21555. ' this.D3 = s.D3;',
  21556. ' this.D4 = s.D4;',
  21557. ' return this;',
  21558. ' };',
  21559. '});',
  21560. 'this.DoConstGUIDIt = function (g) {',
  21561. '};',
  21562. 'this.DoDefGUID = function (g) {',
  21563. '};',
  21564. 'this.DoStr = function (s) {',
  21565. '};',
  21566. 'this.i = null;',
  21567. 'this.g = this.TGUID.$clone({',
  21568. ' D1: 0xD91C9AF4,',
  21569. ' D2: 0x3C93,',
  21570. ' D3: 0x420F,',
  21571. ' D4: [',
  21572. ' 0xA3,',
  21573. ' 0x03,',
  21574. ' 0xBF,',
  21575. ' 0x5B,',
  21576. ' 0xA8,',
  21577. ' 0x2B,',
  21578. ' 0xFD,',
  21579. ' 0x23',
  21580. ' ]',
  21581. '});',
  21582. 'this.s = "";',
  21583. '']),
  21584. LinesToStr([ // $mod.$main
  21585. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  21586. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  21587. '$mod.DoStr($mod.IUnknown.$guid);',
  21588. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  21589. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  21590. '$mod.DoStr($mod.i.$guid);',
  21591. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  21592. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  21593. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  21594. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  21595. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  21596. '$mod.g.$assign({',
  21597. ' D1: 0xD91C9AF4,',
  21598. ' D2: 0x3C93,',
  21599. ' D3: 0x420F,',
  21600. ' D4: [',
  21601. ' 0xA3,',
  21602. ' 0x03,',
  21603. ' 0xBF,',
  21604. ' 0x5B,',
  21605. ' 0xA8,',
  21606. ' 0x2B,',
  21607. ' 0xFD,',
  21608. ' 0x23',
  21609. ' ]',
  21610. '});',
  21611. '$mod.s = $mod.i.$guid;',
  21612. '$mod.s = $mod.IUnknown.$guid;',
  21613. '$mod.s = rtl.guidrToStr($mod.g);',
  21614. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  21615. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  21616. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  21617. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  21618. 'if ($mod.s === $mod.i.$guid) ;',
  21619. 'if ($mod.i.$guid === $mod.s) ;',
  21620. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  21621. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  21622. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  21623. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  21624. '']));
  21625. end;
  21626. procedure TTestModule.TestClassInterface_GUIDProperty;
  21627. begin
  21628. StartProgram(false);
  21629. Add([
  21630. '{$interfaces corba}',
  21631. 'type',
  21632. ' IUnknown = interface',
  21633. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  21634. ' end;',
  21635. ' TGUID = record D1, D2, D3, D4: word; end;',
  21636. ' TAliasGUID = TGUID;',
  21637. ' TGUIDString = type string;',
  21638. ' TAliasGUIDString = TGUIDString;',
  21639. ' TObject = class',
  21640. ' function GetG: TAliasGUID; virtual; abstract;',
  21641. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  21642. ' function GetS: TAliasGUIDString; virtual; abstract;',
  21643. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  21644. ' property g: TAliasGUID read GetG write SetG;',
  21645. ' property s: TAliasGUIDString read GetS write SetS;',
  21646. ' end;',
  21647. 'var o: TObject;',
  21648. 'begin',
  21649. ' o.g:=IUnknown;',
  21650. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  21651. ' o.s:=IUnknown;',
  21652. ' o.s:=o.g;',
  21653. '']);
  21654. ConvertProgram;
  21655. CheckSource('TestClassInterface_GUIDProperty',
  21656. LinesToStr([ // statements
  21657. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  21658. 'rtl.recNewT(this, "TGUID", function () {',
  21659. ' this.D1 = 0;',
  21660. ' this.D2 = 0;',
  21661. ' this.D3 = 0;',
  21662. ' this.D4 = 0;',
  21663. ' this.$eq = function (b) {',
  21664. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  21665. ' };',
  21666. ' this.$assign = function (s) {',
  21667. ' this.D1 = s.D1;',
  21668. ' this.D2 = s.D2;',
  21669. ' this.D3 = s.D3;',
  21670. ' this.D4 = s.D4;',
  21671. ' return this;',
  21672. ' };',
  21673. '});',
  21674. 'rtl.createClass(this, "TObject", null, function () {',
  21675. ' this.$init = function () {',
  21676. ' };',
  21677. ' this.$final = function () {',
  21678. ' };',
  21679. '});',
  21680. 'this.o = null;',
  21681. '']),
  21682. LinesToStr([ // $mod.$main
  21683. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  21684. '$mod.o.SetG({',
  21685. ' D1: 0xD91C9AF4,',
  21686. ' D2: 0x3C93,',
  21687. ' D3: 0x420F,',
  21688. ' D4: [',
  21689. ' 0xA3,',
  21690. ' 0x03,',
  21691. ' 0xBF,',
  21692. ' 0x5B,',
  21693. ' 0xA8,',
  21694. ' 0x2B,',
  21695. ' 0xFD,',
  21696. ' 0x23',
  21697. ' ]',
  21698. '});',
  21699. '$mod.o.SetS($mod.IUnknown.$guid);',
  21700. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  21701. '']));
  21702. end;
  21703. procedure TTestModule.TestClassHelper_ClassVar;
  21704. begin
  21705. StartProgram(false);
  21706. Add([
  21707. 'type',
  21708. ' TObject = class',
  21709. ' end;',
  21710. ' THelper = class helper for TObject',
  21711. ' const',
  21712. ' One = 1;',
  21713. ' Two: word = 2;',
  21714. ' class var',
  21715. ' Glob: word;',
  21716. ' function Foo(w: word): word;',
  21717. ' class function Bar(w: word): word;',
  21718. ' end;',
  21719. 'function THelper.foo(w: word): word;',
  21720. 'begin',
  21721. ' Result:=w;',
  21722. ' Two:=One+w;',
  21723. ' Glob:=Glob;',
  21724. ' Result:=Self.Glob;',
  21725. ' Self.Glob:=Self.Glob;',
  21726. ' with Self do Glob:=Glob;',
  21727. 'end;',
  21728. 'class function THelper.bar(w: word): word;',
  21729. 'begin',
  21730. ' Result:=w;',
  21731. ' Two:=One;',
  21732. ' Glob:=Glob;',
  21733. ' Self.Glob:=Self.Glob;',
  21734. ' with Self do Glob:=Glob;',
  21735. 'end;',
  21736. 'var o: TObject;',
  21737. 'begin',
  21738. ' tobject.two:=tobject.one;',
  21739. ' tobject.Glob:=tobject.Glob;',
  21740. ' with tobject do begin',
  21741. ' two:=one;',
  21742. ' Glob:=Glob;',
  21743. ' end;',
  21744. ' o.two:=o.one;',
  21745. ' o.Glob:=o.Glob;',
  21746. ' with o do begin',
  21747. ' two:=one;',
  21748. ' Glob:=Glob;',
  21749. ' end;',
  21750. '']);
  21751. ConvertProgram;
  21752. CheckSource('TestClassHelper_ClassVar',
  21753. LinesToStr([ // statements
  21754. 'rtl.createClass(this, "TObject", null, function () {',
  21755. ' this.$init = function () {',
  21756. ' };',
  21757. ' this.$final = function () {',
  21758. ' };',
  21759. '});',
  21760. 'rtl.createHelper(this, "THelper", null, function () {',
  21761. ' this.One = 1;',
  21762. ' this.Two = 2;',
  21763. ' this.Glob = 0;',
  21764. ' this.Foo = function (w) {',
  21765. ' var Result = 0;',
  21766. ' Result = w;',
  21767. ' $mod.THelper.Two = 1 + w;',
  21768. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21769. ' Result = $mod.THelper.Glob;',
  21770. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21771. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21772. ' return Result;',
  21773. ' };',
  21774. ' this.Bar = function (w) {',
  21775. ' var Result = 0;',
  21776. ' Result = w;',
  21777. ' $mod.THelper.Two = 1;',
  21778. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21779. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21780. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21781. ' return Result;',
  21782. ' };',
  21783. '});',
  21784. 'this.o = null;',
  21785. '']),
  21786. LinesToStr([ // $mod.$main
  21787. '$mod.THelper.Two = 1;',
  21788. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21789. 'var $with = $mod.TObject;',
  21790. '$mod.THelper.Two = 1;',
  21791. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21792. '$mod.THelper.Two = 1;',
  21793. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21794. 'var $with1 = $mod.o;',
  21795. '$mod.THelper.Two = 1;',
  21796. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21797. '']));
  21798. end;
  21799. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  21800. begin
  21801. StartProgram(false);
  21802. Add([
  21803. 'type',
  21804. ' TObject = class',
  21805. ' FSize: word;',
  21806. ' property Size: word read FSize write FSize;',
  21807. ' end;',
  21808. ' THelper = class helper for TObject',
  21809. ' function Foo(w: word = 1): word;',
  21810. ' end;',
  21811. 'function THelper.foo(w: word): word;',
  21812. 'begin',
  21813. ' Result:=Size;',
  21814. ' Size:=Size+2;',
  21815. ' Self.Size:=Self.Size+3;',
  21816. ' FSize:=FSize+4;',
  21817. ' Self.FSize:=Self.FSize+5;',
  21818. ' with Self do begin',
  21819. ' Size:=Size+6;',
  21820. ' FSize:=FSize+7;',
  21821. ' FSize:=FSize+8;',
  21822. ' end;',
  21823. 'end;',
  21824. 'begin',
  21825. '']);
  21826. ConvertProgram;
  21827. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  21828. LinesToStr([ // statements
  21829. 'rtl.createClass(this, "TObject", null, function () {',
  21830. ' this.$init = function () {',
  21831. ' this.FSize = 0;',
  21832. ' };',
  21833. ' this.$final = function () {',
  21834. ' };',
  21835. '});',
  21836. 'rtl.createHelper(this, "THelper", null, function () {',
  21837. ' this.Foo = function (w) {',
  21838. ' var Result = 0;',
  21839. ' Result = this.FSize;',
  21840. ' this.FSize = this.FSize + 2;',
  21841. ' this.FSize = this.FSize + 3;',
  21842. ' this.FSize = this.FSize + 4;',
  21843. ' this.FSize = this.FSize + 5;',
  21844. ' this.FSize = this.FSize + 6;',
  21845. ' this.FSize = this.FSize + 7;',
  21846. ' this.FSize = this.FSize + 8;',
  21847. ' return Result;',
  21848. ' };',
  21849. '});',
  21850. '']),
  21851. LinesToStr([ // $mod.$main
  21852. '']));
  21853. end;
  21854. procedure TTestModule.TestClassHelper_Method_Call;
  21855. begin
  21856. StartProgram(false);
  21857. Add([
  21858. 'type',
  21859. ' TObject = class',
  21860. ' procedure Run(w: word = 10);',
  21861. ' end;',
  21862. ' THelper = class helper for TObject',
  21863. ' function Foo(w: word = 1): word;',
  21864. ' end;',
  21865. 'procedure TObject.Run(w: word);',
  21866. 'var o: TObject;',
  21867. 'begin',
  21868. ' Foo;',
  21869. ' Foo();',
  21870. ' Foo(2);',
  21871. ' Self.Foo;',
  21872. ' Self.Foo();',
  21873. ' Self.Foo(3);',
  21874. ' with Self do begin',
  21875. ' Foo;',
  21876. ' Foo();',
  21877. ' Foo(4);',
  21878. ' end;',
  21879. ' with o do Foo(5);',
  21880. 'end;',
  21881. 'function THelper.foo(w: word): word;',
  21882. 'begin',
  21883. ' Run;',
  21884. ' Run();',
  21885. ' Run(11);',
  21886. ' Foo;',
  21887. ' Foo();',
  21888. ' Foo(12);',
  21889. ' Self.Foo;',
  21890. ' Self.Foo();',
  21891. ' Self.Foo(13);',
  21892. ' with Self do begin',
  21893. ' Foo;',
  21894. ' Foo();',
  21895. ' Foo(14);',
  21896. ' end;',
  21897. 'end;',
  21898. 'var Obj: TObject;',
  21899. 'begin',
  21900. ' obj.Foo;',
  21901. ' obj.Foo();',
  21902. ' obj.Foo(21);',
  21903. ' with obj do begin',
  21904. ' Foo;',
  21905. ' Foo();',
  21906. ' Foo(22);',
  21907. ' end;',
  21908. '']);
  21909. ConvertProgram;
  21910. CheckSource('TestClassHelper_Method_Call',
  21911. LinesToStr([ // statements
  21912. 'rtl.createClass(this, "TObject", null, function () {',
  21913. ' this.$init = function () {',
  21914. ' };',
  21915. ' this.$final = function () {',
  21916. ' };',
  21917. ' this.Run = function (w) {',
  21918. ' var o = null;',
  21919. ' $mod.THelper.Foo.call(this, 1);',
  21920. ' $mod.THelper.Foo.call(this, 1);',
  21921. ' $mod.THelper.Foo.call(this, 2);',
  21922. ' $mod.THelper.Foo.call(this, 1);',
  21923. ' $mod.THelper.Foo.call(this, 1);',
  21924. ' $mod.THelper.Foo.call(this, 3);',
  21925. ' $mod.THelper.Foo.call(this, 1);',
  21926. ' $mod.THelper.Foo.call(this, 1);',
  21927. ' $mod.THelper.Foo.call(this, 4);',
  21928. ' $mod.THelper.Foo.call(o, 5);',
  21929. ' };',
  21930. '});',
  21931. 'rtl.createHelper(this, "THelper", null, function () {',
  21932. ' this.Foo = function (w) {',
  21933. ' var Result = 0;',
  21934. ' this.Run(10);',
  21935. ' this.Run(10);',
  21936. ' this.Run(11);',
  21937. ' $mod.THelper.Foo.call(this, 1);',
  21938. ' $mod.THelper.Foo.call(this, 1);',
  21939. ' $mod.THelper.Foo.call(this, 12);',
  21940. ' $mod.THelper.Foo.call(this, 1);',
  21941. ' $mod.THelper.Foo.call(this, 1);',
  21942. ' $mod.THelper.Foo.call(this, 13);',
  21943. ' $mod.THelper.Foo.call(this, 1);',
  21944. ' $mod.THelper.Foo.call(this, 1);',
  21945. ' $mod.THelper.Foo.call(this, 14);',
  21946. ' return Result;',
  21947. ' };',
  21948. '});',
  21949. 'this.Obj = null;',
  21950. '']),
  21951. LinesToStr([ // $mod.$main
  21952. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21953. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21954. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21955. 'var $with = $mod.Obj;',
  21956. '$mod.THelper.Foo.call($with, 1);',
  21957. '$mod.THelper.Foo.call($with, 1);',
  21958. '$mod.THelper.Foo.call($with, 22);',
  21959. '']));
  21960. end;
  21961. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  21962. begin
  21963. StartProgram(false);
  21964. Add([
  21965. 'type',
  21966. ' TObject = class',
  21967. ' procedure Run(w: word = 10);',
  21968. ' end;',
  21969. ' THelper = class helper for TObject',
  21970. ' function Foo(w: word = 1): word;',
  21971. ' end;',
  21972. 'procedure TObject.Run(w: word);',
  21973. ' procedure Sub(Self: TObject);',
  21974. ' begin',
  21975. ' Foo;',
  21976. ' Foo();',
  21977. ' Self.Foo;',
  21978. ' Self.Foo();',
  21979. ' with Self do begin',
  21980. ' Foo;',
  21981. ' Foo();',
  21982. ' end;',
  21983. ' end;',
  21984. 'begin',
  21985. 'end;',
  21986. 'function THelper.foo(w: word): word;',
  21987. ' procedure Sub(Self: TObject);',
  21988. ' begin',
  21989. ' Run;',
  21990. ' Run();',
  21991. ' Foo;',
  21992. ' Foo();',
  21993. ' Self.Foo;',
  21994. ' Self.Foo();',
  21995. ' with Self do begin',
  21996. ' Foo;',
  21997. ' Foo();',
  21998. ' end;',
  21999. ' end;',
  22000. 'begin',
  22001. 'end;',
  22002. 'begin',
  22003. '']);
  22004. ConvertProgram;
  22005. CheckSource('TestClassHelper_Method_Nested_Call',
  22006. LinesToStr([ // statements
  22007. 'rtl.createClass(this, "TObject", null, function () {',
  22008. ' this.$init = function () {',
  22009. ' };',
  22010. ' this.$final = function () {',
  22011. ' };',
  22012. ' this.Run = function (w) {',
  22013. ' var $Self = this;',
  22014. ' function Sub(Self) {',
  22015. ' $mod.THelper.Foo.call($Self, 1);',
  22016. ' $mod.THelper.Foo.call($Self, 1);',
  22017. ' $mod.THelper.Foo.call(Self, 1);',
  22018. ' $mod.THelper.Foo.call(Self, 1);',
  22019. ' $mod.THelper.Foo.call(Self, 1);',
  22020. ' $mod.THelper.Foo.call(Self, 1);',
  22021. ' };',
  22022. ' };',
  22023. '});',
  22024. 'rtl.createHelper(this, "THelper", null, function () {',
  22025. ' this.Foo = function (w) {',
  22026. ' var $Self = this;',
  22027. ' var Result = 0;',
  22028. ' function Sub(Self) {',
  22029. ' $Self.Run(10);',
  22030. ' $Self.Run(10);',
  22031. ' $mod.THelper.Foo.call($Self, 1);',
  22032. ' $mod.THelper.Foo.call($Self, 1);',
  22033. ' $mod.THelper.Foo.call(Self, 1);',
  22034. ' $mod.THelper.Foo.call(Self, 1);',
  22035. ' $mod.THelper.Foo.call(Self, 1);',
  22036. ' $mod.THelper.Foo.call(Self, 1);',
  22037. ' };',
  22038. ' return Result;',
  22039. ' };',
  22040. '});',
  22041. '']),
  22042. LinesToStr([ // $mod.$main
  22043. '']));
  22044. end;
  22045. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22046. begin
  22047. StartProgram(false);
  22048. Add([
  22049. 'type',
  22050. ' TObject = class',
  22051. ' class procedure Run(w: word = 10);',
  22052. ' end;',
  22053. ' THelper = class helper for TObject',
  22054. ' class function Foo(w: word = 1): word;',
  22055. ' end;',
  22056. 'class procedure TObject.Run(w: word);',
  22057. 'begin',
  22058. ' Foo;',
  22059. ' Foo();',
  22060. ' Self.Foo;',
  22061. ' Self.Foo();',
  22062. ' with Self do begin',
  22063. ' Foo;',
  22064. ' Foo();',
  22065. ' end;',
  22066. 'end;',
  22067. 'class function THelper.foo(w: word): word;',
  22068. 'begin',
  22069. ' Run;',
  22070. ' Run();',
  22071. ' Foo;',
  22072. ' Foo();',
  22073. ' Self.Foo;',
  22074. ' Self.Foo();',
  22075. ' with Self do begin',
  22076. ' Foo;',
  22077. ' Foo();',
  22078. ' end;',
  22079. 'end;',
  22080. 'var',
  22081. ' Obj: TObject;',
  22082. 'begin',
  22083. ' obj.Foo;',
  22084. ' obj.Foo();',
  22085. ' with obj do begin',
  22086. ' Foo;',
  22087. ' Foo();',
  22088. ' end;',
  22089. ' tobject.Foo;',
  22090. ' tobject.Foo();',
  22091. ' with tobject do begin',
  22092. ' Foo;',
  22093. ' Foo();',
  22094. ' end;',
  22095. '']);
  22096. ConvertProgram;
  22097. CheckSource('TestClassHelper_ClassMethod_Call',
  22098. LinesToStr([ // statements
  22099. 'rtl.createClass(this, "TObject", null, function () {',
  22100. ' this.$init = function () {',
  22101. ' };',
  22102. ' this.$final = function () {',
  22103. ' };',
  22104. ' this.Run = function (w) {',
  22105. ' $mod.THelper.Foo.call(this, 1);',
  22106. ' $mod.THelper.Foo.call(this, 1);',
  22107. ' $mod.THelper.Foo.call(this, 1);',
  22108. ' $mod.THelper.Foo.call(this, 1);',
  22109. ' $mod.THelper.Foo.call(this, 1);',
  22110. ' $mod.THelper.Foo.call(this, 1);',
  22111. ' };',
  22112. '});',
  22113. 'rtl.createHelper(this, "THelper", null, function () {',
  22114. ' this.Foo = function (w) {',
  22115. ' var Result = 0;',
  22116. ' this.Run(10);',
  22117. ' this.Run(10);',
  22118. ' $mod.THelper.Foo.call(this, 1);',
  22119. ' $mod.THelper.Foo.call(this, 1);',
  22120. ' $mod.THelper.Foo.call(this, 1);',
  22121. ' $mod.THelper.Foo.call(this, 1);',
  22122. ' $mod.THelper.Foo.call(this, 1);',
  22123. ' $mod.THelper.Foo.call(this, 1);',
  22124. ' return Result;',
  22125. ' };',
  22126. '});',
  22127. 'this.Obj = null;',
  22128. '']),
  22129. LinesToStr([ // $mod.$main
  22130. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22131. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22132. 'var $with = $mod.Obj;',
  22133. '$mod.THelper.Foo.call($with.$class, 1);',
  22134. '$mod.THelper.Foo.call($with.$class, 1);',
  22135. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22136. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22137. 'var $with1 = $mod.TObject;',
  22138. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22139. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22140. '']));
  22141. end;
  22142. procedure TTestModule.TestClassHelper_ClassOf;
  22143. begin
  22144. StartProgram(false);
  22145. Add([
  22146. 'type',
  22147. ' TObject = class',
  22148. ' end;',
  22149. ' TClass = class of TObject;',
  22150. ' THelper = class helper for TObject',
  22151. ' class function Foo(w: word = 1): word;',
  22152. ' end;',
  22153. 'class function THelper.foo(w: word): word;',
  22154. 'begin',
  22155. 'end;',
  22156. 'var',
  22157. ' c: TClass;',
  22158. 'begin',
  22159. ' c.Foo;',
  22160. ' c.Foo();',
  22161. ' with c do begin',
  22162. ' Foo;',
  22163. ' Foo();',
  22164. ' end;',
  22165. '']);
  22166. ConvertProgram;
  22167. CheckSource('TestClassHelper_ClassOf',
  22168. LinesToStr([ // statements
  22169. 'rtl.createClass(this, "TObject", null, function () {',
  22170. ' this.$init = function () {',
  22171. ' };',
  22172. ' this.$final = function () {',
  22173. ' };',
  22174. '});',
  22175. 'rtl.createHelper(this, "THelper", null, function () {',
  22176. ' this.Foo = function (w) {',
  22177. ' var Result = 0;',
  22178. ' return Result;',
  22179. ' };',
  22180. '});',
  22181. 'this.c = null;',
  22182. '']),
  22183. LinesToStr([ // $mod.$main
  22184. '$mod.THelper.Foo.call($mod.c, 1);',
  22185. '$mod.THelper.Foo.call($mod.c, 1);',
  22186. 'var $with = $mod.c;',
  22187. '$mod.THelper.Foo.call($with, 1);',
  22188. '$mod.THelper.Foo.call($with, 1);',
  22189. '']));
  22190. end;
  22191. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22192. begin
  22193. StartProgram(false);
  22194. Add([
  22195. '{$mode objfpc}',
  22196. 'type',
  22197. ' TObject = class',
  22198. ' procedure DoIt;',
  22199. ' end;',
  22200. ' THelper = class helper for TObject',
  22201. ' procedure Fly(w: word = 1);',
  22202. ' class procedure Glide(w: word = 1);',
  22203. ' class procedure Run(w: word = 1); static;',
  22204. ' end;',
  22205. ' TFly = procedure(w: word) of object;',
  22206. ' TGlide = TFly;',
  22207. ' TRun = procedure(w: word);',
  22208. 'var',
  22209. ' f: TFly;',
  22210. ' g: TGlide;',
  22211. ' r: TRun;',
  22212. 'procedure TObject.DoIt;',
  22213. 'begin',
  22214. ' f:=@fly;',
  22215. ' g:=@glide;',
  22216. ' r:=@run;',
  22217. ' f:[email protected];',
  22218. ' g:[email protected];',
  22219. ' r:[email protected];',
  22220. ' with self do begin',
  22221. ' f:=@fly;',
  22222. ' g:=@glide;',
  22223. ' r:=@run;',
  22224. ' end;',
  22225. 'end;',
  22226. 'procedure THelper.fly(w: word);',
  22227. 'begin',
  22228. ' f:=@fly;',
  22229. ' g:=@glide;',
  22230. ' r:=@run;',
  22231. 'end;',
  22232. 'class procedure THelper.glide(w: word);',
  22233. 'begin',
  22234. ' g:=@glide;',
  22235. ' r:=@run;',
  22236. 'end;',
  22237. 'class procedure THelper.run(w: word);',
  22238. 'begin',
  22239. ' g:=@glide;',
  22240. ' r:=@run;',
  22241. 'end;',
  22242. 'var',
  22243. ' Obj: TObject;',
  22244. 'begin',
  22245. ' f:[email protected];',
  22246. ' g:[email protected];',
  22247. ' r:[email protected];',
  22248. ' with obj do begin',
  22249. ' f:=@fly;',
  22250. ' g:=@glide;',
  22251. ' r:=@run;',
  22252. ' end;',
  22253. ' g:[email protected];',
  22254. ' r:[email protected];',
  22255. ' with tobject do begin',
  22256. ' g:=@glide;',
  22257. ' r:=@run;',
  22258. ' end;',
  22259. '']);
  22260. ConvertProgram;
  22261. CheckSource('TestClassHelper_MethodRefObjFPC',
  22262. LinesToStr([ // statements
  22263. 'rtl.createClass(this, "TObject", null, function () {',
  22264. ' this.$init = function () {',
  22265. ' };',
  22266. ' this.$final = function () {',
  22267. ' };',
  22268. ' this.DoIt = function () {',
  22269. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22270. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22271. ' $mod.r = $mod.THelper.Run;',
  22272. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22273. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22274. ' $mod.r = $mod.THelper.Run;',
  22275. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22276. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22277. ' $mod.r = $mod.THelper.Run;',
  22278. ' };',
  22279. '});',
  22280. 'rtl.createHelper(this, "THelper", null, function () {',
  22281. ' this.Fly = function (w) {',
  22282. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22283. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22284. ' $mod.r = $mod.THelper.Run;',
  22285. ' };',
  22286. ' this.Glide = function (w) {',
  22287. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  22288. ' $mod.r = $mod.THelper.Run;',
  22289. ' };',
  22290. ' this.Run = function (w) {',
  22291. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  22292. ' $mod.r = $mod.THelper.Run;',
  22293. ' };',
  22294. '});',
  22295. 'this.f = null;',
  22296. 'this.g = null;',
  22297. 'this.r = null;',
  22298. 'this.Obj = null;',
  22299. '']),
  22300. LinesToStr([ // $mod.$main
  22301. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  22302. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  22303. '$mod.r = $mod.THelper.Run;',
  22304. 'var $with = $mod.Obj;',
  22305. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  22306. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  22307. '$mod.r = $mod.THelper.Run;',
  22308. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  22309. '$mod.r = $mod.THelper.Run;',
  22310. 'var $with1 = $mod.TObject;',
  22311. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  22312. '$mod.r = $mod.THelper.Run;',
  22313. '']));
  22314. end;
  22315. procedure TTestModule.TestClassHelper_Constructor;
  22316. begin
  22317. StartProgram(false);
  22318. Add([
  22319. 'type',
  22320. ' TObject = class',
  22321. ' constructor Create;',
  22322. ' end;',
  22323. ' TClass = class of TObject;',
  22324. ' THelper = class helper for TObject',
  22325. ' constructor NewHlp(w: word);',
  22326. ' end;',
  22327. 'var',
  22328. ' obj: TObject;',
  22329. ' c: TClass;',
  22330. 'constructor TObject.Create;',
  22331. 'begin',
  22332. ' NewHlp(2);', // normal call
  22333. ' tobject.NewHlp(3);', // new instance
  22334. ' c.newhlp(4);', // new instance
  22335. 'end;',
  22336. 'constructor THelper.NewHlp(w: word);',
  22337. 'begin',
  22338. ' create;', // normal call
  22339. ' tobject.create;', // new instance
  22340. ' NewHlp(2);', // normal call
  22341. ' tobject.NewHlp(3);', // new instance
  22342. ' c.newhlp(4);', // new instance
  22343. 'end;',
  22344. 'begin',
  22345. ' obj.newhlp(2);', // normal call
  22346. ' with Obj do newhlp(12);', // normal call
  22347. ' tobject.newhlp(3);', // new instance
  22348. ' with tobject do newhlp(13);', // new instance
  22349. ' c.newhlp(4);', // new instance
  22350. ' with c do newhlp(14);', // new instance
  22351. '']);
  22352. ConvertProgram;
  22353. CheckSource('TestClassHelper_Constructor',
  22354. LinesToStr([ // statements
  22355. 'rtl.createClass(this, "TObject", null, function () {',
  22356. ' this.$init = function () {',
  22357. ' };',
  22358. ' this.$final = function () {',
  22359. ' };',
  22360. ' this.Create = function () {',
  22361. ' $mod.THelper.NewHlp.call(this, 2);',
  22362. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22363. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  22364. ' return this;',
  22365. ' };',
  22366. '});',
  22367. 'rtl.createHelper(this, "THelper", null, function () {',
  22368. ' this.NewHlp = function (w) {',
  22369. ' this.Create();',
  22370. ' $mod.TObject.$create("Create");',
  22371. ' $mod.THelper.NewHlp.call(this, 2);',
  22372. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22373. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  22374. ' return this;',
  22375. ' };',
  22376. '});',
  22377. 'this.obj = null;',
  22378. 'this.c = null;',
  22379. '']),
  22380. LinesToStr([ // $mod.$main
  22381. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  22382. 'var $with = $mod.obj;',
  22383. '$mod.THelper.NewHlp.call($with, 12);',
  22384. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22385. 'var $with1 = $mod.TObject;',
  22386. '$with1.$create($mod.THelper.NewHlp, [13]);',
  22387. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  22388. 'var $with2 = $mod.c;',
  22389. '$with2.$create($mod.THelper.NewHlp, [14]);',
  22390. '']));
  22391. end;
  22392. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  22393. begin
  22394. StartProgram(false);
  22395. Add([
  22396. 'type',
  22397. ' TObject = class',
  22398. ' procedure Fly;',
  22399. ' end;',
  22400. ' TObjHelper = class helper for TObject',
  22401. ' procedure Fly;',
  22402. ' end;',
  22403. ' TBird = class',
  22404. ' procedure Fly;',
  22405. ' end;',
  22406. ' TBirdHelper = class helper for TBird',
  22407. ' procedure Fly;',
  22408. ' procedure Walk(w: word);',
  22409. ' end;',
  22410. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  22411. ' procedure Fly;',
  22412. ' procedure Walk(w: word);',
  22413. ' end;',
  22414. 'procedure Tobject.fly;',
  22415. 'begin',
  22416. ' inherited;', // ignore
  22417. 'end;',
  22418. 'procedure Tobjhelper.fly;',
  22419. 'begin',
  22420. ' {@TObject_Fly}inherited;',
  22421. ' inherited {@TObject_Fly}Fly;',
  22422. 'end;',
  22423. 'procedure Tbird.fly;',
  22424. 'begin',
  22425. ' {@TObjHelper_Fly}inherited;',
  22426. ' inherited {@TObjHelper_Fly}Fly;',
  22427. 'end;',
  22428. 'procedure Tbirdhelper.fly;',
  22429. 'begin',
  22430. ' {@TBird_Fly}inherited;',
  22431. ' inherited {@TBird_Fly}Fly;',
  22432. 'end;',
  22433. 'procedure Tbirdhelper.walk(w: word);',
  22434. 'begin',
  22435. 'end;',
  22436. 'procedure teagleHelper.fly;',
  22437. 'begin',
  22438. ' {@TBird_Fly}inherited;',
  22439. ' inherited {@TBird_Fly}Fly;',
  22440. 'end;',
  22441. 'procedure teagleHelper.walk(w: word);',
  22442. 'begin',
  22443. ' {@TBirdHelper_Walk}inherited;',
  22444. ' inherited {@TBirdHelper_Walk}Walk(3);',
  22445. 'end;',
  22446. 'begin',
  22447. '']);
  22448. ConvertProgram;
  22449. CheckSource('TestClassHelper_InheritedObjFPC',
  22450. LinesToStr([ // statements
  22451. 'rtl.createClass(this, "TObject", null, function () {',
  22452. ' this.$init = function () {',
  22453. ' };',
  22454. ' this.$final = function () {',
  22455. ' };',
  22456. ' this.Fly = function () {',
  22457. ' };',
  22458. '});',
  22459. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22460. ' this.Fly = function () {',
  22461. ' $mod.TObject.Fly.call(this);',
  22462. ' $mod.TObject.Fly.call(this);',
  22463. ' };',
  22464. '});',
  22465. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22466. ' this.Fly$1 = function () {',
  22467. ' $mod.TObjHelper.Fly.call(this);',
  22468. ' $mod.TObjHelper.Fly.call(this);',
  22469. ' };',
  22470. '});',
  22471. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22472. ' this.Fly = function () {',
  22473. ' $mod.TBird.Fly$1.call(this);',
  22474. ' $mod.TBird.Fly$1.call(this);',
  22475. ' };',
  22476. ' this.Walk = function (w) {',
  22477. ' };',
  22478. '});',
  22479. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  22480. ' this.Fly$1 = function () {',
  22481. ' $mod.TBird.Fly$1.call(this);',
  22482. ' $mod.TBird.Fly$1.call(this);',
  22483. ' };',
  22484. ' this.Walk$1 = function (w) {',
  22485. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  22486. ' $mod.TBirdHelper.Walk.call(this, 3);',
  22487. ' };',
  22488. '});',
  22489. '']),
  22490. LinesToStr([ // $mod.$main
  22491. '']));
  22492. end;
  22493. procedure TTestModule.TestClassHelper_Property;
  22494. begin
  22495. StartProgram(false);
  22496. Add([
  22497. 'type',
  22498. ' TObject = class',
  22499. ' FSize: word;',
  22500. ' function GetSpeed: word;',
  22501. ' procedure SetSpeed(Value: word);',
  22502. ' end;',
  22503. ' TObjHelper = class helper for TObject',
  22504. ' function GetLeft: word;',
  22505. ' procedure SetLeft(Value: word);',
  22506. ' property Size: word read FSize write FSize;',
  22507. ' property Speed: word read GetSpeed write SetSpeed;',
  22508. ' property Left: word read GetLeft write SetLeft;',
  22509. ' end;',
  22510. ' TBird = class',
  22511. ' property NotRight: word read GetLeft write SetLeft;',
  22512. ' procedure DoIt;',
  22513. ' end;',
  22514. 'var',
  22515. ' b: TBird;',
  22516. 'function Tobject.GetSpeed: word;',
  22517. 'begin',
  22518. ' Size:=Size+11;',
  22519. ' Speed:=Speed+12;',
  22520. ' Result:=Left+13;',
  22521. ' Left:=13;',
  22522. ' Left:=Left+13;',
  22523. ' Self.Size:=Self.Size+21;',
  22524. ' Self.Speed:=Self.Speed+22;',
  22525. ' Self.Left:=Self.Left+23;',
  22526. ' with Self do begin',
  22527. ' Size:=Size+31;',
  22528. ' Speed:=Speed+32;',
  22529. ' Left:=Left+33;',
  22530. ' end;',
  22531. 'end;',
  22532. 'procedure Tobject.SetSpeed(Value: word);',
  22533. 'begin',
  22534. 'end;',
  22535. 'function TObjHelper.GetLeft: word;',
  22536. 'begin',
  22537. ' Size:=Size+11;',
  22538. ' Speed:=Speed+12;',
  22539. ' Left:=Left+13;',
  22540. ' Self.Size:=Self.Size+21;',
  22541. ' Self.Speed:=Self.Speed+22;',
  22542. ' Self.Left:=Self.Left+23;',
  22543. ' with Self do begin',
  22544. ' Size:=Size+31;',
  22545. ' Speed:=Speed+32;',
  22546. ' Left:=Left+33;',
  22547. ' end;',
  22548. 'end;',
  22549. 'procedure TObjHelper.SetLeft(Value: word);',
  22550. 'begin',
  22551. 'end;',
  22552. 'procedure TBird.DoIt;',
  22553. 'begin',
  22554. ' NotRight:=NotRight+11;',
  22555. ' Self.NotRight:=Self.NotRight+21;',
  22556. ' with Self do begin',
  22557. ' NotRight:=NotRight+31;',
  22558. ' end;',
  22559. 'end;',
  22560. 'begin',
  22561. ' b.Size:=b.Size+11;',
  22562. ' b.Speed:=b.Speed+12;',
  22563. ' b.Left:=b.Left+13;',
  22564. ' b.NotRight:=b.NotRight+14;',
  22565. ' with b do begin',
  22566. ' Size:=Size+31;',
  22567. ' Speed:=Speed+32;',
  22568. ' Left:=Left+33;',
  22569. ' NotRight:=NotRight+34;',
  22570. ' end;',
  22571. '']);
  22572. ConvertProgram;
  22573. CheckSource('TestClassHelper_Property',
  22574. LinesToStr([ // statements
  22575. 'rtl.createClass(this, "TObject", null, function () {',
  22576. ' this.$init = function () {',
  22577. ' this.FSize = 0;',
  22578. ' };',
  22579. ' this.$final = function () {',
  22580. ' };',
  22581. ' this.GetSpeed = function () {',
  22582. ' var Result = 0;',
  22583. ' this.FSize = this.FSize + 11;',
  22584. ' this.SetSpeed(this.GetSpeed() + 12);',
  22585. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  22586. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  22587. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22588. ' this.FSize = this.FSize + 21;',
  22589. ' this.SetSpeed(this.GetSpeed() + 22);',
  22590. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22591. ' this.FSize = this.FSize + 31;',
  22592. ' this.SetSpeed(this.GetSpeed() + 32);',
  22593. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22594. ' return Result;',
  22595. ' };',
  22596. ' this.SetSpeed = function (Value) {',
  22597. ' };',
  22598. '});',
  22599. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22600. ' this.GetLeft = function () {',
  22601. ' var Result = 0;',
  22602. ' this.FSize = this.FSize + 11;',
  22603. ' this.SetSpeed(this.GetSpeed() + 12);',
  22604. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22605. ' this.FSize = this.FSize + 21;',
  22606. ' this.SetSpeed(this.GetSpeed() + 22);',
  22607. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22608. ' this.FSize = this.FSize + 31;',
  22609. ' this.SetSpeed(this.GetSpeed() + 32);',
  22610. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22611. ' return Result;',
  22612. ' };',
  22613. ' this.SetLeft = function (Value) {',
  22614. ' };',
  22615. '});',
  22616. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22617. ' this.DoIt = function () {',
  22618. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  22619. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  22620. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  22621. ' };',
  22622. '});',
  22623. 'this.b = null;',
  22624. '']),
  22625. LinesToStr([ // $mod.$main
  22626. '$mod.b.FSize = $mod.b.FSize + 11;',
  22627. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  22628. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  22629. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  22630. 'var $with = $mod.b;',
  22631. '$with.FSize = $with.FSize + 31;',
  22632. '$with.SetSpeed($with.GetSpeed() + 32);',
  22633. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  22634. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  22635. '']));
  22636. end;
  22637. procedure TTestModule.TestClassHelper_Property_Array;
  22638. begin
  22639. StartProgram(false);
  22640. Add([
  22641. 'type',
  22642. ' TObject = class',
  22643. ' function GetSpeed(Index: boolean): word;',
  22644. ' procedure SetSpeed(Index: boolean; Value: word);',
  22645. ' end;',
  22646. ' TObjHelper = class helper for TObject',
  22647. ' function GetSize(Index: boolean): word;',
  22648. ' procedure SetSize(Index: boolean; Value: word);',
  22649. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  22650. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22651. ' end;',
  22652. ' TBird = class',
  22653. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  22654. ' procedure DoIt;',
  22655. ' end;',
  22656. 'var',
  22657. ' b: TBird;',
  22658. 'function Tobject.GetSpeed(Index: boolean): word;',
  22659. 'begin',
  22660. ' Result:=Size[false];',
  22661. ' Size[true]:=Size[false]+11;',
  22662. ' Speed[true]:=Speed[false]+12;',
  22663. ' Self.Size[true]:=Self.Size[false]+21;',
  22664. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22665. ' with Self do begin',
  22666. ' Size[true]:=Size[false]+31;',
  22667. ' Speed[true]:=Speed[false]+32;',
  22668. ' end;',
  22669. 'end;',
  22670. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22671. 'begin',
  22672. 'end;',
  22673. 'function TObjHelper.GetSize(Index: boolean): word;',
  22674. 'begin',
  22675. ' Size[true]:=Size[false]+11;',
  22676. ' Speed[true]:=Speed[false]+12;',
  22677. ' Self.Size[true]:=Self.Size[false]+21;',
  22678. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22679. ' with Self do begin',
  22680. ' Size[true]:=Size[false]+31;',
  22681. ' Speed[true]:=Speed[false]+32;',
  22682. ' end;',
  22683. 'end;',
  22684. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22685. 'begin',
  22686. 'end;',
  22687. 'procedure TBird.DoIt;',
  22688. 'begin',
  22689. ' Items[true]:=Items[false]+11;',
  22690. ' Self.Items[true]:=Self.Items[false]+21;',
  22691. ' with Self do Items[true]:=Items[false]+31;',
  22692. 'end;',
  22693. 'begin',
  22694. ' b.Size[true]:=b.Size[false]+11;',
  22695. ' b.Speed[true]:=b.Speed[false]+12;',
  22696. ' b.Items[true]:=b.Items[false]+13;',
  22697. ' with b do begin',
  22698. ' Size[true]:=Size[false]+21;',
  22699. ' Speed[true]:=Speed[false]+22;',
  22700. ' Items[true]:=Items[false]+23;',
  22701. ' end;',
  22702. '']);
  22703. ConvertProgram;
  22704. CheckSource('TestClassHelper_Property_Array',
  22705. LinesToStr([ // statements
  22706. 'rtl.createClass(this, "TObject", null, function () {',
  22707. ' this.$init = function () {',
  22708. ' };',
  22709. ' this.$final = function () {',
  22710. ' };',
  22711. ' this.GetSpeed = function (Index) {',
  22712. ' var Result = 0;',
  22713. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  22714. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22715. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22716. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22717. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22718. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22719. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22720. ' return Result;',
  22721. ' };',
  22722. ' this.SetSpeed = function (Index, Value) {',
  22723. ' };',
  22724. '});',
  22725. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22726. ' this.GetSize = function (Index) {',
  22727. ' var Result = 0;',
  22728. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22729. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22730. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22731. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22732. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22733. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22734. ' return Result;',
  22735. ' };',
  22736. ' this.SetSize = function (Index, Value) {',
  22737. ' };',
  22738. '});',
  22739. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22740. ' this.DoIt = function () {',
  22741. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22742. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22743. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22744. ' };',
  22745. '});',
  22746. 'this.b = null;',
  22747. '']),
  22748. LinesToStr([ // $mod.$main
  22749. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  22750. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  22751. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  22752. 'var $with = $mod.b;',
  22753. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  22754. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  22755. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  22756. '']));
  22757. end;
  22758. procedure TTestModule.TestClassHelper_Property_Array_Default;
  22759. begin
  22760. StartProgram(false);
  22761. Add([
  22762. 'type',
  22763. ' TObject = class',
  22764. ' function GetSpeed(Index: boolean): word;',
  22765. ' procedure SetSpeed(Index: boolean; Value: word);',
  22766. ' end;',
  22767. ' TObjHelper = class helper for TObject',
  22768. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  22769. ' end;',
  22770. ' TBird = class',
  22771. ' end;',
  22772. ' TBirdHelper = class helper for TBird',
  22773. ' function GetSize(Index: word): boolean;',
  22774. ' procedure SetSize(Index: word; Value: boolean);',
  22775. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  22776. ' end;',
  22777. 'function Tobject.GetSpeed(Index: boolean): word;',
  22778. 'begin',
  22779. ' Self[true]:=Self[false]+1;',
  22780. 'end;',
  22781. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22782. 'begin',
  22783. 'end;',
  22784. 'function TBirdHelper.GetSize(Index: word): boolean;',
  22785. 'begin',
  22786. ' Self[1]:=not Self[2];',
  22787. 'end;',
  22788. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  22789. 'begin',
  22790. 'end;',
  22791. 'var',
  22792. ' o: TObject;',
  22793. ' b: TBird;',
  22794. 'begin',
  22795. ' o[true]:=o[false]+1;',
  22796. ' b[3]:=not b[4];',
  22797. '']);
  22798. ConvertProgram;
  22799. CheckSource('TestClassHelper_Property_Array_Default',
  22800. LinesToStr([ // statements
  22801. 'rtl.createClass(this, "TObject", null, function () {',
  22802. ' this.$init = function () {',
  22803. ' };',
  22804. ' this.$final = function () {',
  22805. ' };',
  22806. ' this.GetSpeed = function (Index) {',
  22807. ' var Result = 0;',
  22808. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  22809. ' return Result;',
  22810. ' };',
  22811. ' this.SetSpeed = function (Index, Value) {',
  22812. ' };',
  22813. '});',
  22814. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22815. '});',
  22816. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22817. '});',
  22818. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22819. ' this.GetSize = function (Index) {',
  22820. ' var Result = false;',
  22821. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  22822. ' return Result;',
  22823. ' };',
  22824. ' this.SetSize = function (Index, Value) {',
  22825. ' };',
  22826. '});',
  22827. 'this.o = null;',
  22828. 'this.b = null;',
  22829. '']),
  22830. LinesToStr([ // $mod.$main
  22831. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  22832. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  22833. '']));
  22834. end;
  22835. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  22836. begin
  22837. StartProgram(false);
  22838. Add([
  22839. 'type',
  22840. ' TObject = class',
  22841. ' end;',
  22842. ' TObjHelper = class helper for TObject',
  22843. ' function GetItems(Index: word): TObject;',
  22844. ' procedure SetItems(Index: word; Value: TObject);',
  22845. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  22846. ' end;',
  22847. 'function Tobjhelper.GetItems(Index: word): TObject;',
  22848. 'begin',
  22849. ' Self[1][2]:=Self[3][4];',
  22850. 'end;',
  22851. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  22852. 'begin',
  22853. 'end;',
  22854. 'var',
  22855. ' o: TObject;',
  22856. 'begin',
  22857. ' o[1][2]:=o[3][4];',
  22858. '']);
  22859. ConvertProgram;
  22860. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  22861. LinesToStr([ // statements
  22862. 'rtl.createClass(this, "TObject", null, function () {',
  22863. ' this.$init = function () {',
  22864. ' };',
  22865. ' this.$final = function () {',
  22866. ' };',
  22867. '});',
  22868. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22869. ' this.GetItems = function (Index) {',
  22870. ' var Result = null;',
  22871. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  22872. ' return Result;',
  22873. ' };',
  22874. ' this.SetItems = function (Index, Value) {',
  22875. ' };',
  22876. '});',
  22877. 'this.o = null;',
  22878. '']),
  22879. LinesToStr([ // $mod.$main
  22880. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  22881. '']));
  22882. end;
  22883. procedure TTestModule.TestClassHelper_ClassProperty;
  22884. begin
  22885. StartProgram(false);
  22886. Add([
  22887. 'type',
  22888. ' TObject = class',
  22889. ' class var FSize: word;',
  22890. ' class function GetSpeed: word;',
  22891. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  22892. ' end;',
  22893. ' TObjHelper = class helper for TObject',
  22894. ' class function GetLeft: word;',
  22895. ' class procedure SetLeft(Value: word);',
  22896. ' class property Size: word read FSize write FSize;',
  22897. ' class property Speed: word read GetSpeed write SetSpeed;',
  22898. ' class property Left: word read GetLeft write SetLeft;',
  22899. ' end;',
  22900. ' TBird = class',
  22901. ' class property NotRight: word read GetLeft write SetLeft;',
  22902. ' class procedure DoIt;',
  22903. ' end;',
  22904. ' TBirdClass = class of TBird;',
  22905. 'class function Tobject.GetSpeed: word;',
  22906. 'begin',
  22907. ' Size:=Size+11;',
  22908. ' Speed:=Speed+12;',
  22909. ' Left:=Left+13;',
  22910. ' Self.Size:=Self.Size+21;',
  22911. ' Self.Speed:=Self.Speed+22;',
  22912. ' Self.Left:=Self.Left+23;',
  22913. ' with Self do begin',
  22914. ' Size:=Size+31;',
  22915. ' Speed:=Speed+32;',
  22916. ' Left:=Left+33;',
  22917. ' end;',
  22918. 'end;',
  22919. 'class function TObjHelper.GetLeft: word;',
  22920. 'begin',
  22921. ' Size:=Size+11;',
  22922. ' Speed:=Speed+12;',
  22923. ' Left:=Left+13;',
  22924. ' Self.Size:=Self.Size+21;',
  22925. ' Self.Speed:=Self.Speed+22;',
  22926. ' Self.Left:=Self.Left+23;',
  22927. ' with Self do begin',
  22928. ' Size:=Size+31;',
  22929. ' Speed:=Speed+32;',
  22930. ' Left:=Left+33;',
  22931. ' end;',
  22932. 'end;',
  22933. 'class procedure TObjHelper.SetLeft(Value: word);',
  22934. 'begin',
  22935. 'end;',
  22936. 'class procedure TBird.DoIt;',
  22937. 'begin',
  22938. ' NotRight:=NotRight+11;',
  22939. ' Self.NotRight:=Self.NotRight+21;',
  22940. ' with Self do NotRight:=NotRight+31;',
  22941. 'end;',
  22942. 'var',
  22943. ' b: TBird;',
  22944. ' c: TBirdClass;',
  22945. 'begin',
  22946. ' b.Size:=b.Size+11;',
  22947. ' b.Speed:=b.Speed+12;',
  22948. ' b.Left:=b.Left+13;',
  22949. ' b.NotRight:=b.NotRight+14;',
  22950. ' with b do begin',
  22951. ' Size:=Size+31;',
  22952. ' Speed:=Speed+32;',
  22953. ' Left:=Left+33;',
  22954. ' NotRight:=NotRight+34;',
  22955. ' end;',
  22956. ' c.Size:=c.Size+11;',
  22957. ' c.Speed:=c.Speed+12;',
  22958. ' c.Left:=c.Left+13;',
  22959. ' c.NotRight:=c.NotRight+14;',
  22960. ' with c do begin',
  22961. ' Size:=Size+31;',
  22962. ' Speed:=Speed+32;',
  22963. ' Left:=Left+33;',
  22964. ' NotRight:=NotRight+34;',
  22965. ' end;',
  22966. ' tbird.Size:=tbird.Size+11;',
  22967. ' tbird.Speed:=tbird.Speed+12;',
  22968. ' tbird.Left:=tbird.Left+13;',
  22969. ' tbird.NotRight:=tbird.NotRight+14;',
  22970. ' with tbird do begin',
  22971. ' Size:=Size+31;',
  22972. ' Speed:=Speed+32;',
  22973. ' Left:=Left+33;',
  22974. ' NotRight:=NotRight+34;',
  22975. ' end;',
  22976. '']);
  22977. ConvertProgram;
  22978. CheckSource('TestClassHelper_ClassProperty',
  22979. LinesToStr([ // statements
  22980. 'rtl.createClass(this, "TObject", null, function () {',
  22981. ' this.FSize = 0;',
  22982. ' this.$init = function () {',
  22983. ' };',
  22984. ' this.$final = function () {',
  22985. ' };',
  22986. ' this.GetSpeed = function () {',
  22987. ' var Result = 0;',
  22988. ' $mod.TObject.FSize = this.FSize + 11;',
  22989. ' this.SetSpeed(this.GetSpeed() + 12);',
  22990. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22991. ' $mod.TObject.FSize = this.FSize + 21;',
  22992. ' this.SetSpeed(this.GetSpeed() + 22);',
  22993. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22994. ' $mod.TObject.FSize = this.FSize + 31;',
  22995. ' this.SetSpeed(this.GetSpeed() + 32);',
  22996. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22997. ' return Result;',
  22998. ' };',
  22999. '});',
  23000. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23001. ' this.GetLeft = function () {',
  23002. ' var Result = 0;',
  23003. ' $mod.TObject.FSize = this.FSize + 11;',
  23004. ' this.SetSpeed(this.GetSpeed() + 12);',
  23005. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23006. ' $mod.TObject.FSize = this.FSize + 21;',
  23007. ' this.SetSpeed(this.GetSpeed() + 22);',
  23008. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23009. ' $mod.TObject.FSize = this.FSize + 31;',
  23010. ' this.SetSpeed(this.GetSpeed() + 32);',
  23011. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23012. ' return Result;',
  23013. ' };',
  23014. ' this.SetLeft = function (Value) {',
  23015. ' };',
  23016. '});',
  23017. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23018. ' this.DoIt = function () {',
  23019. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23020. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23021. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23022. ' };',
  23023. '});',
  23024. 'this.b = null;',
  23025. 'this.c = null;',
  23026. '']),
  23027. LinesToStr([ // $mod.$main
  23028. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23029. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23030. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23031. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23032. 'var $with = $mod.b;',
  23033. '$mod.TObject.FSize = $with.FSize + 31;',
  23034. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23035. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23036. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23037. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23038. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23039. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23040. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23041. 'var $with1 = $mod.c;',
  23042. '$mod.TObject.FSize = $with1.FSize + 31;',
  23043. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23044. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23045. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23046. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23047. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23048. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23049. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23050. 'var $with2 = $mod.TBird;',
  23051. '$mod.TObject.FSize = $with2.FSize + 31;',
  23052. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23053. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23054. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23055. '']));
  23056. end;
  23057. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23058. begin
  23059. StartProgram(false);
  23060. Add([
  23061. 'type',
  23062. ' TObject = class',
  23063. ' class function GetSpeed: word; static;',
  23064. ' class procedure SetSpeed(Value: word); static;',
  23065. ' end;',
  23066. ' TObjHelper = class helper for TObject',
  23067. ' class function GetLeft: word; static;',
  23068. ' class procedure SetLeft(Value: word); static;',
  23069. ' class property Speed: word read GetSpeed write SetSpeed;',
  23070. ' class property Left: word read GetLeft write SetLeft;',
  23071. ' end;',
  23072. ' TBird = class',
  23073. ' class property NotRight: word read GetLeft write SetLeft;',
  23074. ' class procedure DoIt; static;',
  23075. ' class procedure DoSome;',
  23076. ' end;',
  23077. ' TBirdClass = class of TBird;',
  23078. 'class function Tobject.GetSpeed: word;',
  23079. 'begin',
  23080. ' Speed:=Speed+12;',
  23081. ' Left:=Left+13;',
  23082. 'end;',
  23083. 'class procedure TObject.SetSpeed(Value: word);',
  23084. 'begin',
  23085. 'end;',
  23086. 'class function TObjHelper.GetLeft: word;',
  23087. 'begin',
  23088. ' Speed:=Speed+12;',
  23089. ' Left:=Left+13;',
  23090. 'end;',
  23091. 'class procedure TObjHelper.SetLeft(Value: word);',
  23092. 'begin',
  23093. 'end;',
  23094. 'class procedure TBird.DoIt;',
  23095. 'begin',
  23096. ' NotRight:=NotRight+11;',
  23097. 'end;',
  23098. 'class procedure TBird.DoSome;',
  23099. 'begin',
  23100. ' Speed:=Speed+12;',
  23101. ' Left:=Left+13;',
  23102. ' Self.Speed:=Self.Speed+22;',
  23103. ' Self.Left:=Self.Left+23;',
  23104. ' with Self do begin',
  23105. ' Speed:=Speed+32;',
  23106. ' Left:=Left+33;',
  23107. ' end;',
  23108. ' NotRight:=NotRight+11;',
  23109. ' Self.NotRight:=Self.NotRight+21;',
  23110. ' with Self do NotRight:=NotRight+31;',
  23111. 'end;',
  23112. 'var',
  23113. ' b: TBird;',
  23114. ' c: TBirdClass;',
  23115. 'begin',
  23116. ' b.Speed:=b.Speed+12;',
  23117. ' b.Left:=b.Left+13;',
  23118. ' b.NotRight:=b.NotRight+14;',
  23119. ' with b do begin',
  23120. ' Speed:=Speed+32;',
  23121. ' Left:=Left+33;',
  23122. ' NotRight:=NotRight+34;',
  23123. ' end;',
  23124. ' c.Speed:=c.Speed+12;',
  23125. ' c.Left:=c.Left+13;',
  23126. ' c.NotRight:=c.NotRight+14;',
  23127. ' with c do begin',
  23128. ' Speed:=Speed+32;',
  23129. ' Left:=Left+33;',
  23130. ' NotRight:=NotRight+34;',
  23131. ' end;',
  23132. ' tbird.Speed:=tbird.Speed+12;',
  23133. ' tbird.Left:=tbird.Left+13;',
  23134. ' tbird.NotRight:=tbird.NotRight+14;',
  23135. ' with tbird do begin',
  23136. ' Speed:=Speed+32;',
  23137. ' Left:=Left+33;',
  23138. ' NotRight:=NotRight+34;',
  23139. ' end;',
  23140. '']);
  23141. ConvertProgram;
  23142. CheckSource('TestClassHelper_ClassPropertyStatic',
  23143. LinesToStr([ // statements
  23144. 'rtl.createClass(this, "TObject", null, function () {',
  23145. ' this.$init = function () {',
  23146. ' };',
  23147. ' this.$final = function () {',
  23148. ' };',
  23149. ' this.GetSpeed = function () {',
  23150. ' var Result = 0;',
  23151. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23152. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23153. ' return Result;',
  23154. ' };',
  23155. ' this.SetSpeed = function (Value) {',
  23156. ' };',
  23157. '});',
  23158. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23159. ' this.GetLeft = function () {',
  23160. ' var Result = 0;',
  23161. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23162. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23163. ' return Result;',
  23164. ' };',
  23165. ' this.SetLeft = function (Value) {',
  23166. ' };',
  23167. '});',
  23168. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23169. ' this.DoIt = function () {',
  23170. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23171. ' };',
  23172. ' this.DoSome = function () {',
  23173. ' this.SetSpeed(this.GetSpeed() + 12);',
  23174. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23175. ' this.SetSpeed(this.GetSpeed() + 22);',
  23176. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23177. ' this.SetSpeed(this.GetSpeed() + 32);',
  23178. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23179. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23180. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23181. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23182. ' };',
  23183. '});',
  23184. 'this.b = null;',
  23185. 'this.c = null;',
  23186. '']),
  23187. LinesToStr([ // $mod.$main
  23188. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23189. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23190. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23191. 'var $with = $mod.b;',
  23192. '$with.SetSpeed($with.GetSpeed() + 32);',
  23193. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23194. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23195. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23196. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23197. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23198. 'var $with1 = $mod.c;',
  23199. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23200. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23201. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23202. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23203. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23204. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23205. 'var $with2 = $mod.TBird;',
  23206. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23207. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23208. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23209. '']));
  23210. end;
  23211. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23212. begin
  23213. StartProgram(false);
  23214. Add([
  23215. 'type',
  23216. ' TObject = class',
  23217. ' class function GetSpeed(Index: boolean): word;',
  23218. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23219. ' end;',
  23220. ' TObjHelper = class helper for TObject',
  23221. ' class function GetSize(Index: boolean): word;',
  23222. ' class procedure SetSize(Index: boolean; Value: word);',
  23223. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23224. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23225. ' end;',
  23226. ' TBird = class',
  23227. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23228. ' class procedure DoIt;',
  23229. ' end;',
  23230. ' TBirdClass = class of TBird;',
  23231. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23232. 'begin',
  23233. ' Size[true]:=Size[false]+11;',
  23234. ' Speed[true]:=Speed[false]+12;',
  23235. ' Self.Size[true]:=Self.Size[false]+21;',
  23236. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23237. ' with Self do begin',
  23238. ' Size[true]:=Size[false]+31;',
  23239. ' Speed[true]:=Speed[false]+32;',
  23240. ' end;',
  23241. 'end;',
  23242. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23243. 'begin',
  23244. ' Size[true]:=Size[false]+11;',
  23245. ' Speed[true]:=Speed[false]+12;',
  23246. ' Self.Size[true]:=Self.Size[false]+21;',
  23247. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23248. ' with Self do begin',
  23249. ' Size[true]:=Size[false]+31;',
  23250. ' Speed[true]:=Speed[false]+32;',
  23251. ' end;',
  23252. 'end;',
  23253. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23254. 'begin',
  23255. 'end;',
  23256. 'class procedure TBird.DoIt;',
  23257. 'begin',
  23258. ' Items[true]:=Items[false]+11;',
  23259. ' Self.Items[true]:=Self.Items[false]+21;',
  23260. ' with Self do Items[true]:=Items[false]+31;',
  23261. 'end;',
  23262. 'var',
  23263. ' b: TBird;',
  23264. ' c: TBirdClass;',
  23265. 'begin',
  23266. ' b.Size[true]:=b.Size[false]+11;',
  23267. ' b.Speed[true]:=b.Speed[false]+12;',
  23268. ' b.Items[true]:=b.Items[false]+13;',
  23269. ' with b do begin',
  23270. ' Size[true]:=Size[false]+21;',
  23271. ' Speed[true]:=Speed[false]+22;',
  23272. ' Items[true]:=Items[false]+23;',
  23273. ' end;',
  23274. ' c.Size[true]:=c.Size[false]+11;',
  23275. ' c.Speed[true]:=c.Speed[false]+12;',
  23276. ' c.Items[true]:=c.Items[false]+13;',
  23277. ' with c do begin',
  23278. ' Size[true]:=Size[false]+21;',
  23279. ' Speed[true]:=Speed[false]+22;',
  23280. ' Items[true]:=Items[false]+23;',
  23281. ' end;',
  23282. ' TBird.Size[true]:=TBird.Size[false]+11;',
  23283. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  23284. ' TBird.Items[true]:=TBird.Items[false]+13;',
  23285. ' with TBird do begin',
  23286. ' Size[true]:=Size[false]+21;',
  23287. ' Speed[true]:=Speed[false]+22;',
  23288. ' Items[true]:=Items[false]+23;',
  23289. ' end;',
  23290. '']);
  23291. ConvertProgram;
  23292. CheckSource('TestClassHelper_ClassProperty_Array',
  23293. LinesToStr([ // statements
  23294. 'rtl.createClass(this, "TObject", null, function () {',
  23295. ' this.$init = function () {',
  23296. ' };',
  23297. ' this.$final = function () {',
  23298. ' };',
  23299. ' this.GetSpeed = function (Index) {',
  23300. ' var Result = 0;',
  23301. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23302. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23303. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23304. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23305. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23306. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23307. ' return Result;',
  23308. ' };',
  23309. '});',
  23310. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23311. ' this.GetSize = function (Index) {',
  23312. ' var Result = 0;',
  23313. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23314. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23315. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23316. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23317. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23318. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23319. ' return Result;',
  23320. ' };',
  23321. ' this.SetSize = function (Index, Value) {',
  23322. ' };',
  23323. '});',
  23324. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23325. ' this.DoIt = function () {',
  23326. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23327. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23328. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23329. ' };',
  23330. '});',
  23331. 'this.b = null;',
  23332. 'this.c = null;',
  23333. '']),
  23334. LinesToStr([ // $mod.$main
  23335. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  23336. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  23337. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  23338. 'var $with = $mod.b;',
  23339. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  23340. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  23341. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  23342. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  23343. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  23344. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  23345. 'var $with1 = $mod.c;',
  23346. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  23347. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  23348. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  23349. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  23350. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  23351. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  23352. 'var $with2 = $mod.TBird;',
  23353. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  23354. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  23355. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  23356. '']));
  23357. end;
  23358. procedure TTestModule.TestClassHelper_ForIn;
  23359. begin
  23360. StartProgram(false);
  23361. Add([
  23362. 'type',
  23363. ' TObject = class end;',
  23364. ' TItem = TObject;',
  23365. ' TEnumerator = class',
  23366. ' FCurrent: TItem;',
  23367. ' property Current: TItem read FCurrent;',
  23368. ' function MoveNext: boolean;',
  23369. ' end;',
  23370. ' TBird = class',
  23371. ' end;',
  23372. ' TBirdHelper = class helper for TBird',
  23373. ' function GetEnumerator: TEnumerator;',
  23374. ' end;',
  23375. 'function TEnumerator.MoveNext: boolean;',
  23376. 'begin',
  23377. 'end;',
  23378. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  23379. 'begin',
  23380. 'end;',
  23381. 'var',
  23382. ' b: TBird;',
  23383. ' i, i2: TItem;',
  23384. 'begin',
  23385. ' for i in b do i2:=i;']);
  23386. ConvertProgram;
  23387. CheckSource('TestClassHelper_ForIn',
  23388. LinesToStr([ // statements
  23389. 'rtl.createClass(this, "TObject", null, function () {',
  23390. ' this.$init = function () {',
  23391. ' };',
  23392. ' this.$final = function () {',
  23393. ' };',
  23394. '});',
  23395. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  23396. ' this.$init = function () {',
  23397. ' $mod.TObject.$init.call(this);',
  23398. ' this.FCurrent = null;',
  23399. ' };',
  23400. ' this.$final = function () {',
  23401. ' this.FCurrent = undefined;',
  23402. ' $mod.TObject.$final.call(this);',
  23403. ' };',
  23404. ' this.MoveNext = function () {',
  23405. ' var Result = false;',
  23406. ' return Result;',
  23407. ' };',
  23408. '});',
  23409. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23410. '});',
  23411. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23412. ' this.GetEnumerator = function () {',
  23413. ' var Result = null;',
  23414. ' return Result;',
  23415. ' };',
  23416. '});',
  23417. 'this.b = null;',
  23418. 'this.i = null;',
  23419. 'this.i2 = null;'
  23420. ]),
  23421. LinesToStr([ // $mod.$main
  23422. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  23423. 'try {',
  23424. ' while ($in.MoveNext()){',
  23425. ' $mod.i = $in.FCurrent;',
  23426. ' $mod.i2 = $mod.i;',
  23427. ' }',
  23428. '} finally {',
  23429. ' $in = rtl.freeLoc($in)',
  23430. '};',
  23431. '']));
  23432. end;
  23433. procedure TTestModule.TestClassHelper_PassProperty;
  23434. begin
  23435. StartProgram(false);
  23436. Add([
  23437. 'type',
  23438. ' TObject = class',
  23439. ' FField: TObject;',
  23440. ' property Field: TObject read FField write FField;',
  23441. ' end;',
  23442. ' THelper = class helper for TObject',
  23443. ' procedure Fly;',
  23444. ' class procedure Run;',
  23445. ' class procedure Jump; static;',
  23446. ' end;',
  23447. 'procedure THelper.Fly;',
  23448. 'begin',
  23449. ' Field.Fly;',
  23450. ' Field.Run;',
  23451. ' Field.Jump;',
  23452. ' with Field do begin',
  23453. ' Fly;',
  23454. ' Run;',
  23455. ' Jump;',
  23456. ' end;',
  23457. 'end;',
  23458. 'class procedure THelper.Run;',
  23459. 'begin',
  23460. 'end;',
  23461. 'class procedure THelper.Jump;',
  23462. 'begin',
  23463. 'end;',
  23464. 'var',
  23465. ' b: TObject;',
  23466. 'begin',
  23467. ' b.Field.Fly;',
  23468. ' b.Field.Run;',
  23469. ' b.Field.Jump;',
  23470. ' with b do begin',
  23471. ' Field.Run;',
  23472. ' Field.Fly;',
  23473. ' Field.Jump;',
  23474. ' end;',
  23475. ' with b.Field do begin',
  23476. ' Run;',
  23477. ' Fly;',
  23478. ' Jump;',
  23479. ' end;',
  23480. '']);
  23481. ConvertProgram;
  23482. CheckSource('TestClassHelper_PassProperty',
  23483. LinesToStr([ // statements
  23484. 'rtl.createClass(this, "TObject", null, function () {',
  23485. ' this.$init = function () {',
  23486. ' this.FField = null;',
  23487. ' };',
  23488. ' this.$final = function () {',
  23489. ' this.FField = undefined;',
  23490. ' };',
  23491. '});',
  23492. 'rtl.createHelper(this, "THelper", null, function () {',
  23493. ' this.Fly = function () {',
  23494. ' $mod.THelper.Fly.call(this.FField);',
  23495. ' $mod.THelper.Run.call(this.FField.$class);',
  23496. ' $mod.THelper.Jump();',
  23497. ' var $with = this.FField;',
  23498. ' $mod.THelper.Fly.call($with);',
  23499. ' $mod.THelper.Run.call($with.$class);',
  23500. ' $mod.THelper.Jump();',
  23501. ' };',
  23502. ' this.Run = function () {',
  23503. ' };',
  23504. ' this.Jump = function () {',
  23505. ' };',
  23506. '});',
  23507. 'this.b = null;',
  23508. '']),
  23509. LinesToStr([ // $mod.$main
  23510. '$mod.THelper.Fly.call($mod.b.FField);',
  23511. '$mod.THelper.Run.call($mod.b.FField.$class);',
  23512. '$mod.THelper.Jump();',
  23513. 'var $with = $mod.b;',
  23514. '$mod.THelper.Run.call($with.FField.$class);',
  23515. '$mod.THelper.Fly.call($with.FField);',
  23516. '$mod.THelper.Jump();',
  23517. 'var $with1 = $mod.b.FField;',
  23518. '$mod.THelper.Run.call($with1.$class);',
  23519. '$mod.THelper.Fly.call($with1);',
  23520. '$mod.THelper.Jump();',
  23521. '']));
  23522. end;
  23523. procedure TTestModule.TestExtClassHelper_ClassVar;
  23524. begin
  23525. StartProgram(false);
  23526. Add([
  23527. '{$modeswitch externalclass}',
  23528. 'type',
  23529. ' TExtA = class external name ''ExtObj''',
  23530. ' end;',
  23531. ' THelper = class helper for TExtA',
  23532. ' const',
  23533. ' One = 1;',
  23534. ' Two: word = 2;',
  23535. ' class var',
  23536. ' Glob: word;',
  23537. ' function Foo(w: word): word;',
  23538. ' class function Bar(w: word): word; static;',
  23539. ' end;',
  23540. 'function THelper.foo(w: word): word;',
  23541. 'begin',
  23542. ' Result:=w;',
  23543. ' Two:=One+w;',
  23544. ' Glob:=Glob;',
  23545. ' Result:=Self.Glob;',
  23546. ' Self.Glob:=Self.Glob;',
  23547. ' with Self do Glob:=Glob;',
  23548. 'end;',
  23549. 'class function THelper.bar(w: word): word;',
  23550. 'begin',
  23551. ' Result:=w;',
  23552. ' Two:=One;',
  23553. ' Glob:=Glob;',
  23554. 'end;',
  23555. 'var o: TExtA;',
  23556. 'begin',
  23557. ' texta.two:=texta.one;',
  23558. ' texta.Glob:=texta.Glob;',
  23559. ' with texta do begin',
  23560. ' two:=one;',
  23561. ' Glob:=Glob;',
  23562. ' end;',
  23563. ' o.two:=o.one;',
  23564. ' o.Glob:=o.Glob;',
  23565. ' with o do begin',
  23566. ' two:=one;',
  23567. ' Glob:=Glob;',
  23568. ' end;',
  23569. '']);
  23570. ConvertProgram;
  23571. CheckSource('TestExtClassHelper_ClassVar',
  23572. LinesToStr([ // statements
  23573. 'rtl.createHelper(this, "THelper", null, function () {',
  23574. ' this.One = 1;',
  23575. ' this.Two = 2;',
  23576. ' this.Glob = 0;',
  23577. ' this.Foo = function (w) {',
  23578. ' var Result = 0;',
  23579. ' Result = w;',
  23580. ' $mod.THelper.Two = 1 + w;',
  23581. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23582. ' Result = $mod.THelper.Glob;',
  23583. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23584. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23585. ' return Result;',
  23586. ' };',
  23587. ' this.Bar = function (w) {',
  23588. ' var Result = 0;',
  23589. ' Result = w;',
  23590. ' $mod.THelper.Two = 1;',
  23591. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23592. ' return Result;',
  23593. ' };',
  23594. '});',
  23595. 'this.o = null;',
  23596. '']),
  23597. LinesToStr([ // $mod.$main
  23598. '$mod.THelper.Two = 1;',
  23599. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23600. '$mod.THelper.Two = 1;',
  23601. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23602. '$mod.THelper.Two = 1;',
  23603. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23604. 'var $with = $mod.o;',
  23605. '$mod.THelper.Two = 1;',
  23606. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23607. '']));
  23608. end;
  23609. procedure TTestModule.TestExtClassHelper_Method_Call;
  23610. begin
  23611. StartProgram(false);
  23612. Add([
  23613. '{$modeswitch externalclass}',
  23614. 'type',
  23615. ' TFly = function(w: word): word of object;',
  23616. ' TExtA = class external name ''ExtObj''',
  23617. ' procedure Run(w: word = 10);',
  23618. ' end;',
  23619. ' THelper = class helper for TExtA',
  23620. ' function Foo(w: word = 1): word;',
  23621. ' function Fly(w: word = 2): word; external name ''Fly'';',
  23622. ' end;',
  23623. 'var p: TFly;',
  23624. 'function THelper.foo(w: word): word;',
  23625. 'begin',
  23626. ' Run;',
  23627. ' Run();',
  23628. ' Run(11);',
  23629. ' Foo;',
  23630. ' Foo();',
  23631. ' Foo(12);',
  23632. ' Self.Foo;',
  23633. ' Self.Foo();',
  23634. ' Self.Foo(13);',
  23635. ' Fly;',
  23636. ' Fly();',
  23637. ' with Self do begin',
  23638. ' Foo;',
  23639. ' Foo();',
  23640. ' Foo(14);',
  23641. ' Fly;',
  23642. ' Fly();',
  23643. ' end;',
  23644. ' p:=@Fly;',
  23645. 'end;',
  23646. 'var Obj: TExtA;',
  23647. 'begin',
  23648. ' obj.Foo;',
  23649. ' obj.Foo();',
  23650. ' obj.Foo(21);',
  23651. ' obj.Fly;',
  23652. ' obj.Fly();',
  23653. ' with obj do begin',
  23654. ' Foo;',
  23655. ' Foo();',
  23656. ' Foo(22);',
  23657. ' Fly;',
  23658. ' Fly();',
  23659. ' end;',
  23660. ' p:[email protected];',
  23661. '']);
  23662. ConvertProgram;
  23663. CheckSource('TestExtClassHelper_Method_Call',
  23664. LinesToStr([ // statements
  23665. 'rtl.createHelper(this, "THelper", null, function () {',
  23666. ' this.Foo = function (w) {',
  23667. ' var Result = 0;',
  23668. ' this.Run(10);',
  23669. ' this.Run(10);',
  23670. ' this.Run(11);',
  23671. ' $mod.THelper.Foo.call(this, 1);',
  23672. ' $mod.THelper.Foo.call(this, 1);',
  23673. ' $mod.THelper.Foo.call(this, 12);',
  23674. ' $mod.THelper.Foo.call(this, 1);',
  23675. ' $mod.THelper.Foo.call(this, 1);',
  23676. ' $mod.THelper.Foo.call(this, 13);',
  23677. ' this.Fly(2);',
  23678. ' this.Fly(2);',
  23679. ' $mod.THelper.Foo.call(this, 1);',
  23680. ' $mod.THelper.Foo.call(this, 1);',
  23681. ' $mod.THelper.Foo.call(this, 14);',
  23682. ' this.Fly(2);',
  23683. ' this.Fly(2);',
  23684. ' $mod.p = rtl.createCallback(this, "Fly");',
  23685. ' return Result;',
  23686. ' };',
  23687. '});',
  23688. 'this.p = null;',
  23689. 'this.Obj = null;',
  23690. '']),
  23691. LinesToStr([ // $mod.$main
  23692. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23693. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23694. '$mod.THelper.Foo.call($mod.Obj, 21);',
  23695. '$mod.Obj.Fly(2);',
  23696. '$mod.Obj.Fly(2);',
  23697. 'var $with = $mod.Obj;',
  23698. '$mod.THelper.Foo.call($with, 1);',
  23699. '$mod.THelper.Foo.call($with, 1);',
  23700. '$mod.THelper.Foo.call($with, 22);',
  23701. '$with.Fly(2);',
  23702. '$with.Fly(2);',
  23703. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  23704. '']));
  23705. end;
  23706. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  23707. begin
  23708. StartProgram(false);
  23709. Add([
  23710. '{$modeswitch externalclass}',
  23711. 'type',
  23712. ' TExtA = class external name ''ExtObj''',
  23713. ' procedure Run(w: word = 10);',
  23714. ' end;',
  23715. ' THelper = class helper for TExtA',
  23716. ' class procedure Fly;',
  23717. ' end;',
  23718. 'class procedure THelper.Fly;',
  23719. 'begin end;',
  23720. 'begin',
  23721. '']);
  23722. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  23723. nHelperClassMethodForExtClassMustBeStatic);
  23724. ConvertProgram;
  23725. end;
  23726. procedure TTestModule.TestRecordHelper_ClassVar;
  23727. begin
  23728. StartProgram(false);
  23729. Add([
  23730. 'type',
  23731. ' TRec = record',
  23732. ' end;',
  23733. ' THelper = record helper for TRec',
  23734. ' const',
  23735. ' One = 1;',
  23736. ' Two: word = 2;',
  23737. ' class var',
  23738. ' Glob: word;',
  23739. ' function Foo(w: word): word;',
  23740. ' class function Bar(w: word): word; static;',
  23741. ' end;',
  23742. 'function THelper.foo(w: word): word;',
  23743. 'begin',
  23744. ' Result:=w;',
  23745. ' Two:=One+w;',
  23746. ' Glob:=Glob;',
  23747. ' Result:=Self.Glob;',
  23748. ' Self.Glob:=Self.Glob;',
  23749. ' with Self do Glob:=Glob;',
  23750. ' Self:=Self;',
  23751. 'end;',
  23752. 'class function THelper.bar(w: word): word;',
  23753. 'begin',
  23754. ' Result:=w;',
  23755. ' Two:=One;',
  23756. ' Glob:=Glob;',
  23757. 'end;',
  23758. 'var r: TRec;',
  23759. 'begin',
  23760. ' trec.two:=trec.one;',
  23761. ' trec.Glob:=trec.Glob;',
  23762. ' with trec do begin',
  23763. ' two:=one;',
  23764. ' Glob:=Glob;',
  23765. ' end;',
  23766. ' r.two:=r.one;',
  23767. ' r.Glob:=r.Glob;',
  23768. ' with r do begin',
  23769. ' two:=one;',
  23770. ' Glob:=Glob;',
  23771. ' end;',
  23772. '']);
  23773. ConvertProgram;
  23774. CheckSource('TestRecordHelper_ClassVar',
  23775. LinesToStr([ // statements
  23776. 'rtl.recNewT(this, "TRec", function () {',
  23777. ' this.$eq = function (b) {',
  23778. ' return true;',
  23779. ' };',
  23780. ' this.$assign = function (s) {',
  23781. ' return this;',
  23782. ' };',
  23783. '});',
  23784. 'rtl.createHelper(this, "THelper", null, function () {',
  23785. ' this.One = 1;',
  23786. ' this.Two = 2;',
  23787. ' this.Glob = 0;',
  23788. ' this.Foo = function (w) {',
  23789. ' var Result = 0;',
  23790. ' Result = w;',
  23791. ' $mod.THelper.Two = 1 + w;',
  23792. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23793. ' Result = $mod.THelper.Glob;',
  23794. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23795. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23796. ' this.$assign(this);',
  23797. ' return Result;',
  23798. ' };',
  23799. ' this.Bar = function (w) {',
  23800. ' var Result = 0;',
  23801. ' Result = w;',
  23802. ' $mod.THelper.Two = 1;',
  23803. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23804. ' return Result;',
  23805. ' };',
  23806. '});',
  23807. 'this.r = this.TRec.$new();',
  23808. '']),
  23809. LinesToStr([ // $mod.$main
  23810. '$mod.THelper.Two = 1;',
  23811. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23812. 'var $with = $mod.TRec;',
  23813. '$mod.THelper.Two = 1;',
  23814. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23815. '$mod.THelper.Two = 1;',
  23816. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23817. 'var $with1 = $mod.r;',
  23818. '$mod.THelper.Two = 1;',
  23819. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23820. '']));
  23821. end;
  23822. procedure TTestModule.TestRecordHelper_Method_Call;
  23823. begin
  23824. StartProgram(false);
  23825. Add([
  23826. '{$modeswitch AdvancedRecords}',
  23827. 'type',
  23828. ' TRec = record',
  23829. ' procedure Run(w: word = 10);',
  23830. ' end;',
  23831. ' THelper = record helper for TRec',
  23832. ' function Foo(w: word = 1): word;',
  23833. ' end;',
  23834. 'procedure TRec.Run(w: word);',
  23835. 'begin',
  23836. ' Foo;',
  23837. ' Foo();',
  23838. ' Foo(2);',
  23839. ' Self.Foo;',
  23840. ' Self.Foo();',
  23841. ' Self.Foo(3);',
  23842. ' with Self do begin',
  23843. ' Foo;',
  23844. ' Foo();',
  23845. ' Foo(4);',
  23846. ' end;',
  23847. 'end;',
  23848. 'function THelper.foo(w: word): word;',
  23849. 'begin',
  23850. ' Run;',
  23851. ' Run();',
  23852. ' Run(11);',
  23853. ' Foo;',
  23854. ' Foo();',
  23855. ' Foo(12);',
  23856. ' Self.Foo;',
  23857. ' Self.Foo();',
  23858. ' Self.Foo(13);',
  23859. ' with Self do begin',
  23860. ' Foo;',
  23861. ' Foo();',
  23862. ' Foo(14);',
  23863. ' end;',
  23864. 'end;',
  23865. 'var Rec: TRec;',
  23866. 'begin',
  23867. ' Rec.Foo;',
  23868. ' Rec.Foo();',
  23869. ' Rec.Foo(21);',
  23870. ' with Rec do begin',
  23871. ' Foo;',
  23872. ' Foo();',
  23873. ' Foo(22);',
  23874. ' end;',
  23875. '']);
  23876. ConvertProgram;
  23877. CheckSource('TestRecordHelper_Method_Call',
  23878. LinesToStr([ // statements
  23879. 'rtl.recNewT(this, "TRec", function () {',
  23880. ' this.$eq = function (b) {',
  23881. ' return true;',
  23882. ' };',
  23883. ' this.$assign = function (s) {',
  23884. ' return this;',
  23885. ' };',
  23886. ' this.Run = function (w) {',
  23887. ' $mod.THelper.Foo.call(this, 1);',
  23888. ' $mod.THelper.Foo.call(this, 1);',
  23889. ' $mod.THelper.Foo.call(this, 2);',
  23890. ' $mod.THelper.Foo.call(this, 1);',
  23891. ' $mod.THelper.Foo.call(this, 1);',
  23892. ' $mod.THelper.Foo.call(this, 3);',
  23893. ' $mod.THelper.Foo.call(this, 1);',
  23894. ' $mod.THelper.Foo.call(this, 1);',
  23895. ' $mod.THelper.Foo.call(this, 4);',
  23896. ' };',
  23897. '});',
  23898. 'rtl.createHelper(this, "THelper", null, function () {',
  23899. ' this.Foo = function (w) {',
  23900. ' var Result = 0;',
  23901. ' this.Run(10);',
  23902. ' this.Run(10);',
  23903. ' this.Run(11);',
  23904. ' $mod.THelper.Foo.call(this, 1);',
  23905. ' $mod.THelper.Foo.call(this, 1);',
  23906. ' $mod.THelper.Foo.call(this, 12);',
  23907. ' $mod.THelper.Foo.call(this, 1);',
  23908. ' $mod.THelper.Foo.call(this, 1);',
  23909. ' $mod.THelper.Foo.call(this, 13);',
  23910. ' $mod.THelper.Foo.call(this, 1);',
  23911. ' $mod.THelper.Foo.call(this, 1);',
  23912. ' $mod.THelper.Foo.call(this, 14);',
  23913. ' return Result;',
  23914. ' };',
  23915. '});',
  23916. 'this.Rec = this.TRec.$new();',
  23917. '']),
  23918. LinesToStr([ // $mod.$main
  23919. '$mod.THelper.Foo.call($mod.Rec, 1);',
  23920. '$mod.THelper.Foo.call($mod.Rec, 1);',
  23921. '$mod.THelper.Foo.call($mod.Rec, 21);',
  23922. 'var $with = $mod.Rec;',
  23923. '$mod.THelper.Foo.call($with, 1);',
  23924. '$mod.THelper.Foo.call($with, 1);',
  23925. '$mod.THelper.Foo.call($with, 22);',
  23926. '']));
  23927. end;
  23928. procedure TTestModule.TestRecordHelper_Constructor;
  23929. begin
  23930. StartProgram(false);
  23931. Add([
  23932. '{$modeswitch AdvancedRecords}',
  23933. 'type',
  23934. ' TRec = record',
  23935. ' constructor Create(w: word);',
  23936. ' end;',
  23937. ' THelper = record helper for TRec',
  23938. ' constructor NewHlp(w: word);',
  23939. ' end;',
  23940. 'var',
  23941. ' Rec: TRec;',
  23942. 'constructor TRec.Create(w: word);',
  23943. 'begin',
  23944. ' NewHlp(2);', // normal call
  23945. ' trec.NewHlp(3);', // new instance
  23946. 'end;',
  23947. 'constructor THelper.NewHlp(w: word);',
  23948. 'begin',
  23949. ' create(2);', // normal call
  23950. ' trec.create(3);', // new instance
  23951. ' NewHlp(4);', // normal call
  23952. ' trec.NewHlp(5);', // new instance
  23953. 'end;',
  23954. 'begin',
  23955. ' rec.newhlp(2);', // normal call
  23956. ' with rec do newhlp(12);', // normal call
  23957. ' trec.newhlp(3);', // new instance
  23958. ' with trec do newhlp(13);', // new instance
  23959. '']);
  23960. ConvertProgram;
  23961. CheckSource('TestRecordHelper_Constructor',
  23962. LinesToStr([ // statements
  23963. 'rtl.recNewT(this, "TRec", function () {',
  23964. ' this.$eq = function (b) {',
  23965. ' return true;',
  23966. ' };',
  23967. ' this.$assign = function (s) {',
  23968. ' return this;',
  23969. ' };',
  23970. ' this.Create = function (w) {',
  23971. ' $mod.THelper.NewHlp.call(this, 2);',
  23972. ' $mod.THelper.$new("NewHlp", [3]);',
  23973. ' return this;',
  23974. ' };',
  23975. '});',
  23976. 'rtl.createHelper(this, "THelper", null, function () {',
  23977. ' this.NewHlp = function (w) {',
  23978. ' this.Create(2);',
  23979. ' $mod.TRec.$new().Create(3);',
  23980. ' $mod.THelper.NewHlp.call(this, 4);',
  23981. ' $mod.THelper.$new("NewHlp", [5]);',
  23982. ' return this;',
  23983. ' };',
  23984. ' this.$new = function (fn, args) {',
  23985. ' return this[fn].apply($mod.TRec.$new(), args);',
  23986. ' };',
  23987. '});',
  23988. 'this.Rec = this.TRec.$new();',
  23989. '']),
  23990. LinesToStr([ // $mod.$main
  23991. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  23992. 'var $with = $mod.Rec;',
  23993. '$mod.THelper.NewHlp.call($with, 12);',
  23994. '$mod.THelper.$new("NewHlp", [3]);',
  23995. 'var $with1 = $mod.TRec;',
  23996. '$mod.THelper.$new("NewHlp", [13]);',
  23997. '']));
  23998. end;
  23999. procedure TTestModule.TestTypeHelper_ClassVar;
  24000. begin
  24001. StartProgram(false);
  24002. Add([
  24003. '{$modeswitch typehelpers}',
  24004. 'type',
  24005. ' THelper = type helper for byte',
  24006. ' const',
  24007. ' One = 1;',
  24008. ' Two: word = 2;',
  24009. ' class var',
  24010. ' Glob: word;',
  24011. ' function Foo(w: word): word;',
  24012. ' class function Bar(w: word): word; static;',
  24013. ' end;',
  24014. 'function THelper.foo(w: word): word;',
  24015. 'begin',
  24016. ' Result:=w;',
  24017. ' Two:=One+w;',
  24018. ' Glob:=Glob;',
  24019. ' Result:=Self.Glob;',
  24020. ' Self.Glob:=Self.Glob;',
  24021. ' with Self do Glob:=Glob;',
  24022. 'end;',
  24023. 'class function THelper.bar(w: word): word;',
  24024. 'begin',
  24025. ' Result:=w;',
  24026. ' Two:=One;',
  24027. ' Glob:=Glob;',
  24028. 'end;',
  24029. 'var b: byte;',
  24030. 'begin',
  24031. ' byte.two:=byte.one;',
  24032. ' byte.Glob:=byte.Glob;',
  24033. ' with byte do begin',
  24034. ' two:=one;',
  24035. ' Glob:=Glob;',
  24036. ' end;',
  24037. ' b.two:=b.one;',
  24038. ' b.Glob:=b.Glob;',
  24039. ' with b do begin',
  24040. ' two:=one;',
  24041. ' Glob:=Glob;',
  24042. ' end;',
  24043. '']);
  24044. ConvertProgram;
  24045. CheckSource('TestTypeHelper_ClassVar',
  24046. LinesToStr([ // statements
  24047. 'rtl.createHelper(this, "THelper", null, function () {',
  24048. ' this.One = 1;',
  24049. ' this.Two = 2;',
  24050. ' this.Glob = 0;',
  24051. ' this.Foo = function (w) {',
  24052. ' var Result = 0;',
  24053. ' Result = w;',
  24054. ' $mod.THelper.Two = 1 + w;',
  24055. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24056. ' Result = $mod.THelper.Glob;',
  24057. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24058. ' var $with = this.get();',
  24059. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24060. ' return Result;',
  24061. ' };',
  24062. ' this.Bar = function (w) {',
  24063. ' var Result = 0;',
  24064. ' Result = w;',
  24065. ' $mod.THelper.Two = 1;',
  24066. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24067. ' return Result;',
  24068. ' };',
  24069. '});',
  24070. 'this.b = 0;',
  24071. '']),
  24072. LinesToStr([ // $mod.$main
  24073. '$mod.THelper.Two = 1;',
  24074. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24075. '$mod.THelper.Two = 1;',
  24076. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24077. '$mod.THelper.Two = 1;',
  24078. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24079. 'var $with = $mod.b;',
  24080. '$mod.THelper.Two = 1;',
  24081. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24082. '']));
  24083. end;
  24084. procedure TTestModule.TestTypeHelper_PassResultElement;
  24085. begin
  24086. StartProgram(false);
  24087. Add([
  24088. '{$modeswitch typehelpers}',
  24089. 'type',
  24090. ' THelper = type helper for word',
  24091. ' procedure DoIt(e: byte = 123);',
  24092. ' class procedure DoSome(e: byte = 456); static;',
  24093. ' end;',
  24094. 'procedure THelper.DoIt(e: byte);',
  24095. 'begin',
  24096. 'end;',
  24097. 'class procedure THelper.DoSome(e: byte);',
  24098. 'begin',
  24099. 'end;',
  24100. 'function Foo(w: word): word;',
  24101. 'begin',
  24102. ' Result.DoIt;',
  24103. ' Result.DoIt();',
  24104. ' Result.DoSome;',
  24105. ' Result.DoSome();',
  24106. ' with Result do begin',
  24107. ' DoIt;',
  24108. ' DoIt();',
  24109. ' DoSome;',
  24110. ' DoSome();',
  24111. ' end;',
  24112. 'end;',
  24113. 'begin',
  24114. '']);
  24115. ConvertProgram;
  24116. CheckSource('TestTypeHelper_PassResultElement',
  24117. LinesToStr([ // statements
  24118. 'rtl.createHelper(this, "THelper", null, function () {',
  24119. ' this.DoIt = function (e) {',
  24120. ' };',
  24121. ' this.DoSome = function (e) {',
  24122. ' };',
  24123. '});',
  24124. 'this.Foo = function (w) {',
  24125. ' var Result = 0;',
  24126. ' $mod.THelper.DoIt.call({',
  24127. ' get: function () {',
  24128. ' return Result;',
  24129. ' },',
  24130. ' set: function (v) {',
  24131. ' Result = v;',
  24132. ' }',
  24133. ' }, 123);',
  24134. ' $mod.THelper.DoIt.call({',
  24135. ' get: function () {',
  24136. ' return Result;',
  24137. ' },',
  24138. ' set: function (v) {',
  24139. ' Result = v;',
  24140. ' }',
  24141. ' }, 123);',
  24142. ' $mod.THelper.DoSome(456);',
  24143. ' $mod.THelper.DoSome(456);',
  24144. ' $mod.THelper.DoIt.call({',
  24145. ' get: function () {',
  24146. ' return Result;',
  24147. ' },',
  24148. ' set: function (v) {',
  24149. ' Result = v;',
  24150. ' }',
  24151. ' }, 123);',
  24152. ' $mod.THelper.DoIt.call({',
  24153. ' get: function () {',
  24154. ' return Result;',
  24155. ' },',
  24156. ' set: function (v) {',
  24157. ' Result = v;',
  24158. ' }',
  24159. ' }, 123);',
  24160. ' $mod.THelper.DoSome(456);',
  24161. ' $mod.THelper.DoSome(456);',
  24162. ' return Result;',
  24163. '};',
  24164. '']),
  24165. LinesToStr([ // $mod.$main
  24166. '']));
  24167. end;
  24168. procedure TTestModule.TestTypeHelper_PassArgs;
  24169. begin
  24170. StartProgram(false);
  24171. Add([
  24172. '{$modeswitch typehelpers}',
  24173. 'type',
  24174. ' THelper = type helper for word',
  24175. ' procedure DoIt(e: byte = 123);',
  24176. ' end;',
  24177. 'procedure THelper.DoIt(e: byte);',
  24178. 'begin',
  24179. 'end;',
  24180. 'procedure FooDefault(a: word);',
  24181. 'begin',
  24182. ' a.DoIt;',
  24183. ' with a do DoIt;',
  24184. 'end;',
  24185. 'procedure FooConst(const a: word);',
  24186. 'begin',
  24187. ' a.DoIt;',
  24188. ' with a do DoIt;',
  24189. 'end;',
  24190. 'procedure FooVar(var a: word);',
  24191. 'begin',
  24192. ' a.DoIt;',
  24193. ' with a do DoIt;',
  24194. 'end;',
  24195. 'begin',
  24196. '']);
  24197. ConvertProgram;
  24198. CheckSource('TestTypeHelper_PassArgs',
  24199. LinesToStr([ // statements
  24200. 'rtl.createHelper(this, "THelper", null, function () {',
  24201. ' this.DoIt = function (e) {',
  24202. ' };',
  24203. '});',
  24204. 'this.FooDefault = function (a) {',
  24205. ' $mod.THelper.DoIt.call({',
  24206. ' get: function () {',
  24207. ' return a;',
  24208. ' },',
  24209. ' set: function (v) {',
  24210. ' a = v;',
  24211. ' }',
  24212. ' }, 123);',
  24213. ' $mod.THelper.DoIt.call({',
  24214. ' get: function () {',
  24215. ' return a;',
  24216. ' },',
  24217. ' set: function (v) {',
  24218. ' a = v;',
  24219. ' }',
  24220. ' }, 123);',
  24221. '};',
  24222. 'this.FooConst = function (a) {',
  24223. ' $mod.THelper.DoIt.call({',
  24224. ' get: function () {',
  24225. ' return a;',
  24226. ' },',
  24227. ' set: function (v) {',
  24228. ' rtl.raiseE("EPropReadOnly");',
  24229. ' }',
  24230. ' }, 123);',
  24231. ' $mod.THelper.DoIt.call({',
  24232. ' get: function () {',
  24233. ' return a;',
  24234. ' },',
  24235. ' set: function () {',
  24236. ' rtl.raiseE("EPropReadOnly");',
  24237. ' }',
  24238. ' }, 123);',
  24239. '};',
  24240. 'this.FooVar = function (a) {',
  24241. ' $mod.THelper.DoIt.call(a, 123);',
  24242. ' var $with = a.get();',
  24243. ' $mod.THelper.DoIt.call(a, 123);',
  24244. '};',
  24245. '']),
  24246. LinesToStr([ // $mod.$main
  24247. '']));
  24248. end;
  24249. procedure TTestModule.TestTypeHelper_PassVarConst;
  24250. begin
  24251. StartProgram(false);
  24252. Add([
  24253. '{$modeswitch typehelpers}',
  24254. 'type',
  24255. ' THelper = type helper for word',
  24256. ' procedure DoIt(e: byte = 123);',
  24257. ' end;',
  24258. 'procedure THelper.DoIt(e: byte);',
  24259. 'begin',
  24260. 'end;',
  24261. 'var a: word;',
  24262. 'const c: word = 2;',
  24263. '{$writeableconst off}',
  24264. 'const r: word = 3;',
  24265. 'begin',
  24266. ' a.DoIt;',
  24267. ' with a do DoIt;',
  24268. ' c.DoIt;',
  24269. ' with c do DoIt;',
  24270. ' r.DoIt;',
  24271. ' with r do DoIt;',
  24272. '']);
  24273. ConvertProgram;
  24274. CheckSource('TestTypeHelper_PassVarConst',
  24275. LinesToStr([ // statements
  24276. 'rtl.createHelper(this, "THelper", null, function () {',
  24277. ' this.DoIt = function (e) {',
  24278. ' };',
  24279. '});',
  24280. 'this.a = 0;',
  24281. 'this.c = 2;',
  24282. 'this.r = 3;',
  24283. '']),
  24284. LinesToStr([ // $mod.$main
  24285. '$mod.THelper.DoIt.call({',
  24286. ' p: $mod,',
  24287. ' get: function () {',
  24288. ' return this.p.a;',
  24289. ' },',
  24290. ' set: function (v) {',
  24291. ' this.p.a = v;',
  24292. ' }',
  24293. '}, 123);',
  24294. 'var $with = $mod.a;',
  24295. '$mod.THelper.DoIt.call({',
  24296. ' get: function () {',
  24297. ' return $with;',
  24298. ' },',
  24299. ' set: function (v) {',
  24300. ' $with = v;',
  24301. ' }',
  24302. '}, 123);',
  24303. '$mod.THelper.DoIt.call({',
  24304. ' p: $mod,',
  24305. ' get: function () {',
  24306. ' return this.p.c;',
  24307. ' },',
  24308. ' set: function (v) {',
  24309. ' this.p.c = v;',
  24310. ' }',
  24311. '}, 123);',
  24312. 'var $with1 = $mod.c;',
  24313. '$mod.THelper.DoIt.call({',
  24314. ' get: function () {',
  24315. ' return $with1;',
  24316. ' },',
  24317. ' set: function (v) {',
  24318. ' $with1 = v;',
  24319. ' }',
  24320. '}, 123);',
  24321. '$mod.THelper.DoIt.call({',
  24322. ' get: function () {',
  24323. ' return 3;',
  24324. ' },',
  24325. ' set: function (v) {',
  24326. ' rtl.raiseE("EPropReadOnly");',
  24327. ' }',
  24328. '}, 123);',
  24329. 'var $with2 = 3;',
  24330. ' $mod.THelper.DoIt.call({',
  24331. ' get: function () {',
  24332. ' return $with2;',
  24333. ' },',
  24334. ' set: function () {',
  24335. ' rtl.raiseE("EPropReadOnly");',
  24336. ' }',
  24337. ' }, 123);',
  24338. '']));
  24339. end;
  24340. procedure TTestModule.TestTypeHelper_PassFuncResult;
  24341. begin
  24342. StartProgram(false);
  24343. Add([
  24344. '{$modeswitch typehelpers}',
  24345. 'type',
  24346. ' THelper = type helper for word',
  24347. ' procedure DoIt(e: byte = 123);',
  24348. ' end;',
  24349. 'procedure THelper.DoIt(e: byte);',
  24350. 'begin',
  24351. 'end;',
  24352. 'function Foo(b: byte = 1): word;',
  24353. 'begin',
  24354. 'end;',
  24355. 'begin',
  24356. ' Foo.DoIt;',
  24357. ' Foo().DoIt;',
  24358. ' with Foo do DoIt;',
  24359. ' with Foo() do DoIt;',
  24360. '']);
  24361. ConvertProgram;
  24362. CheckSource('TestTypeHelper_PassFuncResult',
  24363. LinesToStr([ // statements
  24364. 'rtl.createHelper(this, "THelper", null, function () {',
  24365. ' this.DoIt = function (e) {',
  24366. ' };',
  24367. '});',
  24368. 'this.Foo = function (b) {',
  24369. ' var Result = 0;',
  24370. ' return Result;',
  24371. '};',
  24372. '']),
  24373. LinesToStr([ // $mod.$main
  24374. '$mod.THelper.DoIt.call({',
  24375. ' a: $mod.Foo(1),',
  24376. ' get: function () {',
  24377. ' return this.a;',
  24378. ' },',
  24379. ' set: function (v) {',
  24380. ' this.a = v;',
  24381. ' }',
  24382. '}, 123);',
  24383. '$mod.THelper.DoIt.call({',
  24384. ' a: $mod.Foo(1),',
  24385. ' get: function () {',
  24386. ' return this.a;',
  24387. ' },',
  24388. ' set: function (v) {',
  24389. ' this.a = v;',
  24390. ' }',
  24391. '}, 123);',
  24392. 'var $with = $mod.Foo(1);',
  24393. '$mod.THelper.DoIt.call({',
  24394. ' get: function () {',
  24395. ' return $with;',
  24396. ' },',
  24397. ' set: function (v) {',
  24398. ' $with = v;',
  24399. ' }',
  24400. '}, 123);',
  24401. 'var $with1 = $mod.Foo(1);',
  24402. '$mod.THelper.DoIt.call({',
  24403. ' get: function () {',
  24404. ' return $with1;',
  24405. ' },',
  24406. ' set: function (v) {',
  24407. ' $with1 = v;',
  24408. ' }',
  24409. '}, 123);',
  24410. '']));
  24411. end;
  24412. procedure TTestModule.TestTypeHelper_PassPropertyField;
  24413. begin
  24414. StartProgram(false);
  24415. Add([
  24416. '{$modeswitch typehelpers}',
  24417. 'type',
  24418. ' TObject = class',
  24419. ' FField: word;',
  24420. ' procedure SetField(Value: word);',
  24421. ' property Field: word read FField write SetField;',
  24422. ' end;',
  24423. ' THelper = type helper for word',
  24424. ' procedure Fly;',
  24425. ' class procedure Run; static;',
  24426. ' end;',
  24427. 'procedure TObject.SetField(Value: word);',
  24428. 'begin',
  24429. ' Field.Fly;',
  24430. ' Field.Run;',
  24431. ' Self.Field.Fly;',
  24432. ' Self.Field.Run;',
  24433. ' with Self do begin',
  24434. ' Field.Fly;',
  24435. ' Field.Run;',
  24436. ' end;',
  24437. ' with Self.Field do begin',
  24438. ' Fly;',
  24439. ' Run;',
  24440. ' end;',
  24441. 'end;',
  24442. 'procedure THelper.Fly;',
  24443. 'begin',
  24444. 'end;',
  24445. 'class procedure THelper.Run;',
  24446. 'begin',
  24447. 'end;',
  24448. 'var',
  24449. ' o: TObject;',
  24450. 'begin',
  24451. ' o.Field.Fly;',
  24452. ' o.Field.Run;',
  24453. ' with o do begin',
  24454. ' Field.Fly;',
  24455. ' Field.Run;',
  24456. ' end;',
  24457. ' with o.Field do begin',
  24458. ' Fly;',
  24459. ' Run;',
  24460. ' end;',
  24461. '']);
  24462. ConvertProgram;
  24463. CheckSource('TestTypeHelper_PassPropertyField',
  24464. LinesToStr([ // statements
  24465. 'rtl.createClass(this, "TObject", null, function () {',
  24466. ' this.$init = function () {',
  24467. ' this.FField = 0;',
  24468. ' };',
  24469. ' this.$final = function () {',
  24470. ' };',
  24471. ' this.SetField = function (Value) {',
  24472. ' $mod.THelper.Fly.call({',
  24473. ' p: this,',
  24474. ' get: function () {',
  24475. ' return this.p.FField;',
  24476. ' },',
  24477. ' set: function (v) {',
  24478. ' this.p.FField = v;',
  24479. ' }',
  24480. ' });',
  24481. ' $mod.THelper.Run();',
  24482. ' $mod.THelper.Fly.call({',
  24483. ' p: this,',
  24484. ' get: function () {',
  24485. ' return this.p.FField;',
  24486. ' },',
  24487. ' set: function (v) {',
  24488. ' this.p.FField = v;',
  24489. ' }',
  24490. ' });',
  24491. ' $mod.THelper.Run();',
  24492. ' $mod.THelper.Fly.call({',
  24493. ' p: this,',
  24494. ' get: function () {',
  24495. ' return this.p.FField;',
  24496. ' },',
  24497. ' set: function (v) {',
  24498. ' this.p.FField = v;',
  24499. ' }',
  24500. ' });',
  24501. ' $mod.THelper.Run();',
  24502. ' var $with = this.FField;',
  24503. ' $mod.THelper.Fly.call({',
  24504. ' get: function () {',
  24505. ' return $with;',
  24506. ' },',
  24507. ' set: function (v) {',
  24508. ' $with = v;',
  24509. ' }',
  24510. ' });',
  24511. ' $mod.THelper.Run();',
  24512. ' };',
  24513. '});',
  24514. 'rtl.createHelper(this, "THelper", null, function () {',
  24515. ' this.Fly = function () {',
  24516. ' };',
  24517. ' this.Run = function () {',
  24518. ' };',
  24519. '});',
  24520. 'this.o = null;',
  24521. '']),
  24522. LinesToStr([ // $mod.$main
  24523. '$mod.THelper.Fly.call({',
  24524. ' p: $mod.o,',
  24525. ' get: function () {',
  24526. ' return this.p.FField;',
  24527. ' },',
  24528. ' set: function (v) {',
  24529. ' this.p.FField = v;',
  24530. ' }',
  24531. '});',
  24532. '$mod.THelper.Run();',
  24533. 'var $with = $mod.o;',
  24534. '$mod.THelper.Fly.call({',
  24535. ' p: $with,',
  24536. ' get: function () {',
  24537. ' return this.p.FField;',
  24538. ' },',
  24539. ' set: function (v) {',
  24540. ' this.p.FField = v;',
  24541. ' }',
  24542. '});',
  24543. '$mod.THelper.Run();',
  24544. 'var $with1 = $mod.o.FField;',
  24545. '$mod.THelper.Fly.call({',
  24546. ' get: function () {',
  24547. ' return $with1;',
  24548. ' },',
  24549. ' set: function (v) {',
  24550. ' $with1 = v;',
  24551. ' }',
  24552. '});',
  24553. '$mod.THelper.Run();',
  24554. '']));
  24555. end;
  24556. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  24557. begin
  24558. StartProgram(false);
  24559. Add([
  24560. '{$modeswitch typehelpers}',
  24561. 'type',
  24562. ' TObject = class',
  24563. ' FField: word;',
  24564. ' function GetField: word;',
  24565. ' property Field: word read GetField write FField;',
  24566. ' end;',
  24567. ' THelper = type helper for word',
  24568. ' procedure Fly;',
  24569. ' class procedure Run; static;',
  24570. ' end;',
  24571. 'function TObject.GetField: word;',
  24572. 'begin',
  24573. ' Field.Fly;',
  24574. ' Field.Run;',
  24575. ' Self.Field.Fly;',
  24576. ' Self.Field.Run;',
  24577. ' with Self do begin',
  24578. ' Field.Fly;',
  24579. ' Field.Run;',
  24580. ' end;',
  24581. ' with Self.Field do begin',
  24582. ' Fly;',
  24583. ' Run;',
  24584. ' end;',
  24585. 'end;',
  24586. 'procedure THelper.Fly;',
  24587. 'begin',
  24588. 'end;',
  24589. 'class procedure THelper.Run;',
  24590. 'begin',
  24591. 'end;',
  24592. 'var',
  24593. ' o: TObject;',
  24594. 'begin',
  24595. ' o.Field.Fly;',
  24596. ' o.Field.Run;',
  24597. ' with o do begin',
  24598. ' Field.Fly;',
  24599. ' Field.Run;',
  24600. ' end;',
  24601. ' with o.Field do begin',
  24602. ' Fly;',
  24603. ' Run;',
  24604. ' end;',
  24605. '']);
  24606. ConvertProgram;
  24607. CheckSource('TestTypeHelper_PassPropertyGetter',
  24608. LinesToStr([ // statements
  24609. 'rtl.createClass(this, "TObject", null, function () {',
  24610. ' this.$init = function () {',
  24611. ' this.FField = 0;',
  24612. ' };',
  24613. ' this.$final = function () {',
  24614. ' };',
  24615. ' this.GetField = function () {',
  24616. ' var Result = 0;',
  24617. ' $mod.THelper.Fly.call({',
  24618. ' p: this.GetField(),',
  24619. ' get: function () {',
  24620. ' return this.p;',
  24621. ' },',
  24622. ' set: function (v) {',
  24623. ' this.p = v;',
  24624. ' }',
  24625. ' });',
  24626. ' $mod.THelper.Run();',
  24627. ' $mod.THelper.Fly.call({',
  24628. ' p: this.GetField(),',
  24629. ' get: function () {',
  24630. ' return this.p;',
  24631. ' },',
  24632. ' set: function (v) {',
  24633. ' this.p = v;',
  24634. ' }',
  24635. ' });',
  24636. ' $mod.THelper.Run();',
  24637. ' $mod.THelper.Fly.call({',
  24638. ' p: this.GetField(),',
  24639. ' get: function () {',
  24640. ' return this.p;',
  24641. ' },',
  24642. ' set: function (v) {',
  24643. ' this.p = v;',
  24644. ' }',
  24645. ' });',
  24646. ' $mod.THelper.Run();',
  24647. ' var $with = this.GetField();',
  24648. ' $mod.THelper.Fly.call({',
  24649. ' get: function () {',
  24650. ' return $with;',
  24651. ' },',
  24652. ' set: function (v) {',
  24653. ' $with = v;',
  24654. ' }',
  24655. ' });',
  24656. ' $mod.THelper.Run();',
  24657. ' return Result;',
  24658. ' };',
  24659. '});',
  24660. 'rtl.createHelper(this, "THelper", null, function () {',
  24661. ' this.Fly = function () {',
  24662. ' };',
  24663. ' this.Run = function () {',
  24664. ' };',
  24665. '});',
  24666. 'this.o = null;',
  24667. '']),
  24668. LinesToStr([ // $mod.$main
  24669. '$mod.THelper.Fly.call({',
  24670. ' p: $mod.o.GetField(),',
  24671. ' get: function () {',
  24672. ' return this.p;',
  24673. ' },',
  24674. ' set: function (v) {',
  24675. ' this.p = v;',
  24676. ' }',
  24677. '});',
  24678. '$mod.THelper.Run();',
  24679. 'var $with = $mod.o;',
  24680. '$mod.THelper.Fly.call({',
  24681. ' p: $with.GetField(),',
  24682. ' get: function () {',
  24683. ' return this.p;',
  24684. ' },',
  24685. ' set: function (v) {',
  24686. ' this.p = v;',
  24687. ' }',
  24688. '});',
  24689. '$mod.THelper.Run();',
  24690. 'var $with1 = $mod.o.GetField();',
  24691. '$mod.THelper.Fly.call({',
  24692. ' get: function () {',
  24693. ' return $with1;',
  24694. ' },',
  24695. ' set: function (v) {',
  24696. ' $with1 = v;',
  24697. ' }',
  24698. '});',
  24699. '$mod.THelper.Run();',
  24700. '']));
  24701. end;
  24702. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  24703. begin
  24704. StartProgram(false);
  24705. Add([
  24706. '{$modeswitch typehelpers}',
  24707. 'type',
  24708. ' TObject = class',
  24709. ' class var FField: word;',
  24710. ' class procedure SetField(Value: word);',
  24711. ' class property Field: word read FField write SetField;',
  24712. ' end;',
  24713. ' THelper = type helper for word',
  24714. ' procedure Fly(n: byte);',
  24715. ' end;',
  24716. 'class procedure TObject.SetField(Value: word);',
  24717. 'begin',
  24718. ' Field.Fly(1);',
  24719. ' Self.Field.Fly(2);',
  24720. ' with Self do Field.Fly(3);',
  24721. ' with Self.Field do Fly(4);',
  24722. ' TObject.Field.Fly(5);',
  24723. ' with TObject do Field.Fly(6);',
  24724. ' with TObject.Field do Fly(7);',
  24725. 'end;',
  24726. 'procedure THelper.Fly(n: byte);',
  24727. 'begin',
  24728. 'end;',
  24729. 'var',
  24730. ' o: TObject;',
  24731. 'begin',
  24732. ' o.Field.Fly(11);',
  24733. ' with o do Field.Fly(12);',
  24734. ' with o.Field do Fly(13);',
  24735. ' TObject.Field.Fly(14);',
  24736. ' with TObject do Field.Fly(15);',
  24737. ' with TObject.Field do Fly(16);',
  24738. '']);
  24739. ConvertProgram;
  24740. CheckSource('TestTypeHelper_PassClassPropertyField',
  24741. LinesToStr([ // statements
  24742. 'rtl.createClass(this, "TObject", null, function () {',
  24743. ' this.FField = 0;',
  24744. ' this.$init = function () {',
  24745. ' };',
  24746. ' this.$final = function () {',
  24747. ' };',
  24748. ' this.SetField = function (Value) {',
  24749. ' $mod.THelper.Fly.call({',
  24750. ' p: this,',
  24751. ' get: function () {',
  24752. ' return this.p.FField;',
  24753. ' },',
  24754. ' set: function (v) {',
  24755. ' $mod.TObject.FField = v;',
  24756. ' }',
  24757. ' }, 1);',
  24758. ' $mod.THelper.Fly.call({',
  24759. ' p: this,',
  24760. ' get: function () {',
  24761. ' return this.p.FField;',
  24762. ' },',
  24763. ' set: function (v) {',
  24764. ' $mod.TObject.FField = v;',
  24765. ' }',
  24766. ' }, 2);',
  24767. ' $mod.THelper.Fly.call({',
  24768. ' p: this,',
  24769. ' get: function () {',
  24770. ' return this.p.FField;',
  24771. ' },',
  24772. ' set: function (v) {',
  24773. ' $mod.TObject.FField = v;',
  24774. ' }',
  24775. ' }, 3);',
  24776. ' var $with = this.FField;',
  24777. ' $mod.THelper.Fly.call({',
  24778. ' get: function () {',
  24779. ' return $with;',
  24780. ' },',
  24781. ' set: function (v) {',
  24782. ' $with = v;',
  24783. ' }',
  24784. ' }, 4);',
  24785. ' $mod.THelper.Fly.call({',
  24786. ' p: $mod.TObject,',
  24787. ' get: function () {',
  24788. ' return this.p.FField;',
  24789. ' },',
  24790. ' set: function (v) {',
  24791. ' $mod.TObject.FField = v;',
  24792. ' }',
  24793. ' }, 5);',
  24794. ' var $with1 = $mod.TObject;',
  24795. ' $mod.THelper.Fly.call({',
  24796. ' p: $with1,',
  24797. ' get: function () {',
  24798. ' return this.p.FField;',
  24799. ' },',
  24800. ' set: function (v) {',
  24801. ' $mod.TObject.FField = v;',
  24802. ' }',
  24803. ' }, 6);',
  24804. ' var $with2 = $mod.TObject.FField;',
  24805. ' $mod.THelper.Fly.call({',
  24806. ' get: function () {',
  24807. ' return $with2;',
  24808. ' },',
  24809. ' set: function (v) {',
  24810. ' $with2 = v;',
  24811. ' }',
  24812. ' }, 7);',
  24813. ' };',
  24814. '});',
  24815. 'rtl.createHelper(this, "THelper", null, function () {',
  24816. ' this.Fly = function (n) {',
  24817. ' };',
  24818. '});',
  24819. 'this.o = null;',
  24820. '']),
  24821. LinesToStr([ // $mod.$main
  24822. '$mod.THelper.Fly.call({',
  24823. ' p: $mod.o,',
  24824. ' get: function () {',
  24825. ' return this.p.FField;',
  24826. ' },',
  24827. ' set: function (v) {',
  24828. ' $mod.TObject.FField = v;',
  24829. ' }',
  24830. '}, 11);',
  24831. 'var $with = $mod.o;',
  24832. '$mod.THelper.Fly.call({',
  24833. ' p: $with,',
  24834. ' get: function () {',
  24835. ' return this.p.FField;',
  24836. ' },',
  24837. ' set: function (v) {',
  24838. ' $mod.TObject.FField = v;',
  24839. ' }',
  24840. '}, 12);',
  24841. 'var $with1 = $mod.o.FField;',
  24842. '$mod.THelper.Fly.call({',
  24843. ' get: function () {',
  24844. ' return $with1;',
  24845. ' },',
  24846. ' set: function (v) {',
  24847. ' $with1 = v;',
  24848. ' }',
  24849. '}, 13);',
  24850. '$mod.THelper.Fly.call({',
  24851. ' p: $mod.TObject,',
  24852. ' get: function () {',
  24853. ' return this.p.FField;',
  24854. ' },',
  24855. ' set: function (v) {',
  24856. ' $mod.TObject.FField = v;',
  24857. ' }',
  24858. '}, 14);',
  24859. 'var $with2 = $mod.TObject;',
  24860. '$mod.THelper.Fly.call({',
  24861. ' p: $with2,',
  24862. ' get: function () {',
  24863. ' return this.p.FField;',
  24864. ' },',
  24865. ' set: function (v) {',
  24866. ' $mod.TObject.FField = v;',
  24867. ' }',
  24868. '}, 15);',
  24869. 'var $with3 = $mod.TObject.FField;',
  24870. '$mod.THelper.Fly.call({',
  24871. ' get: function () {',
  24872. ' return $with3;',
  24873. ' },',
  24874. ' set: function (v) {',
  24875. ' $with3 = v;',
  24876. ' }',
  24877. '}, 16);',
  24878. '']));
  24879. end;
  24880. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  24881. begin
  24882. StartProgram(false);
  24883. Add([
  24884. '{$modeswitch typehelpers}',
  24885. 'type',
  24886. ' TObject = class',
  24887. ' class var FField: word;',
  24888. ' class function GetField: word; static;',
  24889. ' class property Field: word read GetField write FField;',
  24890. ' end;',
  24891. ' THelper = type helper for word',
  24892. ' procedure Fly(n: byte);',
  24893. ' end;',
  24894. 'class function TObject.GetField: word;',
  24895. 'begin',
  24896. ' Field.Fly(1);',
  24897. ' TObject.Field.Fly(5);',
  24898. ' with TObject do Field.Fly(6);',
  24899. ' with TObject.Field do Fly(7);',
  24900. 'end;',
  24901. 'procedure THelper.Fly(n: byte);',
  24902. 'begin',
  24903. 'end;',
  24904. 'var',
  24905. ' o: TObject;',
  24906. 'begin',
  24907. ' o.Field.Fly(11);',
  24908. ' with o do Field.Fly(12);',
  24909. ' with o.Field do Fly(13);',
  24910. '']);
  24911. ConvertProgram;
  24912. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  24913. LinesToStr([ // statements
  24914. 'rtl.createClass(this, "TObject", null, function () {',
  24915. ' this.FField = 0;',
  24916. ' this.$init = function () {',
  24917. ' };',
  24918. ' this.$final = function () {',
  24919. ' };',
  24920. ' this.GetField = function () {',
  24921. ' var Result = 0;',
  24922. ' $mod.THelper.Fly.call({',
  24923. ' p: $mod.TObject.GetField(),',
  24924. ' get: function () {',
  24925. ' return this.p;',
  24926. ' },',
  24927. ' set: function (v) {',
  24928. ' this.p = v;',
  24929. ' }',
  24930. ' }, 1);',
  24931. ' $mod.THelper.Fly.call({',
  24932. ' p: $mod.TObject.GetField(),',
  24933. ' get: function () {',
  24934. ' return this.p;',
  24935. ' },',
  24936. ' set: function (v) {',
  24937. ' this.p = v;',
  24938. ' }',
  24939. ' }, 5);',
  24940. ' var $with = $mod.TObject;',
  24941. ' $mod.THelper.Fly.call({',
  24942. ' p: $with.GetField(),',
  24943. ' get: function () {',
  24944. ' return this.p;',
  24945. ' },',
  24946. ' set: function (v) {',
  24947. ' this.p = v;',
  24948. ' }',
  24949. ' }, 6);',
  24950. ' var $with1 = $mod.TObject.GetField();',
  24951. ' $mod.THelper.Fly.call({',
  24952. ' get: function () {',
  24953. ' return $with1;',
  24954. ' },',
  24955. ' set: function (v) {',
  24956. ' $with1 = v;',
  24957. ' }',
  24958. ' }, 7);',
  24959. ' return Result;',
  24960. ' };',
  24961. '});',
  24962. 'rtl.createHelper(this, "THelper", null, function () {',
  24963. ' this.Fly = function (n) {',
  24964. ' };',
  24965. '});',
  24966. 'this.o = null;',
  24967. '']),
  24968. LinesToStr([ // $mod.$main
  24969. '$mod.THelper.Fly.call({',
  24970. ' p: $mod.TObject.GetField(),',
  24971. ' get: function () {',
  24972. ' return this.p;',
  24973. ' },',
  24974. ' set: function (v) {',
  24975. ' this.p = v;',
  24976. ' }',
  24977. '}, 11);',
  24978. 'var $with = $mod.o;',
  24979. '$mod.THelper.Fly.call({',
  24980. ' p: $with.GetField(),',
  24981. ' get: function () {',
  24982. ' return this.p;',
  24983. ' },',
  24984. ' set: function (v) {',
  24985. ' this.p = v;',
  24986. ' }',
  24987. '}, 12);',
  24988. 'var $with1 = $mod.TObject.GetField();',
  24989. '$mod.THelper.Fly.call({',
  24990. ' get: function () {',
  24991. ' return $with1;',
  24992. ' },',
  24993. ' set: function (v) {',
  24994. ' $with1 = v;',
  24995. ' }',
  24996. '}, 13);',
  24997. '']));
  24998. end;
  24999. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25000. begin
  25001. StartProgram(false);
  25002. Add([
  25003. '{$modeswitch typehelpers}',
  25004. 'type',
  25005. ' TObject = class',
  25006. ' class var FField: word;',
  25007. ' class function GetField: word;',
  25008. ' class property Field: word read GetField write FField;',
  25009. ' end;',
  25010. ' TClass = class of TObject;',
  25011. ' THelper = type helper for word',
  25012. ' procedure Fly(n: byte);',
  25013. ' end;',
  25014. 'class function TObject.GetField: word;',
  25015. 'begin',
  25016. ' Field.Fly(1);',
  25017. ' Self.Field.Fly(5);',
  25018. ' with Self do Field.Fly(6);',
  25019. ' with Self.Field do Fly(7);',
  25020. 'end;',
  25021. 'procedure THelper.Fly(n: byte);',
  25022. 'begin',
  25023. 'end;',
  25024. 'var',
  25025. ' o: TObject;',
  25026. ' c: TClass;',
  25027. 'begin',
  25028. ' o.Field.Fly(11);',
  25029. ' with o do Field.Fly(12);',
  25030. ' with o.Field do Fly(13);',
  25031. ' c.Field.Fly(14);',
  25032. ' with c do Field.Fly(15);',
  25033. ' with c.Field do Fly(16);',
  25034. '']);
  25035. ConvertProgram;
  25036. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25037. LinesToStr([ // statements
  25038. 'rtl.createClass(this, "TObject", null, function () {',
  25039. ' this.FField = 0;',
  25040. ' this.$init = function () {',
  25041. ' };',
  25042. ' this.$final = function () {',
  25043. ' };',
  25044. ' this.GetField = function () {',
  25045. ' var Result = 0;',
  25046. ' $mod.THelper.Fly.call({',
  25047. ' p: this.GetField(),',
  25048. ' get: function () {',
  25049. ' return this.p;',
  25050. ' },',
  25051. ' set: function (v) {',
  25052. ' this.p = v;',
  25053. ' }',
  25054. ' }, 1);',
  25055. ' $mod.THelper.Fly.call({',
  25056. ' p: this.GetField(),',
  25057. ' get: function () {',
  25058. ' return this.p;',
  25059. ' },',
  25060. ' set: function (v) {',
  25061. ' this.p = v;',
  25062. ' }',
  25063. ' }, 5);',
  25064. ' $mod.THelper.Fly.call({',
  25065. ' p: this.GetField(),',
  25066. ' get: function () {',
  25067. ' return this.p;',
  25068. ' },',
  25069. ' set: function (v) {',
  25070. ' this.p = v;',
  25071. ' }',
  25072. ' }, 6);',
  25073. ' var $with = this.GetField();',
  25074. ' $mod.THelper.Fly.call({',
  25075. ' get: function () {',
  25076. ' return $with;',
  25077. ' },',
  25078. ' set: function (v) {',
  25079. ' $with = v;',
  25080. ' }',
  25081. ' }, 7);',
  25082. ' return Result;',
  25083. ' };',
  25084. '});',
  25085. 'rtl.createHelper(this, "THelper", null, function () {',
  25086. ' this.Fly = function (n) {',
  25087. ' };',
  25088. '});',
  25089. 'this.o = null;',
  25090. 'this.c = null;',
  25091. '']),
  25092. LinesToStr([ // $mod.$main
  25093. '$mod.THelper.Fly.call({',
  25094. ' p: $mod.o.$class.GetField(),',
  25095. ' get: function () {',
  25096. ' return this.p;',
  25097. ' },',
  25098. ' set: function (v) {',
  25099. ' this.p = v;',
  25100. ' }',
  25101. '}, 11);',
  25102. 'var $with = $mod.o;',
  25103. '$mod.THelper.Fly.call({',
  25104. ' p: $with.$class.GetField(),',
  25105. ' get: function () {',
  25106. ' return this.p;',
  25107. ' },',
  25108. ' set: function (v) {',
  25109. ' this.p = v;',
  25110. ' }',
  25111. '}, 12);',
  25112. 'var $with1 = $mod.o.$class.GetField();',
  25113. '$mod.THelper.Fly.call({',
  25114. ' get: function () {',
  25115. ' return $with1;',
  25116. ' },',
  25117. ' set: function (v) {',
  25118. ' $with1 = v;',
  25119. ' }',
  25120. '}, 13);',
  25121. '$mod.THelper.Fly.call({',
  25122. ' p: $mod.c.GetField(),',
  25123. ' get: function () {',
  25124. ' return this.p;',
  25125. ' },',
  25126. ' set: function (v) {',
  25127. ' this.p = v;',
  25128. ' }',
  25129. '}, 14);',
  25130. 'var $with2 = $mod.c;',
  25131. '$mod.THelper.Fly.call({',
  25132. ' p: $with2.GetField(),',
  25133. ' get: function () {',
  25134. ' return this.p;',
  25135. ' },',
  25136. ' set: function (v) {',
  25137. ' this.p = v;',
  25138. ' }',
  25139. '}, 15);',
  25140. 'var $with3 = $mod.c.GetField();',
  25141. '$mod.THelper.Fly.call({',
  25142. ' get: function () {',
  25143. ' return $with3;',
  25144. ' },',
  25145. ' set: function (v) {',
  25146. ' $with3 = v;',
  25147. ' }',
  25148. '}, 16);',
  25149. '']));
  25150. end;
  25151. procedure TTestModule.TestTypeHelper_Property;
  25152. begin
  25153. StartProgram(false);
  25154. Add([
  25155. '{$modeswitch typehelpers}',
  25156. 'type',
  25157. ' THelper = type helper for word',
  25158. ' function GetSize: longint;',
  25159. ' procedure SetSize(Value: longint);',
  25160. ' property Size: longint read GetSize write SetSize;',
  25161. ' end;',
  25162. 'function THelper.GetSize: longint;',
  25163. 'begin',
  25164. ' Result:=Size+1;',
  25165. ' Size:=2;',
  25166. ' Result:=Self.Size+3;',
  25167. ' Self.Size:=4;',
  25168. ' with Self do begin',
  25169. ' Result:=Size+5;',
  25170. ' Size:=6;',
  25171. ' end;',
  25172. 'end;',
  25173. 'procedure THelper.SetSize(Value: longint);',
  25174. 'begin',
  25175. 'end;',
  25176. 'var w: word;',
  25177. 'begin',
  25178. ' w:=w.Size+7;',
  25179. ' w.Size:=w+8;',
  25180. ' with w do begin',
  25181. ' w:=Size+9;',
  25182. ' Size:=w+10;',
  25183. ' end;',
  25184. '']);
  25185. ConvertProgram;
  25186. CheckSource('TestTypeHelper_Property',
  25187. LinesToStr([ // statements
  25188. 'rtl.createHelper(this, "THelper", null, function () {',
  25189. ' this.GetSize = function () {',
  25190. ' var Result = 0;',
  25191. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25192. ' $mod.THelper.SetSize.call(this, 2);',
  25193. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25194. ' $mod.THelper.SetSize.call(this, 4);',
  25195. ' var $with = this.get();',
  25196. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25197. ' $mod.THelper.SetSize.call(this, 6);',
  25198. ' return Result;',
  25199. ' };',
  25200. ' this.SetSize = function (Value) {',
  25201. ' };',
  25202. '});',
  25203. 'this.w = 0;',
  25204. '']),
  25205. LinesToStr([ // $mod.$main
  25206. '$mod.w = $mod.THelper.GetSize.call({',
  25207. ' p: $mod,',
  25208. ' get: function () {',
  25209. ' return this.p.w;',
  25210. ' },',
  25211. ' set: function (v) {',
  25212. ' this.p.w = v;',
  25213. ' }',
  25214. '}) + 7;',
  25215. '$mod.THelper.SetSize.call({',
  25216. ' p: $mod,',
  25217. ' get: function () {',
  25218. ' return this.p.w;',
  25219. ' },',
  25220. ' set: function (v) {',
  25221. ' this.p.w = v;',
  25222. ' }',
  25223. '}, $mod.w + 8);',
  25224. 'var $with = $mod.w;',
  25225. '$mod.w = $mod.THelper.GetSize.call({',
  25226. ' get: function () {',
  25227. ' return $with;',
  25228. ' },',
  25229. ' set: function (v) {',
  25230. ' $with = v;',
  25231. ' }',
  25232. '}) + 9;',
  25233. '$mod.THelper.SetSize.call({',
  25234. ' get: function () {',
  25235. ' return $with;',
  25236. ' },',
  25237. ' set: function (v) {',
  25238. ' $with = v;',
  25239. ' }',
  25240. '}, $mod.w + 10);',
  25241. '']));
  25242. end;
  25243. procedure TTestModule.TestTypeHelper_Property_Array;
  25244. begin
  25245. StartProgram(false);
  25246. Add([
  25247. '{$modeswitch typehelpers}',
  25248. 'type',
  25249. ' THelper = type helper for word',
  25250. ' function GetItems(Index: byte): boolean;',
  25251. ' procedure SetItems(Index: byte; Value: boolean);',
  25252. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25253. ' end;',
  25254. 'function THelper.GetItems(Index: byte): boolean;',
  25255. 'begin',
  25256. ' Result:=Items[1];',
  25257. ' Items[2]:=false;',
  25258. ' Result:=Self.Items[3];',
  25259. ' Self.Items[4]:=true;',
  25260. ' with Self do begin',
  25261. ' Result:=Items[5];',
  25262. ' Items[6]:=false;',
  25263. ' end;',
  25264. 'end;',
  25265. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  25266. 'begin',
  25267. 'end;',
  25268. 'var',
  25269. ' w: word;',
  25270. ' b: boolean;',
  25271. 'begin',
  25272. ' b:=w.Items[1];',
  25273. ' w.Items[2]:=b;',
  25274. ' with w do begin',
  25275. ' b:=Items[3];',
  25276. ' Items[4]:=b;',
  25277. ' end;',
  25278. '']);
  25279. ConvertProgram;
  25280. CheckSource('TestTypeHelper_Property_Array',
  25281. LinesToStr([ // statements
  25282. 'rtl.createHelper(this, "THelper", null, function () {',
  25283. ' this.GetItems = function (Index) {',
  25284. ' var Result = false;',
  25285. ' Result = $mod.THelper.GetItems.call(this, 1);',
  25286. ' $mod.THelper.SetItems.call(this, 2, false);',
  25287. ' Result = $mod.THelper.GetItems.call(this, 3);',
  25288. ' $mod.THelper.SetItems.call(this, 4, true);',
  25289. ' var $with = this.get();',
  25290. ' Result = $mod.THelper.GetItems.call(this, 5);',
  25291. ' $mod.THelper.SetItems.call(this, 6, false);',
  25292. ' return Result;',
  25293. ' };',
  25294. ' this.SetItems = function (Index, Value) {',
  25295. ' };',
  25296. '});',
  25297. 'this.w = 0;',
  25298. 'this.b = false;',
  25299. '']),
  25300. LinesToStr([ // $mod.$main
  25301. '$mod.b = $mod.THelper.GetItems.call({',
  25302. ' p: $mod,',
  25303. ' get: function () {',
  25304. ' return this.p.w;',
  25305. ' },',
  25306. ' set: function (v) {',
  25307. ' this.p.w = v;',
  25308. ' }',
  25309. '}, 1);',
  25310. '$mod.THelper.SetItems.call({',
  25311. ' p: $mod,',
  25312. ' get: function () {',
  25313. ' return this.p.w;',
  25314. ' },',
  25315. ' set: function (v) {',
  25316. ' this.p.w = v;',
  25317. ' }',
  25318. '}, 2, $mod.b);',
  25319. 'var $with = $mod.w;',
  25320. '$mod.b = $mod.THelper.GetItems.call({',
  25321. ' get: function () {',
  25322. ' return $with;',
  25323. ' },',
  25324. ' set: function (v) {',
  25325. ' $with = v;',
  25326. ' }',
  25327. '}, 3);',
  25328. '$mod.THelper.SetItems.call({',
  25329. ' get: function () {',
  25330. ' return $with;',
  25331. ' },',
  25332. ' set: function (v) {',
  25333. ' $with = v;',
  25334. ' }',
  25335. '}, 4, $mod.b);',
  25336. '']));
  25337. end;
  25338. procedure TTestModule.TestTypeHelper_ClassProperty;
  25339. begin
  25340. StartProgram(false);
  25341. Add([
  25342. '{$modeswitch typehelpers}',
  25343. 'type',
  25344. ' THelper = type helper for word',
  25345. ' class function GetSize: longint; static;',
  25346. ' class procedure SetSize(Value: longint); static;',
  25347. ' class property Size: longint read GetSize write SetSize;',
  25348. ' end;',
  25349. 'class function THelper.GetSize: longint;',
  25350. 'begin',
  25351. ' Result:=Size+1;',
  25352. ' Size:=2;',
  25353. 'end;',
  25354. 'class procedure THelper.SetSize(Value: longint);',
  25355. 'begin',
  25356. 'end;',
  25357. 'begin',
  25358. '']);
  25359. ConvertProgram;
  25360. CheckSource('TestTypeHelper_ClassProperty',
  25361. LinesToStr([ // statements
  25362. 'rtl.createHelper(this, "THelper", null, function () {',
  25363. ' this.GetSize = function () {',
  25364. ' var Result = 0;',
  25365. ' Result = $mod.THelper.GetSize() + 1;',
  25366. ' $mod.THelper.SetSize(2);',
  25367. ' return Result;',
  25368. ' };',
  25369. ' this.SetSize = function (Value) {',
  25370. ' };',
  25371. '});',
  25372. '']),
  25373. LinesToStr([ // $mod.$main
  25374. '']));
  25375. end;
  25376. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  25377. begin
  25378. StartProgram(false);
  25379. Add([
  25380. '{$modeswitch typehelpers}',
  25381. 'type',
  25382. ' THelper = type helper for word',
  25383. ' class function GetItems(Index: byte): boolean; static;',
  25384. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  25385. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  25386. ' end;',
  25387. 'class function THelper.GetItems(Index: byte): boolean;',
  25388. 'begin',
  25389. ' Result:=Items[1];',
  25390. ' Items[2]:=false;',
  25391. 'end;',
  25392. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  25393. 'begin',
  25394. 'end;',
  25395. 'var',
  25396. ' w: word;',
  25397. ' b: boolean;',
  25398. 'begin',
  25399. ' b:=w.Items[1];',
  25400. ' w.Items[2]:=b;',
  25401. ' with w do begin',
  25402. ' b:=Items[3];',
  25403. ' Items[4]:=b;',
  25404. ' end;',
  25405. '']);
  25406. ConvertProgram;
  25407. CheckSource('TestTypeHelper_ClassProperty_Array',
  25408. LinesToStr([ // statements
  25409. 'rtl.createHelper(this, "THelper", null, function () {',
  25410. ' this.GetItems = function (Index) {',
  25411. ' var Result = false;',
  25412. ' Result = $mod.THelper.GetItems(1);',
  25413. ' $mod.THelper.SetItems(2, false);',
  25414. ' return Result;',
  25415. ' };',
  25416. ' this.SetItems = function (Index, Value) {',
  25417. ' };',
  25418. '});',
  25419. 'this.w = 0;',
  25420. 'this.b = false;',
  25421. '']),
  25422. LinesToStr([ // $mod.$main
  25423. '$mod.b = $mod.THelper.GetItems(1);',
  25424. '$mod.THelper.SetItems(2, $mod.b);',
  25425. 'var $with = $mod.w;',
  25426. '$mod.b = $mod.THelper.GetItems(3);',
  25427. '$mod.THelper.SetItems(4, $mod.b);',
  25428. '']));
  25429. end;
  25430. procedure TTestModule.TestTypeHelper_ClassMethod;
  25431. begin
  25432. StartProgram(false);
  25433. Add([
  25434. '{$modeswitch typehelpers}',
  25435. 'type',
  25436. ' THelper = type helper for word',
  25437. ' class procedure DoStatic; static;',
  25438. ' end;',
  25439. 'class procedure THelper.DoStatic;',
  25440. 'begin',
  25441. ' DoStatic;',
  25442. ' DoStatic();',
  25443. 'end;',
  25444. 'var w: word;',
  25445. 'begin',
  25446. ' w.DoStatic;',
  25447. ' w.DoStatic();',
  25448. '']);
  25449. ConvertProgram;
  25450. CheckSource('TestTypeHelper_ClassMethod',
  25451. LinesToStr([ // statements
  25452. 'rtl.createHelper(this, "THelper", null, function () {',
  25453. ' this.DoStatic = function () {',
  25454. ' $mod.THelper.DoStatic();',
  25455. ' $mod.THelper.DoStatic();',
  25456. ' };',
  25457. '});',
  25458. 'this.w = 0;',
  25459. '']),
  25460. LinesToStr([ // $mod.$main
  25461. '$mod.THelper.DoStatic();',
  25462. '$mod.THelper.DoStatic();',
  25463. '']));
  25464. end;
  25465. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  25466. begin
  25467. StartProgram(false);
  25468. Add([
  25469. '{$modeswitch typehelpers}',
  25470. 'type',
  25471. ' THelper = type helper for word',
  25472. ' procedure Run; external name ''Run'';',
  25473. ' end;',
  25474. 'var w: word;',
  25475. 'begin',
  25476. ' w.Run;',
  25477. '']);
  25478. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  25479. ConvertProgram;
  25480. end;
  25481. procedure TTestModule.TestTypeHelper_Constructor;
  25482. begin
  25483. StartProgram(false);
  25484. Add([
  25485. '{$modeswitch typehelpers}',
  25486. 'type',
  25487. ' THelper = type helper for word',
  25488. ' constructor Init(e: longint);',
  25489. ' end;',
  25490. 'constructor THelper.Init(e: longint);',
  25491. 'begin',
  25492. ' Self:=e;',
  25493. ' Init(e+1);',
  25494. 'end;',
  25495. 'var w: word;',
  25496. 'begin',
  25497. ' w:=word.Init(2);',
  25498. ' w:=w.Init(3);',
  25499. ' with word do w:=Init(4);',
  25500. ' with w do w:=Init(5);',
  25501. '']);
  25502. ConvertProgram;
  25503. CheckSource('TestTypeHelper_Constructor',
  25504. LinesToStr([ // statements
  25505. 'rtl.createHelper(this, "THelper", null, function () {',
  25506. ' this.Init = function (e) {',
  25507. ' this.set(e);',
  25508. ' $mod.THelper.Init.call(this, e + 1);',
  25509. ' return this.get();',
  25510. ' };',
  25511. ' this.$new = function (fn, args) {',
  25512. ' return this[fn].apply({',
  25513. ' p: 0,',
  25514. ' get: function () {',
  25515. ' return this.p;',
  25516. ' },',
  25517. ' set: function (v) {',
  25518. ' this.p = v;',
  25519. ' }',
  25520. ' }, args);',
  25521. ' };',
  25522. '});',
  25523. 'this.w = 0;',
  25524. '']),
  25525. LinesToStr([ // $mod.$main
  25526. '$mod.w = $mod.THelper.$new("Init", [2]);',
  25527. '$mod.w = $mod.THelper.Init.call({',
  25528. ' p: $mod,',
  25529. ' get: function () {',
  25530. ' return this.p.w;',
  25531. ' },',
  25532. ' set: function (v) {',
  25533. ' this.p.w = v;',
  25534. ' }',
  25535. '}, 3);',
  25536. '$mod.w = $mod.THelper.$new("Init", [4]);',
  25537. 'var $with = $mod.w;',
  25538. '$mod.w = $mod.THelper.Init.call({',
  25539. ' get: function () {',
  25540. ' return $with;',
  25541. ' },',
  25542. ' set: function (v) {',
  25543. ' $with = v;',
  25544. ' }',
  25545. '}, 5);',
  25546. '']));
  25547. end;
  25548. procedure TTestModule.TestTypeHelper_Word;
  25549. begin
  25550. StartProgram(false);
  25551. Add([
  25552. '{$modeswitch typehelpers}',
  25553. 'type',
  25554. ' THelper = type helper for word',
  25555. ' procedure DoIt(e: byte = 123);',
  25556. ' end;',
  25557. 'procedure THelper.DoIt(e: byte);',
  25558. 'begin',
  25559. ' Self:=e;',
  25560. ' Self:=Self+1;',
  25561. ' with Self do Doit;',
  25562. 'end;',
  25563. 'begin',
  25564. ' word(3).DoIt;',
  25565. '']);
  25566. ConvertProgram;
  25567. CheckSource('TestTypeHelper_Word',
  25568. LinesToStr([ // statements
  25569. 'rtl.createHelper(this, "THelper", null, function () {',
  25570. ' this.DoIt = function (e) {',
  25571. ' this.set(e);',
  25572. ' this.set(this.get() + 1);',
  25573. ' var $with = this.get();',
  25574. ' $mod.THelper.DoIt.call(this, 123);',
  25575. ' };',
  25576. '});',
  25577. '']),
  25578. LinesToStr([ // $mod.$main
  25579. '$mod.THelper.DoIt.call({',
  25580. ' get: function () {',
  25581. ' return 3;',
  25582. ' },',
  25583. ' set: function (v) {',
  25584. ' rtl.raiseE("EPropReadOnly");',
  25585. ' }',
  25586. '}, 123);',
  25587. '']));
  25588. end;
  25589. procedure TTestModule.TestTypeHelper_Boolean;
  25590. begin
  25591. StartProgram(false);
  25592. Add([
  25593. '{$modeswitch typehelpers}',
  25594. 'type',
  25595. ' Integer = longint;',
  25596. ' THelper = type helper for boolean',
  25597. ' procedure Run(e: wordbool = true);',
  25598. ' end;',
  25599. 'procedure THelper.Run(e: wordbool);',
  25600. 'begin',
  25601. ' Self:=e;',
  25602. ' Self:=not Self;',
  25603. ' with Self do Run;',
  25604. ' if Integer(Self)=0 then ;',
  25605. 'end;',
  25606. 'begin',
  25607. ' boolean(3).Run;',
  25608. '']);
  25609. ConvertProgram;
  25610. CheckSource('TestTypeHelper_Boolean',
  25611. LinesToStr([ // statements
  25612. 'rtl.createHelper(this, "THelper", null, function () {',
  25613. ' this.Run = function (e) {',
  25614. ' this.set(e);',
  25615. ' this.set(!this.get());',
  25616. ' var $with = this.get();',
  25617. ' $mod.THelper.Run.call(this, true);',
  25618. ' if ((this.get() ? 1 : 0) === 0) ;',
  25619. ' };',
  25620. '});',
  25621. '']),
  25622. LinesToStr([ // $mod.$main
  25623. '$mod.THelper.Run.call({',
  25624. ' a: 3 != 0,',
  25625. ' get: function () {',
  25626. ' return this.a;',
  25627. ' },',
  25628. ' set: function (v) {',
  25629. ' rtl.raiseE("EPropReadOnly");',
  25630. ' }',
  25631. '}, true);',
  25632. '']));
  25633. end;
  25634. procedure TTestModule.TestTypeHelper_WordBool;
  25635. begin
  25636. StartProgram(false);
  25637. Add([
  25638. '{$modeswitch typehelpers}',
  25639. 'type',
  25640. ' Integer = longint;',
  25641. ' THelper = type helper for WordBool',
  25642. ' procedure Run(e: wordbool = true);',
  25643. ' end;',
  25644. 'procedure THelper.Run(e: wordbool);',
  25645. 'var i: integer;',
  25646. 'begin',
  25647. ' i:=Integer(Self);',
  25648. 'end;',
  25649. 'var w: wordbool;',
  25650. 'begin',
  25651. ' w.Run;',
  25652. ' wordbool(3).Run;',
  25653. '']);
  25654. ConvertProgram;
  25655. CheckSource('TestTypeHelper_WordBool',
  25656. LinesToStr([ // statements
  25657. 'rtl.createHelper(this, "THelper", null, function () {',
  25658. ' this.Run = function (e) {',
  25659. ' var i = 0;',
  25660. ' i = (this.get() ? 1 : 0);',
  25661. ' };',
  25662. '});',
  25663. 'this.w = false;',
  25664. '']),
  25665. LinesToStr([ // $mod.$main
  25666. '$mod.THelper.Run.call({',
  25667. ' p: $mod,',
  25668. ' get: function () {',
  25669. ' return this.p.w;',
  25670. ' },',
  25671. ' set: function (v) {',
  25672. ' this.p.w = v;',
  25673. ' }',
  25674. '}, true);',
  25675. '$mod.THelper.Run.call({',
  25676. ' a: 3 != 0,',
  25677. ' get: function () {',
  25678. ' return this.a;',
  25679. ' },',
  25680. ' set: function (v) {',
  25681. ' rtl.raiseE("EPropReadOnly");',
  25682. ' }',
  25683. '}, true);',
  25684. '']));
  25685. end;
  25686. procedure TTestModule.TestTypeHelper_Double;
  25687. begin
  25688. StartProgram(false);
  25689. Add([
  25690. '{$modeswitch typehelpers}',
  25691. 'type',
  25692. ' Float = type double;',
  25693. ' THelper = type helper for Float',
  25694. ' const NPI = 3.141592;',
  25695. ' function ToStr: String;',
  25696. ' end;',
  25697. 'function THelper.ToStr: String;',
  25698. 'begin',
  25699. 'end;',
  25700. 'procedure DoIt(s: string);',
  25701. 'begin',
  25702. 'end;',
  25703. 'var f: Float;',
  25704. 'begin',
  25705. ' DoIt(f.toStr);',
  25706. ' DoIt(f.toStr());',
  25707. ' (f*f).toStr;',
  25708. ' DoIt((f*f).toStr);',
  25709. '']);
  25710. ConvertProgram;
  25711. CheckSource('TestTypeHelper_Double',
  25712. LinesToStr([ // statements
  25713. 'rtl.createHelper(this, "THelper", null, function () {',
  25714. ' this.NPI = 3.141592;',
  25715. ' this.ToStr = function () {',
  25716. ' var Result = "";',
  25717. ' return Result;',
  25718. ' };',
  25719. '});',
  25720. 'this.DoIt = function (s) {',
  25721. '};',
  25722. 'this.f = 0.0;',
  25723. '']),
  25724. LinesToStr([ // $mod.$main
  25725. '$mod.DoIt($mod.THelper.ToStr.call({',
  25726. ' p: $mod,',
  25727. ' get: function () {',
  25728. ' return this.p.f;',
  25729. ' },',
  25730. ' set: function (v) {',
  25731. ' this.p.f = v;',
  25732. ' }',
  25733. '}));',
  25734. '$mod.DoIt($mod.THelper.ToStr.call({',
  25735. ' p: $mod,',
  25736. ' get: function () {',
  25737. ' return this.p.f;',
  25738. ' },',
  25739. ' set: function (v) {',
  25740. ' this.p.f = v;',
  25741. ' }',
  25742. '}));',
  25743. '$mod.THelper.ToStr.call({',
  25744. ' a: $mod.f * $mod.f,',
  25745. ' get: function () {',
  25746. ' return this.a;',
  25747. ' },',
  25748. ' set: function (v) {',
  25749. ' rtl.raiseE("EPropReadOnly");',
  25750. ' }',
  25751. '});',
  25752. '$mod.DoIt($mod.THelper.ToStr.call({',
  25753. ' a: $mod.f * $mod.f,',
  25754. ' get: function () {',
  25755. ' return this.a;',
  25756. ' },',
  25757. ' set: function (v) {',
  25758. ' rtl.raiseE("EPropReadOnly");',
  25759. ' }',
  25760. '}));',
  25761. '']));
  25762. end;
  25763. procedure TTestModule.TestTypeHelper_NativeInt;
  25764. begin
  25765. StartProgram(false);
  25766. Add([
  25767. '{$modeswitch typehelpers}',
  25768. 'type',
  25769. ' MaxInt = type nativeint;',
  25770. ' THelperI = type helper for MaxInt',
  25771. ' function ToStr: String;',
  25772. ' end;',
  25773. ' MaxUInt = type nativeuint;',
  25774. ' THelperU = type helper for MaxUInt',
  25775. ' function ToStr: String;',
  25776. ' end;',
  25777. 'function THelperI.ToStr: String;',
  25778. 'begin',
  25779. ' Result:=str(Self);',
  25780. 'end;',
  25781. 'function THelperU.ToStr: String;',
  25782. 'begin',
  25783. ' Result:=str(Self);',
  25784. 'end;',
  25785. 'procedure DoIt(s: string);',
  25786. 'begin',
  25787. 'end;',
  25788. 'var i: MaxInt;',
  25789. 'begin',
  25790. ' DoIt(i.toStr);',
  25791. ' DoIt(i.toStr());',
  25792. ' (i*i).toStr;',
  25793. ' DoIt((i*i).toStr);',
  25794. '']);
  25795. ConvertProgram;
  25796. CheckSource('TestTypeHelper_NativeInt',
  25797. LinesToStr([ // statements
  25798. 'rtl.createHelper(this, "THelperI", null, function () {',
  25799. ' this.ToStr = function () {',
  25800. ' var Result = "";',
  25801. ' Result = "" + this.get();',
  25802. ' return Result;',
  25803. ' };',
  25804. '});',
  25805. 'rtl.createHelper(this, "THelperU", null, function () {',
  25806. ' this.ToStr = function () {',
  25807. ' var Result = "";',
  25808. ' Result = "" + this.get();',
  25809. ' return Result;',
  25810. ' };',
  25811. '});',
  25812. 'this.DoIt = function (s) {',
  25813. '};',
  25814. 'this.i = 0;',
  25815. '']),
  25816. LinesToStr([ // $mod.$main
  25817. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25818. ' p: $mod,',
  25819. ' get: function () {',
  25820. ' return this.p.i;',
  25821. ' },',
  25822. ' set: function (v) {',
  25823. ' this.p.i = v;',
  25824. ' }',
  25825. '}));',
  25826. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25827. ' p: $mod,',
  25828. ' get: function () {',
  25829. ' return this.p.i;',
  25830. ' },',
  25831. ' set: function (v) {',
  25832. ' this.p.i = v;',
  25833. ' }',
  25834. '}));',
  25835. '$mod.THelperI.ToStr.call({',
  25836. ' a: $mod.i * $mod.i,',
  25837. ' get: function () {',
  25838. ' return this.a;',
  25839. ' },',
  25840. ' set: function (v) {',
  25841. ' rtl.raiseE("EPropReadOnly");',
  25842. ' }',
  25843. '});',
  25844. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25845. ' a: $mod.i * $mod.i,',
  25846. ' get: function () {',
  25847. ' return this.a;',
  25848. ' },',
  25849. ' set: function (v) {',
  25850. ' rtl.raiseE("EPropReadOnly");',
  25851. ' }',
  25852. '}));',
  25853. '']));
  25854. end;
  25855. procedure TTestModule.TestTypeHelper_StringChar;
  25856. begin
  25857. StartProgram(false);
  25858. Add([
  25859. '{$modeswitch typehelpers}',
  25860. 'type',
  25861. ' TStringHelper = type helper for string',
  25862. ' procedure DoIt(e: byte = 123);',
  25863. ' end;',
  25864. ' TCharHelper = type helper for char',
  25865. ' procedure Fly;',
  25866. ' end;',
  25867. 'procedure TStringHelper.DoIt(e: byte);',
  25868. 'begin',
  25869. ' Self[1]:=''c'';',
  25870. ' Self[2]:=Self[3];',
  25871. 'end;',
  25872. 'procedure TCharHelper.Fly;',
  25873. 'begin',
  25874. ' Self:=''c'';',
  25875. 'end;',
  25876. 'begin',
  25877. ' ''abc''.DoIt;',
  25878. ' ''xyz''.DoIt();',
  25879. ' ''c''.Fly();',
  25880. '']);
  25881. ConvertProgram;
  25882. CheckSource('TestTypeHelper_StringChar',
  25883. LinesToStr([ // statements
  25884. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  25885. ' this.DoIt = function (e) {',
  25886. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  25887. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  25888. ' };',
  25889. '});',
  25890. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  25891. ' this.Fly = function () {',
  25892. ' this.set("c");',
  25893. ' };',
  25894. '});',
  25895. '']),
  25896. LinesToStr([ // $mod.$main
  25897. '$mod.TStringHelper.DoIt.call({',
  25898. ' get: function () {',
  25899. ' return "abc";',
  25900. ' },',
  25901. ' set: function (v) {',
  25902. ' rtl.raiseE("EPropReadOnly");',
  25903. ' }',
  25904. '}, 123);',
  25905. '$mod.TStringHelper.DoIt.call({',
  25906. ' get: function () {',
  25907. ' return "xyz";',
  25908. ' },',
  25909. ' set: function (v) {',
  25910. ' rtl.raiseE("EPropReadOnly");',
  25911. ' }',
  25912. '}, 123);',
  25913. '$mod.TCharHelper.Fly.call({',
  25914. ' get: function () {',
  25915. ' return "c";',
  25916. ' },',
  25917. ' set: function (v) {',
  25918. ' rtl.raiseE("EPropReadOnly");',
  25919. ' }',
  25920. '});',
  25921. '']));
  25922. end;
  25923. procedure TTestModule.TestTypeHelper_JSValue;
  25924. begin
  25925. StartProgram(false);
  25926. Add([
  25927. '{$modeswitch typehelpers}',
  25928. 'type',
  25929. ' TExtValue = type jsvalue;',
  25930. ' THelper = type helper for TExtValue',
  25931. ' function ToStr: String;',
  25932. ' end;',
  25933. 'function THelper.ToStr: String;',
  25934. 'begin',
  25935. 'end;',
  25936. 'var',
  25937. ' s: string;',
  25938. ' v: TExtValue;',
  25939. 'begin',
  25940. ' s:=v.toStr;',
  25941. ' s:=v.toStr();',
  25942. ' TExtValue(s).toStr;',
  25943. '']);
  25944. ConvertProgram;
  25945. CheckSource('TestTypeHelper_JSValue',
  25946. LinesToStr([ // statements
  25947. 'rtl.createHelper(this, "THelper", null, function () {',
  25948. ' this.ToStr = function () {',
  25949. ' var Result = "";',
  25950. ' return Result;',
  25951. ' };',
  25952. '});',
  25953. 'this.s = "";',
  25954. 'this.v = undefined;',
  25955. '']),
  25956. LinesToStr([ // $mod.$main
  25957. '$mod.s = $mod.THelper.ToStr.call({',
  25958. ' p: $mod,',
  25959. ' get: function () {',
  25960. ' return this.p.v;',
  25961. ' },',
  25962. ' set: function (v) {',
  25963. ' this.p.v = v;',
  25964. ' }',
  25965. '});',
  25966. '$mod.s = $mod.THelper.ToStr.call({',
  25967. ' p: $mod,',
  25968. ' get: function () {',
  25969. ' return this.p.v;',
  25970. ' },',
  25971. ' set: function (v) {',
  25972. ' this.p.v = v;',
  25973. ' }',
  25974. '});',
  25975. '$mod.THelper.ToStr.call({',
  25976. ' p: $mod,',
  25977. ' get: function () {',
  25978. ' return this.p.s;',
  25979. ' },',
  25980. ' set: function (v) {',
  25981. ' rtl.raiseE("EPropReadOnly");',
  25982. ' }',
  25983. '});',
  25984. '']));
  25985. end;
  25986. procedure TTestModule.TestTypeHelper_Array;
  25987. begin
  25988. StartProgram(false);
  25989. Add([
  25990. '{$modeswitch typehelpers}',
  25991. 'type',
  25992. ' TArrOfBool = array of boolean;',
  25993. ' TArrOfJS = array of jsvalue;',
  25994. ' THelper = type helper for TArrOfBool',
  25995. ' procedure DoIt(e: byte = 123);',
  25996. ' end;',
  25997. 'procedure THelper.DoIt(e: byte);',
  25998. 'begin',
  25999. ' Self[1]:=true;',
  26000. ' Self[2]:=not Self[3];',
  26001. ' SetLength(Self,4);',
  26002. 'end;',
  26003. 'var',
  26004. ' b: TArrOfBool;',
  26005. ' j: TArrOfJS;',
  26006. 'begin',
  26007. ' b.DoIt;',
  26008. ' TArrOfBool(j).DoIt();',
  26009. '']);
  26010. ConvertProgram;
  26011. CheckSource('TestTypeHelper_Array',
  26012. LinesToStr([ // statements
  26013. 'rtl.createHelper(this, "THelper", null, function () {',
  26014. ' this.DoIt = function (e) {',
  26015. ' this.get()[1] = true;',
  26016. ' this.get()[2] = !this.get()[3];',
  26017. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26018. ' };',
  26019. '});',
  26020. 'this.b = [];',
  26021. 'this.j = [];',
  26022. '']),
  26023. LinesToStr([ // $mod.$main
  26024. '$mod.THelper.DoIt.call({',
  26025. ' p: $mod,',
  26026. ' get: function () {',
  26027. ' return this.p.b;',
  26028. ' },',
  26029. ' set: function (v) {',
  26030. ' this.p.b = v;',
  26031. ' }',
  26032. '}, 123);',
  26033. '$mod.THelper.DoIt.call({',
  26034. ' p: $mod,',
  26035. ' get: function () {',
  26036. ' return this.p.j;',
  26037. ' },',
  26038. ' set: function (v) {',
  26039. ' this.p.j = v;',
  26040. ' }',
  26041. '}, 123);',
  26042. '']));
  26043. end;
  26044. procedure TTestModule.TestTypeHelper_EnumType;
  26045. begin
  26046. StartProgram(false);
  26047. Add([
  26048. '{$modeswitch typehelpers}',
  26049. 'type',
  26050. ' TEnum = (red,blue);',
  26051. ' THelper = type helper for TEnum',
  26052. ' procedure DoIt(e: byte = 123);',
  26053. ' class procedure Swing(w: word); static;',
  26054. ' end;',
  26055. 'procedure THelper.DoIt(e: byte);',
  26056. 'begin',
  26057. ' Self:=red;',
  26058. ' Self:=succ(Self);',
  26059. ' with Self do Doit;',
  26060. 'end;',
  26061. 'class procedure THelper.Swing(w: word);',
  26062. 'begin',
  26063. 'end;',
  26064. 'var e: TEnum;',
  26065. 'begin',
  26066. ' e.DoIt;',
  26067. ' red.DoIt;',
  26068. ' TEnum.blue.DoIt;',
  26069. ' TEnum(1).DoIt;',
  26070. ' TEnum.Swing(3);',
  26071. '']);
  26072. ConvertProgram;
  26073. CheckSource('TestTypeHelper_EnumType',
  26074. LinesToStr([ // statements
  26075. 'this.TEnum = {',
  26076. ' "0": "red",',
  26077. ' red: 0,',
  26078. ' "1": "blue",',
  26079. ' blue: 1',
  26080. '};',
  26081. 'rtl.createHelper(this, "THelper", null, function () {',
  26082. ' this.DoIt = function (e) {',
  26083. ' this.set($mod.TEnum.red);',
  26084. ' this.set(this.get() + 1);',
  26085. ' var $with = this.get();',
  26086. ' $mod.THelper.DoIt.call(this, 123);',
  26087. ' };',
  26088. ' this.Swing = function (w) {',
  26089. ' };',
  26090. '});',
  26091. 'this.e = 0;',
  26092. '']),
  26093. LinesToStr([ // $mod.$main
  26094. '$mod.THelper.DoIt.call({',
  26095. ' p: $mod,',
  26096. ' get: function () {',
  26097. ' return this.p.e;',
  26098. ' },',
  26099. ' set: function (v) {',
  26100. ' this.p.e = v;',
  26101. ' }',
  26102. '}, 123);',
  26103. '$mod.THelper.DoIt.call({',
  26104. ' p: $mod.TEnum,',
  26105. ' get: function () {',
  26106. ' return this.p.red;',
  26107. ' },',
  26108. ' set: function (v) {',
  26109. ' rtl.raiseE("EPropReadOnly");',
  26110. ' }',
  26111. '}, 123);',
  26112. '$mod.THelper.DoIt.call({',
  26113. ' p: $mod.TEnum,',
  26114. ' get: function () {',
  26115. ' return this.p.blue;',
  26116. ' },',
  26117. ' set: function (v) {',
  26118. ' rtl.raiseE("EPropReadOnly");',
  26119. ' }',
  26120. '}, 123);',
  26121. '$mod.THelper.DoIt.call({',
  26122. ' get: function () {',
  26123. ' return 1;',
  26124. ' },',
  26125. ' set: function (v) {',
  26126. ' rtl.raiseE("EPropReadOnly");',
  26127. ' }',
  26128. '}, 123);',
  26129. '$mod.THelper.Swing(3);',
  26130. '']));
  26131. end;
  26132. procedure TTestModule.TestTypeHelper_SetType;
  26133. begin
  26134. StartProgram(false);
  26135. Add([
  26136. '{$modeswitch typehelpers}',
  26137. 'type',
  26138. ' TEnum = (red,blue);',
  26139. ' TSetOfEnum = set of TEnum;',
  26140. ' THelper = type helper for TSetOfEnum',
  26141. ' procedure DoIt(e: byte = 123);',
  26142. ' constructor Init(e: TEnum);',
  26143. ' constructor InitEmpty;',
  26144. ' end;',
  26145. 'procedure THelper.DoIt(e: byte);',
  26146. 'begin',
  26147. ' Self:=[];',
  26148. ' Self:=[red];',
  26149. ' Include(Self,blue);',
  26150. 'end;',
  26151. 'constructor THelper.Init(e: TEnum);',
  26152. 'begin',
  26153. ' Self:=[];',
  26154. ' Self:=[e];',
  26155. ' Include(Self,blue);',
  26156. 'end;',
  26157. 'constructor THelper.InitEmpty;',
  26158. 'begin',
  26159. 'end;',
  26160. 'var s: TSetOfEnum;',
  26161. 'begin',
  26162. ' s.DoIt;',
  26163. //' [red].DoIt;',
  26164. //' with s do DoIt;',
  26165. //' with [red,blue] do DoIt;',
  26166. ' s:=TSetOfEnum.Init(blue);',
  26167. ' s:=s.Init(blue);',
  26168. '']);
  26169. ConvertProgram;
  26170. CheckSource('TestTypeHelper_SetType',
  26171. LinesToStr([ // statements
  26172. 'this.TEnum = {',
  26173. ' "0": "red",',
  26174. ' red: 0,',
  26175. ' "1": "blue",',
  26176. ' blue: 1',
  26177. '};',
  26178. 'rtl.createHelper(this, "THelper", null, function () {',
  26179. ' this.DoIt = function (e) {',
  26180. ' this.set({});',
  26181. ' this.set(rtl.createSet($mod.TEnum.red));',
  26182. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26183. ' };',
  26184. ' this.Init = function (e) {',
  26185. ' this.set({});',
  26186. ' this.set(rtl.createSet(e));',
  26187. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26188. ' return this.get();',
  26189. ' };',
  26190. ' this.InitEmpty = function () {',
  26191. ' return this.get();',
  26192. ' };',
  26193. ' this.$new = function (fn, args) {',
  26194. ' return this[fn].apply({',
  26195. ' p: {},',
  26196. ' get: function () {',
  26197. ' return this.p;',
  26198. ' },',
  26199. ' set: function (v) {',
  26200. ' this.p = v;',
  26201. ' }',
  26202. ' }, args);',
  26203. ' };',
  26204. '});',
  26205. 'this.s = {};',
  26206. '']),
  26207. LinesToStr([ // $mod.$main
  26208. '$mod.THelper.DoIt.call({',
  26209. ' p: $mod,',
  26210. ' get: function () {',
  26211. ' return this.p.s;',
  26212. ' },',
  26213. ' set: function (v) {',
  26214. ' this.p.s = v;',
  26215. ' }',
  26216. '}, 123);',
  26217. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26218. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26219. ' p: $mod,',
  26220. ' get: function () {',
  26221. ' return this.p.s;',
  26222. ' },',
  26223. ' set: function (v) {',
  26224. ' this.p.s = v;',
  26225. ' }',
  26226. '}, $mod.TEnum.blue));',
  26227. '']));
  26228. end;
  26229. procedure TTestModule.TestTypeHelper_InterfaceType;
  26230. begin
  26231. StartProgram(false);
  26232. Add([
  26233. '{$interfaces com}',
  26234. '{$modeswitch typehelpers}',
  26235. 'type',
  26236. ' IUnknown = interface',
  26237. ' function _AddRef: longint;',
  26238. ' function _Release: longint;',
  26239. ' end;',
  26240. ' TObject = class(IUnknown)',
  26241. ' function _AddRef: longint; virtual; abstract;',
  26242. ' function _Release: longint; virtual; abstract;',
  26243. ' end;',
  26244. ' THelper = type helper for IUnknown',
  26245. ' procedure Fly(e: byte = 123);',
  26246. ' class procedure Run; static;',
  26247. ' end;',
  26248. 'var',
  26249. ' i: IUnknown;',
  26250. ' o: TObject;',
  26251. 'procedure THelper.Fly(e: byte);',
  26252. 'begin',
  26253. ' i:=Self;',
  26254. ' o:=Self as TObject;',
  26255. ' Self:=nil;',
  26256. ' Self:=i;',
  26257. ' Self:=o;',
  26258. ' with Self do begin',
  26259. ' Fly;',
  26260. ' Fly();',
  26261. ' end;',
  26262. 'end;',
  26263. 'class procedure THelper.Run;',
  26264. 'var l: IUnknown;',
  26265. 'begin',
  26266. ' l.Fly;',
  26267. ' l.Fly();',
  26268. 'end;',
  26269. 'begin',
  26270. ' i.Fly;',
  26271. ' i.Fly();',
  26272. ' i.Run;',
  26273. ' i.Run();',
  26274. ' IUnknown.Run;',
  26275. ' IUnknown.Run();',
  26276. '']);
  26277. ConvertProgram;
  26278. CheckSource('TestTypeHelper_InterfaceType',
  26279. LinesToStr([ // statements
  26280. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  26281. 'rtl.createClass(this, "TObject", null, function () {',
  26282. ' this.$init = function () {',
  26283. ' };',
  26284. ' this.$final = function () {',
  26285. ' };',
  26286. ' rtl.addIntf(this, $mod.IUnknown);',
  26287. '});',
  26288. 'rtl.createHelper(this, "THelper", null, function () {',
  26289. ' this.Fly = function (e) {',
  26290. ' var $ir = rtl.createIntfRefs();',
  26291. ' try {',
  26292. ' rtl.setIntfP($mod, "i", this.get());',
  26293. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  26294. ' this.set(null);',
  26295. ' this.set($mod.i);',
  26296. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  26297. ' var $with = this.get();',
  26298. ' $mod.THelper.Fly.call(this, 123);',
  26299. ' $mod.THelper.Fly.call(this, 123);',
  26300. ' } finally {',
  26301. ' $ir.free();',
  26302. ' };',
  26303. ' };',
  26304. ' this.Run = function () {',
  26305. ' var l = null;',
  26306. ' try {',
  26307. ' $mod.THelper.Fly.call({',
  26308. ' get: function () {',
  26309. ' return l;',
  26310. ' },',
  26311. ' set: function (v) {',
  26312. ' l = rtl.setIntfL(l, v);',
  26313. ' }',
  26314. ' }, 123);',
  26315. ' $mod.THelper.Fly.call({',
  26316. ' get: function () {',
  26317. ' return l;',
  26318. ' },',
  26319. ' set: function (v) {',
  26320. ' l = rtl.setIntfL(l, v);',
  26321. ' }',
  26322. ' }, 123);',
  26323. ' } finally {',
  26324. ' rtl._Release(l);',
  26325. ' };',
  26326. ' };',
  26327. '});',
  26328. 'this.i = null;',
  26329. 'this.o = null;',
  26330. '']),
  26331. LinesToStr([ // $mod.$main
  26332. '$mod.THelper.Fly.call({',
  26333. ' p: $mod,',
  26334. ' get: function () {',
  26335. ' return this.p.i;',
  26336. ' },',
  26337. ' set: function (v) {',
  26338. ' rtl.setIntfP(this.p, "i", v);',
  26339. ' }',
  26340. '}, 123);',
  26341. '$mod.THelper.Fly.call({',
  26342. ' p: $mod,',
  26343. ' get: function () {',
  26344. ' return this.p.i;',
  26345. ' },',
  26346. ' set: function (v) {',
  26347. ' rtl.setIntfP(this.p, "i", v);',
  26348. ' }',
  26349. '}, 123);',
  26350. '$mod.THelper.Run();',
  26351. '$mod.THelper.Run();',
  26352. '$mod.THelper.Run();',
  26353. '$mod.THelper.Run();',
  26354. '']));
  26355. end;
  26356. procedure TTestModule.TestTypeHelper_NestedSelf;
  26357. begin
  26358. StartProgram(false);
  26359. Add([
  26360. '{$modeswitch typehelpers}',
  26361. 'type',
  26362. ' THelper = type helper for string',
  26363. ' procedure Run(Value: string);',
  26364. ' end;',
  26365. 'procedure THelper.Run(Value: string);',
  26366. ' function Sub(i: nativeint): boolean;',
  26367. ' begin',
  26368. ' Result:=Self[i+1]=Value[i];',
  26369. ' end;',
  26370. 'begin',
  26371. ' if Self[3]=Value[4] then ;',
  26372. 'end;',
  26373. 'begin',
  26374. '']);
  26375. ConvertProgram;
  26376. CheckSource('TestTypeHelper_NestedSelf',
  26377. LinesToStr([ // statements
  26378. 'rtl.createHelper(this, "THelper", null, function () {',
  26379. ' this.Run = function (Value) {',
  26380. ' var $Self = this;',
  26381. ' function Sub(i) {',
  26382. ' var Result = false;',
  26383. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  26384. ' return Result;',
  26385. ' };',
  26386. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  26387. ' };',
  26388. '});',
  26389. '']),
  26390. LinesToStr([ // $mod.$main
  26391. '']));
  26392. end;
  26393. procedure TTestModule.TestProcType;
  26394. begin
  26395. StartProgram(false);
  26396. Add([
  26397. 'type',
  26398. ' TProcInt = procedure(vI: longint = 1);',
  26399. 'procedure DoIt(vJ: longint);',
  26400. 'begin end;',
  26401. 'var',
  26402. ' b: boolean;',
  26403. ' vP, vQ: tprocint;',
  26404. 'begin',
  26405. ' vp:=nil;',
  26406. ' vp:=vp;',
  26407. ' vp:=@doit;',
  26408. ' vp;',
  26409. ' vp();',
  26410. ' vp(2);',
  26411. ' b:=vp=nil;',
  26412. ' b:=nil=vp;',
  26413. ' b:=vp=vq;',
  26414. ' b:=vp=@doit;',
  26415. ' b:=@doit=vp;',
  26416. ' b:=vp<>nil;',
  26417. ' b:=nil<>vp;',
  26418. ' b:=vp<>vq;',
  26419. ' b:=vp<>@doit;',
  26420. ' b:=@doit<>vp;',
  26421. ' b:=Assigned(vp);',
  26422. ' if Assigned(vp) then ;']);
  26423. ConvertProgram;
  26424. CheckSource('TestProcType',
  26425. LinesToStr([ // statements
  26426. 'this.DoIt = function(vJ) {',
  26427. '};',
  26428. 'this.b = false;',
  26429. 'this.vP = null;',
  26430. 'this.vQ = null;'
  26431. ]),
  26432. LinesToStr([ // $mod.$main
  26433. '$mod.vP = null;',
  26434. '$mod.vP = $mod.vP;',
  26435. '$mod.vP = $mod.DoIt;',
  26436. '$mod.vP(1);',
  26437. '$mod.vP(1);',
  26438. '$mod.vP(2);',
  26439. '$mod.b = $mod.vP === null;',
  26440. '$mod.b = null === $mod.vP;',
  26441. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  26442. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26443. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26444. '$mod.b = $mod.vP !== null;',
  26445. '$mod.b = null !== $mod.vP;',
  26446. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  26447. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26448. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26449. '$mod.b = $mod.vP != null;',
  26450. 'if ($mod.vP != null) ;',
  26451. '']));
  26452. end;
  26453. procedure TTestModule.TestProcType_Arg;
  26454. begin
  26455. StartProgram(false);
  26456. Add([
  26457. 'type',
  26458. ' TProcInt = procedure(vI: longint = 1);',
  26459. 'procedure DoIt(vJ: longint); begin end;',
  26460. 'procedure DoSome(vP, vQ: TProcInt);',
  26461. 'var',
  26462. ' b: boolean;',
  26463. 'begin',
  26464. ' vp:=nil;',
  26465. ' vp:=vp;',
  26466. ' vp:=@doit;',
  26467. ' vp;',
  26468. ' vp();',
  26469. ' vp(2);',
  26470. ' b:=vp=nil;',
  26471. ' b:=nil=vp;',
  26472. ' b:=vp=vq;',
  26473. ' b:=vp=@doit;',
  26474. ' b:=@doit=vp;',
  26475. ' b:=vp<>nil;',
  26476. ' b:=nil<>vp;',
  26477. ' b:=vp<>vq;',
  26478. ' b:=vp<>@doit;',
  26479. ' b:=@doit<>vp;',
  26480. ' b:=Assigned(vp);',
  26481. ' if Assigned(vp) then ;',
  26482. 'end;',
  26483. 'begin',
  26484. ' DoSome(@DoIt,nil);']);
  26485. ConvertProgram;
  26486. CheckSource('TestProcType_Arg',
  26487. LinesToStr([ // statements
  26488. 'this.DoIt = function(vJ) {',
  26489. '};',
  26490. 'this.DoSome = function(vP, vQ) {',
  26491. ' var b = false;',
  26492. ' vP = null;',
  26493. ' vP = vP;',
  26494. ' vP = $mod.DoIt;',
  26495. ' vP(1);',
  26496. ' vP(1);',
  26497. ' vP(2);',
  26498. ' b = vP === null;',
  26499. ' b = null === vP;',
  26500. ' b = rtl.eqCallback(vP,vQ);',
  26501. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  26502. ' b = rtl.eqCallback($mod.DoIt, vP);',
  26503. ' b = vP !== null;',
  26504. ' b = null !== vP;',
  26505. ' b = !rtl.eqCallback(vP, vQ);',
  26506. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  26507. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  26508. ' b = vP != null;',
  26509. ' if (vP != null) ;',
  26510. '};',
  26511. '']),
  26512. LinesToStr([ // $mod.$main
  26513. '$mod.DoSome($mod.DoIt,null);',
  26514. '']));
  26515. end;
  26516. procedure TTestModule.TestProcType_FunctionFPC;
  26517. begin
  26518. StartProgram(false);
  26519. Add('type');
  26520. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26521. Add('function DoIt(vI: longint): longint;');
  26522. Add('begin end;');
  26523. Add('var');
  26524. Add(' b: boolean;');
  26525. Add(' vP, vQ: tfuncint;');
  26526. Add('begin');
  26527. Add(' vp:=nil;');
  26528. Add(' vp:=vp;');
  26529. Add(' vp:=@doit;'); // ok in fpc and delphi
  26530. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26531. Add(' vp;'); // ok in fpc and delphi
  26532. Add(' vp();');
  26533. Add(' vp(2);');
  26534. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26535. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26536. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26537. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26538. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26539. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  26540. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  26541. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26542. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26543. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26544. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26545. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26546. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  26547. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  26548. Add(' b:=Assigned(vp);');
  26549. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  26550. Add(' doit(vp());'); // ok in fpc and delphi
  26551. Add(' doit(vp(2));'); // ok in fpc and delphi
  26552. ConvertProgram;
  26553. CheckSource('TestProcType_FunctionFPC',
  26554. LinesToStr([ // statements
  26555. 'this.DoIt = function(vI) {',
  26556. ' var Result = 0;',
  26557. ' return Result;',
  26558. '};',
  26559. 'this.b = false;',
  26560. 'this.vP = null;',
  26561. 'this.vQ = null;'
  26562. ]),
  26563. LinesToStr([ // $mod.$main
  26564. '$mod.vP = null;',
  26565. '$mod.vP = $mod.vP;',
  26566. '$mod.vP = $mod.DoIt;',
  26567. '$mod.vP(1);',
  26568. '$mod.vP(1);',
  26569. '$mod.vP(2);',
  26570. '$mod.b = $mod.vP === null;',
  26571. '$mod.b = null === $mod.vP;',
  26572. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  26573. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26574. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26575. '$mod.b = 4 === $mod.vP(1);',
  26576. '$mod.b = $mod.vP !== null;',
  26577. '$mod.b = null !== $mod.vP;',
  26578. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  26579. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26580. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26581. '$mod.b = 6 !== $mod.vP(1);',
  26582. '$mod.b = $mod.vP != null;',
  26583. '$mod.DoIt($mod.vP(1));',
  26584. '$mod.DoIt($mod.vP(2));',
  26585. '']));
  26586. end;
  26587. procedure TTestModule.TestProcType_FunctionDelphi;
  26588. begin
  26589. StartProgram(false);
  26590. Add('{$mode Delphi}');
  26591. Add('type');
  26592. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26593. Add('function DoIt(vI: longint): longint;');
  26594. Add('begin end;');
  26595. Add('var');
  26596. Add(' b: boolean;');
  26597. Add(' vP, vQ: tfuncint;');
  26598. Add('begin');
  26599. Add(' vp:=nil;');
  26600. Add(' vp:=vp;');
  26601. Add(' vp:=@doit;'); // ok in fpc and delphi
  26602. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26603. Add(' vp;'); // ok in fpc and delphi
  26604. Add(' vp();');
  26605. Add(' vp(2);');
  26606. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26607. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26608. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26609. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26610. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26611. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  26612. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  26613. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26614. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26615. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26616. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26617. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26618. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  26619. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  26620. Add(' b:=Assigned(vp);');
  26621. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  26622. Add(' doit(vp());'); // ok in fpc and delphi
  26623. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  26624. ConvertProgram;
  26625. CheckSource('TestProcType_FunctionDelphi',
  26626. LinesToStr([ // statements
  26627. 'this.DoIt = function(vI) {',
  26628. ' var Result = 0;',
  26629. ' return Result;',
  26630. '};',
  26631. 'this.b = false;',
  26632. 'this.vP = null;',
  26633. 'this.vQ = null;'
  26634. ]),
  26635. LinesToStr([ // $mod.$main
  26636. '$mod.vP = null;',
  26637. '$mod.vP = $mod.vP;',
  26638. '$mod.vP = $mod.DoIt;',
  26639. '$mod.vP = $mod.DoIt;',
  26640. '$mod.vP(1);',
  26641. '$mod.vP(1);',
  26642. '$mod.vP(2);',
  26643. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  26644. '$mod.b = $mod.vP(1) === 3;',
  26645. '$mod.b = 4 === $mod.vP(1);',
  26646. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  26647. '$mod.b = $mod.vP(1) !== 5;',
  26648. '$mod.b = 6 !== $mod.vP(1);',
  26649. '$mod.b = $mod.vP != null;',
  26650. '$mod.DoIt($mod.vP(1));',
  26651. '$mod.DoIt($mod.vP(1));',
  26652. '$mod.DoIt($mod.vP(2));',
  26653. '']));
  26654. end;
  26655. procedure TTestModule.TestProcType_ProcedureDelphi;
  26656. begin
  26657. StartProgram(false);
  26658. Add('{$mode Delphi}');
  26659. Add('type');
  26660. Add(' TProc = procedure;');
  26661. Add('procedure DoIt;');
  26662. Add('begin end;');
  26663. Add('var');
  26664. Add(' b: boolean;');
  26665. Add(' vP, vQ: tproc;');
  26666. Add('begin');
  26667. Add(' vp:=nil;');
  26668. Add(' vp:=vp;');
  26669. Add(' vp:=vq;');
  26670. 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
  26671. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26672. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  26673. Add(' vp;'); // ok in fpc and delphi
  26674. Add(' vp();');
  26675. // equal
  26676. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26677. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  26678. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26679. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26680. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  26681. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26682. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26683. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  26684. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26685. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26686. // unequal
  26687. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26688. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  26689. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26690. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  26691. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26692. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  26693. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26694. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  26695. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26696. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  26697. Add(' b:=Assigned(vp);');
  26698. ConvertProgram;
  26699. CheckSource('TestProcType_ProcedureDelphi',
  26700. LinesToStr([ // statements
  26701. 'this.DoIt = function() {',
  26702. '};',
  26703. 'this.b = false;',
  26704. 'this.vP = null;',
  26705. 'this.vQ = null;'
  26706. ]),
  26707. LinesToStr([ // $mod.$main
  26708. '$mod.vP = null;',
  26709. '$mod.vP = $mod.vP;',
  26710. '$mod.vP = $mod.vQ;',
  26711. '$mod.vP = $mod.DoIt;',
  26712. '$mod.vP = $mod.DoIt;',
  26713. '$mod.vP();',
  26714. '$mod.vP();',
  26715. '$mod.b = $mod.vP === null;',
  26716. '$mod.b = null === $mod.vP;',
  26717. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  26718. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26719. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26720. '$mod.b = $mod.vP !== null;',
  26721. '$mod.b = null !== $mod.vP;',
  26722. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  26723. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26724. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26725. '$mod.b = $mod.vP != null;',
  26726. '']));
  26727. end;
  26728. procedure TTestModule.TestProcType_AsParam;
  26729. begin
  26730. StartProgram(false);
  26731. Add('type');
  26732. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26733. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  26734. Add('var vJ: tfuncint;');
  26735. Add('begin');
  26736. Add(' vg:=vg;');
  26737. Add(' vj:=vh;');
  26738. Add(' vi:=vi;');
  26739. Add(' doit(vg,vg,vg);');
  26740. Add(' doit(vh,vh,vj);');
  26741. Add(' doit(vi,vi,vi);');
  26742. Add(' doit(vj,vj,vj);');
  26743. Add('end;');
  26744. Add('var i: tfuncint;');
  26745. Add('begin');
  26746. Add(' doit(i,i,i);');
  26747. ConvertProgram;
  26748. CheckSource('TestProcType_AsParam',
  26749. LinesToStr([ // statements
  26750. 'this.DoIt = function (vG,vH,vI) {',
  26751. ' var vJ = null;',
  26752. ' vG = vG;',
  26753. ' vJ = vH;',
  26754. ' vI.set(vI.get());',
  26755. ' $mod.DoIt(vG, vG, {',
  26756. ' get: function () {',
  26757. ' return vG;',
  26758. ' },',
  26759. ' set: function (v) {',
  26760. ' vG = v;',
  26761. ' }',
  26762. ' });',
  26763. ' $mod.DoIt(vH, vH, {',
  26764. ' get: function () {',
  26765. ' return vJ;',
  26766. ' },',
  26767. ' set: function (v) {',
  26768. ' vJ = v;',
  26769. ' }',
  26770. ' });',
  26771. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  26772. ' $mod.DoIt(vJ, vJ, {',
  26773. ' get: function () {',
  26774. ' return vJ;',
  26775. ' },',
  26776. ' set: function (v) {',
  26777. ' vJ = v;',
  26778. ' }',
  26779. ' });',
  26780. '};',
  26781. 'this.i = null;'
  26782. ]),
  26783. LinesToStr([
  26784. '$mod.DoIt($mod.i,$mod.i,{',
  26785. ' p: $mod,',
  26786. ' get: function () {',
  26787. ' return this.p.i;',
  26788. ' },',
  26789. ' set: function (v) {',
  26790. ' this.p.i = v;',
  26791. ' }',
  26792. '});'
  26793. ]));
  26794. end;
  26795. procedure TTestModule.TestProcType_MethodFPC;
  26796. begin
  26797. StartProgram(false);
  26798. Add('type');
  26799. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26800. Add(' TObject = class');
  26801. Add(' function DoIt(vA: longint = 1): longint;');
  26802. Add(' end;');
  26803. Add('function TObject.DoIt(vA: longint = 1): longint;');
  26804. Add('begin');
  26805. Add('end;');
  26806. Add('var');
  26807. Add(' Obj: TObject;');
  26808. Add(' vP: tfuncint;');
  26809. Add(' b: boolean;');
  26810. Add('begin');
  26811. Add(' vp:[email protected];'); // ok in fpc and delphi
  26812. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  26813. Add(' vp;'); // ok in fpc and delphi
  26814. Add(' vp();');
  26815. Add(' vp(2);');
  26816. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  26817. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  26818. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  26819. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  26820. ConvertProgram;
  26821. CheckSource('TestProcType_MethodFPC',
  26822. LinesToStr([ // statements
  26823. 'rtl.createClass(this, "TObject", null, function () {',
  26824. ' this.$init = function () {',
  26825. ' };',
  26826. ' this.$final = function () {',
  26827. ' };',
  26828. ' this.DoIt = function (vA) {',
  26829. ' var Result = 0;',
  26830. ' return Result;',
  26831. ' };',
  26832. '});',
  26833. 'this.Obj = null;',
  26834. 'this.vP = null;',
  26835. 'this.b = false;'
  26836. ]),
  26837. LinesToStr([
  26838. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26839. '$mod.vP(1);',
  26840. '$mod.vP(1);',
  26841. '$mod.vP(2);',
  26842. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  26843. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  26844. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  26845. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  26846. '']));
  26847. end;
  26848. procedure TTestModule.TestProcType_MethodDelphi;
  26849. begin
  26850. StartProgram(false);
  26851. Add([
  26852. '{$mode delphi}',
  26853. 'type',
  26854. ' TFuncInt = function(vA: longint = 1): longint of object;',
  26855. ' TObject = class',
  26856. ' function DoIt(vA: longint = 1): longint;',
  26857. ' end;',
  26858. 'function TObject.DoIt(vA: longint = 1): longint;',
  26859. 'begin',
  26860. 'end;',
  26861. 'var',
  26862. ' Obj: TObject;',
  26863. ' vP: tfuncint;',
  26864. ' b: boolean;',
  26865. 'begin',
  26866. ' vp:[email protected];', // ok in fpc and delphi
  26867. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  26868. ' vp;', // ok in fpc and delphi
  26869. ' vp();',
  26870. ' vp(2);',
  26871. //' b:[email protected];', // ok in fpc, illegal in delphi
  26872. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  26873. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  26874. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  26875. '']);
  26876. ConvertProgram;
  26877. CheckSource('TestProcType_MethodDelphi',
  26878. LinesToStr([ // statements
  26879. 'rtl.createClass(this, "TObject", null, function () {',
  26880. ' this.$init = function () {',
  26881. ' };',
  26882. ' this.$final = function () {',
  26883. ' };',
  26884. ' this.DoIt = function (vA) {',
  26885. ' var Result = 0;',
  26886. ' return Result;',
  26887. ' };',
  26888. '});',
  26889. 'this.Obj = null;',
  26890. 'this.vP = null;',
  26891. 'this.b = false;'
  26892. ]),
  26893. LinesToStr([
  26894. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26895. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26896. '$mod.vP(1);',
  26897. '$mod.vP(1);',
  26898. '$mod.vP(2);',
  26899. '']));
  26900. end;
  26901. procedure TTestModule.TestProcType_PropertyFPC;
  26902. begin
  26903. StartProgram(false);
  26904. Add('type');
  26905. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26906. Add(' TObject = class');
  26907. Add(' FOnFoo: TFuncInt;');
  26908. Add(' function DoIt(vA: longint = 1): longint;');
  26909. Add(' function GetFoo: TFuncInt;');
  26910. Add(' procedure SetFoo(const Value: TFuncInt);');
  26911. Add(' function GetEvents(Index: longint): TFuncInt;');
  26912. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26913. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26914. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26915. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26916. Add(' end;');
  26917. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26918. Add('function tobject.getfoo: tfuncint; begin end;');
  26919. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26920. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26921. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26922. Add('var');
  26923. Add(' Obj: TObject;');
  26924. Add(' vP: tfuncint;');
  26925. Add(' b: boolean;');
  26926. Add('begin');
  26927. Add(' obj.onfoo:=nil;');
  26928. Add(' obj.onbar:=nil;');
  26929. Add(' obj.events[1]:=nil;');
  26930. Add(' obj.onfoo:=obj.onfoo;');
  26931. Add(' obj.onbar:=obj.onbar;');
  26932. Add(' obj.events[2]:=obj.events[3];');
  26933. Add(' obj.onfoo:[email protected];');
  26934. Add(' obj.onbar:[email protected];');
  26935. Add(' obj.events[4]:[email protected];');
  26936. //Add(' obj.onfoo:=obj.doit;'); // delphi
  26937. //Add(' obj.onbar:=obj.doit;'); // delphi
  26938. //Add(' obj.events[4]:=obj.doit;'); // delphi
  26939. Add(' obj.onfoo;');
  26940. Add(' obj.onbar;');
  26941. //Add(' obj.events[5];'); ToDo in pasresolver
  26942. Add(' obj.onfoo();');
  26943. Add(' obj.onbar();');
  26944. Add(' obj.events[6]();');
  26945. Add(' b:=obj.onfoo=nil;');
  26946. Add(' b:=obj.onbar=nil;');
  26947. Add(' b:=obj.events[7]=nil;');
  26948. Add(' b:=obj.onfoo<>nil;');
  26949. Add(' b:=obj.onbar<>nil;');
  26950. Add(' b:=obj.events[8]<>nil;');
  26951. Add(' b:=obj.onfoo=vp;');
  26952. Add(' b:=obj.onbar=vp;');
  26953. Add(' b:=obj.events[9]=vp;');
  26954. Add(' b:=obj.onfoo=obj.onfoo;');
  26955. Add(' b:=obj.onbar=obj.onfoo;');
  26956. Add(' b:=obj.events[10]=obj.onfoo;');
  26957. Add(' b:=obj.onfoo<>obj.onfoo;');
  26958. Add(' b:=obj.onbar<>obj.onfoo;');
  26959. Add(' b:=obj.events[11]<>obj.onfoo;');
  26960. Add(' b:[email protected];');
  26961. Add(' b:[email protected];');
  26962. Add(' b:=obj.events[12][email protected];');
  26963. Add(' b:=obj.onfoo<>@obj.doit;');
  26964. Add(' b:=obj.onbar<>@obj.doit;');
  26965. Add(' b:=obj.events[12]<>@obj.doit;');
  26966. Add(' b:=Assigned(obj.onfoo);');
  26967. Add(' b:=Assigned(obj.onbar);');
  26968. Add(' b:=Assigned(obj.events[13]);');
  26969. ConvertProgram;
  26970. CheckSource('TestProcType_PropertyFPC',
  26971. LinesToStr([ // statements
  26972. 'rtl.createClass(this, "TObject", null, function () {',
  26973. ' this.$init = function () {',
  26974. ' this.FOnFoo = null;',
  26975. ' };',
  26976. ' this.$final = function () {',
  26977. ' this.FOnFoo = undefined;',
  26978. ' };',
  26979. ' this.DoIt = function (vA) {',
  26980. ' var Result = 0;',
  26981. ' return Result;',
  26982. ' };',
  26983. 'this.GetFoo = function () {',
  26984. ' var Result = null;',
  26985. ' return Result;',
  26986. '};',
  26987. 'this.SetFoo = function (Value) {',
  26988. '};',
  26989. 'this.GetEvents = function (Index) {',
  26990. ' var Result = null;',
  26991. ' return Result;',
  26992. '};',
  26993. 'this.SetEvents = function (Index, Value) {',
  26994. '};',
  26995. '});',
  26996. 'this.Obj = null;',
  26997. 'this.vP = null;',
  26998. 'this.b = false;'
  26999. ]),
  27000. LinesToStr([
  27001. '$mod.Obj.FOnFoo = null;',
  27002. '$mod.Obj.SetFoo(null);',
  27003. '$mod.Obj.SetEvents(1, null);',
  27004. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27005. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27006. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27007. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27008. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27009. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27010. '$mod.Obj.FOnFoo(1);',
  27011. '$mod.Obj.GetFoo();',
  27012. '$mod.Obj.FOnFoo(1);',
  27013. '$mod.Obj.GetFoo()(1);',
  27014. '$mod.Obj.GetEvents(6)(1);',
  27015. '$mod.b = $mod.Obj.FOnFoo === null;',
  27016. '$mod.b = $mod.Obj.GetFoo() === null;',
  27017. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27018. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27019. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27020. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27021. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27022. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27023. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27024. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27025. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27026. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27027. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27028. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27029. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27030. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27031. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27032. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27033. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27034. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27035. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27036. '$mod.b = $mod.Obj.FOnFoo != null;',
  27037. '$mod.b = $mod.Obj.GetFoo() != null;',
  27038. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27039. '']));
  27040. end;
  27041. procedure TTestModule.TestProcType_PropertyDelphi;
  27042. begin
  27043. StartProgram(false);
  27044. Add('{$mode delphi}');
  27045. Add('type');
  27046. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27047. Add(' TObject = class');
  27048. Add(' FOnFoo: TFuncInt;');
  27049. Add(' function DoIt(vA: longint = 1): longint;');
  27050. Add(' function GetFoo: TFuncInt;');
  27051. Add(' procedure SetFoo(const Value: TFuncInt);');
  27052. Add(' function GetEvents(Index: longint): TFuncInt;');
  27053. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27054. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27055. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27056. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27057. Add(' end;');
  27058. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27059. Add('function tobject.getfoo: tfuncint; begin end;');
  27060. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27061. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27062. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27063. Add('var');
  27064. Add(' Obj: TObject;');
  27065. Add(' vP: tfuncint;');
  27066. Add(' b: boolean;');
  27067. Add('begin');
  27068. Add(' obj.onfoo:=nil;');
  27069. Add(' obj.onbar:=nil;');
  27070. Add(' obj.events[1]:=nil;');
  27071. Add(' obj.onfoo:=obj.onfoo;');
  27072. Add(' obj.onbar:=obj.onbar;');
  27073. Add(' obj.events[2]:=obj.events[3];');
  27074. Add(' obj.onfoo:[email protected];');
  27075. Add(' obj.onbar:[email protected];');
  27076. Add(' obj.events[4]:[email protected];');
  27077. Add(' obj.onfoo:=obj.doit;'); // delphi
  27078. Add(' obj.onbar:=obj.doit;'); // delphi
  27079. Add(' obj.events[4]:=obj.doit;'); // delphi
  27080. Add(' obj.onfoo;');
  27081. Add(' obj.onbar;');
  27082. //Add(' obj.events[5];'); ToDo in pasresolver
  27083. Add(' obj.onfoo();');
  27084. Add(' obj.onbar();');
  27085. Add(' obj.events[6]();');
  27086. //Add(' b:=obj.onfoo=nil;'); // fpc
  27087. //Add(' b:=obj.onbar=nil;'); // fpc
  27088. //Add(' b:=obj.events[7]=nil;'); // fpc
  27089. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27090. //Add(' b:=obj.onbar<>nil;'); // fpc
  27091. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27092. Add(' b:=obj.onfoo=vp;');
  27093. Add(' b:=obj.onbar=vp;');
  27094. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27095. Add(' b:=obj.onfoo=obj.onfoo;');
  27096. Add(' b:=obj.onbar=obj.onfoo;');
  27097. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27098. Add(' b:=obj.onfoo<>obj.onfoo;');
  27099. Add(' b:=obj.onbar<>obj.onfoo;');
  27100. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27101. //Add(' b:[email protected];'); // fpc
  27102. //Add(' b:[email protected];'); // fpc
  27103. //Add(' b:=obj.events[12][email protected];'); // fpc
  27104. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27105. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27106. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27107. Add(' b:=Assigned(obj.onfoo);');
  27108. Add(' b:=Assigned(obj.onbar);');
  27109. Add(' b:=Assigned(obj.events[13]);');
  27110. ConvertProgram;
  27111. CheckSource('TestProcType_PropertyDelphi',
  27112. LinesToStr([ // statements
  27113. 'rtl.createClass(this, "TObject", null, function () {',
  27114. ' this.$init = function () {',
  27115. ' this.FOnFoo = null;',
  27116. ' };',
  27117. ' this.$final = function () {',
  27118. ' this.FOnFoo = undefined;',
  27119. ' };',
  27120. ' this.DoIt = function (vA) {',
  27121. ' var Result = 0;',
  27122. ' return Result;',
  27123. ' };',
  27124. 'this.GetFoo = function () {',
  27125. ' var Result = null;',
  27126. ' return Result;',
  27127. '};',
  27128. 'this.SetFoo = function (Value) {',
  27129. '};',
  27130. 'this.GetEvents = function (Index) {',
  27131. ' var Result = null;',
  27132. ' return Result;',
  27133. '};',
  27134. 'this.SetEvents = function (Index, Value) {',
  27135. '};',
  27136. '});',
  27137. 'this.Obj = null;',
  27138. 'this.vP = null;',
  27139. 'this.b = false;'
  27140. ]),
  27141. LinesToStr([
  27142. '$mod.Obj.FOnFoo = null;',
  27143. '$mod.Obj.SetFoo(null);',
  27144. '$mod.Obj.SetEvents(1, null);',
  27145. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27146. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27147. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27148. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27149. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27150. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27151. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27152. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27153. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27154. '$mod.Obj.FOnFoo(1);',
  27155. '$mod.Obj.GetFoo();',
  27156. '$mod.Obj.FOnFoo(1);',
  27157. '$mod.Obj.GetFoo()(1);',
  27158. '$mod.Obj.GetEvents(6)(1);',
  27159. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27160. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27161. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27162. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27163. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27164. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27165. '$mod.b = $mod.Obj.FOnFoo != null;',
  27166. '$mod.b = $mod.Obj.GetFoo() != null;',
  27167. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27168. '']));
  27169. end;
  27170. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27171. begin
  27172. StartProgram(false);
  27173. Add('type');
  27174. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27175. Add(' TObject = class');
  27176. Add(' FOnFoo: TFuncInt;');
  27177. Add(' function DoIt(vA: longint = 1): longint;');
  27178. Add(' function GetFoo: TFuncInt;');
  27179. Add(' procedure SetFoo(const Value: TFuncInt);');
  27180. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27181. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27182. Add(' end;');
  27183. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27184. Add('function tobject.getfoo: tfuncint; begin end;');
  27185. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27186. Add('var');
  27187. Add(' Obj: TObject;');
  27188. Add(' vP: tfuncint;');
  27189. Add(' b: boolean;');
  27190. Add('begin');
  27191. Add('with obj do begin');
  27192. Add(' fonfoo:=nil;');
  27193. Add(' onfoo:=nil;');
  27194. Add(' onbar:=nil;');
  27195. Add(' fonfoo:=fonfoo;');
  27196. Add(' onfoo:=onfoo;');
  27197. Add(' onbar:=onbar;');
  27198. Add(' fonfoo:=@doit;');
  27199. Add(' onfoo:=@doit;');
  27200. Add(' onbar:=@doit;');
  27201. //Add(' fonfoo:=doit;'); // delphi
  27202. //Add(' onfoo:=doit;'); // delphi
  27203. //Add(' onbar:=doit;'); // delphi
  27204. Add(' fonfoo;');
  27205. Add(' onfoo;');
  27206. Add(' onbar;');
  27207. Add(' fonfoo();');
  27208. Add(' onfoo();');
  27209. Add(' onbar();');
  27210. Add(' b:=fonfoo=nil;');
  27211. Add(' b:=onfoo=nil;');
  27212. Add(' b:=onbar=nil;');
  27213. Add(' b:=fonfoo<>nil;');
  27214. Add(' b:=onfoo<>nil;');
  27215. Add(' b:=onbar<>nil;');
  27216. Add(' b:=fonfoo=vp;');
  27217. Add(' b:=onfoo=vp;');
  27218. Add(' b:=onbar=vp;');
  27219. Add(' b:=fonfoo=fonfoo;');
  27220. Add(' b:=onfoo=onfoo;');
  27221. Add(' b:=onbar=onfoo;');
  27222. Add(' b:=fonfoo<>fonfoo;');
  27223. Add(' b:=onfoo<>onfoo;');
  27224. Add(' b:=onbar<>onfoo;');
  27225. Add(' b:=fonfoo=@doit;');
  27226. Add(' b:=onfoo=@doit;');
  27227. Add(' b:=onbar=@doit;');
  27228. Add(' b:=fonfoo<>@doit;');
  27229. Add(' b:=onfoo<>@doit;');
  27230. Add(' b:=onbar<>@doit;');
  27231. Add(' b:=Assigned(fonfoo);');
  27232. Add(' b:=Assigned(onfoo);');
  27233. Add(' b:=Assigned(onbar);');
  27234. Add('end;');
  27235. ConvertProgram;
  27236. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27237. LinesToStr([ // statements
  27238. 'rtl.createClass(this, "TObject", null, function () {',
  27239. ' this.$init = function () {',
  27240. ' this.FOnFoo = null;',
  27241. ' };',
  27242. ' this.$final = function () {',
  27243. ' this.FOnFoo = undefined;',
  27244. ' };',
  27245. ' this.DoIt = function (vA) {',
  27246. ' var Result = 0;',
  27247. ' return Result;',
  27248. ' };',
  27249. ' this.GetFoo = function () {',
  27250. ' var Result = null;',
  27251. ' return Result;',
  27252. ' };',
  27253. ' this.SetFoo = function (Value) {',
  27254. ' };',
  27255. '});',
  27256. 'this.Obj = null;',
  27257. 'this.vP = null;',
  27258. 'this.b = false;'
  27259. ]),
  27260. LinesToStr([
  27261. 'var $with = $mod.Obj;',
  27262. '$with.FOnFoo = null;',
  27263. '$with.FOnFoo = null;',
  27264. '$with.SetFoo(null);',
  27265. '$with.FOnFoo = $with.FOnFoo;',
  27266. '$with.FOnFoo = $with.FOnFoo;',
  27267. '$with.SetFoo($with.GetFoo());',
  27268. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  27269. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  27270. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  27271. '$with.FOnFoo(1);',
  27272. '$with.FOnFoo(1);',
  27273. '$with.GetFoo();',
  27274. '$with.FOnFoo(1);',
  27275. '$with.FOnFoo(1);',
  27276. '$with.GetFoo()(1);',
  27277. '$mod.b = $with.FOnFoo === null;',
  27278. '$mod.b = $with.FOnFoo === null;',
  27279. '$mod.b = $with.GetFoo() === null;',
  27280. '$mod.b = $with.FOnFoo !== null;',
  27281. '$mod.b = $with.FOnFoo !== null;',
  27282. '$mod.b = $with.GetFoo() !== null;',
  27283. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  27284. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  27285. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  27286. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27287. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27288. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  27289. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27290. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27291. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  27292. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27293. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27294. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  27295. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27296. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27297. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  27298. '$mod.b = $with.FOnFoo != null;',
  27299. '$mod.b = $with.FOnFoo != null;',
  27300. '$mod.b = $with.GetFoo() != null;',
  27301. '']));
  27302. end;
  27303. procedure TTestModule.TestProcType_Nested;
  27304. begin
  27305. StartProgram(false);
  27306. Add([
  27307. 'type',
  27308. ' TProcInt = procedure(vI: longint = 1);',
  27309. 'procedure DoIt(vJ: longint);',
  27310. 'var aProc: TProcInt;',
  27311. ' b: boolean;',
  27312. ' procedure Sub(vK: longint);',
  27313. ' var aSub: TProcInt;',
  27314. ' procedure SubSub(vK: longint);',
  27315. ' var aSubSub: TProcInt;',
  27316. ' begin;',
  27317. ' aProc:=@DoIt;',
  27318. ' aSub:=@DoIt;',
  27319. ' aSubSub:=@DoIt;',
  27320. ' aProc:=@Sub;',
  27321. ' aSub:=@Sub;',
  27322. ' aSubSub:=@Sub;',
  27323. ' aProc:=@SubSub;',
  27324. ' aSub:=@SubSub;',
  27325. ' aSubSub:=@SubSub;',
  27326. ' end;',
  27327. ' begin;',
  27328. ' end;',
  27329. 'begin;',
  27330. ' aProc:=@Sub;',
  27331. ' b:=aProc=@Sub;',
  27332. ' b:=@Sub=aProc;',
  27333. 'end;',
  27334. 'begin',
  27335. '']);
  27336. ConvertProgram;
  27337. CheckSource('TestProcType_Nested',
  27338. LinesToStr([ // statements
  27339. 'this.DoIt = function (vJ) {',
  27340. ' var aProc = null;',
  27341. ' var b = false;',
  27342. ' function Sub(vK) {',
  27343. ' var aSub = null;',
  27344. ' function SubSub(vK) {',
  27345. ' var aSubSub = null;',
  27346. ' aProc = $mod.DoIt;',
  27347. ' aSub = $mod.DoIt;',
  27348. ' aSubSub = $mod.DoIt;',
  27349. ' aProc = Sub;',
  27350. ' aSub = Sub;',
  27351. ' aSubSub = Sub;',
  27352. ' aProc = SubSub;',
  27353. ' aSub = SubSub;',
  27354. ' aSubSub = SubSub;',
  27355. ' };',
  27356. ' };',
  27357. ' aProc = Sub;',
  27358. ' b = rtl.eqCallback(aProc, Sub);',
  27359. ' b = rtl.eqCallback(Sub, aProc);',
  27360. '};',
  27361. '']),
  27362. LinesToStr([ // $mod.$main
  27363. '']));
  27364. end;
  27365. procedure TTestModule.TestProcType_NestedOfObject;
  27366. begin
  27367. StartProgram(false);
  27368. Add([
  27369. 'type',
  27370. ' TProcInt = procedure(vI: longint = 1) of object;',
  27371. ' TObject = class',
  27372. ' procedure DoIt(vJ: longint);',
  27373. ' end;',
  27374. 'procedure TObject.DoIt(vJ: longint);',
  27375. 'var aProc: TProcInt;',
  27376. ' b: boolean;',
  27377. ' procedure Sub(vK: longint);',
  27378. ' var aSub: TProcInt;',
  27379. ' procedure SubSub(vK: longint);',
  27380. ' var aSubSub: TProcInt;',
  27381. ' begin;',
  27382. ' aProc:=@DoIt;',
  27383. ' aSub:=@DoIt;',
  27384. ' aSubSub:=@DoIt;',
  27385. ' aProc:=@Sub;',
  27386. ' aSub:=@Sub;',
  27387. ' aSubSub:=@Sub;',
  27388. ' aProc:=@SubSub;',
  27389. ' aSub:=@SubSub;',
  27390. ' aSubSub:=@SubSub;',
  27391. ' end;',
  27392. ' begin;',
  27393. ' end;',
  27394. 'begin;',
  27395. ' aProc:=@Sub;',
  27396. ' b:=aProc=@Sub;',
  27397. ' b:=@Sub=aProc;',
  27398. 'end;',
  27399. 'begin',
  27400. '']);
  27401. ConvertProgram;
  27402. CheckSource('TestProcType_Nested',
  27403. LinesToStr([ // statements
  27404. 'rtl.createClass(this, "TObject", null, function () {',
  27405. ' this.$init = function () {',
  27406. ' };',
  27407. ' this.$final = function () {',
  27408. ' };',
  27409. ' this.DoIt = function (vJ) {',
  27410. ' var $Self = this;',
  27411. ' var aProc = null;',
  27412. ' var b = false;',
  27413. ' function Sub(vK) {',
  27414. ' var aSub = null;',
  27415. ' function SubSub(vK) {',
  27416. ' var aSubSub = null;',
  27417. ' aProc = rtl.createCallback($Self, "DoIt");',
  27418. ' aSub = rtl.createCallback($Self, "DoIt");',
  27419. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  27420. ' aProc = Sub;',
  27421. ' aSub = Sub;',
  27422. ' aSubSub = Sub;',
  27423. ' aProc = SubSub;',
  27424. ' aSub = SubSub;',
  27425. ' aSubSub = SubSub;',
  27426. ' };',
  27427. ' };',
  27428. ' aProc = Sub;',
  27429. ' b = rtl.eqCallback(aProc, Sub);',
  27430. ' b = rtl.eqCallback(Sub, aProc);',
  27431. ' };',
  27432. '});',
  27433. '']),
  27434. LinesToStr([ // $mod.$main
  27435. '']));
  27436. end;
  27437. procedure TTestModule.TestProcType_ReferenceToProc;
  27438. begin
  27439. StartProgram(false);
  27440. Add([
  27441. 'type',
  27442. ' TProcRef = reference to procedure(i: longint = 0);',
  27443. ' TFuncRef = reference to function(i: longint = 0): longint;',
  27444. 'var',
  27445. ' p: TProcRef;',
  27446. ' f: TFuncRef;',
  27447. 'procedure DoIt(i: longint);',
  27448. 'begin',
  27449. 'end;',
  27450. 'function GetIt(i: longint): longint;',
  27451. 'begin',
  27452. ' p:=@DoIt;',
  27453. ' f:=@GetIt;',
  27454. ' f;',
  27455. ' f();',
  27456. ' f(1);',
  27457. 'end;',
  27458. 'begin',
  27459. ' p:=@DoIt;',
  27460. ' f:=@GetIt;',
  27461. ' f;',
  27462. ' f();',
  27463. ' f(1);',
  27464. ' p:=TProcRef(f);',
  27465. '']);
  27466. ConvertProgram;
  27467. CheckSource('TestProcType_ReferenceToProc',
  27468. LinesToStr([ // statements
  27469. 'this.p = null;',
  27470. 'this.f = null;',
  27471. 'this.DoIt = function (i) {',
  27472. '};',
  27473. 'this.GetIt = function (i) {',
  27474. ' var Result = 0;',
  27475. ' $mod.p = $mod.DoIt;',
  27476. ' $mod.f = $mod.GetIt;',
  27477. ' $mod.f(0);',
  27478. ' $mod.f(0);',
  27479. ' $mod.f(1);',
  27480. ' return Result;',
  27481. '};',
  27482. '']),
  27483. LinesToStr([ // $mod.$main
  27484. '$mod.p = $mod.DoIt;',
  27485. '$mod.f = $mod.GetIt;',
  27486. '$mod.f(0);',
  27487. '$mod.f(0);',
  27488. '$mod.f(1);',
  27489. '$mod.p = $mod.f;',
  27490. '']));
  27491. end;
  27492. procedure TTestModule.TestProcType_ReferenceToMethod;
  27493. begin
  27494. StartProgram(false);
  27495. Add([
  27496. 'type',
  27497. ' TFuncRef = reference to function(i: longint = 5): longint;',
  27498. ' TObject = class',
  27499. ' function Grow(s: longint): longint;',
  27500. ' end;',
  27501. 'var',
  27502. ' f: tfuncref;',
  27503. 'function tobject.grow(s: longint): longint;',
  27504. ' function GrowSub(i: longint): longint;',
  27505. ' begin',
  27506. ' f:=@grow;',
  27507. ' f:=@growsub;',
  27508. ' end;',
  27509. 'begin',
  27510. ' f:=@grow;',
  27511. ' f:=@growsub;',
  27512. 'end;',
  27513. 'begin',
  27514. '']);
  27515. ConvertProgram;
  27516. CheckSource('TestProcType_ReferenceToMethod',
  27517. LinesToStr([ // statements
  27518. 'rtl.createClass(this, "TObject", null, function () {',
  27519. ' this.$init = function () {',
  27520. ' };',
  27521. ' this.$final = function () {',
  27522. ' };',
  27523. ' this.Grow = function (s) {',
  27524. ' var $Self = this;',
  27525. ' var Result = 0;',
  27526. ' function GrowSub(i) {',
  27527. ' var Result = 0;',
  27528. ' $mod.f = rtl.createCallback($Self, "Grow");',
  27529. ' $mod.f = GrowSub;',
  27530. ' return Result;',
  27531. ' };',
  27532. ' $mod.f = rtl.createCallback($Self, "Grow");',
  27533. ' $mod.f = GrowSub;',
  27534. ' return Result;',
  27535. ' };',
  27536. '});',
  27537. 'this.f = null;',
  27538. '']),
  27539. LinesToStr([ // $mod.$main
  27540. '']));
  27541. end;
  27542. procedure TTestModule.TestProcType_Typecast;
  27543. begin
  27544. StartProgram(false);
  27545. Add([
  27546. 'type',
  27547. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  27548. ' TEvent = procedure of object;',
  27549. ' TGetter = function:longint of object;',
  27550. ' TProcA = procedure(i: longint);',
  27551. ' TFuncB = function(i, j: longint): longint;',
  27552. 'procedure DoIt(); varargs; begin end;',
  27553. 'var',
  27554. ' Notify: tnotifyevent;',
  27555. ' Event: tevent;',
  27556. ' Getter: tgetter;',
  27557. ' ProcA: tproca;',
  27558. ' FuncB: tfuncb;',
  27559. ' p: pointer;',
  27560. 'begin',
  27561. ' notify:=tnotifyevent(event);',
  27562. ' event:=tevent(event);',
  27563. ' event:=tevent(notify);',
  27564. ' event:=tevent(getter);',
  27565. ' event:=tevent(proca);',
  27566. ' proca:=tproca(funcb);',
  27567. ' funcb:=tfuncb(funcb);',
  27568. ' funcb:=tfuncb(proca);',
  27569. ' funcb:=tfuncb(getter);',
  27570. ' proca:=tproca(p);',
  27571. ' funcb:=tfuncb(p);',
  27572. ' getter:=tgetter(p);',
  27573. ' p:=pointer(notify);',
  27574. ' p:=notify;',
  27575. ' p:=pointer(proca);',
  27576. ' p:=proca;',
  27577. ' p:=pointer(funcb);',
  27578. ' p:=funcb;',
  27579. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  27580. '']);
  27581. ConvertProgram;
  27582. CheckSource('TestProcType_Typecast',
  27583. LinesToStr([ // statements
  27584. 'this.DoIt = function () {',
  27585. '};',
  27586. 'this.Notify = null;',
  27587. 'this.Event = null;',
  27588. 'this.Getter = null;',
  27589. 'this.ProcA = null;',
  27590. 'this.FuncB = null;',
  27591. 'this.p = null;',
  27592. '']),
  27593. LinesToStr([ // $mod.$main
  27594. '$mod.Notify = $mod.Event;',
  27595. '$mod.Event = $mod.Event;',
  27596. '$mod.Event = $mod.Notify;',
  27597. '$mod.Event = $mod.Getter;',
  27598. '$mod.Event = $mod.ProcA;',
  27599. '$mod.ProcA = $mod.FuncB;',
  27600. '$mod.FuncB = $mod.FuncB;',
  27601. '$mod.FuncB = $mod.ProcA;',
  27602. '$mod.FuncB = $mod.Getter;',
  27603. '$mod.ProcA = $mod.p;',
  27604. '$mod.FuncB = $mod.p;',
  27605. '$mod.Getter = $mod.p;',
  27606. '$mod.p = $mod.Notify;',
  27607. '$mod.p = $mod.Notify;',
  27608. '$mod.p = $mod.ProcA;',
  27609. '$mod.p = $mod.ProcA;',
  27610. '$mod.p = $mod.FuncB;',
  27611. '$mod.p = $mod.FuncB;',
  27612. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  27613. '']));
  27614. end;
  27615. procedure TTestModule.TestProcType_PassProcToUntyped;
  27616. begin
  27617. StartProgram(false);
  27618. Add([
  27619. 'type',
  27620. ' TEvent = procedure of object;',
  27621. ' TFunc = function: longint;',
  27622. 'procedure DoIt(); varargs; begin end;',
  27623. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  27624. 'var',
  27625. ' Event: tevent;',
  27626. ' Func: TFunc;',
  27627. 'begin',
  27628. ' doit(event,func);',
  27629. ' dosome(event,event,event);',
  27630. ' dosome(func,func,func);',
  27631. '']);
  27632. ConvertProgram;
  27633. CheckSource('TestProcType_PassProcToUntyped',
  27634. LinesToStr([ // statements
  27635. 'this.DoIt = function () {',
  27636. '};',
  27637. 'this.DoSome = function (a, b, p) {',
  27638. '};',
  27639. 'this.Event = null;',
  27640. 'this.Func = null;',
  27641. '']),
  27642. LinesToStr([ // $mod.$main
  27643. '$mod.DoIt($mod.Event, $mod.Func);',
  27644. '$mod.DoSome($mod.Event, {',
  27645. ' p: $mod,',
  27646. ' get: function () {',
  27647. ' return this.p.Event;',
  27648. ' },',
  27649. ' set: function (v) {',
  27650. ' this.p.Event = v;',
  27651. ' }',
  27652. '}, $mod.Event);',
  27653. '$mod.DoSome($mod.Func, {',
  27654. ' p: $mod,',
  27655. ' get: function () {',
  27656. ' return this.p.Func;',
  27657. ' },',
  27658. ' set: function (v) {',
  27659. ' this.p.Func = v;',
  27660. ' }',
  27661. '}, $mod.Func);',
  27662. '']));
  27663. end;
  27664. procedure TTestModule.TestProcType_PassProcToArray;
  27665. begin
  27666. StartProgram(false);
  27667. Add([
  27668. 'type',
  27669. ' TFunc = function: longint;',
  27670. ' TArrFunc = array of TFunc;',
  27671. 'procedure DoIt(Arr: TArrFunc); begin end;',
  27672. 'function GetIt: longint; begin end;',
  27673. 'var',
  27674. ' Func: tfunc;',
  27675. 'begin',
  27676. ' doit([]);',
  27677. ' doit([@GetIt]);',
  27678. ' doit([Func]);',
  27679. '']);
  27680. ConvertProgram;
  27681. CheckSource('TestProcType_PassProcToArray',
  27682. LinesToStr([ // statements
  27683. 'this.DoIt = function (Arr) {',
  27684. '};',
  27685. 'this.GetIt = function () {',
  27686. ' var Result = 0;',
  27687. ' return Result;',
  27688. '};',
  27689. 'this.Func = null;',
  27690. '']),
  27691. LinesToStr([ // $mod.$main
  27692. '$mod.DoIt([]);',
  27693. '$mod.DoIt([$mod.GetIt]);',
  27694. '$mod.DoIt([$mod.Func]);',
  27695. '']));
  27696. end;
  27697. procedure TTestModule.TestProcType_SafeCallObjFPC;
  27698. begin
  27699. StartProgram(false);
  27700. Add([
  27701. '{$modeswitch externalclass}',
  27702. 'type',
  27703. ' TProc = reference to procedure(i: longint); safecall;',
  27704. ' TEvent = procedure(i: longint) of object; safecall;',
  27705. ' TExtA = class external name ''ExtObj''',
  27706. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27707. ' procedure DoSome(Id: longint = 1);',
  27708. ' procedure SetOnClick(const e: TEvent);',
  27709. ' property OnClick: TEvent write SetOnClick;',
  27710. ' class procedure Fly(Id: longint = 1); static;',
  27711. ' procedure SetOnShow(const p: TProc);',
  27712. ' property OnShow: TProc write SetOnShow;',
  27713. ' end;',
  27714. 'procedure Run(i: longint = 1);',
  27715. 'begin',
  27716. 'end;',
  27717. 'var',
  27718. ' Obj: texta;',
  27719. ' e: TEvent;',
  27720. ' p: TProc;',
  27721. 'begin',
  27722. ' e:=e;',
  27723. ' e:[email protected];',
  27724. ' e:[email protected];',
  27725. ' e:=TEvent(@obj.dosome);', // no safecall
  27726. ' obj.OnClick:[email protected];',
  27727. ' obj.OnClick:[email protected];',
  27728. ' obj.setonclick(@obj.doit);',
  27729. ' obj.setonclick(@obj.dosome);',
  27730. ' p:=@Run;',
  27731. ' p:[email protected];',
  27732. ' obj.OnShow:=@Run;',
  27733. ' obj.OnShow:[email protected];',
  27734. ' obj.setOnShow(@Run);',
  27735. ' obj.setOnShow(@TExtA.Fly);',
  27736. ' with obj do begin',
  27737. ' e:=@doit;',
  27738. ' e:=@dosome;',
  27739. ' OnClick:=@doit;',
  27740. ' OnClick:=@dosome;',
  27741. ' setonclick(@doit);',
  27742. ' setonclick(@dosome);',
  27743. ' OnShow:=@Run;',
  27744. ' setOnShow(@Run);',
  27745. ' end;']);
  27746. ConvertProgram;
  27747. CheckSource('TestProcType_SafeCallObjFPC',
  27748. LinesToStr([ // statements
  27749. 'this.Run = function (i) {',
  27750. '};',
  27751. 'this.Obj = null;',
  27752. 'this.e = null;',
  27753. 'this.p = null;',
  27754. '']),
  27755. LinesToStr([ // $mod.$main
  27756. '$mod.e = $mod.e;',
  27757. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  27758. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  27759. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  27760. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27761. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27762. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27763. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27764. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  27765. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  27766. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27767. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27768. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27769. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27770. 'var $with = $mod.Obj;',
  27771. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  27772. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  27773. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27774. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27775. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27776. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27777. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27778. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27779. '']));
  27780. end;
  27781. procedure TTestModule.TestProcType_SafeCallDelphi;
  27782. begin
  27783. StartProgram(false);
  27784. Add([
  27785. '{$mode delphi}',
  27786. '{$modeswitch externalclass}',
  27787. 'type',
  27788. ' TProc = reference to procedure(i: longint); safecall;',
  27789. ' TEvent = procedure(i: longint) of object; safecall;',
  27790. ' TExtA = class external name ''ExtObj''',
  27791. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27792. ' procedure DoSome(Id: longint = 1);',
  27793. ' procedure SetOnClick(const e: TEvent);',
  27794. ' property OnClick: TEvent write SetOnClick;',
  27795. ' class procedure Fly(Id: longint = 1); static;',
  27796. ' procedure SetOnShow(const p: TProc);',
  27797. ' property OnShow: TProc write SetOnShow;',
  27798. ' end;',
  27799. 'procedure Run(i: longint = 1);',
  27800. 'begin',
  27801. 'end;',
  27802. 'var',
  27803. ' Obj: texta;',
  27804. ' e: TEvent;',
  27805. ' p: TProc;',
  27806. 'begin',
  27807. ' e:=e;',
  27808. ' e:=obj.doit;',
  27809. ' e:=obj.dosome;',
  27810. ' e:=TEvent(@obj.dosome);', // no safecall
  27811. ' obj.OnClick:=obj.doit;',
  27812. ' obj.OnClick:=obj.dosome;',
  27813. ' obj.setonclick(obj.doit);',
  27814. ' obj.setonclick(obj.dosome);',
  27815. ' p:=Run;',
  27816. ' p:=TExtA.Fly;',
  27817. ' obj.OnShow:=Run;',
  27818. ' obj.OnShow:=TExtA.Fly;',
  27819. ' obj.setOnShow(Run);',
  27820. ' obj.setOnShow(TExtA.Fly);',
  27821. ' with obj do begin',
  27822. ' e:=doit;',
  27823. ' e:=dosome;',
  27824. ' OnClick:=doit;',
  27825. ' OnClick:=dosome;',
  27826. ' setonclick(doit);',
  27827. ' setonclick(dosome);',
  27828. ' OnShow:=@Run;',
  27829. ' setOnShow(@Run);',
  27830. ' end;']);
  27831. ConvertProgram;
  27832. CheckSource('TestProcType_SafeCallDelphi',
  27833. LinesToStr([ // statements
  27834. 'this.Run = function (i) {',
  27835. '};',
  27836. 'this.Obj = null;',
  27837. 'this.e = null;',
  27838. 'this.p = null;',
  27839. '']),
  27840. LinesToStr([ // $mod.$main
  27841. '$mod.e = $mod.e;',
  27842. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  27843. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  27844. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  27845. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27846. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27847. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27848. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27849. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  27850. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  27851. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27852. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27853. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27854. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27855. 'var $with = $mod.Obj;',
  27856. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  27857. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  27858. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27859. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27860. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27861. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27862. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27863. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27864. '']));
  27865. end;
  27866. procedure TTestModule.TestPointer;
  27867. begin
  27868. StartProgram(false);
  27869. Add(['type',
  27870. ' TObject = class end;',
  27871. ' TClass = class of TObject;',
  27872. ' TArrInt = array of longint;',
  27873. 'const',
  27874. ' n = nil;',
  27875. 'var',
  27876. ' v: jsvalue;',
  27877. ' Obj: tobject;',
  27878. ' C: tclass;',
  27879. ' a: tarrint;',
  27880. ' p: Pointer = nil;',
  27881. ' s: string;',
  27882. 'begin',
  27883. ' p:=p;',
  27884. ' p:=nil;',
  27885. ' if p=nil then;',
  27886. ' if nil=p then;',
  27887. ' if Assigned(p) then;',
  27888. ' p:=Pointer(v);',
  27889. ' p:=obj;',
  27890. ' p:=c;',
  27891. ' p:=a;',
  27892. ' p:=tobject;',
  27893. ' obj:=TObject(p);',
  27894. ' c:=TClass(p);',
  27895. ' a:=TArrInt(p);',
  27896. ' p:=n;',
  27897. ' p:=Pointer(a);',
  27898. ' p:=pointer(s);',
  27899. ' s:=string(p);',
  27900. '']);
  27901. ConvertProgram;
  27902. CheckSource('TestPointer',
  27903. LinesToStr([ // statements
  27904. 'rtl.createClass(this, "TObject", null, function () {',
  27905. ' this.$init = function () {',
  27906. ' };',
  27907. ' this.$final = function () {',
  27908. ' };',
  27909. '});',
  27910. 'this.n = null;',
  27911. 'this.v = undefined;',
  27912. 'this.Obj = null;',
  27913. 'this.C = null;',
  27914. 'this.a = [];',
  27915. 'this.p = null;',
  27916. 'this.s = "";',
  27917. '']),
  27918. LinesToStr([ // $mod.$main
  27919. '$mod.p = $mod.p;',
  27920. '$mod.p = null;',
  27921. 'if ($mod.p === null) ;',
  27922. 'if (null === $mod.p) ;',
  27923. 'if ($mod.p != null) ;',
  27924. '$mod.p = $mod.v;',
  27925. '$mod.p = $mod.Obj;',
  27926. '$mod.p = $mod.C;',
  27927. '$mod.p = $mod.a;',
  27928. '$mod.p = $mod.TObject;',
  27929. '$mod.Obj = $mod.p;',
  27930. '$mod.C = $mod.p;',
  27931. '$mod.a = $mod.p;',
  27932. '$mod.p = null;',
  27933. '$mod.p = $mod.a;',
  27934. '$mod.p = $mod.s;',
  27935. '$mod.s = $mod.p;',
  27936. '']));
  27937. end;
  27938. procedure TTestModule.TestPointer_Proc;
  27939. begin
  27940. StartProgram(false);
  27941. Add('type');
  27942. Add(' TObject = class');
  27943. Add(' procedure DoIt; virtual; abstract;');
  27944. Add(' end;');
  27945. Add('procedure DoSome; begin end;');
  27946. Add('var');
  27947. Add(' o: TObject;');
  27948. Add(' p: Pointer;');
  27949. Add('begin');
  27950. Add(' p:=@DoSome;');
  27951. Add(' p:[email protected];');
  27952. ConvertProgram;
  27953. CheckSource('TestPointer_Proc',
  27954. LinesToStr([ // statements
  27955. 'rtl.createClass(this, "TObject", null, function () {',
  27956. ' this.$init = function () {',
  27957. ' };',
  27958. ' this.$final = function () {',
  27959. ' };',
  27960. '});',
  27961. 'this.DoSome = function () {',
  27962. '};',
  27963. 'this.o = null;',
  27964. 'this.p = null;',
  27965. '']),
  27966. LinesToStr([ // $mod.$main
  27967. '$mod.p = $mod.DoSome;',
  27968. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  27969. '']));
  27970. end;
  27971. procedure TTestModule.TestPointer_AssignRecordFail;
  27972. begin
  27973. StartProgram(false);
  27974. Add('type');
  27975. Add(' TRec = record end;');
  27976. Add('var');
  27977. Add(' p: Pointer;');
  27978. Add(' r: TRec;');
  27979. Add('begin');
  27980. Add(' p:=r;');
  27981. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  27982. nIncompatibleTypesGotExpected);
  27983. ConvertProgram;
  27984. end;
  27985. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  27986. begin
  27987. StartProgram(false);
  27988. Add('type');
  27989. Add(' TArr = array[boolean] of longint;');
  27990. Add('var');
  27991. Add(' p: Pointer;');
  27992. Add(' a: TArr;');
  27993. Add('begin');
  27994. Add(' p:=a;');
  27995. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  27996. nIncompatibleTypesGotExpected);
  27997. ConvertProgram;
  27998. end;
  27999. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28000. begin
  28001. StartProgram(false);
  28002. Add([
  28003. 'procedure DoIt(args: array of jsvalue); begin end;',
  28004. 'procedure DoAll; varargs; begin end;',
  28005. 'var',
  28006. ' v: jsvalue;',
  28007. 'begin',
  28008. ' DoIt([pointer(v)]);',
  28009. ' DoAll(pointer(v));',
  28010. '']);
  28011. ConvertProgram;
  28012. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28013. LinesToStr([ // statements
  28014. 'this.DoIt = function (args) {',
  28015. '};',
  28016. 'this.DoAll = function () {',
  28017. '};',
  28018. 'this.v = undefined;',
  28019. '']),
  28020. LinesToStr([ // $mod.$main
  28021. '$mod.DoIt([$mod.v]);',
  28022. '$mod.DoAll($mod.v);',
  28023. '']));
  28024. end;
  28025. procedure TTestModule.TestPointer_NonRecordFail;
  28026. begin
  28027. StartProgram(false);
  28028. Add([
  28029. 'type',
  28030. ' p = ^longint;',
  28031. 'begin',
  28032. '']);
  28033. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28034. ConvertProgram;
  28035. end;
  28036. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28037. begin
  28038. StartProgram(false);
  28039. Add([
  28040. 'procedure DoIt(p: ^longint); begin end;',
  28041. 'begin',
  28042. '']);
  28043. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28044. ConvertProgram;
  28045. end;
  28046. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28047. begin
  28048. StartProgram(false);
  28049. Add([
  28050. 'var p: ^longint;',
  28051. 'begin',
  28052. '']);
  28053. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28054. ConvertProgram;
  28055. end;
  28056. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28057. begin
  28058. StartProgram(false);
  28059. Add([
  28060. 'function DoIt: ^longint; begin end;',
  28061. 'begin',
  28062. '']);
  28063. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28064. ConvertProgram;
  28065. end;
  28066. procedure TTestModule.TestPointer_AddrOperatorFail;
  28067. begin
  28068. StartProgram(false);
  28069. Add([
  28070. 'var i: longint;',
  28071. 'begin',
  28072. ' if @i=nil then ;',
  28073. '']);
  28074. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28075. ConvertProgram;
  28076. end;
  28077. procedure TTestModule.TestPointer_ArrayParamsFail;
  28078. begin
  28079. StartProgram(false);
  28080. Add([
  28081. 'var',
  28082. ' p: Pointer;',
  28083. 'begin',
  28084. ' p:=p[1];',
  28085. '']);
  28086. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28087. ConvertProgram;
  28088. end;
  28089. procedure TTestModule.TestPointer_PointerAddFail;
  28090. begin
  28091. StartProgram(false);
  28092. Add([
  28093. 'var',
  28094. ' p: Pointer;',
  28095. 'begin',
  28096. ' p:=p+1;',
  28097. '']);
  28098. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28099. ConvertProgram;
  28100. end;
  28101. procedure TTestModule.TestPointer_IncPointerFail;
  28102. begin
  28103. StartProgram(false);
  28104. Add([
  28105. 'var',
  28106. ' p: Pointer;',
  28107. 'begin',
  28108. ' inc(p,1);',
  28109. '']);
  28110. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  28111. nIncompatibleTypeArgNo);
  28112. ConvertProgram;
  28113. end;
  28114. procedure TTestModule.TestPointer_Record;
  28115. begin
  28116. StartProgram(false);
  28117. Add([
  28118. 'type',
  28119. ' TRec = record x: longint; end;',
  28120. ' PRec = ^TRec;',
  28121. 'var',
  28122. ' r: TRec;',
  28123. ' p: PRec;',
  28124. ' q: ^TRec;',
  28125. ' Ptr: pointer;',
  28126. 'begin',
  28127. ' new(p);',
  28128. ' p:=@r;',
  28129. ' r:=p^;',
  28130. ' r.x:=p^.x;',
  28131. ' p^.x:=r.x;',
  28132. ' if p^.x=3 then ;',
  28133. ' if 4=p^.x then ;',
  28134. ' dispose(p);',
  28135. ' new(q);',
  28136. ' dispose(q);',
  28137. ' Ptr:=p;',
  28138. ' p:=PRec(ptr);',
  28139. '']);
  28140. ConvertProgram;
  28141. CheckSource('TestPointer_Record',
  28142. LinesToStr([ // statements
  28143. 'rtl.recNewT(this, "TRec", function () {',
  28144. ' this.x = 0;',
  28145. ' this.$eq = function (b) {',
  28146. ' return this.x === b.x;',
  28147. ' };',
  28148. ' this.$assign = function (s) {',
  28149. ' this.x = s.x;',
  28150. ' return this;',
  28151. ' };',
  28152. '});',
  28153. 'this.r = this.TRec.$new();',
  28154. 'this.p = null;',
  28155. 'this.q = null;',
  28156. 'this.Ptr = null;',
  28157. '']),
  28158. LinesToStr([ // $mod.$main
  28159. '$mod.p = $mod.TRec.$new();',
  28160. '$mod.p = $mod.r;',
  28161. '$mod.r.$assign($mod.p);',
  28162. '$mod.r.x = $mod.p.x;',
  28163. '$mod.p.x = $mod.r.x;',
  28164. 'if ($mod.p.x === 3) ;',
  28165. 'if (4 === $mod.p.x) ;',
  28166. '$mod.p = null;',
  28167. '$mod.q = $mod.TRec.$new();',
  28168. '$mod.q = null;',
  28169. '$mod.Ptr = $mod.p;',
  28170. '$mod.p = $mod.Ptr;',
  28171. '']));
  28172. end;
  28173. procedure TTestModule.TestPointer_RecordArg;
  28174. begin
  28175. StartProgram(false);
  28176. Add([
  28177. '{$modeswitch autoderef}',
  28178. 'type',
  28179. ' TRec = record x: longint; end;',
  28180. ' PRec = ^TRec;',
  28181. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28182. 'begin',
  28183. ' a.x:=a.x;',
  28184. ' a^.x:=a^.x;',
  28185. ' with a^ do',
  28186. ' x:=x;',
  28187. 'end;',
  28188. 'function GetIt(p: PRec): PRec;',
  28189. 'begin',
  28190. ' p.x:=p.x;',
  28191. ' p^.x:=p^.x;',
  28192. ' with p^ do',
  28193. ' x:=x;',
  28194. 'end;',
  28195. 'var',
  28196. ' r: TRec;',
  28197. ' p: PRec;',
  28198. 'begin',
  28199. ' p:=GetIt(p);',
  28200. ' p^:=GetIt(@r)^;',
  28201. ' DoIt(p,p,p);',
  28202. ' DoIt(@r,p,p);',
  28203. '']);
  28204. ConvertProgram;
  28205. CheckSource('TestPointer_RecordArg',
  28206. LinesToStr([ // statements
  28207. 'rtl.recNewT(this, "TRec", function () {',
  28208. ' this.x = 0;',
  28209. ' this.$eq = function (b) {',
  28210. ' return this.x === b.x;',
  28211. ' };',
  28212. ' this.$assign = function (s) {',
  28213. ' this.x = s.x;',
  28214. ' return this;',
  28215. ' };',
  28216. '});',
  28217. 'this.DoIt = function (a, b, c) {',
  28218. ' var Result = $mod.TRec.$new();',
  28219. ' a.x = a.x;',
  28220. ' a.x = a.x;',
  28221. ' a.x = a.x;',
  28222. ' return Result;',
  28223. '};',
  28224. 'this.GetIt = function (p) {',
  28225. ' var Result = null;',
  28226. ' p.x = p.x;',
  28227. ' p.x = p.x;',
  28228. ' p.x = p.x;',
  28229. ' return Result;',
  28230. '};',
  28231. 'this.r = this.TRec.$new();',
  28232. 'this.p = null;',
  28233. '']),
  28234. LinesToStr([ // $mod.$main
  28235. '$mod.p = $mod.GetIt($mod.p);',
  28236. '$mod.p.$assign($mod.GetIt($mod.r));',
  28237. '$mod.DoIt($mod.p, {',
  28238. ' p: $mod,',
  28239. ' get: function () {',
  28240. ' return this.p.p;',
  28241. ' },',
  28242. ' set: function (v) {',
  28243. ' this.p.p = v;',
  28244. ' }',
  28245. '}, {',
  28246. ' p: $mod,',
  28247. ' get: function () {',
  28248. ' return this.p.p;',
  28249. ' },',
  28250. ' set: function (v) {',
  28251. ' this.p.p = v;',
  28252. ' }',
  28253. '});',
  28254. '$mod.DoIt($mod.r, {',
  28255. ' p: $mod,',
  28256. ' get: function () {',
  28257. ' return this.p.p;',
  28258. ' },',
  28259. ' set: function (v) {',
  28260. ' this.p.p = v;',
  28261. ' }',
  28262. '}, {',
  28263. ' p: $mod,',
  28264. ' get: function () {',
  28265. ' return this.p.p;',
  28266. ' },',
  28267. ' set: function (v) {',
  28268. ' this.p.p = v;',
  28269. ' }',
  28270. '});',
  28271. '']));
  28272. end;
  28273. procedure TTestModule.TestJSValue_AssignToJSValue;
  28274. begin
  28275. StartProgram(false);
  28276. Add('var');
  28277. Add(' v: jsvalue;');
  28278. Add(' i: longint;');
  28279. Add(' s: string;');
  28280. Add(' b: boolean;');
  28281. Add(' d: double;');
  28282. Add(' p: pointer;');
  28283. Add('begin');
  28284. Add(' v:=v;');
  28285. Add(' v:=1;');
  28286. Add(' v:=i;');
  28287. Add(' v:='''';');
  28288. Add(' v:=''c'';');
  28289. Add(' v:=''foo'';');
  28290. Add(' v:=s;');
  28291. Add(' v:=false;');
  28292. Add(' v:=true;');
  28293. Add(' v:=b;');
  28294. Add(' v:=0.1;');
  28295. Add(' v:=d;');
  28296. Add(' v:=nil;');
  28297. Add(' v:=p;');
  28298. ConvertProgram;
  28299. CheckSource('TestJSValue_AssignToJSValue',
  28300. LinesToStr([ // statements
  28301. 'this.v = undefined;',
  28302. 'this.i = 0;',
  28303. 'this.s = "";',
  28304. 'this.b = false;',
  28305. 'this.d = 0.0;',
  28306. 'this.p = null;',
  28307. '']),
  28308. LinesToStr([ // $mod.$main
  28309. '$mod.v = $mod.v;',
  28310. '$mod.v = 1;',
  28311. '$mod.v = $mod.i;',
  28312. '$mod.v = "";',
  28313. '$mod.v = "c";',
  28314. '$mod.v = "foo";',
  28315. '$mod.v = $mod.s;',
  28316. '$mod.v = false;',
  28317. '$mod.v = true;',
  28318. '$mod.v = $mod.b;',
  28319. '$mod.v = 0.1;',
  28320. '$mod.v = $mod.d;',
  28321. '$mod.v = null;',
  28322. '$mod.v = $mod.p;',
  28323. '']));
  28324. end;
  28325. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  28326. begin
  28327. StartProgram(false);
  28328. Add('type');
  28329. Add(' integer = longint;');
  28330. Add(' TYesNo = boolean;');
  28331. Add(' TFloat = double;');
  28332. Add(' TCaption = string;');
  28333. Add(' TChar = char;');
  28334. Add('var');
  28335. Add(' v: jsvalue;');
  28336. Add(' i: integer;');
  28337. Add(' s: TCaption;');
  28338. Add(' b: TYesNo;');
  28339. Add(' d: TFloat;');
  28340. Add(' c: char;');
  28341. Add('begin');
  28342. Add(' i:=longint(v);');
  28343. Add(' i:=integer(v);');
  28344. Add(' s:=string(v);');
  28345. Add(' s:=TCaption(v);');
  28346. Add(' b:=boolean(v);');
  28347. Add(' b:=TYesNo(v);');
  28348. Add(' d:=double(v);');
  28349. Add(' d:=TFloat(v);');
  28350. Add(' c:=char(v);');
  28351. Add(' c:=TChar(v);');
  28352. ConvertProgram;
  28353. CheckSource('TestJSValue_TypeCastToBaseType',
  28354. LinesToStr([ // statements
  28355. 'this.v = undefined;',
  28356. 'this.i = 0;',
  28357. 'this.s = "";',
  28358. 'this.b = false;',
  28359. 'this.d = 0.0;',
  28360. 'this.c = "";',
  28361. '']),
  28362. LinesToStr([ // $mod.$main
  28363. '$mod.i = rtl.trunc($mod.v);',
  28364. '$mod.i = rtl.trunc($mod.v);',
  28365. '$mod.s = "" + $mod.v;',
  28366. '$mod.s = "" + $mod.v;',
  28367. '$mod.b = !($mod.v == false);',
  28368. '$mod.b = !($mod.v == false);',
  28369. '$mod.d = rtl.getNumber($mod.v);',
  28370. '$mod.d = rtl.getNumber($mod.v);',
  28371. '$mod.c = rtl.getChar($mod.v);',
  28372. '$mod.c = rtl.getChar($mod.v);',
  28373. '']));
  28374. end;
  28375. procedure TTestModule.TestJSValue_TypecastToJSValue;
  28376. begin
  28377. StartProgram(false);
  28378. Add([
  28379. 'type',
  28380. ' TArr = array of word;',
  28381. ' TRec = record end;',
  28382. ' TSet = set of boolean;',
  28383. 'procedure Fly(v: jsvalue);',
  28384. 'begin',
  28385. 'end;',
  28386. 'var',
  28387. ' a: TArr;',
  28388. ' r: TRec;',
  28389. ' s: TSet;',
  28390. 'begin',
  28391. ' Fly(jsvalue(a));',
  28392. ' Fly(jsvalue(r));',
  28393. ' Fly(jsvalue(s));',
  28394. '']);
  28395. ConvertProgram;
  28396. CheckSource('TestJSValue_TypecastToJSValue',
  28397. LinesToStr([ // statements
  28398. 'rtl.recNewT(this, "TRec", function () {',
  28399. ' this.$eq = function (b) {',
  28400. ' return true;',
  28401. ' };',
  28402. ' this.$assign = function (s) {',
  28403. ' return this;',
  28404. ' };',
  28405. '});',
  28406. 'this.Fly = function (v) {',
  28407. '};',
  28408. 'this.a = [];',
  28409. 'this.r = this.TRec.$new();',
  28410. 'this.s = {};',
  28411. '']),
  28412. LinesToStr([ // $mod.$main
  28413. '$mod.Fly($mod.a);',
  28414. '$mod.Fly($mod.r);',
  28415. '$mod.Fly($mod.s);',
  28416. '']));
  28417. end;
  28418. procedure TTestModule.TestJSValue_Equal;
  28419. begin
  28420. StartProgram(false);
  28421. Add('type');
  28422. Add(' integer = longint;');
  28423. Add(' TYesNo = boolean;');
  28424. Add(' TFloat = double;');
  28425. Add(' TCaption = string;');
  28426. Add(' TChar = char;');
  28427. Add(' TMulti = JSValue;');
  28428. Add('var');
  28429. Add(' v: jsvalue;');
  28430. Add(' i: integer;');
  28431. Add(' s: TCaption;');
  28432. Add(' b: TYesNo;');
  28433. Add(' d: TFloat;');
  28434. Add(' c: char;');
  28435. Add(' m: TMulti;');
  28436. Add('begin');
  28437. Add(' b:=v=v;');
  28438. Add(' b:=v<>v;');
  28439. Add(' b:=v=1;');
  28440. Add(' b:=v<>1;');
  28441. Add(' b:=2=v;');
  28442. Add(' b:=2<>v;');
  28443. Add(' b:=v=i;');
  28444. Add(' b:=i=v;');
  28445. Add(' b:=v=nil;');
  28446. Add(' b:=nil=v;');
  28447. Add(' b:=v=false;');
  28448. Add(' b:=true=v;');
  28449. Add(' b:=v=b;');
  28450. Add(' b:=b=v;');
  28451. Add(' b:=v=s;');
  28452. Add(' b:=s=v;');
  28453. Add(' b:=v=''foo'';');
  28454. Add(' b:=''''=v;');
  28455. Add(' b:=v=d;');
  28456. Add(' b:=d=v;');
  28457. Add(' b:=v=3.4;');
  28458. Add(' b:=5.6=v;');
  28459. Add(' b:=v=c;');
  28460. Add(' b:=c=v;');
  28461. Add(' b:=m=m;');
  28462. Add(' b:=v=m;');
  28463. Add(' b:=m=v;');
  28464. ConvertProgram;
  28465. CheckSource('TestJSValue_Equal',
  28466. LinesToStr([ // statements
  28467. 'this.v = undefined;',
  28468. 'this.i = 0;',
  28469. 'this.s = "";',
  28470. 'this.b = false;',
  28471. 'this.d = 0.0;',
  28472. 'this.c = "";',
  28473. 'this.m = undefined;',
  28474. '']),
  28475. LinesToStr([ // $mod.$main
  28476. '$mod.b = $mod.v == $mod.v;',
  28477. '$mod.b = $mod.v != $mod.v;',
  28478. '$mod.b = $mod.v == 1;',
  28479. '$mod.b = $mod.v != 1;',
  28480. '$mod.b = 2 == $mod.v;',
  28481. '$mod.b = 2 != $mod.v;',
  28482. '$mod.b = $mod.v == $mod.i;',
  28483. '$mod.b = $mod.i == $mod.v;',
  28484. '$mod.b = $mod.v == null;',
  28485. '$mod.b = null == $mod.v;',
  28486. '$mod.b = $mod.v == false;',
  28487. '$mod.b = true == $mod.v;',
  28488. '$mod.b = $mod.v == $mod.b;',
  28489. '$mod.b = $mod.b == $mod.v;',
  28490. '$mod.b = $mod.v == $mod.s;',
  28491. '$mod.b = $mod.s == $mod.v;',
  28492. '$mod.b = $mod.v == "foo";',
  28493. '$mod.b = "" == $mod.v;',
  28494. '$mod.b = $mod.v == $mod.d;',
  28495. '$mod.b = $mod.d == $mod.v;',
  28496. '$mod.b = $mod.v == 3.4;',
  28497. '$mod.b = 5.6 == $mod.v;',
  28498. '$mod.b = $mod.v == $mod.c;',
  28499. '$mod.b = $mod.c == $mod.v;',
  28500. '$mod.b = $mod.m == $mod.m;',
  28501. '$mod.b = $mod.v == $mod.m;',
  28502. '$mod.b = $mod.m == $mod.v;',
  28503. '']));
  28504. end;
  28505. procedure TTestModule.TestJSValue_If;
  28506. begin
  28507. StartProgram(false);
  28508. Add([
  28509. 'procedure Fly(var u);',
  28510. 'begin',
  28511. ' if jsvalue(u) then ;',
  28512. 'end;',
  28513. 'var',
  28514. ' v: jsvalue;',
  28515. 'begin',
  28516. ' if v then ;',
  28517. ' while v do ;',
  28518. ' repeat until v;',
  28519. '']);
  28520. ConvertProgram;
  28521. CheckSource('TestJSValue_If',
  28522. LinesToStr([ // statements
  28523. 'this.Fly = function (u) {',
  28524. ' if (u.get()) ;',
  28525. '};',
  28526. 'this.v = undefined;',
  28527. '']),
  28528. LinesToStr([ // $mod.$main
  28529. 'if ($mod.v) ;',
  28530. 'while($mod.v){',
  28531. '};',
  28532. 'do{',
  28533. '} while(!$mod.v);',
  28534. '']));
  28535. end;
  28536. procedure TTestModule.TestJSValue_Not;
  28537. begin
  28538. StartProgram(false);
  28539. Add([
  28540. 'var',
  28541. ' v: jsvalue;',
  28542. ' b: boolean;',
  28543. 'begin',
  28544. ' b:=not v;',
  28545. ' if not v then ;',
  28546. ' while not v do ;',
  28547. ' repeat until not v;',
  28548. '']);
  28549. ConvertProgram;
  28550. CheckSource('TestJSValue_If',
  28551. LinesToStr([ // statements
  28552. 'this.v = undefined;',
  28553. 'this.b = false;',
  28554. '']),
  28555. LinesToStr([ // $mod.$main
  28556. '$mod.b=!$mod.v;',
  28557. 'if (!$mod.v) ;',
  28558. 'while(!$mod.v){',
  28559. '};',
  28560. 'do{',
  28561. '} while($mod.v);',
  28562. '']));
  28563. end;
  28564. procedure TTestModule.TestJSValue_Enum;
  28565. begin
  28566. StartProgram(false);
  28567. Add('type');
  28568. Add(' TColor = (red, blue);');
  28569. Add(' TRedBlue = TColor;');
  28570. Add('var');
  28571. Add(' v: jsvalue;');
  28572. Add(' e: TColor;');
  28573. Add('begin');
  28574. Add(' v:=e;');
  28575. Add(' v:=TColor(e);');
  28576. Add(' v:=TRedBlue(e);');
  28577. Add(' e:=TColor(v);');
  28578. Add(' e:=TRedBlue(v);');
  28579. ConvertProgram;
  28580. CheckSource('TestJSValue_Enum',
  28581. LinesToStr([ // statements
  28582. 'this.TColor = {',
  28583. ' "0": "red",',
  28584. ' red: 0,',
  28585. ' "1": "blue",',
  28586. ' blue: 1',
  28587. '};',
  28588. 'this.v = undefined;',
  28589. 'this.e = 0;',
  28590. '']),
  28591. LinesToStr([ // $mod.$main
  28592. '$mod.v = $mod.e;',
  28593. '$mod.v = $mod.e;',
  28594. '$mod.v = $mod.e;',
  28595. '$mod.e = $mod.v;',
  28596. '$mod.e = $mod.v;',
  28597. '']));
  28598. end;
  28599. procedure TTestModule.TestJSValue_ClassInstance;
  28600. begin
  28601. StartProgram(false);
  28602. Add([
  28603. 'type',
  28604. ' TObject = class',
  28605. ' end;',
  28606. ' TBirdObject = TObject;',
  28607. 'var',
  28608. ' v: jsvalue;',
  28609. ' o: TObject;',
  28610. 'begin',
  28611. ' v:=o;',
  28612. ' v:=TObject(o);',
  28613. ' v:=TBirdObject(o);',
  28614. ' o:=TObject(v);',
  28615. ' o:=TBirdObject(v);',
  28616. ' if v is TObject then ;',
  28617. '']);
  28618. ConvertProgram;
  28619. CheckSource('TestJSValue_ClassInstance',
  28620. LinesToStr([ // statements
  28621. 'rtl.createClass(this, "TObject", null, function () {',
  28622. ' this.$init = function () {',
  28623. ' };',
  28624. ' this.$final = function () {',
  28625. ' };',
  28626. '});',
  28627. 'this.v = undefined;',
  28628. 'this.o = null;',
  28629. '']),
  28630. LinesToStr([ // $mod.$main
  28631. '$mod.v = $mod.o;',
  28632. '$mod.v = $mod.o;',
  28633. '$mod.v = $mod.o;',
  28634. '$mod.o = rtl.getObject($mod.v);',
  28635. '$mod.o = rtl.getObject($mod.v);',
  28636. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  28637. '']));
  28638. end;
  28639. procedure TTestModule.TestJSValue_ClassOf;
  28640. begin
  28641. StartProgram(false);
  28642. Add([
  28643. 'type',
  28644. ' TClass = class of TObject;',
  28645. ' TObject = class',
  28646. ' end;',
  28647. ' TBirds = class of TBird;',
  28648. ' TBird = class(TObject) end;',
  28649. 'var',
  28650. ' v: jsvalue;',
  28651. ' c: TClass;',
  28652. 'begin',
  28653. ' v:=c;',
  28654. ' v:=TObject;',
  28655. ' v:=TClass(c);',
  28656. ' v:=TBirds(c);',
  28657. ' c:=TClass(v);',
  28658. ' c:=TBirds(v);',
  28659. ' if v is TClass then ;',
  28660. '']);
  28661. ConvertProgram;
  28662. CheckSource('TestJSValue_ClassOf',
  28663. LinesToStr([ // statements
  28664. 'rtl.createClass(this, "TObject", null, function () {',
  28665. ' this.$init = function () {',
  28666. ' };',
  28667. ' this.$final = function () {',
  28668. ' };',
  28669. '});',
  28670. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28671. '});',
  28672. 'this.v = undefined;',
  28673. 'this.c = null;',
  28674. '']),
  28675. LinesToStr([ // $mod.$main
  28676. '$mod.v = $mod.c;',
  28677. '$mod.v = $mod.TObject;',
  28678. '$mod.v = $mod.c;',
  28679. '$mod.v = $mod.c;',
  28680. '$mod.c = rtl.getObject($mod.v);',
  28681. '$mod.c = rtl.getObject($mod.v);',
  28682. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  28683. '']));
  28684. end;
  28685. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  28686. begin
  28687. StartProgram(false);
  28688. Add([
  28689. 'type',
  28690. ' integer = longint;',
  28691. ' TArray = array of JSValue;',
  28692. ' TArrgh = tarray;',
  28693. ' TArrInt = array of integer;',
  28694. 'var',
  28695. ' v: jsvalue;',
  28696. ' TheArray: tarray = (1,''2'');',
  28697. ' Arr: tarrgh;',
  28698. ' i: integer;',
  28699. ' ArrInt: tarrint;',
  28700. 'begin',
  28701. ' arr:=thearray;',
  28702. ' thearray:=arr;',
  28703. ' setlength(arr,2);',
  28704. ' setlength(thearray,3);',
  28705. ' arr[4]:=v;',
  28706. ' arr[5]:=length(thearray);',
  28707. ' arr[6]:=nil;',
  28708. ' arr[7]:=thearray[8];',
  28709. ' arr[low(arr)]:=high(thearray);',
  28710. ' arr:=arrint;',
  28711. ' arrInt:=tarrint(arr);',
  28712. ' if TheArray = nil then ;',
  28713. ' if nil = TheArray then ;',
  28714. ' if TheArray <> nil then ;',
  28715. ' if nil <> TheArray then ;',
  28716. '']);
  28717. ConvertProgram;
  28718. CheckSource('TestJSValue_ArrayOfJSValue',
  28719. LinesToStr([ // statements
  28720. 'this.v = undefined;',
  28721. 'this.TheArray = [1, "2"];',
  28722. 'this.Arr = [];',
  28723. 'this.i = 0;',
  28724. 'this.ArrInt = [];',
  28725. '']),
  28726. LinesToStr([ // $mod.$main
  28727. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  28728. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  28729. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  28730. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  28731. '$mod.Arr[4] = $mod.v;',
  28732. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  28733. '$mod.Arr[6] = null;',
  28734. '$mod.Arr[7] = $mod.TheArray[8];',
  28735. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  28736. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  28737. '$mod.ArrInt = $mod.Arr;',
  28738. 'if (rtl.length($mod.TheArray) === 0) ;',
  28739. 'if (rtl.length($mod.TheArray) === 0) ;',
  28740. 'if (rtl.length($mod.TheArray) > 0) ;',
  28741. 'if (rtl.length($mod.TheArray) > 0) ;',
  28742. '']));
  28743. end;
  28744. procedure TTestModule.TestJSValue_ArrayLit;
  28745. begin
  28746. StartProgram(false);
  28747. Add([
  28748. 'type',
  28749. ' TFlag = (big,small);',
  28750. ' TArray = array of JSValue;',
  28751. ' TObject = class end;',
  28752. ' TClass = class of TObject;',
  28753. 'var',
  28754. ' v: jsvalue;',
  28755. ' a: TArray;',
  28756. ' o: TObject;',
  28757. 'begin',
  28758. ' a:=[];',
  28759. ' a:=[1];',
  28760. ' a:=[1,2];',
  28761. ' a:=[big];',
  28762. ' a:=[1,big];',
  28763. ' a:=[o,nil];',
  28764. '']);
  28765. ConvertProgram;
  28766. CheckSource('TestJSValue_ArrayLit',
  28767. LinesToStr([ // statements
  28768. 'this.TFlag = {',
  28769. ' "0": "big",',
  28770. ' big: 0,',
  28771. ' "1": "small",',
  28772. ' small: 1',
  28773. '};',
  28774. 'rtl.createClass(this, "TObject", null, function () {',
  28775. ' this.$init = function () {',
  28776. ' };',
  28777. ' this.$final = function () {',
  28778. ' };',
  28779. '});',
  28780. 'this.v = undefined;',
  28781. 'this.a = [];',
  28782. 'this.o = null;',
  28783. '']),
  28784. LinesToStr([ // $mod.$main
  28785. '$mod.a = [];',
  28786. '$mod.a = [1];',
  28787. '$mod.a = [1, 2];',
  28788. '$mod.a = [$mod.TFlag.big];',
  28789. '$mod.a = [1, $mod.TFlag.big];',
  28790. '$mod.a = [$mod.o, null];',
  28791. '']));
  28792. end;
  28793. procedure TTestModule.TestJSValue_Params;
  28794. begin
  28795. StartProgram(false);
  28796. Add('type');
  28797. Add(' integer = longint;');
  28798. Add(' TYesNo = boolean;');
  28799. Add(' TFloat = double;');
  28800. Add(' TCaption = string;');
  28801. Add(' TChar = char;');
  28802. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  28803. Add('var');
  28804. Add(' l: jsvalue;');
  28805. Add('begin');
  28806. Add(' a:=a;');
  28807. Add(' l:=b;');
  28808. Add(' c:=c;');
  28809. Add(' d:=d;');
  28810. Add(' Result:=l;');
  28811. Add('end;');
  28812. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  28813. Add('var');
  28814. Add(' v: jsvalue;');
  28815. Add(' i: integer;');
  28816. Add(' b: TYesNo;');
  28817. Add(' d: TFloat;');
  28818. Add(' s: TCaption;');
  28819. Add(' c: TChar;');
  28820. Add('begin');
  28821. Add(' v:=doit(v,v,v,v);');
  28822. Add(' i:=integer(dosome(i,i));');
  28823. Add(' b:=TYesNo(dosome(b,b));');
  28824. Add(' d:=TFloat(dosome(d,d));');
  28825. Add(' s:=TCaption(dosome(s,s));');
  28826. Add(' c:=TChar(dosome(c,c));');
  28827. ConvertProgram;
  28828. CheckSource('TestJSValue_Params',
  28829. LinesToStr([ // statements
  28830. 'this.DoIt = function (a, b, c, d) {',
  28831. ' var Result = undefined;',
  28832. ' var l = undefined;',
  28833. ' a = a;',
  28834. ' l = b;',
  28835. ' c.set(c.get());',
  28836. ' d.set(d.get());',
  28837. ' Result = l;',
  28838. ' return Result;',
  28839. '};',
  28840. 'this.DoSome = function (a, b) {',
  28841. ' var Result = undefined;',
  28842. ' return Result;',
  28843. '};',
  28844. 'this.v = undefined;',
  28845. 'this.i = 0;',
  28846. 'this.b = false;',
  28847. 'this.d = 0.0;',
  28848. 'this.s = "";',
  28849. 'this.c = "";',
  28850. '']),
  28851. LinesToStr([ // $mod.$main
  28852. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  28853. ' p: $mod,',
  28854. ' get: function () {',
  28855. ' return this.p.v;',
  28856. ' },',
  28857. ' set: function (v) {',
  28858. ' this.p.v = v;',
  28859. ' }',
  28860. '}, {',
  28861. ' p: $mod,',
  28862. ' get: function () {',
  28863. ' return this.p.v;',
  28864. ' },',
  28865. ' set: function (v) {',
  28866. ' this.p.v = v;',
  28867. ' }',
  28868. '});',
  28869. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  28870. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  28871. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  28872. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  28873. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  28874. '']));
  28875. end;
  28876. procedure TTestModule.TestJSValue_UntypedParam;
  28877. begin
  28878. StartProgram(false);
  28879. Add('function DoIt(const a; var b; out c): jsvalue;');
  28880. Add('begin');
  28881. Add(' Result:=a;');
  28882. Add(' Result:=b;');
  28883. Add(' Result:=c;');
  28884. Add(' b:=Result;');
  28885. Add(' c:=Result;');
  28886. Add('end;');
  28887. Add('var i: longint;');
  28888. Add('begin');
  28889. Add(' doit(i,i,i);');
  28890. ConvertProgram;
  28891. CheckSource('TestJSValue_UntypedParam',
  28892. LinesToStr([ // statements
  28893. 'this.DoIt = function (a, b, c) {',
  28894. ' var Result = undefined;',
  28895. ' Result = a;',
  28896. ' Result = b.get();',
  28897. ' Result = c.get();',
  28898. ' b.set(Result);',
  28899. ' c.set(Result);',
  28900. ' return Result;',
  28901. '};',
  28902. 'this.i = 0;',
  28903. '']),
  28904. LinesToStr([ // $mod.$main
  28905. '$mod.DoIt($mod.i, {',
  28906. ' p: $mod,',
  28907. ' get: function () {',
  28908. ' return this.p.i;',
  28909. ' },',
  28910. ' set: function (v) {',
  28911. ' this.p.i = v;',
  28912. ' }',
  28913. '}, {',
  28914. ' p: $mod,',
  28915. ' get: function () {',
  28916. ' return this.p.i;',
  28917. ' },',
  28918. ' set: function (v) {',
  28919. ' this.p.i = v;',
  28920. ' }',
  28921. '});',
  28922. '']));
  28923. end;
  28924. procedure TTestModule.TestJSValue_FuncResultType;
  28925. begin
  28926. StartProgram(false);
  28927. Add('type');
  28928. Add(' integer = longint;');
  28929. Add(' TJSValueArray = array of JSValue;');
  28930. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  28931. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  28932. Add('begin');
  28933. Add(' while Compare(P,aList[0])>0 do ;');
  28934. Add('end;');
  28935. Add('var');
  28936. Add(' Compare: TListSortCompare;');
  28937. Add(' V: JSValue;');
  28938. Add(' i: integer;');
  28939. Add('begin');
  28940. Add(' if Compare(V,V)>0 then ;');
  28941. Add(' if Compare(i,i)>1 then ;');
  28942. Add(' if Compare(nil,false)>2 then ;');
  28943. Add(' if Compare(1,true)>3 then ;');
  28944. ConvertProgram;
  28945. CheckSource('TestJSValue_UntypedParam',
  28946. LinesToStr([ // statements
  28947. 'this.Sort = function (P, aList, Compare) {',
  28948. ' while (Compare(P, aList[0]) > 0) {',
  28949. ' };',
  28950. '};',
  28951. 'this.Compare = null;',
  28952. 'this.V = undefined;',
  28953. 'this.i = 0;',
  28954. '']),
  28955. LinesToStr([ // $mod.$main
  28956. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  28957. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  28958. 'if ($mod.Compare(null, false) > 2) ;',
  28959. 'if ($mod.Compare(1, true) > 3) ;',
  28960. '']));
  28961. end;
  28962. procedure TTestModule.TestJSValue_ProcType_Assign;
  28963. begin
  28964. StartProgram(false);
  28965. Add('type');
  28966. Add(' integer = longint;');
  28967. Add(' TObject = class');
  28968. Add(' class function GetGlob: integer;');
  28969. Add(' function Getter: integer;');
  28970. Add(' end;');
  28971. Add('class function TObject.GetGlob: integer;');
  28972. Add('var v1: jsvalue;');
  28973. Add('begin');
  28974. Add(' v1:=@GetGlob;');
  28975. Add(' v1:[email protected];');
  28976. Add('end;');
  28977. Add('function TObject.Getter: integer;');
  28978. Add('var v2: jsvalue;');
  28979. Add('begin');
  28980. Add(' v2:=@Getter;');
  28981. Add(' v2:[email protected];');
  28982. Add(' v2:=@GetGlob;');
  28983. Add(' v2:[email protected];');
  28984. Add('end;');
  28985. Add('function GetIt(i: integer): integer;');
  28986. Add('var v3: jsvalue;');
  28987. Add('begin');
  28988. Add(' v3:=@GetIt;');
  28989. Add('end;');
  28990. Add('var');
  28991. Add(' V: JSValue;');
  28992. Add(' o: TObject;');
  28993. Add('begin');
  28994. Add(' v:=@GetIt;');
  28995. Add(' v:[email protected];');
  28996. Add(' v:[email protected];');
  28997. ConvertProgram;
  28998. CheckSource('TestJSValue_ProcType_Assign',
  28999. LinesToStr([ // statements
  29000. 'rtl.createClass(this, "TObject", null, function () {',
  29001. ' this.$init = function () {',
  29002. ' };',
  29003. ' this.$final = function () {',
  29004. ' };',
  29005. ' this.GetGlob = function () {',
  29006. ' var Result = 0;',
  29007. ' var v1 = undefined;',
  29008. ' v1 = rtl.createCallback(this, "GetGlob");',
  29009. ' v1 = rtl.createCallback(this, "GetGlob");',
  29010. ' return Result;',
  29011. ' };',
  29012. ' this.Getter = function () {',
  29013. ' var Result = 0;',
  29014. ' var v2 = undefined;',
  29015. ' v2 = rtl.createCallback(this, "Getter");',
  29016. ' v2 = rtl.createCallback(this, "Getter");',
  29017. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29018. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29019. ' return Result;',
  29020. ' };',
  29021. '});',
  29022. 'this.GetIt = function (i) {',
  29023. ' var Result = 0;',
  29024. ' var v3 = undefined;',
  29025. ' v3 = $mod.GetIt;',
  29026. ' return Result;',
  29027. '};',
  29028. 'this.V = undefined;',
  29029. 'this.o = null;',
  29030. '']),
  29031. LinesToStr([ // $mod.$main
  29032. '$mod.V = $mod.GetIt;',
  29033. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29034. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29035. '']));
  29036. end;
  29037. procedure TTestModule.TestJSValue_ProcType_Equal;
  29038. begin
  29039. StartProgram(false);
  29040. Add('type');
  29041. Add(' integer = longint;');
  29042. Add(' TObject = class');
  29043. Add(' class function GetGlob: integer;');
  29044. Add(' function Getter: integer;');
  29045. Add(' end;');
  29046. Add('class function TObject.GetGlob: integer;');
  29047. Add('var v1: jsvalue;');
  29048. Add('begin');
  29049. Add(' if v1=@GetGlob then;');
  29050. Add(' if [email protected] then ;');
  29051. Add('end;');
  29052. Add('function TObject.Getter: integer;');
  29053. Add('var v2: jsvalue;');
  29054. Add('begin');
  29055. Add(' if v2=@Getter then;');
  29056. Add(' if [email protected] then ;');
  29057. Add(' if v2=@GetGlob then;');
  29058. Add(' if [email protected] then;');
  29059. Add('end;');
  29060. Add('function GetIt(i: integer): integer;');
  29061. Add('var v3: jsvalue;');
  29062. Add('begin');
  29063. Add(' if v3=@GetIt then;');
  29064. Add('end;');
  29065. Add('var');
  29066. Add(' V: JSValue;');
  29067. Add(' o: TObject;');
  29068. Add('begin');
  29069. Add(' if v=@GetIt then;');
  29070. Add(' if [email protected] then;');
  29071. Add(' if [email protected] then;');
  29072. Add(' if @GetIt=v then;');
  29073. Add(' if @o.Getter=v then;');
  29074. Add(' if @o.GetGlob=v then;');
  29075. ConvertProgram;
  29076. CheckSource('TestJSValue_ProcType_Equal',
  29077. LinesToStr([ // statements
  29078. 'rtl.createClass(this, "TObject", null, function () {',
  29079. ' this.$init = function () {',
  29080. ' };',
  29081. ' this.$final = function () {',
  29082. ' };',
  29083. ' this.GetGlob = function () {',
  29084. ' var Result = 0;',
  29085. ' var v1 = undefined;',
  29086. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29087. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29088. ' return Result;',
  29089. ' };',
  29090. ' this.Getter = function () {',
  29091. ' var Result = 0;',
  29092. ' var v2 = undefined;',
  29093. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29094. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29095. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29096. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29097. ' return Result;',
  29098. ' };',
  29099. '});',
  29100. 'this.GetIt = function (i) {',
  29101. ' var Result = 0;',
  29102. ' var v3 = undefined;',
  29103. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29104. ' return Result;',
  29105. '};',
  29106. 'this.V = undefined;',
  29107. 'this.o = null;',
  29108. '']),
  29109. LinesToStr([ // $mod.$main
  29110. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29111. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29112. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29113. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29114. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29115. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29116. '']));
  29117. end;
  29118. procedure TTestModule.TestJSValue_ProcType_Param;
  29119. begin
  29120. StartProgram(false);
  29121. Add([
  29122. 'type',
  29123. ' variant = jsvalue;',
  29124. ' TArrVariant = array of variant;',
  29125. ' TArrVar2 = TArrVariant;',
  29126. ' TFuncInt = function: longint;',
  29127. 'function GetIt: longint;',
  29128. 'begin',
  29129. 'end;',
  29130. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29131. 'var v: variant;',
  29132. 'begin',
  29133. ' v:=arr[1];',
  29134. 'end;',
  29135. 'var s: string;',
  29136. 'begin',
  29137. ' DoIt(GetIt,[]);',
  29138. ' DoIt(@GetIt,[]);',
  29139. ' DoIt(1,[s,GetIt]);',
  29140. ' DoIt(1,[s,@GetIt]);',
  29141. '']);
  29142. ConvertProgram;
  29143. CheckSource('TestJSValue_ProcType_Param',
  29144. LinesToStr([ // statements
  29145. 'this.GetIt = function () {',
  29146. ' var Result = 0;',
  29147. ' return Result;',
  29148. '};',
  29149. 'this.DoIt = function (p, Arr) {',
  29150. ' var v = undefined;',
  29151. ' v = Arr[1];',
  29152. '};',
  29153. 'this.s = "";',
  29154. '']),
  29155. LinesToStr([ // $mod.$main
  29156. '$mod.DoIt($mod.GetIt(), []);',
  29157. '$mod.DoIt($mod.GetIt, []);',
  29158. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29159. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29160. '']));
  29161. end;
  29162. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29163. begin
  29164. StartProgram(false);
  29165. Add([
  29166. 'var',
  29167. ' v: JSValue;',
  29168. ' p: Pointer;',
  29169. 'begin',
  29170. ' p:=v;',
  29171. '']);
  29172. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29173. nIncompatibleTypesGotExpected);
  29174. ConvertProgram;
  29175. end;
  29176. procedure TTestModule.TestJSValue_OverloadDouble;
  29177. begin
  29178. StartProgram(false);
  29179. Add([
  29180. 'type',
  29181. ' integer = longint;',
  29182. ' tdatetime = double;',
  29183. 'procedure DoIt(d: double); begin end;',
  29184. 'procedure DoIt(v: jsvalue); begin end;',
  29185. 'var',
  29186. ' d: double;',
  29187. ' dt: tdatetime;',
  29188. ' i: integer;',
  29189. ' b: byte;',
  29190. ' shi: shortint;',
  29191. ' w: word;',
  29192. ' smi: smallint;',
  29193. ' lw: longword;',
  29194. ' li: longint;',
  29195. ' ni: nativeint;',
  29196. ' nu: nativeuint;',
  29197. 'begin',
  29198. ' DoIt(d);',
  29199. ' DoIt(dt);',
  29200. ' DoIt(i);',
  29201. ' DoIt(b);',
  29202. ' DoIt(shi);',
  29203. ' DoIt(w);',
  29204. ' DoIt(smi);',
  29205. ' DoIt(lw);',
  29206. ' DoIt(li);',
  29207. ' DoIt(ni);',
  29208. ' DoIt(nu);',
  29209. '']);
  29210. ConvertProgram;
  29211. CheckSource('TestJSValue_OverloadDouble',
  29212. LinesToStr([ // statements
  29213. 'this.DoIt = function (d) {',
  29214. '};',
  29215. 'this.DoIt$1 = function (v) {',
  29216. '};',
  29217. 'this.d = 0.0;',
  29218. 'this.dt = 0.0;',
  29219. 'this.i = 0;',
  29220. 'this.b = 0;',
  29221. 'this.shi = 0;',
  29222. 'this.w = 0;',
  29223. 'this.smi = 0;',
  29224. 'this.lw = 0;',
  29225. 'this.li = 0;',
  29226. 'this.ni = 0;',
  29227. 'this.nu = 0;',
  29228. '']),
  29229. LinesToStr([ // $mod.$main
  29230. '$mod.DoIt($mod.d);',
  29231. '$mod.DoIt($mod.dt);',
  29232. '$mod.DoIt$1($mod.i);',
  29233. '$mod.DoIt$1($mod.b);',
  29234. '$mod.DoIt$1($mod.shi);',
  29235. '$mod.DoIt$1($mod.w);',
  29236. '$mod.DoIt$1($mod.smi);',
  29237. '$mod.DoIt$1($mod.lw);',
  29238. '$mod.DoIt$1($mod.li);',
  29239. '$mod.DoIt$1($mod.ni);',
  29240. '$mod.DoIt$1($mod.nu);',
  29241. '']));
  29242. end;
  29243. procedure TTestModule.TestJSValue_OverloadNativeInt;
  29244. begin
  29245. StartProgram(false);
  29246. Add([
  29247. 'type',
  29248. ' integer = longint;',
  29249. ' int53 = nativeint;',
  29250. ' tdatetime = double;',
  29251. 'procedure DoIt(n: nativeint); begin end;',
  29252. 'procedure DoIt(v: jsvalue); begin end;',
  29253. 'var',
  29254. ' d: double;',
  29255. ' dt: tdatetime;',
  29256. ' i: integer;',
  29257. ' b: byte;',
  29258. ' shi: shortint;',
  29259. ' w: word;',
  29260. ' smi: smallint;',
  29261. ' lw: longword;',
  29262. ' li: longint;',
  29263. ' ni: nativeint;',
  29264. ' nu: nativeuint;',
  29265. 'begin',
  29266. ' DoIt(d);',
  29267. ' DoIt(dt);',
  29268. ' DoIt(i);',
  29269. ' DoIt(b);',
  29270. ' DoIt(shi);',
  29271. ' DoIt(w);',
  29272. ' DoIt(smi);',
  29273. ' DoIt(lw);',
  29274. ' DoIt(li);',
  29275. ' DoIt(ni);',
  29276. ' DoIt(nu);',
  29277. '']);
  29278. ConvertProgram;
  29279. CheckSource('TestJSValue_OverloadNativeInt',
  29280. LinesToStr([ // statements
  29281. 'this.DoIt = function (n) {',
  29282. '};',
  29283. 'this.DoIt$1 = function (v) {',
  29284. '};',
  29285. 'this.d = 0.0;',
  29286. 'this.dt = 0.0;',
  29287. 'this.i = 0;',
  29288. 'this.b = 0;',
  29289. 'this.shi = 0;',
  29290. 'this.w = 0;',
  29291. 'this.smi = 0;',
  29292. 'this.lw = 0;',
  29293. 'this.li = 0;',
  29294. 'this.ni = 0;',
  29295. 'this.nu = 0;',
  29296. '']),
  29297. LinesToStr([ // $mod.$main
  29298. '$mod.DoIt$1($mod.d);',
  29299. '$mod.DoIt$1($mod.dt);',
  29300. '$mod.DoIt($mod.i);',
  29301. '$mod.DoIt($mod.b);',
  29302. '$mod.DoIt($mod.shi);',
  29303. '$mod.DoIt($mod.w);',
  29304. '$mod.DoIt($mod.smi);',
  29305. '$mod.DoIt($mod.lw);',
  29306. '$mod.DoIt($mod.li);',
  29307. '$mod.DoIt($mod.ni);',
  29308. '$mod.DoIt($mod.nu);',
  29309. '']));
  29310. end;
  29311. procedure TTestModule.TestJSValue_OverloadWord;
  29312. begin
  29313. StartProgram(false);
  29314. Add([
  29315. 'type',
  29316. ' integer = longint;',
  29317. ' int53 = nativeint;',
  29318. ' tdatetime = double;',
  29319. 'procedure DoIt(w: word); begin end;',
  29320. 'procedure DoIt(v: jsvalue); begin end;',
  29321. 'var',
  29322. ' d: double;',
  29323. ' dt: tdatetime;',
  29324. ' i: integer;',
  29325. ' b: byte;',
  29326. ' shi: shortint;',
  29327. ' w: word;',
  29328. ' smi: smallint;',
  29329. ' lw: longword;',
  29330. ' li: longint;',
  29331. ' ni: nativeint;',
  29332. ' nu: nativeuint;',
  29333. 'begin',
  29334. ' DoIt(d);',
  29335. ' DoIt(dt);',
  29336. ' DoIt(i);',
  29337. ' DoIt(b);',
  29338. ' DoIt(shi);',
  29339. ' DoIt(w);',
  29340. ' DoIt(smi);',
  29341. ' DoIt(lw);',
  29342. ' DoIt(li);',
  29343. ' DoIt(ni);',
  29344. ' DoIt(nu);',
  29345. '']);
  29346. ConvertProgram;
  29347. CheckSource('TestJSValue_OverloadWord',
  29348. LinesToStr([ // statements
  29349. 'this.DoIt = function (w) {',
  29350. '};',
  29351. 'this.DoIt$1 = function (v) {',
  29352. '};',
  29353. 'this.d = 0.0;',
  29354. 'this.dt = 0.0;',
  29355. 'this.i = 0;',
  29356. 'this.b = 0;',
  29357. 'this.shi = 0;',
  29358. 'this.w = 0;',
  29359. 'this.smi = 0;',
  29360. 'this.lw = 0;',
  29361. 'this.li = 0;',
  29362. 'this.ni = 0;',
  29363. 'this.nu = 0;',
  29364. '']),
  29365. LinesToStr([ // $mod.$main
  29366. '$mod.DoIt$1($mod.d);',
  29367. '$mod.DoIt$1($mod.dt);',
  29368. '$mod.DoIt$1($mod.i);',
  29369. '$mod.DoIt($mod.b);',
  29370. '$mod.DoIt($mod.shi);',
  29371. '$mod.DoIt($mod.w);',
  29372. '$mod.DoIt$1($mod.smi);',
  29373. '$mod.DoIt$1($mod.lw);',
  29374. '$mod.DoIt$1($mod.li);',
  29375. '$mod.DoIt$1($mod.ni);',
  29376. '$mod.DoIt$1($mod.nu);',
  29377. '']));
  29378. end;
  29379. procedure TTestModule.TestJSValue_OverloadString;
  29380. begin
  29381. StartProgram(false);
  29382. Add([
  29383. 'type',
  29384. ' uni = string;',
  29385. ' WChar = char;',
  29386. 'procedure DoIt(s: string); begin end;',
  29387. 'procedure DoIt(v: jsvalue); begin end;',
  29388. 'var',
  29389. ' s: string;',
  29390. ' c: char;',
  29391. ' u: uni;',
  29392. 'begin',
  29393. ' DoIt(s);',
  29394. ' DoIt(c);',
  29395. ' DoIt(u);',
  29396. '']);
  29397. ConvertProgram;
  29398. CheckSource('TestJSValue_OverloadString',
  29399. LinesToStr([ // statements
  29400. 'this.DoIt = function (s) {',
  29401. '};',
  29402. 'this.DoIt$1 = function (v) {',
  29403. '};',
  29404. 'this.s = "";',
  29405. 'this.c = "";',
  29406. 'this.u = "";',
  29407. '']),
  29408. LinesToStr([ // $mod.$main
  29409. '$mod.DoIt($mod.s);',
  29410. '$mod.DoIt($mod.c);',
  29411. '$mod.DoIt($mod.u);',
  29412. '']));
  29413. end;
  29414. procedure TTestModule.TestJSValue_OverloadChar;
  29415. begin
  29416. StartProgram(false);
  29417. Add([
  29418. 'type',
  29419. ' uni = string;',
  29420. ' WChar = char;',
  29421. 'procedure DoIt(c: char); begin end;',
  29422. 'procedure DoIt(v: jsvalue); begin end;',
  29423. 'var',
  29424. ' s: string;',
  29425. ' c: char;',
  29426. ' u: uni;',
  29427. 'begin',
  29428. ' DoIt(s);',
  29429. ' DoIt(c);',
  29430. ' DoIt(u);',
  29431. '']);
  29432. ConvertProgram;
  29433. CheckSource('TestJSValue_OverloadChar',
  29434. LinesToStr([ // statements
  29435. 'this.DoIt = function (c) {',
  29436. '};',
  29437. 'this.DoIt$1 = function (v) {',
  29438. '};',
  29439. 'this.s = "";',
  29440. 'this.c = "";',
  29441. 'this.u = "";',
  29442. '']),
  29443. LinesToStr([ // $mod.$main
  29444. '$mod.DoIt$1($mod.s);',
  29445. '$mod.DoIt($mod.c);',
  29446. '$mod.DoIt$1($mod.u);',
  29447. '']));
  29448. end;
  29449. procedure TTestModule.TestJSValue_OverloadPointer;
  29450. begin
  29451. StartProgram(false);
  29452. Add([
  29453. 'type',
  29454. ' TObject = class end;',
  29455. 'procedure DoIt(p: pointer); begin end;',
  29456. 'procedure DoIt(v: jsvalue); begin end;',
  29457. 'var',
  29458. ' o: TObject;',
  29459. 'begin',
  29460. ' DoIt(o);',
  29461. '']);
  29462. ConvertProgram;
  29463. CheckSource('TestJSValue_OverloadPointer',
  29464. LinesToStr([ // statements
  29465. 'rtl.createClass(this, "TObject", null, function () {',
  29466. ' this.$init = function () {',
  29467. ' };',
  29468. ' this.$final = function () {',
  29469. ' };',
  29470. '});',
  29471. 'this.DoIt = function (p) {',
  29472. '};',
  29473. 'this.DoIt$1 = function (v) {',
  29474. '};',
  29475. 'this.o = null;',
  29476. '']),
  29477. LinesToStr([ // $mod.$main
  29478. '$mod.DoIt($mod.o);',
  29479. '']));
  29480. end;
  29481. procedure TTestModule.TestJSValue_ForIn;
  29482. begin
  29483. StartProgram(false);
  29484. Add([
  29485. 'var',
  29486. ' v: JSValue;',
  29487. ' key: string;',
  29488. 'begin',
  29489. ' for key in v do begin',
  29490. ' if key=''abc'' then ;',
  29491. ' end;',
  29492. '']);
  29493. ConvertProgram;
  29494. CheckSource('TestJSValue_ForIn',
  29495. LinesToStr([ // statements
  29496. 'this.v = undefined;',
  29497. 'this.key = "";',
  29498. '']),
  29499. LinesToStr([ // $mod.$main
  29500. 'for ($mod.key in $mod.v) {',
  29501. ' if ($mod.key === "abc") ;',
  29502. '};',
  29503. '']));
  29504. end;
  29505. procedure TTestModule.TestRTTI_IntRange;
  29506. begin
  29507. WithTypeInfo:=true;
  29508. StartProgram(true,[supTypeInfo]);
  29509. Add([
  29510. '{$modeswitch externalclass}',
  29511. 'type',
  29512. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  29513. ' TColor = type TGraphicsColor;',
  29514. 'var',
  29515. ' p: TTypeInfo;',
  29516. ' k: TTypeKind;',
  29517. 'begin',
  29518. ' p:=typeinfo(TGraphicsColor);',
  29519. ' p:=typeinfo(TColor);',
  29520. ' k:=GetTypeKind(TGraphicsColor);',
  29521. ' k:=GetTypeKind(TColor);',
  29522. '']);
  29523. ConvertProgram;
  29524. CheckSource('TestRTTI_IntRange',
  29525. LinesToStr([ // statements
  29526. 'this.$rtti.$Int("TGraphicsColor", {',
  29527. ' minvalue: -2147483648,',
  29528. ' maxvalue: 2147483647,',
  29529. ' ordtype: 4',
  29530. '});',
  29531. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  29532. 'this.p = null;',
  29533. 'this.k = 0;',
  29534. '']),
  29535. LinesToStr([ // $mod.$main
  29536. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  29537. '$mod.p = $mod.$rtti["TColor"];',
  29538. '$mod.k = 1;',
  29539. '$mod.k = 1;',
  29540. '']));
  29541. end;
  29542. procedure TTestModule.TestRTTI_Double;
  29543. begin
  29544. WithTypeInfo:=true;
  29545. StartProgram(true,[supTypeInfo]);
  29546. Add([
  29547. '{$modeswitch externalclass}',
  29548. 'type',
  29549. ' TFloat = type double;',
  29550. 'var',
  29551. ' p: TTypeInfo;',
  29552. 'begin',
  29553. ' p:=typeinfo(double);',
  29554. ' p:=typeinfo(TFloat);',
  29555. '']);
  29556. ConvertProgram;
  29557. CheckSource('TestRTTI_Double',
  29558. LinesToStr([ // statements
  29559. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  29560. 'this.p = null;',
  29561. '']),
  29562. LinesToStr([ // $mod.$main
  29563. '$mod.p = rtl.double;',
  29564. '$mod.p = $mod.$rtti["TFloat"];',
  29565. '']));
  29566. end;
  29567. procedure TTestModule.TestRTTI_ProcType;
  29568. begin
  29569. WithTypeInfo:=true;
  29570. StartProgram(false);
  29571. Add('type');
  29572. Add(' TProcA = procedure;');
  29573. Add(' TMethodB = procedure of object;');
  29574. Add(' TProcC = procedure; varargs;');
  29575. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  29576. Add(' TProcE = function: nativeint;');
  29577. Add(' TProcF = function(const p: TProcA): nativeuint;');
  29578. Add('var p: pointer;');
  29579. Add('begin');
  29580. Add(' p:=typeinfo(tproca);');
  29581. ConvertProgram;
  29582. CheckSource('TestRTTI_ProcType',
  29583. LinesToStr([ // statements
  29584. 'this.$rtti.$ProcVar("TProcA", {',
  29585. ' procsig: rtl.newTIProcSig([])',
  29586. '});',
  29587. 'this.$rtti.$MethodVar("TMethodB", {',
  29588. ' procsig: rtl.newTIProcSig([]),',
  29589. ' methodkind: 0',
  29590. '});',
  29591. 'this.$rtti.$ProcVar("TProcC", {',
  29592. ' procsig: rtl.newTIProcSig([], null, 2)',
  29593. '});',
  29594. 'this.$rtti.$ProcVar("TProcD", {',
  29595. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  29596. '});',
  29597. 'this.$rtti.$ProcVar("TProcE", {',
  29598. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  29599. '});',
  29600. 'this.$rtti.$ProcVar("TProcF", {',
  29601. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  29602. '});',
  29603. 'this.p = null;',
  29604. '']),
  29605. LinesToStr([ // $mod.$main
  29606. '$mod.p = $mod.$rtti["TProcA"];',
  29607. '']));
  29608. end;
  29609. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  29610. begin
  29611. WithTypeInfo:=true;
  29612. AddModuleWithIntfImplSrc('unit2.pas',
  29613. LinesToStr([
  29614. 'type',
  29615. ' TObject = class end;'
  29616. ]),
  29617. '');
  29618. StartUnit(true);
  29619. Add('interface');
  29620. Add('uses unit2;');
  29621. Add('type');
  29622. Add(' TProcA = function(o: tobject): tobject;');
  29623. Add('implementation');
  29624. Add('type');
  29625. Add(' TProcB = function(o: tobject): tobject;');
  29626. Add('var p: Pointer;');
  29627. Add('initialization');
  29628. Add(' p:=typeinfo(tproca);');
  29629. Add(' p:=typeinfo(tprocb);');
  29630. ConvertUnit;
  29631. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  29632. LinesToStr([ // statements
  29633. 'var $impl = $mod.$impl;',
  29634. 'this.$rtti.$ProcVar("TProcA", {',
  29635. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  29636. '});',
  29637. '']),
  29638. LinesToStr([ // this.$init
  29639. '$impl.p = $mod.$rtti["TProcA"];',
  29640. '$impl.p = $mod.$rtti["TProcB"];',
  29641. '']),
  29642. LinesToStr([ // implementation
  29643. '$mod.$rtti.$ProcVar("TProcB", {',
  29644. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  29645. '});',
  29646. '$impl.p = null;',
  29647. '']) );
  29648. end;
  29649. procedure TTestModule.TestRTTI_EnumAndSetType;
  29650. begin
  29651. WithTypeInfo:=true;
  29652. StartProgram(false);
  29653. Add('type');
  29654. Add(' TFlag = (light,dark);');
  29655. Add(' TFlags = set of TFlag;');
  29656. Add(' TProc = function(f: TFlags): TFlag;');
  29657. Add('var p: pointer;');
  29658. Add('begin');
  29659. Add(' p:=typeinfo(tflag);');
  29660. Add(' p:=typeinfo(tflags);');
  29661. ConvertProgram;
  29662. CheckSource('TestRTTI_EnumAndType',
  29663. LinesToStr([ // statements
  29664. 'this.TFlag = {',
  29665. ' "0": "light",',
  29666. ' light: 0,',
  29667. ' "1": "dark",',
  29668. ' dark: 1',
  29669. '};',
  29670. 'this.$rtti.$Enum("TFlag", {',
  29671. ' minvalue: 0,',
  29672. ' maxvalue: 1,',
  29673. ' ordtype: 1,',
  29674. ' enumtype: this.TFlag',
  29675. '});',
  29676. 'this.$rtti.$Set("TFlags", {',
  29677. ' comptype: this.$rtti["TFlag"]',
  29678. '});',
  29679. 'this.$rtti.$ProcVar("TProc", {',
  29680. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  29681. '});',
  29682. 'this.p = null;',
  29683. '']),
  29684. LinesToStr([ // $mod.$main
  29685. '$mod.p = $mod.$rtti["TFlag"];',
  29686. '$mod.p = $mod.$rtti["TFlags"];',
  29687. '']));
  29688. end;
  29689. procedure TTestModule.TestRTTI_EnumRange;
  29690. begin
  29691. WithTypeInfo:=true;
  29692. StartProgram(false);
  29693. Add([
  29694. 'type',
  29695. ' TCol = (red,green,blue);',
  29696. ' TColRg = green..blue;',
  29697. ' TSetOfColRg = set of TColRg;',
  29698. 'var p: pointer;',
  29699. 'begin',
  29700. ' p:=typeinfo(tcolrg);',
  29701. ' p:=typeinfo(tsetofcolrg);',
  29702. '']);
  29703. ConvertProgram;
  29704. end;
  29705. procedure TTestModule.TestRTTI_AnonymousEnumType;
  29706. begin
  29707. WithTypeInfo:=true;
  29708. StartProgram(false);
  29709. Add('type');
  29710. Add(' TFlags = set of (red, green);');
  29711. Add('var');
  29712. Add(' f: TFlags;');
  29713. Add('begin');
  29714. Add(' Include(f,red);');
  29715. ConvertProgram;
  29716. CheckSource('TestRTTI_AnonymousEnumType',
  29717. LinesToStr([ // statements
  29718. 'this.TFlags$a = {',
  29719. ' "0": "red",',
  29720. ' red: 0,',
  29721. ' "1": "green",',
  29722. ' green: 1',
  29723. '};',
  29724. 'this.$rtti.$Enum("TFlags$a", {',
  29725. ' minvalue: 0,',
  29726. ' maxvalue: 1,',
  29727. ' ordtype: 1,',
  29728. ' enumtype: this.TFlags$a',
  29729. '});',
  29730. 'this.$rtti.$Set("TFlags", {',
  29731. ' comptype: this.$rtti["TFlags$a"]',
  29732. '});',
  29733. 'this.f = {};',
  29734. '']),
  29735. LinesToStr([
  29736. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  29737. '']));
  29738. end;
  29739. procedure TTestModule.TestRTTI_StaticArray;
  29740. begin
  29741. WithTypeInfo:=true;
  29742. StartProgram(false);
  29743. Add('type');
  29744. Add(' TFlag = (light,dark);');
  29745. Add(' TFlagNames = array[TFlag] of string;');
  29746. Add(' TBoolNames = array[boolean] of string;');
  29747. Add(' TByteArray = array[1..32768] of byte;');
  29748. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  29749. Add('var p: pointer;');
  29750. Add('begin');
  29751. Add(' p:=typeinfo(TFlagNames);');
  29752. Add(' p:=typeinfo(TBoolNames);');
  29753. ConvertProgram;
  29754. CheckSource('TestRTTI_StaticArray',
  29755. LinesToStr([ // statements
  29756. 'this.TFlag = {',
  29757. ' "0": "light",',
  29758. ' light: 0,',
  29759. ' "1": "dark",',
  29760. ' dark: 1',
  29761. '};',
  29762. 'this.$rtti.$Enum("TFlag", {',
  29763. ' minvalue: 0,',
  29764. ' maxvalue: 1,',
  29765. ' ordtype: 1,',
  29766. ' enumtype: this.TFlag',
  29767. '});',
  29768. 'this.$rtti.$StaticArray("TFlagNames", {',
  29769. ' dims: [2],',
  29770. ' eltype: rtl.string',
  29771. '});',
  29772. 'this.$rtti.$StaticArray("TBoolNames", {',
  29773. ' dims: [2],',
  29774. ' eltype: rtl.string',
  29775. '});',
  29776. 'this.$rtti.$StaticArray("TByteArray", {',
  29777. ' dims: [32768],',
  29778. ' eltype: rtl.byte',
  29779. '});',
  29780. 'this.$rtti.$ProcVar("TProc", {',
  29781. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  29782. '});',
  29783. 'this.p = null;',
  29784. '']),
  29785. LinesToStr([ // $mod.$main
  29786. '$mod.p = $mod.$rtti["TFlagNames"];',
  29787. '$mod.p = $mod.$rtti["TBoolNames"];',
  29788. '']));
  29789. end;
  29790. procedure TTestModule.TestRTTI_DynArray;
  29791. begin
  29792. WithTypeInfo:=true;
  29793. StartProgram(false);
  29794. Add('type');
  29795. Add(' TArrStr = array of string;');
  29796. Add(' TArr2Dim = array of tarrstr;');
  29797. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  29798. Add('var p: pointer;');
  29799. Add('begin');
  29800. Add(' p:=typeinfo(tarrstr);');
  29801. Add(' p:=typeinfo(tarr2dim);');
  29802. ConvertProgram;
  29803. CheckSource('TestRTTI_DynArray',
  29804. LinesToStr([ // statements
  29805. 'this.$rtti.$DynArray("TArrStr", {',
  29806. ' eltype: rtl.string',
  29807. '});',
  29808. 'this.$rtti.$DynArray("TArr2Dim", {',
  29809. ' eltype: this.$rtti["TArrStr"]',
  29810. '});',
  29811. 'this.$rtti.$ProcVar("TProc", {',
  29812. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  29813. '});',
  29814. 'this.p = null;',
  29815. '']),
  29816. LinesToStr([ // $mod.$main
  29817. '$mod.p = $mod.$rtti["TArrStr"];',
  29818. '$mod.p = $mod.$rtti["TArr2Dim"];',
  29819. '']));
  29820. end;
  29821. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  29822. begin
  29823. WithTypeInfo:=true;
  29824. StartProgram(false);
  29825. Add('type');
  29826. Add(' TArr = array of array of longint;');
  29827. Add('var a: TArr;');
  29828. Add('begin');
  29829. ConvertProgram;
  29830. CheckSource('TestRTTI_ArrayNestedAnonymous',
  29831. LinesToStr([ // statements
  29832. 'this.$rtti.$DynArray("TArr$a", {',
  29833. ' eltype: rtl.longint',
  29834. '});',
  29835. 'this.$rtti.$DynArray("TArr", {',
  29836. ' eltype: this.$rtti["TArr$a"]',
  29837. '});',
  29838. 'this.a = [];',
  29839. '']),
  29840. LinesToStr([ // $mod.$main
  29841. ]));
  29842. end;
  29843. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  29844. begin
  29845. WithTypeInfo:=true;
  29846. StartProgram(false);
  29847. Add('type');
  29848. Add(' TObject = class');
  29849. Add(' published');
  29850. Add(' procedure Proc; virtual; abstract;');
  29851. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  29852. Add(' end;');
  29853. Add('begin');
  29854. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  29855. nDuplicatePublishedMethodXAtY);
  29856. ConvertProgram;
  29857. end;
  29858. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  29859. begin
  29860. WithTypeInfo:=true;
  29861. StartUnit(false);
  29862. Add([
  29863. 'interface',
  29864. 'type',
  29865. ' TObject = class',
  29866. ' end;',
  29867. ' {$M+}',
  29868. ' TBird = class',
  29869. ' procedure Fly;',
  29870. ' end;',
  29871. ' {$M-}',
  29872. 'type',
  29873. ' TEagle = class(TBird)',
  29874. ' procedure Fly;',
  29875. ' end;',
  29876. 'implementation',
  29877. 'procedure TBird.Fly;',
  29878. 'begin',
  29879. 'end;',
  29880. 'procedure TEagle.Fly;',
  29881. 'begin',
  29882. 'end;',
  29883. '']);
  29884. ConvertUnit;
  29885. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  29886. LinesToStr([ // statements
  29887. 'rtl.createClass(this, "TObject", null, function () {',
  29888. ' this.$init = function () {',
  29889. ' };',
  29890. ' this.$final = function () {',
  29891. ' };',
  29892. '});',
  29893. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29894. ' this.Fly = function () {',
  29895. ' };',
  29896. ' var $r = this.$rtti;',
  29897. ' $r.addMethod("Fly", 0, []);',
  29898. '});',
  29899. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  29900. ' this.Fly = function () {',
  29901. ' };',
  29902. ' var $r = this.$rtti;',
  29903. ' $r.addMethod("Fly", 0, []);',
  29904. '});',
  29905. '']),
  29906. LinesToStr([ // $mod.$main
  29907. ]));
  29908. CheckResolverUnexpectedHints(true);
  29909. end;
  29910. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  29911. begin
  29912. WithTypeInfo:=true;
  29913. StartProgram(false);
  29914. Add('type');
  29915. Add(' TObject = class');
  29916. Add(' published');
  29917. Add(' procedure Proc; external name ''foo'';');
  29918. Add(' end;');
  29919. Add('begin');
  29920. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  29921. nPublishedNameMustMatchExternal);
  29922. ConvertProgram;
  29923. end;
  29924. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  29925. begin
  29926. WithTypeInfo:=true;
  29927. StartProgram(false);
  29928. Add('type');
  29929. Add(' TObject = class');
  29930. Add(' class var FA: longint;');
  29931. Add(' published');
  29932. Add(' class property A: longint read FA;');
  29933. Add(' end;');
  29934. Add('begin');
  29935. SetExpectedPasResolverError('Invalid published property modifier "class"',
  29936. nInvalidXModifierY);
  29937. ConvertProgram;
  29938. end;
  29939. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  29940. begin
  29941. WithTypeInfo:=true;
  29942. StartProgram(false);
  29943. Add('type');
  29944. Add(' TObject = class');
  29945. Add(' published');
  29946. Add(' class var FA: longint;');
  29947. Add(' end;');
  29948. Add('begin');
  29949. SetExpectedPasResolverError(sSymbolCannotBePublished,
  29950. nSymbolCannotBePublished);
  29951. ConvertProgram;
  29952. end;
  29953. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  29954. begin
  29955. WithTypeInfo:=true;
  29956. StartProgram(false);
  29957. Add('{$modeswitch externalclass}');
  29958. Add('type');
  29959. Add(' TObject = class');
  29960. Add(' published');
  29961. Add(' V: longint; external name ''foo'';');
  29962. Add(' end;');
  29963. Add('begin');
  29964. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  29965. nPublishedNameMustMatchExternal);
  29966. ConvertProgram;
  29967. end;
  29968. procedure TTestModule.TestRTTI_Class_Field;
  29969. begin
  29970. WithTypeInfo:=true;
  29971. StartProgram(false);
  29972. Add('{$modeswitch externalclass}');
  29973. Add('type');
  29974. Add(' TObject = class');
  29975. Add(' private');
  29976. Add(' FPropA: string;');
  29977. Add(' published');
  29978. Add(' VarLI: longint;');
  29979. Add(' VarC: char;');
  29980. Add(' VarS: string;');
  29981. Add(' VarD: double;');
  29982. Add(' VarB: boolean;');
  29983. Add(' VarLW: longword;');
  29984. Add(' VarSmI: smallint;');
  29985. Add(' VarW: word;');
  29986. Add(' VarShI: shortint;');
  29987. Add(' VarBy: byte;');
  29988. Add(' VarExt: longint external name ''VarExt'';');
  29989. Add(' ArrA, ArrB: array of byte;');
  29990. Add(' end;');
  29991. Add('var p: pointer;');
  29992. Add(' Obj: tobject;');
  29993. Add('begin');
  29994. Add(' p:=typeinfo(tobject);');
  29995. Add(' p:=typeinfo(p);');
  29996. Add(' p:=typeinfo(obj);');
  29997. ConvertProgram;
  29998. CheckSource('TestRTTI_Class_Field',
  29999. LinesToStr([ // statements
  30000. 'rtl.createClass(this, "TObject", null, function () {',
  30001. ' this.$init = function () {',
  30002. ' this.FPropA = "";',
  30003. ' this.VarLI = 0;',
  30004. ' this.VarC = "";',
  30005. ' this.VarS = "";',
  30006. ' this.VarD = 0.0;',
  30007. ' this.VarB = false;',
  30008. ' this.VarLW = 0;',
  30009. ' this.VarSmI = 0;',
  30010. ' this.VarW = 0;',
  30011. ' this.VarShI = 0;',
  30012. ' this.VarBy = 0;',
  30013. ' this.ArrA = [];',
  30014. ' this.ArrB = [];',
  30015. ' };',
  30016. ' this.$final = function () {',
  30017. ' this.ArrA = undefined;',
  30018. ' this.ArrB = undefined;',
  30019. ' };',
  30020. ' var $r = this.$rtti;',
  30021. ' $r.addField("VarLI", rtl.longint);',
  30022. ' $r.addField("VarC", rtl.char);',
  30023. ' $r.addField("VarS", rtl.string);',
  30024. ' $r.addField("VarD", rtl.double);',
  30025. ' $r.addField("VarB", rtl.boolean);',
  30026. ' $r.addField("VarLW", rtl.longword);',
  30027. ' $r.addField("VarSmI", rtl.smallint);',
  30028. ' $r.addField("VarW", rtl.word);',
  30029. ' $r.addField("VarShI", rtl.shortint);',
  30030. ' $r.addField("VarBy", rtl.byte);',
  30031. ' $r.addField("VarExt", rtl.longint);',
  30032. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30033. ' eltype: rtl.byte',
  30034. ' });',
  30035. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30036. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30037. '});',
  30038. 'this.p = null;',
  30039. 'this.Obj = null;',
  30040. '']),
  30041. LinesToStr([ // $mod.$main
  30042. '$mod.p = $mod.$rtti["TObject"];',
  30043. '$mod.p = rtl.pointer;',
  30044. '$mod.p = $mod.Obj.$rtti;',
  30045. '']));
  30046. end;
  30047. procedure TTestModule.TestRTTI_Class_Method;
  30048. begin
  30049. WithTypeInfo:=true;
  30050. StartProgram(false);
  30051. Add([
  30052. 'type',
  30053. ' TObject = class',
  30054. ' private',
  30055. ' procedure Internal; external name ''$intern'';',
  30056. ' published',
  30057. ' procedure Click; virtual; abstract;',
  30058. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30059. ' function GetNotify: boolean; external name ''GetNotify'';',
  30060. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30061. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30062. ' end;',
  30063. 'begin']);
  30064. ConvertProgram;
  30065. CheckSource('TestRTTI_Class_Method',
  30066. LinesToStr([ // statements
  30067. 'rtl.createClass(this, "TObject", null, function () {',
  30068. ' this.$init = function () {',
  30069. ' };',
  30070. ' this.$final = function () {',
  30071. ' };',
  30072. ' var $r = this.$rtti;',
  30073. ' $r.addMethod("Click", 0, []);',
  30074. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30075. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30076. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30077. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30078. '});',
  30079. '']),
  30080. LinesToStr([ // $mod.$main
  30081. '']));
  30082. end;
  30083. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30084. begin
  30085. WithTypeInfo:=true;
  30086. StartProgram(false);
  30087. Add('type');
  30088. Add(' TObject = class');
  30089. Add(' published');
  30090. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30091. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30092. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30093. Add(' end;');
  30094. Add('begin');
  30095. ConvertProgram;
  30096. CheckSource('TestRTTI_Class_MethodOpenArray',
  30097. LinesToStr([ // statements
  30098. 'rtl.createClass(this, "TObject", null, function () {',
  30099. ' this.$init = function () {',
  30100. ' };',
  30101. ' this.$final = function () {',
  30102. ' };',
  30103. ' var $r = this.$rtti;',
  30104. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30105. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30106. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30107. '});',
  30108. '']),
  30109. LinesToStr([ // $mod.$main
  30110. '']));
  30111. end;
  30112. procedure TTestModule.TestRTTI_Class_Property;
  30113. begin
  30114. WithTypeInfo:=true;
  30115. StartProgram(false);
  30116. Add('{$modeswitch externalclass}');
  30117. Add('type');
  30118. Add(' TObject = class');
  30119. Add(' private');
  30120. Add(' FColor: longint;');
  30121. Add(' FColorStored: boolean;');
  30122. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30123. Add(' function GetColor: longint; virtual; abstract;');
  30124. Add(' function GetColorStored: boolean; virtual; abstract;');
  30125. Add(' FExtSize: longint external name ''$extSize'';');
  30126. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30127. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30128. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30129. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30130. Add(' published');
  30131. Add(' property ColorA: longint read FColor;');
  30132. Add(' property ColorB: longint write FColor;');
  30133. Add(' property ColorC: longint read GetColor write SetColor;');
  30134. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30135. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30136. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30137. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30138. Add(' end;');
  30139. Add('begin');
  30140. ConvertProgram;
  30141. CheckSource('TestRTTI_Class_Property',
  30142. LinesToStr([ // statements
  30143. 'rtl.createClass(this, "TObject", null, function () {',
  30144. ' this.$init = function () {',
  30145. ' this.FColor = 0;',
  30146. ' this.FColorStored = false;',
  30147. ' };',
  30148. ' this.$final = function () {',
  30149. ' };',
  30150. ' var $r = this.$rtti;',
  30151. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30152. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30153. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30154. ' $r.addProperty(',
  30155. ' "ColorD",',
  30156. ' 8,',
  30157. ' rtl.longint,',
  30158. ' "FColor",',
  30159. ' "FColor",',
  30160. ' {',
  30161. ' stored: "FColorStored"',
  30162. ' }',
  30163. ' );',
  30164. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30165. ' $r.addProperty(',
  30166. ' "ExtSizeB",',
  30167. ' 11,',
  30168. ' rtl.longint,',
  30169. ' "$getSize",',
  30170. ' "$setSize",',
  30171. ' {',
  30172. ' stored: "$extSizeStored"',
  30173. ' }',
  30174. ' );',
  30175. ' $r.addProperty(',
  30176. ' "ExtSizeC",',
  30177. ' 12,',
  30178. ' rtl.longint,',
  30179. ' "$extSize",',
  30180. ' "$extSize",',
  30181. ' {',
  30182. ' stored: "$getExtSizeStored"',
  30183. ' }',
  30184. ' );',
  30185. '});',
  30186. '']),
  30187. LinesToStr([ // $mod.$main
  30188. '']));
  30189. end;
  30190. procedure TTestModule.TestRTTI_Class_PropertyParams;
  30191. begin
  30192. WithTypeInfo:=true;
  30193. StartProgram(false);
  30194. Add('{$modeswitch externalclass}');
  30195. Add('type');
  30196. Add(' integer = longint;');
  30197. Add(' TObject = class');
  30198. Add(' private');
  30199. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  30200. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  30201. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  30202. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  30203. Add(' published');
  30204. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  30205. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  30206. Add(' end;');
  30207. Add('begin');
  30208. ConvertProgram;
  30209. CheckSource('TestRTTI_Class_PropertyParams',
  30210. LinesToStr([ // statements
  30211. 'rtl.createClass(this, "TObject", null, function () {',
  30212. ' this.$init = function () {',
  30213. ' };',
  30214. ' this.$final = function () {',
  30215. ' };',
  30216. ' var $r = this.$rtti;',
  30217. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  30218. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  30219. '});',
  30220. '']),
  30221. LinesToStr([ // $mod.$main
  30222. '']));
  30223. end;
  30224. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  30225. begin
  30226. WithTypeInfo:=true;
  30227. AddModuleWithIntfImplSrc('unit1.pas',
  30228. 'type TColor = -5..5;',
  30229. '');
  30230. StartProgram(true);
  30231. Add([
  30232. 'uses unit1;',
  30233. 'type',
  30234. ' TColorAlias = TColor;',
  30235. ' TColorTypeAlias = type TColor;',
  30236. ' TObject = class',
  30237. ' private',
  30238. ' fColor: TColor;',
  30239. ' fAlias: TColorAlias;',
  30240. ' fTypeAlias: TColorTypeAlias;',
  30241. ' published',
  30242. ' property Color: TColor read fcolor;',
  30243. ' property Alias: TColorAlias read falias;',
  30244. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  30245. ' end;',
  30246. 'begin',
  30247. '']);
  30248. ConvertProgram;
  30249. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  30250. LinesToStr([ // statements
  30251. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  30252. 'rtl.createClass(this, "TObject", null, function () {',
  30253. ' this.$init = function () {',
  30254. ' this.fColor = 0;',
  30255. ' this.fAlias = 0;',
  30256. ' this.fTypeAlias = 0;',
  30257. ' };',
  30258. ' this.$final = function () {',
  30259. ' };',
  30260. ' var $r = this.$rtti;',
  30261. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  30262. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  30263. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  30264. '});',
  30265. '']),
  30266. LinesToStr([ // $mod.$main
  30267. '']));
  30268. end;
  30269. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  30270. begin
  30271. WithTypeInfo:=true;
  30272. StartProgram(false);
  30273. Add([
  30274. '{$modeswitch omitrtti}',
  30275. 'type',
  30276. ' TObject = class',
  30277. ' private',
  30278. ' FA: byte;',
  30279. ' published',
  30280. ' property A: byte read FA write FA;',
  30281. ' end;',
  30282. 'begin']);
  30283. ConvertProgram;
  30284. CheckSource('TestRTTI_Class_OmitRTTI',
  30285. LinesToStr([ // statements
  30286. 'rtl.createClass(this, "TObject", null, function () {',
  30287. ' this.$init = function () {',
  30288. ' this.FA = 0;',
  30289. ' };',
  30290. ' this.$final = function () {',
  30291. ' };',
  30292. '});',
  30293. '']),
  30294. LinesToStr([ // $mod.$main
  30295. '']));
  30296. end;
  30297. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  30298. begin
  30299. WithTypeInfo:=true;
  30300. StartUnit(true,[supTObject]);
  30301. Add([
  30302. 'interface',
  30303. 'type',
  30304. ' {$M+}',
  30305. ' TBird = class',
  30306. ' published',
  30307. ' Swarm: array of TBird;',
  30308. ' end;',
  30309. 'implementation',
  30310. '']);
  30311. ConvertUnit;
  30312. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  30313. LinesToStr([ // statements
  30314. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  30315. ' this.$init = function () {',
  30316. ' pas.system.TObject.$init.call(this);',
  30317. ' this.Swarm = [];',
  30318. ' };',
  30319. ' this.$final = function () {',
  30320. ' this.Swarm = undefined;',
  30321. ' pas.system.TObject.$final.call(this);',
  30322. ' };',
  30323. ' var $r = this.$rtti;',
  30324. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  30325. ' eltype: $r',
  30326. ' });',
  30327. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  30328. '});',
  30329. '']),
  30330. LinesToStr([ // $mod.$main
  30331. '']));
  30332. end;
  30333. procedure TTestModule.TestRTTI_IndexModifier;
  30334. begin
  30335. WithTypeInfo:=true;
  30336. StartProgram(false);
  30337. Add([
  30338. 'type',
  30339. ' TEnum = (red, blue);',
  30340. ' TObject = class',
  30341. ' FB: boolean;',
  30342. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  30343. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  30344. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  30345. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  30346. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  30347. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  30348. ' published',
  30349. ' property B1: boolean index 1 read FB write SetIntBool;',
  30350. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  30351. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  30352. ' end;',
  30353. 'begin']);
  30354. ConvertProgram;
  30355. CheckSource('TestRTTI_IndexModifier',
  30356. LinesToStr([ // statements
  30357. 'this.TEnum = {',
  30358. ' "0": "red",',
  30359. ' red: 0,',
  30360. ' "1": "blue",',
  30361. ' blue: 1',
  30362. '};',
  30363. 'this.$rtti.$Enum("TEnum", {',
  30364. ' minvalue: 0,',
  30365. ' maxvalue: 1,',
  30366. ' ordtype: 1,',
  30367. ' enumtype: this.TEnum',
  30368. '});',
  30369. 'rtl.createClass(this, "TObject", null, function () {',
  30370. ' this.$init = function () {',
  30371. ' this.FB = false;',
  30372. ' };',
  30373. ' this.$final = function () {',
  30374. ' };',
  30375. ' var $r = this.$rtti;',
  30376. ' $r.addProperty(',
  30377. ' "B1",',
  30378. ' 18,',
  30379. ' rtl.boolean,',
  30380. ' "FB",',
  30381. ' "SetIntBool",',
  30382. ' {',
  30383. ' index: 1',
  30384. ' }',
  30385. ' );',
  30386. ' $r.addProperty(',
  30387. ' "B2",',
  30388. ' 17,',
  30389. ' rtl.boolean,',
  30390. ' "GetEnumBool",',
  30391. ' "FB",',
  30392. ' {',
  30393. ' index: $mod.TEnum.blue',
  30394. ' }',
  30395. ' );',
  30396. ' $r.addProperty(',
  30397. ' "I1",',
  30398. ' 19,',
  30399. ' rtl.boolean,',
  30400. ' "GetStrIntBool",',
  30401. ' "SetStrIntBool",',
  30402. ' {',
  30403. ' index: 2',
  30404. ' }',
  30405. ' );',
  30406. '});',
  30407. '']),
  30408. LinesToStr([ // $mod.$main
  30409. '']));
  30410. end;
  30411. procedure TTestModule.TestRTTI_StoredModifier;
  30412. begin
  30413. WithTypeInfo:=true;
  30414. StartProgram(false);
  30415. Add([
  30416. 'const',
  30417. ' ConstB = true;',
  30418. 'type',
  30419. ' TObject = class',
  30420. ' private',
  30421. ' FB: boolean;',
  30422. ' function IsBStored: boolean; virtual; abstract;',
  30423. ' published',
  30424. ' property BoolA: boolean read FB stored true;',
  30425. ' property BoolB: boolean read FB stored false;',
  30426. ' property BoolC: boolean read FB stored FB;',
  30427. ' property BoolD: boolean read FB stored ConstB;',
  30428. ' property BoolE: boolean read FB stored IsBStored;',
  30429. ' end;',
  30430. 'begin']);
  30431. ConvertProgram;
  30432. CheckSource('TestRTTI_StoredModifier',
  30433. LinesToStr([ // statements
  30434. 'this.ConstB = true;',
  30435. 'rtl.createClass(this, "TObject", null, function () {',
  30436. ' this.$init = function () {',
  30437. ' this.FB = false;',
  30438. ' };',
  30439. ' this.$final = function () {',
  30440. ' };',
  30441. ' var $r = this.$rtti;',
  30442. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  30443. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  30444. ' $r.addProperty(',
  30445. ' "BoolC",',
  30446. ' 8,',
  30447. ' rtl.boolean,',
  30448. ' "FB",',
  30449. ' "",',
  30450. ' {',
  30451. ' stored: "FB"',
  30452. ' }',
  30453. ' );',
  30454. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  30455. ' $r.addProperty(',
  30456. ' "BoolE",',
  30457. ' 12,',
  30458. ' rtl.boolean,',
  30459. ' "FB",',
  30460. ' "",',
  30461. ' {',
  30462. ' stored: "IsBStored"',
  30463. ' }',
  30464. ' );',
  30465. '});',
  30466. '']),
  30467. LinesToStr([ // $mod.$main
  30468. '']));
  30469. end;
  30470. procedure TTestModule.TestRTTI_DefaultValue;
  30471. begin
  30472. WithTypeInfo:=true;
  30473. StartProgram(false);
  30474. Add([
  30475. 'type',
  30476. ' TEnum = (red, blue);',
  30477. 'const',
  30478. ' CB = true or false;',
  30479. ' CI = 1+2;',
  30480. 'type',
  30481. ' TObject = class',
  30482. ' FB: boolean;',
  30483. ' FI: longint;',
  30484. ' FE: TEnum;',
  30485. ' published',
  30486. ' property B1: boolean read FB default true;',
  30487. ' property B2: boolean read FB default CB;',
  30488. ' property B3: boolean read FB default test1.cb;',
  30489. ' property I1: longint read FI default 2;',
  30490. ' property I2: longint read FI default CI;',
  30491. ' property E1: TEnum read FE default red;',
  30492. ' property E2: TEnum read FE default TEnum.blue;',
  30493. ' end;',
  30494. 'begin']);
  30495. ConvertProgram;
  30496. CheckSource('TestRTTI_DefaultValue',
  30497. LinesToStr([ // statements
  30498. 'this.TEnum = {',
  30499. ' "0": "red",',
  30500. ' red: 0,',
  30501. ' "1": "blue",',
  30502. ' blue: 1',
  30503. '};',
  30504. 'this.$rtti.$Enum("TEnum", {',
  30505. ' minvalue: 0,',
  30506. ' maxvalue: 1,',
  30507. ' ordtype: 1,',
  30508. ' enumtype: this.TEnum',
  30509. '});',
  30510. 'this.CB = true || false;',
  30511. 'this.CI = 1 + 2;',
  30512. 'rtl.createClass(this, "TObject", null, function () {',
  30513. ' this.$init = function () {',
  30514. ' this.FB = false;',
  30515. ' this.FI = 0;',
  30516. ' this.FE = 0;',
  30517. ' };',
  30518. ' this.$final = function () {',
  30519. ' };',
  30520. ' var $r = this.$rtti;',
  30521. ' $r.addProperty(',
  30522. ' "B1",',
  30523. ' 0,',
  30524. ' rtl.boolean,',
  30525. ' "FB",',
  30526. ' "",',
  30527. ' {',
  30528. ' Default: true',
  30529. ' }',
  30530. ' );',
  30531. ' $r.addProperty(',
  30532. ' "B2",',
  30533. ' 0,',
  30534. ' rtl.boolean,',
  30535. ' "FB",',
  30536. ' "",',
  30537. ' {',
  30538. ' Default: true',
  30539. ' }',
  30540. ' );',
  30541. ' $r.addProperty(',
  30542. ' "B3",',
  30543. ' 0,',
  30544. ' rtl.boolean,',
  30545. ' "FB",',
  30546. ' "",',
  30547. ' {',
  30548. ' Default: true',
  30549. ' }',
  30550. ' );',
  30551. ' $r.addProperty(',
  30552. ' "I1",',
  30553. ' 0,',
  30554. ' rtl.longint,',
  30555. ' "FI",',
  30556. ' "",',
  30557. ' {',
  30558. ' Default: 2',
  30559. ' }',
  30560. ' );',
  30561. ' $r.addProperty(',
  30562. ' "I2",',
  30563. ' 0,',
  30564. ' rtl.longint,',
  30565. ' "FI",',
  30566. ' "",',
  30567. ' {',
  30568. ' Default: 3',
  30569. ' }',
  30570. ' );',
  30571. ' $r.addProperty(',
  30572. ' "E1",',
  30573. ' 0,',
  30574. ' $mod.$rtti["TEnum"],',
  30575. ' "FE",',
  30576. ' "",',
  30577. ' {',
  30578. ' Default: $mod.TEnum.red',
  30579. ' }',
  30580. ' );',
  30581. ' $r.addProperty(',
  30582. ' "E2",',
  30583. ' 0,',
  30584. ' $mod.$rtti["TEnum"],',
  30585. ' "FE",',
  30586. ' "",',
  30587. ' {',
  30588. ' Default: $mod.TEnum.blue',
  30589. ' }',
  30590. ' );',
  30591. '});',
  30592. '']),
  30593. LinesToStr([ // $mod.$main
  30594. '']));
  30595. end;
  30596. procedure TTestModule.TestRTTI_DefaultValueSet;
  30597. begin
  30598. WithTypeInfo:=true;
  30599. StartProgram(false);
  30600. Add([
  30601. 'type',
  30602. ' TEnum = (red, blue);',
  30603. ' TSet = set of TEnum;',
  30604. 'const',
  30605. ' CSet = [red,blue];',
  30606. 'type',
  30607. ' TObject = class',
  30608. ' FSet: TSet;',
  30609. ' published',
  30610. ' property Set1: TSet read FSet default [];',
  30611. ' property Set2: TSet read FSet default [red];',
  30612. ' property Set3: TSet read FSet default [red,blue];',
  30613. ' property Set4: TSet read FSet default CSet;',
  30614. ' end;',
  30615. 'begin']);
  30616. ConvertProgram;
  30617. CheckSource('TestRTTI_DefaultValueSet',
  30618. LinesToStr([ // statements
  30619. 'this.TEnum = {',
  30620. ' "0": "red",',
  30621. ' red: 0,',
  30622. ' "1": "blue",',
  30623. ' blue: 1',
  30624. '};',
  30625. 'this.$rtti.$Enum("TEnum", {',
  30626. ' minvalue: 0,',
  30627. ' maxvalue: 1,',
  30628. ' ordtype: 1,',
  30629. ' enumtype: this.TEnum',
  30630. '});',
  30631. 'this.$rtti.$Set("TSet", {',
  30632. ' comptype: this.$rtti["TEnum"]',
  30633. '});',
  30634. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  30635. 'rtl.createClass(this, "TObject", null, function () {',
  30636. ' this.$init = function () {',
  30637. ' this.FSet = {};',
  30638. ' };',
  30639. ' this.$final = function () {',
  30640. ' this.FSet = undefined;',
  30641. ' };',
  30642. ' var $r = this.$rtti;',
  30643. ' $r.addProperty(',
  30644. ' "Set1",',
  30645. ' 0,',
  30646. ' $mod.$rtti["TSet"],',
  30647. ' "FSet",',
  30648. ' "",',
  30649. ' {',
  30650. ' Default: {}',
  30651. ' }',
  30652. ' );',
  30653. ' $r.addProperty(',
  30654. ' "Set2",',
  30655. ' 0,',
  30656. ' $mod.$rtti["TSet"],',
  30657. ' "FSet",',
  30658. ' "",',
  30659. ' {',
  30660. ' Default: rtl.createSet($mod.TEnum.red)',
  30661. ' }',
  30662. ' );',
  30663. ' $r.addProperty(',
  30664. ' "Set3",',
  30665. ' 0,',
  30666. ' $mod.$rtti["TSet"],',
  30667. ' "FSet",',
  30668. ' "",',
  30669. ' {',
  30670. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  30671. ' }',
  30672. ' );',
  30673. ' $r.addProperty(',
  30674. ' "Set4",',
  30675. ' 0,',
  30676. ' $mod.$rtti["TSet"],',
  30677. ' "FSet",',
  30678. ' "",',
  30679. ' {',
  30680. ' Default: $mod.CSet',
  30681. ' }',
  30682. ' );',
  30683. '});',
  30684. '']),
  30685. LinesToStr([ // $mod.$main
  30686. '']));
  30687. end;
  30688. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  30689. begin
  30690. WithTypeInfo:=true;
  30691. StartProgram(false);
  30692. Add([
  30693. 'type',
  30694. ' TRg = -1..1;',
  30695. 'const',
  30696. ' l = low(TRg);',
  30697. ' h = high(TRg);',
  30698. 'type',
  30699. ' TObject = class',
  30700. ' FV: TRg;',
  30701. ' published',
  30702. ' property V1: TRg read FV default -1;',
  30703. ' end;',
  30704. 'begin']);
  30705. ConvertProgram;
  30706. CheckSource('TestRTTI_DefaultValueRangeType',
  30707. LinesToStr([ // statements
  30708. 'this.$rtti.$Int("TRg", {',
  30709. ' minvalue: -1,',
  30710. ' maxvalue: 1,',
  30711. ' ordtype: 0',
  30712. '});',
  30713. 'this.l = -1;',
  30714. 'this.h = 1;',
  30715. 'rtl.createClass(this, "TObject", null, function () {',
  30716. ' this.$init = function () {',
  30717. ' this.FV = 0;',
  30718. ' };',
  30719. ' this.$final = function () {',
  30720. ' };',
  30721. ' var $r = this.$rtti;',
  30722. ' $r.addProperty(',
  30723. ' "V1",',
  30724. ' 0,',
  30725. ' $mod.$rtti["TRg"],',
  30726. ' "FV",',
  30727. ' "",',
  30728. ' {',
  30729. ' Default: -1',
  30730. ' }',
  30731. ' );',
  30732. '});',
  30733. '']),
  30734. LinesToStr([ // $mod.$main
  30735. '']));
  30736. end;
  30737. procedure TTestModule.TestRTTI_DefaultValueInherit;
  30738. begin
  30739. WithTypeInfo:=true;
  30740. StartProgram(false);
  30741. Add([
  30742. 'type',
  30743. ' TObject = class',
  30744. ' FA, FB: byte;',
  30745. ' property A: byte read FA default 1;',
  30746. ' property B: byte read FB default 2;',
  30747. ' end;',
  30748. ' TBird = class',
  30749. ' published',
  30750. ' property A;',
  30751. ' property B nodefault;',
  30752. ' end;',
  30753. 'begin']);
  30754. ConvertProgram;
  30755. CheckSource('TestRTTI_DefaultValueInherit',
  30756. LinesToStr([ // statements
  30757. 'rtl.createClass(this, "TObject", null, function () {',
  30758. ' this.$init = function () {',
  30759. ' this.FA = 0;',
  30760. ' this.FB = 0;',
  30761. ' };',
  30762. ' this.$final = function () {',
  30763. ' };',
  30764. '});',
  30765. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30766. ' var $r = this.$rtti;',
  30767. ' $r.addProperty(',
  30768. ' "A",',
  30769. ' 0,',
  30770. ' rtl.byte,',
  30771. ' "FA",',
  30772. ' "",',
  30773. ' {',
  30774. ' Default: 1',
  30775. ' }',
  30776. ' );',
  30777. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  30778. '});',
  30779. '']),
  30780. LinesToStr([ // $mod.$main
  30781. '']));
  30782. end;
  30783. procedure TTestModule.TestRTTI_OverrideMethod;
  30784. begin
  30785. WithTypeInfo:=true;
  30786. StartProgram(false);
  30787. Add('type');
  30788. Add(' TObject = class');
  30789. Add(' published');
  30790. Add(' procedure DoIt; virtual; abstract;');
  30791. Add(' end;');
  30792. Add(' TSky = class');
  30793. Add(' published');
  30794. Add(' procedure DoIt; override;');
  30795. Add(' end;');
  30796. Add('procedure TSky.DoIt; begin end;');
  30797. Add('begin');
  30798. ConvertProgram;
  30799. CheckSource('TestRTTI_OverrideMethod',
  30800. LinesToStr([ // statements
  30801. 'rtl.createClass(this, "TObject", null, function () {',
  30802. ' this.$init = function () {',
  30803. ' };',
  30804. ' this.$final = function () {',
  30805. ' };',
  30806. ' var $r = this.$rtti;',
  30807. ' $r.addMethod("DoIt", 0, []);',
  30808. '});',
  30809. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30810. ' this.DoIt = function () {',
  30811. ' };',
  30812. '});',
  30813. '']),
  30814. LinesToStr([ // $mod.$main
  30815. '']));
  30816. end;
  30817. procedure TTestModule.TestRTTI_ReintroduceMethod;
  30818. begin
  30819. WithTypeInfo:=true;
  30820. StartProgram(false);
  30821. Add([
  30822. 'type',
  30823. ' TObject = class',
  30824. ' published',
  30825. ' procedure DoIt;',
  30826. ' end;',
  30827. ' TSky = class',
  30828. ' published',
  30829. ' procedure DoIt; reintroduce;',
  30830. ' end;',
  30831. 'procedure TObject.DoIt; begin end;',
  30832. 'procedure TSky.DoIt;',
  30833. 'begin',
  30834. ' inherited DoIt;',
  30835. 'end;',
  30836. 'begin']);
  30837. ConvertProgram;
  30838. CheckSource('TestRTTI_ReintroduceMethod',
  30839. LinesToStr([ // statements
  30840. 'rtl.createClass(this, "TObject", null, function () {',
  30841. ' this.$init = function () {',
  30842. ' };',
  30843. ' this.$final = function () {',
  30844. ' };',
  30845. ' this.DoIt = function () {',
  30846. ' };',
  30847. ' var $r = this.$rtti;',
  30848. ' $r.addMethod("DoIt", 0, []);',
  30849. '});',
  30850. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30851. ' this.DoIt = function () {',
  30852. ' $mod.TObject.DoIt.call(this);',
  30853. ' };',
  30854. ' var $r = this.$rtti;',
  30855. ' $r.addMethod("DoIt", 0, []);',
  30856. '});',
  30857. '']),
  30858. LinesToStr([ // $mod.$main
  30859. '']));
  30860. end;
  30861. procedure TTestModule.TestRTTI_OverloadProperty;
  30862. begin
  30863. WithTypeInfo:=true;
  30864. StartProgram(false);
  30865. Add('type');
  30866. Add(' TObject = class');
  30867. Add(' protected');
  30868. Add(' FFlag: longint;');
  30869. Add(' published');
  30870. Add(' property Flag: longint read fflag;');
  30871. Add(' end;');
  30872. Add(' TSky = class');
  30873. Add(' published');
  30874. Add(' property FLAG: longint write fflag;');
  30875. Add(' end;');
  30876. Add('begin');
  30877. ConvertProgram;
  30878. CheckSource('TestRTTI_OverrideMethod',
  30879. LinesToStr([ // statements
  30880. 'rtl.createClass(this, "TObject", null, function () {',
  30881. ' this.$init = function () {',
  30882. ' this.FFlag = 0;',
  30883. ' };',
  30884. ' this.$final = function () {',
  30885. ' };',
  30886. ' var $r = this.$rtti;',
  30887. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  30888. '});',
  30889. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30890. ' var $r = this.$rtti;',
  30891. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  30892. '});',
  30893. '']),
  30894. LinesToStr([ // $mod.$main
  30895. '']));
  30896. end;
  30897. procedure TTestModule.TestRTTI_ClassForward;
  30898. begin
  30899. WithTypeInfo:=true;
  30900. StartProgram(false);
  30901. Add('type');
  30902. Add(' TObject = class end;');
  30903. Add(' tbridge = class;');
  30904. Add(' TProc = function: tbridge;');
  30905. Add(' TOger = class');
  30906. Add(' published');
  30907. Add(' FBridge: tbridge;');
  30908. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  30909. Add(' property Bridge: tbridge read fbridge write setbridge;');
  30910. Add(' end;');
  30911. Add(' TBridge = class');
  30912. Add(' FOger: toger;');
  30913. Add(' end;');
  30914. Add('var p: Pointer;');
  30915. Add(' b: tbridge;');
  30916. Add('begin');
  30917. Add(' p:=typeinfo(tbridge);');
  30918. Add(' p:=typeinfo(b);');
  30919. ConvertProgram;
  30920. CheckSource('TestRTTI_ClassForward',
  30921. LinesToStr([ // statements
  30922. 'rtl.createClass(this, "TObject", null, function () {',
  30923. ' this.$init = function () {',
  30924. ' };',
  30925. ' this.$final = function () {',
  30926. ' };',
  30927. '});',
  30928. 'this.$rtti.$Class("TBridge");',
  30929. 'this.$rtti.$ProcVar("TProc", {',
  30930. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  30931. '});',
  30932. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  30933. ' this.$init = function () {',
  30934. ' $mod.TObject.$init.call(this);',
  30935. ' this.FBridge = null;',
  30936. ' };',
  30937. ' this.$final = function () {',
  30938. ' this.FBridge = undefined;',
  30939. ' $mod.TObject.$final.call(this);',
  30940. ' };',
  30941. ' var $r = this.$rtti;',
  30942. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  30943. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  30944. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  30945. '});',
  30946. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  30947. ' this.$init = function () {',
  30948. ' $mod.TObject.$init.call(this);',
  30949. ' this.FOger = null;',
  30950. ' };',
  30951. ' this.$final = function () {',
  30952. ' this.FOger = undefined;',
  30953. ' $mod.TObject.$final.call(this);',
  30954. ' };',
  30955. '});',
  30956. 'this.p = null;',
  30957. 'this.b = null;',
  30958. '']),
  30959. LinesToStr([ // $mod.$main
  30960. '$mod.p = $mod.$rtti["TBridge"];',
  30961. '$mod.p = $mod.b.$rtti;',
  30962. '']));
  30963. end;
  30964. procedure TTestModule.TestRTTI_ClassOf;
  30965. begin
  30966. WithTypeInfo:=true;
  30967. StartProgram(false);
  30968. Add('type');
  30969. Add(' TClass = class of tobject;');
  30970. Add(' TProcA = function: TClass;');
  30971. Add(' TObject = class');
  30972. Add(' published');
  30973. Add(' C: tclass;');
  30974. Add(' end;');
  30975. Add(' tfox = class;');
  30976. Add(' TBird = class end;');
  30977. Add(' TBirds = class of tbird;');
  30978. Add(' TFox = class end;');
  30979. Add(' TFoxes = class of tfox;');
  30980. Add(' TCows = class of TCow;');
  30981. Add(' TCow = class;');
  30982. Add(' TCow = class end;');
  30983. Add('begin');
  30984. ConvertProgram;
  30985. CheckSource('TestRTTI_ClassOf',
  30986. LinesToStr([ // statements
  30987. 'this.$rtti.$Class("TObject");',
  30988. 'this.$rtti.$ClassRef("TClass", {',
  30989. ' instancetype: this.$rtti["TObject"]',
  30990. '});',
  30991. 'this.$rtti.$ProcVar("TProcA", {',
  30992. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  30993. '});',
  30994. 'rtl.createClass(this, "TObject", null, function () {',
  30995. ' this.$init = function () {',
  30996. ' this.C = null;',
  30997. ' };',
  30998. ' this.$final = function () {',
  30999. ' this.C = undefined;',
  31000. ' };',
  31001. ' var $r = this.$rtti;',
  31002. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31003. '});',
  31004. 'this.$rtti.$Class("TFox");',
  31005. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31006. '});',
  31007. 'this.$rtti.$ClassRef("TBirds", {',
  31008. ' instancetype: this.$rtti["TBird"]',
  31009. '});',
  31010. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31011. '});',
  31012. 'this.$rtti.$ClassRef("TFoxes", {',
  31013. ' instancetype: this.$rtti["TFox"]',
  31014. '});',
  31015. 'this.$rtti.$Class("TCow");',
  31016. 'this.$rtti.$ClassRef("TCows", {',
  31017. ' instancetype: this.$rtti["TCow"]',
  31018. '});',
  31019. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31020. '});',
  31021. '']),
  31022. LinesToStr([ // $mod.$main
  31023. '']));
  31024. end;
  31025. procedure TTestModule.TestRTTI_Record;
  31026. begin
  31027. WithTypeInfo:=true;
  31028. StartProgram(false);
  31029. Add('type');
  31030. Add(' integer = longint;');
  31031. Add(' TPoint = record');
  31032. Add(' x,y: integer;');
  31033. Add(' end;');
  31034. Add('var p: pointer;');
  31035. Add(' r: tpoint;');
  31036. Add('begin');
  31037. Add(' p:=typeinfo(tpoint);');
  31038. Add(' p:=typeinfo(r);');
  31039. Add(' p:=typeinfo(r.x);');
  31040. ConvertProgram;
  31041. CheckSource('TestRTTI_Record',
  31042. LinesToStr([ // statements
  31043. 'rtl.recNewT(this, "TPoint", function () {',
  31044. ' this.x = 0;',
  31045. ' this.y = 0;',
  31046. ' this.$eq = function (b) {',
  31047. ' return (this.x === b.x) && (this.y === b.y);',
  31048. ' };',
  31049. ' this.$assign = function (s) {',
  31050. ' this.x = s.x;',
  31051. ' this.y = s.y;',
  31052. ' return this;',
  31053. ' };',
  31054. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31055. ' $r.addField("x", rtl.longint);',
  31056. ' $r.addField("y", rtl.longint);',
  31057. '});',
  31058. 'this.p = null;',
  31059. 'this.r = this.TPoint.$new();',
  31060. '']),
  31061. LinesToStr([ // $mod.$main
  31062. '$mod.p = $mod.$rtti["TPoint"];',
  31063. '$mod.p = $mod.$rtti["TPoint"];',
  31064. '$mod.p = rtl.longint;',
  31065. '']));
  31066. end;
  31067. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31068. begin
  31069. WithTypeInfo:=true;
  31070. StartProgram(false);
  31071. Add('type');
  31072. Add(' TFloatRec = record');
  31073. Add(' c,d: array of char;');
  31074. // Add(' i: array of array of longint;');
  31075. Add(' end;');
  31076. Add('var p: pointer;');
  31077. Add(' r: tfloatrec;');
  31078. Add('begin');
  31079. Add(' p:=typeinfo(tfloatrec);');
  31080. Add(' p:=typeinfo(r);');
  31081. Add(' p:=typeinfo(r.d);');
  31082. ConvertProgram;
  31083. CheckSource('TestRTTI_Record',
  31084. LinesToStr([ // statements
  31085. 'rtl.recNewT(this, "TFloatRec", function () {',
  31086. ' this.$new = function () {',
  31087. ' var r = Object.create(this);',
  31088. ' r.c = [];',
  31089. ' r.d = [];',
  31090. ' return r;',
  31091. ' };',
  31092. ' this.$eq = function (b) {',
  31093. ' return (this.c === b.c) && (this.d === b.d);',
  31094. ' };',
  31095. ' this.$assign = function (s) {',
  31096. ' this.c = rtl.arrayRef(s.c);',
  31097. ' this.d = rtl.arrayRef(s.d);',
  31098. ' return this;',
  31099. ' };',
  31100. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31101. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31102. ' eltype: rtl.char',
  31103. ' });',
  31104. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31105. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31106. '});',
  31107. 'this.p = null;',
  31108. 'this.r = this.TFloatRec.$new();',
  31109. '']),
  31110. LinesToStr([ // $mod.$main
  31111. '$mod.p = $mod.$rtti["TFloatRec"];',
  31112. '$mod.p = $mod.$rtti["TFloatRec"];',
  31113. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31114. '']));
  31115. end;
  31116. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31117. begin
  31118. WithTypeInfo:=true;
  31119. StartProgram(false);
  31120. Add([
  31121. '{$modeswitch AdvancedRecords}',
  31122. 'type',
  31123. ' TPoint = record',
  31124. ' type TProc = procedure(w: word);',
  31125. ' class var p: TProc;',
  31126. ' end;',
  31127. 'begin',
  31128. '']);
  31129. ConvertProgram;
  31130. CheckSource('TestRTTI_Record_ClassVarType',
  31131. LinesToStr([ // statements
  31132. 'rtl.recNewT(this, "TPoint", function () {',
  31133. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31134. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31135. ' });',
  31136. ' this.p = null;',
  31137. ' this.$eq = function (b) {',
  31138. ' return true;',
  31139. ' };',
  31140. ' this.$assign = function (s) {',
  31141. ' return this;',
  31142. ' };',
  31143. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31144. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31145. '}, true);',
  31146. '']),
  31147. LinesToStr([ // $mod.$main
  31148. '']));
  31149. end;
  31150. procedure TTestModule.TestRTTI_LocalTypes;
  31151. begin
  31152. WithTypeInfo:=true;
  31153. StartProgram(false);
  31154. Add([
  31155. 'procedure DoIt;',
  31156. 'type',
  31157. ' integer = longint;',
  31158. ' TPoint = record',
  31159. ' x,y: integer;',
  31160. ' end;',
  31161. 'var p: TPoint;',
  31162. 'begin',
  31163. 'end;',
  31164. 'begin']);
  31165. ConvertProgram;
  31166. CheckSource('TestRTTI_LocalTypes',
  31167. LinesToStr([ // statements
  31168. 'var TPoint = rtl.recNewT(null, "", function () {',
  31169. ' this.x = 0;',
  31170. ' this.y = 0;',
  31171. ' this.$eq = function (b) {',
  31172. ' return (this.x === b.x) && (this.y === b.y);',
  31173. ' };',
  31174. ' this.$assign = function (s) {',
  31175. ' this.x = s.x;',
  31176. ' this.y = s.y;',
  31177. ' return this;',
  31178. ' };',
  31179. '});',
  31180. 'this.DoIt = function () {',
  31181. ' var p = TPoint.$new();',
  31182. '};',
  31183. '']),
  31184. LinesToStr([ // $mod.$main
  31185. '']));
  31186. end;
  31187. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  31188. begin
  31189. WithTypeInfo:=true;
  31190. StartProgram(false);
  31191. Add([
  31192. 'type',
  31193. ' TCaption = string;',
  31194. ' TYesNo = boolean;',
  31195. ' TLetter = char;',
  31196. ' TFloat = double;',
  31197. ' TPtr = pointer;',
  31198. ' TShortInt = shortint;',
  31199. ' TByte = byte;',
  31200. ' TSmallInt = smallint;',
  31201. ' TWord = word;',
  31202. ' TInt32 = longint;',
  31203. ' TDWord = longword;',
  31204. ' TValue = jsvalue;',
  31205. 'var p: TPtr;',
  31206. 'begin',
  31207. ' p:=typeinfo(string);',
  31208. ' p:=typeinfo(tcaption);',
  31209. ' p:=typeinfo(boolean);',
  31210. ' p:=typeinfo(tyesno);',
  31211. ' p:=typeinfo(char);',
  31212. ' p:=typeinfo(tletter);',
  31213. ' p:=typeinfo(double);',
  31214. ' p:=typeinfo(tfloat);',
  31215. ' p:=typeinfo(pointer);',
  31216. ' p:=typeinfo(tptr);',
  31217. ' p:=typeinfo(shortint);',
  31218. ' p:=typeinfo(tshortint);',
  31219. ' p:=typeinfo(byte);',
  31220. ' p:=typeinfo(tbyte);',
  31221. ' p:=typeinfo(smallint);',
  31222. ' p:=typeinfo(tsmallint);',
  31223. ' p:=typeinfo(word);',
  31224. ' p:=typeinfo(tword);',
  31225. ' p:=typeinfo(longword);',
  31226. ' p:=typeinfo(tdword);',
  31227. ' p:=typeinfo(jsvalue);',
  31228. ' p:=typeinfo(tvalue);',
  31229. '']);
  31230. ConvertProgram;
  31231. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  31232. LinesToStr([ // statements
  31233. 'this.p = null;',
  31234. '']),
  31235. LinesToStr([ // $mod.$main
  31236. '$mod.p = rtl.string;',
  31237. '$mod.p = rtl.string;',
  31238. '$mod.p = rtl.boolean;',
  31239. '$mod.p = rtl.boolean;',
  31240. '$mod.p = rtl.char;',
  31241. '$mod.p = rtl.char;',
  31242. '$mod.p = rtl.double;',
  31243. '$mod.p = rtl.double;',
  31244. '$mod.p = rtl.pointer;',
  31245. '$mod.p = rtl.pointer;',
  31246. '$mod.p = rtl.shortint;',
  31247. '$mod.p = rtl.shortint;',
  31248. '$mod.p = rtl.byte;',
  31249. '$mod.p = rtl.byte;',
  31250. '$mod.p = rtl.smallint;',
  31251. '$mod.p = rtl.smallint;',
  31252. '$mod.p = rtl.word;',
  31253. '$mod.p = rtl.word;',
  31254. '$mod.p = rtl.longword;',
  31255. '$mod.p = rtl.longword;',
  31256. '$mod.p = rtl.jsvalue;',
  31257. '$mod.p = rtl.jsvalue;',
  31258. '']));
  31259. end;
  31260. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  31261. begin
  31262. WithTypeInfo:=true;
  31263. StartProgram(false);
  31264. Add([
  31265. 'type',
  31266. ' TCaption = type string;',
  31267. ' TYesNo = type boolean;',
  31268. ' TLetter = type char;',
  31269. ' TFloat = type double;',
  31270. ' TPtr = type pointer;',
  31271. ' TShortInt = type shortint;',
  31272. ' TByte = type byte;',
  31273. ' TSmallInt = type smallint;',
  31274. ' TWord = type word;',
  31275. ' TInt32 = type longint;',
  31276. ' TDWord = type longword;',
  31277. ' TValue = type jsvalue;',
  31278. ' TAliasValue = type TValue;',
  31279. 'var',
  31280. ' p: TPtr;',
  31281. ' a: TAliasValue;',
  31282. 'begin',
  31283. ' p:=typeinfo(tcaption);',
  31284. ' p:=typeinfo(tyesno);',
  31285. ' p:=typeinfo(tletter);',
  31286. ' p:=typeinfo(tfloat);',
  31287. ' p:=typeinfo(tptr);',
  31288. ' p:=typeinfo(tshortint);',
  31289. ' p:=typeinfo(tbyte);',
  31290. ' p:=typeinfo(tsmallint);',
  31291. ' p:=typeinfo(tword);',
  31292. ' p:=typeinfo(tdword);',
  31293. ' p:=typeinfo(tvalue);',
  31294. ' p:=typeinfo(taliasvalue);',
  31295. ' p:=typeinfo(a);',
  31296. '']);
  31297. ConvertProgram;
  31298. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  31299. LinesToStr([ // statements
  31300. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  31301. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  31302. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  31303. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  31304. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  31305. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  31306. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  31307. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  31308. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  31309. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  31310. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  31311. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  31312. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  31313. 'this.p = null;',
  31314. 'this.a = undefined;',
  31315. '']),
  31316. LinesToStr([ // $mod.$main
  31317. '$mod.p = $mod.$rtti["TCaption"];',
  31318. '$mod.p = $mod.$rtti["TYesNo"];',
  31319. '$mod.p = $mod.$rtti["TLetter"];',
  31320. '$mod.p = $mod.$rtti["TFloat"];',
  31321. '$mod.p = $mod.$rtti["TPtr"];',
  31322. '$mod.p = $mod.$rtti["TShortInt"];',
  31323. '$mod.p = $mod.$rtti["TByte"];',
  31324. '$mod.p = $mod.$rtti["TSmallInt"];',
  31325. '$mod.p = $mod.$rtti["TWord"];',
  31326. '$mod.p = $mod.$rtti["TDWord"];',
  31327. '$mod.p = $mod.$rtti["TValue"];',
  31328. '$mod.p = $mod.$rtti["TAliasValue"];',
  31329. '$mod.p = $mod.$rtti["TAliasValue"];',
  31330. '']));
  31331. end;
  31332. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  31333. begin
  31334. WithTypeInfo:=true;
  31335. StartProgram(false);
  31336. Add('procedure DoIt;');
  31337. Add('type');
  31338. Add(' integer = longint;');
  31339. Add(' TPoint = record');
  31340. Add(' x,y: integer;');
  31341. Add(' end;');
  31342. Add('var p: pointer;');
  31343. Add('begin');
  31344. Add(' p:=typeinfo(tpoint);');
  31345. Add('end;');
  31346. Add('begin');
  31347. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  31348. ConvertProgram;
  31349. end;
  31350. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  31351. begin
  31352. WithTypeInfo:=true;
  31353. StartProgram(true,[supTypeInfo]);
  31354. Add([
  31355. '{$modeswitch externalclass}',
  31356. 'type',
  31357. ' TFlag = (up,down);',
  31358. ' TFlags = set of TFlag;',
  31359. 'var',
  31360. ' ti: TTypeInfo;',
  31361. ' tiInt: TTypeInfoInteger;',
  31362. ' tiEnum: TTypeInfoEnum;',
  31363. ' tiSet: TTypeInfoSet;',
  31364. 'begin',
  31365. ' ti:=typeinfo(string);',
  31366. ' ti:=typeinfo(boolean);',
  31367. ' ti:=typeinfo(char);',
  31368. ' ti:=typeinfo(double);',
  31369. ' tiInt:=typeinfo(shortint);',
  31370. ' tiInt:=typeinfo(byte);',
  31371. ' tiInt:=typeinfo(smallint);',
  31372. ' tiInt:=typeinfo(word);',
  31373. ' tiInt:=typeinfo(longint);',
  31374. ' tiInt:=typeinfo(longword);',
  31375. ' ti:=typeinfo(jsvalue);',
  31376. ' tiEnum:=typeinfo(tflag);',
  31377. ' tiSet:=typeinfo(tflags);']);
  31378. ConvertProgram;
  31379. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  31380. LinesToStr([ // statements
  31381. 'this.TFlag = {',
  31382. ' "0": "up",',
  31383. ' up: 0,',
  31384. ' "1": "down",',
  31385. ' down: 1',
  31386. '};',
  31387. 'this.$rtti.$Enum("TFlag", {',
  31388. ' minvalue: 0,',
  31389. ' maxvalue: 1,',
  31390. ' ordtype: 1,',
  31391. ' enumtype: this.TFlag',
  31392. '});',
  31393. 'this.$rtti.$Set("TFlags", {',
  31394. ' comptype: this.$rtti["TFlag"]',
  31395. '});',
  31396. 'this.ti = null;',
  31397. 'this.tiInt = null;',
  31398. 'this.tiEnum = null;',
  31399. 'this.tiSet = null;',
  31400. '']),
  31401. LinesToStr([ // $mod.$main
  31402. '$mod.ti = rtl.string;',
  31403. '$mod.ti = rtl.boolean;',
  31404. '$mod.ti = rtl.char;',
  31405. '$mod.ti = rtl.double;',
  31406. '$mod.tiInt = rtl.shortint;',
  31407. '$mod.tiInt = rtl.byte;',
  31408. '$mod.tiInt = rtl.smallint;',
  31409. '$mod.tiInt = rtl.word;',
  31410. '$mod.tiInt = rtl.longint;',
  31411. '$mod.tiInt = rtl.longword;',
  31412. '$mod.ti = rtl.jsvalue;',
  31413. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  31414. '$mod.tiSet = $mod.$rtti["TFlags"];',
  31415. '']));
  31416. end;
  31417. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  31418. begin
  31419. WithTypeInfo:=true;
  31420. StartProgram(true,[supTypeInfo]);
  31421. Add('{$modeswitch externalclass}');
  31422. Add('type');
  31423. Add(' TStaticArr = array[boolean] of string;');
  31424. Add(' TDynArr = array of string;');
  31425. Add(' TProc = procedure;');
  31426. Add(' TMethod = procedure of object;');
  31427. Add('var');
  31428. Add(' StaticArray: TStaticArr;');
  31429. Add(' tiStaticArray: TTypeInfoStaticArray;');
  31430. Add(' DynArray: TDynArr;');
  31431. Add(' tiDynArray: TTypeInfoDynArray;');
  31432. Add(' ProcVar: TProc;');
  31433. Add(' tiProcVar: TTypeInfoProcVar;');
  31434. Add(' MethodVar: TMethod;');
  31435. Add(' tiMethodVar: TTypeInfoMethodVar;');
  31436. Add('begin');
  31437. Add(' tiStaticArray:=typeinfo(StaticArray);');
  31438. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  31439. Add(' tiDynArray:=typeinfo(DynArray);');
  31440. Add(' tiDynArray:=typeinfo(TDynArr);');
  31441. Add(' tiProcVar:=typeinfo(ProcVar);');
  31442. Add(' tiProcVar:=typeinfo(TProc);');
  31443. Add(' tiMethodVar:=typeinfo(MethodVar);');
  31444. Add(' tiMethodVar:=typeinfo(TMethod);');
  31445. ConvertProgram;
  31446. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  31447. LinesToStr([ // statements
  31448. 'this.$rtti.$StaticArray("TStaticArr", {',
  31449. ' dims: [2],',
  31450. ' eltype: rtl.string',
  31451. '});',
  31452. 'this.$rtti.$DynArray("TDynArr", {',
  31453. ' eltype: rtl.string',
  31454. '});',
  31455. 'this.$rtti.$ProcVar("TProc", {',
  31456. ' procsig: rtl.newTIProcSig([])',
  31457. '});',
  31458. 'this.$rtti.$MethodVar("TMethod", {',
  31459. ' procsig: rtl.newTIProcSig([]),',
  31460. ' methodkind: 0',
  31461. '});',
  31462. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  31463. 'this.tiStaticArray = null;',
  31464. 'this.DynArray = [];',
  31465. 'this.tiDynArray = null;',
  31466. 'this.ProcVar = null;',
  31467. 'this.tiProcVar = null;',
  31468. 'this.MethodVar = null;',
  31469. 'this.tiMethodVar = null;',
  31470. '']),
  31471. LinesToStr([ // $mod.$main
  31472. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  31473. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  31474. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  31475. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  31476. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  31477. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  31478. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  31479. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  31480. '']));
  31481. end;
  31482. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  31483. begin
  31484. WithTypeInfo:=true;
  31485. StartProgram(true,[supTypeInfo]);
  31486. Add('{$modeswitch externalclass}');
  31487. Add('type');
  31488. Add(' TRec = record end;');
  31489. // ToDo: ^TRec
  31490. Add(' TObject = class end;');
  31491. Add(' TClass = class of tobject;');
  31492. Add('var');
  31493. Add(' Rec: trec;');
  31494. Add(' tiRecord: ttypeinforecord;');
  31495. Add(' Obj: tobject;');
  31496. Add(' tiClass: ttypeinfoclass;');
  31497. Add(' aClass: tclass;');
  31498. Add(' tiClassRef: ttypeinfoclassref;');
  31499. // ToDo: ^TRec
  31500. Add(' tiPointer: ttypeinfopointer;');
  31501. Add('begin');
  31502. Add(' tirecord:=typeinfo(trec);');
  31503. Add(' tirecord:=typeinfo(trec);');
  31504. Add(' ticlass:=typeinfo(obj);');
  31505. Add(' ticlass:=typeinfo(tobject);');
  31506. Add(' ticlass:=typeinfo(aclass);');
  31507. Add(' ticlassref:=typeinfo(tclass);');
  31508. ConvertProgram;
  31509. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  31510. LinesToStr([ // statements
  31511. 'rtl.recNewT(this, "TRec", function () {',
  31512. ' this.$eq = function (b) {',
  31513. ' return true;',
  31514. ' };',
  31515. ' this.$assign = function (s) {',
  31516. ' return this;',
  31517. ' };',
  31518. ' $mod.$rtti.$Record("TRec", {});',
  31519. '});',
  31520. 'rtl.createClass(this, "TObject", null, function () {',
  31521. ' this.$init = function () {',
  31522. ' };',
  31523. ' this.$final = function () {',
  31524. ' };',
  31525. '});',
  31526. 'this.$rtti.$ClassRef("TClass", {',
  31527. ' instancetype: this.$rtti["TObject"]',
  31528. '});',
  31529. 'this.Rec = this.TRec.$new();',
  31530. 'this.tiRecord = null;',
  31531. 'this.Obj = null;',
  31532. 'this.tiClass = null;',
  31533. 'this.aClass = null;',
  31534. 'this.tiClassRef = null;',
  31535. 'this.tiPointer = null;',
  31536. '']),
  31537. LinesToStr([ // $mod.$main
  31538. '$mod.tiRecord = $mod.$rtti["TRec"];',
  31539. '$mod.tiRecord = $mod.$rtti["TRec"];',
  31540. '$mod.tiClass = $mod.Obj.$rtti;',
  31541. '$mod.tiClass = $mod.$rtti["TObject"];',
  31542. '$mod.tiClass = $mod.aClass.$rtti;',
  31543. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  31544. '']));
  31545. end;
  31546. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  31547. begin
  31548. WithTypeInfo:=true;
  31549. StartProgram(true,[supTypeInfo]);
  31550. Add([
  31551. '{$modeswitch externalclass}',
  31552. 'type',
  31553. ' TClass = class of tobject;',
  31554. ' TObject = class',
  31555. ' function MyClass: TClass;',
  31556. ' class function ClassType: TClass;',
  31557. ' end;',
  31558. 'function TObject.MyClass: TClass;',
  31559. 'var t: TTypeInfoClass;',
  31560. 'begin',
  31561. ' t:=TypeInfo(Self);',
  31562. ' t:=TypeInfo(Result);',
  31563. ' t:=TypeInfo(TObject);',
  31564. 'end;',
  31565. 'class function TObject.ClassType: TClass;',
  31566. 'var t: TTypeInfoClass;',
  31567. 'begin',
  31568. ' t:=TypeInfo(Self);',
  31569. ' t:=TypeInfo(Result);',
  31570. 'end;',
  31571. 'var',
  31572. ' Obj: TObject;',
  31573. ' t: TTypeInfoClass;',
  31574. 'begin',
  31575. ' t:=TypeInfo(TObject.ClassType);',
  31576. ' t:=TypeInfo(Obj.ClassType);',
  31577. ' t:=TypeInfo(Obj.MyClass);',
  31578. '']);
  31579. ConvertProgram;
  31580. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  31581. LinesToStr([ // statements
  31582. 'this.$rtti.$Class("TObject");',
  31583. 'this.$rtti.$ClassRef("TClass", {',
  31584. ' instancetype: this.$rtti["TObject"]',
  31585. '});',
  31586. 'rtl.createClass(this, "TObject", null, function () {',
  31587. ' this.$init = function () {',
  31588. ' };',
  31589. ' this.$final = function () {',
  31590. ' };',
  31591. ' this.MyClass = function () {',
  31592. ' var Result = null;',
  31593. ' var t = null;',
  31594. ' t = this.$rtti;',
  31595. ' t = Result.$rtti;',
  31596. ' t = $mod.$rtti["TObject"];',
  31597. ' return Result;',
  31598. ' };',
  31599. ' this.ClassType = function () {',
  31600. ' var Result = null;',
  31601. ' var t = null;',
  31602. ' t = this.$rtti;',
  31603. ' t = Result.$rtti;',
  31604. ' return Result;',
  31605. ' };',
  31606. '});',
  31607. 'this.Obj = null;',
  31608. 'this.t = null;',
  31609. '']),
  31610. LinesToStr([ // $mod.$main
  31611. '$mod.t = $mod.TObject.ClassType().$rtti;',
  31612. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  31613. '$mod.t = $mod.Obj.MyClass().$rtti;',
  31614. '']));
  31615. end;
  31616. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  31617. begin
  31618. WithTypeInfo:=true;
  31619. AddModuleWithIntfImplSrc('typinfo.pas',
  31620. LinesToStr([
  31621. '{$modeswitch externalclass}',
  31622. 'type',
  31623. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  31624. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  31625. '']),
  31626. '');
  31627. AddModuleWithIntfImplSrc('unit2.pas',
  31628. LinesToStr([
  31629. 'uses typinfo;',
  31630. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  31631. 'procedure DoPtr(p: PTypeInfo);',
  31632. 'procedure DoInfo(t: TTypeInfo);',
  31633. 'procedure DoInt(t: TTypeInfoInteger);',
  31634. '']),
  31635. LinesToStr([
  31636. 'procedure DoPtr(p: PTypeInfo);',
  31637. 'begin end;',
  31638. 'procedure DoInfo(t: TTypeInfo);',
  31639. 'begin end;',
  31640. 'procedure DoInt(t: TTypeInfoInteger);',
  31641. 'begin end;',
  31642. '']));
  31643. StartUnit(true);
  31644. Add([
  31645. 'interface',
  31646. 'uses unit2;', // does not use unit typinfo
  31647. 'implementation',
  31648. 'var',
  31649. ' i: byte;',
  31650. ' p: pointer;',
  31651. ' t: PTypeInfo;',
  31652. 'initialization',
  31653. ' p:=typeinfo(i);',
  31654. ' t:=typeinfo(i);',
  31655. ' if p=t then ;',
  31656. ' if p=typeinfo(i) then ;',
  31657. ' if typeinfo(i)=p then ;',
  31658. ' if t=typeinfo(i) then ;',
  31659. ' if typeinfo(i)=t then ;',
  31660. ' DoPtr(p);',
  31661. ' DoPtr(t);',
  31662. ' DoPtr(typeinfo(i));',
  31663. ' DoInfo(p);',
  31664. ' DoInfo(t);',
  31665. ' DoInfo(typeinfo(i));',
  31666. ' DoInt(typeinfo(i));',
  31667. '']);
  31668. ConvertUnit;
  31669. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  31670. LinesToStr([ // statements
  31671. 'var $impl = $mod.$impl;',
  31672. '']),
  31673. LinesToStr([ // this.$init
  31674. '$impl.p = rtl.byte;',
  31675. '$impl.t = rtl.byte;',
  31676. 'if ($impl.p === $impl.t) ;',
  31677. 'if ($impl.p === rtl.byte) ;',
  31678. 'if (rtl.byte === $impl.p) ;',
  31679. 'if ($impl.t === rtl.byte) ;',
  31680. 'if (rtl.byte === $impl.t) ;',
  31681. 'pas.unit2.DoPtr($impl.p);',
  31682. 'pas.unit2.DoPtr($impl.t);',
  31683. 'pas.unit2.DoPtr(rtl.byte);',
  31684. 'pas.unit2.DoInfo($impl.p);',
  31685. 'pas.unit2.DoInfo($impl.t);',
  31686. 'pas.unit2.DoInfo(rtl.byte);',
  31687. 'pas.unit2.DoInt(rtl.byte);',
  31688. '']),
  31689. LinesToStr([ // implementation
  31690. '$impl.i = 0;',
  31691. '$impl.p = null;',
  31692. '$impl.t = null;',
  31693. '']) );
  31694. end;
  31695. procedure TTestModule.TestRTTI_Interface_Corba;
  31696. begin
  31697. WithTypeInfo:=true;
  31698. StartProgram(true,[supTypeInfo]);
  31699. Add([
  31700. '{$interfaces corba}',
  31701. '{$modeswitch externalclass}',
  31702. 'type',
  31703. ' IUnknown = interface',
  31704. ' end;',
  31705. ' IBird = interface',
  31706. ' function GetItem: longint;',
  31707. ' procedure SetItem(Value: longint);',
  31708. ' property Item: longint read GetItem write SetItem;',
  31709. ' end;',
  31710. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  31711. 'var',
  31712. ' i: IBird;',
  31713. ' t: TTypeInfoInterface;',
  31714. 'begin',
  31715. ' t:=TypeInfo(IBird);',
  31716. ' t:=TypeInfo(i);',
  31717. ' DoIt(t);',
  31718. ' DoIt(TypeInfo(IBird));',
  31719. '']);
  31720. ConvertProgram;
  31721. CheckSource('TestRTTI_Interface_Corba',
  31722. LinesToStr([ // statements
  31723. 'rtl.createInterface(',
  31724. ' this,',
  31725. ' "IUnknown",',
  31726. ' "{B92D5841-758A-322B-B800-000000000000}",',
  31727. ' [],',
  31728. ' null,',
  31729. ' function () {',
  31730. ' }',
  31731. ');',
  31732. 'rtl.createInterface(',
  31733. ' this,',
  31734. ' "IBird",',
  31735. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  31736. ' ["GetItem", "SetItem"],',
  31737. ' null,',
  31738. ' function () {',
  31739. ' var $r = this.$rtti;',
  31740. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31741. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  31742. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  31743. ' }',
  31744. ');',
  31745. 'this.DoIt = function (t) {',
  31746. '}; ',
  31747. 'this.i = null;',
  31748. 'this.t = null;',
  31749. '']),
  31750. LinesToStr([ // $mod.$main
  31751. '$mod.t = $mod.$rtti["IBird"];',
  31752. '$mod.t = $mod.i.$rtti;',
  31753. '$mod.DoIt($mod.t);',
  31754. '$mod.DoIt($mod.$rtti["IBird"]);',
  31755. '']));
  31756. end;
  31757. procedure TTestModule.TestRTTI_Interface_COM;
  31758. begin
  31759. WithTypeInfo:=true;
  31760. StartProgram(true,[supTypeInfo]);
  31761. Add([
  31762. '{$interfaces com}',
  31763. '{$modeswitch externalclass}',
  31764. 'type',
  31765. ' TGuid = record end;',
  31766. ' integer = longint;',
  31767. ' IUnknown = interface',
  31768. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  31769. ' function _AddRef: Integer;',
  31770. ' function _Release: Integer;',
  31771. ' end;',
  31772. ' IBird = interface',
  31773. ' function GetItem: longint;',
  31774. ' procedure SetItem(Value: longint);',
  31775. ' property Item: longint read GetItem write SetItem;',
  31776. ' end;',
  31777. 'var',
  31778. ' i: IBird;',
  31779. ' t: TTypeInfoInterface;',
  31780. 'begin',
  31781. ' t:=TypeInfo(IBird);',
  31782. ' t:=TypeInfo(i);',
  31783. '']);
  31784. ConvertProgram;
  31785. CheckSource('TestRTTI_Interface_COM',
  31786. LinesToStr([ // statements
  31787. 'rtl.recNewT(this, "TGuid", function () {',
  31788. ' this.$eq = function (b) {',
  31789. ' return true;',
  31790. ' };',
  31791. ' this.$assign = function (s) {',
  31792. ' return this;',
  31793. ' };',
  31794. ' $mod.$rtti.$Record("TGuid", {});',
  31795. '});',
  31796. 'rtl.createInterface(',
  31797. ' this,',
  31798. ' "IUnknown",',
  31799. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  31800. ' ["QueryInterface", "_AddRef", "_Release"],',
  31801. ' null,',
  31802. ' function () {',
  31803. ' this.$kind = "com";',
  31804. ' var $r = this.$rtti;',
  31805. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  31806. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  31807. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  31808. ' }',
  31809. ');',
  31810. 'rtl.createInterface(',
  31811. ' this,',
  31812. ' "IBird",',
  31813. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  31814. ' ["GetItem", "SetItem"],',
  31815. ' this.IUnknown,',
  31816. ' function () {',
  31817. ' var $r = this.$rtti;',
  31818. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31819. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  31820. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  31821. ' }',
  31822. ');',
  31823. 'this.i = null;',
  31824. 'this.t = null;',
  31825. '']),
  31826. LinesToStr([ // $mod.$main
  31827. '$mod.t = $mod.$rtti["IBird"];',
  31828. '$mod.t = $mod.i.$rtti;',
  31829. '']));
  31830. end;
  31831. procedure TTestModule.TestRTTI_ClassHelper;
  31832. begin
  31833. WithTypeInfo:=true;
  31834. StartProgram(true,[supTypeInfo]);
  31835. Add([
  31836. '{$interfaces com}',
  31837. '{$modeswitch externalclass}',
  31838. 'type',
  31839. ' TObject = class',
  31840. ' end;',
  31841. ' THelper = class helper for TObject',
  31842. ' published',
  31843. ' function GetItem: longint;',
  31844. ' property Item: longint read GetItem;',
  31845. ' end;',
  31846. 'function THelper.GetItem: longint;',
  31847. 'begin',
  31848. 'end;',
  31849. 'var',
  31850. ' t: TTypeInfoHelper;',
  31851. 'begin',
  31852. ' t:=TypeInfo(THelper);',
  31853. '']);
  31854. ConvertProgram;
  31855. CheckSource('TestRTTI_ClassHelper',
  31856. LinesToStr([ // statements
  31857. 'rtl.createClass(this, "TObject", null, function () {',
  31858. ' this.$init = function () {',
  31859. ' };',
  31860. ' this.$final = function () {',
  31861. ' };',
  31862. '});',
  31863. 'rtl.createHelper(this, "THelper", null, function () {',
  31864. ' this.GetItem = function () {',
  31865. ' var Result = 0;',
  31866. ' return Result;',
  31867. ' };',
  31868. ' var $r = this.$rtti;',
  31869. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31870. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  31871. '});',
  31872. 'this.t = null;',
  31873. '']),
  31874. LinesToStr([ // $mod.$main
  31875. '$mod.t = $mod.$rtti["THelper"];',
  31876. '']));
  31877. end;
  31878. procedure TTestModule.TestRTTI_ExternalClass;
  31879. begin
  31880. WithTypeInfo:=true;
  31881. StartProgram(true,[supTypeInfo]);
  31882. Add([
  31883. '{$modeswitch externalclass}',
  31884. 'type',
  31885. ' TJSObject = class external name ''Object''',
  31886. ' end;',
  31887. ' TJSArray = class external name ''Array'' (TJSObject)',
  31888. ' end;',
  31889. 'var',
  31890. ' p: Pointer;',
  31891. ' tc: TTypeInfoExtClass;',
  31892. 'begin',
  31893. ' p:=typeinfo(TJSArray);']);
  31894. ConvertProgram;
  31895. CheckSource('TestRTTI_ExternalClass',
  31896. LinesToStr([ // statements
  31897. 'this.$rtti.$ExtClass("TJSObject", {',
  31898. ' jsclass: "Object"',
  31899. '});',
  31900. 'this.$rtti.$ExtClass("TJSArray", {',
  31901. ' ancestor: this.$rtti["TJSObject"],',
  31902. ' jsclass: "Array"',
  31903. '});',
  31904. 'this.p = null;',
  31905. 'this.tc = null;',
  31906. '']),
  31907. LinesToStr([ // $mod.$main
  31908. '$mod.p = $mod.$rtti["TJSArray"];',
  31909. '']));
  31910. end;
  31911. procedure TTestModule.TestRTTI_Unit;
  31912. begin
  31913. WithTypeInfo:=true;
  31914. AddModuleWithIntfImplSrc('unit2.pas',
  31915. LinesToStr([
  31916. '{$mode delphi}',
  31917. 'type',
  31918. ' TWordArray = array of word;',
  31919. ' TArray<T> = array of T;',
  31920. '']),
  31921. '');
  31922. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  31923. Add([
  31924. '{$mode delphi}',
  31925. 'interface',
  31926. 'uses unit2;',
  31927. 'type',
  31928. ' IBird = interface',
  31929. ' function Swoop: TWordArray;',
  31930. ' function Glide: TArray<word>;',
  31931. ' end;',
  31932. 'procedure Fly;',
  31933. 'implementation',
  31934. 'procedure Fly;',
  31935. 'var',
  31936. ' ta: tTypeInfoDynArray;',
  31937. ' ti: tTypeInfoInterface;',
  31938. 'begin',
  31939. ' ta:=typeinfo(TWordArray);',
  31940. ' ta:=typeinfo(TArray<word>);',
  31941. ' ti:=typeinfo(IBird);',
  31942. 'end;',
  31943. '']);
  31944. ConvertUnit;
  31945. CheckSource('TestRTTI_ExternalClass',
  31946. LinesToStr([ // statements
  31947. 'rtl.createInterface(',
  31948. ' this,',
  31949. ' "IBird",',
  31950. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  31951. ' ["Swoop", "Glide"],',
  31952. ' pas.system.IUnknown,',
  31953. ' function () {',
  31954. ' var $r = this.$rtti;',
  31955. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  31956. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  31957. ' }',
  31958. ');',
  31959. 'this.Fly = function () {',
  31960. ' var ta = null;',
  31961. ' var ti = null;',
  31962. ' ta = pas.unit2.$rtti["TWordArray"];',
  31963. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  31964. ' ti = $mod.$rtti["IBird"];',
  31965. '};',
  31966. '']),
  31967. LinesToStr([ // $mod.$main
  31968. '']));
  31969. end;
  31970. procedure TTestModule.TestResourcestringProgram;
  31971. begin
  31972. AddModuleWithIntfImplSrc('unit2.pas',
  31973. LinesToStr([
  31974. 'resourcestring Title = ''Nice'';',
  31975. '']),
  31976. '');
  31977. StartProgram(true);
  31978. Add([
  31979. 'uses unit2;',
  31980. 'const Bar = ''bar'';',
  31981. 'resourcestring',
  31982. ' Red = ''red'';',
  31983. ' Foobar = ''fOo''+bar;',
  31984. 'var s: string;',
  31985. ' c: char;',
  31986. 'begin',
  31987. ' s:=red;',
  31988. ' s:=test1.red;',
  31989. ' s:=Title;',
  31990. ' c:=red[1];',
  31991. ' c:=test1.red[2];',
  31992. ' if red=foobar then ;',
  31993. ' if red[3]=red[4] then ;']);
  31994. ConvertProgram;
  31995. CheckSource('TestResourcestringProgram',
  31996. LinesToStr([ // statements
  31997. 'this.Bar = "bar";',
  31998. 'this.s = "";',
  31999. 'this.c = "";',
  32000. '$mod.$resourcestrings = {',
  32001. ' Red: {',
  32002. ' org: "red"',
  32003. ' },',
  32004. ' Foobar: {',
  32005. ' org: "fOobar"',
  32006. ' }',
  32007. '};',
  32008. '']),
  32009. LinesToStr([ // $mod.$main
  32010. '$mod.s = rtl.getResStr($mod, "Red");',
  32011. '$mod.s = rtl.getResStr($mod, "Red");',
  32012. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32013. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32014. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32015. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32016. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32017. '']));
  32018. end;
  32019. procedure TTestModule.TestResourcestringUnit;
  32020. begin
  32021. AddModuleWithIntfImplSrc('unit2.pas',
  32022. LinesToStr([
  32023. 'resourcestring Title = ''Nice'';',
  32024. '']),
  32025. '');
  32026. StartUnit(true);
  32027. Add([
  32028. 'interface',
  32029. 'uses unit2;',
  32030. 'const Red = ''rEd'';',
  32031. 'resourcestring',
  32032. ' Blue = ''blue'';',
  32033. ' NotRed = ''not''+Red;',
  32034. 'var s: string;',
  32035. 'implementation',
  32036. 'resourcestring',
  32037. ' ImplGreen = ''green'';',
  32038. 'initialization',
  32039. ' s:=blue+ImplGreen;',
  32040. ' s:=test1.blue+test1.implgreen;',
  32041. ' s:=blue[1]+implgreen[2];',
  32042. ' s:=Title;',
  32043. '']);
  32044. ConvertUnit;
  32045. CheckSource('TestResourcestringUnit',
  32046. LinesToStr([ // statements
  32047. 'this.Red = "rEd";',
  32048. 'this.s = "";',
  32049. '$mod.$resourcestrings = {',
  32050. ' Blue: {',
  32051. ' org: "blue"',
  32052. ' },',
  32053. ' NotRed: {',
  32054. ' org: "notrEd"',
  32055. ' },',
  32056. ' ImplGreen: {',
  32057. ' org: "green"',
  32058. ' }',
  32059. '};',
  32060. '']),
  32061. LinesToStr([ // $mod.$main
  32062. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32063. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32064. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32065. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32066. '']));
  32067. end;
  32068. procedure TTestModule.TestResourcestringImplementation;
  32069. begin
  32070. StartUnit(false);
  32071. Add([
  32072. 'interface',
  32073. 'implementation',
  32074. 'resourcestring',
  32075. ' ImplRed = ''red'';']);
  32076. ConvertUnit;
  32077. CheckSource('TestResourcestringImplementation',
  32078. LinesToStr([ // intf statements
  32079. 'var $impl = $mod.$impl;']),
  32080. LinesToStr([ // $mod.$init
  32081. '']),
  32082. LinesToStr([ // impl statements
  32083. '$mod.$resourcestrings = {',
  32084. ' ImplRed: {',
  32085. ' org: "red"',
  32086. ' }',
  32087. '};',
  32088. '']));
  32089. end;
  32090. procedure TTestModule.TestAttributes_Members;
  32091. begin
  32092. WithTypeInfo:=true;
  32093. StartProgram(false);
  32094. Add([
  32095. '{$modeswitch PrefixedAttributes}',
  32096. 'type',
  32097. ' TObject = class',
  32098. ' constructor Create;',
  32099. ' end;',
  32100. ' TCustomAttribute = class',
  32101. ' constructor Create(Id: word);',
  32102. ' end;',
  32103. ' [Missing]',
  32104. ' TBird = class',
  32105. ' published',
  32106. ' [Tcustom]',
  32107. ' FField: word;',
  32108. ' [tcustom(14)]',
  32109. ' property Size: word read FField;',
  32110. ' [Tcustom(15)]',
  32111. ' procedure Fly; virtual; abstract;',
  32112. ' end;',
  32113. ' TRec = record',
  32114. ' [Tcustom,tcustom(14)]',
  32115. ' Size: word;',
  32116. ' end;',
  32117. 'constructor TObject.Create; begin end;',
  32118. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32119. 'begin',
  32120. '']);
  32121. ConvertProgram;
  32122. CheckSource('TestAttributes_Members',
  32123. LinesToStr([ // statements
  32124. 'rtl.createClass(this, "TObject", null, function () {',
  32125. ' this.$init = function () {',
  32126. ' };',
  32127. ' this.$final = function () {',
  32128. ' };',
  32129. ' this.Create = function () {',
  32130. ' return this;',
  32131. ' };',
  32132. '});',
  32133. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32134. ' this.Create$1 = function (Id) {',
  32135. ' return this;',
  32136. ' };',
  32137. '});',
  32138. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32139. ' this.$init = function () {',
  32140. ' $mod.TObject.$init.call(this);',
  32141. ' this.FField = 0;',
  32142. ' };',
  32143. ' var $r = this.$rtti;',
  32144. ' $r.addField("FField", rtl.word, {',
  32145. ' attr: [$mod.TCustomAttribute, "Create"]',
  32146. ' });',
  32147. ' $r.addProperty(',
  32148. ' "Size",',
  32149. ' 0,',
  32150. ' rtl.word,',
  32151. ' "FField",',
  32152. ' "",',
  32153. ' {',
  32154. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32155. ' }',
  32156. ' );',
  32157. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32158. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32159. ' });',
  32160. '});',
  32161. 'rtl.recNewT(this, "TRec", function () {',
  32162. ' this.Size = 0;',
  32163. ' this.$eq = function (b) {',
  32164. ' return this.Size === b.Size;',
  32165. ' };',
  32166. ' this.$assign = function (s) {',
  32167. ' this.Size = s.Size;',
  32168. ' return this;',
  32169. ' };',
  32170. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32171. ' $r.addField("Size", rtl.word, {',
  32172. ' attr: [',
  32173. ' $mod.TCustomAttribute,',
  32174. ' "Create",',
  32175. ' $mod.TCustomAttribute,',
  32176. ' "Create$1",',
  32177. ' [14]',
  32178. ' ]',
  32179. ' });',
  32180. '});',
  32181. '']),
  32182. LinesToStr([ // $mod.$main
  32183. '']));
  32184. end;
  32185. procedure TTestModule.TestAttributes_Types;
  32186. begin
  32187. WithTypeInfo:=true;
  32188. StartProgram(false);
  32189. Add([
  32190. '{$modeswitch PrefixedAttributes}',
  32191. 'type',
  32192. ' TObject = class',
  32193. ' constructor Create(Id: word);',
  32194. ' end;',
  32195. ' TCustomAttribute = class',
  32196. ' end;',
  32197. ' [TCustom(1)]',
  32198. ' TMyClass = class',
  32199. ' end;',
  32200. ' [TCustom(11)]',
  32201. ' TMyDescendant = class(TMyClass)',
  32202. ' end;',
  32203. ' [TCustom(2)]',
  32204. ' TRec = record',
  32205. ' end;',
  32206. ' [TCustom(3)]',
  32207. ' TInt = type word;',
  32208. 'constructor TObject.Create(Id: word);',
  32209. 'begin',
  32210. 'end;',
  32211. 'var p: pointer;',
  32212. 'begin',
  32213. ' p:=typeinfo(TMyClass);',
  32214. ' p:=typeinfo(TRec);',
  32215. ' p:=typeinfo(TInt);',
  32216. '']);
  32217. ConvertProgram;
  32218. CheckSource('TestAttributes_Types',
  32219. LinesToStr([ // statements
  32220. 'rtl.createClass(this, "TObject", null, function () {',
  32221. ' this.$init = function () {',
  32222. ' };',
  32223. ' this.$final = function () {',
  32224. ' };',
  32225. ' this.Create = function (Id) {',
  32226. ' return this;',
  32227. ' };',
  32228. '});',
  32229. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32230. '});',
  32231. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  32232. ' var $r = this.$rtti;',
  32233. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  32234. '});',
  32235. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  32236. ' var $r = this.$rtti;',
  32237. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  32238. '});',
  32239. 'rtl.recNewT(this, "TRec", function () {',
  32240. ' this.$eq = function (b) {',
  32241. ' return true;',
  32242. ' };',
  32243. ' this.$assign = function (s) {',
  32244. ' return this;',
  32245. ' };',
  32246. ' $mod.$rtti.$Record("TRec", {',
  32247. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  32248. ' });',
  32249. '});',
  32250. 'this.$rtti.$inherited("TInt", rtl.word, {',
  32251. ' attr: [this.TCustomAttribute, "Create", [3]]',
  32252. '});',
  32253. 'this.p = null;',
  32254. '']),
  32255. LinesToStr([ // $mod.$main
  32256. '$mod.p = $mod.$rtti["TMyClass"];',
  32257. '$mod.p = $mod.$rtti["TRec"];',
  32258. '$mod.p = $mod.$rtti["TInt"];',
  32259. '']));
  32260. end;
  32261. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  32262. begin
  32263. WithTypeInfo:=true;
  32264. StartProgram(false);
  32265. Add([
  32266. '{$modeswitch PrefixedAttributes}',
  32267. 'type',
  32268. ' TObject = class',
  32269. ' constructor Create;',
  32270. ' end;',
  32271. ' TCustomAttribute = class',
  32272. ' end;',
  32273. ' THelper = class helper for TCustomAttribute',
  32274. ' constructor Create(Id: word);',
  32275. ' end;',
  32276. ' [TCustom(3)]',
  32277. ' TMyInt = word;',
  32278. 'constructor TObject.Create; begin end;',
  32279. 'constructor THelper.Create(Id: word); begin end;',
  32280. 'begin',
  32281. ' if typeinfo(TMyInt)=nil then ;']);
  32282. ConvertProgram;
  32283. end;
  32284. procedure TTestModule.TestAttributes_InterfacesList;
  32285. begin
  32286. WithTypeInfo:=true;
  32287. StartProgram(false);
  32288. Add([
  32289. '{$mode Delphi}',
  32290. 'type',
  32291. ' TObject = class',
  32292. ' constructor Create;',
  32293. ' end;',
  32294. ' IInterface = interface end;',
  32295. ' TCustomAttribute = class',
  32296. ' end;',
  32297. ' Red = class(TCustomAttribute);',
  32298. ' Blue = class(TCustomAttribute);',
  32299. ' [Red]',
  32300. ' IBird<T> = interface',
  32301. ' procedure Fly;',
  32302. ' end;',
  32303. ' [Blue]',
  32304. ' IEagle = interface(IBird<Word>)',
  32305. ' procedure Dive;',
  32306. ' end;',
  32307. ' TAnt = class(TObject, IEagle)',
  32308. ' procedure Fly; virtual; abstract;',
  32309. ' procedure Dive; virtual; abstract;',
  32310. ' end;',
  32311. 'constructor TObject.Create;',
  32312. 'begin',
  32313. 'end;',
  32314. 'begin',
  32315. '']);
  32316. ConvertProgram;
  32317. CheckSource('TestAttributes_InterfacesList',
  32318. LinesToStr([ // statements
  32319. '$mod.$rtti.$Interface("IBird<System.Word>");',
  32320. 'rtl.createClass(this, "TObject", null, function () {',
  32321. ' this.$init = function () {',
  32322. ' };',
  32323. ' this.$final = function () {',
  32324. ' };',
  32325. ' this.Create = function () {',
  32326. ' return this;',
  32327. ' };',
  32328. '});',
  32329. 'rtl.createInterface(',
  32330. ' this,',
  32331. ' "IInterface",',
  32332. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  32333. ' [],',
  32334. ' null,',
  32335. ' function () {',
  32336. ' this.$kind = "com";',
  32337. ' }',
  32338. ');',
  32339. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32340. '});',
  32341. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  32342. '});',
  32343. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  32344. '});',
  32345. 'rtl.createInterface(',
  32346. ' this,',
  32347. ' "IBird$G1",',
  32348. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  32349. ' ["Fly"],',
  32350. ' this.IInterface,',
  32351. ' function () {',
  32352. ' var $r = this.$rtti;',
  32353. ' $r.addMethod("Fly", 0, []);',
  32354. ' $r.attr = [$mod.Red, "Create"];',
  32355. ' },',
  32356. ' "IBird<System.Word>"',
  32357. ');',
  32358. 'rtl.createInterface(',
  32359. ' this,',
  32360. ' "IEagle",',
  32361. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  32362. ' ["Dive"],',
  32363. ' this.IBird$G1,',
  32364. ' function () {',
  32365. ' var $r = this.$rtti;',
  32366. ' $r.addMethod("Dive", 0, []);',
  32367. ' $r.attr = [$mod.Blue, "Create"];',
  32368. ' }',
  32369. ');',
  32370. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  32371. ' rtl.addIntf(this, $mod.IEagle);',
  32372. '});',
  32373. '']),
  32374. LinesToStr([ // $mod.$main
  32375. '']));
  32376. end;
  32377. procedure TTestModule.TestAssert;
  32378. begin
  32379. StartProgram(false);
  32380. Add([
  32381. 'procedure DoIt;',
  32382. 'var',
  32383. ' b: boolean;',
  32384. ' s: string;',
  32385. 'begin',
  32386. ' {$Assertions on}',
  32387. ' Assert(b);',
  32388. 'end;',
  32389. 'begin',
  32390. ' DoIt;',
  32391. '']);
  32392. ConvertProgram;
  32393. CheckSource('TestAssert',
  32394. LinesToStr([ // statements
  32395. 'this.DoIt = function () {',
  32396. ' var b = false;',
  32397. ' var s = "";',
  32398. ' if (!b) throw "assert failed";',
  32399. '};',
  32400. '']),
  32401. LinesToStr([ // $mod.$main
  32402. '$mod.DoIt();',
  32403. '']));
  32404. end;
  32405. procedure TTestModule.TestAssert_SysUtils;
  32406. begin
  32407. AddModuleWithIntfImplSrc('SysUtils.pas',
  32408. LinesToStr([
  32409. 'type',
  32410. ' TObject = class',
  32411. ' constructor Create;',
  32412. ' end;',
  32413. ' EAssertionFailed = class',
  32414. ' constructor Create(s: string);',
  32415. ' end;',
  32416. '']),
  32417. LinesToStr([
  32418. 'constructor TObject.Create;',
  32419. 'begin end;',
  32420. 'constructor EAssertionFailed.Create(s: string);',
  32421. 'begin end;',
  32422. '']) );
  32423. StartProgram(true);
  32424. Add([
  32425. 'uses sysutils;',
  32426. 'procedure DoIt;',
  32427. 'var',
  32428. ' b: boolean;',
  32429. ' s: string;',
  32430. 'begin',
  32431. ' {$Assertions on}',
  32432. ' Assert(b);',
  32433. ' Assert(b,''msg'');',
  32434. 'end;',
  32435. 'begin',
  32436. ' DoIt;',
  32437. '']);
  32438. ConvertProgram;
  32439. CheckSource('TestAssert_SysUtils',
  32440. LinesToStr([ // statements
  32441. 'this.DoIt = function () {',
  32442. ' var b = false;',
  32443. ' var s = "";',
  32444. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  32445. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  32446. '};',
  32447. '']),
  32448. LinesToStr([ // $mod.$main
  32449. '$mod.DoIt();',
  32450. '']));
  32451. end;
  32452. procedure TTestModule.TestObjectChecks;
  32453. begin
  32454. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  32455. StartProgram(false);
  32456. Add([
  32457. 'type',
  32458. ' TObject = class',
  32459. ' procedure DoIt;',
  32460. ' end;',
  32461. ' TClass = class of tobject;',
  32462. ' TBird = class',
  32463. ' end;',
  32464. ' TBirdClass = class of TBird;',
  32465. 'var',
  32466. ' o : TObject;',
  32467. ' c: TClass;',
  32468. ' b: TBird;',
  32469. ' bc: TBirdClass;',
  32470. 'procedure TObject.DoIt;',
  32471. 'begin',
  32472. ' b:=TBird(o);',
  32473. 'end;',
  32474. 'begin',
  32475. ' o.DoIt;',
  32476. ' b:=TBird(o);',
  32477. ' bc:=TBirdClass(c);',
  32478. '']);
  32479. ConvertProgram;
  32480. CheckSource('TestCheckMethodCall',
  32481. LinesToStr([ // statements
  32482. 'rtl.createClass(this, "TObject", null, function () {',
  32483. ' this.$init = function () {',
  32484. ' };',
  32485. ' this.$final = function () {',
  32486. ' };',
  32487. ' this.DoIt = function () {',
  32488. ' rtl.checkMethodCall(this,$mod.TObject);',
  32489. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  32490. ' };',
  32491. '});',
  32492. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32493. '});',
  32494. 'this.o = null;',
  32495. 'this.c = null;',
  32496. 'this.b = null;',
  32497. 'this.bc = null;',
  32498. '']),
  32499. LinesToStr([ // $mod.$main
  32500. '$mod.o.DoIt();',
  32501. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  32502. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  32503. '']));
  32504. end;
  32505. procedure TTestModule.TestOverflowChecks_Int;
  32506. begin
  32507. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  32508. StartProgram(false);
  32509. Add([
  32510. 'procedure DoIt;',
  32511. 'var',
  32512. ' b: byte;',
  32513. ' n: nativeint;',
  32514. ' u: nativeuint;',
  32515. ' c: currency;',
  32516. 'begin',
  32517. ' n:=n+n;',
  32518. ' n:=n-n;',
  32519. ' n:=n+b;',
  32520. ' n:=b-n;',
  32521. ' n:=n*n;',
  32522. ' n:=n*u;',
  32523. ' c:=c+b;',
  32524. ' c:=b+c;',
  32525. ' c:=c*b;',
  32526. ' c:=b*c;',
  32527. 'end;',
  32528. 'begin',
  32529. '']);
  32530. ConvertProgram;
  32531. CheckSource('TestOverflowChecks_Int',
  32532. LinesToStr([ // statements
  32533. 'this.DoIt = function () {',
  32534. ' var b = 0;',
  32535. ' var n = 0;',
  32536. ' var u = 0;',
  32537. ' var c = 0;',
  32538. ' n = rtl.oc(n + n);',
  32539. ' n = rtl.oc(n - n);',
  32540. ' n = rtl.oc(n + b);',
  32541. ' n = rtl.oc(b - n);',
  32542. ' n = rtl.oc(n * n);',
  32543. ' n = rtl.oc(n * u);',
  32544. ' c = rtl.oc(c + (b * 10000));',
  32545. ' c = rtl.oc((b * 10000) + c);',
  32546. ' c = rtl.oc(c * b);',
  32547. ' c = rtl.oc(b * c);',
  32548. '};',
  32549. '']),
  32550. LinesToStr([ // $mod.$main
  32551. '']));
  32552. end;
  32553. procedure TTestModule.TestRangeChecks_AssignInt;
  32554. begin
  32555. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32556. StartProgram(false);
  32557. Add([
  32558. '{$R+}',
  32559. 'var',
  32560. ' b: byte = 2;',
  32561. ' w: word = 3;',
  32562. 'procedure DoIt(p: byte);',
  32563. 'begin',
  32564. ' b:=w;',
  32565. ' b+=w;',
  32566. ' b:=1;',
  32567. 'end;',
  32568. '{$R-}',
  32569. 'procedure DoSome;',
  32570. 'begin',
  32571. ' DoIt(w);',
  32572. ' b:=w;',
  32573. ' b:=2;',
  32574. 'end;',
  32575. 'begin',
  32576. '{$R+}',
  32577. '']);
  32578. ConvertProgram;
  32579. CheckSource('TestRangeChecks_AssignInt',
  32580. LinesToStr([ // statements
  32581. 'this.b = 2;',
  32582. 'this.w = 3;',
  32583. 'this.DoIt = function (p) {',
  32584. ' rtl.rc(p, 0, 255);',
  32585. ' $mod.b = rtl.rc($mod.w,0,255);',
  32586. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  32587. ' $mod.b = 1;',
  32588. '};',
  32589. 'this.DoSome = function () {',
  32590. ' $mod.DoIt($mod.w);',
  32591. ' $mod.b = $mod.w;',
  32592. ' $mod.b = 2;',
  32593. '};',
  32594. '']),
  32595. LinesToStr([ // $mod.$main
  32596. '']));
  32597. end;
  32598. procedure TTestModule.TestRangeChecks_AssignIntRange;
  32599. begin
  32600. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32601. StartProgram(false);
  32602. Add([
  32603. '{$R+}',
  32604. 'type Ten = 1..10;',
  32605. 'var',
  32606. ' b: Ten = 2;',
  32607. ' w: Ten = 3;',
  32608. 'procedure DoIt(p: Ten);',
  32609. 'begin',
  32610. ' b:=w;',
  32611. ' b+=w;',
  32612. ' b:=1;',
  32613. 'end;',
  32614. '{$R-}',
  32615. 'procedure DoSome;',
  32616. 'begin',
  32617. ' DoIt(w);',
  32618. ' b:=w;',
  32619. ' b:=2;',
  32620. 'end;',
  32621. 'begin',
  32622. '{$R+}',
  32623. '']);
  32624. ConvertProgram;
  32625. CheckSource('TestRangeChecks_AssignIntRange',
  32626. LinesToStr([ // statements
  32627. 'this.b = 2;',
  32628. 'this.w = 3;',
  32629. 'this.DoIt = function (p) {',
  32630. ' rtl.rc(p, 1, 10);',
  32631. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  32632. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  32633. ' $mod.b = 1;',
  32634. '};',
  32635. 'this.DoSome = function () {',
  32636. ' $mod.DoIt($mod.w);',
  32637. ' $mod.b = $mod.w;',
  32638. ' $mod.b = 2;',
  32639. '};',
  32640. '']),
  32641. LinesToStr([ // $mod.$main
  32642. '']));
  32643. end;
  32644. procedure TTestModule.TestRangeChecks_AssignEnum;
  32645. begin
  32646. StartProgram(false);
  32647. Add([
  32648. '{$R+}',
  32649. 'type TEnum = (red,green);',
  32650. 'var',
  32651. ' e: TEnum = red;',
  32652. 'procedure DoIt(p: TEnum);',
  32653. 'begin',
  32654. ' e:=p;',
  32655. ' p:=TEnum(0);',
  32656. ' p:=succ(e);',
  32657. 'end;',
  32658. '{$R-}',
  32659. 'procedure DoSome;',
  32660. 'begin',
  32661. ' DoIt(e);',
  32662. ' e:=TEnum(1);',
  32663. ' e:=pred(e);',
  32664. 'end;',
  32665. 'begin',
  32666. '{$R+}',
  32667. '']);
  32668. ConvertProgram;
  32669. CheckSource('TestRangeChecks_AssignEnum',
  32670. LinesToStr([ // statements
  32671. 'this.TEnum = {',
  32672. ' "0": "red",',
  32673. ' red: 0,',
  32674. ' "1": "green",',
  32675. ' green: 1',
  32676. '};',
  32677. 'this.e = this.TEnum.red;',
  32678. 'this.DoIt = function (p) {',
  32679. ' rtl.rc(p, 0, 1);',
  32680. ' $mod.e = rtl.rc(p, 0, 1);',
  32681. ' p = 0;',
  32682. ' p = rtl.rc($mod.e + 1, 0, 1);',
  32683. '};',
  32684. 'this.DoSome = function () {',
  32685. ' $mod.DoIt($mod.e);',
  32686. ' $mod.e = 1;',
  32687. ' $mod.e = $mod.e - 1;',
  32688. '};',
  32689. '']),
  32690. LinesToStr([ // $mod.$main
  32691. '']));
  32692. end;
  32693. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  32694. begin
  32695. StartProgram(false);
  32696. Add([
  32697. '{$R+}',
  32698. 'type',
  32699. ' TEnum = (red,green);',
  32700. ' TEnumRg = red..green;',
  32701. 'var',
  32702. ' e: TEnumRg = red;',
  32703. 'procedure DoIt(p: TEnumRg);',
  32704. 'begin',
  32705. ' e:=p;',
  32706. ' p:=TEnumRg(0);',
  32707. ' p:=succ(e);',
  32708. 'end;',
  32709. '{$R-}',
  32710. 'procedure DoSome;',
  32711. 'begin',
  32712. ' DoIt(e);',
  32713. ' e:=TEnum(1);',
  32714. ' e:=pred(e);',
  32715. 'end;',
  32716. 'begin',
  32717. '{$R+}',
  32718. '']);
  32719. ConvertProgram;
  32720. CheckSource('TestRangeChecks_AssignEnumRange',
  32721. LinesToStr([ // statements
  32722. 'this.TEnum = {',
  32723. ' "0": "red",',
  32724. ' red: 0,',
  32725. ' "1": "green",',
  32726. ' green: 1',
  32727. '};',
  32728. 'this.e = this.TEnum.red;',
  32729. 'this.DoIt = function (p) {',
  32730. ' rtl.rc(p, 0, 1);',
  32731. ' $mod.e = rtl.rc(p, 0, 1);',
  32732. ' p = 0;',
  32733. ' p = rtl.rc($mod.e + 1, 0, 1);',
  32734. '};',
  32735. 'this.DoSome = function () {',
  32736. ' $mod.DoIt($mod.e);',
  32737. ' $mod.e = 1;',
  32738. ' $mod.e = $mod.e - 1;',
  32739. '};',
  32740. '']),
  32741. LinesToStr([ // $mod.$main
  32742. '']));
  32743. end;
  32744. procedure TTestModule.TestRangeChecks_AssignChar;
  32745. begin
  32746. StartProgram(false);
  32747. Add([
  32748. '{$R+}',
  32749. 'type',
  32750. ' TLetter = char;',
  32751. 'var',
  32752. ' b: TLetter = ''2'';',
  32753. ' w: TLetter = ''3'';',
  32754. 'procedure DoIt(p: TLetter);',
  32755. 'begin',
  32756. ' b:=w;',
  32757. ' b:=''1'';',
  32758. 'end;',
  32759. '{$R-}',
  32760. 'procedure DoSome;',
  32761. 'begin',
  32762. ' DoIt(w);',
  32763. ' b:=w;',
  32764. ' b:=''2'';',
  32765. 'end;',
  32766. 'begin',
  32767. '{$R+}',
  32768. '']);
  32769. ConvertProgram;
  32770. CheckSource('TestRangeChecks_AssignChar',
  32771. LinesToStr([ // statements
  32772. 'this.b = "2";',
  32773. 'this.w = "3";',
  32774. 'this.DoIt = function (p) {',
  32775. ' rtl.rcc(p, 0, 65535);',
  32776. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  32777. ' $mod.b = "1";',
  32778. '};',
  32779. 'this.DoSome = function () {',
  32780. ' $mod.DoIt($mod.w);',
  32781. ' $mod.b = $mod.w;',
  32782. ' $mod.b = "2";',
  32783. '};',
  32784. '']),
  32785. LinesToStr([ // $mod.$main
  32786. '']));
  32787. end;
  32788. procedure TTestModule.TestRangeChecks_AssignCharRange;
  32789. begin
  32790. StartProgram(false);
  32791. Add([
  32792. '{$R+}',
  32793. 'type TDigit = ''0''..''9'';',
  32794. 'var',
  32795. ' b: TDigit = ''2'';',
  32796. ' w: TDigit = ''3'';',
  32797. 'procedure DoIt(p: TDigit);',
  32798. 'begin',
  32799. ' b:=w;',
  32800. ' b:=''1'';',
  32801. 'end;',
  32802. '{$R-}',
  32803. 'procedure DoSome;',
  32804. 'begin',
  32805. ' DoIt(w);',
  32806. ' b:=w;',
  32807. ' b:=''2'';',
  32808. 'end;',
  32809. 'begin',
  32810. '{$R+}',
  32811. '']);
  32812. ConvertProgram;
  32813. CheckSource('TestRangeChecks_AssignCharRange',
  32814. LinesToStr([ // statements
  32815. 'this.b = "2";',
  32816. 'this.w = "3";',
  32817. 'this.DoIt = function (p) {',
  32818. ' rtl.rcc(p, 48, 57);',
  32819. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  32820. ' $mod.b = "1";',
  32821. '};',
  32822. 'this.DoSome = function () {',
  32823. ' $mod.DoIt($mod.w);',
  32824. ' $mod.b = $mod.w;',
  32825. ' $mod.b = "2";',
  32826. '};',
  32827. '']),
  32828. LinesToStr([ // $mod.$main
  32829. '']));
  32830. end;
  32831. procedure TTestModule.TestRangeChecks_ArrayIndex;
  32832. begin
  32833. StartProgram(false);
  32834. Add([
  32835. '{$R+}',
  32836. 'type',
  32837. ' Ten = 1..10;',
  32838. ' TArr = array of Ten;',
  32839. ' TArrArr = array of TArr;',
  32840. ' TArrByte = array[byte] of Ten;',
  32841. ' TArrChar = array[''0''..''9''] of Ten;',
  32842. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  32843. ' TObject = class',
  32844. ' A: TArr;',
  32845. ' end;',
  32846. 'procedure DoIt;',
  32847. 'var',
  32848. ' Arr: TArr;',
  32849. ' ArrArr: TArrArr;',
  32850. ' ArrByte: TArrByte;',
  32851. ' ArrChar: TArrChar;',
  32852. ' ArrByteChar: TArrByteChar;',
  32853. ' i: Ten;',
  32854. ' c: char;',
  32855. ' o: tobject;',
  32856. 'begin',
  32857. ' i:=Arr[1];',
  32858. ' i:=ArrByteChar[1,''2''];',
  32859. ' Arr[1]:=Arr[1];',
  32860. ' Arr[i]:=Arr[i];',
  32861. ' ArrByte[3]:=ArrByte[3];',
  32862. ' ArrByte[i]:=ArrByte[i];',
  32863. ' ArrChar[''5'']:=ArrChar[''5''];',
  32864. ' ArrChar[c]:=ArrChar[c];',
  32865. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  32866. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  32867. ' o.a[i]:=o.a[i];',
  32868. 'end;',
  32869. 'begin',
  32870. '']);
  32871. ConvertProgram;
  32872. CheckSource('TestRangeChecks_ArrayIndex',
  32873. LinesToStr([ // statements
  32874. 'this.TArrByteChar$clone = function (a) {',
  32875. ' var b = [];',
  32876. ' b.length = 256;',
  32877. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  32878. ' return b;',
  32879. '};',
  32880. 'rtl.createClass(this, "TObject", null, function () {',
  32881. ' this.$init = function () {',
  32882. ' this.A = [];',
  32883. ' };',
  32884. ' this.$final = function () {',
  32885. ' this.A = undefined;',
  32886. ' };',
  32887. '});',
  32888. 'this.DoIt = function () {',
  32889. ' var Arr = [];',
  32890. ' var ArrArr = [];',
  32891. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  32892. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  32893. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  32894. ' var i = 0;',
  32895. ' var c = "";',
  32896. ' var o = null;',
  32897. ' i = rtl.rc(Arr[1], 1, 10);',
  32898. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  32899. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  32900. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  32901. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  32902. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  32903. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  32904. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  32905. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  32906. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  32907. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  32908. '};',
  32909. '']),
  32910. LinesToStr([ // $mod.$main
  32911. '']));
  32912. end;
  32913. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  32914. begin
  32915. StartProgram(false);
  32916. Add([
  32917. '{$R+}',
  32918. 'type',
  32919. ' Ten = 1..10;',
  32920. ' TRec = record x: Ten end;',
  32921. ' TArr = array of TRec;',
  32922. ' TArrArr = array of TArr;',
  32923. ' TObject = class',
  32924. ' A: TArr;',
  32925. ' end;',
  32926. 'procedure DoIt;',
  32927. 'var',
  32928. ' Arr: TArr;',
  32929. ' ArrArr: TArrArr;',
  32930. ' i: Ten;',
  32931. ' o: tobject;',
  32932. 'begin',
  32933. ' Arr[1]:=Arr[1];',
  32934. ' Arr[i]:=Arr[i+1];',
  32935. ' o.a[i]:=o.a[i+2];',
  32936. 'end;',
  32937. 'begin',
  32938. '']);
  32939. ConvertProgram;
  32940. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  32941. LinesToStr([ // statements
  32942. 'rtl.recNewT(this, "TRec", function () {',
  32943. ' this.x = 0;',
  32944. ' this.$eq = function (b) {',
  32945. ' return this.x === b.x;',
  32946. ' };',
  32947. ' this.$assign = function (s) {',
  32948. ' this.x = s.x;',
  32949. ' return this;',
  32950. ' };',
  32951. '});',
  32952. 'rtl.createClass(this, "TObject", null, function () {',
  32953. ' this.$init = function () {',
  32954. ' this.A = [];',
  32955. ' };',
  32956. ' this.$final = function () {',
  32957. ' this.A = undefined;',
  32958. ' };',
  32959. '});',
  32960. 'this.DoIt = function () {',
  32961. ' var Arr = [];',
  32962. ' var ArrArr = [];',
  32963. ' var i = 0;',
  32964. ' var o = null;',
  32965. ' Arr[1].$assign(Arr[1]);',
  32966. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  32967. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  32968. '};',
  32969. '']),
  32970. LinesToStr([ // $mod.$main
  32971. '']));
  32972. end;
  32973. procedure TTestModule.TestRangeChecks_StringIndex;
  32974. begin
  32975. StartProgram(false);
  32976. Add([
  32977. 'type',
  32978. ' TObject = class',
  32979. ' S: string;',
  32980. ' end;',
  32981. '{$R+}',
  32982. 'procedure DoIt(var h: string);',
  32983. 'var',
  32984. ' s: string;',
  32985. ' i: longint;',
  32986. ' c: char;',
  32987. ' o: tobject;',
  32988. 'begin',
  32989. ' c:=s[1];',
  32990. ' s[i]:=s[i];',
  32991. ' h[i]:=h[i];',
  32992. ' c:=o.s[i];',
  32993. ' o.s[i]:=c;',
  32994. 'end;',
  32995. 'begin',
  32996. '']);
  32997. ConvertProgram;
  32998. CheckSource('TestRangeChecks_StringIndex',
  32999. LinesToStr([ // statements
  33000. 'rtl.createClass(this, "TObject", null, function () {',
  33001. ' this.$init = function () {',
  33002. ' this.S = "";',
  33003. ' };',
  33004. ' this.$final = function () {',
  33005. ' };',
  33006. '});',
  33007. 'this.DoIt = function (h) {',
  33008. ' var s = "";',
  33009. ' var i = 0;',
  33010. ' var c = "";',
  33011. ' var o = null;',
  33012. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33013. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33014. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33015. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33016. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33017. '};',
  33018. '']),
  33019. LinesToStr([ // $mod.$main
  33020. '']));
  33021. end;
  33022. procedure TTestModule.TestRangeChecks_TypecastInt;
  33023. begin
  33024. StartProgram(false);
  33025. Add([
  33026. '{$R+}',
  33027. 'var',
  33028. ' i: nativeint;',
  33029. ' b: byte;',
  33030. ' sh: shortint;',
  33031. ' w: word;',
  33032. ' sm: smallint;',
  33033. ' lw: longword;',
  33034. ' li: longint;',
  33035. 'begin',
  33036. ' b:=12+byte(i);',
  33037. ' sh:=12+shortint(i);',
  33038. ' w:=12+word(i);',
  33039. ' sm:=12+smallint(i);',
  33040. ' lw:=12+longword(i);',
  33041. ' li:=12+longint(i);',
  33042. '']);
  33043. ConvertProgram;
  33044. CheckSource('TestRangeChecks_TypecastInt',
  33045. LinesToStr([
  33046. 'this.i = 0;',
  33047. 'this.b = 0;',
  33048. 'this.sh = 0;',
  33049. 'this.w = 0;',
  33050. 'this.sm = 0;',
  33051. 'this.lw = 0;',
  33052. 'this.li = 0;',
  33053. '']),
  33054. LinesToStr([
  33055. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33056. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33057. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33058. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33059. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33060. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33061. '']));
  33062. end;
  33063. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33064. begin
  33065. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33066. StartProgram(false);
  33067. Add([
  33068. '{$modeswitch typehelpers}',
  33069. '{$R+}',
  33070. 'type',
  33071. ' TObject = class',
  33072. ' FSize: byte;',
  33073. ' property Size: byte read FSize;',
  33074. ' end;',
  33075. ' THelper = type helper for byte',
  33076. ' procedure SetIt(w: word);',
  33077. ' end;',
  33078. 'procedure THelper.SetIt(w: word);',
  33079. 'begin',
  33080. ' Self:=w;',
  33081. 'end;',
  33082. 'function GetIt: byte;',
  33083. 'begin',
  33084. ' Result.SetIt(2);',
  33085. 'end;',
  33086. 'var',
  33087. ' b: byte = 3;',
  33088. ' o: TObject;',
  33089. 'begin',
  33090. ' b.SetIt(14);',
  33091. ' with b do SetIt(15);',
  33092. ' o.Size.SetIt(16);',
  33093. '']);
  33094. ConvertProgram;
  33095. CheckSource('TestRangeChecks_AssignInt',
  33096. LinesToStr([ // statements
  33097. 'rtl.createClass(this, "TObject", null, function () {',
  33098. ' this.$init = function () {',
  33099. ' this.FSize = 0;',
  33100. ' };',
  33101. ' this.$final = function () {',
  33102. ' };',
  33103. '});',
  33104. 'rtl.createHelper(this, "THelper", null, function () {',
  33105. ' this.SetIt = function (w) {',
  33106. ' rtl.rc(w, 0, 65535);',
  33107. ' this.set(w);',
  33108. ' };',
  33109. '});',
  33110. 'this.GetIt = function () {',
  33111. ' var Result = 0;',
  33112. ' $mod.THelper.SetIt.call({',
  33113. ' get: function () {',
  33114. ' return Result;',
  33115. ' },',
  33116. ' set: function (v) {',
  33117. ' rtl.rc(v, 0, 255);',
  33118. ' Result = v;',
  33119. ' }',
  33120. ' }, 2);',
  33121. ' return Result;',
  33122. '};',
  33123. 'this.b = 3;',
  33124. 'this.o = null;',
  33125. '']),
  33126. LinesToStr([ // $mod.$main
  33127. '$mod.THelper.SetIt.call({',
  33128. ' p: $mod,',
  33129. ' get: function () {',
  33130. ' return this.p.b;',
  33131. ' },',
  33132. ' set: function (v) {',
  33133. ' rtl.rc(v, 0, 255);',
  33134. ' this.p.b = v;',
  33135. ' }',
  33136. '}, 14);',
  33137. 'var $with = $mod.b;',
  33138. '$mod.THelper.SetIt.call({',
  33139. ' get: function () {',
  33140. ' return $with;',
  33141. ' },',
  33142. ' set: function (v) {',
  33143. ' rtl.rc(v, 0, 255);',
  33144. ' $with = v;',
  33145. ' }',
  33146. '}, 15);',
  33147. '$mod.THelper.SetIt.call({',
  33148. ' p: $mod.o,',
  33149. ' get: function () {',
  33150. ' return this.p.FSize;',
  33151. ' },',
  33152. ' set: function (v) {',
  33153. ' rtl.rc(v, 0, 255);',
  33154. ' this.p.FSize = v;',
  33155. ' }',
  33156. '}, 16);',
  33157. '']));
  33158. end;
  33159. procedure TTestModule.TestAsync_Proc;
  33160. begin
  33161. StartProgram(false);
  33162. Add([
  33163. 'procedure Fly(w: word = 1); async; forward;',
  33164. 'procedure Run(w: word = 2); async;',
  33165. 'begin',
  33166. ' Fly(w);',
  33167. ' Fly;',
  33168. ' await(Fly(w));',
  33169. ' await(Fly);',
  33170. 'end;',
  33171. 'procedure Fly(w: word); ',
  33172. 'begin',
  33173. 'end;',
  33174. 'begin',
  33175. ' Run;',
  33176. ' Run(3);',
  33177. '']);
  33178. CheckResolverUnexpectedHints();
  33179. ConvertProgram;
  33180. CheckSource('TestAsync_Proc',
  33181. LinesToStr([ // statements
  33182. 'this.Run = async function (w) {',
  33183. ' $mod.Fly(w);',
  33184. ' $mod.Fly(1);',
  33185. ' await $mod.Fly(w);',
  33186. ' await $mod.Fly(1);',
  33187. '};',
  33188. 'this.Fly = async function (w) {',
  33189. '};',
  33190. '']),
  33191. LinesToStr([
  33192. '$mod.Run(2);',
  33193. '$mod.Run(3);',
  33194. '']));
  33195. end;
  33196. procedure TTestModule.TestAsync_CallResultIsPromise;
  33197. begin
  33198. StartProgram(false);
  33199. Add([
  33200. '{$modeswitch externalclass}',
  33201. 'type',
  33202. ' TObject = class',
  33203. ' end;',
  33204. ' TJSPromise = class external name ''Promise''',
  33205. ' end;',
  33206. ' TBird = class',
  33207. ' function Fly: word; async; ',
  33208. ' end;',
  33209. 'function TBird.Fly: word; async; ',
  33210. 'begin',
  33211. ' Result:=3;',
  33212. ' Fly:=4+Result;',
  33213. ' if Result=5 then ;',
  33214. ' exit(6);',
  33215. 'end;',
  33216. 'function Run: word; async;',
  33217. 'begin',
  33218. ' Result:=11+Result;',
  33219. ' inc(Result);',
  33220. 'end;',
  33221. 'var',
  33222. ' p: TJSPromise;',
  33223. ' o: TBird;',
  33224. 'begin',
  33225. ' p:=Run;',
  33226. ' p:=Run();',
  33227. ' if Run=p then ;',
  33228. ' if p=Run then ;',
  33229. ' if Run()=p then ;',
  33230. ' if p=Run() then ;',
  33231. ' p:=o.Fly;',
  33232. ' p:=o.Fly();',
  33233. ' if o.Fly=p then ;',
  33234. ' if o.Fly()=p then ;',
  33235. ' with o do begin',
  33236. ' p:=Fly;',
  33237. ' p:=Fly();',
  33238. ' if Fly=p then ;',
  33239. ' if Fly()=p then ;',
  33240. ' end;',
  33241. '']);
  33242. CheckResolverUnexpectedHints();
  33243. ConvertProgram;
  33244. CheckSource('TestAsync_CallResultIsPromise',
  33245. LinesToStr([ // statements
  33246. 'rtl.createClass(this, "TObject", null, function () {',
  33247. ' this.$init = function () {',
  33248. ' };',
  33249. ' this.$final = function () {',
  33250. ' };',
  33251. '});',
  33252. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33253. ' this.Fly = async function () {',
  33254. ' var Result = 0;',
  33255. ' Result = 3;',
  33256. ' Result = 4 + Result;',
  33257. ' if (Result === 5) ;',
  33258. ' return 6;',
  33259. ' return Result;',
  33260. ' };',
  33261. '});',
  33262. 'this.Run = async function () {',
  33263. ' var Result = 0;',
  33264. ' Result = 11 + Result;',
  33265. ' Result += 1;',
  33266. ' return Result;',
  33267. '};',
  33268. 'this.p = null;',
  33269. 'this.o = null;',
  33270. '']),
  33271. LinesToStr([
  33272. '$mod.p = $mod.Run();',
  33273. '$mod.p = $mod.Run();',
  33274. 'if ($mod.Run() === $mod.p) ;',
  33275. 'if ($mod.p === $mod.Run()) ;',
  33276. 'if ($mod.Run() === $mod.p) ;',
  33277. 'if ($mod.p === $mod.Run()) ;',
  33278. '$mod.p = $mod.o.Fly();',
  33279. '$mod.p = $mod.o.Fly();',
  33280. 'if ($mod.o.Fly() === $mod.p) ;',
  33281. 'if ($mod.o.Fly() === $mod.p) ;',
  33282. 'var $with = $mod.o;',
  33283. '$mod.p = $with.Fly();',
  33284. '$mod.p = $with.Fly();',
  33285. 'if ($with.Fly() === $mod.p) ;',
  33286. 'if ($with.Fly() === $mod.p) ;',
  33287. '']));
  33288. end;
  33289. procedure TTestModule.TestAsync_ConstructorFail;
  33290. begin
  33291. StartProgram(false);
  33292. Add([
  33293. 'type',
  33294. ' TObject = class',
  33295. ' end;',
  33296. ' TBird = class',
  33297. ' constructor Create; async;',
  33298. ' end;',
  33299. 'constructor TBird.Create; async;',
  33300. 'begin',
  33301. 'end;',
  33302. 'begin',
  33303. '']);
  33304. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  33305. ConvertProgram;
  33306. end;
  33307. procedure TTestModule.TestAsync_PropertyGetterFail;
  33308. begin
  33309. StartProgram(false);
  33310. Add([
  33311. 'type',
  33312. ' TObject = class',
  33313. ' end;',
  33314. ' TBird = class',
  33315. ' function GetSize: word; async;',
  33316. ' property Size: word read GetSize;',
  33317. ' end;',
  33318. 'function TBird.GetSize: word; async;',
  33319. 'begin',
  33320. 'end;',
  33321. 'begin',
  33322. '']);
  33323. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  33324. ConvertProgram;
  33325. end;
  33326. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  33327. begin
  33328. StartProgram(false);
  33329. Add([
  33330. 'procedure Run; async;',
  33331. 'begin',
  33332. ' await(word,1);',
  33333. 'end;',
  33334. 'begin',
  33335. '']);
  33336. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  33337. ConvertProgram;
  33338. end;
  33339. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  33340. begin
  33341. StartProgram(false);
  33342. Add([
  33343. 'type',
  33344. ' TObject = class',
  33345. ' end;',
  33346. ' TBird = class',
  33347. ' end;',
  33348. 'function Fly: TObject; async;',
  33349. 'begin',
  33350. 'end;',
  33351. 'procedure Run; async;',
  33352. 'begin',
  33353. ' await(TBird,Fly);',
  33354. 'end;',
  33355. 'begin',
  33356. '']);
  33357. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  33358. ConvertProgram;
  33359. end;
  33360. procedure TTestModule.TestAWait_OutsideAsyncFail;
  33361. begin
  33362. StartProgram(false);
  33363. Add([
  33364. 'procedure Crawl(w: double); ',
  33365. 'begin',
  33366. 'end;',
  33367. 'procedure Run(w: double);',
  33368. 'begin',
  33369. ' await(Crawl(w));',
  33370. 'end;',
  33371. 'begin',
  33372. ' Run(1);']);
  33373. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  33374. ConvertProgram;
  33375. end;
  33376. procedure TTestModule.TestAWait_IntegerFail;
  33377. begin
  33378. StartProgram(false);
  33379. Add([
  33380. 'function Run: word;',
  33381. 'begin',
  33382. 'end;',
  33383. 'procedure Fly(w: word); async;',
  33384. 'begin',
  33385. ' await(Run());',
  33386. 'end;',
  33387. 'begin',
  33388. ' Fly(1);']);
  33389. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  33390. ConvertProgram;
  33391. end;
  33392. procedure TTestModule.TestAWait_ExternalClassPromise;
  33393. begin
  33394. StartProgram(false);
  33395. Add([
  33396. '{$modeswitch externalclass}',
  33397. 'type',
  33398. ' TJSPromise = class external name ''Promise''',
  33399. ' end;',
  33400. ' TJSThenable = class external name ''Thenable''',
  33401. ' end;',
  33402. 'function Fly(w: word): TJSPromise;',
  33403. 'begin',
  33404. 'end;',
  33405. 'function Jump(w: word): word; async;',
  33406. 'begin',
  33407. 'end;',
  33408. 'function Eat(w: word): TJSPromise; async;',
  33409. 'begin',
  33410. 'end;',
  33411. 'function Run(d: double): word; async;',
  33412. 'var',
  33413. ' p: TJSPromise;',
  33414. 'begin',
  33415. ' Result:=await(word,p);', // promise needs type
  33416. ' Result:=await(word,Fly(3));', // promise needs type
  33417. ' Result:=await(Jump(4));', // async non promise must omit the type
  33418. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  33419. ' Result:=await(word,Eat(6));', // promise needs type
  33420. 'end;',
  33421. 'begin',
  33422. '']);
  33423. ConvertProgram;
  33424. CheckSource('TestAWait_ExternalClassPromise',
  33425. LinesToStr([ // statements
  33426. 'this.Fly = function (w) {',
  33427. ' var Result = null;',
  33428. ' return Result;',
  33429. '};',
  33430. 'this.Jump = async function (w) {',
  33431. ' var Result = 0;',
  33432. ' return Result;',
  33433. '};',
  33434. 'this.Eat = async function (w) {',
  33435. ' var Result = null;',
  33436. ' return Result;',
  33437. '};',
  33438. 'this.Run = async function (d) {',
  33439. ' var Result = 0;',
  33440. ' var p = null;',
  33441. ' Result = await p;',
  33442. ' Result = await $mod.Fly(3);',
  33443. ' Result = await $mod.Jump(4);',
  33444. ' Result = await $mod.Jump(5);',
  33445. ' Result = await $mod.Eat(6);',
  33446. ' return Result;',
  33447. '};',
  33448. '']),
  33449. LinesToStr([
  33450. ]));
  33451. CheckResolverUnexpectedHints();
  33452. end;
  33453. procedure TTestModule.TestAWait_JSValue;
  33454. begin
  33455. StartProgram(false);
  33456. Add([
  33457. '{$modeswitch externalclass}',
  33458. 'type',
  33459. ' TJSPromise = class external name ''Promise''',
  33460. ' end;',
  33461. 'function Fly(w: word): jsvalue; async;',
  33462. 'begin',
  33463. 'end;',
  33464. 'function Run(d: jsvalue; var e): word; async;',
  33465. 'begin',
  33466. ' Result:=await(word,d);', // promise needs type
  33467. ' d:=await(Fly(4));', // async non promise must omit the type
  33468. ' Result:=await(word,e);', // promise needs type
  33469. 'end;',
  33470. 'begin',
  33471. '']);
  33472. ConvertProgram;
  33473. CheckSource('TestAWait_JSValue',
  33474. LinesToStr([ // statements
  33475. 'this.Fly = async function (w) {',
  33476. ' var Result = undefined;',
  33477. ' return Result;',
  33478. '};',
  33479. 'this.Run = async function (d, e) {',
  33480. ' var Result = 0;',
  33481. ' Result = await d;',
  33482. ' d = await $mod.Fly(4);',
  33483. ' Result = await e.get();',
  33484. ' return Result;',
  33485. '};',
  33486. '']),
  33487. LinesToStr([
  33488. ]));
  33489. CheckResolverUnexpectedHints();
  33490. end;
  33491. procedure TTestModule.TestAWait_Result;
  33492. begin
  33493. StartProgram(false);
  33494. Add([
  33495. '{$modeswitch externalclass}',
  33496. 'type',
  33497. ' TJSPromise = class external name ''Promise''',
  33498. ' end;',
  33499. 'function Crawl(d: double = 1.3): TJSPromise; ',
  33500. 'begin',
  33501. 'end;',
  33502. 'function Run(d: double = 1.6): word; async;',
  33503. 'begin',
  33504. ' Result:=await(word,Crawl);',
  33505. ' Result:=await(word,Crawl(4.5));',
  33506. ' Result:=await(Run);',
  33507. ' Result:=await(Run(6.7));',
  33508. 'end;',
  33509. 'begin',
  33510. ' Run(1);']);
  33511. ConvertProgram;
  33512. CheckSource('TestAWait_Result',
  33513. LinesToStr([ // statements
  33514. 'this.Crawl = function (d) {',
  33515. ' var Result = null;',
  33516. ' return Result;',
  33517. '};',
  33518. 'this.Run = async function (d) {',
  33519. ' var Result = 0;',
  33520. ' Result = await $mod.Crawl(1.3);',
  33521. ' Result = await $mod.Crawl(4.5);',
  33522. ' Result = await $mod.Run(1.6);',
  33523. ' Result = await $mod.Run(6.7);',
  33524. ' return Result;',
  33525. '};',
  33526. '']),
  33527. LinesToStr([
  33528. '$mod.Run(1);'
  33529. ]));
  33530. CheckResolverUnexpectedHints();
  33531. end;
  33532. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  33533. begin
  33534. StartProgram(false);
  33535. Add([
  33536. '{$mode objfpc}',
  33537. '{$modeswitch externalclass}',
  33538. 'type',
  33539. ' TJSPromise = class external name ''Promise''',
  33540. ' end;',
  33541. 'function Run: TJSPromise; async;',
  33542. 'begin',
  33543. 'end;',
  33544. 'procedure Fly(w: word); async;',
  33545. 'begin',
  33546. ' await(Run());',
  33547. 'end;',
  33548. 'begin',
  33549. ' Fly(1);']);
  33550. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  33551. nWrongNumberOfParametersForCallTo);
  33552. ConvertProgram;
  33553. end;
  33554. procedure TTestModule.TestAsync_AnonymousProc;
  33555. begin
  33556. StartProgram(false);
  33557. Add([
  33558. '{$mode objfpc}',
  33559. '{$modeswitch externalclass}',
  33560. 'type',
  33561. ' TJSPromise = class external name ''Promise''',
  33562. ' end;',
  33563. 'type',
  33564. ' TFunc = reference to function(x: double): word; async;',
  33565. 'function Crawl(d: double = 1.3): word; async;',
  33566. 'begin',
  33567. 'end;',
  33568. 'var Func: TFunc;',
  33569. 'begin',
  33570. ' Func:=function(c:double):word async begin',
  33571. ' Result:=await(Crawl(c));',
  33572. ' end;',
  33573. ' Func:=function(c:double):word async assembler asm',
  33574. ' end;',
  33575. ' ']);
  33576. ConvertProgram;
  33577. CheckSource('TestAsync_AnonymousProc',
  33578. LinesToStr([ // statements
  33579. 'this.Crawl = async function (d) {',
  33580. ' var Result = 0;',
  33581. ' return Result;',
  33582. '};',
  33583. 'this.Func = null;',
  33584. '']),
  33585. LinesToStr([
  33586. '$mod.Func = async function (c) {',
  33587. ' var Result = 0;',
  33588. ' Result = await $mod.Crawl(c);',
  33589. ' return Result;',
  33590. '};',
  33591. '$mod.Func = async function (c) {',
  33592. '};',
  33593. '']));
  33594. CheckResolverUnexpectedHints();
  33595. end;
  33596. procedure TTestModule.TestAsync_ProcType;
  33597. begin
  33598. StartProgram(false);
  33599. Add([
  33600. '{$mode objfpc}',
  33601. 'type',
  33602. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  33603. ' TFunc = function(x: double = 1.1): word; async;',
  33604. ' TProc = procedure(x: longint = 7); async;',
  33605. 'function Crawl(d: double): word; async;',
  33606. 'begin',
  33607. 'end;',
  33608. 'procedure Run(e:longint); async;',
  33609. 'begin',
  33610. 'end;',
  33611. 'procedure Fly(p: TProc); async;',
  33612. 'begin',
  33613. ' await(p);',
  33614. ' await(p());',
  33615. 'end;',
  33616. 'var',
  33617. ' RefFunc: TRefFunc;',
  33618. ' Func: TFunc;',
  33619. ' Proc, ProcB: TProc;',
  33620. 'begin',
  33621. ' Func:=@Crawl;',
  33622. ' RefFunc:=@Crawl;',
  33623. ' RefFunc:=function(c:double):word async begin',
  33624. ' Result:=await(RefFunc);',
  33625. ' Result:=await(RefFunc());',
  33626. ' Result:=await(Func);',
  33627. ' Result:=await(Func());',
  33628. ' await(Proc);',
  33629. ' await(Proc());',
  33630. ' await(Proc(13));',
  33631. ' end;',
  33632. ' Proc:=@Run;',
  33633. ' if Proc=ProcB then ;',
  33634. ' ']);
  33635. ConvertProgram;
  33636. CheckResolverUnexpectedHints();
  33637. CheckSource('TestAsync_ProcType',
  33638. LinesToStr([ // statements
  33639. 'this.Crawl = async function (d) {',
  33640. ' var Result = 0;',
  33641. ' return Result;',
  33642. '};',
  33643. 'this.Run = async function (e) {',
  33644. '};',
  33645. 'this.Fly = async function (p) {',
  33646. ' await p(7);',
  33647. ' await p(7);',
  33648. '};',
  33649. 'this.RefFunc = null;',
  33650. 'this.Func = null;',
  33651. 'this.Proc = null;',
  33652. 'this.ProcB = null;',
  33653. '']),
  33654. LinesToStr([
  33655. '$mod.Func = $mod.Crawl;',
  33656. '$mod.RefFunc = $mod.Crawl;',
  33657. '$mod.RefFunc = async function (c) {',
  33658. ' var Result = 0;',
  33659. ' Result = await $mod.RefFunc(1.3);',
  33660. ' Result = await $mod.RefFunc(1.3);',
  33661. ' Result = await $mod.Func(1.1);',
  33662. ' Result = await $mod.Func(1.1);',
  33663. ' await $mod.Proc(7);',
  33664. ' await $mod.Proc(7);',
  33665. ' await $mod.Proc(13);',
  33666. ' return Result;',
  33667. '};',
  33668. '$mod.Proc = $mod.Run;',
  33669. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  33670. '']));
  33671. end;
  33672. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  33673. begin
  33674. StartProgram(false);
  33675. Add([
  33676. '{$mode objfpc}',
  33677. 'type',
  33678. ' TRefFunc = reference to function(x: double = 1.3): word;',
  33679. 'function Crawl(d: double): word; async;',
  33680. 'begin',
  33681. 'end;',
  33682. 'var',
  33683. ' RefFunc: TRefFunc;',
  33684. 'begin',
  33685. ' RefFunc:=@Crawl;',
  33686. ' ']);
  33687. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  33688. ConvertProgram;
  33689. end;
  33690. procedure TTestModule.TestAsync_Inherited;
  33691. begin
  33692. StartProgram(false);
  33693. Add([
  33694. '{$mode objfpc}',
  33695. '{$modeswitch externalclass}',
  33696. 'type',
  33697. ' TJSPromise = class external name ''Promise''',
  33698. ' end;',
  33699. ' TObject = class',
  33700. ' function Run(w: word = 3): word; async; virtual;',
  33701. ' end;',
  33702. ' TBird = class',
  33703. ' function Run(w: word = 3): word; async; override;',
  33704. ' end;',
  33705. 'function TObject.Run(w: word = 3): word; async;',
  33706. 'begin',
  33707. 'end;',
  33708. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  33709. 'var p: TJSPromise;',
  33710. 'begin',
  33711. ' p:=inherited;',
  33712. ' p:=inherited Run;',
  33713. ' p:=inherited Run();',
  33714. ' p:=inherited Run(4);',
  33715. ' exit(p);',
  33716. ' exit(inherited);',
  33717. ' exit(inherited Run);',
  33718. ' exit(inherited Run(5));',
  33719. ' exit(6);',
  33720. 'end;',
  33721. 'begin',
  33722. ' ']);
  33723. ConvertProgram;
  33724. CheckSource('TestAsync_Inherited',
  33725. LinesToStr([ // statements
  33726. 'rtl.createClass(this, "TObject", null, function () {',
  33727. ' this.$init = function () {',
  33728. ' };',
  33729. ' this.$final = function () {',
  33730. ' };',
  33731. ' this.Run = async function (w) {',
  33732. ' var Result = 0;',
  33733. ' return Result;',
  33734. ' };',
  33735. '});',
  33736. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33737. ' this.Run = async function (w) {',
  33738. ' var Result = 0;',
  33739. ' var p = null;',
  33740. ' p = $mod.TObject.Run.apply(this, arguments);',
  33741. ' p = $mod.TObject.Run.call(this, 3);',
  33742. ' p = $mod.TObject.Run.call(this, 3);',
  33743. ' p = $mod.TObject.Run.call(this, 4);',
  33744. ' return p;',
  33745. ' return $mod.TObject.Run.apply(this, arguments);',
  33746. ' return $mod.TObject.Run.call(this, 3);',
  33747. ' return $mod.TObject.Run.call(this, 5);',
  33748. ' return 6;',
  33749. ' return Result;',
  33750. ' };',
  33751. '});',
  33752. '']),
  33753. LinesToStr([
  33754. '']));
  33755. CheckResolverUnexpectedHints();
  33756. end;
  33757. procedure TTestModule.TestAsync_ClassInterface;
  33758. begin
  33759. StartProgram(false);
  33760. Add([
  33761. '{$mode objfpc}',
  33762. '{$modeswitch externalclass}',
  33763. 'type',
  33764. ' TJSPromise = class external name ''Promise''',
  33765. ' end;',
  33766. ' IUnknown = interface',
  33767. ' function _AddRef: longint;',
  33768. ' function _Release: longint;',
  33769. ' end;',
  33770. 'function Say(i: IUnknown): IUnknown; async;',
  33771. 'begin',
  33772. 'end;',
  33773. 'function Run: IUnknown; async;',
  33774. 'begin',
  33775. ' Result:=await(Run);',
  33776. ' Result:=await(Run());',
  33777. ' Result:=await(Run) as IUnknown;',
  33778. ' Result:=await(Say(nil));',
  33779. ' Result:=await(Say(await(Run())));',
  33780. ' Result:=await(Say(await(Run()) as IUnknown));',
  33781. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  33782. 'end;',
  33783. 'procedure Fly;',
  33784. 'var p: TJSPromise;',
  33785. 'begin',
  33786. ' Run;',
  33787. ' Run();',
  33788. ' p:=Run;',
  33789. ' p:=Run();',
  33790. 'end;',
  33791. 'begin',
  33792. ' ']);
  33793. ConvertProgram;
  33794. CheckSource('TestAsync_ClassInterface',
  33795. LinesToStr([ // statements
  33796. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  33797. 'this.Say = async function (i) {',
  33798. ' var Result = null;',
  33799. ' return Result;',
  33800. '};',
  33801. 'this.Run = async function () {',
  33802. ' var Result = null;',
  33803. ' var $ok = false;',
  33804. ' try {',
  33805. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  33806. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  33807. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  33808. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  33809. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  33810. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  33811. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  33812. ' $ok = true;',
  33813. ' } finally {',
  33814. ' if (!$ok) rtl._Release(Result);',
  33815. ' };',
  33816. ' return Result;',
  33817. '};',
  33818. 'this.Fly = function () {',
  33819. ' var p = null;',
  33820. ' $mod.Run();',
  33821. ' $mod.Run();',
  33822. ' p = $mod.Run();',
  33823. ' p = $mod.Run();',
  33824. '};',
  33825. '']),
  33826. LinesToStr([
  33827. '']));
  33828. CheckResolverUnexpectedHints();
  33829. end;
  33830. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  33831. begin
  33832. StartProgram(true,[supTInterfacedObject]);
  33833. Add([
  33834. '{$mode objfpc}',
  33835. '{$modeswitch externalclass}',
  33836. 'type',
  33837. ' TJSPromise = class external name ''Promise''',
  33838. ' end;',
  33839. ' IBird = interface',
  33840. ' procedure Run;',
  33841. ' end;',
  33842. ' TBird = class(TInterfacedObject,IBird)',
  33843. ' procedure Run; async;',
  33844. ' end;',
  33845. 'procedure TBird.Run;',
  33846. 'begin',
  33847. 'end;',
  33848. 'begin',
  33849. ' ']);
  33850. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  33851. ConvertProgram;
  33852. end;
  33853. procedure TTestModule.TestAWait_ClassAs;
  33854. begin
  33855. StartProgram(false);
  33856. Add([
  33857. '{$mode objfpc}',
  33858. '{$modeswitch externalclass}',
  33859. 'type',
  33860. ' TJSPromise = class external name ''Promise''',
  33861. ' end;',
  33862. ' TObject = class',
  33863. ' function Run: TObject; async;',
  33864. ' end;',
  33865. ' TBird = class',
  33866. ' function Fly: TBird; async;',
  33867. ' end;',
  33868. 'function TObject.Run: TObject; async;',
  33869. 'begin',
  33870. 'end;',
  33871. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  33872. 'var o: TObject;',
  33873. 'begin',
  33874. ' o:=await(TObject,Run);',
  33875. ' o:=await(TObject,Fly);',
  33876. ' o:=await(TBird,Fly);',
  33877. ' o:=await(TObject,inherited Run);',
  33878. ' o:=await(TObject,inherited Run) as TBird;',
  33879. 'end;',
  33880. 'begin',
  33881. ' ']);
  33882. ConvertProgram;
  33883. CheckSource('TestAWait_ClassAs',
  33884. LinesToStr([ // statements
  33885. 'rtl.createClass(this, "TObject", null, function () {',
  33886. ' this.$init = function () {',
  33887. ' };',
  33888. ' this.$final = function () {',
  33889. ' };',
  33890. ' this.Run = async function () {',
  33891. ' var Result = null;',
  33892. ' return Result;',
  33893. ' };',
  33894. '});',
  33895. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33896. ' this.Fly = async function () {',
  33897. ' var Result = null;',
  33898. ' var o = null;',
  33899. ' o = await this.Run();',
  33900. ' o = await this.Fly();',
  33901. ' o = await this.Fly();',
  33902. ' o = await $mod.TObject.Run.call(this);',
  33903. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  33904. ' return Result;',
  33905. ' };',
  33906. '});',
  33907. '']),
  33908. LinesToStr([
  33909. '']));
  33910. CheckResolverUnexpectedHints();
  33911. end;
  33912. procedure TTestModule.TestLibrary_Empty;
  33913. begin
  33914. StartLibrary(false);
  33915. Add([
  33916. '']);
  33917. ConvertLibrary;
  33918. CheckFullSource('TestLibrary_Empty',
  33919. LinesToStr([ // statements
  33920. 'rtl.module("library", [], function () {',
  33921. ' var $mod = this;',
  33922. ' $mod.$main = function () {',
  33923. ' };',
  33924. '});',
  33925. 'rtl.run("library");',
  33926. '']));
  33927. CheckResolverUnexpectedHints();
  33928. end;
  33929. procedure TTestModule.TestLibrary_ExportFunc;
  33930. begin
  33931. StartLibrary(false);
  33932. Add([
  33933. 'procedure Run(w: word);',
  33934. 'begin',
  33935. 'end;',
  33936. 'exports',
  33937. ' Run;',
  33938. ' run name ''Foo'';',
  33939. ' test1.run name ''Test1Run'';',
  33940. '']);
  33941. ConvertLibrary;
  33942. CheckFullSource('TestLibrary_ExportFunc',
  33943. LinesToStr([ // statements
  33944. 'rtl.module("library", [], function () {',
  33945. ' var $mod = this;',
  33946. ' this.Run = function (w) {',
  33947. ' };',
  33948. ' $mod.$main = function () {',
  33949. ' };',
  33950. '});',
  33951. 'rtl.run("library");',
  33952. 'export const Run = pas.library.Run;',
  33953. 'export const Foo = pas.library.Run;',
  33954. 'export const Test1Run = pas.library.Run;',
  33955. '']));
  33956. CheckResolverUnexpectedHints();
  33957. end;
  33958. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  33959. begin
  33960. StartLibrary(false);
  33961. Add([
  33962. 'procedure Run(w: word); overload;',
  33963. 'begin',
  33964. 'end;',
  33965. 'procedure Run(s: string); overload;',
  33966. 'begin',
  33967. 'end;',
  33968. 'exports',
  33969. ' Run;',
  33970. '']);
  33971. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  33972. nCantDetermineWhichOverloadedFunctionToCall);
  33973. ConvertLibrary;
  33974. end;
  33975. procedure TTestModule.TestLibrary_Export_Index_Fail;
  33976. begin
  33977. StartLibrary(false);
  33978. Add([
  33979. 'procedure Run(w: word);',
  33980. 'begin',
  33981. 'end;',
  33982. 'exports',
  33983. ' Run index 3;',
  33984. '']);
  33985. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  33986. ConvertLibrary;
  33987. end;
  33988. procedure TTestModule.TestLibrary_ExportVar;
  33989. begin
  33990. StartLibrary(false);
  33991. Add([
  33992. 'var Wing: word;',
  33993. 'exports',
  33994. ' Wing, wing name ''BirdArm'';',
  33995. '']);
  33996. ConvertLibrary;
  33997. CheckFullSource('TestLibrary_ExportVar',
  33998. LinesToStr([ // statements
  33999. 'rtl.module("library", [], function () {',
  34000. ' var $mod = this;',
  34001. ' this.Wing = 0;',
  34002. ' $mod.$main = function () {',
  34003. ' };',
  34004. '});',
  34005. 'rtl.run("library");',
  34006. 'export const vars = {};',
  34007. 'Object.defineProperties(vars, {',
  34008. ' Wing: {',
  34009. ' enumerable: true,',
  34010. ' get: function () {',
  34011. ' return pas.library.Wing;',
  34012. ' },',
  34013. ' set: function (v) {',
  34014. ' pas.library.Wing = v;',
  34015. ' }',
  34016. ' },',
  34017. ' BirdArm: {',
  34018. ' enumerable: true,',
  34019. ' get: function () {',
  34020. ' return pas.library.Wing;',
  34021. ' },',
  34022. ' set: function (v) {',
  34023. ' pas.library.Wing = v;',
  34024. ' }',
  34025. ' }',
  34026. '});',
  34027. '']));
  34028. CheckResolverUnexpectedHints();
  34029. end;
  34030. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34031. begin
  34032. AddModuleWithIntfImplSrc('Unit1.pas',
  34033. LinesToStr([
  34034. 'type',
  34035. ' TAnt = class',
  34036. ' class function Crawl: word; static;',
  34037. ' end;',
  34038. 'function Fly: word;',
  34039. '']),
  34040. LinesToStr([
  34041. 'function Fly: word;',
  34042. 'begin',
  34043. 'end;',
  34044. 'class function TAnt.Crawl: word;',
  34045. 'begin',
  34046. 'end;',
  34047. '']));
  34048. StartLibrary(true,[supTObject]);
  34049. Add([
  34050. 'uses unit1;',
  34051. 'exports',
  34052. ' Fly;',
  34053. ' TAnt.Crawl;',
  34054. '']);
  34055. ConvertLibrary;
  34056. CheckFullSource('TestLibrary_ExportUnitFunc',
  34057. LinesToStr([ // statements
  34058. 'rtl.module("library", ["system", "Unit1"], function () {',
  34059. ' var $mod = this;',
  34060. ' $mod.$main = function () {',
  34061. ' };',
  34062. '});',
  34063. 'rtl.run("library");',
  34064. 'export const Fly = pas.Unit1.Fly;',
  34065. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34066. '']));
  34067. CheckResolverUnexpectedHints();
  34068. end;
  34069. Initialization
  34070. RegisterTests([TTestModule]);
  34071. end.