tcmodules.pas 945 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853
  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, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. type
  27. TSrcMarkerKind = (
  28. mkLabel,
  29. mkResolverReference,
  30. mkDirectReference
  31. );
  32. const
  33. SrcMarker: array[TSrcMarkerKind] of char = (
  34. '#', // mkLabel
  35. '@', // mkResolverReference
  36. '=' // mkDirectReference
  37. );
  38. type
  39. PSrcMarker = ^TSrcMarker;
  40. TSrcMarker = record
  41. Kind: TSrcMarkerKind;
  42. Filename: string;
  43. Row: integer;
  44. StartCol, EndCol: integer; // token start, end column
  45. Identifier: string;
  46. Next: PSrcMarker;
  47. end;
  48. TSystemUnitPart = (
  49. supTObject,
  50. supTVarRec,
  51. supTypeInfo,
  52. supTInterfacedObject,
  53. supWriteln
  54. );
  55. TSystemUnitParts = set of TSystemUnitPart;
  56. { TTestHintMessage }
  57. TTestHintMessage = class
  58. public
  59. Id: int64;
  60. MsgType: TMessageType;
  61. MsgNumber: integer;
  62. Msg: string;
  63. SourcePos: TPasSourcePos;
  64. end;
  65. TTestResolverReferenceData = record
  66. Filename: string;
  67. Row: integer;
  68. StartCol: integer;
  69. EndCol: integer;
  70. Found: TFPList; // list of TPasElement at this token
  71. end;
  72. PTestResolverReferenceData = ^TTestResolverReferenceData;
  73. { TTestPasParser }
  74. TTestPasParser = Class(TPasParser)
  75. end;
  76. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  77. { TTestEnginePasResolver }
  78. TTestEnginePasResolver = class(TPas2JsResolver)
  79. private
  80. FFilename: string;
  81. FModule: TPasModule;
  82. FOnFindUnit: TOnFindUnit;
  83. FParser: TTestPasParser;
  84. FStreamResolver: TStreamResolver;
  85. FScanner: TPas2jsPasScanner;
  86. FSource: string;
  87. procedure SetModule(const AValue: TPasModule);
  88. public
  89. destructor Destroy; override;
  90. function CreateElement(AClass: TPTreeElement; const AName: String;
  91. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  92. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  93. overload; override;
  94. function FindUnit(const AName, InFilename: String; NameExpr,
  95. InFileExpr: TPasExpr): TPasModule; override;
  96. procedure UsedInterfacesFinished(Section: TPasSection); override;
  97. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  98. property Filename: string read FFilename write FFilename;
  99. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  100. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  101. property Parser: TTestPasParser read FParser write FParser;
  102. property Source: string read FSource write FSource;
  103. property Module: TPasModule read FModule write SetModule;
  104. end;
  105. { TCustomTestModule }
  106. TCustomTestModule = Class(TTestCase)
  107. private
  108. FWithTypeInfo: boolean;
  109. FSource: TStringList;
  110. FSkipTests: boolean;
  111. FScanner: TPas2jsPasScanner;
  112. FResolvers: TObjectList;// list of TTestEnginePasResolver
  113. FPasProgram: TPasProgram;
  114. FPasLibrary: TPasLibrary;
  115. FParser: TTestPasParser;
  116. FModule: TPasModule;
  117. FJSSource: TStringList;
  118. FJSRegModuleCall: TJSCallExpression;
  119. FJSModuleSrc: TJSSourceElements;
  120. FJSModuleCallArgs: TJSArguments;
  121. FJSModule: TJSSourceElements;
  122. FJSInterfaceUses: TJSArrayLiteral;
  123. FJSInitBody: TJSFunctionBody;
  124. FJSImplentationUses: TJSArrayLiteral;
  125. FJSImplementationUses: TJSArrayLiteral;
  126. FHub: TPas2JSResolverHub;
  127. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  128. FHintMsgs: TObjectList; // list of TTestHintMessage
  129. FFirstPasStatement: TPasImplBlock;
  130. FFileResolver: TStreamResolver;
  131. FFilename: string;
  132. FExpectedErrorNumber: integer;
  133. FExpectedErrorMsg: string;
  134. FExpectedErrorClass: ExceptClass;
  135. FEngine: TTestEnginePasResolver;
  136. FConverter: TPasToJSConverter;
  137. {$IFDEF EnablePasTreeGlobalRefCount}
  138. FElementRefCountAtSetup: int64;
  139. {$ENDIF}
  140. procedure FreeSrcMarkers;
  141. function GetResolverCount: integer;
  142. function GetResolvers(Index: integer): TTestEnginePasResolver;
  143. function GetMsgCount: integer;
  144. function GetMsgs(Index: integer): TTestHintMessage;
  145. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  146. procedure OnParserLog(Sender: TObject; const Msg: String);
  147. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  148. procedure OnScannerLog(Sender: TObject; const Msg: String);
  149. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  150. procedure OnFindReference(El: TPasElement; FindData: pointer);
  151. procedure SetWithTypeInfo(const AValue: boolean);
  152. protected
  153. procedure SetUp; override;
  154. function CreateConverter: TPasToJSConverter; virtual;
  155. function LoadUnit(const aUnitName: String): TPasModule;
  156. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  157. procedure TearDown; override;
  158. Procedure Add(Line: string); virtual;
  159. Procedure Add(const Lines: array of string);
  160. Procedure StartParsing; virtual;
  161. procedure ParseModuleQueue; virtual;
  162. procedure ParseModule; virtual;
  163. procedure ParseProgram; virtual;
  164. procedure ParseLibrary; virtual;
  165. procedure ParseUnit; virtual;
  166. protected
  167. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  168. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  169. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  172. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  173. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  174. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  175. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure ConvertModule; virtual;
  178. procedure ConvertProgram; virtual;
  179. procedure ConvertLibrary; virtual;
  180. procedure ConvertUnit; virtual;
  181. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  182. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  183. function GetDottedIdentifier(El: TJSElement): string;
  184. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  185. ImplStatements: string = ''); virtual;
  186. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  187. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  188. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  189. procedure CheckReferenceDirectives; virtual;
  190. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  191. Msg: string; Marker: PSrcMarker = nil); virtual;
  192. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  193. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  194. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  197. function IsErrorExpected(E: Exception): boolean;
  198. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  199. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  200. procedure HandleScannerError(E: EScannerError);
  201. procedure HandleParserError(E: EParserError);
  202. procedure HandlePasResolveError(E: EPasResolve);
  203. procedure HandlePas2JSError(E: EPas2JS);
  204. procedure HandleException(E: Exception);
  205. procedure FailException(E: Exception);
  206. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  207. function IndexOfResolver(const Filename: string): integer;
  208. function GetResolver(const Filename: string): TTestEnginePasResolver;
  209. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  210. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  211. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  212. function FindSrcLabel(const Identifier: string): PSrcMarker;
  213. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  214. function GetDefaultNamespace: string;
  215. property PasProgram: TPasProgram Read FPasProgram;
  216. property PasLibrary: TPasLibrary Read FPasLibrary;
  217. property ResolverEngine: TTestEnginePasResolver read FEngine;
  218. property Filename: string read FFilename;
  219. Property Module: TPasModule Read FModule;
  220. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  221. property Converter: TPasToJSConverter read FConverter;
  222. property JSSource: TStringList read FJSSource;
  223. property JSModule: TJSSourceElements read FJSModule;
  224. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  225. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  226. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  227. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  228. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  229. property JSInitBody: TJSFunctionBody read FJSInitBody;
  230. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  231. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  232. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  233. property SkipTests: boolean read FSkipTests write FSkipTests;
  234. public
  235. constructor Create; override;
  236. destructor Destroy; override;
  237. property Hub: TPas2JSResolverHub read FHub;
  238. property Source: TStringList read FSource;
  239. property FileResolver: TStreamResolver read FFileResolver;
  240. property Scanner: TPas2jsPasScanner read FScanner;
  241. property Parser: TTestPasParser read FParser;
  242. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  243. property ResolverCount: integer read GetResolverCount;
  244. property MsgCount: integer read GetMsgCount;
  245. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  246. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  247. end;
  248. { TTestModule }
  249. TTestModule = class(TCustomTestModule)
  250. Published
  251. Procedure TestReservedWords;
  252. // program, units, includes
  253. Procedure TestEmptyProgram;
  254. Procedure TestEmptyProgramUseStrict;
  255. Procedure TestEmptyUnit;
  256. Procedure TestEmptyUnitUseStrict;
  257. Procedure TestDottedUnitNames;
  258. Procedure TestDottedUnitNameImpl;
  259. Procedure TestDottedUnitExpr;
  260. Procedure Test_ModeFPCFail;
  261. Procedure Test_ModeSwitchCBlocksFail;
  262. Procedure TestUnit_UseSystem;
  263. Procedure TestUnit_Intf1Impl2Intf1;
  264. Procedure TestIncludeVersion;
  265. // vars/const
  266. Procedure TestVarInt;
  267. Procedure TestVarBaseTypes;
  268. Procedure TestBaseTypeSingleFail;
  269. Procedure TestBaseTypeExtendedFail;
  270. Procedure TestConstBaseTypes;
  271. Procedure TestUnitImplVars;
  272. Procedure TestUnitImplConsts;
  273. Procedure TestUnitImplRecord;
  274. Procedure TestRenameJSNameConflict;
  275. Procedure TestLocalConst;
  276. Procedure TestVarExternal;
  277. Procedure TestVarExternalOtherUnit;
  278. Procedure TestVarAbsoluteFail;
  279. Procedure TestConstExternal;
  280. // numbers
  281. Procedure TestDouble;
  282. Procedure TestDoubleSmall;
  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 TestStringConst_Multiline;
  302. Procedure TestString_Length;
  303. Procedure TestString_Compare;
  304. Procedure TestString_SetLength;
  305. Procedure TestString_CharAt;
  306. Procedure TestStringHMinusFail;
  307. Procedure TestStr;
  308. Procedure TestBaseType_AnsiStringFail;
  309. Procedure TestBaseType_WideStringFail;
  310. Procedure TestBaseType_ShortStringFail;
  311. Procedure TestBaseType_RawByteStringFail;
  312. Procedure TestTypeShortstring_Fail;
  313. Procedure TestCharSet_Custom;
  314. Procedure TestWideChar;
  315. Procedure TestForCharDo;
  316. Procedure TestForCharInDo;
  317. // alias types
  318. Procedure TestAliasTypeRef;
  319. Procedure TestTypeCast_BaseTypes;
  320. Procedure TestTypeCast_AliasBaseTypes;
  321. // functions
  322. Procedure TestEmptyProc;
  323. Procedure TestProcOneParam;
  324. Procedure TestFunctionWithoutParams;
  325. Procedure TestProcedureWithoutParams;
  326. Procedure TestPrgProcVar;
  327. Procedure TestProcTwoArgs;
  328. Procedure TestProc_DefaultValue;
  329. Procedure TestUnitProcVar;
  330. Procedure TestImplProc;
  331. Procedure TestFunctionResult;
  332. Procedure TestNestedProc;
  333. Procedure TestNestedProc_ResultString;
  334. Procedure TestForwardProc;
  335. Procedure TestNestedForwardProc;
  336. Procedure TestAssignFunctionResult;
  337. Procedure TestFunctionResultInCondition;
  338. Procedure TestFunctionResultInForLoop;
  339. Procedure TestFunctionResultInTypeCast;
  340. Procedure TestExit;
  341. Procedure TestExit_ResultInFinally;
  342. Procedure TestBreak;
  343. Procedure TestBreakAsVar;
  344. Procedure TestContinue;
  345. Procedure TestProc_External;
  346. Procedure TestProc_ExternalOtherUnit;
  347. Procedure TestProc_Asm;
  348. Procedure TestProc_AsmSubBlock;
  349. Procedure TestProc_Assembler;
  350. Procedure TestProc_VarParam;
  351. Procedure TestProc_VarParamString;
  352. Procedure TestProc_VarParamV;
  353. Procedure TestProc_Overload;
  354. Procedure TestProc_OverloadForward;
  355. Procedure TestProc_OverloadIntfImpl;
  356. Procedure TestProc_OverloadNested;
  357. Procedure TestProc_OverloadNestedForward;
  358. Procedure TestProc_OverloadUnitCycle;
  359. Procedure TestProc_Varargs;
  360. Procedure TestProc_ConstOrder;
  361. Procedure TestProc_DuplicateConst;
  362. Procedure TestProc_LocalVarAbsolute;
  363. Procedure TestProc_ResultAbsolute;
  364. Procedure TestProc_LocalVarInit;
  365. Procedure TestProc_ReservedWords;
  366. Procedure TestProc_ConstRefWord;
  367. // anonymous functions
  368. Procedure TestAnonymousProc_Assign_ObjFPC;
  369. Procedure TestAnonymousProc_Assign_Delphi;
  370. Procedure TestAnonymousProc_Arg;
  371. Procedure TestAnonymousProc_Typecast;
  372. Procedure TestAnonymousProc_With;
  373. Procedure TestAnonymousProc_ExceptOn;
  374. Procedure TestAnonymousProc_Nested;
  375. Procedure TestAnonymousProc_NestedAssignResult;
  376. Procedure TestAnonymousProc_Class;
  377. Procedure TestAnonymousProc_ForLoop;
  378. Procedure TestAnonymousProc_AsmDelphi;
  379. // enums, sets
  380. Procedure TestEnum_Name;
  381. Procedure TestEnum_Number;
  382. Procedure TestEnum_ConstFail;
  383. Procedure TestEnum_Functions;
  384. Procedure TestEnumRg_Functions;
  385. Procedure TestEnum_AsParams;
  386. Procedure TestEnumRange_Array;
  387. Procedure TestEnum_ForIn;
  388. Procedure TestEnum_ScopedNumber;
  389. Procedure TestEnum_InFunction;
  390. Procedure TestEnum_Name_Anonymous_Unit;
  391. Procedure TestSet_Enum;
  392. Procedure TestSet_Operators;
  393. Procedure TestSet_Operator_In;
  394. Procedure TestSet_Functions;
  395. Procedure TestSet_PassAsArgClone;
  396. Procedure TestSet_AsParams;
  397. Procedure TestSet_Property;
  398. Procedure TestSet_EnumConst;
  399. Procedure TestSet_IntConst;
  400. Procedure TestSet_IntRange;
  401. Procedure TestSet_AnonymousEnumType;
  402. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  403. Procedure TestSet_ConstEnum;
  404. Procedure TestSet_ConstChar;
  405. Procedure TestSet_ConstInt;
  406. Procedure TestSet_InFunction;
  407. Procedure TestSet_ForIn;
  408. // statements
  409. Procedure TestNestBegin;
  410. Procedure TestIncDec;
  411. Procedure TestLoHiFpcMode;
  412. Procedure TestLoHiDelphiMode;
  413. Procedure TestAssignments;
  414. Procedure TestArithmeticOperators1;
  415. Procedure TestMultiAdd;
  416. Procedure TestLogicalOperators;
  417. Procedure TestBitwiseOperators;
  418. Procedure TestBitwiseOperatorsLongword;
  419. Procedure TestFunctionInt;
  420. Procedure TestFunctionString;
  421. Procedure TestIfThen;
  422. Procedure TestForLoop;
  423. Procedure TestForLoopInsideFunction;
  424. Procedure TestForLoop_ReadVarAfter;
  425. Procedure TestForLoop_Nested;
  426. Procedure TestRepeatUntil;
  427. Procedure TestAsmBlock;
  428. Procedure TestAsmPas_Impl; // ToDo
  429. Procedure TestTryFinally;
  430. Procedure TestTryExcept;
  431. Procedure TestTryExcept_ReservedWords;
  432. Procedure TestIfThenRaiseElse;
  433. Procedure TestCaseOf;
  434. Procedure TestCaseOf_UseSwitch;
  435. Procedure TestCaseOfNoElse;
  436. Procedure TestCaseOfNoElse_UseSwitch;
  437. Procedure TestCaseOfRange;
  438. Procedure TestCaseOfString;
  439. Procedure TestCaseOfChar;
  440. Procedure TestCaseOfExternalClassConst;
  441. Procedure TestDebugger;
  442. // arrays
  443. Procedure TestArray_Dynamic;
  444. Procedure TestArray_Dynamic_Nil;
  445. Procedure TestArray_DynMultiDimensional;
  446. Procedure TestArray_DynamicAssign;
  447. Procedure TestArray_StaticInt;
  448. Procedure TestArray_StaticBool;
  449. Procedure TestArray_StaticChar;
  450. Procedure TestArray_StaticMultiDim;
  451. Procedure TestArray_StaticInFunction;
  452. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  453. Procedure TestArrayOfRecord;
  454. Procedure TestArray_StaticRecord;
  455. Procedure TestArrayOfSet;
  456. Procedure TestArray_DynAsParam;
  457. Procedure TestArray_StaticAsParam;
  458. Procedure TestArrayElement_AsParams;
  459. Procedure TestArrayElementFromFuncResult_AsParams;
  460. Procedure TestArrayEnumTypeRange;
  461. Procedure TestArray_SetLengthOutArg;
  462. Procedure TestArray_SetLengthProperty;
  463. Procedure TestArray_SetLengthMultiDim;
  464. Procedure TestArray_SetLengthDynOfStatic;
  465. Procedure TestArray_OpenArrayOfString;
  466. Procedure TestArray_ArrayOfCharAssignString;
  467. Procedure TestArray_ConstRef;
  468. Procedure TestArray_Concat;
  469. Procedure TestArray_Concat_Append;
  470. Procedure TestArray_Concat_Append_Var;
  471. Procedure TestArray_Copy;
  472. Procedure TestArray_InsertDelete;
  473. Procedure TestArray_Add_Append;
  474. Procedure TestArray_DynArrayConstObjFPC;
  475. Procedure TestArray_DynArrayConstDelphi;
  476. Procedure TestArray_ArrayLitAsParam;
  477. Procedure TestArray_ArrayLitMultiDimAsParam;
  478. Procedure TestArray_ArrayLitStaticAsParam;
  479. Procedure TestArray_ForInArrOfString;
  480. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  481. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  482. Procedure TestArrayOfConst_TVarRec;
  483. Procedure TestArrayOfConst_PassBaseTypes;
  484. Procedure TestArrayOfConst_PassObj;
  485. // record
  486. Procedure TestRecord_Empty;
  487. Procedure TestRecord_Var;
  488. Procedure TestRecord_VarExternal;
  489. Procedure TestRecord_WithDo;
  490. Procedure TestRecord_Assign;
  491. Procedure TestRecord_AsParams;
  492. Procedure TestRecord_ConstRef;
  493. Procedure TestRecordElement_AsParams;
  494. Procedure TestRecordElementFromFuncResult_AsParams;
  495. Procedure TestRecordElementFromWith_AsParams;
  496. Procedure TestRecord_Equal;
  497. Procedure TestRecord_JSValue;
  498. Procedure TestRecord_VariantFail;
  499. Procedure TestRecord_FieldArray;
  500. Procedure TestRecord_Const;
  501. Procedure TestRecord_TypecastFail;
  502. Procedure TestRecord_InFunction;
  503. // anonymous record
  504. Procedure TestRecordAnonym_Field;
  505. Procedure TestRecordAnonym_Assign;
  506. Procedure TestRecordAnonym_Nested;
  507. Procedure TestRecordAnonym_Const;
  508. Procedure TestRecordAnonym_InFunction;
  509. // advanced record
  510. Procedure TestAdvRecord_Function;
  511. Procedure TestAdvRecord_Property;
  512. Procedure TestAdvRecord_PropertyDefault;
  513. Procedure TestAdvRecord_Property_ClassMethod;
  514. Procedure TestAdvRecord_Const;
  515. Procedure TestAdvRecord_ExternalField;
  516. Procedure TestAdvRecord_SubRecord;
  517. Procedure TestAdvRecord_SubClass;
  518. Procedure TestAdvRecord_SubInterfaceFail;
  519. Procedure TestAdvRecord_Constructor;
  520. Procedure TestAdvRecord_ClassConstructor_Program;
  521. Procedure TestAdvRecord_ClassConstructor_Unit;
  522. // classes
  523. Procedure TestClass_TObjectDefaultConstructor;
  524. Procedure TestClass_TObjectConstructorWithParams;
  525. Procedure TestClass_TObjectConstructorWithDefaultParam;
  526. Procedure TestClass_Var;
  527. Procedure TestClass_Method;
  528. Procedure TestClass_Implementation;
  529. Procedure TestClass_Inheritance;
  530. Procedure TestClass_TypeAlias;
  531. Procedure TestClass_AbstractMethod;
  532. Procedure TestClass_CallInherited_ProcNoParams;
  533. Procedure TestClass_CallInherited_WithParams;
  534. Procedure TestClasS_CallInheritedConstructor;
  535. Procedure TestClass_ClassVar_Assign;
  536. Procedure TestClass_CallClassMethod;
  537. Procedure TestClass_CallClassMethodStatic;
  538. Procedure TestClass_Property;
  539. Procedure TestClass_Property_ClassMethod;
  540. Procedure TestClass_Property_ClassMethodStatic;
  541. Procedure TestClass_Property_Indexed;
  542. Procedure TestClass_Property_IndexSpec;
  543. Procedure TestClass_PropertyOfTypeArray;
  544. Procedure TestClass_PropertyDefault;
  545. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  546. //Procedure TestClass_PropertyDefault;
  547. Procedure TestClass_PropertyOverride;
  548. Procedure TestClass_PropertyIncVisibility;
  549. Procedure TestClass_Assigned;
  550. Procedure TestClass_WithClassDoCreate;
  551. Procedure TestClass_WithClassInstDoProperty;
  552. Procedure TestClass_WithClassInstDoPropertyWithParams;
  553. Procedure TestClass_WithClassInstDoFunc;
  554. Procedure TestClass_TypeCast;
  555. Procedure TestClass_TypeCastUntypedParam;
  556. Procedure TestClass_Overloads;
  557. Procedure TestClass_OverloadsAncestor;
  558. Procedure TestClass_OverloadConstructor;
  559. Procedure TestClass_OverloadDelphiOverride;
  560. Procedure TestClass_ReintroduceVarDelphi;
  561. Procedure TestClass_ReintroducedVar;
  562. Procedure TestClass_RaiseDescendant;
  563. Procedure TestClass_ExternalMethod;
  564. Procedure TestClass_ExternalVirtualNameMismatchFail;
  565. Procedure TestClass_ExternalOverrideFail;
  566. Procedure TestClass_ExternalVar;
  567. Procedure TestClass_Const;
  568. Procedure TestClass_ConstEnum;
  569. Procedure TestClass_LocalConstDuplicate_Prg;
  570. Procedure TestClass_LocalConstDuplicate_Unit;
  571. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  572. Procedure TestClass_LocalVarSelfFail;
  573. Procedure TestClass_ArgSelfFail;
  574. Procedure TestClass_NestedProcSelf;
  575. Procedure TestClass_NestedProcSelf2;
  576. Procedure TestClass_NestedProcClassSelf;
  577. Procedure TestClass_NestedProcCallInherited;
  578. Procedure TestClass_TObjectFree;
  579. Procedure TestClass_TObjectFree_VarArg;
  580. Procedure TestClass_TObjectFreeNewInstance;
  581. Procedure TestClass_TObjectFreeLowerCase;
  582. Procedure TestClass_TObjectFreeFunctionFail;
  583. Procedure TestClass_TObjectFreePropertyFail;
  584. Procedure TestClass_ForIn;
  585. Procedure TestClass_DispatchMessage;
  586. Procedure TestClass_Message_DuplicateIntFail;
  587. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  588. // class of
  589. Procedure TestClassOf_Create;
  590. Procedure TestClassOf_Call;
  591. Procedure TestClassOf_Assign;
  592. Procedure TestClassOf_Is;
  593. Procedure TestClassOf_Compare;
  594. Procedure TestClassOf_ClassVar;
  595. Procedure TestClassOf_ClassMethod;
  596. Procedure TestClassOf_ClassProperty;
  597. Procedure TestClassOf_ClassMethodSelf;
  598. Procedure TestClassOf_TypeCast;
  599. Procedure TestClassOf_ImplicitFunctionCall;
  600. Procedure TestClassOf_Const;
  601. // nested class
  602. Procedure TestNestedClass_Alias;
  603. Procedure TestNestedClass_Record;
  604. Procedure TestNestedClass_Class;
  605. Procedure TestNestedClass_CallInherited;
  606. // external class
  607. Procedure TestExternalClass_Var;
  608. Procedure TestExternalClass_Const;
  609. Procedure TestExternalClass_Dollar;
  610. Procedure TestExternalClass_DuplicateVarFail;
  611. Procedure TestExternalClass_Method;
  612. Procedure TestExternalClass_ClassMethod;
  613. Procedure TestExternalClass_ClassMethodStatic;
  614. Procedure TestExternalClass_FunctionResultInTypeCast;
  615. Procedure TestExternalClass_NonExternalOverride;
  616. Procedure TestExternalClass_OverloadHint;
  617. Procedure TestExternalClass_SameNamePublishedProperty;
  618. Procedure TestExternalClass_Property;
  619. Procedure TestExternalClass_PropertyDate;
  620. Procedure TestExternalClass_ClassProperty;
  621. Procedure TestExternalClass_ClassOf;
  622. Procedure TestExternalClass_ClassOtherUnit;
  623. Procedure TestExternalClass_Is;
  624. Procedure TestExternalClass_As;
  625. Procedure TestExternalClass_DestructorFail;
  626. Procedure TestExternalClass_New;
  627. Procedure TestExternalClass_ClassOf_New;
  628. Procedure TestExternalClass_FuncClassOf_New;
  629. Procedure TestExternalClass_New_PasClassFail;
  630. Procedure TestExternalClass_New_PasClassBracketsFail;
  631. Procedure TestExternalClass_NewExtName;
  632. Procedure TestExternalClass_Constructor;
  633. Procedure TestExternalClass_ConstructorBrackets;
  634. Procedure TestExternalClass_LocalConstSameName;
  635. Procedure TestExternalClass_ReintroduceOverload;
  636. Procedure TestExternalClass_Inherited;
  637. Procedure TestExternalClass_PascalAncestorFail;
  638. Procedure TestExternalClass_NewInstance;
  639. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  640. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  641. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  642. Procedure TestExternalClass_JSFunctionPasDescendant;
  643. Procedure TestExternalClass_PascalProperty;
  644. Procedure TestExternalClass_TypeCastToRootClass;
  645. Procedure TestExternalClass_TypeCastToJSObject;
  646. Procedure TestExternalClass_TypeCastStringToExternalString;
  647. Procedure TestExternalClass_TypeCastToJSFunction;
  648. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  649. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  650. Procedure TestExternalClass_BracketAccessor;
  651. Procedure TestExternalClass_BracketAccessor_Call;
  652. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  653. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  654. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  655. Procedure TestExternalClass_BracketAccessor_MultiType;
  656. Procedure TestExternalClass_BracketAccessor_Index;
  657. Procedure TestExternalClass_ForInJSObject;
  658. Procedure TestExternalClass_ForInJSArray;
  659. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  660. Procedure TestExternalClass_NestedConstructor;
  661. // class interfaces
  662. Procedure TestClassInterface_Corba;
  663. Procedure TestClassInterface_Corba_ProcExternalFail;
  664. Procedure TestClassInterface_Corba_Overloads;
  665. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  666. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  667. Procedure TestClassInterface_Corba_AncestorImpl;
  668. Procedure TestClassInterface_Corba_ImplReintroduce;
  669. Procedure TestClassInterface_Corba_MethodResolution;
  670. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  671. Procedure TestClassInterface_Corba_MethodOverride;
  672. Procedure TestClassInterface_Corba_Delegation;
  673. Procedure TestClassInterface_Corba_DelegationStatic;
  674. Procedure TestClassInterface_Corba_Operators;
  675. Procedure TestClassInterface_Corba_Args;
  676. Procedure TestClassInterface_Corba_ForIn;
  677. Procedure TestClassInterface_Corba_ArrayOfIntf;
  678. Procedure TestClassInterface_COM_AssignVar;
  679. Procedure TestClassInterface_COM_AssignArg;
  680. Procedure TestClassInterface_COM_FunctionResult;
  681. Procedure TestClassInterface_COM_InheritedFuncResult;
  682. Procedure TestClassInterface_COM_IsAsTypeCasts;
  683. Procedure TestClassInterface_COM_PassAsArg;
  684. Procedure TestClassInterface_COM_PassToUntypedParam;
  685. Procedure TestClassInterface_COM_FunctionInExpr;
  686. Procedure TestClassInterface_COM_Property;
  687. Procedure TestClassInterface_COM_IntfProperty;
  688. Procedure TestClassInterface_COM_Delegation;
  689. Procedure TestClassInterface_COM_With;
  690. Procedure TestClassInterface_COM_ForIn;
  691. Procedure TestClassInterface_COM_ArrayOfIntf;
  692. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  693. Procedure TestClassInterface_COM_RecordIntfFail;
  694. Procedure TestClassInterface_COM_UnitInitialization;
  695. Procedure TestClassInterface_Corba_GUID;
  696. Procedure TestClassInterface_Corba_GUIDProperty;
  697. // helpers
  698. Procedure TestClassHelper_ClassVar;
  699. Procedure TestClassHelper_Method_AccessInstanceFields;
  700. Procedure TestClassHelper_Method_Call;
  701. Procedure TestClassHelper_Method_Nested_Call;
  702. Procedure TestClassHelper_ClassMethod_Call;
  703. Procedure TestClassHelper_ClassOf;
  704. Procedure TestClassHelper_MethodRefObjFPC;
  705. Procedure TestClassHelper_Constructor;
  706. Procedure TestClassHelper_InheritedObjFPC;
  707. Procedure TestClassHelper_Property;
  708. Procedure TestClassHelper_Property_Array;
  709. Procedure TestClassHelper_Property_Array_Default;
  710. Procedure TestClassHelper_Property_Array_DefaultDefault;
  711. Procedure TestClassHelper_ClassProperty;
  712. Procedure TestClassHelper_ClassPropertyStatic;
  713. Procedure TestClassHelper_ClassProperty_Array;
  714. Procedure TestClassHelper_ForIn;
  715. Procedure TestClassHelper_PassProperty;
  716. Procedure TestExtClassHelper_ClassVar;
  717. Procedure TestExtClassHelper_Method_Call;
  718. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  719. Procedure TestRecordHelper_ClassVar;
  720. Procedure TestRecordHelper_Method_Call;
  721. Procedure TestRecordHelper_Constructor;
  722. Procedure TestTypeHelper_ClassVar;
  723. Procedure TestTypeHelper_PassResultElement;
  724. Procedure TestTypeHelper_PassArgs;
  725. Procedure TestTypeHelper_PassVarConst;
  726. Procedure TestTypeHelper_PassFuncResult;
  727. Procedure TestTypeHelper_PassPropertyField;
  728. Procedure TestTypeHelper_PassPropertyGetter;
  729. Procedure TestTypeHelper_PassClassPropertyField;
  730. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  731. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  732. Procedure TestTypeHelper_Property;
  733. Procedure TestTypeHelper_Property_Array;
  734. Procedure TestTypeHelper_ClassProperty;
  735. Procedure TestTypeHelper_ClassProperty_Array;
  736. Procedure TestTypeHelper_ClassMethod;
  737. Procedure TestTypeHelper_ExtClassMethodFail;
  738. Procedure TestTypeHelper_Constructor;
  739. Procedure TestTypeHelper_Word;
  740. Procedure TestTypeHelper_Boolean;
  741. Procedure TestTypeHelper_WordBool;
  742. Procedure TestTypeHelper_Double;
  743. Procedure TestTypeHelper_NativeInt;
  744. Procedure TestTypeHelper_StringChar;
  745. Procedure TestTypeHelper_JSValue;
  746. Procedure TestTypeHelper_Array;
  747. Procedure TestTypeHelper_EnumType;
  748. Procedure TestTypeHelper_SetType;
  749. Procedure TestTypeHelper_InterfaceType;
  750. Procedure TestTypeHelper_NestedSelf;
  751. // proc types
  752. Procedure TestProcType;
  753. Procedure TestProcType_Arg;
  754. Procedure TestProcType_FunctionFPC;
  755. Procedure TestProcType_FunctionDelphi;
  756. Procedure TestProcType_ProcedureDelphi;
  757. Procedure TestProcType_AsParam;
  758. Procedure TestProcType_MethodFPC;
  759. Procedure TestProcType_MethodDelphi;
  760. Procedure TestProcType_PropertyFPC;
  761. Procedure TestProcType_PropertyDelphi;
  762. Procedure TestProcType_WithClassInstDoPropertyFPC;
  763. Procedure TestProcType_Nested;
  764. Procedure TestProcType_NestedOfObject;
  765. Procedure TestProcType_ReferenceToProc;
  766. Procedure TestProcType_ReferenceToMethod;
  767. Procedure TestProcType_Typecast;
  768. Procedure TestProcType_PassProcToUntyped;
  769. Procedure TestProcType_PassProcToArray;
  770. Procedure TestProcType_SafeCallObjFPC;
  771. Procedure TestProcType_SafeCallDelphi;
  772. Procedure TestProcType_SafeCall_Arg;
  773. // pointer
  774. Procedure TestPointer;
  775. Procedure TestPointer_Proc;
  776. Procedure TestPointer_AssignRecordFail;
  777. Procedure TestPointer_AssignStaticArrayFail;
  778. Procedure TestPointer_TypeCastJSValueToPointer;
  779. Procedure TestPointer_NonRecordFail;
  780. Procedure TestPointer_AnonymousArgTypeFail;
  781. Procedure TestPointer_AnonymousVarTypeFail;
  782. Procedure TestPointer_AnonymousResultTypeFail;
  783. Procedure TestPointer_AddrOperatorFail;
  784. Procedure TestPointer_ArrayParamsFail;
  785. Procedure TestPointer_PointerAddFail;
  786. Procedure TestPointer_IncPointerFail;
  787. Procedure TestPointer_Record;
  788. Procedure TestPointer_RecordArg;
  789. // jsvalue
  790. Procedure TestJSValue_AssignToJSValue;
  791. Procedure TestJSValue_TypeCastToBaseType;
  792. Procedure TestJSValue_TypecastToJSValue;
  793. Procedure TestJSValue_Equal;
  794. Procedure TestJSValue_If;
  795. Procedure TestJSValue_Not;
  796. Procedure TestJSValue_Enum;
  797. Procedure TestJSValue_ClassInstance;
  798. Procedure TestJSValue_ClassOf;
  799. Procedure TestJSValue_ArrayOfJSValue;
  800. Procedure TestJSValue_ArrayLit;
  801. Procedure TestJSValue_Params;
  802. Procedure TestJSValue_UntypedParam;
  803. Procedure TestJSValue_FuncResultType;
  804. Procedure TestJSValue_ProcType_Assign;
  805. Procedure TestJSValue_ProcType_Equal;
  806. Procedure TestJSValue_ProcType_Param;
  807. Procedure TestJSValue_AssignToPointerFail;
  808. Procedure TestJSValue_OverloadDouble;
  809. Procedure TestJSValue_OverloadNativeInt;
  810. Procedure TestJSValue_OverloadWord;
  811. Procedure TestJSValue_OverloadString;
  812. Procedure TestJSValue_OverloadChar;
  813. Procedure TestJSValue_OverloadPointer;
  814. Procedure TestJSValue_ForIn;
  815. // RTTI
  816. Procedure TestRTTI_IntRange;
  817. Procedure TestRTTI_Double;
  818. Procedure TestRTTI_ProcType;
  819. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  820. Procedure TestRTTI_ProcTypeAnonymous;
  821. Procedure TestRTTI_EnumAndSetType;
  822. Procedure TestRTTI_EnumRange;
  823. Procedure TestRTTI_AnonymousEnumType;
  824. Procedure TestRTTI_StaticArray;
  825. Procedure TestRTTI_DynArray;
  826. Procedure TestRTTI_ArrayNestedAnonymous;
  827. Procedure TestRTTI_PublishedMethodOverloadFail;
  828. Procedure TestRTTI_PublishedMethodHideNoHint;
  829. Procedure TestRTTI_PublishedMethodExternalFail;
  830. Procedure TestRTTI_PublishedClassPropertyFail;
  831. Procedure TestRTTI_PublishedClassFieldFail;
  832. Procedure TestRTTI_PublishedFieldExternalFail;
  833. Procedure TestRTTI_Class_Field;
  834. Procedure TestRTTI_Class_Method;
  835. Procedure TestRTTI_Class_MethodArgFlags;
  836. Procedure TestRTTI_Class_Property;
  837. Procedure TestRTTI_Class_PropertyParams;
  838. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  839. Procedure TestRTTI_Class_OmitRTTI;
  840. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  841. Procedure TestRTTI_IndexModifier;
  842. Procedure TestRTTI_StoredModifier;
  843. Procedure TestRTTI_DefaultValue;
  844. Procedure TestRTTI_DefaultValueSet;
  845. Procedure TestRTTI_DefaultValueRangeType;
  846. Procedure TestRTTI_DefaultValueInherit;
  847. Procedure TestRTTI_OverrideMethod;
  848. Procedure TestRTTI_ReintroduceMethod;
  849. Procedure TestRTTI_OverloadProperty;
  850. // ToDo: array argument
  851. Procedure TestRTTI_ClassForward;
  852. Procedure TestRTTI_ClassOf;
  853. Procedure TestRTTI_Record;
  854. Procedure TestRTTI_RecordAnonymousArray;
  855. Procedure TestRTTI_Record_ClassVarType;
  856. Procedure TestRTTI_LocalTypes;
  857. Procedure TestRTTI_TypeInfo_BaseTypes;
  858. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  859. Procedure TestRTTI_TypeInfo_LocalFail;
  860. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  861. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  862. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  863. Procedure TestRTTI_TypeInfo_FunctionClassType;
  864. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  865. Procedure TestRTTI_Interface_Corba;
  866. Procedure TestRTTI_Interface_COM;
  867. Procedure TestRTTI_ClassHelper;
  868. Procedure TestRTTI_ExternalClass;
  869. Procedure TestRTTI_Unit;
  870. // Resourcestring
  871. Procedure TestResourcestringProgram;
  872. Procedure TestResourcestringUnit;
  873. Procedure TestResourcestringImplementation;
  874. // Attributes
  875. Procedure TestAttributes_Members;
  876. Procedure TestAttributes_Types;
  877. Procedure TestAttributes_HelperConstructor_Fail;
  878. Procedure TestAttributes_InterfacesList;
  879. // Assertions, checks
  880. procedure TestAssert;
  881. procedure TestAssert_SysUtils;
  882. procedure TestObjectChecks;
  883. procedure TestOverflowChecks_Int;
  884. procedure TestRangeChecks_AssignInt;
  885. procedure TestRangeChecks_AssignIntRange;
  886. procedure TestRangeChecks_AssignEnum;
  887. procedure TestRangeChecks_AssignEnumRange;
  888. procedure TestRangeChecks_AssignChar;
  889. procedure TestRangeChecks_AssignCharRange;
  890. procedure TestRangeChecks_ArrayIndex;
  891. procedure TestRangeChecks_ArrayOfRecIndex;
  892. procedure TestRangeChecks_StringIndex;
  893. procedure TestRangeChecks_TypecastInt;
  894. procedure TestRangeChecks_TypeHelperInt;
  895. procedure TestRangeChecks_AssignCurrency;
  896. // Async/AWait
  897. Procedure TestAsync_Proc;
  898. Procedure TestAsync_CallResultIsPromise;
  899. Procedure TestAsync_ConstructorFail;
  900. Procedure TestAsync_PropertyGetterFail;
  901. Procedure TestAwait_NonPromiseWithTypeFail;
  902. Procedure TestAwait_AsyncCallTypeMismatch;
  903. Procedure TestAWait_OutsideAsyncFail;
  904. Procedure TestAWait_IntegerFail;
  905. Procedure TestAWait_ExternalClassPromise;
  906. Procedure TestAWait_JSValue;
  907. Procedure TestAWait_Result;
  908. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  909. Procedure TestAsync_AnonymousProc;
  910. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  911. Procedure TestAsync_ProcType;
  912. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  913. Procedure TestAsync_Inherited;
  914. Procedure TestAsync_ClassInterface;
  915. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  916. Procedure TestAWait_ClassAs;
  917. // Library
  918. Procedure TestLibrary_Empty;
  919. Procedure TestLibrary_ExportFunc;
  920. Procedure TestLibrary_ExportFuncOverloadedFail;
  921. Procedure TestLibrary_Export_Index_Fail;
  922. Procedure TestLibrary_ExportVar;
  923. Procedure TestLibrary_ExportUnitFunc;
  924. end;
  925. function LinesToStr(Args: array of const): string;
  926. function ExtractFileUnitName(aFilename: string): string;
  927. function JSToStr(El: TJSElement): string;
  928. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  929. implementation
  930. function LinesToStr(Args: array of const): string;
  931. var
  932. s: String;
  933. i: Integer;
  934. begin
  935. s:='';
  936. for i:=Low(Args) to High(Args) do
  937. case Args[i].VType of
  938. vtChar: s += Args[i].VChar+LineEnding;
  939. vtString: s += Args[i].VString^+LineEnding;
  940. vtPChar: s += Args[i].VPChar+LineEnding;
  941. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  942. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  943. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  944. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  945. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  946. end;
  947. Result:=s;
  948. end;
  949. function ExtractFileUnitName(aFilename: string): string;
  950. var
  951. p: Integer;
  952. begin
  953. Result:=ExtractFileName(aFilename);
  954. if Result='' then exit;
  955. for p:=length(Result) downto 1 do
  956. case Result[p] of
  957. '/','\': exit;
  958. '.':
  959. begin
  960. Delete(Result,p,length(Result));
  961. exit;
  962. end;
  963. end;
  964. end;
  965. function JSToStr(El: TJSElement): string;
  966. var
  967. aWriter: TBufferWriter;
  968. aJSWriter: TJSWriter;
  969. begin
  970. aJSWriter:=nil;
  971. aWriter:=TBufferWriter.Create(1000);
  972. try
  973. aJSWriter:=TJSWriter.Create(aWriter);
  974. aJSWriter.IndentSize:=2;
  975. aJSWriter.WriteJS(El);
  976. Result:=aWriter.AsString;
  977. finally
  978. aJSWriter.Free;
  979. aWriter.Free;
  980. end;
  981. end;
  982. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  983. // search diff, ignore changes in spaces
  984. const
  985. SpaceChars = [#9,#10,#13,' '];
  986. var
  987. ExpectedP, ActualP: PChar;
  988. function FindLineEnd(p: PChar): PChar;
  989. begin
  990. Result:=p;
  991. while not (Result^ in [#0,#10,#13]) do inc(Result);
  992. end;
  993. function FindLineStart(p, MinP: PChar): PChar;
  994. begin
  995. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  996. Result:=p;
  997. end;
  998. procedure SkipLineEnd(var p: PChar);
  999. begin
  1000. if p^ in [#10,#13] then
  1001. begin
  1002. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1003. inc(p,2)
  1004. else
  1005. inc(p);
  1006. end;
  1007. end;
  1008. function HasSpecialChar(s: string): boolean;
  1009. var
  1010. i: Integer;
  1011. begin
  1012. for i:=1 to length(s) do
  1013. if s[i] in [#0..#31,#127..#255] then
  1014. exit(true);
  1015. Result:=false;
  1016. end;
  1017. function HashSpecialChars(s: string): string;
  1018. var
  1019. i: Integer;
  1020. begin
  1021. Result:='';
  1022. for i:=1 to length(s) do
  1023. if s[i] in [#0..#31,#127..#255] then
  1024. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1025. else
  1026. Result:=Result+s[i];
  1027. end;
  1028. procedure DiffFound;
  1029. var
  1030. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1031. ExpLine, ActLine: String;
  1032. i, LineNo, DiffLineNo: Integer;
  1033. begin
  1034. writeln('Diff found "',Msg,'". Lines:');
  1035. // write correct lines
  1036. p:=PChar(Expected);
  1037. LineNo:=0;
  1038. DiffLineNo:=0;
  1039. repeat
  1040. StartPos:=p;
  1041. while not (p^ in [#0,#10,#13]) do inc(p);
  1042. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1043. SkipLineEnd(p);
  1044. inc(LineNo);
  1045. if (p<=ExpectedP) and (p^<>#0) then
  1046. begin
  1047. writeln('= ',ExpLine);
  1048. end else begin
  1049. // diff line
  1050. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1051. // write actual line
  1052. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1053. ActLineEndP:=FindLineEnd(ActualP);
  1054. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1055. writeln('- ',ActLine);
  1056. if HasSpecialChar(ActLine) then
  1057. writeln('- ',HashSpecialChars(ActLine));
  1058. // write expected line
  1059. writeln('+ ',ExpLine);
  1060. if HasSpecialChar(ExpLine) then
  1061. writeln('- ',HashSpecialChars(ExpLine));
  1062. // write empty line with pointer ^
  1063. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1064. writeln('^');
  1065. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1066. CheckSrcDiff:=false;
  1067. // write up to three following actual lines to get some context
  1068. for i:=1 to 3 do begin
  1069. ActLineStartP:=ActLineEndP;
  1070. SkipLineEnd(ActLineStartP);
  1071. if ActLineStartP^=#0 then break;
  1072. ActLineEndP:=FindLineEnd(ActLineStartP);
  1073. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1074. writeln('~ ',ActLine);
  1075. end;
  1076. exit;
  1077. end;
  1078. until p^=#0;
  1079. writeln('DiffFound Actual:-----------------------');
  1080. writeln(Actual);
  1081. writeln('DiffFound Expected:---------------------');
  1082. writeln(Expected);
  1083. writeln('DiffFound ------------------------------');
  1084. Msg:='diff found, but lines are the same, internal error';
  1085. CheckSrcDiff:=false;
  1086. end;
  1087. var
  1088. IsSpaceNeeded: Boolean;
  1089. LastChar, Quote: Char;
  1090. begin
  1091. Result:=true;
  1092. Msg:='';
  1093. if Expected='' then Expected:=' ';
  1094. if Actual='' then Actual:=' ';
  1095. ExpectedP:=PChar(Expected);
  1096. ActualP:=PChar(Actual);
  1097. repeat
  1098. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1099. case ExpectedP^ of
  1100. #0:
  1101. begin
  1102. // check that rest of Actual has only spaces
  1103. while ActualP^ in SpaceChars do inc(ActualP);
  1104. if ActualP^<>#0 then
  1105. begin
  1106. DiffFound;
  1107. exit;
  1108. end;
  1109. exit(true);
  1110. end;
  1111. ' ',#9,#10,#13:
  1112. begin
  1113. // skip space in Expected
  1114. IsSpaceNeeded:=false;
  1115. if ExpectedP>PChar(Expected) then
  1116. LastChar:=ExpectedP[-1]
  1117. else
  1118. LastChar:=#0;
  1119. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1120. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1121. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1122. IsSpaceNeeded:=true;
  1123. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1124. begin
  1125. DiffFound;
  1126. exit;
  1127. end;
  1128. while ActualP^ in SpaceChars do inc(ActualP);
  1129. end;
  1130. '''','"':
  1131. begin
  1132. while ActualP^ in SpaceChars do inc(ActualP);
  1133. if ExpectedP^<>ActualP^ then
  1134. begin
  1135. DiffFound;
  1136. exit;
  1137. end;
  1138. Quote:=ExpectedP^;
  1139. repeat
  1140. inc(ExpectedP);
  1141. inc(ActualP);
  1142. if ExpectedP^<>ActualP^ then
  1143. begin
  1144. DiffFound;
  1145. exit;
  1146. end;
  1147. if (ExpectedP^ in [#0,#10,#13]) then
  1148. break
  1149. else if (ExpectedP^=Quote) then
  1150. begin
  1151. inc(ExpectedP);
  1152. inc(ActualP);
  1153. break;
  1154. end;
  1155. until false;
  1156. end;
  1157. else
  1158. while ActualP^ in SpaceChars do inc(ActualP);
  1159. if ExpectedP^<>ActualP^ then
  1160. begin
  1161. DiffFound;
  1162. exit;
  1163. end;
  1164. inc(ExpectedP);
  1165. inc(ActualP);
  1166. end;
  1167. until false;
  1168. end;
  1169. { TTestEnginePasResolver }
  1170. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1171. begin
  1172. if FModule=AValue then Exit;
  1173. FModule:=AValue;
  1174. end;
  1175. destructor TTestEnginePasResolver.Destroy;
  1176. begin
  1177. FreeAndNil(FStreamResolver);
  1178. FreeAndNil(FParser);
  1179. FreeAndNil(FScanner);
  1180. FreeAndNil(FStreamResolver);
  1181. Module:=nil;
  1182. inherited Destroy;
  1183. end;
  1184. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1185. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1186. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1187. begin
  1188. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1189. TypeParams);
  1190. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1191. Module:=TPasModule(Result);
  1192. end;
  1193. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1194. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1195. begin
  1196. Result:=nil;
  1197. if InFilename<>'' then
  1198. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1199. if Assigned(OnFindUnit) then
  1200. Result:=OnFindUnit(AName);
  1201. if NameExpr=nil then ;
  1202. end;
  1203. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1204. begin
  1205. // do not parse recursively
  1206. // parse via the queue
  1207. if Section=nil then ;
  1208. end;
  1209. { TCustomTestModule }
  1210. procedure TCustomTestModule.FreeSrcMarkers;
  1211. var
  1212. aMarker, Last: PSrcMarker;
  1213. begin
  1214. aMarker:=FirstSrcMarker;
  1215. while aMarker<>nil do
  1216. begin
  1217. Last:=aMarker;
  1218. aMarker:=aMarker^.Next;
  1219. Dispose(Last);
  1220. end;
  1221. FirstSrcMarker:=nil;
  1222. LastSrcMarker:=nil;
  1223. end;
  1224. function TCustomTestModule.GetResolverCount: integer;
  1225. begin
  1226. Result:=FResolvers.Count;
  1227. end;
  1228. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1229. begin
  1230. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1231. end;
  1232. function TCustomTestModule.GetMsgCount: integer;
  1233. begin
  1234. Result:=FHintMsgs.Count;
  1235. end;
  1236. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1237. begin
  1238. Result:=TTestHintMessage(FHintMsgs[Index]);
  1239. end;
  1240. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1241. ): TPasModule;
  1242. var
  1243. DefNamespace: String;
  1244. begin
  1245. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1246. if (Pos('.',aUnitName)<1) then
  1247. begin
  1248. DefNamespace:=GetDefaultNamespace;
  1249. if DefNamespace<>'' then
  1250. begin
  1251. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1252. if Result<>nil then exit;
  1253. end;
  1254. end;
  1255. Result:=LoadUnit(aUnitName);
  1256. if Result<>nil then exit;
  1257. {$IFDEF VerbosePas2JS}
  1258. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1259. {$ENDIF}
  1260. Fail('can''t find unit "'+aUnitName+'"');
  1261. end;
  1262. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1263. var
  1264. aParser: TPasParser;
  1265. Item: TTestHintMessage;
  1266. begin
  1267. aParser:=Sender as TPasParser;
  1268. Item:=TTestHintMessage.Create;
  1269. Item.Id:=aParser.LastMsgNumber;
  1270. Item.MsgType:=aParser.LastMsgType;
  1271. Item.MsgNumber:=aParser.LastMsgNumber;
  1272. Item.Msg:=Msg;
  1273. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1274. {$IFDEF VerbosePas2JS}
  1275. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1276. {$ENDIF}
  1277. FHintMsgs.Add(Item);
  1278. end;
  1279. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1280. );
  1281. var
  1282. aResolver: TTestEnginePasResolver;
  1283. Item: TTestHintMessage;
  1284. begin
  1285. aResolver:=Sender as TTestEnginePasResolver;
  1286. Item:=TTestHintMessage.Create;
  1287. Item.Id:=aResolver.LastMsgId;
  1288. Item.MsgType:=aResolver.LastMsgType;
  1289. Item.MsgNumber:=aResolver.LastMsgNumber;
  1290. Item.Msg:=Msg;
  1291. Item.SourcePos:=aResolver.LastSourcePos;
  1292. {$IFDEF VerbosePas2JS}
  1293. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1294. {$ENDIF}
  1295. FHintMsgs.Add(Item);
  1296. end;
  1297. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1298. var
  1299. Item: TTestHintMessage;
  1300. aScanner: TPas2jsPasScanner;
  1301. begin
  1302. aScanner:=Sender as TPas2jsPasScanner;
  1303. Item:=TTestHintMessage.Create;
  1304. Item.Id:=aScanner.LastMsgNumber;
  1305. Item.MsgType:=aScanner.LastMsgType;
  1306. Item.MsgNumber:=aScanner.LastMsgNumber;
  1307. Item.Msg:=Msg;
  1308. Item.SourcePos:=aScanner.CurSourcePos;
  1309. {$IFDEF VerbosePas2JS}
  1310. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1311. {$ENDIF}
  1312. FHintMsgs.Add(Item);
  1313. end;
  1314. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1315. var
  1316. SubEl: TPasElement;
  1317. i: Integer;
  1318. procedure E(Msg: string);
  1319. var
  1320. s: String;
  1321. begin
  1322. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1323. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1324. writeln('ERROR: ',s);
  1325. Fail(s);
  1326. end;
  1327. begin
  1328. if arg=nil then ;
  1329. if El=nil then exit;
  1330. if El.Parent=El then
  1331. E('El.Parent=El='+GetObjName(El));
  1332. if El is TBinaryExpr then
  1333. begin
  1334. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1335. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1336. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1337. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1338. end
  1339. else if El is TParamsExpr then
  1340. begin
  1341. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1342. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1343. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1344. if TParamsExpr(El).Params[i].Parent<>El then
  1345. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1346. end
  1347. else if El is TProcedureExpr then
  1348. begin
  1349. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1350. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1351. end
  1352. else if El is TPasDeclarations then
  1353. begin
  1354. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1355. begin
  1356. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1357. if SubEl.Parent<>El then
  1358. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1359. end;
  1360. end
  1361. else if El is TPasImplBlock then
  1362. begin
  1363. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1364. begin
  1365. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1366. if SubEl.Parent<>El then
  1367. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1368. end;
  1369. end
  1370. else if El is TPasImplWithDo then
  1371. begin
  1372. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1373. begin
  1374. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1375. if SubEl.Parent<>El then
  1376. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1377. end;
  1378. end
  1379. else if El is TPasProcedure then
  1380. begin
  1381. if TPasProcedure(El).ProcType.Parent<>El then
  1382. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1383. end
  1384. else if El is TPasProcedureType then
  1385. begin
  1386. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1387. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1388. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1389. end;
  1390. end;
  1391. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1392. var
  1393. Data: PTestResolverReferenceData absolute FindData;
  1394. Line, Col: integer;
  1395. begin
  1396. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1397. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1398. if (Data^.Filename=El.SourceFilename)
  1399. and (Data^.Row=Line)
  1400. and (Data^.StartCol<=Col)
  1401. and (Data^.EndCol>=Col)
  1402. then
  1403. Data^.Found.Add(El);
  1404. end;
  1405. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1406. begin
  1407. if FWithTypeInfo=AValue then Exit;
  1408. FWithTypeInfo:=AValue;
  1409. if AValue then
  1410. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1411. else
  1412. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1413. end;
  1414. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1415. var
  1416. i: Integer;
  1417. CurEngine: TTestEnginePasResolver;
  1418. CurUnitName: String;
  1419. begin
  1420. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1421. Result:=nil;
  1422. if (Module.ClassType=TPasModule)
  1423. and (CompareText(Module.Name,aUnitName)=0) then
  1424. exit(Module);
  1425. for i:=0 to ResolverCount-1 do
  1426. begin
  1427. CurEngine:=Resolvers[i];
  1428. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1429. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1430. if CompareText(aUnitName,CurUnitName)=0 then
  1431. begin
  1432. Result:=CurEngine.Module;
  1433. if Result<>nil then exit;
  1434. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1435. FileResolver.FindSourceFile(aUnitName);
  1436. CurEngine.StreamResolver:=TStreamResolver.Create;
  1437. CurEngine.StreamResolver.OwnsStreams:=True;
  1438. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1439. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1440. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1441. InitScanner(CurEngine.Scanner);
  1442. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1443. CurEngine.Parser.Options:=po_tcmodules;
  1444. if CompareText(CurUnitName,'System')=0 then
  1445. CurEngine.Parser.ImplicitUses.Clear;
  1446. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1447. try
  1448. CurEngine.Parser.NextToken;
  1449. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1450. except
  1451. on E: Exception do
  1452. HandleException(E);
  1453. end;
  1454. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1455. Result:=CurEngine.Module;
  1456. exit;
  1457. end;
  1458. end;
  1459. end;
  1460. procedure TCustomTestModule.SetUp;
  1461. begin
  1462. {$IFDEF EnablePasTreeGlobalRefCount}
  1463. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1464. {$ENDIF}
  1465. if FResolvers<>nil then
  1466. begin
  1467. writeln('TCustomTestModule.SetUp FModules<>nil');
  1468. Halt;
  1469. end;
  1470. inherited SetUp;
  1471. FSkipTests:=false;
  1472. FWithTypeInfo:=false;
  1473. FSource:=TStringList.Create;
  1474. FHub:=TPas2JSResolverHub.Create(Self);
  1475. FResolvers:=TObjectList.Create(true);
  1476. FFilename:='test1.pp';
  1477. FFileResolver:=TStreamResolver.Create;
  1478. FFileResolver.OwnsStreams:=True;
  1479. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1480. InitScanner(FScanner);
  1481. FEngine:=AddModule(Filename);
  1482. FEngine.Scanner:=FScanner;
  1483. FScanner.Resolver:=FEngine;
  1484. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1485. FParser.OnLog:=@OnParserLog;
  1486. FEngine.Parser:=FParser;
  1487. Parser.Options:=po_tcmodules;
  1488. FModule:=Nil;
  1489. FConverter:=CreateConverter;
  1490. FExpectedErrorClass:=nil;
  1491. end;
  1492. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1493. var
  1494. Options: TPasToJsConverterOptions;
  1495. begin
  1496. Result:=TPasToJSConverter.Create;
  1497. Options:=co_tcmodules;
  1498. if WithTypeInfo then
  1499. Exclude(Options,coNoTypeInfo)
  1500. else
  1501. Include(Options,coNoTypeInfo);
  1502. Result.Options:=Options;
  1503. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1504. end;
  1505. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1506. begin
  1507. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1508. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1509. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1510. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1511. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1512. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1513. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1514. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1515. aScanner.OnLog:=@OnScannerLog;
  1516. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1517. end;
  1518. procedure TCustomTestModule.TearDown;
  1519. {$IFDEF CheckPasTreeRefCount}
  1520. var
  1521. El: TPasElement;
  1522. {$ENDIF}
  1523. var
  1524. i: Integer;
  1525. CurModule: TPasModule;
  1526. begin
  1527. FreeSrcMarkers;
  1528. FHintMsgs.Clear;
  1529. FHintMsgsGood.Clear;
  1530. FSkipTests:=false;
  1531. FWithTypeInfo:=false;
  1532. FJSRegModuleCall:=nil;
  1533. FJSModuleCallArgs:=nil;
  1534. FJSImplentationUses:=nil;
  1535. FJSInterfaceUses:=nil;
  1536. FJSModuleSrc:=nil;
  1537. FJSInitBody:=nil;
  1538. FreeAndNil(FJSSource);
  1539. FreeAndNil(FJSModule);
  1540. FreeAndNil(FConverter);
  1541. ResolverEngine.Clear;
  1542. FreeAndNil(FSource);
  1543. FreeAndNil(FFileResolver);
  1544. if FResolvers<>nil then
  1545. begin
  1546. for i:=0 to FResolvers.Count-1 do
  1547. begin
  1548. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1549. if CurModule=nil then continue;
  1550. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1551. end;
  1552. for i:=0 to FResolvers.Count-1 do
  1553. begin
  1554. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1555. if CurModule=nil then continue;
  1556. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1557. end;
  1558. FreeAndNil(FResolvers);
  1559. FModule:=nil;
  1560. FEngine:=nil;
  1561. end;
  1562. FreeAndNil(FHub);
  1563. inherited TearDown;
  1564. {$IFDEF EnablePasTreeGlobalRefCount}
  1565. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1566. begin
  1567. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1568. {$IFDEF CheckPasTreeRefCount}
  1569. El:=TPasElement.FirstRefEl;
  1570. while El<>nil do
  1571. begin
  1572. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1573. for i:=0 to El.RefIds.Count-1 do
  1574. writeln(' ',El.RefIds[i]);
  1575. El:=El.NextRefEl;
  1576. end;
  1577. {$ENDIF}
  1578. Halt;
  1579. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1580. end;
  1581. {$ENDIF}
  1582. end;
  1583. procedure TCustomTestModule.Add(Line: string);
  1584. begin
  1585. Source.Add(Line);
  1586. end;
  1587. procedure TCustomTestModule.Add(const Lines: array of string);
  1588. var
  1589. i: Integer;
  1590. begin
  1591. for i:=low(Lines) to high(Lines) do
  1592. Add(Lines[i]);
  1593. end;
  1594. procedure TCustomTestModule.StartParsing;
  1595. var
  1596. Src: String;
  1597. begin
  1598. Src:=Source.Text;
  1599. FEngine.Source:=Src;
  1600. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1601. Scanner.OpenFile(FileName);
  1602. Writeln('// Test : ',Self.TestName);
  1603. Writeln(Src);
  1604. end;
  1605. procedure TCustomTestModule.ParseModuleQueue;
  1606. var
  1607. i: Integer;
  1608. CurResolver: TTestEnginePasResolver;
  1609. Found: Boolean;
  1610. Section: TPasSection;
  1611. begin
  1612. // parse til exception or all Resolvers finished
  1613. while not SkipTests do
  1614. begin
  1615. Found:=false;
  1616. for i:=0 to ResolverCount-1 do
  1617. begin
  1618. CurResolver:=Resolvers[i];
  1619. if CurResolver.CurrentParser=nil then continue;
  1620. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1621. continue;
  1622. CurResolver.Parser.ParseContinue;
  1623. Found:=true;
  1624. break;
  1625. end;
  1626. if not Found then break;
  1627. end;
  1628. for i:=0 to ResolverCount-1 do
  1629. begin
  1630. CurResolver:=Resolvers[i];
  1631. if CurResolver.Parser=nil then
  1632. begin
  1633. if CurResolver.CurrentParser<>nil then
  1634. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1635. continue;
  1636. end;
  1637. if CurResolver.Parser.CurModule<>nil then
  1638. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1639. end;
  1640. end;
  1641. procedure TCustomTestModule.ParseModule;
  1642. begin
  1643. if SkipTests then exit;
  1644. FFirstPasStatement:=nil;
  1645. try
  1646. StartParsing;
  1647. Parser.ParseMain(FModule);
  1648. ParseModuleQueue;
  1649. except
  1650. on E: Exception do
  1651. HandleException(E);
  1652. end;
  1653. if SkipTests then exit;
  1654. AssertNotNull('Module resulted in Module',Module);
  1655. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1656. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1657. end;
  1658. procedure TCustomTestModule.ParseProgram;
  1659. begin
  1660. if SkipTests then exit;
  1661. ParseModule;
  1662. if SkipTests then exit;
  1663. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1664. FPasProgram:=TPasProgram(Module);
  1665. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1666. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1667. if (PasProgram.InitializationSection.Elements.Count>0) then
  1668. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1669. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1670. end;
  1671. procedure TCustomTestModule.ParseLibrary;
  1672. var
  1673. Init: TInitializationSection;
  1674. begin
  1675. if SkipTests then exit;
  1676. ParseModule;
  1677. if SkipTests then exit;
  1678. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1679. FPasLibrary:=TPasLibrary(Module);
  1680. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1681. Init:=PasLibrary.InitializationSection;
  1682. if (Init<>nil) and (Init.Elements.Count>0) then
  1683. if TObject(Init.Elements[0]) is TPasImplBlock then
  1684. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1685. end;
  1686. procedure TCustomTestModule.ParseUnit;
  1687. begin
  1688. if SkipTests then exit;
  1689. ParseModule;
  1690. if SkipTests then exit;
  1691. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1692. AssertNotNull('Has interface section',Module.InterfaceSection);
  1693. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1694. if (Module.InitializationSection<>nil)
  1695. and (Module.InitializationSection.Elements.Count>0)
  1696. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1697. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1698. end;
  1699. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1700. ): TTestEnginePasResolver;
  1701. var
  1702. i: Integer;
  1703. begin
  1704. for i:=0 to ResolverCount-1 do
  1705. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1706. exit(Resolvers[i]);
  1707. Result:=nil;
  1708. end;
  1709. function TCustomTestModule.AddModule(aFilename: string
  1710. ): TTestEnginePasResolver;
  1711. begin
  1712. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1713. if FindModuleWithFilename(aFilename)<>nil then
  1714. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1715. Result:=TTestEnginePasResolver.Create;
  1716. Result.Filename:=aFilename;
  1717. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1718. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1719. Result.OnLog:=@OnPasResolverLog;
  1720. Result.Hub:=Hub;
  1721. FResolvers.Add(Result);
  1722. end;
  1723. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1724. ): TTestEnginePasResolver;
  1725. begin
  1726. Result:=AddModule(aFilename);
  1727. Result.Source:=Src;
  1728. end;
  1729. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1730. ImplementationSrc: string): TTestEnginePasResolver;
  1731. var
  1732. Src: String;
  1733. begin
  1734. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1735. Src+=LineEnding;
  1736. Src+='interface'+LineEnding;
  1737. Src+=LineEnding;
  1738. Src+=InterfaceSrc;
  1739. Src+='implementation'+LineEnding;
  1740. Src+=LineEnding;
  1741. Src+=ImplementationSrc;
  1742. Src+='end.'+LineEnding;
  1743. Result:=AddModuleWithSrc(aFilename,Src);
  1744. end;
  1745. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1746. var
  1747. Intf, Impl: TStringList;
  1748. begin
  1749. Intf:=TStringList.Create;
  1750. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1751. // unit interface
  1752. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1753. Intf.Add('{$modeswitch externalclass}');
  1754. Intf.Add('type');
  1755. Intf.Add(' integer=longint;');
  1756. Intf.Add(' sizeint=nativeint;');
  1757. //'const',
  1758. //' LineEnding = #10;',
  1759. //' DirectorySeparator = ''/'';',
  1760. //' DriveSeparator = '''';',
  1761. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1762. //' AllowDriveSeparators : set of char = [];',
  1763. if supTObject in Parts then
  1764. Intf.AddStrings([
  1765. 'type',
  1766. ' TClass = class of TObject;',
  1767. ' TObject = class',
  1768. ' constructor Create;',
  1769. ' destructor Destroy; virtual;',
  1770. ' class function ClassType: TClass; assembler;',
  1771. ' class function ClassName: String; assembler;',
  1772. ' class function ClassNameIs(const Name: string): boolean;',
  1773. ' class function ClassParent: TClass; assembler;',
  1774. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1775. ' class function UnitName: String; assembler;',
  1776. ' procedure AfterConstruction; virtual;',
  1777. ' procedure BeforeDestruction;virtual;',
  1778. ' function Equals(Obj: TObject): boolean; virtual;',
  1779. ' function ToString: String; virtual;',
  1780. ' end;']);
  1781. if supTInterfacedObject in Parts then
  1782. Intf.AddStrings([
  1783. ' {$Interfaces COM}',
  1784. ' IUnknown = interface',
  1785. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1786. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1787. ' function _AddRef: Integer;',
  1788. ' function _Release: Integer;',
  1789. ' end;',
  1790. ' IInterface = IUnknown;',
  1791. ' TInterfacedObject = class(TObject,IUnknown)',
  1792. ' protected',
  1793. ' fRefCount: Integer;',
  1794. ' { implement methods of IUnknown }',
  1795. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1796. ' function _AddRef: Integer; virtual;',
  1797. ' function _Release: Integer; virtual;',
  1798. ' end;',
  1799. ' TInterfacedClass = class of TInterfacedObject;',
  1800. '',
  1801. '']);
  1802. if supTVarRec in Parts then
  1803. Intf.AddStrings([
  1804. 'const',
  1805. ' vtInteger = 0;',
  1806. ' vtBoolean = 1;',
  1807. ' vtJSValue = 19;',
  1808. 'type',
  1809. ' PVarRec = ^TVarRec;',
  1810. ' TVarRec = record',
  1811. ' VType : byte;',
  1812. ' VJSValue: JSValue;',
  1813. ' vInteger: longint external name ''VJSValue'';',
  1814. ' vBoolean: boolean external name ''VJSValue'';',
  1815. ' end;',
  1816. ' TVarRecArray = array of TVarRec;',
  1817. 'function VarRecs: TVarRecArray; varargs;',
  1818. '']);
  1819. if supTypeInfo in Parts then
  1820. begin
  1821. Intf.AddStrings([
  1822. 'type',
  1823. ' TTypeKind = (',
  1824. ' tkUnknown, // 0',
  1825. ' tkInteger, // 1',
  1826. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1827. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1828. ' tkEnumeration, // 4',
  1829. ' tkSet, // 5',
  1830. ' tkDouble, // 6',
  1831. ' tkBool, // 7',
  1832. ' tkProcVar, // 8 function or procedure',
  1833. ' tkMethod, // 9 proc var of object',
  1834. ' tkArray, // 10 static array',
  1835. ' tkDynArray, // 11',
  1836. ' tkRecord, // 12',
  1837. ' tkClass, // 13',
  1838. ' tkClassRef, // 14',
  1839. ' tkPointer, // 15',
  1840. ' tkJSValue, // 16',
  1841. ' tkRefToProcVar, // 17 variable of procedure type',
  1842. ' tkInterface, // 18',
  1843. ' //tkObject,',
  1844. ' //tkSString,tkLString,tkAString,tkWString,',
  1845. ' //tkVariant,',
  1846. ' //tkWChar,',
  1847. ' //tkInt64,',
  1848. ' //tkQWord,',
  1849. ' //tkInterfaceRaw,',
  1850. ' //tkUString,tkUChar,',
  1851. ' tkHelper, // 19',
  1852. ' //tkFile,',
  1853. ' tkExtClass // 20',
  1854. ' );',
  1855. ' TTypeKinds = set of TTypeKind;',
  1856. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1857. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1858. ' end;',
  1859. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1860. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1861. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1862. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1863. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1864. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1865. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1866. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1867. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1868. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1869. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1870. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1871. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1872. '']);
  1873. end;
  1874. if supWriteln in Parts then
  1875. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1876. Intf.Add('var');
  1877. Intf.Add(' ExitCode: Longint = 0;');
  1878. // unit implementation
  1879. Impl:=TStringList.Create;
  1880. if supTObject in Parts then
  1881. Impl.AddStrings([
  1882. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1883. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1884. 'asm',
  1885. 'end;',
  1886. 'constructor TObject.Create; begin end;',
  1887. 'destructor TObject.Destroy; begin end;',
  1888. 'class function TObject.ClassType: TClass; assembler;',
  1889. 'asm',
  1890. 'end;',
  1891. 'class function TObject.ClassName: String; assembler;',
  1892. 'asm',
  1893. 'end;',
  1894. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1895. 'begin',
  1896. ' Result:=SameText(Name,ClassName);',
  1897. 'end;',
  1898. 'class function TObject.ClassParent: TClass; assembler;',
  1899. 'asm',
  1900. 'end;',
  1901. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1902. 'asm',
  1903. 'end;',
  1904. 'class function TObject.UnitName: String; assembler;',
  1905. 'asm',
  1906. 'end;',
  1907. 'procedure TObject.AfterConstruction; begin end;',
  1908. 'procedure TObject.BeforeDestruction; begin end;',
  1909. 'function TObject.Equals(Obj: TObject): boolean;',
  1910. 'begin',
  1911. ' Result:=Obj=Self;',
  1912. 'end;',
  1913. 'function TObject.ToString: String;',
  1914. 'begin',
  1915. ' Result:=ClassName;',
  1916. 'end;'
  1917. ]);
  1918. if supTInterfacedObject in Parts then
  1919. Impl.AddStrings([
  1920. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1921. //'begin',
  1922. //'end;',
  1923. 'function TInterfacedObject._AddRef: Integer;',
  1924. 'begin',
  1925. 'end;',
  1926. 'function TInterfacedObject._Release: Integer;',
  1927. 'begin',
  1928. 'end;',
  1929. '']);
  1930. if supTVarRec in Parts then
  1931. Impl.AddStrings([
  1932. 'function VarRecs: TVarRecArray; varargs;',
  1933. 'var',
  1934. ' v: PVarRec;',
  1935. 'begin',
  1936. ' v^.VType:=1;',
  1937. ' v^.VJSValue:=2;',
  1938. 'end;',
  1939. '']);
  1940. try
  1941. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1942. finally
  1943. Intf.Free;
  1944. Impl.Free;
  1945. end;
  1946. end;
  1947. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1948. SystemUnitParts: TSystemUnitParts);
  1949. begin
  1950. if NeedSystemUnit then
  1951. AddSystemUnit(SystemUnitParts)
  1952. else
  1953. Parser.ImplicitUses.Clear;
  1954. Add('program '+ExtractFileUnitName(Filename)+';');
  1955. Add('');
  1956. end;
  1957. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1958. SystemUnitParts: TSystemUnitParts);
  1959. begin
  1960. if NeedSystemUnit then
  1961. AddSystemUnit(SystemUnitParts)
  1962. else
  1963. Parser.ImplicitUses.Clear;
  1964. Add('library '+ExtractFileUnitName(Filename)+';');
  1965. Add('');
  1966. end;
  1967. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1968. SystemUnitParts: TSystemUnitParts);
  1969. begin
  1970. if NeedSystemUnit then
  1971. AddSystemUnit(SystemUnitParts)
  1972. else
  1973. Parser.ImplicitUses.Clear;
  1974. Add('unit Test1;');
  1975. Add('');
  1976. end;
  1977. procedure TCustomTestModule.ConvertModule;
  1978. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1979. out UsesLit: TJSArrayLiteral);
  1980. var
  1981. i: Integer;
  1982. Item: TJSElement;
  1983. Lit: TJSLiteral;
  1984. begin
  1985. UsesLit:=nil;
  1986. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1987. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1988. exit; // null is ok
  1989. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1990. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1991. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1992. begin
  1993. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1994. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1995. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1996. Lit:=TJSLiteral(Item);
  1997. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1998. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1999. end;
  2000. end;
  2001. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2002. out Src: TJSSourceElements);
  2003. var
  2004. FunDecl: TJSFunctionDeclarationStatement;
  2005. FunDef: TJSFuncDef;
  2006. FunBody: TJSFunctionBody;
  2007. begin
  2008. Src:=nil;
  2009. AssertNotNull(ParamName,Arg.Expr);
  2010. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2011. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2012. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2013. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2014. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2015. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2016. AssertNotNull(ParamName+' body',FunDef.Body);
  2017. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2018. FunBody:=FunDef.Body as TJSFunctionBody;
  2019. AssertNotNull(ParamName+' body.A',FunBody.A);
  2020. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2021. Src:=FunBody.A as TJSSourceElements;
  2022. end;
  2023. var
  2024. ModuleNameExpr: TJSLiteral;
  2025. InitFunction: TJSFunctionDeclarationStatement;
  2026. InitAssign: TJSSimpleAssignStatement;
  2027. InitName: String;
  2028. LastNode, FirstNode: TJSElement;
  2029. Arg: TJSArrayLiteralElement;
  2030. IsProg, IsLib: Boolean;
  2031. begin
  2032. if SkipTests then exit;
  2033. IsProg:=false;
  2034. IsLib:=false;
  2035. if Module is TPasProgram then
  2036. IsProg:=true
  2037. else if Module is TPasLibrary then
  2038. IsLib:=true;
  2039. try
  2040. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2041. except
  2042. on E: Exception do
  2043. HandleException(E);
  2044. end;
  2045. if SkipTests then exit;
  2046. if ExpectedErrorClass<>nil then
  2047. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2048. FJSSource:=TStringList.Create;
  2049. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2050. {$IFDEF VerbosePas2JS}
  2051. writeln('TTestModule.ConvertModule JS:');
  2052. write(FJSSource.Text);
  2053. {$ENDIF}
  2054. // rtl.module(...
  2055. if JSModule.Statements.Count<1 then
  2056. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2057. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2058. AssertNotNull('register module call',FirstNode);
  2059. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2060. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2061. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2062. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2063. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2064. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2065. // parameter 'unitname'
  2066. if JSModuleCallArgs.Elements.Count<1 then
  2067. Fail('rtl.module first param unit missing');
  2068. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2069. AssertNotNull('module name param',Arg.Expr);
  2070. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2071. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2072. if IsProg then
  2073. begin
  2074. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2075. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2076. end
  2077. else if IsLib then
  2078. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2079. else
  2080. begin
  2081. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2082. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2083. end;
  2084. // main uses section
  2085. if JSModuleCallArgs.Elements.Count<2 then
  2086. Fail('rtl.module second param main uses missing');
  2087. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2088. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2089. // program/library/interface function()
  2090. if JSModuleCallArgs.Elements.Count<3 then
  2091. Fail('rtl.module third param intf-function missing');
  2092. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2093. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2094. // search for $mod.$init or $mod.$main - the last statement
  2095. if IsProg or IsLib then
  2096. begin
  2097. InitName:='$main';
  2098. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2099. end
  2100. else
  2101. InitName:='$init';
  2102. InitAssign:=nil;
  2103. InitFunction:=nil;
  2104. FJSInitBody:=nil;
  2105. if JSModuleSrc.Statements.Count>0 then
  2106. begin
  2107. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2108. if LastNode is TJSSimpleAssignStatement then
  2109. begin
  2110. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2111. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2112. begin
  2113. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2114. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2115. end
  2116. else if IsProg or IsLib then
  2117. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2118. end;
  2119. end;
  2120. // optional: implementation uses section
  2121. if JSModuleCallArgs.Elements.Count<4 then
  2122. exit;
  2123. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2124. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2125. end;
  2126. procedure TCustomTestModule.ConvertProgram;
  2127. begin
  2128. Add('end.');
  2129. ParseProgram;
  2130. ConvertModule;
  2131. end;
  2132. procedure TCustomTestModule.ConvertLibrary;
  2133. begin
  2134. Add('end.');
  2135. ParseLibrary;
  2136. ConvertModule;
  2137. end;
  2138. procedure TCustomTestModule.ConvertUnit;
  2139. begin
  2140. Add('end.');
  2141. ParseUnit;
  2142. ConvertModule;
  2143. end;
  2144. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2145. begin
  2146. Result:=tcmodules.JSToStr(El);
  2147. end;
  2148. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2149. DottedName: string);
  2150. begin
  2151. if DottedName='' then
  2152. begin
  2153. AssertNull(Msg,El);
  2154. end
  2155. else
  2156. begin
  2157. AssertNotNull(Msg,El);
  2158. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2159. end;
  2160. end;
  2161. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2162. begin
  2163. if El=nil then
  2164. Result:=''
  2165. else if El is TJSPrimaryExpressionIdent then
  2166. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2167. else if El is TJSDotMemberExpression then
  2168. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2169. else
  2170. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2171. end;
  2172. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2173. InitStatements: string; ImplStatements: string);
  2174. var
  2175. ActualSrc, ExpectedSrc, InitName: String;
  2176. IsProg, IsLib: Boolean;
  2177. begin
  2178. ActualSrc:=JSToStr(JSModuleSrc);
  2179. if coUseStrict in Converter.Options then
  2180. ExpectedSrc:='"use strict";'+LineEnding
  2181. else
  2182. ExpectedSrc:='';
  2183. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2184. ExpectedSrc:=ExpectedSrc+Statements;
  2185. // unit implementation
  2186. if (Trim(ImplStatements)<>'') then
  2187. ExpectedSrc:=ExpectedSrc+LineEnding
  2188. +'$mod.$implcode = function () {'+LineEnding
  2189. +ImplStatements
  2190. +'};'+LineEnding;
  2191. // program main or unit initialization
  2192. IsProg:=false;
  2193. IsLib:=false;
  2194. if Module is TPasProgram then
  2195. IsProg:=true
  2196. else if Module is TPasLibrary then
  2197. IsLib:=true;
  2198. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2199. begin
  2200. if IsProg or IsLib then
  2201. InitName:='$main'
  2202. else
  2203. InitName:='$init';
  2204. ExpectedSrc:=ExpectedSrc+LineEnding
  2205. +'$mod.'+InitName+' = function () {'+LineEnding
  2206. +InitStatements
  2207. +'};'+LineEnding;
  2208. end;
  2209. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2210. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2211. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2212. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2213. end;
  2214. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2215. var
  2216. ActualSrc: String;
  2217. begin
  2218. ActualSrc:=JSToStr(JSModule);
  2219. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2220. end;
  2221. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2222. // search diff, ignore changes in spaces
  2223. var
  2224. s: string;
  2225. begin
  2226. if CheckSrcDiff(Expected,Actual,s) then exit;
  2227. Fail(Msg+': '+s);
  2228. end;
  2229. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2230. var
  2231. aResolver: TTestEnginePasResolver;
  2232. aConverter: TPasToJSConverter;
  2233. aJSModule: TJSSourceElements;
  2234. ActualSrc: String;
  2235. begin
  2236. aResolver:=GetResolver(Filename);
  2237. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2238. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2239. {$IFDEF VerbosePas2JS}
  2240. writeln('CheckUnit '+Filename+' converting ...');
  2241. {$ENDIF}
  2242. aConverter:=CreateConverter;
  2243. aJSModule:=nil;
  2244. try
  2245. try
  2246. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2247. except
  2248. on E: Exception do
  2249. HandleException(E);
  2250. end;
  2251. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2252. {$IFDEF VerbosePas2JS}
  2253. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2254. write(aResolver.Source);
  2255. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2256. write(ActualSrc);
  2257. {$ENDIF}
  2258. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2259. finally
  2260. aJSModule.Free;
  2261. aConverter.Free;
  2262. end;
  2263. end;
  2264. procedure TCustomTestModule.CheckReferenceDirectives;
  2265. var
  2266. CurFilename: string;
  2267. LineNumber: Integer;
  2268. SrcLine: String;
  2269. CommentStartP, CommentEndP: PChar;
  2270. procedure RaiseError(Msg: string; p: PChar);
  2271. begin
  2272. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2273. end;
  2274. procedure AddMarker(Marker: PSrcMarker);
  2275. begin
  2276. if LastSrcMarker<>nil then
  2277. LastSrcMarker^.Next:=Marker
  2278. else
  2279. FirstSrcMarker:=Marker;
  2280. LastSrcMarker:=Marker;
  2281. end;
  2282. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2283. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2284. begin
  2285. New(Result);
  2286. Result^.Kind:=Kind;
  2287. Result^.Filename:=aFilename;
  2288. Result^.Row:=aLine;
  2289. Result^.StartCol:=aStartCol;
  2290. Result^.EndCol:=aEndCol;
  2291. Result^.Identifier:=Identifier;
  2292. Result^.Next:=nil;
  2293. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2294. AddMarker(Result);
  2295. end;
  2296. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2297. const Identifier: string): PSrcMarker;
  2298. var
  2299. TokenStart, p: PChar;
  2300. begin
  2301. p:=CommentEndP;
  2302. ReadNextPascalToken(p,TokenStart,false,false);
  2303. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2304. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2305. end;
  2306. function ReadIdentifier(var p: PChar): string;
  2307. var
  2308. StartP: PChar;
  2309. begin
  2310. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2311. RaiseError('identifier expected',p);
  2312. StartP:=p;
  2313. inc(p);
  2314. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2315. Result:='';
  2316. SetLength(Result,p-StartP);
  2317. Move(StartP^,Result[1],length(Result));
  2318. end;
  2319. procedure AddLabel;
  2320. var
  2321. Identifier: String;
  2322. p: PChar;
  2323. begin
  2324. p:=CommentStartP+2;
  2325. Identifier:=ReadIdentifier(p);
  2326. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2327. if FindSrcLabel(Identifier)<>nil then
  2328. RaiseError('duplicate label "'+Identifier+'"',p);
  2329. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2330. end;
  2331. procedure AddResolverReference;
  2332. var
  2333. Identifier: String;
  2334. p: PChar;
  2335. begin
  2336. p:=CommentStartP+2;
  2337. Identifier:=ReadIdentifier(p);
  2338. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2339. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2340. end;
  2341. procedure AddDirectReference;
  2342. var
  2343. Identifier: String;
  2344. p: PChar;
  2345. begin
  2346. p:=CommentStartP+2;
  2347. Identifier:=ReadIdentifier(p);
  2348. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2349. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2350. end;
  2351. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2352. var
  2353. p: PChar;
  2354. IsDirective: Boolean;
  2355. begin
  2356. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2357. CurFilename:=aFilename;
  2358. // parse code, find all labels
  2359. LineNumber:=0;
  2360. while LineNumber<SrcLines.Count do
  2361. begin
  2362. inc(LineNumber);
  2363. SrcLine:=SrcLines[LineNumber-1];
  2364. if SrcLine='' then continue;
  2365. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2366. p:=PChar(SrcLine);
  2367. repeat
  2368. case p^ of
  2369. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2370. '{':
  2371. begin
  2372. CommentStartP:=p;
  2373. inc(p);
  2374. IsDirective:=p^ in ['#','@','='];
  2375. // skip to end of comment
  2376. repeat
  2377. case p^ of
  2378. #0:
  2379. if (p-PChar(SrcLine)=length(SrcLine)) then
  2380. begin
  2381. // multi line comment
  2382. if IsDirective then
  2383. RaiseError('directive missing closing bracket',CommentStartP);
  2384. repeat
  2385. inc(LineNumber);
  2386. if LineNumber>SrcLines.Count then exit;
  2387. SrcLine:=SrcLines[LineNumber-1];
  2388. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2389. until SrcLine<>'';
  2390. p:=PChar(SrcLine);
  2391. continue;
  2392. end;
  2393. '}':
  2394. begin
  2395. inc(p);
  2396. break;
  2397. end;
  2398. end;
  2399. inc(p);
  2400. until false;
  2401. CommentEndP:=p;
  2402. case CommentStartP[1] of
  2403. '#': AddLabel;
  2404. '@': AddResolverReference;
  2405. '=': AddDirectReference;
  2406. end;
  2407. p:=CommentEndP;
  2408. continue;
  2409. end;
  2410. '/':
  2411. if p[1]='/' then
  2412. break; // rest of line is comment -> skip
  2413. end;
  2414. inc(p);
  2415. until false;
  2416. end;
  2417. end;
  2418. procedure CheckResolverReference(aMarker: PSrcMarker);
  2419. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2420. var
  2421. aLabel: PSrcMarker;
  2422. ReferenceElements, LabelElements: TFPList;
  2423. i, j, aLine, aCol: Integer;
  2424. El, Ref, LabelEl: TPasElement;
  2425. begin
  2426. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2427. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2428. if aLabel=nil then
  2429. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2430. LabelElements:=nil;
  2431. ReferenceElements:=nil;
  2432. try
  2433. LabelElements:=FindElementsAt(aLabel);
  2434. ReferenceElements:=FindElementsAt(aMarker);
  2435. for i:=0 to ReferenceElements.Count-1 do
  2436. begin
  2437. El:=TPasElement(ReferenceElements[i]);
  2438. Ref:=nil;
  2439. if El.CustomData is TResolvedReference then
  2440. Ref:=TResolvedReference(El.CustomData).Declaration
  2441. else if El.CustomData is TPasPropertyScope then
  2442. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2443. else if El.CustomData is TPasSpecializeTypeData then
  2444. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2445. if Ref<>nil then
  2446. for j:=0 to LabelElements.Count-1 do
  2447. begin
  2448. LabelEl:=TPasElement(LabelElements[j]);
  2449. if Ref=LabelEl then
  2450. exit; // success
  2451. end;
  2452. end;
  2453. // failure write candidates
  2454. for i:=0 to ReferenceElements.Count-1 do
  2455. begin
  2456. El:=TPasElement(ReferenceElements[i]);
  2457. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2458. write(' El=',GetObjName(El));
  2459. if EL is TPrimitiveExpr then
  2460. begin
  2461. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2462. end;
  2463. Ref:=nil;
  2464. if El.CustomData is TResolvedReference then
  2465. Ref:=TResolvedReference(El.CustomData).Declaration
  2466. else if El.CustomData is TPasPropertyScope then
  2467. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2468. else if El.CustomData is TPasSpecializeTypeData then
  2469. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2470. if Ref<>nil then
  2471. begin
  2472. write(' Decl=',GetObjName(Ref));
  2473. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2474. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2475. end
  2476. else
  2477. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2478. writeln;
  2479. end;
  2480. for i:=0 to LabelElements.Count-1 do
  2481. begin
  2482. El:=TPasElement(LabelElements[i]);
  2483. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2484. write(' El=',GetObjName(El));
  2485. writeln;
  2486. end;
  2487. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2488. finally
  2489. LabelElements.Free;
  2490. ReferenceElements.Free;
  2491. end;
  2492. end;
  2493. procedure CheckDirectReference(aMarker: PSrcMarker);
  2494. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2495. var
  2496. aLabel: PSrcMarker;
  2497. ReferenceElements, LabelElements: TFPList;
  2498. i, LabelLine, LabelCol, j: Integer;
  2499. El, LabelEl: TPasElement;
  2500. DeclEl, TypeEl: TPasType;
  2501. begin
  2502. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2503. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2504. if aLabel=nil then
  2505. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2506. LabelElements:=nil;
  2507. ReferenceElements:=nil;
  2508. try
  2509. //writeln('CheckDirectReference finding elements at label ...');
  2510. LabelElements:=FindElementsAt(aLabel);
  2511. //writeln('CheckDirectReference finding elements at reference ...');
  2512. ReferenceElements:=FindElementsAt(aMarker);
  2513. for i:=0 to ReferenceElements.Count-1 do
  2514. begin
  2515. El:=TPasElement(ReferenceElements[i]);
  2516. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2517. if El.ClassType=TPasVariable then
  2518. begin
  2519. if TPasVariable(El).VarType=nil then
  2520. begin
  2521. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2522. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2523. end;
  2524. TypeEl:=TPasVariable(El).VarType;
  2525. for j:=0 to LabelElements.Count-1 do
  2526. begin
  2527. LabelEl:=TPasElement(LabelElements[j]);
  2528. if TypeEl=LabelEl then
  2529. exit; // success
  2530. end;
  2531. end
  2532. else if El is TPasAliasType then
  2533. begin
  2534. DeclEl:=TPasAliasType(El).DestType;
  2535. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2536. if (aLabel^.Filename=DeclEl.SourceFilename)
  2537. and (integer(aLabel^.Row)=LabelLine)
  2538. and (aLabel^.StartCol<=LabelCol)
  2539. and (aLabel^.EndCol>=LabelCol) then
  2540. exit; // success
  2541. end
  2542. else if El.ClassType=TPasArgument then
  2543. begin
  2544. TypeEl:=TPasArgument(El).ArgType;
  2545. for j:=0 to LabelElements.Count-1 do
  2546. begin
  2547. LabelEl:=TPasElement(LabelElements[j]);
  2548. if TypeEl=LabelEl then
  2549. exit; // success
  2550. end;
  2551. end;
  2552. end;
  2553. // failed -> show candidates
  2554. writeln('CheckDirectReference failed: Labels:');
  2555. for j:=0 to LabelElements.Count-1 do
  2556. begin
  2557. LabelEl:=TPasElement(LabelElements[j]);
  2558. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2559. end;
  2560. writeln('CheckDirectReference failed: References:');
  2561. for i:=0 to ReferenceElements.Count-1 do
  2562. begin
  2563. El:=TPasElement(ReferenceElements[i]);
  2564. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2565. //if EL is TPasVariable then
  2566. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2567. end;
  2568. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2569. finally
  2570. LabelElements.Free;
  2571. ReferenceElements.Free;
  2572. end;
  2573. end;
  2574. var
  2575. aMarker: PSrcMarker;
  2576. i: Integer;
  2577. SrcLines: TStringList;
  2578. begin
  2579. Module.ForEachCall(@OnCheckElementParent,nil);
  2580. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2581. // find all markers
  2582. for i:=0 to FileResolver.Streams.Count-1 do
  2583. begin
  2584. GetSrc(i,SrcLines,CurFilename);
  2585. ParseCode(SrcLines,CurFilename);
  2586. SrcLines.Free;
  2587. end;
  2588. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2589. // check references
  2590. aMarker:=FirstSrcMarker;
  2591. while aMarker<>nil do
  2592. begin
  2593. case aMarker^.Kind of
  2594. mkResolverReference: CheckResolverReference(aMarker);
  2595. mkDirectReference: CheckDirectReference(aMarker);
  2596. end;
  2597. aMarker:=aMarker^.Next;
  2598. end;
  2599. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2600. end;
  2601. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2602. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2603. var
  2604. i: Integer;
  2605. Item: TTestHintMessage;
  2606. Expected,Actual: string;
  2607. begin
  2608. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2609. for i:=0 to MsgCount-1 do
  2610. begin
  2611. Item:=Msgs[i];
  2612. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2613. if (Marker<>nil) then
  2614. begin
  2615. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2616. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2617. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2618. end;
  2619. // found
  2620. FHintMsgsGood.Add(Item);
  2621. str(Item.MsgType,Actual);
  2622. str(MsgType,Expected);
  2623. AssertEquals('MsgType',Expected,Actual);
  2624. exit;
  2625. end;
  2626. // needed message missing -> show emitted messages
  2627. WriteSources('',0,0);
  2628. for i:=0 to MsgCount-1 do
  2629. begin
  2630. Item:=Msgs[i];
  2631. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2632. ' ('+IntToStr(Item.MsgNumber),')');
  2633. if Marker<>nil then
  2634. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2635. writeln(' {',Item.Msg,'}');
  2636. end;
  2637. str(MsgType,Expected);
  2638. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2639. if Marker<>nil then
  2640. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2641. Actual:=Actual+' '+Msg;
  2642. Fail(Actual);
  2643. end;
  2644. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2645. );
  2646. var
  2647. i: Integer;
  2648. s, Txt: String;
  2649. Msg: TTestHintMessage;
  2650. begin
  2651. for i:=0 to MsgCount-1 do
  2652. begin
  2653. Msg:=Msgs[i];
  2654. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2655. s:='';
  2656. str(Msg.MsgType,s);
  2657. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2658. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2659. if WithSourcePos then
  2660. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2661. Txt:=Txt+' {'+Msg.Msg+'}';
  2662. Fail(Txt);
  2663. end;
  2664. end;
  2665. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2666. MsgNumber: integer);
  2667. begin
  2668. ExpectedErrorClass:=EScannerError;
  2669. ExpectedErrorMsg:=Msg;
  2670. ExpectedErrorNumber:=MsgNumber;
  2671. end;
  2672. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2673. MsgNumber: integer);
  2674. begin
  2675. ExpectedErrorClass:=EParserError;
  2676. ExpectedErrorMsg:=Msg;
  2677. ExpectedErrorNumber:=MsgNumber;
  2678. end;
  2679. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2680. MsgNumber: integer);
  2681. begin
  2682. ExpectedErrorClass:=EPasResolve;
  2683. ExpectedErrorMsg:=Msg;
  2684. ExpectedErrorNumber:=MsgNumber;
  2685. end;
  2686. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2687. MsgNumber: integer);
  2688. begin
  2689. ExpectedErrorClass:=EPas2JS;
  2690. ExpectedErrorMsg:=Msg;
  2691. ExpectedErrorNumber:=MsgNumber;
  2692. end;
  2693. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2694. var
  2695. MsgNumber: Integer;
  2696. Msg: String;
  2697. begin
  2698. Result:=false;
  2699. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2700. Msg:=E.Message;
  2701. if E is EPas2JS then
  2702. MsgNumber:=EPas2JS(E).MsgNumber
  2703. else if E is EPasResolve then
  2704. MsgNumber:=EPasResolve(E).MsgNumber
  2705. else if E is EParserError then
  2706. MsgNumber:=Parser.LastMsgNumber
  2707. else if E is EScannerError then
  2708. begin
  2709. MsgNumber:=Scanner.LastMsgNumber;
  2710. Msg:=Scanner.LastMsg;
  2711. end
  2712. else
  2713. MsgNumber:=0;
  2714. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2715. if Result then
  2716. SkipTests:=true;
  2717. end;
  2718. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2719. const aFilename: string; aRow, aCol: integer);
  2720. var
  2721. s: String;
  2722. begin
  2723. WriteSources(aFilename,aRow,aCol);
  2724. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2725. writeln('ERROR: ',s);
  2726. Fail(s);
  2727. end;
  2728. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2729. aMarker: PSrcMarker);
  2730. begin
  2731. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2732. end;
  2733. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2734. begin
  2735. if IsErrorExpected(E) then exit;
  2736. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2737. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2738. +' '+Scanner.CurFilename
  2739. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2740. FailException(E);
  2741. end;
  2742. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2743. begin
  2744. if IsErrorExpected(E) then exit;
  2745. WriteSources(E.Filename,E.Row,E.Column);
  2746. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2747. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2748. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2749. );
  2750. FailException(E);
  2751. end;
  2752. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2753. var
  2754. P: TPasSourcePos;
  2755. begin
  2756. if IsErrorExpected(E) then exit;
  2757. P:=E.SourcePos;
  2758. WriteSources(P.FileName,P.Row,P.Column);
  2759. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2760. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2761. FailException(E);
  2762. end;
  2763. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2764. var
  2765. Row, Col: integer;
  2766. begin
  2767. if IsErrorExpected(E) then exit;
  2768. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2769. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2770. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2771. +' '+E.PasElement.SourceFilename
  2772. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2773. FailException(E);
  2774. end;
  2775. procedure TCustomTestModule.HandleException(E: Exception);
  2776. begin
  2777. if E is EScannerError then
  2778. HandleScannerError(EScannerError(E))
  2779. else if E is EParserError then
  2780. HandleParserError(EParserError(E))
  2781. else if E is EPasResolve then
  2782. HandlePasResolveError(EPasResolve(E))
  2783. else if E is EPas2JS then
  2784. HandlePas2JSError(EPas2JS(E))
  2785. else
  2786. begin
  2787. if IsErrorExpected(E) then exit;
  2788. if not (E is EAssertionFailedError) then
  2789. begin
  2790. WriteSources('',0,0);
  2791. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2792. end;
  2793. FailException(E);
  2794. end;
  2795. end;
  2796. procedure TCustomTestModule.FailException(E: Exception);
  2797. var
  2798. MsgNumber: Integer;
  2799. begin
  2800. if ExpectedErrorClass<>nil then
  2801. begin
  2802. if FExpectedErrorClass=E.ClassType then
  2803. begin
  2804. if E is EPas2JS then
  2805. MsgNumber:=EPas2JS(E).MsgNumber
  2806. else if E is EPasResolve then
  2807. MsgNumber:=EPasResolve(E).MsgNumber
  2808. else if E is EParserError then
  2809. MsgNumber:=Parser.LastMsgNumber
  2810. else if E is EScannerError then
  2811. MsgNumber:=Scanner.LastMsgNumber
  2812. else
  2813. MsgNumber:=0;
  2814. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2815. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2816. ExpectedErrorNumber,MsgNumber);
  2817. end else begin
  2818. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2819. end;
  2820. end;
  2821. Fail(E.Message);
  2822. end;
  2823. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2824. aCol: integer);
  2825. var
  2826. IsSrc: Boolean;
  2827. i, j: Integer;
  2828. SrcLines: TStringList;
  2829. Line: string;
  2830. aModule: TTestEnginePasResolver;
  2831. begin
  2832. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2833. for i:=0 to ResolverCount-1 do
  2834. begin
  2835. aModule:=Resolvers[i];
  2836. SrcLines:=TStringList.Create;
  2837. try
  2838. SrcLines.Text:=aModule.Source;
  2839. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2840. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2841. for j:=1 to SrcLines.Count do
  2842. begin
  2843. Line:=SrcLines[j-1];
  2844. if IsSrc and (j=aRow) then
  2845. begin
  2846. write('*');
  2847. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2848. end;
  2849. writeln(Format('%:4d: ',[j]),Line);
  2850. end;
  2851. finally
  2852. SrcLines.Free;
  2853. end;
  2854. end;
  2855. end;
  2856. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2857. var
  2858. i: Integer;
  2859. begin
  2860. for i:=0 to ResolverCount-1 do
  2861. if Filename=Resolvers[i].Filename then exit(i);
  2862. Result:=-1;
  2863. end;
  2864. function TCustomTestModule.GetResolver(const Filename: string
  2865. ): TTestEnginePasResolver;
  2866. var
  2867. i: Integer;
  2868. begin
  2869. i:=IndexOfResolver(Filename);
  2870. if i<0 then exit(nil);
  2871. Result:=Resolvers[i];
  2872. end;
  2873. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2874. out aFilename: string);
  2875. var
  2876. aStream: TStream;
  2877. begin
  2878. SrcLines:=TStringList.Create;
  2879. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2880. aStream.Position:=0;
  2881. SrcLines.LoadFromStream(aStream);
  2882. aFilename:=FileResolver.Streams[Index];
  2883. end;
  2884. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2885. aEndCol: integer): TFPList;
  2886. var
  2887. ok: Boolean;
  2888. FoundRefs: TTestResolverReferenceData;
  2889. i: Integer;
  2890. CurResolver: TTestEnginePasResolver;
  2891. begin
  2892. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2893. FoundRefs:=Default(TTestResolverReferenceData);
  2894. FoundRefs.Filename:=aFilename;
  2895. FoundRefs.Row:=aLine;
  2896. FoundRefs.StartCol:=aStartCol;
  2897. FoundRefs.EndCol:=aEndCol;
  2898. FoundRefs.Found:=TFPList.Create;
  2899. ok:=false;
  2900. try
  2901. // find all markers
  2902. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2903. for i:=0 to ResolverCount-1 do
  2904. begin
  2905. CurResolver:=Resolvers[i];
  2906. if CurResolver.Module=Module then continue;
  2907. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2908. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2909. end;
  2910. ok:=true;
  2911. finally
  2912. if not ok then
  2913. FreeAndNil(FoundRefs.Found);
  2914. end;
  2915. Result:=FoundRefs.Found;
  2916. FoundRefs.Found:=nil;
  2917. end;
  2918. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2919. ErrorOnNoElements: boolean): TFPList;
  2920. begin
  2921. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2922. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2923. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2924. end;
  2925. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2926. begin
  2927. Result:=FirstSrcMarker;
  2928. while Result<>nil do
  2929. begin
  2930. if (Result^.Kind=mkLabel)
  2931. and (CompareText(Result^.Identifier,Identifier)=0) then
  2932. exit;
  2933. Result:=Result^.Next;
  2934. end;
  2935. end;
  2936. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2937. ErrorOnNoElements: boolean): TFPList;
  2938. var
  2939. SrcLabel: PSrcMarker;
  2940. begin
  2941. SrcLabel:=FindSrcLabel(Identifier);
  2942. if SrcLabel=nil then
  2943. Fail('missing label "'+Identifier+'"');
  2944. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2945. end;
  2946. function TCustomTestModule.GetDefaultNamespace: string;
  2947. var
  2948. C: TClass;
  2949. begin
  2950. Result:='';
  2951. if FModule=nil then exit;
  2952. C:=FModule.ClassType;
  2953. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2954. Result:=ResolverEngine.DefaultNameSpace;
  2955. end;
  2956. constructor TCustomTestModule.Create;
  2957. begin
  2958. inherited Create;
  2959. FHintMsgs:=TObjectList.Create(true);
  2960. FHintMsgsGood:=TFPList.Create;
  2961. end;
  2962. destructor TCustomTestModule.Destroy;
  2963. begin
  2964. FreeAndNil(FHintMsgs);
  2965. FreeAndNil(FHintMsgsGood);
  2966. inherited Destroy;
  2967. end;
  2968. { TTestModule }
  2969. procedure TTestModule.TestReservedWords;
  2970. var
  2971. i: integer;
  2972. begin
  2973. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2974. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2975. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2976. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2977. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2978. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2979. end;
  2980. procedure TTestModule.TestEmptyProgram;
  2981. begin
  2982. StartProgram(false);
  2983. Add('begin');
  2984. ConvertProgram;
  2985. CheckSource('TestEmptyProgram','','');
  2986. end;
  2987. procedure TTestModule.TestEmptyProgramUseStrict;
  2988. begin
  2989. Converter.Options:=Converter.Options+[coUseStrict];
  2990. StartProgram(false);
  2991. Add('begin');
  2992. ConvertProgram;
  2993. CheckSource('TestEmptyProgramUseStrict','','');
  2994. end;
  2995. procedure TTestModule.TestEmptyUnit;
  2996. begin
  2997. StartUnit(false);
  2998. Add('interface');
  2999. Add('implementation');
  3000. ConvertUnit;
  3001. CheckSource('TestEmptyUnit',
  3002. LinesToStr([
  3003. ]),
  3004. '');
  3005. end;
  3006. procedure TTestModule.TestEmptyUnitUseStrict;
  3007. begin
  3008. Converter.Options:=Converter.Options+[coUseStrict];
  3009. StartUnit(false);
  3010. Add('interface');
  3011. Add('implementation');
  3012. ConvertUnit;
  3013. CheckSource('TestEmptyUnitUseStrict',
  3014. LinesToStr([
  3015. ''
  3016. ]),
  3017. '');
  3018. end;
  3019. procedure TTestModule.TestDottedUnitNames;
  3020. begin
  3021. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3022. LinesToStr([
  3023. 'var iV: longint;'
  3024. ]),
  3025. '');
  3026. FFilename:='ns1.test1.pp';
  3027. StartProgram(true);
  3028. Add('uses unIt2;');
  3029. Add('var');
  3030. Add(' i: longint;');
  3031. Add('begin');
  3032. Add(' i:=iv;');
  3033. Add(' i:=uNit2.iv;');
  3034. Add(' i:=Ns1.TEst1.i;');
  3035. ConvertProgram;
  3036. CheckSource('TestDottedUnitNames',
  3037. LinesToStr([
  3038. 'this.i = 0;',
  3039. '']),
  3040. LinesToStr([ // this.$init
  3041. '$mod.i = pas["NS1.Unit2"].iV;',
  3042. '$mod.i = pas["NS1.Unit2"].iV;',
  3043. '$mod.i = $mod.i;',
  3044. '']) );
  3045. end;
  3046. procedure TTestModule.TestDottedUnitNameImpl;
  3047. begin
  3048. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3049. LinesToStr([
  3050. 'type',
  3051. ' TObject = class end;',
  3052. ' TTestA = class',
  3053. ' end;'
  3054. ]),
  3055. LinesToStr(['uses TEST.UnitB;'])
  3056. );
  3057. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3058. LinesToStr([
  3059. 'uses TEST.UnitA;',
  3060. 'type TTestB = class(TTestA);'
  3061. ]),
  3062. ''
  3063. );
  3064. StartProgram(true);
  3065. Add('uses TEST.UnitA;');
  3066. Add('begin');
  3067. ConvertProgram;
  3068. CheckSource('TestDottedUnitNameImpl',
  3069. LinesToStr([
  3070. '']),
  3071. LinesToStr([ // this.$init
  3072. '']) );
  3073. CheckUnit('TEST.UnitA.pas',
  3074. LinesToStr([
  3075. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3076. ' var $mod = this;',
  3077. ' rtl.createClass(this, "TObject", null, function () {',
  3078. ' this.$init = function () {',
  3079. ' };',
  3080. ' this.$final = function () {',
  3081. ' };',
  3082. ' });',
  3083. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3084. ' });',
  3085. '}, ["TEST.UnitB"]);'
  3086. ]));
  3087. CheckUnit('TEST.UnitB.pas',
  3088. LinesToStr([
  3089. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3090. ' var $mod = this;',
  3091. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3092. ' });',
  3093. '});'
  3094. ]));
  3095. end;
  3096. procedure TTestModule.TestDottedUnitExpr;
  3097. begin
  3098. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3099. LinesToStr([
  3100. 'procedure DoIt;'
  3101. ]),
  3102. 'procedure DoIt; begin end;');
  3103. FFilename:='Ns1.SubNs1.Test1.pp';
  3104. StartProgram(true);
  3105. Add('uses Ns2.sUbnS2.unIt2;');
  3106. Add('var');
  3107. Add(' i: longint;');
  3108. Add('begin');
  3109. Add(' ns2.subns2.unit2.doit;');
  3110. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3111. ConvertProgram;
  3112. CheckSource('TestDottedUnitExpr',
  3113. LinesToStr([
  3114. 'this.i = 0;',
  3115. '']),
  3116. LinesToStr([ // this.$init
  3117. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3118. '$mod.i = $mod.i;',
  3119. '']) );
  3120. end;
  3121. procedure TTestModule.Test_ModeFPCFail;
  3122. begin
  3123. StartProgram(false);
  3124. Add('{$mode FPC}');
  3125. Add('begin');
  3126. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3127. ConvertProgram;
  3128. end;
  3129. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3130. begin
  3131. StartProgram(false);
  3132. Add('{$modeswitch cblocks-}');
  3133. Add('begin');
  3134. ConvertProgram;
  3135. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3136. CheckResolverUnexpectedHints();
  3137. end;
  3138. procedure TTestModule.TestUnit_UseSystem;
  3139. begin
  3140. StartUnit(true);
  3141. Add([
  3142. 'interface',
  3143. 'var i: integer;',
  3144. 'implementation']);
  3145. ConvertUnit;
  3146. CheckSource('TestUnit_UseSystem',
  3147. LinesToStr([
  3148. 'this.i = 0;',
  3149. '']),
  3150. LinesToStr([
  3151. '']) );
  3152. end;
  3153. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3154. begin
  3155. AddModuleWithIntfImplSrc('unit1.pp',
  3156. LinesToStr([
  3157. 'type number = longint;']),
  3158. LinesToStr([
  3159. 'uses test1;',
  3160. 'procedure DoIt;',
  3161. 'begin',
  3162. ' i:=3;',
  3163. 'end;']));
  3164. StartUnit(true);
  3165. Add([
  3166. 'interface',
  3167. 'uses unit1;',
  3168. 'var i: number;',
  3169. 'implementation']);
  3170. ConvertUnit;
  3171. CheckSource('TestUnit_Intf1Impl2Intf1',
  3172. LinesToStr([
  3173. 'this.i = 0;',
  3174. '']),
  3175. LinesToStr([
  3176. '']) );
  3177. end;
  3178. procedure TTestModule.TestIncludeVersion;
  3179. begin
  3180. StartProgram(false);
  3181. Add([
  3182. 'var',
  3183. ' s: string;',
  3184. ' i: word;',
  3185. 'begin',
  3186. ' s:={$I %line%};',
  3187. ' i:={$I %linenum%};',
  3188. ' s:={$I %currentroutine%};',
  3189. ' s:={$I %pas2jsversion%};',
  3190. ' s:={$I %pas2jstarget%};',
  3191. ' s:={$I %pas2jstargetos%};',
  3192. ' s:={$I %pas2jstargetcpu%};',
  3193. ' s:={$I %file%};',
  3194. '']);
  3195. ConvertProgram;
  3196. CheckSource('TestIncludeVersion',
  3197. LinesToStr([
  3198. 'this.s="";',
  3199. 'this.i = 0;']),
  3200. LinesToStr([
  3201. '$mod.s = "7";',
  3202. '$mod.i = 8;',
  3203. '$mod.s = "<anonymous>";',
  3204. '$mod.s = "Comp.Ver.tcmodules";',
  3205. '$mod.s = "Browser";',
  3206. '$mod.s = "Browser";',
  3207. '$mod.s = "ECMAScript5";',
  3208. '$mod.s = "test1.pp";',
  3209. '']));
  3210. end;
  3211. procedure TTestModule.TestVarInt;
  3212. begin
  3213. StartProgram(false);
  3214. Add('var MyI: longint;');
  3215. Add('begin');
  3216. ConvertProgram;
  3217. CheckSource('TestVarInt','this.MyI=0;','');
  3218. end;
  3219. procedure TTestModule.TestVarBaseTypes;
  3220. begin
  3221. StartProgram(false);
  3222. Add('var');
  3223. Add(' i: longint;');
  3224. Add(' s: string;');
  3225. Add(' c: char;');
  3226. Add(' b: boolean;');
  3227. Add(' d: double;');
  3228. Add(' i2: longint = 3;');
  3229. Add(' s2: string = ''foo'';');
  3230. Add(' c2: char = ''4'';');
  3231. Add(' b2: boolean = true;');
  3232. Add(' d2: double = 5.6;');
  3233. Add(' i3: longint = $707;');
  3234. Add(' i4: nativeint = 9007199254740991;');
  3235. Add(' i5: nativeint = -9007199254740991-1;');
  3236. Add(' i6: nativeint = $fffffffffffff;');
  3237. Add(' i7: nativeint = -$fffffffffffff-1;');
  3238. Add(' i8: byte = 00;');
  3239. Add(' u8: nativeuint = $fffffffffffff;');
  3240. Add(' u9: nativeuint = $0000000000000;');
  3241. Add(' u10: nativeuint = $00ff00;');
  3242. Add('begin');
  3243. ConvertProgram;
  3244. CheckSource('TestVarBaseTypes',
  3245. LinesToStr([
  3246. 'this.i = 0;',
  3247. 'this.s = "";',
  3248. 'this.c = "";',
  3249. 'this.b = false;',
  3250. 'this.d = 0.0;',
  3251. 'this.i2 = 3;',
  3252. 'this.s2 = "foo";',
  3253. 'this.c2 = "4";',
  3254. 'this.b2 = true;',
  3255. 'this.d2 = 5.6;',
  3256. 'this.i3 = 0x707;',
  3257. 'this.i4 = 9007199254740991;',
  3258. 'this.i5 = -9007199254740991-1;',
  3259. 'this.i6 = 0xfffffffffffff;',
  3260. 'this.i7 =-0xfffffffffffff-1;',
  3261. 'this.i8 = 0;',
  3262. 'this.u8 = 0xfffffffffffff;',
  3263. 'this.u9 = 0x0;',
  3264. 'this.u10 = 0xff00;'
  3265. ]),
  3266. '');
  3267. end;
  3268. procedure TTestModule.TestBaseTypeSingleFail;
  3269. begin
  3270. StartProgram(false);
  3271. Add('var s: single;');
  3272. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3273. ConvertProgram;
  3274. end;
  3275. procedure TTestModule.TestBaseTypeExtendedFail;
  3276. begin
  3277. StartProgram(false);
  3278. Add('var e: extended;');
  3279. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3280. ConvertProgram;
  3281. end;
  3282. procedure TTestModule.TestConstBaseTypes;
  3283. begin
  3284. StartProgram(false);
  3285. Add('const');
  3286. Add(' i: longint = 3;');
  3287. Add(' s: string = ''foo'';');
  3288. Add(' c: char = ''4'';');
  3289. Add(' b: boolean = true;');
  3290. Add(' d: double = 5.6;');
  3291. Add(' e = low(word);');
  3292. Add(' f = high(word);');
  3293. Add('begin');
  3294. ConvertProgram;
  3295. CheckSource('TestVarBaseTypes',
  3296. LinesToStr([
  3297. 'this.i=3;',
  3298. 'this.s="foo";',
  3299. 'this.c="4";',
  3300. 'this.b=true;',
  3301. 'this.d=5.6;',
  3302. 'this.e = 0;',
  3303. 'this.f = 65535;'
  3304. ]),
  3305. '');
  3306. end;
  3307. procedure TTestModule.TestAliasTypeRef;
  3308. begin
  3309. StartProgram(false);
  3310. Add('type');
  3311. Add(' a=longint;');
  3312. Add(' b=a;');
  3313. Add('var');
  3314. Add(' c: A;');
  3315. Add(' d: B;');
  3316. Add('begin');
  3317. ConvertProgram;
  3318. CheckSource('TestAliasTypeRef',
  3319. LinesToStr([ // statements
  3320. 'this.c = 0;',
  3321. 'this.d = 0;'
  3322. ]),
  3323. LinesToStr([ // this.$main
  3324. ''
  3325. ]));
  3326. end;
  3327. procedure TTestModule.TestTypeCast_BaseTypes;
  3328. begin
  3329. StartProgram(false);
  3330. Add([
  3331. 'var',
  3332. ' i: longint;',
  3333. ' b: boolean;',
  3334. ' d: double;',
  3335. ' s: string;',
  3336. ' c: char;',
  3337. 'begin',
  3338. ' i:=longint(i);',
  3339. ' i:=longint(b);',
  3340. ' b:=boolean(b);',
  3341. ' b:=boolean(i);',
  3342. ' d:=double(d);',
  3343. ' d:=double(i);',
  3344. ' s:=string(s);',
  3345. ' s:=string(c);',
  3346. ' c:=char(c);',
  3347. ' c:=char(i);',
  3348. ' c:=char(65);',
  3349. ' c:=char(#10);',
  3350. ' c:=char(#$E000);',
  3351. '']);
  3352. ConvertProgram;
  3353. CheckSource('TestAliasTypeRef',
  3354. LinesToStr([ // statements
  3355. 'this.i = 0;',
  3356. 'this.b = false;',
  3357. 'this.d = 0.0;',
  3358. 'this.s = "";',
  3359. 'this.c = "";',
  3360. '']),
  3361. LinesToStr([ // this.$main
  3362. '$mod.i = $mod.i;',
  3363. '$mod.i = ($mod.b ? 1 : 0);',
  3364. '$mod.b = $mod.b;',
  3365. '$mod.b = $mod.i != 0;',
  3366. '$mod.d = $mod.d;',
  3367. '$mod.d = $mod.i;',
  3368. '$mod.s = $mod.s;',
  3369. '$mod.s = $mod.c;',
  3370. '$mod.c = $mod.c;',
  3371. '$mod.c = String.fromCharCode($mod.i);',
  3372. '$mod.c = "A";',
  3373. '$mod.c = "\n";',
  3374. '$mod.c = "";',
  3375. '']));
  3376. end;
  3377. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3378. begin
  3379. StartProgram(false);
  3380. Add('type');
  3381. Add(' integer = longint;');
  3382. Add(' TYesNo = boolean;');
  3383. Add(' TFloat = double;');
  3384. Add(' TCaption = string;');
  3385. Add(' TChar = char;');
  3386. Add('var');
  3387. Add(' i: integer;');
  3388. Add(' b: TYesNo;');
  3389. Add(' d: TFloat;');
  3390. Add(' s: TCaption;');
  3391. Add(' c: TChar;');
  3392. Add('begin');
  3393. Add(' i:=integer(i);');
  3394. Add(' i:=integer(b);');
  3395. Add(' b:=TYesNo(b);');
  3396. Add(' b:=TYesNo(i);');
  3397. Add(' d:=TFloat(d);');
  3398. Add(' d:=TFloat(i);');
  3399. Add(' s:=TCaption(s);');
  3400. Add(' s:=TCaption(c);');
  3401. Add(' c:=TChar(c);');
  3402. ConvertProgram;
  3403. CheckSource('TestAliasTypeRef',
  3404. LinesToStr([ // statements
  3405. 'this.i = 0;',
  3406. 'this.b = false;',
  3407. 'this.d = 0.0;',
  3408. 'this.s = "";',
  3409. 'this.c = "";',
  3410. '']),
  3411. LinesToStr([ // this.$main
  3412. '$mod.i = $mod.i;',
  3413. '$mod.i = ($mod.b ? 1 : 0);',
  3414. '$mod.b = $mod.b;',
  3415. '$mod.b = $mod.i != 0;',
  3416. '$mod.d = $mod.d;',
  3417. '$mod.d = $mod.i;',
  3418. '$mod.s = $mod.s;',
  3419. '$mod.s = $mod.c;',
  3420. '$mod.c = $mod.c;',
  3421. '']));
  3422. end;
  3423. procedure TTestModule.TestEmptyProc;
  3424. begin
  3425. StartProgram(false);
  3426. Add('procedure Test;');
  3427. Add('begin');
  3428. Add('end;');
  3429. Add('begin');
  3430. ConvertProgram;
  3431. CheckSource('TestEmptyProc',
  3432. LinesToStr([ // statements
  3433. 'this.Test = function () {',
  3434. '};'
  3435. ]),
  3436. LinesToStr([ // this.$main
  3437. ''
  3438. ]));
  3439. end;
  3440. procedure TTestModule.TestProcOneParam;
  3441. begin
  3442. StartProgram(false);
  3443. Add('procedure ProcA(i: longint);');
  3444. Add('begin');
  3445. Add('end;');
  3446. Add('begin');
  3447. Add(' PROCA(3);');
  3448. ConvertProgram;
  3449. CheckSource('TestProcOneParam',
  3450. LinesToStr([ // statements
  3451. 'this.ProcA = function (i) {',
  3452. '};'
  3453. ]),
  3454. LinesToStr([ // this.$main
  3455. '$mod.ProcA(3);'
  3456. ]));
  3457. end;
  3458. procedure TTestModule.TestFunctionWithoutParams;
  3459. begin
  3460. StartProgram(false);
  3461. Add('function FuncA: longint;');
  3462. Add('begin');
  3463. Add('end;');
  3464. Add('var i: longint;');
  3465. Add('begin');
  3466. Add(' I:=FUNCA();');
  3467. Add(' I:=FUNCA;');
  3468. Add(' FUNCA();');
  3469. Add(' FUNCA;');
  3470. ConvertProgram;
  3471. CheckSource('TestProcWithoutParams',
  3472. LinesToStr([ // statements
  3473. 'this.FuncA = function () {',
  3474. ' var Result = 0;',
  3475. ' return Result;',
  3476. '};',
  3477. 'this.i=0;'
  3478. ]),
  3479. LinesToStr([ // this.$main
  3480. '$mod.i=$mod.FuncA();',
  3481. '$mod.i=$mod.FuncA();',
  3482. '$mod.FuncA();',
  3483. '$mod.FuncA();'
  3484. ]));
  3485. end;
  3486. procedure TTestModule.TestProcedureWithoutParams;
  3487. begin
  3488. StartProgram(false);
  3489. Add('procedure ProcA;');
  3490. Add('begin');
  3491. Add('end;');
  3492. Add('begin');
  3493. Add(' PROCA();');
  3494. Add(' PROCA;');
  3495. ConvertProgram;
  3496. CheckSource('TestProcWithoutParams',
  3497. LinesToStr([ // statements
  3498. 'this.ProcA = function () {',
  3499. '};'
  3500. ]),
  3501. LinesToStr([ // this.$main
  3502. '$mod.ProcA();',
  3503. '$mod.ProcA();'
  3504. ]));
  3505. end;
  3506. procedure TTestModule.TestIncDec;
  3507. begin
  3508. StartProgram(false);
  3509. Add([
  3510. 'procedure DoIt(var i: longint);',
  3511. 'begin',
  3512. ' inc(i);',
  3513. ' inc(i,2);',
  3514. 'end;',
  3515. 'var',
  3516. ' Bar: longint;',
  3517. 'begin',
  3518. ' inc(bar);',
  3519. ' inc(bar,2);',
  3520. ' dec(bar);',
  3521. ' dec(bar,3);',
  3522. '']);
  3523. ConvertProgram;
  3524. CheckSource('TestIncDec',
  3525. LinesToStr([ // statements
  3526. 'this.DoIt = function (i) {',
  3527. ' i.set(i.get()+1);',
  3528. ' i.set(i.get()+2);',
  3529. '};',
  3530. 'this.Bar = 0;'
  3531. ]),
  3532. LinesToStr([ // this.$main
  3533. '$mod.Bar+=1;',
  3534. '$mod.Bar+=2;',
  3535. '$mod.Bar-=1;',
  3536. '$mod.Bar-=3;'
  3537. ]));
  3538. end;
  3539. procedure TTestModule.TestLoHiFpcMode;
  3540. begin
  3541. StartProgram(false);
  3542. Add([
  3543. '{$mode objfpc}',
  3544. 'const',
  3545. ' LoByte1 = Lo(Word($1234));',
  3546. ' HiByte1 = Hi(Word($1234));',
  3547. ' LoByte2 = Lo(SmallInt($1234));',
  3548. ' HiByte2 = Hi(SmallInt($1234));',
  3549. ' LoWord1 = Lo($1234CDEF);',
  3550. ' HiWord1 = Hi($1234CDEF);',
  3551. ' LoWord2 = Lo(-$1234CDEF);',
  3552. ' HiWord2 = Hi(-$1234CDEF);',
  3553. ' lo4:byte=lo(byte($34));',
  3554. ' hi4:byte=hi(byte($34));',
  3555. ' lo5:byte=lo(shortint(-$34));',
  3556. ' hi5:byte=hi(shortint(-$34));',
  3557. ' lo6:longword=lo($123456789ABCD);',
  3558. ' hi6:longword=hi($123456789ABCD);',
  3559. ' lo7:longword=lo(-$123456789ABCD);',
  3560. ' hi7:longword=hi(-$123456789ABCD);',
  3561. 'var',
  3562. ' b: Byte;',
  3563. ' ss: shortint;',
  3564. ' w: Word;',
  3565. ' si: SmallInt;',
  3566. ' lw: LongWord;',
  3567. ' li: LongInt;',
  3568. ' b2: Byte;',
  3569. ' ni: nativeint;',
  3570. 'begin',
  3571. ' w := $1234;',
  3572. ' ss := -$12;',
  3573. ' b := lo(ss);',
  3574. ' b := HI(ss);',
  3575. ' b := lo(w);',
  3576. ' b := HI(w);',
  3577. ' b2 := lo(b);',
  3578. ' b2 := hi(b);',
  3579. ' lw := $1234CDEF;',
  3580. ' w := lo(lw);',
  3581. ' w := hi(lw);',
  3582. ' ni := $123456789ABCD;',
  3583. ' lw := lo(ni);',
  3584. ' lw := hi(ni);',
  3585. '']);
  3586. ConvertProgram;
  3587. CheckSource('TestLoHiFpcMode',
  3588. LinesToStr([ // statements
  3589. 'this.LoByte1 = 0x1234 & 0xFF;',
  3590. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3591. 'this.LoByte2 = 0x1234 & 0xFF;',
  3592. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3593. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3594. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3595. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3596. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3597. 'this.lo4 = 0x34 & 0xF;',
  3598. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3599. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3600. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3601. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3602. 'this.hi6 = 74565 >>> 0;',
  3603. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3604. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3605. 'this.b = 0;',
  3606. 'this.ss = 0;',
  3607. 'this.w = 0;',
  3608. 'this.si = 0;',
  3609. 'this.lw = 0;',
  3610. 'this.li = 0;',
  3611. 'this.b2 = 0;',
  3612. 'this.ni = 0;',
  3613. '']),
  3614. LinesToStr([ // this.$main
  3615. '$mod.w = 0x1234;',
  3616. '$mod.ss = -0x12;',
  3617. '$mod.b = $mod.ss & 0xFF;',
  3618. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3619. '$mod.b = $mod.w & 0xFF;',
  3620. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3621. '$mod.b2 = $mod.b & 0xF;',
  3622. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3623. '$mod.lw = 0x1234CDEF;',
  3624. '$mod.w = $mod.lw & 0xFFFF;',
  3625. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3626. '$mod.ni = 0x123456789ABCD;',
  3627. '$mod.lw = $mod.ni >>> 0;',
  3628. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3629. '']));
  3630. end;
  3631. procedure TTestModule.TestLoHiDelphiMode;
  3632. begin
  3633. StartProgram(false);
  3634. Add([
  3635. '{$mode delphi}',
  3636. 'const',
  3637. ' LoByte1 = Lo(Word($1234));',
  3638. ' HiByte1 = Hi(Word($1234));',
  3639. ' LoByte2 = Lo(SmallInt($1234));',
  3640. ' HiByte2 = Hi(SmallInt($1234));',
  3641. ' LoByte3 = Lo($1234CDEF);',
  3642. ' HiByte3 = Hi($1234CDEF);',
  3643. ' LoByte4 = Lo(-$1234CDEF);',
  3644. ' HiByte4 = Hi(-$1234CDEF);',
  3645. 'var',
  3646. ' b: Byte;',
  3647. ' w: Word;',
  3648. ' si: SmallInt;',
  3649. ' lw: LongWord;',
  3650. ' li: LongInt;',
  3651. 'begin',
  3652. ' w := $1234;',
  3653. ' b := lo(w);',
  3654. ' b := HI(w);',
  3655. ' lw := $1234CDEF;',
  3656. ' b := lo(lw);',
  3657. ' b := hi(lw);',
  3658. '']);
  3659. ConvertProgram;
  3660. CheckSource('TestLoHiDelphiMode',
  3661. LinesToStr([ // statements
  3662. 'this.LoByte1 = 0x1234 & 0xFF;',
  3663. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3664. 'this.LoByte2 = 0x1234 & 0xFF;',
  3665. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3666. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3667. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3668. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3669. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3670. 'this.b = 0;',
  3671. 'this.w = 0;',
  3672. 'this.si = 0;',
  3673. 'this.lw = 0;',
  3674. 'this.li = 0;'
  3675. ]),
  3676. LinesToStr([ // this.$main
  3677. '$mod.w = 0x1234;',
  3678. '$mod.b = $mod.w & 0xFF;',
  3679. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3680. '$mod.lw = 0x1234CDEF;',
  3681. '$mod.b = $mod.lw & 0xFF;',
  3682. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3683. ]));
  3684. end;
  3685. procedure TTestModule.TestAssignments;
  3686. begin
  3687. StartProgram(false);
  3688. Parser.Options:=Parser.Options+[po_cassignments];
  3689. Add('var');
  3690. Add(' Bar:longint;');
  3691. Add('begin');
  3692. Add(' bar:=3;');
  3693. Add(' bar+=4;');
  3694. Add(' bar-=5;');
  3695. Add(' bar*=6;');
  3696. ConvertProgram;
  3697. CheckSource('TestAssignments',
  3698. LinesToStr([ // statements
  3699. 'this.Bar = 0;'
  3700. ]),
  3701. LinesToStr([ // this.$main
  3702. '$mod.Bar=3;',
  3703. '$mod.Bar+=4;',
  3704. '$mod.Bar-=5;',
  3705. '$mod.Bar*=6;'
  3706. ]));
  3707. end;
  3708. procedure TTestModule.TestArithmeticOperators1;
  3709. begin
  3710. StartProgram(false);
  3711. Add('var');
  3712. Add(' vA,vB,vC:longint;');
  3713. Add('begin');
  3714. Add(' va:=1;');
  3715. Add(' vb:=va+va;');
  3716. Add(' vb:=va div vb;');
  3717. Add(' vb:=va mod vb;');
  3718. Add(' vb:=va+va*vb+va div vb;');
  3719. Add(' vc:=-va;');
  3720. Add(' va:=va-vb;');
  3721. Add(' vb:=va;');
  3722. Add(' if va<vb then vc:=va else vc:=vb;');
  3723. ConvertProgram;
  3724. CheckSource('TestArithmeticOperators1',
  3725. LinesToStr([ // statements
  3726. 'this.vA = 0;',
  3727. 'this.vB = 0;',
  3728. 'this.vC = 0;'
  3729. ]),
  3730. LinesToStr([ // this.$main
  3731. '$mod.vA = 1;',
  3732. '$mod.vB = $mod.vA + $mod.vA;',
  3733. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3734. '$mod.vB = $mod.vA % $mod.vB;',
  3735. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3736. '$mod.vC = -$mod.vA;',
  3737. '$mod.vA = $mod.vA - $mod.vB;',
  3738. '$mod.vB = $mod.vA;',
  3739. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3740. ]));
  3741. end;
  3742. procedure TTestModule.TestMultiAdd;
  3743. begin
  3744. StartProgram(false);
  3745. Add([
  3746. 'function Fly: string; external name ''fly'';',
  3747. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3748. 'var',
  3749. ' Date: double;',
  3750. 'begin',
  3751. ' Result:=(Year>0) and (Year<10000) and',
  3752. ' (Month >= 1) and (Month<=12) and',
  3753. ' (Day>0) and (Day<=31);',
  3754. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3755. 'end;',
  3756. 'var s: string;',
  3757. 'begin',
  3758. ' s:=''a''+''b''+''c''+''d'';',
  3759. ' s:=s+Fly+''e'';',
  3760. ' s:=Fly+Fly+Fly;',
  3761. '']);
  3762. ConvertProgram;
  3763. CheckSource('TestMultiAdd',
  3764. LinesToStr([ // statements
  3765. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3766. ' var Result = false;',
  3767. ' var date = 0.0;',
  3768. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3769. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3770. ' return Result;',
  3771. '};',
  3772. 'this.s = "";',
  3773. '']),
  3774. LinesToStr([ // this.$main
  3775. '$mod.s = "a" + "b" + "c" + "d";',
  3776. '$mod.s = $mod.s + fly() + "e";',
  3777. '$mod.s = fly() + fly() + fly();',
  3778. '']));
  3779. end;
  3780. procedure TTestModule.TestLogicalOperators;
  3781. begin
  3782. StartProgram(false);
  3783. Add('var');
  3784. Add(' vA,vB,vC:boolean;');
  3785. Add('begin');
  3786. Add(' va:=vb and vc;');
  3787. Add(' va:=vb or vc;');
  3788. Add(' va:=vb xor vc;');
  3789. Add(' va:=true and vc;');
  3790. Add(' va:=(vb and vc) or (va and vb);');
  3791. Add(' va:=not vb;');
  3792. ConvertProgram;
  3793. CheckSource('TestLogicalOperators',
  3794. LinesToStr([ // statements
  3795. 'this.vA = false;',
  3796. 'this.vB = false;',
  3797. 'this.vC = false;'
  3798. ]),
  3799. LinesToStr([ // this.$main
  3800. '$mod.vA = $mod.vB && $mod.vC;',
  3801. '$mod.vA = $mod.vB || $mod.vC;',
  3802. '$mod.vA = $mod.vB ^ $mod.vC;',
  3803. '$mod.vA = true && $mod.vC;',
  3804. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3805. '$mod.vA = !$mod.vB;'
  3806. ]));
  3807. end;
  3808. procedure TTestModule.TestBitwiseOperators;
  3809. begin
  3810. StartProgram(false);
  3811. Add([
  3812. 'var',
  3813. ' vA,vB,vC:longint;',
  3814. ' X,Y,Z: nativeint;',
  3815. 'begin',
  3816. ' va:=vb and vc;',
  3817. ' va:=vb or vc;',
  3818. ' va:=vb xor vc;',
  3819. ' va:=vb shl vc;',
  3820. ' va:=vb shr vc;',
  3821. ' va:=3 and vc;',
  3822. ' va:=(vb and vc) or (va and vb);',
  3823. ' va:=not vb;',
  3824. ' X:=Y and Z;',
  3825. ' X:=Y and va;',
  3826. ' X:=Y or Z;',
  3827. ' X:=Y or va;',
  3828. ' X:=Y xor Z;',
  3829. ' X:=Y xor va;',
  3830. '']);
  3831. ConvertProgram;
  3832. CheckSource('TestBitwiseOperators',
  3833. LinesToStr([ // statements
  3834. 'this.vA = 0;',
  3835. 'this.vB = 0;',
  3836. 'this.vC = 0;',
  3837. 'this.X = 0;',
  3838. 'this.Y = 0;',
  3839. 'this.Z = 0;',
  3840. '']),
  3841. LinesToStr([ // this.$main
  3842. '$mod.vA = $mod.vB & $mod.vC;',
  3843. '$mod.vA = $mod.vB | $mod.vC;',
  3844. '$mod.vA = $mod.vB ^ $mod.vC;',
  3845. '$mod.vA = $mod.vB << $mod.vC;',
  3846. '$mod.vA = $mod.vB >>> $mod.vC;',
  3847. '$mod.vA = 3 & $mod.vC;',
  3848. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3849. '$mod.vA = ~$mod.vB;',
  3850. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3851. '$mod.X = $mod.Y & $mod.vA;',
  3852. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3853. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3854. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3855. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3856. '']));
  3857. end;
  3858. procedure TTestModule.TestBitwiseOperatorsLongword;
  3859. begin
  3860. StartProgram(false);
  3861. Add([
  3862. 'var',
  3863. ' a,b,c:longword;',
  3864. ' i: longint;',
  3865. 'begin',
  3866. ' a:=$12345678;',
  3867. ' b:=$EDCBA987;',
  3868. ' c:=not a;',
  3869. ' c:=a and b;',
  3870. ' c:=a and $ffff0000;',
  3871. ' c:=a or b;',
  3872. ' c:=a or $ff00ff00;',
  3873. ' c:=a xor b;',
  3874. ' c:=a xor $f0f0f0f0;',
  3875. ' c:=a shl 1;',
  3876. ' c:=a shl 16;',
  3877. ' c:=a shl 24;',
  3878. ' c:=a shl b;',
  3879. ' c:=a shr 1;',
  3880. ' c:=a shr 16;',
  3881. ' c:=a shr 24;',
  3882. ' c:=a shr b;',
  3883. ' c:=(b and c) or (a and b);',
  3884. ' c:=i and a;',
  3885. ' c:=i or a;',
  3886. ' c:=i xor a;',
  3887. '']);
  3888. ConvertProgram;
  3889. CheckSource('TestBitwiseOperatorsLongword',
  3890. LinesToStr([ // statements
  3891. 'this.a = 0;',
  3892. 'this.b = 0;',
  3893. 'this.c = 0;',
  3894. 'this.i = 0;',
  3895. '']),
  3896. LinesToStr([ // this.$main
  3897. '$mod.a = 0x12345678;',
  3898. '$mod.b = 0xEDCBA987;',
  3899. '$mod.c = rtl.lw(~$mod.a);',
  3900. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3901. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3902. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3903. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3904. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3905. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3906. '$mod.c = rtl.lw($mod.a << 1);',
  3907. '$mod.c = rtl.lw($mod.a << 16);',
  3908. '$mod.c = rtl.lw($mod.a << 24);',
  3909. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3910. '$mod.c = rtl.lw($mod.a >>> 1);',
  3911. '$mod.c = rtl.lw($mod.a >>> 16);',
  3912. '$mod.c = rtl.lw($mod.a >>> 24);',
  3913. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3914. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3915. '$mod.c = $mod.i & $mod.a;',
  3916. '$mod.c = $mod.i | $mod.a;',
  3917. '$mod.c = $mod.i ^ $mod.a;',
  3918. '']));
  3919. end;
  3920. procedure TTestModule.TestPrgProcVar;
  3921. begin
  3922. StartProgram(false);
  3923. Add('procedure Proc1;');
  3924. Add('type');
  3925. Add(' t1=longint;');
  3926. Add('var');
  3927. Add(' vA:t1;');
  3928. Add('begin');
  3929. Add('end;');
  3930. Add('begin');
  3931. ConvertProgram;
  3932. CheckSource('TestPrgProcVar',
  3933. LinesToStr([ // statements
  3934. 'this.Proc1 = function () {',
  3935. ' var vA=0;',
  3936. '};'
  3937. ]),
  3938. LinesToStr([ // this.$main
  3939. ''
  3940. ]));
  3941. end;
  3942. procedure TTestModule.TestUnitProcVar;
  3943. begin
  3944. StartUnit(false);
  3945. Add('interface');
  3946. Add('');
  3947. Add('type tA=string; // unit scope');
  3948. Add('procedure Proc1;');
  3949. Add('');
  3950. Add('implementation');
  3951. Add('');
  3952. Add('procedure Proc1;');
  3953. Add('type tA=longint; // local proc scope');
  3954. Add('var v1:tA; // using local tA');
  3955. Add('begin');
  3956. Add('end;');
  3957. Add('var v2:tA; // using interface tA');
  3958. ConvertUnit;
  3959. CheckSource('TestUnitProcVar',
  3960. LinesToStr([ // statements
  3961. 'var $impl = $mod.$impl;',
  3962. 'this.Proc1 = function () {',
  3963. ' var v1 = 0;',
  3964. '};',
  3965. '']),
  3966. // this.$init
  3967. '',
  3968. // implementation
  3969. LinesToStr([
  3970. '$impl.v2 = "";',
  3971. '']));
  3972. end;
  3973. procedure TTestModule.TestImplProc;
  3974. begin
  3975. StartUnit(false);
  3976. Add('interface');
  3977. Add('');
  3978. Add('procedure Proc1;');
  3979. Add('');
  3980. Add('implementation');
  3981. Add('');
  3982. Add('procedure Proc1; begin end;');
  3983. Add('procedure Proc2; begin end;');
  3984. Add('initialization');
  3985. Add(' Proc1;');
  3986. Add(' Proc2;');
  3987. ConvertUnit;
  3988. CheckSource('TestImplProc',
  3989. LinesToStr([ // statements
  3990. 'var $impl = $mod.$impl;',
  3991. 'this.Proc1 = function () {',
  3992. '};',
  3993. '']),
  3994. LinesToStr([ // this.$init
  3995. '$mod.Proc1();',
  3996. '$impl.Proc2();',
  3997. '']),
  3998. LinesToStr([ // implementation
  3999. '$impl.Proc2 = function () {',
  4000. '};',
  4001. ''])
  4002. );
  4003. end;
  4004. procedure TTestModule.TestFunctionResult;
  4005. begin
  4006. StartProgram(false);
  4007. Add('function Func1: longint;');
  4008. Add('begin');
  4009. Add(' Result:=3;');
  4010. Add(' Func1:=4;');
  4011. Add('end;');
  4012. Add('begin');
  4013. ConvertProgram;
  4014. CheckSource('TestFunctionResult',
  4015. LinesToStr([ // statements
  4016. 'this.Func1 = function () {',
  4017. ' var Result = 0;',
  4018. ' Result = 3;',
  4019. ' Result = 4;',
  4020. ' return Result;',
  4021. '};'
  4022. ]),
  4023. '');
  4024. end;
  4025. procedure TTestModule.TestNestedProc;
  4026. begin
  4027. StartProgram(false);
  4028. Add([
  4029. 'var vInUnit: longint;',
  4030. 'function DoIt(pA,pD: longint): longint;',
  4031. 'var',
  4032. ' vB: longint;',
  4033. ' vC: longint;',
  4034. ' function Nesty(pA: longint): longint; ',
  4035. ' var vB: longint;',
  4036. ' begin',
  4037. ' Result:=pa+vb+vc+pd+vInUnit;',
  4038. ' nesty:=3;',
  4039. ' doit:=4;',
  4040. ' exit;',
  4041. ' end;',
  4042. 'begin',
  4043. ' Result:=pa+vb+vc;',
  4044. ' doit:=6;',
  4045. ' exit;',
  4046. 'end;',
  4047. 'begin']);
  4048. ConvertProgram;
  4049. CheckSource('TestNestedProc',
  4050. LinesToStr([ // statements
  4051. 'this.vInUnit = 0;',
  4052. 'this.DoIt = function (pA, pD) {',
  4053. ' var Result = 0;',
  4054. ' var vB = 0;',
  4055. ' var vC = 0;',
  4056. ' function Nesty(pA) {',
  4057. ' var Result$1 = 0;',
  4058. ' var vB = 0;',
  4059. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4060. ' Result$1 = 3;',
  4061. ' Result = 4;',
  4062. ' return Result$1;',
  4063. ' return Result$1;',
  4064. ' };',
  4065. ' Result = pA + vB + vC;',
  4066. ' Result = 6;',
  4067. ' return Result;',
  4068. ' return Result;',
  4069. '};'
  4070. ]),
  4071. '');
  4072. end;
  4073. procedure TTestModule.TestNestedProc_ResultString;
  4074. begin
  4075. StartProgram(false);
  4076. Add([
  4077. 'function DoIt: string;',
  4078. ' function Nesty: string; ',
  4079. ' begin',
  4080. ' nesty:=#65#66;',
  4081. ' nesty[1]:=#67;',
  4082. ' doit:=#68;',
  4083. ' doit[2]:=#69;',
  4084. ' end;',
  4085. 'begin',
  4086. ' doit:=#70;',
  4087. ' doit[3]:=#71;',
  4088. 'end;',
  4089. 'begin']);
  4090. ConvertProgram;
  4091. CheckSource('TestNestedProc_ResultString',
  4092. LinesToStr([ // statements
  4093. 'this.DoIt = function () {',
  4094. ' var Result = "";',
  4095. ' function Nesty() {',
  4096. ' var Result$1 = "";',
  4097. ' Result$1 = "AB";',
  4098. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4099. ' Result = "D";',
  4100. ' Result = rtl.setCharAt(Result, 1, "E");',
  4101. ' return Result$1;',
  4102. ' };',
  4103. ' Result = "F";',
  4104. ' Result = rtl.setCharAt(Result, 2, "G");',
  4105. ' return Result;',
  4106. '};'
  4107. ]),
  4108. '');
  4109. end;
  4110. procedure TTestModule.TestForwardProc;
  4111. begin
  4112. StartProgram(false);
  4113. Add('procedure FuncA(Bar: longint); forward;');
  4114. Add('procedure FuncB(Bar: longint);');
  4115. Add('begin');
  4116. Add(' funca(bar);');
  4117. Add('end;');
  4118. Add('procedure funca(bar: longint);');
  4119. Add('begin');
  4120. Add(' if bar=3 then ;');
  4121. Add('end;');
  4122. Add('begin');
  4123. Add(' funca(4);');
  4124. Add(' funcb(5);');
  4125. ConvertProgram;
  4126. CheckSource('TestForwardProc',
  4127. LinesToStr([ // statements'
  4128. 'this.FuncB = function (Bar) {',
  4129. ' $mod.FuncA(Bar);',
  4130. '};',
  4131. 'this.FuncA = function (Bar) {',
  4132. ' if (Bar === 3);',
  4133. '};'
  4134. ]),
  4135. LinesToStr([
  4136. '$mod.FuncA(4);',
  4137. '$mod.FuncB(5);'
  4138. ])
  4139. );
  4140. end;
  4141. procedure TTestModule.TestNestedForwardProc;
  4142. begin
  4143. StartProgram(false);
  4144. Add('procedure FuncA;');
  4145. Add(' procedure FuncB(i: longint); forward;');
  4146. Add(' procedure FuncC(i: longint);');
  4147. Add(' begin');
  4148. Add(' funcb(i);');
  4149. Add(' end;');
  4150. Add(' procedure FuncB(i: longint);');
  4151. Add(' begin');
  4152. Add(' if i=3 then ;');
  4153. Add(' end;');
  4154. Add('begin');
  4155. Add(' funcc(4)');
  4156. Add('end;');
  4157. Add('begin');
  4158. Add(' funca;');
  4159. ConvertProgram;
  4160. CheckSource('TestNestedForwardProc',
  4161. LinesToStr([ // statements'
  4162. 'this.FuncA = function () {',
  4163. ' function FuncC(i) {',
  4164. ' FuncB(i);',
  4165. ' };',
  4166. ' function FuncB(i) {',
  4167. ' if (i === 3);',
  4168. ' };',
  4169. ' FuncC(4);',
  4170. '};'
  4171. ]),
  4172. LinesToStr([
  4173. '$mod.FuncA();'
  4174. ])
  4175. );
  4176. end;
  4177. procedure TTestModule.TestAssignFunctionResult;
  4178. begin
  4179. StartProgram(false);
  4180. Add('function Func1: longint;');
  4181. Add('begin');
  4182. Add('end;');
  4183. Add('var i: longint;');
  4184. Add('begin');
  4185. Add(' i:=func1();');
  4186. Add(' i:=func1()+func1();');
  4187. ConvertProgram;
  4188. CheckSource('TestAssignFunctionResult',
  4189. LinesToStr([ // statements
  4190. 'this.Func1 = function () {',
  4191. ' var Result = 0;',
  4192. ' return Result;',
  4193. '};',
  4194. 'this.i = 0;'
  4195. ]),
  4196. LinesToStr([
  4197. '$mod.i = $mod.Func1();',
  4198. '$mod.i = $mod.Func1() + $mod.Func1();'
  4199. ]));
  4200. end;
  4201. procedure TTestModule.TestFunctionResultInCondition;
  4202. begin
  4203. StartProgram(false);
  4204. Add('function Func1: longint;');
  4205. Add('begin');
  4206. Add('end;');
  4207. Add('function Func2: boolean;');
  4208. Add('begin');
  4209. Add('end;');
  4210. Add('var i: longint;');
  4211. Add('begin');
  4212. Add(' if func2 then ;');
  4213. Add(' if i=func1() then ;');
  4214. Add(' if i=func1 then ;');
  4215. ConvertProgram;
  4216. CheckSource('TestFunctionResultInCondition',
  4217. LinesToStr([ // statements
  4218. 'this.Func1 = function () {',
  4219. ' var Result = 0;',
  4220. ' return Result;',
  4221. '};',
  4222. 'this.Func2 = function () {',
  4223. ' var Result = false;',
  4224. ' return Result;',
  4225. '};',
  4226. 'this.i = 0;'
  4227. ]),
  4228. LinesToStr([
  4229. 'if ($mod.Func2());',
  4230. 'if ($mod.i === $mod.Func1());',
  4231. 'if ($mod.i === $mod.Func1());'
  4232. ]));
  4233. end;
  4234. procedure TTestModule.TestFunctionResultInForLoop;
  4235. begin
  4236. StartProgram(false);
  4237. Add([
  4238. 'function Func1(a: array of longint): longint;',
  4239. 'begin',
  4240. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4241. ' for Result in a do if a[Result]=0 then exit;',
  4242. 'end;',
  4243. 'begin',
  4244. ' Func1([1,2,3])']);
  4245. ConvertProgram;
  4246. CheckSource('TestFunctionResultInForLoop',
  4247. LinesToStr([ // statements
  4248. 'this.Func1 = function (a) {',
  4249. ' var Result = 0;',
  4250. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4251. ' Result = $l;',
  4252. ' if (a[Result] === 0) return Result;',
  4253. ' };',
  4254. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4255. ' Result = $in[$l1];',
  4256. ' if (a[Result] === 0) return Result;',
  4257. ' };',
  4258. ' return Result;',
  4259. '};',
  4260. '']),
  4261. LinesToStr([
  4262. '$mod.Func1([1, 2, 3]);'
  4263. ]));
  4264. end;
  4265. procedure TTestModule.TestFunctionResultInTypeCast;
  4266. begin
  4267. StartProgram(false);
  4268. Add([
  4269. 'function GetInt: longint;',
  4270. 'begin',
  4271. 'end;',
  4272. 'begin',
  4273. ' if Byte(GetInt)=0 then ;',
  4274. '']);
  4275. ConvertProgram;
  4276. CheckSource('TestFunctionResultInTypeCast',
  4277. LinesToStr([ // statements
  4278. 'this.GetInt = function () {',
  4279. ' var Result = 0;',
  4280. ' return Result;',
  4281. '};',
  4282. '']),
  4283. LinesToStr([
  4284. 'if (($mod.GetInt() & 255) === 0) ;'
  4285. ]));
  4286. end;
  4287. procedure TTestModule.TestExit;
  4288. begin
  4289. StartProgram(false);
  4290. Add('procedure ProcA;');
  4291. Add('begin');
  4292. Add(' exit;');
  4293. Add('end;');
  4294. Add('function FuncB: longint;');
  4295. Add('begin');
  4296. Add(' exit;');
  4297. Add(' exit(3);');
  4298. Add('end;');
  4299. Add('function FuncC: string;');
  4300. Add('begin');
  4301. Add(' exit;');
  4302. Add(' exit(''a'');');
  4303. Add(' exit(''abc'');');
  4304. Add('end;');
  4305. Add('begin');
  4306. Add(' exit;');
  4307. Add(' exit(1);');
  4308. ConvertProgram;
  4309. CheckSource('TestExit',
  4310. LinesToStr([ // statements
  4311. 'this.ProcA = function () {',
  4312. ' return;',
  4313. '};',
  4314. 'this.FuncB = function () {',
  4315. ' var Result = 0;',
  4316. ' return Result;',
  4317. ' return 3;',
  4318. ' return Result;',
  4319. '};',
  4320. 'this.FuncC = function () {',
  4321. ' var Result = "";',
  4322. ' return Result;',
  4323. ' return "a";',
  4324. ' return "abc";',
  4325. ' return Result;',
  4326. '};'
  4327. ]),
  4328. LinesToStr([
  4329. 'return;',
  4330. 'return 1;',
  4331. '']));
  4332. end;
  4333. procedure TTestModule.TestExit_ResultInFinally;
  4334. begin
  4335. StartProgram(false);
  4336. Add([
  4337. 'function Run: word;',
  4338. 'begin',
  4339. ' try',
  4340. ' exit(3);', // no Result in finally -> use return 3
  4341. ' finally',
  4342. ' end;',
  4343. 'end;',
  4344. 'function Fly: word;',
  4345. 'begin',
  4346. ' try',
  4347. ' exit(3);',
  4348. ' finally',
  4349. ' if Result>0 then ;',
  4350. ' end;',
  4351. 'end;',
  4352. 'function Jump: word;',
  4353. 'begin',
  4354. ' try',
  4355. ' try',
  4356. ' exit(4);',
  4357. ' finally',
  4358. ' end;',
  4359. ' finally',
  4360. ' if Result>0 then ;',
  4361. ' end;',
  4362. 'end;',
  4363. 'begin',
  4364. '']);
  4365. ConvertProgram;
  4366. CheckSource('TestExit_ResultInFinally',
  4367. LinesToStr([ // statements
  4368. 'this.Run = function () {',
  4369. ' var Result = 0;',
  4370. ' try {',
  4371. ' return 3;',
  4372. ' } finally {',
  4373. ' };',
  4374. ' return Result;',
  4375. '};',
  4376. 'this.Fly = function () {',
  4377. ' var Result = 0;',
  4378. ' try {',
  4379. ' Result = 3;',
  4380. ' return Result;',
  4381. ' } finally {',
  4382. ' if (Result > 0) ;',
  4383. ' };',
  4384. ' return Result;',
  4385. '};',
  4386. 'this.Jump = function () {',
  4387. ' var Result = 0;',
  4388. ' try {',
  4389. ' try {',
  4390. ' Result = 4;',
  4391. ' return Result;',
  4392. ' } finally {',
  4393. ' };',
  4394. ' } finally {',
  4395. ' if (Result > 0) ;',
  4396. ' };',
  4397. ' return Result;',
  4398. '};',
  4399. '']),
  4400. LinesToStr([
  4401. '']));
  4402. end;
  4403. procedure TTestModule.TestBreak;
  4404. begin
  4405. StartProgram(false);
  4406. Add([
  4407. 'var',
  4408. ' i: longint;',
  4409. 'begin',
  4410. ' repeat',
  4411. ' break;',
  4412. ' until true;',
  4413. ' while true do',
  4414. ' break;',
  4415. ' for i:=1 to 2 do',
  4416. ' break;']);
  4417. ConvertProgram;
  4418. CheckSource('TestBreak',
  4419. LinesToStr([ // statements
  4420. 'this.i = 0;'
  4421. ]),
  4422. LinesToStr([
  4423. 'do {',
  4424. ' break;',
  4425. '} while (!true);',
  4426. 'while (true) break;',
  4427. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4428. '']));
  4429. end;
  4430. procedure TTestModule.TestBreakAsVar;
  4431. begin
  4432. StartProgram(false);
  4433. Add([
  4434. 'procedure DoIt(break: boolean);',
  4435. 'begin',
  4436. ' if break then ;',
  4437. 'end;',
  4438. 'var',
  4439. ' break: boolean;',
  4440. 'begin',
  4441. ' if break then ;']);
  4442. ConvertProgram;
  4443. CheckSource('TestBreakAsVar',
  4444. LinesToStr([ // statements
  4445. 'this.DoIt = function (Break) {',
  4446. ' if (Break) ;',
  4447. '};',
  4448. 'this.Break = false;',
  4449. '']),
  4450. LinesToStr([
  4451. 'if($mod.Break) ;',
  4452. '']));
  4453. end;
  4454. procedure TTestModule.TestContinue;
  4455. begin
  4456. StartProgram(false);
  4457. Add('var i: longint;');
  4458. Add('begin');
  4459. Add(' repeat');
  4460. Add(' continue;');
  4461. Add(' until true;');
  4462. Add(' while true do');
  4463. Add(' continue;');
  4464. Add(' for i:=1 to 2 do');
  4465. Add(' continue;');
  4466. ConvertProgram;
  4467. CheckSource('TestContinue',
  4468. LinesToStr([ // statements
  4469. 'this.i = 0;'
  4470. ]),
  4471. LinesToStr([
  4472. 'do {',
  4473. ' continue;',
  4474. '} while (!true);',
  4475. 'while (true) continue;',
  4476. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4477. '']));
  4478. end;
  4479. procedure TTestModule.TestProc_External;
  4480. begin
  4481. StartProgram(false);
  4482. Add('procedure Foo; external name ''console.log'';');
  4483. Add('function Bar: longint; external name ''get.item'';');
  4484. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4485. Add('var');
  4486. Add(' i: longint;');
  4487. Add('begin');
  4488. Add(' Foo;');
  4489. Add(' i:=Bar;');
  4490. Add(' i:=Bla(''abc'');');
  4491. ConvertProgram;
  4492. CheckSource('TestProc_External',
  4493. LinesToStr([ // statements
  4494. 'this.i = 0;'
  4495. ]),
  4496. LinesToStr([
  4497. 'console.log();',
  4498. '$mod.i = get.item();',
  4499. '$mod.i = apply.something("abc");'
  4500. ]));
  4501. end;
  4502. procedure TTestModule.TestProc_ExternalOtherUnit;
  4503. begin
  4504. AddModuleWithIntfImplSrc('unit2.pas',
  4505. LinesToStr([
  4506. 'procedure Now; external name ''Date.now'';',
  4507. 'procedure DoIt;'
  4508. ]),
  4509. 'procedure doit; begin end;');
  4510. StartUnit(true);
  4511. Add('interface');
  4512. Add('uses unit2;');
  4513. Add('implementation');
  4514. Add('begin');
  4515. Add(' now;');
  4516. Add(' now();');
  4517. Add(' uNit2.now;');
  4518. Add(' uNit2.now();');
  4519. Add(' doit;');
  4520. Add(' uNit2.doit;');
  4521. ConvertUnit;
  4522. CheckSource('TestProc_ExternalOtherUnit',
  4523. LinesToStr([
  4524. '']),
  4525. LinesToStr([
  4526. 'Date.now();',
  4527. 'Date.now();',
  4528. 'Date.now();',
  4529. 'Date.now();',
  4530. 'pas.unit2.DoIt();',
  4531. 'pas.unit2.DoIt();',
  4532. '']));
  4533. end;
  4534. procedure TTestModule.TestProc_Asm;
  4535. begin
  4536. StartProgram(false);
  4537. Add([
  4538. '{$mode delphi}',
  4539. 'function DoIt: longint;',
  4540. 'begin;',
  4541. ' asm',
  4542. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4543. ' end;',
  4544. ' asm console.log(); end;',
  4545. ' asm',
  4546. ' s = "'' ";',
  4547. ' s = ''" '';',
  4548. ' s = s + "world" + "''";',
  4549. ' // end',
  4550. ' s = ''end'';',
  4551. ' s = "end";',
  4552. ' s = "foo\"bar";',
  4553. ' s = ''a\''b'';',
  4554. ' s = `${expr}\`-"-''-`;',
  4555. ' s = `multi',
  4556. 'line`;',
  4557. ' end;',
  4558. 'end;',
  4559. 'procedure Fly;',
  4560. 'asm',
  4561. ' return;',
  4562. 'end;',
  4563. 'begin']);
  4564. ConvertProgram;
  4565. CheckSource('TestProc_Asm',
  4566. LinesToStr([ // statements
  4567. 'this.DoIt = function () {',
  4568. ' var Result = 0;',
  4569. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4570. ' console.log();',
  4571. ' s = "'' ";',
  4572. ' s = ''" '';',
  4573. ' s = s + "world" + "''";',
  4574. ' // end',
  4575. ' s = ''end'';',
  4576. ' s = "end";',
  4577. ' s = "foo\"bar";',
  4578. ' s = ''a\''b'';',
  4579. ' s = `${expr}\`-"-''-`;',
  4580. ' s = `multi',
  4581. 'line`;',
  4582. ' return Result;',
  4583. '};',
  4584. 'this.Fly = function () {',
  4585. ' return;',
  4586. '};',
  4587. '']),
  4588. LinesToStr([
  4589. ''
  4590. ]));
  4591. end;
  4592. procedure TTestModule.TestProc_AsmSubBlock;
  4593. begin
  4594. StartProgram(true,[supTObject]);
  4595. Add([
  4596. '{$mode delphi}',
  4597. 'type',
  4598. ' TBird = class end;',
  4599. 'procedure Run(w: word);',
  4600. 'begin;',
  4601. ' if true then asm console.log(); end;',
  4602. ' if w>3 then asm',
  4603. ' var a = w+1;',
  4604. ' w = a+3;',
  4605. ' end;',
  4606. ' while (w>7) do asm',
  4607. ' w+=3; w*=2;',
  4608. ' end;',
  4609. ' try',
  4610. ' except',
  4611. ' on E: TBird do',
  4612. ' asm console.log(E); end;',
  4613. ' on E: TObject do',
  4614. ' asm var i=3; i--; end;',
  4615. ' else asm Fly; High; end;',
  4616. ' end;',
  4617. 'end;',
  4618. 'begin']);
  4619. ConvertProgram;
  4620. CheckSource('TestProc_AsmSubBlock',
  4621. LinesToStr([ // statements
  4622. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4623. '});',
  4624. 'this.Run = function (w) {',
  4625. ' if (true) console.log();',
  4626. ' if (w > 3) {',
  4627. ' var a = w+1;',
  4628. ' w = a+3;',
  4629. ' };',
  4630. ' while (w > 7) {',
  4631. ' w+=3; w*=2;',
  4632. ' };',
  4633. ' try {} catch ($e) {',
  4634. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4635. ' var E = $e;',
  4636. ' console.log(E);',
  4637. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4638. ' var E = $e;',
  4639. ' var i=3; i--;',
  4640. ' } else {',
  4641. ' Fly; High;',
  4642. ' }',
  4643. ' };',
  4644. '};',
  4645. '']),
  4646. LinesToStr([
  4647. ''
  4648. ]));
  4649. end;
  4650. procedure TTestModule.TestProc_Assembler;
  4651. begin
  4652. StartProgram(false);
  4653. Add('function DoIt: longint; assembler;');
  4654. Add('asm');
  4655. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4656. Add('end;');
  4657. Add('begin');
  4658. ConvertProgram;
  4659. CheckSource('TestProc_Assembler',
  4660. LinesToStr([ // statements
  4661. 'this.DoIt = function () {',
  4662. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4663. '};'
  4664. ]),
  4665. LinesToStr([
  4666. ''
  4667. ]));
  4668. end;
  4669. procedure TTestModule.TestProc_VarParam;
  4670. begin
  4671. StartProgram(false);
  4672. Add('type integer = longint;');
  4673. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4674. Add('var vJ: integer;');
  4675. Add('begin');
  4676. Add(' vg:=vg+1;');
  4677. Add(' vj:=vh+2;');
  4678. Add(' vi:=vi+3;');
  4679. Add(' doit(vg,vg,vg);');
  4680. Add(' doit(vh,vh,vj);');
  4681. Add(' doit(vi,vi,vi);');
  4682. Add(' doit(vj,vj,vj);');
  4683. Add('end;');
  4684. Add('var i: integer;');
  4685. Add('begin');
  4686. Add(' doit(i,i,i);');
  4687. ConvertProgram;
  4688. CheckSource('TestProc_VarParam',
  4689. LinesToStr([ // statements
  4690. 'this.DoIt = function (vG,vH,vI) {',
  4691. ' var vJ = 0;',
  4692. ' vG = vG + 1;',
  4693. ' vJ = vH + 2;',
  4694. ' vI.set(vI.get()+3);',
  4695. ' $mod.DoIt(vG, vG, {',
  4696. ' get: function () {',
  4697. ' return vG;',
  4698. ' },',
  4699. ' set: function (v) {',
  4700. ' vG = v;',
  4701. ' }',
  4702. ' });',
  4703. ' $mod.DoIt(vH, vH, {',
  4704. ' get: function () {',
  4705. ' return vJ;',
  4706. ' },',
  4707. ' set: function (v) {',
  4708. ' vJ = v;',
  4709. ' }',
  4710. ' });',
  4711. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4712. ' $mod.DoIt(vJ, vJ, {',
  4713. ' get: function () {',
  4714. ' return vJ;',
  4715. ' },',
  4716. ' set: function (v) {',
  4717. ' vJ = v;',
  4718. ' }',
  4719. ' });',
  4720. '};',
  4721. 'this.i = 0;'
  4722. ]),
  4723. LinesToStr([
  4724. '$mod.DoIt($mod.i,$mod.i,{',
  4725. ' p: $mod,',
  4726. ' get: function () {',
  4727. ' return this.p.i;',
  4728. ' },',
  4729. ' set: function (v) {',
  4730. ' this.p.i = v;',
  4731. ' }',
  4732. '});'
  4733. ]));
  4734. end;
  4735. procedure TTestModule.TestProc_VarParamString;
  4736. begin
  4737. StartProgram(false);
  4738. Add(['type TCaption = string;',
  4739. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4740. 'var c: char;',
  4741. 'begin',
  4742. ' va[1]:=c;',
  4743. ' vb[2]:=c;',
  4744. ' vc[3]:=c;',
  4745. 'end;',
  4746. 'begin']);
  4747. ConvertProgram;
  4748. CheckSource('TestProc_VarParamString',
  4749. LinesToStr([ // statements
  4750. 'this.DoIt = function (vA,vB,vC) {',
  4751. ' var c = "";',
  4752. ' vA = rtl.setCharAt(vA, 0, c);',
  4753. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4754. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4755. '};',
  4756. '']),
  4757. LinesToStr([
  4758. ]));
  4759. end;
  4760. procedure TTestModule.TestProc_VarParamV;
  4761. begin
  4762. StartProgram(false);
  4763. Add([
  4764. 'procedure Inc2(var i: longint);',
  4765. 'begin',
  4766. ' i:=i+2;',
  4767. 'end;',
  4768. 'procedure DoIt(v: longint);',
  4769. 'var p: array of longint;',
  4770. 'begin',
  4771. ' Inc2(v);',
  4772. ' Inc2(p[v]);',
  4773. 'end;',
  4774. 'begin']);
  4775. ConvertProgram;
  4776. CheckSource('TestProc_VarParamV',
  4777. LinesToStr([ // statements
  4778. 'this.Inc2 = function (i) {',
  4779. ' i.set(i.get()+2);',
  4780. '};',
  4781. 'this.DoIt = function (v) {',
  4782. ' var p = [];',
  4783. ' $mod.Inc2({get: function () {',
  4784. ' return v;',
  4785. ' }, set: function (w) {',
  4786. ' v = w;',
  4787. ' }});',
  4788. ' $mod.Inc2({',
  4789. ' a: v,',
  4790. ' p: p,',
  4791. ' get: function () {',
  4792. ' return this.p[this.a];',
  4793. ' },',
  4794. ' set: function (v) {',
  4795. ' this.p[this.a] = v;',
  4796. ' }',
  4797. ' });',
  4798. '};',
  4799. '']),
  4800. LinesToStr([
  4801. '']));
  4802. end;
  4803. procedure TTestModule.TestProc_Overload;
  4804. begin
  4805. StartProgram(false);
  4806. Add('procedure DoIt(vI: longint); begin end;');
  4807. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4808. Add('procedure DoIt(vD: double); begin end;');
  4809. Add('begin');
  4810. Add(' DoIt(1);');
  4811. Add(' DoIt(2,3);');
  4812. Add(' DoIt(4.5);');
  4813. ConvertProgram;
  4814. CheckSource('TestProcedureOverload',
  4815. LinesToStr([ // statements
  4816. 'this.DoIt = function (vI) {',
  4817. '};',
  4818. 'this.DoIt$1 = function (vI, vJ) {',
  4819. '};',
  4820. 'this.DoIt$2 = function (vD) {',
  4821. '};',
  4822. '']),
  4823. LinesToStr([
  4824. '$mod.DoIt(1);',
  4825. '$mod.DoIt$1(2, 3);',
  4826. '$mod.DoIt$2(4.5);',
  4827. '']));
  4828. end;
  4829. procedure TTestModule.TestProc_OverloadForward;
  4830. begin
  4831. StartProgram(false);
  4832. Add('procedure DoIt(vI: longint); forward;');
  4833. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4834. Add('procedure doit(vi: longint); begin end;');
  4835. Add('begin');
  4836. Add(' doit(1);');
  4837. Add(' doit(2,3);');
  4838. ConvertProgram;
  4839. CheckSource('TestProcedureOverloadForward',
  4840. LinesToStr([ // statements
  4841. 'this.DoIt$1 = function (vI, vJ) {',
  4842. '};',
  4843. 'this.DoIt = function (vI) {',
  4844. '};',
  4845. '']),
  4846. LinesToStr([
  4847. '$mod.DoIt(1);',
  4848. '$mod.DoIt$1(2, 3);',
  4849. '']));
  4850. end;
  4851. procedure TTestModule.TestProc_OverloadIntfImpl;
  4852. begin
  4853. StartUnit(false);
  4854. Add('interface');
  4855. Add('procedure DoIt(vI: longint);');
  4856. Add('procedure DoIt(vI, vJ: longint);');
  4857. Add('implementation');
  4858. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4859. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4860. Add('procedure DoIt(vi: longint); begin end;');
  4861. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4862. Add('procedure DoIt(vi, vj: longint); begin end;');
  4863. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4864. Add('begin');
  4865. Add(' doit(1);');
  4866. Add(' doit(2,3);');
  4867. Add(' doit(4,5,6);');
  4868. Add(' doit(7,8,9,10);');
  4869. Add(' doit(11,12,13,14,15);');
  4870. ConvertUnit;
  4871. CheckSource('TestProcedureOverloadUnit',
  4872. LinesToStr([ // statements
  4873. 'var $impl = $mod.$impl;',
  4874. 'this.DoIt = function (vI) {',
  4875. '};',
  4876. 'this.DoIt$1 = function (vI, vJ) {',
  4877. '};',
  4878. '']),
  4879. LinesToStr([ // this.$init
  4880. '$mod.DoIt(1);',
  4881. '$mod.DoIt$1(2, 3);',
  4882. '$impl.DoIt$3(4,5,6);',
  4883. '$impl.DoIt$4(7,8,9,10);',
  4884. '$impl.DoIt$2(11,12,13,14,15);',
  4885. '']),
  4886. LinesToStr([ // implementation
  4887. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4888. '};',
  4889. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4890. '};',
  4891. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4892. '};',
  4893. '']));
  4894. end;
  4895. procedure TTestModule.TestProc_OverloadNested;
  4896. begin
  4897. StartProgram(false);
  4898. Add([
  4899. 'procedure doit(vA: longint);',
  4900. ' procedure DoIt(vA, vB: longint); overload;',
  4901. ' begin',
  4902. ' doit(1);',
  4903. ' doit(1,2);',
  4904. ' end;',
  4905. ' procedure doit(vA, vB, vC: longint);',
  4906. ' begin',
  4907. ' doit(1);',
  4908. ' doit(1,2);',
  4909. ' doit(1,2,3);',
  4910. ' end;',
  4911. 'begin',
  4912. ' doit(1);',
  4913. ' doit(1,2);',
  4914. ' doit(1,2,3);',
  4915. 'end;',
  4916. 'begin // main',
  4917. ' doit(1);']);
  4918. ConvertProgram;
  4919. CheckSource('TestProcedureOverloadNested',
  4920. LinesToStr([ // statements
  4921. 'this.doit = function (vA) {',
  4922. ' function DoIt$1(vA, vB) {',
  4923. ' $mod.doit(1);',
  4924. ' DoIt$1(1, 2);',
  4925. ' };',
  4926. ' function doit$2(vA, vB, vC) {',
  4927. ' $mod.doit(1);',
  4928. ' DoIt$1(1, 2);',
  4929. ' doit$2(1, 2, 3);',
  4930. ' };',
  4931. ' $mod.doit(1);',
  4932. ' DoIt$1(1, 2);',
  4933. ' doit$2(1, 2, 3);',
  4934. '};',
  4935. '']),
  4936. LinesToStr([
  4937. '$mod.doit(1);',
  4938. '']));
  4939. end;
  4940. procedure TTestModule.TestProc_OverloadNestedForward;
  4941. begin
  4942. StartProgram(false);
  4943. Add([
  4944. 'procedure DoIt(vA: longint); overload; forward;',
  4945. 'procedure DoIt(vB, vC: longint); overload;',
  4946. 'begin // 2 param overload',
  4947. ' doit(1);',
  4948. ' doit(1,2);',
  4949. 'end;',
  4950. 'procedure doit(vA: longint);',
  4951. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4952. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4953. ' begin // 4 param overload',
  4954. ' doit(1);',
  4955. ' doit(1,2);',
  4956. ' doit(1,2,3);',
  4957. ' doit(1,2,3,4);',
  4958. ' end;',
  4959. ' procedure doit(vA, vB, vC: longint);',
  4960. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4961. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4962. ' begin // 6 param overload',
  4963. ' doit(1);',
  4964. ' doit(1,2);',
  4965. ' doit(1,2,3);',
  4966. ' doit(1,2,3,4);',
  4967. ' doit(1,2,3,4,5);',
  4968. ' doit(1,2,3,4,5,6);',
  4969. ' end;',
  4970. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4971. ' begin // 5 param overload',
  4972. ' doit(1);',
  4973. ' doit(1,2);',
  4974. ' doit(1,2,3);',
  4975. ' doit(1,2,3,4);',
  4976. ' doit(1,2,3,4,5);',
  4977. ' doit(1,2,3,4,5,6);',
  4978. ' end;',
  4979. ' begin // 3 param overload',
  4980. ' doit(1);',
  4981. ' doit(1,2);',
  4982. ' doit(1,2,3);',
  4983. ' doit(1,2,3,4);',
  4984. ' doit(1,2,3,4,5);',
  4985. ' doit(1,2,3,4,5,6);',
  4986. ' end;',
  4987. 'begin // 1 param overload',
  4988. ' doit(1);',
  4989. ' doit(1,2);',
  4990. ' doit(1,2,3);',
  4991. ' doit(1,2,3,4);',
  4992. 'end;',
  4993. 'begin // main',
  4994. ' doit(1);',
  4995. ' doit(1,2);']);
  4996. ConvertProgram;
  4997. CheckSource('TestProc_OverloadNestedForward',
  4998. LinesToStr([ // statements
  4999. 'this.DoIt$1 = function (vB, vC) {',
  5000. ' $mod.DoIt(1);',
  5001. ' $mod.DoIt$1(1, 2);',
  5002. '};',
  5003. 'this.DoIt = function (vA) {',
  5004. ' function DoIt$3(vA, vB, vC, vD) {',
  5005. ' $mod.DoIt(1);',
  5006. ' $mod.DoIt$1(1, 2);',
  5007. ' DoIt$2(1, 2, 3);',
  5008. ' DoIt$3(1, 2, 3, 4);',
  5009. ' };',
  5010. ' function DoIt$2(vA, vB, vC) {',
  5011. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5012. ' $mod.DoIt(1);',
  5013. ' $mod.DoIt$1(1, 2);',
  5014. ' DoIt$2(1, 2, 3);',
  5015. ' DoIt$3(1, 2, 3, 4);',
  5016. ' DoIt$4(1, 2, 3, 4, 5);',
  5017. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5018. ' };',
  5019. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5020. ' $mod.DoIt(1);',
  5021. ' $mod.DoIt$1(1, 2);',
  5022. ' DoIt$2(1, 2, 3);',
  5023. ' DoIt$3(1, 2, 3, 4);',
  5024. ' DoIt$4(1, 2, 3, 4, 5);',
  5025. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5026. ' };',
  5027. ' $mod.DoIt(1);',
  5028. ' $mod.DoIt$1(1, 2);',
  5029. ' DoIt$2(1, 2, 3);',
  5030. ' DoIt$3(1, 2, 3, 4);',
  5031. ' DoIt$4(1, 2, 3, 4, 5);',
  5032. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5033. ' };',
  5034. ' $mod.DoIt(1);',
  5035. ' $mod.DoIt$1(1, 2);',
  5036. ' DoIt$2(1, 2, 3);',
  5037. ' DoIt$3(1, 2, 3, 4);',
  5038. '};',
  5039. '']),
  5040. LinesToStr([
  5041. '$mod.DoIt(1);',
  5042. '$mod.DoIt$1(1, 2);',
  5043. '']));
  5044. end;
  5045. procedure TTestModule.TestProc_OverloadUnitCycle;
  5046. begin
  5047. AddModuleWithIntfImplSrc('Unit2.pas',
  5048. LinesToStr([
  5049. 'type',
  5050. ' TObject = class',
  5051. ' procedure DoIt(b: boolean); virtual; abstract;',
  5052. ' procedure DoIt(i: longint); virtual; abstract;',
  5053. ' end;',
  5054. '']),
  5055. 'uses test1;');
  5056. StartUnit(true);
  5057. Add([
  5058. 'interface',
  5059. 'uses unit2;',
  5060. 'type',
  5061. ' TEagle = class(TObject)',
  5062. ' procedure DoIt(b: boolean); override;',
  5063. ' procedure DoIt(i: longint); override;',
  5064. ' end;',
  5065. 'implementation',
  5066. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5067. 'procedure TEagle.DoIt(i: longint); begin end;',
  5068. '']);
  5069. ConvertUnit;
  5070. CheckSource('TestProc_OverloadUnitCycle',
  5071. LinesToStr([ // statements
  5072. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5073. ' this.DoIt = function (b) {',
  5074. ' };',
  5075. ' this.DoIt$1 = function (i) {',
  5076. ' };',
  5077. '});',
  5078. '']),
  5079. '',
  5080. LinesToStr([
  5081. '']));
  5082. end;
  5083. procedure TTestModule.TestProc_Varargs;
  5084. begin
  5085. StartProgram(false);
  5086. Add([
  5087. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5088. 'procedure ProcB; varargs; external name ''ProcB'';',
  5089. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5090. 'function GetIt: longint; begin end;',
  5091. 'begin',
  5092. ' ProcA(1);',
  5093. ' ProcA(1,2);',
  5094. ' ProcA(1,2.0);',
  5095. ' ProcA(1,2,3);',
  5096. ' ProcA(1,''2'');',
  5097. ' ProcA(2,'''');',
  5098. ' ProcA(3,false);',
  5099. ' ProcB;',
  5100. ' ProcB();',
  5101. ' ProcB(4);',
  5102. ' ProcB(''foo'');',
  5103. ' ProcC;',
  5104. ' ProcC();',
  5105. ' ProcC(4);',
  5106. ' ProcC(5,''foo'');',
  5107. ' ProcB(GetIt);',
  5108. ' ProcB(GetIt());',
  5109. ' ProcB(GetIt,GetIt());']);
  5110. ConvertProgram;
  5111. CheckSource('TestProc_Varargs',
  5112. LinesToStr([ // statements
  5113. 'this.GetIt = function () {',
  5114. ' var Result = 0;',
  5115. ' return Result;',
  5116. '};',
  5117. '']),
  5118. LinesToStr([
  5119. 'ProcA(1);',
  5120. 'ProcA(1, 2);',
  5121. 'ProcA(1, 2.0);',
  5122. 'ProcA(1, 2, 3);',
  5123. 'ProcA(1, "2");',
  5124. 'ProcA(2, "");',
  5125. 'ProcA(3, false);',
  5126. 'ProcB();',
  5127. 'ProcB();',
  5128. 'ProcB(4);',
  5129. 'ProcB("foo");',
  5130. 'ProcC(17);',
  5131. 'ProcC(17);',
  5132. 'ProcC(4);',
  5133. 'ProcC(5, "foo");',
  5134. 'ProcB($mod.GetIt());',
  5135. 'ProcB($mod.GetIt());',
  5136. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5137. '']));
  5138. end;
  5139. procedure TTestModule.TestProc_ConstOrder;
  5140. begin
  5141. StartProgram(false);
  5142. Add([
  5143. 'const A = 3;',
  5144. 'const B = A+1;',
  5145. 'procedure DoIt;',
  5146. 'const C = A+1;',
  5147. 'const D = B+1;',
  5148. 'const E = D+C+B+A;',
  5149. 'begin',
  5150. 'end;',
  5151. 'begin'
  5152. ]);
  5153. ConvertProgram;
  5154. CheckSource('TestProc_ConstOrder',
  5155. LinesToStr([ // statements
  5156. 'this.A = 3;',
  5157. 'this.B = 3 + 1;',
  5158. 'var C = 3 + 1;',
  5159. 'var D = 4 + 1;',
  5160. 'var E = 5 + 4 + 4 + 3;',
  5161. 'this.DoIt = function () {',
  5162. '};',
  5163. '']),
  5164. LinesToStr([
  5165. ''
  5166. ]));
  5167. end;
  5168. procedure TTestModule.TestProc_DuplicateConst;
  5169. begin
  5170. StartProgram(false);
  5171. Add([
  5172. 'const A = 1;',
  5173. 'procedure DoIt;',
  5174. 'const A = 2;',
  5175. ' procedure SubIt;',
  5176. ' const A = 21;',
  5177. ' begin',
  5178. ' end;',
  5179. 'begin',
  5180. 'end;',
  5181. 'procedure DoSome;',
  5182. 'const A = 3;',
  5183. 'begin',
  5184. 'end;',
  5185. 'begin'
  5186. ]);
  5187. ConvertProgram;
  5188. CheckSource('TestProc_DuplicateConst',
  5189. LinesToStr([ // statements
  5190. 'this.A = 1;',
  5191. 'var A$1 = 2;',
  5192. 'var A$2 = 21;',
  5193. 'this.DoIt = function () {',
  5194. ' function SubIt() {',
  5195. ' };',
  5196. '};',
  5197. 'var A$3 = 3;',
  5198. 'this.DoSome = function () {',
  5199. '};',
  5200. '']),
  5201. LinesToStr([
  5202. ''
  5203. ]));
  5204. end;
  5205. procedure TTestModule.TestProc_LocalVarAbsolute;
  5206. begin
  5207. StartProgram(false);
  5208. Add([
  5209. 'type',
  5210. ' TObject = class',
  5211. ' Index: longint;',
  5212. ' procedure DoAbs(Item: pointer);',
  5213. ' end;',
  5214. 'procedure TObject.DoAbs(Item: pointer);',
  5215. 'var',
  5216. ' o: TObject absolute Item;',
  5217. 'begin',
  5218. ' if o.Index<o.Index then o.Index:=o.Index;',
  5219. 'end;',
  5220. 'procedure DoIt(i: longint; p: pointer);',
  5221. 'var',
  5222. ' d: double absolute i;',
  5223. ' s: string absolute d;',
  5224. ' oi: TObject absolute i;',
  5225. ' op: TObject absolute p;',
  5226. 'begin',
  5227. ' if d=d then d:=d;',
  5228. ' if s=s then s:=s;',
  5229. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5230. ' if op.Index=op.Index then op.Index:=op.Index;',
  5231. 'end;',
  5232. 'begin']);
  5233. ConvertProgram;
  5234. CheckSource('TestProc_LocalVarAbsolute',
  5235. LinesToStr([ // statements
  5236. 'rtl.createClass(this, "TObject", null, function () {',
  5237. ' this.$init = function () {',
  5238. ' this.Index = 0;',
  5239. ' };',
  5240. ' this.$final = function () {',
  5241. ' };',
  5242. ' this.DoAbs = function (Item) {',
  5243. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5244. ' };',
  5245. '});',
  5246. 'this.DoIt = function (i, p) {',
  5247. ' if (i === i) i = i;',
  5248. ' if (i === i) i = i;',
  5249. ' if (i.Index < i.Index) i.Index = i.Index;',
  5250. ' if (p.Index === p.Index) p.Index = p.Index;',
  5251. '};'
  5252. ]),
  5253. LinesToStr([
  5254. ]));
  5255. end;
  5256. procedure TTestModule.TestProc_ResultAbsolute;
  5257. begin
  5258. StartProgram(false);
  5259. Add([
  5260. 'type',
  5261. ' TObject = class',
  5262. ' Index: longint;',
  5263. ' function DoAbs: pointer;',
  5264. ' end;',
  5265. 'function TObject.DoAbs: pointer;',
  5266. 'var',
  5267. ' o: TObject absolute Result;',
  5268. 'begin',
  5269. ' if o.Index<o.Index then o.Index:=o.Index;',
  5270. 'end;',
  5271. 'function DoIt: jsvalue;',
  5272. 'var',
  5273. ' d: double absolute Result;',
  5274. ' s: string absolute Result;',
  5275. ' o: TObject absolute Result;',
  5276. 'begin',
  5277. ' if d=d then d:=d;',
  5278. ' if s=s then s:=s;',
  5279. ' if o.Index<o.Index then o.Index:=o.Index;',
  5280. 'end;',
  5281. 'begin']);
  5282. ConvertProgram;
  5283. CheckSource('TestProc_ResultAbsolute',
  5284. LinesToStr([ // statements
  5285. 'rtl.createClass(this, "TObject", null, function () {',
  5286. ' this.$init = function () {',
  5287. ' this.Index = 0;',
  5288. ' };',
  5289. ' this.$final = function () {',
  5290. ' };',
  5291. ' this.DoAbs = function () {',
  5292. ' var Result = null;',
  5293. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5294. ' return Result;',
  5295. ' };',
  5296. '});',
  5297. 'this.DoIt = function () {',
  5298. ' var Result = undefined;',
  5299. ' if (Result === Result) Result = Result;',
  5300. ' if (Result === Result) Result = Result;',
  5301. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5302. ' return Result;',
  5303. '};',
  5304. '']),
  5305. LinesToStr([
  5306. ]));
  5307. end;
  5308. procedure TTestModule.TestProc_LocalVarInit;
  5309. begin
  5310. StartProgram(false);
  5311. Add([
  5312. 'type TBytes = array of byte;',
  5313. 'procedure DoIt;',
  5314. 'const c = 4;',
  5315. 'var',
  5316. ' b: byte = 1;',
  5317. ' w: word = 2+c;',
  5318. ' p: pointer = nil;',
  5319. ' Buffer: TBytes = nil;',
  5320. 'begin',
  5321. 'end;',
  5322. 'begin']);
  5323. ConvertProgram;
  5324. CheckSource('TestProc_LocalVarInit',
  5325. LinesToStr([ // statements
  5326. 'var c = 4;',
  5327. 'this.DoIt = function () {',
  5328. ' var b = 1;',
  5329. ' var w = 2 + 4;',
  5330. ' var p = null;',
  5331. ' var Buffer = [];',
  5332. '};',
  5333. '']),
  5334. LinesToStr([
  5335. ]));
  5336. end;
  5337. procedure TTestModule.TestProc_ReservedWords;
  5338. begin
  5339. StartProgram(false);
  5340. Add([
  5341. 'procedure Date(ArrayBuffer: longint);',
  5342. 'const',
  5343. ' NaN: longint = 3;',
  5344. 'var',
  5345. ' &Boolean: longint;',
  5346. ' procedure Error(ArrayBuffer: longint);',
  5347. ' begin',
  5348. ' end;',
  5349. 'begin',
  5350. ' Nan:=&bOolean;',
  5351. 'end;',
  5352. 'begin',
  5353. ' Date(1);']);
  5354. ConvertProgram;
  5355. CheckSource('TestProc_ReservedWords',
  5356. LinesToStr([ // statements
  5357. 'var naN = 3;',
  5358. 'this.Date = function (arrayBuffer) {',
  5359. ' var boolean = 0;',
  5360. ' function error(arrayBuffer) {',
  5361. ' };',
  5362. ' naN = boolean;',
  5363. '};',
  5364. '']),
  5365. LinesToStr([
  5366. ' $mod.Date(1);'
  5367. ]));
  5368. end;
  5369. procedure TTestModule.TestProc_ConstRefWord;
  5370. begin
  5371. StartProgram(false);
  5372. Add([
  5373. 'procedure Run(constref w: word);',
  5374. 'var l: word;',
  5375. 'begin',
  5376. ' l:=w;',
  5377. ' Run(w);',
  5378. ' Run(l);',
  5379. 'end;',
  5380. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5381. 'begin',
  5382. ' Run(a);',
  5383. ' Run(b);',
  5384. ' Run(c);',
  5385. ' Run(d);',
  5386. ' Run(e);',
  5387. 'end;',
  5388. 'begin',
  5389. ' Run(1);']);
  5390. ConvertProgram;
  5391. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5392. CheckSource('TestProc_ConstRefWord',
  5393. LinesToStr([ // statements
  5394. 'this.Run = function (w) {',
  5395. ' var l = 0;',
  5396. ' l = w;',
  5397. ' $mod.Run(w);',
  5398. ' $mod.Run(l);',
  5399. '};',
  5400. 'this.Fly = function (a, b, c, d, e) {',
  5401. ' $mod.Run(a);',
  5402. ' $mod.Run(b.get());',
  5403. ' $mod.Run(c.get());',
  5404. ' $mod.Run(d);',
  5405. ' $mod.Run(e);',
  5406. '};',
  5407. '']),
  5408. LinesToStr([
  5409. '$mod.Run(1);'
  5410. ]));
  5411. end;
  5412. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5413. begin
  5414. StartProgram(false);
  5415. Add([
  5416. '{$mode objfpc}',
  5417. 'type',
  5418. ' TFunc = reference to function(x: word): word;',
  5419. 'var Func: TFunc;',
  5420. 'procedure DoIt(a: word);',
  5421. 'begin',
  5422. ' Func:=function(b:word): word',
  5423. ' begin',
  5424. ' Result:=a+b;',
  5425. ' exit(b);',
  5426. ' exit(Result);',
  5427. ' end;',// test semicolon
  5428. ' a:=3;',
  5429. 'end;',
  5430. 'begin',
  5431. ' Func:=function(c:word):word begin',
  5432. ' Result:=3+c;',
  5433. ' exit(c);',
  5434. ' exit(Result);',
  5435. ' end;']);
  5436. ConvertProgram;
  5437. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5438. LinesToStr([ // statements
  5439. 'this.Func = null;',
  5440. 'this.DoIt = function (a) {',
  5441. ' $mod.Func = function (b) {',
  5442. ' var Result = 0;',
  5443. ' Result = a + b;',
  5444. ' return b;',
  5445. ' return Result;',
  5446. ' return Result;',
  5447. ' };',
  5448. ' a = 3;',
  5449. '};',
  5450. '']),
  5451. LinesToStr([
  5452. '$mod.Func = function (c) {',
  5453. ' var Result = 0;',
  5454. ' Result = 3 + c;',
  5455. ' return c;',
  5456. ' return Result;',
  5457. ' return Result;',
  5458. '};',
  5459. '']));
  5460. end;
  5461. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5462. begin
  5463. StartProgram(false);
  5464. Add([
  5465. '{$mode delphi}',
  5466. 'type',
  5467. ' TProc = reference to procedure(x: word);',
  5468. 'procedure DoIt(a: word);',
  5469. 'var Proc: TProc;',
  5470. 'begin',
  5471. ' Proc:=procedure(b:word) begin end;',
  5472. 'end;',
  5473. 'var Proc: TProc;',
  5474. 'begin',
  5475. ' Proc:=procedure(c:word) begin end;',
  5476. '']);
  5477. ConvertProgram;
  5478. CheckSource('TestAnonymousProc_Assign_Delphi',
  5479. LinesToStr([ // statements
  5480. 'this.DoIt = function (a) {',
  5481. ' var Proc = null;',
  5482. ' Proc = function (b) {',
  5483. ' };',
  5484. '};',
  5485. 'this.Proc = null;',
  5486. '']),
  5487. LinesToStr([
  5488. '$mod.Proc = function (c) {',
  5489. '};',
  5490. '']));
  5491. end;
  5492. procedure TTestModule.TestAnonymousProc_Arg;
  5493. begin
  5494. StartProgram(false);
  5495. Add([
  5496. 'type',
  5497. ' TProc = reference to procedure;',
  5498. ' TFunc = reference to function(x: word): word;',
  5499. 'procedure DoMore(f,g: TProc);',
  5500. 'begin',
  5501. 'end;',
  5502. 'procedure DoOdd(v: jsvalue);',
  5503. 'begin',
  5504. 'end;',
  5505. 'procedure DoIt(f: TFunc);',
  5506. 'begin',
  5507. ' DoIt(function(b:word): word',
  5508. ' begin',
  5509. ' Result:=1+b;',
  5510. ' end);',
  5511. ' DoMore(procedure begin end, procedure begin end);',
  5512. ' DoOdd(procedure begin end);',
  5513. 'end;',
  5514. 'begin',
  5515. ' DoMore(procedure begin end,',
  5516. ' procedure assembler asm',
  5517. ' console.log("c");',
  5518. ' end);',
  5519. '']);
  5520. ConvertProgram;
  5521. CheckSource('TestAnonymousProc_Arg',
  5522. LinesToStr([ // statements
  5523. 'this.DoMore = function (f, g) {',
  5524. '};',
  5525. 'this.DoOdd = function (v) {',
  5526. '};',
  5527. 'this.DoIt = function (f) {',
  5528. ' $mod.DoIt(function (b) {',
  5529. ' var Result = 0;',
  5530. ' Result = 1 + b;',
  5531. ' return Result;',
  5532. ' });',
  5533. ' $mod.DoMore(function () {',
  5534. ' }, function () {',
  5535. ' });',
  5536. ' $mod.DoOdd(function () {',
  5537. ' });',
  5538. '};',
  5539. '']),
  5540. LinesToStr([
  5541. '$mod.DoMore(function () {',
  5542. '}, function () {',
  5543. ' console.log("c");',
  5544. '});',
  5545. '']));
  5546. end;
  5547. procedure TTestModule.TestAnonymousProc_Typecast;
  5548. begin
  5549. StartProgram(false);
  5550. Add([
  5551. 'type',
  5552. ' TProc = reference to procedure(w: word);',
  5553. ' TArr = array of word;',
  5554. ' TFuncArr = reference to function: TArr;',
  5555. 'procedure DoIt(p: TProc);',
  5556. 'var',
  5557. ' w: word;',
  5558. ' a: TArr;',
  5559. 'begin',
  5560. ' p:=TProc(procedure(b: smallint) begin end);',
  5561. ' a:=TFuncArr(function: TArr begin end)();',
  5562. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5563. 'end;',
  5564. 'begin']);
  5565. ConvertProgram;
  5566. CheckSource('TestAnonymousProc_Typecast',
  5567. LinesToStr([ // statements
  5568. 'this.DoIt = function (p) {',
  5569. ' var w = 0;',
  5570. ' var a = [];',
  5571. ' p = function (b) {',
  5572. ' };',
  5573. ' a = function () {',
  5574. ' var Result = [];',
  5575. ' return Result;',
  5576. ' }();',
  5577. ' w = function () {',
  5578. ' var Result = [];',
  5579. ' return Result;',
  5580. ' }()[3];',
  5581. '};',
  5582. '']),
  5583. LinesToStr([
  5584. '']));
  5585. end;
  5586. procedure TTestModule.TestAnonymousProc_With;
  5587. begin
  5588. StartProgram(false);
  5589. Add([
  5590. 'type',
  5591. ' TProc = reference to procedure(w: word);',
  5592. ' TObject = class',
  5593. ' b: boolean;',
  5594. ' end;',
  5595. 'var',
  5596. ' p: TProc;',
  5597. ' bird: TObject;',
  5598. 'begin',
  5599. ' with bird do',
  5600. ' p:=procedure(w: word)',
  5601. ' begin',
  5602. ' b:=w>2;',
  5603. ' end;',
  5604. '']);
  5605. ConvertProgram;
  5606. CheckSource('TestAnonymousProc_With',
  5607. LinesToStr([ // statements
  5608. 'rtl.createClass(this, "TObject", null, function () {',
  5609. ' this.$init = function () {',
  5610. ' this.b = false;',
  5611. ' };',
  5612. ' this.$final = function () {',
  5613. ' };',
  5614. '});',
  5615. 'this.p = null;',
  5616. 'this.bird = null;',
  5617. '']),
  5618. LinesToStr([
  5619. 'var $with = $mod.bird;',
  5620. '$mod.p = function (w) {',
  5621. ' $with.b = w > 2;',
  5622. '};',
  5623. '']));
  5624. end;
  5625. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5626. begin
  5627. StartProgram(false);
  5628. Add([
  5629. 'type',
  5630. ' TProc = reference to procedure;',
  5631. ' TObject = class',
  5632. ' b: boolean;',
  5633. ' end;',
  5634. 'procedure DoIt;',
  5635. 'var',
  5636. ' p: TProc;',
  5637. 'begin',
  5638. ' try',
  5639. ' except',
  5640. ' on E: TObject do',
  5641. ' p:=procedure',
  5642. ' begin',
  5643. ' E.b:=true;',
  5644. ' end;',
  5645. ' end;',
  5646. 'end;',
  5647. 'begin']);
  5648. ConvertProgram;
  5649. CheckSource('TestAnonymousProc_ExceptOn',
  5650. LinesToStr([ // statements
  5651. 'rtl.createClass(this, "TObject", null, function () {',
  5652. ' this.$init = function () {',
  5653. ' this.b = false;',
  5654. ' };',
  5655. ' this.$final = function () {',
  5656. ' };',
  5657. '});',
  5658. 'this.DoIt = function () {',
  5659. ' var p = null;',
  5660. ' try {} catch ($e) {',
  5661. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5662. ' var E = $e;',
  5663. ' p = function () {',
  5664. ' E.b = true;',
  5665. ' };',
  5666. ' } else throw $e',
  5667. ' };',
  5668. '};',
  5669. '']),
  5670. LinesToStr([
  5671. '']));
  5672. end;
  5673. procedure TTestModule.TestAnonymousProc_Nested;
  5674. begin
  5675. StartProgram(false);
  5676. Add([
  5677. 'type',
  5678. ' TProc = reference to procedure;',
  5679. ' TObject = class',
  5680. ' i: byte;',
  5681. ' procedure DoIt;',
  5682. ' end;',
  5683. 'procedure TObject.DoIt;',
  5684. 'var',
  5685. ' p: TProc;',
  5686. ' procedure Sub;',
  5687. ' begin',
  5688. ' p:=procedure',
  5689. ' begin',
  5690. ' i:=3;',
  5691. ' Self.i:=4;',
  5692. ' p:=procedure',
  5693. ' procedure SubSub;',
  5694. ' begin',
  5695. ' i:=13;',
  5696. ' Self.i:=14;',
  5697. ' end;',
  5698. ' begin',
  5699. ' i:=13;',
  5700. ' Self.i:=14;',
  5701. ' end;',
  5702. ' end;',
  5703. ' end;',
  5704. 'begin',
  5705. 'end;',
  5706. 'begin']);
  5707. ConvertProgram;
  5708. CheckSource('TestAnonymousProc_Nested',
  5709. LinesToStr([ // statements
  5710. 'rtl.createClass(this, "TObject", null, function () {',
  5711. ' this.$init = function () {',
  5712. ' this.i = 0;',
  5713. ' };',
  5714. ' this.$final = function () {',
  5715. ' };',
  5716. ' this.DoIt = function () {',
  5717. ' var $Self = this;',
  5718. ' var p = null;',
  5719. ' function Sub() {',
  5720. ' p = function () {',
  5721. ' $Self.i = 3;',
  5722. ' $Self.i = 4;',
  5723. ' p = function () {',
  5724. ' function SubSub() {',
  5725. ' $Self.i = 13;',
  5726. ' $Self.i = 14;',
  5727. ' };',
  5728. ' $Self.i = 13;',
  5729. ' $Self.i = 14;',
  5730. ' };',
  5731. ' };',
  5732. ' };',
  5733. ' };',
  5734. '});',
  5735. '']),
  5736. LinesToStr([
  5737. '']));
  5738. end;
  5739. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5740. begin
  5741. StartProgram(false);
  5742. Add([
  5743. 'type',
  5744. ' TProc = reference to procedure;',
  5745. 'function DoIt: TProc;',
  5746. ' function Sub: TProc;',
  5747. ' begin',
  5748. ' Result:=procedure',
  5749. ' begin',
  5750. ' Sub:=procedure',
  5751. ' procedure SubSub;',
  5752. ' begin',
  5753. ' Result:=nil;',
  5754. ' Sub:=nil;',
  5755. ' DoIt:=nil;',
  5756. ' end;',
  5757. ' begin',
  5758. ' Result:=nil;',
  5759. ' Sub:=nil;',
  5760. ' DoIt:=nil;',
  5761. ' end;',
  5762. ' end;',
  5763. ' end;',
  5764. 'begin',
  5765. 'end;',
  5766. 'begin']);
  5767. ConvertProgram;
  5768. CheckSource('TestAnonymousProc_NestedAssignResult',
  5769. LinesToStr([ // statements
  5770. 'this.DoIt = function () {',
  5771. ' var Result = null;',
  5772. ' function Sub() {',
  5773. ' var Result$1 = null;',
  5774. ' Result$1 = function () {',
  5775. ' Result$1 = function () {',
  5776. ' function SubSub() {',
  5777. ' Result$1 = null;',
  5778. ' Result$1 = null;',
  5779. ' Result = null;',
  5780. ' };',
  5781. ' Result$1 = null;',
  5782. ' Result$1 = null;',
  5783. ' Result = null;',
  5784. ' };',
  5785. ' };',
  5786. ' return Result$1;',
  5787. ' };',
  5788. ' return Result;',
  5789. '};',
  5790. '']),
  5791. LinesToStr([
  5792. '']));
  5793. end;
  5794. procedure TTestModule.TestAnonymousProc_Class;
  5795. begin
  5796. StartProgram(false);
  5797. Add([
  5798. 'type',
  5799. ' TProc = reference to procedure;',
  5800. ' TEvent = procedure of object;',
  5801. ' TObject = class',
  5802. ' Size: word;',
  5803. ' function GetIt: TProc;',
  5804. ' procedure DoIt; virtual; abstract;',
  5805. ' end;',
  5806. 'function TObject.GetIt: TProc;',
  5807. 'begin',
  5808. ' Result:=procedure',
  5809. ' var p: TEvent;',
  5810. ' begin',
  5811. ' Size:=Size;',
  5812. ' Size:=Self.Size;',
  5813. ' p:=@DoIt;',
  5814. ' p:[email protected];',
  5815. ' end;',
  5816. 'end;',
  5817. 'begin']);
  5818. ConvertProgram;
  5819. CheckSource('TestAnonymousProc_Class',
  5820. LinesToStr([ // statements
  5821. 'rtl.createClass(this, "TObject", null, function () {',
  5822. ' this.$init = function () {',
  5823. ' this.Size = 0;',
  5824. ' };',
  5825. ' this.$final = function () {',
  5826. ' };',
  5827. ' this.GetIt = function () {',
  5828. ' var $Self = this;',
  5829. ' var Result = null;',
  5830. ' Result = function () {',
  5831. ' var p = null;',
  5832. ' $Self.Size = $Self.Size;',
  5833. ' $Self.Size = $Self.Size;',
  5834. ' p = rtl.createCallback($Self, "DoIt");',
  5835. ' p = rtl.createCallback($Self, "DoIt");',
  5836. ' };',
  5837. ' return Result;',
  5838. ' };',
  5839. '});',
  5840. '']),
  5841. LinesToStr([
  5842. '']));
  5843. end;
  5844. procedure TTestModule.TestAnonymousProc_ForLoop;
  5845. begin
  5846. StartProgram(false);
  5847. Add([
  5848. 'type TProc = reference to procedure;',
  5849. 'procedure Foo(p: TProc);',
  5850. 'begin',
  5851. 'end;',
  5852. 'procedure DoIt;',
  5853. 'var i: word;',
  5854. ' a: word;',
  5855. 'begin',
  5856. ' for i:=1 to 10 do begin',
  5857. ' Foo(procedure begin a:=3; end);',
  5858. ' end;',
  5859. 'end;',
  5860. 'begin',
  5861. ' DoIt;']);
  5862. ConvertProgram;
  5863. CheckSource('TestAnonymousProc_ForLoop',
  5864. LinesToStr([ // statements
  5865. 'this.Foo = function (p) {',
  5866. '};',
  5867. 'this.DoIt = function () {',
  5868. ' var i = 0;',
  5869. ' var a = 0;',
  5870. ' for (i = 1; i <= 10; i++) {',
  5871. ' $mod.Foo(function () {',
  5872. ' a = 3;',
  5873. ' });',
  5874. ' };',
  5875. '};',
  5876. '']),
  5877. LinesToStr([
  5878. '$mod.DoIt();'
  5879. ]));
  5880. end;
  5881. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5882. begin
  5883. StartProgram(false);
  5884. Add([
  5885. '{$mode delphi}',
  5886. 'type',
  5887. ' TProc = reference to procedure;',
  5888. ' TFunc = reference to function(x: word): word;',
  5889. 'procedure Run;',
  5890. 'asm',
  5891. 'end;',
  5892. 'procedure Walk(p: TProc; f: TFunc);',
  5893. 'begin',
  5894. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5895. 'end;',
  5896. 'begin',
  5897. ' Walk(procedure',
  5898. ' asm',
  5899. ' console.log("a");',
  5900. ' end,',
  5901. ' function(x: word): word asm',
  5902. ' console.log("c");',
  5903. ' end);',
  5904. '']);
  5905. ConvertProgram;
  5906. CheckSource('TestAnonymousProc_AsmDelphi',
  5907. LinesToStr([ // statements
  5908. 'this.Run = function () {',
  5909. '};',
  5910. 'this.Walk = function (p, f) {',
  5911. ' $mod.Walk(function () {',
  5912. ' }, function (b) {',
  5913. ' return 1+b;',
  5914. ' });',
  5915. '};',
  5916. '']),
  5917. LinesToStr([
  5918. '$mod.Walk(function () {',
  5919. ' console.log("a");',
  5920. '}, function (x) {',
  5921. ' console.log("c");',
  5922. '});',
  5923. '']));
  5924. end;
  5925. procedure TTestModule.TestEnum_Name;
  5926. begin
  5927. StartProgram(false);
  5928. Add('type TMyEnum = (Red, Green, Blue);');
  5929. Add('var e: TMyEnum;');
  5930. Add('var f: TMyEnum = Blue;');
  5931. Add('begin');
  5932. Add(' e:=green;');
  5933. Add(' e:=default(TMyEnum);');
  5934. ConvertProgram;
  5935. CheckSource('TestEnum_Name',
  5936. LinesToStr([ // statements
  5937. 'this.TMyEnum = {',
  5938. ' "0":"Red",',
  5939. ' Red:0,',
  5940. ' "1":"Green",',
  5941. ' Green:1,',
  5942. ' "2":"Blue",',
  5943. ' Blue:2',
  5944. ' };',
  5945. 'this.e = 0;',
  5946. 'this.f = this.TMyEnum.Blue;'
  5947. ]),
  5948. LinesToStr([
  5949. '$mod.e=$mod.TMyEnum.Green;',
  5950. '$mod.e=$mod.TMyEnum.Red;'
  5951. ]));
  5952. end;
  5953. procedure TTestModule.TestEnum_Number;
  5954. begin
  5955. Converter.Options:=Converter.Options+[coEnumNumbers];
  5956. StartProgram(false);
  5957. Add('type TMyEnum = (Red, Green);');
  5958. Add('var');
  5959. Add(' e: TMyEnum;');
  5960. Add(' f: TMyEnum = Green;');
  5961. Add(' i: longint;');
  5962. Add('begin');
  5963. Add(' e:=green;');
  5964. Add(' i:=longint(e);');
  5965. ConvertProgram;
  5966. CheckSource('TestEnumNumber',
  5967. LinesToStr([ // statements
  5968. 'this.TMyEnum = {',
  5969. ' "0":"Red",',
  5970. ' Red:0,',
  5971. ' "1":"Green",',
  5972. ' Green:1',
  5973. ' };',
  5974. 'this.e = 0;',
  5975. 'this.f = 1;',
  5976. 'this.i = 0;'
  5977. ]),
  5978. LinesToStr([
  5979. '$mod.e=1;',
  5980. '$mod.i=$mod.e;'
  5981. ]));
  5982. end;
  5983. procedure TTestModule.TestEnum_ConstFail;
  5984. begin
  5985. StartProgram(false);
  5986. Add([
  5987. 'type TMyEnum = (Red = 100, Green = 101);',
  5988. 'var',
  5989. ' e: TMyEnum;',
  5990. ' f: TMyEnum = Green;',
  5991. 'begin',
  5992. ' e:=green;']);
  5993. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5994. ConvertProgram;
  5995. end;
  5996. procedure TTestModule.TestEnum_Functions;
  5997. begin
  5998. StartProgram(false);
  5999. Add([
  6000. 'type TMyEnum = (Red, Green);',
  6001. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6002. 'var',
  6003. ' v: longint;',
  6004. ' s: string;',
  6005. 'begin',
  6006. ' val(s,e,v);',
  6007. ' val(s,e,i);',
  6008. 'end;',
  6009. 'var',
  6010. ' e: TMyEnum;',
  6011. ' i: longint;',
  6012. ' s: string;',
  6013. ' b: boolean;',
  6014. 'begin',
  6015. ' i:=ord(red);',
  6016. ' i:=ord(green);',
  6017. ' i:=ord(e);',
  6018. ' i:=ord(b);',
  6019. ' e:=low(tmyenum);',
  6020. ' e:=low(e);',
  6021. ' b:=low(boolean);',
  6022. ' e:=high(tmyenum);',
  6023. ' e:=high(e);',
  6024. ' b:=high(boolean);',
  6025. ' e:=pred(green);',
  6026. ' e:=pred(e);',
  6027. ' b:=pred(b);',
  6028. ' e:=succ(red);',
  6029. ' e:=succ(e);',
  6030. ' b:=succ(b);',
  6031. ' e:=tmyenum(1);',
  6032. ' e:=tmyenum(i);',
  6033. ' s:=str(e);',
  6034. ' str(e,s);',
  6035. ' str(red,s);',
  6036. ' s:=str(e:3);',
  6037. ' writestr(s,e:3,red);',
  6038. ' val(s,e,i);',
  6039. ' i:=longint(e);']);
  6040. ConvertProgram;
  6041. CheckSource('TestEnum_Functions',
  6042. LinesToStr([ // statements
  6043. 'this.TMyEnum = {',
  6044. ' "0":"Red",',
  6045. ' Red:0,',
  6046. ' "1":"Green",',
  6047. ' Green:1',
  6048. ' };',
  6049. 'this.DoIt = function (e, i) {',
  6050. ' var v = 0;',
  6051. ' var s = "";',
  6052. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6053. ' v = w;',
  6054. ' }));',
  6055. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6056. '};',
  6057. 'this.e = 0;',
  6058. 'this.i = 0;',
  6059. 'this.s = "";',
  6060. 'this.b = false;',
  6061. '']),
  6062. LinesToStr([
  6063. '$mod.i=$mod.TMyEnum.Red;',
  6064. '$mod.i=$mod.TMyEnum.Green;',
  6065. '$mod.i=$mod.e;',
  6066. '$mod.i=$mod.b+0;',
  6067. '$mod.e=$mod.TMyEnum.Red;',
  6068. '$mod.e=$mod.TMyEnum.Red;',
  6069. '$mod.b=false;',
  6070. '$mod.e=$mod.TMyEnum.Green;',
  6071. '$mod.e=$mod.TMyEnum.Green;',
  6072. '$mod.b=true;',
  6073. '$mod.e=$mod.TMyEnum.Green-1;',
  6074. '$mod.e=$mod.e-1;',
  6075. '$mod.b=false;',
  6076. '$mod.e=$mod.TMyEnum.Red+1;',
  6077. '$mod.e=$mod.e+1;',
  6078. '$mod.b=true;',
  6079. '$mod.e=1;',
  6080. '$mod.e=$mod.i;',
  6081. '$mod.s = $mod.TMyEnum[$mod.e];',
  6082. '$mod.s = $mod.TMyEnum[$mod.e];',
  6083. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6084. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6085. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6086. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6087. ' $mod.i = v;',
  6088. '});',
  6089. '$mod.i=$mod.e;',
  6090. '']));
  6091. end;
  6092. procedure TTestModule.TestEnumRg_Functions;
  6093. begin
  6094. StartProgram(false);
  6095. Add([
  6096. 'type',
  6097. ' TEnum = (Red, Green, Blue);',
  6098. ' TEnumRg = Green..Blue;',
  6099. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6100. 'var',
  6101. ' v: longint;',
  6102. ' s: string;',
  6103. 'begin',
  6104. ' val(s,e,v);',
  6105. ' val(s,e,i);',
  6106. 'end;',
  6107. 'var',
  6108. ' e: TEnumRg;',
  6109. ' i: longint;',
  6110. ' s: string;',
  6111. 'begin',
  6112. ' i:=ord(green);',
  6113. ' i:=ord(e);',
  6114. ' e:=low(tenumrg);',
  6115. ' e:=low(e);',
  6116. ' e:=high(tenumrg);',
  6117. ' e:=high(e);',
  6118. ' e:=pred(blue);',
  6119. ' e:=pred(e);',
  6120. ' e:=succ(green);',
  6121. ' e:=succ(e);',
  6122. ' e:=tenumrg(1);',
  6123. ' e:=tenumrg(i);',
  6124. ' s:=str(e);',
  6125. ' str(e,s);',
  6126. ' str(red,s);',
  6127. ' s:=str(e:3);',
  6128. ' writestr(s,e:3,blue);',
  6129. ' val(s,e,i);',
  6130. ' i:=longint(e);']);
  6131. ConvertProgram;
  6132. CheckSource('TestEnumRg_Functions',
  6133. LinesToStr([ // statements
  6134. 'this.TEnum = {',
  6135. ' "0":"Red",',
  6136. ' Red:0,',
  6137. ' "1":"Green",',
  6138. ' Green:1,',
  6139. ' "2":"Blue",',
  6140. ' Blue:2',
  6141. ' };',
  6142. 'this.DoIt = function (e, i) {',
  6143. ' var v = 0;',
  6144. ' var s = "";',
  6145. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6146. ' v = w;',
  6147. ' }));',
  6148. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6149. '};',
  6150. 'this.e = this.TEnum.Green;',
  6151. 'this.i = 0;',
  6152. 'this.s = "";',
  6153. '']),
  6154. LinesToStr([
  6155. '$mod.i=$mod.TEnum.Green;',
  6156. '$mod.i=$mod.e;',
  6157. '$mod.e=$mod.TEnum.Green;',
  6158. '$mod.e=$mod.TEnum.Green;',
  6159. '$mod.e=$mod.TEnum.Blue;',
  6160. '$mod.e=$mod.TEnum.Blue;',
  6161. '$mod.e=$mod.TEnum.Blue-1;',
  6162. '$mod.e=$mod.e-1;',
  6163. '$mod.e=$mod.TEnum.Green+1;',
  6164. '$mod.e=$mod.e+1;',
  6165. '$mod.e=1;',
  6166. '$mod.e=$mod.i;',
  6167. '$mod.s = $mod.TEnum[$mod.e];',
  6168. '$mod.s = $mod.TEnum[$mod.e];',
  6169. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6170. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6171. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6172. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6173. ' $mod.i = v;',
  6174. '});',
  6175. '$mod.i=$mod.e;',
  6176. '']));
  6177. end;
  6178. procedure TTestModule.TestEnum_AsParams;
  6179. begin
  6180. StartProgram(false);
  6181. Add('type TEnum = (Red,Blue);');
  6182. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6183. Add('var vJ: TEnum;');
  6184. Add('begin');
  6185. Add(' vg:=vg;');
  6186. Add(' vj:=vh;');
  6187. Add(' vi:=vi;');
  6188. Add(' doit(vg,vg,vg);');
  6189. Add(' doit(vh,vh,vj);');
  6190. Add(' doit(vi,vi,vi);');
  6191. Add(' doit(vj,vj,vj);');
  6192. Add('end;');
  6193. Add('var i: TEnum;');
  6194. Add('begin');
  6195. Add(' doit(i,i,i);');
  6196. ConvertProgram;
  6197. CheckSource('TestEnum_AsParams',
  6198. LinesToStr([ // statements
  6199. 'this.TEnum = {',
  6200. ' "0": "Red",',
  6201. ' Red: 0,',
  6202. ' "1": "Blue",',
  6203. ' Blue: 1',
  6204. '};',
  6205. 'this.DoIt = function (vG,vH,vI) {',
  6206. ' var vJ = 0;',
  6207. ' vG = vG;',
  6208. ' vJ = vH;',
  6209. ' vI.set(vI.get());',
  6210. ' $mod.DoIt(vG, vG, {',
  6211. ' get: function () {',
  6212. ' return vG;',
  6213. ' },',
  6214. ' set: function (v) {',
  6215. ' vG = v;',
  6216. ' }',
  6217. ' });',
  6218. ' $mod.DoIt(vH, vH, {',
  6219. ' get: function () {',
  6220. ' return vJ;',
  6221. ' },',
  6222. ' set: function (v) {',
  6223. ' vJ = v;',
  6224. ' }',
  6225. ' });',
  6226. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6227. ' $mod.DoIt(vJ, vJ, {',
  6228. ' get: function () {',
  6229. ' return vJ;',
  6230. ' },',
  6231. ' set: function (v) {',
  6232. ' vJ = v;',
  6233. ' }',
  6234. ' });',
  6235. '};',
  6236. 'this.i = 0;'
  6237. ]),
  6238. LinesToStr([
  6239. '$mod.DoIt($mod.i,$mod.i,{',
  6240. ' p: $mod,',
  6241. ' get: function () {',
  6242. ' return this.p.i;',
  6243. ' },',
  6244. ' set: function (v) {',
  6245. ' this.p.i = v;',
  6246. ' }',
  6247. '});'
  6248. ]));
  6249. end;
  6250. procedure TTestModule.TestEnumRange_Array;
  6251. begin
  6252. StartProgram(false);
  6253. Add([
  6254. 'type',
  6255. ' TEnum = (Red, Green, Blue);',
  6256. ' TEnumRg = green..blue;',
  6257. ' TArr = array[TEnumRg] of byte;',
  6258. ' TArr2 = array[green..blue] of byte;',
  6259. 'var',
  6260. ' a: TArr;',
  6261. ' b: TArr = (3,4);',
  6262. ' c: TArr2 = (5,6);',
  6263. 'begin',
  6264. ' a[green] := b[blue];',
  6265. ' c[green] := c[blue];',
  6266. '']);
  6267. ConvertProgram;
  6268. CheckSource('TestEnumRange_Array',
  6269. LinesToStr([ // statements
  6270. 'this.TEnum = {',
  6271. ' "0": "Red",',
  6272. ' Red: 0,',
  6273. ' "1": "Green",',
  6274. ' Green: 1,',
  6275. ' "2": "Blue",',
  6276. ' Blue: 2',
  6277. '};',
  6278. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6279. 'this.b = [3, 4];',
  6280. 'this.c = [5, 6];',
  6281. '']),
  6282. LinesToStr([
  6283. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6284. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6285. '']));
  6286. end;
  6287. procedure TTestModule.TestEnum_ForIn;
  6288. begin
  6289. StartProgram(false);
  6290. Add([
  6291. 'type',
  6292. ' TEnum = (Red, Green, Blue);',
  6293. ' TEnumRg = green..blue;',
  6294. ' TArr = array[TEnum] of byte;',
  6295. ' TArrRg = array[TEnumRg] of byte;',
  6296. 'var',
  6297. ' e: TEnum;',
  6298. ' a1: TArr = (3,4,5);',
  6299. ' a2: TArrRg = (11,12);',
  6300. ' b: byte;',
  6301. 'begin',
  6302. ' for e in TEnum do ;',
  6303. ' for e in TEnumRg do ;',
  6304. ' for e in TArr do ;',
  6305. ' for e in TArrRg do ;',
  6306. ' for b in a1 do ;',
  6307. ' for b in a2 do ;',
  6308. '']);
  6309. ConvertProgram;
  6310. CheckSource('TestEnum_ForIn',
  6311. LinesToStr([ // statements
  6312. 'this.TEnum = {',
  6313. ' "0": "Red",',
  6314. ' Red: 0,',
  6315. ' "1": "Green",',
  6316. ' Green: 1,',
  6317. ' "2": "Blue",',
  6318. ' Blue: 2',
  6319. '};',
  6320. 'this.e = 0;',
  6321. 'this.a1 = [3, 4, 5];',
  6322. 'this.a2 = [11, 12];',
  6323. 'this.b = 0;',
  6324. '']),
  6325. LinesToStr([
  6326. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6327. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6328. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6329. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6330. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6331. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6332. '']));
  6333. end;
  6334. procedure TTestModule.TestEnum_ScopedNumber;
  6335. begin
  6336. Converter.Options:=Converter.Options+[coEnumNumbers];
  6337. StartProgram(false);
  6338. Add([
  6339. 'type',
  6340. ' TEnum = (Red, Green);',
  6341. 'var',
  6342. ' e: TEnum;',
  6343. 'begin',
  6344. ' e:=TEnum.Green;',
  6345. '']);
  6346. ConvertProgram;
  6347. CheckSource('TestEnum_ScopedNumber',
  6348. LinesToStr([ // statements
  6349. 'this.TEnum = {',
  6350. ' "0": "Red",',
  6351. ' Red: 0,',
  6352. ' "1": "Green",',
  6353. ' Green: 1',
  6354. '};',
  6355. 'this.e = 0;',
  6356. '']),
  6357. LinesToStr([
  6358. '$mod.e = 1;']));
  6359. end;
  6360. procedure TTestModule.TestEnum_InFunction;
  6361. begin
  6362. StartProgram(false);
  6363. Add([
  6364. 'const TEnum = 3;',
  6365. 'procedure DoIt;',
  6366. 'type',
  6367. ' TEnum = (Red, Green, Blue);',
  6368. ' procedure Sub;',
  6369. ' type',
  6370. ' TEnumSub = (Left, Right);',
  6371. ' var',
  6372. ' es: TEnumSub;',
  6373. ' begin',
  6374. ' es:=Left;',
  6375. ' end;',
  6376. 'var',
  6377. ' e, e2: TEnum;',
  6378. 'begin',
  6379. ' if e in [red,blue] then e2:=e;',
  6380. 'end;',
  6381. 'begin']);
  6382. ConvertProgram;
  6383. CheckSource('TestEnum_InFunction',
  6384. LinesToStr([ // statements
  6385. 'this.TEnum = 3;',
  6386. 'var TEnum$1 = {',
  6387. ' "0":"Red",',
  6388. ' Red:0,',
  6389. ' "1":"Green",',
  6390. ' Green:1,',
  6391. ' "2":"Blue",',
  6392. ' Blue:2',
  6393. ' };',
  6394. 'var TEnumSub = {',
  6395. ' "0": "Left",',
  6396. ' Left: 0,',
  6397. ' "1": "Right",',
  6398. ' Right: 1',
  6399. '};',
  6400. 'this.DoIt = function () {',
  6401. ' function Sub() {',
  6402. ' var es = 0;',
  6403. ' es = TEnumSub.Left;',
  6404. ' };',
  6405. ' var e = 0;',
  6406. ' var e2 = 0;',
  6407. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6408. '};',
  6409. '']),
  6410. LinesToStr([
  6411. '']));
  6412. end;
  6413. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6414. begin
  6415. StartUnit(true);
  6416. Add([
  6417. 'interface',
  6418. 'var color: (red, green);',
  6419. 'implementation',
  6420. 'initialization',
  6421. ' color:=green;',
  6422. '']);
  6423. ConvertUnit;
  6424. CheckSource('TestEnum_Name_Anonymous_Unit',
  6425. LinesToStr([
  6426. 'this.color$a = {',
  6427. ' "0": "red",',
  6428. ' red: 0,',
  6429. ' "1": "green",',
  6430. ' green: 1',
  6431. '};',
  6432. 'this.color = 0;',
  6433. '']),
  6434. LinesToStr([ // this.$init
  6435. '$mod.color = $mod.color$a.green;',
  6436. '']),
  6437. LinesToStr([ // implementation
  6438. '']) );
  6439. end;
  6440. procedure TTestModule.TestSet_Enum;
  6441. begin
  6442. StartProgram(false);
  6443. Add([
  6444. 'type',
  6445. ' TColor = (Red, Green, Blue);',
  6446. ' TColors = set of TColor;',
  6447. 'var',
  6448. ' c: TColor;',
  6449. ' s: TColors;',
  6450. ' t: TColors = [];',
  6451. ' u: TColors = [Red];',
  6452. 'begin',
  6453. ' s:=[];',
  6454. ' s:=[Green];',
  6455. ' s:=[Green,Blue];',
  6456. ' s:=[Red..Blue];',
  6457. ' s:=[Red,Green..Blue];',
  6458. ' s:=[Red,c];',
  6459. ' s:=t;',
  6460. ' s:=default(TColors);',
  6461. '']);
  6462. ConvertProgram;
  6463. CheckSource('TestSet',
  6464. LinesToStr([ // statements
  6465. 'this.TColor = {',
  6466. ' "0":"Red",',
  6467. ' Red:0,',
  6468. ' "1":"Green",',
  6469. ' Green:1,',
  6470. ' "2":"Blue",',
  6471. ' Blue:2',
  6472. ' };',
  6473. 'this.c = 0;',
  6474. 'this.s = {};',
  6475. 'this.t = {};',
  6476. 'this.u = rtl.createSet(this.TColor.Red);'
  6477. ]),
  6478. LinesToStr([
  6479. '$mod.s={};',
  6480. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6481. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6482. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6483. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6484. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6485. '$mod.s=rtl.refSet($mod.t);',
  6486. '$mod.s={};',
  6487. '']));
  6488. end;
  6489. procedure TTestModule.TestSet_Operators;
  6490. begin
  6491. StartProgram(false);
  6492. Add('type');
  6493. Add(' TColor = (Red, Green, Blue);');
  6494. Add(' TColors = set of tcolor;');
  6495. Add('var');
  6496. Add(' vC: TColor;');
  6497. Add(' vS: TColors;');
  6498. Add(' vT: TColors;');
  6499. Add(' vU: TColors;');
  6500. Add(' B: boolean;');
  6501. Add('begin');
  6502. Add(' include(vs,green);');
  6503. Add(' exclude(vs,vc);');
  6504. Add(' vs:=vt+vu;');
  6505. Add(' vs:=vt+[red];');
  6506. Add(' vs:=[red]+vt;');
  6507. Add(' vs:=[red]+[green];');
  6508. Add(' vs:=vt-vu;');
  6509. Add(' vs:=vt-[red];');
  6510. Add(' vs:=[red]-vt;');
  6511. Add(' vs:=[red]-[green];');
  6512. Add(' vs:=vt*vu;');
  6513. Add(' vs:=vt*[red];');
  6514. Add(' vs:=[red]*vt;');
  6515. Add(' vs:=[red]*[green];');
  6516. Add(' vs:=vt><vu;');
  6517. Add(' vs:=vt><[red];');
  6518. Add(' vs:=[red]><vt;');
  6519. Add(' vs:=[red]><[green];');
  6520. Add(' b:=vt=vu;');
  6521. Add(' b:=vt=[red];');
  6522. Add(' b:=[red]=vt;');
  6523. Add(' b:=[red]=[green];');
  6524. Add(' b:=vt<>vu;');
  6525. Add(' b:=vt<>[red];');
  6526. Add(' b:=[red]<>vt;');
  6527. Add(' b:=[red]<>[green];');
  6528. Add(' b:=vt<=vu;');
  6529. Add(' b:=vt<=[red];');
  6530. Add(' b:=[red]<=vt;');
  6531. Add(' b:=[red]<=[green];');
  6532. Add(' b:=vt>=vu;');
  6533. Add(' b:=vt>=[red];');
  6534. Add(' b:=[red]>=vt;');
  6535. Add(' b:=[red]>=[green];');
  6536. ConvertProgram;
  6537. CheckSource('TestSet_Operators',
  6538. LinesToStr([ // statements
  6539. 'this.TColor = {',
  6540. ' "0":"Red",',
  6541. ' Red:0,',
  6542. ' "1":"Green",',
  6543. ' Green:1,',
  6544. ' "2":"Blue",',
  6545. ' Blue:2',
  6546. ' };',
  6547. 'this.vC = 0;',
  6548. 'this.vS = {};',
  6549. 'this.vT = {};',
  6550. 'this.vU = {};',
  6551. 'this.B = false;'
  6552. ]),
  6553. LinesToStr([
  6554. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6555. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6556. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6557. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6558. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6559. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6560. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6561. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6562. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6563. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6564. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6565. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6566. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6567. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6568. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6569. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6570. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6571. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6572. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6573. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6574. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6575. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6576. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6577. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6578. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6579. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6580. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6581. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6582. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6583. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6584. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6585. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6586. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6587. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6588. '']));
  6589. end;
  6590. procedure TTestModule.TestSet_Operator_In;
  6591. begin
  6592. StartProgram(false);
  6593. Add([
  6594. 'type',
  6595. ' TColor = (Red, Green, Blue);',
  6596. ' TColors = set of tcolor;',
  6597. ' TColorRg = green..blue;',
  6598. 'var',
  6599. ' vC: tcolor;',
  6600. ' vT: tcolors;',
  6601. ' B: boolean;',
  6602. ' rg: TColorRg;',
  6603. 'begin',
  6604. ' b:=red in vt;',
  6605. ' b:=vc in vt;',
  6606. ' b:=green in [red..blue];',
  6607. ' b:=vc in [red..blue];',
  6608. ' ',
  6609. ' if red in vt then ;',
  6610. ' while vC in vt do ;',
  6611. ' repeat',
  6612. ' until vC in vt;',
  6613. ' if rg in [green..blue] then ;',
  6614. '']);
  6615. ConvertProgram;
  6616. CheckSource('TestSet_Operator_In',
  6617. LinesToStr([ // statements
  6618. 'this.TColor = {',
  6619. ' "0":"Red",',
  6620. ' Red:0,',
  6621. ' "1":"Green",',
  6622. ' Green:1,',
  6623. ' "2":"Blue",',
  6624. ' Blue:2',
  6625. ' };',
  6626. 'this.vC = 0;',
  6627. 'this.vT = {};',
  6628. 'this.B = false;',
  6629. 'this.rg = this.TColor.Green;',
  6630. '']),
  6631. LinesToStr([
  6632. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6633. '$mod.B = $mod.vC in $mod.vT;',
  6634. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6635. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6636. 'if ($mod.TColor.Red in $mod.vT) ;',
  6637. 'while ($mod.vC in $mod.vT) {',
  6638. '};',
  6639. 'do {',
  6640. '} while (!($mod.vC in $mod.vT));',
  6641. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6642. '']));
  6643. end;
  6644. procedure TTestModule.TestSet_Functions;
  6645. begin
  6646. StartProgram(false);
  6647. Add('type');
  6648. Add(' TMyEnum = (Red, Green);');
  6649. Add(' TMyEnums = set of TMyEnum;');
  6650. Add('var');
  6651. Add(' e: TMyEnum;');
  6652. Add(' s: TMyEnums;');
  6653. Add('begin');
  6654. Add(' e:=Low(TMyEnums);');
  6655. Add(' e:=Low(s);');
  6656. Add(' e:=High(TMyEnums);');
  6657. Add(' e:=High(s);');
  6658. ConvertProgram;
  6659. CheckSource('TestSetFunctions',
  6660. LinesToStr([ // statements
  6661. 'this.TMyEnum = {',
  6662. ' "0":"Red",',
  6663. ' Red:0,',
  6664. ' "1":"Green",',
  6665. ' Green:1',
  6666. ' };',
  6667. 'this.e = 0;',
  6668. 'this.s = {};'
  6669. ]),
  6670. LinesToStr([
  6671. '$mod.e=$mod.TMyEnum.Red;',
  6672. '$mod.e=$mod.TMyEnum.Red;',
  6673. '$mod.e=$mod.TMyEnum.Green;',
  6674. '$mod.e=$mod.TMyEnum.Green;',
  6675. '']));
  6676. end;
  6677. procedure TTestModule.TestSet_PassAsArgClone;
  6678. begin
  6679. StartProgram(false);
  6680. Add('type');
  6681. Add(' TMyEnum = (Red, Green);');
  6682. Add(' TMyEnums = set of TMyEnum;');
  6683. Add('procedure DoDefault(s: tmyenums); begin end;');
  6684. Add('procedure DoConst(const s: tmyenums); begin end;');
  6685. Add('var');
  6686. Add(' aSet: tmyenums;');
  6687. Add('begin');
  6688. Add(' dodefault(aset);');
  6689. Add(' doconst(aset);');
  6690. ConvertProgram;
  6691. CheckSource('TestSetFunctions',
  6692. LinesToStr([ // statements
  6693. 'this.TMyEnum = {',
  6694. ' "0":"Red",',
  6695. ' Red:0,',
  6696. ' "1":"Green",',
  6697. ' Green:1',
  6698. ' };',
  6699. 'this.DoDefault = function (s) {',
  6700. '};',
  6701. 'this.DoConst = function (s) {',
  6702. '};',
  6703. 'this.aSet = {};'
  6704. ]),
  6705. LinesToStr([
  6706. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6707. '$mod.DoConst($mod.aSet);',
  6708. '']));
  6709. end;
  6710. procedure TTestModule.TestSet_AsParams;
  6711. begin
  6712. StartProgram(false);
  6713. Add([
  6714. 'type TEnum = (Red,Blue);',
  6715. 'type TEnums = set of TEnum;',
  6716. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6717. 'var vJ: TEnums;',
  6718. 'begin',
  6719. ' Include(vg,red);',
  6720. ' Include(result,blue);',
  6721. ' vg:=vg;',
  6722. ' vj:=vh;',
  6723. ' vi:=vi;',
  6724. ' doit(vg,vg,vg);',
  6725. ' doit(vh,vh,vj);',
  6726. ' doit(vi,vi,vi);',
  6727. ' doit(vj,vj,vj);',
  6728. 'end;',
  6729. 'var i: TEnums;',
  6730. 'begin',
  6731. ' doit(i,i,i);']);
  6732. ConvertProgram;
  6733. CheckSource('TestSet_AsParams',
  6734. LinesToStr([ // statements
  6735. 'this.TEnum = {',
  6736. ' "0": "Red",',
  6737. ' Red: 0,',
  6738. ' "1": "Blue",',
  6739. ' Blue: 1',
  6740. '};',
  6741. 'this.DoIt = function (vG,vH,vI) {',
  6742. ' var Result = {};',
  6743. ' var vJ = {};',
  6744. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6745. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6746. ' vG = rtl.refSet(vG);',
  6747. ' vJ = rtl.refSet(vH);',
  6748. ' vI.set(rtl.refSet(vI.get()));',
  6749. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6750. ' get: function () {',
  6751. ' return vG;',
  6752. ' },',
  6753. ' set: function (v) {',
  6754. ' vG = v;',
  6755. ' }',
  6756. ' });',
  6757. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6758. ' get: function () {',
  6759. ' return vJ;',
  6760. ' },',
  6761. ' set: function (v) {',
  6762. ' vJ = v;',
  6763. ' }',
  6764. ' });',
  6765. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6766. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6767. ' get: function () {',
  6768. ' return vJ;',
  6769. ' },',
  6770. ' set: function (v) {',
  6771. ' vJ = v;',
  6772. ' }',
  6773. ' });',
  6774. ' return Result;',
  6775. '};',
  6776. 'this.i = {};'
  6777. ]),
  6778. LinesToStr([
  6779. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6780. ' p: $mod,',
  6781. ' get: function () {',
  6782. ' return this.p.i;',
  6783. ' },',
  6784. ' set: function (v) {',
  6785. ' this.p.i = v;',
  6786. ' }',
  6787. '});'
  6788. ]));
  6789. end;
  6790. procedure TTestModule.TestSet_Property;
  6791. begin
  6792. StartProgram(false);
  6793. Add('type');
  6794. Add(' TEnum = (Red,Blue);');
  6795. Add(' TEnums = set of TEnum;');
  6796. Add(' TObject = class');
  6797. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6798. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6799. Add(' property Colors: TEnums read GetColors write SetColors;');
  6800. Add(' end;');
  6801. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6802. Add('begin end;');
  6803. Add('var Obj: TObject;');
  6804. Add('begin');
  6805. Add(' Include(Obj.Colors,Red);');
  6806. Add(' Exclude(Obj.Colors,Red);');
  6807. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6808. ConvertProgram;
  6809. CheckSource('TestSet_Property',
  6810. LinesToStr([ // statements
  6811. 'this.TEnum = {',
  6812. ' "0": "Red",',
  6813. ' Red: 0,',
  6814. ' "1": "Blue",',
  6815. ' Blue: 1',
  6816. '};',
  6817. 'rtl.createClass(this, "TObject", null, function () {',
  6818. ' this.$init = function () {',
  6819. ' };',
  6820. ' this.$final = function () {',
  6821. ' };',
  6822. '});',
  6823. 'this.DoIt = function (i, j, k, l) {',
  6824. '};',
  6825. 'this.Obj = null;',
  6826. '']),
  6827. LinesToStr([
  6828. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6829. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6830. '']));
  6831. end;
  6832. procedure TTestModule.TestSet_EnumConst;
  6833. begin
  6834. StartProgram(false);
  6835. Add([
  6836. 'type',
  6837. ' TEnum = (Red,Blue);',
  6838. ' TEnums = set of TEnum;',
  6839. 'const',
  6840. ' Orange = red;',
  6841. 'var',
  6842. ' Enum: tenum;',
  6843. ' Enums: tenums;',
  6844. 'begin',
  6845. ' Include(enums,orange);',
  6846. ' Exclude(enums,orange);',
  6847. ' if orange in enums then;',
  6848. ' if orange in [orange,red] then;']);
  6849. ConvertProgram;
  6850. CheckSource('TestSet_EnumConst',
  6851. LinesToStr([ // statements
  6852. 'this.TEnum = {',
  6853. ' "0": "Red",',
  6854. ' Red: 0,',
  6855. ' "1": "Blue",',
  6856. ' Blue: 1',
  6857. '};',
  6858. 'this.Orange = this.TEnum.Red;',
  6859. 'this.Enum = 0;',
  6860. 'this.Enums = {};',
  6861. '']),
  6862. LinesToStr([
  6863. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6864. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6865. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6866. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6867. '']));
  6868. end;
  6869. procedure TTestModule.TestSet_IntConst;
  6870. begin
  6871. StartProgram(false);
  6872. Add([
  6873. 'type',
  6874. ' TEnums = set of Byte;',
  6875. 'const',
  6876. ' Orange = 0;',
  6877. 'var',
  6878. ' Enum: byte;',
  6879. ' Enums: tenums;',
  6880. 'begin',
  6881. ' Enums:=[];',
  6882. ' Enums:=[0];',
  6883. ' Enums:=[1..2];',
  6884. //' Include(enums,orange);',
  6885. //' Exclude(enums,orange);',
  6886. ' if orange in enums then;',
  6887. ' if orange in [orange,1] then;']);
  6888. ConvertProgram;
  6889. CheckSource('TestSet_IntConst',
  6890. LinesToStr([ // statements
  6891. 'this.Orange = 0;',
  6892. 'this.Enum = 0;',
  6893. 'this.Enums = {};',
  6894. '']),
  6895. LinesToStr([
  6896. '$mod.Enums = {};',
  6897. '$mod.Enums = rtl.createSet(0);',
  6898. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6899. 'if (0 in $mod.Enums) ;',
  6900. 'if (0 in rtl.createSet(0, 1)) ;',
  6901. '']));
  6902. end;
  6903. procedure TTestModule.TestSet_IntRange;
  6904. begin
  6905. StartProgram(false);
  6906. Add([
  6907. 'type',
  6908. ' TRange = 1..3;',
  6909. ' TEnums = set of TRange;',
  6910. 'const',
  6911. ' Orange = 2;',
  6912. 'var',
  6913. ' Enum: byte;',
  6914. ' Enums: TEnums;',
  6915. 'begin',
  6916. ' Enums:=[];',
  6917. ' Enums:=[1];',
  6918. ' Enums:=[2..3];',
  6919. ' Include(enums,orange);',
  6920. ' Exclude(enums,orange);',
  6921. ' if orange in enums then;',
  6922. ' if orange in [orange,1] then;']);
  6923. ConvertProgram;
  6924. CheckSource('TestSet_IntRange',
  6925. LinesToStr([ // statements
  6926. 'this.Orange = 2;',
  6927. 'this.Enum = 0;',
  6928. 'this.Enums = {};',
  6929. '']),
  6930. LinesToStr([
  6931. '$mod.Enums = {};',
  6932. '$mod.Enums = rtl.createSet(1);',
  6933. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6934. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6935. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6936. 'if (2 in $mod.Enums) ;',
  6937. 'if (2 in rtl.createSet(2, 1)) ;',
  6938. '']));
  6939. end;
  6940. procedure TTestModule.TestSet_AnonymousEnumType;
  6941. begin
  6942. StartProgram(false);
  6943. Add('type');
  6944. Add(' TFlags = set of (red, green);');
  6945. Add('const');
  6946. Add(' favorite = red;');
  6947. Add('var');
  6948. Add(' f: TFlags;');
  6949. Add(' i: longint;');
  6950. Add('begin');
  6951. Add(' Include(f,red);');
  6952. Add(' Include(f,favorite);');
  6953. Add(' i:=ord(red);');
  6954. Add(' i:=ord(favorite);');
  6955. Add(' i:=ord(low(TFlags));');
  6956. Add(' i:=ord(low(f));');
  6957. Add(' i:=ord(low(favorite));');
  6958. Add(' i:=ord(high(TFlags));');
  6959. Add(' i:=ord(high(f));');
  6960. Add(' i:=ord(high(favorite));');
  6961. Add(' f:=[green,favorite];');
  6962. ConvertProgram;
  6963. CheckSource('TestSet_AnonymousEnumType',
  6964. LinesToStr([ // statements
  6965. 'this.TFlags$a = {',
  6966. ' "0": "red",',
  6967. ' red: 0,',
  6968. ' "1": "green",',
  6969. ' green: 1',
  6970. '};',
  6971. 'this.favorite = this.TFlags$a.red;',
  6972. 'this.f = {};',
  6973. 'this.i = 0;',
  6974. '']),
  6975. LinesToStr([
  6976. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6977. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6978. '$mod.i = $mod.TFlags$a.red;',
  6979. '$mod.i = $mod.TFlags$a.red;',
  6980. '$mod.i = $mod.TFlags$a.red;',
  6981. '$mod.i = $mod.TFlags$a.red;',
  6982. '$mod.i = $mod.TFlags$a.red;',
  6983. '$mod.i = $mod.TFlags$a.green;',
  6984. '$mod.i = $mod.TFlags$a.green;',
  6985. '$mod.i = $mod.TFlags$a.green;',
  6986. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6987. '']));
  6988. end;
  6989. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6990. begin
  6991. exit;
  6992. StartProgram(false);
  6993. Add([
  6994. 'type',
  6995. ' TAtoZ = ''A''..''Z'';',
  6996. ' TSetOfAZ = set of TAtoZ;',
  6997. 'var',
  6998. ' c: char;',
  6999. ' a: TAtoZ;',
  7000. ' s: TSetOfAZ = [''P'',''A''];',
  7001. ' i: longint;',
  7002. 'begin',
  7003. ' Include(s,''S'');',
  7004. ' Include(s,c);',
  7005. ' Include(s,a);',
  7006. ' c:=low(TAtoZ);',
  7007. ' i:=ord(low(TAtoZ));',
  7008. ' a:=high(TAtoZ);',
  7009. ' a:=high(TSetOfAtoZ);',
  7010. ' s:=[a,c,''M''];',
  7011. '']);
  7012. ConvertProgram;
  7013. CheckSource('TestSet_AnonymousEnumTypeChar',
  7014. LinesToStr([ // statements
  7015. '']),
  7016. LinesToStr([
  7017. '']));
  7018. end;
  7019. procedure TTestModule.TestSet_ConstEnum;
  7020. begin
  7021. StartProgram(false);
  7022. Add([
  7023. 'type',
  7024. ' TEnum = (red,blue,green);',
  7025. ' TEnums = set of TEnum;',
  7026. 'const',
  7027. ' teAny = [low(TEnum)..high(TEnum)];',
  7028. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7029. 'var',
  7030. ' e: TEnum;',
  7031. ' s: TEnums;',
  7032. 'begin',
  7033. ' if blue in teAny then;',
  7034. ' if blue in teAny+[e] then;',
  7035. ' if blue in teAny+teRedBlue then;',
  7036. ' if e in [red,blue] then;',
  7037. ' s:=teAny;',
  7038. ' s:=teAny+[e];',
  7039. ' s:=[e]+teAny;',
  7040. ' s:=teAny+teRedBlue;',
  7041. ' s:=teAny+teRedBlue+[e];',
  7042. '']);
  7043. ConvertProgram;
  7044. CheckSource('TestSet_ConstEnum',
  7045. LinesToStr([ // statements
  7046. 'this.TEnum = {',
  7047. ' "0": "red",',
  7048. ' red: 0,',
  7049. ' "1": "blue",',
  7050. ' blue: 1,',
  7051. ' "2": "green",',
  7052. ' green: 2',
  7053. '};',
  7054. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7055. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7056. 'this.e = 0;',
  7057. 'this.s = {};',
  7058. '']),
  7059. LinesToStr([
  7060. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7061. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7062. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7063. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7064. '$mod.s = rtl.refSet($mod.teAny);',
  7065. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7066. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7067. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7068. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7069. '']));
  7070. end;
  7071. procedure TTestModule.TestSet_ConstChar;
  7072. begin
  7073. StartProgram(false);
  7074. Add([
  7075. 'const',
  7076. ' LowChars = [''a''..''z''];',
  7077. ' Chars = LowChars+[''A''..''Z''];',
  7078. ' sc = [''А'', ''Я''];',
  7079. 'var',
  7080. ' c: char;',
  7081. ' s: string;',
  7082. 'begin',
  7083. ' if c in lowchars then ;',
  7084. ' if ''a'' in lowchars then ;',
  7085. ' if s[1] in lowchars then ;',
  7086. ' if c in chars then ;',
  7087. ' if c in [''a''..''z'',''_''] then ;',
  7088. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7089. ' if ''Я'' in sc then ;',
  7090. ' if 3=ord('' '') then ;',
  7091. '']);
  7092. ConvertProgram;
  7093. CheckSource('TestSet_ConstChar',
  7094. LinesToStr([ // statements
  7095. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7096. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7097. 'this.sc = rtl.createSet(1040, 1071);',
  7098. 'this.c = "";',
  7099. 'this.s = "";',
  7100. '']),
  7101. LinesToStr([
  7102. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7103. 'if (97 in $mod.LowChars) ;',
  7104. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7105. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7106. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7107. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7108. 'if (1071 in $mod.sc) ;',
  7109. 'if (3 === 32) ;',
  7110. '']));
  7111. end;
  7112. procedure TTestModule.TestSet_ConstInt;
  7113. begin
  7114. StartProgram(false);
  7115. Add([
  7116. 'const',
  7117. ' Months = [1..12];',
  7118. ' Mirror = [-12..-1]+Months;',
  7119. 'var',
  7120. ' i: smallint;',
  7121. 'begin',
  7122. ' if 3 in Months then;',
  7123. ' if i in Months+[i] then;',
  7124. ' if i in Months+Mirror then;',
  7125. ' if i in [4..6,8] then;',
  7126. '']);
  7127. ConvertProgram;
  7128. CheckSource('TestSet_ConstInt',
  7129. LinesToStr([ // statements
  7130. 'this.Months = rtl.createSet(null, 1, 12);',
  7131. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7132. 'this.i = 0;',
  7133. '']),
  7134. LinesToStr([
  7135. 'if (3 in $mod.Months) ;',
  7136. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7137. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7138. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7139. '']));
  7140. end;
  7141. procedure TTestModule.TestSet_InFunction;
  7142. begin
  7143. StartProgram(false);
  7144. Add([
  7145. 'const',
  7146. ' TEnum = 3;',
  7147. ' TSetOfEnum = 4;',
  7148. ' TSetOfAno = 5;',
  7149. 'procedure DoIt;',
  7150. 'type',
  7151. ' TEnum = (red, blue);',
  7152. ' TSetOfEnum = set of TEnum;',
  7153. ' TSetOfAno = set of (up,down);',
  7154. 'var',
  7155. ' e: TEnum;',
  7156. ' se: TSetOfEnum;',
  7157. ' sa: TSetOfAno;',
  7158. 'begin',
  7159. ' se:=[e];',
  7160. ' sa:=[up];',
  7161. 'end;',
  7162. 'begin',
  7163. '']);
  7164. ConvertProgram;
  7165. CheckSource('TestSet_InFunction',
  7166. LinesToStr([ // statements
  7167. 'this.TEnum = 3;',
  7168. 'this.TSetOfEnum = 4;',
  7169. 'this.TSetOfAno = 5;',
  7170. 'var TEnum$1 = {',
  7171. ' "0": "red",',
  7172. ' red: 0,',
  7173. ' "1": "blue",',
  7174. ' blue: 1',
  7175. '};',
  7176. 'var TSetOfAno$a = {',
  7177. ' "0": "up",',
  7178. ' up: 0,',
  7179. ' "1": "down",',
  7180. ' down: 1',
  7181. '};',
  7182. 'this.DoIt = function () {',
  7183. ' var e = 0;',
  7184. ' var se = {};',
  7185. ' var sa = {};',
  7186. ' se = rtl.createSet(e);',
  7187. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7188. '};',
  7189. '']),
  7190. LinesToStr([
  7191. '']));
  7192. end;
  7193. procedure TTestModule.TestSet_ForIn;
  7194. begin
  7195. StartProgram(false);
  7196. Add([
  7197. 'type',
  7198. ' TEnum = (Red, Green, Blue);',
  7199. ' TEnumRg = green..blue;',
  7200. ' TSetOfEnum = set of TEnum;',
  7201. ' TSetOfEnumRg = set of TEnumRg;',
  7202. 'var',
  7203. ' e, e2: TEnum;',
  7204. ' er: TEnum;',
  7205. ' s: TSetOfEnum;',
  7206. 'begin',
  7207. ' for e in TSetOfEnum do ;',
  7208. ' for e in TSetOfEnumRg do ;',
  7209. ' for e in [] do e2:=e;',
  7210. ' for e in [red..green] do e2:=e;',
  7211. ' for e in [green,blue] do e2:=e;',
  7212. ' for e in [red,blue] do e2:=e;',
  7213. ' for e in s do e2:=e;',
  7214. ' for er in TSetOfEnumRg do ;',
  7215. '']);
  7216. ConvertProgram;
  7217. CheckSource('TestSet_ForIn',
  7218. LinesToStr([ // statements
  7219. 'this.TEnum = {',
  7220. ' "0":"Red",',
  7221. ' Red:0,',
  7222. ' "1":"Green",',
  7223. ' Green:1,',
  7224. ' "2":"Blue",',
  7225. ' Blue:2',
  7226. ' };',
  7227. 'this.e = 0;',
  7228. 'this.e2 = 0;',
  7229. 'this.er = 0;',
  7230. 'this.s = {};',
  7231. '']),
  7232. LinesToStr([
  7233. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7234. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7235. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7236. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7237. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7238. 'for (var $l in $mod.s){',
  7239. ' $mod.e = +$l;',
  7240. ' $mod.e2 = $mod.e;',
  7241. '};',
  7242. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7243. '']));
  7244. end;
  7245. procedure TTestModule.TestNestBegin;
  7246. begin
  7247. StartProgram(false);
  7248. Add('begin');
  7249. Add(' begin');
  7250. Add(' begin');
  7251. Add(' end;');
  7252. Add(' begin');
  7253. Add(' if true then ;');
  7254. Add(' end;');
  7255. Add(' end;');
  7256. ConvertProgram;
  7257. CheckSource('TestNestBegin',
  7258. '',
  7259. 'if (true) ;');
  7260. end;
  7261. procedure TTestModule.TestUnitImplVars;
  7262. begin
  7263. StartUnit(false);
  7264. Add('interface');
  7265. Add('implementation');
  7266. Add('var');
  7267. Add(' V1:longint;');
  7268. Add(' V2:longint = 3;');
  7269. Add(' V3:string = ''abc'';');
  7270. ConvertUnit;
  7271. CheckSource('TestUnitImplVars',
  7272. LinesToStr([ // statements
  7273. 'var $impl = $mod.$impl;',
  7274. '']),
  7275. '', // this.$init
  7276. LinesToStr([ // implementation
  7277. '$impl.V1 = 0;',
  7278. '$impl.V2 = 3;',
  7279. '$impl.V3 = "abc";',
  7280. '']) );
  7281. end;
  7282. procedure TTestModule.TestUnitImplConsts;
  7283. begin
  7284. StartUnit(false);
  7285. Add('interface');
  7286. Add('implementation');
  7287. Add('const');
  7288. Add(' v1 = 3;');
  7289. Add(' v2:longint = 4;');
  7290. Add(' v3:string = ''abc'';');
  7291. ConvertUnit;
  7292. CheckSource('TestUnitImplConsts',
  7293. LinesToStr([ // statements
  7294. 'var $impl = $mod.$impl;',
  7295. '']),
  7296. '', // this.$init
  7297. LinesToStr([ // implementation
  7298. '$impl.v1 = 3;',
  7299. '$impl.v2 = 4;',
  7300. '$impl.v3 = "abc";',
  7301. '']) );
  7302. end;
  7303. procedure TTestModule.TestUnitImplRecord;
  7304. begin
  7305. StartUnit(false);
  7306. Add('interface');
  7307. Add('implementation');
  7308. Add('type');
  7309. Add(' TMyRecord = record');
  7310. Add(' i: longint;');
  7311. Add(' end;');
  7312. Add('var aRec: TMyRecord;');
  7313. Add('initialization');
  7314. Add(' arec.i:=3;');
  7315. ConvertUnit;
  7316. CheckSource('TestUnitImplRecord',
  7317. LinesToStr([ // statements
  7318. 'var $impl = $mod.$impl;',
  7319. '']),
  7320. // this.$init
  7321. '$impl.aRec.i = 3;',
  7322. LinesToStr([ // implementation
  7323. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7324. ' this.i = 0;',
  7325. ' this.$eq = function (b) {',
  7326. ' return this.i === b.i;',
  7327. ' };',
  7328. ' this.$assign = function (s) {',
  7329. ' this.i = s.i;',
  7330. ' return this;',
  7331. ' };',
  7332. '});',
  7333. '$impl.aRec = $impl.TMyRecord.$new();',
  7334. '']) );
  7335. end;
  7336. procedure TTestModule.TestRenameJSNameConflict;
  7337. begin
  7338. StartProgram(false);
  7339. Add('var apply: longint;');
  7340. Add('var bind: longint;');
  7341. Add('var call: longint;');
  7342. Add('begin');
  7343. ConvertProgram;
  7344. CheckSource('TestRenameJSNameConflict',
  7345. LinesToStr([ // statements
  7346. 'this.Apply = 0;',
  7347. 'this.Bind = 0;',
  7348. 'this.Call = 0;'
  7349. ]),
  7350. LinesToStr([ // this.$main
  7351. ''
  7352. ]));
  7353. end;
  7354. procedure TTestModule.TestLocalConst;
  7355. begin
  7356. StartProgram(false);
  7357. Add('procedure DoIt;');
  7358. Add('const');
  7359. Add(' cA: longint = 1;');
  7360. Add(' cB = 2;');
  7361. Add(' procedure Sub;');
  7362. Add(' const');
  7363. Add(' csA = 3;');
  7364. Add(' cB: double = 4;');
  7365. Add(' begin');
  7366. Add(' cb:=cb+csa;');
  7367. Add(' ca:=ca+csa+5;');
  7368. Add(' end;');
  7369. Add('begin');
  7370. Add(' ca:=ca+cb+6;');
  7371. Add('end;');
  7372. Add('begin');
  7373. ConvertProgram;
  7374. CheckSource('TestLocalConst',
  7375. LinesToStr([
  7376. 'var cA = 1;',
  7377. 'var cB = 2;',
  7378. 'var csA = 3;',
  7379. 'var cB$1 = 4;',
  7380. 'this.DoIt = function () {',
  7381. ' function Sub() {',
  7382. ' cB$1 = cB$1 + 3;',
  7383. ' cA = cA + 3 + 5;',
  7384. ' };',
  7385. ' cA = cA + 2 + 6;',
  7386. '};'
  7387. ]),
  7388. LinesToStr([
  7389. ]));
  7390. end;
  7391. procedure TTestModule.TestVarExternal;
  7392. begin
  7393. StartProgram(false);
  7394. Add('var');
  7395. Add(' NaN: double; external name ''Global.NaN'';');
  7396. Add(' d: double;');
  7397. Add('begin');
  7398. Add(' d:=NaN;');
  7399. ConvertProgram;
  7400. CheckSource('TestVarExternal',
  7401. LinesToStr([
  7402. 'this.d = 0.0;'
  7403. ]),
  7404. LinesToStr([
  7405. '$mod.d = Global.NaN;'
  7406. ]));
  7407. end;
  7408. procedure TTestModule.TestVarExternalOtherUnit;
  7409. begin
  7410. AddModuleWithIntfImplSrc('unit2.pas',
  7411. LinesToStr([
  7412. 'var NaN: double; external name ''Global.NaN'';',
  7413. 'var iV: longint;'
  7414. ]),
  7415. '');
  7416. StartUnit(true);
  7417. Add('interface');
  7418. Add('uses unit2;');
  7419. Add('implementation');
  7420. Add('var');
  7421. Add(' d: double;');
  7422. Add(' i: longint; external name ''$i'';');
  7423. Add('begin');
  7424. Add(' d:=nan;');
  7425. Add(' d:=uNit2.nan;');
  7426. Add(' d:=test1.d;');
  7427. Add(' i:=iv;');
  7428. Add(' i:=uNit2.iv;');
  7429. Add(' i:=test1.i;');
  7430. ConvertUnit;
  7431. CheckSource('TestVarExternalOtherUnit',
  7432. LinesToStr([
  7433. 'var $impl = $mod.$impl;',
  7434. '']),
  7435. LinesToStr([ // this.$init
  7436. '$impl.d = Global.NaN;',
  7437. '$impl.d = Global.NaN;',
  7438. '$impl.d = $impl.d;',
  7439. '$i = pas.unit2.iV;',
  7440. '$i = pas.unit2.iV;',
  7441. '$i = $i;',
  7442. '']),
  7443. LinesToStr([ // implementation
  7444. '$impl.d = 0.0;',
  7445. '']) );
  7446. end;
  7447. procedure TTestModule.TestVarAbsoluteFail;
  7448. begin
  7449. StartProgram(false);
  7450. Add([
  7451. 'var',
  7452. ' a: longint;',
  7453. ' b: longword absolute a;',
  7454. 'begin']);
  7455. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7456. ConvertProgram;
  7457. end;
  7458. procedure TTestModule.TestConstExternal;
  7459. begin
  7460. StartProgram(false);
  7461. Add([
  7462. 'const',
  7463. ' PI: double; external name ''Global.PI'';',
  7464. ' Tau = 2*pi;',
  7465. 'var d: double;',
  7466. 'begin',
  7467. ' d:=pi;',
  7468. ' d:=tau+pi;']);
  7469. ConvertProgram;
  7470. CheckSource('TestConstExternal',
  7471. LinesToStr([
  7472. 'this.Tau = 2*Global.PI;',
  7473. 'this.d = 0.0;'
  7474. ]),
  7475. LinesToStr([
  7476. '$mod.d = Global.PI;',
  7477. '$mod.d = $mod.Tau + Global.PI;'
  7478. ]));
  7479. end;
  7480. procedure TTestModule.TestDouble;
  7481. begin
  7482. StartProgram(false);
  7483. Add([
  7484. 'type',
  7485. ' TDateTime = double;',
  7486. 'const',
  7487. ' a = TDateTime(2.7);',
  7488. ' b = a + TDateTime(1.7);',
  7489. ' c = 0.9 + 0.1;',
  7490. ' f0_1 = 0.1;',
  7491. ' f0_3 = 0.3;',
  7492. ' fn0_1 = -0.1;',
  7493. ' fn0_3 = -0.3;',
  7494. ' fn0_003 = -0.003;',
  7495. ' fn0_123456789 = -0.123456789;',
  7496. ' fn300_0 = -300.0;',
  7497. ' fn123456_0 = -123456.0;',
  7498. ' fn1234567_8 = -1234567.8;',
  7499. ' fn12345678_9 = -12345678.9;',
  7500. ' f1_0En12 = 1E-12;',
  7501. ' fn1_0En12 = -1E-12;',
  7502. ' maxdouble = 1.7e+308;',
  7503. ' mindouble = -1.7e+308;',
  7504. ' MinSafeIntDouble = -$1fffffffffffff;',
  7505. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7506. ' MaxSafeIntDouble = $1fffffffffffff;',
  7507. ' DZeroResolution = 1E-12;',
  7508. ' Minus1 = -1E-12;',
  7509. ' EPS = 1E-9;',
  7510. ' DELTA = 0.001;',
  7511. ' Big = 129.789E+100;',
  7512. ' Test0_15 = 0.15;',
  7513. ' Test999 = 2.9999999999999;',
  7514. ' Test111999 = 211199999999999000.0;',
  7515. ' TestMinus111999 = -211199999999999000.0;',
  7516. ' Inf = 1.0 / 0.0;',
  7517. ' NegInf = -1.0 / 0.0;',
  7518. 'procedure Run(d: double); external name ''Run'';',
  7519. 'var',
  7520. ' d: double = b;',
  7521. 'begin',
  7522. ' d:=1.0;',
  7523. ' d:=1.0/3.0;',
  7524. ' d:=1.0/(3-2-1);',
  7525. ' d:=1/3;',
  7526. ' d:=5.0E-324;',
  7527. ' d:=1.7E308;',
  7528. ' d:=001.00E00;',
  7529. ' d:=002.00E001;',
  7530. ' d:=003.000E000;',
  7531. ' d:=-004.00E-00;',
  7532. ' d:=-005.00E-001;',
  7533. ' d:=10**3;',
  7534. ' d:=10 mod 3;',
  7535. ' d:=10 div 3;',
  7536. ' d:=c;',
  7537. ' d:=f0_1;',
  7538. ' d:=f0_3;',
  7539. ' d:=fn0_1;',
  7540. ' d:=fn0_3;',
  7541. ' d:=fn0_003;',
  7542. ' d:=fn0_123456789;',
  7543. ' d:=fn300_0;',
  7544. ' d:=fn123456_0;',
  7545. ' d:=fn1234567_8;',
  7546. ' d:=fn12345678_9;',
  7547. ' d:=f1_0En12;',
  7548. ' d:=fn1_0En12;',
  7549. ' d:=maxdouble;',
  7550. ' d:=mindouble;',
  7551. ' d:=MinSafeIntDouble;',
  7552. ' d:=double(MinSafeIntDouble);',
  7553. ' d:=MinSafeIntDouble2;',
  7554. ' d:=double(MinSafeIntDouble2);',
  7555. ' d:=MaxSafeIntDouble;',
  7556. ' d:=default(double);',
  7557. ' Run(Inf);',
  7558. ' Run(NegInf);',
  7559. '']);
  7560. ConvertProgram;
  7561. CheckSource('TestDouble',
  7562. LinesToStr([
  7563. 'this.a = 2.7;',
  7564. 'this.b = 2.7 + 1.7;',
  7565. 'this.c = 0.9 + 0.1;',
  7566. 'this.f0_1 = 0.1;',
  7567. 'this.f0_3 = 0.3;',
  7568. 'this.fn0_1 = -0.1;',
  7569. 'this.fn0_3 = -0.3;',
  7570. 'this.fn0_003 = -0.003;',
  7571. 'this.fn0_123456789 = -0.123456789;',
  7572. 'this.fn300_0 = -300.0;',
  7573. 'this.fn123456_0 = -123456.0;',
  7574. 'this.fn1234567_8 = -1234567.8;',
  7575. 'this.fn12345678_9 = -12345678.9;',
  7576. 'this.f1_0En12 = 1E-12;',
  7577. 'this.fn1_0En12 = -1E-12;',
  7578. 'this.maxdouble = 1.7e+308;',
  7579. 'this.mindouble = -1.7e+308;',
  7580. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7581. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7582. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7583. 'this.DZeroResolution = 1E-12;',
  7584. 'this.Minus1 = -1E-12;',
  7585. 'this.EPS = 1E-9;',
  7586. 'this.DELTA = 0.001;',
  7587. 'this.Big = 129.789E+100;',
  7588. 'this.Test0_15 = 0.15;',
  7589. 'this.Test999 = 2.9999999999999;',
  7590. 'this.Test111999 = 211199999999999000.0;',
  7591. 'this.TestMinus111999 = -211199999999999000.0;',
  7592. 'this.Inf = 1.0 / 0.0;',
  7593. 'this.NegInf = -1.0 / 0.0;',
  7594. 'this.d = 4.4;',
  7595. '']),
  7596. LinesToStr([
  7597. '$mod.d = 1.0;',
  7598. '$mod.d = 1.0 / 3.0;',
  7599. '$mod.d = 1.0 / (3 - 2 - 1);',
  7600. '$mod.d = 1 / 3;',
  7601. '$mod.d = 5.0E-324;',
  7602. '$mod.d = 1.7E308;',
  7603. '$mod.d = 1.00E0;',
  7604. '$mod.d = 2.00E1;',
  7605. '$mod.d = 3.000E0;',
  7606. '$mod.d = -4.00E-0;',
  7607. '$mod.d = -5.00E-1;',
  7608. '$mod.d = Math.pow(10, 3);',
  7609. '$mod.d = 10 % 3;',
  7610. '$mod.d = rtl.trunc(10 / 3);',
  7611. '$mod.d = 1;',
  7612. '$mod.d = 0.1;',
  7613. '$mod.d = 0.3;',
  7614. '$mod.d = -0.1;',
  7615. '$mod.d = -0.3;',
  7616. '$mod.d = -3E-3;',
  7617. '$mod.d = -0.123456789;',
  7618. '$mod.d = -300;',
  7619. '$mod.d = -123456;',
  7620. '$mod.d = -1234567.8;',
  7621. '$mod.d = -1.23456789E7;',
  7622. '$mod.d = 1E-12;',
  7623. '$mod.d = -1E-12;',
  7624. '$mod.d = 1.7E308;',
  7625. '$mod.d = -1.7E308;',
  7626. '$mod.d = -9007199254740991;',
  7627. '$mod.d = -9007199254740991;',
  7628. '$mod.d = -9.007199254740992E15;',
  7629. '$mod.d = -9.007199254740992E15;',
  7630. '$mod.d = 9007199254740991;',
  7631. '$mod.d = 0.0;',
  7632. 'Run(1 / 0);',
  7633. 'Run(-1 / 0);',
  7634. '']));
  7635. end;
  7636. procedure TTestModule.TestDoubleSmall;
  7637. begin
  7638. StartProgram(false);
  7639. Add([
  7640. 'const',
  7641. ' a = 1e-1;',
  7642. ' b = 1e-2;',
  7643. ' c = 1e-3;',
  7644. ' d = 1e-4;',
  7645. ' e = 1e-5;',
  7646. ' f = 1e-6;',
  7647. ' g = 1e-7;',
  7648. ' h = -1e-1;',
  7649. ' i = -1e-2;',
  7650. 'procedure Fly(d: double);',
  7651. 'begin',
  7652. 'end;',
  7653. 'begin',
  7654. ' Fly(a);',
  7655. ' Fly(b);',
  7656. ' Fly(c);',
  7657. ' Fly(d);',
  7658. ' Fly(e);',
  7659. ' Fly(f);',
  7660. ' Fly(g);',
  7661. ' Fly(h);',
  7662. ' Fly(i);',
  7663. '']);
  7664. ConvertProgram;
  7665. CheckSource('TestDoubleSmall',
  7666. LinesToStr([
  7667. 'this.a = 1e-1;',
  7668. 'this.b = 1e-2;',
  7669. 'this.c = 1e-3;',
  7670. 'this.d = 1e-4;',
  7671. 'this.e = 1e-5;',
  7672. 'this.f = 1e-6;',
  7673. 'this.g = 1e-7;',
  7674. 'this.h = -1e-1;',
  7675. 'this.i = -1e-2;',
  7676. 'this.Fly = function (d) {',
  7677. '};',
  7678. '']),
  7679. LinesToStr([
  7680. '$mod.Fly(0.1);',
  7681. '$mod.Fly(0.01);',
  7682. '$mod.Fly(1E-3);',
  7683. '$mod.Fly(1E-4);',
  7684. '$mod.Fly(1E-5);',
  7685. '$mod.Fly(1E-6);',
  7686. '$mod.Fly(1E-7);',
  7687. '$mod.Fly(-0.1);',
  7688. '$mod.Fly(-0.01);',
  7689. '']));
  7690. end;
  7691. procedure TTestModule.TestInteger;
  7692. begin
  7693. StartProgram(false);
  7694. Add([
  7695. 'const',
  7696. ' MinInt = low(NativeInt);',
  7697. ' MaxInt = high(NativeInt);',
  7698. 'type',
  7699. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7700. 'const',
  7701. ' a = low(TMyInt)+High(TMyInt);',
  7702. 'var',
  7703. ' i: TMyInt;',
  7704. 'begin',
  7705. ' i:=-MinInt;',
  7706. ' i:=default(TMyInt);',
  7707. ' i:=low(i)+high(i);',
  7708. '']);
  7709. ConvertProgram;
  7710. CheckSource('TestIntegerRange',
  7711. LinesToStr([
  7712. 'this.MinInt = -9007199254740991;',
  7713. 'this.MaxInt = 9007199254740991;',
  7714. 'this.a = -9007199254740991 + 9007199254740991;',
  7715. 'this.i = 0;',
  7716. '']),
  7717. LinesToStr([
  7718. '$mod.i = - -9007199254740991;',
  7719. '$mod.i = -9007199254740991;',
  7720. '$mod.i = -9007199254740991 + 9007199254740991;',
  7721. '']));
  7722. end;
  7723. procedure TTestModule.TestIntegerRange;
  7724. begin
  7725. StartProgram(false);
  7726. Add([
  7727. 'const',
  7728. ' MinInt = -1;',
  7729. ' MaxInt = +1;',
  7730. 'type',
  7731. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7732. ' TInt2 = 1..3;',
  7733. 'const',
  7734. ' a = low(TMyInt)+High(TMyInt);',
  7735. ' b = low(TInt2)+High(TInt2);',
  7736. ' s1 = [1];',
  7737. ' s2 = [1,2];',
  7738. ' s3 = [1..3];',
  7739. ' s4 = [low(shortint)..high(shortint)];',
  7740. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7741. ' s6 = 1 in s2;',
  7742. 'var',
  7743. ' i: TMyInt;',
  7744. ' i2: TInt2;',
  7745. 'begin',
  7746. ' i:=i2;',
  7747. ' i:=default(TMyInt);',
  7748. ' if i=i2 then ;',
  7749. ' i:=ord(i2);',
  7750. '']);
  7751. ConvertProgram;
  7752. CheckSource('TestIntegerRange',
  7753. LinesToStr([
  7754. 'this.MinInt = -1;',
  7755. 'this.MaxInt = +1;',
  7756. 'this.a = -1 + 1;',
  7757. 'this.b = 1 + 3;',
  7758. 'this.s1 = rtl.createSet(1);',
  7759. 'this.s2 = rtl.createSet(1, 2);',
  7760. 'this.s3 = rtl.createSet(null, 1, 3);',
  7761. 'this.s4 = rtl.createSet(null, -128, 127);',
  7762. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7763. 'this.s6 = 1 in this.s2;',
  7764. 'this.i = 0;',
  7765. 'this.i2 = 0;',
  7766. '']),
  7767. LinesToStr([
  7768. '$mod.i = $mod.i2;',
  7769. '$mod.i = -1;',
  7770. 'if ($mod.i === $mod.i2) ;',
  7771. '$mod.i = $mod.i2;',
  7772. '']));
  7773. end;
  7774. procedure TTestModule.TestIntegerTypecasts;
  7775. begin
  7776. StartProgram(false);
  7777. Add([
  7778. 'var',
  7779. ' i: nativeint;',
  7780. ' b: byte;',
  7781. ' sh: shortint;',
  7782. ' w: word;',
  7783. ' sm: smallint;',
  7784. ' lw: longword;',
  7785. ' li: longint;',
  7786. 'begin',
  7787. ' b:=byte(i);',
  7788. ' sh:=shortint(i);',
  7789. ' w:=word(i);',
  7790. ' sm:=smallint(i);',
  7791. ' lw:=longword(i);',
  7792. ' li:=longint(i);',
  7793. '']);
  7794. ConvertProgram;
  7795. CheckSource('TestIntegerTypecasts',
  7796. LinesToStr([
  7797. 'this.i = 0;',
  7798. 'this.b = 0;',
  7799. 'this.sh = 0;',
  7800. 'this.w = 0;',
  7801. 'this.sm = 0;',
  7802. 'this.lw = 0;',
  7803. 'this.li = 0;',
  7804. '']),
  7805. LinesToStr([
  7806. '$mod.b = $mod.i & 255;',
  7807. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7808. '$mod.w = $mod.i & 65535;',
  7809. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7810. '$mod.lw = $mod.i >>> 0;',
  7811. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7812. '']));
  7813. end;
  7814. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7815. begin
  7816. StartProgram(false);
  7817. Add([
  7818. 'var',
  7819. ' i,j: nativeint;',
  7820. 'begin',
  7821. ' i:=i shr 0;',
  7822. ' i:=i shr 1;',
  7823. ' i:=i shr 3;',
  7824. ' i:=i shr 54;',
  7825. ' i:=j shr i;',
  7826. '']);
  7827. ConvertProgram;
  7828. CheckResolverUnexpectedHints;
  7829. CheckSource('TestInteger_BitwiseShrNativeInt',
  7830. LinesToStr([
  7831. 'this.i = 0;',
  7832. 'this.j = 0;',
  7833. '']),
  7834. LinesToStr([
  7835. '$mod.i = $mod.i;',
  7836. '$mod.i = Math.floor($mod.i / 2);',
  7837. '$mod.i = Math.floor($mod.i / 8);',
  7838. '$mod.i = 0;',
  7839. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7840. '']));
  7841. end;
  7842. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7843. begin
  7844. StartProgram(false);
  7845. Add([
  7846. 'var',
  7847. ' i: nativeint;',
  7848. 'begin',
  7849. ' i:=i shl 0;',
  7850. ' i:=i shl 54;',
  7851. ' i:=123456789012 shl 1;',
  7852. ' i:=i shl 1;',
  7853. '']);
  7854. ConvertProgram;
  7855. CheckResolverUnexpectedHints;
  7856. CheckSource('TestInteger_BitwiseShrNativeInt',
  7857. LinesToStr([
  7858. 'this.i = 0;',
  7859. '']),
  7860. LinesToStr([
  7861. '$mod.i = $mod.i;',
  7862. '$mod.i = 0;',
  7863. '$mod.i = 246913578024;',
  7864. '$mod.i = rtl.shl($mod.i, 1);',
  7865. '']));
  7866. end;
  7867. procedure TTestModule.TestInteger_SystemFunc;
  7868. begin
  7869. StartProgram(true);
  7870. Add([
  7871. 'var',
  7872. ' i: byte;',
  7873. ' s: string;',
  7874. 'begin',
  7875. ' system.inc(i);',
  7876. ' system.str(i,s);',
  7877. ' s:=system.str(i);',
  7878. ' i:=system.low(i);',
  7879. ' i:=system.high(i);',
  7880. ' i:=system.pred(i);',
  7881. ' i:=system.succ(i);',
  7882. ' i:=system.ord(i);',
  7883. '']);
  7884. ConvertProgram;
  7885. CheckResolverUnexpectedHints;
  7886. CheckSource('TestInteger_SystemFunc',
  7887. LinesToStr([
  7888. 'this.i = 0;',
  7889. 'this.s = "";',
  7890. '']),
  7891. LinesToStr([
  7892. '$mod.i += 1;',
  7893. '$mod.s = "" + $mod.i;',
  7894. '$mod.s = "" + $mod.i;',
  7895. '$mod.i = 0;',
  7896. '$mod.i = 255;',
  7897. '$mod.i = $mod.i - 1;',
  7898. '$mod.i = $mod.i + 1;',
  7899. '$mod.i = $mod.i;',
  7900. '']));
  7901. end;
  7902. procedure TTestModule.TestInteger_AssignOutsideConst;
  7903. begin
  7904. StartProgram(false);
  7905. Add([
  7906. 'const',
  7907. ' MinInt = low(longint);',
  7908. ' MaxInt = high(longint);',
  7909. 'type',
  7910. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7911. 'var',
  7912. ' i: TMyInt;',
  7913. ' aByte: byte;',
  7914. ' aShortInt: shortint;',
  7915. ' aWord: word;',
  7916. ' aSmallInt: smallint;',
  7917. ' aLongWord: longword;',
  7918. ' aLongInt: longint;',
  7919. ' aNativeInt: nativeint;',
  7920. ' aNativeUInt: nativeuint;',
  7921. 'begin',
  7922. ' aByte:=$FF;',
  7923. ' aByte:=$100;',
  7924. ' aByte:=-1;',
  7925. ' aByte:=-127;',
  7926. ' aByte:=-128;',
  7927. ' aByte:=-254;',
  7928. ' aByte:=-255;',
  7929. ' aByte:=-256;',
  7930. ' aShortInt:=127;',
  7931. ' aShortInt:=128;',
  7932. ' aShortInt:=-128;',
  7933. ' aShortInt:=-129;',
  7934. ' aWord:=$ffff;',
  7935. ' aWord:=$10000;',
  7936. ' aWord:=-1;',
  7937. ' aWord:=-$ffff;',
  7938. ' aWord:=-$10000;',
  7939. ' aWord:=-$10001;',
  7940. ' aSmallInt:=$7fff;',
  7941. ' aSmallInt:=$8000;',
  7942. ' aSmallInt:=-$8000;',
  7943. ' aSmallInt:=-$8001;',
  7944. ' aLongWord:=$ffffffff;',
  7945. ' aLongWord:=$100000000;',
  7946. ' aLongWord:=-1;',
  7947. ' aLongWord:=-$ffffffff;',
  7948. ' aNativeInt:=$1fffffffffffff;',
  7949. ' aNativeInt:=-$1fffffffffffff;',
  7950. ' aNativeUInt:=$1fffffffffffff;',
  7951. ' aNativeUInt:=-$1fffffffffffff;',
  7952. '']);
  7953. ConvertProgram;
  7954. CheckSource('TestInteger_AssignOutsideConst',
  7955. LinesToStr([
  7956. 'this.MinInt = -2147483648;',
  7957. 'this.MaxInt = 2147483647;',
  7958. 'this.i = 0;',
  7959. 'this.aByte = 0;',
  7960. 'this.aShortInt = 0;',
  7961. 'this.aWord = 0;',
  7962. 'this.aSmallInt = 0;',
  7963. 'this.aLongWord = 0;',
  7964. 'this.aLongInt = 0;',
  7965. 'this.aNativeInt = 0;',
  7966. 'this.aNativeUInt = 0;',
  7967. '']),
  7968. LinesToStr([
  7969. '$mod.aByte = 0xFF;',
  7970. '$mod.aByte = 0;',
  7971. '$mod.aByte = 255;',
  7972. '$mod.aByte = 129;',
  7973. '$mod.aByte = 128;',
  7974. '$mod.aByte = 2;',
  7975. '$mod.aByte = 1;',
  7976. '$mod.aByte = 0;',
  7977. '$mod.aShortInt = 127;',
  7978. '$mod.aShortInt = -128;',
  7979. '$mod.aShortInt = -128;',
  7980. '$mod.aShortInt = 127;',
  7981. '$mod.aWord = 0xffff;',
  7982. '$mod.aWord = 0;',
  7983. '$mod.aWord = 65535;',
  7984. '$mod.aWord = 1;',
  7985. '$mod.aWord = 0;',
  7986. '$mod.aWord = 65535;',
  7987. '$mod.aSmallInt = 0x7fff;',
  7988. '$mod.aSmallInt = -32768;',
  7989. '$mod.aSmallInt = -0x8000;',
  7990. '$mod.aSmallInt = 32767;',
  7991. '$mod.aLongWord = 0xffffffff;',
  7992. '$mod.aLongWord = 0;',
  7993. '$mod.aLongWord = 4294967295;',
  7994. '$mod.aLongWord = 1;',
  7995. '$mod.aNativeInt = 0x1fffffffffffff;',
  7996. '$mod.aNativeInt = -0x1fffffffffffff;',
  7997. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7998. '$mod.aNativeUInt = 1;',
  7999. '']));
  8000. end;
  8001. procedure TTestModule.TestCurrency;
  8002. begin
  8003. StartProgram(false);
  8004. Add([
  8005. 'type',
  8006. ' TCoin = currency;',
  8007. 'const',
  8008. ' a = TCoin(2.7);',
  8009. ' b = a + TCoin(1.7);',
  8010. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8011. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8012. 'var',
  8013. ' c: TCoin = b;',
  8014. ' i: nativeint;',
  8015. ' d: double;',
  8016. ' j: jsvalue;',
  8017. 'function DoIt(c: currency): currency; begin end;',
  8018. 'function GetIt(d: double): double; begin end;',
  8019. 'procedure Write(v: jsvalue); begin end;',
  8020. 'begin',
  8021. ' c:=1.0;',
  8022. ' c:=0.1;',
  8023. ' c:=1.0/3.0;',
  8024. ' c:=1/3;',
  8025. ' c:=a;',
  8026. ' d:=c;',
  8027. ' c:=d;',
  8028. ' c:=currency(c);',
  8029. ' c:=currency(d);',
  8030. ' d:=double(c);',
  8031. ' c:=i;',
  8032. ' c:=currency(i);',
  8033. //' i:=c;', not allowed
  8034. ' i:=nativeint(c);',
  8035. ' c:=c+a;',
  8036. ' c:=-c-a;',
  8037. ' c:=d+c;',
  8038. ' c:=c+d;',
  8039. ' c:=d-c;',
  8040. ' c:=c-d;',
  8041. ' c:=c*a;',
  8042. ' c:=a*c;',
  8043. ' c:=d*c;',
  8044. ' c:=c*d;',
  8045. ' c:=c/a;',
  8046. ' c:=a/c;',
  8047. ' c:=d/c;',
  8048. ' c:=c/d;',
  8049. ' c:=c**a;',
  8050. ' c:=a**c;',
  8051. ' c:=d**c;',
  8052. ' c:=c**d;',
  8053. ' if c=c then ;',
  8054. ' if c=a then ;',
  8055. ' if a=c then ;',
  8056. ' if d=c then ;',
  8057. ' if c=d then ;',
  8058. ' c:=DoIt(c);',
  8059. ' c:=DoIt(i);',
  8060. ' c:=DoIt(d);',
  8061. ' c:=GetIt(c);',
  8062. ' j:=c;',
  8063. ' Write(c);',
  8064. ' c:=default(currency);',
  8065. ' j:=str(c);',
  8066. ' j:=str(c:0:3);',
  8067. '']);
  8068. ConvertProgram;
  8069. CheckSource('TestCurrency',
  8070. LinesToStr([
  8071. 'this.a = 27000;',
  8072. 'this.b = this.a + 17000;',
  8073. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8074. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8075. 'this.c = this.b;',
  8076. 'this.i = 0;',
  8077. 'this.d = 0.0;',
  8078. 'this.j = undefined;',
  8079. 'this.DoIt = function (c) {',
  8080. ' var Result = 0;',
  8081. ' return Result;',
  8082. '};',
  8083. 'this.GetIt = function (d) {',
  8084. ' var Result = 0.0;',
  8085. ' return Result;',
  8086. '};',
  8087. 'this.Write = function (v) {',
  8088. '};',
  8089. '']),
  8090. LinesToStr([
  8091. '$mod.c = 10000;',
  8092. '$mod.c = 1000;',
  8093. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8094. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8095. '$mod.c = $mod.a;',
  8096. '$mod.d = $mod.c / 10000;',
  8097. '$mod.c = rtl.trunc($mod.d * 10000);',
  8098. '$mod.c = $mod.c;',
  8099. '$mod.c = $mod.d * 10000;',
  8100. '$mod.d = $mod.c / 10000;',
  8101. '$mod.c = $mod.i * 10000;',
  8102. '$mod.c = $mod.i * 10000;',
  8103. '$mod.i = rtl.trunc($mod.c / 10000);',
  8104. '$mod.c = $mod.c + $mod.a;',
  8105. '$mod.c = -$mod.c - $mod.a;',
  8106. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8107. '$mod.c = $mod.c + ($mod.d * 10000);',
  8108. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8109. '$mod.c = $mod.c - ($mod.d * 10000);',
  8110. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8111. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8112. '$mod.c = $mod.d * $mod.c;',
  8113. '$mod.c = $mod.c * $mod.d;',
  8114. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8115. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8116. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8117. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8118. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8119. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8120. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8121. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8122. 'if ($mod.c === $mod.c) ;',
  8123. 'if ($mod.c === $mod.a) ;',
  8124. 'if ($mod.a === $mod.c) ;',
  8125. 'if (($mod.d * 10000) === $mod.c) ;',
  8126. 'if ($mod.c === ($mod.d * 10000)) ;',
  8127. '$mod.c = $mod.DoIt($mod.c);',
  8128. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8129. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8130. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8131. '$mod.j = $mod.c / 10000;',
  8132. '$mod.Write($mod.c / 10000);',
  8133. '$mod.c = 0;',
  8134. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8135. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8136. '']));
  8137. end;
  8138. procedure TTestModule.TestForBoolDo;
  8139. begin
  8140. StartProgram(false);
  8141. Add([
  8142. 'var b: boolean;',
  8143. 'begin',
  8144. ' for b:=false to true do ;',
  8145. ' for b:=b downto false do ;',
  8146. ' for b in boolean do ;',
  8147. '']);
  8148. ConvertProgram;
  8149. CheckSource('TestForBoolDo',
  8150. LinesToStr([ // statements
  8151. 'this.b = false;']),
  8152. LinesToStr([ // this.$main
  8153. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8154. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8155. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8156. '']));
  8157. end;
  8158. procedure TTestModule.TestForIntDo;
  8159. begin
  8160. StartProgram(false);
  8161. Add([
  8162. 'var i: longint;',
  8163. 'begin',
  8164. ' for i:=3 to 5 do ;',
  8165. ' for i:=i downto 2 do ;',
  8166. ' for i in byte do ;',
  8167. '']);
  8168. ConvertProgram;
  8169. CheckSource('TestForIntDo',
  8170. LinesToStr([ // statements
  8171. 'this.i = 0;']),
  8172. LinesToStr([ // this.$main
  8173. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8174. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8175. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8176. '']));
  8177. end;
  8178. procedure TTestModule.TestForIntInDo;
  8179. begin
  8180. StartProgram(false);
  8181. Add([
  8182. 'type',
  8183. ' TSetOfInt = set of byte;',
  8184. ' TIntRg = 3..7;',
  8185. ' TSetOfIntRg = set of TIntRg;',
  8186. 'var',
  8187. ' i,i2: longint;',
  8188. ' a1: array of byte;',
  8189. ' a2: array[1..3] of byte;',
  8190. ' soi: TSetOfInt;',
  8191. ' soir: TSetOfIntRg;',
  8192. ' ir: TIntRg;',
  8193. 'begin',
  8194. ' for i in byte do ;',
  8195. ' for i in a1 do ;',
  8196. ' for i in a2 do ;',
  8197. ' for i in [11..13] do ;',
  8198. ' for i in TSetOfInt do ;',
  8199. ' for i in TIntRg do ;',
  8200. ' for i in soi do i2:=i;',
  8201. ' for i in TSetOfIntRg do ;',
  8202. ' for i in soir do ;',
  8203. ' for ir in TIntRg do ;',
  8204. ' for ir in TSetOfIntRg do ;',
  8205. ' for ir in soir do ;',
  8206. '']);
  8207. ConvertProgram;
  8208. CheckSource('TestForIntInDo',
  8209. LinesToStr([ // statements
  8210. 'this.i = 0;',
  8211. 'this.i2 = 0;',
  8212. 'this.a1 = [];',
  8213. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8214. 'this.soi = {};',
  8215. 'this.soir = {};',
  8216. 'this.ir = 0;',
  8217. '']),
  8218. LinesToStr([ // this.$main
  8219. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8220. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8221. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8222. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8223. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8224. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8225. 'for (var $l6 in $mod.soi) {',
  8226. ' $mod.i = +$l6;',
  8227. ' $mod.i2 = $mod.i;',
  8228. '};',
  8229. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8230. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8231. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8232. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8233. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8234. '']));
  8235. end;
  8236. procedure TTestModule.TestCharConst;
  8237. begin
  8238. StartProgram(false);
  8239. Add([
  8240. 'const',
  8241. ' a = #$00F3;',
  8242. ' c: char = ''1'';',
  8243. ' wc: widechar = ''ä'';',
  8244. 'begin',
  8245. ' c:=#0;',
  8246. ' c:=#1;',
  8247. ' c:=#9;',
  8248. ' c:=#10;',
  8249. ' c:=#13;',
  8250. ' c:=#31;',
  8251. ' c:=#32;',
  8252. ' c:=#$A;',
  8253. ' c:=#$0A;',
  8254. ' c:=#$b;',
  8255. ' c:=#$0b;',
  8256. ' c:=^A;',
  8257. ' c:=''"'';',
  8258. ' c:=default(char);',
  8259. ' c:=#$00E4;', // ä
  8260. ' c:=''ä'';',
  8261. ' c:=#$E4;', // ä
  8262. ' c:=#$D800;', // invalid UTF-16
  8263. ' c:=#$DFFF;', // invalid UTF-16
  8264. ' c:=#$FFFF;', // last UCS-2
  8265. ' c:=high(c);', // last UCS-2
  8266. ' c:=#269;',
  8267. '']);
  8268. ConvertProgram;
  8269. CheckSource('TestCharConst',
  8270. LinesToStr([
  8271. 'this.a="ó";',
  8272. 'this.c="1";',
  8273. 'this.wc="ä";'
  8274. ]),
  8275. LinesToStr([
  8276. '$mod.c="\x00";',
  8277. '$mod.c="\x01";',
  8278. '$mod.c="\t";',
  8279. '$mod.c="\n";',
  8280. '$mod.c="\r";',
  8281. '$mod.c="\x1F";',
  8282. '$mod.c=" ";',
  8283. '$mod.c="\n";',
  8284. '$mod.c="\n";',
  8285. '$mod.c="\x0B";',
  8286. '$mod.c="\x0B";',
  8287. '$mod.c="\x01";',
  8288. '$mod.c=''"'';',
  8289. '$mod.c="\x00";',
  8290. '$mod.c = "ä";',
  8291. '$mod.c = "ä";',
  8292. '$mod.c = "ä";',
  8293. '$mod.c="\uD800";',
  8294. '$mod.c="\uDFFF";',
  8295. '$mod.c="\uFFFF";',
  8296. '$mod.c="\uFFFF";',
  8297. '$mod.c = "č";',
  8298. '']));
  8299. end;
  8300. procedure TTestModule.TestChar_Compare;
  8301. begin
  8302. StartProgram(false);
  8303. Add('var');
  8304. Add(' c: char;');
  8305. Add(' b: boolean;');
  8306. Add('begin');
  8307. Add(' b:=c=''1'';');
  8308. Add(' b:=''2''=c;');
  8309. Add(' b:=''3''=''4'';');
  8310. Add(' b:=c<>''5'';');
  8311. Add(' b:=''6''<>c;');
  8312. Add(' b:=c>''7'';');
  8313. Add(' b:=''8''>c;');
  8314. Add(' b:=c>=''9'';');
  8315. Add(' b:=''A''>=c;');
  8316. Add(' b:=c<''B'';');
  8317. Add(' b:=''C''<c;');
  8318. Add(' b:=c<=''D'';');
  8319. Add(' b:=''E''<=c;');
  8320. ConvertProgram;
  8321. CheckSource('TestChar_Compare',
  8322. LinesToStr([
  8323. 'this.c="";',
  8324. 'this.b = false;'
  8325. ]),
  8326. LinesToStr([
  8327. '$mod.b = $mod.c === "1";',
  8328. '$mod.b = "2" === $mod.c;',
  8329. '$mod.b = "3" === "4";',
  8330. '$mod.b = $mod.c !== "5";',
  8331. '$mod.b = "6" !== $mod.c;',
  8332. '$mod.b = $mod.c > "7";',
  8333. '$mod.b = "8" > $mod.c;',
  8334. '$mod.b = $mod.c >= "9";',
  8335. '$mod.b = "A" >= $mod.c;',
  8336. '$mod.b = $mod.c < "B";',
  8337. '$mod.b = "C" < $mod.c;',
  8338. '$mod.b = $mod.c <= "D";',
  8339. '$mod.b = "E" <= $mod.c;',
  8340. '']));
  8341. end;
  8342. procedure TTestModule.TestChar_BuiltInProcs;
  8343. begin
  8344. StartProgram(false);
  8345. Add([
  8346. 'var',
  8347. ' c: char;',
  8348. ' i: longint;',
  8349. ' s: string;',
  8350. 'begin',
  8351. ' i:=ord(c);',
  8352. ' i:=ord(s[i]);',
  8353. ' c:=chr(i);',
  8354. ' c:=pred(c);',
  8355. ' c:=succ(c);',
  8356. ' c:=low(c);',
  8357. ' c:=high(c);',
  8358. ' i:=byte(c);',
  8359. ' i:=word(c);',
  8360. ' i:=longint(c);',
  8361. '']);
  8362. ConvertProgram;
  8363. CheckSource('TestChar_BuiltInProcs',
  8364. LinesToStr([
  8365. 'this.c = "";',
  8366. 'this.i = 0;',
  8367. 'this.s = "";'
  8368. ]),
  8369. LinesToStr([
  8370. '$mod.i = $mod.c.charCodeAt();',
  8371. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8372. '$mod.c = String.fromCharCode($mod.i);',
  8373. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8374. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8375. '$mod.c = "\x00";',
  8376. '$mod.c = "\uFFFF";',
  8377. '$mod.i = $mod.c.charCodeAt() & 255;',
  8378. '$mod.i = $mod.c.charCodeAt();',
  8379. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8380. '']));
  8381. end;
  8382. procedure TTestModule.TestStringConst;
  8383. begin
  8384. StartProgram(false);
  8385. Add([
  8386. '{$H+}',
  8387. 'const',
  8388. ' a = #$00F3#$017C;', // first <256, then >=256
  8389. ' b = string(''a'');',
  8390. ' c = string(''ä'');',
  8391. ' d = UnicodeString(''b'');',
  8392. ' e = UnicodeString(''ö'');',
  8393. ' f = low(a)+high(b);',
  8394. ' g: word = low(a);',
  8395. 'var',
  8396. ' s: string = ''abc'';',
  8397. ' i: longint;',
  8398. 'begin',
  8399. ' s:='''';',
  8400. ' s:=#13#10;',
  8401. ' s:=#9''foo'';',
  8402. ' s:=#$A9;',
  8403. ' s:=''foo''#13''bar'';',
  8404. ' s:=''"'';',
  8405. ' s:=''"''''"'';',
  8406. ' s:=#$20AC;', // euro
  8407. ' s:=#$10437;', // outside BMP
  8408. ' s:=''abc''#$20AC;', // ascii,#
  8409. ' s:=''ä''#$20AC;', // non ascii,#
  8410. ' s:=#$20AC''abc'';', // #, ascii
  8411. ' s:=#$20AC''ä'';', // #, non ascii
  8412. ' s:=default(string);',
  8413. ' s:=concat(s);',
  8414. ' s:=concat(s,''a'',s);',
  8415. ' s:=#250#269;',
  8416. ' i:=low(s)+high(a);',
  8417. ' s:=''a/b'';',
  8418. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8419. '']);
  8420. ConvertProgram;
  8421. CheckSource('TestStringConst',
  8422. LinesToStr([
  8423. 'this.a = "óż";',
  8424. 'this.b = "a";',
  8425. 'this.c = "ä";',
  8426. 'this.d = "b";',
  8427. 'this.e = "ö";',
  8428. 'this.f = 1 + this.b.length;',
  8429. 'this.g = 1;',
  8430. 'this.s="abc";',
  8431. 'this.i = 0;',
  8432. '']),
  8433. LinesToStr([
  8434. '$mod.s="";',
  8435. '$mod.s="\r\n";',
  8436. '$mod.s="\tfoo";',
  8437. '$mod.s="©";',
  8438. '$mod.s="foo\rbar";',
  8439. '$mod.s=''"'';',
  8440. '$mod.s=''"\''"'';',
  8441. '$mod.s="€";',
  8442. '$mod.s="'#$F0#$90#$90#$B7'";',
  8443. '$mod.s = "abc€";',
  8444. '$mod.s = "ä€";',
  8445. '$mod.s = "€abc";',
  8446. '$mod.s = "ۊ";',
  8447. '$mod.s="";',
  8448. '$mod.s = $mod.s;',
  8449. '$mod.s = $mod.s.concat("a", $mod.s);',
  8450. '$mod.s = "úč";',
  8451. '$mod.i = 1 + $mod.a.length;',
  8452. '$mod.s = "a/b";',
  8453. '']));
  8454. end;
  8455. procedure TTestModule.TestStringConst_InvalidUTF16;
  8456. begin
  8457. StartProgram(false);
  8458. Add([
  8459. 'const',
  8460. ' a: char = #$D87E;',
  8461. ' b: string = #$D87E;',
  8462. ' c: string = #$D87E#43;',
  8463. 'begin',
  8464. ' c:=''abc''#$D87E;',
  8465. ' c:=#0#1#2;',
  8466. ' c:=#127;',
  8467. ' c:=#128;',
  8468. ' c:=#255;',
  8469. ' c:=#256;',
  8470. '']);
  8471. ConvertProgram;
  8472. CheckSource('TestStringConst',
  8473. LinesToStr([
  8474. 'this.a = "\uD87E";',
  8475. 'this.b = "\uD87E";',
  8476. 'this.c = "\uD87E+";',
  8477. '']),
  8478. LinesToStr([
  8479. '$mod.c = "abc\uD87E";',
  8480. '$mod.c = "\x00\x01\x02";',
  8481. '$mod.c = "'#127'";',
  8482. '$mod.c = "'#$c2#$80'";',
  8483. '$mod.c = "'#$c3#$BF'";',
  8484. '$mod.c = "'#$c4#$80'";',
  8485. '']));
  8486. end;
  8487. procedure TTestModule.TestStringConstSurrogate;
  8488. begin
  8489. StartProgram(false);
  8490. Add([
  8491. 'var',
  8492. ' s: string;',
  8493. 'begin',
  8494. ' s:=''😊'';', // 1F60A
  8495. '']);
  8496. ConvertProgram;
  8497. CheckSource('TestStringConstSurrogate',
  8498. LinesToStr([
  8499. 'this.s="";'
  8500. ]),
  8501. LinesToStr([
  8502. '$mod.s="😊";'
  8503. ]));
  8504. end;
  8505. procedure TTestModule.TestStringConst_Multiline;
  8506. begin
  8507. StartProgram(false);
  8508. Add([
  8509. '{$modeswitch multilinestrings}',
  8510. 'const',
  8511. ' a = ``;',
  8512. ' b = `',
  8513. 'line`;',
  8514. ' c = `Single`;',
  8515. ' d = ````;',
  8516. ' e = `abc``xyz`;',
  8517. ' f = `first''line',
  8518. ' second''line`#10;',
  8519. 'begin',
  8520. '']);
  8521. ConvertProgram;
  8522. CheckSource('TestStringConst_Multiline',
  8523. LinesToStr([
  8524. 'this.a = "";',
  8525. 'this.b = "\nline";',
  8526. 'this.c = "Single";',
  8527. 'this.d = "`";',
  8528. 'this.e = "abc`xyz";',
  8529. 'this.f = "first''line\n second''line\n";',
  8530. '']),
  8531. LinesToStr([
  8532. ]));
  8533. end;
  8534. procedure TTestModule.TestString_Length;
  8535. begin
  8536. StartProgram(false);
  8537. Add('const c = ''foo'';');
  8538. Add('var');
  8539. Add(' s: string;');
  8540. Add(' i: longint;');
  8541. Add('begin');
  8542. Add(' i:=length(s);');
  8543. Add(' i:=length(s+s);');
  8544. Add(' i:=length(''abc'');');
  8545. Add(' i:=length(c);');
  8546. ConvertProgram;
  8547. CheckSource('TestString_Length',
  8548. LinesToStr([
  8549. 'this.c = "foo";',
  8550. 'this.s = "";',
  8551. 'this.i = 0;',
  8552. '']),
  8553. LinesToStr([
  8554. '$mod.i = $mod.s.length;',
  8555. '$mod.i = ($mod.s+$mod.s).length;',
  8556. '$mod.i = "abc".length;',
  8557. '$mod.i = $mod.c.length;',
  8558. '']));
  8559. end;
  8560. procedure TTestModule.TestString_Compare;
  8561. begin
  8562. StartProgram(false);
  8563. Add('var');
  8564. Add(' s, t: string;');
  8565. Add(' b: boolean;');
  8566. Add('begin');
  8567. Add(' b:=s=t;');
  8568. Add(' b:=s<>t;');
  8569. Add(' b:=s>t;');
  8570. Add(' b:=s>=t;');
  8571. Add(' b:=s<t;');
  8572. Add(' b:=s<=t;');
  8573. ConvertProgram;
  8574. CheckSource('TestString_Compare',
  8575. LinesToStr([ // statements
  8576. 'this.s = "";',
  8577. 'this.t = "";',
  8578. 'this.b =false;'
  8579. ]),
  8580. LinesToStr([ // this.$main
  8581. '$mod.b = $mod.s === $mod.t;',
  8582. '$mod.b = $mod.s !== $mod.t;',
  8583. '$mod.b = $mod.s > $mod.t;',
  8584. '$mod.b = $mod.s >= $mod.t;',
  8585. '$mod.b = $mod.s < $mod.t;',
  8586. '$mod.b = $mod.s <= $mod.t;',
  8587. '']));
  8588. end;
  8589. procedure TTestModule.TestString_SetLength;
  8590. begin
  8591. StartProgram(false);
  8592. Add([
  8593. 'procedure Fly(var s: string);',
  8594. 'begin',
  8595. ' SetLength(s,1);',
  8596. 'end;',
  8597. 'procedure Run(var s: unicodestring);',
  8598. 'begin',
  8599. ' SetLength(s,2);',
  8600. 'end;',
  8601. 'var s: string;',
  8602. ' u: unicodestring;',
  8603. 'begin',
  8604. ' SetLength(s,3);',
  8605. ' SetLength(u,4);',
  8606. '']);
  8607. ConvertProgram;
  8608. CheckSource('TestString_SetLength',
  8609. LinesToStr([ // statements
  8610. 'this.Fly = function (s) {',
  8611. ' s.set(rtl.strSetLength(s.get(), 1));',
  8612. '};',
  8613. 'this.Run = function (s) {',
  8614. ' s.set(rtl.strSetLength(s.get(), 2));',
  8615. '};',
  8616. 'this.s = "";',
  8617. 'this.u = "";',
  8618. '']),
  8619. LinesToStr([ // this.$main
  8620. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8621. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8622. ]));
  8623. end;
  8624. procedure TTestModule.TestString_CharAt;
  8625. begin
  8626. StartProgram(false);
  8627. Add([
  8628. 'var',
  8629. ' s: string;',
  8630. ' c: char;',
  8631. ' b: boolean;',
  8632. 'begin',
  8633. ' b:= s[1] = c;',
  8634. ' b:= c = s[1];',
  8635. ' b:= c <> s[1];',
  8636. ' b:= c > s[1];',
  8637. ' b:= c >= s[1];',
  8638. ' b:= c < s[2];',
  8639. ' b:= c <= s[1];',
  8640. ' s[1] := c;',
  8641. ' s[2+3] := c;']);
  8642. ConvertProgram;
  8643. CheckSource('TestString_CharAt',
  8644. LinesToStr([ // statements
  8645. 'this.s = "";',
  8646. 'this.c = "";',
  8647. 'this.b = false;'
  8648. ]),
  8649. LinesToStr([ // this.$main
  8650. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8651. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8652. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8653. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8654. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8655. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8656. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8657. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8658. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8659. '']));
  8660. end;
  8661. procedure TTestModule.TestStringHMinusFail;
  8662. begin
  8663. StartProgram(false);
  8664. Add([
  8665. '{$H-}',
  8666. 'var s: string;',
  8667. 'begin']);
  8668. ConvertProgram;
  8669. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8670. end;
  8671. procedure TTestModule.TestStr;
  8672. begin
  8673. StartProgram(false);
  8674. Add('var');
  8675. Add(' b: boolean;');
  8676. Add(' i: longint;');
  8677. Add(' d: double;');
  8678. Add(' s: string;');
  8679. Add('begin');
  8680. Add(' str(b,s);');
  8681. Add(' str(i,s);');
  8682. Add(' str(d,s);');
  8683. Add(' str(i:3,s);');
  8684. Add(' str(d:3:2,s);');
  8685. Add(' Str(12.456:12:1,s);');
  8686. Add(' Str(12.456:12,s);');
  8687. Add(' s:=str(b);');
  8688. Add(' s:=str(i);');
  8689. Add(' s:=str(d);');
  8690. Add(' s:=str(i,i);');
  8691. Add(' s:=str(i:3);');
  8692. Add(' s:=str(d:3:2);');
  8693. Add(' s:=str(i:4,i);');
  8694. Add(' s:=str(i,i:5);');
  8695. Add(' s:=str(i:4,i:5);');
  8696. Add(' s:=str(s,s);');
  8697. Add(' s:=str(s,''foo'');');
  8698. ConvertProgram;
  8699. CheckSource('TestStr',
  8700. LinesToStr([ // statements
  8701. 'this.b = false;',
  8702. 'this.i = 0;',
  8703. 'this.d = 0.0;',
  8704. 'this.s = "";',
  8705. '']),
  8706. LinesToStr([ // this.$main
  8707. '$mod.s = ""+$mod.b;',
  8708. '$mod.s = ""+$mod.i;',
  8709. '$mod.s = rtl.floatToStr($mod.d);',
  8710. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8711. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8712. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8713. '$mod.s = rtl.floatToStr(12.456,12);',
  8714. '$mod.s = ""+$mod.b;',
  8715. '$mod.s = ""+$mod.i;',
  8716. '$mod.s = rtl.floatToStr($mod.d);',
  8717. '$mod.s = ""+$mod.i+$mod.i;',
  8718. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8719. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8720. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8721. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8722. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8723. '$mod.s = $mod.s + $mod.s;',
  8724. '$mod.s = $mod.s + "foo";',
  8725. '']));
  8726. end;
  8727. procedure TTestModule.TestBaseType_AnsiStringFail;
  8728. begin
  8729. StartProgram(false);
  8730. Add('var s: AnsiString');
  8731. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8732. ConvertProgram;
  8733. end;
  8734. procedure TTestModule.TestBaseType_WideStringFail;
  8735. begin
  8736. StartProgram(false);
  8737. Add('var s: WideString');
  8738. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8739. ConvertProgram;
  8740. end;
  8741. procedure TTestModule.TestBaseType_ShortStringFail;
  8742. begin
  8743. StartProgram(false);
  8744. Add('var s: ShortString');
  8745. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8746. ConvertProgram;
  8747. end;
  8748. procedure TTestModule.TestBaseType_RawByteStringFail;
  8749. begin
  8750. StartProgram(false);
  8751. Add('var s: RawByteString');
  8752. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8753. ConvertProgram;
  8754. end;
  8755. procedure TTestModule.TestTypeShortstring_Fail;
  8756. begin
  8757. StartProgram(false);
  8758. Add('type t = string[12];');
  8759. Add('var s: t;');
  8760. Add('begin');
  8761. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8762. ConvertProgram;
  8763. end;
  8764. procedure TTestModule.TestCharSet_Custom;
  8765. begin
  8766. StartProgram(false);
  8767. Add([
  8768. 'type',
  8769. ' TCharRg = ''a''..''z'';',
  8770. ' TSetOfCharRg = set of TCharRg;',
  8771. ' TCharRg2 = ''m''..''p'';',
  8772. 'const',
  8773. ' crg: TCharRg = ''b'';',
  8774. 'var',
  8775. ' c: char;',
  8776. ' crg2: TCharRg2;',
  8777. ' s: TSetOfCharRg;',
  8778. 'begin',
  8779. ' c:=crg;',
  8780. ' crg:=c;',
  8781. ' crg2:=crg;',
  8782. ' if c=crg then ;',
  8783. ' if crg=c then ;',
  8784. ' if crg=crg2 then ;',
  8785. ' if c in s then ;',
  8786. ' if crg2 in s then ;',
  8787. ' c:=default(TCharRg);',
  8788. '']);
  8789. ConvertProgram;
  8790. CheckSource('TestCharSet_Custom',
  8791. LinesToStr([ // statements
  8792. 'this.crg = "b";',
  8793. 'this.c = "";',
  8794. 'this.crg2 = "m";',
  8795. 'this.s = {};',
  8796. '']),
  8797. LinesToStr([ // this.$main
  8798. '$mod.c = $mod.crg;',
  8799. '$mod.crg = $mod.c;',
  8800. '$mod.crg2 = $mod.crg;',
  8801. 'if ($mod.c === $mod.crg) ;',
  8802. 'if ($mod.crg === $mod.c) ;',
  8803. 'if ($mod.crg === $mod.crg2) ;',
  8804. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8805. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8806. '$mod.c = "a";',
  8807. '']));
  8808. end;
  8809. procedure TTestModule.TestWideChar;
  8810. begin
  8811. StartProgram(false);
  8812. Add([
  8813. 'procedure Fly(var c: char);',
  8814. 'begin',
  8815. 'end;',
  8816. 'procedure Run(var c: widechar);',
  8817. 'begin',
  8818. 'end;',
  8819. 'var',
  8820. ' c: char;',
  8821. ' wc: widechar;',
  8822. ' w: word;',
  8823. 'begin',
  8824. ' Fly(wc);',
  8825. ' Run(c);',
  8826. ' wc:=WideChar(w);',
  8827. ' w:=ord(wc);',
  8828. '']);
  8829. ConvertProgram;
  8830. CheckSource('TestWideChar_VarArg',
  8831. LinesToStr([ // statements
  8832. 'this.Fly = function (c) {',
  8833. '};',
  8834. 'this.Run = function (c) {',
  8835. '};',
  8836. 'this.c = "";',
  8837. 'this.wc = "";',
  8838. 'this.w = 0;',
  8839. '']),
  8840. LinesToStr([ // this.$main
  8841. '$mod.Fly({',
  8842. ' p: $mod,',
  8843. ' get: function () {',
  8844. ' return this.p.wc;',
  8845. ' },',
  8846. ' set: function (v) {',
  8847. ' this.p.wc = v;',
  8848. ' }',
  8849. '});',
  8850. '$mod.Run({',
  8851. ' p: $mod,',
  8852. ' get: function () {',
  8853. ' return this.p.c;',
  8854. ' },',
  8855. ' set: function (v) {',
  8856. ' this.p.c = v;',
  8857. ' }',
  8858. '});',
  8859. '$mod.wc = String.fromCharCode($mod.w);',
  8860. '$mod.w = $mod.wc.charCodeAt();',
  8861. '',
  8862. '']));
  8863. end;
  8864. procedure TTestModule.TestForCharDo;
  8865. begin
  8866. StartProgram(false);
  8867. Add([
  8868. 'var c: char;',
  8869. 'begin',
  8870. ' for c:=''a'' to ''c'' do ;',
  8871. ' for c:=c downto ''a'' do ;',
  8872. ' for c:=''Б'' to ''Я'' do ;',
  8873. '']);
  8874. ConvertProgram;
  8875. CheckSource('TestForCharDo',
  8876. LinesToStr([ // statements
  8877. 'this.c = "";']),
  8878. LinesToStr([ // this.$main
  8879. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8880. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8881. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8882. '']));
  8883. end;
  8884. procedure TTestModule.TestForCharInDo;
  8885. begin
  8886. StartProgram(false);
  8887. Add([
  8888. 'type',
  8889. ' TSetOfChar = set of char;',
  8890. ' TCharRg = ''a''..''z'';',
  8891. ' TSetOfCharRg = set of TCharRg;',
  8892. 'const Foo = ''foo'';',
  8893. 'var',
  8894. ' c,c2: char;',
  8895. ' s: string;',
  8896. ' a1: array of char;',
  8897. ' a2: array[1..3] of char;',
  8898. ' soc: TSetOfChar;',
  8899. ' socr: TSetOfCharRg;',
  8900. ' cr: TCharRg;',
  8901. 'begin',
  8902. ' for c in foo do ;',
  8903. ' for c in s do ;',
  8904. ' for c in char do ;',
  8905. ' for c in a1 do ;',
  8906. ' for c in a2 do ;',
  8907. ' for c in [''1''..''3''] do ;',
  8908. ' for c in TSetOfChar do ;',
  8909. ' for c in TCharRg do ;',
  8910. ' for c in soc do c2:=c;',
  8911. ' for c in TSetOfCharRg do ;',
  8912. ' for c in socr do ;',
  8913. ' for cr in TCharRg do ;',
  8914. ' for cr in TSetOfCharRg do ;',
  8915. ' for cr in socr do ;',
  8916. '']);
  8917. ConvertProgram;
  8918. CheckSource('TestForCharInDo',
  8919. LinesToStr([ // statements
  8920. 'this.Foo = "foo";',
  8921. 'this.c = "";',
  8922. 'this.c2 = "";',
  8923. 'this.s = "";',
  8924. 'this.a1 = [];',
  8925. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8926. 'this.soc = {};',
  8927. 'this.socr = {};',
  8928. 'this.cr = "a";',
  8929. '']),
  8930. LinesToStr([ // this.$main
  8931. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8932. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8933. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8934. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8935. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8936. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8937. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8938. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8939. 'for (var $l8 in $mod.soc) {',
  8940. ' $mod.c = String.fromCharCode($l8);',
  8941. ' $mod.c2 = $mod.c;',
  8942. '};',
  8943. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8944. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8945. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8946. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8947. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8948. '']));
  8949. end;
  8950. procedure TTestModule.TestProcTwoArgs;
  8951. begin
  8952. StartProgram(false);
  8953. Add('procedure Test(a,b: longint);');
  8954. Add('begin');
  8955. Add('end;');
  8956. Add('begin');
  8957. ConvertProgram;
  8958. CheckSource('TestProcTwoArgs',
  8959. LinesToStr([ // statements
  8960. 'this.Test = function (a,b) {',
  8961. '};'
  8962. ]),
  8963. LinesToStr([ // this.$main
  8964. ''
  8965. ]));
  8966. end;
  8967. procedure TTestModule.TestProc_DefaultValue;
  8968. begin
  8969. StartProgram(false);
  8970. Add('procedure p1(i: longint = 1);');
  8971. Add('begin');
  8972. Add('end;');
  8973. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8974. Add('begin');
  8975. Add('end;');
  8976. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8977. Add('begin');
  8978. Add('end;');
  8979. Add('begin');
  8980. Add(' p1;');
  8981. Add(' p1();');
  8982. Add(' p1(11);');
  8983. Add(' p2;');
  8984. Add(' p2();');
  8985. Add(' p2(12);');
  8986. Add(' p2(13,''b'');');
  8987. Add(' p3();');
  8988. ConvertProgram;
  8989. CheckSource('TestProc_DefaultValue',
  8990. LinesToStr([ // statements
  8991. 'this.p1 = function (i) {',
  8992. '};',
  8993. 'this.p2 = function (i,c) {',
  8994. '};',
  8995. 'this.p3 = function (d,b,s) {',
  8996. '};'
  8997. ]),
  8998. LinesToStr([ // this.$main
  8999. ' $mod.p1(1);',
  9000. ' $mod.p1(1);',
  9001. ' $mod.p1(11);',
  9002. ' $mod.p2(1,"a");',
  9003. ' $mod.p2(1,"a");',
  9004. ' $mod.p2(12,"a");',
  9005. ' $mod.p2(13,"b");',
  9006. ' $mod.p3(1.0,false,"abc");'
  9007. ]));
  9008. end;
  9009. procedure TTestModule.TestFunctionInt;
  9010. begin
  9011. StartProgram(false);
  9012. Add('function MyTest(Bar: longint): longint;');
  9013. Add('begin');
  9014. Add(' Result:=2*bar');
  9015. Add('end;');
  9016. Add('begin');
  9017. ConvertProgram;
  9018. CheckSource('TestFunctionInt',
  9019. LinesToStr([ // statements
  9020. 'this.MyTest = function (Bar) {',
  9021. ' var Result = 0;',
  9022. ' Result = 2*Bar;',
  9023. ' return Result;',
  9024. '};'
  9025. ]),
  9026. LinesToStr([ // this.$main
  9027. ''
  9028. ]));
  9029. end;
  9030. procedure TTestModule.TestFunctionString;
  9031. begin
  9032. StartProgram(false);
  9033. Add('function Test(Bar: string): string;');
  9034. Add('begin');
  9035. Add(' Result:=bar+BAR');
  9036. Add('end;');
  9037. Add('begin');
  9038. ConvertProgram;
  9039. CheckSource('TestFunctionString',
  9040. LinesToStr([ // statements
  9041. 'this.Test = function (Bar) {',
  9042. ' var Result = "";',
  9043. ' Result = Bar+Bar;',
  9044. ' return Result;',
  9045. '};'
  9046. ]),
  9047. LinesToStr([ // this.$main
  9048. ''
  9049. ]));
  9050. end;
  9051. procedure TTestModule.TestIfThen;
  9052. begin
  9053. StartProgram(false);
  9054. Add([
  9055. 'var b: boolean;',
  9056. 'begin',
  9057. ' if b then ;',
  9058. ' if b then else ;']);
  9059. ConvertProgram;
  9060. CheckSource('TestIfThen',
  9061. LinesToStr([ // statements
  9062. 'this.b = false;',
  9063. '']),
  9064. LinesToStr([ // this.$main
  9065. 'if ($mod.b) ;',
  9066. 'if ($mod.b) ;',
  9067. '']));
  9068. end;
  9069. procedure TTestModule.TestForLoop;
  9070. begin
  9071. StartProgram(false);
  9072. Add('var');
  9073. Add(' vI, vJ, vN: longint;');
  9074. Add('begin');
  9075. Add(' VJ:=0;');
  9076. Add(' VN:=3;');
  9077. Add(' for VI:=1 to VN do');
  9078. Add(' begin');
  9079. Add(' VJ:=VJ+VI;');
  9080. Add(' end;');
  9081. ConvertProgram;
  9082. CheckSource('TestForLoop',
  9083. LinesToStr([ // statements
  9084. 'this.vI = 0;',
  9085. 'this.vJ = 0;',
  9086. 'this.vN = 0;'
  9087. ]),
  9088. LinesToStr([ // this.$main
  9089. ' $mod.vJ = 0;',
  9090. ' $mod.vN = 3;',
  9091. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9092. ' $mod.vI = $l;',
  9093. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9094. ' };',
  9095. '']));
  9096. end;
  9097. procedure TTestModule.TestForLoopInsideFunction;
  9098. begin
  9099. StartProgram(false);
  9100. Add('function SumNumbers(Count: longint): longint;');
  9101. Add('var');
  9102. Add(' vI, vJ: longint;');
  9103. Add('begin');
  9104. Add(' vj:=0;');
  9105. Add(' for vi:=1 to count do');
  9106. Add(' begin');
  9107. Add(' vj:=vj+vi;');
  9108. Add(' end;');
  9109. Add('end;');
  9110. Add('begin');
  9111. Add(' sumnumbers(3);');
  9112. ConvertProgram;
  9113. CheckSource('TestForLoopInsideFunction',
  9114. LinesToStr([ // statements
  9115. 'this.SumNumbers = function (Count) {',
  9116. ' var Result = 0;',
  9117. ' var vI = 0;',
  9118. ' var vJ = 0;',
  9119. ' vJ = 0;',
  9120. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9121. ' vI = $l;',
  9122. ' vJ = vJ + vI;',
  9123. ' };',
  9124. ' return Result;',
  9125. '};'
  9126. ]),
  9127. LinesToStr([ // $mod.$main
  9128. ' $mod.SumNumbers(3);'
  9129. ]));
  9130. end;
  9131. procedure TTestModule.TestForLoop_ReadVarAfter;
  9132. begin
  9133. StartProgram(false);
  9134. Add('var');
  9135. Add(' vI: longint;');
  9136. Add('begin');
  9137. Add(' for vi:=1 to 2 do ;');
  9138. Add(' if vi=3 then ;');
  9139. ConvertProgram;
  9140. CheckSource('TestForLoop',
  9141. LinesToStr([ // statements
  9142. 'this.vI = 0;'
  9143. ]),
  9144. LinesToStr([ // this.$main
  9145. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9146. ' if ($mod.vI===3) ;'
  9147. ]));
  9148. end;
  9149. procedure TTestModule.TestForLoop_Nested;
  9150. begin
  9151. StartProgram(false);
  9152. Add('function SumNumbers(Count: longint): longint;');
  9153. Add('var');
  9154. Add(' vI, vJ, vK: longint;');
  9155. Add('begin');
  9156. Add(' VK:=0;');
  9157. Add(' for VI:=1 to count do');
  9158. Add(' begin');
  9159. Add(' for vj:=1 to vi do');
  9160. Add(' begin');
  9161. Add(' vk:=VK+VI;');
  9162. Add(' end;');
  9163. Add(' end;');
  9164. Add('end;');
  9165. Add('begin');
  9166. Add(' sumnumbers(3);');
  9167. ConvertProgram;
  9168. CheckSource('TestForLoopInFunction',
  9169. LinesToStr([ // statements
  9170. 'this.SumNumbers = function (Count) {',
  9171. ' var Result = 0;',
  9172. ' var vI = 0;',
  9173. ' var vJ = 0;',
  9174. ' var vK = 0;',
  9175. ' vK = 0;',
  9176. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9177. ' vI = $l;',
  9178. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9179. ' vJ = $l1;',
  9180. ' vK = vK + vI;',
  9181. ' };',
  9182. ' };',
  9183. ' return Result;',
  9184. '};'
  9185. ]),
  9186. LinesToStr([ // $mod.$main
  9187. ' $mod.SumNumbers(3);'
  9188. ]));
  9189. end;
  9190. procedure TTestModule.TestRepeatUntil;
  9191. begin
  9192. StartProgram(false);
  9193. Add('var');
  9194. Add(' vI, vJ, vN: longint;');
  9195. Add('begin');
  9196. Add(' vn:=3;');
  9197. Add(' vj:=0;');
  9198. Add(' VI:=0;');
  9199. Add(' repeat');
  9200. Add(' VI:=vi+1;');
  9201. Add(' vj:=VJ+vI;');
  9202. Add(' until vi>=vn');
  9203. ConvertProgram;
  9204. CheckSource('TestRepeatUntil',
  9205. LinesToStr([ // statements
  9206. 'this.vI = 0;',
  9207. 'this.vJ = 0;',
  9208. 'this.vN = 0;'
  9209. ]),
  9210. LinesToStr([ // $mod.$main
  9211. ' $mod.vN = 3;',
  9212. ' $mod.vJ = 0;',
  9213. ' $mod.vI = 0;',
  9214. ' do{',
  9215. ' $mod.vI = $mod.vI + 1;',
  9216. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9217. ' }while(!($mod.vI>=$mod.vN));'
  9218. ]));
  9219. end;
  9220. procedure TTestModule.TestAsmBlock;
  9221. begin
  9222. StartProgram(false);
  9223. Add([
  9224. 'var',
  9225. ' vI: longint;',
  9226. 'begin',
  9227. ' vi:=1;',
  9228. ' asm',
  9229. ' if (vI===1) {',
  9230. ' vI=2;',
  9231. //' console.log(''end;'');', ToDo
  9232. ' }',
  9233. ' if (vI===2){ vI=3; }',
  9234. ' end;',
  9235. ' VI:=4;']);
  9236. ConvertProgram;
  9237. CheckSource('TestAsmBlock',
  9238. LinesToStr([ // statements
  9239. 'this.vI = 0;'
  9240. ]),
  9241. LinesToStr([ // $mod.$main
  9242. '$mod.vI = 1;',
  9243. 'if (vI===1) {',
  9244. ' vI=2;',
  9245. '}',
  9246. 'if (vI===2){ vI=3; }',
  9247. ';',
  9248. '$mod.vI = 4;'
  9249. ]));
  9250. end;
  9251. procedure TTestModule.TestAsmPas_Impl;
  9252. begin
  9253. StartUnit(false);
  9254. Add('interface');
  9255. Add('const cIntf: longint = 1;');
  9256. Add('var vIntf: longint;');
  9257. Add('implementation');
  9258. Add('const cImpl: longint = 2;');
  9259. Add('var vImpl: longint;');
  9260. Add('procedure DoIt;');
  9261. Add('const cLoc: longint = 3;');
  9262. Add('var vLoc: longint;');
  9263. Add('begin;');
  9264. Add(' asm');
  9265. //Add(' pas(vIntf)=pas(cIntf);');
  9266. //Add(' pas(vImpl)=pas(cImpl);');
  9267. //Add(' pas(vLoc)=pas(cLoc);');
  9268. Add(' end;');
  9269. Add('end;');
  9270. ConvertUnit;
  9271. CheckSource('TestAsmPas_Impl',
  9272. LinesToStr([
  9273. 'var $impl = $mod.$impl;',
  9274. 'this.cIntf = 1;',
  9275. 'this.vIntf = 0;',
  9276. '']),
  9277. '', // this.$init
  9278. LinesToStr([ // implementation
  9279. '$impl.cImpl = 2;',
  9280. '$impl.vImpl = 0;',
  9281. 'var cLoc = 3;',
  9282. '$impl.DoIt = function () {',
  9283. ' var vLoc = 0;',
  9284. '};',
  9285. '']) );
  9286. end;
  9287. procedure TTestModule.TestTryFinally;
  9288. begin
  9289. StartProgram(false);
  9290. Add('var i: longint;');
  9291. Add('begin');
  9292. Add(' try');
  9293. Add(' i:=0; i:=2 div i;');
  9294. Add(' finally');
  9295. Add(' i:=3');
  9296. Add(' end;');
  9297. ConvertProgram;
  9298. CheckSource('TestTryFinally',
  9299. LinesToStr([ // statements
  9300. 'this.i = 0;'
  9301. ]),
  9302. LinesToStr([ // $mod.$main
  9303. 'try {',
  9304. ' $mod.i = 0;',
  9305. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9306. '} finally {',
  9307. ' $mod.i = 3;',
  9308. '};'
  9309. ]));
  9310. end;
  9311. procedure TTestModule.TestTryExcept;
  9312. begin
  9313. StartProgram(false);
  9314. Add([
  9315. 'type',
  9316. ' TObject = class end;',
  9317. ' Exception = class Msg: string; end;',
  9318. ' EInvalidCast = class(Exception) end;',
  9319. 'var vI: longint;',
  9320. 'begin',
  9321. ' try',
  9322. ' vi:=1;',
  9323. ' except',
  9324. ' vi:=2',
  9325. ' end;',
  9326. ' try',
  9327. ' vi:=3;',
  9328. ' except',
  9329. ' raise;',
  9330. ' end;',
  9331. ' try',
  9332. ' VI:=4;',
  9333. ' except',
  9334. ' on einvalidcast do',
  9335. ' raise;',
  9336. ' on E: exception do',
  9337. ' if e.msg='''' then',
  9338. ' raise e;',
  9339. ' else',
  9340. ' vi:=5',
  9341. ' end;',
  9342. ' try',
  9343. ' VI:=6;',
  9344. ' except',
  9345. ' on einvalidcast do ;',
  9346. ' end;',
  9347. '']);
  9348. ConvertProgram;
  9349. CheckSource('TestTryExcept',
  9350. LinesToStr([ // statements
  9351. 'rtl.createClass(this, "TObject", null, function () {',
  9352. ' this.$init = function () {',
  9353. ' };',
  9354. ' this.$final = function () {',
  9355. ' };',
  9356. '});',
  9357. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9358. ' this.$init = function () {',
  9359. ' $mod.TObject.$init.call(this);',
  9360. ' this.Msg = "";',
  9361. ' };',
  9362. '});',
  9363. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9364. '});',
  9365. 'this.vI = 0;'
  9366. ]),
  9367. LinesToStr([ // $mod.$main
  9368. 'try {',
  9369. ' $mod.vI = 1;',
  9370. '} catch ($e) {',
  9371. ' $mod.vI = 2;',
  9372. '};',
  9373. 'try {',
  9374. ' $mod.vI = 3;',
  9375. '} catch ($e) {',
  9376. ' throw $e;',
  9377. '};',
  9378. 'try {',
  9379. ' $mod.vI = 4;',
  9380. '} catch ($e) {',
  9381. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9382. ' throw $e',
  9383. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9384. ' var E = $e;',
  9385. ' if (E.Msg === "") throw E;',
  9386. ' } else {',
  9387. ' $mod.vI = 5;',
  9388. ' }',
  9389. '};',
  9390. 'try {',
  9391. ' $mod.vI = 6;',
  9392. '} catch ($e) {',
  9393. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9394. ' } else throw $e',
  9395. '};',
  9396. '']));
  9397. end;
  9398. procedure TTestModule.TestTryExcept_ReservedWords;
  9399. begin
  9400. StartProgram(false);
  9401. Add([
  9402. 'type',
  9403. ' TObject = class end;',
  9404. ' Exception = class',
  9405. ' Symbol: string;',
  9406. ' end;',
  9407. 'var &try: longint;',
  9408. 'begin',
  9409. ' try',
  9410. ' &try:=4;',
  9411. ' except',
  9412. ' on Error: exception do',
  9413. ' if errOR.symBol='''' then',
  9414. ' raise ERRor;',
  9415. ' end;',
  9416. '']);
  9417. ConvertProgram;
  9418. CheckSource('TestTryExcept_ReservedWords',
  9419. LinesToStr([ // statements
  9420. 'rtl.createClass(this, "TObject", null, function () {',
  9421. ' this.$init = function () {',
  9422. ' };',
  9423. ' this.$final = function () {',
  9424. ' };',
  9425. '});',
  9426. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9427. ' this.$init = function () {',
  9428. ' $mod.TObject.$init.call(this);',
  9429. ' this.Symbol = "";',
  9430. ' };',
  9431. '});',
  9432. 'this.Try = 0;',
  9433. '']),
  9434. LinesToStr([ // $mod.$main
  9435. 'try {',
  9436. ' $mod.Try = 4;',
  9437. '} catch ($e) {',
  9438. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9439. ' var error = $e;',
  9440. ' if (error.Symbol === "") throw error;',
  9441. ' } else throw $e',
  9442. '};',
  9443. '']));
  9444. end;
  9445. procedure TTestModule.TestIfThenRaiseElse;
  9446. begin
  9447. StartProgram(false);
  9448. Add([
  9449. 'type',
  9450. ' TObject = class',
  9451. ' constructor Create;',
  9452. ' end;',
  9453. 'constructor TObject.Create;',
  9454. 'begin',
  9455. 'end;',
  9456. 'var b: boolean;',
  9457. 'begin',
  9458. ' if b then',
  9459. ' raise TObject.Create',
  9460. ' else',
  9461. ' b:=false;',
  9462. '']);
  9463. ConvertProgram;
  9464. CheckSource('TestIfThenRaiseElse',
  9465. LinesToStr([ // statements
  9466. 'rtl.createClass(this, "TObject", null, function () {',
  9467. ' this.$init = function () {',
  9468. ' };',
  9469. ' this.$final = function () {',
  9470. ' };',
  9471. ' this.Create = function () {',
  9472. ' return this;',
  9473. ' };',
  9474. '});',
  9475. 'this.b = false;',
  9476. '']),
  9477. LinesToStr([ // $mod.$main
  9478. 'if ($mod.b) {',
  9479. ' throw $mod.TObject.$create("Create")}',
  9480. ' else $mod.b = false;',
  9481. '']));
  9482. end;
  9483. procedure TTestModule.TestCaseOf;
  9484. begin
  9485. StartProgram(false);
  9486. Add([
  9487. 'const e: longint; external name ''$e'';',
  9488. 'var vI: longint;',
  9489. 'begin',
  9490. ' case vi of',
  9491. ' 1: ;',
  9492. ' 2: vi:=3;',
  9493. ' e: ;',
  9494. ' else',
  9495. ' VI:=4',
  9496. ' end;']);
  9497. ConvertProgram;
  9498. CheckSource('TestCaseOf',
  9499. LinesToStr([ // statements
  9500. 'this.vI = 0;'
  9501. ]),
  9502. LinesToStr([ // $mod.$main
  9503. 'var $tmp = $mod.vI;',
  9504. 'if ($tmp === 1) {}',
  9505. 'else if ($tmp === 2) {',
  9506. ' $mod.vI = 3}',
  9507. ' else if ($tmp === $e) {}',
  9508. 'else {',
  9509. ' $mod.vI = 4;',
  9510. '};'
  9511. ]));
  9512. end;
  9513. procedure TTestModule.TestCaseOf_UseSwitch;
  9514. begin
  9515. StartProgram(false);
  9516. Converter.UseSwitchStatement:=true;
  9517. Add('var Vi: longint;');
  9518. Add('begin');
  9519. Add(' case vi of');
  9520. Add(' 1: ;');
  9521. Add(' 2: VI:=3;');
  9522. Add(' else');
  9523. Add(' vi:=4');
  9524. Add(' end;');
  9525. ConvertProgram;
  9526. CheckSource('TestCaseOf_UseSwitch',
  9527. LinesToStr([ // statements
  9528. 'this.Vi = 0;'
  9529. ]),
  9530. LinesToStr([ // $mod.$main
  9531. 'switch ($mod.Vi) {',
  9532. 'case 1:',
  9533. ' break;',
  9534. 'case 2:',
  9535. ' $mod.Vi = 3;',
  9536. ' break;',
  9537. 'default:',
  9538. ' $mod.Vi = 4;',
  9539. '};'
  9540. ]));
  9541. end;
  9542. procedure TTestModule.TestCaseOfNoElse;
  9543. begin
  9544. StartProgram(false);
  9545. Add('var Vi: longint;');
  9546. Add('begin');
  9547. Add(' case vi of');
  9548. Add(' 1: begin vi:=2; VI:=3; end;');
  9549. Add(' end;');
  9550. ConvertProgram;
  9551. CheckSource('TestCaseOfNoElse',
  9552. LinesToStr([ // statements
  9553. 'this.Vi = 0;'
  9554. ]),
  9555. LinesToStr([ // $mod.$main
  9556. 'var $tmp = $mod.Vi;',
  9557. 'if ($tmp === 1) {',
  9558. ' $mod.Vi = 2;',
  9559. ' $mod.Vi = 3;',
  9560. '};'
  9561. ]));
  9562. end;
  9563. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9564. begin
  9565. StartProgram(false);
  9566. Converter.UseSwitchStatement:=true;
  9567. Add('var vI: longint;');
  9568. Add('begin');
  9569. Add(' case vi of');
  9570. Add(' 1: begin VI:=2; vi:=3; end;');
  9571. Add(' end;');
  9572. ConvertProgram;
  9573. CheckSource('TestCaseOfNoElse_UseSwitch',
  9574. LinesToStr([ // statements
  9575. 'this.vI = 0;'
  9576. ]),
  9577. LinesToStr([ // $mod.$main
  9578. 'switch ($mod.vI) {',
  9579. 'case 1:',
  9580. ' $mod.vI = 2;',
  9581. ' $mod.vI = 3;',
  9582. ' break;',
  9583. '};'
  9584. ]));
  9585. end;
  9586. procedure TTestModule.TestCaseOfRange;
  9587. begin
  9588. StartProgram(false);
  9589. Add('var vI: longint;');
  9590. Add('begin');
  9591. Add(' case vi of');
  9592. Add(' 1..3: vi:=14;');
  9593. Add(' 4,5: vi:=16;');
  9594. Add(' 6..7,9..10: ;');
  9595. Add(' else ;');
  9596. Add(' end;');
  9597. ConvertProgram;
  9598. CheckSource('TestCaseOfRange',
  9599. LinesToStr([ // statements
  9600. 'this.vI = 0;'
  9601. ]),
  9602. LinesToStr([ // $mod.$main
  9603. 'var $tmp = $mod.vI;',
  9604. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9605. ' $mod.vI = 14',
  9606. '} else if (($tmp === 4) || ($tmp === 5)){',
  9607. ' $mod.vI = 16',
  9608. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9609. ]));
  9610. end;
  9611. procedure TTestModule.TestCaseOfString;
  9612. begin
  9613. StartProgram(false);
  9614. Add([
  9615. 'var s,h: string;',
  9616. 'begin',
  9617. ' case s of',
  9618. ' ''foo'': s:=h;',
  9619. ' ''a''..''z'': h:=s;',
  9620. ' ''ў'', ''ё'': ;',
  9621. ' ''Б''..''Я'': ;',
  9622. ' end;',
  9623. '']);
  9624. ConvertProgram;
  9625. CheckSource('TestCaseOfString',
  9626. LinesToStr([ // statements
  9627. 'this.s = "";',
  9628. 'this.h = "";',
  9629. '']),
  9630. LinesToStr([ // $mod.$main
  9631. 'var $tmp = $mod.s;',
  9632. 'if ($tmp === "foo") {',
  9633. ' $mod.s = $mod.h}',
  9634. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9635. ' $mod.h = $mod.s}',
  9636. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9637. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9638. '']));
  9639. end;
  9640. procedure TTestModule.TestCaseOfChar;
  9641. begin
  9642. StartProgram(false);
  9643. Add([
  9644. 'var s,h: char;',
  9645. 'begin',
  9646. ' case s of',
  9647. ' ''a''..''z'': h:=s;',
  9648. ' ''ä'': ;',
  9649. ' ''ў'', ''ё'': ;',
  9650. ' ''Б''..''Я'': ;',
  9651. ' end;',
  9652. '']);
  9653. ConvertProgram;
  9654. CheckSource('TestCaseOfString',
  9655. LinesToStr([ // statements
  9656. 'this.s = "";',
  9657. 'this.h = "";',
  9658. '']),
  9659. LinesToStr([ // $mod.$main
  9660. 'var $tmp = $mod.s;',
  9661. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9662. ' $mod.h = $mod.s}',
  9663. ' else if ($tmp === "ä") {}',
  9664. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9665. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9666. '']));
  9667. end;
  9668. procedure TTestModule.TestCaseOfExternalClassConst;
  9669. begin
  9670. StartProgram(false);
  9671. Add([
  9672. '{$modeswitch externalclass}',
  9673. 'type',
  9674. ' TBird = class external name ''Bird''',
  9675. ' const e: longint;',
  9676. ' end;',
  9677. 'var vI: longint;',
  9678. 'begin',
  9679. ' case vi of',
  9680. ' 1: vi:=3;',
  9681. ' TBird.e: ;',
  9682. ' end;']);
  9683. ConvertProgram;
  9684. CheckSource('TestCaseOfExternalClassConst',
  9685. LinesToStr([ // statements
  9686. 'this.vI = 0;'
  9687. ]),
  9688. LinesToStr([ // $mod.$main
  9689. 'var $tmp = $mod.vI;',
  9690. 'if ($tmp === 1) {',
  9691. ' $mod.vI = 3}',
  9692. ' else if ($tmp === Bird.e) ;'
  9693. ]));
  9694. end;
  9695. procedure TTestModule.TestDebugger;
  9696. begin
  9697. StartProgram(false);
  9698. Add([
  9699. 'procedure DoIt;',
  9700. 'begin',
  9701. ' deBugger;',
  9702. ' DeBugger();',
  9703. 'end;',
  9704. 'begin',
  9705. ' Debugger;']);
  9706. ConvertProgram;
  9707. CheckSource('TestDebugger',
  9708. LinesToStr([ // statements
  9709. 'this.DoIt = function () {',
  9710. ' debugger;',
  9711. ' debugger;',
  9712. '};',
  9713. '']),
  9714. LinesToStr([ // $mod.$main
  9715. 'debugger;',
  9716. '']));
  9717. end;
  9718. procedure TTestModule.TestArray_Dynamic;
  9719. begin
  9720. StartProgram(false);
  9721. Add([
  9722. 'type',
  9723. ' TArrayInt = array of longint;',
  9724. 'var',
  9725. ' Arr: TArrayInt;',
  9726. ' i: longint;',
  9727. ' b: boolean;',
  9728. 'begin',
  9729. ' SetLength(arr,3);',
  9730. ' arr[0]:=4;',
  9731. ' arr[1]:=length(arr)+arr[0];',
  9732. ' arr[i]:=5;',
  9733. ' arr[arr[i]]:=arr[6];',
  9734. ' i:=low(arr);',
  9735. ' i:=high(arr);',
  9736. ' b:=Assigned(arr);',
  9737. ' Arr:=default(TArrayInt);']);
  9738. ConvertProgram;
  9739. CheckSource('TestArray_Dynamic',
  9740. LinesToStr([ // statements
  9741. 'this.Arr = [];',
  9742. 'this.i = 0;',
  9743. 'this.b = false;'
  9744. ]),
  9745. LinesToStr([ // $mod.$main
  9746. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9747. '$mod.Arr[0] = 4;',
  9748. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9749. '$mod.Arr[$mod.i] = 5;',
  9750. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9751. '$mod.i = 0;',
  9752. '$mod.i = rtl.length($mod.Arr) - 1;',
  9753. '$mod.b = rtl.length($mod.Arr) > 0;',
  9754. '$mod.Arr = [];',
  9755. '']));
  9756. end;
  9757. procedure TTestModule.TestArray_Dynamic_Nil;
  9758. begin
  9759. StartProgram(false);
  9760. Add('type');
  9761. Add(' TArrayInt = array of longint;');
  9762. Add('var');
  9763. Add(' Arr: TArrayInt;');
  9764. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9765. Add('begin');
  9766. Add(' arr:=nil;');
  9767. Add(' if arr=nil then;');
  9768. Add(' if nil=arr then;');
  9769. Add(' if arr<>nil then;');
  9770. Add(' if nil<>arr then;');
  9771. Add(' DoIt(nil,nil);');
  9772. ConvertProgram;
  9773. CheckSource('TestArray_Dynamic',
  9774. LinesToStr([ // statements
  9775. 'this.Arr = [];',
  9776. 'this.DoIt = function(i,j){',
  9777. '};'
  9778. ]),
  9779. LinesToStr([ // $mod.$main
  9780. '$mod.Arr = [];',
  9781. 'if (rtl.length($mod.Arr) === 0) ;',
  9782. 'if (rtl.length($mod.Arr) === 0) ;',
  9783. 'if (rtl.length($mod.Arr) > 0) ;',
  9784. 'if (rtl.length($mod.Arr) > 0) ;',
  9785. '$mod.DoIt([],[]);',
  9786. '']));
  9787. end;
  9788. procedure TTestModule.TestArray_DynMultiDimensional;
  9789. begin
  9790. StartProgram(false);
  9791. Add([
  9792. 'type',
  9793. ' TArrayInt = array of longint;',
  9794. ' TArrayArrayInt = array of TArrayInt;',
  9795. 'var',
  9796. ' Arr: TArrayInt;',
  9797. ' Arr2: TArrayArrayInt;',
  9798. ' i: longint;',
  9799. 'begin',
  9800. ' arr2:=nil;',
  9801. ' if arr2=nil then;',
  9802. ' if nil=arr2 then;',
  9803. ' i:=low(arr2);',
  9804. ' i:=low(arr2[1]);',
  9805. ' i:=high(arr2);',
  9806. ' i:=high(arr2[2]);',
  9807. ' arr2[3]:=arr;',
  9808. ' arr2[4][5]:=i;',
  9809. ' i:=arr2[6][7];',
  9810. ' arr2[8,9]:=i;',
  9811. ' i:=arr2[10,11];',
  9812. ' SetLength(arr2,14);',
  9813. ' SetLength(arr2[15],16);']);
  9814. ConvertProgram;
  9815. CheckSource('TestArray_Dynamic',
  9816. LinesToStr([ // statements
  9817. 'this.Arr = [];',
  9818. 'this.Arr2 = [];',
  9819. 'this.i = 0;'
  9820. ]),
  9821. LinesToStr([ // $mod.$main
  9822. '$mod.Arr2 = [];',
  9823. 'if (rtl.length($mod.Arr2) === 0) ;',
  9824. 'if (rtl.length($mod.Arr2) === 0) ;',
  9825. '$mod.i = 0;',
  9826. '$mod.i = 0;',
  9827. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9828. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9829. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9830. '$mod.Arr2[4][5] = $mod.i;',
  9831. '$mod.i = $mod.Arr2[6][7];',
  9832. '$mod.Arr2[8][9] = $mod.i;',
  9833. '$mod.i = $mod.Arr2[10][11];',
  9834. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9835. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9836. '']));
  9837. end;
  9838. procedure TTestModule.TestArray_DynamicAssign;
  9839. begin
  9840. StartProgram(false);
  9841. Add([
  9842. 'type',
  9843. ' TArrayInt = array of longint;',
  9844. ' TArrayArrayInt = array of TArrayInt;',
  9845. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9846. 'begin',
  9847. 'end;',
  9848. 'procedure Fly(var a: TArrayInt);',
  9849. 'begin',
  9850. 'end;',
  9851. 'var',
  9852. ' Arr: TArrayInt;',
  9853. ' Arr2: TArrayArrayInt;',
  9854. 'begin',
  9855. ' arr:=nil;',
  9856. ' arr2:=nil;',
  9857. ' arr2[1]:=nil;',
  9858. ' arr2[2]:=arr;',
  9859. ' Run(arr,arr,arr);',
  9860. ' Fly(arr);',
  9861. ' Run(arr2[4],arr2[5],arr2[6]);',
  9862. ' Fly(arr2[7]);',
  9863. '']);
  9864. ConvertProgram;
  9865. CheckSource('TestArray_DynamicAssign',
  9866. LinesToStr([ // statements
  9867. 'this.Run = function (a, b, c) {',
  9868. '};',
  9869. 'this.Fly = function (a) {',
  9870. '};',
  9871. 'this.Arr = [];',
  9872. 'this.Arr2 = [];',
  9873. '']),
  9874. LinesToStr([ // $mod.$main
  9875. '$mod.Arr = [];',
  9876. '$mod.Arr2 = [];',
  9877. '$mod.Arr2[1] = [];',
  9878. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9879. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9880. '$mod.Fly({',
  9881. ' p: $mod,',
  9882. ' get: function () {',
  9883. ' return this.p.Arr;',
  9884. ' },',
  9885. ' set: function (v) {',
  9886. ' this.p.Arr = v;',
  9887. ' }',
  9888. '});',
  9889. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9890. '$mod.Fly({',
  9891. ' a: 7,',
  9892. ' p: $mod.Arr2,',
  9893. ' get: function () {',
  9894. ' return this.p[this.a];',
  9895. ' },',
  9896. ' set: function (v) {',
  9897. ' this.p[this.a] = v;',
  9898. ' }',
  9899. '});',
  9900. '']));
  9901. end;
  9902. procedure TTestModule.TestArray_StaticInt;
  9903. begin
  9904. StartProgram(false);
  9905. Add('type');
  9906. Add(' TArrayInt = array[2..4] of longint;');
  9907. Add('var');
  9908. Add(' Arr: TArrayInt;');
  9909. Add(' Arr2: TArrayInt = (5,6,7);');
  9910. Add(' i: longint;');
  9911. Add(' b: boolean;');
  9912. Add('begin');
  9913. Add(' arr[2]:=4;');
  9914. Add(' arr[3]:=arr[2]+arr[3];');
  9915. Add(' arr[i]:=5;');
  9916. Add(' arr[arr[i]]:=arr[high(arr)];');
  9917. Add(' i:=low(arr);');
  9918. Add(' i:=high(arr);');
  9919. Add(' b:=arr[2]=arr[3];');
  9920. Add(' arr:=default(TArrayInt);');
  9921. ConvertProgram;
  9922. CheckSource('TestArray_StaticInt',
  9923. LinesToStr([ // statements
  9924. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9925. 'this.Arr2 = [5, 6, 7];',
  9926. 'this.i = 0;',
  9927. 'this.b = false;'
  9928. ]),
  9929. LinesToStr([ // $mod.$main
  9930. '$mod.Arr[0] = 4;',
  9931. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9932. '$mod.Arr[$mod.i-2] = 5;',
  9933. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9934. '$mod.i = 2;',
  9935. '$mod.i = 4;',
  9936. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9937. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9938. '']));
  9939. end;
  9940. procedure TTestModule.TestArray_StaticBool;
  9941. begin
  9942. StartProgram(false);
  9943. Add('type');
  9944. Add(' TBools = array[boolean] of boolean;');
  9945. Add(' TBool2 = array[true..true] of boolean;');
  9946. Add('var');
  9947. Add(' Arr: TBools;');
  9948. Add(' Arr2: TBool2;');
  9949. Add(' Arr3: TBools = (true,false);');
  9950. Add(' b: boolean;');
  9951. Add('begin');
  9952. Add(' b:=low(arr);');
  9953. Add(' b:=high(arr);');
  9954. Add(' arr[true]:=false;');
  9955. Add(' arr[false]:=arr[b] or arr[true];');
  9956. Add(' arr[b]:=true;');
  9957. Add(' arr[arr[b]]:=arr[high(arr)];');
  9958. Add(' b:=arr[false]=arr[true];');
  9959. Add(' b:=low(arr2);');
  9960. Add(' b:=high(arr2);');
  9961. Add(' arr2[true]:=true;');
  9962. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9963. Add(' arr2[b]:=false;');
  9964. ConvertProgram;
  9965. CheckSource('TestArray_StaticBool',
  9966. LinesToStr([ // statements
  9967. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9968. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9969. 'this.Arr3 = [true, false];',
  9970. 'this.b = false;'
  9971. ]),
  9972. LinesToStr([ // $mod.$main
  9973. '$mod.b = false;',
  9974. '$mod.b = true;',
  9975. '$mod.Arr[1] = false;',
  9976. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9977. '$mod.Arr[+$mod.b] = true;',
  9978. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9979. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9980. '$mod.b = true;',
  9981. '$mod.b = true;',
  9982. '$mod.Arr2[0] = true;',
  9983. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9984. '$mod.Arr2[1-$mod.b] = false;',
  9985. '']));
  9986. end;
  9987. procedure TTestModule.TestArray_StaticChar;
  9988. begin
  9989. StartProgram(false);
  9990. Add([
  9991. 'type',
  9992. ' TChars = array[char] of char;',
  9993. ' TChars2 = array[''a''..''z''] of char;',
  9994. 'var',
  9995. ' Arr: TChars;',
  9996. ' Arr2: TChars2;',
  9997. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9998. ' Arr4: array[11..13] of char = ''pas'';',
  9999. ' Arr5: array[21..22] of char = ''äö'';',
  10000. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10001. ' c: char;',
  10002. ' b: boolean;',
  10003. 'begin',
  10004. ' c:=low(arr);',
  10005. ' c:=high(arr);',
  10006. ' arr[''B'']:=''a'';',
  10007. ' arr[''D'']:=arr[c];',
  10008. ' arr[c]:=arr[''d''];',
  10009. ' arr[arr[c]]:=arr[high(arr)];',
  10010. ' b:=arr[low(arr)]=arr[''e''];',
  10011. ' c:=low(arr2);',
  10012. ' c:=high(arr2);',
  10013. ' arr2[''b'']:=''f'';',
  10014. ' arr2[''a'']:=arr2[c];',
  10015. ' arr2[c]:=arr2[''g''];']);
  10016. ConvertProgram;
  10017. CheckSource('TestArray_StaticChar',
  10018. LinesToStr([ // statements
  10019. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  10020. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  10021. 'this.Arr3 = ["p", "a", "s"];',
  10022. 'this.Arr4 = ["p", "a", "s"];',
  10023. 'this.Arr5 = ["ä", "ö"];',
  10024. 'this.Arr6 = ["ä", "ö"];',
  10025. 'this.c = "";',
  10026. 'this.b = false;',
  10027. '']),
  10028. LinesToStr([ // $mod.$main
  10029. '$mod.c = "\x00";',
  10030. '$mod.c = "\uFFFF";',
  10031. '$mod.Arr[66] = "a";',
  10032. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10033. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10034. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10035. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10036. '$mod.c = "a";',
  10037. '$mod.c = "z";',
  10038. '$mod.Arr2[1] = "f";',
  10039. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10040. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10041. '']));
  10042. end;
  10043. procedure TTestModule.TestArray_StaticMultiDim;
  10044. begin
  10045. StartProgram(false);
  10046. Add([
  10047. 'type',
  10048. ' TArrayInt = array[1..3] of longint;',
  10049. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10050. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10051. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10052. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10053. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10054. 'var',
  10055. ' Arr: TArrayInt;',
  10056. ' Arr2: TArrayArrayInt;',
  10057. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10058. ' Arr4: TArrayArrayInt;',
  10059. ' ArrDim2: TArrayDim2Int;',
  10060. ' ArrDim3: TArrayDim3Int;',
  10061. ' ArrDim4: TArrayDim4Int;',
  10062. ' i: longint;',
  10063. 'begin',
  10064. ' i:=low(arr);',
  10065. ' i:=low(arr2);',
  10066. ' i:=low(arr2[5]);',
  10067. ' i:=high(arr);',
  10068. ' i:=high(arr2);',
  10069. ' i:=high(arr2[6]);',
  10070. ' arr2[5]:=arr;',
  10071. ' arr2[6][2]:=i;',
  10072. ' i:=arr2[6][3];',
  10073. ' arr2[6,3]:=i;',
  10074. ' i:=arr2[5,2];',
  10075. ' arr2:=arr2;',// clone multi dim static array
  10076. ' arr3:=arr3;',// clone anonymous multi dim static array
  10077. ' arr4:=arr4;',
  10078. ' Arr:=Arr;',
  10079. ' ArrDim2:=ArrDim2;',
  10080. ' ArrDim3:=ArrDim3;',
  10081. ' ArrDim4:=ArrDim4;',
  10082. '']);
  10083. ConvertProgram;
  10084. CheckSource('TestArray_StaticMultiDim',
  10085. LinesToStr([ // statements
  10086. 'this.TArrayArrayInt$clone = function (a) {',
  10087. ' var b = [];',
  10088. ' b.length = 2;',
  10089. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10090. ' return b;',
  10091. '};',
  10092. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10093. ' var b = [];',
  10094. ' b.length = 2;',
  10095. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10096. ' return b;',
  10097. '};',
  10098. 'this.TArrayDim2Int$clone = function (a) {',
  10099. ' var b = [];',
  10100. ' b.length = 2;',
  10101. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10102. ' return b;',
  10103. '};',
  10104. 'this.TArrayDim3Int$clone = function (a) {',
  10105. ' var b = [];',
  10106. ' b.length = 2;',
  10107. ' for (var c = 0; c < 2; c++) {',
  10108. ' var d = b[c] = [];',
  10109. ' d.length = 3;',
  10110. ' var e = a[c];',
  10111. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10112. ' };',
  10113. ' return b;',
  10114. '};',
  10115. 'this.TArrayDim4Int$clone = function (a) {',
  10116. ' var b = [];',
  10117. ' b.length = 2;',
  10118. ' for (var c = 0; c < 2; c++) {',
  10119. ' var d = b[c] = [];',
  10120. ' d.length = 3;',
  10121. ' var e = a[c];',
  10122. ' for (var f = 0; f < 3; f++) {',
  10123. ' var g = d[f] = [];',
  10124. ' g.length = 4;',
  10125. ' var h = e[f];',
  10126. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10127. ' };',
  10128. ' };',
  10129. ' return b;',
  10130. '};',
  10131. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10132. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10133. 'this.Arr3$a$clone = function (a) {',
  10134. ' var b = [];',
  10135. ' b.length = 2;',
  10136. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10137. ' return b;',
  10138. '};',
  10139. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10140. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10141. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10142. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10143. 'this.ArrDim4 = rtl.arraySetLength(',
  10144. ' null,',
  10145. ' 0,',
  10146. ' 2,',
  10147. ' 3,',
  10148. ' 4,',
  10149. ' 5',
  10150. ');',
  10151. 'this.i = 0;'
  10152. ]),
  10153. LinesToStr([ // $mod.$main
  10154. '$mod.i = 1;',
  10155. '$mod.i = 5;',
  10156. '$mod.i = 1;',
  10157. '$mod.i = 3;',
  10158. '$mod.i = 6;',
  10159. '$mod.i = 3;',
  10160. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10161. '$mod.Arr2[1][1] = $mod.i;',
  10162. '$mod.i = $mod.Arr2[1][2];',
  10163. '$mod.Arr2[1][2] = $mod.i;',
  10164. '$mod.i = $mod.Arr2[0][1];',
  10165. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10166. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10167. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10168. '$mod.Arr = $mod.Arr.slice(0);',
  10169. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10170. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10171. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10172. '']));
  10173. end;
  10174. procedure TTestModule.TestArray_StaticInFunction;
  10175. begin
  10176. StartProgram(false);
  10177. Add([
  10178. 'const TArrayInt = 3;',
  10179. 'const TArrayArrayInt = 4;',
  10180. 'procedure DoIt;',
  10181. 'type',
  10182. ' TArrayInt = array[1..3] of longint;',
  10183. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10184. 'var',
  10185. ' Arr: TArrayInt;',
  10186. ' Arr2: TArrayArrayInt;',
  10187. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10188. ' i: longint;',
  10189. 'begin',
  10190. ' arr2[5]:=arr;',
  10191. ' arr2:=arr2;',// clone multi dim static array
  10192. ' arr3:=arr3;',// clone multi dim anonymous static array
  10193. 'end;',
  10194. 'begin',
  10195. '']);
  10196. ConvertProgram;
  10197. CheckSource('TestArray_StaticInFunction',
  10198. LinesToStr([ // statements
  10199. 'this.TArrayInt = 3;',
  10200. 'this.TArrayArrayInt = 4;',
  10201. 'var TArrayArrayInt$1$clone = function (a) {',
  10202. ' var b = [];',
  10203. ' b.length = 2;',
  10204. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10205. ' return b;',
  10206. '};',
  10207. 'var Arr3$a$clone = function (a) {',
  10208. ' var b = [];',
  10209. ' b.length = 2;',
  10210. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10211. ' return b;',
  10212. '};',
  10213. 'this.DoIt = function () {',
  10214. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10215. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10216. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10217. ' var i = 0;',
  10218. ' Arr2[0] = Arr.slice(0);',
  10219. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10220. ' Arr3 = Arr3$a$clone(Arr3);',
  10221. '};',
  10222. '']),
  10223. LinesToStr([ // $mod.$main
  10224. '']));
  10225. end;
  10226. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10227. begin
  10228. StartProgram(false);
  10229. Add([
  10230. 'type',
  10231. ' TArrayInt = array[1..3,1..2] of longint;',
  10232. 'var',
  10233. ' a,b: TArrayInt;',
  10234. 'begin',
  10235. ' if a=b then ;',
  10236. '']);
  10237. SetExpectedPasResolverError('compare static array is not supported',
  10238. nXIsNotSupported);
  10239. ConvertProgram;
  10240. end;
  10241. procedure TTestModule.TestArrayOfRecord;
  10242. begin
  10243. StartProgram(false);
  10244. Add([
  10245. 'type',
  10246. ' TRec = record',
  10247. ' Int: longint;',
  10248. ' end;',
  10249. ' TArrayRec = array of TRec;',
  10250. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10251. 'begin',
  10252. 'end;',
  10253. 'var',
  10254. ' Arr: TArrayRec;',
  10255. ' r: TRec;',
  10256. ' i: longint;',
  10257. 'begin',
  10258. ' SetLength(arr,3);',
  10259. ' arr[0].int:=4;',
  10260. ' arr[1].int:=length(arr)+arr[2].int;',
  10261. ' arr[arr[i].int].int:=arr[5].int;',
  10262. ' arr[7]:=r;',
  10263. ' r:=arr[8];',
  10264. ' i:=low(arr);',
  10265. ' i:=high(arr);',
  10266. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10267. ConvertProgram;
  10268. CheckSource('TestArrayOfRecord',
  10269. LinesToStr([ // statements
  10270. 'rtl.recNewT(this, "TRec", function () {',
  10271. ' this.Int = 0;',
  10272. ' this.$eq = function (b) {',
  10273. ' return this.Int === b.Int;',
  10274. ' };',
  10275. ' this.$assign = function (s) {',
  10276. ' this.Int = s.Int;',
  10277. ' return this;',
  10278. ' };',
  10279. '});',
  10280. 'this.DoIt = function (vd, vc, vv) {',
  10281. '};',
  10282. 'this.Arr = [];',
  10283. 'this.r = this.TRec.$new();',
  10284. 'this.i = 0;'
  10285. ]),
  10286. LinesToStr([ // $mod.$main
  10287. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10288. '$mod.Arr[0].Int = 4;',
  10289. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10290. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10291. '$mod.Arr[7].$assign($mod.r);',
  10292. '$mod.r.$assign($mod.Arr[8]);',
  10293. '$mod.i = 0;',
  10294. '$mod.i = rtl.length($mod.Arr)-1;',
  10295. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10296. '']));
  10297. end;
  10298. procedure TTestModule.TestArray_StaticRecord;
  10299. begin
  10300. StartProgram(false);
  10301. Add([
  10302. 'type',
  10303. ' TRec = record',
  10304. ' Int: longint;',
  10305. ' end;',
  10306. ' TArrayRec = array[1..2] of TRec;',
  10307. 'var',
  10308. ' Arr: TArrayRec;',
  10309. 'begin',
  10310. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10311. '']);
  10312. ConvertProgram;
  10313. CheckSource('TestArray_StaticRecord',
  10314. LinesToStr([ // statements
  10315. 'rtl.recNewT(this, "TRec", function () {',
  10316. ' this.Int = 0;',
  10317. ' this.$eq = function (b) {',
  10318. ' return this.Int === b.Int;',
  10319. ' };',
  10320. ' this.$assign = function (s) {',
  10321. ' this.Int = s.Int;',
  10322. ' return this;',
  10323. ' };',
  10324. '});',
  10325. 'this.TArrayRec$clone = function (a) {',
  10326. ' var b = [];',
  10327. ' b.length = 2;',
  10328. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10329. ' return b;',
  10330. '};',
  10331. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10332. '']),
  10333. LinesToStr([ // $mod.$main
  10334. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10335. end;
  10336. procedure TTestModule.TestArrayOfSet;
  10337. begin
  10338. StartProgram(false);
  10339. Add([
  10340. 'type',
  10341. ' TFlag = (big,small);',
  10342. ' TSetOfFlag = set of tflag;',
  10343. ' TArrayFlag = array of TSetOfFlag;',
  10344. 'procedure DoIt(const a: Tarrayflag);',
  10345. 'begin',
  10346. 'end;',
  10347. 'var',
  10348. ' f: TFlag;',
  10349. ' s: TSetOfFlag;',
  10350. ' Arr: TArrayFlag;',
  10351. ' i: longint;',
  10352. 'begin',
  10353. ' SetLength(arr,3);',
  10354. ' arr[0]:=s;',
  10355. ' arr[1]:=[big];',
  10356. ' arr[2]:=[big]+s;',
  10357. ' arr[3]:=s+[big];',
  10358. ' arr[4]:=arr[5];',
  10359. ' s:=arr[6];',
  10360. ' i:=low(arr);',
  10361. ' i:=high(arr);',
  10362. ' DoIt(arr);',
  10363. ' DoIt([s]);',
  10364. ' DoIt([[],s]);',
  10365. ' DoIt([s,[]]);',
  10366. '']);
  10367. ConvertProgram;
  10368. CheckSource('TestArrayOfSet',
  10369. LinesToStr([ // statements
  10370. 'this.TFlag = {',
  10371. ' "0": "big",',
  10372. ' big: 0,',
  10373. ' "1": "small",',
  10374. ' small: 1',
  10375. '};',
  10376. 'this.DoIt = function (a) {',
  10377. '};',
  10378. 'this.f = 0;',
  10379. 'this.s = {};',
  10380. 'this.Arr = [];',
  10381. 'this.i = 0;',
  10382. '']),
  10383. LinesToStr([ // $mod.$main
  10384. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10385. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10386. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10387. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10388. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10389. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10390. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10391. '$mod.i = 0;',
  10392. '$mod.i = rtl.length($mod.Arr) - 1;',
  10393. '$mod.DoIt($mod.Arr);',
  10394. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10395. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10396. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10397. '']));
  10398. end;
  10399. procedure TTestModule.TestArray_DynAsParam;
  10400. begin
  10401. StartProgram(false);
  10402. Add([
  10403. 'type integer = longint;',
  10404. 'type TArrInt = array of integer;',
  10405. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10406. 'var vJ: TArrInt;',
  10407. 'begin',
  10408. ' vg:=vg;',
  10409. ' vj:=vh;',
  10410. ' vi:=vi;',
  10411. ' doit(vg,vg,vg);',
  10412. ' doit(vh,vh,vj);',
  10413. ' doit(vi,vi,vi);',
  10414. ' doit(vj,vj,vj);',
  10415. 'end;',
  10416. 'var i: TArrInt;',
  10417. 'begin',
  10418. ' doit(i,i,i);']);
  10419. ConvertProgram;
  10420. CheckSource('TestArray_DynAsParams',
  10421. LinesToStr([ // statements
  10422. 'this.DoIt = function (vG,vH,vI) {',
  10423. ' var vJ = [];',
  10424. ' vG = rtl.arrayRef(vG);',
  10425. ' vJ = rtl.arrayRef(vH);',
  10426. ' vI.set(rtl.arrayRef(vI.get()));',
  10427. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10428. ' get: function () {',
  10429. ' return vG;',
  10430. ' },',
  10431. ' set: function (v) {',
  10432. ' vG = v;',
  10433. ' }',
  10434. ' });',
  10435. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10436. ' get: function () {',
  10437. ' return vJ;',
  10438. ' },',
  10439. ' set: function (v) {',
  10440. ' vJ = v;',
  10441. ' }',
  10442. ' });',
  10443. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10444. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10445. ' get: function () {',
  10446. ' return vJ;',
  10447. ' },',
  10448. ' set: function (v) {',
  10449. ' vJ = v;',
  10450. ' }',
  10451. ' });',
  10452. '};',
  10453. 'this.i = [];'
  10454. ]),
  10455. LinesToStr([
  10456. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10457. ' p: $mod,',
  10458. ' get: function () {',
  10459. ' return this.p.i;',
  10460. ' },',
  10461. ' set: function (v) {',
  10462. ' this.p.i = v;',
  10463. ' }',
  10464. '});'
  10465. ]));
  10466. end;
  10467. procedure TTestModule.TestArray_StaticAsParam;
  10468. begin
  10469. StartProgram(false);
  10470. Add([
  10471. 'type integer = longint;',
  10472. 'type TArrInt = array[1..2] of integer;',
  10473. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10474. 'var vJ: TArrInt;',
  10475. 'begin',
  10476. ' vg:=vg;',
  10477. ' vj:=vh;',
  10478. ' vi:=vi;',
  10479. ' doit(vg,vg,vg);',
  10480. ' doit(vh,vh,vj);',
  10481. ' doit(vi,vi,vi);',
  10482. ' doit(vj,vj,vj);',
  10483. 'end;',
  10484. 'var i: TArrInt;',
  10485. 'begin',
  10486. ' doit(i,i,i);']);
  10487. ConvertProgram;
  10488. CheckSource('TestArray_StaticAsParams',
  10489. LinesToStr([ // statements
  10490. 'this.DoIt = function (vG,vH,vI) {',
  10491. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10492. ' vG = vG.slice(0);',
  10493. ' vJ = vH.slice(0);',
  10494. ' vI.set(vI.get().slice(0));',
  10495. ' $mod.DoIt(vG.slice(0), vG, {',
  10496. ' get: function () {',
  10497. ' return vG;',
  10498. ' },',
  10499. ' set: function (v) {',
  10500. ' vG = v;',
  10501. ' }',
  10502. ' });',
  10503. ' $mod.DoIt(vH.slice(0), vH, {',
  10504. ' get: function () {',
  10505. ' return vJ;',
  10506. ' },',
  10507. ' set: function (v) {',
  10508. ' vJ = v;',
  10509. ' }',
  10510. ' });',
  10511. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10512. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10513. ' get: function () {',
  10514. ' return vJ;',
  10515. ' },',
  10516. ' set: function (v) {',
  10517. ' vJ = v;',
  10518. ' }',
  10519. ' });',
  10520. '};',
  10521. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10522. ]),
  10523. LinesToStr([
  10524. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10525. ' p: $mod,',
  10526. ' get: function () {',
  10527. ' return this.p.i;',
  10528. ' },',
  10529. ' set: function (v) {',
  10530. ' this.p.i = v;',
  10531. ' }',
  10532. '});'
  10533. ]));
  10534. end;
  10535. procedure TTestModule.TestArrayElement_AsParams;
  10536. begin
  10537. StartProgram(false);
  10538. Add('type integer = longint;');
  10539. Add('type TArrayInt = array of integer;');
  10540. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10541. Add('var vJ: tarrayint;');
  10542. Add('begin');
  10543. Add(' vi:=vi;');
  10544. Add(' doit(vi,vi,vi);');
  10545. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10546. Add('end;');
  10547. Add('var a: TArrayInt;');
  10548. Add('begin');
  10549. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10550. ConvertProgram;
  10551. CheckSource('TestArrayElement_AsParams',
  10552. LinesToStr([ // statements
  10553. 'this.DoIt = function (vG,vH,vI) {',
  10554. ' var vJ = [];',
  10555. ' vI.set(vI.get());',
  10556. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10557. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10558. ' a:1+3,',
  10559. ' p:vJ,',
  10560. ' get: function () {',
  10561. ' return this.p[this.a];',
  10562. ' },',
  10563. ' set: function (v) {',
  10564. ' this.p[this.a] = v;',
  10565. ' }',
  10566. ' });',
  10567. '};',
  10568. 'this.a = [];'
  10569. ]),
  10570. LinesToStr([
  10571. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10572. ' a: 1+6,',
  10573. ' p: $mod.a,',
  10574. ' get: function () {',
  10575. ' return this.p[this.a];',
  10576. ' },',
  10577. ' set: function (v) {',
  10578. ' this.p[this.a] = v;',
  10579. ' }',
  10580. '});'
  10581. ]));
  10582. end;
  10583. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10584. begin
  10585. StartProgram(false);
  10586. Add('type Integer = longint;');
  10587. Add('type TArrayInt = array of integer;');
  10588. Add('function GetArr(vB: integer = 0): tarrayint;');
  10589. Add('begin');
  10590. Add('end;');
  10591. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10592. Add('begin');
  10593. Add('end;');
  10594. Add('begin');
  10595. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10596. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10597. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10598. ConvertProgram;
  10599. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10600. LinesToStr([ // statements
  10601. 'this.GetArr = function (vB) {',
  10602. ' var Result = [];',
  10603. ' return Result;',
  10604. '};',
  10605. 'this.DoIt = function (vG,vH,vI) {',
  10606. '};'
  10607. ]),
  10608. LinesToStr([
  10609. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10610. ' a: 1+3,',
  10611. ' p: $mod.GetArr(0),',
  10612. ' get: function () {',
  10613. ' return this.p[this.a];',
  10614. ' },',
  10615. ' set: function (v) {',
  10616. ' this.p[this.a] = v;',
  10617. ' }',
  10618. '});',
  10619. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10620. ' a: 2+3,',
  10621. ' p: $mod.GetArr(0),',
  10622. ' get: function () {',
  10623. ' return this.p[this.a];',
  10624. ' },',
  10625. ' set: function (v) {',
  10626. ' this.p[this.a] = v;',
  10627. ' }',
  10628. '});',
  10629. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10630. ' a: 3+3,',
  10631. ' p: $mod.GetArr(9),',
  10632. ' get: function () {',
  10633. ' return this.p[this.a];',
  10634. ' },',
  10635. ' set: function (v) {',
  10636. ' this.p[this.a] = v;',
  10637. ' }',
  10638. '});',
  10639. '']));
  10640. end;
  10641. procedure TTestModule.TestArrayEnumTypeRange;
  10642. begin
  10643. StartProgram(false);
  10644. Add([
  10645. 'type',
  10646. ' TEnum = (red,blue);',
  10647. ' TEnumArray = array[TEnum] of longint;',
  10648. 'var',
  10649. ' e: TEnum;',
  10650. ' i: longint;',
  10651. ' a: TEnumArray;',
  10652. ' numbers: TEnumArray = (1,2);',
  10653. ' names: array[TEnum] of string = (''red'',''blue'');',
  10654. 'begin',
  10655. ' e:=low(a);',
  10656. ' e:=high(a);',
  10657. ' i:=a[red];',
  10658. ' a[e]:=a[e];']);
  10659. ConvertProgram;
  10660. CheckSource('TestArrayEnumTypeRange',
  10661. LinesToStr([ // statements
  10662. ' this.TEnum = {',
  10663. ' "0": "red",',
  10664. ' red: 0,',
  10665. ' "1": "blue",',
  10666. ' blue: 1',
  10667. '};',
  10668. 'this.e = 0;',
  10669. 'this.i = 0;',
  10670. 'this.a = rtl.arraySetLength(null,0,2);',
  10671. 'this.numbers = [1, 2];',
  10672. 'this.names = ["red", "blue"];',
  10673. '']),
  10674. LinesToStr([ // $mod.$main
  10675. '$mod.e = $mod.TEnum.red;',
  10676. '$mod.e = $mod.TEnum.blue;',
  10677. '$mod.i = $mod.a[$mod.TEnum.red];',
  10678. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10679. '']));
  10680. end;
  10681. procedure TTestModule.TestArray_SetLengthOutArg;
  10682. begin
  10683. StartProgram(false);
  10684. Add([
  10685. 'type TArrInt = array of longint;',
  10686. 'procedure DoIt(out a: TArrInt);',
  10687. 'begin',
  10688. ' SetLength(a,2);',
  10689. 'end;',
  10690. 'begin',
  10691. '']);
  10692. ConvertProgram;
  10693. CheckSource('TestArray_SetLengthOutArg',
  10694. LinesToStr([ // statements
  10695. 'this.DoIt = function (a) {',
  10696. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10697. '};',
  10698. '']),
  10699. LinesToStr([
  10700. '']));
  10701. end;
  10702. procedure TTestModule.TestArray_SetLengthProperty;
  10703. begin
  10704. StartProgram(false);
  10705. Add('type');
  10706. Add(' TArrInt = array of longint;');
  10707. Add(' TObject = class');
  10708. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10709. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10710. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10711. Add(' end;');
  10712. Add('var Obj: TObject;');
  10713. Add('begin');
  10714. Add(' SetLength(Obj.Colors,2);');
  10715. ConvertProgram;
  10716. CheckSource('TestArray_SetLengthProperty',
  10717. LinesToStr([ // statements
  10718. 'rtl.createClass(this, "TObject", null, function () {',
  10719. ' this.$init = function () {',
  10720. ' };',
  10721. ' this.$final = function () {',
  10722. ' };',
  10723. '});',
  10724. 'this.Obj = null;',
  10725. '']),
  10726. LinesToStr([
  10727. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10728. '']));
  10729. end;
  10730. procedure TTestModule.TestArray_SetLengthMultiDim;
  10731. begin
  10732. StartProgram(false);
  10733. Add([
  10734. 'type',
  10735. ' TArrArrInt = array of array of longint;',
  10736. ' TArrStaInt = array of array[1..2] of longint;',
  10737. 'var',
  10738. ' a: TArrArrInt;',
  10739. ' b: TArrStaInt;',
  10740. 'begin',
  10741. ' SetLength(a,2);',
  10742. ' SetLength(a,3,4);',
  10743. ' SetLength(b,5);',
  10744. '']);
  10745. ConvertProgram;
  10746. CheckSource('TestArray_SetLengthMultiDim',
  10747. LinesToStr([ // statements
  10748. 'this.a = [];',
  10749. 'this.b = [];',
  10750. '']),
  10751. LinesToStr([
  10752. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10753. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10754. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10755. '']));
  10756. end;
  10757. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10758. begin
  10759. StartProgram(false);
  10760. Add([
  10761. 'type',
  10762. ' TStaArr1 = array[1..3] of boolean;',
  10763. //' TStaArr2 = array[5..6] of TStaArr1;',
  10764. ' TDynArr1StaArr1 = array of TStaArr1;',
  10765. //' TDynArr1StaArr2 = array of TStaArr2;',
  10766. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10767. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10768. 'var',
  10769. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10770. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10771. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10772. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10773. 'begin',
  10774. ' SetLength(DynArr1StaArr1,11);',
  10775. ' SetLength(DynArr2StaArr1,12);',
  10776. ' SetLength(DynArr2StaArr1[13],14);',
  10777. ' SetLength(DynArr2StaArr1,15,16);',
  10778. //' SetLength(DynArr1StaArr2,21);',
  10779. //' SetLength(DynArr2StaArr2,22);',
  10780. //' SetLength(DynArr2StaArr2[23],24);',
  10781. //' SetLength(DynArr2StaArr2,25,26);',
  10782. '']);
  10783. ConvertProgram;
  10784. CheckSource('TestArray_DynOfStatic',
  10785. LinesToStr([ // statements
  10786. 'this.DynArr1StaArr1 = [];',
  10787. 'this.DynArr2StaArr1 = [];',
  10788. '']),
  10789. LinesToStr([ // $mod.$main
  10790. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10791. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10792. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10793. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10794. ' $mod.DynArr2StaArr1,',
  10795. ' false,',
  10796. ' 15,',
  10797. ' 16,',
  10798. ' "s",',
  10799. ' 3',
  10800. ');',
  10801. '']));
  10802. end;
  10803. procedure TTestModule.TestArray_OpenArrayOfString;
  10804. begin
  10805. StartProgram(false);
  10806. Add('procedure DoIt(const a: array of String);');
  10807. Add('var');
  10808. Add(' i: longint;');
  10809. Add(' s: string;');
  10810. Add('begin');
  10811. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10812. Add('end;');
  10813. Add('var s: string;');
  10814. Add('begin');
  10815. Add(' DoIt([]);');
  10816. Add(' DoIt([s,''foo'','''',s+s]);');
  10817. ConvertProgram;
  10818. CheckSource('TestArray_OpenArrayOfString',
  10819. LinesToStr([ // statements
  10820. 'this.DoIt = function (a) {',
  10821. ' var i = 0;',
  10822. ' var s = "";',
  10823. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10824. ' i = $l;',
  10825. ' s = a[rtl.length(a) - i - 1];',
  10826. ' };',
  10827. '};',
  10828. 'this.s = "";',
  10829. '']),
  10830. LinesToStr([
  10831. '$mod.DoIt([]);',
  10832. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10833. '']));
  10834. end;
  10835. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10836. begin
  10837. StartProgram(false);
  10838. Add([
  10839. 'type TArr = array of char;',
  10840. 'var',
  10841. ' c: char;',
  10842. ' s: string;',
  10843. ' a: TArr;',
  10844. 'procedure Run(const a: array of char);',
  10845. 'begin',
  10846. ' Run(c);',
  10847. ' Run(s);',
  10848. 'end;',
  10849. 'begin',
  10850. ' a:=c;',
  10851. ' a:=s;',
  10852. ' a:=#13;',
  10853. ' a:=''Foo'';',
  10854. ' Run(c);',
  10855. ' Run(s);',
  10856. '']);
  10857. ConvertProgram;
  10858. CheckSource('TestArray_ArrayOfCharAssignString',
  10859. LinesToStr([ // statements
  10860. 'this.c = "";',
  10861. 'this.s = "";',
  10862. 'this.a = [];',
  10863. 'this.Run = function (a) {',
  10864. ' $mod.Run($mod.c.split(""));',
  10865. ' $mod.Run($mod.s.split(""));',
  10866. '};',
  10867. '']),
  10868. LinesToStr([
  10869. '$mod.a = $mod.c.split("");',
  10870. '$mod.a = $mod.s.split("");',
  10871. '$mod.a = "\r".split("");',
  10872. '$mod.a = "Foo".split("");',
  10873. '$mod.Run($mod.c.split(""));',
  10874. '$mod.Run($mod.s.split(""));',
  10875. '']));
  10876. end;
  10877. procedure TTestModule.TestArray_ConstRef;
  10878. begin
  10879. StartProgram(false);
  10880. Add([
  10881. 'type TArr = array of word;',
  10882. 'procedure Run(constref a: TArr);',
  10883. 'begin',
  10884. 'end;',
  10885. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10886. 'var l: TArr;',
  10887. 'begin',
  10888. ' Run(l);',
  10889. ' Run(a);',
  10890. ' Run(b);',
  10891. ' Run(c);',
  10892. ' Run(d);',
  10893. ' Run(e);',
  10894. 'end;',
  10895. 'begin',
  10896. '']);
  10897. ConvertProgram;
  10898. CheckResolverUnexpectedHints();
  10899. CheckSource('TestArray_ConstRef',
  10900. LinesToStr([ // statements
  10901. 'this.Run = function (a) {',
  10902. '};',
  10903. 'this.Fly = function (a, b, c, d, e) {',
  10904. ' var l = [];',
  10905. ' $mod.Run(l);',
  10906. ' $mod.Run(a);',
  10907. ' $mod.Run(b.get());',
  10908. ' $mod.Run(c.get());',
  10909. ' $mod.Run(d);',
  10910. ' $mod.Run(e);',
  10911. '};',
  10912. '']),
  10913. LinesToStr([
  10914. '']));
  10915. end;
  10916. procedure TTestModule.TestArray_Concat;
  10917. begin
  10918. StartProgram(false);
  10919. Add([
  10920. 'type',
  10921. ' integer = longint;',
  10922. ' TFlag = (big,small);',
  10923. ' TFlags = set of TFlag;',
  10924. ' TRec = record',
  10925. ' i: integer;',
  10926. ' end;',
  10927. ' TArrInt = array of integer;',
  10928. ' TArrRec = array of TRec;',
  10929. ' TArrFlag = array of TFlag;',
  10930. ' TArrSet = array of TFlags;',
  10931. ' TArrJSValue = array of jsvalue;',
  10932. 'var',
  10933. ' ArrInt1, ArrInt2: tarrint;',
  10934. ' ArrRec1, ArrRec2: tarrrec;',
  10935. ' ArrFlag1, ArrFlag2: tarrflag;',
  10936. ' ArrSet1, ArrSet2: tarrset;',
  10937. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10938. 'begin',
  10939. ' arrint1:=concat(arrint2);',
  10940. ' arrint1:=concat(arrint2,arrint2);',
  10941. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10942. ' arrrec1:=concat(arrrec2);',
  10943. ' arrrec1:=concat(arrrec2,arrrec2);',
  10944. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10945. ' arrset1:=concat(arrset2);',
  10946. ' arrset1:=concat(arrset2,arrset2);',
  10947. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10948. ' arrjsvalue1:=concat(arrjsvalue2);',
  10949. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10950. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10951. ' arrint1:=concat([1],arrint2);',
  10952. ' arrflag1:=concat([big]);',
  10953. ' arrflag1:=concat([big],arrflag2);',
  10954. ' arrflag1:=concat(arrflag2,[small]);',
  10955. '']);
  10956. ConvertProgram;
  10957. CheckSource('TestArray_Concat',
  10958. LinesToStr([ // statements
  10959. 'this.TFlag = {',
  10960. ' "0": "big",',
  10961. ' big: 0,',
  10962. ' "1": "small",',
  10963. ' small: 1',
  10964. '};',
  10965. 'rtl.recNewT(this, "TRec", function () {',
  10966. ' this.i = 0;',
  10967. ' this.$eq = function (b) {',
  10968. ' return this.i === b.i;',
  10969. ' };',
  10970. ' this.$assign = function (s) {',
  10971. ' this.i = s.i;',
  10972. ' return this;',
  10973. ' };',
  10974. '});',
  10975. 'this.ArrInt1 = [];',
  10976. 'this.ArrInt2 = [];',
  10977. 'this.ArrRec1 = [];',
  10978. 'this.ArrRec2 = [];',
  10979. 'this.ArrFlag1 = [];',
  10980. 'this.ArrFlag2 = [];',
  10981. 'this.ArrSet1 = [];',
  10982. 'this.ArrSet2 = [];',
  10983. 'this.ArrJSValue1 = [];',
  10984. 'this.ArrJSValue2 = [];',
  10985. '']),
  10986. LinesToStr([ // $mod.$main
  10987. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  10988. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  10989. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  10990. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  10991. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  10992. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  10993. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  10994. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  10995. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  10996. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  10997. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  10998. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  10999. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11000. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11001. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11002. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11003. '']));
  11004. end;
  11005. procedure TTestModule.TestArray_Concat_Append;
  11006. begin
  11007. StartProgram(false);
  11008. Add([
  11009. 'type',
  11010. ' integer = longint;',
  11011. ' TFlag = (big,small);',
  11012. ' TFlags = set of TFlag;',
  11013. ' TRec = record',
  11014. ' i: integer;',
  11015. ' end;',
  11016. ' TArrInt = array of integer;',
  11017. ' TArrRec = array of TRec;',
  11018. ' TArrFlag = array of TFlag;',
  11019. ' TArrSet = array of TFlags;',
  11020. ' TArrJSValue = array of jsvalue;',
  11021. 'var',
  11022. ' ArrInt: tarrint;',
  11023. ' ArrRec: tarrrec;',
  11024. ' ArrFlag: tarrflag;',
  11025. ' ArrSet: tarrset;',
  11026. ' ArrJSValue: tarrjsvalue;',
  11027. ' r: TRec;',
  11028. ' f: TFlags;',
  11029. 'begin',
  11030. ' // append',
  11031. ' arrint:=concat(arrint);',
  11032. ' arrint:=concat(arrint,[2]);',
  11033. ' arrint:=concat(arrint,[3,4]);',
  11034. ' arrrec:=concat(arrrec);',
  11035. ' arrrec:=concat(arrrec,[r]);',
  11036. ' arrrec:=concat(arrrec,[r,r]);',
  11037. ' arrset:=concat(arrset);',
  11038. ' arrset:=concat(arrset,[f]);',
  11039. ' arrset:=concat(arrset,[f,f]);',
  11040. ' arrjsvalue:=concat(arrjsvalue);',
  11041. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11042. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11043. ' arrflag:=concat(arrflag);',
  11044. ' arrflag:=concat(arrflag,[small]);',
  11045. ' arrflag:=concat(arrflag,[small,big]);',
  11046. '']);
  11047. ConvertProgram;
  11048. CheckSource('TestArray_Concat_Append',
  11049. LinesToStr([ // statements
  11050. 'this.TFlag = {',
  11051. ' "0": "big",',
  11052. ' big: 0,',
  11053. ' "1": "small",',
  11054. ' small: 1',
  11055. '};',
  11056. 'rtl.recNewT(this, "TRec", function () {',
  11057. ' this.i = 0;',
  11058. ' this.$eq = function (b) {',
  11059. ' return this.i === b.i;',
  11060. ' };',
  11061. ' this.$assign = function (s) {',
  11062. ' this.i = s.i;',
  11063. ' return this;',
  11064. ' };',
  11065. '});',
  11066. 'this.ArrInt = [];',
  11067. 'this.ArrRec = [];',
  11068. 'this.ArrFlag = [];',
  11069. 'this.ArrSet = [];',
  11070. 'this.ArrJSValue = [];',
  11071. 'this.r = this.TRec.$new();',
  11072. 'this.f = {};',
  11073. '']),
  11074. LinesToStr([ // $mod.$main
  11075. '$mod.ArrInt = $mod.ArrInt;',
  11076. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11077. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11078. '$mod.ArrRec = $mod.ArrRec;',
  11079. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11080. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11081. '$mod.ArrSet = $mod.ArrSet;',
  11082. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11083. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11084. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11085. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11086. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11087. '$mod.ArrFlag = $mod.ArrFlag;',
  11088. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11089. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11090. '']));
  11091. end;
  11092. procedure TTestModule.TestArray_Concat_Append_Var;
  11093. begin
  11094. StartProgram(false);
  11095. Add([
  11096. 'type',
  11097. ' TArrInt = array of word;',
  11098. '',
  11099. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11100. 'begin',
  11101. ' a:=concat(a,[2]);',
  11102. ' b:=concat(b,[2]);',
  11103. 'end;',
  11104. 'var',
  11105. ' ArrInt: tarrint;',
  11106. 'begin',
  11107. ' Fly(ArrInt,ArrInt);',
  11108. '']);
  11109. ConvertProgram;
  11110. CheckSource('TestArray_Concat_Append_Var',
  11111. LinesToStr([ // statements
  11112. 'this.Fly = function (a, b) {',
  11113. ' a = rtl.arrayPushN(a, 2);',
  11114. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11115. '};',
  11116. 'this.ArrInt = [];',
  11117. '']),
  11118. LinesToStr([ // $mod.$main
  11119. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11120. ' p: $mod,',
  11121. ' get: function () {',
  11122. ' return this.p.ArrInt;',
  11123. ' },',
  11124. ' set: function (v) {',
  11125. ' this.p.ArrInt = v;',
  11126. ' }',
  11127. '});',
  11128. '']));
  11129. end;
  11130. procedure TTestModule.TestArray_Copy;
  11131. begin
  11132. StartProgram(false);
  11133. Add([
  11134. 'type',
  11135. ' integer = longint;',
  11136. ' TFlag = (big,small);',
  11137. ' TFlags = set of TFlag;',
  11138. ' TRec = record',
  11139. ' i: integer;',
  11140. ' end;',
  11141. ' TArrInt = array of integer;',
  11142. ' TArrRec = array of TRec;',
  11143. ' TArrSet = array of TFlags;',
  11144. ' TArrJSValue = array of jsvalue;',
  11145. 'var',
  11146. ' ArrInt: tarrint;',
  11147. ' ArrRec: tarrrec;',
  11148. ' ArrSet: tarrset;',
  11149. ' ArrJSValue: tarrjsvalue;',
  11150. 'begin',
  11151. ' arrint:=copy(arrint);',
  11152. ' arrint:=copy(arrint,2);',
  11153. ' arrint:=copy(arrint,3,4);',
  11154. ' arrint:=copy([1,1],1,2);',
  11155. ' arrrec:=copy(arrrec);',
  11156. ' arrrec:=copy(arrrec,5);',
  11157. ' arrrec:=copy(arrrec,6,7);',
  11158. ' arrset:=copy(arrset);',
  11159. ' arrset:=copy(arrset,8);',
  11160. ' arrset:=copy(arrset,9,10);',
  11161. ' arrjsvalue:=copy(arrjsvalue);',
  11162. ' arrjsvalue:=copy(arrjsvalue,11);',
  11163. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11164. ' ']);
  11165. ConvertProgram;
  11166. CheckSource('TestArray_Copy',
  11167. LinesToStr([ // statements
  11168. 'this.TFlag = {',
  11169. ' "0": "big",',
  11170. ' big: 0,',
  11171. ' "1": "small",',
  11172. ' small: 1',
  11173. '};',
  11174. 'rtl.recNewT(this, "TRec", function () {',
  11175. ' this.i = 0;',
  11176. ' this.$eq = function (b) {',
  11177. ' return this.i === b.i;',
  11178. ' };',
  11179. ' this.$assign = function (s) {',
  11180. ' this.i = s.i;',
  11181. ' return this;',
  11182. ' };',
  11183. '});',
  11184. 'this.ArrInt = [];',
  11185. 'this.ArrRec = [];',
  11186. 'this.ArrSet = [];',
  11187. 'this.ArrJSValue = [];',
  11188. '']),
  11189. LinesToStr([ // $mod.$main
  11190. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11191. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11192. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11193. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11194. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11195. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11196. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11197. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11198. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11199. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11200. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11201. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11202. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11203. '']));
  11204. end;
  11205. procedure TTestModule.TestArray_InsertDelete;
  11206. begin
  11207. StartProgram(false);
  11208. Add([
  11209. 'type',
  11210. ' integer = longint;',
  11211. ' TFlag = (big,small);',
  11212. ' TFlags = set of TFlag;',
  11213. ' TRec = record',
  11214. ' i: integer;',
  11215. ' end;',
  11216. ' TArrInt = array of integer;',
  11217. ' TArrRec = array of TRec;',
  11218. ' TArrSet = array of TFlags;',
  11219. ' TArrJSValue = array of jsvalue;',
  11220. ' TArrArrInt = array of TArrInt;',
  11221. 'var',
  11222. ' ArrInt: tarrint;',
  11223. ' ArrRec: tarrrec;',
  11224. ' ArrSet: tarrset;',
  11225. ' ArrJSValue: tarrjsvalue;',
  11226. ' ArrArrInt: TArrArrInt;',
  11227. 'begin',
  11228. ' Insert(1,arrint,2);',
  11229. ' Insert(arrint[3],arrint,4);',
  11230. ' Insert(arrrec[5],arrrec,6);',
  11231. ' Insert(arrset[7],arrset,7);',
  11232. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11233. ' Insert(10,arrjsvalue,11);',
  11234. ' Insert([23],arrarrint,22);',
  11235. ' Delete(arrint,12,13);',
  11236. ' Delete(arrrec,14,15);',
  11237. ' Delete(arrset,17,18);',
  11238. ' Delete(arrjsvalue,19,10);']);
  11239. ConvertProgram;
  11240. CheckSource('TestArray_InsertDelete',
  11241. LinesToStr([ // statements
  11242. 'this.TFlag = {',
  11243. ' "0": "big",',
  11244. ' big: 0,',
  11245. ' "1": "small",',
  11246. ' small: 1',
  11247. '};',
  11248. 'rtl.recNewT(this, "TRec", function () {',
  11249. ' this.i = 0;',
  11250. ' this.$eq = function (b) {',
  11251. ' return this.i === b.i;',
  11252. ' };',
  11253. ' this.$assign = function (s) {',
  11254. ' this.i = s.i;',
  11255. ' return this;',
  11256. ' };',
  11257. '});',
  11258. 'this.ArrInt = [];',
  11259. 'this.ArrRec = [];',
  11260. 'this.ArrSet = [];',
  11261. 'this.ArrJSValue = [];',
  11262. 'this.ArrArrInt = [];',
  11263. '']),
  11264. LinesToStr([ // $mod.$main
  11265. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11266. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11267. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11268. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11269. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11270. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11271. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11272. '$mod.ArrInt.splice(12, 13);',
  11273. '$mod.ArrRec.splice(14, 15);',
  11274. '$mod.ArrSet.splice(17, 18);',
  11275. '$mod.ArrJSValue.splice(19, 10);',
  11276. '']));
  11277. end;
  11278. procedure TTestModule.TestArray_Add_Append;
  11279. begin
  11280. StartProgram(false);
  11281. Add([
  11282. '{$modeswitch arrayoperators}',
  11283. 'type',
  11284. ' integer = longint;',
  11285. ' TFlag = (big,small);',
  11286. ' TFlags = set of TFlag;',
  11287. ' TRec = record',
  11288. ' i: integer;',
  11289. ' end;',
  11290. ' TArrInt = array of integer;',
  11291. ' TArrRec = array of TRec;',
  11292. ' TArrFlag = array of TFlag;',
  11293. ' TArrSet = array of TFlags;',
  11294. ' TArrJSValue = array of jsvalue;',
  11295. 'var',
  11296. ' ArrInt: tarrint;',
  11297. ' ArrRec: tarrrec;',
  11298. ' ArrFlag: tarrflag;',
  11299. ' ArrSet: tarrset;',
  11300. ' ArrJSValue: tarrjsvalue;',
  11301. ' r: TRec;',
  11302. ' f: TFlags;',
  11303. 'begin',
  11304. ' // append',
  11305. ' arrint:=arrint+[2];',
  11306. ' arrint:=arrint+[3,4];',
  11307. ' arrrec:=arrrec+[r];',
  11308. ' arrrec:=arrrec+[r,r];',
  11309. ' arrset:=arrset+[f];',
  11310. ' arrset:=arrset+[f,f];',
  11311. ' arrjsvalue:=arrjsvalue+[11];',
  11312. ' arrjsvalue:=arrjsvalue+[12,13];',
  11313. ' arrflag:=arrflag+[small];',
  11314. ' arrflag:=arrflag+[small,big];',
  11315. '']);
  11316. ConvertProgram;
  11317. CheckSource('TestArray_Add_Append',
  11318. LinesToStr([ // statements
  11319. 'this.TFlag = {',
  11320. ' "0": "big",',
  11321. ' big: 0,',
  11322. ' "1": "small",',
  11323. ' small: 1',
  11324. '};',
  11325. 'rtl.recNewT(this, "TRec", function () {',
  11326. ' this.i = 0;',
  11327. ' this.$eq = function (b) {',
  11328. ' return this.i === b.i;',
  11329. ' };',
  11330. ' this.$assign = function (s) {',
  11331. ' this.i = s.i;',
  11332. ' return this;',
  11333. ' };',
  11334. '});',
  11335. 'this.ArrInt = [];',
  11336. 'this.ArrRec = [];',
  11337. 'this.ArrFlag = [];',
  11338. 'this.ArrSet = [];',
  11339. 'this.ArrJSValue = [];',
  11340. 'this.r = this.TRec.$new();',
  11341. 'this.f = {};',
  11342. '']),
  11343. LinesToStr([ // $mod.$main
  11344. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11345. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11346. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11347. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11348. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11349. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11350. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11351. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11352. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11353. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11354. '']));
  11355. end;
  11356. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11357. begin
  11358. Parser.Options:=Parser.Options+[po_cassignments];
  11359. StartProgram(false);
  11360. Add([
  11361. '{$modeswitch arrayoperators}',
  11362. 'type',
  11363. ' integer = longint;',
  11364. ' TArrInt = array of integer;',
  11365. ' TArrStr = array of string;',
  11366. 'const',
  11367. ' Ints: TArrInt = (1,2,3);',
  11368. ' Aliases: TarrStr = (''foo'',''b'');',
  11369. ' OneInt: TArrInt = (7);',
  11370. ' OneStr: array of integer = (7);',
  11371. ' Chars: array of char = ''aoc'';',
  11372. ' Names: array of string = (''a'',''foo'');',
  11373. ' NameCount = low(Names)+high(Names)+length(Names);',
  11374. 'var i: integer;',
  11375. 'begin',
  11376. ' Ints:=[];',
  11377. ' Ints:=[1,1];',
  11378. ' Ints:=[1]+[2];',
  11379. ' Ints:=[2];',
  11380. ' Ints:=[]+ints;',
  11381. ' Ints:=Ints+[];',
  11382. ' Ints:=Ints+OneInt;',
  11383. ' Ints:=Ints+[1,1];',
  11384. ' Ints:=[i,i]+Ints;',
  11385. ' Ints:=[1]+[i]+[3];',
  11386. '']);
  11387. ConvertProgram;
  11388. CheckSource('TestArray_DynArrayConstObjFPC',
  11389. LinesToStr([ // statements
  11390. 'this.Ints = [1, 2, 3];',
  11391. 'this.Aliases = ["foo", "b"];',
  11392. 'this.OneInt = [7];',
  11393. 'this.OneStr = [7];',
  11394. 'this.Chars = ["a", "o", "c"];',
  11395. 'this.Names = ["a", "foo"];',
  11396. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11397. 'this.i = 0;',
  11398. '']),
  11399. LinesToStr([ // $mod.$main
  11400. '$mod.Ints = [];',
  11401. '$mod.Ints = [1, 1];',
  11402. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11403. '$mod.Ints = [2];',
  11404. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11405. '$mod.Ints = $mod.Ints;',
  11406. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11407. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11408. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11409. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11410. '']));
  11411. end;
  11412. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11413. begin
  11414. StartProgram(false);
  11415. // Note: const c = [1,1]; defines a set!
  11416. Add([
  11417. '{$mode delphi}',
  11418. 'type',
  11419. ' integer = longint;',
  11420. ' TArrInt = array of integer;',
  11421. ' TArrStr = array of string;',
  11422. 'const',
  11423. ' Ints: TArrInt = [1,1,2];',
  11424. ' Aliases: TarrStr = [''foo'',''b''];',
  11425. ' OneInt: TArrInt = [7];',
  11426. ' OneStr: array of integer = [7]+[8];',
  11427. ' Chars: array of char = ''aoc'';',
  11428. ' Names: array of string = [''a'',''a''];',
  11429. ' NameCount = low(Names)+high(Names)+length(Names);',
  11430. 'begin',
  11431. '']);
  11432. ConvertProgram;
  11433. CheckSource('TestArray_DynArrayConstDelphi',
  11434. LinesToStr([ // statements
  11435. 'this.Ints = [1, 1, 2];',
  11436. 'this.Aliases = ["foo", "b"];',
  11437. 'this.OneInt = [7];',
  11438. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11439. 'this.Chars = ["a", "o", "c"];',
  11440. 'this.Names = ["a", "a"];',
  11441. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11442. '']),
  11443. LinesToStr([ // $mod.$main
  11444. '']));
  11445. end;
  11446. procedure TTestModule.TestArray_ArrayLitAsParam;
  11447. begin
  11448. StartProgram(false);
  11449. Add([
  11450. '{$modeswitch arrayoperators}',
  11451. 'type',
  11452. ' integer = longint;',
  11453. ' TArrInt = array of integer;',
  11454. ' TArrSet = array of (red,green,blue);',
  11455. 'procedure DoOpenInt(const a: array of integer); forward;',
  11456. 'procedure DoInt(const a: TArrInt);',
  11457. 'begin',
  11458. ' DoInt(a+[1]);',
  11459. ' DoInt([1]+a);',
  11460. ' DoOpenInt(a);',
  11461. ' DoOpenInt(a+[1]);',
  11462. ' DoOpenInt([1]+a);',
  11463. 'end;',
  11464. 'procedure DoOpenInt(const a: array of integer);',
  11465. 'begin',
  11466. ' DoOpenInt(a+[1]);',
  11467. ' DoOpenInt([1]+a);',
  11468. ' DoInt(a);',
  11469. ' DoInt(a+[1]);',
  11470. ' DoInt([1]+a);',
  11471. 'end;',
  11472. 'procedure DoSet(const a: TArrSet);',
  11473. 'begin',
  11474. ' DoSet(a+[red]);',
  11475. ' DoSet([blue]+a);',
  11476. 'end;',
  11477. 'var',
  11478. ' i: TArrInt;',
  11479. ' s: TArrSet;',
  11480. 'begin',
  11481. ' DoInt([1]);',
  11482. ' DoInt([1]+[2]);',
  11483. ' DoInt(i+[1]);',
  11484. ' DoInt([1]+i);',
  11485. ' DoOpenInt([1]);',
  11486. ' DoOpenInt([1]+[2]);',
  11487. ' DoOpenInt(i+[1]);',
  11488. ' DoOpenInt([1]+i);',
  11489. ' DoSet([red]);',
  11490. ' DoSet([blue]+[green]);',
  11491. ' DoSet(s+[blue]);',
  11492. ' DoSet([red]+s);',
  11493. '']);
  11494. ConvertProgram;
  11495. CheckSource('TestArray_ArrayLitAsParam',
  11496. LinesToStr([ // statements
  11497. 'this.TArrSet$a = {',
  11498. ' "0": "red",',
  11499. ' red: 0,',
  11500. ' "1": "green",',
  11501. ' green: 1,',
  11502. ' "2": "blue",',
  11503. ' blue: 2',
  11504. '};',
  11505. 'this.DoInt = function (a) {',
  11506. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11507. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11508. ' $mod.DoOpenInt(a);',
  11509. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11510. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11511. '};',
  11512. 'this.DoOpenInt = function (a) {',
  11513. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11514. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11515. ' $mod.DoInt(a);',
  11516. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11517. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11518. '};',
  11519. 'this.DoSet = function (a) {',
  11520. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11521. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11522. '};',
  11523. 'this.i = [];',
  11524. 'this.s = [];',
  11525. '']),
  11526. LinesToStr([ // $mod.$main
  11527. '$mod.DoInt([1]);',
  11528. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11529. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11530. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11531. '$mod.DoOpenInt([1]);',
  11532. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11533. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11534. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11535. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11536. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11537. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11538. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11539. '']));
  11540. end;
  11541. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11542. begin
  11543. StartProgram(false);
  11544. Add([
  11545. '{$modeswitch arrayoperators}',
  11546. 'type',
  11547. ' integer = longint;',
  11548. ' TArrInt = array of integer;',
  11549. ' TArrArrInt = array of TArrInt;',
  11550. 'procedure DoInt(const a: TArrArrInt);',
  11551. 'begin',
  11552. ' DoInt(a+[[1]]);',
  11553. ' DoInt([[1]]+a);',
  11554. ' DoInt(a);',
  11555. 'end;',
  11556. 'var',
  11557. ' i: TArrInt;',
  11558. ' a: TArrArrInt;',
  11559. 'begin',
  11560. ' a:=[[1]];',
  11561. ' a:=[i];',
  11562. ' a:=a+[i];',
  11563. ' a:=[i]+a;',
  11564. ' a:=[[1]+i];',
  11565. ' a:=[[1]+[2]];',
  11566. ' a:=[i+[2]];',
  11567. ' DoInt([[1]]);',
  11568. ' DoInt([[1]+[2],[3,4],[5]]);',
  11569. ' DoInt([i+[1]]+a);',
  11570. ' DoInt([i]+a);',
  11571. '']);
  11572. ConvertProgram;
  11573. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11574. LinesToStr([ // statements
  11575. 'this.DoInt = function (a) {',
  11576. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11577. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11578. ' $mod.DoInt(a);',
  11579. '};',
  11580. 'this.i = [];',
  11581. 'this.a = [];',
  11582. '']),
  11583. LinesToStr([ // $mod.$main
  11584. '$mod.a = [[1]];',
  11585. '$mod.a = [$mod.i];',
  11586. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11587. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11588. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11589. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11590. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11591. '$mod.DoInt([[1]]);',
  11592. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11593. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11594. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11595. '']));
  11596. end;
  11597. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11598. begin
  11599. StartProgram(false);
  11600. Add([
  11601. '{$modeswitch arrayoperators}',
  11602. 'type',
  11603. ' integer = longint;',
  11604. ' TArrInt = array[1..2] of integer;',
  11605. ' TArrArrInt = array of TArrInt;',
  11606. 'procedure DoInt(const a: TArrArrInt);',
  11607. 'begin',
  11608. ' DoInt(a+[[1,2]]);',
  11609. ' DoInt([[1,2]]+a);',
  11610. ' DoInt(a);',
  11611. 'end;',
  11612. 'var',
  11613. ' i: TArrInt;',
  11614. ' a: TArrArrInt;',
  11615. 'begin',
  11616. ' a:=[[1,1]];',
  11617. ' a:=[i];',
  11618. ' a:=a+[i];',
  11619. ' a:=[i]+a;',
  11620. ' DoInt([[1,1]]);',
  11621. ' DoInt([[1,2],[3,4]]);',
  11622. '']);
  11623. ConvertProgram;
  11624. CheckSource('TestArray_ArrayLitStaticAsParam',
  11625. LinesToStr([ // statements
  11626. 'this.DoInt = function (a) {',
  11627. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11628. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11629. ' $mod.DoInt(a);',
  11630. '};',
  11631. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11632. 'this.a = [];',
  11633. '']),
  11634. LinesToStr([ // $mod.$main
  11635. '$mod.a = [[1, 1]];',
  11636. '$mod.a = [$mod.i.slice(0)];',
  11637. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11638. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11639. '$mod.DoInt([[1, 1]]);',
  11640. '$mod.DoInt([[1, 2], [3, 4]]);',
  11641. '']));
  11642. end;
  11643. procedure TTestModule.TestArray_ForInArrOfString;
  11644. begin
  11645. StartProgram(false);
  11646. Add([
  11647. 'type',
  11648. 'type',
  11649. ' TMonthNameArray = array [1..12] of string;',
  11650. ' TMonthNames = TMonthNameArray;',
  11651. ' TObject = class',
  11652. ' private',
  11653. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11654. ' public',
  11655. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11656. ' end;',
  11657. 'var',
  11658. ' f: TObject;',
  11659. ' Month: string;',
  11660. ' Names: array of string = (''a'',''foo'',''bar'');',
  11661. ' i: longint;',
  11662. 'begin',
  11663. ' for Month in f.LongMonthNames do ;',
  11664. ' for Month in Names do ;',
  11665. ' for i:=low(Names) to high(Names) do ;',
  11666. '']);
  11667. ConvertProgram;
  11668. CheckSource('TestArray_ForInArrOfString',
  11669. LinesToStr([ // statements
  11670. 'rtl.createClass(this, "TObject", null, function () {',
  11671. ' this.$init = function () {',
  11672. ' };',
  11673. ' this.$final = function () {',
  11674. ' };',
  11675. '});',
  11676. 'this.f = null;',
  11677. 'this.Month = "";',
  11678. 'this.Names = ["a", "foo", "bar"];',
  11679. 'this.i = 0;',
  11680. '']),
  11681. LinesToStr([ // $mod.$main
  11682. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11683. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11684. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11685. '']));
  11686. end;
  11687. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11688. begin
  11689. StartProgram(false);
  11690. Add([
  11691. '{$modeswitch externalclass}',
  11692. 'type',
  11693. ' TJSObject = class external name ''Object''',
  11694. ' end;',
  11695. ' TJSArray = class external name ''Array''',
  11696. ' class function isArray(Value: JSValue) : boolean;',
  11697. ' function concat() : TJSArray; varargs;',
  11698. ' end;',
  11699. 'var',
  11700. ' aObj: TJSArray;',
  11701. ' a: array of longint;',
  11702. ' o: TJSObject;',
  11703. 'begin',
  11704. ' if TJSArray.isArray(65) then ;',
  11705. ' aObj:=TJSArray(a).concat(a);',
  11706. ' o:=TJSObject(a);',
  11707. ' aObj:=TJSArray([''bird'',''ant'']);',
  11708. '']);
  11709. ConvertProgram;
  11710. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11711. LinesToStr([ // statements
  11712. 'this.aObj = null;',
  11713. 'this.a = [];',
  11714. 'this.o = null;',
  11715. '']),
  11716. LinesToStr([ // $mod.$main
  11717. 'if (Array.isArray(65)) ;',
  11718. '$mod.aObj = $mod.a.concat($mod.a);',
  11719. '$mod.o = $mod.a;',
  11720. '$mod.aObj = ["bird", "ant"];',
  11721. '']));
  11722. end;
  11723. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11724. begin
  11725. StartProgram(false);
  11726. Add([
  11727. '{$modeswitch externalclass}',
  11728. 'type',
  11729. ' TArrStr = array of string;',
  11730. ' TJSArray = class external name ''Array''',
  11731. ' end;',
  11732. ' TJSObject = class external name ''Object''',
  11733. ' end;',
  11734. 'var',
  11735. ' aObj: TJSArray;',
  11736. ' a: TArrStr;',
  11737. ' jo: TJSObject;',
  11738. 'begin',
  11739. ' a:=TArrStr(aObj);',
  11740. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11741. ' a:=TarrStr(jo);',
  11742. '']);
  11743. ConvertProgram;
  11744. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11745. LinesToStr([ // statements
  11746. 'this.aObj = null;',
  11747. 'this.a = [];',
  11748. 'this.jo = null;',
  11749. '']),
  11750. LinesToStr([ // $mod.$main
  11751. '$mod.a = $mod.aObj;',
  11752. '$mod.aObj[1] = $mod.aObj[2];',
  11753. '$mod.a = $mod.jo;',
  11754. '']));
  11755. end;
  11756. procedure TTestModule.TestArrayOfConst_TVarRec;
  11757. begin
  11758. StartProgram(true,[supTVarRec]);
  11759. Add([
  11760. 'procedure Say(args: array of const);',
  11761. 'var',
  11762. ' i: longint;',
  11763. ' v: TVarRec;',
  11764. 'begin',
  11765. ' for i:=low(args) to high(args) do begin',
  11766. ' v:=args[i];',
  11767. ' case v.vtype of',
  11768. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11769. ' end;',
  11770. ' end;',
  11771. ' for v in args do ;',
  11772. ' args:=nil;',
  11773. ' SetLength(args,2);',
  11774. 'end;',
  11775. 'begin']);
  11776. ConvertProgram;
  11777. CheckSource('TestArrayOfConst_TVarRec',
  11778. LinesToStr([ // statements
  11779. 'this.Say = function (args) {',
  11780. ' var i = 0;',
  11781. ' var v = pas.system.TVarRec.$new();',
  11782. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11783. ' i = $l;',
  11784. ' v.$assign(args[i]);',
  11785. ' var $tmp = v.VType;',
  11786. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11787. ' };',
  11788. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11789. ' args = [];',
  11790. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11791. '};',
  11792. '']),
  11793. LinesToStr([ // $mod.$main
  11794. ]));
  11795. end;
  11796. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11797. begin
  11798. StartProgram(true,[supTVarRec]);
  11799. Add([
  11800. 'procedure Say(args: array of const);',
  11801. 'begin',
  11802. ' Say(args);',
  11803. 'end;',
  11804. 'var',
  11805. ' p: Pointer;',
  11806. ' j: jsvalue;',
  11807. ' c: currency;',
  11808. 'begin',
  11809. ' Say([]);',
  11810. ' Say([1]);',
  11811. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11812. '']);
  11813. ConvertProgram;
  11814. CheckSource('TestArrayOfConst_PassBaseTypes',
  11815. LinesToStr([ // statements
  11816. 'this.Say = function (args) {',
  11817. ' $mod.Say(args);',
  11818. '};',
  11819. 'this.p = null;',
  11820. 'this.j = undefined;',
  11821. 'this.c = 0;',
  11822. '']),
  11823. LinesToStr([ // $mod.$main
  11824. '$mod.Say([]);',
  11825. '$mod.Say(pas.system.VarRecs(0, 1));',
  11826. '$mod.Say(pas.system.VarRecs(',
  11827. ' 9,',
  11828. ' "c",',
  11829. ' 18,',
  11830. ' "foo",',
  11831. ' 5,',
  11832. ' null,',
  11833. ' 1,',
  11834. ' true,',
  11835. ' 3,',
  11836. ' 1.3,',
  11837. ' 5,',
  11838. ' $mod.p,',
  11839. ' 20,',
  11840. ' $mod.j,',
  11841. ' 12,',
  11842. ' $mod.c',
  11843. ' ));',
  11844. '']));
  11845. end;
  11846. procedure TTestModule.TestArrayOfConst_PassObj;
  11847. begin
  11848. StartProgram(true,[supTVarRec]);
  11849. Add([
  11850. '{$interfaces corba}',
  11851. 'type',
  11852. ' TObject = class',
  11853. ' end;',
  11854. ' TClass = class of TObject;',
  11855. ' IUnknown = interface',
  11856. ' end;',
  11857. 'procedure Say(args: array of const);',
  11858. 'begin',
  11859. 'end;',
  11860. 'var',
  11861. ' o: TObject;',
  11862. ' c: TClass;',
  11863. ' i: IUnknown;',
  11864. 'begin',
  11865. ' Say([o,c,TObject]);',
  11866. ' Say([nil,i]);',
  11867. '']);
  11868. ConvertProgram;
  11869. CheckSource('TestArrayOfConst_PassObj',
  11870. LinesToStr([ // statements
  11871. 'rtl.createClass(this, "TObject", null, function () {',
  11872. ' this.$init = function () {',
  11873. ' };',
  11874. ' this.$final = function () {',
  11875. ' };',
  11876. '});',
  11877. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11878. 'this.Say = function (args) {',
  11879. '};',
  11880. 'this.o = null;',
  11881. 'this.c = null;',
  11882. 'this.i = null;',
  11883. '']),
  11884. LinesToStr([ // $mod.$main
  11885. '$mod.Say(pas.system.VarRecs(',
  11886. ' 7,',
  11887. ' $mod.o,',
  11888. ' 8,',
  11889. ' $mod.c,',
  11890. ' 8,',
  11891. ' $mod.TObject',
  11892. '));',
  11893. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11894. '']));
  11895. end;
  11896. procedure TTestModule.TestRecord_Empty;
  11897. begin
  11898. StartProgram(false);
  11899. Add([
  11900. 'type',
  11901. ' TRecA = record',
  11902. ' end;',
  11903. 'var a,b: TRecA;',
  11904. 'begin',
  11905. ' if a=b then ;']);
  11906. ConvertProgram;
  11907. CheckSource('TestRecord_Empty',
  11908. LinesToStr([ // statements
  11909. 'rtl.recNewT(this, "TRecA", function () {',
  11910. ' this.$eq = function (b) {',
  11911. ' return true;',
  11912. ' };',
  11913. ' this.$assign = function (s) {',
  11914. ' return this;',
  11915. ' };',
  11916. '});',
  11917. 'this.a = this.TRecA.$new();',
  11918. 'this.b = this.TRecA.$new();',
  11919. '']),
  11920. LinesToStr([ // $mod.$main
  11921. 'if ($mod.a.$eq($mod.b)) ;'
  11922. ]));
  11923. end;
  11924. procedure TTestModule.TestRecord_Var;
  11925. begin
  11926. StartProgram(false);
  11927. Add('type');
  11928. Add(' TRecA = record');
  11929. Add(' Bold: longint;');
  11930. Add(' end;');
  11931. Add('var Rec: TRecA;');
  11932. Add('begin');
  11933. Add(' rec.bold:=123');
  11934. ConvertProgram;
  11935. CheckSource('TestRecord_Var',
  11936. LinesToStr([ // statements
  11937. 'rtl.recNewT(this, "TRecA", function () {',
  11938. ' this.Bold = 0;',
  11939. ' this.$eq = function (b) {',
  11940. ' return this.Bold === b.Bold;',
  11941. ' };',
  11942. ' this.$assign = function (s) {',
  11943. ' this.Bold = s.Bold;',
  11944. ' return this;',
  11945. ' };',
  11946. '});',
  11947. 'this.Rec = this.TRecA.$new();',
  11948. '']),
  11949. LinesToStr([ // $mod.$main
  11950. '$mod.Rec.Bold = 123;'
  11951. ]));
  11952. end;
  11953. procedure TTestModule.TestRecord_VarExternal;
  11954. begin
  11955. StartProgram(false);
  11956. Add([
  11957. '{$modeswitch externalclass}',
  11958. 'type',
  11959. ' TRecA = record',
  11960. ' i: byte;',
  11961. ' length_: longint external name ''length'';',
  11962. ' end;',
  11963. 'var Rec: TRecA;',
  11964. 'begin',
  11965. ' rec.length_ := rec.length_',
  11966. '']);
  11967. ConvertProgram;
  11968. CheckSource('TestRecord_VarExternal',
  11969. LinesToStr([ // statements
  11970. 'rtl.recNewT(this, "TRecA", function () {',
  11971. ' this.i = 0;',
  11972. ' this.$eq = function (b) {',
  11973. ' return (this.i === b.i) && (this.length === b.length);',
  11974. ' };',
  11975. ' this.$assign = function (s) {',
  11976. ' this.i = s.i;',
  11977. ' this.length = s.length;',
  11978. ' return this;',
  11979. ' };',
  11980. '});',
  11981. 'this.Rec = this.TRecA.$new();',
  11982. '']),
  11983. LinesToStr([ // $mod.$main
  11984. '$mod.Rec.length = $mod.Rec.length;'
  11985. ]));
  11986. end;
  11987. procedure TTestModule.TestRecord_WithDo;
  11988. begin
  11989. StartProgram(false);
  11990. Add('type');
  11991. Add(' TRec = record');
  11992. Add(' vI: longint;');
  11993. Add(' end;');
  11994. Add('var');
  11995. Add(' Int: longint;');
  11996. Add(' r: TRec;');
  11997. Add('begin');
  11998. Add(' with r do');
  11999. Add(' int:=vi;');
  12000. Add(' with r do begin');
  12001. Add(' int:=vi;');
  12002. Add(' vi:=int;');
  12003. Add(' end;');
  12004. ConvertProgram;
  12005. CheckSource('TestWithRecordDo',
  12006. LinesToStr([ // statements
  12007. 'rtl.recNewT(this, "TRec", function () {',
  12008. ' this.vI = 0;',
  12009. ' this.$eq = function (b) {',
  12010. ' return this.vI === b.vI;',
  12011. ' };',
  12012. ' this.$assign = function (s) {',
  12013. ' this.vI = s.vI;',
  12014. ' return this;',
  12015. ' };',
  12016. '});',
  12017. 'this.Int = 0;',
  12018. 'this.r = this.TRec.$new();',
  12019. '']),
  12020. LinesToStr([ // $mod.$main
  12021. 'var $with = $mod.r;',
  12022. '$mod.Int = $with.vI;',
  12023. 'var $with1 = $mod.r;',
  12024. '$mod.Int = $with1.vI;',
  12025. '$with1.vI = $mod.Int;'
  12026. ]));
  12027. end;
  12028. procedure TTestModule.TestRecord_Assign;
  12029. begin
  12030. StartProgram(false);
  12031. Add([
  12032. 'type',
  12033. ' TEnum = (red,green);',
  12034. ' TEnums = set of TEnum;',
  12035. ' TSmallRec = record',
  12036. ' N: longint;',
  12037. ' end;',
  12038. ' TBigRec = record',
  12039. ' Int: longint;',
  12040. ' D: double;',
  12041. ' Arr: array of longint;',
  12042. ' Arr2: array[1..2] of longint;',
  12043. ' Small: TSmallRec;',
  12044. ' Enums: TEnums;',
  12045. ' end;',
  12046. 'var',
  12047. ' r, s: TBigRec;',
  12048. 'begin',
  12049. ' r:=s;',
  12050. ' r:=default(TBigRec);',
  12051. ' r:=default(s);',
  12052. '']);
  12053. ConvertProgram;
  12054. CheckSource('TestRecord_Assign',
  12055. LinesToStr([ // statements
  12056. 'this.TEnum = {',
  12057. ' "0": "red",',
  12058. ' red: 0,',
  12059. ' "1": "green",',
  12060. ' green: 1',
  12061. '};',
  12062. 'rtl.recNewT(this, "TSmallRec", function () {',
  12063. ' this.N = 0;',
  12064. ' this.$eq = function (b) {',
  12065. ' return this.N === b.N;',
  12066. ' };',
  12067. ' this.$assign = function (s) {',
  12068. ' this.N = s.N;',
  12069. ' return this;',
  12070. ' };',
  12071. '});',
  12072. 'rtl.recNewT(this, "TBigRec", function () {',
  12073. ' this.Int = 0;',
  12074. ' this.D = 0.0;',
  12075. ' this.$new = function () {',
  12076. ' var r = Object.create(this);',
  12077. ' r.Arr = [];',
  12078. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12079. ' r.Small = $mod.TSmallRec.$new();',
  12080. ' r.Enums = {};',
  12081. ' return r;',
  12082. ' };',
  12083. ' this.$eq = function (b) {',
  12084. ' 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);',
  12085. ' };',
  12086. ' this.$assign = function (s) {',
  12087. ' this.Int = s.Int;',
  12088. ' this.D = s.D;',
  12089. ' this.Arr = rtl.arrayRef(s.Arr);',
  12090. ' this.Arr2 = s.Arr2.slice(0);',
  12091. ' this.Small.$assign(s.Small);',
  12092. ' this.Enums = rtl.refSet(s.Enums);',
  12093. ' return this;',
  12094. ' };',
  12095. '});',
  12096. 'this.r = this.TBigRec.$new();',
  12097. 'this.s = this.TBigRec.$new();',
  12098. '']),
  12099. LinesToStr([ // $mod.$main
  12100. '$mod.r.$assign($mod.s);',
  12101. '$mod.r.$assign($mod.TBigRec.$new());',
  12102. '$mod.r.$assign($mod.TBigRec.$new());',
  12103. '']));
  12104. end;
  12105. procedure TTestModule.TestRecord_AsParams;
  12106. begin
  12107. StartProgram(false);
  12108. Add([
  12109. 'type',
  12110. ' integer = longint;',
  12111. ' TRecord = record',
  12112. ' i: integer;',
  12113. ' end;',
  12114. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12115. 'var vL: TRecord;',
  12116. 'begin',
  12117. ' vd:=vd;',
  12118. ' vd.i:=vd.i;',
  12119. ' vl:=vc;',
  12120. ' vv:=vv;',
  12121. ' vv.i:=vv.i;',
  12122. ' U:=vl;',
  12123. ' U:=vd;',
  12124. ' U:=vc;',
  12125. ' U:=vv;',
  12126. ' vl:=TRecord(U);',
  12127. ' vd:=TRecord(U);',
  12128. ' vv:=TRecord(U);',
  12129. ' doit(vd,vd,vd,vd);',
  12130. ' doit(vc,vc,vl,vl);',
  12131. ' doit(vv,vv,vv,vv);',
  12132. ' doit(vl,vl,vl,vl);',
  12133. ' TRecord(U).i:=3;',
  12134. 'end;',
  12135. 'var i: TRecord;',
  12136. 'begin',
  12137. ' doit(i,i,i,i);',
  12138. '']);
  12139. ConvertProgram;
  12140. CheckSource('TestRecord_AsParams',
  12141. LinesToStr([ // statements
  12142. 'rtl.recNewT(this, "TRecord", function () {',
  12143. ' this.i = 0;',
  12144. ' this.$eq = function (b) {',
  12145. ' return this.i === b.i;',
  12146. ' };',
  12147. ' this.$assign = function (s) {',
  12148. ' this.i = s.i;',
  12149. ' return this;',
  12150. ' };',
  12151. '});',
  12152. 'this.DoIt = function (vD, vC, vV, U) {',
  12153. ' var vL = $mod.TRecord.$new();',
  12154. ' vD.$assign(vD);',
  12155. ' vD.i = vD.i;',
  12156. ' vL.$assign(vC);',
  12157. ' vV.$assign(vV);',
  12158. ' vV.i = vV.i;',
  12159. ' U.$assign(vL);',
  12160. ' U.$assign(vD);',
  12161. ' U.$assign(vC);',
  12162. ' U.$assign(vV);',
  12163. ' vL.$assign(U);',
  12164. ' vD.$assign(U);',
  12165. ' vV.$assign(U);',
  12166. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12167. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12168. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12169. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12170. ' U.i = 3;',
  12171. '};',
  12172. 'this.i = this.TRecord.$new();'
  12173. ]),
  12174. LinesToStr([
  12175. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12176. '']));
  12177. end;
  12178. procedure TTestModule.TestRecord_ConstRef;
  12179. begin
  12180. StartProgram(false);
  12181. Add([
  12182. 'type TRec = record i: word; end;',
  12183. 'procedure Run(constref a: TRec);',
  12184. 'begin',
  12185. 'end;',
  12186. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12187. 'var l: TRec;',
  12188. 'begin',
  12189. ' Run(l);',
  12190. ' Run(a);',
  12191. ' Run(b);',
  12192. ' Run(c);',
  12193. ' Run(d);',
  12194. ' Run(e);',
  12195. 'end;',
  12196. 'begin',
  12197. '']);
  12198. ConvertProgram;
  12199. CheckResolverUnexpectedHints();
  12200. CheckSource('TestRecord_ConstRef',
  12201. LinesToStr([ // statements
  12202. 'rtl.recNewT(this, "TRec", function () {',
  12203. ' this.i = 0;',
  12204. ' this.$eq = function (b) {',
  12205. ' return this.i === b.i;',
  12206. ' };',
  12207. ' this.$assign = function (s) {',
  12208. ' this.i = s.i;',
  12209. ' return this;',
  12210. ' };',
  12211. '});',
  12212. 'this.Run = function (a) {',
  12213. '};',
  12214. 'this.Fly = function (a, b, c, d, e) {',
  12215. ' var l = $mod.TRec.$new();',
  12216. ' $mod.Run(l);',
  12217. ' $mod.Run(a);',
  12218. ' $mod.Run(b);',
  12219. ' $mod.Run(c);',
  12220. ' $mod.Run(d);',
  12221. ' $mod.Run(e);',
  12222. '};',
  12223. '']),
  12224. LinesToStr([
  12225. '']));
  12226. end;
  12227. procedure TTestModule.TestRecordElement_AsParams;
  12228. begin
  12229. StartProgram(false);
  12230. Add('type');
  12231. Add(' integer = longint;');
  12232. Add(' TRecord = record');
  12233. Add(' i: integer;');
  12234. Add(' end;');
  12235. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12236. Add('var vJ: TRecord;');
  12237. Add('begin');
  12238. Add(' doit(vj.i,vj.i,vj.i);');
  12239. Add('end;');
  12240. Add('var r: TRecord;');
  12241. Add('begin');
  12242. Add(' doit(r.i,r.i,r.i);');
  12243. ConvertProgram;
  12244. CheckSource('TestRecordElement_AsParams',
  12245. LinesToStr([ // statements
  12246. 'rtl.recNewT(this, "TRecord", function () {',
  12247. ' this.i = 0;',
  12248. ' this.$eq = function (b) {',
  12249. ' return this.i === b.i;',
  12250. ' };',
  12251. ' this.$assign = function (s) {',
  12252. ' this.i = s.i;',
  12253. ' return this;',
  12254. ' };',
  12255. '});',
  12256. 'this.DoIt = function (vG,vH,vI) {',
  12257. ' var vJ = $mod.TRecord.$new();',
  12258. ' $mod.DoIt(vJ.i, vJ.i, {',
  12259. ' p: vJ,',
  12260. ' get: function () {',
  12261. ' return this.p.i;',
  12262. ' },',
  12263. ' set: function (v) {',
  12264. ' this.p.i = v;',
  12265. ' }',
  12266. ' });',
  12267. '};',
  12268. 'this.r = this.TRecord.$new();'
  12269. ]),
  12270. LinesToStr([
  12271. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12272. ' p: $mod.r,',
  12273. ' get: function () {',
  12274. ' return this.p.i;',
  12275. ' },',
  12276. ' set: function (v) {',
  12277. ' this.p.i = v;',
  12278. ' }',
  12279. '});'
  12280. ]));
  12281. end;
  12282. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12283. begin
  12284. StartProgram(false);
  12285. Add('type');
  12286. Add(' integer = longint;');
  12287. Add(' TRecord = record');
  12288. Add(' i: integer;');
  12289. Add(' end;');
  12290. Add('function GetRec(vB: integer = 0): TRecord;');
  12291. Add('begin');
  12292. Add('end;');
  12293. Add('procedure DoIt(vG: integer; const vH: integer);');
  12294. Add('begin');
  12295. Add('end;');
  12296. Add('begin');
  12297. Add(' doit(getrec.i,getrec.i);');
  12298. Add(' doit(getrec().i,getrec().i);');
  12299. Add(' doit(getrec(1).i,getrec(2).i);');
  12300. ConvertProgram;
  12301. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12302. LinesToStr([ // statements
  12303. 'rtl.recNewT(this, "TRecord", function () {',
  12304. ' this.i = 0;',
  12305. ' this.$eq = function (b) {',
  12306. ' return this.i === b.i;',
  12307. ' };',
  12308. ' this.$assign = function (s) {',
  12309. ' this.i = s.i;',
  12310. ' return this;',
  12311. ' };',
  12312. '});',
  12313. 'this.GetRec = function (vB) {',
  12314. ' var Result = $mod.TRecord.$new();',
  12315. ' return Result;',
  12316. '};',
  12317. 'this.DoIt = function (vG, vH) {',
  12318. '};',
  12319. '']),
  12320. LinesToStr([
  12321. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12322. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12323. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12324. '']));
  12325. end;
  12326. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12327. begin
  12328. StartProgram(false);
  12329. Add('type');
  12330. Add(' integer = longint;');
  12331. Add(' TRecord = record');
  12332. Add(' i: integer;');
  12333. Add(' end;');
  12334. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12335. Add('begin');
  12336. Add('end;');
  12337. Add('var r: trecord;');
  12338. Add('begin');
  12339. Add(' with r do ');
  12340. Add(' doit(i,i,i);');
  12341. ConvertProgram;
  12342. CheckSource('TestRecordElementFromWith_AsParams',
  12343. LinesToStr([ // statements
  12344. 'rtl.recNewT(this, "TRecord", function () {',
  12345. ' this.i = 0;',
  12346. ' this.$eq = function (b) {',
  12347. ' return this.i === b.i;',
  12348. ' };',
  12349. ' this.$assign = function (s) {',
  12350. ' this.i = s.i;',
  12351. ' return this;',
  12352. ' };',
  12353. '});',
  12354. 'this.DoIt = function (vG,vH,vI) {',
  12355. '};',
  12356. 'this.r = this.TRecord.$new();'
  12357. ]),
  12358. LinesToStr([
  12359. 'var $with = $mod.r;',
  12360. '$mod.DoIt($with.i,$with.i,{',
  12361. ' p: $with,',
  12362. ' get: function () {',
  12363. ' return this.p.i;',
  12364. ' },',
  12365. ' set: function (v) {',
  12366. ' this.p.i = v;',
  12367. ' }',
  12368. '});',
  12369. '']));
  12370. end;
  12371. procedure TTestModule.TestRecord_Equal;
  12372. begin
  12373. StartProgram(false);
  12374. Add('type');
  12375. Add(' integer = longint;');
  12376. Add(' TFlag = (red,blue);');
  12377. Add(' TFlags = set of TFlag;');
  12378. Add(' TProc = procedure;');
  12379. Add(' TRecord = record');
  12380. Add(' i: integer;');
  12381. Add(' Event: TProc;');
  12382. Add(' f: TFlags;');
  12383. Add(' end;');
  12384. Add(' TNested = record');
  12385. Add(' r: TRecord;');
  12386. Add(' end;');
  12387. Add('var');
  12388. Add(' b: boolean;');
  12389. Add(' r,s: trecord;');
  12390. Add('begin');
  12391. Add(' b:=r=s;');
  12392. Add(' b:=r<>s;');
  12393. ConvertProgram;
  12394. CheckSource('TestRecord_Equal',
  12395. LinesToStr([ // statements
  12396. 'this.TFlag = {',
  12397. ' "0": "red",',
  12398. ' red: 0,',
  12399. ' "1": "blue",',
  12400. ' blue: 1',
  12401. '};',
  12402. 'rtl.recNewT(this, "TRecord", function () {',
  12403. ' this.i = 0;',
  12404. ' this.Event = null;',
  12405. ' this.$new = function () {',
  12406. ' var r = Object.create(this);',
  12407. ' r.f = {};',
  12408. ' return r;',
  12409. ' };',
  12410. ' this.$eq = function (b) {',
  12411. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12412. ' };',
  12413. ' this.$assign = function (s) {',
  12414. ' this.i = s.i;',
  12415. ' this.Event = s.Event;',
  12416. ' this.f = rtl.refSet(s.f);',
  12417. ' return this;',
  12418. ' };',
  12419. '});',
  12420. 'rtl.recNewT(this, "TNested", function () {',
  12421. ' this.$new = function () {',
  12422. ' var r = Object.create(this);',
  12423. ' r.r = $mod.TRecord.$new();',
  12424. ' return r;',
  12425. ' };',
  12426. ' this.$eq = function (b) {',
  12427. ' return this.r.$eq(b.r);',
  12428. ' };',
  12429. ' this.$assign = function (s) {',
  12430. ' this.r.$assign(s.r);',
  12431. ' return this;',
  12432. ' };',
  12433. '});',
  12434. 'this.b = false;',
  12435. 'this.r = this.TRecord.$new();',
  12436. 'this.s = this.TRecord.$new();',
  12437. '']),
  12438. LinesToStr([
  12439. '$mod.b = $mod.r.$eq($mod.s);',
  12440. '$mod.b = !$mod.r.$eq($mod.s);',
  12441. '']));
  12442. end;
  12443. procedure TTestModule.TestRecord_JSValue;
  12444. begin
  12445. StartProgram(false);
  12446. Add([
  12447. 'type',
  12448. ' TRecord = record',
  12449. ' i: longint;',
  12450. ' end;',
  12451. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12452. 'begin',
  12453. 'end;',
  12454. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12455. 'begin',
  12456. ' if jsvalue(d) then ;',
  12457. ' if jsvalue(c) then ;',
  12458. ' if jsvalue(v) then ;',
  12459. 'end;',
  12460. 'var',
  12461. ' Jv: jsvalue;',
  12462. ' Rec: trecord;',
  12463. 'begin',
  12464. ' rec:=trecord(jv);',
  12465. ' jv:=rec;',
  12466. ' Fly(rec,rec);',
  12467. ' Fly(@rec,@rec);',
  12468. ' if jsvalue(Rec) then ;',
  12469. ' Run(trecord(jv),trecord(jv),rec);',
  12470. '']);
  12471. ConvertProgram;
  12472. CheckSource('TestRecord_JSValue',
  12473. LinesToStr([ // statements
  12474. 'rtl.recNewT(this, "TRecord", function () {',
  12475. ' this.i = 0;',
  12476. ' this.$eq = function (b) {',
  12477. ' return this.i === b.i;',
  12478. ' };',
  12479. ' this.$assign = function (s) {',
  12480. ' this.i = s.i;',
  12481. ' return this;',
  12482. ' };',
  12483. '});',
  12484. 'this.Fly = function (d, c) {',
  12485. '};',
  12486. 'this.Run = function (d, c, v) {',
  12487. ' if (d) ;',
  12488. ' if (c) ;',
  12489. ' if (v) ;',
  12490. '};',
  12491. 'this.Jv = undefined;',
  12492. 'this.Rec = this.TRecord.$new();',
  12493. '']),
  12494. LinesToStr([
  12495. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12496. '$mod.Jv = $mod.Rec;',
  12497. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12498. '$mod.Fly($mod.Rec, $mod.Rec);',
  12499. 'if ($mod.Rec) ;',
  12500. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12501. '']));
  12502. end;
  12503. procedure TTestModule.TestRecord_VariantFail;
  12504. begin
  12505. StartProgram(false);
  12506. Add([
  12507. 'type',
  12508. ' TRec = record',
  12509. ' case word of',
  12510. ' 0: (b0, b1: Byte);',
  12511. ' 1: (i: word);',
  12512. ' end;',
  12513. 'begin']);
  12514. SetExpectedPasResolverError('Not supported: variant record',
  12515. nNotSupportedX);
  12516. ConvertProgram;
  12517. end;
  12518. procedure TTestModule.TestRecord_FieldArray;
  12519. begin
  12520. StartProgram(false);
  12521. Add([
  12522. 'type',
  12523. ' TArrInt = array[3..4] of longint;',
  12524. ' TArrArrInt = array[3..4] of longint;',
  12525. ' TRec = record',
  12526. ' a: array of longint;',
  12527. ' s: array[1..2] of longint;',
  12528. ' m: array[1..2,3..4] of longint;',
  12529. ' o: TArrArrInt;',
  12530. ' end;',
  12531. 'begin']);
  12532. ConvertProgram;
  12533. CheckSource('TestRecord_FieldArray',
  12534. LinesToStr([ // statements
  12535. 'rtl.recNewT(this, "TRec", function () {',
  12536. ' this.m$a$clone = function (a) {',
  12537. ' var b = [];',
  12538. ' b.length = 2;',
  12539. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12540. ' return b;',
  12541. ' };',
  12542. ' this.$new = function () {',
  12543. ' var r = Object.create(this);',
  12544. ' r.a = [];',
  12545. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12546. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12547. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12548. ' return r;',
  12549. ' };',
  12550. ' this.$eq = function (b) {',
  12551. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12552. ' };',
  12553. ' this.$assign = function (s) {',
  12554. ' this.a = rtl.arrayRef(s.a);',
  12555. ' this.s = s.s.slice(0);',
  12556. ' this.m = this.m$a$clone(s.m);',
  12557. ' this.o = s.o.slice(0);',
  12558. ' return this;',
  12559. ' };',
  12560. '});',
  12561. '']),
  12562. LinesToStr([ // $mod.$main
  12563. '']));
  12564. end;
  12565. procedure TTestModule.TestRecord_Const;
  12566. begin
  12567. StartProgram(false);
  12568. Add([
  12569. 'type',
  12570. ' TArrInt = array[3..4] of longint;',
  12571. ' TPoint = record x,y: longint; end;',
  12572. ' TRec = record',
  12573. ' i: longint;',
  12574. ' a: array of longint;',
  12575. ' s: array[1..2] of longint;',
  12576. ' m: array[1..2,3..4] of longint;',
  12577. ' p: TPoint;',
  12578. ' end;',
  12579. ' TPoints = array of TPoint;',
  12580. 'const',
  12581. ' r: TRec = (',
  12582. ' i:1;',
  12583. ' a:(2,3);',
  12584. ' s:(4,5);',
  12585. ' m:( (11,12), (13,14) );',
  12586. ' p: (x:21; y:22)',
  12587. ' );',
  12588. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12589. 'begin']);
  12590. ConvertProgram;
  12591. CheckSource('TestRecord_Const',
  12592. LinesToStr([ // statements
  12593. 'rtl.recNewT(this, "TPoint", function () {',
  12594. ' this.x = 0;',
  12595. ' this.y = 0;',
  12596. ' this.$eq = function (b) {',
  12597. ' return (this.x === b.x) && (this.y === b.y);',
  12598. ' };',
  12599. ' this.$assign = function (s) {',
  12600. ' this.x = s.x;',
  12601. ' this.y = s.y;',
  12602. ' return this;',
  12603. ' };',
  12604. '});',
  12605. 'rtl.recNewT(this, "TRec", function () {',
  12606. ' this.i = 0;',
  12607. ' this.m$a$clone = function (a) {',
  12608. ' var b = [];',
  12609. ' b.length = 2;',
  12610. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12611. ' return b;',
  12612. ' };',
  12613. ' this.$new = function () {',
  12614. ' var r = Object.create(this);',
  12615. ' r.a = [];',
  12616. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12617. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12618. ' r.p = $mod.TPoint.$new();',
  12619. ' return r;',
  12620. ' };',
  12621. ' this.$eq = function (b) {',
  12622. ' 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);',
  12623. ' };',
  12624. ' this.$assign = function (s) {',
  12625. ' this.i = s.i;',
  12626. ' this.a = rtl.arrayRef(s.a);',
  12627. ' this.s = s.s.slice(0);',
  12628. ' this.m = this.m$a$clone(s.m);',
  12629. ' this.p.$assign(s.p);',
  12630. ' return this;',
  12631. ' };',
  12632. '});',
  12633. 'this.r = this.TRec.$clone({',
  12634. ' i: 1,',
  12635. ' a: [2, 3],',
  12636. ' s: [4, 5],',
  12637. ' m: [[11, 12], [13, 14]],',
  12638. ' p: this.TPoint.$clone({',
  12639. ' x: 21,',
  12640. ' y: 22',
  12641. ' })',
  12642. '});',
  12643. 'this.p = [this.TPoint.$clone({',
  12644. ' x: 1,',
  12645. ' y: 2',
  12646. '}), this.TPoint.$clone({',
  12647. ' x: 3,',
  12648. ' y: 4',
  12649. '})];',
  12650. '']),
  12651. LinesToStr([ // $mod.$main
  12652. '']));
  12653. end;
  12654. procedure TTestModule.TestRecord_TypecastFail;
  12655. begin
  12656. StartProgram(false);
  12657. Add([
  12658. 'type',
  12659. ' TPoint = record x,y: longint; end;',
  12660. ' TRec = record l: longint end;',
  12661. 'var p: TPoint;',
  12662. 'begin',
  12663. ' if TRec(p).l=2 then ;']);
  12664. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12665. nIllegalTypeConversionTo);
  12666. ConvertProgram;
  12667. end;
  12668. procedure TTestModule.TestRecord_InFunction;
  12669. begin
  12670. StartProgram(false);
  12671. Add([
  12672. 'var TPoint: longint = 3;',
  12673. 'procedure DoIt;',
  12674. 'type',
  12675. ' TPoint = record x,y: longint; end;',
  12676. ' TPoints = array of TPoint;',
  12677. 'var',
  12678. ' r: TPoint;',
  12679. ' p: TPoints;',
  12680. 'begin',
  12681. ' SetLength(p,2);',
  12682. 'end;',
  12683. 'begin']);
  12684. ConvertProgram;
  12685. CheckSource('TestRecord_InFunction',
  12686. LinesToStr([ // statements
  12687. 'this.TPoint = 3;',
  12688. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12689. ' this.x = 0;',
  12690. ' this.y = 0;',
  12691. ' this.$eq = function (b) {',
  12692. ' return (this.x === b.x) && (this.y === b.y);',
  12693. ' };',
  12694. ' this.$assign = function (s) {',
  12695. ' this.x = s.x;',
  12696. ' this.y = s.y;',
  12697. ' return this;',
  12698. ' };',
  12699. '});',
  12700. 'this.DoIt = function () {',
  12701. ' var r = TPoint$1.$new();',
  12702. ' var p = [];',
  12703. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12704. '};',
  12705. '']),
  12706. LinesToStr([ // $mod.$main
  12707. '']));
  12708. end;
  12709. procedure TTestModule.TestRecordAnonym_Field;
  12710. begin
  12711. StartProgram(false);
  12712. Add(['',
  12713. 'var Rec: record',
  12714. ' Bold: longint;',
  12715. ' end;',
  12716. 'begin',
  12717. ' rec.bold:=123;',
  12718. ' rec.bold:=rec.bold+7;',
  12719. '']);
  12720. ConvertProgram;
  12721. CheckSource('TestRecordAnonym_Field',
  12722. LinesToStr([ // statements
  12723. 'rtl.recNewT(this, "Rec$a", function () {',
  12724. ' this.Bold = 0;',
  12725. ' this.$eq = function (b) {',
  12726. ' return this.Bold === b.Bold;',
  12727. ' };',
  12728. ' this.$assign = function (s) {',
  12729. ' this.Bold = s.Bold;',
  12730. ' return this;',
  12731. ' };',
  12732. '});',
  12733. 'this.Rec = this.Rec$a.$new();',
  12734. '']),
  12735. LinesToStr([ // $mod.$main
  12736. '$mod.Rec.Bold = 123;',
  12737. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12738. '']));
  12739. end;
  12740. procedure TTestModule.TestRecordAnonym_Assign;
  12741. begin
  12742. StartProgram(false);
  12743. Add(['',
  12744. 'var S,T: record',
  12745. ' Bold: longint;',
  12746. ' end;',
  12747. ' b: boolean;',
  12748. 'begin',
  12749. ' S:=T;',
  12750. ' b:=s=t;',
  12751. '']);
  12752. ConvertProgram;
  12753. CheckSource('TestRecordAnonym_Assign',
  12754. LinesToStr([ // statements
  12755. 'rtl.recNewT(this, "T$a", function () {',
  12756. ' this.Bold = 0;',
  12757. ' this.$eq = function (b) {',
  12758. ' return this.Bold === b.Bold;',
  12759. ' };',
  12760. ' this.$assign = function (s) {',
  12761. ' this.Bold = s.Bold;',
  12762. ' return this;',
  12763. ' };',
  12764. '});',
  12765. 'this.S = this.T$a.$new();',
  12766. 'this.T = this.T$a.$new();',
  12767. 'this.b = false;',
  12768. '']),
  12769. LinesToStr([ // $mod.$main
  12770. '$mod.S.$assign($mod.T);',
  12771. '$mod.b = $mod.S.$eq($mod.T);',
  12772. '']));
  12773. end;
  12774. procedure TTestModule.TestRecordAnonym_Nested;
  12775. begin
  12776. StartProgram(false);
  12777. Add(['',
  12778. 'var S,T: record',
  12779. ' Bold: longint;',
  12780. ' Sub: record',
  12781. ' Color: word;',
  12782. ' end;',
  12783. ' end;',
  12784. ' b: boolean;',
  12785. 'begin',
  12786. ' S:=T;',
  12787. ' S.Sub:=T.Sub;',
  12788. ' S.Sub.Color:=T.Sub.Color+3;',
  12789. ' b:=s=t;',
  12790. ' b:=s.Sub=t.Sub;',
  12791. '']);
  12792. ConvertProgram;
  12793. CheckSource('TestRecordAnonym_Nested',
  12794. LinesToStr([ // statements
  12795. 'rtl.recNewT(this, "T$a", function () {',
  12796. ' this.Bold = 0;',
  12797. ' rtl.recNewT(this, "Sub$a", function () {',
  12798. ' this.Color = 0;',
  12799. ' this.$eq = function (b) {',
  12800. ' return this.Color === b.Color;',
  12801. ' };',
  12802. ' this.$assign = function (s) {',
  12803. ' this.Color = s.Color;',
  12804. ' return this;',
  12805. ' };',
  12806. ' });',
  12807. ' this.$new = function () {',
  12808. ' var r = Object.create(this);',
  12809. ' r.Sub = this.Sub$a.$new();',
  12810. ' return r;',
  12811. ' };',
  12812. ' this.$eq = function (b) {',
  12813. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12814. ' };',
  12815. ' this.$assign = function (s) {',
  12816. ' this.Bold = s.Bold;',
  12817. ' this.Sub.$assign(s.Sub);',
  12818. ' return this;',
  12819. ' };',
  12820. '}, true);',
  12821. 'this.S = this.T$a.$new();',
  12822. 'this.T = this.T$a.$new();',
  12823. 'this.b = false;',
  12824. '']),
  12825. LinesToStr([ // $mod.$main
  12826. '$mod.S.$assign($mod.T);',
  12827. '$mod.S.Sub.$assign($mod.T.Sub);',
  12828. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12829. '$mod.b = $mod.S.$eq($mod.T);',
  12830. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12831. '']));
  12832. end;
  12833. procedure TTestModule.TestRecordAnonym_Const;
  12834. begin
  12835. StartProgram(false);
  12836. Add(['',
  12837. 'var T: record',
  12838. ' Bold: longint;',
  12839. ' Sub: record',
  12840. ' Color: word;',
  12841. ' end;',
  12842. ' end = (Bold: 2; Sub: (Color: 3));',
  12843. 'begin',
  12844. '']);
  12845. ConvertProgram;
  12846. CheckSource('TestRecordAnonym_Const',
  12847. LinesToStr([ // statements
  12848. 'rtl.recNewT(this, "T$a", function () {',
  12849. ' this.Bold = 0;',
  12850. ' rtl.recNewT(this, "Sub$a", function () {',
  12851. ' this.Color = 0;',
  12852. ' this.$eq = function (b) {',
  12853. ' return this.Color === b.Color;',
  12854. ' };',
  12855. ' this.$assign = function (s) {',
  12856. ' this.Color = s.Color;',
  12857. ' return this;',
  12858. ' };',
  12859. ' });',
  12860. ' this.$new = function () {',
  12861. ' var r = Object.create(this);',
  12862. ' r.Sub = this.Sub$a.$new();',
  12863. ' return r;',
  12864. ' };',
  12865. ' this.$eq = function (b) {',
  12866. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12867. ' };',
  12868. ' this.$assign = function (s) {',
  12869. ' this.Bold = s.Bold;',
  12870. ' this.Sub.$assign(s.Sub);',
  12871. ' return this;',
  12872. ' };',
  12873. '}, true);',
  12874. 'this.T = this.T$a.$clone({',
  12875. ' Bold: 2,',
  12876. ' Sub: this.T$a.Sub$a.$clone({',
  12877. ' Color: 3',
  12878. ' })',
  12879. '});',
  12880. '']),
  12881. LinesToStr([ // $mod.$main
  12882. '']));
  12883. end;
  12884. procedure TTestModule.TestRecordAnonym_InFunction;
  12885. begin
  12886. StartProgram(false);
  12887. Add(['',
  12888. 'procedure Fly;',
  12889. 'var T: record',
  12890. ' Bold: longint;',
  12891. ' Sub: record',
  12892. ' Color: word;',
  12893. ' end;',
  12894. ' end = (Bold: 2; Sub: (Color: 3));',
  12895. 'begin',
  12896. 'end;',
  12897. 'begin',
  12898. '']);
  12899. ConvertProgram;
  12900. CheckSource('TestRecordAnonym_InFunction',
  12901. LinesToStr([ // statements
  12902. 'var T$a = rtl.recNewT(null, "", function () {',
  12903. ' this.Bold = 0;',
  12904. ' rtl.recNewT(this, "Sub$a", function () {',
  12905. ' this.Color = 0;',
  12906. ' this.$eq = function (b) {',
  12907. ' return this.Color === b.Color;',
  12908. ' };',
  12909. ' this.$assign = function (s) {',
  12910. ' this.Color = s.Color;',
  12911. ' return this;',
  12912. ' };',
  12913. ' });',
  12914. ' this.$new = function () {',
  12915. ' var r = Object.create(this);',
  12916. ' r.Sub = this.Sub$a.$new();',
  12917. ' return r;',
  12918. ' };',
  12919. ' this.$eq = function (b) {',
  12920. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12921. ' };',
  12922. ' this.$assign = function (s) {',
  12923. ' this.Bold = s.Bold;',
  12924. ' this.Sub.$assign(s.Sub);',
  12925. ' return this;',
  12926. ' };',
  12927. '}, true);',
  12928. 'this.Fly = function () {',
  12929. ' var T = T$a.$clone({',
  12930. ' Bold: 2,',
  12931. ' Sub: T$a.Sub$a.$clone({',
  12932. ' Color: 3',
  12933. ' })',
  12934. ' });',
  12935. '};',
  12936. '']),
  12937. LinesToStr([ // $mod.$main
  12938. '']));
  12939. end;
  12940. procedure TTestModule.TestAdvRecord_Function;
  12941. begin
  12942. StartProgram(false);
  12943. Parser.Options:=Parser.Options+[po_cassignments];
  12944. Add([
  12945. '{$modeswitch AdvancedRecords}',
  12946. 'type',
  12947. ' TPoint = record',
  12948. ' x,y: word;',
  12949. ' function Add(const apt: TPoint): TPoint;',
  12950. ' end;',
  12951. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12952. 'begin',
  12953. ' Result:=Self;',
  12954. ' Result.x+=apt.x;',
  12955. ' Result.y:=Result.y+apt.y;',
  12956. ' Self:=apt;',
  12957. 'end;',
  12958. 'var p,q: TPoint;',
  12959. 'begin',
  12960. ' p.add(q);',
  12961. ' p:=default(TPoint);',
  12962. ' p:=q;',
  12963. '']);
  12964. ConvertProgram;
  12965. CheckSource('TestAdvRecord_Function',
  12966. LinesToStr([ // statements
  12967. 'rtl.recNewT(this, "TPoint", function () {',
  12968. ' this.x = 0;',
  12969. ' this.y = 0;',
  12970. ' this.$eq = function (b) {',
  12971. ' return (this.x === b.x) && (this.y === b.y);',
  12972. ' };',
  12973. ' this.$assign = function (s) {',
  12974. ' this.x = s.x;',
  12975. ' this.y = s.y;',
  12976. ' return this;',
  12977. ' };',
  12978. ' this.Add = function (apt) {',
  12979. ' var Result = $mod.TPoint.$new();',
  12980. ' Result.$assign(this);',
  12981. ' Result.x += apt.x;',
  12982. ' Result.y = Result.y + apt.y;',
  12983. ' this.$assign(apt);',
  12984. ' return Result;',
  12985. ' };',
  12986. '});',
  12987. 'this.p = this.TPoint.$new();',
  12988. 'this.q = this.TPoint.$new();',
  12989. '']),
  12990. LinesToStr([ // $mod.$main
  12991. '$mod.p.Add($mod.q);',
  12992. '$mod.p.$assign($mod.TPoint.$new());',
  12993. '$mod.p.$assign($mod.q);',
  12994. '']));
  12995. end;
  12996. procedure TTestModule.TestAdvRecord_Property;
  12997. begin
  12998. StartProgram(false);
  12999. Add([
  13000. '{$modeswitch AdvancedRecords}',
  13001. 'type',
  13002. ' TPoint = record',
  13003. ' x,y: word;',
  13004. ' strict private',
  13005. ' function GetSize: longword;',
  13006. ' procedure SetSize(Value: longword);',
  13007. ' public',
  13008. ' property Size: longword read GetSize write SetSize;',
  13009. ' property Left: word read x write y;',
  13010. ' end;',
  13011. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13012. 'function TPoint.GetSize: longword;',
  13013. 'begin',
  13014. ' x:=y;',
  13015. ' Size:=Size;',
  13016. ' Left:=Left;',
  13017. 'end;',
  13018. 'procedure TPoint.SetSize(Value: longword);',
  13019. 'begin',
  13020. 'end;',
  13021. 'var p,q: TPoint;',
  13022. 'begin',
  13023. ' p.Size:=q.Size;',
  13024. ' p.Left:=q.Left;',
  13025. '']);
  13026. ConvertProgram;
  13027. CheckSource('TestAdvRecord_Property',
  13028. LinesToStr([ // statements
  13029. 'rtl.recNewT(this, "TPoint", function () {',
  13030. ' this.x = 0;',
  13031. ' this.y = 0;',
  13032. ' this.$eq = function (b) {',
  13033. ' return (this.x === b.x) && (this.y === b.y);',
  13034. ' };',
  13035. ' this.$assign = function (s) {',
  13036. ' this.x = s.x;',
  13037. ' this.y = s.y;',
  13038. ' return this;',
  13039. ' };',
  13040. ' this.GetSize = function () {',
  13041. ' var Result = 0;',
  13042. ' this.x = this.y;',
  13043. ' this.SetSize(this.GetSize());',
  13044. ' this.y = this.x;',
  13045. ' return Result;',
  13046. ' };',
  13047. ' this.SetSize = function (Value) {',
  13048. ' };',
  13049. '});',
  13050. 'this.SetSize = function (Value) {',
  13051. '};',
  13052. 'this.p = this.TPoint.$new();',
  13053. 'this.q = this.TPoint.$new();',
  13054. '']),
  13055. LinesToStr([ // $mod.$main
  13056. '$mod.p.SetSize($mod.q.GetSize());',
  13057. '$mod.p.y = $mod.q.x;',
  13058. '']));
  13059. end;
  13060. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13061. begin
  13062. StartProgram(false);
  13063. Add([
  13064. '{$modeswitch AdvancedRecords}',
  13065. 'type',
  13066. ' TPoint = record',
  13067. ' strict private',
  13068. ' function GetItems(Index: word): word;',
  13069. ' procedure SetItems(Index: word; Value: word);',
  13070. ' public',
  13071. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13072. ' end;',
  13073. 'function TPoint.GetItems(Index: word): word;',
  13074. 'begin',
  13075. ' Items[index]:=Items[index];',
  13076. ' self.Items[index]:=self.Items[index];',
  13077. 'end;',
  13078. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13079. 'begin',
  13080. 'end;',
  13081. 'var p: TPoint;',
  13082. 'begin',
  13083. ' p[1]:=p[2];',
  13084. ' p.Items[3]:=p.Items[4];',
  13085. '']);
  13086. ConvertProgram;
  13087. CheckSource('TestAdvRecord_PropertyDefault',
  13088. LinesToStr([ // statements
  13089. 'rtl.recNewT(this, "TPoint", function () {',
  13090. ' this.$eq = function (b) {',
  13091. ' return true;',
  13092. ' };',
  13093. ' this.$assign = function (s) {',
  13094. ' return this;',
  13095. ' };',
  13096. ' this.GetItems = function (Index) {',
  13097. ' var Result = 0;',
  13098. ' this.SetItems(Index, this.GetItems(Index));',
  13099. ' this.SetItems(Index, this.GetItems(Index));',
  13100. ' return Result;',
  13101. ' };',
  13102. ' this.SetItems = function (Index, Value) {',
  13103. ' };',
  13104. '});',
  13105. 'this.p = this.TPoint.$new();',
  13106. '']),
  13107. LinesToStr([ // $mod.$main
  13108. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13109. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13110. '']));
  13111. end;
  13112. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13113. begin
  13114. StartProgram(false);
  13115. Add([
  13116. '{$modeswitch AdvancedRecords}',
  13117. 'type',
  13118. ' TRec = record',
  13119. ' class var',
  13120. ' Fx: longint;',
  13121. ' Fy: longint;',
  13122. ' class function GetInt: longint; static;',
  13123. ' class procedure SetInt(Value: longint); static;',
  13124. ' class procedure DoIt; static;',
  13125. ' class property IntA: longint read Fx write Fy;',
  13126. ' class property IntB: longint read GetInt write SetInt;',
  13127. ' end;',
  13128. 'class function trec.getint: longint;',
  13129. 'begin',
  13130. ' result:=fx;',
  13131. 'end;',
  13132. 'class procedure trec.setint(value: longint);',
  13133. 'begin',
  13134. 'end;',
  13135. 'class procedure trec.doit;',
  13136. 'begin',
  13137. ' IntA:=IntA+1;',
  13138. ' IntB:=IntB+1;',
  13139. 'end;',
  13140. 'var r: trec;',
  13141. 'begin',
  13142. ' trec.inta:=trec.inta+1;',
  13143. ' if trec.intb=2 then;',
  13144. ' trec.intb:=trec.intb+2;',
  13145. ' trec.setint(trec.inta);',
  13146. ' r.inta:=r.inta+1;',
  13147. ' if r.intb=2 then;',
  13148. ' r.intb:=r.intb+2;',
  13149. ' r.setint(r.inta);']);
  13150. ConvertProgram;
  13151. CheckSource('TestAdvRecord_Property_ClassMethod',
  13152. LinesToStr([ // statements
  13153. 'rtl.recNewT(this, "TRec", function () {',
  13154. ' this.Fx = 0;',
  13155. ' this.Fy = 0;',
  13156. ' this.$eq = function (b) {',
  13157. ' return true;',
  13158. ' };',
  13159. ' this.$assign = function (s) {',
  13160. ' return this;',
  13161. ' };',
  13162. ' this.GetInt = function () {',
  13163. ' var Result = 0;',
  13164. ' Result = $mod.TRec.Fx;',
  13165. ' return Result;',
  13166. ' };',
  13167. ' this.SetInt = function (Value) {',
  13168. ' };',
  13169. ' this.DoIt = function () {',
  13170. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13171. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13172. ' };',
  13173. '}, true);',
  13174. 'this.r = this.TRec.$new();',
  13175. '']),
  13176. LinesToStr([ // $mod.$main
  13177. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13178. 'if ($mod.TRec.GetInt() === 2) ;',
  13179. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13180. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13181. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13182. 'if ($mod.TRec.GetInt() === 2) ;',
  13183. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13184. '$mod.TRec.SetInt($mod.r.Fx);',
  13185. '']));
  13186. end;
  13187. procedure TTestModule.TestAdvRecord_Const;
  13188. begin
  13189. StartProgram(false);
  13190. Add([
  13191. '{$modeswitch AdvancedRecords}',
  13192. 'type',
  13193. ' TArrInt = array[3..4] of longint;',
  13194. ' TPoint = record',
  13195. ' x,y: longint;',
  13196. ' class var Count: nativeint;',
  13197. ' end;',
  13198. ' TRec = record',
  13199. ' i: longint;',
  13200. ' a: array of longint;',
  13201. ' s: array[1..2] of longint;',
  13202. ' m: array[1..2,3..4] of longint;',
  13203. ' p: TPoint;',
  13204. ' end;',
  13205. ' TPoints = array of TPoint;',
  13206. 'const',
  13207. ' r: TRec = (',
  13208. ' i:1;',
  13209. ' a:(2,3);',
  13210. ' s:(4,5);',
  13211. ' m:( (11,12), (13,14) );',
  13212. ' p: (x:21)',
  13213. ' );',
  13214. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13215. 'begin']);
  13216. ConvertProgram;
  13217. CheckSource('TestAdvRecord_Const',
  13218. LinesToStr([ // statements
  13219. 'rtl.recNewT(this, "TPoint", function () {',
  13220. ' this.x = 0;',
  13221. ' this.y = 0;',
  13222. ' this.Count = 0;',
  13223. ' this.$eq = function (b) {',
  13224. ' return (this.x === b.x) && (this.y === b.y);',
  13225. ' };',
  13226. ' this.$assign = function (s) {',
  13227. ' this.x = s.x;',
  13228. ' this.y = s.y;',
  13229. ' return this;',
  13230. ' };',
  13231. '}, true);',
  13232. 'rtl.recNewT(this, "TRec", function () {',
  13233. ' this.i = 0;',
  13234. ' this.m$a$clone = function (a) {',
  13235. ' var b = [];',
  13236. ' b.length = 2;',
  13237. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13238. ' return b;',
  13239. ' };',
  13240. ' this.$new = function () {',
  13241. ' var r = Object.create(this);',
  13242. ' r.a = [];',
  13243. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13244. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13245. ' r.p = $mod.TPoint.$new();',
  13246. ' return r;',
  13247. ' };',
  13248. ' this.$eq = function (b) {',
  13249. ' 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);',
  13250. ' };',
  13251. ' this.$assign = function (s) {',
  13252. ' this.i = s.i;',
  13253. ' this.a = rtl.arrayRef(s.a);',
  13254. ' this.s = s.s.slice(0);',
  13255. ' this.m = this.m$a$clone(s.m);',
  13256. ' this.p.$assign(s.p);',
  13257. ' return this;',
  13258. ' };',
  13259. '});',
  13260. 'this.r = this.TRec.$clone({',
  13261. ' i: 1,',
  13262. ' a: [2, 3],',
  13263. ' s: [4, 5],',
  13264. ' m: [[11, 12], [13, 14]],',
  13265. ' p: this.TPoint.$clone({',
  13266. ' x: 21,',
  13267. ' y: 0',
  13268. ' })',
  13269. '});',
  13270. 'this.p = [this.TPoint.$clone({',
  13271. ' x: 1,',
  13272. ' y: 2',
  13273. '}), this.TPoint.$clone({',
  13274. ' x: 3,',
  13275. ' y: 4',
  13276. '})];',
  13277. '']),
  13278. LinesToStr([ // $mod.$main
  13279. '']));
  13280. end;
  13281. procedure TTestModule.TestAdvRecord_ExternalField;
  13282. begin
  13283. StartProgram(false);
  13284. Add([
  13285. '{$modeswitch AdvancedRecords}',
  13286. '{$modeswitch externalclass}',
  13287. 'type',
  13288. ' TCar = record',
  13289. ' public',
  13290. ' Intern: longint external name ''$Intern'';',
  13291. ' Intern2: longint external name ''$Intern2'';',
  13292. ' Bracket: longint external name ''["A B"]'';',
  13293. ' procedure DoIt;',
  13294. ' end;',
  13295. 'procedure tcar.doit;',
  13296. 'begin',
  13297. ' Intern:=Intern+1;',
  13298. ' Intern2:=Intern2+2;',
  13299. ' Bracket:=Bracket+3;',
  13300. 'end;',
  13301. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13302. 'begin',
  13303. ' Rec.intern:=Rec.intern+1;',
  13304. ' Rec.intern2:=Rec.intern2+2;',
  13305. ' Rec.Bracket:=Rec.Bracket+3;',
  13306. ' with Rec do begin',
  13307. ' intern:=intern+1;',
  13308. ' intern2:=intern2+2;',
  13309. ' Bracket:=Bracket+3;',
  13310. ' end;']);
  13311. ConvertProgram;
  13312. CheckSource('TestAdvRecord_ExternalField',
  13313. LinesToStr([ // statements
  13314. 'rtl.recNewT(this, "TCar", function () {',
  13315. ' this.$eq = function (b) {',
  13316. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13317. ' };',
  13318. ' this.$assign = function (s) {',
  13319. ' this.$Intern = s.$Intern;',
  13320. ' this.$Intern2 = s.$Intern2;',
  13321. ' this["A B"] = s["A B"];',
  13322. ' return this;',
  13323. ' };',
  13324. ' this.DoIt = function () {',
  13325. ' this.$Intern = this.$Intern + 1;',
  13326. ' this.$Intern2 = this.$Intern2 + 2;',
  13327. ' this["A B"] = this["A B"] + 3;',
  13328. ' };',
  13329. '});',
  13330. 'this.Rec = this.TCar.$clone({',
  13331. ' $Intern: 11,',
  13332. ' $Intern2: 12,',
  13333. ' "A B": 13',
  13334. '});',
  13335. '']),
  13336. LinesToStr([ // $mod.$main
  13337. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13338. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13339. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13340. 'var $with = $mod.Rec;',
  13341. '$with.$Intern = $with.$Intern + 1;',
  13342. '$with.$Intern2 = $with.$Intern2 + 2;',
  13343. '$with["A B"] = $with["A B"] + 3;',
  13344. '']));
  13345. end;
  13346. procedure TTestModule.TestAdvRecord_SubRecord;
  13347. begin
  13348. StartProgram(false);
  13349. Add([
  13350. '{$modeswitch AdvancedRecords}',
  13351. 'type',
  13352. ' TRec = record',
  13353. ' type',
  13354. ' TPoint = record',
  13355. ' x,y: longint;',
  13356. ' class var Count: nativeint;',
  13357. ' procedure DoIt;',
  13358. ' class procedure DoThat; static;',
  13359. ' end;',
  13360. ' var',
  13361. ' i: longint;',
  13362. ' p: TPoint;',
  13363. ' procedure DoSome;',
  13364. ' end;',
  13365. 'const',
  13366. ' r: TRec = (',
  13367. ' i:1;',
  13368. ' p: (x:21;y:22)',
  13369. ' );',
  13370. 'procedure TRec.DoSome;',
  13371. 'begin',
  13372. ' p.x:=p.y+1;',
  13373. ' p.Count:=p.Count+2;',
  13374. 'end;',
  13375. 'procedure TRec.TPoint.DoIt;',
  13376. 'begin',
  13377. ' Count:=Count+3;',
  13378. 'end;',
  13379. 'class procedure TRec.TPoint.DoThat;',
  13380. 'begin',
  13381. ' Count:=Count+4;',
  13382. 'end;',
  13383. 'begin']);
  13384. ConvertProgram;
  13385. CheckSource('TestAdvRecord_SubRecord',
  13386. LinesToStr([ // statements
  13387. 'rtl.recNewT(this, "TRec", function () {',
  13388. ' rtl.recNewT(this, "TPoint", function () {',
  13389. ' this.x = 0;',
  13390. ' this.y = 0;',
  13391. ' this.Count = 0;',
  13392. ' this.$eq = function (b) {',
  13393. ' return (this.x === b.x) && (this.y === b.y);',
  13394. ' };',
  13395. ' this.$assign = function (s) {',
  13396. ' this.x = s.x;',
  13397. ' this.y = s.y;',
  13398. ' return this;',
  13399. ' };',
  13400. ' this.DoIt = function () {',
  13401. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13402. ' };',
  13403. ' this.DoThat = function () {',
  13404. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13405. ' };',
  13406. ' }, true);',
  13407. ' this.i = 0;',
  13408. ' this.$new = function () {',
  13409. ' var r = Object.create(this);',
  13410. ' r.p = this.TPoint.$new();',
  13411. ' return r;',
  13412. ' };',
  13413. ' this.$eq = function (b) {',
  13414. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13415. ' };',
  13416. ' this.$assign = function (s) {',
  13417. ' this.i = s.i;',
  13418. ' this.p.$assign(s.p);',
  13419. ' return this;',
  13420. ' };',
  13421. ' this.DoSome = function () {',
  13422. ' this.p.x = this.p.y + 1;',
  13423. ' this.TPoint.Count = this.p.Count + 2;',
  13424. ' };',
  13425. '}, true);',
  13426. 'this.r = this.TRec.$clone({',
  13427. ' i: 1,',
  13428. ' p: this.TRec.TPoint.$clone({',
  13429. ' x: 21,',
  13430. ' y: 22',
  13431. ' })',
  13432. '});',
  13433. '']),
  13434. LinesToStr([ // $mod.$main
  13435. '']));
  13436. end;
  13437. procedure TTestModule.TestAdvRecord_SubClass;
  13438. begin
  13439. StartProgram(false);
  13440. Add([
  13441. '{$modeswitch AdvancedRecords}',
  13442. 'type',
  13443. ' TObject = class end;',
  13444. ' TPoint = record',
  13445. ' type',
  13446. ' TBird = class',
  13447. ' procedure DoIt;',
  13448. ' class procedure Glob;',
  13449. ' end;',
  13450. ' procedure DoIt(b: TBird);',
  13451. ' end;',
  13452. 'procedure TPoint.TBird.DoIt;',
  13453. 'begin',
  13454. ' doit;',
  13455. ' self.doit;',
  13456. ' glob;',
  13457. ' self.glob;',
  13458. 'end;',
  13459. 'class procedure TPoint.TBird.Glob;',
  13460. 'begin',
  13461. ' glob;',
  13462. ' self.glob;',
  13463. 'end;',
  13464. 'procedure TPoint.DoIt(b: TBird);',
  13465. 'begin',
  13466. ' b.doit;',
  13467. ' b.glob;',
  13468. ' TBird.glob;',
  13469. 'end;',
  13470. 'begin',
  13471. '']);
  13472. ConvertProgram;
  13473. CheckSource('TestAdvRecord_SubClass',
  13474. LinesToStr([ // statements
  13475. 'rtl.createClass(this, "TObject", null, function () {',
  13476. ' this.$init = function () {',
  13477. ' };',
  13478. ' this.$final = function () {',
  13479. ' };',
  13480. '});',
  13481. 'rtl.recNewT(this, "TPoint", function () {',
  13482. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13483. ' this.DoIt = function () {',
  13484. ' this.DoIt();',
  13485. ' this.DoIt();',
  13486. ' this.$class.Glob();',
  13487. ' this.$class.Glob();',
  13488. ' };',
  13489. ' this.Glob = function () {',
  13490. ' this.Glob();',
  13491. ' this.Glob();',
  13492. ' };',
  13493. ' }, "TPoint.TBird");',
  13494. ' this.$eq = function (b) {',
  13495. ' return true;',
  13496. ' };',
  13497. ' this.$assign = function (s) {',
  13498. ' return this;',
  13499. ' };',
  13500. ' this.DoIt = function (b) {',
  13501. ' b.DoIt();',
  13502. ' b.$class.Glob();',
  13503. ' this.TBird.Glob();',
  13504. ' };',
  13505. '}, true);',
  13506. '']),
  13507. LinesToStr([ // $mod.$main
  13508. '']));
  13509. end;
  13510. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13511. begin
  13512. StartProgram(false);
  13513. Add([
  13514. '{$modeswitch AdvancedRecords}',
  13515. 'type',
  13516. ' IUnknown = interface end;',
  13517. ' TPoint = record',
  13518. ' type IBird = interface end;',
  13519. ' end;',
  13520. 'begin',
  13521. '']);
  13522. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13523. nNotYetImplemented);
  13524. ParseProgram;
  13525. end;
  13526. procedure TTestModule.TestAdvRecord_Constructor;
  13527. begin
  13528. StartProgram(false);
  13529. Add([
  13530. '{$modeswitch AdvancedRecords}',
  13531. 'type',
  13532. ' TPoint = record',
  13533. ' x,y: longint;',
  13534. ' class procedure Run(w: longint = 13); static;',
  13535. ' constructor Create(ax: longint; ay: longint = -1);',
  13536. ' end;',
  13537. 'class procedure tpoint.run(w: longint);',
  13538. 'begin',
  13539. ' run;',
  13540. ' run();',
  13541. 'end;',
  13542. 'constructor tpoint.create(ax,ay: longint);',
  13543. 'begin',
  13544. ' x:=ax;',
  13545. ' self.y:=ay;',
  13546. ' run;',
  13547. ' run(ax);',
  13548. 'end;',
  13549. 'var r: TPoint;',
  13550. 'begin',
  13551. ' r:=TPoint.Create(1,2);',
  13552. ' with TPoint do r:=Create(1,2);',
  13553. ' r.Create(3);',
  13554. ' r:=r.Create(4);',
  13555. '']);
  13556. ConvertProgram;
  13557. CheckSource('TestAdvRecord_Constructor',
  13558. LinesToStr([ // statements
  13559. 'rtl.recNewT(this, "TPoint", function () {',
  13560. ' this.x = 0;',
  13561. ' this.y = 0;',
  13562. ' this.$eq = function (b) {',
  13563. ' return (this.x === b.x) && (this.y === b.y);',
  13564. ' };',
  13565. ' this.$assign = function (s) {',
  13566. ' this.x = s.x;',
  13567. ' this.y = s.y;',
  13568. ' return this;',
  13569. ' };',
  13570. ' this.Run = function (w) {',
  13571. ' $mod.TPoint.Run(13);',
  13572. ' $mod.TPoint.Run(13);',
  13573. ' };',
  13574. ' this.Create = function (ax, ay) {',
  13575. ' this.x = ax;',
  13576. ' this.y = ay;',
  13577. ' this.Run(13);',
  13578. ' this.Run(ax);',
  13579. ' return this;',
  13580. ' };',
  13581. '});',
  13582. 'this.r = this.TPoint.$new();',
  13583. '']),
  13584. LinesToStr([ // $mod.$main
  13585. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13586. 'var $with = $mod.TPoint;',
  13587. '$mod.r.$assign($with.$new().Create(1, 2));',
  13588. '$mod.r.Create(3, -1);',
  13589. '$mod.r.$assign($mod.r.Create(4, -1));',
  13590. '']));
  13591. end;
  13592. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13593. begin
  13594. StartProgram(false);
  13595. Add([
  13596. '{$modeswitch AdvancedRecords}',
  13597. 'type',
  13598. ' TPoint = record',
  13599. ' class var x: longint;',
  13600. ' class procedure Fly; static;',
  13601. ' class constructor Init;',
  13602. ' end;',
  13603. 'var count: word;',
  13604. 'class procedure Tpoint.Fly;',
  13605. 'begin',
  13606. 'end;',
  13607. 'class constructor tpoint.init;',
  13608. 'begin',
  13609. ' count:=count+1;',
  13610. ' x:=x+3;',
  13611. ' tpoint.x:=tpoint.x+4;',
  13612. ' fly;',
  13613. ' tpoint.fly;',
  13614. 'end;',
  13615. 'var r: TPoint;',
  13616. 'begin',
  13617. ' r.x:=r.x+10;',
  13618. ' r.Fly;',
  13619. ' r.Fly();',
  13620. '']);
  13621. ConvertProgram;
  13622. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13623. LinesToStr([ // statements
  13624. 'rtl.recNewT(this, "TPoint", function () {',
  13625. ' this.x = 0;',
  13626. ' this.$eq = function (b) {',
  13627. ' return true;',
  13628. ' };',
  13629. ' this.$assign = function (s) {',
  13630. ' return this;',
  13631. ' };',
  13632. ' this.Fly = function () {',
  13633. ' };',
  13634. '}, true);',
  13635. 'this.count = 0;',
  13636. 'this.r = this.TPoint.$new();',
  13637. '']),
  13638. LinesToStr([ // $mod.$main
  13639. '(function () {',
  13640. ' $mod.count = $mod.count + 1;',
  13641. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13642. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13643. ' $mod.TPoint.Fly();',
  13644. ' $mod.TPoint.Fly();',
  13645. '})();',
  13646. '$mod.TPoint.x = $mod.r.x + 10;',
  13647. '$mod.TPoint.Fly();',
  13648. '$mod.TPoint.Fly();',
  13649. '']));
  13650. end;
  13651. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13652. begin
  13653. StartUnit(false);
  13654. Add([
  13655. 'interface',
  13656. '{$modeswitch AdvancedRecords}',
  13657. 'type',
  13658. ' TPoint = record',
  13659. ' class var x: longint;',
  13660. ' class procedure Fly; static;',
  13661. ' class constructor Init;',
  13662. ' end;',
  13663. 'implementation',
  13664. 'var count: word;',
  13665. 'class procedure Tpoint.Fly;',
  13666. 'begin',
  13667. 'end;',
  13668. 'class constructor tpoint.init;',
  13669. 'begin',
  13670. ' count:=count+1;',
  13671. ' x:=3;',
  13672. ' tpoint.x:=4;',
  13673. ' fly;',
  13674. ' tpoint.fly;',
  13675. 'end;',
  13676. '']);
  13677. ConvertUnit;
  13678. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13679. LinesToStr([ // statements
  13680. 'var $impl = $mod.$impl;',
  13681. 'rtl.recNewT(this, "TPoint", function () {',
  13682. ' this.x = 0;',
  13683. ' this.$eq = function (b) {',
  13684. ' return true;',
  13685. ' };',
  13686. ' this.$assign = function (s) {',
  13687. ' return this;',
  13688. ' };',
  13689. ' this.Fly = function () {',
  13690. ' };',
  13691. '}, true);',
  13692. '']),
  13693. LinesToStr([ // $mod.$init
  13694. '(function () {',
  13695. ' $impl.count = $impl.count + 1;',
  13696. ' $mod.TPoint.x = 3;',
  13697. ' $mod.TPoint.x = 4;',
  13698. ' $mod.TPoint.Fly();',
  13699. ' $mod.TPoint.Fly();',
  13700. '})();',
  13701. '']),
  13702. LinesToStr([ // $mod.$main
  13703. '$impl.count = 0;',
  13704. '']));
  13705. end;
  13706. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13707. begin
  13708. StartProgram(false);
  13709. Add(['type',
  13710. ' TObject = class',
  13711. ' public',
  13712. ' constructor Create;',
  13713. ' destructor Destroy;',
  13714. ' end;',
  13715. ' TBird = TObject;',
  13716. 'constructor tobject.create;',
  13717. 'begin end;',
  13718. 'destructor tobject.destroy;',
  13719. 'begin end;',
  13720. 'var Obj: tobject;',
  13721. 'begin',
  13722. ' obj:=tobject.create;',
  13723. ' obj:=tobject.create();',
  13724. ' obj:=tbird.create;',
  13725. ' obj:=tbird.create();',
  13726. ' obj:=obj.create();',
  13727. ' obj.destroy;',
  13728. '']);
  13729. ConvertProgram;
  13730. CheckSource('TestClass_TObjectDefaultConstructor',
  13731. LinesToStr([ // statements
  13732. 'rtl.createClass(this,"TObject",null,function(){',
  13733. ' this.$init = function () {',
  13734. ' };',
  13735. ' this.$final = function () {',
  13736. ' };',
  13737. ' this.Create = function(){',
  13738. ' return this;',
  13739. ' };',
  13740. ' this.Destroy = function(){',
  13741. ' };',
  13742. '});',
  13743. 'this.Obj = null;'
  13744. ]),
  13745. LinesToStr([ // $mod.$main
  13746. '$mod.Obj = $mod.TObject.$create("Create");',
  13747. '$mod.Obj = $mod.TObject.$create("Create");',
  13748. '$mod.Obj = $mod.TObject.$create("Create");',
  13749. '$mod.Obj = $mod.TObject.$create("Create");',
  13750. '$mod.Obj = $mod.Obj.Create();',
  13751. '$mod.Obj.$destroy("Destroy");',
  13752. '']));
  13753. end;
  13754. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13755. begin
  13756. StartProgram(false);
  13757. Add('type');
  13758. Add(' TObject = class');
  13759. Add(' public');
  13760. Add(' constructor Create(Par: longint);');
  13761. Add(' end;');
  13762. Add('constructor tobject.create(par: longint);');
  13763. Add('begin end;');
  13764. Add('var Obj: tobject;');
  13765. Add('begin');
  13766. Add(' obj:=tobject.create(3);');
  13767. ConvertProgram;
  13768. CheckSource('TestClass_TObjectConstructorWithParams',
  13769. LinesToStr([ // statements
  13770. 'rtl.createClass(this,"TObject",null,function(){',
  13771. ' this.$init = function () {',
  13772. ' };',
  13773. ' this.$final = function () {',
  13774. ' };',
  13775. ' this.Create = function(Par){',
  13776. ' return this;',
  13777. ' };',
  13778. '});',
  13779. 'this.Obj = null;'
  13780. ]),
  13781. LinesToStr([ // $mod.$main
  13782. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13783. ]));
  13784. end;
  13785. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13786. begin
  13787. StartProgram(false);
  13788. Add('type');
  13789. Add(' TObject = class');
  13790. Add(' public');
  13791. Add(' constructor Create;');
  13792. Add(' end;');
  13793. Add(' TTest = class(TObject)');
  13794. Add(' public');
  13795. Add(' constructor Create(const Par: longint = 1);');
  13796. Add(' end;');
  13797. Add('constructor tobject.create;');
  13798. Add('begin end;');
  13799. Add('constructor ttest.create(const par: longint);');
  13800. Add('begin end;');
  13801. Add('var t: ttest;');
  13802. Add('begin');
  13803. Add(' t:=ttest.create;');
  13804. Add(' t:=ttest.create(2);');
  13805. ConvertProgram;
  13806. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13807. LinesToStr([ // statements
  13808. 'rtl.createClass(this,"TObject",null,function(){',
  13809. ' this.$init = function () {',
  13810. ' };',
  13811. ' this.$final = function () {',
  13812. ' };',
  13813. ' this.Create = function(){',
  13814. ' return this;',
  13815. ' };',
  13816. '});',
  13817. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13818. ' this.Create$1 = function (Par) {',
  13819. ' return this;',
  13820. ' };',
  13821. '});',
  13822. 'this.t = null;'
  13823. ]),
  13824. LinesToStr([ // $mod.$main
  13825. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13826. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13827. ]));
  13828. end;
  13829. procedure TTestModule.TestClass_Var;
  13830. begin
  13831. StartProgram(false);
  13832. Add([
  13833. 'type',
  13834. ' TObject = class',
  13835. ' public',
  13836. ' vI: longint;',
  13837. ' constructor Create(Par: longint);',
  13838. ' end;',
  13839. 'constructor tobject.create(par: longint);',
  13840. 'begin',
  13841. ' vi:=par+3',
  13842. 'end;',
  13843. 'var Obj: tobject;',
  13844. 'begin',
  13845. ' obj:=tobject.create(4);',
  13846. ' obj.vi:=obj.VI+5;']);
  13847. ConvertProgram;
  13848. CheckSource('TestClass_Var',
  13849. LinesToStr([ // statements
  13850. 'rtl.createClass(this,"TObject",null,function(){',
  13851. ' this.$init = function () {',
  13852. ' this.vI = 0;',
  13853. ' };',
  13854. ' this.$final = function () {',
  13855. ' };',
  13856. ' this.Create = function(Par){',
  13857. ' this.vI = Par+3;',
  13858. ' return this;',
  13859. ' };',
  13860. '});',
  13861. 'this.Obj = null;'
  13862. ]),
  13863. LinesToStr([ // $mod.$main
  13864. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13865. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13866. ]));
  13867. end;
  13868. procedure TTestModule.TestClass_Method;
  13869. begin
  13870. StartProgram(false);
  13871. Add('type');
  13872. Add(' TObject = class');
  13873. Add(' public');
  13874. Add(' vI: longint;');
  13875. Add(' Sub: TObject;');
  13876. Add(' constructor Create;');
  13877. Add(' function GetIt(Par: longint): tobject;');
  13878. Add(' end;');
  13879. Add('constructor tobject.create; begin end;');
  13880. Add('function tobject.getit(par: longint): tobject;');
  13881. Add('begin');
  13882. Add(' Self.vi:=par+3;');
  13883. Add(' Result:=self.sub;');
  13884. Add('end;');
  13885. Add('var Obj: tobject;');
  13886. Add('begin');
  13887. Add(' obj:=tobject.create;');
  13888. Add(' obj.getit(4);');
  13889. Add(' obj.sub.sub:=nil;');
  13890. Add(' obj.sub.getit(5);');
  13891. Add(' obj.sub.getit(6).SUB:=nil;');
  13892. Add(' obj.sub.getit(7).GETIT(8);');
  13893. Add(' obj.sub.getit(9).SuB.getit(10);');
  13894. ConvertProgram;
  13895. CheckSource('TestClass_Method',
  13896. LinesToStr([ // statements
  13897. 'rtl.createClass(this,"TObject",null,function(){',
  13898. ' this.$init = function () {',
  13899. ' this.vI = 0;',
  13900. ' this.Sub = null;',
  13901. ' };',
  13902. ' this.$final = function () {',
  13903. ' this.Sub = undefined;',
  13904. ' };',
  13905. ' this.Create = function(){',
  13906. ' return this;',
  13907. ' };',
  13908. ' this.GetIt = function(Par){',
  13909. ' var Result = null;',
  13910. ' this.vI = Par + 3;',
  13911. ' Result = this.Sub;',
  13912. ' return Result;',
  13913. ' };',
  13914. '});',
  13915. 'this.Obj = null;'
  13916. ]),
  13917. LinesToStr([ // $mod.$main
  13918. '$mod.Obj = $mod.TObject.$create("Create");',
  13919. '$mod.Obj.GetIt(4);',
  13920. '$mod.Obj.Sub.Sub=null;',
  13921. '$mod.Obj.Sub.GetIt(5);',
  13922. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13923. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13924. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13925. ]));
  13926. end;
  13927. procedure TTestModule.TestClass_Implementation;
  13928. begin
  13929. StartUnit(false);
  13930. Add([
  13931. 'interface',
  13932. 'type',
  13933. ' TObject = class',
  13934. ' constructor Create;',
  13935. ' end;',
  13936. 'implementation',
  13937. 'type',
  13938. ' TIntClass = class',
  13939. ' constructor Create; reintroduce;',
  13940. ' class procedure DoGlob;',
  13941. ' end;',
  13942. 'constructor tintclass.create;',
  13943. 'begin',
  13944. ' inherited;',
  13945. ' inherited create;',
  13946. ' doglob;',
  13947. 'end;',
  13948. 'class procedure tintclass.doglob;',
  13949. 'begin',
  13950. 'end;',
  13951. 'constructor tobject.create;',
  13952. 'var',
  13953. ' iC: tintclass;',
  13954. 'begin',
  13955. ' ic:=tintclass.create;',
  13956. ' tintclass.doglob;',
  13957. ' ic.doglob;',
  13958. 'end;',
  13959. 'initialization',
  13960. ' tintclass.doglob;',
  13961. '']);
  13962. ConvertUnit;
  13963. CheckSource('TestClass_Implementation',
  13964. LinesToStr([ // statements
  13965. 'var $impl = $mod.$impl;',
  13966. 'rtl.createClass(this, "TObject", null, function () {',
  13967. ' this.$init = function () {',
  13968. ' };',
  13969. ' this.$final = function () {',
  13970. ' };',
  13971. ' this.Create = function () {',
  13972. ' var iC = null;',
  13973. ' iC = $impl.TIntClass.$create("Create$1");',
  13974. ' $impl.TIntClass.DoGlob();',
  13975. ' iC.$class.DoGlob();',
  13976. ' return this;',
  13977. ' };',
  13978. '});',
  13979. '']),
  13980. LinesToStr([ // $mod.$main
  13981. '$impl.TIntClass.DoGlob();',
  13982. '']),
  13983. LinesToStr([
  13984. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13985. ' this.Create$1 = function () {',
  13986. ' $mod.TObject.Create.call(this);',
  13987. ' $mod.TObject.Create.call(this);',
  13988. ' this.$class.DoGlob();',
  13989. ' return this;',
  13990. ' };',
  13991. ' this.DoGlob = function () {',
  13992. ' };',
  13993. '});',
  13994. '']));
  13995. end;
  13996. procedure TTestModule.TestClass_Inheritance;
  13997. begin
  13998. StartProgram(false);
  13999. Add('type');
  14000. Add(' TObject = class');
  14001. Add(' public');
  14002. Add(' constructor Create;');
  14003. Add(' end;');
  14004. Add(' TClassA = class');
  14005. Add(' end;');
  14006. Add(' TClassB = class(TObject)');
  14007. Add(' procedure ProcB;');
  14008. Add(' end;');
  14009. Add('constructor tobject.create; begin end;');
  14010. Add('procedure tclassb.procb; begin end;');
  14011. Add('var');
  14012. Add(' oO: TObject;');
  14013. Add(' oA: TClassA;');
  14014. Add(' oB: TClassB;');
  14015. Add('begin');
  14016. Add(' oO:=tobject.Create;');
  14017. Add(' oA:=tclassa.Create;');
  14018. Add(' ob:=tclassb.Create;');
  14019. Add(' if oo is tclassa then ;');
  14020. Add(' ob:=oo as tclassb;');
  14021. Add(' (oo as tclassb).procb;');
  14022. ConvertProgram;
  14023. CheckSource('TestClass_Inheritance',
  14024. LinesToStr([ // statements
  14025. 'rtl.createClass(this,"TObject",null,function(){',
  14026. ' this.$init = function () {',
  14027. ' };',
  14028. ' this.$final = function () {',
  14029. ' };',
  14030. ' this.Create = function () {',
  14031. ' return this;',
  14032. ' };',
  14033. '});',
  14034. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14035. '});',
  14036. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14037. ' this.ProcB = function () {',
  14038. ' };',
  14039. '});',
  14040. 'this.oO = null;',
  14041. 'this.oA = null;',
  14042. 'this.oB = null;'
  14043. ]),
  14044. LinesToStr([ // $mod.$main
  14045. '$mod.oO = $mod.TObject.$create("Create");',
  14046. '$mod.oA = $mod.TClassA.$create("Create");',
  14047. '$mod.oB = $mod.TClassB.$create("Create");',
  14048. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14049. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14050. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14051. ]));
  14052. end;
  14053. procedure TTestModule.TestClass_TypeAlias;
  14054. begin
  14055. StartProgram(false);
  14056. Add([
  14057. '{$interfaces corba}',
  14058. 'type',
  14059. ' IObject = interface',
  14060. ' end;',
  14061. ' IBird = type IObject;',
  14062. ' TObject = class',
  14063. ' end;',
  14064. ' TBird = type TObject;',
  14065. 'var',
  14066. ' oObj: TObject;',
  14067. ' oBird: TBird;',
  14068. ' IntfObj: IObject;',
  14069. ' IntfBird: IBird;',
  14070. 'begin',
  14071. ' oObj:=oBird;',
  14072. '']);
  14073. ConvertProgram;
  14074. CheckSource('TestClass_TypeAlias',
  14075. LinesToStr([ // statements
  14076. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14077. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14078. 'rtl.createClass(this, "TObject", null, function () {',
  14079. ' this.$init = function () {',
  14080. ' };',
  14081. ' this.$final = function () {',
  14082. ' };',
  14083. '});',
  14084. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14085. '});',
  14086. 'this.oObj = null;',
  14087. 'this.oBird = null;',
  14088. 'this.IntfObj = null;',
  14089. 'this.IntfBird = null;',
  14090. '']),
  14091. LinesToStr([ // $mod.$main
  14092. '$mod.oObj = $mod.oBird;',
  14093. '']));
  14094. end;
  14095. procedure TTestModule.TestClass_AbstractMethod;
  14096. begin
  14097. StartProgram(false);
  14098. Add('type');
  14099. Add(' TObject = class');
  14100. Add(' public');
  14101. Add(' procedure DoIt; virtual; abstract;');
  14102. Add(' end;');
  14103. Add('begin');
  14104. ConvertProgram;
  14105. CheckSource('TestClass_AbstractMethod',
  14106. LinesToStr([ // statements
  14107. 'rtl.createClass(this,"TObject",null,function(){',
  14108. ' this.$init = function () {',
  14109. ' };',
  14110. ' this.$final = function () {',
  14111. ' };',
  14112. '});'
  14113. ]),
  14114. LinesToStr([ // this.$main
  14115. ''
  14116. ]));
  14117. end;
  14118. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14119. begin
  14120. StartProgram(false);
  14121. Add([
  14122. 'type',
  14123. ' TObject = class',
  14124. ' procedure DoAbstract; virtual; abstract;',
  14125. ' procedure DoVirtual; virtual;',
  14126. ' procedure DoIt;',
  14127. ' end;',
  14128. ' TA = class',
  14129. ' procedure doabstract; override;',
  14130. ' procedure dovirtual; override;',
  14131. ' procedure DoSome;',
  14132. ' end;',
  14133. 'procedure tobject.dovirtual;',
  14134. 'begin',
  14135. ' inherited; // call non existing ancestor -> ignore silently',
  14136. 'end;',
  14137. 'procedure tobject.doit;',
  14138. 'begin',
  14139. 'end;',
  14140. 'procedure ta.doabstract;',
  14141. 'begin',
  14142. ' inherited dovirtual; // call TObject.DoVirtual',
  14143. 'end;',
  14144. 'procedure ta.dovirtual;',
  14145. 'begin',
  14146. ' inherited; // call TObject.DoVirtual',
  14147. ' inherited dovirtual; // call TObject.DoVirtual',
  14148. ' inherited dovirtual(); // call TObject.DoVirtual',
  14149. ' doit;',
  14150. ' doit();',
  14151. 'end;',
  14152. 'procedure ta.dosome;',
  14153. 'begin',
  14154. ' inherited; // call non existing ancestor method -> silently ignore',
  14155. 'end;',
  14156. 'begin']);
  14157. ConvertProgram;
  14158. CheckSource('TestClass_CallInherited_ProcNoParams',
  14159. LinesToStr([ // statements
  14160. 'rtl.createClass(this,"TObject",null,function(){',
  14161. ' this.$init = function () {',
  14162. ' };',
  14163. ' this.$final = function () {',
  14164. ' };',
  14165. ' this.DoVirtual = function () {',
  14166. ' };',
  14167. ' this.DoIt = function () {',
  14168. ' };',
  14169. '});',
  14170. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14171. ' this.DoAbstract = function () {',
  14172. ' $mod.TObject.DoVirtual.call(this);',
  14173. ' };',
  14174. ' this.DoVirtual = function () {',
  14175. ' $mod.TObject.DoVirtual.call(this);',
  14176. ' $mod.TObject.DoVirtual.call(this);',
  14177. ' $mod.TObject.DoVirtual.call(this);',
  14178. ' this.DoIt();',
  14179. ' this.DoIt();',
  14180. ' };',
  14181. ' this.DoSome = function () {',
  14182. ' };',
  14183. '});'
  14184. ]),
  14185. LinesToStr([ // this.$main
  14186. ''
  14187. ]));
  14188. end;
  14189. procedure TTestModule.TestClass_CallInherited_WithParams;
  14190. begin
  14191. StartProgram(false);
  14192. Add([
  14193. 'type',
  14194. ' TObject = class',
  14195. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14196. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14197. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14198. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14199. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14200. ' end;',
  14201. ' TClassA = class',
  14202. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14203. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14204. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14205. ' end;',
  14206. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14207. 'begin',
  14208. 'end;',
  14209. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14210. 'begin',
  14211. 'end;',
  14212. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14213. 'begin',
  14214. 'end;',
  14215. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14216. 'begin',
  14217. 'end;',
  14218. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14219. 'begin',
  14220. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14221. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14222. 'end;',
  14223. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14224. 'begin',
  14225. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14226. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14227. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14228. ' doit(pa,pb);',
  14229. ' doit(pa);',
  14230. ' doit2(pa);',
  14231. ' doit2;',
  14232. 'end;',
  14233. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14234. 'begin',
  14235. ' pa:=inherited;',
  14236. 'end;',
  14237. 'begin']);
  14238. ConvertProgram;
  14239. CheckSource('TestClass_CallInherited_WithParams',
  14240. LinesToStr([ // statements
  14241. 'rtl.createClass(this,"TObject",null,function(){',
  14242. ' this.$init = function () {',
  14243. ' };',
  14244. ' this.$final = function () {',
  14245. ' };',
  14246. ' this.DoVirtual = function (pA,pB) {',
  14247. ' };',
  14248. ' this.DoIt = function (pA,pB) {',
  14249. ' };',
  14250. ' this.DoIt2 = function (pA,pB) {',
  14251. ' };',
  14252. ' this.GetIt = function (pA, pB) {',
  14253. ' var Result = 0;',
  14254. ' return Result;',
  14255. ' };',
  14256. '});',
  14257. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14258. ' this.DoAbstract = function (pA,pB) {',
  14259. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14260. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14261. ' };',
  14262. ' this.DoVirtual = function (pA,pB) {',
  14263. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14264. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14265. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14266. ' this.DoIt(pA,pB);',
  14267. ' this.DoIt(pA,0);',
  14268. ' this.DoIt2(pA,2);',
  14269. ' this.DoIt2(1,2);',
  14270. ' };',
  14271. ' this.GetIt$1 = function (pA, pB) {',
  14272. ' var Result = 0;',
  14273. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14274. ' return Result;',
  14275. ' };',
  14276. '});'
  14277. ]),
  14278. LinesToStr([ // this.$main
  14279. ''
  14280. ]));
  14281. end;
  14282. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14283. begin
  14284. StartProgram(false);
  14285. Add('type');
  14286. Add(' TObject = class');
  14287. Add(' constructor Create; virtual;');
  14288. Add(' constructor CreateWithB(b: boolean);');
  14289. Add(' end;');
  14290. Add(' TA = class');
  14291. Add(' constructor Create; override;');
  14292. Add(' constructor CreateWithC(c: char);');
  14293. Add(' procedure DoIt;');
  14294. Add(' class function DoSome: TObject;');
  14295. Add(' end;');
  14296. Add('constructor tobject.create;');
  14297. Add('begin');
  14298. Add(' inherited; // call non existing ancestor -> ignore silently');
  14299. Add('end;');
  14300. Add('constructor tobject.createwithb(b: boolean);');
  14301. Add('begin');
  14302. Add(' inherited; // call non existing ancestor -> ignore silently');
  14303. Add(' create; // normal call');
  14304. Add('end;');
  14305. Add('constructor ta.create;');
  14306. Add('begin');
  14307. Add(' inherited; // normal call TObject.Create');
  14308. Add(' inherited create; // normal call TObject.Create');
  14309. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14310. Add('end;');
  14311. Add('constructor ta.createwithc(c: char);');
  14312. Add('begin');
  14313. Add(' inherited create; // call TObject.Create');
  14314. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14315. Add(' doit;');
  14316. Add(' doit();');
  14317. Add(' dosome;');
  14318. Add('end;');
  14319. Add('procedure ta.doit;');
  14320. Add('begin');
  14321. Add(' create; // normal call');
  14322. Add(' createwithb(false); // normal call');
  14323. Add(' createwithc(''c''); // normal call');
  14324. Add('end;');
  14325. Add('class function ta.dosome: TObject;');
  14326. Add('begin');
  14327. Add(' Result:=create; // constructor');
  14328. Add(' Result:=createwithb(true); // constructor');
  14329. Add(' Result:=createwithc(''c''); // constructor');
  14330. Add('end;');
  14331. Add('begin');
  14332. ConvertProgram;
  14333. CheckSource('TestClass_CallInheritedConstructor',
  14334. LinesToStr([ // statements
  14335. 'rtl.createClass(this,"TObject",null,function(){',
  14336. ' this.$init = function () {',
  14337. ' };',
  14338. ' this.$final = function () {',
  14339. ' };',
  14340. ' this.Create = function () {',
  14341. ' return this;',
  14342. ' };',
  14343. ' this.CreateWithB = function (b) {',
  14344. ' this.Create();',
  14345. ' return this;',
  14346. ' };',
  14347. '});',
  14348. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14349. ' this.Create = function () {',
  14350. ' $mod.TObject.Create.call(this);',
  14351. ' $mod.TObject.Create.call(this);',
  14352. ' $mod.TObject.CreateWithB.call(this, false);',
  14353. ' return this;',
  14354. ' };',
  14355. ' this.CreateWithC = function (c) {',
  14356. ' $mod.TObject.Create.call(this);',
  14357. ' $mod.TObject.CreateWithB.call(this, true);',
  14358. ' this.DoIt();',
  14359. ' this.DoIt();',
  14360. ' this.$class.DoSome();',
  14361. ' return this;',
  14362. ' };',
  14363. ' this.DoIt = function () {',
  14364. ' this.Create();',
  14365. ' this.CreateWithB(false);',
  14366. ' this.CreateWithC("c");',
  14367. ' };',
  14368. ' this.DoSome = function () {',
  14369. ' var Result = null;',
  14370. ' Result = this.$create("Create");',
  14371. ' Result = this.$create("CreateWithB", [true]);',
  14372. ' Result = this.$create("CreateWithC", ["c"]);',
  14373. ' return Result;',
  14374. ' };',
  14375. '});'
  14376. ]),
  14377. LinesToStr([ // this.$main
  14378. ''
  14379. ]));
  14380. end;
  14381. procedure TTestModule.TestClass_ClassVar_Assign;
  14382. begin
  14383. StartProgram(false);
  14384. Add([
  14385. 'type',
  14386. ' TObject = class',
  14387. ' public',
  14388. ' class var vI: longint;',
  14389. ' class var Sub: TObject;',
  14390. ' constructor Create;',
  14391. ' class function GetIt(var Par: longint): tobject;',
  14392. ' end;',
  14393. 'constructor tobject.create;',
  14394. 'begin',
  14395. ' vi:=vi+1;',
  14396. ' Self.vi:=Self.vi+1;',
  14397. ' inc(vi);',
  14398. 'end;',
  14399. 'class function tobject.getit(var par: longint): tobject;',
  14400. 'begin',
  14401. ' vi:=vi+3;',
  14402. ' Self.vi:=Self.vi+4;',
  14403. ' inc(vi);',
  14404. ' Result:=self.sub;',
  14405. ' GetIt(vi);',
  14406. 'end;',
  14407. 'var Obj: tobject;',
  14408. 'begin',
  14409. ' obj:=tobject.create;',
  14410. ' tobject.vi:=3;',
  14411. ' if tobject.vi=4 then ;',
  14412. ' tobject.sub:=nil;',
  14413. ' obj.sub:=nil;',
  14414. ' obj.sub.sub:=nil;']);
  14415. ConvertProgram;
  14416. CheckSource('TestClass_ClassVar_Assign',
  14417. LinesToStr([ // statements
  14418. 'rtl.createClass(this,"TObject",null,function(){',
  14419. ' this.vI = 0;',
  14420. ' this.Sub = null;',
  14421. ' this.$init = function () {',
  14422. ' };',
  14423. ' this.$final = function () {',
  14424. ' };',
  14425. ' this.Create = function(){',
  14426. ' $mod.TObject.vI = this.vI+1;',
  14427. ' $mod.TObject.vI = this.vI+1;',
  14428. ' $mod.TObject.vI += 1;',
  14429. ' return this;',
  14430. ' };',
  14431. ' this.GetIt = function(Par){',
  14432. ' var Result = null;',
  14433. ' $mod.TObject.vI = this.vI + 3;',
  14434. ' $mod.TObject.vI = this.vI + 4;',
  14435. ' $mod.TObject.vI += 1;',
  14436. ' Result = this.Sub;',
  14437. ' this.GetIt({',
  14438. ' p: $mod.TObject,',
  14439. ' get: function () {',
  14440. ' return this.p.vI;',
  14441. ' },',
  14442. ' set: function (v) {',
  14443. ' this.p.vI = v;',
  14444. ' }',
  14445. ' });',
  14446. ' return Result;',
  14447. ' };',
  14448. '});',
  14449. 'this.Obj = null;'
  14450. ]),
  14451. LinesToStr([ // $mod.$main
  14452. '$mod.Obj = $mod.TObject.$create("Create");',
  14453. '$mod.TObject.vI = 3;',
  14454. 'if ($mod.TObject.vI === 4);',
  14455. '$mod.TObject.Sub=null;',
  14456. '$mod.TObject.Sub=null;',
  14457. '$mod.TObject.Sub=null;',
  14458. '']));
  14459. end;
  14460. procedure TTestModule.TestClass_CallClassMethod;
  14461. begin
  14462. StartProgram(false);
  14463. Add('type');
  14464. Add(' TObject = class');
  14465. Add(' public');
  14466. Add(' class var vI: longint;');
  14467. Add(' class var Sub: TObject;');
  14468. Add(' constructor Create;');
  14469. Add(' function GetMore(Par: longint): longint;');
  14470. Add(' class function GetIt(Par: longint): tobject;');
  14471. Add(' end;');
  14472. Add('constructor tobject.create;');
  14473. Add('begin');
  14474. Add(' sub:=getit(3);');
  14475. Add(' vi:=getmore(4);');
  14476. Add(' sub:=Self.getit(5);');
  14477. Add(' vi:=Self.getmore(6);');
  14478. Add('end;');
  14479. Add('function tobject.getmore(par: longint): longint;');
  14480. Add('begin');
  14481. Add(' sub:=getit(11);');
  14482. Add(' vi:=getmore(12);');
  14483. Add(' sub:=self.getit(13);');
  14484. Add(' vi:=self.getmore(14);');
  14485. Add('end;');
  14486. Add('class function tobject.getit(par: longint): tobject;');
  14487. Add('begin');
  14488. Add(' sub:=getit(21);');
  14489. Add(' vi:=sub.getmore(22);');
  14490. Add(' sub:=self.getit(23);');
  14491. Add(' vi:=self.sub.getmore(24);');
  14492. Add('end;');
  14493. Add('var Obj: tobject;');
  14494. Add('begin');
  14495. Add(' obj:=tobject.create;');
  14496. Add(' tobject.getit(5);');
  14497. Add(' obj.getit(6);');
  14498. Add(' obj.sub.getit(7);');
  14499. Add(' obj.sub.getit(8).SUB:=nil;');
  14500. Add(' obj.sub.getit(9).GETIT(10);');
  14501. Add(' obj.sub.getit(11).SuB.getit(12);');
  14502. ConvertProgram;
  14503. CheckSource('TestClass_CallClassMethod',
  14504. LinesToStr([ // statements
  14505. 'rtl.createClass(this,"TObject",null,function(){',
  14506. ' this.vI = 0;',
  14507. ' this.Sub = null;',
  14508. ' this.$init = function () {',
  14509. ' };',
  14510. ' this.$final = function () {',
  14511. ' };',
  14512. ' this.Create = function(){',
  14513. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14514. ' $mod.TObject.vI = this.GetMore(4);',
  14515. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14516. ' $mod.TObject.vI = this.GetMore(6);',
  14517. ' return this;',
  14518. ' };',
  14519. ' this.GetMore = function(Par){',
  14520. ' var Result = 0;',
  14521. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14522. ' $mod.TObject.vI = this.GetMore(12);',
  14523. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14524. ' $mod.TObject.vI = this.GetMore(14);',
  14525. ' return Result;',
  14526. ' };',
  14527. ' this.GetIt = function(Par){',
  14528. ' var Result = null;',
  14529. ' $mod.TObject.Sub = this.GetIt(21);',
  14530. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14531. ' $mod.TObject.Sub = this.GetIt(23);',
  14532. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14533. ' return Result;',
  14534. ' };',
  14535. '});',
  14536. 'this.Obj = null;'
  14537. ]),
  14538. LinesToStr([ // $mod.$main
  14539. '$mod.Obj = $mod.TObject.$create("Create");',
  14540. '$mod.TObject.GetIt(5);',
  14541. '$mod.Obj.$class.GetIt(6);',
  14542. '$mod.Obj.Sub.$class.GetIt(7);',
  14543. '$mod.TObject.Sub=null;',
  14544. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14545. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14546. '']));
  14547. end;
  14548. procedure TTestModule.TestClass_CallClassMethodStatic;
  14549. begin
  14550. StartProgram(false);
  14551. Add([
  14552. 'type',
  14553. ' TObject = class',
  14554. ' public',
  14555. ' class function Fly: tobject; static;',
  14556. ' end;',
  14557. 'class function tobject.Fly: tobject;',
  14558. 'begin',
  14559. ' Result.Fly;',
  14560. ' Result.Fly();',
  14561. ' Fly;',
  14562. ' Fly();',
  14563. ' Fly.Fly;',
  14564. ' Fly.Fly();',
  14565. 'end;',
  14566. 'var Obj: tobject;',
  14567. 'begin',
  14568. ' obj.Fly;',
  14569. ' obj.Fly();',
  14570. ' with obj do begin',
  14571. ' Fly;',
  14572. ' Fly();',
  14573. ' end;',
  14574. '']);
  14575. ConvertProgram;
  14576. CheckSource('TestClass_CallClassMethodStatic',
  14577. LinesToStr([ // statements
  14578. 'rtl.createClass(this, "TObject", null, function () {',
  14579. ' this.$init = function () {',
  14580. ' };',
  14581. ' this.$final = function () {',
  14582. ' };',
  14583. ' this.Fly = function () {',
  14584. ' var Result = null;',
  14585. ' $mod.TObject.Fly();',
  14586. ' $mod.TObject.Fly();',
  14587. ' $mod.TObject.Fly();',
  14588. ' $mod.TObject.Fly();',
  14589. ' $mod.TObject.Fly();',
  14590. ' $mod.TObject.Fly();',
  14591. ' return Result;',
  14592. ' };',
  14593. '});',
  14594. 'this.Obj = null;'
  14595. ]),
  14596. LinesToStr([ // $mod.$main
  14597. '$mod.TObject.Fly();',
  14598. '$mod.TObject.Fly();',
  14599. 'var $with = $mod.Obj;',
  14600. '$with.Fly();',
  14601. '$with.Fly();',
  14602. '']));
  14603. end;
  14604. procedure TTestModule.TestClass_Property;
  14605. begin
  14606. StartProgram(false);
  14607. Add('type');
  14608. Add(' TObject = class');
  14609. Add(' Fx: longint;');
  14610. Add(' Fy: longint;');
  14611. Add(' function GetInt: longint;');
  14612. Add(' procedure SetInt(Value: longint);');
  14613. Add(' procedure DoIt;');
  14614. Add(' property IntA: longint read Fx write Fy;');
  14615. Add(' property IntB: longint read GetInt write SetInt;');
  14616. Add(' end;');
  14617. Add('function tobject.getint: longint;');
  14618. Add('begin');
  14619. Add(' result:=fx;');
  14620. Add('end;');
  14621. Add('procedure tobject.setint(value: longint);');
  14622. Add('begin');
  14623. Add(' if value=fy then exit;');
  14624. Add(' fy:=value;');
  14625. Add('end;');
  14626. Add('procedure tobject.doit;');
  14627. Add('begin');
  14628. Add(' IntA:=IntA+1;');
  14629. Add(' Self.IntA:=Self.IntA+1;');
  14630. Add(' IntB:=IntB+1;');
  14631. Add(' Self.IntB:=Self.IntB+1;');
  14632. Add('end;');
  14633. Add('var Obj: tobject;');
  14634. Add('begin');
  14635. Add(' obj.inta:=obj.inta+1;');
  14636. Add(' if obj.intb=2 then;');
  14637. Add(' obj.intb:=obj.intb+2;');
  14638. Add(' obj.setint(obj.inta);');
  14639. ConvertProgram;
  14640. CheckSource('TestClass_Property',
  14641. LinesToStr([ // statements
  14642. 'rtl.createClass(this, "TObject", null, function () {',
  14643. ' this.$init = function () {',
  14644. ' this.Fx = 0;',
  14645. ' this.Fy = 0;',
  14646. ' };',
  14647. ' this.$final = function () {',
  14648. ' };',
  14649. ' this.GetInt = function () {',
  14650. ' var Result = 0;',
  14651. ' Result = this.Fx;',
  14652. ' return Result;',
  14653. ' };',
  14654. ' this.SetInt = function (Value) {',
  14655. ' if (Value === this.Fy) return;',
  14656. ' this.Fy = Value;',
  14657. ' };',
  14658. ' this.DoIt = function () {',
  14659. ' this.Fy = this.Fx + 1;',
  14660. ' this.Fy = this.Fx + 1;',
  14661. ' this.SetInt(this.GetInt() + 1);',
  14662. ' this.SetInt(this.GetInt() + 1);',
  14663. ' };',
  14664. '});',
  14665. 'this.Obj = null;'
  14666. ]),
  14667. LinesToStr([ // $mod.$main
  14668. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14669. 'if ($mod.Obj.GetInt() === 2);',
  14670. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14671. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14672. ]));
  14673. end;
  14674. procedure TTestModule.TestClass_Property_ClassMethod;
  14675. begin
  14676. StartProgram(false);
  14677. Add([
  14678. 'type',
  14679. ' TObject = class',
  14680. ' class var Fx: longint;',
  14681. ' class var Fy: longint;',
  14682. ' class function GetInt: longint;',
  14683. ' class procedure SetInt(Value: longint);',
  14684. ' end;',
  14685. ' TBird = class',
  14686. ' class procedure DoIt;',
  14687. ' class property IntA: longint read Fx write Fy;',
  14688. ' class property IntB: longint read GetInt write SetInt;',
  14689. ' end;',
  14690. 'class function tobject.getint: longint;',
  14691. 'begin',
  14692. ' result:=fx;',
  14693. 'end;',
  14694. 'class procedure tobject.setint(value: longint);',
  14695. 'begin',
  14696. 'end;',
  14697. 'class procedure tbird.doit;',
  14698. 'begin',
  14699. ' FX:=3;',
  14700. ' IntA:=IntA+1;',
  14701. ' Self.IntA:=Self.IntA+1;',
  14702. ' IntB:=IntB+1;',
  14703. ' Self.IntB:=Self.IntB+1;',
  14704. ' with Self do begin',
  14705. ' FX:=11;',
  14706. ' IntA:=IntA+12;',
  14707. ' IntB:=IntB+13;',
  14708. ' end;',
  14709. 'end;',
  14710. 'var Obj: tbird;',
  14711. 'begin',
  14712. ' tbird.fx:=tbird.fx+1;',
  14713. ' tbird.inta:=tbird.inta+1;',
  14714. ' if tbird.intb=2 then;',
  14715. ' tbird.intb:=tbird.intb+2;',
  14716. ' tbird.setint(tbird.inta);',
  14717. ' obj.inta:=obj.inta+1;',
  14718. ' if obj.intb=2 then;',
  14719. ' obj.intb:=obj.intb+2;',
  14720. ' obj.setint(obj.inta);',
  14721. ' with Tbird do begin',
  14722. ' FX:=FY+1;',
  14723. ' inta:=inta+2;',
  14724. ' intb:=intb+3;',
  14725. ' end;',
  14726. ' with Obj do begin',
  14727. ' FX:=FY+1;',
  14728. ' inta:=inta+2;',
  14729. ' intb:=intb+3;',
  14730. ' end;',
  14731. '']);
  14732. ConvertProgram;
  14733. CheckSource('TestClass_Property_ClassMethod',
  14734. LinesToStr([ // statements
  14735. 'rtl.createClass(this, "TObject", null, function () {',
  14736. ' this.Fx = 0;',
  14737. ' this.Fy = 0;',
  14738. ' this.$init = function () {',
  14739. ' };',
  14740. ' this.$final = function () {',
  14741. ' };',
  14742. ' this.GetInt = function () {',
  14743. ' var Result = 0;',
  14744. ' Result = this.Fx;',
  14745. ' return Result;',
  14746. ' };',
  14747. ' this.SetInt = function (Value) {',
  14748. ' };',
  14749. '});',
  14750. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14751. ' this.DoIt = function () {',
  14752. ' $mod.TObject.Fx = 3;',
  14753. ' $mod.TObject.Fy = this.Fx + 1;',
  14754. ' $mod.TObject.Fy = this.Fx + 1;',
  14755. ' this.SetInt(this.GetInt() + 1);',
  14756. ' this.SetInt(this.GetInt() + 1);',
  14757. ' $mod.TObject.Fx = 11;',
  14758. ' $mod.TObject.Fy = this.Fx + 12;',
  14759. ' this.SetInt(this.GetInt() + 13);',
  14760. ' };',
  14761. '});',
  14762. 'this.Obj = null;'
  14763. ]),
  14764. LinesToStr([ // $mod.$main
  14765. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14766. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14767. 'if ($mod.TBird.GetInt() === 2);',
  14768. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14769. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14770. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14771. 'if ($mod.Obj.$class.GetInt() === 2);',
  14772. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14773. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14774. 'var $with = $mod.TBird;',
  14775. '$mod.TObject.Fx = $with.Fy + 1;',
  14776. '$mod.TObject.Fy = $with.Fx + 2;',
  14777. '$with.SetInt($with.GetInt() + 3);',
  14778. 'var $with1 = $mod.Obj;',
  14779. '$mod.TObject.Fx = $with1.Fy + 1;',
  14780. '$mod.TObject.Fy = $with1.Fx + 2;',
  14781. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14782. '']));
  14783. end;
  14784. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14785. begin
  14786. StartProgram(false);
  14787. Add([
  14788. 'type',
  14789. ' TObject = class',
  14790. ' class function GetInt: longint; static;',
  14791. ' class procedure SetInt(Value: longint); static;',
  14792. ' class function GetItems(Index: word): longint; static;',
  14793. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14794. ' end;',
  14795. ' TBird = class',
  14796. ' class procedure Fly;',
  14797. ' class property IntA: longint read GetInt write SetInt;',
  14798. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14799. ' end;',
  14800. 'class function tobject.getint: longint;',
  14801. 'begin',
  14802. 'end;',
  14803. 'class procedure tobject.setint(value: longint);',
  14804. 'begin',
  14805. 'end;',
  14806. 'class function tobject.GetItems(Index: word): longint;',
  14807. 'begin',
  14808. 'end;',
  14809. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14810. 'begin',
  14811. 'end;',
  14812. 'class procedure tbird.fly;',
  14813. 'var w: longint;',
  14814. 'begin',
  14815. ' inta:=inta+51;',
  14816. ' w:=items[52];',
  14817. ' items[53]:=54;',
  14818. 'end;',
  14819. 'var Obj: tbird;',
  14820. ' i: longint;',
  14821. 'begin',
  14822. ' tbird.inta:=tbird.inta+1;',
  14823. ' i:=tbird.items[2];',
  14824. ' tbird.items[3]:=4;',
  14825. ' obj.inta:=obj.inta+11;',
  14826. ' i:=obj.items[12];',
  14827. ' obj.items[13]:=14;',
  14828. ' with Tbird do begin',
  14829. ' inta:=inta+21;',
  14830. ' i:=items[22];',
  14831. ' items[23]:=24;',
  14832. ' end;',
  14833. ' with Obj do begin',
  14834. ' inta:=inta+31;',
  14835. ' i:=items[32];',
  14836. ' items[33]:=34;',
  14837. ' end;',
  14838. '']);
  14839. ConvertProgram;
  14840. CheckSource('TestClass_Property_ClassMethod',
  14841. LinesToStr([ // statements
  14842. 'rtl.createClass(this, "TObject", null, function () {',
  14843. ' this.$init = function () {',
  14844. ' };',
  14845. ' this.$final = function () {',
  14846. ' };',
  14847. ' this.GetInt = function () {',
  14848. ' var Result = 0;',
  14849. ' return Result;',
  14850. ' };',
  14851. ' this.SetInt = function (Value) {',
  14852. ' };',
  14853. ' this.GetItems = function (Index) {',
  14854. ' var Result = 0;',
  14855. ' return Result;',
  14856. ' };',
  14857. ' this.SetItems = function (Index, Value) {',
  14858. ' };',
  14859. '});',
  14860. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14861. ' this.Fly = function () {',
  14862. ' var w = 0;',
  14863. ' this.SetInt(this.GetInt() + 51);',
  14864. ' w = this.GetItems(52);',
  14865. ' this.SetItems(53, 54);',
  14866. ' };',
  14867. '});',
  14868. 'this.Obj = null;',
  14869. 'this.i = 0;',
  14870. '']),
  14871. LinesToStr([ // $mod.$main
  14872. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14873. '$mod.i = $mod.TObject.GetItems(2);',
  14874. '$mod.TObject.SetItems(3, 4);',
  14875. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14876. '$mod.i = $mod.TObject.GetItems(12);',
  14877. '$mod.TObject.SetItems(13, 14);',
  14878. 'var $with = $mod.TBird;',
  14879. '$with.SetInt($with.GetInt() + 21);',
  14880. '$mod.i = $with.GetItems(22);',
  14881. '$with.SetItems(23, 24);',
  14882. 'var $with1 = $mod.Obj;',
  14883. '$with1.SetInt($with1.GetInt() + 31);',
  14884. '$mod.i = $with1.GetItems(32);',
  14885. '$with1.SetItems(33, 34);',
  14886. '']));
  14887. end;
  14888. procedure TTestModule.TestClass_Property_Indexed;
  14889. begin
  14890. StartProgram(false);
  14891. Add([
  14892. 'type',
  14893. ' TObject = class',
  14894. ' FItems: array of longint;',
  14895. ' function GetItems(Index: longint): longint;',
  14896. ' procedure SetItems(Index: longint; Value: longint);',
  14897. ' procedure DoIt;',
  14898. ' property Items[Index: longint]: longint read getitems write setitems;',
  14899. ' end;',
  14900. 'function tobject.getitems(index: longint): longint;',
  14901. 'begin',
  14902. ' Result:=fitems[index];',
  14903. 'end;',
  14904. 'procedure tobject.setitems(index: longint; value: longint);',
  14905. 'begin',
  14906. ' fitems[index]:=value;',
  14907. 'end;',
  14908. 'procedure tobject.doit;',
  14909. 'begin',
  14910. ' items[1]:=2;',
  14911. ' items[3]:=items[4];',
  14912. ' self.items[5]:=self.items[6];',
  14913. ' items[items[7]]:=items[items[8]];',
  14914. 'end;',
  14915. 'var Obj: tobject;',
  14916. 'begin',
  14917. ' obj.Items[11]:=obj.Items[12];',
  14918. '']);
  14919. ConvertProgram;
  14920. CheckSource('TestClass_Property_Indexed',
  14921. LinesToStr([ // statements
  14922. 'rtl.createClass(this, "TObject", null, function () {',
  14923. ' this.$init = function () {',
  14924. ' this.FItems = [];',
  14925. ' };',
  14926. ' this.$final = function () {',
  14927. ' this.FItems = undefined;',
  14928. ' };',
  14929. ' this.GetItems = function (Index) {',
  14930. ' var Result = 0;',
  14931. ' Result = this.FItems[Index];',
  14932. ' return Result;',
  14933. ' };',
  14934. ' this.SetItems = function (Index, Value) {',
  14935. ' this.FItems[Index] = Value;',
  14936. ' };',
  14937. ' this.DoIt = function () {',
  14938. ' this.SetItems(1, 2);',
  14939. ' this.SetItems(3,this.GetItems(4));',
  14940. ' this.SetItems(5,this.GetItems(6));',
  14941. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14942. ' };',
  14943. '});',
  14944. 'this.Obj = null;'
  14945. ]),
  14946. LinesToStr([ // $mod.$main
  14947. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14948. ]));
  14949. end;
  14950. procedure TTestModule.TestClass_Property_IndexSpec;
  14951. begin
  14952. StartProgram(false);
  14953. Add([
  14954. 'type',
  14955. ' TEnum = (red, blue);',
  14956. ' TObject = class',
  14957. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14958. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14959. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14960. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14961. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14962. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14963. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14964. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14965. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14966. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14967. ' end;',
  14968. 'procedure DoIt(b: boolean); begin end;',
  14969. 'var',
  14970. ' o: TObject;',
  14971. 'begin',
  14972. ' o.B1:=o.B1;',
  14973. ' o.B2:=o.B2;',
  14974. ' o.B3:=o.B3;',
  14975. ' o.I1[''a'']:=o.I1[''b''];',
  14976. ' doit(o.b1);',
  14977. ' doit(o.b2);',
  14978. ' doit(o.i1[''c'']);',
  14979. '']);
  14980. ConvertProgram;
  14981. CheckSource('TestClass_Property_IndexSpec',
  14982. LinesToStr([ // statements
  14983. 'this.TEnum = {',
  14984. ' "0": "red",',
  14985. ' red: 0,',
  14986. ' "1": "blue",',
  14987. ' blue: 1',
  14988. '};',
  14989. 'rtl.createClass(this, "TObject", null, function () {',
  14990. ' this.$init = function () {',
  14991. ' };',
  14992. ' this.$final = function () {',
  14993. ' };',
  14994. '});',
  14995. 'this.DoIt = function (b) {',
  14996. '};',
  14997. 'this.o = null;',
  14998. '']),
  14999. LinesToStr([ // $mod.$main
  15000. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15001. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15002. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15003. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15004. '$mod.DoIt($mod.o.GetIntBool(1));',
  15005. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15006. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15007. '']));
  15008. end;
  15009. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15010. begin
  15011. StartProgram(false);
  15012. Add('type');
  15013. Add(' TArray = array of longint;');
  15014. Add(' TObject = class');
  15015. Add(' FItems: TArray;');
  15016. Add(' function GetItems: tarray;');
  15017. Add(' procedure SetItems(Value: tarray);');
  15018. Add(' property Items: tarray read getitems write setitems;');
  15019. Add(' procedure SetNumbers(const Value: tarray);');
  15020. Add(' property Numbers: tarray write setnumbers;');
  15021. Add(' end;');
  15022. Add('function tobject.getitems: tarray;');
  15023. Add('begin');
  15024. Add(' Result:=fitems;');
  15025. Add('end;');
  15026. Add('procedure tobject.setitems(value: tarray);');
  15027. Add('begin');
  15028. Add(' fitems:=value;');
  15029. Add(' fitems:=nil;');
  15030. Add(' Items:=nil;');
  15031. Add(' Items:=Items;');
  15032. Add(' Items[1]:=2;');
  15033. Add(' fitems[3]:=Items[4];');
  15034. Add(' Items[5]:=Items[6];');
  15035. Add(' Self.Items[7]:=8;');
  15036. Add(' Self.Items[9]:=Self.Items[10];');
  15037. Add(' Items[Items[11]]:=Items[Items[12]];');
  15038. Add('end;');
  15039. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15040. Add('begin;');
  15041. Add(' Numbers:=nil;');
  15042. Add(' Numbers:=Value;');
  15043. Add(' Self.Numbers:=Value;');
  15044. Add('end;');
  15045. Add('var Obj: tobject;');
  15046. Add('begin');
  15047. Add(' obj.items:=nil;');
  15048. Add(' obj.items:=obj.items;');
  15049. Add(' obj.items[11]:=obj.items[12];');
  15050. ConvertProgram;
  15051. CheckSource('TestClass_PropertyOfTypeArray',
  15052. LinesToStr([ // statements
  15053. 'rtl.createClass(this, "TObject", null, function () {',
  15054. ' this.$init = function () {',
  15055. ' this.FItems = [];',
  15056. ' };',
  15057. ' this.$final = function () {',
  15058. ' this.FItems = undefined;',
  15059. ' };',
  15060. ' this.GetItems = function () {',
  15061. ' var Result = [];',
  15062. ' Result = rtl.arrayRef(this.FItems);',
  15063. ' return Result;',
  15064. ' };',
  15065. ' this.SetItems = function (Value) {',
  15066. ' this.FItems = rtl.arrayRef(Value);',
  15067. ' this.FItems = [];',
  15068. ' this.SetItems([]);',
  15069. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15070. ' this.GetItems()[1] = 2;',
  15071. ' this.FItems[3] = this.GetItems()[4];',
  15072. ' this.GetItems()[5] = this.GetItems()[6];',
  15073. ' this.GetItems()[7] = 8;',
  15074. ' this.GetItems()[9] = this.GetItems()[10];',
  15075. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15076. ' };',
  15077. ' this.SetNumbers = function (Value) {',
  15078. ' this.SetNumbers([]);',
  15079. ' this.SetNumbers(Value);',
  15080. ' this.SetNumbers(Value);',
  15081. ' };',
  15082. '});',
  15083. 'this.Obj = null;'
  15084. ]),
  15085. LinesToStr([ // $mod.$main
  15086. '$mod.Obj.SetItems([]);',
  15087. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15088. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15089. ]));
  15090. end;
  15091. procedure TTestModule.TestClass_PropertyDefault;
  15092. begin
  15093. StartProgram(false);
  15094. Add([
  15095. 'type',
  15096. ' TArray = array of longint;',
  15097. ' TObject = class',
  15098. ' end;',
  15099. ' TBird = class',
  15100. ' FItems: TArray;',
  15101. ' function GetItems(Index: longint): longint;',
  15102. ' procedure SetItems(Index, Value: longint);',
  15103. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15104. ' end;',
  15105. 'function TBird.getitems(index: longint): longint;',
  15106. 'begin',
  15107. 'end;',
  15108. 'procedure TBird.setitems(index, value: longint);',
  15109. 'begin',
  15110. ' Self[1]:=2;',
  15111. ' Self[3]:=Self[index];',
  15112. ' Self[index]:=Self[Self[value]];',
  15113. ' Self[Self[4]]:=value;',
  15114. 'end;',
  15115. 'var',
  15116. ' Bird: TBird;',
  15117. ' Obj: TObject;',
  15118. 'begin',
  15119. ' bird[11]:=12;',
  15120. ' bird[13]:=bird[14];',
  15121. ' bird[Bird[15]]:=bird[Bird[15]];',
  15122. ' TBird(obj)[16]:=TBird(obj)[17];',
  15123. ' (obj as tbird)[18]:=19;',
  15124. '']);
  15125. ConvertProgram;
  15126. CheckSource('TestClass_PropertyDefault',
  15127. LinesToStr([ // statements
  15128. 'rtl.createClass(this, "TObject", null, function () {',
  15129. ' this.$init = function () {',
  15130. ' };',
  15131. ' this.$final = function () {',
  15132. ' };',
  15133. '});',
  15134. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15135. ' this.$init = function () {',
  15136. ' $mod.TObject.$init.call(this);',
  15137. ' this.FItems = [];',
  15138. ' };',
  15139. ' this.$final = function () {',
  15140. ' this.FItems = undefined;',
  15141. ' $mod.TObject.$final.call(this);',
  15142. ' };',
  15143. ' this.GetItems = function (Index) {',
  15144. ' var Result = 0;',
  15145. ' return Result;',
  15146. ' };',
  15147. ' this.SetItems = function (Index, Value) {',
  15148. ' this.SetItems(1, 2);',
  15149. ' this.SetItems(3, this.GetItems(Index));',
  15150. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15151. ' this.SetItems(this.GetItems(4), Value);',
  15152. ' };',
  15153. '});',
  15154. 'this.Bird = null;',
  15155. 'this.Obj = null;',
  15156. '']),
  15157. LinesToStr([ // $mod.$main
  15158. '$mod.Bird.SetItems(11, 12);',
  15159. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15160. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15161. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15162. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15163. '']));
  15164. end;
  15165. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15166. begin
  15167. StartProgram(false);
  15168. Add([
  15169. 'type',
  15170. ' TObject = class end;',
  15171. ' TAlphaList = class',
  15172. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15173. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15174. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15175. ' end;',
  15176. ' TBetaList = class',
  15177. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15178. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15179. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15180. ' end;',
  15181. ' TBird = class',
  15182. ' procedure DoIt;',
  15183. ' end;',
  15184. 'procedure TBird.DoIt;',
  15185. 'var',
  15186. ' List: TAlphaList;',
  15187. 'begin',
  15188. ' if TBetaList(List[true])[3]=nil then ;',
  15189. ' TBetaList(List[false])[5]:=nil;',
  15190. 'end;',
  15191. 'var',
  15192. ' List: TAlphaList;',
  15193. 'begin',
  15194. ' if TBetaList(List[true])[3]=nil then ;',
  15195. ' TBetaList(List[false])[5]:=nil;',
  15196. '']);
  15197. ConvertProgram;
  15198. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15199. LinesToStr([ // statements
  15200. 'rtl.createClass(this, "TObject", null, function () {',
  15201. ' this.$init = function () {',
  15202. ' };',
  15203. ' this.$final = function () {',
  15204. ' };',
  15205. '});',
  15206. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15207. '});',
  15208. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15209. '});',
  15210. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15211. ' this.DoIt = function () {',
  15212. ' var List = null;',
  15213. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15214. ' List.GetAlphas(false).SetBetas(5, null);',
  15215. ' };',
  15216. '});',
  15217. 'this.List = null;',
  15218. '']),
  15219. LinesToStr([ // $mod.$main
  15220. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15221. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15222. '']));
  15223. end;
  15224. procedure TTestModule.TestClass_PropertyOverride;
  15225. begin
  15226. StartProgram(false);
  15227. Add('type');
  15228. Add(' integer = longint;');
  15229. Add(' TObject = class');
  15230. Add(' FItem: integer;');
  15231. Add(' function GetItem: integer; external name ''GetItem'';');
  15232. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15233. Add(' property Item: integer read getitem write setitem;');
  15234. Add(' end;');
  15235. Add(' TCar = class');
  15236. Add(' FBag: integer;');
  15237. Add(' function GetBag: integer; external name ''GetBag'';');
  15238. Add(' property Item read getbag;');
  15239. Add(' end;');
  15240. Add('var');
  15241. Add(' Obj: tobject;');
  15242. Add(' Car: tcar;');
  15243. Add('begin');
  15244. Add(' Obj.Item:=Obj.Item;');
  15245. Add(' Car.Item:=Car.Item;');
  15246. ConvertProgram;
  15247. CheckSource('TestClass_PropertyOverride',
  15248. LinesToStr([ // statements
  15249. 'rtl.createClass(this, "TObject", null, function () {',
  15250. ' this.$init = function () {',
  15251. ' this.FItem = 0;',
  15252. ' };',
  15253. ' this.$final = function () {',
  15254. ' };',
  15255. '});',
  15256. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15257. ' this.$init = function () {',
  15258. ' $mod.TObject.$init.call(this);',
  15259. ' this.FBag = 0;',
  15260. ' };',
  15261. '});',
  15262. 'this.Obj = null;',
  15263. 'this.Car = null;',
  15264. '']),
  15265. LinesToStr([ // $mod.$main
  15266. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15267. '$mod.Car.SetItem($mod.Car.GetBag());',
  15268. '']));
  15269. end;
  15270. procedure TTestModule.TestClass_PropertyIncVisibility;
  15271. begin
  15272. AddModuleWithIntfImplSrc('unit1.pp',
  15273. LinesToStr([
  15274. 'type',
  15275. ' TNumber = longint;',
  15276. ' TInteger = longint;',
  15277. ' TObject = class',
  15278. ' private',
  15279. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15280. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15281. ' protected',
  15282. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15283. ' end;']),
  15284. LinesToStr([
  15285. '']));
  15286. StartProgram(true);
  15287. Add([
  15288. 'uses unit1;',
  15289. 'type',
  15290. ' TBird = class',
  15291. ' public',
  15292. ' property Items;',
  15293. ' end;',
  15294. 'procedure DoIt(i: TInteger);',
  15295. 'begin',
  15296. 'end;',
  15297. 'var b: TBird;',
  15298. 'begin',
  15299. ' b.Items[1]:=2;',
  15300. ' b.Items[3]:=b.Items[4];',
  15301. ' DoIt(b.Items[5]);',
  15302. '']);
  15303. ConvertProgram;
  15304. CheckSource('TestClass_PropertyIncVisibility',
  15305. LinesToStr([ // statements
  15306. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15307. '});',
  15308. 'this.DoIt = function (i) {',
  15309. '};',
  15310. 'this.b = null;'
  15311. ]),
  15312. LinesToStr([ // $mod.$main
  15313. '$mod.b.SetItems(1, 2);',
  15314. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15315. '$mod.DoIt($mod.b.GetItems(5));'
  15316. ]));
  15317. end;
  15318. procedure TTestModule.TestClass_Assigned;
  15319. begin
  15320. StartProgram(false);
  15321. Add('type');
  15322. Add(' TObject = class');
  15323. Add(' end;');
  15324. Add('var');
  15325. Add(' Obj: tobject;');
  15326. Add(' b: boolean;');
  15327. Add('begin');
  15328. Add(' if Assigned(obj) then ;');
  15329. Add(' b:=Assigned(obj) or false;');
  15330. ConvertProgram;
  15331. CheckSource('TestClass_Assigned',
  15332. LinesToStr([ // statements
  15333. 'rtl.createClass(this, "TObject", null, function () {',
  15334. ' this.$init = function () {',
  15335. ' };',
  15336. ' this.$final = function () {',
  15337. ' };',
  15338. '});',
  15339. 'this.Obj = null;',
  15340. 'this.b = false;'
  15341. ]),
  15342. LinesToStr([ // $mod.$main
  15343. 'if ($mod.Obj != null);',
  15344. '$mod.b = ($mod.Obj != null) || false;'
  15345. ]));
  15346. end;
  15347. procedure TTestModule.TestClass_WithClassDoCreate;
  15348. begin
  15349. StartProgram(false);
  15350. Add('type');
  15351. Add(' TObject = class');
  15352. Add(' aBool: boolean;');
  15353. Add(' Arr: array of boolean;');
  15354. Add(' constructor Create;');
  15355. Add(' end;');
  15356. Add('constructor TObject.Create; begin end;');
  15357. Add('var');
  15358. Add(' Obj: tobject;');
  15359. Add(' b: boolean;');
  15360. Add('begin');
  15361. Add(' with tobject.create do begin');
  15362. Add(' b:=abool;');
  15363. Add(' abool:=b;');
  15364. Add(' b:=arr[1];');
  15365. Add(' arr[2]:=b;');
  15366. Add(' end;');
  15367. Add(' with tobject do');
  15368. Add(' obj:=create;');
  15369. Add(' with obj do begin');
  15370. Add(' create;');
  15371. Add(' b:=abool;');
  15372. Add(' abool:=b;');
  15373. Add(' b:=arr[3];');
  15374. Add(' arr[4]:=b;');
  15375. Add(' end;');
  15376. ConvertProgram;
  15377. CheckSource('TestClass_WithClassDoCreate',
  15378. LinesToStr([ // statements
  15379. 'rtl.createClass(this, "TObject", null, function () {',
  15380. ' this.$init = function () {',
  15381. ' this.aBool = false;',
  15382. ' this.Arr = [];',
  15383. ' };',
  15384. ' this.$final = function () {',
  15385. ' this.Arr = undefined;',
  15386. ' };',
  15387. ' this.Create = function () {',
  15388. ' return this;',
  15389. ' };',
  15390. '});',
  15391. 'this.Obj = null;',
  15392. 'this.b = false;'
  15393. ]),
  15394. LinesToStr([ // $mod.$main
  15395. 'var $with = $mod.TObject.$create("Create");',
  15396. '$mod.b = $with.aBool;',
  15397. '$with.aBool = $mod.b;',
  15398. '$mod.b = $with.Arr[1];',
  15399. '$with.Arr[2] = $mod.b;',
  15400. 'var $with1 = $mod.TObject;',
  15401. '$mod.Obj = $with1.$create("Create");',
  15402. 'var $with2 = $mod.Obj;',
  15403. '$with2.Create();',
  15404. '$mod.b = $with2.aBool;',
  15405. '$with2.aBool = $mod.b;',
  15406. '$mod.b = $with2.Arr[3];',
  15407. '$with2.Arr[4] = $mod.b;',
  15408. '']));
  15409. end;
  15410. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15411. begin
  15412. StartProgram(false);
  15413. Add('type');
  15414. Add(' TObject = class');
  15415. Add(' FInt: longint;');
  15416. Add(' constructor Create;');
  15417. Add(' function GetSize: longint;');
  15418. Add(' procedure SetSize(Value: longint);');
  15419. Add(' property Int: longint read FInt write FInt;');
  15420. Add(' property Size: longint read GetSize write SetSize;');
  15421. Add(' end;');
  15422. Add('constructor TObject.Create; begin end;');
  15423. Add('function TObject.GetSize: longint; begin; end;');
  15424. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15425. Add('var');
  15426. Add(' Obj: tobject;');
  15427. Add(' i: longint;');
  15428. Add('begin');
  15429. Add(' with TObject.Create do begin');
  15430. Add(' i:=int;');
  15431. Add(' int:=i;');
  15432. Add(' i:=size;');
  15433. Add(' size:=i;');
  15434. Add(' end;');
  15435. Add(' with obj do begin');
  15436. Add(' i:=int;');
  15437. Add(' int:=i;');
  15438. Add(' i:=size;');
  15439. Add(' size:=i;');
  15440. Add(' end;');
  15441. ConvertProgram;
  15442. CheckSource('TestClass_WithClassInstDoProperty',
  15443. LinesToStr([ // statements
  15444. 'rtl.createClass(this, "TObject", null, function () {',
  15445. ' this.$init = function () {',
  15446. ' this.FInt = 0;',
  15447. ' };',
  15448. ' this.$final = function () {',
  15449. ' };',
  15450. ' this.Create = function () {',
  15451. ' return this;',
  15452. ' };',
  15453. ' this.GetSize = function () {',
  15454. ' var Result = 0;',
  15455. ' return Result;',
  15456. ' };',
  15457. ' this.SetSize = function (Value) {',
  15458. ' };',
  15459. '});',
  15460. 'this.Obj = null;',
  15461. 'this.i = 0;'
  15462. ]),
  15463. LinesToStr([ // $mod.$main
  15464. 'var $with = $mod.TObject.$create("Create");',
  15465. '$mod.i = $with.FInt;',
  15466. '$with.FInt = $mod.i;',
  15467. '$mod.i = $with.GetSize();',
  15468. '$with.SetSize($mod.i);',
  15469. 'var $with1 = $mod.Obj;',
  15470. '$mod.i = $with1.FInt;',
  15471. '$with1.FInt = $mod.i;',
  15472. '$mod.i = $with1.GetSize();',
  15473. '$with1.SetSize($mod.i);',
  15474. '']));
  15475. end;
  15476. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15477. begin
  15478. StartProgram(false);
  15479. Add('type');
  15480. Add(' TObject = class');
  15481. Add(' constructor Create;');
  15482. Add(' function GetItems(Index: longint): longint;');
  15483. Add(' procedure SetItems(Index, Value: longint);');
  15484. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15485. Add(' end;');
  15486. Add('constructor TObject.Create; begin end;');
  15487. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15488. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15489. Add('var');
  15490. Add(' Obj: tobject;');
  15491. Add(' i: longint;');
  15492. Add('begin');
  15493. Add(' with TObject.Create do begin');
  15494. Add(' i:=Items[1];');
  15495. Add(' Items[2]:=i;');
  15496. Add(' end;');
  15497. Add(' with obj do begin');
  15498. Add(' i:=Items[3];');
  15499. Add(' Items[4]:=i;');
  15500. Add(' end;');
  15501. ConvertProgram;
  15502. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15503. LinesToStr([ // statements
  15504. 'rtl.createClass(this, "TObject", null, function () {',
  15505. ' this.$init = function () {',
  15506. ' };',
  15507. ' this.$final = function () {',
  15508. ' };',
  15509. ' this.Create = function () {',
  15510. ' return this;',
  15511. ' };',
  15512. ' this.GetItems = function (Index) {',
  15513. ' var Result = 0;',
  15514. ' return Result;',
  15515. ' };',
  15516. ' this.SetItems = function (Index, Value) {',
  15517. ' };',
  15518. '});',
  15519. 'this.Obj = null;',
  15520. 'this.i = 0;'
  15521. ]),
  15522. LinesToStr([ // $mod.$main
  15523. 'var $with = $mod.TObject.$create("Create");',
  15524. '$mod.i = $with.GetItems(1);',
  15525. '$with.SetItems(2, $mod.i);',
  15526. 'var $with1 = $mod.Obj;',
  15527. '$mod.i = $with1.GetItems(3);',
  15528. '$with1.SetItems(4, $mod.i);',
  15529. '']));
  15530. end;
  15531. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15532. begin
  15533. StartProgram(false);
  15534. Add('type');
  15535. Add(' TObject = class');
  15536. Add(' constructor Create;');
  15537. Add(' function GetSize: longint;');
  15538. Add(' procedure SetSize(Value: longint);');
  15539. Add(' end;');
  15540. Add('constructor TObject.Create; begin end;');
  15541. Add('function TObject.GetSize: longint; begin; end;');
  15542. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15543. Add('var');
  15544. Add(' Obj: tobject;');
  15545. Add(' i: longint;');
  15546. Add('begin');
  15547. Add(' with TObject.Create do begin');
  15548. Add(' i:=GetSize;');
  15549. Add(' i:=GetSize();');
  15550. Add(' SetSize(i);');
  15551. Add(' end;');
  15552. Add(' with obj do begin');
  15553. Add(' i:=GetSize;');
  15554. Add(' i:=GetSize();');
  15555. Add(' SetSize(i);');
  15556. Add(' end;');
  15557. ConvertProgram;
  15558. CheckSource('TestClass_WithClassInstDoFunc',
  15559. LinesToStr([ // statements
  15560. 'rtl.createClass(this, "TObject", null, function () {',
  15561. ' this.$init = function () {',
  15562. ' };',
  15563. ' this.$final = function () {',
  15564. ' };',
  15565. ' this.Create = function () {',
  15566. ' return this;',
  15567. ' };',
  15568. ' this.GetSize = function () {',
  15569. ' var Result = 0;',
  15570. ' return Result;',
  15571. ' };',
  15572. ' this.SetSize = function (Value) {',
  15573. ' };',
  15574. '});',
  15575. 'this.Obj = null;',
  15576. 'this.i = 0;'
  15577. ]),
  15578. LinesToStr([ // $mod.$main
  15579. 'var $with = $mod.TObject.$create("Create");',
  15580. '$mod.i = $with.GetSize();',
  15581. '$mod.i = $with.GetSize();',
  15582. '$with.SetSize($mod.i);',
  15583. 'var $with1 = $mod.Obj;',
  15584. '$mod.i = $with1.GetSize();',
  15585. '$mod.i = $with1.GetSize();',
  15586. '$with1.SetSize($mod.i);',
  15587. '']));
  15588. end;
  15589. procedure TTestModule.TestClass_TypeCast;
  15590. begin
  15591. StartProgram(false);
  15592. Add('type');
  15593. Add(' TObject = class');
  15594. Add(' Next: TObject;');
  15595. Add(' constructor Create;');
  15596. Add(' end;');
  15597. Add(' TControl = class(TObject)');
  15598. Add(' Arr: array of TObject;');
  15599. Add(' function GetIt(vI: longint = 0): TObject;');
  15600. Add(' end;');
  15601. Add('constructor tobject.create; begin end;');
  15602. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15603. Add('var');
  15604. Add(' Obj: tobject;');
  15605. Add('begin');
  15606. Add(' obj:=tcontrol(obj).next;');
  15607. Add(' tcontrol(obj):=nil;');
  15608. Add(' obj:=tcontrol(obj);');
  15609. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15610. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15611. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15612. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15613. Add(' obj:=tcontrol(nil);');
  15614. ConvertProgram;
  15615. CheckSource('TestClass_TypeCast',
  15616. LinesToStr([ // statements
  15617. 'rtl.createClass(this, "TObject", null, function () {',
  15618. ' this.$init = function () {',
  15619. ' this.Next = null;',
  15620. ' };',
  15621. ' this.$final = function () {',
  15622. ' this.Next = undefined;',
  15623. ' };',
  15624. ' this.Create = function () {',
  15625. ' return this;',
  15626. ' };',
  15627. '});',
  15628. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15629. ' this.$init = function () {',
  15630. ' $mod.TObject.$init.call(this);',
  15631. ' this.Arr = [];',
  15632. ' };',
  15633. ' this.$final = function () {',
  15634. ' this.Arr = undefined;',
  15635. ' $mod.TObject.$final.call(this);',
  15636. ' };',
  15637. ' this.GetIt = function (vI) {',
  15638. ' var Result = null;',
  15639. ' return Result;',
  15640. ' };',
  15641. '});',
  15642. 'this.Obj = null;'
  15643. ]),
  15644. LinesToStr([ // $mod.$main
  15645. '$mod.Obj = $mod.Obj.Next;',
  15646. '$mod.Obj = null;',
  15647. '$mod.Obj = $mod.Obj;',
  15648. '$mod.Obj = $mod.Obj.GetIt(0);',
  15649. '$mod.Obj = $mod.Obj.GetIt(0);',
  15650. '$mod.Obj = $mod.Obj.GetIt(1);',
  15651. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15652. '$mod.Obj = null;',
  15653. '']));
  15654. end;
  15655. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15656. begin
  15657. StartProgram(false);
  15658. Add('type');
  15659. Add(' TObject = class end;');
  15660. Add('procedure ProcA(var A);');
  15661. Add('begin');
  15662. Add(' TObject(A):=nil;');
  15663. Add(' TObject(A):=TObject(A);');
  15664. Add(' if TObject(A)=nil then ;');
  15665. Add(' if nil=TObject(A) then ;');
  15666. Add('end;');
  15667. Add('procedure ProcB(out A);');
  15668. Add('begin');
  15669. Add(' TObject(A):=nil;');
  15670. Add(' TObject(A):=TObject(A);');
  15671. Add(' if TObject(A)=nil then ;');
  15672. Add(' if nil=TObject(A) then ;');
  15673. Add('end;');
  15674. Add('procedure ProcC(const A);');
  15675. Add('begin');
  15676. Add(' if TObject(A)=nil then ;');
  15677. Add(' if nil=TObject(A) then ;');
  15678. Add('end;');
  15679. Add('var o: TObject;');
  15680. Add('begin');
  15681. Add(' ProcA(o);');
  15682. Add(' ProcB(o);');
  15683. Add(' ProcC(o);');
  15684. ConvertProgram;
  15685. CheckSource('TestClass_TypeCastUntypedParam',
  15686. LinesToStr([ // statements
  15687. 'rtl.createClass(this, "TObject", null, function () {',
  15688. ' this.$init = function () {',
  15689. ' };',
  15690. ' this.$final = function () {',
  15691. ' };',
  15692. '});',
  15693. 'this.ProcA = function (A) {',
  15694. ' A.set(null);',
  15695. ' A.set(A.get());',
  15696. ' if (A.get() === null);',
  15697. ' if (null === A.get());',
  15698. '};',
  15699. 'this.ProcB = function (A) {',
  15700. ' A.set(null);',
  15701. ' A.set(A.get());',
  15702. ' if (A.get() === null);',
  15703. ' if (null === A.get());',
  15704. '};',
  15705. 'this.ProcC = function (A) {',
  15706. ' if (A === null);',
  15707. ' if (null === A);',
  15708. '};',
  15709. 'this.o = null;',
  15710. '']),
  15711. LinesToStr([ // $mod.$main
  15712. '$mod.ProcA({',
  15713. ' p: $mod,',
  15714. ' get: function () {',
  15715. ' return this.p.o;',
  15716. ' },',
  15717. ' set: function (v) {',
  15718. ' this.p.o = v;',
  15719. ' }',
  15720. '});',
  15721. '$mod.ProcB({',
  15722. ' p: $mod,',
  15723. ' get: function () {',
  15724. ' return this.p.o;',
  15725. ' },',
  15726. ' set: function (v) {',
  15727. ' this.p.o = v;',
  15728. ' }',
  15729. '});',
  15730. '$mod.ProcC($mod.o);',
  15731. '']));
  15732. end;
  15733. procedure TTestModule.TestClass_Overloads;
  15734. begin
  15735. StartProgram(false);
  15736. Add('type');
  15737. Add(' TObject = class');
  15738. Add(' procedure DoIt;');
  15739. Add(' procedure DoIt(vI: longint);');
  15740. Add(' end;');
  15741. Add('procedure TObject.DoIt;');
  15742. Add('begin');
  15743. Add(' DoIt;');
  15744. Add(' DoIt(1);');
  15745. Add('end;');
  15746. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15747. Add('begin');
  15748. ConvertProgram;
  15749. CheckSource('TestClass_Overloads',
  15750. LinesToStr([ // statements
  15751. 'rtl.createClass(this, "TObject", null, function () {',
  15752. ' this.$init = function () {',
  15753. ' };',
  15754. ' this.$final = function () {',
  15755. ' };',
  15756. ' this.DoIt = function () {',
  15757. ' this.DoIt();',
  15758. ' this.DoIt$1(1);',
  15759. ' };',
  15760. ' this.DoIt$1 = function (vI) {',
  15761. ' };',
  15762. '});',
  15763. '']),
  15764. LinesToStr([ // $mod.$main
  15765. '']));
  15766. end;
  15767. procedure TTestModule.TestClass_OverloadsAncestor;
  15768. begin
  15769. StartProgram(false);
  15770. Add('type');
  15771. Add(' TObject = class;');
  15772. Add(' TObject = class');
  15773. Add(' procedure DoIt(vA: longint);');
  15774. Add(' procedure DoIt(vA, vB: longint);');
  15775. Add(' end;');
  15776. Add(' TCar = class;');
  15777. Add(' TCar = class');
  15778. Add(' procedure DoIt(vA: longint);');
  15779. Add(' procedure DoIt(vA, vB: longint);');
  15780. Add(' end;');
  15781. Add('procedure tobject.doit(va: longint);');
  15782. Add('begin');
  15783. Add(' doit(1);');
  15784. Add(' doit(1,2);');
  15785. Add('end;');
  15786. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15787. Add('procedure tcar.doit(va: longint);');
  15788. Add('begin');
  15789. Add(' doit(1);');
  15790. Add(' doit(1,2);');
  15791. Add(' inherited doit(1);');
  15792. Add(' inherited doit(1,2);');
  15793. Add('end;');
  15794. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15795. Add('begin');
  15796. ConvertProgram;
  15797. CheckSource('TestClass_OverloadsAncestor',
  15798. LinesToStr([ // statements
  15799. 'rtl.createClass(this, "TObject", null, function () {',
  15800. ' this.$init = function () {',
  15801. ' };',
  15802. ' this.$final = function () {',
  15803. ' };',
  15804. ' this.DoIt = function (vA) {',
  15805. ' this.DoIt(1);',
  15806. ' this.DoIt$1(1,2);',
  15807. ' };',
  15808. ' this.DoIt$1 = function (vA, vB) {',
  15809. ' };',
  15810. '});',
  15811. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15812. ' this.DoIt$2 = function (vA) {',
  15813. ' this.DoIt$2(1);',
  15814. ' this.DoIt$3(1, 2);',
  15815. ' $mod.TObject.DoIt.call(this, 1);',
  15816. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15817. ' };',
  15818. ' this.DoIt$3 = function (vA, vB) {',
  15819. ' };',
  15820. '});',
  15821. '']),
  15822. LinesToStr([ // $mod.$main
  15823. '']));
  15824. end;
  15825. procedure TTestModule.TestClass_OverloadConstructor;
  15826. begin
  15827. StartProgram(false);
  15828. Add('type');
  15829. Add(' TObject = class');
  15830. Add(' constructor Create(vA: longint);');
  15831. Add(' constructor Create(vA, vB: longint);');
  15832. Add(' end;');
  15833. Add(' TCar = class');
  15834. Add(' constructor Create(vA: longint);');
  15835. Add(' constructor Create(vA, vB: longint);');
  15836. Add(' end;');
  15837. Add('constructor tobject.create(va: longint);');
  15838. Add('begin');
  15839. Add(' create(1);');
  15840. Add(' create(1,2);');
  15841. Add('end;');
  15842. Add('constructor tobject.create(va, vb: longint); begin end;');
  15843. Add('constructor tcar.create(va: longint);');
  15844. Add('begin');
  15845. Add(' create(1);');
  15846. Add(' create(1,2);');
  15847. Add(' inherited create(1);');
  15848. Add(' inherited create(1,2);');
  15849. Add('end;');
  15850. Add('constructor tcar.create(va, vb: longint); begin end;');
  15851. Add('begin');
  15852. Add(' tobject.create(1);');
  15853. Add(' tobject.create(1,2);');
  15854. Add(' tcar.create(1);');
  15855. Add(' tcar.create(1,2);');
  15856. ConvertProgram;
  15857. CheckSource('TestClass_OverloadConstructor',
  15858. LinesToStr([ // statements
  15859. 'rtl.createClass(this, "TObject", null, function () {',
  15860. ' this.$init = function () {',
  15861. ' };',
  15862. ' this.$final = function () {',
  15863. ' };',
  15864. ' this.Create = function (vA) {',
  15865. ' this.Create(1);',
  15866. ' this.Create$1(1,2);',
  15867. ' return this;',
  15868. ' };',
  15869. ' this.Create$1 = function (vA, vB) {',
  15870. ' return this;',
  15871. ' };',
  15872. '});',
  15873. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15874. ' this.Create$2 = function (vA) {',
  15875. ' this.Create$2(1);',
  15876. ' this.Create$3(1, 2);',
  15877. ' $mod.TObject.Create.call(this, 1);',
  15878. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15879. ' return this;',
  15880. ' };',
  15881. ' this.Create$3 = function (vA, vB) {',
  15882. ' return this;',
  15883. ' };',
  15884. '});',
  15885. '']),
  15886. LinesToStr([ // $mod.$main
  15887. '$mod.TObject.$create("Create", [1]);',
  15888. '$mod.TObject.$create("Create$1", [1, 2]);',
  15889. '$mod.TCar.$create("Create$2", [1]);',
  15890. '$mod.TCar.$create("Create$3", [1, 2]);',
  15891. '']));
  15892. end;
  15893. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15894. begin
  15895. StartProgram(false);
  15896. Add([
  15897. '{$mode delphi}',
  15898. 'type',
  15899. ' TObject = class end;',
  15900. ' TBird = class',
  15901. ' function {#a}GetValue: longint; overload; virtual;',
  15902. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15903. ' end;',
  15904. ' TEagle = class(TBird)',
  15905. ' function {#c}GetValue: longint; overload; override;',
  15906. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15907. ' end;',
  15908. 'function TBird.GetValue: longint;',
  15909. 'begin',
  15910. ' if 3={@a}GetValue then ;',
  15911. ' if 4={@b}GetValue(5) then ;',
  15912. 'end;',
  15913. 'function TBird.GetValue(AValue: longint): longint;',
  15914. 'begin',
  15915. 'end;',
  15916. 'function TEagle.GetValue: longint;',
  15917. 'begin',
  15918. ' if 13={@c}GetValue then ;',
  15919. ' if 14={@d}GetValue(15) then ;',
  15920. ' if 15=inherited {@a}GetValue then ;',
  15921. ' if 16=inherited {@b}GetValue(17) then ;',
  15922. 'end;',
  15923. 'function TEagle.GetValue(AValue: longint): longint;',
  15924. 'begin',
  15925. 'end;',
  15926. 'var',
  15927. ' e: TEagle;',
  15928. 'begin',
  15929. ' if 23=e.{@c}GetValue then ;',
  15930. ' if 24=e.{@d}GetValue(25) then ;']);
  15931. ConvertProgram;
  15932. CheckSource('TestClass_OverloadDelphiOverride',
  15933. LinesToStr([ // statements
  15934. 'rtl.createClass(this, "TObject", null, function () {',
  15935. ' this.$init = function () {',
  15936. ' };',
  15937. ' this.$final = function () {',
  15938. ' };',
  15939. '});',
  15940. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15941. ' this.GetValue = function () {',
  15942. ' var Result = 0;',
  15943. ' if (3 === this.GetValue()) ;',
  15944. ' if (4 === this.GetValue$1(5)) ;',
  15945. ' return Result;',
  15946. ' };',
  15947. ' this.GetValue$1 = function (AValue) {',
  15948. ' var Result = 0;',
  15949. ' return Result;',
  15950. ' };',
  15951. '});',
  15952. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15953. ' this.GetValue = function () {',
  15954. ' var Result = 0;',
  15955. ' if (13 === this.GetValue()) ;',
  15956. ' if (14 === this.GetValue$1(15)) ;',
  15957. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15958. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15959. ' return Result;',
  15960. ' };',
  15961. ' this.GetValue$1 = function (AValue) {',
  15962. ' var Result = 0;',
  15963. ' return Result;',
  15964. ' };',
  15965. '});',
  15966. 'this.e = null;',
  15967. '']),
  15968. LinesToStr([ // $mod.$main
  15969. 'if (23 === $mod.e.GetValue()) ;',
  15970. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15971. '']));
  15972. end;
  15973. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15974. begin
  15975. StartProgram(false);
  15976. Add([
  15977. '{$mode delphi}',
  15978. 'type',
  15979. ' TObject = class end;',
  15980. ' TAnimal = class',
  15981. ' public',
  15982. ' {#animal_a}A: longint;',
  15983. ' function {#animal_b}B: longint;',
  15984. ' end;',
  15985. ' TBird = class(TAnimal)',
  15986. ' public',
  15987. ' {#bird_a}A: double;',
  15988. ' {#bird_b}B: boolean;',
  15989. ' end;',
  15990. ' TEagle = class(TBird)',
  15991. ' public',
  15992. ' function {#eagle_a}A: boolean;',
  15993. ' {#eagle_b}B: double;',
  15994. ' end;',
  15995. 'function TAnimal.B: longint;',
  15996. 'begin',
  15997. 'end;',
  15998. 'function TEagle.A: boolean;',
  15999. 'begin',
  16000. ' {@eagle_b}B:=3.3;',
  16001. ' {@eagle_a}A();',
  16002. ' TBird(Self).{@bird_b}B:=true;',
  16003. ' TAnimal(Self).{@animal_a}A:=17;',
  16004. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16005. 'end;',
  16006. 'var',
  16007. ' e: TEagle;',
  16008. 'begin',
  16009. ' e.{@eagle_b}B:=5.3;',
  16010. ' if e.{@eagle_a}A then ;',
  16011. '']);
  16012. ConvertProgram;
  16013. CheckSource('TestClass_ReintroduceVarDelphi',
  16014. LinesToStr([ // statements
  16015. 'rtl.createClass(this, "TObject", null, function () {',
  16016. ' this.$init = function () {',
  16017. ' };',
  16018. ' this.$final = function () {',
  16019. ' };',
  16020. '});',
  16021. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16022. ' this.$init = function () {',
  16023. ' $mod.TObject.$init.call(this);',
  16024. ' this.A = 0;',
  16025. ' };',
  16026. ' this.B = function () {',
  16027. ' var Result = 0;',
  16028. ' return Result;',
  16029. ' };',
  16030. '});',
  16031. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16032. ' this.$init = function () {',
  16033. ' $mod.TAnimal.$init.call(this);',
  16034. ' this.A$1 = 0.0;',
  16035. ' this.B$1 = false;',
  16036. ' };',
  16037. '});',
  16038. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16039. ' this.$init = function () {',
  16040. ' $mod.TBird.$init.call(this);',
  16041. ' this.B$2 = 0.0;',
  16042. ' };',
  16043. ' this.A$2 = function () {',
  16044. ' var Result = false;',
  16045. ' this.B$2 = 3.3;',
  16046. ' this.A$2();',
  16047. ' this.B$1 = true;',
  16048. ' this.A = 17;',
  16049. ' this.B$1 = this.A$1 > 1;',
  16050. ' return Result;',
  16051. ' };',
  16052. '});',
  16053. 'this.e = null;',
  16054. '']),
  16055. LinesToStr([ // $mod.$main
  16056. '$mod.e.B$2 = 5.3;',
  16057. 'if ($mod.e.A$2()) ;',
  16058. '']));
  16059. end;
  16060. procedure TTestModule.TestClass_ReintroducedVar;
  16061. begin
  16062. StartProgram(false);
  16063. Add('type');
  16064. Add(' TObject = class');
  16065. Add(' strict private');
  16066. Add(' Some: longint;');
  16067. Add(' end;');
  16068. Add(' TMobile = class');
  16069. Add(' strict private');
  16070. Add(' Some: string;');
  16071. Add(' end;');
  16072. Add(' TCar = class(tmobile)');
  16073. Add(' procedure Some;');
  16074. Add(' procedure Some(vA: longint);');
  16075. Add(' end;');
  16076. Add('procedure tcar.some;');
  16077. Add('begin');
  16078. Add(' Some;');
  16079. Add(' Some(1);');
  16080. Add('end;');
  16081. Add('procedure tcar.some(va: longint); begin end;');
  16082. Add('begin');
  16083. ConvertProgram;
  16084. CheckSource('TestClass_ReintroducedVar',
  16085. LinesToStr([ // statements
  16086. 'rtl.createClass(this, "TObject", null, function () {',
  16087. ' this.$init = function () {',
  16088. ' this.Some = 0;',
  16089. ' };',
  16090. ' this.$final = function () {',
  16091. ' };',
  16092. '});',
  16093. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16094. ' this.$init = function () {',
  16095. ' $mod.TObject.$init.call(this);',
  16096. ' this.Some$1 = "";',
  16097. ' };',
  16098. '});',
  16099. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16100. ' this.Some$2 = function () {',
  16101. ' this.Some$2();',
  16102. ' this.Some$3(1);',
  16103. ' };',
  16104. ' this.Some$3 = function (vA) {',
  16105. ' };',
  16106. '});',
  16107. '']),
  16108. LinesToStr([ // $mod.$main
  16109. '']));
  16110. end;
  16111. procedure TTestModule.TestClass_RaiseDescendant;
  16112. begin
  16113. StartProgram(false);
  16114. Add([
  16115. 'type',
  16116. ' TObject = class',
  16117. ' constructor Create(Msg: string);',
  16118. ' end;',
  16119. ' Exception = class',
  16120. ' end;',
  16121. ' EConvertError = class(Exception)',
  16122. ' end;',
  16123. 'constructor TObject.Create(Msg: string); begin end;',
  16124. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16125. 'begin',
  16126. ' raise Exception.Create(''Bar1'');',
  16127. ' raise EConvertError.Create(''Bar2'');',
  16128. ' raise AssertConv(''Bar2'');',
  16129. ' raise AssertConv;',
  16130. '']);
  16131. ConvertProgram;
  16132. CheckSource('TestClass_RaiseDescendant',
  16133. LinesToStr([ // statements
  16134. 'rtl.createClass(this, "TObject", null, function () {',
  16135. ' this.$init = function () {',
  16136. ' };',
  16137. ' this.$final = function () {',
  16138. ' };',
  16139. ' this.Create = function (Msg) {',
  16140. ' return this;',
  16141. ' };',
  16142. '});',
  16143. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16144. '});',
  16145. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16146. '});',
  16147. 'this.AssertConv = function (Msg) {',
  16148. ' var Result = null;',
  16149. ' return Result;',
  16150. '};',
  16151. '']),
  16152. LinesToStr([ // $mod.$main
  16153. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16154. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16155. 'throw $mod.AssertConv("Bar2");',
  16156. 'throw $mod.AssertConv("def");',
  16157. '']));
  16158. end;
  16159. procedure TTestModule.TestClass_ExternalMethod;
  16160. begin
  16161. AddModuleWithIntfImplSrc('unit2.pas',
  16162. LinesToStr([
  16163. 'type',
  16164. ' TObject = class',
  16165. ' public',
  16166. ' procedure Intern; external name ''$DoIntern'';',
  16167. ' end;',
  16168. '']),
  16169. LinesToStr([
  16170. '']));
  16171. StartUnit(true);
  16172. Add('interface');
  16173. Add('uses unit2;');
  16174. Add('type');
  16175. Add(' TCar = class(TObject)');
  16176. Add(' public');
  16177. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16178. Add(' procedure DoIt;');
  16179. Add(' end;');
  16180. Add('implementation');
  16181. Add('procedure tcar.doit;');
  16182. Add('begin');
  16183. Add(' Intern;');
  16184. Add(' Intern();');
  16185. Add(' Intern2;');
  16186. Add(' Intern2();');
  16187. Add('end;');
  16188. Add('var Obj: TCar;');
  16189. Add('begin');
  16190. Add(' obj.intern;');
  16191. Add(' obj.intern();');
  16192. Add(' obj.intern2;');
  16193. Add(' obj.intern2();');
  16194. Add(' obj.doit;');
  16195. Add(' obj.doit();');
  16196. Add(' with obj do begin');
  16197. Add(' Intern;');
  16198. Add(' Intern();');
  16199. Add(' Intern2;');
  16200. Add(' Intern2();');
  16201. Add(' end;');
  16202. ConvertUnit;
  16203. CheckSource('TestClass_ExternalMethod',
  16204. LinesToStr([
  16205. 'var $impl = $mod.$impl;',
  16206. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16207. ' this.DoIt = function () {',
  16208. ' this.$DoIntern();',
  16209. ' this.$DoIntern();',
  16210. ' this.$DoIntern2();',
  16211. ' this.$DoIntern2();',
  16212. ' };',
  16213. ' });',
  16214. '']),
  16215. LinesToStr([ // this.$init
  16216. '$impl.Obj.$DoIntern();',
  16217. '$impl.Obj.$DoIntern();',
  16218. '$impl.Obj.$DoIntern2();',
  16219. '$impl.Obj.$DoIntern2();',
  16220. '$impl.Obj.DoIt();',
  16221. '$impl.Obj.DoIt();',
  16222. 'var $with = $impl.Obj;',
  16223. '$with.$DoIntern();',
  16224. '$with.$DoIntern();',
  16225. '$with.$DoIntern2();',
  16226. '$with.$DoIntern2();',
  16227. '']),
  16228. LinesToStr([ // implementation
  16229. '$impl.Obj = null;',
  16230. '']) );
  16231. end;
  16232. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16233. begin
  16234. StartProgram(false);
  16235. Add('type');
  16236. Add(' TObject = class');
  16237. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16238. Add(' end;');
  16239. Add('begin');
  16240. SetExpectedPasResolverError('Virtual method name must match external',
  16241. nVirtualMethodNameMustMatchExternal);
  16242. ConvertProgram;
  16243. end;
  16244. procedure TTestModule.TestClass_ExternalOverrideFail;
  16245. begin
  16246. StartProgram(false);
  16247. Add('type');
  16248. Add(' TObject = class');
  16249. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16250. Add(' end;');
  16251. Add(' TCar = class');
  16252. Add(' procedure DoIt; override; external name ''DoIt'';');
  16253. Add(' end;');
  16254. Add('begin');
  16255. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16256. nInvalidXModifierY);
  16257. ConvertProgram;
  16258. end;
  16259. procedure TTestModule.TestClass_ExternalVar;
  16260. begin
  16261. AddModuleWithIntfImplSrc('unit2.pas',
  16262. LinesToStr([
  16263. '{$modeswitch externalclass}',
  16264. 'type',
  16265. ' TObject = class',
  16266. ' public',
  16267. ' Intern: longint external name ''$Intern'';',
  16268. ' Bracket: longint external name ''["A B"]'';',
  16269. ' end;',
  16270. '']),
  16271. LinesToStr([
  16272. '']));
  16273. StartUnit(true);
  16274. Add([
  16275. 'interface',
  16276. 'uses unit2;',
  16277. '{$modeswitch externalclass}',
  16278. 'type',
  16279. ' TCar = class(tobject)',
  16280. ' public',
  16281. ' Intern2: longint external name ''$Intern2'';',
  16282. ' procedure DoIt;',
  16283. ' end;',
  16284. 'implementation',
  16285. 'procedure tcar.doit;',
  16286. 'begin',
  16287. ' Intern:=Intern+1;',
  16288. ' Intern2:=Intern2+2;',
  16289. ' Bracket:=Bracket+3;',
  16290. 'end;',
  16291. 'var Obj: TCar;',
  16292. 'begin',
  16293. ' obj.intern:=obj.intern+1;',
  16294. ' obj.intern2:=obj.intern2+2;',
  16295. ' obj.Bracket:=obj.Bracket+3;',
  16296. ' with obj do begin',
  16297. ' intern:=intern+1;',
  16298. ' intern2:=intern2+2;',
  16299. ' Bracket:=Bracket+3;',
  16300. ' end;']);
  16301. ConvertUnit;
  16302. CheckSource('TestClass_ExternalVar',
  16303. LinesToStr([
  16304. 'var $impl = $mod.$impl;',
  16305. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16306. ' this.DoIt = function () {',
  16307. ' this.$Intern = this.$Intern + 1;',
  16308. ' this.$Intern2 = this.$Intern2 + 2;',
  16309. ' this["A B"] = this["A B"] + 3;',
  16310. ' };',
  16311. ' });',
  16312. '']),
  16313. LinesToStr([
  16314. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16315. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16316. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16317. 'var $with = $impl.Obj;',
  16318. '$with.$Intern = $with.$Intern + 1;',
  16319. '$with.$Intern2 = $with.$Intern2 + 2;',
  16320. '$with["A B"] = $with["A B"] + 3;',
  16321. '']),
  16322. LinesToStr([ // implementation
  16323. '$impl.Obj = null;',
  16324. '']));
  16325. end;
  16326. procedure TTestModule.TestClass_Const;
  16327. begin
  16328. StartProgram(false);
  16329. Add([
  16330. 'type',
  16331. ' integer = longint;',
  16332. ' TClass = class of TObject;',
  16333. ' TObject = class',
  16334. ' public',
  16335. ' const cI: integer = 3;',
  16336. ' procedure DoIt;',
  16337. ' class procedure DoMore;',
  16338. ' end;',
  16339. 'procedure tobject.doit;',
  16340. 'begin',
  16341. ' if cI=4 then;',
  16342. ' if 5=cI then;',
  16343. ' if Self.cI=6 then;',
  16344. ' if 7=Self.cI then;',
  16345. ' with Self do begin',
  16346. ' if cI=11 then;',
  16347. ' if 12=cI then;',
  16348. ' end;',
  16349. 'end;',
  16350. 'class procedure tobject.domore;',
  16351. 'begin',
  16352. ' if cI=8 then;',
  16353. ' if Self.cI=9 then;',
  16354. ' if 10=cI then;',
  16355. ' if 11=Self.cI then;',
  16356. ' with Self do begin',
  16357. ' if cI=13 then;',
  16358. ' if 14=cI then;',
  16359. ' end;',
  16360. 'end;',
  16361. 'var',
  16362. ' Obj: TObject;',
  16363. ' Cla: TClass;',
  16364. 'begin',
  16365. ' if TObject.cI=21 then ;',
  16366. ' if Obj.cI=22 then ;',
  16367. ' if Cla.cI=23 then ;',
  16368. ' with obj do if ci=24 then;',
  16369. ' with TObject do if ci=25 then;',
  16370. ' with Cla do if ci=26 then;']);
  16371. ConvertProgram;
  16372. CheckSource('TestClass_Const',
  16373. LinesToStr([
  16374. 'rtl.createClass(this, "TObject", null, function () {',
  16375. ' this.cI = 3;',
  16376. ' this.$init = function () {',
  16377. ' };',
  16378. ' this.$final = function () {',
  16379. ' };',
  16380. ' this.DoIt = function () {',
  16381. ' if (this.cI === 4) ;',
  16382. ' if (5 === this.cI) ;',
  16383. ' if (this.cI === 6) ;',
  16384. ' if (7 === this.cI) ;',
  16385. ' if (this.cI === 11) ;',
  16386. ' if (12 === this.cI) ;',
  16387. ' };',
  16388. ' this.DoMore = function () {',
  16389. ' if (this.cI === 8) ;',
  16390. ' if (this.cI === 9) ;',
  16391. ' if (10 === this.cI) ;',
  16392. ' if (11 === this.cI) ;',
  16393. ' if (this.cI === 13) ;',
  16394. ' if (14 === this.cI) ;',
  16395. ' };',
  16396. '});',
  16397. 'this.Obj = null;',
  16398. 'this.Cla = null;',
  16399. '']),
  16400. LinesToStr([
  16401. 'if ($mod.TObject.cI === 21) ;',
  16402. 'if ($mod.Obj.cI === 22) ;',
  16403. 'if ($mod.Cla.cI === 23) ;',
  16404. 'var $with = $mod.Obj;',
  16405. 'if ($with.cI === 24) ;',
  16406. 'var $with1 = $mod.TObject;',
  16407. 'if ($with1.cI === 25) ;',
  16408. 'var $with2 = $mod.Cla;',
  16409. 'if ($with2.cI === 26) ;',
  16410. '']));
  16411. end;
  16412. procedure TTestModule.TestClass_ConstEnum;
  16413. begin
  16414. StartProgram(false);
  16415. Add([
  16416. 'type',
  16417. ' TEnum = (red,blue);',
  16418. ' TObject = class',
  16419. ' end;',
  16420. ' TAnimal = class',
  16421. ' public',
  16422. ' type TSubEnum = (light,dark);',
  16423. ' const a = high(TEnum);',
  16424. ' const b = high(TSubEnum);',
  16425. ' end;',
  16426. ' TBird = class(TAnimal)',
  16427. ' public',
  16428. ' const c = high(TEnum);',
  16429. ' const d = high(TSubEnum);',
  16430. ' end;',
  16431. ' TAnt = class',
  16432. ' public',
  16433. ' const e = high(TEnum);',
  16434. ' const f = high(TBird.TSubEnum);',
  16435. ' end;',
  16436. 'begin',
  16437. '']);
  16438. ConvertProgram;
  16439. CheckSource('TestClass_ConstEnum',
  16440. LinesToStr([
  16441. 'this.TEnum = {',
  16442. ' "0": "red",',
  16443. ' red: 0,',
  16444. ' "1": "blue",',
  16445. ' blue: 1',
  16446. '};',
  16447. 'rtl.createClass(this, "TObject", null, function () {',
  16448. ' this.$init = function () {',
  16449. ' };',
  16450. ' this.$final = function () {',
  16451. ' };',
  16452. '});',
  16453. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16454. ' this.TSubEnum = {',
  16455. ' "0": "light",',
  16456. ' light: 0,',
  16457. ' "1": "dark",',
  16458. ' dark: 1',
  16459. ' };',
  16460. ' this.a = $mod.TEnum.blue;',
  16461. ' this.b = this.TSubEnum.dark;',
  16462. '});',
  16463. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16464. ' this.c = $mod.TEnum.blue;',
  16465. ' this.d = this.TSubEnum.dark;',
  16466. '});',
  16467. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16468. ' this.e = $mod.TEnum.blue;',
  16469. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16470. '});',
  16471. '']),
  16472. LinesToStr([
  16473. '']));
  16474. end;
  16475. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16476. begin
  16477. StartProgram(false);
  16478. Add([
  16479. 'type',
  16480. ' TObject = class',
  16481. ' const cI: longint = 3;',
  16482. ' procedure Fly;',
  16483. ' procedure Run;',
  16484. ' end;',
  16485. ' TBird = class',
  16486. ' procedure Go;',
  16487. ' end;',
  16488. 'procedure tobject.fly;',
  16489. 'const cI: word = 4;',
  16490. 'begin',
  16491. ' if cI=Self.cI then ;',
  16492. 'end;',
  16493. 'procedure tobject.run;',
  16494. 'const cI: word = 5;',
  16495. 'begin',
  16496. ' if cI=Self.cI then ;',
  16497. 'end;',
  16498. 'procedure tbird.go;',
  16499. 'const cI: word = 6;',
  16500. 'begin',
  16501. ' if cI=Self.cI then ;',
  16502. 'end;',
  16503. 'begin',
  16504. '']);
  16505. ConvertProgram;
  16506. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16507. LinesToStr([
  16508. 'rtl.createClass(this, "TObject", null, function () {',
  16509. ' this.cI = 3;',
  16510. ' this.$init = function () {',
  16511. ' };',
  16512. ' this.$final = function () {',
  16513. ' };',
  16514. ' var cI$1 = 4;',
  16515. ' this.Fly = function () {',
  16516. ' if (cI$1 === this.cI) ;',
  16517. ' };',
  16518. ' var cI$2 = 5;',
  16519. ' this.Run = function () {',
  16520. ' if (cI$2 === this.cI) ;',
  16521. ' };',
  16522. '});',
  16523. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16524. ' var cI$3 = 6;',
  16525. ' this.Go = function () {',
  16526. ' if (cI$3 === this.cI) ;',
  16527. ' };',
  16528. '});',
  16529. '']),
  16530. LinesToStr([
  16531. '']));
  16532. end;
  16533. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16534. begin
  16535. StartUnit(false);
  16536. Add([
  16537. 'interface',
  16538. 'type',
  16539. ' TObject = class',
  16540. ' const cI: longint = 3;',
  16541. ' procedure Fly;',
  16542. ' procedure Run;',
  16543. ' end;',
  16544. ' TBird = class',
  16545. ' procedure Go;',
  16546. ' end;',
  16547. 'implementation',
  16548. 'procedure tobject.fly;',
  16549. 'const cI: word = 4;',
  16550. 'begin',
  16551. ' if cI=Self.cI then ;',
  16552. 'end;',
  16553. 'procedure tobject.run;',
  16554. 'const cI: word = 5;',
  16555. 'begin',
  16556. ' if cI=Self.cI then ;',
  16557. 'end;',
  16558. 'procedure tbird.go;',
  16559. 'const cI: word = 6;',
  16560. 'begin',
  16561. ' if cI=Self.cI then ;',
  16562. 'end;',
  16563. '']);
  16564. ConvertUnit;
  16565. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16566. LinesToStr([
  16567. 'rtl.createClass(this, "TObject", null, function () {',
  16568. ' this.cI = 3;',
  16569. ' this.$init = function () {',
  16570. ' };',
  16571. ' this.$final = function () {',
  16572. ' };',
  16573. ' var cI$1 = 4;',
  16574. ' this.Fly = function () {',
  16575. ' if (cI$1 === this.cI) ;',
  16576. ' };',
  16577. ' var cI$2 = 5;',
  16578. ' this.Run = function () {',
  16579. ' if (cI$2 === this.cI) ;',
  16580. ' };',
  16581. '});',
  16582. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16583. ' var cI$3 = 6;',
  16584. ' this.Go = function () {',
  16585. ' if (cI$3 === this.cI) ;',
  16586. ' };',
  16587. '});',
  16588. '']),
  16589. '',
  16590. '');
  16591. end;
  16592. procedure TTestModule.TestClass_LocalVarSelfFail;
  16593. begin
  16594. StartProgram(false);
  16595. Add([
  16596. 'type',
  16597. ' TObject = class',
  16598. ' constructor Create;',
  16599. ' end;',
  16600. 'constructor tobject.create;',
  16601. 'var self: longint;',
  16602. 'begin',
  16603. 'end',
  16604. 'begin',
  16605. '']);
  16606. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16607. ConvertProgram;
  16608. end;
  16609. procedure TTestModule.TestClass_ArgSelfFail;
  16610. begin
  16611. StartProgram(false);
  16612. Add([
  16613. 'type',
  16614. ' TObject = class',
  16615. ' procedure DoIt(Self: longint);',
  16616. ' end;',
  16617. 'procedure tobject.doit(self: longint);',
  16618. 'begin',
  16619. 'end',
  16620. 'begin',
  16621. '']);
  16622. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16623. ConvertProgram;
  16624. end;
  16625. procedure TTestModule.TestClass_NestedProcSelf;
  16626. begin
  16627. StartProgram(false);
  16628. Add([
  16629. 'type',
  16630. ' TObject = class',
  16631. ' Key: longint;',
  16632. ' class var State: longint;',
  16633. ' procedure DoIt;',
  16634. ' function GetSize: longint; virtual; abstract;',
  16635. ' procedure SetSize(Value: longint); virtual; abstract;',
  16636. ' property Size: longint read GetSize write SetSize;',
  16637. ' end;',
  16638. 'procedure tobject.doit;',
  16639. ' procedure Sub;',
  16640. ' begin',
  16641. ' key:=key+2;',
  16642. ' self.key:=self.key+3;',
  16643. ' state:=state+4;',
  16644. ' self.state:=self.state+5;',
  16645. ' tobject.state:=tobject.state+6;',
  16646. ' size:=size+7;',
  16647. ' self.size:=self.size+8;',
  16648. ' end;',
  16649. 'begin',
  16650. ' sub;',
  16651. ' key:=key+12;',
  16652. ' self.key:=self.key+13;',
  16653. ' state:=state+14;',
  16654. ' self.state:=self.state+15;',
  16655. ' tobject.state:=tobject.state+16;',
  16656. ' size:=size+17;',
  16657. ' self.size:=self.size+18;',
  16658. 'end;',
  16659. 'begin',
  16660. '']);
  16661. ConvertProgram;
  16662. CheckSource('TestClass_NestedProcSelf',
  16663. LinesToStr([ // statements
  16664. 'rtl.createClass(this, "TObject", null, function () {',
  16665. ' this.State = 0;',
  16666. ' this.$init = function () {',
  16667. ' this.Key = 0;',
  16668. ' };',
  16669. ' this.$final = function () {',
  16670. ' };',
  16671. ' this.DoIt = function () {',
  16672. ' var $Self = this;',
  16673. ' function Sub() {',
  16674. ' $Self.Key = $Self.Key + 2;',
  16675. ' $Self.Key = $Self.Key + 3;',
  16676. ' $mod.TObject.State = $Self.State + 4;',
  16677. ' $mod.TObject.State = $Self.State + 5;',
  16678. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16679. ' $Self.SetSize($Self.GetSize() + 7);',
  16680. ' $Self.SetSize($Self.GetSize() + 8);',
  16681. ' };',
  16682. ' Sub();',
  16683. ' this.Key = this.Key + 12;',
  16684. ' $Self.Key = $Self.Key + 13;',
  16685. ' $mod.TObject.State = this.State + 14;',
  16686. ' $mod.TObject.State = $Self.State + 15;',
  16687. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16688. ' this.SetSize(this.GetSize() + 17);',
  16689. ' $Self.SetSize($Self.GetSize() + 18);',
  16690. ' };',
  16691. '});',
  16692. '']),
  16693. LinesToStr([ // $mod.$main
  16694. '']));
  16695. end;
  16696. procedure TTestModule.TestClass_NestedProcSelf2;
  16697. begin
  16698. StartProgram(false);
  16699. Add([
  16700. 'type',
  16701. ' TObject = class',
  16702. ' Key: longint;',
  16703. ' class var State: longint;',
  16704. ' function GetSize: longint; virtual; abstract;',
  16705. ' procedure SetSize(Value: longint); virtual; abstract;',
  16706. ' property Size: longint read GetSize write SetSize;',
  16707. ' end;',
  16708. ' TBird = class',
  16709. ' procedure DoIt;',
  16710. ' end;',
  16711. 'procedure tbird.doit;',
  16712. ' procedure Sub;',
  16713. ' begin',
  16714. ' key:=key+2;',
  16715. ' self.key:=self.key+3;',
  16716. ' state:=state+4;',
  16717. ' self.state:=self.state+5;',
  16718. ' tobject.state:=tobject.state+6;',
  16719. ' size:=size+7;',
  16720. ' self.size:=self.size+8;',
  16721. ' end;',
  16722. 'begin',
  16723. ' sub;',
  16724. ' key:=key+12;',
  16725. ' self.key:=self.key+13;',
  16726. ' state:=state+14;',
  16727. ' self.state:=self.state+15;',
  16728. ' tobject.state:=tobject.state+16;',
  16729. ' size:=size+17;',
  16730. ' self.size:=self.size+18;',
  16731. 'end;',
  16732. 'begin',
  16733. '']);
  16734. ConvertProgram;
  16735. CheckSource('TestClass_NestedProcSelf2',
  16736. LinesToStr([ // statements
  16737. 'rtl.createClass(this, "TObject", null, function () {',
  16738. ' this.State = 0;',
  16739. ' this.$init = function () {',
  16740. ' this.Key = 0;',
  16741. ' };',
  16742. ' this.$final = function () {',
  16743. ' };',
  16744. '});',
  16745. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16746. ' this.DoIt = function () {',
  16747. ' var $Self = this;',
  16748. ' function Sub() {',
  16749. ' $Self.Key = $Self.Key + 2;',
  16750. ' $Self.Key = $Self.Key + 3;',
  16751. ' $mod.TObject.State = $Self.State + 4;',
  16752. ' $mod.TObject.State = $Self.State + 5;',
  16753. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16754. ' $Self.SetSize($Self.GetSize() + 7);',
  16755. ' $Self.SetSize($Self.GetSize() + 8);',
  16756. ' };',
  16757. ' Sub();',
  16758. ' this.Key = this.Key + 12;',
  16759. ' $Self.Key = $Self.Key + 13;',
  16760. ' $mod.TObject.State = this.State + 14;',
  16761. ' $mod.TObject.State = $Self.State + 15;',
  16762. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16763. ' this.SetSize(this.GetSize() + 17);',
  16764. ' $Self.SetSize($Self.GetSize() + 18);',
  16765. ' };',
  16766. '});',
  16767. '']),
  16768. LinesToStr([ // $mod.$main
  16769. '']));
  16770. end;
  16771. procedure TTestModule.TestClass_NestedProcClassSelf;
  16772. begin
  16773. StartProgram(false);
  16774. Add([
  16775. 'type',
  16776. ' TObject = class',
  16777. ' class var State: longint;',
  16778. ' class procedure DoIt;',
  16779. ' class function GetSize: longint; virtual; abstract;',
  16780. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16781. ' class property Size: longint read GetSize write SetSize;',
  16782. ' end;',
  16783. 'class procedure tobject.doit;',
  16784. ' procedure Sub;',
  16785. ' begin',
  16786. ' state:=state+2;',
  16787. ' self.state:=self.state+3;',
  16788. ' tobject.state:=tobject.state+4;',
  16789. ' size:=size+5;',
  16790. ' self.size:=self.size+6;',
  16791. ' tobject.size:=tobject.size+7;',
  16792. ' end;',
  16793. 'begin',
  16794. ' sub;',
  16795. ' state:=state+12;',
  16796. ' self.state:=self.state+13;',
  16797. ' tobject.state:=tobject.state+14;',
  16798. ' size:=size+15;',
  16799. ' self.size:=self.size+16;',
  16800. ' tobject.size:=tobject.size+17;',
  16801. 'end;',
  16802. 'begin',
  16803. '']);
  16804. ConvertProgram;
  16805. CheckSource('TestClass_NestedProcClassSelf',
  16806. LinesToStr([ // statements
  16807. 'rtl.createClass(this, "TObject", null, function () {',
  16808. ' this.State = 0;',
  16809. ' this.$init = function () {',
  16810. ' };',
  16811. ' this.$final = function () {',
  16812. ' };',
  16813. ' this.DoIt = function () {',
  16814. ' var $Self = this;',
  16815. ' function Sub() {',
  16816. ' $mod.TObject.State = $Self.State + 2;',
  16817. ' $mod.TObject.State = $Self.State + 3;',
  16818. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16819. ' $Self.SetSize($Self.GetSize() + 5);',
  16820. ' $Self.SetSize($Self.GetSize() + 6);',
  16821. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16822. ' };',
  16823. ' Sub();',
  16824. ' $mod.TObject.State = this.State + 12;',
  16825. ' $mod.TObject.State = $Self.State + 13;',
  16826. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16827. ' this.SetSize(this.GetSize() + 15);',
  16828. ' $Self.SetSize($Self.GetSize() + 16);',
  16829. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16830. ' };',
  16831. '});',
  16832. '']),
  16833. LinesToStr([ // $mod.$main
  16834. '']));
  16835. end;
  16836. procedure TTestModule.TestClass_NestedProcCallInherited;
  16837. begin
  16838. StartProgram(false);
  16839. Add([
  16840. 'type',
  16841. ' TObject = class',
  16842. ' function DoIt(k: boolean): longint; virtual;',
  16843. ' end;',
  16844. ' TBird = class',
  16845. ' function DoIt(k: boolean): longint; override;',
  16846. ' end;',
  16847. 'function tobject.doit(k: boolean): longint;',
  16848. 'begin',
  16849. 'end;',
  16850. 'function tbird.doit(k: boolean): longint;',
  16851. ' procedure Sub;',
  16852. ' begin',
  16853. ' inherited DoIt(true);',
  16854. //' if inherited DoIt(false)=4 then ;',
  16855. ' end;',
  16856. 'begin',
  16857. ' Sub;',
  16858. ' inherited;',
  16859. ' inherited DoIt(true);',
  16860. //' if inherited DoIt(false)=14 then ;',
  16861. 'end;',
  16862. 'begin',
  16863. '']);
  16864. ConvertProgram;
  16865. CheckSource('TestClass_NestedProcCallInherited',
  16866. LinesToStr([ // statements
  16867. 'rtl.createClass(this, "TObject", null, function () {',
  16868. ' this.$init = function () {',
  16869. ' };',
  16870. ' this.$final = function () {',
  16871. ' };',
  16872. ' this.DoIt = function (k) {',
  16873. ' var Result = 0;',
  16874. ' return Result;',
  16875. ' };',
  16876. '});',
  16877. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16878. ' this.DoIt = function (k) {',
  16879. ' var $Self = this;',
  16880. ' var Result = 0;',
  16881. ' function Sub() {',
  16882. ' $mod.TObject.DoIt.call($Self, true);',
  16883. ' };',
  16884. ' Sub();',
  16885. ' $mod.TObject.DoIt.apply(this, arguments);',
  16886. ' $mod.TObject.DoIt.call(this, true);',
  16887. ' return Result;',
  16888. ' };',
  16889. '});',
  16890. '']),
  16891. LinesToStr([ // $mod.$main
  16892. '']));
  16893. end;
  16894. procedure TTestModule.TestClass_TObjectFree;
  16895. begin
  16896. StartProgram(false);
  16897. Add([
  16898. 'type',
  16899. ' TObject = class',
  16900. ' Obj: tobject;',
  16901. ' procedure Free;',
  16902. ' procedure Release;',
  16903. ' end;',
  16904. 'procedure tobject.free;',
  16905. 'begin',
  16906. 'end;',
  16907. 'procedure tobject.release;',
  16908. 'begin',
  16909. ' free;',
  16910. ' if true then free;',
  16911. 'end;',
  16912. 'function DoIt(o: tobject): tobject;',
  16913. 'var l: tobject;',
  16914. 'begin',
  16915. ' o.free;',
  16916. ' o.free();',
  16917. ' l.free;',
  16918. ' l.free();',
  16919. ' o.obj.free;',
  16920. ' o.obj.free();',
  16921. ' with o do obj.free;',
  16922. ' with o do obj.free();',
  16923. ' result.Free;',
  16924. ' result.Free();',
  16925. 'end;',
  16926. 'var o: tobject;',
  16927. ' a: array of tobject;',
  16928. 'begin',
  16929. ' o.free;',
  16930. ' o.obj.free;',
  16931. ' a[1+2].free;',
  16932. '']);
  16933. ConvertProgram;
  16934. CheckSource('TestClass_TObjectFree',
  16935. LinesToStr([ // statements
  16936. 'rtl.createClass(this, "TObject", null, function () {',
  16937. ' this.$init = function () {',
  16938. ' this.Obj = null;',
  16939. ' };',
  16940. ' this.$final = function () {',
  16941. ' this.Obj = undefined;',
  16942. ' };',
  16943. ' this.Free = function () {',
  16944. ' };',
  16945. ' this.Release = function () {',
  16946. ' this.Free();',
  16947. ' if (true) this.Free();',
  16948. ' };',
  16949. '});',
  16950. 'this.DoIt = function (o) {',
  16951. ' var Result = null;',
  16952. ' var l = null;',
  16953. ' o = rtl.freeLoc(o);',
  16954. ' o = rtl.freeLoc(o);',
  16955. ' l = rtl.freeLoc(l);',
  16956. ' l = rtl.freeLoc(l);',
  16957. ' rtl.free(o, "Obj");',
  16958. ' rtl.free(o, "Obj");',
  16959. ' rtl.free(o, "Obj");',
  16960. ' rtl.free(o, "Obj");',
  16961. ' Result = rtl.freeLoc(Result);',
  16962. ' Result = rtl.freeLoc(Result);',
  16963. ' return Result;',
  16964. '};',
  16965. 'this.o = null;',
  16966. 'this.a = [];',
  16967. '']),
  16968. LinesToStr([ // $mod.$main
  16969. 'rtl.free($mod, "o");',
  16970. 'rtl.free($mod.o, "Obj");',
  16971. 'rtl.free($mod.a, 1 + 2);',
  16972. '']));
  16973. end;
  16974. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16975. begin
  16976. StartProgram(false);
  16977. Add([
  16978. 'type',
  16979. ' TObject = class',
  16980. ' Obj: tobject;',
  16981. ' procedure Free;',
  16982. ' end;',
  16983. 'procedure tobject.free;',
  16984. 'begin',
  16985. 'end;',
  16986. 'procedure DoIt(var o: tobject);',
  16987. 'begin',
  16988. ' o.free;',
  16989. ' o.free();',
  16990. 'end;',
  16991. 'begin',
  16992. '']);
  16993. ConvertProgram;
  16994. CheckSource('TestClass_TObjectFree_VarArg',
  16995. LinesToStr([ // statements
  16996. 'rtl.createClass(this, "TObject", null, function () {',
  16997. ' this.$init = function () {',
  16998. ' this.Obj = null;',
  16999. ' };',
  17000. ' this.$final = function () {',
  17001. ' this.Obj = undefined;',
  17002. ' };',
  17003. ' this.Free = function () {',
  17004. ' };',
  17005. '});',
  17006. 'this.DoIt = function (o) {',
  17007. ' o.set(rtl.freeLoc(o.get()));',
  17008. ' o.set(rtl.freeLoc(o.get()));',
  17009. '};',
  17010. '']),
  17011. LinesToStr([ // $mod.$main
  17012. '']));
  17013. end;
  17014. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17015. begin
  17016. StartProgram(false);
  17017. Add([
  17018. 'type',
  17019. ' TObject = class',
  17020. ' constructor Create;',
  17021. ' procedure Free;',
  17022. ' end;',
  17023. 'constructor TObject.Create; begin end;',
  17024. 'procedure tobject.free; begin end;',
  17025. 'begin',
  17026. ' with tobject.create do free;',
  17027. '']);
  17028. ConvertProgram;
  17029. CheckSource('TestClass_TObjectFreeNewInstance',
  17030. LinesToStr([ // statements
  17031. 'rtl.createClass(this, "TObject", null, function () {',
  17032. ' this.$init = function () {',
  17033. ' };',
  17034. ' this.$final = function () {',
  17035. ' };',
  17036. ' this.Create = function () {',
  17037. ' return this;',
  17038. ' };',
  17039. ' this.Free = function () {',
  17040. ' };',
  17041. '});',
  17042. '']),
  17043. LinesToStr([ // $mod.$main
  17044. 'var $with = $mod.TObject.$create("Create");',
  17045. '$with=rtl.freeLoc($with);',
  17046. '']));
  17047. end;
  17048. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17049. begin
  17050. StartProgram(false);
  17051. Add([
  17052. 'type',
  17053. ' TObject = class',
  17054. ' destructor Destroy;',
  17055. ' procedure Free;',
  17056. ' end;',
  17057. 'destructor TObject.Destroy; begin end;',
  17058. 'procedure tobject.free; begin end;',
  17059. 'var o: tobject;',
  17060. 'begin',
  17061. ' o.free;',
  17062. '']);
  17063. Converter.UseLowerCase:=true;
  17064. ConvertProgram;
  17065. CheckSource('TestClass_TObjectFreeLowerCase',
  17066. LinesToStr([ // statements
  17067. 'rtl.createClass(this, "tobject", null, function () {',
  17068. ' this.$init = function () {',
  17069. ' };',
  17070. ' this.$final = function () {',
  17071. ' };',
  17072. ' rtl.tObjectDestroy = "destroy";',
  17073. ' this.destroy = function () {',
  17074. ' };',
  17075. ' this.free = function () {',
  17076. ' };',
  17077. '});',
  17078. 'this.o = null;',
  17079. '']),
  17080. LinesToStr([ // $mod.$main
  17081. 'rtl.free($mod, "o");',
  17082. '']));
  17083. end;
  17084. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17085. begin
  17086. StartProgram(false);
  17087. Add([
  17088. 'type',
  17089. ' TObject = class',
  17090. ' procedure Free;',
  17091. ' function GetObj: tobject; virtual; abstract;',
  17092. ' end;',
  17093. 'procedure tobject.free;',
  17094. 'begin',
  17095. 'end;',
  17096. 'var o: tobject;',
  17097. 'begin',
  17098. ' o.getobj.free;',
  17099. '']);
  17100. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17101. ConvertProgram;
  17102. end;
  17103. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17104. begin
  17105. StartProgram(false);
  17106. Add([
  17107. 'type',
  17108. ' TObject = class',
  17109. ' procedure Free;',
  17110. ' FObj: TObject;',
  17111. ' property Obj: tobject read FObj write FObj;',
  17112. ' end;',
  17113. 'procedure tobject.free;',
  17114. 'begin',
  17115. 'end;',
  17116. 'var o: tobject;',
  17117. 'begin',
  17118. ' o.obj.free;',
  17119. '']);
  17120. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17121. ConvertProgram;
  17122. end;
  17123. procedure TTestModule.TestClass_ForIn;
  17124. begin
  17125. StartProgram(false);
  17126. Add([
  17127. 'type',
  17128. ' TObject = class end;',
  17129. ' TItem = TObject;',
  17130. ' TEnumerator = class',
  17131. ' FCurrent: TItem;',
  17132. ' property Current: TItem read FCurrent;',
  17133. ' function MoveNext: boolean;',
  17134. ' end;',
  17135. ' TBird = class',
  17136. ' function GetEnumerator: TEnumerator;',
  17137. ' end;',
  17138. 'function TEnumerator.MoveNext: boolean;',
  17139. 'begin',
  17140. 'end;',
  17141. 'function TBird.GetEnumerator: TEnumerator;',
  17142. 'begin',
  17143. 'end;',
  17144. 'var',
  17145. ' b: TBird;',
  17146. ' i, i2: TItem;',
  17147. 'begin',
  17148. ' for i in b do i2:=i;']);
  17149. ConvertProgram;
  17150. CheckSource('TestClass_ForIn',
  17151. LinesToStr([ // statements
  17152. 'rtl.createClass(this, "TObject", null, function () {',
  17153. ' this.$init = function () {',
  17154. ' };',
  17155. ' this.$final = function () {',
  17156. ' };',
  17157. '});',
  17158. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17159. ' this.$init = function () {',
  17160. ' $mod.TObject.$init.call(this);',
  17161. ' this.FCurrent = null;',
  17162. ' };',
  17163. ' this.$final = function () {',
  17164. ' this.FCurrent = undefined;',
  17165. ' $mod.TObject.$final.call(this);',
  17166. ' };',
  17167. ' this.MoveNext = function () {',
  17168. ' var Result = false;',
  17169. ' return Result;',
  17170. ' };',
  17171. '});',
  17172. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17173. ' this.GetEnumerator = function () {',
  17174. ' var Result = null;',
  17175. ' return Result;',
  17176. ' };',
  17177. '});',
  17178. 'this.b = null;',
  17179. 'this.i = null;',
  17180. 'this.i2 = null;'
  17181. ]),
  17182. LinesToStr([ // $mod.$main
  17183. 'var $in = $mod.b.GetEnumerator();',
  17184. 'try {',
  17185. ' while ($in.MoveNext()){',
  17186. ' $mod.i = $in.FCurrent;',
  17187. ' $mod.i2 = $mod.i;',
  17188. ' }',
  17189. '} finally {',
  17190. ' $in = rtl.freeLoc($in)',
  17191. '};',
  17192. '']));
  17193. end;
  17194. procedure TTestModule.TestClass_DispatchMessage;
  17195. begin
  17196. StartProgram(false);
  17197. Add([
  17198. 'type',
  17199. ' {$DispatchField DispInt}',
  17200. ' {$DispatchStrField DispStr}',
  17201. ' TObject = class',
  17202. ' procedure Dispatch(var Msg); virtual; abstract;',
  17203. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17204. ' end;',
  17205. ' THopMsg = record',
  17206. ' DispInt: longint;',
  17207. ' end;',
  17208. ' TPutMsg = record',
  17209. ' DispStr: string;',
  17210. ' end;',
  17211. ' TBird = class',
  17212. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17213. ' procedure Run; overload; virtual; abstract;',
  17214. ' procedure Run(var Msg); overload; message ''Fast'';',
  17215. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17216. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17217. ' end;',
  17218. 'procedure TBird.Run(var Msg);',
  17219. 'begin',
  17220. 'end;',
  17221. 'begin',
  17222. '']);
  17223. ConvertProgram;
  17224. CheckResolverUnexpectedHints(true);
  17225. CheckSource('TestClass_Message',
  17226. LinesToStr([ // statements
  17227. 'rtl.createClass(this, "TObject", null, function () {',
  17228. ' this.$init = function () {',
  17229. ' };',
  17230. ' this.$final = function () {',
  17231. ' };',
  17232. '});',
  17233. 'rtl.recNewT(this, "THopMsg", function () {',
  17234. ' this.DispInt = 0;',
  17235. ' this.$eq = function (b) {',
  17236. ' return this.DispInt === b.DispInt;',
  17237. ' };',
  17238. ' this.$assign = function (s) {',
  17239. ' this.DispInt = s.DispInt;',
  17240. ' return this;',
  17241. ' };',
  17242. '});',
  17243. 'rtl.recNewT(this, "TPutMsg", function () {',
  17244. ' this.DispStr = "";',
  17245. ' this.$eq = function (b) {',
  17246. ' return this.DispStr === b.DispStr;',
  17247. ' };',
  17248. ' this.$assign = function (s) {',
  17249. ' this.DispStr = s.DispStr;',
  17250. ' return this;',
  17251. ' };',
  17252. '});',
  17253. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17254. ' this.Run$1 = function (Msg) {',
  17255. ' };',
  17256. ' this.$msgint = {',
  17257. ' "2": "Fly",',
  17258. ' "3": "Hop"',
  17259. ' };',
  17260. ' this.$msgstr = {',
  17261. ' Fast: "Run$1",',
  17262. ' foo: "Put"',
  17263. ' };',
  17264. '});',
  17265. '']),
  17266. LinesToStr([ // $mod.$main
  17267. '']));
  17268. end;
  17269. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17270. begin
  17271. StartProgram(false);
  17272. Add([
  17273. 'type',
  17274. ' TObject = class',
  17275. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17276. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17277. ' end;',
  17278. 'begin',
  17279. '']);
  17280. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17281. ConvertProgram;
  17282. end;
  17283. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17284. begin
  17285. StartProgram(false);
  17286. Add([
  17287. 'type',
  17288. ' TObject = class',
  17289. ' procedure Dispatch(var Msg); virtual; abstract;',
  17290. ' end;',
  17291. ' TFlyMsg = record',
  17292. ' FlyId: longint;',
  17293. ' end;',
  17294. ' TBird = class',
  17295. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17296. ' end;',
  17297. 'begin',
  17298. '']);
  17299. ConvertProgram;
  17300. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17301. end;
  17302. procedure TTestModule.TestClassOf_Create;
  17303. begin
  17304. StartProgram(false);
  17305. Add('type');
  17306. Add(' TObject = class');
  17307. Add(' constructor Create;');
  17308. Add(' end;');
  17309. Add(' TClass = class of TObject;');
  17310. Add('constructor tobject.create; begin end;');
  17311. Add('var');
  17312. Add(' Obj: tobject;');
  17313. Add(' C: tclass;');
  17314. Add('begin');
  17315. Add(' obj:=C.create;');
  17316. Add(' with c do obj:=create;');
  17317. ConvertProgram;
  17318. CheckSource('TestClassOf_Create',
  17319. LinesToStr([ // statements
  17320. 'rtl.createClass(this, "TObject", null, function () {',
  17321. ' this.$init = function () {',
  17322. ' };',
  17323. ' this.$final = function () {',
  17324. ' };',
  17325. ' this.Create = function () {',
  17326. ' return this;',
  17327. ' };',
  17328. '});',
  17329. 'this.Obj = null;',
  17330. 'this.C = null;'
  17331. ]),
  17332. LinesToStr([ // $mod.$main
  17333. '$mod.Obj = $mod.C.$create("Create");',
  17334. 'var $with = $mod.C;',
  17335. '$mod.Obj = $with.$create("Create");',
  17336. '']));
  17337. end;
  17338. procedure TTestModule.TestClassOf_Call;
  17339. begin
  17340. StartProgram(false);
  17341. Add('type');
  17342. Add(' TObject = class');
  17343. Add(' class procedure DoIt;');
  17344. Add(' end;');
  17345. Add(' TClass = class of TObject;');
  17346. Add('class procedure tobject.doit; begin end;');
  17347. Add('var');
  17348. Add(' C: tclass;');
  17349. Add('begin');
  17350. Add(' c.doit;');
  17351. Add(' with c do doit;');
  17352. ConvertProgram;
  17353. CheckSource('TestClassOf_Call',
  17354. LinesToStr([ // statements
  17355. 'rtl.createClass(this, "TObject", null, function () {',
  17356. ' this.$init = function () {',
  17357. ' };',
  17358. ' this.$final = function () {',
  17359. ' };',
  17360. ' this.DoIt = function () {',
  17361. ' };',
  17362. '});',
  17363. 'this.C = null;'
  17364. ]),
  17365. LinesToStr([ // $mod.$main
  17366. '$mod.C.DoIt();',
  17367. 'var $with = $mod.C;',
  17368. '$with.DoIt();',
  17369. '']));
  17370. end;
  17371. procedure TTestModule.TestClassOf_Assign;
  17372. begin
  17373. StartProgram(false);
  17374. Add('type');
  17375. Add(' TClass = class of TObject;');
  17376. Add(' TObject = class');
  17377. Add(' ClassType: TClass; ');
  17378. Add(' end;');
  17379. Add('var');
  17380. Add(' Obj: tobject;');
  17381. Add(' C: tclass;');
  17382. Add('begin');
  17383. Add(' c:=nil;');
  17384. Add(' c:=obj.classtype;');
  17385. ConvertProgram;
  17386. CheckSource('TestClassOf_Assign',
  17387. LinesToStr([ // statements
  17388. 'rtl.createClass(this, "TObject", null, function () {',
  17389. ' this.$init = function () {',
  17390. ' this.ClassType = null;',
  17391. ' };',
  17392. ' this.$final = function () {',
  17393. ' this.ClassType = undefined;',
  17394. ' };',
  17395. '});',
  17396. 'this.Obj = null;',
  17397. 'this.C = null;'
  17398. ]),
  17399. LinesToStr([ // $mod.$main
  17400. '$mod.C = null;',
  17401. '$mod.C = $mod.Obj.ClassType;',
  17402. '']));
  17403. end;
  17404. procedure TTestModule.TestClassOf_Is;
  17405. begin
  17406. StartProgram(false);
  17407. Add('type');
  17408. Add(' TClass = class of TObject;');
  17409. Add(' TObject = class');
  17410. Add(' end;');
  17411. Add(' TCar = class');
  17412. Add(' end;');
  17413. Add(' TCars = class of TCar;');
  17414. Add('var');
  17415. Add(' Obj: tobject;');
  17416. Add(' C: tclass;');
  17417. Add(' Cars: tcars;');
  17418. Add('begin');
  17419. Add(' if c is tcar then ;');
  17420. Add(' if c is tcars then ;');
  17421. ConvertProgram;
  17422. CheckSource('TestClassOf_Is',
  17423. LinesToStr([ // statements
  17424. 'rtl.createClass(this, "TObject", null, function () {',
  17425. ' this.$init = function () {',
  17426. ' };',
  17427. ' this.$final = function () {',
  17428. ' };',
  17429. '});',
  17430. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17431. '});',
  17432. 'this.Obj = null;',
  17433. 'this.C = null;',
  17434. 'this.Cars = null;'
  17435. ]),
  17436. LinesToStr([ // $mod.$main
  17437. 'if(rtl.is($mod.C,$mod.TCar));',
  17438. 'if(rtl.is($mod.C,$mod.TCar));',
  17439. '']));
  17440. end;
  17441. procedure TTestModule.TestClassOf_Compare;
  17442. begin
  17443. StartProgram(false);
  17444. Add('type');
  17445. Add(' TClass = class of TObject;');
  17446. Add(' TObject = class');
  17447. Add(' ClassType: TClass; ');
  17448. Add(' end;');
  17449. Add('var');
  17450. Add(' b: boolean;');
  17451. Add(' Obj: tobject;');
  17452. Add(' C: tclass;');
  17453. Add('begin');
  17454. Add(' b:=c=nil;');
  17455. Add(' b:=nil=c;');
  17456. Add(' b:=c=obj.classtype;');
  17457. Add(' b:=obj.classtype=c;');
  17458. Add(' b:=c=TObject;');
  17459. Add(' b:=TObject=c;');
  17460. Add(' b:=c<>nil;');
  17461. Add(' b:=nil<>c;');
  17462. Add(' b:=c<>obj.classtype;');
  17463. Add(' b:=obj.classtype<>c;');
  17464. Add(' b:=c<>TObject;');
  17465. Add(' b:=TObject<>c;');
  17466. ConvertProgram;
  17467. CheckSource('TestClassOf_Compare',
  17468. LinesToStr([ // statements
  17469. 'rtl.createClass(this, "TObject", null, function () {',
  17470. ' this.$init = function () {',
  17471. ' this.ClassType = null;',
  17472. ' };',
  17473. ' this.$final = function () {',
  17474. ' this.ClassType = undefined;',
  17475. ' };',
  17476. '});',
  17477. 'this.b = false;',
  17478. 'this.Obj = null;',
  17479. 'this.C = null;'
  17480. ]),
  17481. LinesToStr([ // $mod.$main
  17482. '$mod.b = $mod.C === null;',
  17483. '$mod.b = null === $mod.C;',
  17484. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17485. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17486. '$mod.b = $mod.C === $mod.TObject;',
  17487. '$mod.b = $mod.TObject === $mod.C;',
  17488. '$mod.b = $mod.C !== null;',
  17489. '$mod.b = null !== $mod.C;',
  17490. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17491. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17492. '$mod.b = $mod.C !== $mod.TObject;',
  17493. '$mod.b = $mod.TObject !== $mod.C;',
  17494. '']));
  17495. end;
  17496. procedure TTestModule.TestClassOf_ClassVar;
  17497. begin
  17498. StartProgram(false);
  17499. Add('type');
  17500. Add(' TObject = class');
  17501. Add(' class var id: longint;');
  17502. Add(' end;');
  17503. Add(' TClass = class of TObject;');
  17504. Add('var');
  17505. Add(' C: tclass;');
  17506. Add('begin');
  17507. Add(' C.id:=C.id;');
  17508. ConvertProgram;
  17509. CheckSource('TestClassOf_ClassVar',
  17510. LinesToStr([ // statements
  17511. 'rtl.createClass(this, "TObject", null, function () {',
  17512. ' this.id = 0;',
  17513. ' this.$init = function () {',
  17514. ' };',
  17515. ' this.$final = function () {',
  17516. ' };',
  17517. '});',
  17518. 'this.C = null;'
  17519. ]),
  17520. LinesToStr([ // $mod.$main
  17521. '$mod.TObject.id = $mod.C.id;',
  17522. '']));
  17523. end;
  17524. procedure TTestModule.TestClassOf_ClassMethod;
  17525. begin
  17526. StartProgram(false);
  17527. Add('type');
  17528. Add(' TObject = class');
  17529. Add(' class function DoIt(i: longint = 0): longint;');
  17530. Add(' end;');
  17531. Add(' TClass = class of TObject;');
  17532. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17533. Add('var');
  17534. Add(' i: longint;');
  17535. Add(' C: tclass;');
  17536. Add('begin');
  17537. Add(' C.DoIt;');
  17538. Add(' C.DoIt();');
  17539. Add(' i:=C.DoIt;');
  17540. Add(' i:=C.DoIt();');
  17541. ConvertProgram;
  17542. CheckSource('TestClassOf_ClassMethod',
  17543. LinesToStr([ // statements
  17544. 'rtl.createClass(this, "TObject", null, function () {',
  17545. ' this.$init = function () {',
  17546. ' };',
  17547. ' this.$final = function () {',
  17548. ' };',
  17549. ' this.DoIt = function (i) {',
  17550. ' var Result = 0;',
  17551. ' return Result;',
  17552. ' };',
  17553. '});',
  17554. 'this.i = 0;',
  17555. 'this.C = null;'
  17556. ]),
  17557. LinesToStr([ // $mod.$main
  17558. '$mod.C.DoIt(0);',
  17559. '$mod.C.DoIt(0);',
  17560. '$mod.i = $mod.C.DoIt(0);',
  17561. '$mod.i = $mod.C.DoIt(0);',
  17562. '']));
  17563. end;
  17564. procedure TTestModule.TestClassOf_ClassProperty;
  17565. begin
  17566. StartProgram(false);
  17567. Add([
  17568. 'type',
  17569. ' TObject = class',
  17570. ' class var FA: longint;',
  17571. ' class function GetA: longint;',
  17572. ' class procedure SetA(Value: longint);',
  17573. ' class property pA: longint read fa write fa;',
  17574. ' class property pB: longint read geta write seta;',
  17575. ' end;',
  17576. ' TObjectClass = class of tobject;',
  17577. 'class function tobject.geta: longint; begin end;',
  17578. 'class procedure tobject.seta(value: longint); begin end;',
  17579. 'var',
  17580. ' b: boolean;',
  17581. ' Obj: tobject;',
  17582. ' Cla: tobjectclass;',
  17583. 'begin',
  17584. ' obj.pa:=obj.pa;',
  17585. ' obj.pb:=obj.pb;',
  17586. ' b:=obj.pa=4;',
  17587. ' b:=obj.pb=obj.pb;',
  17588. ' b:=5=obj.pa;',
  17589. ' cla.pa:=6;',
  17590. ' cla.pa:=cla.pa;',
  17591. ' cla.pb:=cla.pb;',
  17592. ' b:=cla.pa=7;',
  17593. ' b:=cla.pb=cla.pb;',
  17594. ' b:=8=cla.pa;',
  17595. ' tobject.pa:=9;',
  17596. ' tobject.pb:=tobject.pb;',
  17597. ' b:=tobject.pa=10;',
  17598. ' b:=11=tobject.pa;',
  17599. '']);
  17600. ConvertProgram;
  17601. CheckSource('TestClassOf_ClassProperty',
  17602. LinesToStr([ // statements
  17603. 'rtl.createClass(this, "TObject", null, function () {',
  17604. ' this.FA = 0;',
  17605. ' this.$init = function () {',
  17606. ' };',
  17607. ' this.$final = function () {',
  17608. ' };',
  17609. ' this.GetA = function () {',
  17610. ' var Result = 0;',
  17611. ' return Result;',
  17612. ' };',
  17613. ' this.SetA = function (Value) {',
  17614. ' };',
  17615. '});',
  17616. 'this.b = false;',
  17617. 'this.Obj = null;',
  17618. 'this.Cla = null;'
  17619. ]),
  17620. LinesToStr([ // $mod.$main
  17621. '$mod.TObject.FA = $mod.Obj.FA;',
  17622. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17623. '$mod.b = $mod.Obj.FA === 4;',
  17624. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17625. '$mod.b = 5 === $mod.Obj.FA;',
  17626. '$mod.TObject.FA = 6;',
  17627. '$mod.TObject.FA = $mod.Cla.FA;',
  17628. '$mod.Cla.SetA($mod.Cla.GetA());',
  17629. '$mod.b = $mod.Cla.FA === 7;',
  17630. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17631. '$mod.b = 8 === $mod.Cla.FA;',
  17632. '$mod.TObject.FA = 9;',
  17633. '$mod.TObject.SetA($mod.TObject.GetA());',
  17634. '$mod.b = $mod.TObject.FA === 10;',
  17635. '$mod.b = 11 === $mod.TObject.FA;',
  17636. '']));
  17637. end;
  17638. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17639. begin
  17640. StartProgram(false);
  17641. Add('type');
  17642. Add(' TObject = class');
  17643. Add(' class var GlobalId: longint;');
  17644. Add(' class procedure ProcA;');
  17645. Add(' end;');
  17646. Add('class procedure tobject.proca;');
  17647. Add('var b: boolean;');
  17648. Add('begin');
  17649. Add(' b:=self=nil;');
  17650. Add(' b:=self.globalid=3;');
  17651. Add(' b:=4=self.globalid;');
  17652. Add(' self.globalid:=5;');
  17653. Add(' self.proca;');
  17654. Add('end;');
  17655. Add('begin');
  17656. ConvertProgram;
  17657. CheckSource('TestClassOf_ClassMethodSelf',
  17658. LinesToStr([ // statements
  17659. 'rtl.createClass(this, "TObject", null, function () {',
  17660. ' this.GlobalId = 0;',
  17661. ' this.$init = function () {',
  17662. ' };',
  17663. ' this.$final = function () {',
  17664. ' };',
  17665. ' this.ProcA = function () {',
  17666. ' var b = false;',
  17667. ' b = this === null;',
  17668. ' b = this.GlobalId === 3;',
  17669. ' b = 4 === this.GlobalId;',
  17670. ' $mod.TObject.GlobalId = 5;',
  17671. ' this.ProcA();',
  17672. ' };',
  17673. '});'
  17674. ]),
  17675. LinesToStr([ // $mod.$main
  17676. '']));
  17677. end;
  17678. procedure TTestModule.TestClassOf_TypeCast;
  17679. begin
  17680. StartProgram(false);
  17681. Add('type');
  17682. Add(' TObject = class');
  17683. Add(' class procedure {#TObject_DoIt}DoIt;');
  17684. Add(' end;');
  17685. Add(' TClass = class of TObject;');
  17686. Add(' TMobile = class');
  17687. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17688. Add(' end;');
  17689. Add(' TMobileClass = class of TMobile;');
  17690. Add(' TCar = class(TMobile)');
  17691. Add(' class procedure {#TCar_DoIt}DoIt;');
  17692. Add(' end;');
  17693. Add(' TCarClass = class of TCar;');
  17694. Add('class procedure TObject.DoIt;');
  17695. Add('begin');
  17696. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17697. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17698. Add('end;');
  17699. Add('class procedure TMobile.DoIt;');
  17700. Add('begin');
  17701. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17702. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17703. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17704. Add('end;');
  17705. Add('class procedure TCar.DoIt; begin end;');
  17706. Add('var');
  17707. Add(' ObjC: TClass;');
  17708. Add(' MobileC: TMobileClass;');
  17709. Add(' CarC: TCarClass;');
  17710. Add('begin');
  17711. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17712. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17713. Add(' CarC.{@TCar_DoIt}DoIt;');
  17714. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17715. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17716. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17717. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17718. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17719. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17720. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17721. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17722. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17723. ConvertProgram;
  17724. CheckSource('TestClassOf_TypeCast',
  17725. LinesToStr([ // statements
  17726. 'rtl.createClass(this, "TObject", null, function () {',
  17727. ' this.$init = function () {',
  17728. ' };',
  17729. ' this.$final = function () {',
  17730. ' };',
  17731. ' this.DoIt = function () {',
  17732. ' this.DoIt();',
  17733. ' this.DoIt$1();',
  17734. ' };',
  17735. '});',
  17736. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17737. ' this.DoIt$1 = function () {',
  17738. ' this.DoIt();',
  17739. ' this.DoIt$1();',
  17740. ' this.DoIt$2();',
  17741. ' };',
  17742. '});',
  17743. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17744. ' this.DoIt$2 = function () {',
  17745. ' };',
  17746. '});',
  17747. 'this.ObjC = null;',
  17748. 'this.MobileC = null;',
  17749. 'this.CarC = null;',
  17750. '']),
  17751. LinesToStr([ // $mod.$main
  17752. '$mod.ObjC.DoIt();',
  17753. '$mod.MobileC.DoIt$1();',
  17754. '$mod.CarC.DoIt$2();',
  17755. '$mod.ObjC.DoIt();',
  17756. '$mod.ObjC.DoIt$1();',
  17757. '$mod.ObjC.DoIt$2();',
  17758. '$mod.MobileC.DoIt();',
  17759. '$mod.MobileC.DoIt$1();',
  17760. '$mod.MobileC.DoIt$2();',
  17761. '$mod.CarC.DoIt();',
  17762. '$mod.CarC.DoIt$1();',
  17763. '$mod.CarC.DoIt$2();',
  17764. '']));
  17765. end;
  17766. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17767. begin
  17768. StartProgram(false);
  17769. Add('type');
  17770. Add(' TObject = class');
  17771. Add(' function CurNow: longint; ');
  17772. Add(' class function Now: longint; ');
  17773. Add(' end;');
  17774. Add('function TObject.CurNow: longint; begin end;');
  17775. Add('class function TObject.Now: longint; begin end;');
  17776. Add('var');
  17777. Add(' Obj: tobject;');
  17778. Add(' vI: longint;');
  17779. Add('begin');
  17780. Add(' obj.curnow;');
  17781. Add(' vi:=obj.curnow;');
  17782. Add(' tobject.now;');
  17783. Add(' vi:=tobject.now;');
  17784. ConvertProgram;
  17785. CheckSource('TestClassOf_ImplicitFunctionCall',
  17786. LinesToStr([ // statements
  17787. 'rtl.createClass(this, "TObject", null, function () {',
  17788. ' this.$init = function () {',
  17789. ' };',
  17790. ' this.$final = function () {',
  17791. ' };',
  17792. ' this.CurNow = function () {',
  17793. ' var Result = 0;',
  17794. ' return Result;',
  17795. ' };',
  17796. ' this.Now = function () {',
  17797. ' var Result = 0;',
  17798. ' return Result;',
  17799. ' };',
  17800. '});',
  17801. 'this.Obj = null;',
  17802. 'this.vI = 0;',
  17803. '']),
  17804. LinesToStr([ // $mod.$main
  17805. '$mod.Obj.CurNow();',
  17806. '$mod.vI = $mod.Obj.CurNow();',
  17807. '$mod.TObject.Now();',
  17808. '$mod.vI = $mod.TObject.Now();',
  17809. '']));
  17810. end;
  17811. procedure TTestModule.TestClassOf_Const;
  17812. begin
  17813. StartProgram(false);
  17814. Add([
  17815. 'type',
  17816. ' TObject = class',
  17817. ' end;',
  17818. ' TBird = TObject;',
  17819. ' TBirds = class of TBird;',
  17820. ' TEagles = TBirds;',
  17821. ' THawk = class(TBird);',
  17822. 'const',
  17823. ' Hawk: TEagles = THawk;',
  17824. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17825. ' TBird,',
  17826. ' THawk',
  17827. ' );',
  17828. 'begin']);
  17829. ConvertProgram;
  17830. CheckSource('TestClassOf_Const',
  17831. LinesToStr([ // statements
  17832. 'rtl.createClass(this, "TObject", null, function () {',
  17833. ' this.$init = function () {',
  17834. ' };',
  17835. ' this.$final = function () {',
  17836. ' };',
  17837. '});',
  17838. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17839. '});',
  17840. 'this.Hawk = this.THawk;',
  17841. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17842. '']),
  17843. LinesToStr([ // $mod.$main
  17844. '']));
  17845. end;
  17846. procedure TTestModule.TestNestedClass_Alias;
  17847. begin
  17848. WithTypeInfo:=true;
  17849. StartProgram(false);
  17850. Add([
  17851. 'type',
  17852. ' TObject = class',
  17853. ' type TNested = type longint;',
  17854. ' end;',
  17855. 'type TAlias = type tobject.tnested;',
  17856. 'var i: tobject.tnested = 3;',
  17857. 'var j: TAlias = 4;',
  17858. 'begin',
  17859. ' if typeinfo(TAlias)=nil then ;',
  17860. ' if typeinfo(tobject.tnested)=nil then ;',
  17861. '']);
  17862. ConvertProgram;
  17863. CheckSource('TestNestedClass_Alias',
  17864. LinesToStr([ // statements
  17865. 'rtl.createClass(this, "TObject", null, function () {',
  17866. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17867. ' this.$init = function () {',
  17868. ' };',
  17869. ' this.$final = function () {',
  17870. ' };',
  17871. '});',
  17872. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17873. 'this.i = 3;',
  17874. 'this.j = 4;',
  17875. '']),
  17876. LinesToStr([ // $mod.$main
  17877. 'if ($mod.$rtti["TAlias"] === null) ;',
  17878. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17879. '']));
  17880. end;
  17881. procedure TTestModule.TestNestedClass_Record;
  17882. begin
  17883. WithTypeInfo:=true;
  17884. StartProgram(false);
  17885. Add([
  17886. 'type',
  17887. ' TObject = class',
  17888. ' type TPoint = record',
  17889. ' x,y: byte;',
  17890. ' end;',
  17891. ' procedure DoIt(t: TPoint);',
  17892. ' end;',
  17893. 'procedure tobject.DoIt(t: TPoint);',
  17894. 'var p: TPoint;',
  17895. 'begin',
  17896. ' t.x:=t.y;',
  17897. ' p:=t;',
  17898. 'end;',
  17899. 'var',
  17900. ' p: tobject.tpoint = (x:2; y:4);',
  17901. ' o: TObject;',
  17902. 'begin',
  17903. ' p:=p;',
  17904. ' o.doit(p);',
  17905. '']);
  17906. ConvertProgram;
  17907. CheckSource('TestNestedClass_Record',
  17908. LinesToStr([ // statements
  17909. 'rtl.createClass(this, "TObject", null, function () {',
  17910. ' rtl.recNewT(this, "TPoint", function () {',
  17911. ' this.x = 0;',
  17912. ' this.y = 0;',
  17913. ' this.$eq = function (b) {',
  17914. ' return (this.x === b.x) && (this.y === b.y);',
  17915. ' };',
  17916. ' this.$assign = function (s) {',
  17917. ' this.x = s.x;',
  17918. ' this.y = s.y;',
  17919. ' return this;',
  17920. ' };',
  17921. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17922. ' $r.addField("x", rtl.byte);',
  17923. ' $r.addField("y", rtl.byte);',
  17924. ' });',
  17925. ' this.$init = function () {',
  17926. ' };',
  17927. ' this.$final = function () {',
  17928. ' };',
  17929. ' this.DoIt = function (t) {',
  17930. ' var p = this.TPoint.$new();',
  17931. ' t.x = t.y;',
  17932. ' p.$assign(t);',
  17933. ' };',
  17934. '});',
  17935. 'this.p = this.TObject.TPoint.$clone({',
  17936. ' x: 2,',
  17937. ' y: 4',
  17938. '});',
  17939. 'this.o = null;',
  17940. '']),
  17941. LinesToStr([ // $mod.$main
  17942. '$mod.p.$assign($mod.p);',
  17943. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17944. '']));
  17945. end;
  17946. procedure TTestModule.TestNestedClass_Class;
  17947. begin
  17948. StartProgram(false);
  17949. Add([
  17950. 'type',
  17951. ' TObject = class end;',
  17952. ' TBird = class',
  17953. ' type TLeg = class',
  17954. ' FId: longint;',
  17955. ' constructor Create;',
  17956. ' function Create(i: longint): TLeg;',
  17957. ' end;',
  17958. ' function DoIt(b: TBird): Tleg;',
  17959. ' end;',
  17960. 'constructor tbird.tleg.create;',
  17961. 'begin',
  17962. ' FId:=3;',
  17963. 'end;',
  17964. 'function tbird.tleg.Create(i: longint): TLeg;',
  17965. 'begin',
  17966. ' Create;',
  17967. ' Result:=TLeg.Create;',
  17968. ' Result:=TBird.TLeg.Create;',
  17969. ' Result:=Create(3);',
  17970. ' FId:=i;',
  17971. 'end;',
  17972. 'function tbird.DoIt(b: tbird): tleg;',
  17973. 'begin',
  17974. ' Result.Create;',
  17975. ' Result:=TLeg.Create;',
  17976. ' Result:=TBird.TLeg.Create;',
  17977. ' Result:=Result.Create(3);',
  17978. 'end;',
  17979. 'var',
  17980. ' b: Tbird.tleg;',
  17981. 'begin',
  17982. ' b.Create;',
  17983. ' b:=TBird.TLeg.Create;',
  17984. ' b:=b.Create(3);',
  17985. '']);
  17986. ConvertProgram;
  17987. CheckSource('TestNestedClass_Class',
  17988. LinesToStr([ // statements
  17989. 'rtl.createClass(this, "TObject", null, function () {',
  17990. ' this.$init = function () {',
  17991. ' };',
  17992. ' this.$final = function () {',
  17993. ' };',
  17994. '});',
  17995. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17996. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  17997. ' this.$init = function () {',
  17998. ' $mod.TObject.$init.call(this);',
  17999. ' this.FId = 0;',
  18000. ' };',
  18001. ' this.Create = function () {',
  18002. ' this.FId = 3;',
  18003. ' return this;',
  18004. ' };',
  18005. ' this.Create$1 = function (i) {',
  18006. ' var Result = null;',
  18007. ' this.Create();',
  18008. ' Result = $mod.TBird.TLeg.$create("Create");',
  18009. ' Result = $mod.TBird.TLeg.$create("Create");',
  18010. ' Result = this.Create$1(3);',
  18011. ' this.FId = i;',
  18012. ' return Result;',
  18013. ' };',
  18014. ' }, "TBird.TLeg");',
  18015. ' this.DoIt = function (b) {',
  18016. ' var Result = null;',
  18017. ' Result.Create();',
  18018. ' Result = this.TLeg.$create("Create");',
  18019. ' Result = $mod.TBird.TLeg.$create("Create");',
  18020. ' Result = Result.Create$1(3);',
  18021. ' return Result;',
  18022. ' };',
  18023. '});',
  18024. 'this.b = null;',
  18025. '']),
  18026. LinesToStr([ // $mod.$main
  18027. '$mod.b.Create();',
  18028. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18029. '$mod.b = $mod.b.Create$1(3);',
  18030. '']));
  18031. end;
  18032. procedure TTestModule.TestNestedClass_CallInherited;
  18033. begin
  18034. StartProgram(false);
  18035. Add([
  18036. 'type',
  18037. ' TObject = class end;',
  18038. ' TBird = class',
  18039. ' type',
  18040. ' TWing = class',
  18041. ' function Fly(w: word = 17): word; virtual;',
  18042. ' end;',
  18043. ' end;',
  18044. ' TEagle = class(TBird)',
  18045. ' type',
  18046. ' TEagleWing = class(TWing)',
  18047. ' function Fly(w: word): word; override;',
  18048. ' end;',
  18049. ' end;',
  18050. 'function TBird.TWing.Fly(w: word): word;',
  18051. 'begin',
  18052. 'end;',
  18053. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18054. 'begin',
  18055. ' inherited;',
  18056. ' inherited Fly;',
  18057. ' inherited Fly(3);',
  18058. ' Result:=inherited Fly;',
  18059. ' Result:=inherited Fly(4);',
  18060. 'end;',
  18061. 'begin',
  18062. '']);
  18063. ConvertProgram;
  18064. CheckSource('TestNestedClass_CallInherited',
  18065. LinesToStr([ // statements
  18066. 'rtl.createClass(this, "TObject", null, function () {',
  18067. ' this.$init = function () {',
  18068. ' };',
  18069. ' this.$final = function () {',
  18070. ' };',
  18071. '});',
  18072. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18073. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18074. ' this.Fly = function (w) {',
  18075. ' var Result = 0;',
  18076. ' return Result;',
  18077. ' };',
  18078. ' }, "TBird.TWing");',
  18079. '});',
  18080. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18081. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18082. ' this.Fly = function (w) {',
  18083. ' var Result = 0;',
  18084. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18085. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18086. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18087. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18088. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18089. ' return Result;',
  18090. ' };',
  18091. ' }, "TEagle.TEagleWing");',
  18092. '});',
  18093. '']),
  18094. LinesToStr([ // $mod.$main
  18095. '']));
  18096. end;
  18097. procedure TTestModule.TestExternalClass_Var;
  18098. begin
  18099. StartProgram(false);
  18100. Add([
  18101. '{$modeswitch externalclass}',
  18102. 'type',
  18103. ' TExtA = class external name ''ExtObj''',
  18104. ' Id: longint external name ''$Id'';',
  18105. ' B: longint;',
  18106. ' end;',
  18107. 'var Obj: TExtA;',
  18108. 'begin',
  18109. ' obj.id:=obj.id+1;',
  18110. ' obj.B:=obj.B+1;']);
  18111. ConvertProgram;
  18112. CheckSource('TestExternalClass_Var',
  18113. LinesToStr([ // statements
  18114. 'this.Obj = null;',
  18115. '']),
  18116. LinesToStr([ // $mod.$main
  18117. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18118. '$mod.Obj.B = $mod.Obj.B + 1;',
  18119. '']));
  18120. end;
  18121. procedure TTestModule.TestExternalClass_Const;
  18122. begin
  18123. StartProgram(false);
  18124. Add([
  18125. '{$modeswitch externalclass}',
  18126. 'type',
  18127. ' TExtA = class external name ''ExtObj''',
  18128. ' const Two: longint = 2;',
  18129. ' const Three = 3;',
  18130. ' const Id: longint;',
  18131. ' end;',
  18132. ' TExtB = class external name ''ExtB''',
  18133. ' A: TExtA;',
  18134. ' end;',
  18135. 'var',
  18136. ' A: texta;',
  18137. ' B: textb;',
  18138. ' i: longint;',
  18139. 'begin',
  18140. ' i:=a.two;',
  18141. ' i:=texta.two;',
  18142. ' i:=a.three;',
  18143. ' i:=texta.three;',
  18144. ' i:=a.id;',
  18145. ' i:=texta.id;',
  18146. '']);
  18147. ConvertProgram;
  18148. CheckSource('TestExternalClass_Const',
  18149. LinesToStr([ // statements
  18150. 'this.A = null;',
  18151. 'this.B = null;',
  18152. 'this.i = 0;',
  18153. '']),
  18154. LinesToStr([ // $mod.$main
  18155. '$mod.i = 2;',
  18156. '$mod.i = 2;',
  18157. '$mod.i = 3;',
  18158. '$mod.i = 3;',
  18159. '$mod.i = $mod.A.Id;',
  18160. '$mod.i = ExtObj.Id;',
  18161. '']));
  18162. end;
  18163. procedure TTestModule.TestExternalClass_Dollar;
  18164. begin
  18165. StartProgram(false);
  18166. Add([
  18167. '{$modeswitch externalclass}',
  18168. 'type',
  18169. ' TExtA = class external name ''$''',
  18170. ' Id: longint external name ''$'';',
  18171. ' function Bla(i: longint): longint; external name ''$'';',
  18172. ' end;',
  18173. 'function dollar(k: longint): longint; external name ''$'';',
  18174. 'var Obj: TExtA;',
  18175. 'begin',
  18176. ' dollar(1);',
  18177. ' obj.id:=obj.id+2;',
  18178. ' obj.Bla(3);',
  18179. '']);
  18180. ConvertProgram;
  18181. CheckSource('TestExternalClass_Dollar',
  18182. LinesToStr([ // statements
  18183. 'this.Obj = null;',
  18184. '']),
  18185. LinesToStr([ // $mod.$main
  18186. '$(1);',
  18187. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18188. '$mod.Obj.$(3);',
  18189. '']));
  18190. end;
  18191. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18192. begin
  18193. StartProgram(false);
  18194. Add('{$modeswitch externalclass}');
  18195. Add('type');
  18196. Add(' TExtA = class external name ''ExtA''');
  18197. Add(' Id: longint external name ''$Id'';');
  18198. Add(' end;');
  18199. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18200. Add(' Id: longint;');
  18201. Add(' end;');
  18202. Add('begin');
  18203. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18204. ConvertProgram;
  18205. end;
  18206. procedure TTestModule.TestExternalClass_Method;
  18207. begin
  18208. StartProgram(false);
  18209. Add(['{$modeswitch externalclass}',
  18210. 'type',
  18211. ' TExtA = class external name ''ExtObj''',
  18212. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18213. ' procedure DoSome(Id: longint = 1);',
  18214. ' end;',
  18215. 'var Obj: texta;',
  18216. 'begin',
  18217. ' obj.doit;',
  18218. ' obj.doit();',
  18219. ' obj.doit(2);',
  18220. ' with obj do begin',
  18221. ' doit;',
  18222. ' doit();',
  18223. ' doit(3);',
  18224. ' end;']);
  18225. ConvertProgram;
  18226. CheckSource('TestExternalClass_Method',
  18227. LinesToStr([ // statements
  18228. 'this.Obj = null;',
  18229. '']),
  18230. LinesToStr([ // $mod.$main
  18231. '$mod.Obj.$Execute(1);',
  18232. '$mod.Obj.$Execute(1);',
  18233. '$mod.Obj.$Execute(2);',
  18234. 'var $with = $mod.Obj;',
  18235. '$with.$Execute(1);',
  18236. '$with.$Execute(1);',
  18237. '$with.$Execute(3);',
  18238. '']));
  18239. end;
  18240. procedure TTestModule.TestExternalClass_ClassMethod;
  18241. begin
  18242. StartProgram(false);
  18243. Add([
  18244. '{$modeswitch externalclass}',
  18245. 'type',
  18246. ' TExtA = class external name ''ExtObj''',
  18247. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18248. ' end;',
  18249. ' TExtB = TExtA;',
  18250. 'var p: Pointer;',
  18251. 'begin',
  18252. ' texta.doit;',
  18253. ' texta.doit();',
  18254. ' texta.doit(2);',
  18255. ' p:[email protected];',
  18256. ' with texta do begin',
  18257. ' doit;',
  18258. ' doit();',
  18259. ' doit(3);',
  18260. ' p:=@DoIt;',
  18261. ' end;',
  18262. ' textb.doit;',
  18263. ' textb.doit();',
  18264. ' textb.doit(4);',
  18265. ' with textb do begin',
  18266. ' doit;',
  18267. ' doit();',
  18268. ' doit(5);',
  18269. ' end;',
  18270. '']);
  18271. ConvertProgram;
  18272. CheckSource('TestExternalClass_ClassMethod',
  18273. LinesToStr([ // statements
  18274. 'this.p = null;',
  18275. '']),
  18276. LinesToStr([ // $mod.$main
  18277. 'ExtObj.$Execute(1);',
  18278. 'ExtObj.$Execute(1);',
  18279. 'ExtObj.$Execute(2);',
  18280. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18281. 'ExtObj.$Execute(1);',
  18282. 'ExtObj.$Execute(1);',
  18283. 'ExtObj.$Execute(3);',
  18284. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18285. 'ExtObj.$Execute(1);',
  18286. 'ExtObj.$Execute(1);',
  18287. 'ExtObj.$Execute(4);',
  18288. 'ExtObj.$Execute(1);',
  18289. 'ExtObj.$Execute(1);',
  18290. 'ExtObj.$Execute(5);',
  18291. '']));
  18292. end;
  18293. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18294. begin
  18295. StartProgram(false);
  18296. Add([
  18297. '{$modeswitch externalclass}',
  18298. 'type',
  18299. ' TExtA = class external name ''ExtObj''',
  18300. ' class procedure DoIt(Id: longint = 1); static;',
  18301. ' end;',
  18302. 'var p: Pointer;',
  18303. 'begin',
  18304. ' texta.doit;',
  18305. ' texta.doit();',
  18306. ' texta.doit(2);',
  18307. ' p:[email protected];',
  18308. ' with texta do begin',
  18309. ' doit;',
  18310. ' doit();',
  18311. ' doit(3);',
  18312. ' p:=@DoIt;',
  18313. ' end;',
  18314. '']);
  18315. ConvertProgram;
  18316. CheckSource('TestExternalClass_ClassMethodStatic',
  18317. LinesToStr([ // statements
  18318. 'this.p = null;',
  18319. '']),
  18320. LinesToStr([ // $mod.$main
  18321. 'ExtObj.DoIt(1);',
  18322. 'ExtObj.DoIt(1);',
  18323. 'ExtObj.DoIt(2);',
  18324. '$mod.p = ExtObj.DoIt;',
  18325. 'ExtObj.DoIt(1);',
  18326. 'ExtObj.DoIt(1);',
  18327. 'ExtObj.DoIt(3);',
  18328. '$mod.p = ExtObj.DoIt;',
  18329. '']));
  18330. end;
  18331. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18332. begin
  18333. StartProgram(false);
  18334. Add([
  18335. '{$modeswitch externalclass}',
  18336. 'type',
  18337. ' TBird = class external name ''Array''',
  18338. ' end;',
  18339. 'function GetPtr: Pointer;',
  18340. 'begin',
  18341. 'end;',
  18342. 'procedure Write(const p);',
  18343. 'begin',
  18344. 'end;',
  18345. 'procedure WriteLn; varargs;',
  18346. 'begin',
  18347. 'end;',
  18348. 'begin',
  18349. ' if TBird(GetPtr)=nil then ;',
  18350. ' Write(GetPtr);',
  18351. ' WriteLn(GetPtr);',
  18352. ' Write(TBird(GetPtr));',
  18353. ' WriteLn(TBird(GetPtr));',
  18354. '']);
  18355. ConvertProgram;
  18356. CheckSource('TestFunctionResultInTypeCast',
  18357. LinesToStr([ // statements
  18358. 'this.GetPtr = function () {',
  18359. ' var Result = null;',
  18360. ' return Result;',
  18361. '};',
  18362. 'this.Write = function (p) {',
  18363. '};',
  18364. 'this.WriteLn = function () {',
  18365. '};',
  18366. '']),
  18367. LinesToStr([
  18368. 'if ($mod.GetPtr() === null) ;',
  18369. '$mod.Write($mod.GetPtr());',
  18370. '$mod.WriteLn($mod.GetPtr());',
  18371. '$mod.Write($mod.GetPtr());',
  18372. '$mod.WriteLn($mod.GetPtr());',
  18373. '']));
  18374. end;
  18375. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18376. begin
  18377. StartProgram(false);
  18378. Add([
  18379. '{$modeswitch externalclass}',
  18380. 'type',
  18381. ' TExtA = class external name ''ExtObjA''',
  18382. ' procedure ProcA; virtual;',
  18383. ' procedure ProcB; virtual;',
  18384. ' end;',
  18385. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18386. ' end;',
  18387. ' TExtC = class (TExtB)',
  18388. ' procedure ProcA; override;',
  18389. ' end;',
  18390. 'procedure TExtC.ProcA;',
  18391. 'begin',
  18392. ' ProcA;',
  18393. ' Self.ProcA;',
  18394. ' ProcB;',
  18395. ' Self.ProcB;',
  18396. 'end;',
  18397. 'var',
  18398. ' A: texta;',
  18399. ' B: textb;',
  18400. ' C: textc;',
  18401. 'begin',
  18402. ' a.proca;',
  18403. ' b.proca;',
  18404. ' c.proca;']);
  18405. ConvertProgram;
  18406. CheckSource('TestExternalClass_NonExternalOverride',
  18407. LinesToStr([ // statements
  18408. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18409. ' this.$init = function () {',
  18410. ' };',
  18411. ' this.$final = function () {',
  18412. ' };',
  18413. ' this.ProcA = function () {',
  18414. ' this.ProcA();',
  18415. ' this.ProcA();',
  18416. ' this.ProcB();',
  18417. ' this.ProcB();',
  18418. ' };',
  18419. '});',
  18420. 'this.A = null;',
  18421. 'this.B = null;',
  18422. 'this.C = null;',
  18423. '']),
  18424. LinesToStr([ // $mod.$main
  18425. '$mod.A.ProcA();',
  18426. '$mod.B.ProcA();',
  18427. '$mod.C.ProcA();',
  18428. '']));
  18429. end;
  18430. procedure TTestModule.TestExternalClass_OverloadHint;
  18431. begin
  18432. StartProgram(false);
  18433. Add([
  18434. '{$modeswitch externalclass}',
  18435. 'type',
  18436. ' TExtA = class external name ''ExtObjA''',
  18437. ' procedure DoIt;',
  18438. ' procedure DoIt(i: longint);',
  18439. ' end;',
  18440. 'begin',
  18441. '']);
  18442. ConvertProgram;
  18443. CheckResolverUnexpectedHints(true);
  18444. CheckSource('TestExternalClass_OverloadHint',
  18445. LinesToStr([ // statements
  18446. '']),
  18447. LinesToStr([ // $mod.$main
  18448. '']));
  18449. end;
  18450. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18451. begin
  18452. WithTypeInfo:=true;
  18453. StartProgram(false);
  18454. Add([
  18455. '{$modeswitch externalclass}',
  18456. 'type',
  18457. ' JSwiper = class external name ''Swiper''',
  18458. ' constructor New;',
  18459. ' end;',
  18460. ' TObject = class',
  18461. ' private',
  18462. ' FSwiper: JSwiper;',
  18463. ' published',
  18464. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18465. ' end;',
  18466. 'begin',
  18467. ' JSwiper.new;',
  18468. '']);
  18469. ConvertProgram;
  18470. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18471. LinesToStr([ // statements
  18472. 'this.$rtti.$ExtClass("JSwiper", {',
  18473. ' jsclass: "Swiper"',
  18474. '});',
  18475. 'rtl.createClass(this, "TObject", null, function () {',
  18476. ' this.$init = function () {',
  18477. ' this.FSwiper = null;',
  18478. ' };',
  18479. ' this.$final = function () {',
  18480. ' this.FSwiper = undefined;',
  18481. ' };',
  18482. ' var $r = this.$rtti;',
  18483. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18484. '});',
  18485. '']),
  18486. LinesToStr([ // $mod.$main
  18487. 'new Swiper();',
  18488. '']));
  18489. end;
  18490. procedure TTestModule.TestExternalClass_Property;
  18491. begin
  18492. StartProgram(false);
  18493. Add([
  18494. '{$modeswitch externalclass}',
  18495. 'type',
  18496. ' TExtA = class external name ''ExtA''',
  18497. ' function getYear: longint;',
  18498. ' procedure setYear(Value: longint);',
  18499. ' property Year: longint read getyear write setyear;',
  18500. ' end;',
  18501. ' TExtB = class (TExtA)',
  18502. ' procedure OtherSetYear(Value: longint);',
  18503. ' property year write othersetyear;',
  18504. ' end;',
  18505. 'procedure textb.othersetyear(value: longint);',
  18506. 'begin',
  18507. ' setYear(Value+4);',
  18508. 'end;',
  18509. 'var',
  18510. ' A: texta;',
  18511. ' B: textb;',
  18512. 'begin',
  18513. ' a.year:=a.year+1;',
  18514. ' b.year:=b.year+2;']);
  18515. ConvertProgram;
  18516. CheckSource('TestExternalClass_NonExternalOverride',
  18517. LinesToStr([ // statements
  18518. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18519. ' this.$init = function () {',
  18520. ' };',
  18521. ' this.$final = function () {',
  18522. ' };',
  18523. ' this.OtherSetYear = function (Value) {',
  18524. ' this.setYear(Value+4);',
  18525. ' };',
  18526. '});',
  18527. 'this.A = null;',
  18528. 'this.B = null;',
  18529. '']),
  18530. LinesToStr([ // $mod.$main
  18531. '$mod.A.setYear($mod.A.getYear()+1);',
  18532. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18533. '']));
  18534. end;
  18535. procedure TTestModule.TestExternalClass_PropertyDate;
  18536. begin
  18537. StartProgram(false);
  18538. Add([
  18539. '{$modeswitch externalclass}',
  18540. 'type',
  18541. ' TExtA = class external name ''ExtA''',
  18542. ' end;',
  18543. ' TExtB = class (TExtA)',
  18544. ' FDate: string;',
  18545. ' property Date: string read FDate write FDate;',
  18546. ' property ExtA: string read FDate write FDate;',
  18547. ' end;',
  18548. ' {$M+}',
  18549. ' TObject = class',
  18550. ' FDate: string;',
  18551. ' published',
  18552. ' property Date: string read FDate write FDate;',
  18553. ' property ExtA: string read FDate write FDate;',
  18554. ' end;',
  18555. 'var',
  18556. ' B: textb;',
  18557. ' o: TObject;',
  18558. 'begin',
  18559. ' b.date:=b.exta;',
  18560. ' o.date:=o.exta;']);
  18561. ConvertProgram;
  18562. CheckSource('TestExternalClass_PropertyDate',
  18563. LinesToStr([ // statements
  18564. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18565. ' this.$init = function () {',
  18566. ' this.FDate = "";',
  18567. ' };',
  18568. ' this.$final = function () {',
  18569. ' };',
  18570. '});',
  18571. 'rtl.createClass(this, "TObject", null, function () {',
  18572. ' this.$init = function () {',
  18573. ' this.FDate = "";',
  18574. ' };',
  18575. ' this.$final = function () {',
  18576. ' };',
  18577. ' var $r = this.$rtti;',
  18578. ' $r.addField("FDate", rtl.string);',
  18579. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18580. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18581. '});',
  18582. 'this.B = null;',
  18583. 'this.o = null;',
  18584. '']),
  18585. LinesToStr([ // $mod.$main
  18586. '$mod.B.FDate = $mod.B.FDate;',
  18587. '$mod.o.FDate = $mod.o.FDate;',
  18588. '']));
  18589. end;
  18590. procedure TTestModule.TestExternalClass_ClassProperty;
  18591. begin
  18592. StartProgram(false);
  18593. Add('{$modeswitch externalclass}');
  18594. Add('type');
  18595. Add(' TExtA = class external name ''ExtA''');
  18596. Add(' class function getYear: longint;');
  18597. Add(' class procedure setYear(Value: longint);');
  18598. Add(' class property Year: longint read getyear write setyear;');
  18599. Add(' end;');
  18600. Add(' TExtB = class (TExtA)');
  18601. Add(' class function GetCentury: longint;');
  18602. Add(' class procedure SetCentury(Value: longint);');
  18603. Add(' class property Century: longint read getcentury write setcentury;');
  18604. Add(' end;');
  18605. Add('class function textb.getcentury: longint;');
  18606. Add('begin');
  18607. Add('end;');
  18608. Add('class procedure textb.setcentury(value: longint);');
  18609. Add('begin');
  18610. Add(' setyear(value+11);');
  18611. Add(' texta.year:=texta.year+12;');
  18612. Add(' year:=year+13;');
  18613. Add(' textb.century:=textb.century+14;');
  18614. Add(' century:=century+15;');
  18615. Add('end;');
  18616. Add('var');
  18617. Add(' A: texta;');
  18618. Add(' B: textb;');
  18619. Add('begin');
  18620. Add(' texta.year:=texta.year+1;');
  18621. Add(' textb.year:=textb.year+2;');
  18622. Add(' TextA.year:=TextA.year+3;');
  18623. Add(' b.year:=b.year+4;');
  18624. Add(' textb.century:=textb.century+5;');
  18625. Add(' b.century:=b.century+6;');
  18626. ConvertProgram;
  18627. CheckSource('TestExternalClass_ClassProperty',
  18628. LinesToStr([ // statements
  18629. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18630. ' this.$init = function () {',
  18631. ' };',
  18632. ' this.$final = function () {',
  18633. ' };',
  18634. ' this.GetCentury = function () {',
  18635. ' var Result = 0;',
  18636. ' return Result;',
  18637. ' };',
  18638. ' this.SetCentury = function (Value) {',
  18639. ' this.setYear(Value + 11);',
  18640. ' ExtA.setYear(ExtA.getYear() + 12);',
  18641. ' this.setYear(this.getYear() + 13);',
  18642. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18643. ' this.SetCentury(this.GetCentury() + 15);',
  18644. ' };',
  18645. '});',
  18646. 'this.A = null;',
  18647. 'this.B = null;',
  18648. '']),
  18649. LinesToStr([ // $mod.$main
  18650. 'ExtA.setYear(ExtA.getYear() + 1);',
  18651. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18652. 'ExtA.setYear(ExtA.getYear() + 3);',
  18653. '$mod.B.setYear($mod.B.getYear() + 4);',
  18654. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18655. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18656. '']));
  18657. end;
  18658. procedure TTestModule.TestExternalClass_ClassOf;
  18659. begin
  18660. StartProgram(false);
  18661. Add('{$modeswitch externalclass}');
  18662. Add('type');
  18663. Add(' TExtA = class external name ''ExtA''');
  18664. Add(' procedure ProcA; virtual;');
  18665. Add(' procedure ProcB; virtual;');
  18666. Add(' end;');
  18667. Add(' TExtAClass = class of TExtA;');
  18668. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18669. Add(' end;');
  18670. Add(' TExtBClass = class of TExtB;');
  18671. Add(' TExtC = class (TExtB)');
  18672. Add(' procedure ProcA; override;');
  18673. Add(' end;');
  18674. Add(' TExtCClass = class of TExtC;');
  18675. Add('procedure TExtC.ProcA; begin end;');
  18676. Add('var');
  18677. Add(' A: texta; ClA: TExtAClass;');
  18678. Add(' B: textb; ClB: TExtBClass;');
  18679. Add(' C: textc; ClC: TExtCClass;');
  18680. Add('begin');
  18681. Add(' ClA:=texta;');
  18682. Add(' ClA:=textb;');
  18683. Add(' ClA:=textc;');
  18684. Add(' ClB:=textb;');
  18685. Add(' ClB:=textc;');
  18686. Add(' ClC:=textc;');
  18687. ConvertProgram;
  18688. CheckSource('TestExternalClass_ClassOf',
  18689. LinesToStr([ // statements
  18690. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18691. ' this.$init = function () {',
  18692. ' };',
  18693. ' this.$final = function () {',
  18694. ' };',
  18695. ' this.ProcA = function () {',
  18696. ' };',
  18697. '});',
  18698. 'this.A = null;',
  18699. 'this.ClA = null;',
  18700. 'this.B = null;',
  18701. 'this.ClB = null;',
  18702. 'this.C = null;',
  18703. 'this.ClC = null;',
  18704. '']),
  18705. LinesToStr([ // $mod.$main
  18706. '$mod.ClA = ExtA;',
  18707. '$mod.ClA = ExtB;',
  18708. '$mod.ClA = $mod.TExtC;',
  18709. '$mod.ClB = ExtB;',
  18710. '$mod.ClB = $mod.TExtC;',
  18711. '$mod.ClC = $mod.TExtC;',
  18712. '']));
  18713. end;
  18714. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18715. begin
  18716. AddModuleWithIntfImplSrc('unit2.pas',
  18717. LinesToStr([
  18718. '{$modeswitch externalclass}',
  18719. 'type',
  18720. ' TExtA = class external name ''ExtA''',
  18721. ' class var Id: longint;',
  18722. ' end;',
  18723. '']),
  18724. '');
  18725. StartUnit(true);
  18726. Add('interface');
  18727. Add('uses unit2;');
  18728. Add('implementation');
  18729. Add('begin');
  18730. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18731. ConvertUnit;
  18732. CheckSource('TestExternalClass_ClassOtherUnit',
  18733. LinesToStr([
  18734. '']),
  18735. LinesToStr([
  18736. 'ExtA.Id = ExtA.Id + 1;',
  18737. '']));
  18738. end;
  18739. procedure TTestModule.TestExternalClass_Is;
  18740. begin
  18741. StartProgram(false);
  18742. Add([
  18743. '{$modeswitch externalclass}',
  18744. 'type',
  18745. ' TExtA = class external name ''ExtA''',
  18746. ' end;',
  18747. ' TExtAClass = class of TExtA;',
  18748. ' TExtB = class external name ''ExtB'' (TExtA)',
  18749. ' end;',
  18750. ' TExtBClass = class of TExtB;',
  18751. ' TExtC = class (TExtB)',
  18752. ' end;',
  18753. ' TExtCClass = class of TExtC;',
  18754. 'var',
  18755. ' A: texta; ClA: TExtAClass;',
  18756. ' B: textb; ClB: TExtBClass;',
  18757. ' C: textc; ClC: TExtCClass;',
  18758. 'begin',
  18759. ' if a is textb then ;',
  18760. ' if a is textc then ;',
  18761. ' if b is textc then ;',
  18762. ' if cla is textb then ;',
  18763. ' if cla is textc then ;',
  18764. ' if clb is textc then ;',
  18765. ' try',
  18766. ' except',
  18767. ' on TExtA do ;',
  18768. ' on e: TExtB do ;',
  18769. ' end;',
  18770. '']);
  18771. ConvertProgram;
  18772. CheckSource('TestExternalClass_Is',
  18773. LinesToStr([ // statements
  18774. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18775. ' this.$init = function () {',
  18776. ' };',
  18777. ' this.$final = function () {',
  18778. ' };',
  18779. '});',
  18780. 'this.A = null;',
  18781. 'this.ClA = null;',
  18782. 'this.B = null;',
  18783. 'this.ClB = null;',
  18784. 'this.C = null;',
  18785. 'this.ClC = null;',
  18786. '']),
  18787. LinesToStr([ // $mod.$main
  18788. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18789. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18790. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18791. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18792. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18793. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18794. 'try {} catch ($e) {',
  18795. ' if (rtl.isExt($e,ExtA)) {}',
  18796. ' else if (rtl.isExt($e,ExtB)) {',
  18797. ' var e = $e;',
  18798. ' } else throw $e',
  18799. '};',
  18800. '']));
  18801. end;
  18802. procedure TTestModule.TestExternalClass_As;
  18803. begin
  18804. StartProgram(false);
  18805. Add('{$modeswitch externalclass}');
  18806. Add('type');
  18807. Add(' TExtA = class external name ''ExtA''');
  18808. Add(' end;');
  18809. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18810. Add(' end;');
  18811. Add(' TExtC = class (TExtB)');
  18812. Add(' end;');
  18813. Add('var');
  18814. Add(' A: texta;');
  18815. Add(' B: textb;');
  18816. Add(' C: textc;');
  18817. Add('begin');
  18818. Add(' b:=a as textb;');
  18819. Add(' c:=a as textc;');
  18820. Add(' c:=b as textc;');
  18821. ConvertProgram;
  18822. CheckSource('TestExternalClass_Is',
  18823. LinesToStr([ // statements
  18824. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18825. ' this.$init = function () {',
  18826. ' };',
  18827. ' this.$final = function () {',
  18828. ' };',
  18829. '});',
  18830. 'this.A = null;',
  18831. 'this.B = null;',
  18832. 'this.C = null;',
  18833. '']),
  18834. LinesToStr([ // $mod.$main
  18835. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18836. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18837. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18838. '']));
  18839. end;
  18840. procedure TTestModule.TestExternalClass_DestructorFail;
  18841. begin
  18842. StartProgram(false);
  18843. Add('{$modeswitch externalclass}');
  18844. Add('type');
  18845. Add(' TExtA = class external name ''ExtA''');
  18846. Add(' destructor Free;');
  18847. Add(' end;');
  18848. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18849. nPasElementNotSupported);
  18850. ConvertProgram;
  18851. end;
  18852. procedure TTestModule.TestExternalClass_New;
  18853. begin
  18854. StartProgram(false);
  18855. Add([
  18856. '{$modeswitch externalclass}',
  18857. 'type',
  18858. ' TExtA = class external name ''ExtA''',
  18859. ' constructor New;',
  18860. ' constructor New(i: longint; j: longint = 2);',
  18861. ' end;',
  18862. 'var',
  18863. ' A: texta;',
  18864. 'begin',
  18865. ' a:=texta.new;',
  18866. ' a:=texta(texta.new);',
  18867. ' a:=texta.new();',
  18868. ' a:=texta.new(1);',
  18869. ' with texta do begin',
  18870. ' a:=new;',
  18871. ' a:=new();',
  18872. ' a:=new(2);',
  18873. ' end;',
  18874. ' a:=test1.texta.new;',
  18875. ' a:=test1.texta.new();',
  18876. ' a:=test1.texta.new(3);',
  18877. '']);
  18878. ConvertProgram;
  18879. CheckSource('TestExternalClass_New',
  18880. LinesToStr([ // statements
  18881. 'this.A = null;',
  18882. '']),
  18883. LinesToStr([ // $mod.$main
  18884. '$mod.A = new ExtA();',
  18885. '$mod.A = new ExtA();',
  18886. '$mod.A = new ExtA();',
  18887. '$mod.A = new ExtA(1,2);',
  18888. '$mod.A = new ExtA();',
  18889. '$mod.A = new ExtA();',
  18890. '$mod.A = new ExtA(2,2);',
  18891. '$mod.A = new ExtA();',
  18892. '$mod.A = new ExtA();',
  18893. '$mod.A = new ExtA(3,2);',
  18894. '']));
  18895. end;
  18896. procedure TTestModule.TestExternalClass_ClassOf_New;
  18897. begin
  18898. StartProgram(false);
  18899. Add('{$modeswitch externalclass}');
  18900. Add('type');
  18901. Add(' TExtAClass = class of TExtA;');
  18902. Add(' TExtA = class external name ''ExtA''');
  18903. Add(' C: TExtAClass;');
  18904. Add(' constructor New;');
  18905. Add(' end;');
  18906. Add('var');
  18907. Add(' A: texta;');
  18908. Add(' C: textaclass;');
  18909. Add('begin');
  18910. Add(' a:=c.new;');
  18911. Add(' a:=c.new();');
  18912. Add(' with C do begin');
  18913. Add(' a:=new;');
  18914. Add(' a:=new();');
  18915. Add(' end;');
  18916. Add(' a:=test1.c.new;');
  18917. Add(' a:=test1.c.new();');
  18918. Add(' a:=A.c.new();');
  18919. ConvertProgram;
  18920. CheckSource('TestExternalClass_ClassOf_New',
  18921. LinesToStr([ // statements
  18922. 'this.A = null;',
  18923. 'this.C = null;',
  18924. '']),
  18925. LinesToStr([ // $mod.$main
  18926. '$mod.A = new $mod.C();',
  18927. '$mod.A = new $mod.C();',
  18928. 'var $with = $mod.C;',
  18929. '$mod.A = new $with();',
  18930. '$mod.A = new $with();',
  18931. '$mod.A = new $mod.C();',
  18932. '$mod.A = new $mod.C();',
  18933. '$mod.A = new $mod.A.C();',
  18934. '']));
  18935. end;
  18936. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18937. begin
  18938. StartProgram(false);
  18939. Add([
  18940. '{$modeswitch externalclass}',
  18941. 'type',
  18942. ' TExtAClass = class of TExtA;',
  18943. ' TExtA = class external name ''ExtA''',
  18944. ' constructor New;',
  18945. ' end;',
  18946. 'function GetCreator: TExtAClass;',
  18947. 'begin',
  18948. ' Result:=TExtA;',
  18949. 'end;',
  18950. 'var',
  18951. ' A: texta;',
  18952. 'begin',
  18953. ' a:=getcreator.new;',
  18954. ' a:=getcreator().new;',
  18955. ' a:=getcreator().new();',
  18956. ' a:=getcreator.new();',
  18957. ' with getcreator do begin',
  18958. ' a:=new;',
  18959. ' a:=new();',
  18960. ' end;']);
  18961. ConvertProgram;
  18962. CheckSource('TestExternalClass_FuncClassOf_New',
  18963. LinesToStr([ // statements
  18964. 'this.GetCreator = function () {',
  18965. ' var Result = null;',
  18966. ' Result = ExtA;',
  18967. ' return Result;',
  18968. '};',
  18969. 'this.A = null;',
  18970. '']),
  18971. LinesToStr([ // $mod.$main
  18972. '$mod.A = new ($mod.GetCreator())();',
  18973. '$mod.A = new ($mod.GetCreator())();',
  18974. '$mod.A = new ($mod.GetCreator())();',
  18975. '$mod.A = new ($mod.GetCreator())();',
  18976. 'var $with = $mod.GetCreator();',
  18977. '$mod.A = new $with();',
  18978. '$mod.A = new $with();',
  18979. '']));
  18980. end;
  18981. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18982. begin
  18983. StartProgram(false);
  18984. Add([
  18985. '{$modeswitch externalclass}',
  18986. 'type',
  18987. ' TExtA = class external name ''ExtA''',
  18988. ' constructor New;',
  18989. ' end;',
  18990. ' TBird = class(TExtA)',
  18991. ' end;',
  18992. 'begin',
  18993. ' TBird.new;',
  18994. '']);
  18995. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18996. ConvertProgram;
  18997. end;
  18998. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  18999. begin
  19000. StartProgram(false);
  19001. Add([
  19002. '{$modeswitch externalclass}',
  19003. 'type',
  19004. ' TExtA = class external name ''ExtA''',
  19005. ' constructor New;',
  19006. ' end;',
  19007. ' TBird = class(TExtA)',
  19008. ' end;',
  19009. 'begin',
  19010. ' TBird.new();',
  19011. '']);
  19012. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19013. ConvertProgram;
  19014. end;
  19015. procedure TTestModule.TestExternalClass_NewExtName;
  19016. begin
  19017. StartProgram(false);
  19018. Add([
  19019. '{$modeswitch externalclass}',
  19020. 'type',
  19021. ' TExtA = class external name ''ExtA''',
  19022. ' constructor New; external name ''Other'';',
  19023. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19024. ' end;',
  19025. 'var',
  19026. ' A: texta;',
  19027. 'begin',
  19028. ' a:=texta.new;',
  19029. ' a:=texta(texta.new);',
  19030. ' a:=texta.new();',
  19031. ' a:=texta.new(1);',
  19032. ' with texta do begin',
  19033. ' a:=new;',
  19034. ' a:=new();',
  19035. ' a:=new(2);',
  19036. ' end;',
  19037. ' a:=test1.texta.new;',
  19038. ' a:=test1.texta.new();',
  19039. ' a:=test1.texta.new(3);',
  19040. '']);
  19041. ConvertProgram;
  19042. CheckSource('TestExternalClass_NewExtName',
  19043. LinesToStr([ // statements
  19044. 'this.A = null;',
  19045. '']),
  19046. LinesToStr([ // $mod.$main
  19047. '$mod.A = new Other();',
  19048. '$mod.A = new Other();',
  19049. '$mod.A = new Other();',
  19050. '$mod.A = new A.B(1,2);',
  19051. '$mod.A = new Other();',
  19052. '$mod.A = new Other();',
  19053. '$mod.A = new A.B(2,2);',
  19054. '$mod.A = new Other();',
  19055. '$mod.A = new Other();',
  19056. '$mod.A = new A.B(3,2);',
  19057. '']));
  19058. end;
  19059. procedure TTestModule.TestExternalClass_Constructor;
  19060. begin
  19061. StartProgram(false);
  19062. Add([
  19063. '{$modeswitch externalclass}',
  19064. 'type',
  19065. ' TExtA = class external name ''ExtA''',
  19066. ' public type',
  19067. ' TExtB = class external name ''ExtB''',
  19068. ' public type',
  19069. ' TExtC = class external name ''ExtC''',
  19070. ' constructor New;',
  19071. ' constructor New(i: word);',
  19072. ' end;',
  19073. ' end;',
  19074. ' constructor Create;',
  19075. ' constructor Create(i: longint; j: longint = 2);',
  19076. ' end;',
  19077. 'var',
  19078. ' A: texta;',
  19079. ' C: texta.textb.textc;',
  19080. 'begin',
  19081. ' a:=texta.create;',
  19082. ' a:=texta(texta.create);',
  19083. ' a:=texta.create();',
  19084. ' a:=texta.create(1);',
  19085. ' with texta do begin',
  19086. ' a:=create;',
  19087. ' a:=create();',
  19088. ' a:=create(2);',
  19089. ' end;',
  19090. ' a:=test1.texta.create;',
  19091. ' a:=test1.texta.create();',
  19092. ' a:=test1.texta.create(3);',
  19093. ' c:=texta.textb.textc.new;',
  19094. ' c:=texta.textb.textc.new();',
  19095. ' c:=texta.textb.textc.new(4);',
  19096. '']);
  19097. ConvertProgram;
  19098. CheckSource('TestExternalClass_Constructor',
  19099. LinesToStr([ // statements
  19100. 'this.A = null;',
  19101. 'this.C = null;',
  19102. '']),
  19103. LinesToStr([ // $mod.$main
  19104. '$mod.A = new ExtA.Create();',
  19105. '$mod.A = new ExtA.Create();',
  19106. '$mod.A = new ExtA.Create();',
  19107. '$mod.A = new ExtA.Create(1,2);',
  19108. '$mod.A = new ExtA.Create();',
  19109. '$mod.A = new ExtA.Create();',
  19110. '$mod.A = new ExtA.Create(2,2);',
  19111. '$mod.A = new ExtA.Create();',
  19112. '$mod.A = new ExtA.Create();',
  19113. '$mod.A = new ExtA.Create(3,2);',
  19114. '$mod.C = new ExtA.ExtB.ExtC();',
  19115. '$mod.C = new ExtA.ExtB.ExtC();',
  19116. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19117. '']));
  19118. end;
  19119. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19120. begin
  19121. StartProgram(false);
  19122. Add([
  19123. '{$modeswitch externalclass}',
  19124. 'type',
  19125. ' TExtA = class external name ''ExtA''',
  19126. ' constructor Create; external name ''{}'';',
  19127. ' end;',
  19128. 'var',
  19129. ' A: texta;',
  19130. 'begin',
  19131. ' a:=texta.create;',
  19132. ' a:=texta(texta.create);',
  19133. ' a:=texta.create();',
  19134. ' with texta do begin',
  19135. ' a:=create;',
  19136. ' a:=create();',
  19137. ' end;',
  19138. ' a:=test1.texta.create;',
  19139. ' a:=test1.texta.create();',
  19140. '']);
  19141. ConvertProgram;
  19142. CheckSource('TestExternalClass_ConstructorBrackets',
  19143. LinesToStr([ // statements
  19144. 'this.A = null;',
  19145. '']),
  19146. LinesToStr([ // $mod.$main
  19147. '$mod.A = {};',
  19148. '$mod.A = {};',
  19149. '$mod.A = {};',
  19150. '$mod.A = {};',
  19151. '$mod.A = {};',
  19152. '$mod.A = {};',
  19153. '$mod.A = {};',
  19154. '']));
  19155. end;
  19156. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19157. begin
  19158. StartProgram(false);
  19159. Add('{$modeswitch externalclass}');
  19160. Add('type');
  19161. Add(' TExtA = class external name ''ExtA''');
  19162. Add(' constructor New;');
  19163. Add(' end;');
  19164. Add('function DoIt: longint;');
  19165. Add('const ExtA: longint = 3;');
  19166. Add('begin');
  19167. Add(' Result:=ExtA;');
  19168. Add('end;');
  19169. Add('var');
  19170. Add(' A: texta;');
  19171. Add('begin');
  19172. Add(' a:=texta.new;');
  19173. ConvertProgram;
  19174. CheckSource('TestExternalClass_LocalConstSameName',
  19175. LinesToStr([ // statements
  19176. 'var ExtA$1 = 3;',
  19177. 'this.DoIt = function () {',
  19178. ' var Result = 0;',
  19179. ' Result = ExtA$1;',
  19180. ' return Result;',
  19181. '};',
  19182. 'this.A = null;',
  19183. '']),
  19184. LinesToStr([ // $mod.$main
  19185. '$mod.A = new ExtA();',
  19186. '']));
  19187. end;
  19188. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19189. begin
  19190. StartProgram(false);
  19191. Add('{$modeswitch externalclass}');
  19192. Add('type');
  19193. Add(' TExtA = class external name ''ExtA''');
  19194. Add(' procedure DoIt;');
  19195. Add(' end;');
  19196. Add(' TMyA = class(TExtA)');
  19197. Add(' procedure DoIt;');
  19198. Add(' end;');
  19199. Add('procedure TMyA.DoIt; begin end;');
  19200. Add('begin');
  19201. ConvertProgram;
  19202. CheckSource('TestExternalClass_ReintroduceOverload',
  19203. LinesToStr([ // statements
  19204. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19205. ' this.$init = function () {',
  19206. ' };',
  19207. ' this.$final = function () {',
  19208. ' };',
  19209. ' this.DoIt$1 = function () {',
  19210. ' };',
  19211. '});',
  19212. '']),
  19213. LinesToStr([ // $mod.$main
  19214. '']));
  19215. end;
  19216. procedure TTestModule.TestExternalClass_Inherited;
  19217. begin
  19218. StartProgram(false);
  19219. Add('{$modeswitch externalclass}');
  19220. Add('type');
  19221. Add(' TExtA = class external name ''ExtA''');
  19222. Add(' procedure DoIt(i: longint = 1); virtual;');
  19223. Add(' procedure DoSome(j: longint = 2);');
  19224. Add(' end;');
  19225. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19226. Add(' end;');
  19227. Add(' TMyC = class(TExtB)');
  19228. Add(' procedure DoIt(i: longint = 1); override;');
  19229. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19230. Add(' end;');
  19231. Add('procedure TMyC.DoIt(i: longint);');
  19232. Add('begin');
  19233. Add(' inherited;');
  19234. Add(' inherited DoIt;');
  19235. Add(' inherited DoIt();');
  19236. Add(' inherited DoIt(3);');
  19237. Add(' inherited DoSome;');
  19238. Add(' inherited DoSome();');
  19239. Add(' inherited DoSome(4);');
  19240. Add('end;');
  19241. Add('procedure TMyC.DoSome(j: longint);');
  19242. Add('begin');
  19243. Add(' inherited;');
  19244. Add('end;');
  19245. Add('begin');
  19246. ConvertProgram;
  19247. CheckSource('TestExternalClass_ReintroduceOverload',
  19248. LinesToStr([ // statements
  19249. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19250. ' this.$init = function () {',
  19251. ' };',
  19252. ' this.$final = function () {',
  19253. ' };',
  19254. ' this.DoIt = function (i) {',
  19255. ' ExtB.DoIt.apply(this, arguments);',
  19256. ' ExtB.DoIt.call(this, 1);',
  19257. ' ExtB.DoIt.call(this, 1);',
  19258. ' ExtB.DoIt.call(this, 3);',
  19259. ' ExtB.DoSome.call(this, 2);',
  19260. ' ExtB.DoSome.call(this, 2);',
  19261. ' ExtB.DoSome.call(this, 4);',
  19262. ' };',
  19263. ' this.DoSome$1 = function (j) {',
  19264. ' ExtB.DoSome.apply(this, arguments);',
  19265. ' };',
  19266. '});',
  19267. '']),
  19268. LinesToStr([ // $mod.$main
  19269. '']));
  19270. end;
  19271. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19272. begin
  19273. StartProgram(false);
  19274. Add('{$modeswitch externalclass}');
  19275. Add('type');
  19276. Add(' TObject = class');
  19277. Add(' end;');
  19278. Add(' TExtA = class external name ''ExtA''(TObject)');
  19279. Add(' end;');
  19280. Add('begin');
  19281. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19282. ConvertProgram;
  19283. end;
  19284. procedure TTestModule.TestExternalClass_NewInstance;
  19285. begin
  19286. StartProgram(false);
  19287. Add('{$modeswitch externalclass}');
  19288. Add('type');
  19289. Add(' TExtA = class external name ''ExtA''');
  19290. Add(' end;');
  19291. Add(' TMyB = class(TExtA)');
  19292. Add(' protected');
  19293. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19294. Add(' end;');
  19295. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19296. Add('begin end;');
  19297. Add('begin');
  19298. ConvertProgram;
  19299. CheckSource('TestExternalClass_NewInstance',
  19300. LinesToStr([ // statements
  19301. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19302. ' this.$init = function () {',
  19303. ' };',
  19304. ' this.$final = function () {',
  19305. ' };',
  19306. ' this.NewInstance = function (fnname, paramarray) {',
  19307. ' var Result = null;',
  19308. ' return Result;',
  19309. ' };',
  19310. '});',
  19311. '']),
  19312. LinesToStr([ // $mod.$main
  19313. '']));
  19314. end;
  19315. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19316. begin
  19317. StartProgram(false);
  19318. Add('{$modeswitch externalclass}');
  19319. Add('type');
  19320. Add(' TExtA = class external name ''ExtA''');
  19321. Add(' end;');
  19322. Add(' TMyB = class(TExtA)');
  19323. Add(' protected');
  19324. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19325. Add(' end;');
  19326. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19327. Add('begin end;');
  19328. Add('begin');
  19329. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19330. ConvertProgram;
  19331. end;
  19332. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19333. begin
  19334. StartProgram(false);
  19335. Add('{$modeswitch externalclass}');
  19336. Add('type');
  19337. Add(' TExtA = class external name ''ExtA''');
  19338. Add(' end;');
  19339. Add(' TMyB = class(TExtA)');
  19340. Add(' protected');
  19341. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19342. Add(' end;');
  19343. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19344. Add('begin end;');
  19345. Add('begin');
  19346. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19347. nIncompatibleTypeArgNo);
  19348. ConvertProgram;
  19349. end;
  19350. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19351. begin
  19352. StartProgram(false);
  19353. Add('{$modeswitch externalclass}');
  19354. Add('type');
  19355. Add(' TExtA = class external name ''ExtA''');
  19356. Add(' end;');
  19357. Add(' TMyB = class(TExtA)');
  19358. Add(' protected');
  19359. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19360. Add(' end;');
  19361. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19362. Add('begin end;');
  19363. Add('begin');
  19364. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19365. nIncompatibleTypeArgNo);
  19366. ConvertProgram;
  19367. end;
  19368. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19369. begin
  19370. StartProgram(false);
  19371. Add([
  19372. '{$modeswitch externalclass}',
  19373. 'type',
  19374. ' TJSFunction = class external name ''Function''',
  19375. ' end;',
  19376. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19377. ' constructor New(w: word);',
  19378. ' end;',
  19379. ' TBird = class (TExtA)',
  19380. ' public',
  19381. ' Size: word;',
  19382. ' class var Legs: word;',
  19383. ' constructor Create(a: word);',
  19384. ' end;',
  19385. ' TEagle = class (TBird)',
  19386. ' public',
  19387. ' constructor Create(b: word); reintroduce;',
  19388. ' end;',
  19389. 'constructor TBird.Create(a: word);',
  19390. 'begin',
  19391. ' inherited;', // silently ignored
  19392. ' inherited New(a);', // this.$func(a)
  19393. 'end;',
  19394. 'constructor TEagle.Create(b: word);',
  19395. 'begin',
  19396. ' inherited Create(b);',
  19397. 'end;',
  19398. 'var',
  19399. ' Bird: TBird;',
  19400. ' Eagle: TEagle;',
  19401. 'begin',
  19402. ' Bird:=TBird.Create(3);',
  19403. ' Eagle:=TEagle.Create(4);',
  19404. ' Bird.Size:=Bird.Size+5;',
  19405. ' Bird.Legs:=Bird.Legs+6;',
  19406. ' Eagle.Size:=Eagle.Size+5;',
  19407. ' Eagle.Legs:=Eagle.Legs+6;',
  19408. '']);
  19409. ConvertProgram;
  19410. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19411. LinesToStr([ // statements
  19412. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19413. ' this.Legs = 0;',
  19414. ' this.$init = function () {',
  19415. ' this.Size = 0;',
  19416. ' };',
  19417. ' this.$final = function () {',
  19418. ' };',
  19419. ' this.Create = function (a) {',
  19420. ' this.$ancestorfunc(a);',
  19421. ' return this;',
  19422. ' };',
  19423. '});',
  19424. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19425. ' this.Create$1 = function (b) {',
  19426. ' $mod.TBird.Create.call(this, b);',
  19427. ' return this;',
  19428. ' };',
  19429. '});',
  19430. 'this.Bird = null;',
  19431. 'this.Eagle = null;',
  19432. '']),
  19433. LinesToStr([ // $mod.$main
  19434. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19435. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19436. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19437. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19438. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19439. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19440. '']));
  19441. end;
  19442. procedure TTestModule.TestExternalClass_PascalProperty;
  19443. begin
  19444. StartProgram(false);
  19445. Add('{$modeswitch externalclass}');
  19446. Add('type');
  19447. Add(' TJSElement = class;');
  19448. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19449. Add(' TJSElement = class external name ''ExtA''');
  19450. Add(' end;');
  19451. Add(' TControl = class(TJSElement)');
  19452. Add(' private');
  19453. Add(' FOnClick: TJSNotifyEvent;');
  19454. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19455. Add(' procedure Click(Sender: TJSElement);');
  19456. Add(' end;');
  19457. Add('procedure TControl.Click(Sender: TJSElement);');
  19458. Add('begin');
  19459. Add(' OnClick(Self);');
  19460. Add('end;');
  19461. Add('var');
  19462. Add(' Ctrl: TControl;');
  19463. Add('begin');
  19464. Add(' Ctrl.OnClick:[email protected];');
  19465. Add(' Ctrl.OnClick(Ctrl);');
  19466. ConvertProgram;
  19467. CheckSource('TestExternalClass_PascalProperty',
  19468. LinesToStr([ // statements
  19469. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19470. ' this.$init = function () {',
  19471. ' this.FOnClick = null;',
  19472. ' };',
  19473. ' this.$final = function () {',
  19474. ' this.FOnClick = undefined;',
  19475. ' };',
  19476. ' this.Click = function (Sender) {',
  19477. ' this.FOnClick(this);',
  19478. ' };',
  19479. '});',
  19480. 'this.Ctrl = null;',
  19481. '']),
  19482. LinesToStr([ // $mod.$main
  19483. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19484. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19485. '']));
  19486. end;
  19487. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19488. begin
  19489. StartProgram(false);
  19490. Add([
  19491. '{$modeswitch externalclass}',
  19492. 'type',
  19493. ' IUnknown = interface end;',
  19494. ' TObject = class',
  19495. ' end;',
  19496. ' TChild = class',
  19497. ' end;',
  19498. ' TExtRootA = class external name ''ExtRootA''',
  19499. ' end;',
  19500. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19501. ' end;',
  19502. ' TExtRootB = class external name ''ExtRootB''',
  19503. ' end;',
  19504. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19505. ' end;',
  19506. ' TExtString = class external name ''String''',
  19507. ' function charAt(aIndex : NativeInt) : string;',
  19508. ' end;',
  19509. 'var',
  19510. ' Obj: TObject;',
  19511. ' Child: TChild;',
  19512. ' RootA: TExtRootA;',
  19513. ' ChildA: TExtChildA;',
  19514. ' RootB: TExtRootB;',
  19515. ' ChildB: TExtChildB;',
  19516. ' i: IUnknown;',
  19517. ' s: string;',
  19518. ' v: jsvalue;',
  19519. 'begin',
  19520. ' obj:=tobject(roota);',
  19521. ' obj:=tobject(childa);',
  19522. ' child:=tchild(tobject(roota));',
  19523. ' roota:=textroota(obj);',
  19524. ' roota:=textroota(child);',
  19525. ' roota:=textroota(rootb);',
  19526. ' roota:=textroota(childb);',
  19527. ' childa:=textchilda(textroota(obj));',
  19528. ' roota:=TExtRootA(i);',
  19529. ' s:=TExtString(s).charAt(7);',
  19530. ' s:=TExtString(v).charAt(8);',
  19531. '']);
  19532. ConvertProgram;
  19533. CheckSource('TestExternalClass_TypeCastToRootClass',
  19534. LinesToStr([ // statements
  19535. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19536. 'rtl.createClass(this, "TObject", null, function () {',
  19537. ' this.$init = function () {',
  19538. ' };',
  19539. ' this.$final = function () {',
  19540. ' };',
  19541. '});',
  19542. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19543. '});',
  19544. 'this.Obj = null;',
  19545. 'this.Child = null;',
  19546. 'this.RootA = null;',
  19547. 'this.ChildA = null;',
  19548. 'this.RootB = null;',
  19549. 'this.ChildB = null;',
  19550. 'this.i = null;',
  19551. 'this.s = "";',
  19552. 'this.v = undefined;',
  19553. '']),
  19554. LinesToStr([ // $mod.$main
  19555. '$mod.Obj = $mod.RootA;',
  19556. '$mod.Obj = $mod.ChildA;',
  19557. '$mod.Child = $mod.RootA;',
  19558. '$mod.RootA = $mod.Obj;',
  19559. '$mod.RootA = $mod.Child;',
  19560. '$mod.RootA = $mod.RootB;',
  19561. '$mod.RootA = $mod.ChildB;',
  19562. '$mod.ChildA = $mod.Obj;',
  19563. '$mod.RootA = $mod.i;',
  19564. '$mod.s = $mod.s.charAt(7);',
  19565. '$mod.s = $mod.v.charAt(8);',
  19566. '']));
  19567. end;
  19568. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19569. begin
  19570. StartProgram(false);
  19571. Add([
  19572. '{$modeswitch externalclass}',
  19573. 'type',
  19574. ' IUnknown = interface end;',
  19575. ' IBird = interface(IUnknown) end;',
  19576. ' TClass = class of TObject;',
  19577. ' TObject = class',
  19578. ' end;',
  19579. ' TChild = class',
  19580. ' end;',
  19581. ' TJSObject = class external name ''Object''',
  19582. ' end;',
  19583. ' TRec = record end;',
  19584. 'var',
  19585. ' Obj: TObject;',
  19586. ' Child: TChild;',
  19587. ' i: IUnknown;',
  19588. ' Bird: IBird;',
  19589. ' j: TJSObject;',
  19590. ' r: TRec;',
  19591. ' c: TClass;',
  19592. 'begin',
  19593. ' j:=tjsobject(IUnknown);',
  19594. ' j:=tjsobject(IBird);',
  19595. ' j:=tjsobject(TObject);',
  19596. ' j:=tjsobject(TChild);',
  19597. ' j:=tjsobject(TRec);',
  19598. ' j:=tjsobject(Obj);',
  19599. ' j:=tjsobject(Child);',
  19600. ' j:=tjsobject(i);',
  19601. ' j:=tjsobject(Bird);',
  19602. ' j:=tjsobject(r);',
  19603. ' j:=tjsobject(c);',
  19604. '']);
  19605. ConvertProgram;
  19606. CheckSource('TestExternalClass_TypeCastToJSObject',
  19607. LinesToStr([ // statements
  19608. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19609. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19610. 'rtl.createClass(this, "TObject", null, function () {',
  19611. ' this.$init = function () {',
  19612. ' };',
  19613. ' this.$final = function () {',
  19614. ' };',
  19615. '});',
  19616. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19617. '});',
  19618. 'rtl.recNewT(this, "TRec", function () {',
  19619. ' this.$eq = function (b) {',
  19620. ' return true;',
  19621. ' };',
  19622. ' this.$assign = function (s) {',
  19623. ' return this;',
  19624. ' };',
  19625. '});',
  19626. 'this.Obj = null;',
  19627. 'this.Child = null;',
  19628. 'this.i = null;',
  19629. 'this.Bird = null;',
  19630. 'this.j = null;',
  19631. 'this.r = this.TRec.$new();',
  19632. 'this.c = null;',
  19633. '']),
  19634. LinesToStr([ // $mod.$main
  19635. '$mod.j = $mod.IUnknown;',
  19636. '$mod.j = $mod.IBird;',
  19637. '$mod.j = $mod.TObject;',
  19638. '$mod.j = $mod.TChild;',
  19639. '$mod.j = $mod.TRec;',
  19640. '$mod.j = $mod.Obj;',
  19641. '$mod.j = $mod.Child;',
  19642. '$mod.j = $mod.i;',
  19643. '$mod.j = $mod.Bird;',
  19644. '$mod.j = $mod.r;',
  19645. '$mod.j = $mod.c;',
  19646. '']));
  19647. end;
  19648. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19649. begin
  19650. StartProgram(false);
  19651. Add('{$modeswitch externalclass}');
  19652. Add('type');
  19653. Add(' TJSString = class external name ''String''');
  19654. Add(' class function fromCharCode() : string; varargs;');
  19655. Add(' function anchor(const aName : string) : string;');
  19656. Add(' end;');
  19657. Add('var');
  19658. Add(' s: string;');
  19659. Add('begin');
  19660. Add(' s:=TJSString.fromCharCode(65,66);');
  19661. Add(' s:=TJSString(s).anchor(s);');
  19662. Add(' s:=TJSString(''foo'').anchor(s);');
  19663. ConvertProgram;
  19664. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19665. LinesToStr([ // statements
  19666. 'this.s = "";',
  19667. '']),
  19668. LinesToStr([ // $mod.$main
  19669. '$mod.s = String.fromCharCode(65, 66);',
  19670. '$mod.s = $mod.s.anchor($mod.s);',
  19671. '$mod.s = "foo".anchor($mod.s);',
  19672. '']));
  19673. end;
  19674. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19675. begin
  19676. StartProgram(false);
  19677. Add([
  19678. '{$modeswitch externalclass}',
  19679. 'type',
  19680. ' TJSObject = class external name ''Object'' end;',
  19681. ' TJSFunction = class external name ''Function''',
  19682. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19683. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19684. ' end;',
  19685. ' TObject = class',
  19686. ' procedure DoIt(i: longint);',
  19687. ' end;',
  19688. ' TFuncInt = function(o: TObject): longint;',
  19689. 'function GetIt(o: TObject): longint;',
  19690. ' procedure Sub; begin end;',
  19691. 'var',
  19692. ' f: TJSFunction;',
  19693. ' fi: TFuncInt;',
  19694. 'begin',
  19695. ' fi:=TFuncInt(f);',
  19696. ' f:=TJSFunction(fi);',
  19697. ' f:=TJSFunction(@GetIt);',
  19698. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19699. ' f:=TJSFunction(@Sub);',
  19700. ' f:=TJSFunction(@o.doit);',
  19701. ' f:=TJSFunction(fi).bind(nil,4)',
  19702. 'end;',
  19703. 'procedure TObject.DoIt(i: longint);',
  19704. ' procedure Sub; begin end;',
  19705. 'var f: TJSFunction;',
  19706. 'begin',
  19707. ' f:=TJSFunction(@DoIt);',
  19708. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19709. ' f:=TJSFunction(@Sub);',
  19710. ' f:=TJSFunction(@GetIt);',
  19711. 'end;',
  19712. 'begin']);
  19713. ConvertProgram;
  19714. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19715. LinesToStr([ // statements
  19716. 'rtl.createClass(this, "TObject", null, function () {',
  19717. ' this.$init = function () {',
  19718. ' };',
  19719. ' this.$final = function () {',
  19720. ' };',
  19721. ' this.DoIt = function (i) {',
  19722. ' var $Self = this;',
  19723. ' function Sub() {',
  19724. ' };',
  19725. ' var f = null;',
  19726. ' f = this.DoIt;',
  19727. ' f = this.DoIt.bind(null, 13);',
  19728. ' f = Sub;',
  19729. ' f = $mod.GetIt;',
  19730. ' };',
  19731. '});',
  19732. 'this.GetIt = function (o) {',
  19733. ' var Result = 0;',
  19734. ' function Sub() {',
  19735. ' };',
  19736. ' var f = null;',
  19737. ' var fi = null;',
  19738. ' fi = f;',
  19739. ' f = fi;',
  19740. ' f = $mod.GetIt;',
  19741. ' f = $mod.GetIt.bind(null, 3);',
  19742. ' f = Sub;',
  19743. ' f = $mod.TObject.DoIt;',
  19744. ' f = fi.bind(null, 4);',
  19745. ' return Result;',
  19746. '};',
  19747. '']),
  19748. LinesToStr([ // $mod.$main
  19749. '']));
  19750. end;
  19751. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19752. begin
  19753. StartProgram(false);
  19754. Add([
  19755. '{$mode delphi}',
  19756. '{$modeswitch externalclass}',
  19757. 'type',
  19758. ' TJSObject = class external name ''Object'' end;',
  19759. ' TJSWindow = class external name ''Window''(TJSObject)',
  19760. ' procedure Open;',
  19761. ' end;',
  19762. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19763. ' procedure Execute;',
  19764. ' end;',
  19765. 'procedure Fly;',
  19766. 'var',
  19767. ' w: TJSWindow;',
  19768. ' e: TJSEventTarget;',
  19769. 'begin',
  19770. ' w:=TJSWindow(e);',
  19771. ' e:=TJSEventTarget(w);',
  19772. 'end;',
  19773. 'begin']);
  19774. ConvertProgram;
  19775. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19776. LinesToStr([ // statements
  19777. 'this.Fly = function () {',
  19778. ' var w = null;',
  19779. ' var e = null;',
  19780. ' w = e;',
  19781. ' e = w;',
  19782. '};',
  19783. '']),
  19784. LinesToStr([ // $mod.$main
  19785. '']));
  19786. end;
  19787. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19788. begin
  19789. StartProgram(false);
  19790. Add('{$modeswitch externalclass}');
  19791. Add('type');
  19792. Add(' TJSString = class external name ''String''');
  19793. Add(' class function fromCharCode() : string; varargs;');
  19794. Add(' end;');
  19795. Add('var');
  19796. Add(' s: string;');
  19797. Add(' sObj: TJSString;');
  19798. Add('begin');
  19799. Add(' s:=sObj.fromCharCode(65,66);');
  19800. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19801. nExternalClassInstanceCannotAccessStaticX);
  19802. ConvertProgram;
  19803. end;
  19804. procedure TTestModule.TestExternalClass_BracketAccessor;
  19805. begin
  19806. StartProgram(false);
  19807. Add([
  19808. '{$modeswitch externalclass}',
  19809. 'type',
  19810. ' TJSArray = class external name ''Array2''',
  19811. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19812. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19813. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19814. ' end;',
  19815. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19816. 'begin end;',
  19817. 'var',
  19818. ' Arr: tjsarray;',
  19819. ' s: string;',
  19820. ' i: longint;',
  19821. ' v: jsvalue;',
  19822. 'begin',
  19823. ' v:=arr[0];',
  19824. ' v:=arr.items[1];',
  19825. ' arr[2]:=s;',
  19826. ' arr.items[3]:=s;',
  19827. ' arr[4]:=i;',
  19828. ' arr[5]:=arr[6];',
  19829. ' arr.items[7]:=arr.items[8];',
  19830. ' with arr do items[9]:=items[10];',
  19831. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19832. ' with arr do begin',
  19833. ' v:=GetItems(14);',
  19834. ' setitems(15,16);',
  19835. ' end;',
  19836. ' v:=test1.arr.items[17];',
  19837. ' test1.arr.items[18]:=v;',
  19838. '']);
  19839. ConvertProgram;
  19840. CheckSource('TestExternalClass_BracketAccessor',
  19841. LinesToStr([ // statements
  19842. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19843. '};',
  19844. 'this.Arr = null;',
  19845. 'this.s = "";',
  19846. 'this.i = 0;',
  19847. 'this.v = undefined;',
  19848. '']),
  19849. LinesToStr([ // $mod.$main
  19850. '$mod.v = $mod.Arr[0];',
  19851. '$mod.v = $mod.Arr[1];',
  19852. '$mod.Arr[2] = $mod.s;',
  19853. '$mod.Arr[3] = $mod.s;',
  19854. '$mod.Arr[4] = $mod.i;',
  19855. '$mod.Arr[5] = $mod.Arr[6];',
  19856. '$mod.Arr[7] = $mod.Arr[8];',
  19857. 'var $with = $mod.Arr;',
  19858. '$with[9] = $with[10];',
  19859. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19860. ' a: 9,',
  19861. ' p: $mod.Arr,',
  19862. ' get: function () {',
  19863. ' return this.p[this.a];',
  19864. ' },',
  19865. ' set: function (v) {',
  19866. ' this.p[this.a] = v;',
  19867. ' }',
  19868. '}, {',
  19869. ' a: 10,',
  19870. ' p: $mod.Arr,',
  19871. ' get: function () {',
  19872. ' return this.p[this.a];',
  19873. ' },',
  19874. ' set: function (v) {',
  19875. ' this.p[this.a] = v;',
  19876. ' }',
  19877. '});',
  19878. 'var $with1 = $mod.Arr;',
  19879. '$mod.v = $with1[14];',
  19880. '$with1[15] = 16;',
  19881. '$mod.v = $mod.Arr[17];',
  19882. '$mod.Arr[18] = $mod.v;',
  19883. '']));
  19884. end;
  19885. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19886. begin
  19887. StartProgram(false);
  19888. Add([
  19889. '{$modeswitch externalclass}',
  19890. 'type',
  19891. ' TJSArray = class external name ''Array2''',
  19892. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19893. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19894. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19895. ' end;',
  19896. ' TMyArr = class(TJSArray)',
  19897. ' procedure DoIt;',
  19898. ' end;',
  19899. 'procedure tmyarr.DoIt;',
  19900. 'begin',
  19901. ' Items[1]:=Items[2];',
  19902. ' SetItems(3,getItems(4));',
  19903. 'end;',
  19904. 'var',
  19905. ' Arr: tmyarr;',
  19906. ' s: string;',
  19907. ' i: longint;',
  19908. ' v: jsvalue;',
  19909. 'begin',
  19910. ' v:=arr[0];',
  19911. ' v:=arr.items[1];',
  19912. ' arr[2]:=s;',
  19913. ' arr.items[3]:=s;',
  19914. ' arr[4]:=i;',
  19915. ' arr[5]:=arr[6];',
  19916. ' arr.items[7]:=arr.items[8];',
  19917. ' with arr do items[9]:=items[10];',
  19918. ' with arr do begin',
  19919. ' v:=GetItems(14);',
  19920. ' setitems(15,16);',
  19921. ' end;',
  19922. '']);
  19923. ConvertProgram;
  19924. CheckSource('TestExternalClass_BracketAccessor_Call',
  19925. LinesToStr([ // statements
  19926. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19927. ' this.$init = function () {',
  19928. ' };',
  19929. ' this.$final = function () {',
  19930. ' };',
  19931. ' this.DoIt = function () {',
  19932. ' this[1] = this[2];',
  19933. ' this[3] = this[4];',
  19934. ' };',
  19935. '});',
  19936. 'this.Arr = null;',
  19937. 'this.s = "";',
  19938. 'this.i = 0;',
  19939. 'this.v = undefined;',
  19940. '']),
  19941. LinesToStr([ // $mod.$main
  19942. '$mod.v = $mod.Arr[0];',
  19943. '$mod.v = $mod.Arr[1];',
  19944. '$mod.Arr[2] = $mod.s;',
  19945. '$mod.Arr[3] = $mod.s;',
  19946. '$mod.Arr[4] = $mod.i;',
  19947. '$mod.Arr[5] = $mod.Arr[6];',
  19948. '$mod.Arr[7] = $mod.Arr[8];',
  19949. 'var $with = $mod.Arr;',
  19950. '$with[9] = $with[10];',
  19951. 'var $with1 = $mod.Arr;',
  19952. '$mod.v = $with1[14];',
  19953. '$with1[15] = 16;',
  19954. '']));
  19955. end;
  19956. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19957. begin
  19958. StartProgram(false);
  19959. Add('{$modeswitch externalclass}');
  19960. Add('type');
  19961. Add(' TJSArray = class external name ''Array2''');
  19962. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19963. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19964. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19965. Add(' end;');
  19966. Add('begin');
  19967. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19968. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19969. ConvertProgram;
  19970. end;
  19971. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19972. begin
  19973. StartProgram(false);
  19974. Add('{$modeswitch externalclass}');
  19975. Add('type');
  19976. Add(' TJSArray = class external name ''Array2''');
  19977. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19978. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19979. Add(' end;');
  19980. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19981. Add('begin end;');
  19982. Add('var');
  19983. Add(' Arr: tjsarray;');
  19984. Add(' v: jsvalue;');
  19985. Add('begin');
  19986. Add(' v:=arr[0];');
  19987. Add(' v:=arr.items[1];');
  19988. Add(' with arr do v:=items[2];');
  19989. Add(' doit(arr[3],arr[4]);');
  19990. ConvertProgram;
  19991. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19992. LinesToStr([ // statements
  19993. 'this.DoIt = function (vI, vJ) {',
  19994. '};',
  19995. 'this.Arr = null;',
  19996. 'this.v = undefined;',
  19997. '']),
  19998. LinesToStr([ // $mod.$main
  19999. '$mod.v = $mod.Arr[0];',
  20000. '$mod.v = $mod.Arr[1];',
  20001. 'var $with = $mod.Arr;',
  20002. '$mod.v = $with[2];',
  20003. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20004. '']));
  20005. end;
  20006. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20007. begin
  20008. StartProgram(false);
  20009. Add('{$modeswitch externalclass}');
  20010. Add('type');
  20011. Add(' TJSArray = class external name ''Array2''');
  20012. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20013. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20014. Add(' end;');
  20015. Add('var');
  20016. Add(' Arr: tjsarray;');
  20017. Add(' s: string;');
  20018. Add(' i: longint;');
  20019. Add(' v: jsvalue;');
  20020. Add('begin');
  20021. Add(' arr[2]:=s;');
  20022. Add(' arr.items[3]:=s;');
  20023. Add(' arr[4]:=i;');
  20024. Add(' with arr do items[5]:=i;');
  20025. ConvertProgram;
  20026. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20027. LinesToStr([ // statements
  20028. 'this.Arr = null;',
  20029. 'this.s = "";',
  20030. 'this.i = 0;',
  20031. 'this.v = undefined;',
  20032. '']),
  20033. LinesToStr([ // $mod.$main
  20034. '$mod.Arr[2] = $mod.s;',
  20035. '$mod.Arr[3] = $mod.s;',
  20036. '$mod.Arr[4] = $mod.i;',
  20037. 'var $with = $mod.Arr;',
  20038. '$with[5] = $mod.i;',
  20039. '']));
  20040. end;
  20041. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20042. begin
  20043. StartProgram(false);
  20044. Add('{$modeswitch externalclass}');
  20045. Add('type');
  20046. Add(' TJSArray = class external name ''Array2''');
  20047. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20048. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20049. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20050. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20051. Add(' end;');
  20052. Add('var');
  20053. Add(' Arr: tjsarray;');
  20054. Add(' s: string;');
  20055. Add(' i: longint;');
  20056. Add(' v: jsvalue;');
  20057. Add('begin');
  20058. Add(' arr[2]:=s;');
  20059. Add(' arr.items[3]:=s;');
  20060. Add(' arr.numbers[4]:=i;');
  20061. Add(' with arr do items[5]:=i;');
  20062. Add(' with arr do numbers[6]:=i;');
  20063. ConvertProgram;
  20064. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20065. LinesToStr([ // statements
  20066. 'this.Arr = null;',
  20067. 'this.s = "";',
  20068. 'this.i = 0;',
  20069. 'this.v = undefined;',
  20070. '']),
  20071. LinesToStr([ // $mod.$main
  20072. '$mod.Arr[2] = $mod.s;',
  20073. '$mod.Arr[3] = $mod.s;',
  20074. '$mod.Arr[4] = $mod.i;',
  20075. 'var $with = $mod.Arr;',
  20076. '$with[5] = $mod.i;',
  20077. 'var $with1 = $mod.Arr;',
  20078. '$with1[6] = $mod.i;',
  20079. '']));
  20080. end;
  20081. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20082. begin
  20083. StartProgram(false);
  20084. Add('{$modeswitch externalclass}');
  20085. Add('type');
  20086. Add(' TJSArray = class external name ''Array2''');
  20087. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20088. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20089. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20090. Add(' end;');
  20091. Add('var');
  20092. Add(' Arr: tjsarray;');
  20093. Add(' i: longint;');
  20094. Add(' IntArr: array of longint;');
  20095. Add(' v: jsvalue;');
  20096. Add('begin');
  20097. Add(' v:=arr.items[i];');
  20098. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20099. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20100. ConvertProgram;
  20101. CheckSource('TestExternalClass_BracketAccessor_Index',
  20102. LinesToStr([ // statements
  20103. 'this.Arr = null;',
  20104. 'this.i = 0;',
  20105. 'this.IntArr = [];',
  20106. 'this.v = undefined;',
  20107. '']),
  20108. LinesToStr([ // $mod.$main
  20109. '$mod.v = $mod.Arr[$mod.i];',
  20110. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20111. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20112. '']));
  20113. end;
  20114. procedure TTestModule.TestExternalClass_ForInJSObject;
  20115. begin
  20116. StartProgram(false);
  20117. Add([
  20118. '{$modeswitch externalclass}',
  20119. 'type',
  20120. ' TJSObject = class external name ''Object''',
  20121. ' end;',
  20122. 'var',
  20123. ' o: TJSObject;',
  20124. ' key: string;',
  20125. 'begin',
  20126. ' for key in o do',
  20127. ' if key=''abc'' then ;',
  20128. '']);
  20129. ConvertProgram;
  20130. CheckSource('TestExternalClass_ForInJSObject',
  20131. LinesToStr([ // statements
  20132. 'this.o = null;',
  20133. 'this.key = "";',
  20134. '']),
  20135. LinesToStr([ // $mod.$main
  20136. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20137. '']));
  20138. end;
  20139. procedure TTestModule.TestExternalClass_ForInJSArray;
  20140. begin
  20141. StartProgram(false);
  20142. Add([
  20143. '{$modeswitch externalclass}',
  20144. 'type',
  20145. ' TJSInt8Array = class external name ''Int8Array''',
  20146. ' private',
  20147. ' flength: NativeInt external name ''length'';',
  20148. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20149. ' public',
  20150. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20151. ' property Length: NativeInt read flength;',
  20152. ' end;',
  20153. 'var',
  20154. ' a: TJSInt8Array;',
  20155. ' value: shortint;',
  20156. 'begin',
  20157. ' for value in a do',
  20158. ' if value=3 then ;',
  20159. '']);
  20160. ConvertProgram;
  20161. CheckSource('TestExternalClass_ForInJSArray',
  20162. LinesToStr([ // statements
  20163. 'this.a = null;',
  20164. 'this.value = 0;',
  20165. '']),
  20166. LinesToStr([ // $mod.$main
  20167. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20168. ' $mod.value = $in[$l];',
  20169. ' if ($mod.value === 3) ;',
  20170. '};',
  20171. '']));
  20172. end;
  20173. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20174. begin
  20175. AddModuleWithIntfImplSrc('unit2.pas',
  20176. LinesToStr([
  20177. '{$modeswitch externalclass}',
  20178. 'type',
  20179. ' TJSBufferSource = class external name ''BufferSource''',
  20180. ' end;',
  20181. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20182. '']),
  20183. '');
  20184. AddModuleWithIntfImplSrc('unit3.pas',
  20185. LinesToStr([
  20186. '{$modeswitch externalclass}',
  20187. 'type',
  20188. ' TJSBufferSource = class external name ''BufferSource''',
  20189. ' end;',
  20190. '']),
  20191. '');
  20192. StartUnit(true);
  20193. Add([
  20194. 'interface',
  20195. 'uses unit2, unit3;',
  20196. 'procedure DoSome(s: TJSBufferSource);',
  20197. 'implementation',
  20198. 'procedure DoSome(s: TJSBufferSource);',
  20199. 'begin',
  20200. ' DoIt(s);',
  20201. 'end;',
  20202. '']);
  20203. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20204. nIncompatibleTypeArgNo);
  20205. ConvertUnit;
  20206. end;
  20207. procedure TTestModule.TestExternalClass_NestedConstructor;
  20208. begin
  20209. StartProgram(false);
  20210. Add([
  20211. '{$modeswitch externalclass}',
  20212. 'type',
  20213. ' TJSObject = class external name ''Object''',
  20214. ' type TBird = class external name ''Bird''',
  20215. ' type TWing = class external name ''Wing''',
  20216. ' constructor New;',
  20217. ' constructor Create(w: word = 3);',
  20218. ' end;',
  20219. ' end;',
  20220. ' end;',
  20221. 'var',
  20222. ' w: TJSObject.TBird.TWing;',
  20223. 'begin',
  20224. ' w:=tjsobject.tbird.twing.new;',
  20225. ' w:=tjsobject.tbird.twing.new();',
  20226. ' w:=tjsobject.tbird.twing.create;',
  20227. ' w:=tjsobject.tbird.twing.create(4);',
  20228. ' with tjsobject do begin',
  20229. ' w:=tbird.twing.new;',
  20230. ' w:=tbird.twing.new();',
  20231. ' w:=tbird.twing.create;',
  20232. ' w:=tbird.twing.create(11);',
  20233. ' end;',
  20234. ' with tjsobject.tbird do begin',
  20235. ' w:=twing.new;',
  20236. ' w:=twing.new();',
  20237. ' w:=twing.create;',
  20238. ' w:=twing.create(21);',
  20239. ' end;',
  20240. ' with tjsobject.tbird.twing do begin',
  20241. ' w:=new;',
  20242. ' w:=new();',
  20243. ' w:=create;',
  20244. ' w:=create(31);',
  20245. ' end;',
  20246. '']);
  20247. ConvertProgram;
  20248. CheckSource('TestExternalClass_NestedConstructor',
  20249. LinesToStr([ // statements
  20250. 'this.w = null;',
  20251. '']),
  20252. LinesToStr([ // $mod.$main
  20253. '$mod.w = new Object.Bird.Wing();',
  20254. '$mod.w = new Object.Bird.Wing();',
  20255. '$mod.w = new Object.Bird.Wing.Create();',
  20256. '$mod.w = new Object.Bird.Wing.Create(4);',
  20257. '$mod.w = new Object.Bird.Wing();',
  20258. '$mod.w = new Object.Bird.Wing();',
  20259. '$mod.w = new Object.Bird.Wing.Create();',
  20260. '$mod.w = new Object.Bird.Wing.Create(11);',
  20261. 'var $with = Object.Bird;',
  20262. '$mod.w = new Object.Bird.Wing();',
  20263. '$mod.w = new Object.Bird.Wing();',
  20264. '$mod.w = new Object.Bird.Wing.Create();',
  20265. '$mod.w = new Object.Bird.Wing.Create(21);',
  20266. 'var $with1 = Object.Bird.Wing;',
  20267. '$mod.w = new $with1();',
  20268. '$mod.w = new $with1();',
  20269. '$mod.w = new Object.Bird.Wing.Create();',
  20270. '$mod.w = new Object.Bird.Wing.Create(31);',
  20271. '']));
  20272. end;
  20273. procedure TTestModule.TestClassInterface_Corba;
  20274. begin
  20275. StartProgram(false);
  20276. Add([
  20277. '{$interfaces corba}',
  20278. 'type',
  20279. ' IUnknown = interface;',
  20280. ' IUnknown = interface',
  20281. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20282. ' end;',
  20283. ' IInterface = IUnknown;',
  20284. ' IBird = interface(IInterface)',
  20285. ' function GetSize: longint;',
  20286. ' procedure SetSize(i: longint);',
  20287. ' property Size: longint read GetSize write SetSize;',
  20288. ' procedure DoIt(i: longint);',
  20289. ' end;',
  20290. ' TObject = class',
  20291. ' end;',
  20292. ' TBird = class(TObject,IBird)',
  20293. ' function GetSize: longint; virtual; abstract;',
  20294. ' procedure SetSize(i: longint); virtual; abstract;',
  20295. ' procedure DoIt(i: longint); virtual; abstract;',
  20296. ' end;',
  20297. 'var',
  20298. ' BirdIntf: IBird;',
  20299. 'begin',
  20300. ' BirdIntf.Size:=BirdIntf.Size;',
  20301. '']);
  20302. ConvertProgram;
  20303. CheckSource('TestClassInterface_Corba',
  20304. LinesToStr([ // statements
  20305. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20306. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20307. 'rtl.createClass(this, "TObject", null, function () {',
  20308. ' this.$init = function () {',
  20309. ' };',
  20310. ' this.$final = function () {',
  20311. ' };',
  20312. '});',
  20313. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20314. ' rtl.addIntf(this, $mod.IBird);',
  20315. '});',
  20316. 'this.BirdIntf = null;',
  20317. '']),
  20318. LinesToStr([ // $mod.$main
  20319. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20320. '']));
  20321. end;
  20322. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20323. begin
  20324. StartProgram(false);
  20325. Add([
  20326. '{$interfaces corba}',
  20327. 'type',
  20328. ' IUnknown = interface',
  20329. ' procedure DoIt; external name ''foo'';',
  20330. ' end;',
  20331. 'begin']);
  20332. SetExpectedParserError(
  20333. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20334. nParserNoFieldsAllowed);
  20335. ConvertProgram;
  20336. end;
  20337. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20338. begin
  20339. StartProgram(false);
  20340. Add([
  20341. '{$interfaces corba}',
  20342. 'type',
  20343. ' integer = longint;',
  20344. ' IUnknown = interface',
  20345. ' procedure DoIt(i: integer);',
  20346. ' procedure DoIt(s: string);',
  20347. ' end;',
  20348. ' IBird = interface(IUnknown)',
  20349. ' procedure DoIt(b: boolean); overload;',
  20350. ' end;',
  20351. ' TObject = class',
  20352. ' end;',
  20353. ' TBird = class(TObject,IBird)',
  20354. ' procedure DoIt(o: TObject);',
  20355. ' procedure DoIt(s: string);',
  20356. ' procedure DoIt(i: integer);',
  20357. ' procedure DoIt(b: boolean);',
  20358. ' end;',
  20359. 'procedure TBird.DoIt(o: TObject); begin end;',
  20360. 'procedure TBird.DoIt(s: string); begin end;',
  20361. 'procedure TBird.DoIt(i: integer); begin end;',
  20362. 'procedure TBird.DoIt(b: boolean); begin end;',
  20363. 'var',
  20364. ' BirdIntf: IBird;',
  20365. 'begin',
  20366. ' BirdIntf.DoIt(3);',
  20367. ' BirdIntf.DoIt(''abc'');',
  20368. ' BirdIntf.DoIt(true);',
  20369. '']);
  20370. ConvertProgram;
  20371. CheckSource('TestClassInterface_Overloads',
  20372. LinesToStr([ // statements
  20373. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20374. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20375. 'rtl.createClass(this, "TObject", null, function () {',
  20376. ' this.$init = function () {',
  20377. ' };',
  20378. ' this.$final = function () {',
  20379. ' };',
  20380. '});',
  20381. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20382. ' this.DoIt = function (o) {',
  20383. ' };',
  20384. ' this.DoIt$1 = function (s) {',
  20385. ' };',
  20386. ' this.DoIt$2 = function (i) {',
  20387. ' };',
  20388. ' this.DoIt$3 = function (b) {',
  20389. ' };',
  20390. ' rtl.addIntf(this, $mod.IBird, {',
  20391. ' DoIt$2: "DoIt$3",',
  20392. ' DoIt: "DoIt$2"',
  20393. ' });',
  20394. '});',
  20395. 'this.BirdIntf = null;',
  20396. '']),
  20397. LinesToStr([ // $mod.$main
  20398. '$mod.BirdIntf.DoIt(3);',
  20399. '$mod.BirdIntf.DoIt$1("abc");',
  20400. '$mod.BirdIntf.DoIt$2(true);',
  20401. '']));
  20402. end;
  20403. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20404. begin
  20405. StartProgram(false);
  20406. Add([
  20407. '{$interfaces corba}',
  20408. 'type',
  20409. ' IBird = interface',
  20410. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20411. ' end;',
  20412. ' IDog = interface',
  20413. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20414. ' end;',
  20415. ' TObject = class(IBird,IDog)',
  20416. ' end;',
  20417. 'begin']);
  20418. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20419. nDuplicateGUIDXInYZ);
  20420. ConvertProgram;
  20421. end;
  20422. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20423. begin
  20424. StartProgram(false);
  20425. Add([
  20426. '{$interfaces corba}',
  20427. 'type',
  20428. ' IAnimal = interface',
  20429. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20430. ' end;',
  20431. ' IBird = interface(IAnimal)',
  20432. ' end;',
  20433. ' IHawk = interface(IBird)',
  20434. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20435. ' end;',
  20436. 'begin']);
  20437. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20438. nDuplicateGUIDXInYZ);
  20439. ConvertProgram;
  20440. end;
  20441. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20442. begin
  20443. StartProgram(false);
  20444. Add([
  20445. '{$interfaces corba}',
  20446. 'type',
  20447. ' integer = longint;',
  20448. ' IUnknown = interface',
  20449. ' procedure DoIt(i: integer);',
  20450. ' end;',
  20451. ' IBird = interface',
  20452. ' procedure Fly(i: integer);',
  20453. ' end;',
  20454. ' TObject = class(IUnknown)',
  20455. ' procedure DoIt(i: integer);',
  20456. ' end;',
  20457. ' TBird = class(IBird)',
  20458. ' procedure Fly(i: integer);',
  20459. ' end;',
  20460. 'procedure TObject.DoIt(i: integer); begin end;',
  20461. 'procedure TBird.Fly(i: integer); begin end;',
  20462. 'begin',
  20463. '']);
  20464. ConvertProgram;
  20465. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20466. LinesToStr([ // statements
  20467. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20468. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20469. 'rtl.createClass(this, "TObject", null, function () {',
  20470. ' this.$init = function () {',
  20471. ' };',
  20472. ' this.$final = function () {',
  20473. ' };',
  20474. ' this.DoIt = function (i) {',
  20475. ' };',
  20476. ' rtl.addIntf(this, $mod.IUnknown);',
  20477. '});',
  20478. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20479. ' this.Fly = function (i) {',
  20480. ' };',
  20481. ' rtl.addIntf(this, $mod.IBird);',
  20482. ' rtl.addIntf(this, $mod.IUnknown);',
  20483. '});',
  20484. '']),
  20485. LinesToStr([ // $mod.$main
  20486. '']));
  20487. end;
  20488. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20489. begin
  20490. StartProgram(false);
  20491. Add([
  20492. '{$interfaces corba}',
  20493. 'type',
  20494. ' integer = longint;',
  20495. ' IBird = interface',
  20496. ' procedure DoIt(i: integer);',
  20497. ' end;',
  20498. ' TObject = class',
  20499. ' procedure DoIt(i: integer);',
  20500. ' end;',
  20501. ' TBird = class(IBird)',
  20502. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20503. ' end;',
  20504. 'procedure TObject.DoIt(i: integer); begin end;',
  20505. 'procedure TBird.DoIt(i: integer); begin end;',
  20506. 'begin',
  20507. '']);
  20508. ConvertProgram;
  20509. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20510. LinesToStr([ // statements
  20511. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20512. 'rtl.createClass(this, "TObject", null, function () {',
  20513. ' this.$init = function () {',
  20514. ' };',
  20515. ' this.$final = function () {',
  20516. ' };',
  20517. ' this.DoIt = function (i) {',
  20518. ' };',
  20519. '});',
  20520. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20521. ' this.DoIt$1 = function (i) {',
  20522. ' };',
  20523. ' rtl.addIntf(this, $mod.IBird, {',
  20524. ' DoIt: "DoIt$1"',
  20525. ' });',
  20526. '});',
  20527. '']),
  20528. LinesToStr([ // $mod.$main
  20529. '']));
  20530. end;
  20531. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20532. begin
  20533. StartProgram(false);
  20534. Add([
  20535. '{$interfaces corba}',
  20536. 'type',
  20537. ' IUnknown = interface',
  20538. ' procedure Walk(i: longint);',
  20539. ' end;',
  20540. ' IBird = interface(IUnknown)',
  20541. ' procedure Walk(b: boolean); overload;',
  20542. ' procedure Fly(s: string);',
  20543. ' end;',
  20544. ' TObject = class',
  20545. ' end;',
  20546. ' TBird = class(TObject,IBird)',
  20547. ' procedure IBird.Fly = Move;',
  20548. ' procedure IBird.Walk = Hop;',
  20549. ' procedure Hop(i: longint);',
  20550. ' procedure Move(s: string);',
  20551. ' procedure Hop(b: boolean);',
  20552. ' end;',
  20553. 'procedure TBird.Move(s: string); begin end;',
  20554. 'procedure TBird.Hop(i: longint); begin end;',
  20555. 'procedure TBird.Hop(b: boolean); begin end;',
  20556. 'var',
  20557. ' BirdIntf: IBird;',
  20558. 'begin',
  20559. ' BirdIntf.Walk(3);',
  20560. ' BirdIntf.Walk(true);',
  20561. ' BirdIntf.Fly(''abc'');',
  20562. '']);
  20563. ConvertProgram;
  20564. CheckSource('TestClassInterface_Corba_MethodResolution',
  20565. LinesToStr([ // statements
  20566. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20567. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20568. 'rtl.createClass(this, "TObject", null, function () {',
  20569. ' this.$init = function () {',
  20570. ' };',
  20571. ' this.$final = function () {',
  20572. ' };',
  20573. '});',
  20574. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20575. ' this.Hop = function (i) {',
  20576. ' };',
  20577. ' this.Move = function (s) {',
  20578. ' };',
  20579. ' this.Hop$1 = function (b) {',
  20580. ' };',
  20581. ' rtl.addIntf(this, $mod.IBird, {',
  20582. ' Walk$1: "Hop$1",',
  20583. ' Fly: "Move",',
  20584. ' Walk: "Hop"',
  20585. ' });',
  20586. '});',
  20587. 'this.BirdIntf = null;',
  20588. '']),
  20589. LinesToStr([ // $mod.$main
  20590. '$mod.BirdIntf.Walk(3);',
  20591. '$mod.BirdIntf.Walk$1(true);',
  20592. '$mod.BirdIntf.Fly("abc");',
  20593. '']));
  20594. end;
  20595. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20596. begin
  20597. StartProgram(false);
  20598. Add([
  20599. '{$interfaces com}',
  20600. 'type',
  20601. ' IUnknown = interface',
  20602. ' function _AddRef: longint;',
  20603. ' procedure Walk;',
  20604. ' end;',
  20605. ' IBird = interface end;',
  20606. ' IDog = interface end;',
  20607. ' TObject = class(IBird,IDog)',
  20608. ' function _AddRef: longint; virtual; abstract;',
  20609. ' procedure Walk; virtual; abstract;',
  20610. ' end;',
  20611. ' TBird = class(IUnknown)',
  20612. ' end;',
  20613. 'begin',
  20614. '']);
  20615. ConvertProgram;
  20616. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20617. LinesToStr([ // statements
  20618. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20619. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20620. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20621. 'rtl.createClass(this, "TObject", null, function () {',
  20622. ' this.$init = function () {',
  20623. ' };',
  20624. ' this.$final = function () {',
  20625. ' };',
  20626. ' rtl.addIntf(this, $mod.IBird);',
  20627. ' rtl.addIntf(this, $mod.IDog);',
  20628. '});',
  20629. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20630. ' rtl.addIntf(this, $mod.IUnknown);',
  20631. ' rtl.addIntf(this, $mod.IBird);',
  20632. ' rtl.addIntf(this, $mod.IDog);',
  20633. '});',
  20634. '']),
  20635. LinesToStr([ // $mod.$main
  20636. '']));
  20637. end;
  20638. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20639. begin
  20640. StartProgram(false);
  20641. Add([
  20642. '{$interfaces corba}',
  20643. 'type',
  20644. ' IUnknown = interface',
  20645. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20646. ' procedure Go;',
  20647. ' end;',
  20648. ' TObject = class(IUnknown)',
  20649. ' procedure Go; virtual; abstract;',
  20650. ' end;',
  20651. ' TBird = class',
  20652. ' procedure Go; override;',
  20653. ' end;',
  20654. ' TCat = class(TObject)',
  20655. ' procedure Go; override;',
  20656. ' end;',
  20657. ' TDog = class(TObject, IUnknown)',
  20658. ' procedure Go; override;',
  20659. ' end;',
  20660. 'procedure TBird.Go; begin end;',
  20661. 'procedure TCat.Go; begin end;',
  20662. 'procedure TDog.Go; begin end;',
  20663. 'begin',
  20664. '']);
  20665. ConvertProgram;
  20666. CheckSource('TestClassInterface_Corba_MethodOverride',
  20667. LinesToStr([ // statements
  20668. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20669. 'rtl.createClass(this, "TObject", null, function () {',
  20670. ' this.$init = function () {',
  20671. ' };',
  20672. ' this.$final = function () {',
  20673. ' };',
  20674. ' rtl.addIntf(this, $mod.IUnknown);',
  20675. '});',
  20676. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20677. ' this.Go = function () {',
  20678. ' };',
  20679. ' rtl.addIntf(this, $mod.IUnknown);',
  20680. '});',
  20681. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20682. ' this.Go = function () {',
  20683. ' };',
  20684. ' rtl.addIntf(this, $mod.IUnknown);',
  20685. '});',
  20686. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20687. ' this.Go = function () {',
  20688. ' };',
  20689. ' rtl.addIntf(this, $mod.IUnknown);',
  20690. '});',
  20691. '']),
  20692. LinesToStr([ // $mod.$main
  20693. '']));
  20694. end;
  20695. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20696. begin
  20697. StartProgram(false);
  20698. Add([
  20699. '{$interfaces corba}',
  20700. 'type',
  20701. ' IUnknown = interface',
  20702. ' end;',
  20703. ' IBird = interface(IUnknown)',
  20704. ' procedure Fly(s: string);',
  20705. ' end;',
  20706. ' IEagle = interface(IBird)',
  20707. ' end;',
  20708. ' IDove = interface(IBird)',
  20709. ' end;',
  20710. ' ISwallow = interface(IBird)',
  20711. ' end;',
  20712. ' TObject = class',
  20713. ' end;',
  20714. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20715. ' procedure Fly(s: string); virtual; abstract;',
  20716. ' end;',
  20717. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20718. ' FBirdIntf: IBird;',
  20719. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20720. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20721. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20722. ' FDoveObj: TBird;',
  20723. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20724. ' function GetSwallowObj: TBird; virtual; abstract;',
  20725. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20726. ' end;',
  20727. 'begin',
  20728. '']);
  20729. ConvertProgram;
  20730. CheckSource('TestClassInterface_Corba_Delegation',
  20731. LinesToStr([ // statements
  20732. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20733. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20734. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20735. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20736. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20737. 'rtl.createClass(this, "TObject", null, function () {',
  20738. ' this.$init = function () {',
  20739. ' };',
  20740. ' this.$final = function () {',
  20741. ' };',
  20742. '});',
  20743. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20744. ' rtl.addIntf(this, $mod.IBird);',
  20745. ' rtl.addIntf(this, $mod.IEagle);',
  20746. ' rtl.addIntf(this, $mod.IDove);',
  20747. ' rtl.addIntf(this, $mod.ISwallow);',
  20748. '});',
  20749. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20750. ' this.$init = function () {',
  20751. ' $mod.TObject.$init.call(this);',
  20752. ' this.FBirdIntf = null;',
  20753. ' this.FDoveObj = null;',
  20754. ' };',
  20755. ' this.$final = function () {',
  20756. ' this.FBirdIntf = undefined;',
  20757. ' this.FDoveObj = undefined;',
  20758. ' $mod.TObject.$final.call(this);',
  20759. ' };',
  20760. ' this.$intfmaps = {',
  20761. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20762. ' return this.FBirdIntf;',
  20763. ' },',
  20764. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20765. ' return this.GetEagleIntf();',
  20766. ' },',
  20767. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20768. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20769. ' },',
  20770. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20771. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20772. ' }',
  20773. ' };',
  20774. '});',
  20775. '']),
  20776. LinesToStr([ // $mod.$main
  20777. '']));
  20778. end;
  20779. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20780. begin
  20781. StartProgram(false);
  20782. Add([
  20783. '{$interfaces corba}',
  20784. 'type',
  20785. ' IUnknown = interface',
  20786. ' end;',
  20787. ' IBird = interface(IUnknown)',
  20788. ' procedure Fly(s: string);',
  20789. ' end;',
  20790. ' IEagle = interface(IBird)',
  20791. ' end;',
  20792. ' IDove = interface(IBird)',
  20793. ' end;',
  20794. ' ISwallow = interface(IBird)',
  20795. ' end;',
  20796. ' TObject = class',
  20797. ' end;',
  20798. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20799. ' procedure Fly(s: string); virtual; abstract;',
  20800. ' end;',
  20801. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20802. ' private',
  20803. ' class var FBirdIntf: IBird;',
  20804. ' class var FDoveObj: TBird;',
  20805. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20806. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20807. ' protected',
  20808. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20809. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20810. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20811. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20812. ' end;',
  20813. 'begin',
  20814. '']);
  20815. ConvertProgram;
  20816. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20817. LinesToStr([ // statements
  20818. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20819. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20820. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20821. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20822. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20823. 'rtl.createClass(this, "TObject", null, function () {',
  20824. ' this.$init = function () {',
  20825. ' };',
  20826. ' this.$final = function () {',
  20827. ' };',
  20828. '});',
  20829. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20830. ' rtl.addIntf(this, $mod.IBird);',
  20831. ' rtl.addIntf(this, $mod.IEagle);',
  20832. ' rtl.addIntf(this, $mod.IDove);',
  20833. ' rtl.addIntf(this, $mod.ISwallow);',
  20834. '});',
  20835. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20836. ' this.FBirdIntf = null;',
  20837. ' this.FDoveObj = null;',
  20838. ' this.$intfmaps = {',
  20839. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20840. ' return this.FBirdIntf;',
  20841. ' },',
  20842. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20843. ' return this.GetEagleIntf();',
  20844. ' },',
  20845. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20846. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20847. ' },',
  20848. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20849. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20850. ' }',
  20851. ' };',
  20852. '});',
  20853. '']),
  20854. LinesToStr([ // $mod.$main
  20855. '']));
  20856. end;
  20857. procedure TTestModule.TestClassInterface_Corba_Operators;
  20858. begin
  20859. StartProgram(false);
  20860. Add([
  20861. '{$interfaces corba}',
  20862. 'type',
  20863. ' IUnknown = interface',
  20864. ' end;',
  20865. ' IBird = interface(IUnknown)',
  20866. ' function GetItems(Index: longint): longint;',
  20867. ' procedure SetItems(Index: longint; Value: longint);',
  20868. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20869. ' end;',
  20870. ' TObject = class',
  20871. ' end;',
  20872. ' TBird = class(TObject,IBird)',
  20873. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20874. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20875. ' end;',
  20876. 'var',
  20877. ' IntfVar: IBird = nil;',
  20878. ' IntfVar2: IBird;',
  20879. ' ObjVar: TBird;',
  20880. ' v: JSValue;',
  20881. 'begin',
  20882. ' IntfVar:=nil;',
  20883. ' IntfVar[3]:=IntfVar[4];',
  20884. ' if Assigned(IntfVar) then ;',
  20885. ' IntfVar:=IntfVar2;',
  20886. ' IntfVar:=ObjVar;',
  20887. ' if IntfVar=IntfVar2 then ;',
  20888. ' if IntfVar<>IntfVar2 then ;',
  20889. ' if IntfVar is IBird then ;',
  20890. ' if IntfVar is TBird then ;',
  20891. ' if ObjVar is IBird then ;',
  20892. ' IntfVar:=IntfVar2 as IBird;',
  20893. ' ObjVar:=IntfVar2 as TBird;',
  20894. ' IntfVar:=ObjVar as IBird;',
  20895. ' IntfVar:=IBird(IntfVar2);',
  20896. ' ObjVar:=TBird(IntfVar);',
  20897. ' IntfVar:=IBird(ObjVar);',
  20898. ' v:=IntfVar;',
  20899. ' IntfVar:=IBird(v);',
  20900. ' if v is IBird then ;',
  20901. ' v:=JSValue(IntfVar);',
  20902. ' v:=IBird;',
  20903. '']);
  20904. ConvertProgram;
  20905. CheckSource('TestClassInterface_Corba_Operators',
  20906. LinesToStr([ // statements
  20907. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20908. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20909. 'rtl.createClass(this, "TObject", null, function () {',
  20910. ' this.$init = function () {',
  20911. ' };',
  20912. ' this.$final = function () {',
  20913. ' };',
  20914. '});',
  20915. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20916. ' rtl.addIntf(this, $mod.IBird);',
  20917. '});',
  20918. 'this.IntfVar = null;',
  20919. 'this.IntfVar2 = null;',
  20920. 'this.ObjVar = null;',
  20921. 'this.v = undefined;',
  20922. '']),
  20923. LinesToStr([ // $mod.$main
  20924. '$mod.IntfVar = null;',
  20925. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20926. 'if ($mod.IntfVar != null) ;',
  20927. '$mod.IntfVar = $mod.IntfVar2;',
  20928. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20929. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20930. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20931. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20932. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20933. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20934. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20935. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20936. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20937. '$mod.IntfVar = $mod.IntfVar2;',
  20938. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20939. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20940. '$mod.v = $mod.IntfVar;',
  20941. '$mod.IntfVar = rtl.getObject($mod.v);',
  20942. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20943. '$mod.v = $mod.IntfVar;',
  20944. '$mod.v = $mod.IBird;',
  20945. '']));
  20946. end;
  20947. procedure TTestModule.TestClassInterface_Corba_Args;
  20948. begin
  20949. StartProgram(false);
  20950. Add([
  20951. '{$interfaces corba}',
  20952. 'type',
  20953. ' IUnknown = interface',
  20954. ' end;',
  20955. ' IBird = interface(IUnknown)',
  20956. ' end;',
  20957. ' TObject = class',
  20958. ' end;',
  20959. ' TBird = class(TObject,IBird)',
  20960. ' end;',
  20961. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20962. 'begin',
  20963. ' DoIt(i,i,i);',
  20964. 'end;',
  20965. 'procedure Change(var i: IBird; out j: IBird);',
  20966. 'begin',
  20967. ' DoIt(i,i,i);',
  20968. ' Change(i,i);',
  20969. 'end;',
  20970. 'var',
  20971. ' i: IBird;',
  20972. ' o: TBird;',
  20973. 'begin',
  20974. ' DoIt(i,i,i);',
  20975. ' Change(i,i);',
  20976. ' DoIt(o,o,o);',
  20977. '']);
  20978. ConvertProgram;
  20979. CheckSource('TestClassInterface_Corba_Args',
  20980. LinesToStr([ // statements
  20981. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20982. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20983. 'rtl.createClass(this, "TObject", null, function () {',
  20984. ' this.$init = function () {',
  20985. ' };',
  20986. ' this.$final = function () {',
  20987. ' };',
  20988. '});',
  20989. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20990. ' rtl.addIntf(this, $mod.IBird);',
  20991. '});',
  20992. 'this.DoIt = function (u, i, j) {',
  20993. ' $mod.DoIt({',
  20994. ' get: function () {',
  20995. ' return i;',
  20996. ' },',
  20997. ' set: function (v) {',
  20998. ' i = v;',
  20999. ' }',
  21000. ' }, i, i);',
  21001. '};',
  21002. 'this.Change = function (i, j) {',
  21003. ' $mod.DoIt(i, i.get(), i.get());',
  21004. ' $mod.Change(i, i);',
  21005. '};',
  21006. 'this.i = null;',
  21007. 'this.o = null;',
  21008. '']),
  21009. LinesToStr([ // $mod.$main
  21010. '$mod.DoIt({',
  21011. ' p: $mod,',
  21012. ' get: function () {',
  21013. ' return this.p.i;',
  21014. ' },',
  21015. ' set: function (v) {',
  21016. ' this.p.i = v;',
  21017. ' }',
  21018. '}, $mod.i, $mod.i);',
  21019. '$mod.Change({',
  21020. ' p: $mod,',
  21021. ' get: function () {',
  21022. ' return this.p.i;',
  21023. ' },',
  21024. ' set: function (v) {',
  21025. ' this.p.i = v;',
  21026. ' }',
  21027. '}, {',
  21028. ' p: $mod,',
  21029. ' get: function () {',
  21030. ' return this.p.i;',
  21031. ' },',
  21032. ' set: function (v) {',
  21033. ' this.p.i = v;',
  21034. ' }',
  21035. '});',
  21036. '$mod.DoIt({',
  21037. ' p: $mod,',
  21038. ' get: function () {',
  21039. ' return this.p.o;',
  21040. ' },',
  21041. ' set: function (v) {',
  21042. ' this.p.o = v;',
  21043. ' }',
  21044. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21045. '']));
  21046. end;
  21047. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21048. begin
  21049. StartProgram(false);
  21050. Add([
  21051. '{$interfaces corba}',
  21052. 'type',
  21053. ' IUnknown = interface end;',
  21054. ' TObject = class',
  21055. ' Id: longint;',
  21056. ' end;',
  21057. ' IEnumerator = interface(IUnknown)',
  21058. ' function GetCurrent: TObject;',
  21059. ' function MoveNext: Boolean;',
  21060. ' property Current: TObject read GetCurrent;',
  21061. ' end;',
  21062. ' IEnumerable = interface(IUnknown)',
  21063. ' function GetEnumerator: IEnumerator;',
  21064. ' end;',
  21065. 'var',
  21066. ' o: TObject;',
  21067. ' i: IEnumerable;',
  21068. 'begin',
  21069. ' for o in i do o.Id:=3;',
  21070. '']);
  21071. ConvertProgram;
  21072. CheckSource('TestClassInterface_Corba_ForIn',
  21073. LinesToStr([ // statements
  21074. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21075. 'rtl.createClass(this, "TObject", null, function () {',
  21076. ' this.$init = function () {',
  21077. ' this.Id = 0;',
  21078. ' };',
  21079. ' this.$final = function () {',
  21080. ' };',
  21081. '});',
  21082. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21083. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21084. 'this.o = null;',
  21085. 'this.i = null;',
  21086. '']),
  21087. LinesToStr([ // $mod.$main
  21088. 'var $in = $mod.i.GetEnumerator();',
  21089. 'while ($in.MoveNext()) {',
  21090. ' $mod.o = $in.GetCurrent();',
  21091. ' $mod.o.Id = 3;',
  21092. '};',
  21093. '']));
  21094. end;
  21095. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21096. begin
  21097. StartProgram(false);
  21098. Add([
  21099. '{$interfaces corba}',
  21100. 'type',
  21101. ' IUnknown = interface end;',
  21102. ' IBird = interface(IUnknown)',
  21103. ' function Fly(w: word): word;',
  21104. ' end;',
  21105. ' TBirdArray = array of IBird;',
  21106. 'var',
  21107. ' i: IBird;',
  21108. ' a: TBirdArray;',
  21109. 'begin',
  21110. ' SetLength(a,3);',
  21111. ' i:=a[1];',
  21112. ' a[2]:=i;',
  21113. ' for i in a do i.fly(3);',
  21114. '']);
  21115. ConvertProgram;
  21116. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21117. LinesToStr([ // statements
  21118. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21119. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21120. 'this.i = null;',
  21121. 'this.a = [];',
  21122. '']),
  21123. LinesToStr([ // $mod.$main
  21124. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21125. '$mod.i = $mod.a[1];',
  21126. '$mod.a[2] = $mod.i;',
  21127. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21128. ' $mod.i = $in[$l];',
  21129. ' $mod.i.Fly(3);',
  21130. '};',
  21131. '']));
  21132. end;
  21133. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21134. begin
  21135. StartProgram(false);
  21136. Add([
  21137. '{$interfaces com}',
  21138. 'type',
  21139. ' IUnknown = interface',
  21140. ' function _AddRef: longint;',
  21141. ' function _Release: longint;',
  21142. ' end;',
  21143. ' TObject = class(IUnknown)',
  21144. ' function _AddRef: longint; virtual; abstract;',
  21145. ' function _Release: longint; virtual; abstract;',
  21146. ' end;',
  21147. 'var',
  21148. ' i: IUnknown;',
  21149. 'procedure DoGlobal(o: TObject);',
  21150. 'begin',
  21151. ' i:=nil;',
  21152. ' i:=o;',
  21153. ' i:=i;',
  21154. 'end;',
  21155. 'procedure DoLocal(o: TObject);',
  21156. 'const k: IUnknown = nil;',
  21157. 'var j: IUnknown;',
  21158. 'begin',
  21159. ' k:=o;',
  21160. ' k:=i;',
  21161. ' j:=o;',
  21162. ' j:=i;',
  21163. 'end;',
  21164. 'var o: TObject;',
  21165. 'begin',
  21166. ' i:=nil;',
  21167. ' i:=o;',
  21168. '']);
  21169. ConvertProgram;
  21170. CheckSource('TestClassInterface_COM_AssignVar',
  21171. LinesToStr([ // statements
  21172. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21173. 'rtl.createClass(this, "TObject", null, function () {',
  21174. ' this.$init = function () {',
  21175. ' };',
  21176. ' this.$final = function () {',
  21177. ' };',
  21178. ' rtl.addIntf(this, $mod.IUnknown);',
  21179. '});',
  21180. 'this.i = null;',
  21181. 'this.DoGlobal = function (o) {',
  21182. ' rtl.setIntfP($mod, "i", null);',
  21183. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21184. ' rtl.setIntfP($mod, "i", $mod.i);',
  21185. '};',
  21186. 'var k = null;',
  21187. 'this.DoLocal = function (o) {',
  21188. ' var j = null;',
  21189. ' try{',
  21190. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21191. ' k = rtl.setIntfL(k, $mod.i);',
  21192. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21193. ' j = rtl.setIntfL(j, $mod.i);',
  21194. ' }finally{',
  21195. ' rtl._Release(j);',
  21196. ' };',
  21197. '};',
  21198. 'this.o = null;',
  21199. '']),
  21200. LinesToStr([ // $mod.$main
  21201. 'rtl.setIntfP($mod, "i", null);',
  21202. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21203. '']));
  21204. end;
  21205. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21206. begin
  21207. StartProgram(false);
  21208. Add([
  21209. '{$interfaces com}',
  21210. 'type',
  21211. ' IUnknown = interface',
  21212. ' function _AddRef: longint;',
  21213. ' function _Release: longint;',
  21214. ' end;',
  21215. ' TObject = class(IUnknown)',
  21216. ' function _AddRef: longint; virtual; abstract;',
  21217. ' function _Release: longint; virtual; abstract;',
  21218. ' end;',
  21219. 'procedure DoDefault(i, j: IUnknown);',
  21220. 'begin',
  21221. ' i:=nil;',
  21222. ' i:=j;',
  21223. 'end;',
  21224. 'begin',
  21225. '']);
  21226. ConvertProgram;
  21227. CheckSource('TestClassInterface_COM_AssignArg',
  21228. LinesToStr([ // statements
  21229. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21230. 'rtl.createClass(this, "TObject", null, function () {',
  21231. ' this.$init = function () {',
  21232. ' };',
  21233. ' this.$final = function () {',
  21234. ' };',
  21235. ' rtl.addIntf(this, $mod.IUnknown);',
  21236. '});',
  21237. 'this.DoDefault = function (i, j) {',
  21238. ' rtl._AddRef(i);',
  21239. ' try {',
  21240. ' i = rtl.setIntfL(i, null);',
  21241. ' i = rtl.setIntfL(i, j);',
  21242. ' } finally {',
  21243. ' rtl._Release(i);',
  21244. ' };',
  21245. '};',
  21246. '']),
  21247. LinesToStr([ // $mod.$main
  21248. '']));
  21249. end;
  21250. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21251. begin
  21252. StartProgram(false);
  21253. Add([
  21254. '{$interfaces com}',
  21255. 'type',
  21256. ' IUnknown = interface',
  21257. ' function _AddRef: longint;',
  21258. ' function _Release: longint;',
  21259. ' end;',
  21260. ' TObject = class(IUnknown)',
  21261. ' function _AddRef: longint; virtual; abstract;',
  21262. ' function _Release: longint; virtual; abstract;',
  21263. ' end;',
  21264. 'function DoDefault(i: IUnknown): IUnknown;',
  21265. 'begin',
  21266. ' Result:=i;',
  21267. ' if Result<>nil then exit;',
  21268. 'end;',
  21269. 'begin',
  21270. '']);
  21271. ConvertProgram;
  21272. CheckSource('TestClassInterface_COM_FunctionResult',
  21273. LinesToStr([ // statements
  21274. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21275. 'rtl.createClass(this, "TObject", null, function () {',
  21276. ' this.$init = function () {',
  21277. ' };',
  21278. ' this.$final = function () {',
  21279. ' };',
  21280. ' rtl.addIntf(this, $mod.IUnknown);',
  21281. '});',
  21282. 'this.DoDefault = function (i) {',
  21283. ' var Result = null;',
  21284. ' var $ok = false;',
  21285. ' try {',
  21286. ' Result = rtl.setIntfL(Result, i);',
  21287. ' if(Result !== null){',
  21288. ' $ok = true;',
  21289. ' return Result;',
  21290. ' };',
  21291. ' $ok = true;',
  21292. ' } finally {',
  21293. ' if(!$ok) rtl._Release(Result);',
  21294. ' };',
  21295. ' return Result;',
  21296. '};',
  21297. '']),
  21298. LinesToStr([ // $mod.$main
  21299. '']));
  21300. end;
  21301. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21302. begin
  21303. StartProgram(false);
  21304. Add([
  21305. '{$interfaces com}',
  21306. 'type',
  21307. ' IUnknown = interface',
  21308. ' function _AddRef: longint;',
  21309. ' function _Release: longint;',
  21310. ' end;',
  21311. ' TObject = class(IUnknown)',
  21312. ' function _AddRef: longint; virtual; abstract;',
  21313. ' function _Release: longint; virtual; abstract;',
  21314. ' function GetIntf: IUnknown; virtual;',
  21315. ' end;',
  21316. ' TMouse = class',
  21317. ' function GetIntf: IUnknown; override;',
  21318. ' end;',
  21319. 'function TObject.GetIntf: IUnknown; begin end;',
  21320. 'function TMouse.GetIntf: IUnknown;',
  21321. 'var i: IUnknown;',
  21322. 'begin',
  21323. ' inherited;',
  21324. ' inherited GetIntf;',
  21325. ' inherited GetIntf();',
  21326. ' Result:=inherited GetIntf;',
  21327. ' Result:=inherited GetIntf();',
  21328. ' i:=inherited GetIntf;',
  21329. ' i:=inherited GetIntf();',
  21330. 'end;',
  21331. 'begin',
  21332. '']);
  21333. ConvertProgram;
  21334. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21335. LinesToStr([ // statements
  21336. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21337. 'rtl.createClass(this, "TObject", null, function () {',
  21338. ' this.$init = function () {',
  21339. ' };',
  21340. ' this.$final = function () {',
  21341. ' };',
  21342. ' this.GetIntf = function () {',
  21343. ' var Result = null;',
  21344. ' return Result;',
  21345. ' };',
  21346. ' rtl.addIntf(this, $mod.IUnknown);',
  21347. '});',
  21348. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21349. ' this.GetIntf = function () {',
  21350. ' var Result = null;',
  21351. ' var i = null;',
  21352. ' var $ir = rtl.createIntfRefs();',
  21353. ' var $ok = false;',
  21354. ' try {',
  21355. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21356. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21357. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21358. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21359. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21360. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21361. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21362. ' $ok = true;',
  21363. ' } finally {',
  21364. ' $ir.free();',
  21365. ' rtl._Release(i);',
  21366. ' if (!$ok) rtl._Release(Result);',
  21367. ' };',
  21368. ' return Result;',
  21369. ' };',
  21370. ' rtl.addIntf(this, $mod.IUnknown);',
  21371. '});',
  21372. '']),
  21373. LinesToStr([ // $mod.$main
  21374. '']));
  21375. end;
  21376. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21377. begin
  21378. StartProgram(false);
  21379. Add([
  21380. '{$interfaces com}',
  21381. 'type',
  21382. ' IUnknown = interface',
  21383. ' function _AddRef: longint;',
  21384. ' function _Release: longint;',
  21385. ' end;',
  21386. ' TObject = class(IUnknown)',
  21387. ' function _AddRef: longint; virtual; abstract;',
  21388. ' function _Release: longint; virtual; abstract;',
  21389. ' end;',
  21390. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21391. 'begin',
  21392. ' if i is IUnknown then ;',
  21393. ' if o is IUnknown then ;',
  21394. ' if i is TObject then ;',
  21395. ' i:=j as IUnknown;',
  21396. ' i:=o as IUnknown;',
  21397. ' o:=j as TObject;',
  21398. ' i:=IUnknown(j);',
  21399. ' i:=IUnknown(o);',
  21400. ' o:=TObject(i);',
  21401. 'end;',
  21402. 'begin',
  21403. '']);
  21404. ConvertProgram;
  21405. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21406. LinesToStr([ // statements
  21407. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21408. 'rtl.createClass(this, "TObject", null, function () {',
  21409. ' this.$init = function () {',
  21410. ' };',
  21411. ' this.$final = function () {',
  21412. ' };',
  21413. ' rtl.addIntf(this, $mod.IUnknown);',
  21414. '});',
  21415. 'this.DoDefault = function (i, j, o) {',
  21416. ' rtl._AddRef(i);',
  21417. ' try {',
  21418. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21419. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21420. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21421. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21422. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21423. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21424. ' i = rtl.setIntfL(i, j);',
  21425. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21426. ' o = rtl.intfToClass(i, $mod.TObject);',
  21427. ' } finally {',
  21428. ' rtl._Release(i);',
  21429. ' };',
  21430. '};',
  21431. '']),
  21432. LinesToStr([ // $mod.$main
  21433. '']));
  21434. end;
  21435. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21436. begin
  21437. StartProgram(false);
  21438. Add([
  21439. '{$interfaces com}',
  21440. 'type',
  21441. ' IUnknown = interface',
  21442. ' function _AddRef: longint;',
  21443. ' function _Release: longint;',
  21444. ' end;',
  21445. ' TObject = class(IUnknown)',
  21446. ' function _AddRef: longint; virtual; abstract;',
  21447. ' function _Release: longint; virtual; abstract;',
  21448. ' end;',
  21449. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21450. 'var o: TObject;',
  21451. 'begin',
  21452. ' DoIt(v,v,v,v);',
  21453. ' DoIt(o,o,k,k);',
  21454. 'end;',
  21455. 'procedure DoSome;',
  21456. 'var v: IUnknown;',
  21457. 'begin',
  21458. ' DoIt(v,v,v,v);',
  21459. 'end;',
  21460. 'var i: IUnknown;',
  21461. 'begin',
  21462. ' DoIt(i,i,i,i);',
  21463. '']);
  21464. ConvertProgram;
  21465. CheckSource('TestClassInterface_COM_PassAsArg',
  21466. LinesToStr([ // statements
  21467. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21468. 'rtl.createClass(this, "TObject", null, function () {',
  21469. ' this.$init = function () {',
  21470. ' };',
  21471. ' this.$final = function () {',
  21472. ' };',
  21473. ' rtl.addIntf(this, $mod.IUnknown);',
  21474. '});',
  21475. 'this.DoIt = function (v, j, k, l) {',
  21476. ' var o = null;',
  21477. ' var $ir = rtl.createIntfRefs();',
  21478. ' rtl._AddRef(v);',
  21479. ' try {',
  21480. ' $mod.DoIt(v, v, {',
  21481. ' get: function () {',
  21482. ' return v;',
  21483. ' },',
  21484. ' set: function (w) {',
  21485. ' v = rtl.setIntfL(v, w);',
  21486. ' }',
  21487. ' }, {',
  21488. ' get: function () {',
  21489. ' return v;',
  21490. ' },',
  21491. ' set: function (w) {',
  21492. ' v = rtl.setIntfL(v, w);',
  21493. ' }',
  21494. ' });',
  21495. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21496. ' } finally {',
  21497. ' $ir.free();',
  21498. ' rtl._Release(v);',
  21499. ' };',
  21500. '};',
  21501. 'this.DoSome = function () {',
  21502. ' var v = null;',
  21503. ' try {',
  21504. ' $mod.DoIt(v, v, {',
  21505. ' get: function () {',
  21506. ' return v;',
  21507. ' },',
  21508. ' set: function (w) {',
  21509. ' v = rtl.setIntfL(v, w);',
  21510. ' }',
  21511. ' }, {',
  21512. ' get: function () {',
  21513. ' return v;',
  21514. ' },',
  21515. ' set: function (w) {',
  21516. ' v = rtl.setIntfL(v, w);',
  21517. ' }',
  21518. ' });',
  21519. ' } finally {',
  21520. ' rtl._Release(v);',
  21521. ' };',
  21522. '};',
  21523. 'this.i = null;',
  21524. '']),
  21525. LinesToStr([ // $mod.$main
  21526. '$mod.DoIt($mod.i, $mod.i, {',
  21527. ' p: $mod,',
  21528. ' get: function () {',
  21529. ' return this.p.i;',
  21530. ' },',
  21531. ' set: function (v) {',
  21532. ' rtl.setIntfP(this.p, "i", v);',
  21533. ' }',
  21534. '}, {',
  21535. ' p: $mod,',
  21536. ' get: function () {',
  21537. ' return this.p.i;',
  21538. ' },',
  21539. ' set: function (v) {',
  21540. ' rtl.setIntfP(this.p, "i", v);',
  21541. ' }',
  21542. '});',
  21543. '']));
  21544. end;
  21545. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21546. begin
  21547. StartProgram(false);
  21548. Add([
  21549. '{$interfaces com}',
  21550. 'type',
  21551. ' IUnknown = interface',
  21552. ' function _AddRef: longint;',
  21553. ' function _Release: longint;',
  21554. ' end;',
  21555. ' TObject = class(IUnknown)',
  21556. ' function _AddRef: longint; virtual; abstract;',
  21557. ' function _Release: longint; virtual; abstract;',
  21558. ' end;',
  21559. 'procedure DoIt(out i);',
  21560. 'begin end;',
  21561. 'procedure DoSome;',
  21562. 'var v: IUnknown;',
  21563. 'begin',
  21564. ' DoIt(v);',
  21565. 'end;',
  21566. 'function GetIt: IUnknown;',
  21567. 'begin',
  21568. ' DoIt(Result);',
  21569. 'end;',
  21570. 'var i: IUnknown;',
  21571. 'begin',
  21572. ' DoIt(i);',
  21573. '']);
  21574. ConvertProgram;
  21575. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21576. LinesToStr([ // statements
  21577. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21578. 'rtl.createClass(this, "TObject", null, function () {',
  21579. ' this.$init = function () {',
  21580. ' };',
  21581. ' this.$final = function () {',
  21582. ' };',
  21583. ' rtl.addIntf(this, $mod.IUnknown);',
  21584. '});',
  21585. 'this.DoIt = function (i) {',
  21586. '};',
  21587. 'this.DoSome = function () {',
  21588. ' var v = null;',
  21589. ' try {',
  21590. ' $mod.DoIt({',
  21591. ' get: function () {',
  21592. ' return v;',
  21593. ' },',
  21594. ' set: function (w) {',
  21595. ' v = w;',
  21596. ' }',
  21597. ' });',
  21598. ' } finally {',
  21599. ' rtl._Release(v);',
  21600. ' };',
  21601. '};',
  21602. 'this.GetIt = function () {',
  21603. ' var Result = null;',
  21604. ' var $ok = false;',
  21605. ' try {',
  21606. ' $mod.DoIt({',
  21607. ' get: function () {',
  21608. ' return Result;',
  21609. ' },',
  21610. ' set: function (v) {',
  21611. ' Result = v;',
  21612. ' }',
  21613. ' });',
  21614. ' $ok = true;',
  21615. ' } finally {',
  21616. ' if (!$ok) rtl._Release(Result);',
  21617. ' };',
  21618. ' return Result;',
  21619. '};',
  21620. 'this.i = null;',
  21621. '']),
  21622. LinesToStr([ // $mod.$main
  21623. 'try {',
  21624. ' $mod.DoIt({',
  21625. ' p: $mod,',
  21626. ' get: function () {',
  21627. ' return this.p.i;',
  21628. ' },',
  21629. ' set: function (v) {',
  21630. ' this.p.i = v;',
  21631. ' }',
  21632. ' });',
  21633. '} finally {',
  21634. ' rtl._Release($mod.i);',
  21635. '};',
  21636. '']));
  21637. end;
  21638. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21639. begin
  21640. StartProgram(false);
  21641. Add([
  21642. '{$interfaces com}',
  21643. 'type',
  21644. ' IUnknown = interface',
  21645. ' function _AddRef: longint;',
  21646. ' function _Release: longint;',
  21647. ' end;',
  21648. ' TObject = class(IUnknown)',
  21649. ' function _AddRef: longint; virtual; abstract;',
  21650. ' function _Release: longint; virtual; abstract;',
  21651. ' end;',
  21652. 'function GetIt: IUnknown;',
  21653. 'begin',
  21654. 'end;',
  21655. 'procedure DoSome;',
  21656. 'var v: IUnknown;',
  21657. ' i: longint;',
  21658. 'begin',
  21659. ' v:=GetIt;',
  21660. ' v:=GetIt();',
  21661. ' GetIt()._AddRef;',
  21662. ' i:=GetIt()._AddRef;',
  21663. 'end;',
  21664. 'var v: IUnknown;',
  21665. ' i: longint;',
  21666. 'begin',
  21667. ' v:=GetIt;',
  21668. ' v:=GetIt();',
  21669. ' GetIt()._AddRef;',
  21670. ' i:=GetIt()._AddRef;',
  21671. '']);
  21672. ConvertProgram;
  21673. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21674. LinesToStr([ // statements
  21675. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21676. 'rtl.createClass(this, "TObject", null, function () {',
  21677. ' this.$init = function () {',
  21678. ' };',
  21679. ' this.$final = function () {',
  21680. ' };',
  21681. ' rtl.addIntf(this, $mod.IUnknown);',
  21682. '});',
  21683. 'this.GetIt = function () {',
  21684. ' var Result = null;',
  21685. ' return Result;',
  21686. '};',
  21687. 'this.DoSome = function () {',
  21688. ' var v = null;',
  21689. ' var i = 0;',
  21690. ' var $ir = rtl.createIntfRefs();',
  21691. ' try {',
  21692. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21693. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21694. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21695. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21696. ' } finally {',
  21697. ' $ir.free();',
  21698. ' rtl._Release(v);',
  21699. ' };',
  21700. '};',
  21701. 'this.v = null;',
  21702. 'this.i = 0;',
  21703. '']),
  21704. LinesToStr([ // $mod.$main
  21705. 'var $ir = rtl.createIntfRefs();',
  21706. 'try {',
  21707. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21708. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21709. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21710. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21711. '} finally {',
  21712. ' $ir.free();',
  21713. '};',
  21714. '']));
  21715. end;
  21716. procedure TTestModule.TestClassInterface_COM_Property;
  21717. begin
  21718. StartProgram(false);
  21719. Add([
  21720. '{$interfaces com}',
  21721. 'type',
  21722. ' IUnknown = interface',
  21723. ' function _AddRef: longint;',
  21724. ' function _Release: longint;',
  21725. ' end;',
  21726. ' TObject = class(IUnknown)',
  21727. ' FAnt: IUnknown;',
  21728. ' function _AddRef: longint; virtual; abstract;',
  21729. ' function _Release: longint; virtual; abstract;',
  21730. ' function GetBird: IUnknown; virtual; abstract;',
  21731. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21732. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21733. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21734. ' property Ant: IUnknown read FAnt write FAnt;',
  21735. ' property Bird: IUnknown read GetBird write SetBird;',
  21736. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21737. ' end;',
  21738. 'procedure DoIt;',
  21739. 'var',
  21740. ' o: TObject;',
  21741. ' v: IUnknown;',
  21742. 'begin',
  21743. ' v:=o.Ant;',
  21744. ' o.Ant:=v;',
  21745. ' o.Ant:=o.Ant;',
  21746. ' v:=o.Bird;',
  21747. ' o.Bird:=v;',
  21748. ' o.Bird:=o.Bird;',
  21749. ' v:=o.Items[1];',
  21750. ' o.Items[2]:=v;',
  21751. ' o.Items[3]:=o.Items[4];',
  21752. ' v:=o[5];',
  21753. ' o[6]:=v;',
  21754. ' o[7]:=o[8];',
  21755. 'end;',
  21756. 'begin',
  21757. '']);
  21758. ConvertProgram;
  21759. CheckSource('TestClassInterface_COM_Property',
  21760. LinesToStr([ // statements
  21761. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21762. 'rtl.createClass(this, "TObject", null, function () {',
  21763. ' this.$init = function () {',
  21764. ' this.FAnt = null;',
  21765. ' };',
  21766. ' this.$final = function () {',
  21767. ' rtl.setIntfP(this, "FAnt", null);',
  21768. ' };',
  21769. ' rtl.addIntf(this, $mod.IUnknown);',
  21770. '});',
  21771. 'this.DoIt = function () {',
  21772. ' var o = null;',
  21773. ' var v = null;',
  21774. ' var $ir = rtl.createIntfRefs();',
  21775. ' try {',
  21776. ' v = rtl.setIntfL(v, o.FAnt);',
  21777. ' rtl.setIntfP(o, "FAnt", v);',
  21778. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21779. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21780. ' o.SetBird(v);',
  21781. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21782. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21783. ' o.SetItems(2, v);',
  21784. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21785. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21786. ' o.SetItems(6, v);',
  21787. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21788. ' } finally {',
  21789. ' $ir.free();',
  21790. ' rtl._Release(v);',
  21791. ' };',
  21792. '};',
  21793. '']),
  21794. LinesToStr([ // $mod.$main
  21795. '']));
  21796. end;
  21797. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21798. begin
  21799. StartProgram(false);
  21800. Add([
  21801. '{$interfaces com}',
  21802. 'type',
  21803. ' IUnknown = interface',
  21804. ' function _AddRef: longint;',
  21805. ' function _Release: longint;',
  21806. ' function GetBird: IUnknown;',
  21807. ' procedure SetBird(Value: IUnknown);',
  21808. ' function GetItems(Index: longint): IUnknown;',
  21809. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21810. ' property Bird: IUnknown read GetBird write SetBird;',
  21811. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21812. ' end;',
  21813. ' TObject = class(IUnknown)',
  21814. ' function _AddRef: longint; virtual; abstract;',
  21815. ' function _Release: longint; virtual; abstract;',
  21816. ' function GetBird: IUnknown; virtual; abstract;',
  21817. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21818. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21819. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21820. ' end;',
  21821. 'procedure DoIt;',
  21822. 'var',
  21823. ' o: TObject;',
  21824. ' v: IUnknown;',
  21825. 'begin',
  21826. ' v:=v.Items[1];',
  21827. ' v.Items[2]:=v;',
  21828. ' v.Items[3]:=v.Items[4];',
  21829. ' v:=v[5];',
  21830. ' v[6]:=v;',
  21831. ' v[7]:=v[8];',
  21832. ' v[9].Bird.Bird:=v;',
  21833. ' v:=v.Bird[10].Bird',
  21834. 'end;',
  21835. 'begin',
  21836. '']);
  21837. ConvertProgram;
  21838. CheckSource('TestClassInterface_COM_IntfProperty',
  21839. LinesToStr([ // statements
  21840. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21841. ' "_AddRef",',
  21842. ' "_Release",',
  21843. ' "GetBird",',
  21844. ' "SetBird",',
  21845. ' "GetItems",',
  21846. ' "SetItems"',
  21847. '], null);',
  21848. 'rtl.createClass(this, "TObject", null, function () {',
  21849. ' this.$init = function () {',
  21850. ' };',
  21851. ' this.$final = function () {',
  21852. ' };',
  21853. ' rtl.addIntf(this, $mod.IUnknown);',
  21854. '});',
  21855. 'this.DoIt = function () {',
  21856. ' var o = null;',
  21857. ' var v = null;',
  21858. ' var $ir = rtl.createIntfRefs();',
  21859. ' try {',
  21860. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21861. ' v.SetItems(2, v);',
  21862. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21863. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21864. ' v.SetItems(6, v);',
  21865. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21866. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21867. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21868. ' } finally {',
  21869. ' $ir.free();',
  21870. ' rtl._Release(v);',
  21871. ' };',
  21872. '};',
  21873. '']),
  21874. LinesToStr([ // $mod.$main
  21875. '']));
  21876. end;
  21877. procedure TTestModule.TestClassInterface_COM_Delegation;
  21878. begin
  21879. StartProgram(false);
  21880. Add([
  21881. '{$interfaces com}',
  21882. 'type',
  21883. ' IUnknown = interface',
  21884. ' function _AddRef: longint;',
  21885. ' function _Release: longint;',
  21886. ' end;',
  21887. ' IBird = interface(IUnknown)',
  21888. ' procedure Fly(s: string);',
  21889. ' end;',
  21890. ' IEagle = interface(IBird) end;',
  21891. ' IDove = interface(IBird) end;',
  21892. ' ISwallow = interface(IBird) end;',
  21893. ' TObject = class',
  21894. ' end;',
  21895. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21896. ' function _AddRef: longint; virtual; abstract;',
  21897. ' function _Release: longint; virtual; abstract;',
  21898. ' procedure Fly(s: string); virtual; abstract;',
  21899. ' end;',
  21900. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21901. ' function _AddRef: longint; virtual; abstract;',
  21902. ' function _Release: longint; virtual; abstract;',
  21903. ' FBirdIntf: IBird;',
  21904. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21905. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21906. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21907. ' FDoveObj: TBird;',
  21908. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21909. ' function GetSwallowObj: TBird; virtual; abstract;',
  21910. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21911. ' end;',
  21912. 'begin',
  21913. '']);
  21914. ConvertProgram;
  21915. CheckSource('TestClassInterface_COM_Delegation',
  21916. LinesToStr([ // statements
  21917. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21918. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21919. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21920. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21921. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21922. 'rtl.createClass(this, "TObject", null, function () {',
  21923. ' this.$init = function () {',
  21924. ' };',
  21925. ' this.$final = function () {',
  21926. ' };',
  21927. '});',
  21928. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21929. ' rtl.addIntf(this, $mod.IBird);',
  21930. ' rtl.addIntf(this, $mod.IEagle);',
  21931. ' rtl.addIntf(this, $mod.IDove);',
  21932. ' rtl.addIntf(this, $mod.ISwallow);',
  21933. '});',
  21934. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21935. ' this.$init = function () {',
  21936. ' $mod.TObject.$init.call(this);',
  21937. ' this.FBirdIntf = null;',
  21938. ' this.FDoveObj = null;',
  21939. ' };',
  21940. ' this.$final = function () {',
  21941. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21942. ' this.FDoveObj = undefined;',
  21943. ' $mod.TObject.$final.call(this);',
  21944. ' };',
  21945. ' this.$intfmaps = {',
  21946. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21947. ' return rtl._AddRef(this.FBirdIntf);',
  21948. ' },',
  21949. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21950. ' return this.GetEagleIntf();',
  21951. ' },',
  21952. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21953. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21954. ' },',
  21955. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21956. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21957. ' }',
  21958. ' };',
  21959. '});',
  21960. '']),
  21961. LinesToStr([ // $mod.$main
  21962. '']));
  21963. end;
  21964. procedure TTestModule.TestClassInterface_COM_With;
  21965. begin
  21966. StartProgram(false);
  21967. Add([
  21968. '{$interfaces com}',
  21969. 'type',
  21970. ' IUnknown = interface',
  21971. ' function _AddRef: longint;',
  21972. ' function _Release: longint;',
  21973. ' function GetAnt: IUnknown;',
  21974. ' property Ant: IUnknown read GetAnt;',
  21975. ' end;',
  21976. ' TObject = class(IUnknown)',
  21977. ' function _AddRef: longint; virtual; abstract;',
  21978. ' function _Release: longint; virtual; abstract;',
  21979. ' function GetAnt: IUnknown; virtual; abstract;',
  21980. ' property Ant: IUnknown read GetAnt;',
  21981. ' end;',
  21982. 'procedure DoIt;',
  21983. 'var',
  21984. ' i: IUnknown;',
  21985. 'begin',
  21986. ' with i do ',
  21987. ' GetAnt;',
  21988. ' with i.Ant, Ant do ',
  21989. ' GetAnt;',
  21990. 'end;',
  21991. 'begin',
  21992. '']);
  21993. ConvertProgram;
  21994. CheckSource('TestClassInterface_COM_With',
  21995. LinesToStr([ // statements
  21996. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  21997. 'rtl.createClass(this, "TObject", null, function () {',
  21998. ' this.$init = function () {',
  21999. ' };',
  22000. ' this.$final = function () {',
  22001. ' };',
  22002. ' rtl.addIntf(this, $mod.IUnknown);',
  22003. '});',
  22004. 'this.DoIt = function () {',
  22005. ' var i = null;',
  22006. ' var $ir = rtl.createIntfRefs();',
  22007. ' try {',
  22008. ' $ir.ref(1, i.GetAnt());',
  22009. ' var $with = $ir.ref(2, i.GetAnt());',
  22010. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22011. ' $ir.ref(4, $with1.GetAnt());',
  22012. ' } finally {',
  22013. ' $ir.free();',
  22014. ' };',
  22015. '};',
  22016. '']),
  22017. LinesToStr([ // $mod.$main
  22018. '']));
  22019. end;
  22020. procedure TTestModule.TestClassInterface_COM_ForIn;
  22021. begin
  22022. StartProgram(false);
  22023. Add([
  22024. '{$interfaces com}',
  22025. 'type',
  22026. ' IUnknown = interface end;',
  22027. ' TObject = class',
  22028. ' Id: longint;',
  22029. ' end;',
  22030. ' IEnumerator = interface(IUnknown)',
  22031. ' function GetCurrent: TObject;',
  22032. ' function MoveNext: Boolean;',
  22033. ' property Current: TObject read GetCurrent;',
  22034. ' end;',
  22035. ' IEnumerable = interface(IUnknown)',
  22036. ' function GetEnumerator: IEnumerator;',
  22037. ' end;',
  22038. 'var',
  22039. ' o: TObject;',
  22040. ' i: IEnumerable;',
  22041. 'begin',
  22042. ' for o in i do o.Id:=3;',
  22043. '']);
  22044. ConvertProgram;
  22045. CheckSource('TestClassInterface_COM_ForIn',
  22046. LinesToStr([ // statements
  22047. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22048. 'rtl.createClass(this, "TObject", null, function () {',
  22049. ' this.$init = function () {',
  22050. ' this.Id = 0;',
  22051. ' };',
  22052. ' this.$final = function () {',
  22053. ' };',
  22054. '});',
  22055. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22056. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22057. 'this.o = null;',
  22058. 'this.i = null;',
  22059. '']),
  22060. LinesToStr([ // $mod.$main
  22061. 'var $in = $mod.i.GetEnumerator();',
  22062. 'try {',
  22063. ' while ($in.MoveNext()) {',
  22064. ' $mod.o = $in.GetCurrent();',
  22065. ' $mod.o.Id = 3;',
  22066. ' }',
  22067. '} finally {',
  22068. ' rtl._Release($in)',
  22069. '};',
  22070. '']));
  22071. end;
  22072. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22073. begin
  22074. {$IFNDEF EnableCOMArrayOfIntf}
  22075. exit;
  22076. {$ENDIF}
  22077. StartProgram(false);
  22078. Add([
  22079. '{$interfaces com}',
  22080. 'type',
  22081. ' IUnknown = interface end;',
  22082. ' IBird = interface(IUnknown)',
  22083. ' function Fly(w: word): word;',
  22084. ' end;',
  22085. ' TBirdArray = array of IBird;',
  22086. 'procedure Run;',
  22087. 'var',
  22088. ' i: IBird;',
  22089. ' a,b: TBirdArray;',
  22090. 'begin',
  22091. //' SetLength(a,3);',
  22092. ' a:=b;',
  22093. ' i:=a[1];',
  22094. ' a[2]:=i;',
  22095. //' for i in a do i.fly(3);',
  22096. // a:=copy(b,1,2);
  22097. // a:=concat(b,a);
  22098. // insert(i,b,1);
  22099. // a:=[i,i];
  22100. 'end;',
  22101. // ToDo: pass TBirdArray as arg
  22102. 'begin',
  22103. '']);
  22104. ConvertProgram;
  22105. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22106. LinesToStr([ // statements
  22107. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22108. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22109. 'this.Run = function () {',
  22110. ' var i = null;',
  22111. ' var a = [];',
  22112. ' var b = [];',
  22113. ' try {',
  22114. ' a = rtl.arrayRef(b);',
  22115. ' i = rtl.setIntfL(i, a[1]);',
  22116. ' rtl.setIntfP(a, 2, i);',
  22117. ' } finally {',
  22118. ' rtl._Release(i);',
  22119. ' rtl._ReleaseArray(a,1);',
  22120. ' };',
  22121. '};',
  22122. '']),
  22123. LinesToStr([ // $mod.$main
  22124. '']));
  22125. end;
  22126. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22127. begin
  22128. StartProgram(false);
  22129. Add([
  22130. '{$interfaces com}',
  22131. 'type',
  22132. ' IUnknown = interface',
  22133. ' function _AddRef: longint;',
  22134. ' function _Release: longint;',
  22135. ' end;',
  22136. ' TObject = class',
  22137. ' end;',
  22138. ' TArrOfIntf = array of IUnknown;',
  22139. 'begin',
  22140. '']);
  22141. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22142. ConvertProgram;
  22143. end;
  22144. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22145. begin
  22146. StartProgram(false);
  22147. Add([
  22148. '{$interfaces com}',
  22149. 'type',
  22150. ' IUnknown = interface',
  22151. ' function _AddRef: longint;',
  22152. ' function _Release: longint;',
  22153. ' end;',
  22154. ' TRec = record',
  22155. ' i: IUnknown;',
  22156. ' end;',
  22157. 'begin',
  22158. '']);
  22159. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22160. ConvertProgram;
  22161. end;
  22162. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22163. begin
  22164. StartUnit(false);
  22165. Add([
  22166. '{$interfaces com}',
  22167. 'interface',
  22168. 'implementation',
  22169. 'type',
  22170. ' IUnknown = interface',
  22171. ' function _AddRef: longint;',
  22172. ' end;',
  22173. ' TObject = class(IUnknown)',
  22174. ' function _AddRef: longint;',
  22175. ' end;',
  22176. 'function TObject._AddRef: longint; begin end;',
  22177. 'var i: IUnknown;',
  22178. ' o: TObject;',
  22179. 'initialization',
  22180. ' i:=nil;',
  22181. ' i:=i;',
  22182. ' i:=o;',
  22183. ' if (o as IUnknown)=nil then ;',
  22184. '']);
  22185. ConvertUnit;
  22186. CheckSource('TestClassInterface_COM_UnitInitialization',
  22187. LinesToStr([ // statements
  22188. 'var $impl = $mod.$impl;',
  22189. '']),
  22190. LinesToStr([ // this.$init
  22191. 'var $ir = rtl.createIntfRefs();',
  22192. 'try {',
  22193. ' rtl.setIntfP($impl, "i", null);',
  22194. ' rtl.setIntfP($impl, "i", $impl.i);',
  22195. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22196. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22197. '} finally {',
  22198. ' $ir.free();',
  22199. '};',
  22200. '']),
  22201. LinesToStr([ // implementation
  22202. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22203. 'rtl.createClass($impl, "TObject", null, function () {',
  22204. ' this.$init = function () {',
  22205. ' };',
  22206. ' this.$final = function () {',
  22207. ' };',
  22208. ' this._AddRef = function () {',
  22209. ' var Result = 0;',
  22210. ' return Result;',
  22211. ' };',
  22212. ' rtl.addIntf(this, $impl.IUnknown);',
  22213. '});',
  22214. '$impl.i = null;',
  22215. '$impl.o = null;',
  22216. ''])
  22217. );
  22218. end;
  22219. procedure TTestModule.TestClassInterface_Corba_GUID;
  22220. begin
  22221. StartProgram(false);
  22222. Add([
  22223. '{$interfaces corba}',
  22224. 'type',
  22225. ' IUnknown = interface',
  22226. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22227. ' end;',
  22228. ' TObject = class end;',
  22229. ' TGUID = record D1, D2, D3, D4: word; end;',
  22230. ' TAliasGUID = TGUID;',
  22231. ' TGUIDString = type string;',
  22232. ' TAliasGUIDString = TGUIDString;',
  22233. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22234. 'begin end;',
  22235. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22236. 'begin end;',
  22237. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22238. 'begin end;',
  22239. 'var',
  22240. ' i: IUnknown;',
  22241. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22242. ' s: TAliasGUIDString;',
  22243. 'begin',
  22244. ' DoConstGUIDIt(IUnknown);',
  22245. ' DoDefGUID(IUnknown);',
  22246. ' DoStr(IUnknown);',
  22247. ' DoConstGUIDIt(i);',
  22248. ' DoDefGUID(i);',
  22249. ' DoStr(i);',
  22250. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22251. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22252. ' DoStr(g);',
  22253. ' g:=i;',
  22254. ' g:=IUnknown;',
  22255. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22256. ' s:=i;',
  22257. ' s:=IUnknown;',
  22258. ' s:=g;',
  22259. ' if g=i then ;',
  22260. ' if i=g then ;',
  22261. ' if g=IUnknown then ;',
  22262. ' if IUnknown=g then ;',
  22263. ' if s=i then ;',
  22264. ' if i=s then ;',
  22265. ' if s=IUnknown then ;',
  22266. ' if IUnknown=s then ;',
  22267. ' if s=g then ;',
  22268. ' if g=s then ;',
  22269. '']);
  22270. ConvertProgram;
  22271. CheckSource('TestClassInterface_Corba_GUID',
  22272. LinesToStr([ // statements
  22273. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22274. 'rtl.createClass(this, "TObject", null, function () {',
  22275. ' this.$init = function () {',
  22276. ' };',
  22277. ' this.$final = function () {',
  22278. ' };',
  22279. '});',
  22280. 'rtl.recNewT(this, "TGUID", function () {',
  22281. ' this.D1 = 0;',
  22282. ' this.D2 = 0;',
  22283. ' this.D3 = 0;',
  22284. ' this.D4 = 0;',
  22285. ' this.$eq = function (b) {',
  22286. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22287. ' };',
  22288. ' this.$assign = function (s) {',
  22289. ' this.D1 = s.D1;',
  22290. ' this.D2 = s.D2;',
  22291. ' this.D3 = s.D3;',
  22292. ' this.D4 = s.D4;',
  22293. ' return this;',
  22294. ' };',
  22295. '});',
  22296. 'this.DoConstGUIDIt = function (g) {',
  22297. '};',
  22298. 'this.DoDefGUID = function (g) {',
  22299. '};',
  22300. 'this.DoStr = function (s) {',
  22301. '};',
  22302. 'this.i = null;',
  22303. 'this.g = this.TGUID.$clone({',
  22304. ' D1: 0xD91C9AF4,',
  22305. ' D2: 0x3C93,',
  22306. ' D3: 0x420F,',
  22307. ' D4: [',
  22308. ' 0xA3,',
  22309. ' 0x03,',
  22310. ' 0xBF,',
  22311. ' 0x5B,',
  22312. ' 0xA8,',
  22313. ' 0x2B,',
  22314. ' 0xFD,',
  22315. ' 0x23',
  22316. ' ]',
  22317. '});',
  22318. 'this.s = "";',
  22319. '']),
  22320. LinesToStr([ // $mod.$main
  22321. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22322. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22323. '$mod.DoStr($mod.IUnknown.$guid);',
  22324. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22325. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22326. '$mod.DoStr($mod.i.$guid);',
  22327. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22328. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22329. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22330. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22331. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22332. '$mod.g.$assign({',
  22333. ' D1: 0xD91C9AF4,',
  22334. ' D2: 0x3C93,',
  22335. ' D3: 0x420F,',
  22336. ' D4: [',
  22337. ' 0xA3,',
  22338. ' 0x03,',
  22339. ' 0xBF,',
  22340. ' 0x5B,',
  22341. ' 0xA8,',
  22342. ' 0x2B,',
  22343. ' 0xFD,',
  22344. ' 0x23',
  22345. ' ]',
  22346. '});',
  22347. '$mod.s = $mod.i.$guid;',
  22348. '$mod.s = $mod.IUnknown.$guid;',
  22349. '$mod.s = rtl.guidrToStr($mod.g);',
  22350. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22351. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22352. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22353. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22354. 'if ($mod.s === $mod.i.$guid) ;',
  22355. 'if ($mod.i.$guid === $mod.s) ;',
  22356. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22357. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22358. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22359. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22360. '']));
  22361. end;
  22362. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22363. begin
  22364. StartProgram(false);
  22365. Add([
  22366. '{$interfaces corba}',
  22367. 'type',
  22368. ' IUnknown = interface',
  22369. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22370. ' end;',
  22371. ' TGUID = record D1, D2, D3, D4: word; end;',
  22372. ' TAliasGUID = TGUID;',
  22373. ' TGUIDString = type string;',
  22374. ' TAliasGUIDString = TGUIDString;',
  22375. ' TObject = class',
  22376. ' function GetG: TAliasGUID; virtual; abstract;',
  22377. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22378. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22379. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22380. ' property g: TAliasGUID read GetG write SetG;',
  22381. ' property s: TAliasGUIDString read GetS write SetS;',
  22382. ' end;',
  22383. 'var o: TObject;',
  22384. 'begin',
  22385. ' o.g:=IUnknown;',
  22386. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22387. ' o.s:=IUnknown;',
  22388. ' o.s:=o.g;',
  22389. '']);
  22390. ConvertProgram;
  22391. CheckSource('TestClassInterface_GUIDProperty',
  22392. LinesToStr([ // statements
  22393. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22394. 'rtl.recNewT(this, "TGUID", function () {',
  22395. ' this.D1 = 0;',
  22396. ' this.D2 = 0;',
  22397. ' this.D3 = 0;',
  22398. ' this.D4 = 0;',
  22399. ' this.$eq = function (b) {',
  22400. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22401. ' };',
  22402. ' this.$assign = function (s) {',
  22403. ' this.D1 = s.D1;',
  22404. ' this.D2 = s.D2;',
  22405. ' this.D3 = s.D3;',
  22406. ' this.D4 = s.D4;',
  22407. ' return this;',
  22408. ' };',
  22409. '});',
  22410. 'rtl.createClass(this, "TObject", null, function () {',
  22411. ' this.$init = function () {',
  22412. ' };',
  22413. ' this.$final = function () {',
  22414. ' };',
  22415. '});',
  22416. 'this.o = null;',
  22417. '']),
  22418. LinesToStr([ // $mod.$main
  22419. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22420. '$mod.o.SetG({',
  22421. ' D1: 0xD91C9AF4,',
  22422. ' D2: 0x3C93,',
  22423. ' D3: 0x420F,',
  22424. ' D4: [',
  22425. ' 0xA3,',
  22426. ' 0x03,',
  22427. ' 0xBF,',
  22428. ' 0x5B,',
  22429. ' 0xA8,',
  22430. ' 0x2B,',
  22431. ' 0xFD,',
  22432. ' 0x23',
  22433. ' ]',
  22434. '});',
  22435. '$mod.o.SetS($mod.IUnknown.$guid);',
  22436. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22437. '']));
  22438. end;
  22439. procedure TTestModule.TestClassHelper_ClassVar;
  22440. begin
  22441. StartProgram(false);
  22442. Add([
  22443. 'type',
  22444. ' TObject = class',
  22445. ' end;',
  22446. ' THelper = class helper for TObject',
  22447. ' const',
  22448. ' One = 1;',
  22449. ' Two: word = 2;',
  22450. ' class var',
  22451. ' Glob: word;',
  22452. ' function Foo(w: word): word;',
  22453. ' class function Bar(w: word): word;',
  22454. ' end;',
  22455. 'function THelper.foo(w: word): word;',
  22456. 'begin',
  22457. ' Result:=w;',
  22458. ' Two:=One+w;',
  22459. ' Glob:=Glob;',
  22460. ' Result:=Self.Glob;',
  22461. ' Self.Glob:=Self.Glob;',
  22462. ' with Self do Glob:=Glob;',
  22463. 'end;',
  22464. 'class function THelper.bar(w: word): word;',
  22465. 'begin',
  22466. ' Result:=w;',
  22467. ' Two:=One;',
  22468. ' Glob:=Glob;',
  22469. ' Self.Glob:=Self.Glob;',
  22470. ' with Self do Glob:=Glob;',
  22471. 'end;',
  22472. 'var o: TObject;',
  22473. 'begin',
  22474. ' tobject.two:=tobject.one;',
  22475. ' tobject.Glob:=tobject.Glob;',
  22476. ' with tobject do begin',
  22477. ' two:=one;',
  22478. ' Glob:=Glob;',
  22479. ' end;',
  22480. ' o.two:=o.one;',
  22481. ' o.Glob:=o.Glob;',
  22482. ' with o do begin',
  22483. ' two:=one;',
  22484. ' Glob:=Glob;',
  22485. ' end;',
  22486. '']);
  22487. ConvertProgram;
  22488. CheckSource('TestClassHelper_ClassVar',
  22489. LinesToStr([ // statements
  22490. 'rtl.createClass(this, "TObject", null, function () {',
  22491. ' this.$init = function () {',
  22492. ' };',
  22493. ' this.$final = function () {',
  22494. ' };',
  22495. '});',
  22496. 'rtl.createHelper(this, "THelper", null, function () {',
  22497. ' this.One = 1;',
  22498. ' this.Two = 2;',
  22499. ' this.Glob = 0;',
  22500. ' this.Foo = function (w) {',
  22501. ' var Result = 0;',
  22502. ' Result = w;',
  22503. ' $mod.THelper.Two = 1 + w;',
  22504. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22505. ' Result = $mod.THelper.Glob;',
  22506. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22507. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22508. ' return Result;',
  22509. ' };',
  22510. ' this.Bar = function (w) {',
  22511. ' var Result = 0;',
  22512. ' Result = w;',
  22513. ' $mod.THelper.Two = 1;',
  22514. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22515. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22516. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22517. ' return Result;',
  22518. ' };',
  22519. '});',
  22520. 'this.o = null;',
  22521. '']),
  22522. LinesToStr([ // $mod.$main
  22523. '$mod.THelper.Two = 1;',
  22524. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22525. 'var $with = $mod.TObject;',
  22526. '$mod.THelper.Two = 1;',
  22527. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22528. '$mod.THelper.Two = 1;',
  22529. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22530. 'var $with1 = $mod.o;',
  22531. '$mod.THelper.Two = 1;',
  22532. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22533. '']));
  22534. end;
  22535. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22536. begin
  22537. StartProgram(false);
  22538. Add([
  22539. 'type',
  22540. ' TObject = class',
  22541. ' FSize: word;',
  22542. ' property Size: word read FSize write FSize;',
  22543. ' end;',
  22544. ' THelper = class helper for TObject',
  22545. ' function Foo(w: word = 1): word;',
  22546. ' end;',
  22547. 'function THelper.foo(w: word): word;',
  22548. 'begin',
  22549. ' Result:=Size;',
  22550. ' Size:=Size+2;',
  22551. ' Self.Size:=Self.Size+3;',
  22552. ' FSize:=FSize+4;',
  22553. ' Self.FSize:=Self.FSize+5;',
  22554. ' with Self do begin',
  22555. ' Size:=Size+6;',
  22556. ' FSize:=FSize+7;',
  22557. ' FSize:=FSize+8;',
  22558. ' end;',
  22559. 'end;',
  22560. 'begin',
  22561. '']);
  22562. ConvertProgram;
  22563. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22564. LinesToStr([ // statements
  22565. 'rtl.createClass(this, "TObject", null, function () {',
  22566. ' this.$init = function () {',
  22567. ' this.FSize = 0;',
  22568. ' };',
  22569. ' this.$final = function () {',
  22570. ' };',
  22571. '});',
  22572. 'rtl.createHelper(this, "THelper", null, function () {',
  22573. ' this.Foo = function (w) {',
  22574. ' var Result = 0;',
  22575. ' Result = this.FSize;',
  22576. ' this.FSize = this.FSize + 2;',
  22577. ' this.FSize = this.FSize + 3;',
  22578. ' this.FSize = this.FSize + 4;',
  22579. ' this.FSize = this.FSize + 5;',
  22580. ' this.FSize = this.FSize + 6;',
  22581. ' this.FSize = this.FSize + 7;',
  22582. ' this.FSize = this.FSize + 8;',
  22583. ' return Result;',
  22584. ' };',
  22585. '});',
  22586. '']),
  22587. LinesToStr([ // $mod.$main
  22588. '']));
  22589. end;
  22590. procedure TTestModule.TestClassHelper_Method_Call;
  22591. begin
  22592. StartProgram(false);
  22593. Add([
  22594. 'type',
  22595. ' TObject = class',
  22596. ' procedure Run(w: word = 10);',
  22597. ' end;',
  22598. ' THelper = class helper for TObject',
  22599. ' function Foo(w: word = 1): word;',
  22600. ' end;',
  22601. 'procedure TObject.Run(w: word);',
  22602. 'var o: TObject;',
  22603. 'begin',
  22604. ' Foo;',
  22605. ' Foo();',
  22606. ' Foo(2);',
  22607. ' Self.Foo;',
  22608. ' Self.Foo();',
  22609. ' Self.Foo(3);',
  22610. ' with Self do begin',
  22611. ' Foo;',
  22612. ' Foo();',
  22613. ' Foo(4);',
  22614. ' end;',
  22615. ' with o do Foo(5);',
  22616. 'end;',
  22617. 'function THelper.foo(w: word): word;',
  22618. 'begin',
  22619. ' Run;',
  22620. ' Run();',
  22621. ' Run(11);',
  22622. ' Foo;',
  22623. ' Foo();',
  22624. ' Foo(12);',
  22625. ' Self.Foo;',
  22626. ' Self.Foo();',
  22627. ' Self.Foo(13);',
  22628. ' with Self do begin',
  22629. ' Foo;',
  22630. ' Foo();',
  22631. ' Foo(14);',
  22632. ' end;',
  22633. 'end;',
  22634. 'var Obj: TObject;',
  22635. 'begin',
  22636. ' obj.Foo;',
  22637. ' obj.Foo();',
  22638. ' obj.Foo(21);',
  22639. ' with obj do begin',
  22640. ' Foo;',
  22641. ' Foo();',
  22642. ' Foo(22);',
  22643. ' end;',
  22644. '']);
  22645. ConvertProgram;
  22646. CheckSource('TestClassHelper_Method_Call',
  22647. LinesToStr([ // statements
  22648. 'rtl.createClass(this, "TObject", null, function () {',
  22649. ' this.$init = function () {',
  22650. ' };',
  22651. ' this.$final = function () {',
  22652. ' };',
  22653. ' this.Run = function (w) {',
  22654. ' var o = null;',
  22655. ' $mod.THelper.Foo.call(this, 1);',
  22656. ' $mod.THelper.Foo.call(this, 1);',
  22657. ' $mod.THelper.Foo.call(this, 2);',
  22658. ' $mod.THelper.Foo.call(this, 1);',
  22659. ' $mod.THelper.Foo.call(this, 1);',
  22660. ' $mod.THelper.Foo.call(this, 3);',
  22661. ' $mod.THelper.Foo.call(this, 1);',
  22662. ' $mod.THelper.Foo.call(this, 1);',
  22663. ' $mod.THelper.Foo.call(this, 4);',
  22664. ' $mod.THelper.Foo.call(o, 5);',
  22665. ' };',
  22666. '});',
  22667. 'rtl.createHelper(this, "THelper", null, function () {',
  22668. ' this.Foo = function (w) {',
  22669. ' var Result = 0;',
  22670. ' this.Run(10);',
  22671. ' this.Run(10);',
  22672. ' this.Run(11);',
  22673. ' $mod.THelper.Foo.call(this, 1);',
  22674. ' $mod.THelper.Foo.call(this, 1);',
  22675. ' $mod.THelper.Foo.call(this, 12);',
  22676. ' $mod.THelper.Foo.call(this, 1);',
  22677. ' $mod.THelper.Foo.call(this, 1);',
  22678. ' $mod.THelper.Foo.call(this, 13);',
  22679. ' $mod.THelper.Foo.call(this, 1);',
  22680. ' $mod.THelper.Foo.call(this, 1);',
  22681. ' $mod.THelper.Foo.call(this, 14);',
  22682. ' return Result;',
  22683. ' };',
  22684. '});',
  22685. 'this.Obj = null;',
  22686. '']),
  22687. LinesToStr([ // $mod.$main
  22688. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22689. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22690. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22691. 'var $with = $mod.Obj;',
  22692. '$mod.THelper.Foo.call($with, 1);',
  22693. '$mod.THelper.Foo.call($with, 1);',
  22694. '$mod.THelper.Foo.call($with, 22);',
  22695. '']));
  22696. end;
  22697. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22698. begin
  22699. StartProgram(false);
  22700. Add([
  22701. 'type',
  22702. ' TObject = class',
  22703. ' procedure Run(w: word = 10);',
  22704. ' end;',
  22705. ' THelper = class helper for TObject',
  22706. ' function Foo(w: word = 1): word;',
  22707. ' end;',
  22708. 'procedure TObject.Run(w: word);',
  22709. ' procedure Sub(Self: TObject);',
  22710. ' begin',
  22711. ' Foo;',
  22712. ' Foo();',
  22713. ' Self.Foo;',
  22714. ' Self.Foo();',
  22715. ' with Self do begin',
  22716. ' Foo;',
  22717. ' Foo();',
  22718. ' end;',
  22719. ' end;',
  22720. 'begin',
  22721. 'end;',
  22722. 'function THelper.foo(w: word): word;',
  22723. ' procedure Sub(Self: TObject);',
  22724. ' begin',
  22725. ' Run;',
  22726. ' Run();',
  22727. ' Foo;',
  22728. ' Foo();',
  22729. ' Self.Foo;',
  22730. ' Self.Foo();',
  22731. ' with Self do begin',
  22732. ' Foo;',
  22733. ' Foo();',
  22734. ' end;',
  22735. ' end;',
  22736. 'begin',
  22737. 'end;',
  22738. 'begin',
  22739. '']);
  22740. ConvertProgram;
  22741. CheckSource('TestClassHelper_Method_Nested_Call',
  22742. LinesToStr([ // statements
  22743. 'rtl.createClass(this, "TObject", null, function () {',
  22744. ' this.$init = function () {',
  22745. ' };',
  22746. ' this.$final = function () {',
  22747. ' };',
  22748. ' this.Run = function (w) {',
  22749. ' var $Self = this;',
  22750. ' function Sub(Self) {',
  22751. ' $mod.THelper.Foo.call($Self, 1);',
  22752. ' $mod.THelper.Foo.call($Self, 1);',
  22753. ' $mod.THelper.Foo.call(Self, 1);',
  22754. ' $mod.THelper.Foo.call(Self, 1);',
  22755. ' $mod.THelper.Foo.call(Self, 1);',
  22756. ' $mod.THelper.Foo.call(Self, 1);',
  22757. ' };',
  22758. ' };',
  22759. '});',
  22760. 'rtl.createHelper(this, "THelper", null, function () {',
  22761. ' this.Foo = function (w) {',
  22762. ' var $Self = this;',
  22763. ' var Result = 0;',
  22764. ' function Sub(Self) {',
  22765. ' $Self.Run(10);',
  22766. ' $Self.Run(10);',
  22767. ' $mod.THelper.Foo.call($Self, 1);',
  22768. ' $mod.THelper.Foo.call($Self, 1);',
  22769. ' $mod.THelper.Foo.call(Self, 1);',
  22770. ' $mod.THelper.Foo.call(Self, 1);',
  22771. ' $mod.THelper.Foo.call(Self, 1);',
  22772. ' $mod.THelper.Foo.call(Self, 1);',
  22773. ' };',
  22774. ' return Result;',
  22775. ' };',
  22776. '});',
  22777. '']),
  22778. LinesToStr([ // $mod.$main
  22779. '']));
  22780. end;
  22781. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22782. begin
  22783. StartProgram(false);
  22784. Add([
  22785. 'type',
  22786. ' TObject = class',
  22787. ' class procedure Run(w: word = 10);',
  22788. ' end;',
  22789. ' THelper = class helper for TObject',
  22790. ' class function Foo(w: word = 1): word;',
  22791. ' end;',
  22792. 'class procedure TObject.Run(w: word);',
  22793. 'begin',
  22794. ' Foo;',
  22795. ' Foo();',
  22796. ' Self.Foo;',
  22797. ' Self.Foo();',
  22798. ' with Self do begin',
  22799. ' Foo;',
  22800. ' Foo();',
  22801. ' end;',
  22802. 'end;',
  22803. 'class function THelper.foo(w: word): word;',
  22804. 'begin',
  22805. ' Run;',
  22806. ' Run();',
  22807. ' Foo;',
  22808. ' Foo();',
  22809. ' Self.Foo;',
  22810. ' Self.Foo();',
  22811. ' with Self do begin',
  22812. ' Foo;',
  22813. ' Foo();',
  22814. ' end;',
  22815. 'end;',
  22816. 'var',
  22817. ' Obj: TObject;',
  22818. 'begin',
  22819. ' obj.Foo;',
  22820. ' obj.Foo();',
  22821. ' with obj do begin',
  22822. ' Foo;',
  22823. ' Foo();',
  22824. ' end;',
  22825. ' tobject.Foo;',
  22826. ' tobject.Foo();',
  22827. ' with tobject do begin',
  22828. ' Foo;',
  22829. ' Foo();',
  22830. ' end;',
  22831. '']);
  22832. ConvertProgram;
  22833. CheckSource('TestClassHelper_ClassMethod_Call',
  22834. LinesToStr([ // statements
  22835. 'rtl.createClass(this, "TObject", null, function () {',
  22836. ' this.$init = function () {',
  22837. ' };',
  22838. ' this.$final = function () {',
  22839. ' };',
  22840. ' this.Run = function (w) {',
  22841. ' $mod.THelper.Foo.call(this, 1);',
  22842. ' $mod.THelper.Foo.call(this, 1);',
  22843. ' $mod.THelper.Foo.call(this, 1);',
  22844. ' $mod.THelper.Foo.call(this, 1);',
  22845. ' $mod.THelper.Foo.call(this, 1);',
  22846. ' $mod.THelper.Foo.call(this, 1);',
  22847. ' };',
  22848. '});',
  22849. 'rtl.createHelper(this, "THelper", null, function () {',
  22850. ' this.Foo = function (w) {',
  22851. ' var Result = 0;',
  22852. ' this.Run(10);',
  22853. ' this.Run(10);',
  22854. ' $mod.THelper.Foo.call(this, 1);',
  22855. ' $mod.THelper.Foo.call(this, 1);',
  22856. ' $mod.THelper.Foo.call(this, 1);',
  22857. ' $mod.THelper.Foo.call(this, 1);',
  22858. ' $mod.THelper.Foo.call(this, 1);',
  22859. ' $mod.THelper.Foo.call(this, 1);',
  22860. ' return Result;',
  22861. ' };',
  22862. '});',
  22863. 'this.Obj = null;',
  22864. '']),
  22865. LinesToStr([ // $mod.$main
  22866. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22867. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22868. 'var $with = $mod.Obj;',
  22869. '$mod.THelper.Foo.call($with.$class, 1);',
  22870. '$mod.THelper.Foo.call($with.$class, 1);',
  22871. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22872. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22873. 'var $with1 = $mod.TObject;',
  22874. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22875. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22876. '']));
  22877. end;
  22878. procedure TTestModule.TestClassHelper_ClassOf;
  22879. begin
  22880. StartProgram(false);
  22881. Add([
  22882. 'type',
  22883. ' TObject = class',
  22884. ' end;',
  22885. ' TClass = class of TObject;',
  22886. ' THelper = class helper for TObject',
  22887. ' class function Foo(w: word = 1): word;',
  22888. ' end;',
  22889. 'class function THelper.foo(w: word): word;',
  22890. 'begin',
  22891. 'end;',
  22892. 'var',
  22893. ' c: TClass;',
  22894. 'begin',
  22895. ' c.Foo;',
  22896. ' c.Foo();',
  22897. ' with c do begin',
  22898. ' Foo;',
  22899. ' Foo();',
  22900. ' end;',
  22901. '']);
  22902. ConvertProgram;
  22903. CheckSource('TestClassHelper_ClassOf',
  22904. LinesToStr([ // statements
  22905. 'rtl.createClass(this, "TObject", null, function () {',
  22906. ' this.$init = function () {',
  22907. ' };',
  22908. ' this.$final = function () {',
  22909. ' };',
  22910. '});',
  22911. 'rtl.createHelper(this, "THelper", null, function () {',
  22912. ' this.Foo = function (w) {',
  22913. ' var Result = 0;',
  22914. ' return Result;',
  22915. ' };',
  22916. '});',
  22917. 'this.c = null;',
  22918. '']),
  22919. LinesToStr([ // $mod.$main
  22920. '$mod.THelper.Foo.call($mod.c, 1);',
  22921. '$mod.THelper.Foo.call($mod.c, 1);',
  22922. 'var $with = $mod.c;',
  22923. '$mod.THelper.Foo.call($with, 1);',
  22924. '$mod.THelper.Foo.call($with, 1);',
  22925. '']));
  22926. end;
  22927. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22928. begin
  22929. StartProgram(false);
  22930. Add([
  22931. '{$mode objfpc}',
  22932. 'type',
  22933. ' TObject = class',
  22934. ' procedure DoIt;',
  22935. ' end;',
  22936. ' THelper = class helper for TObject',
  22937. ' procedure Fly(w: word = 1);',
  22938. ' class procedure Glide(w: word = 1);',
  22939. ' class procedure Run(w: word = 1); static;',
  22940. ' end;',
  22941. ' TFly = procedure(w: word) of object;',
  22942. ' TGlide = TFly;',
  22943. ' TRun = procedure(w: word);',
  22944. 'var',
  22945. ' f: TFly;',
  22946. ' g: TGlide;',
  22947. ' r: TRun;',
  22948. 'procedure TObject.DoIt;',
  22949. 'begin',
  22950. ' f:=@fly;',
  22951. ' g:=@glide;',
  22952. ' r:=@run;',
  22953. ' f:[email protected];',
  22954. ' g:[email protected];',
  22955. ' r:[email protected];',
  22956. ' with self do begin',
  22957. ' f:=@fly;',
  22958. ' g:=@glide;',
  22959. ' r:=@run;',
  22960. ' end;',
  22961. 'end;',
  22962. 'procedure THelper.fly(w: word);',
  22963. 'begin',
  22964. ' f:=@fly;',
  22965. ' g:=@glide;',
  22966. ' r:=@run;',
  22967. 'end;',
  22968. 'class procedure THelper.glide(w: word);',
  22969. 'begin',
  22970. ' g:=@glide;',
  22971. ' r:=@run;',
  22972. 'end;',
  22973. 'class procedure THelper.run(w: word);',
  22974. 'begin',
  22975. ' g:=@glide;',
  22976. ' r:=@run;',
  22977. 'end;',
  22978. 'var',
  22979. ' Obj: TObject;',
  22980. 'begin',
  22981. ' f:[email protected];',
  22982. ' g:[email protected];',
  22983. ' r:[email protected];',
  22984. ' with obj do begin',
  22985. ' f:=@fly;',
  22986. ' g:=@glide;',
  22987. ' r:=@run;',
  22988. ' end;',
  22989. ' g:[email protected];',
  22990. ' r:[email protected];',
  22991. ' with tobject do begin',
  22992. ' g:=@glide;',
  22993. ' r:=@run;',
  22994. ' end;',
  22995. '']);
  22996. ConvertProgram;
  22997. CheckSource('TestClassHelper_MethodRefObjFPC',
  22998. LinesToStr([ // statements
  22999. 'rtl.createClass(this, "TObject", null, function () {',
  23000. ' this.$init = function () {',
  23001. ' };',
  23002. ' this.$final = function () {',
  23003. ' };',
  23004. ' this.DoIt = function () {',
  23005. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23006. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23007. ' $mod.r = $mod.THelper.Run;',
  23008. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23009. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23010. ' $mod.r = $mod.THelper.Run;',
  23011. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23012. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23013. ' $mod.r = $mod.THelper.Run;',
  23014. ' };',
  23015. '});',
  23016. 'rtl.createHelper(this, "THelper", null, function () {',
  23017. ' this.Fly = function (w) {',
  23018. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23019. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23020. ' $mod.r = $mod.THelper.Run;',
  23021. ' };',
  23022. ' this.Glide = function (w) {',
  23023. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23024. ' $mod.r = $mod.THelper.Run;',
  23025. ' };',
  23026. ' this.Run = function (w) {',
  23027. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23028. ' $mod.r = $mod.THelper.Run;',
  23029. ' };',
  23030. '});',
  23031. 'this.f = null;',
  23032. 'this.g = null;',
  23033. 'this.r = null;',
  23034. 'this.Obj = null;',
  23035. '']),
  23036. LinesToStr([ // $mod.$main
  23037. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23038. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23039. '$mod.r = $mod.THelper.Run;',
  23040. 'var $with = $mod.Obj;',
  23041. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23042. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23043. '$mod.r = $mod.THelper.Run;',
  23044. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23045. '$mod.r = $mod.THelper.Run;',
  23046. 'var $with1 = $mod.TObject;',
  23047. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23048. '$mod.r = $mod.THelper.Run;',
  23049. '']));
  23050. end;
  23051. procedure TTestModule.TestClassHelper_Constructor;
  23052. begin
  23053. StartProgram(false);
  23054. Add([
  23055. 'type',
  23056. ' TObject = class',
  23057. ' constructor Create;',
  23058. ' end;',
  23059. ' TClass = class of TObject;',
  23060. ' THelper = class helper for TObject',
  23061. ' constructor NewHlp(w: word);',
  23062. ' end;',
  23063. 'var',
  23064. ' obj: TObject;',
  23065. ' c: TClass;',
  23066. 'constructor TObject.Create;',
  23067. 'begin',
  23068. ' NewHlp(2);', // normal call
  23069. ' tobject.NewHlp(3);', // new instance
  23070. ' c.newhlp(4);', // new instance
  23071. 'end;',
  23072. 'constructor THelper.NewHlp(w: word);',
  23073. 'begin',
  23074. ' create;', // normal call
  23075. ' tobject.create;', // new instance
  23076. ' NewHlp(2);', // normal call
  23077. ' tobject.NewHlp(3);', // new instance
  23078. ' c.newhlp(4);', // new instance
  23079. 'end;',
  23080. 'begin',
  23081. ' obj.newhlp(2);', // normal call
  23082. ' with Obj do newhlp(12);', // normal call
  23083. ' tobject.newhlp(3);', // new instance
  23084. ' with tobject do newhlp(13);', // new instance
  23085. ' c.newhlp(4);', // new instance
  23086. ' with c do newhlp(14);', // new instance
  23087. '']);
  23088. ConvertProgram;
  23089. CheckSource('TestClassHelper_Constructor',
  23090. LinesToStr([ // statements
  23091. 'rtl.createClass(this, "TObject", null, function () {',
  23092. ' this.$init = function () {',
  23093. ' };',
  23094. ' this.$final = function () {',
  23095. ' };',
  23096. ' this.Create = function () {',
  23097. ' $mod.THelper.NewHlp.call(this, 2);',
  23098. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23099. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23100. ' return this;',
  23101. ' };',
  23102. '});',
  23103. 'rtl.createHelper(this, "THelper", null, function () {',
  23104. ' this.NewHlp = function (w) {',
  23105. ' this.Create();',
  23106. ' $mod.TObject.$create("Create");',
  23107. ' $mod.THelper.NewHlp.call(this, 2);',
  23108. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23109. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23110. ' return this;',
  23111. ' };',
  23112. '});',
  23113. 'this.obj = null;',
  23114. 'this.c = null;',
  23115. '']),
  23116. LinesToStr([ // $mod.$main
  23117. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23118. 'var $with = $mod.obj;',
  23119. '$mod.THelper.NewHlp.call($with, 12);',
  23120. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23121. 'var $with1 = $mod.TObject;',
  23122. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23123. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23124. 'var $with2 = $mod.c;',
  23125. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23126. '']));
  23127. end;
  23128. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23129. begin
  23130. StartProgram(false);
  23131. Add([
  23132. 'type',
  23133. ' TObject = class',
  23134. ' procedure Fly;',
  23135. ' end;',
  23136. ' TObjHelper = class helper for TObject',
  23137. ' procedure Fly;',
  23138. ' end;',
  23139. ' TBird = class',
  23140. ' procedure Fly;',
  23141. ' end;',
  23142. ' TBirdHelper = class helper for TBird',
  23143. ' procedure Fly;',
  23144. ' procedure Walk(w: word);',
  23145. ' end;',
  23146. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23147. ' procedure Fly;',
  23148. ' procedure Walk(w: word);',
  23149. ' end;',
  23150. 'procedure Tobject.fly;',
  23151. 'begin',
  23152. ' inherited;', // ignore
  23153. 'end;',
  23154. 'procedure Tobjhelper.fly;',
  23155. 'begin',
  23156. ' {@TObject_Fly}inherited;',
  23157. ' inherited {@TObject_Fly}Fly;',
  23158. 'end;',
  23159. 'procedure Tbird.fly;',
  23160. 'begin',
  23161. ' {@TObjHelper_Fly}inherited;',
  23162. ' inherited {@TObjHelper_Fly}Fly;',
  23163. 'end;',
  23164. 'procedure Tbirdhelper.fly;',
  23165. 'begin',
  23166. ' {@TBird_Fly}inherited;',
  23167. ' inherited {@TBird_Fly}Fly;',
  23168. 'end;',
  23169. 'procedure Tbirdhelper.walk(w: word);',
  23170. 'begin',
  23171. 'end;',
  23172. 'procedure teagleHelper.fly;',
  23173. 'begin',
  23174. ' {@TBird_Fly}inherited;',
  23175. ' inherited {@TBird_Fly}Fly;',
  23176. 'end;',
  23177. 'procedure teagleHelper.walk(w: word);',
  23178. 'begin',
  23179. ' {@TBirdHelper_Walk}inherited;',
  23180. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23181. 'end;',
  23182. 'begin',
  23183. '']);
  23184. ConvertProgram;
  23185. CheckSource('TestClassHelper_InheritedObjFPC',
  23186. LinesToStr([ // statements
  23187. 'rtl.createClass(this, "TObject", null, function () {',
  23188. ' this.$init = function () {',
  23189. ' };',
  23190. ' this.$final = function () {',
  23191. ' };',
  23192. ' this.Fly = function () {',
  23193. ' };',
  23194. '});',
  23195. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23196. ' this.Fly = function () {',
  23197. ' $mod.TObject.Fly.call(this);',
  23198. ' $mod.TObject.Fly.call(this);',
  23199. ' };',
  23200. '});',
  23201. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23202. ' this.Fly$1 = function () {',
  23203. ' $mod.TObjHelper.Fly.call(this);',
  23204. ' $mod.TObjHelper.Fly.call(this);',
  23205. ' };',
  23206. '});',
  23207. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23208. ' this.Fly = function () {',
  23209. ' $mod.TBird.Fly$1.call(this);',
  23210. ' $mod.TBird.Fly$1.call(this);',
  23211. ' };',
  23212. ' this.Walk = function (w) {',
  23213. ' };',
  23214. '});',
  23215. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23216. ' this.Fly$1 = function () {',
  23217. ' $mod.TBird.Fly$1.call(this);',
  23218. ' $mod.TBird.Fly$1.call(this);',
  23219. ' };',
  23220. ' this.Walk$1 = function (w) {',
  23221. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23222. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23223. ' };',
  23224. '});',
  23225. '']),
  23226. LinesToStr([ // $mod.$main
  23227. '']));
  23228. end;
  23229. procedure TTestModule.TestClassHelper_Property;
  23230. begin
  23231. StartProgram(false);
  23232. Add([
  23233. 'type',
  23234. ' TObject = class',
  23235. ' FSize: word;',
  23236. ' function GetSpeed: word;',
  23237. ' procedure SetSpeed(Value: word);',
  23238. ' end;',
  23239. ' TObjHelper = class helper for TObject',
  23240. ' function GetLeft: word;',
  23241. ' procedure SetLeft(Value: word);',
  23242. ' property Size: word read FSize write FSize;',
  23243. ' property Speed: word read GetSpeed write SetSpeed;',
  23244. ' property Left: word read GetLeft write SetLeft;',
  23245. ' end;',
  23246. ' TBird = class',
  23247. ' property NotRight: word read GetLeft write SetLeft;',
  23248. ' procedure DoIt;',
  23249. ' end;',
  23250. 'var',
  23251. ' b: TBird;',
  23252. 'function Tobject.GetSpeed: word;',
  23253. 'begin',
  23254. ' Size:=Size+11;',
  23255. ' Speed:=Speed+12;',
  23256. ' Result:=Left+13;',
  23257. ' Left:=13;',
  23258. ' Left:=Left+13;',
  23259. ' Self.Size:=Self.Size+21;',
  23260. ' Self.Speed:=Self.Speed+22;',
  23261. ' Self.Left:=Self.Left+23;',
  23262. ' with Self do begin',
  23263. ' Size:=Size+31;',
  23264. ' Speed:=Speed+32;',
  23265. ' Left:=Left+33;',
  23266. ' end;',
  23267. 'end;',
  23268. 'procedure Tobject.SetSpeed(Value: word);',
  23269. 'begin',
  23270. 'end;',
  23271. 'function TObjHelper.GetLeft: word;',
  23272. 'begin',
  23273. ' Size:=Size+11;',
  23274. ' Speed:=Speed+12;',
  23275. ' Left:=Left+13;',
  23276. ' Self.Size:=Self.Size+21;',
  23277. ' Self.Speed:=Self.Speed+22;',
  23278. ' Self.Left:=Self.Left+23;',
  23279. ' with Self do begin',
  23280. ' Size:=Size+31;',
  23281. ' Speed:=Speed+32;',
  23282. ' Left:=Left+33;',
  23283. ' end;',
  23284. 'end;',
  23285. 'procedure TObjHelper.SetLeft(Value: word);',
  23286. 'begin',
  23287. 'end;',
  23288. 'procedure TBird.DoIt;',
  23289. 'begin',
  23290. ' NotRight:=NotRight+11;',
  23291. ' Self.NotRight:=Self.NotRight+21;',
  23292. ' with Self do begin',
  23293. ' NotRight:=NotRight+31;',
  23294. ' end;',
  23295. 'end;',
  23296. 'begin',
  23297. ' b.Size:=b.Size+11;',
  23298. ' b.Speed:=b.Speed+12;',
  23299. ' b.Left:=b.Left+13;',
  23300. ' b.NotRight:=b.NotRight+14;',
  23301. ' with b do begin',
  23302. ' Size:=Size+31;',
  23303. ' Speed:=Speed+32;',
  23304. ' Left:=Left+33;',
  23305. ' NotRight:=NotRight+34;',
  23306. ' end;',
  23307. '']);
  23308. ConvertProgram;
  23309. CheckSource('TestClassHelper_Property',
  23310. LinesToStr([ // statements
  23311. 'rtl.createClass(this, "TObject", null, function () {',
  23312. ' this.$init = function () {',
  23313. ' this.FSize = 0;',
  23314. ' };',
  23315. ' this.$final = function () {',
  23316. ' };',
  23317. ' this.GetSpeed = function () {',
  23318. ' var Result = 0;',
  23319. ' this.FSize = this.FSize + 11;',
  23320. ' this.SetSpeed(this.GetSpeed() + 12);',
  23321. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23322. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23323. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23324. ' this.FSize = this.FSize + 21;',
  23325. ' this.SetSpeed(this.GetSpeed() + 22);',
  23326. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23327. ' this.FSize = this.FSize + 31;',
  23328. ' this.SetSpeed(this.GetSpeed() + 32);',
  23329. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23330. ' return Result;',
  23331. ' };',
  23332. ' this.SetSpeed = function (Value) {',
  23333. ' };',
  23334. '});',
  23335. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23336. ' this.GetLeft = function () {',
  23337. ' var Result = 0;',
  23338. ' this.FSize = this.FSize + 11;',
  23339. ' this.SetSpeed(this.GetSpeed() + 12);',
  23340. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23341. ' this.FSize = this.FSize + 21;',
  23342. ' this.SetSpeed(this.GetSpeed() + 22);',
  23343. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23344. ' this.FSize = this.FSize + 31;',
  23345. ' this.SetSpeed(this.GetSpeed() + 32);',
  23346. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23347. ' return Result;',
  23348. ' };',
  23349. ' this.SetLeft = function (Value) {',
  23350. ' };',
  23351. '});',
  23352. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23353. ' this.DoIt = function () {',
  23354. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23355. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23356. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23357. ' };',
  23358. '});',
  23359. 'this.b = null;',
  23360. '']),
  23361. LinesToStr([ // $mod.$main
  23362. '$mod.b.FSize = $mod.b.FSize + 11;',
  23363. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23364. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23365. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23366. 'var $with = $mod.b;',
  23367. '$with.FSize = $with.FSize + 31;',
  23368. '$with.SetSpeed($with.GetSpeed() + 32);',
  23369. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23370. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23371. '']));
  23372. end;
  23373. procedure TTestModule.TestClassHelper_Property_Array;
  23374. begin
  23375. StartProgram(false);
  23376. Add([
  23377. 'type',
  23378. ' TObject = class',
  23379. ' function GetSpeed(Index: boolean): word;',
  23380. ' procedure SetSpeed(Index: boolean; Value: word);',
  23381. ' end;',
  23382. ' TObjHelper = class helper for TObject',
  23383. ' function GetSize(Index: boolean): word;',
  23384. ' procedure SetSize(Index: boolean; Value: word);',
  23385. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23386. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23387. ' end;',
  23388. ' TBird = class',
  23389. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23390. ' procedure DoIt;',
  23391. ' end;',
  23392. 'var',
  23393. ' b: TBird;',
  23394. 'function Tobject.GetSpeed(Index: boolean): word;',
  23395. 'begin',
  23396. ' Result:=Size[false];',
  23397. ' Size[true]:=Size[false]+11;',
  23398. ' Speed[true]:=Speed[false]+12;',
  23399. ' Self.Size[true]:=Self.Size[false]+21;',
  23400. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23401. ' with Self do begin',
  23402. ' Size[true]:=Size[false]+31;',
  23403. ' Speed[true]:=Speed[false]+32;',
  23404. ' end;',
  23405. 'end;',
  23406. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23407. 'begin',
  23408. 'end;',
  23409. 'function TObjHelper.GetSize(Index: boolean): word;',
  23410. 'begin',
  23411. ' Size[true]:=Size[false]+11;',
  23412. ' Speed[true]:=Speed[false]+12;',
  23413. ' Self.Size[true]:=Self.Size[false]+21;',
  23414. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23415. ' with Self do begin',
  23416. ' Size[true]:=Size[false]+31;',
  23417. ' Speed[true]:=Speed[false]+32;',
  23418. ' end;',
  23419. 'end;',
  23420. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23421. 'begin',
  23422. 'end;',
  23423. 'procedure TBird.DoIt;',
  23424. 'begin',
  23425. ' Items[true]:=Items[false]+11;',
  23426. ' Self.Items[true]:=Self.Items[false]+21;',
  23427. ' with Self do Items[true]:=Items[false]+31;',
  23428. 'end;',
  23429. 'begin',
  23430. ' b.Size[true]:=b.Size[false]+11;',
  23431. ' b.Speed[true]:=b.Speed[false]+12;',
  23432. ' b.Items[true]:=b.Items[false]+13;',
  23433. ' with b do begin',
  23434. ' Size[true]:=Size[false]+21;',
  23435. ' Speed[true]:=Speed[false]+22;',
  23436. ' Items[true]:=Items[false]+23;',
  23437. ' end;',
  23438. '']);
  23439. ConvertProgram;
  23440. CheckSource('TestClassHelper_Property_Array',
  23441. LinesToStr([ // statements
  23442. 'rtl.createClass(this, "TObject", null, function () {',
  23443. ' this.$init = function () {',
  23444. ' };',
  23445. ' this.$final = function () {',
  23446. ' };',
  23447. ' this.GetSpeed = function (Index) {',
  23448. ' var Result = 0;',
  23449. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23450. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23451. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23452. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23453. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23454. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23455. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23456. ' return Result;',
  23457. ' };',
  23458. ' this.SetSpeed = function (Index, Value) {',
  23459. ' };',
  23460. '});',
  23461. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23462. ' this.GetSize = function (Index) {',
  23463. ' var Result = 0;',
  23464. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23465. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23466. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23467. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23468. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23469. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23470. ' return Result;',
  23471. ' };',
  23472. ' this.SetSize = function (Index, Value) {',
  23473. ' };',
  23474. '});',
  23475. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23476. ' this.DoIt = function () {',
  23477. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23478. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23479. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23480. ' };',
  23481. '});',
  23482. 'this.b = null;',
  23483. '']),
  23484. LinesToStr([ // $mod.$main
  23485. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23486. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23487. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23488. 'var $with = $mod.b;',
  23489. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23490. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23491. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23492. '']));
  23493. end;
  23494. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23495. begin
  23496. StartProgram(false);
  23497. Add([
  23498. 'type',
  23499. ' TObject = class',
  23500. ' function GetSpeed(Index: boolean): word;',
  23501. ' procedure SetSpeed(Index: boolean; Value: word);',
  23502. ' end;',
  23503. ' TObjHelper = class helper for TObject',
  23504. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23505. ' end;',
  23506. ' TBird = class',
  23507. ' end;',
  23508. ' TBirdHelper = class helper for TBird',
  23509. ' function GetSize(Index: word): boolean;',
  23510. ' procedure SetSize(Index: word; Value: boolean);',
  23511. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23512. ' end;',
  23513. 'function Tobject.GetSpeed(Index: boolean): word;',
  23514. 'begin',
  23515. ' Self[true]:=Self[false]+1;',
  23516. 'end;',
  23517. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23518. 'begin',
  23519. 'end;',
  23520. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23521. 'begin',
  23522. ' Self[1]:=not Self[2];',
  23523. 'end;',
  23524. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23525. 'begin',
  23526. 'end;',
  23527. 'var',
  23528. ' o: TObject;',
  23529. ' b: TBird;',
  23530. 'begin',
  23531. ' o[true]:=o[false]+1;',
  23532. ' b[3]:=not b[4];',
  23533. '']);
  23534. ConvertProgram;
  23535. CheckSource('TestClassHelper_Property_Array_Default',
  23536. LinesToStr([ // statements
  23537. 'rtl.createClass(this, "TObject", null, function () {',
  23538. ' this.$init = function () {',
  23539. ' };',
  23540. ' this.$final = function () {',
  23541. ' };',
  23542. ' this.GetSpeed = function (Index) {',
  23543. ' var Result = 0;',
  23544. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23545. ' return Result;',
  23546. ' };',
  23547. ' this.SetSpeed = function (Index, Value) {',
  23548. ' };',
  23549. '});',
  23550. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23551. '});',
  23552. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23553. '});',
  23554. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23555. ' this.GetSize = function (Index) {',
  23556. ' var Result = false;',
  23557. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23558. ' return Result;',
  23559. ' };',
  23560. ' this.SetSize = function (Index, Value) {',
  23561. ' };',
  23562. '});',
  23563. 'this.o = null;',
  23564. 'this.b = null;',
  23565. '']),
  23566. LinesToStr([ // $mod.$main
  23567. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23568. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23569. '']));
  23570. end;
  23571. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23572. begin
  23573. StartProgram(false);
  23574. Add([
  23575. 'type',
  23576. ' TObject = class',
  23577. ' end;',
  23578. ' TObjHelper = class helper for TObject',
  23579. ' function GetItems(Index: word): TObject;',
  23580. ' procedure SetItems(Index: word; Value: TObject);',
  23581. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23582. ' end;',
  23583. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23584. 'begin',
  23585. ' Self[1][2]:=Self[3][4];',
  23586. 'end;',
  23587. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23588. 'begin',
  23589. 'end;',
  23590. 'var',
  23591. ' o: TObject;',
  23592. 'begin',
  23593. ' o[1][2]:=o[3][4];',
  23594. '']);
  23595. ConvertProgram;
  23596. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23597. LinesToStr([ // statements
  23598. 'rtl.createClass(this, "TObject", null, function () {',
  23599. ' this.$init = function () {',
  23600. ' };',
  23601. ' this.$final = function () {',
  23602. ' };',
  23603. '});',
  23604. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23605. ' this.GetItems = function (Index) {',
  23606. ' var Result = null;',
  23607. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23608. ' return Result;',
  23609. ' };',
  23610. ' this.SetItems = function (Index, Value) {',
  23611. ' };',
  23612. '});',
  23613. 'this.o = null;',
  23614. '']),
  23615. LinesToStr([ // $mod.$main
  23616. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23617. '']));
  23618. end;
  23619. procedure TTestModule.TestClassHelper_ClassProperty;
  23620. begin
  23621. StartProgram(false);
  23622. Add([
  23623. 'type',
  23624. ' TObject = class',
  23625. ' class var FSize: word;',
  23626. ' class function GetSpeed: word;',
  23627. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23628. ' end;',
  23629. ' TObjHelper = class helper for TObject',
  23630. ' class function GetLeft: word;',
  23631. ' class procedure SetLeft(Value: word);',
  23632. ' class property Size: word read FSize write FSize;',
  23633. ' class property Speed: word read GetSpeed write SetSpeed;',
  23634. ' class property Left: word read GetLeft write SetLeft;',
  23635. ' end;',
  23636. ' TBird = class',
  23637. ' class property NotRight: word read GetLeft write SetLeft;',
  23638. ' class procedure DoIt;',
  23639. ' end;',
  23640. ' TBirdClass = class of TBird;',
  23641. 'class function Tobject.GetSpeed: word;',
  23642. 'begin',
  23643. ' Size:=Size+11;',
  23644. ' Speed:=Speed+12;',
  23645. ' Left:=Left+13;',
  23646. ' Self.Size:=Self.Size+21;',
  23647. ' Self.Speed:=Self.Speed+22;',
  23648. ' Self.Left:=Self.Left+23;',
  23649. ' with Self do begin',
  23650. ' Size:=Size+31;',
  23651. ' Speed:=Speed+32;',
  23652. ' Left:=Left+33;',
  23653. ' end;',
  23654. 'end;',
  23655. 'class function TObjHelper.GetLeft: word;',
  23656. 'begin',
  23657. ' Size:=Size+11;',
  23658. ' Speed:=Speed+12;',
  23659. ' Left:=Left+13;',
  23660. ' Self.Size:=Self.Size+21;',
  23661. ' Self.Speed:=Self.Speed+22;',
  23662. ' Self.Left:=Self.Left+23;',
  23663. ' with Self do begin',
  23664. ' Size:=Size+31;',
  23665. ' Speed:=Speed+32;',
  23666. ' Left:=Left+33;',
  23667. ' end;',
  23668. 'end;',
  23669. 'class procedure TObjHelper.SetLeft(Value: word);',
  23670. 'begin',
  23671. 'end;',
  23672. 'class procedure TBird.DoIt;',
  23673. 'begin',
  23674. ' NotRight:=NotRight+11;',
  23675. ' Self.NotRight:=Self.NotRight+21;',
  23676. ' with Self do NotRight:=NotRight+31;',
  23677. 'end;',
  23678. 'var',
  23679. ' b: TBird;',
  23680. ' c: TBirdClass;',
  23681. 'begin',
  23682. ' b.Size:=b.Size+11;',
  23683. ' b.Speed:=b.Speed+12;',
  23684. ' b.Left:=b.Left+13;',
  23685. ' b.NotRight:=b.NotRight+14;',
  23686. ' with b do begin',
  23687. ' Size:=Size+31;',
  23688. ' Speed:=Speed+32;',
  23689. ' Left:=Left+33;',
  23690. ' NotRight:=NotRight+34;',
  23691. ' end;',
  23692. ' c.Size:=c.Size+11;',
  23693. ' c.Speed:=c.Speed+12;',
  23694. ' c.Left:=c.Left+13;',
  23695. ' c.NotRight:=c.NotRight+14;',
  23696. ' with c do begin',
  23697. ' Size:=Size+31;',
  23698. ' Speed:=Speed+32;',
  23699. ' Left:=Left+33;',
  23700. ' NotRight:=NotRight+34;',
  23701. ' end;',
  23702. ' tbird.Size:=tbird.Size+11;',
  23703. ' tbird.Speed:=tbird.Speed+12;',
  23704. ' tbird.Left:=tbird.Left+13;',
  23705. ' tbird.NotRight:=tbird.NotRight+14;',
  23706. ' with tbird do begin',
  23707. ' Size:=Size+31;',
  23708. ' Speed:=Speed+32;',
  23709. ' Left:=Left+33;',
  23710. ' NotRight:=NotRight+34;',
  23711. ' end;',
  23712. '']);
  23713. ConvertProgram;
  23714. CheckSource('TestClassHelper_ClassProperty',
  23715. LinesToStr([ // statements
  23716. 'rtl.createClass(this, "TObject", null, function () {',
  23717. ' this.FSize = 0;',
  23718. ' this.$init = function () {',
  23719. ' };',
  23720. ' this.$final = function () {',
  23721. ' };',
  23722. ' this.GetSpeed = function () {',
  23723. ' var Result = 0;',
  23724. ' $mod.TObject.FSize = this.FSize + 11;',
  23725. ' this.SetSpeed(this.GetSpeed() + 12);',
  23726. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23727. ' $mod.TObject.FSize = this.FSize + 21;',
  23728. ' this.SetSpeed(this.GetSpeed() + 22);',
  23729. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23730. ' $mod.TObject.FSize = this.FSize + 31;',
  23731. ' this.SetSpeed(this.GetSpeed() + 32);',
  23732. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23733. ' return Result;',
  23734. ' };',
  23735. '});',
  23736. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23737. ' this.GetLeft = function () {',
  23738. ' var Result = 0;',
  23739. ' $mod.TObject.FSize = this.FSize + 11;',
  23740. ' this.SetSpeed(this.GetSpeed() + 12);',
  23741. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23742. ' $mod.TObject.FSize = this.FSize + 21;',
  23743. ' this.SetSpeed(this.GetSpeed() + 22);',
  23744. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23745. ' $mod.TObject.FSize = this.FSize + 31;',
  23746. ' this.SetSpeed(this.GetSpeed() + 32);',
  23747. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23748. ' return Result;',
  23749. ' };',
  23750. ' this.SetLeft = function (Value) {',
  23751. ' };',
  23752. '});',
  23753. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23754. ' this.DoIt = function () {',
  23755. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23756. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23757. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23758. ' };',
  23759. '});',
  23760. 'this.b = null;',
  23761. 'this.c = null;',
  23762. '']),
  23763. LinesToStr([ // $mod.$main
  23764. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23765. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23766. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23767. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23768. 'var $with = $mod.b;',
  23769. '$mod.TObject.FSize = $with.FSize + 31;',
  23770. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23771. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23772. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23773. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23774. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23775. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23776. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23777. 'var $with1 = $mod.c;',
  23778. '$mod.TObject.FSize = $with1.FSize + 31;',
  23779. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23780. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23781. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23782. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23783. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23784. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23785. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23786. 'var $with2 = $mod.TBird;',
  23787. '$mod.TObject.FSize = $with2.FSize + 31;',
  23788. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23789. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23790. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23791. '']));
  23792. end;
  23793. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23794. begin
  23795. StartProgram(false);
  23796. Add([
  23797. 'type',
  23798. ' TObject = class',
  23799. ' class function GetSpeed: word; static;',
  23800. ' class procedure SetSpeed(Value: word); static;',
  23801. ' end;',
  23802. ' TObjHelper = class helper for TObject',
  23803. ' class function GetLeft: word; static;',
  23804. ' class procedure SetLeft(Value: word); static;',
  23805. ' class property Speed: word read GetSpeed write SetSpeed;',
  23806. ' class property Left: word read GetLeft write SetLeft;',
  23807. ' end;',
  23808. ' TBird = class',
  23809. ' class property NotRight: word read GetLeft write SetLeft;',
  23810. ' class procedure DoIt; static;',
  23811. ' class procedure DoSome;',
  23812. ' end;',
  23813. ' TBirdClass = class of TBird;',
  23814. 'class function Tobject.GetSpeed: word;',
  23815. 'begin',
  23816. ' Speed:=Speed+12;',
  23817. ' Left:=Left+13;',
  23818. 'end;',
  23819. 'class procedure TObject.SetSpeed(Value: word);',
  23820. 'begin',
  23821. 'end;',
  23822. 'class function TObjHelper.GetLeft: word;',
  23823. 'begin',
  23824. ' Speed:=Speed+12;',
  23825. ' Left:=Left+13;',
  23826. 'end;',
  23827. 'class procedure TObjHelper.SetLeft(Value: word);',
  23828. 'begin',
  23829. 'end;',
  23830. 'class procedure TBird.DoIt;',
  23831. 'begin',
  23832. ' NotRight:=NotRight+11;',
  23833. 'end;',
  23834. 'class procedure TBird.DoSome;',
  23835. 'begin',
  23836. ' Speed:=Speed+12;',
  23837. ' Left:=Left+13;',
  23838. ' Self.Speed:=Self.Speed+22;',
  23839. ' Self.Left:=Self.Left+23;',
  23840. ' with Self do begin',
  23841. ' Speed:=Speed+32;',
  23842. ' Left:=Left+33;',
  23843. ' end;',
  23844. ' NotRight:=NotRight+11;',
  23845. ' Self.NotRight:=Self.NotRight+21;',
  23846. ' with Self do NotRight:=NotRight+31;',
  23847. 'end;',
  23848. 'var',
  23849. ' b: TBird;',
  23850. ' c: TBirdClass;',
  23851. 'begin',
  23852. ' b.Speed:=b.Speed+12;',
  23853. ' b.Left:=b.Left+13;',
  23854. ' b.NotRight:=b.NotRight+14;',
  23855. ' with b do begin',
  23856. ' Speed:=Speed+32;',
  23857. ' Left:=Left+33;',
  23858. ' NotRight:=NotRight+34;',
  23859. ' end;',
  23860. ' c.Speed:=c.Speed+12;',
  23861. ' c.Left:=c.Left+13;',
  23862. ' c.NotRight:=c.NotRight+14;',
  23863. ' with c do begin',
  23864. ' Speed:=Speed+32;',
  23865. ' Left:=Left+33;',
  23866. ' NotRight:=NotRight+34;',
  23867. ' end;',
  23868. ' tbird.Speed:=tbird.Speed+12;',
  23869. ' tbird.Left:=tbird.Left+13;',
  23870. ' tbird.NotRight:=tbird.NotRight+14;',
  23871. ' with tbird do begin',
  23872. ' Speed:=Speed+32;',
  23873. ' Left:=Left+33;',
  23874. ' NotRight:=NotRight+34;',
  23875. ' end;',
  23876. '']);
  23877. ConvertProgram;
  23878. CheckSource('TestClassHelper_ClassPropertyStatic',
  23879. LinesToStr([ // statements
  23880. 'rtl.createClass(this, "TObject", null, function () {',
  23881. ' this.$init = function () {',
  23882. ' };',
  23883. ' this.$final = function () {',
  23884. ' };',
  23885. ' this.GetSpeed = function () {',
  23886. ' var Result = 0;',
  23887. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23888. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23889. ' return Result;',
  23890. ' };',
  23891. ' this.SetSpeed = function (Value) {',
  23892. ' };',
  23893. '});',
  23894. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23895. ' this.GetLeft = function () {',
  23896. ' var Result = 0;',
  23897. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23898. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23899. ' return Result;',
  23900. ' };',
  23901. ' this.SetLeft = function (Value) {',
  23902. ' };',
  23903. '});',
  23904. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23905. ' this.DoIt = function () {',
  23906. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23907. ' };',
  23908. ' this.DoSome = function () {',
  23909. ' this.SetSpeed(this.GetSpeed() + 12);',
  23910. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23911. ' this.SetSpeed(this.GetSpeed() + 22);',
  23912. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23913. ' this.SetSpeed(this.GetSpeed() + 32);',
  23914. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23915. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23916. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23917. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23918. ' };',
  23919. '});',
  23920. 'this.b = null;',
  23921. 'this.c = null;',
  23922. '']),
  23923. LinesToStr([ // $mod.$main
  23924. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23925. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23926. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23927. 'var $with = $mod.b;',
  23928. '$with.SetSpeed($with.GetSpeed() + 32);',
  23929. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23930. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23931. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23932. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23933. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23934. 'var $with1 = $mod.c;',
  23935. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23936. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23937. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23938. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23939. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23940. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23941. 'var $with2 = $mod.TBird;',
  23942. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23943. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23944. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23945. '']));
  23946. end;
  23947. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23948. begin
  23949. StartProgram(false);
  23950. Add([
  23951. 'type',
  23952. ' TObject = class',
  23953. ' class function GetSpeed(Index: boolean): word;',
  23954. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23955. ' end;',
  23956. ' TObjHelper = class helper for TObject',
  23957. ' class function GetSize(Index: boolean): word;',
  23958. ' class procedure SetSize(Index: boolean; Value: word);',
  23959. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23960. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23961. ' end;',
  23962. ' TBird = class',
  23963. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23964. ' class procedure DoIt;',
  23965. ' end;',
  23966. ' TBirdClass = class of TBird;',
  23967. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23968. 'begin',
  23969. ' Size[true]:=Size[false]+11;',
  23970. ' Speed[true]:=Speed[false]+12;',
  23971. ' Self.Size[true]:=Self.Size[false]+21;',
  23972. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23973. ' with Self do begin',
  23974. ' Size[true]:=Size[false]+31;',
  23975. ' Speed[true]:=Speed[false]+32;',
  23976. ' end;',
  23977. 'end;',
  23978. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23979. 'begin',
  23980. ' Size[true]:=Size[false]+11;',
  23981. ' Speed[true]:=Speed[false]+12;',
  23982. ' Self.Size[true]:=Self.Size[false]+21;',
  23983. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23984. ' with Self do begin',
  23985. ' Size[true]:=Size[false]+31;',
  23986. ' Speed[true]:=Speed[false]+32;',
  23987. ' end;',
  23988. 'end;',
  23989. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23990. 'begin',
  23991. 'end;',
  23992. 'class procedure TBird.DoIt;',
  23993. 'begin',
  23994. ' Items[true]:=Items[false]+11;',
  23995. ' Self.Items[true]:=Self.Items[false]+21;',
  23996. ' with Self do Items[true]:=Items[false]+31;',
  23997. 'end;',
  23998. 'var',
  23999. ' b: TBird;',
  24000. ' c: TBirdClass;',
  24001. 'begin',
  24002. ' b.Size[true]:=b.Size[false]+11;',
  24003. ' b.Speed[true]:=b.Speed[false]+12;',
  24004. ' b.Items[true]:=b.Items[false]+13;',
  24005. ' with b do begin',
  24006. ' Size[true]:=Size[false]+21;',
  24007. ' Speed[true]:=Speed[false]+22;',
  24008. ' Items[true]:=Items[false]+23;',
  24009. ' end;',
  24010. ' c.Size[true]:=c.Size[false]+11;',
  24011. ' c.Speed[true]:=c.Speed[false]+12;',
  24012. ' c.Items[true]:=c.Items[false]+13;',
  24013. ' with c do begin',
  24014. ' Size[true]:=Size[false]+21;',
  24015. ' Speed[true]:=Speed[false]+22;',
  24016. ' Items[true]:=Items[false]+23;',
  24017. ' end;',
  24018. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24019. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24020. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24021. ' with TBird do begin',
  24022. ' Size[true]:=Size[false]+21;',
  24023. ' Speed[true]:=Speed[false]+22;',
  24024. ' Items[true]:=Items[false]+23;',
  24025. ' end;',
  24026. '']);
  24027. ConvertProgram;
  24028. CheckSource('TestClassHelper_ClassProperty_Array',
  24029. LinesToStr([ // statements
  24030. 'rtl.createClass(this, "TObject", null, function () {',
  24031. ' this.$init = function () {',
  24032. ' };',
  24033. ' this.$final = function () {',
  24034. ' };',
  24035. ' this.GetSpeed = function (Index) {',
  24036. ' var Result = 0;',
  24037. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24038. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24039. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24040. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24041. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24042. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24043. ' return Result;',
  24044. ' };',
  24045. '});',
  24046. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24047. ' this.GetSize = function (Index) {',
  24048. ' var Result = 0;',
  24049. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24050. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24051. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24052. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24053. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24054. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24055. ' return Result;',
  24056. ' };',
  24057. ' this.SetSize = function (Index, Value) {',
  24058. ' };',
  24059. '});',
  24060. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24061. ' this.DoIt = function () {',
  24062. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24063. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24064. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24065. ' };',
  24066. '});',
  24067. 'this.b = null;',
  24068. 'this.c = null;',
  24069. '']),
  24070. LinesToStr([ // $mod.$main
  24071. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24072. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24073. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24074. 'var $with = $mod.b;',
  24075. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24076. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24077. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24078. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24079. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24080. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24081. 'var $with1 = $mod.c;',
  24082. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24083. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24084. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24085. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24086. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24087. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24088. 'var $with2 = $mod.TBird;',
  24089. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24090. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24091. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24092. '']));
  24093. end;
  24094. procedure TTestModule.TestClassHelper_ForIn;
  24095. begin
  24096. StartProgram(false);
  24097. Add([
  24098. 'type',
  24099. ' TObject = class end;',
  24100. ' TItem = TObject;',
  24101. ' TEnumerator = class',
  24102. ' FCurrent: TItem;',
  24103. ' property Current: TItem read FCurrent;',
  24104. ' function MoveNext: boolean;',
  24105. ' end;',
  24106. ' TBird = class',
  24107. ' end;',
  24108. ' TBirdHelper = class helper for TBird',
  24109. ' function GetEnumerator: TEnumerator;',
  24110. ' end;',
  24111. 'function TEnumerator.MoveNext: boolean;',
  24112. 'begin',
  24113. 'end;',
  24114. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24115. 'begin',
  24116. 'end;',
  24117. 'var',
  24118. ' b: TBird;',
  24119. ' i, i2: TItem;',
  24120. 'begin',
  24121. ' for i in b do i2:=i;']);
  24122. ConvertProgram;
  24123. CheckSource('TestClassHelper_ForIn',
  24124. LinesToStr([ // statements
  24125. 'rtl.createClass(this, "TObject", null, function () {',
  24126. ' this.$init = function () {',
  24127. ' };',
  24128. ' this.$final = function () {',
  24129. ' };',
  24130. '});',
  24131. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24132. ' this.$init = function () {',
  24133. ' $mod.TObject.$init.call(this);',
  24134. ' this.FCurrent = null;',
  24135. ' };',
  24136. ' this.$final = function () {',
  24137. ' this.FCurrent = undefined;',
  24138. ' $mod.TObject.$final.call(this);',
  24139. ' };',
  24140. ' this.MoveNext = function () {',
  24141. ' var Result = false;',
  24142. ' return Result;',
  24143. ' };',
  24144. '});',
  24145. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24146. '});',
  24147. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24148. ' this.GetEnumerator = function () {',
  24149. ' var Result = null;',
  24150. ' return Result;',
  24151. ' };',
  24152. '});',
  24153. 'this.b = null;',
  24154. 'this.i = null;',
  24155. 'this.i2 = null;'
  24156. ]),
  24157. LinesToStr([ // $mod.$main
  24158. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24159. 'try {',
  24160. ' while ($in.MoveNext()){',
  24161. ' $mod.i = $in.FCurrent;',
  24162. ' $mod.i2 = $mod.i;',
  24163. ' }',
  24164. '} finally {',
  24165. ' $in = rtl.freeLoc($in)',
  24166. '};',
  24167. '']));
  24168. end;
  24169. procedure TTestModule.TestClassHelper_PassProperty;
  24170. begin
  24171. StartProgram(false);
  24172. Add([
  24173. 'type',
  24174. ' TObject = class',
  24175. ' FField: TObject;',
  24176. ' property Field: TObject read FField write FField;',
  24177. ' end;',
  24178. ' THelper = class helper for TObject',
  24179. ' procedure Fly;',
  24180. ' class procedure Run;',
  24181. ' class procedure Jump; static;',
  24182. ' end;',
  24183. 'procedure THelper.Fly;',
  24184. 'begin',
  24185. ' Field.Fly;',
  24186. ' Field.Run;',
  24187. ' Field.Jump;',
  24188. ' with Field do begin',
  24189. ' Fly;',
  24190. ' Run;',
  24191. ' Jump;',
  24192. ' end;',
  24193. 'end;',
  24194. 'class procedure THelper.Run;',
  24195. 'begin',
  24196. 'end;',
  24197. 'class procedure THelper.Jump;',
  24198. 'begin',
  24199. 'end;',
  24200. 'var',
  24201. ' b: TObject;',
  24202. 'begin',
  24203. ' b.Field.Fly;',
  24204. ' b.Field.Run;',
  24205. ' b.Field.Jump;',
  24206. ' with b do begin',
  24207. ' Field.Run;',
  24208. ' Field.Fly;',
  24209. ' Field.Jump;',
  24210. ' end;',
  24211. ' with b.Field do begin',
  24212. ' Run;',
  24213. ' Fly;',
  24214. ' Jump;',
  24215. ' end;',
  24216. '']);
  24217. ConvertProgram;
  24218. CheckSource('TestClassHelper_PassProperty',
  24219. LinesToStr([ // statements
  24220. 'rtl.createClass(this, "TObject", null, function () {',
  24221. ' this.$init = function () {',
  24222. ' this.FField = null;',
  24223. ' };',
  24224. ' this.$final = function () {',
  24225. ' this.FField = undefined;',
  24226. ' };',
  24227. '});',
  24228. 'rtl.createHelper(this, "THelper", null, function () {',
  24229. ' this.Fly = function () {',
  24230. ' $mod.THelper.Fly.call(this.FField);',
  24231. ' $mod.THelper.Run.call(this.FField.$class);',
  24232. ' $mod.THelper.Jump();',
  24233. ' var $with = this.FField;',
  24234. ' $mod.THelper.Fly.call($with);',
  24235. ' $mod.THelper.Run.call($with.$class);',
  24236. ' $mod.THelper.Jump();',
  24237. ' };',
  24238. ' this.Run = function () {',
  24239. ' };',
  24240. ' this.Jump = function () {',
  24241. ' };',
  24242. '});',
  24243. 'this.b = null;',
  24244. '']),
  24245. LinesToStr([ // $mod.$main
  24246. '$mod.THelper.Fly.call($mod.b.FField);',
  24247. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24248. '$mod.THelper.Jump();',
  24249. 'var $with = $mod.b;',
  24250. '$mod.THelper.Run.call($with.FField.$class);',
  24251. '$mod.THelper.Fly.call($with.FField);',
  24252. '$mod.THelper.Jump();',
  24253. 'var $with1 = $mod.b.FField;',
  24254. '$mod.THelper.Run.call($with1.$class);',
  24255. '$mod.THelper.Fly.call($with1);',
  24256. '$mod.THelper.Jump();',
  24257. '']));
  24258. end;
  24259. procedure TTestModule.TestExtClassHelper_ClassVar;
  24260. begin
  24261. StartProgram(false);
  24262. Add([
  24263. '{$modeswitch externalclass}',
  24264. 'type',
  24265. ' TExtA = class external name ''ExtObj''',
  24266. ' end;',
  24267. ' THelper = class helper for TExtA',
  24268. ' const',
  24269. ' One = 1;',
  24270. ' Two: word = 2;',
  24271. ' class var',
  24272. ' Glob: word;',
  24273. ' function Foo(w: word): word;',
  24274. ' class function Bar(w: word): word; static;',
  24275. ' end;',
  24276. 'function THelper.foo(w: word): word;',
  24277. 'begin',
  24278. ' Result:=w;',
  24279. ' Two:=One+w;',
  24280. ' Glob:=Glob;',
  24281. ' Result:=Self.Glob;',
  24282. ' Self.Glob:=Self.Glob;',
  24283. ' with Self do Glob:=Glob;',
  24284. 'end;',
  24285. 'class function THelper.bar(w: word): word;',
  24286. 'begin',
  24287. ' Result:=w;',
  24288. ' Two:=One;',
  24289. ' Glob:=Glob;',
  24290. 'end;',
  24291. 'var o: TExtA;',
  24292. 'begin',
  24293. ' texta.two:=texta.one;',
  24294. ' texta.Glob:=texta.Glob;',
  24295. ' with texta do begin',
  24296. ' two:=one;',
  24297. ' Glob:=Glob;',
  24298. ' end;',
  24299. ' o.two:=o.one;',
  24300. ' o.Glob:=o.Glob;',
  24301. ' with o do begin',
  24302. ' two:=one;',
  24303. ' Glob:=Glob;',
  24304. ' end;',
  24305. '']);
  24306. ConvertProgram;
  24307. CheckSource('TestExtClassHelper_ClassVar',
  24308. LinesToStr([ // statements
  24309. 'rtl.createHelper(this, "THelper", null, function () {',
  24310. ' this.One = 1;',
  24311. ' this.Two = 2;',
  24312. ' this.Glob = 0;',
  24313. ' this.Foo = function (w) {',
  24314. ' var Result = 0;',
  24315. ' Result = w;',
  24316. ' $mod.THelper.Two = 1 + w;',
  24317. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24318. ' Result = $mod.THelper.Glob;',
  24319. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24320. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24321. ' return Result;',
  24322. ' };',
  24323. ' this.Bar = function (w) {',
  24324. ' var Result = 0;',
  24325. ' Result = w;',
  24326. ' $mod.THelper.Two = 1;',
  24327. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24328. ' return Result;',
  24329. ' };',
  24330. '});',
  24331. 'this.o = null;',
  24332. '']),
  24333. LinesToStr([ // $mod.$main
  24334. '$mod.THelper.Two = 1;',
  24335. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24336. '$mod.THelper.Two = 1;',
  24337. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24338. '$mod.THelper.Two = 1;',
  24339. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24340. 'var $with = $mod.o;',
  24341. '$mod.THelper.Two = 1;',
  24342. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24343. '']));
  24344. end;
  24345. procedure TTestModule.TestExtClassHelper_Method_Call;
  24346. begin
  24347. StartProgram(false);
  24348. Add([
  24349. '{$modeswitch externalclass}',
  24350. 'type',
  24351. ' TFly = function(w: word): word of object;',
  24352. ' TExtA = class external name ''ExtObj''',
  24353. ' procedure Run(w: word = 10);',
  24354. ' end;',
  24355. ' THelper = class helper for TExtA',
  24356. ' function Foo(w: word = 1): word;',
  24357. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24358. ' end;',
  24359. 'var p: TFly;',
  24360. 'function THelper.foo(w: word): word;',
  24361. 'begin',
  24362. ' Run;',
  24363. ' Run();',
  24364. ' Run(11);',
  24365. ' Foo;',
  24366. ' Foo();',
  24367. ' Foo(12);',
  24368. ' Self.Foo;',
  24369. ' Self.Foo();',
  24370. ' Self.Foo(13);',
  24371. ' Fly;',
  24372. ' Fly();',
  24373. ' with Self do begin',
  24374. ' Foo;',
  24375. ' Foo();',
  24376. ' Foo(14);',
  24377. ' Fly;',
  24378. ' Fly();',
  24379. ' end;',
  24380. ' p:=@Fly;',
  24381. 'end;',
  24382. 'var Obj: TExtA;',
  24383. 'begin',
  24384. ' obj.Foo;',
  24385. ' obj.Foo();',
  24386. ' obj.Foo(21);',
  24387. ' obj.Fly;',
  24388. ' obj.Fly();',
  24389. ' with obj do begin',
  24390. ' Foo;',
  24391. ' Foo();',
  24392. ' Foo(22);',
  24393. ' Fly;',
  24394. ' Fly();',
  24395. ' end;',
  24396. ' p:[email protected];',
  24397. '']);
  24398. ConvertProgram;
  24399. CheckSource('TestExtClassHelper_Method_Call',
  24400. LinesToStr([ // statements
  24401. 'rtl.createHelper(this, "THelper", null, function () {',
  24402. ' this.Foo = function (w) {',
  24403. ' var Result = 0;',
  24404. ' this.Run(10);',
  24405. ' this.Run(10);',
  24406. ' this.Run(11);',
  24407. ' $mod.THelper.Foo.call(this, 1);',
  24408. ' $mod.THelper.Foo.call(this, 1);',
  24409. ' $mod.THelper.Foo.call(this, 12);',
  24410. ' $mod.THelper.Foo.call(this, 1);',
  24411. ' $mod.THelper.Foo.call(this, 1);',
  24412. ' $mod.THelper.Foo.call(this, 13);',
  24413. ' this.Fly(2);',
  24414. ' this.Fly(2);',
  24415. ' $mod.THelper.Foo.call(this, 1);',
  24416. ' $mod.THelper.Foo.call(this, 1);',
  24417. ' $mod.THelper.Foo.call(this, 14);',
  24418. ' this.Fly(2);',
  24419. ' this.Fly(2);',
  24420. ' $mod.p = rtl.createCallback(this, "Fly");',
  24421. ' return Result;',
  24422. ' };',
  24423. '});',
  24424. 'this.p = null;',
  24425. 'this.Obj = null;',
  24426. '']),
  24427. LinesToStr([ // $mod.$main
  24428. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24429. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24430. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24431. '$mod.Obj.Fly(2);',
  24432. '$mod.Obj.Fly(2);',
  24433. 'var $with = $mod.Obj;',
  24434. '$mod.THelper.Foo.call($with, 1);',
  24435. '$mod.THelper.Foo.call($with, 1);',
  24436. '$mod.THelper.Foo.call($with, 22);',
  24437. '$with.Fly(2);',
  24438. '$with.Fly(2);',
  24439. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24440. '']));
  24441. end;
  24442. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24443. begin
  24444. StartProgram(false);
  24445. Add([
  24446. '{$modeswitch externalclass}',
  24447. 'type',
  24448. ' TExtA = class external name ''ExtObj''',
  24449. ' procedure Run(w: word = 10);',
  24450. ' end;',
  24451. ' THelper = class helper for TExtA',
  24452. ' class procedure Fly;',
  24453. ' end;',
  24454. 'class procedure THelper.Fly;',
  24455. 'begin end;',
  24456. 'begin',
  24457. '']);
  24458. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24459. nHelperClassMethodForExtClassMustBeStatic);
  24460. ConvertProgram;
  24461. end;
  24462. procedure TTestModule.TestRecordHelper_ClassVar;
  24463. begin
  24464. StartProgram(false);
  24465. Add([
  24466. 'type',
  24467. ' TRec = record',
  24468. ' end;',
  24469. ' THelper = record helper for TRec',
  24470. ' const',
  24471. ' One = 1;',
  24472. ' Two: word = 2;',
  24473. ' class var',
  24474. ' Glob: word;',
  24475. ' function Foo(w: word): word;',
  24476. ' class function Bar(w: word): word; static;',
  24477. ' end;',
  24478. 'function THelper.foo(w: word): word;',
  24479. 'begin',
  24480. ' Result:=w;',
  24481. ' Two:=One+w;',
  24482. ' Glob:=Glob;',
  24483. ' Result:=Self.Glob;',
  24484. ' Self.Glob:=Self.Glob;',
  24485. ' with Self do Glob:=Glob;',
  24486. ' Self:=Self;',
  24487. 'end;',
  24488. 'class function THelper.bar(w: word): word;',
  24489. 'begin',
  24490. ' Result:=w;',
  24491. ' Two:=One;',
  24492. ' Glob:=Glob;',
  24493. 'end;',
  24494. 'var r: TRec;',
  24495. 'begin',
  24496. ' trec.two:=trec.one;',
  24497. ' trec.Glob:=trec.Glob;',
  24498. ' with trec do begin',
  24499. ' two:=one;',
  24500. ' Glob:=Glob;',
  24501. ' end;',
  24502. ' r.two:=r.one;',
  24503. ' r.Glob:=r.Glob;',
  24504. ' with r do begin',
  24505. ' two:=one;',
  24506. ' Glob:=Glob;',
  24507. ' end;',
  24508. '']);
  24509. ConvertProgram;
  24510. CheckSource('TestRecordHelper_ClassVar',
  24511. LinesToStr([ // statements
  24512. 'rtl.recNewT(this, "TRec", function () {',
  24513. ' this.$eq = function (b) {',
  24514. ' return true;',
  24515. ' };',
  24516. ' this.$assign = function (s) {',
  24517. ' return this;',
  24518. ' };',
  24519. '});',
  24520. 'rtl.createHelper(this, "THelper", null, function () {',
  24521. ' this.One = 1;',
  24522. ' this.Two = 2;',
  24523. ' this.Glob = 0;',
  24524. ' this.Foo = function (w) {',
  24525. ' var Result = 0;',
  24526. ' Result = w;',
  24527. ' $mod.THelper.Two = 1 + w;',
  24528. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24529. ' Result = $mod.THelper.Glob;',
  24530. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24531. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24532. ' this.$assign(this);',
  24533. ' return Result;',
  24534. ' };',
  24535. ' this.Bar = function (w) {',
  24536. ' var Result = 0;',
  24537. ' Result = w;',
  24538. ' $mod.THelper.Two = 1;',
  24539. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24540. ' return Result;',
  24541. ' };',
  24542. '});',
  24543. 'this.r = this.TRec.$new();',
  24544. '']),
  24545. LinesToStr([ // $mod.$main
  24546. '$mod.THelper.Two = 1;',
  24547. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24548. 'var $with = $mod.TRec;',
  24549. '$mod.THelper.Two = 1;',
  24550. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24551. '$mod.THelper.Two = 1;',
  24552. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24553. 'var $with1 = $mod.r;',
  24554. '$mod.THelper.Two = 1;',
  24555. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24556. '']));
  24557. end;
  24558. procedure TTestModule.TestRecordHelper_Method_Call;
  24559. begin
  24560. StartProgram(false);
  24561. Add([
  24562. '{$modeswitch AdvancedRecords}',
  24563. 'type',
  24564. ' TRec = record',
  24565. ' procedure Run(w: word = 10);',
  24566. ' end;',
  24567. ' THelper = record helper for TRec',
  24568. ' function Foo(w: word = 1): word;',
  24569. ' end;',
  24570. 'procedure TRec.Run(w: word);',
  24571. 'begin',
  24572. ' Foo;',
  24573. ' Foo();',
  24574. ' Foo(2);',
  24575. ' Self.Foo;',
  24576. ' Self.Foo();',
  24577. ' Self.Foo(3);',
  24578. ' with Self do begin',
  24579. ' Foo;',
  24580. ' Foo();',
  24581. ' Foo(4);',
  24582. ' end;',
  24583. 'end;',
  24584. 'function THelper.foo(w: word): word;',
  24585. 'begin',
  24586. ' Run;',
  24587. ' Run();',
  24588. ' Run(11);',
  24589. ' Foo;',
  24590. ' Foo();',
  24591. ' Foo(12);',
  24592. ' Self.Foo;',
  24593. ' Self.Foo();',
  24594. ' Self.Foo(13);',
  24595. ' with Self do begin',
  24596. ' Foo;',
  24597. ' Foo();',
  24598. ' Foo(14);',
  24599. ' end;',
  24600. 'end;',
  24601. 'var Rec: TRec;',
  24602. 'begin',
  24603. ' Rec.Foo;',
  24604. ' Rec.Foo();',
  24605. ' Rec.Foo(21);',
  24606. ' with Rec do begin',
  24607. ' Foo;',
  24608. ' Foo();',
  24609. ' Foo(22);',
  24610. ' end;',
  24611. '']);
  24612. ConvertProgram;
  24613. CheckSource('TestRecordHelper_Method_Call',
  24614. LinesToStr([ // statements
  24615. 'rtl.recNewT(this, "TRec", function () {',
  24616. ' this.$eq = function (b) {',
  24617. ' return true;',
  24618. ' };',
  24619. ' this.$assign = function (s) {',
  24620. ' return this;',
  24621. ' };',
  24622. ' this.Run = function (w) {',
  24623. ' $mod.THelper.Foo.call(this, 1);',
  24624. ' $mod.THelper.Foo.call(this, 1);',
  24625. ' $mod.THelper.Foo.call(this, 2);',
  24626. ' $mod.THelper.Foo.call(this, 1);',
  24627. ' $mod.THelper.Foo.call(this, 1);',
  24628. ' $mod.THelper.Foo.call(this, 3);',
  24629. ' $mod.THelper.Foo.call(this, 1);',
  24630. ' $mod.THelper.Foo.call(this, 1);',
  24631. ' $mod.THelper.Foo.call(this, 4);',
  24632. ' };',
  24633. '});',
  24634. 'rtl.createHelper(this, "THelper", null, function () {',
  24635. ' this.Foo = function (w) {',
  24636. ' var Result = 0;',
  24637. ' this.Run(10);',
  24638. ' this.Run(10);',
  24639. ' this.Run(11);',
  24640. ' $mod.THelper.Foo.call(this, 1);',
  24641. ' $mod.THelper.Foo.call(this, 1);',
  24642. ' $mod.THelper.Foo.call(this, 12);',
  24643. ' $mod.THelper.Foo.call(this, 1);',
  24644. ' $mod.THelper.Foo.call(this, 1);',
  24645. ' $mod.THelper.Foo.call(this, 13);',
  24646. ' $mod.THelper.Foo.call(this, 1);',
  24647. ' $mod.THelper.Foo.call(this, 1);',
  24648. ' $mod.THelper.Foo.call(this, 14);',
  24649. ' return Result;',
  24650. ' };',
  24651. '});',
  24652. 'this.Rec = this.TRec.$new();',
  24653. '']),
  24654. LinesToStr([ // $mod.$main
  24655. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24656. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24657. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24658. 'var $with = $mod.Rec;',
  24659. '$mod.THelper.Foo.call($with, 1);',
  24660. '$mod.THelper.Foo.call($with, 1);',
  24661. '$mod.THelper.Foo.call($with, 22);',
  24662. '']));
  24663. end;
  24664. procedure TTestModule.TestRecordHelper_Constructor;
  24665. begin
  24666. StartProgram(false);
  24667. Add([
  24668. '{$modeswitch AdvancedRecords}',
  24669. 'type',
  24670. ' TRec = record',
  24671. ' constructor Create(w: word);',
  24672. ' end;',
  24673. ' THelper = record helper for TRec',
  24674. ' constructor NewHlp(w: word);',
  24675. ' end;',
  24676. 'var',
  24677. ' Rec: TRec;',
  24678. 'constructor TRec.Create(w: word);',
  24679. 'begin',
  24680. ' NewHlp(2);', // normal call
  24681. ' trec.NewHlp(3);', // new instance
  24682. 'end;',
  24683. 'constructor THelper.NewHlp(w: word);',
  24684. 'begin',
  24685. ' create(2);', // normal call
  24686. ' trec.create(3);', // new instance
  24687. ' NewHlp(4);', // normal call
  24688. ' trec.NewHlp(5);', // new instance
  24689. 'end;',
  24690. 'begin',
  24691. ' rec.newhlp(2);', // normal call
  24692. ' with rec do newhlp(12);', // normal call
  24693. ' trec.newhlp(3);', // new instance
  24694. ' with trec do newhlp(13);', // new instance
  24695. '']);
  24696. ConvertProgram;
  24697. CheckSource('TestRecordHelper_Constructor',
  24698. LinesToStr([ // statements
  24699. 'rtl.recNewT(this, "TRec", function () {',
  24700. ' this.$eq = function (b) {',
  24701. ' return true;',
  24702. ' };',
  24703. ' this.$assign = function (s) {',
  24704. ' return this;',
  24705. ' };',
  24706. ' this.Create = function (w) {',
  24707. ' $mod.THelper.NewHlp.call(this, 2);',
  24708. ' $mod.THelper.$new("NewHlp", [3]);',
  24709. ' return this;',
  24710. ' };',
  24711. '});',
  24712. 'rtl.createHelper(this, "THelper", null, function () {',
  24713. ' this.NewHlp = function (w) {',
  24714. ' this.Create(2);',
  24715. ' $mod.TRec.$new().Create(3);',
  24716. ' $mod.THelper.NewHlp.call(this, 4);',
  24717. ' $mod.THelper.$new("NewHlp", [5]);',
  24718. ' return this;',
  24719. ' };',
  24720. ' this.$new = function (fn, args) {',
  24721. ' return this[fn].apply($mod.TRec.$new(), args);',
  24722. ' };',
  24723. '});',
  24724. 'this.Rec = this.TRec.$new();',
  24725. '']),
  24726. LinesToStr([ // $mod.$main
  24727. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24728. 'var $with = $mod.Rec;',
  24729. '$mod.THelper.NewHlp.call($with, 12);',
  24730. '$mod.THelper.$new("NewHlp", [3]);',
  24731. 'var $with1 = $mod.TRec;',
  24732. '$mod.THelper.$new("NewHlp", [13]);',
  24733. '']));
  24734. end;
  24735. procedure TTestModule.TestTypeHelper_ClassVar;
  24736. begin
  24737. StartProgram(false);
  24738. Add([
  24739. '{$modeswitch typehelpers}',
  24740. 'type',
  24741. ' THelper = type helper for byte',
  24742. ' const',
  24743. ' One = 1;',
  24744. ' Two: word = 2;',
  24745. ' class var',
  24746. ' Glob: word;',
  24747. ' function Foo(w: word): word;',
  24748. ' class function Bar(w: word): word; static;',
  24749. ' end;',
  24750. 'function THelper.foo(w: word): word;',
  24751. 'begin',
  24752. ' Result:=w;',
  24753. ' Two:=One+w;',
  24754. ' Glob:=Glob;',
  24755. ' Result:=Self.Glob;',
  24756. ' Self.Glob:=Self.Glob;',
  24757. ' with Self do Glob:=Glob;',
  24758. 'end;',
  24759. 'class function THelper.bar(w: word): word;',
  24760. 'begin',
  24761. ' Result:=w;',
  24762. ' Two:=One;',
  24763. ' Glob:=Glob;',
  24764. 'end;',
  24765. 'var b: byte;',
  24766. 'begin',
  24767. ' byte.two:=byte.one;',
  24768. ' byte.Glob:=byte.Glob;',
  24769. ' with byte do begin',
  24770. ' two:=one;',
  24771. ' Glob:=Glob;',
  24772. ' end;',
  24773. ' b.two:=b.one;',
  24774. ' b.Glob:=b.Glob;',
  24775. ' with b do begin',
  24776. ' two:=one;',
  24777. ' Glob:=Glob;',
  24778. ' end;',
  24779. '']);
  24780. ConvertProgram;
  24781. CheckSource('TestTypeHelper_ClassVar',
  24782. LinesToStr([ // statements
  24783. 'rtl.createHelper(this, "THelper", null, function () {',
  24784. ' this.One = 1;',
  24785. ' this.Two = 2;',
  24786. ' this.Glob = 0;',
  24787. ' this.Foo = function (w) {',
  24788. ' var Result = 0;',
  24789. ' Result = w;',
  24790. ' $mod.THelper.Two = 1 + w;',
  24791. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24792. ' Result = $mod.THelper.Glob;',
  24793. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24794. ' var $with = this.get();',
  24795. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24796. ' return Result;',
  24797. ' };',
  24798. ' this.Bar = function (w) {',
  24799. ' var Result = 0;',
  24800. ' Result = w;',
  24801. ' $mod.THelper.Two = 1;',
  24802. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24803. ' return Result;',
  24804. ' };',
  24805. '});',
  24806. 'this.b = 0;',
  24807. '']),
  24808. LinesToStr([ // $mod.$main
  24809. '$mod.THelper.Two = 1;',
  24810. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24811. '$mod.THelper.Two = 1;',
  24812. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24813. '$mod.THelper.Two = 1;',
  24814. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24815. 'var $with = $mod.b;',
  24816. '$mod.THelper.Two = 1;',
  24817. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24818. '']));
  24819. end;
  24820. procedure TTestModule.TestTypeHelper_PassResultElement;
  24821. begin
  24822. StartProgram(false);
  24823. Add([
  24824. '{$modeswitch typehelpers}',
  24825. 'type',
  24826. ' THelper = type helper for word',
  24827. ' procedure DoIt(e: byte = 123);',
  24828. ' class procedure DoSome(e: byte = 456); static;',
  24829. ' end;',
  24830. 'procedure THelper.DoIt(e: byte);',
  24831. 'begin',
  24832. 'end;',
  24833. 'class procedure THelper.DoSome(e: byte);',
  24834. 'begin',
  24835. 'end;',
  24836. 'function Foo(w: word): word;',
  24837. 'begin',
  24838. ' Result.DoIt;',
  24839. ' Result.DoIt();',
  24840. ' Result.DoSome;',
  24841. ' Result.DoSome();',
  24842. ' with Result do begin',
  24843. ' DoIt;',
  24844. ' DoIt();',
  24845. ' DoSome;',
  24846. ' DoSome();',
  24847. ' end;',
  24848. 'end;',
  24849. 'begin',
  24850. '']);
  24851. ConvertProgram;
  24852. CheckSource('TestTypeHelper_PassResultElement',
  24853. LinesToStr([ // statements
  24854. 'rtl.createHelper(this, "THelper", null, function () {',
  24855. ' this.DoIt = function (e) {',
  24856. ' };',
  24857. ' this.DoSome = function (e) {',
  24858. ' };',
  24859. '});',
  24860. 'this.Foo = function (w) {',
  24861. ' var Result = 0;',
  24862. ' $mod.THelper.DoIt.call({',
  24863. ' get: function () {',
  24864. ' return Result;',
  24865. ' },',
  24866. ' set: function (v) {',
  24867. ' Result = v;',
  24868. ' }',
  24869. ' }, 123);',
  24870. ' $mod.THelper.DoIt.call({',
  24871. ' get: function () {',
  24872. ' return Result;',
  24873. ' },',
  24874. ' set: function (v) {',
  24875. ' Result = v;',
  24876. ' }',
  24877. ' }, 123);',
  24878. ' $mod.THelper.DoSome(456);',
  24879. ' $mod.THelper.DoSome(456);',
  24880. ' $mod.THelper.DoIt.call({',
  24881. ' get: function () {',
  24882. ' return Result;',
  24883. ' },',
  24884. ' set: function (v) {',
  24885. ' Result = v;',
  24886. ' }',
  24887. ' }, 123);',
  24888. ' $mod.THelper.DoIt.call({',
  24889. ' get: function () {',
  24890. ' return Result;',
  24891. ' },',
  24892. ' set: function (v) {',
  24893. ' Result = v;',
  24894. ' }',
  24895. ' }, 123);',
  24896. ' $mod.THelper.DoSome(456);',
  24897. ' $mod.THelper.DoSome(456);',
  24898. ' return Result;',
  24899. '};',
  24900. '']),
  24901. LinesToStr([ // $mod.$main
  24902. '']));
  24903. end;
  24904. procedure TTestModule.TestTypeHelper_PassArgs;
  24905. begin
  24906. StartProgram(false);
  24907. Add([
  24908. '{$modeswitch typehelpers}',
  24909. 'type',
  24910. ' THelper = type helper for word',
  24911. ' procedure DoIt(e: byte = 123);',
  24912. ' end;',
  24913. 'procedure THelper.DoIt(e: byte);',
  24914. 'begin',
  24915. 'end;',
  24916. 'procedure FooDefault(a: word);',
  24917. 'begin',
  24918. ' a.DoIt;',
  24919. ' with a do DoIt;',
  24920. 'end;',
  24921. 'procedure FooConst(const a: word);',
  24922. 'begin',
  24923. ' a.DoIt;',
  24924. ' with a do DoIt;',
  24925. 'end;',
  24926. 'procedure FooVar(var a: word);',
  24927. 'begin',
  24928. ' a.DoIt;',
  24929. ' with a do DoIt;',
  24930. 'end;',
  24931. 'begin',
  24932. '']);
  24933. ConvertProgram;
  24934. CheckSource('TestTypeHelper_PassArgs',
  24935. LinesToStr([ // statements
  24936. 'rtl.createHelper(this, "THelper", null, function () {',
  24937. ' this.DoIt = function (e) {',
  24938. ' };',
  24939. '});',
  24940. 'this.FooDefault = function (a) {',
  24941. ' $mod.THelper.DoIt.call({',
  24942. ' get: function () {',
  24943. ' return a;',
  24944. ' },',
  24945. ' set: function (v) {',
  24946. ' a = v;',
  24947. ' }',
  24948. ' }, 123);',
  24949. ' $mod.THelper.DoIt.call({',
  24950. ' get: function () {',
  24951. ' return a;',
  24952. ' },',
  24953. ' set: function (v) {',
  24954. ' a = v;',
  24955. ' }',
  24956. ' }, 123);',
  24957. '};',
  24958. 'this.FooConst = function (a) {',
  24959. ' $mod.THelper.DoIt.call({',
  24960. ' get: function () {',
  24961. ' return a;',
  24962. ' },',
  24963. ' set: function (v) {',
  24964. ' rtl.raiseE("EPropReadOnly");',
  24965. ' }',
  24966. ' }, 123);',
  24967. ' $mod.THelper.DoIt.call({',
  24968. ' get: function () {',
  24969. ' return a;',
  24970. ' },',
  24971. ' set: function () {',
  24972. ' rtl.raiseE("EPropReadOnly");',
  24973. ' }',
  24974. ' }, 123);',
  24975. '};',
  24976. 'this.FooVar = function (a) {',
  24977. ' $mod.THelper.DoIt.call(a, 123);',
  24978. ' var $with = a.get();',
  24979. ' $mod.THelper.DoIt.call(a, 123);',
  24980. '};',
  24981. '']),
  24982. LinesToStr([ // $mod.$main
  24983. '']));
  24984. end;
  24985. procedure TTestModule.TestTypeHelper_PassVarConst;
  24986. begin
  24987. StartProgram(false);
  24988. Add([
  24989. '{$modeswitch typehelpers}',
  24990. 'type',
  24991. ' THelper = type helper for word',
  24992. ' procedure DoIt(e: byte = 123);',
  24993. ' end;',
  24994. 'procedure THelper.DoIt(e: byte);',
  24995. 'begin',
  24996. 'end;',
  24997. 'var a: word;',
  24998. 'const c: word = 2;',
  24999. '{$writeableconst off}',
  25000. 'const r: word = 3;',
  25001. 'begin',
  25002. ' a.DoIt;',
  25003. ' with a do DoIt;',
  25004. ' c.DoIt;',
  25005. ' with c do DoIt;',
  25006. ' r.DoIt;',
  25007. ' with r do DoIt;',
  25008. '']);
  25009. ConvertProgram;
  25010. CheckSource('TestTypeHelper_PassVarConst',
  25011. LinesToStr([ // statements
  25012. 'rtl.createHelper(this, "THelper", null, function () {',
  25013. ' this.DoIt = function (e) {',
  25014. ' };',
  25015. '});',
  25016. 'this.a = 0;',
  25017. 'this.c = 2;',
  25018. 'this.r = 3;',
  25019. '']),
  25020. LinesToStr([ // $mod.$main
  25021. '$mod.THelper.DoIt.call({',
  25022. ' p: $mod,',
  25023. ' get: function () {',
  25024. ' return this.p.a;',
  25025. ' },',
  25026. ' set: function (v) {',
  25027. ' this.p.a = v;',
  25028. ' }',
  25029. '}, 123);',
  25030. 'var $with = $mod.a;',
  25031. '$mod.THelper.DoIt.call({',
  25032. ' get: function () {',
  25033. ' return $with;',
  25034. ' },',
  25035. ' set: function (v) {',
  25036. ' $with = v;',
  25037. ' }',
  25038. '}, 123);',
  25039. '$mod.THelper.DoIt.call({',
  25040. ' p: $mod,',
  25041. ' get: function () {',
  25042. ' return this.p.c;',
  25043. ' },',
  25044. ' set: function (v) {',
  25045. ' this.p.c = v;',
  25046. ' }',
  25047. '}, 123);',
  25048. 'var $with1 = $mod.c;',
  25049. '$mod.THelper.DoIt.call({',
  25050. ' get: function () {',
  25051. ' return $with1;',
  25052. ' },',
  25053. ' set: function (v) {',
  25054. ' $with1 = v;',
  25055. ' }',
  25056. '}, 123);',
  25057. '$mod.THelper.DoIt.call({',
  25058. ' get: function () {',
  25059. ' return 3;',
  25060. ' },',
  25061. ' set: function (v) {',
  25062. ' rtl.raiseE("EPropReadOnly");',
  25063. ' }',
  25064. '}, 123);',
  25065. 'var $with2 = 3;',
  25066. ' $mod.THelper.DoIt.call({',
  25067. ' get: function () {',
  25068. ' return $with2;',
  25069. ' },',
  25070. ' set: function () {',
  25071. ' rtl.raiseE("EPropReadOnly");',
  25072. ' }',
  25073. ' }, 123);',
  25074. '']));
  25075. end;
  25076. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25077. begin
  25078. StartProgram(false);
  25079. Add([
  25080. '{$modeswitch typehelpers}',
  25081. 'type',
  25082. ' THelper = type helper for word',
  25083. ' procedure DoIt(e: byte = 123);',
  25084. ' end;',
  25085. 'procedure THelper.DoIt(e: byte);',
  25086. 'begin',
  25087. 'end;',
  25088. 'function Foo(b: byte = 1): word;',
  25089. 'begin',
  25090. 'end;',
  25091. 'begin',
  25092. ' Foo.DoIt;',
  25093. ' Foo().DoIt;',
  25094. ' with Foo do DoIt;',
  25095. ' with Foo() do DoIt;',
  25096. '']);
  25097. ConvertProgram;
  25098. CheckSource('TestTypeHelper_PassFuncResult',
  25099. LinesToStr([ // statements
  25100. 'rtl.createHelper(this, "THelper", null, function () {',
  25101. ' this.DoIt = function (e) {',
  25102. ' };',
  25103. '});',
  25104. 'this.Foo = function (b) {',
  25105. ' var Result = 0;',
  25106. ' return Result;',
  25107. '};',
  25108. '']),
  25109. LinesToStr([ // $mod.$main
  25110. '$mod.THelper.DoIt.call({',
  25111. ' a: $mod.Foo(1),',
  25112. ' get: function () {',
  25113. ' return this.a;',
  25114. ' },',
  25115. ' set: function (v) {',
  25116. ' this.a = v;',
  25117. ' }',
  25118. '}, 123);',
  25119. '$mod.THelper.DoIt.call({',
  25120. ' a: $mod.Foo(1),',
  25121. ' get: function () {',
  25122. ' return this.a;',
  25123. ' },',
  25124. ' set: function (v) {',
  25125. ' this.a = v;',
  25126. ' }',
  25127. '}, 123);',
  25128. 'var $with = $mod.Foo(1);',
  25129. '$mod.THelper.DoIt.call({',
  25130. ' get: function () {',
  25131. ' return $with;',
  25132. ' },',
  25133. ' set: function (v) {',
  25134. ' $with = v;',
  25135. ' }',
  25136. '}, 123);',
  25137. 'var $with1 = $mod.Foo(1);',
  25138. '$mod.THelper.DoIt.call({',
  25139. ' get: function () {',
  25140. ' return $with1;',
  25141. ' },',
  25142. ' set: function (v) {',
  25143. ' $with1 = v;',
  25144. ' }',
  25145. '}, 123);',
  25146. '']));
  25147. end;
  25148. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25149. begin
  25150. StartProgram(false);
  25151. Add([
  25152. '{$modeswitch typehelpers}',
  25153. 'type',
  25154. ' TObject = class',
  25155. ' FField: word;',
  25156. ' procedure SetField(Value: word);',
  25157. ' property Field: word read FField write SetField;',
  25158. ' end;',
  25159. ' THelper = type helper for word',
  25160. ' procedure Fly;',
  25161. ' class procedure Run; static;',
  25162. ' end;',
  25163. 'procedure TObject.SetField(Value: word);',
  25164. 'begin',
  25165. ' Field.Fly;',
  25166. ' Field.Run;',
  25167. ' Self.Field.Fly;',
  25168. ' Self.Field.Run;',
  25169. ' with Self do begin',
  25170. ' Field.Fly;',
  25171. ' Field.Run;',
  25172. ' end;',
  25173. ' with Self.Field do begin',
  25174. ' Fly;',
  25175. ' Run;',
  25176. ' end;',
  25177. 'end;',
  25178. 'procedure THelper.Fly;',
  25179. 'begin',
  25180. 'end;',
  25181. 'class procedure THelper.Run;',
  25182. 'begin',
  25183. 'end;',
  25184. 'var',
  25185. ' o: TObject;',
  25186. 'begin',
  25187. ' o.Field.Fly;',
  25188. ' o.Field.Run;',
  25189. ' with o do begin',
  25190. ' Field.Fly;',
  25191. ' Field.Run;',
  25192. ' end;',
  25193. ' with o.Field do begin',
  25194. ' Fly;',
  25195. ' Run;',
  25196. ' end;',
  25197. '']);
  25198. ConvertProgram;
  25199. CheckSource('TestTypeHelper_PassPropertyField',
  25200. LinesToStr([ // statements
  25201. 'rtl.createClass(this, "TObject", null, function () {',
  25202. ' this.$init = function () {',
  25203. ' this.FField = 0;',
  25204. ' };',
  25205. ' this.$final = function () {',
  25206. ' };',
  25207. ' this.SetField = function (Value) {',
  25208. ' $mod.THelper.Fly.call({',
  25209. ' p: this,',
  25210. ' get: function () {',
  25211. ' return this.p.FField;',
  25212. ' },',
  25213. ' set: function (v) {',
  25214. ' this.p.FField = v;',
  25215. ' }',
  25216. ' });',
  25217. ' $mod.THelper.Run();',
  25218. ' $mod.THelper.Fly.call({',
  25219. ' p: this,',
  25220. ' get: function () {',
  25221. ' return this.p.FField;',
  25222. ' },',
  25223. ' set: function (v) {',
  25224. ' this.p.FField = v;',
  25225. ' }',
  25226. ' });',
  25227. ' $mod.THelper.Run();',
  25228. ' $mod.THelper.Fly.call({',
  25229. ' p: this,',
  25230. ' get: function () {',
  25231. ' return this.p.FField;',
  25232. ' },',
  25233. ' set: function (v) {',
  25234. ' this.p.FField = v;',
  25235. ' }',
  25236. ' });',
  25237. ' $mod.THelper.Run();',
  25238. ' var $with = this.FField;',
  25239. ' $mod.THelper.Fly.call({',
  25240. ' get: function () {',
  25241. ' return $with;',
  25242. ' },',
  25243. ' set: function (v) {',
  25244. ' $with = v;',
  25245. ' }',
  25246. ' });',
  25247. ' $mod.THelper.Run();',
  25248. ' };',
  25249. '});',
  25250. 'rtl.createHelper(this, "THelper", null, function () {',
  25251. ' this.Fly = function () {',
  25252. ' };',
  25253. ' this.Run = function () {',
  25254. ' };',
  25255. '});',
  25256. 'this.o = null;',
  25257. '']),
  25258. LinesToStr([ // $mod.$main
  25259. '$mod.THelper.Fly.call({',
  25260. ' p: $mod.o,',
  25261. ' get: function () {',
  25262. ' return this.p.FField;',
  25263. ' },',
  25264. ' set: function (v) {',
  25265. ' this.p.FField = v;',
  25266. ' }',
  25267. '});',
  25268. '$mod.THelper.Run();',
  25269. 'var $with = $mod.o;',
  25270. '$mod.THelper.Fly.call({',
  25271. ' p: $with,',
  25272. ' get: function () {',
  25273. ' return this.p.FField;',
  25274. ' },',
  25275. ' set: function (v) {',
  25276. ' this.p.FField = v;',
  25277. ' }',
  25278. '});',
  25279. '$mod.THelper.Run();',
  25280. 'var $with1 = $mod.o.FField;',
  25281. '$mod.THelper.Fly.call({',
  25282. ' get: function () {',
  25283. ' return $with1;',
  25284. ' },',
  25285. ' set: function (v) {',
  25286. ' $with1 = v;',
  25287. ' }',
  25288. '});',
  25289. '$mod.THelper.Run();',
  25290. '']));
  25291. end;
  25292. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25293. begin
  25294. StartProgram(false);
  25295. Add([
  25296. '{$modeswitch typehelpers}',
  25297. 'type',
  25298. ' TObject = class',
  25299. ' FField: word;',
  25300. ' function GetField: word;',
  25301. ' property Field: word read GetField write FField;',
  25302. ' end;',
  25303. ' THelper = type helper for word',
  25304. ' procedure Fly;',
  25305. ' class procedure Run; static;',
  25306. ' end;',
  25307. 'function TObject.GetField: word;',
  25308. 'begin',
  25309. ' Field.Fly;',
  25310. ' Field.Run;',
  25311. ' Self.Field.Fly;',
  25312. ' Self.Field.Run;',
  25313. ' with Self do begin',
  25314. ' Field.Fly;',
  25315. ' Field.Run;',
  25316. ' end;',
  25317. ' with Self.Field do begin',
  25318. ' Fly;',
  25319. ' Run;',
  25320. ' end;',
  25321. 'end;',
  25322. 'procedure THelper.Fly;',
  25323. 'begin',
  25324. 'end;',
  25325. 'class procedure THelper.Run;',
  25326. 'begin',
  25327. 'end;',
  25328. 'var',
  25329. ' o: TObject;',
  25330. 'begin',
  25331. ' o.Field.Fly;',
  25332. ' o.Field.Run;',
  25333. ' with o do begin',
  25334. ' Field.Fly;',
  25335. ' Field.Run;',
  25336. ' end;',
  25337. ' with o.Field do begin',
  25338. ' Fly;',
  25339. ' Run;',
  25340. ' end;',
  25341. '']);
  25342. ConvertProgram;
  25343. CheckSource('TestTypeHelper_PassPropertyGetter',
  25344. LinesToStr([ // statements
  25345. 'rtl.createClass(this, "TObject", null, function () {',
  25346. ' this.$init = function () {',
  25347. ' this.FField = 0;',
  25348. ' };',
  25349. ' this.$final = function () {',
  25350. ' };',
  25351. ' this.GetField = function () {',
  25352. ' var Result = 0;',
  25353. ' $mod.THelper.Fly.call({',
  25354. ' p: this.GetField(),',
  25355. ' get: function () {',
  25356. ' return this.p;',
  25357. ' },',
  25358. ' set: function (v) {',
  25359. ' this.p = v;',
  25360. ' }',
  25361. ' });',
  25362. ' $mod.THelper.Run();',
  25363. ' $mod.THelper.Fly.call({',
  25364. ' p: this.GetField(),',
  25365. ' get: function () {',
  25366. ' return this.p;',
  25367. ' },',
  25368. ' set: function (v) {',
  25369. ' this.p = v;',
  25370. ' }',
  25371. ' });',
  25372. ' $mod.THelper.Run();',
  25373. ' $mod.THelper.Fly.call({',
  25374. ' p: this.GetField(),',
  25375. ' get: function () {',
  25376. ' return this.p;',
  25377. ' },',
  25378. ' set: function (v) {',
  25379. ' this.p = v;',
  25380. ' }',
  25381. ' });',
  25382. ' $mod.THelper.Run();',
  25383. ' var $with = this.GetField();',
  25384. ' $mod.THelper.Fly.call({',
  25385. ' get: function () {',
  25386. ' return $with;',
  25387. ' },',
  25388. ' set: function (v) {',
  25389. ' $with = v;',
  25390. ' }',
  25391. ' });',
  25392. ' $mod.THelper.Run();',
  25393. ' return Result;',
  25394. ' };',
  25395. '});',
  25396. 'rtl.createHelper(this, "THelper", null, function () {',
  25397. ' this.Fly = function () {',
  25398. ' };',
  25399. ' this.Run = function () {',
  25400. ' };',
  25401. '});',
  25402. 'this.o = null;',
  25403. '']),
  25404. LinesToStr([ // $mod.$main
  25405. '$mod.THelper.Fly.call({',
  25406. ' p: $mod.o.GetField(),',
  25407. ' get: function () {',
  25408. ' return this.p;',
  25409. ' },',
  25410. ' set: function (v) {',
  25411. ' this.p = v;',
  25412. ' }',
  25413. '});',
  25414. '$mod.THelper.Run();',
  25415. 'var $with = $mod.o;',
  25416. '$mod.THelper.Fly.call({',
  25417. ' p: $with.GetField(),',
  25418. ' get: function () {',
  25419. ' return this.p;',
  25420. ' },',
  25421. ' set: function (v) {',
  25422. ' this.p = v;',
  25423. ' }',
  25424. '});',
  25425. '$mod.THelper.Run();',
  25426. 'var $with1 = $mod.o.GetField();',
  25427. '$mod.THelper.Fly.call({',
  25428. ' get: function () {',
  25429. ' return $with1;',
  25430. ' },',
  25431. ' set: function (v) {',
  25432. ' $with1 = v;',
  25433. ' }',
  25434. '});',
  25435. '$mod.THelper.Run();',
  25436. '']));
  25437. end;
  25438. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25439. begin
  25440. StartProgram(false);
  25441. Add([
  25442. '{$modeswitch typehelpers}',
  25443. 'type',
  25444. ' TObject = class',
  25445. ' class var FField: word;',
  25446. ' class procedure SetField(Value: word);',
  25447. ' class property Field: word read FField write SetField;',
  25448. ' end;',
  25449. ' THelper = type helper for word',
  25450. ' procedure Fly(n: byte);',
  25451. ' end;',
  25452. 'class procedure TObject.SetField(Value: word);',
  25453. 'begin',
  25454. ' Field.Fly(1);',
  25455. ' Self.Field.Fly(2);',
  25456. ' with Self do Field.Fly(3);',
  25457. ' with Self.Field do Fly(4);',
  25458. ' TObject.Field.Fly(5);',
  25459. ' with TObject do Field.Fly(6);',
  25460. ' with TObject.Field do Fly(7);',
  25461. 'end;',
  25462. 'procedure THelper.Fly(n: byte);',
  25463. 'begin',
  25464. 'end;',
  25465. 'var',
  25466. ' o: TObject;',
  25467. 'begin',
  25468. ' o.Field.Fly(11);',
  25469. ' with o do Field.Fly(12);',
  25470. ' with o.Field do Fly(13);',
  25471. ' TObject.Field.Fly(14);',
  25472. ' with TObject do Field.Fly(15);',
  25473. ' with TObject.Field do Fly(16);',
  25474. '']);
  25475. ConvertProgram;
  25476. CheckSource('TestTypeHelper_PassClassPropertyField',
  25477. LinesToStr([ // statements
  25478. 'rtl.createClass(this, "TObject", null, function () {',
  25479. ' this.FField = 0;',
  25480. ' this.$init = function () {',
  25481. ' };',
  25482. ' this.$final = function () {',
  25483. ' };',
  25484. ' this.SetField = function (Value) {',
  25485. ' $mod.THelper.Fly.call({',
  25486. ' p: this,',
  25487. ' get: function () {',
  25488. ' return this.p.FField;',
  25489. ' },',
  25490. ' set: function (v) {',
  25491. ' $mod.TObject.FField = v;',
  25492. ' }',
  25493. ' }, 1);',
  25494. ' $mod.THelper.Fly.call({',
  25495. ' p: this,',
  25496. ' get: function () {',
  25497. ' return this.p.FField;',
  25498. ' },',
  25499. ' set: function (v) {',
  25500. ' $mod.TObject.FField = v;',
  25501. ' }',
  25502. ' }, 2);',
  25503. ' $mod.THelper.Fly.call({',
  25504. ' p: this,',
  25505. ' get: function () {',
  25506. ' return this.p.FField;',
  25507. ' },',
  25508. ' set: function (v) {',
  25509. ' $mod.TObject.FField = v;',
  25510. ' }',
  25511. ' }, 3);',
  25512. ' var $with = this.FField;',
  25513. ' $mod.THelper.Fly.call({',
  25514. ' get: function () {',
  25515. ' return $with;',
  25516. ' },',
  25517. ' set: function (v) {',
  25518. ' $with = v;',
  25519. ' }',
  25520. ' }, 4);',
  25521. ' $mod.THelper.Fly.call({',
  25522. ' p: $mod.TObject,',
  25523. ' get: function () {',
  25524. ' return this.p.FField;',
  25525. ' },',
  25526. ' set: function (v) {',
  25527. ' $mod.TObject.FField = v;',
  25528. ' }',
  25529. ' }, 5);',
  25530. ' var $with1 = $mod.TObject;',
  25531. ' $mod.THelper.Fly.call({',
  25532. ' p: $with1,',
  25533. ' get: function () {',
  25534. ' return this.p.FField;',
  25535. ' },',
  25536. ' set: function (v) {',
  25537. ' $mod.TObject.FField = v;',
  25538. ' }',
  25539. ' }, 6);',
  25540. ' var $with2 = $mod.TObject.FField;',
  25541. ' $mod.THelper.Fly.call({',
  25542. ' get: function () {',
  25543. ' return $with2;',
  25544. ' },',
  25545. ' set: function (v) {',
  25546. ' $with2 = v;',
  25547. ' }',
  25548. ' }, 7);',
  25549. ' };',
  25550. '});',
  25551. 'rtl.createHelper(this, "THelper", null, function () {',
  25552. ' this.Fly = function (n) {',
  25553. ' };',
  25554. '});',
  25555. 'this.o = null;',
  25556. '']),
  25557. LinesToStr([ // $mod.$main
  25558. '$mod.THelper.Fly.call({',
  25559. ' p: $mod.o,',
  25560. ' get: function () {',
  25561. ' return this.p.FField;',
  25562. ' },',
  25563. ' set: function (v) {',
  25564. ' $mod.TObject.FField = v;',
  25565. ' }',
  25566. '}, 11);',
  25567. 'var $with = $mod.o;',
  25568. '$mod.THelper.Fly.call({',
  25569. ' p: $with,',
  25570. ' get: function () {',
  25571. ' return this.p.FField;',
  25572. ' },',
  25573. ' set: function (v) {',
  25574. ' $mod.TObject.FField = v;',
  25575. ' }',
  25576. '}, 12);',
  25577. 'var $with1 = $mod.o.FField;',
  25578. '$mod.THelper.Fly.call({',
  25579. ' get: function () {',
  25580. ' return $with1;',
  25581. ' },',
  25582. ' set: function (v) {',
  25583. ' $with1 = v;',
  25584. ' }',
  25585. '}, 13);',
  25586. '$mod.THelper.Fly.call({',
  25587. ' p: $mod.TObject,',
  25588. ' get: function () {',
  25589. ' return this.p.FField;',
  25590. ' },',
  25591. ' set: function (v) {',
  25592. ' $mod.TObject.FField = v;',
  25593. ' }',
  25594. '}, 14);',
  25595. 'var $with2 = $mod.TObject;',
  25596. '$mod.THelper.Fly.call({',
  25597. ' p: $with2,',
  25598. ' get: function () {',
  25599. ' return this.p.FField;',
  25600. ' },',
  25601. ' set: function (v) {',
  25602. ' $mod.TObject.FField = v;',
  25603. ' }',
  25604. '}, 15);',
  25605. 'var $with3 = $mod.TObject.FField;',
  25606. '$mod.THelper.Fly.call({',
  25607. ' get: function () {',
  25608. ' return $with3;',
  25609. ' },',
  25610. ' set: function (v) {',
  25611. ' $with3 = v;',
  25612. ' }',
  25613. '}, 16);',
  25614. '']));
  25615. end;
  25616. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25617. begin
  25618. StartProgram(false);
  25619. Add([
  25620. '{$modeswitch typehelpers}',
  25621. 'type',
  25622. ' TObject = class',
  25623. ' class var FField: word;',
  25624. ' class function GetField: word; static;',
  25625. ' class property Field: word read GetField write FField;',
  25626. ' end;',
  25627. ' THelper = type helper for word',
  25628. ' procedure Fly(n: byte);',
  25629. ' end;',
  25630. 'class function TObject.GetField: word;',
  25631. 'begin',
  25632. ' Field.Fly(1);',
  25633. ' TObject.Field.Fly(5);',
  25634. ' with TObject do Field.Fly(6);',
  25635. ' with TObject.Field do Fly(7);',
  25636. 'end;',
  25637. 'procedure THelper.Fly(n: byte);',
  25638. 'begin',
  25639. 'end;',
  25640. 'var',
  25641. ' o: TObject;',
  25642. 'begin',
  25643. ' o.Field.Fly(11);',
  25644. ' with o do Field.Fly(12);',
  25645. ' with o.Field do Fly(13);',
  25646. '']);
  25647. ConvertProgram;
  25648. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25649. LinesToStr([ // statements
  25650. 'rtl.createClass(this, "TObject", null, function () {',
  25651. ' this.FField = 0;',
  25652. ' this.$init = function () {',
  25653. ' };',
  25654. ' this.$final = function () {',
  25655. ' };',
  25656. ' this.GetField = function () {',
  25657. ' var Result = 0;',
  25658. ' $mod.THelper.Fly.call({',
  25659. ' p: $mod.TObject.GetField(),',
  25660. ' get: function () {',
  25661. ' return this.p;',
  25662. ' },',
  25663. ' set: function (v) {',
  25664. ' this.p = v;',
  25665. ' }',
  25666. ' }, 1);',
  25667. ' $mod.THelper.Fly.call({',
  25668. ' p: $mod.TObject.GetField(),',
  25669. ' get: function () {',
  25670. ' return this.p;',
  25671. ' },',
  25672. ' set: function (v) {',
  25673. ' this.p = v;',
  25674. ' }',
  25675. ' }, 5);',
  25676. ' var $with = $mod.TObject;',
  25677. ' $mod.THelper.Fly.call({',
  25678. ' p: $with.GetField(),',
  25679. ' get: function () {',
  25680. ' return this.p;',
  25681. ' },',
  25682. ' set: function (v) {',
  25683. ' this.p = v;',
  25684. ' }',
  25685. ' }, 6);',
  25686. ' var $with1 = $mod.TObject.GetField();',
  25687. ' $mod.THelper.Fly.call({',
  25688. ' get: function () {',
  25689. ' return $with1;',
  25690. ' },',
  25691. ' set: function (v) {',
  25692. ' $with1 = v;',
  25693. ' }',
  25694. ' }, 7);',
  25695. ' return Result;',
  25696. ' };',
  25697. '});',
  25698. 'rtl.createHelper(this, "THelper", null, function () {',
  25699. ' this.Fly = function (n) {',
  25700. ' };',
  25701. '});',
  25702. 'this.o = null;',
  25703. '']),
  25704. LinesToStr([ // $mod.$main
  25705. '$mod.THelper.Fly.call({',
  25706. ' p: $mod.TObject.GetField(),',
  25707. ' get: function () {',
  25708. ' return this.p;',
  25709. ' },',
  25710. ' set: function (v) {',
  25711. ' this.p = v;',
  25712. ' }',
  25713. '}, 11);',
  25714. 'var $with = $mod.o;',
  25715. '$mod.THelper.Fly.call({',
  25716. ' p: $with.GetField(),',
  25717. ' get: function () {',
  25718. ' return this.p;',
  25719. ' },',
  25720. ' set: function (v) {',
  25721. ' this.p = v;',
  25722. ' }',
  25723. '}, 12);',
  25724. 'var $with1 = $mod.TObject.GetField();',
  25725. '$mod.THelper.Fly.call({',
  25726. ' get: function () {',
  25727. ' return $with1;',
  25728. ' },',
  25729. ' set: function (v) {',
  25730. ' $with1 = v;',
  25731. ' }',
  25732. '}, 13);',
  25733. '']));
  25734. end;
  25735. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25736. begin
  25737. StartProgram(false);
  25738. Add([
  25739. '{$modeswitch typehelpers}',
  25740. 'type',
  25741. ' TObject = class',
  25742. ' class var FField: word;',
  25743. ' class function GetField: word;',
  25744. ' class property Field: word read GetField write FField;',
  25745. ' end;',
  25746. ' TClass = class of TObject;',
  25747. ' THelper = type helper for word',
  25748. ' procedure Fly(n: byte);',
  25749. ' end;',
  25750. 'class function TObject.GetField: word;',
  25751. 'begin',
  25752. ' Field.Fly(1);',
  25753. ' Self.Field.Fly(5);',
  25754. ' with Self do Field.Fly(6);',
  25755. ' with Self.Field do Fly(7);',
  25756. 'end;',
  25757. 'procedure THelper.Fly(n: byte);',
  25758. 'begin',
  25759. 'end;',
  25760. 'var',
  25761. ' o: TObject;',
  25762. ' c: TClass;',
  25763. 'begin',
  25764. ' o.Field.Fly(11);',
  25765. ' with o do Field.Fly(12);',
  25766. ' with o.Field do Fly(13);',
  25767. ' c.Field.Fly(14);',
  25768. ' with c do Field.Fly(15);',
  25769. ' with c.Field do Fly(16);',
  25770. '']);
  25771. ConvertProgram;
  25772. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25773. LinesToStr([ // statements
  25774. 'rtl.createClass(this, "TObject", null, function () {',
  25775. ' this.FField = 0;',
  25776. ' this.$init = function () {',
  25777. ' };',
  25778. ' this.$final = function () {',
  25779. ' };',
  25780. ' this.GetField = function () {',
  25781. ' var Result = 0;',
  25782. ' $mod.THelper.Fly.call({',
  25783. ' p: this.GetField(),',
  25784. ' get: function () {',
  25785. ' return this.p;',
  25786. ' },',
  25787. ' set: function (v) {',
  25788. ' this.p = v;',
  25789. ' }',
  25790. ' }, 1);',
  25791. ' $mod.THelper.Fly.call({',
  25792. ' p: this.GetField(),',
  25793. ' get: function () {',
  25794. ' return this.p;',
  25795. ' },',
  25796. ' set: function (v) {',
  25797. ' this.p = v;',
  25798. ' }',
  25799. ' }, 5);',
  25800. ' $mod.THelper.Fly.call({',
  25801. ' p: this.GetField(),',
  25802. ' get: function () {',
  25803. ' return this.p;',
  25804. ' },',
  25805. ' set: function (v) {',
  25806. ' this.p = v;',
  25807. ' }',
  25808. ' }, 6);',
  25809. ' var $with = this.GetField();',
  25810. ' $mod.THelper.Fly.call({',
  25811. ' get: function () {',
  25812. ' return $with;',
  25813. ' },',
  25814. ' set: function (v) {',
  25815. ' $with = v;',
  25816. ' }',
  25817. ' }, 7);',
  25818. ' return Result;',
  25819. ' };',
  25820. '});',
  25821. 'rtl.createHelper(this, "THelper", null, function () {',
  25822. ' this.Fly = function (n) {',
  25823. ' };',
  25824. '});',
  25825. 'this.o = null;',
  25826. 'this.c = null;',
  25827. '']),
  25828. LinesToStr([ // $mod.$main
  25829. '$mod.THelper.Fly.call({',
  25830. ' p: $mod.o.$class.GetField(),',
  25831. ' get: function () {',
  25832. ' return this.p;',
  25833. ' },',
  25834. ' set: function (v) {',
  25835. ' this.p = v;',
  25836. ' }',
  25837. '}, 11);',
  25838. 'var $with = $mod.o;',
  25839. '$mod.THelper.Fly.call({',
  25840. ' p: $with.$class.GetField(),',
  25841. ' get: function () {',
  25842. ' return this.p;',
  25843. ' },',
  25844. ' set: function (v) {',
  25845. ' this.p = v;',
  25846. ' }',
  25847. '}, 12);',
  25848. 'var $with1 = $mod.o.$class.GetField();',
  25849. '$mod.THelper.Fly.call({',
  25850. ' get: function () {',
  25851. ' return $with1;',
  25852. ' },',
  25853. ' set: function (v) {',
  25854. ' $with1 = v;',
  25855. ' }',
  25856. '}, 13);',
  25857. '$mod.THelper.Fly.call({',
  25858. ' p: $mod.c.GetField(),',
  25859. ' get: function () {',
  25860. ' return this.p;',
  25861. ' },',
  25862. ' set: function (v) {',
  25863. ' this.p = v;',
  25864. ' }',
  25865. '}, 14);',
  25866. 'var $with2 = $mod.c;',
  25867. '$mod.THelper.Fly.call({',
  25868. ' p: $with2.GetField(),',
  25869. ' get: function () {',
  25870. ' return this.p;',
  25871. ' },',
  25872. ' set: function (v) {',
  25873. ' this.p = v;',
  25874. ' }',
  25875. '}, 15);',
  25876. 'var $with3 = $mod.c.GetField();',
  25877. '$mod.THelper.Fly.call({',
  25878. ' get: function () {',
  25879. ' return $with3;',
  25880. ' },',
  25881. ' set: function (v) {',
  25882. ' $with3 = v;',
  25883. ' }',
  25884. '}, 16);',
  25885. '']));
  25886. end;
  25887. procedure TTestModule.TestTypeHelper_Property;
  25888. begin
  25889. StartProgram(false);
  25890. Add([
  25891. '{$modeswitch typehelpers}',
  25892. 'type',
  25893. ' THelper = type helper for word',
  25894. ' function GetSize: longint;',
  25895. ' procedure SetSize(Value: longint);',
  25896. ' property Size: longint read GetSize write SetSize;',
  25897. ' end;',
  25898. 'function THelper.GetSize: longint;',
  25899. 'begin',
  25900. ' Result:=Size+1;',
  25901. ' Size:=2;',
  25902. ' Result:=Self.Size+3;',
  25903. ' Self.Size:=4;',
  25904. ' with Self do begin',
  25905. ' Result:=Size+5;',
  25906. ' Size:=6;',
  25907. ' end;',
  25908. 'end;',
  25909. 'procedure THelper.SetSize(Value: longint);',
  25910. 'begin',
  25911. 'end;',
  25912. 'var w: word;',
  25913. 'begin',
  25914. ' w:=w.Size+7;',
  25915. ' w.Size:=w+8;',
  25916. ' with w do begin',
  25917. ' w:=Size+9;',
  25918. ' Size:=w+10;',
  25919. ' end;',
  25920. '']);
  25921. ConvertProgram;
  25922. CheckSource('TestTypeHelper_Property',
  25923. LinesToStr([ // statements
  25924. 'rtl.createHelper(this, "THelper", null, function () {',
  25925. ' this.GetSize = function () {',
  25926. ' var Result = 0;',
  25927. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25928. ' $mod.THelper.SetSize.call(this, 2);',
  25929. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25930. ' $mod.THelper.SetSize.call(this, 4);',
  25931. ' var $with = this.get();',
  25932. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25933. ' $mod.THelper.SetSize.call(this, 6);',
  25934. ' return Result;',
  25935. ' };',
  25936. ' this.SetSize = function (Value) {',
  25937. ' };',
  25938. '});',
  25939. 'this.w = 0;',
  25940. '']),
  25941. LinesToStr([ // $mod.$main
  25942. '$mod.w = $mod.THelper.GetSize.call({',
  25943. ' p: $mod,',
  25944. ' get: function () {',
  25945. ' return this.p.w;',
  25946. ' },',
  25947. ' set: function (v) {',
  25948. ' this.p.w = v;',
  25949. ' }',
  25950. '}) + 7;',
  25951. '$mod.THelper.SetSize.call({',
  25952. ' p: $mod,',
  25953. ' get: function () {',
  25954. ' return this.p.w;',
  25955. ' },',
  25956. ' set: function (v) {',
  25957. ' this.p.w = v;',
  25958. ' }',
  25959. '}, $mod.w + 8);',
  25960. 'var $with = $mod.w;',
  25961. '$mod.w = $mod.THelper.GetSize.call({',
  25962. ' get: function () {',
  25963. ' return $with;',
  25964. ' },',
  25965. ' set: function (v) {',
  25966. ' $with = v;',
  25967. ' }',
  25968. '}) + 9;',
  25969. '$mod.THelper.SetSize.call({',
  25970. ' get: function () {',
  25971. ' return $with;',
  25972. ' },',
  25973. ' set: function (v) {',
  25974. ' $with = v;',
  25975. ' }',
  25976. '}, $mod.w + 10);',
  25977. '']));
  25978. end;
  25979. procedure TTestModule.TestTypeHelper_Property_Array;
  25980. begin
  25981. StartProgram(false);
  25982. Add([
  25983. '{$modeswitch typehelpers}',
  25984. 'type',
  25985. ' THelper = type helper for word',
  25986. ' function GetItems(Index: byte): boolean;',
  25987. ' procedure SetItems(Index: byte; Value: boolean);',
  25988. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25989. ' end;',
  25990. 'function THelper.GetItems(Index: byte): boolean;',
  25991. 'begin',
  25992. ' Result:=Items[1];',
  25993. ' Items[2]:=false;',
  25994. ' Result:=Self.Items[3];',
  25995. ' Self.Items[4]:=true;',
  25996. ' with Self do begin',
  25997. ' Result:=Items[5];',
  25998. ' Items[6]:=false;',
  25999. ' end;',
  26000. 'end;',
  26001. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26002. 'begin',
  26003. 'end;',
  26004. 'var',
  26005. ' w: word;',
  26006. ' b: boolean;',
  26007. 'begin',
  26008. ' b:=w.Items[1];',
  26009. ' w.Items[2]:=b;',
  26010. ' with w do begin',
  26011. ' b:=Items[3];',
  26012. ' Items[4]:=b;',
  26013. ' end;',
  26014. '']);
  26015. ConvertProgram;
  26016. CheckSource('TestTypeHelper_Property_Array',
  26017. LinesToStr([ // statements
  26018. 'rtl.createHelper(this, "THelper", null, function () {',
  26019. ' this.GetItems = function (Index) {',
  26020. ' var Result = false;',
  26021. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26022. ' $mod.THelper.SetItems.call(this, 2, false);',
  26023. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26024. ' $mod.THelper.SetItems.call(this, 4, true);',
  26025. ' var $with = this.get();',
  26026. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26027. ' $mod.THelper.SetItems.call(this, 6, false);',
  26028. ' return Result;',
  26029. ' };',
  26030. ' this.SetItems = function (Index, Value) {',
  26031. ' };',
  26032. '});',
  26033. 'this.w = 0;',
  26034. 'this.b = false;',
  26035. '']),
  26036. LinesToStr([ // $mod.$main
  26037. '$mod.b = $mod.THelper.GetItems.call({',
  26038. ' p: $mod,',
  26039. ' get: function () {',
  26040. ' return this.p.w;',
  26041. ' },',
  26042. ' set: function (v) {',
  26043. ' this.p.w = v;',
  26044. ' }',
  26045. '}, 1);',
  26046. '$mod.THelper.SetItems.call({',
  26047. ' p: $mod,',
  26048. ' get: function () {',
  26049. ' return this.p.w;',
  26050. ' },',
  26051. ' set: function (v) {',
  26052. ' this.p.w = v;',
  26053. ' }',
  26054. '}, 2, $mod.b);',
  26055. 'var $with = $mod.w;',
  26056. '$mod.b = $mod.THelper.GetItems.call({',
  26057. ' get: function () {',
  26058. ' return $with;',
  26059. ' },',
  26060. ' set: function (v) {',
  26061. ' $with = v;',
  26062. ' }',
  26063. '}, 3);',
  26064. '$mod.THelper.SetItems.call({',
  26065. ' get: function () {',
  26066. ' return $with;',
  26067. ' },',
  26068. ' set: function (v) {',
  26069. ' $with = v;',
  26070. ' }',
  26071. '}, 4, $mod.b);',
  26072. '']));
  26073. end;
  26074. procedure TTestModule.TestTypeHelper_ClassProperty;
  26075. begin
  26076. StartProgram(false);
  26077. Add([
  26078. '{$modeswitch typehelpers}',
  26079. 'type',
  26080. ' THelper = type helper for word',
  26081. ' class function GetSize: longint; static;',
  26082. ' class procedure SetSize(Value: longint); static;',
  26083. ' class property Size: longint read GetSize write SetSize;',
  26084. ' end;',
  26085. 'class function THelper.GetSize: longint;',
  26086. 'begin',
  26087. ' Result:=Size+1;',
  26088. ' Size:=2;',
  26089. 'end;',
  26090. 'class procedure THelper.SetSize(Value: longint);',
  26091. 'begin',
  26092. 'end;',
  26093. 'begin',
  26094. '']);
  26095. ConvertProgram;
  26096. CheckSource('TestTypeHelper_ClassProperty',
  26097. LinesToStr([ // statements
  26098. 'rtl.createHelper(this, "THelper", null, function () {',
  26099. ' this.GetSize = function () {',
  26100. ' var Result = 0;',
  26101. ' Result = $mod.THelper.GetSize() + 1;',
  26102. ' $mod.THelper.SetSize(2);',
  26103. ' return Result;',
  26104. ' };',
  26105. ' this.SetSize = function (Value) {',
  26106. ' };',
  26107. '});',
  26108. '']),
  26109. LinesToStr([ // $mod.$main
  26110. '']));
  26111. end;
  26112. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26113. begin
  26114. StartProgram(false);
  26115. Add([
  26116. '{$modeswitch typehelpers}',
  26117. 'type',
  26118. ' THelper = type helper for word',
  26119. ' class function GetItems(Index: byte): boolean; static;',
  26120. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26121. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26122. ' end;',
  26123. 'class function THelper.GetItems(Index: byte): boolean;',
  26124. 'begin',
  26125. ' Result:=Items[1];',
  26126. ' Items[2]:=false;',
  26127. 'end;',
  26128. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26129. 'begin',
  26130. 'end;',
  26131. 'var',
  26132. ' w: word;',
  26133. ' b: boolean;',
  26134. 'begin',
  26135. ' b:=w.Items[1];',
  26136. ' w.Items[2]:=b;',
  26137. ' with w do begin',
  26138. ' b:=Items[3];',
  26139. ' Items[4]:=b;',
  26140. ' end;',
  26141. '']);
  26142. ConvertProgram;
  26143. CheckSource('TestTypeHelper_ClassProperty_Array',
  26144. LinesToStr([ // statements
  26145. 'rtl.createHelper(this, "THelper", null, function () {',
  26146. ' this.GetItems = function (Index) {',
  26147. ' var Result = false;',
  26148. ' Result = $mod.THelper.GetItems(1);',
  26149. ' $mod.THelper.SetItems(2, false);',
  26150. ' return Result;',
  26151. ' };',
  26152. ' this.SetItems = function (Index, Value) {',
  26153. ' };',
  26154. '});',
  26155. 'this.w = 0;',
  26156. 'this.b = false;',
  26157. '']),
  26158. LinesToStr([ // $mod.$main
  26159. '$mod.b = $mod.THelper.GetItems(1);',
  26160. '$mod.THelper.SetItems(2, $mod.b);',
  26161. 'var $with = $mod.w;',
  26162. '$mod.b = $mod.THelper.GetItems(3);',
  26163. '$mod.THelper.SetItems(4, $mod.b);',
  26164. '']));
  26165. end;
  26166. procedure TTestModule.TestTypeHelper_ClassMethod;
  26167. begin
  26168. StartProgram(false);
  26169. Add([
  26170. '{$modeswitch typehelpers}',
  26171. 'type',
  26172. ' THelper = type helper for word',
  26173. ' class procedure DoStatic; static;',
  26174. ' end;',
  26175. 'class procedure THelper.DoStatic;',
  26176. 'begin',
  26177. ' DoStatic;',
  26178. ' DoStatic();',
  26179. 'end;',
  26180. 'var w: word;',
  26181. 'begin',
  26182. ' w.DoStatic;',
  26183. ' w.DoStatic();',
  26184. '']);
  26185. ConvertProgram;
  26186. CheckSource('TestTypeHelper_ClassMethod',
  26187. LinesToStr([ // statements
  26188. 'rtl.createHelper(this, "THelper", null, function () {',
  26189. ' this.DoStatic = function () {',
  26190. ' $mod.THelper.DoStatic();',
  26191. ' $mod.THelper.DoStatic();',
  26192. ' };',
  26193. '});',
  26194. 'this.w = 0;',
  26195. '']),
  26196. LinesToStr([ // $mod.$main
  26197. '$mod.THelper.DoStatic();',
  26198. '$mod.THelper.DoStatic();',
  26199. '']));
  26200. end;
  26201. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26202. begin
  26203. StartProgram(false);
  26204. Add([
  26205. '{$modeswitch typehelpers}',
  26206. 'type',
  26207. ' THelper = type helper for word',
  26208. ' procedure Run; external name ''Run'';',
  26209. ' end;',
  26210. 'var w: word;',
  26211. 'begin',
  26212. ' w.Run;',
  26213. '']);
  26214. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26215. ConvertProgram;
  26216. end;
  26217. procedure TTestModule.TestTypeHelper_Constructor;
  26218. begin
  26219. StartProgram(false);
  26220. Add([
  26221. '{$modeswitch typehelpers}',
  26222. 'type',
  26223. ' THelper = type helper for word',
  26224. ' constructor Init(e: longint);',
  26225. ' end;',
  26226. 'constructor THelper.Init(e: longint);',
  26227. 'begin',
  26228. ' Self:=e;',
  26229. ' Init(e+1);',
  26230. 'end;',
  26231. 'var w: word;',
  26232. 'begin',
  26233. ' w:=word.Init(2);',
  26234. ' w:=w.Init(3);',
  26235. ' with word do w:=Init(4);',
  26236. ' with w do w:=Init(5);',
  26237. '']);
  26238. ConvertProgram;
  26239. CheckSource('TestTypeHelper_Constructor',
  26240. LinesToStr([ // statements
  26241. 'rtl.createHelper(this, "THelper", null, function () {',
  26242. ' this.Init = function (e) {',
  26243. ' this.set(e);',
  26244. ' $mod.THelper.Init.call(this, e + 1);',
  26245. ' return this.get();',
  26246. ' };',
  26247. ' this.$new = function (fn, args) {',
  26248. ' return this[fn].apply({',
  26249. ' p: 0,',
  26250. ' get: function () {',
  26251. ' return this.p;',
  26252. ' },',
  26253. ' set: function (v) {',
  26254. ' this.p = v;',
  26255. ' }',
  26256. ' }, args);',
  26257. ' };',
  26258. '});',
  26259. 'this.w = 0;',
  26260. '']),
  26261. LinesToStr([ // $mod.$main
  26262. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26263. '$mod.w = $mod.THelper.Init.call({',
  26264. ' p: $mod,',
  26265. ' get: function () {',
  26266. ' return this.p.w;',
  26267. ' },',
  26268. ' set: function (v) {',
  26269. ' this.p.w = v;',
  26270. ' }',
  26271. '}, 3);',
  26272. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26273. 'var $with = $mod.w;',
  26274. '$mod.w = $mod.THelper.Init.call({',
  26275. ' get: function () {',
  26276. ' return $with;',
  26277. ' },',
  26278. ' set: function (v) {',
  26279. ' $with = v;',
  26280. ' }',
  26281. '}, 5);',
  26282. '']));
  26283. end;
  26284. procedure TTestModule.TestTypeHelper_Word;
  26285. begin
  26286. StartProgram(false);
  26287. Add([
  26288. '{$modeswitch typehelpers}',
  26289. 'type',
  26290. ' THelper = type helper for word',
  26291. ' procedure DoIt(e: byte = 123);',
  26292. ' end;',
  26293. 'procedure THelper.DoIt(e: byte);',
  26294. 'begin',
  26295. ' Self:=e;',
  26296. ' Self:=Self+1;',
  26297. ' with Self do Doit;',
  26298. 'end;',
  26299. 'begin',
  26300. ' word(3).DoIt;',
  26301. '']);
  26302. ConvertProgram;
  26303. CheckSource('TestTypeHelper_Word',
  26304. LinesToStr([ // statements
  26305. 'rtl.createHelper(this, "THelper", null, function () {',
  26306. ' this.DoIt = function (e) {',
  26307. ' this.set(e);',
  26308. ' this.set(this.get() + 1);',
  26309. ' var $with = this.get();',
  26310. ' $mod.THelper.DoIt.call(this, 123);',
  26311. ' };',
  26312. '});',
  26313. '']),
  26314. LinesToStr([ // $mod.$main
  26315. '$mod.THelper.DoIt.call({',
  26316. ' get: function () {',
  26317. ' return 3;',
  26318. ' },',
  26319. ' set: function (v) {',
  26320. ' rtl.raiseE("EPropReadOnly");',
  26321. ' }',
  26322. '}, 123);',
  26323. '']));
  26324. end;
  26325. procedure TTestModule.TestTypeHelper_Boolean;
  26326. begin
  26327. StartProgram(false);
  26328. Add([
  26329. '{$modeswitch typehelpers}',
  26330. 'type',
  26331. ' Integer = longint;',
  26332. ' THelper = type helper for boolean',
  26333. ' procedure Run(e: wordbool = true);',
  26334. ' end;',
  26335. 'procedure THelper.Run(e: wordbool);',
  26336. 'begin',
  26337. ' Self:=e;',
  26338. ' Self:=not Self;',
  26339. ' with Self do Run;',
  26340. ' if Integer(Self)=0 then ;',
  26341. 'end;',
  26342. 'begin',
  26343. ' boolean(3).Run;',
  26344. '']);
  26345. ConvertProgram;
  26346. CheckSource('TestTypeHelper_Boolean',
  26347. LinesToStr([ // statements
  26348. 'rtl.createHelper(this, "THelper", null, function () {',
  26349. ' this.Run = function (e) {',
  26350. ' this.set(e);',
  26351. ' this.set(!this.get());',
  26352. ' var $with = this.get();',
  26353. ' $mod.THelper.Run.call(this, true);',
  26354. ' if ((this.get() ? 1 : 0) === 0) ;',
  26355. ' };',
  26356. '});',
  26357. '']),
  26358. LinesToStr([ // $mod.$main
  26359. '$mod.THelper.Run.call({',
  26360. ' a: 3 != 0,',
  26361. ' get: function () {',
  26362. ' return this.a;',
  26363. ' },',
  26364. ' set: function (v) {',
  26365. ' rtl.raiseE("EPropReadOnly");',
  26366. ' }',
  26367. '}, true);',
  26368. '']));
  26369. end;
  26370. procedure TTestModule.TestTypeHelper_WordBool;
  26371. begin
  26372. StartProgram(false);
  26373. Add([
  26374. '{$modeswitch typehelpers}',
  26375. 'type',
  26376. ' Integer = longint;',
  26377. ' THelper = type helper for WordBool',
  26378. ' procedure Run(e: wordbool = true);',
  26379. ' end;',
  26380. 'procedure THelper.Run(e: wordbool);',
  26381. 'var i: integer;',
  26382. 'begin',
  26383. ' i:=Integer(Self);',
  26384. 'end;',
  26385. 'var w: wordbool;',
  26386. 'begin',
  26387. ' w.Run;',
  26388. ' wordbool(3).Run;',
  26389. '']);
  26390. ConvertProgram;
  26391. CheckSource('TestTypeHelper_WordBool',
  26392. LinesToStr([ // statements
  26393. 'rtl.createHelper(this, "THelper", null, function () {',
  26394. ' this.Run = function (e) {',
  26395. ' var i = 0;',
  26396. ' i = (this.get() ? 1 : 0);',
  26397. ' };',
  26398. '});',
  26399. 'this.w = false;',
  26400. '']),
  26401. LinesToStr([ // $mod.$main
  26402. '$mod.THelper.Run.call({',
  26403. ' p: $mod,',
  26404. ' get: function () {',
  26405. ' return this.p.w;',
  26406. ' },',
  26407. ' set: function (v) {',
  26408. ' this.p.w = v;',
  26409. ' }',
  26410. '}, true);',
  26411. '$mod.THelper.Run.call({',
  26412. ' a: 3 != 0,',
  26413. ' get: function () {',
  26414. ' return this.a;',
  26415. ' },',
  26416. ' set: function (v) {',
  26417. ' rtl.raiseE("EPropReadOnly");',
  26418. ' }',
  26419. '}, true);',
  26420. '']));
  26421. end;
  26422. procedure TTestModule.TestTypeHelper_Double;
  26423. begin
  26424. StartProgram(false);
  26425. Add([
  26426. '{$modeswitch typehelpers}',
  26427. 'type',
  26428. ' Float = type double;',
  26429. ' THelper = type helper for Float',
  26430. ' const NPI = 3.141592;',
  26431. ' function ToStr: String;',
  26432. ' end;',
  26433. 'function THelper.ToStr: String;',
  26434. 'begin',
  26435. 'end;',
  26436. 'procedure DoIt(s: string);',
  26437. 'begin',
  26438. 'end;',
  26439. 'var f: Float;',
  26440. 'begin',
  26441. ' DoIt(f.toStr);',
  26442. ' DoIt(f.toStr());',
  26443. ' (f*f).toStr;',
  26444. ' DoIt((f*f).toStr);',
  26445. '']);
  26446. ConvertProgram;
  26447. CheckSource('TestTypeHelper_Double',
  26448. LinesToStr([ // statements
  26449. 'rtl.createHelper(this, "THelper", null, function () {',
  26450. ' this.NPI = 3.141592;',
  26451. ' this.ToStr = function () {',
  26452. ' var Result = "";',
  26453. ' return Result;',
  26454. ' };',
  26455. '});',
  26456. 'this.DoIt = function (s) {',
  26457. '};',
  26458. 'this.f = 0.0;',
  26459. '']),
  26460. LinesToStr([ // $mod.$main
  26461. '$mod.DoIt($mod.THelper.ToStr.call({',
  26462. ' p: $mod,',
  26463. ' get: function () {',
  26464. ' return this.p.f;',
  26465. ' },',
  26466. ' set: function (v) {',
  26467. ' this.p.f = v;',
  26468. ' }',
  26469. '}));',
  26470. '$mod.DoIt($mod.THelper.ToStr.call({',
  26471. ' p: $mod,',
  26472. ' get: function () {',
  26473. ' return this.p.f;',
  26474. ' },',
  26475. ' set: function (v) {',
  26476. ' this.p.f = v;',
  26477. ' }',
  26478. '}));',
  26479. '$mod.THelper.ToStr.call({',
  26480. ' a: $mod.f * $mod.f,',
  26481. ' get: function () {',
  26482. ' return this.a;',
  26483. ' },',
  26484. ' set: function (v) {',
  26485. ' rtl.raiseE("EPropReadOnly");',
  26486. ' }',
  26487. '});',
  26488. '$mod.DoIt($mod.THelper.ToStr.call({',
  26489. ' a: $mod.f * $mod.f,',
  26490. ' get: function () {',
  26491. ' return this.a;',
  26492. ' },',
  26493. ' set: function (v) {',
  26494. ' rtl.raiseE("EPropReadOnly");',
  26495. ' }',
  26496. '}));',
  26497. '']));
  26498. end;
  26499. procedure TTestModule.TestTypeHelper_NativeInt;
  26500. begin
  26501. StartProgram(false);
  26502. Add([
  26503. '{$modeswitch typehelpers}',
  26504. 'type',
  26505. ' MaxInt = type nativeint;',
  26506. ' THelperI = type helper for MaxInt',
  26507. ' function ToStr: String;',
  26508. ' end;',
  26509. ' MaxUInt = type nativeuint;',
  26510. ' THelperU = type helper for MaxUInt',
  26511. ' function ToStr: String;',
  26512. ' end;',
  26513. 'function THelperI.ToStr: String;',
  26514. 'begin',
  26515. ' Result:=str(Self);',
  26516. 'end;',
  26517. 'function THelperU.ToStr: String;',
  26518. 'begin',
  26519. ' Result:=str(Self);',
  26520. 'end;',
  26521. 'procedure DoIt(s: string);',
  26522. 'begin',
  26523. 'end;',
  26524. 'var i: MaxInt;',
  26525. 'begin',
  26526. ' DoIt(i.toStr);',
  26527. ' DoIt(i.toStr());',
  26528. ' (i*i).toStr;',
  26529. ' DoIt((i*i).toStr);',
  26530. '']);
  26531. ConvertProgram;
  26532. CheckSource('TestTypeHelper_NativeInt',
  26533. LinesToStr([ // statements
  26534. 'rtl.createHelper(this, "THelperI", null, function () {',
  26535. ' this.ToStr = function () {',
  26536. ' var Result = "";',
  26537. ' Result = "" + this.get();',
  26538. ' return Result;',
  26539. ' };',
  26540. '});',
  26541. 'rtl.createHelper(this, "THelperU", null, function () {',
  26542. ' this.ToStr = function () {',
  26543. ' var Result = "";',
  26544. ' Result = "" + this.get();',
  26545. ' return Result;',
  26546. ' };',
  26547. '});',
  26548. 'this.DoIt = function (s) {',
  26549. '};',
  26550. 'this.i = 0;',
  26551. '']),
  26552. LinesToStr([ // $mod.$main
  26553. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26554. ' p: $mod,',
  26555. ' get: function () {',
  26556. ' return this.p.i;',
  26557. ' },',
  26558. ' set: function (v) {',
  26559. ' this.p.i = v;',
  26560. ' }',
  26561. '}));',
  26562. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26563. ' p: $mod,',
  26564. ' get: function () {',
  26565. ' return this.p.i;',
  26566. ' },',
  26567. ' set: function (v) {',
  26568. ' this.p.i = v;',
  26569. ' }',
  26570. '}));',
  26571. '$mod.THelperI.ToStr.call({',
  26572. ' a: $mod.i * $mod.i,',
  26573. ' get: function () {',
  26574. ' return this.a;',
  26575. ' },',
  26576. ' set: function (v) {',
  26577. ' rtl.raiseE("EPropReadOnly");',
  26578. ' }',
  26579. '});',
  26580. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26581. ' a: $mod.i * $mod.i,',
  26582. ' get: function () {',
  26583. ' return this.a;',
  26584. ' },',
  26585. ' set: function (v) {',
  26586. ' rtl.raiseE("EPropReadOnly");',
  26587. ' }',
  26588. '}));',
  26589. '']));
  26590. end;
  26591. procedure TTestModule.TestTypeHelper_StringChar;
  26592. begin
  26593. StartProgram(false);
  26594. Add([
  26595. '{$modeswitch typehelpers}',
  26596. 'type',
  26597. ' TStringHelper = type helper for string',
  26598. ' procedure DoIt(e: byte = 123);',
  26599. ' end;',
  26600. ' TCharHelper = type helper for char',
  26601. ' procedure Fly;',
  26602. ' end;',
  26603. 'procedure TStringHelper.DoIt(e: byte);',
  26604. 'begin',
  26605. ' Self[1]:=''c'';',
  26606. ' Self[2]:=Self[3];',
  26607. 'end;',
  26608. 'procedure TCharHelper.Fly;',
  26609. 'begin',
  26610. ' Self:=''c'';',
  26611. 'end;',
  26612. 'begin',
  26613. ' ''abc''.DoIt;',
  26614. ' ''xyz''.DoIt();',
  26615. ' ''c''.Fly();',
  26616. '']);
  26617. ConvertProgram;
  26618. CheckSource('TestTypeHelper_StringChar',
  26619. LinesToStr([ // statements
  26620. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26621. ' this.DoIt = function (e) {',
  26622. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26623. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26624. ' };',
  26625. '});',
  26626. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26627. ' this.Fly = function () {',
  26628. ' this.set("c");',
  26629. ' };',
  26630. '});',
  26631. '']),
  26632. LinesToStr([ // $mod.$main
  26633. '$mod.TStringHelper.DoIt.call({',
  26634. ' get: function () {',
  26635. ' return "abc";',
  26636. ' },',
  26637. ' set: function (v) {',
  26638. ' rtl.raiseE("EPropReadOnly");',
  26639. ' }',
  26640. '}, 123);',
  26641. '$mod.TStringHelper.DoIt.call({',
  26642. ' get: function () {',
  26643. ' return "xyz";',
  26644. ' },',
  26645. ' set: function (v) {',
  26646. ' rtl.raiseE("EPropReadOnly");',
  26647. ' }',
  26648. '}, 123);',
  26649. '$mod.TCharHelper.Fly.call({',
  26650. ' get: function () {',
  26651. ' return "c";',
  26652. ' },',
  26653. ' set: function (v) {',
  26654. ' rtl.raiseE("EPropReadOnly");',
  26655. ' }',
  26656. '});',
  26657. '']));
  26658. end;
  26659. procedure TTestModule.TestTypeHelper_JSValue;
  26660. begin
  26661. StartProgram(false);
  26662. Add([
  26663. '{$modeswitch typehelpers}',
  26664. 'type',
  26665. ' TExtValue = type jsvalue;',
  26666. ' THelper = type helper for TExtValue',
  26667. ' function ToStr: String;',
  26668. ' end;',
  26669. 'function THelper.ToStr: String;',
  26670. 'begin',
  26671. 'end;',
  26672. 'var',
  26673. ' s: string;',
  26674. ' v: TExtValue;',
  26675. 'begin',
  26676. ' s:=v.toStr;',
  26677. ' s:=v.toStr();',
  26678. ' TExtValue(s).toStr;',
  26679. '']);
  26680. ConvertProgram;
  26681. CheckSource('TestTypeHelper_JSValue',
  26682. LinesToStr([ // statements
  26683. 'rtl.createHelper(this, "THelper", null, function () {',
  26684. ' this.ToStr = function () {',
  26685. ' var Result = "";',
  26686. ' return Result;',
  26687. ' };',
  26688. '});',
  26689. 'this.s = "";',
  26690. 'this.v = undefined;',
  26691. '']),
  26692. LinesToStr([ // $mod.$main
  26693. '$mod.s = $mod.THelper.ToStr.call({',
  26694. ' p: $mod,',
  26695. ' get: function () {',
  26696. ' return this.p.v;',
  26697. ' },',
  26698. ' set: function (v) {',
  26699. ' this.p.v = v;',
  26700. ' }',
  26701. '});',
  26702. '$mod.s = $mod.THelper.ToStr.call({',
  26703. ' p: $mod,',
  26704. ' get: function () {',
  26705. ' return this.p.v;',
  26706. ' },',
  26707. ' set: function (v) {',
  26708. ' this.p.v = v;',
  26709. ' }',
  26710. '});',
  26711. '$mod.THelper.ToStr.call({',
  26712. ' p: $mod,',
  26713. ' get: function () {',
  26714. ' return this.p.s;',
  26715. ' },',
  26716. ' set: function (v) {',
  26717. ' rtl.raiseE("EPropReadOnly");',
  26718. ' }',
  26719. '});',
  26720. '']));
  26721. end;
  26722. procedure TTestModule.TestTypeHelper_Array;
  26723. begin
  26724. StartProgram(false);
  26725. Add([
  26726. '{$modeswitch typehelpers}',
  26727. 'type',
  26728. ' TArrOfBool = array of boolean;',
  26729. ' TArrOfJS = array of jsvalue;',
  26730. ' THelper = type helper for TArrOfBool',
  26731. ' procedure DoIt(e: byte = 123);',
  26732. ' end;',
  26733. 'procedure THelper.DoIt(e: byte);',
  26734. 'begin',
  26735. ' Self[1]:=true;',
  26736. ' Self[2]:=not Self[3];',
  26737. ' SetLength(Self,4);',
  26738. ' Self:=Concat(Self,[true]);',
  26739. 'end;',
  26740. 'var',
  26741. ' b: TArrOfBool;',
  26742. ' j: TArrOfJS;',
  26743. 'begin',
  26744. ' b.DoIt;',
  26745. ' TArrOfBool(j).DoIt();',
  26746. '']);
  26747. ConvertProgram;
  26748. CheckSource('TestTypeHelper_Array',
  26749. LinesToStr([ // statements
  26750. 'rtl.createHelper(this, "THelper", null, function () {',
  26751. ' this.DoIt = function (e) {',
  26752. ' this.get()[1] = true;',
  26753. ' this.get()[2] = !this.get()[3];',
  26754. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26755. ' this.set(rtl.arrayPushN(this.get(), true));',
  26756. ' };',
  26757. '});',
  26758. 'this.b = [];',
  26759. 'this.j = [];',
  26760. '']),
  26761. LinesToStr([ // $mod.$main
  26762. '$mod.THelper.DoIt.call({',
  26763. ' p: $mod,',
  26764. ' get: function () {',
  26765. ' return this.p.b;',
  26766. ' },',
  26767. ' set: function (v) {',
  26768. ' this.p.b = v;',
  26769. ' }',
  26770. '}, 123);',
  26771. '$mod.THelper.DoIt.call({',
  26772. ' p: $mod,',
  26773. ' get: function () {',
  26774. ' return this.p.j;',
  26775. ' },',
  26776. ' set: function (v) {',
  26777. ' this.p.j = v;',
  26778. ' }',
  26779. '}, 123);',
  26780. '']));
  26781. end;
  26782. procedure TTestModule.TestTypeHelper_EnumType;
  26783. begin
  26784. StartProgram(false);
  26785. Add([
  26786. '{$modeswitch typehelpers}',
  26787. 'type',
  26788. ' TEnum = (red,blue);',
  26789. ' THelper = type helper for TEnum',
  26790. ' procedure DoIt(e: byte = 123);',
  26791. ' class procedure Swing(w: word); static;',
  26792. ' end;',
  26793. 'procedure THelper.DoIt(e: byte);',
  26794. 'begin',
  26795. ' Self:=red;',
  26796. ' Self:=succ(Self);',
  26797. ' with Self do Doit;',
  26798. 'end;',
  26799. 'class procedure THelper.Swing(w: word);',
  26800. 'begin',
  26801. 'end;',
  26802. 'var e: TEnum;',
  26803. 'begin',
  26804. ' e.DoIt;',
  26805. ' red.DoIt;',
  26806. ' TEnum.blue.DoIt;',
  26807. ' TEnum(1).DoIt;',
  26808. ' TEnum.Swing(3);',
  26809. '']);
  26810. ConvertProgram;
  26811. CheckSource('TestTypeHelper_EnumType',
  26812. LinesToStr([ // statements
  26813. 'this.TEnum = {',
  26814. ' "0": "red",',
  26815. ' red: 0,',
  26816. ' "1": "blue",',
  26817. ' blue: 1',
  26818. '};',
  26819. 'rtl.createHelper(this, "THelper", null, function () {',
  26820. ' this.DoIt = function (e) {',
  26821. ' this.set($mod.TEnum.red);',
  26822. ' this.set(this.get() + 1);',
  26823. ' var $with = this.get();',
  26824. ' $mod.THelper.DoIt.call(this, 123);',
  26825. ' };',
  26826. ' this.Swing = function (w) {',
  26827. ' };',
  26828. '});',
  26829. 'this.e = 0;',
  26830. '']),
  26831. LinesToStr([ // $mod.$main
  26832. '$mod.THelper.DoIt.call({',
  26833. ' p: $mod,',
  26834. ' get: function () {',
  26835. ' return this.p.e;',
  26836. ' },',
  26837. ' set: function (v) {',
  26838. ' this.p.e = v;',
  26839. ' }',
  26840. '}, 123);',
  26841. '$mod.THelper.DoIt.call({',
  26842. ' p: $mod.TEnum,',
  26843. ' get: function () {',
  26844. ' return this.p.red;',
  26845. ' },',
  26846. ' set: function (v) {',
  26847. ' rtl.raiseE("EPropReadOnly");',
  26848. ' }',
  26849. '}, 123);',
  26850. '$mod.THelper.DoIt.call({',
  26851. ' p: $mod.TEnum,',
  26852. ' get: function () {',
  26853. ' return this.p.blue;',
  26854. ' },',
  26855. ' set: function (v) {',
  26856. ' rtl.raiseE("EPropReadOnly");',
  26857. ' }',
  26858. '}, 123);',
  26859. '$mod.THelper.DoIt.call({',
  26860. ' get: function () {',
  26861. ' return 1;',
  26862. ' },',
  26863. ' set: function (v) {',
  26864. ' rtl.raiseE("EPropReadOnly");',
  26865. ' }',
  26866. '}, 123);',
  26867. '$mod.THelper.Swing(3);',
  26868. '']));
  26869. end;
  26870. procedure TTestModule.TestTypeHelper_SetType;
  26871. begin
  26872. StartProgram(false);
  26873. Add([
  26874. '{$modeswitch typehelpers}',
  26875. 'type',
  26876. ' TEnum = (red,blue);',
  26877. ' TSetOfEnum = set of TEnum;',
  26878. ' THelper = type helper for TSetOfEnum',
  26879. ' procedure DoIt(e: byte = 123);',
  26880. ' constructor Init(e: TEnum);',
  26881. ' constructor InitEmpty;',
  26882. ' end;',
  26883. 'procedure THelper.DoIt(e: byte);',
  26884. 'begin',
  26885. ' Self:=[];',
  26886. ' Self:=[red];',
  26887. ' Include(Self,blue);',
  26888. 'end;',
  26889. 'constructor THelper.Init(e: TEnum);',
  26890. 'begin',
  26891. ' Self:=[];',
  26892. ' Self:=[e];',
  26893. ' Include(Self,blue);',
  26894. 'end;',
  26895. 'constructor THelper.InitEmpty;',
  26896. 'begin',
  26897. 'end;',
  26898. 'var s: TSetOfEnum;',
  26899. 'begin',
  26900. ' s.DoIt;',
  26901. //' [red].DoIt;',
  26902. //' with s do DoIt;',
  26903. //' with [red,blue] do DoIt;',
  26904. ' s:=TSetOfEnum.Init(blue);',
  26905. ' s:=s.Init(blue);',
  26906. '']);
  26907. ConvertProgram;
  26908. CheckSource('TestTypeHelper_SetType',
  26909. LinesToStr([ // statements
  26910. 'this.TEnum = {',
  26911. ' "0": "red",',
  26912. ' red: 0,',
  26913. ' "1": "blue",',
  26914. ' blue: 1',
  26915. '};',
  26916. 'rtl.createHelper(this, "THelper", null, function () {',
  26917. ' this.DoIt = function (e) {',
  26918. ' this.set({});',
  26919. ' this.set(rtl.createSet($mod.TEnum.red));',
  26920. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26921. ' };',
  26922. ' this.Init = function (e) {',
  26923. ' this.set({});',
  26924. ' this.set(rtl.createSet(e));',
  26925. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26926. ' return this.get();',
  26927. ' };',
  26928. ' this.InitEmpty = function () {',
  26929. ' return this.get();',
  26930. ' };',
  26931. ' this.$new = function (fn, args) {',
  26932. ' return this[fn].apply({',
  26933. ' p: {},',
  26934. ' get: function () {',
  26935. ' return this.p;',
  26936. ' },',
  26937. ' set: function (v) {',
  26938. ' this.p = v;',
  26939. ' }',
  26940. ' }, args);',
  26941. ' };',
  26942. '});',
  26943. 'this.s = {};',
  26944. '']),
  26945. LinesToStr([ // $mod.$main
  26946. '$mod.THelper.DoIt.call({',
  26947. ' p: $mod,',
  26948. ' get: function () {',
  26949. ' return this.p.s;',
  26950. ' },',
  26951. ' set: function (v) {',
  26952. ' this.p.s = v;',
  26953. ' }',
  26954. '}, 123);',
  26955. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26956. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26957. ' p: $mod,',
  26958. ' get: function () {',
  26959. ' return this.p.s;',
  26960. ' },',
  26961. ' set: function (v) {',
  26962. ' this.p.s = v;',
  26963. ' }',
  26964. '}, $mod.TEnum.blue));',
  26965. '']));
  26966. end;
  26967. procedure TTestModule.TestTypeHelper_InterfaceType;
  26968. begin
  26969. StartProgram(false);
  26970. Add([
  26971. '{$interfaces com}',
  26972. '{$modeswitch typehelpers}',
  26973. 'type',
  26974. ' IUnknown = interface',
  26975. ' function _AddRef: longint;',
  26976. ' function _Release: longint;',
  26977. ' end;',
  26978. ' TObject = class(IUnknown)',
  26979. ' function _AddRef: longint; virtual; abstract;',
  26980. ' function _Release: longint; virtual; abstract;',
  26981. ' end;',
  26982. ' THelper = type helper for IUnknown',
  26983. ' procedure Fly(e: byte = 123);',
  26984. ' class procedure Run; static;',
  26985. ' end;',
  26986. 'var',
  26987. ' i: IUnknown;',
  26988. ' o: TObject;',
  26989. 'procedure THelper.Fly(e: byte);',
  26990. 'begin',
  26991. ' i:=Self;',
  26992. ' o:=Self as TObject;',
  26993. ' Self:=nil;',
  26994. ' Self:=i;',
  26995. ' Self:=o;',
  26996. ' with Self do begin',
  26997. ' Fly;',
  26998. ' Fly();',
  26999. ' end;',
  27000. 'end;',
  27001. 'class procedure THelper.Run;',
  27002. 'var l: IUnknown;',
  27003. 'begin',
  27004. ' l.Fly;',
  27005. ' l.Fly();',
  27006. 'end;',
  27007. 'begin',
  27008. ' i.Fly;',
  27009. ' i.Fly();',
  27010. ' i.Run;',
  27011. ' i.Run();',
  27012. ' IUnknown.Run;',
  27013. ' IUnknown.Run();',
  27014. '']);
  27015. ConvertProgram;
  27016. CheckSource('TestTypeHelper_InterfaceType',
  27017. LinesToStr([ // statements
  27018. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27019. 'rtl.createClass(this, "TObject", null, function () {',
  27020. ' this.$init = function () {',
  27021. ' };',
  27022. ' this.$final = function () {',
  27023. ' };',
  27024. ' rtl.addIntf(this, $mod.IUnknown);',
  27025. '});',
  27026. 'rtl.createHelper(this, "THelper", null, function () {',
  27027. ' this.Fly = function (e) {',
  27028. ' var $ir = rtl.createIntfRefs();',
  27029. ' try {',
  27030. ' rtl.setIntfP($mod, "i", this.get());',
  27031. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27032. ' this.set(null);',
  27033. ' this.set($mod.i);',
  27034. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27035. ' var $with = this.get();',
  27036. ' $mod.THelper.Fly.call(this, 123);',
  27037. ' $mod.THelper.Fly.call(this, 123);',
  27038. ' } finally {',
  27039. ' $ir.free();',
  27040. ' };',
  27041. ' };',
  27042. ' this.Run = function () {',
  27043. ' var l = null;',
  27044. ' try {',
  27045. ' $mod.THelper.Fly.call({',
  27046. ' get: function () {',
  27047. ' return l;',
  27048. ' },',
  27049. ' set: function (v) {',
  27050. ' l = rtl.setIntfL(l, v);',
  27051. ' }',
  27052. ' }, 123);',
  27053. ' $mod.THelper.Fly.call({',
  27054. ' get: function () {',
  27055. ' return l;',
  27056. ' },',
  27057. ' set: function (v) {',
  27058. ' l = rtl.setIntfL(l, v);',
  27059. ' }',
  27060. ' }, 123);',
  27061. ' } finally {',
  27062. ' rtl._Release(l);',
  27063. ' };',
  27064. ' };',
  27065. '});',
  27066. 'this.i = null;',
  27067. 'this.o = null;',
  27068. '']),
  27069. LinesToStr([ // $mod.$main
  27070. '$mod.THelper.Fly.call({',
  27071. ' p: $mod,',
  27072. ' get: function () {',
  27073. ' return this.p.i;',
  27074. ' },',
  27075. ' set: function (v) {',
  27076. ' rtl.setIntfP(this.p, "i", v);',
  27077. ' }',
  27078. '}, 123);',
  27079. '$mod.THelper.Fly.call({',
  27080. ' p: $mod,',
  27081. ' get: function () {',
  27082. ' return this.p.i;',
  27083. ' },',
  27084. ' set: function (v) {',
  27085. ' rtl.setIntfP(this.p, "i", v);',
  27086. ' }',
  27087. '}, 123);',
  27088. '$mod.THelper.Run();',
  27089. '$mod.THelper.Run();',
  27090. '$mod.THelper.Run();',
  27091. '$mod.THelper.Run();',
  27092. '']));
  27093. end;
  27094. procedure TTestModule.TestTypeHelper_NestedSelf;
  27095. begin
  27096. StartProgram(false);
  27097. Add([
  27098. '{$modeswitch typehelpers}',
  27099. 'type',
  27100. ' THelper = type helper for string',
  27101. ' procedure Run(Value: string);',
  27102. ' end;',
  27103. 'procedure THelper.Run(Value: string);',
  27104. ' function Sub(i: nativeint): boolean;',
  27105. ' begin',
  27106. ' Result:=Self[i+1]=Value[i];',
  27107. ' end;',
  27108. 'begin',
  27109. ' if Self[3]=Value[4] then ;',
  27110. 'end;',
  27111. 'begin',
  27112. '']);
  27113. ConvertProgram;
  27114. CheckSource('TestTypeHelper_NestedSelf',
  27115. LinesToStr([ // statements
  27116. 'rtl.createHelper(this, "THelper", null, function () {',
  27117. ' this.Run = function (Value) {',
  27118. ' var $Self = this;',
  27119. ' function Sub(i) {',
  27120. ' var Result = false;',
  27121. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27122. ' return Result;',
  27123. ' };',
  27124. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27125. ' };',
  27126. '});',
  27127. '']),
  27128. LinesToStr([ // $mod.$main
  27129. '']));
  27130. end;
  27131. procedure TTestModule.TestProcType;
  27132. begin
  27133. StartProgram(false);
  27134. Add([
  27135. 'type',
  27136. ' TProcInt = procedure(vI: longint = 1);',
  27137. 'procedure DoIt(vJ: longint);',
  27138. 'begin end;',
  27139. 'var',
  27140. ' b: boolean;',
  27141. ' vP, vQ: tprocint;',
  27142. 'begin',
  27143. ' vp:=nil;',
  27144. ' vp:=vp;',
  27145. ' vp:=@doit;',
  27146. ' vp;',
  27147. ' vp();',
  27148. ' vp(2);',
  27149. ' b:=vp=nil;',
  27150. ' b:=nil=vp;',
  27151. ' b:=vp=vq;',
  27152. ' b:=vp=@doit;',
  27153. ' b:=@doit=vp;',
  27154. ' b:=vp<>nil;',
  27155. ' b:=nil<>vp;',
  27156. ' b:=vp<>vq;',
  27157. ' b:=vp<>@doit;',
  27158. ' b:=@doit<>vp;',
  27159. ' b:=Assigned(vp);',
  27160. ' if Assigned(vp) then ;']);
  27161. ConvertProgram;
  27162. CheckSource('TestProcType',
  27163. LinesToStr([ // statements
  27164. 'this.DoIt = function(vJ) {',
  27165. '};',
  27166. 'this.b = false;',
  27167. 'this.vP = null;',
  27168. 'this.vQ = null;'
  27169. ]),
  27170. LinesToStr([ // $mod.$main
  27171. '$mod.vP = null;',
  27172. '$mod.vP = $mod.vP;',
  27173. '$mod.vP = $mod.DoIt;',
  27174. '$mod.vP(1);',
  27175. '$mod.vP(1);',
  27176. '$mod.vP(2);',
  27177. '$mod.b = $mod.vP === null;',
  27178. '$mod.b = null === $mod.vP;',
  27179. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27180. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27181. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27182. '$mod.b = $mod.vP !== null;',
  27183. '$mod.b = null !== $mod.vP;',
  27184. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27185. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27186. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27187. '$mod.b = $mod.vP != null;',
  27188. 'if ($mod.vP != null) ;',
  27189. '']));
  27190. end;
  27191. procedure TTestModule.TestProcType_Arg;
  27192. begin
  27193. StartProgram(false);
  27194. Add([
  27195. 'type',
  27196. ' TProcInt = procedure(vI: longint = 1);',
  27197. 'procedure DoIt(vJ: longint); begin end;',
  27198. 'procedure DoSome(vP, vQ: TProcInt);',
  27199. 'var',
  27200. ' b: boolean;',
  27201. 'begin',
  27202. ' vp:=nil;',
  27203. ' vp:=vp;',
  27204. ' vp:=@doit;',
  27205. ' vp;',
  27206. ' vp();',
  27207. ' vp(2);',
  27208. ' b:=vp=nil;',
  27209. ' b:=nil=vp;',
  27210. ' b:=vp=vq;',
  27211. ' b:=vp=@doit;',
  27212. ' b:=@doit=vp;',
  27213. ' b:=vp<>nil;',
  27214. ' b:=nil<>vp;',
  27215. ' b:=vp<>vq;',
  27216. ' b:=vp<>@doit;',
  27217. ' b:=@doit<>vp;',
  27218. ' b:=Assigned(vp);',
  27219. ' if Assigned(vp) then ;',
  27220. 'end;',
  27221. 'begin',
  27222. ' DoSome(@DoIt,nil);']);
  27223. ConvertProgram;
  27224. CheckSource('TestProcType_Arg',
  27225. LinesToStr([ // statements
  27226. 'this.DoIt = function(vJ) {',
  27227. '};',
  27228. 'this.DoSome = function(vP, vQ) {',
  27229. ' var b = false;',
  27230. ' vP = null;',
  27231. ' vP = vP;',
  27232. ' vP = $mod.DoIt;',
  27233. ' vP(1);',
  27234. ' vP(1);',
  27235. ' vP(2);',
  27236. ' b = vP === null;',
  27237. ' b = null === vP;',
  27238. ' b = rtl.eqCallback(vP,vQ);',
  27239. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27240. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27241. ' b = vP !== null;',
  27242. ' b = null !== vP;',
  27243. ' b = !rtl.eqCallback(vP, vQ);',
  27244. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27245. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27246. ' b = vP != null;',
  27247. ' if (vP != null) ;',
  27248. '};',
  27249. '']),
  27250. LinesToStr([ // $mod.$main
  27251. '$mod.DoSome($mod.DoIt,null);',
  27252. '']));
  27253. end;
  27254. procedure TTestModule.TestProcType_FunctionFPC;
  27255. begin
  27256. StartProgram(false);
  27257. Add('type');
  27258. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27259. Add('function DoIt(vI: longint): longint;');
  27260. Add('begin end;');
  27261. Add('var');
  27262. Add(' b: boolean;');
  27263. Add(' vP, vQ: tfuncint;');
  27264. Add('begin');
  27265. Add(' vp:=nil;');
  27266. Add(' vp:=vp;');
  27267. Add(' vp:=@doit;'); // ok in fpc and delphi
  27268. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27269. Add(' vp;'); // ok in fpc and delphi
  27270. Add(' vp();');
  27271. Add(' vp(2);');
  27272. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27273. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27274. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27275. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27276. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27277. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27278. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27279. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27280. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27281. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27282. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27283. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27284. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27285. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27286. Add(' b:=Assigned(vp);');
  27287. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27288. Add(' doit(vp());'); // ok in fpc and delphi
  27289. Add(' doit(vp(2));'); // ok in fpc and delphi
  27290. ConvertProgram;
  27291. CheckSource('TestProcType_FunctionFPC',
  27292. LinesToStr([ // statements
  27293. 'this.DoIt = function(vI) {',
  27294. ' var Result = 0;',
  27295. ' return Result;',
  27296. '};',
  27297. 'this.b = false;',
  27298. 'this.vP = null;',
  27299. 'this.vQ = null;'
  27300. ]),
  27301. LinesToStr([ // $mod.$main
  27302. '$mod.vP = null;',
  27303. '$mod.vP = $mod.vP;',
  27304. '$mod.vP = $mod.DoIt;',
  27305. '$mod.vP(1);',
  27306. '$mod.vP(1);',
  27307. '$mod.vP(2);',
  27308. '$mod.b = $mod.vP === null;',
  27309. '$mod.b = null === $mod.vP;',
  27310. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27311. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27312. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27313. '$mod.b = 4 === $mod.vP(1);',
  27314. '$mod.b = $mod.vP !== null;',
  27315. '$mod.b = null !== $mod.vP;',
  27316. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27317. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27318. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27319. '$mod.b = 6 !== $mod.vP(1);',
  27320. '$mod.b = $mod.vP != null;',
  27321. '$mod.DoIt($mod.vP(1));',
  27322. '$mod.DoIt($mod.vP(2));',
  27323. '']));
  27324. end;
  27325. procedure TTestModule.TestProcType_FunctionDelphi;
  27326. begin
  27327. StartProgram(false);
  27328. Add('{$mode Delphi}');
  27329. Add('type');
  27330. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27331. Add('function DoIt(vI: longint): longint;');
  27332. Add('begin end;');
  27333. Add('var');
  27334. Add(' b: boolean;');
  27335. Add(' vP, vQ: tfuncint;');
  27336. Add('begin');
  27337. Add(' vp:=nil;');
  27338. Add(' vp:=vp;');
  27339. Add(' vp:=@doit;'); // ok in fpc and delphi
  27340. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27341. Add(' vp;'); // ok in fpc and delphi
  27342. Add(' vp();');
  27343. Add(' vp(2);');
  27344. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27345. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27346. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27347. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27348. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27349. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27350. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27351. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27352. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27353. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27354. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27355. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27356. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27357. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27358. Add(' b:=Assigned(vp);');
  27359. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27360. Add(' doit(vp());'); // ok in fpc and delphi
  27361. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27362. ConvertProgram;
  27363. CheckSource('TestProcType_FunctionDelphi',
  27364. LinesToStr([ // statements
  27365. 'this.DoIt = function(vI) {',
  27366. ' var Result = 0;',
  27367. ' return Result;',
  27368. '};',
  27369. 'this.b = false;',
  27370. 'this.vP = null;',
  27371. 'this.vQ = null;'
  27372. ]),
  27373. LinesToStr([ // $mod.$main
  27374. '$mod.vP = null;',
  27375. '$mod.vP = $mod.vP;',
  27376. '$mod.vP = $mod.DoIt;',
  27377. '$mod.vP = $mod.DoIt;',
  27378. '$mod.vP(1);',
  27379. '$mod.vP(1);',
  27380. '$mod.vP(2);',
  27381. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27382. '$mod.b = $mod.vP(1) === 3;',
  27383. '$mod.b = 4 === $mod.vP(1);',
  27384. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27385. '$mod.b = $mod.vP(1) !== 5;',
  27386. '$mod.b = 6 !== $mod.vP(1);',
  27387. '$mod.b = $mod.vP != null;',
  27388. '$mod.DoIt($mod.vP(1));',
  27389. '$mod.DoIt($mod.vP(1));',
  27390. '$mod.DoIt($mod.vP(2));',
  27391. '']));
  27392. end;
  27393. procedure TTestModule.TestProcType_ProcedureDelphi;
  27394. begin
  27395. StartProgram(false);
  27396. Add('{$mode Delphi}');
  27397. Add('type');
  27398. Add(' TProc = procedure;');
  27399. Add('procedure DoIt;');
  27400. Add('begin end;');
  27401. Add('var');
  27402. Add(' b: boolean;');
  27403. Add(' vP, vQ: tproc;');
  27404. Add('begin');
  27405. Add(' vp:=nil;');
  27406. Add(' vp:=vp;');
  27407. Add(' vp:=vq;');
  27408. 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
  27409. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27410. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27411. Add(' vp;'); // ok in fpc and delphi
  27412. Add(' vp();');
  27413. // equal
  27414. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27415. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27416. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27417. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27418. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27419. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27420. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27421. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27422. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27423. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27424. // unequal
  27425. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27426. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27427. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27428. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27429. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27430. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27431. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27432. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27433. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27434. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27435. Add(' b:=Assigned(vp);');
  27436. ConvertProgram;
  27437. CheckSource('TestProcType_ProcedureDelphi',
  27438. LinesToStr([ // statements
  27439. 'this.DoIt = function() {',
  27440. '};',
  27441. 'this.b = false;',
  27442. 'this.vP = null;',
  27443. 'this.vQ = null;'
  27444. ]),
  27445. LinesToStr([ // $mod.$main
  27446. '$mod.vP = null;',
  27447. '$mod.vP = $mod.vP;',
  27448. '$mod.vP = $mod.vQ;',
  27449. '$mod.vP = $mod.DoIt;',
  27450. '$mod.vP = $mod.DoIt;',
  27451. '$mod.vP();',
  27452. '$mod.vP();',
  27453. '$mod.b = $mod.vP === null;',
  27454. '$mod.b = null === $mod.vP;',
  27455. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27456. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27457. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27458. '$mod.b = $mod.vP !== null;',
  27459. '$mod.b = null !== $mod.vP;',
  27460. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27461. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27462. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27463. '$mod.b = $mod.vP != null;',
  27464. '']));
  27465. end;
  27466. procedure TTestModule.TestProcType_AsParam;
  27467. begin
  27468. StartProgram(false);
  27469. Add('type');
  27470. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27471. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27472. Add('var vJ: tfuncint;');
  27473. Add('begin');
  27474. Add(' vg:=vg;');
  27475. Add(' vj:=vh;');
  27476. Add(' vi:=vi;');
  27477. Add(' doit(vg,vg,vg);');
  27478. Add(' doit(vh,vh,vj);');
  27479. Add(' doit(vi,vi,vi);');
  27480. Add(' doit(vj,vj,vj);');
  27481. Add('end;');
  27482. Add('var i: tfuncint;');
  27483. Add('begin');
  27484. Add(' doit(i,i,i);');
  27485. ConvertProgram;
  27486. CheckSource('TestProcType_AsParam',
  27487. LinesToStr([ // statements
  27488. 'this.DoIt = function (vG,vH,vI) {',
  27489. ' var vJ = null;',
  27490. ' vG = vG;',
  27491. ' vJ = vH;',
  27492. ' vI.set(vI.get());',
  27493. ' $mod.DoIt(vG, vG, {',
  27494. ' get: function () {',
  27495. ' return vG;',
  27496. ' },',
  27497. ' set: function (v) {',
  27498. ' vG = v;',
  27499. ' }',
  27500. ' });',
  27501. ' $mod.DoIt(vH, vH, {',
  27502. ' get: function () {',
  27503. ' return vJ;',
  27504. ' },',
  27505. ' set: function (v) {',
  27506. ' vJ = v;',
  27507. ' }',
  27508. ' });',
  27509. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27510. ' $mod.DoIt(vJ, vJ, {',
  27511. ' get: function () {',
  27512. ' return vJ;',
  27513. ' },',
  27514. ' set: function (v) {',
  27515. ' vJ = v;',
  27516. ' }',
  27517. ' });',
  27518. '};',
  27519. 'this.i = null;'
  27520. ]),
  27521. LinesToStr([
  27522. '$mod.DoIt($mod.i,$mod.i,{',
  27523. ' p: $mod,',
  27524. ' get: function () {',
  27525. ' return this.p.i;',
  27526. ' },',
  27527. ' set: function (v) {',
  27528. ' this.p.i = v;',
  27529. ' }',
  27530. '});'
  27531. ]));
  27532. end;
  27533. procedure TTestModule.TestProcType_MethodFPC;
  27534. begin
  27535. StartProgram(false);
  27536. Add('type');
  27537. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27538. Add(' TObject = class');
  27539. Add(' function DoIt(vA: longint = 1): longint;');
  27540. Add(' end;');
  27541. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27542. Add('begin');
  27543. Add('end;');
  27544. Add('var');
  27545. Add(' Obj: TObject;');
  27546. Add(' vP: tfuncint;');
  27547. Add(' b: boolean;');
  27548. Add('begin');
  27549. Add(' vp:[email protected];'); // ok in fpc and delphi
  27550. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27551. Add(' vp;'); // ok in fpc and delphi
  27552. Add(' vp();');
  27553. Add(' vp(2);');
  27554. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27555. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27556. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27557. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27558. ConvertProgram;
  27559. CheckSource('TestProcType_MethodFPC',
  27560. LinesToStr([ // statements
  27561. 'rtl.createClass(this, "TObject", null, function () {',
  27562. ' this.$init = function () {',
  27563. ' };',
  27564. ' this.$final = function () {',
  27565. ' };',
  27566. ' this.DoIt = function (vA) {',
  27567. ' var Result = 0;',
  27568. ' return Result;',
  27569. ' };',
  27570. '});',
  27571. 'this.Obj = null;',
  27572. 'this.vP = null;',
  27573. 'this.b = false;'
  27574. ]),
  27575. LinesToStr([
  27576. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27577. '$mod.vP(1);',
  27578. '$mod.vP(1);',
  27579. '$mod.vP(2);',
  27580. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27581. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27582. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27583. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27584. '']));
  27585. end;
  27586. procedure TTestModule.TestProcType_MethodDelphi;
  27587. begin
  27588. StartProgram(false);
  27589. Add([
  27590. '{$mode delphi}',
  27591. 'type',
  27592. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27593. ' TObject = class',
  27594. ' function DoIt(vA: longint = 1): longint;',
  27595. ' end;',
  27596. 'function TObject.DoIt(vA: longint = 1): longint;',
  27597. 'begin',
  27598. 'end;',
  27599. 'var',
  27600. ' Obj: TObject;',
  27601. ' vP: tfuncint;',
  27602. ' b: boolean;',
  27603. 'begin',
  27604. ' vp:[email protected];', // ok in fpc and delphi
  27605. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27606. ' vp;', // ok in fpc and delphi
  27607. ' vp();',
  27608. ' vp(2);',
  27609. //' b:[email protected];', // ok in fpc, illegal in delphi
  27610. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27611. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27612. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27613. '']);
  27614. ConvertProgram;
  27615. CheckSource('TestProcType_MethodDelphi',
  27616. LinesToStr([ // statements
  27617. 'rtl.createClass(this, "TObject", null, function () {',
  27618. ' this.$init = function () {',
  27619. ' };',
  27620. ' this.$final = function () {',
  27621. ' };',
  27622. ' this.DoIt = function (vA) {',
  27623. ' var Result = 0;',
  27624. ' return Result;',
  27625. ' };',
  27626. '});',
  27627. 'this.Obj = null;',
  27628. 'this.vP = null;',
  27629. 'this.b = false;'
  27630. ]),
  27631. LinesToStr([
  27632. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27633. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27634. '$mod.vP(1);',
  27635. '$mod.vP(1);',
  27636. '$mod.vP(2);',
  27637. '']));
  27638. end;
  27639. procedure TTestModule.TestProcType_PropertyFPC;
  27640. begin
  27641. StartProgram(false);
  27642. Add('type');
  27643. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27644. Add(' TObject = class');
  27645. Add(' FOnFoo: TFuncInt;');
  27646. Add(' function DoIt(vA: longint = 1): longint;');
  27647. Add(' function GetFoo: TFuncInt;');
  27648. Add(' procedure SetFoo(const Value: TFuncInt);');
  27649. Add(' function GetEvents(Index: longint): TFuncInt;');
  27650. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27651. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27652. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27653. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27654. Add(' end;');
  27655. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27656. Add('function tobject.getfoo: tfuncint; begin end;');
  27657. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27658. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27659. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27660. Add('var');
  27661. Add(' Obj: TObject;');
  27662. Add(' vP: tfuncint;');
  27663. Add(' b: boolean;');
  27664. Add('begin');
  27665. Add(' obj.onfoo:=nil;');
  27666. Add(' obj.onbar:=nil;');
  27667. Add(' obj.events[1]:=nil;');
  27668. Add(' obj.onfoo:=obj.onfoo;');
  27669. Add(' obj.onbar:=obj.onbar;');
  27670. Add(' obj.events[2]:=obj.events[3];');
  27671. Add(' obj.onfoo:[email protected];');
  27672. Add(' obj.onbar:[email protected];');
  27673. Add(' obj.events[4]:[email protected];');
  27674. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27675. //Add(' obj.onbar:=obj.doit;'); // delphi
  27676. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27677. Add(' obj.onfoo;');
  27678. Add(' obj.onbar;');
  27679. //Add(' obj.events[5];'); ToDo in pasresolver
  27680. Add(' obj.onfoo();');
  27681. Add(' obj.onbar();');
  27682. Add(' obj.events[6]();');
  27683. Add(' b:=obj.onfoo=nil;');
  27684. Add(' b:=obj.onbar=nil;');
  27685. Add(' b:=obj.events[7]=nil;');
  27686. Add(' b:=obj.onfoo<>nil;');
  27687. Add(' b:=obj.onbar<>nil;');
  27688. Add(' b:=obj.events[8]<>nil;');
  27689. Add(' b:=obj.onfoo=vp;');
  27690. Add(' b:=obj.onbar=vp;');
  27691. Add(' b:=obj.events[9]=vp;');
  27692. Add(' b:=obj.onfoo=obj.onfoo;');
  27693. Add(' b:=obj.onbar=obj.onfoo;');
  27694. Add(' b:=obj.events[10]=obj.onfoo;');
  27695. Add(' b:=obj.onfoo<>obj.onfoo;');
  27696. Add(' b:=obj.onbar<>obj.onfoo;');
  27697. Add(' b:=obj.events[11]<>obj.onfoo;');
  27698. Add(' b:[email protected];');
  27699. Add(' b:[email protected];');
  27700. Add(' b:=obj.events[12][email protected];');
  27701. Add(' b:=obj.onfoo<>@obj.doit;');
  27702. Add(' b:=obj.onbar<>@obj.doit;');
  27703. Add(' b:=obj.events[12]<>@obj.doit;');
  27704. Add(' b:=Assigned(obj.onfoo);');
  27705. Add(' b:=Assigned(obj.onbar);');
  27706. Add(' b:=Assigned(obj.events[13]);');
  27707. ConvertProgram;
  27708. CheckSource('TestProcType_PropertyFPC',
  27709. LinesToStr([ // statements
  27710. 'rtl.createClass(this, "TObject", null, function () {',
  27711. ' this.$init = function () {',
  27712. ' this.FOnFoo = null;',
  27713. ' };',
  27714. ' this.$final = function () {',
  27715. ' this.FOnFoo = undefined;',
  27716. ' };',
  27717. ' this.DoIt = function (vA) {',
  27718. ' var Result = 0;',
  27719. ' return Result;',
  27720. ' };',
  27721. 'this.GetFoo = function () {',
  27722. ' var Result = null;',
  27723. ' return Result;',
  27724. '};',
  27725. 'this.SetFoo = function (Value) {',
  27726. '};',
  27727. 'this.GetEvents = function (Index) {',
  27728. ' var Result = null;',
  27729. ' return Result;',
  27730. '};',
  27731. 'this.SetEvents = function (Index, Value) {',
  27732. '};',
  27733. '});',
  27734. 'this.Obj = null;',
  27735. 'this.vP = null;',
  27736. 'this.b = false;'
  27737. ]),
  27738. LinesToStr([
  27739. '$mod.Obj.FOnFoo = null;',
  27740. '$mod.Obj.SetFoo(null);',
  27741. '$mod.Obj.SetEvents(1, null);',
  27742. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27743. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27744. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27745. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27746. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27747. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27748. '$mod.Obj.FOnFoo(1);',
  27749. '$mod.Obj.GetFoo();',
  27750. '$mod.Obj.FOnFoo(1);',
  27751. '$mod.Obj.GetFoo()(1);',
  27752. '$mod.Obj.GetEvents(6)(1);',
  27753. '$mod.b = $mod.Obj.FOnFoo === null;',
  27754. '$mod.b = $mod.Obj.GetFoo() === null;',
  27755. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27756. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27757. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27758. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27759. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27760. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27761. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27762. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27763. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27764. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27765. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27766. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27767. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27768. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27769. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27770. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27771. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27772. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27773. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27774. '$mod.b = $mod.Obj.FOnFoo != null;',
  27775. '$mod.b = $mod.Obj.GetFoo() != null;',
  27776. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27777. '']));
  27778. end;
  27779. procedure TTestModule.TestProcType_PropertyDelphi;
  27780. begin
  27781. StartProgram(false);
  27782. Add('{$mode delphi}');
  27783. Add('type');
  27784. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27785. Add(' TObject = class');
  27786. Add(' FOnFoo: TFuncInt;');
  27787. Add(' function DoIt(vA: longint = 1): longint;');
  27788. Add(' function GetFoo: TFuncInt;');
  27789. Add(' procedure SetFoo(const Value: TFuncInt);');
  27790. Add(' function GetEvents(Index: longint): TFuncInt;');
  27791. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27792. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27793. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27794. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27795. Add(' end;');
  27796. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27797. Add('function tobject.getfoo: tfuncint; begin end;');
  27798. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27799. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27800. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27801. Add('var');
  27802. Add(' Obj: TObject;');
  27803. Add(' vP: tfuncint;');
  27804. Add(' b: boolean;');
  27805. Add('begin');
  27806. Add(' obj.onfoo:=nil;');
  27807. Add(' obj.onbar:=nil;');
  27808. Add(' obj.events[1]:=nil;');
  27809. Add(' obj.onfoo:=obj.onfoo;');
  27810. Add(' obj.onbar:=obj.onbar;');
  27811. Add(' obj.events[2]:=obj.events[3];');
  27812. Add(' obj.onfoo:[email protected];');
  27813. Add(' obj.onbar:[email protected];');
  27814. Add(' obj.events[4]:[email protected];');
  27815. Add(' obj.onfoo:=obj.doit;'); // delphi
  27816. Add(' obj.onbar:=obj.doit;'); // delphi
  27817. Add(' obj.events[4]:=obj.doit;'); // delphi
  27818. Add(' obj.onfoo;');
  27819. Add(' obj.onbar;');
  27820. //Add(' obj.events[5];'); ToDo in pasresolver
  27821. Add(' obj.onfoo();');
  27822. Add(' obj.onbar();');
  27823. Add(' obj.events[6]();');
  27824. //Add(' b:=obj.onfoo=nil;'); // fpc
  27825. //Add(' b:=obj.onbar=nil;'); // fpc
  27826. //Add(' b:=obj.events[7]=nil;'); // fpc
  27827. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27828. //Add(' b:=obj.onbar<>nil;'); // fpc
  27829. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27830. Add(' b:=obj.onfoo=vp;');
  27831. Add(' b:=obj.onbar=vp;');
  27832. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27833. Add(' b:=obj.onfoo=obj.onfoo;');
  27834. Add(' b:=obj.onbar=obj.onfoo;');
  27835. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27836. Add(' b:=obj.onfoo<>obj.onfoo;');
  27837. Add(' b:=obj.onbar<>obj.onfoo;');
  27838. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27839. //Add(' b:[email protected];'); // fpc
  27840. //Add(' b:[email protected];'); // fpc
  27841. //Add(' b:=obj.events[12][email protected];'); // fpc
  27842. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27843. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27844. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27845. Add(' b:=Assigned(obj.onfoo);');
  27846. Add(' b:=Assigned(obj.onbar);');
  27847. Add(' b:=Assigned(obj.events[13]);');
  27848. ConvertProgram;
  27849. CheckSource('TestProcType_PropertyDelphi',
  27850. LinesToStr([ // statements
  27851. 'rtl.createClass(this, "TObject", null, function () {',
  27852. ' this.$init = function () {',
  27853. ' this.FOnFoo = null;',
  27854. ' };',
  27855. ' this.$final = function () {',
  27856. ' this.FOnFoo = undefined;',
  27857. ' };',
  27858. ' this.DoIt = function (vA) {',
  27859. ' var Result = 0;',
  27860. ' return Result;',
  27861. ' };',
  27862. 'this.GetFoo = function () {',
  27863. ' var Result = null;',
  27864. ' return Result;',
  27865. '};',
  27866. 'this.SetFoo = function (Value) {',
  27867. '};',
  27868. 'this.GetEvents = function (Index) {',
  27869. ' var Result = null;',
  27870. ' return Result;',
  27871. '};',
  27872. 'this.SetEvents = function (Index, Value) {',
  27873. '};',
  27874. '});',
  27875. 'this.Obj = null;',
  27876. 'this.vP = null;',
  27877. 'this.b = false;'
  27878. ]),
  27879. LinesToStr([
  27880. '$mod.Obj.FOnFoo = null;',
  27881. '$mod.Obj.SetFoo(null);',
  27882. '$mod.Obj.SetEvents(1, null);',
  27883. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27884. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27885. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27886. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27887. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27888. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27889. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27890. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27891. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27892. '$mod.Obj.FOnFoo(1);',
  27893. '$mod.Obj.GetFoo();',
  27894. '$mod.Obj.FOnFoo(1);',
  27895. '$mod.Obj.GetFoo()(1);',
  27896. '$mod.Obj.GetEvents(6)(1);',
  27897. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27898. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27899. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27900. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27901. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27902. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27903. '$mod.b = $mod.Obj.FOnFoo != null;',
  27904. '$mod.b = $mod.Obj.GetFoo() != null;',
  27905. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27906. '']));
  27907. end;
  27908. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27909. begin
  27910. StartProgram(false);
  27911. Add('type');
  27912. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27913. Add(' TObject = class');
  27914. Add(' FOnFoo: TFuncInt;');
  27915. Add(' function DoIt(vA: longint = 1): longint;');
  27916. Add(' function GetFoo: TFuncInt;');
  27917. Add(' procedure SetFoo(const Value: TFuncInt);');
  27918. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27919. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27920. Add(' end;');
  27921. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27922. Add('function tobject.getfoo: tfuncint; begin end;');
  27923. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27924. Add('var');
  27925. Add(' Obj: TObject;');
  27926. Add(' vP: tfuncint;');
  27927. Add(' b: boolean;');
  27928. Add('begin');
  27929. Add('with obj do begin');
  27930. Add(' fonfoo:=nil;');
  27931. Add(' onfoo:=nil;');
  27932. Add(' onbar:=nil;');
  27933. Add(' fonfoo:=fonfoo;');
  27934. Add(' onfoo:=onfoo;');
  27935. Add(' onbar:=onbar;');
  27936. Add(' fonfoo:=@doit;');
  27937. Add(' onfoo:=@doit;');
  27938. Add(' onbar:=@doit;');
  27939. //Add(' fonfoo:=doit;'); // delphi
  27940. //Add(' onfoo:=doit;'); // delphi
  27941. //Add(' onbar:=doit;'); // delphi
  27942. Add(' fonfoo;');
  27943. Add(' onfoo;');
  27944. Add(' onbar;');
  27945. Add(' fonfoo();');
  27946. Add(' onfoo();');
  27947. Add(' onbar();');
  27948. Add(' b:=fonfoo=nil;');
  27949. Add(' b:=onfoo=nil;');
  27950. Add(' b:=onbar=nil;');
  27951. Add(' b:=fonfoo<>nil;');
  27952. Add(' b:=onfoo<>nil;');
  27953. Add(' b:=onbar<>nil;');
  27954. Add(' b:=fonfoo=vp;');
  27955. Add(' b:=onfoo=vp;');
  27956. Add(' b:=onbar=vp;');
  27957. Add(' b:=fonfoo=fonfoo;');
  27958. Add(' b:=onfoo=onfoo;');
  27959. Add(' b:=onbar=onfoo;');
  27960. Add(' b:=fonfoo<>fonfoo;');
  27961. Add(' b:=onfoo<>onfoo;');
  27962. Add(' b:=onbar<>onfoo;');
  27963. Add(' b:=fonfoo=@doit;');
  27964. Add(' b:=onfoo=@doit;');
  27965. Add(' b:=onbar=@doit;');
  27966. Add(' b:=fonfoo<>@doit;');
  27967. Add(' b:=onfoo<>@doit;');
  27968. Add(' b:=onbar<>@doit;');
  27969. Add(' b:=Assigned(fonfoo);');
  27970. Add(' b:=Assigned(onfoo);');
  27971. Add(' b:=Assigned(onbar);');
  27972. Add('end;');
  27973. ConvertProgram;
  27974. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27975. LinesToStr([ // statements
  27976. 'rtl.createClass(this, "TObject", null, function () {',
  27977. ' this.$init = function () {',
  27978. ' this.FOnFoo = null;',
  27979. ' };',
  27980. ' this.$final = function () {',
  27981. ' this.FOnFoo = undefined;',
  27982. ' };',
  27983. ' this.DoIt = function (vA) {',
  27984. ' var Result = 0;',
  27985. ' return Result;',
  27986. ' };',
  27987. ' this.GetFoo = function () {',
  27988. ' var Result = null;',
  27989. ' return Result;',
  27990. ' };',
  27991. ' this.SetFoo = function (Value) {',
  27992. ' };',
  27993. '});',
  27994. 'this.Obj = null;',
  27995. 'this.vP = null;',
  27996. 'this.b = false;'
  27997. ]),
  27998. LinesToStr([
  27999. 'var $with = $mod.Obj;',
  28000. '$with.FOnFoo = null;',
  28001. '$with.FOnFoo = null;',
  28002. '$with.SetFoo(null);',
  28003. '$with.FOnFoo = $with.FOnFoo;',
  28004. '$with.FOnFoo = $with.FOnFoo;',
  28005. '$with.SetFoo($with.GetFoo());',
  28006. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28007. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28008. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28009. '$with.FOnFoo(1);',
  28010. '$with.FOnFoo(1);',
  28011. '$with.GetFoo();',
  28012. '$with.FOnFoo(1);',
  28013. '$with.FOnFoo(1);',
  28014. '$with.GetFoo()(1);',
  28015. '$mod.b = $with.FOnFoo === null;',
  28016. '$mod.b = $with.FOnFoo === null;',
  28017. '$mod.b = $with.GetFoo() === null;',
  28018. '$mod.b = $with.FOnFoo !== null;',
  28019. '$mod.b = $with.FOnFoo !== null;',
  28020. '$mod.b = $with.GetFoo() !== null;',
  28021. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28022. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28023. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28024. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28025. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28026. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28027. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28028. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28029. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28030. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28031. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28032. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28033. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28034. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28035. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28036. '$mod.b = $with.FOnFoo != null;',
  28037. '$mod.b = $with.FOnFoo != null;',
  28038. '$mod.b = $with.GetFoo() != null;',
  28039. '']));
  28040. end;
  28041. procedure TTestModule.TestProcType_Nested;
  28042. begin
  28043. StartProgram(false);
  28044. Add([
  28045. 'type',
  28046. ' TProcInt = procedure(vI: longint = 1);',
  28047. 'procedure DoIt(vJ: longint);',
  28048. 'var aProc: TProcInt;',
  28049. ' b: boolean;',
  28050. ' procedure Sub(vK: longint);',
  28051. ' var aSub: TProcInt;',
  28052. ' procedure SubSub(vK: longint);',
  28053. ' var aSubSub: TProcInt;',
  28054. ' begin;',
  28055. ' aProc:=@DoIt;',
  28056. ' aSub:=@DoIt;',
  28057. ' aSubSub:=@DoIt;',
  28058. ' aProc:=@Sub;',
  28059. ' aSub:=@Sub;',
  28060. ' aSubSub:=@Sub;',
  28061. ' aProc:=@SubSub;',
  28062. ' aSub:=@SubSub;',
  28063. ' aSubSub:=@SubSub;',
  28064. ' end;',
  28065. ' begin;',
  28066. ' end;',
  28067. 'begin;',
  28068. ' aProc:=@Sub;',
  28069. ' b:=aProc=@Sub;',
  28070. ' b:=@Sub=aProc;',
  28071. 'end;',
  28072. 'begin',
  28073. '']);
  28074. ConvertProgram;
  28075. CheckSource('TestProcType_Nested',
  28076. LinesToStr([ // statements
  28077. 'this.DoIt = function (vJ) {',
  28078. ' var aProc = null;',
  28079. ' var b = false;',
  28080. ' function Sub(vK) {',
  28081. ' var aSub = null;',
  28082. ' function SubSub(vK) {',
  28083. ' var aSubSub = null;',
  28084. ' aProc = $mod.DoIt;',
  28085. ' aSub = $mod.DoIt;',
  28086. ' aSubSub = $mod.DoIt;',
  28087. ' aProc = Sub;',
  28088. ' aSub = Sub;',
  28089. ' aSubSub = Sub;',
  28090. ' aProc = SubSub;',
  28091. ' aSub = SubSub;',
  28092. ' aSubSub = SubSub;',
  28093. ' };',
  28094. ' };',
  28095. ' aProc = Sub;',
  28096. ' b = rtl.eqCallback(aProc, Sub);',
  28097. ' b = rtl.eqCallback(Sub, aProc);',
  28098. '};',
  28099. '']),
  28100. LinesToStr([ // $mod.$main
  28101. '']));
  28102. end;
  28103. procedure TTestModule.TestProcType_NestedOfObject;
  28104. begin
  28105. StartProgram(false);
  28106. Add([
  28107. 'type',
  28108. ' TProcInt = procedure(vI: longint = 1) of object;',
  28109. ' TObject = class',
  28110. ' procedure DoIt(vJ: longint);',
  28111. ' end;',
  28112. 'procedure TObject.DoIt(vJ: longint);',
  28113. 'var aProc: TProcInt;',
  28114. ' b: boolean;',
  28115. ' procedure Sub(vK: longint);',
  28116. ' var aSub: TProcInt;',
  28117. ' procedure SubSub(vK: longint);',
  28118. ' var aSubSub: TProcInt;',
  28119. ' begin;',
  28120. ' aProc:=@DoIt;',
  28121. ' aSub:=@DoIt;',
  28122. ' aSubSub:=@DoIt;',
  28123. ' aProc:=@Sub;',
  28124. ' aSub:=@Sub;',
  28125. ' aSubSub:=@Sub;',
  28126. ' aProc:=@SubSub;',
  28127. ' aSub:=@SubSub;',
  28128. ' aSubSub:=@SubSub;',
  28129. ' end;',
  28130. ' begin;',
  28131. ' end;',
  28132. 'begin;',
  28133. ' aProc:=@Sub;',
  28134. ' b:=aProc=@Sub;',
  28135. ' b:=@Sub=aProc;',
  28136. 'end;',
  28137. 'begin',
  28138. '']);
  28139. ConvertProgram;
  28140. CheckSource('TestProcType_Nested',
  28141. LinesToStr([ // statements
  28142. 'rtl.createClass(this, "TObject", null, function () {',
  28143. ' this.$init = function () {',
  28144. ' };',
  28145. ' this.$final = function () {',
  28146. ' };',
  28147. ' this.DoIt = function (vJ) {',
  28148. ' var $Self = this;',
  28149. ' var aProc = null;',
  28150. ' var b = false;',
  28151. ' function Sub(vK) {',
  28152. ' var aSub = null;',
  28153. ' function SubSub(vK) {',
  28154. ' var aSubSub = null;',
  28155. ' aProc = rtl.createCallback($Self, "DoIt");',
  28156. ' aSub = rtl.createCallback($Self, "DoIt");',
  28157. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28158. ' aProc = Sub;',
  28159. ' aSub = Sub;',
  28160. ' aSubSub = Sub;',
  28161. ' aProc = SubSub;',
  28162. ' aSub = SubSub;',
  28163. ' aSubSub = SubSub;',
  28164. ' };',
  28165. ' };',
  28166. ' aProc = Sub;',
  28167. ' b = rtl.eqCallback(aProc, Sub);',
  28168. ' b = rtl.eqCallback(Sub, aProc);',
  28169. ' };',
  28170. '});',
  28171. '']),
  28172. LinesToStr([ // $mod.$main
  28173. '']));
  28174. end;
  28175. procedure TTestModule.TestProcType_ReferenceToProc;
  28176. begin
  28177. StartProgram(false);
  28178. Add([
  28179. 'type',
  28180. ' TProcRef = reference to procedure(i: longint = 0);',
  28181. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28182. 'var',
  28183. ' p: TProcRef;',
  28184. ' f: TFuncRef;',
  28185. 'procedure DoIt(i: longint);',
  28186. 'begin',
  28187. 'end;',
  28188. 'function GetIt(i: longint): longint;',
  28189. 'begin',
  28190. ' p:=@DoIt;',
  28191. ' f:=@GetIt;',
  28192. ' f;',
  28193. ' f();',
  28194. ' f(1);',
  28195. 'end;',
  28196. 'begin',
  28197. ' p:=@DoIt;',
  28198. ' f:=@GetIt;',
  28199. ' f;',
  28200. ' f();',
  28201. ' f(1);',
  28202. ' p:=TProcRef(f);',
  28203. '']);
  28204. ConvertProgram;
  28205. CheckSource('TestProcType_ReferenceToProc',
  28206. LinesToStr([ // statements
  28207. 'this.p = null;',
  28208. 'this.f = null;',
  28209. 'this.DoIt = function (i) {',
  28210. '};',
  28211. 'this.GetIt = function (i) {',
  28212. ' var Result = 0;',
  28213. ' $mod.p = $mod.DoIt;',
  28214. ' $mod.f = $mod.GetIt;',
  28215. ' $mod.f(0);',
  28216. ' $mod.f(0);',
  28217. ' $mod.f(1);',
  28218. ' return Result;',
  28219. '};',
  28220. '']),
  28221. LinesToStr([ // $mod.$main
  28222. '$mod.p = $mod.DoIt;',
  28223. '$mod.f = $mod.GetIt;',
  28224. '$mod.f(0);',
  28225. '$mod.f(0);',
  28226. '$mod.f(1);',
  28227. '$mod.p = $mod.f;',
  28228. '']));
  28229. end;
  28230. procedure TTestModule.TestProcType_ReferenceToMethod;
  28231. begin
  28232. StartProgram(false);
  28233. Add([
  28234. 'type',
  28235. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28236. ' TObject = class',
  28237. ' function Grow(s: longint): longint;',
  28238. ' end;',
  28239. 'var',
  28240. ' f: tfuncref;',
  28241. 'function tobject.grow(s: longint): longint;',
  28242. ' function GrowSub(i: longint): longint;',
  28243. ' begin',
  28244. ' f:=@grow;',
  28245. ' f:=@growsub;',
  28246. ' end;',
  28247. 'begin',
  28248. ' f:=@grow;',
  28249. ' f:=@growsub;',
  28250. 'end;',
  28251. 'begin',
  28252. '']);
  28253. ConvertProgram;
  28254. CheckSource('TestProcType_ReferenceToMethod',
  28255. LinesToStr([ // statements
  28256. 'rtl.createClass(this, "TObject", null, function () {',
  28257. ' this.$init = function () {',
  28258. ' };',
  28259. ' this.$final = function () {',
  28260. ' };',
  28261. ' this.Grow = function (s) {',
  28262. ' var $Self = this;',
  28263. ' var Result = 0;',
  28264. ' function GrowSub(i) {',
  28265. ' var Result = 0;',
  28266. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28267. ' $mod.f = GrowSub;',
  28268. ' return Result;',
  28269. ' };',
  28270. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28271. ' $mod.f = GrowSub;',
  28272. ' return Result;',
  28273. ' };',
  28274. '});',
  28275. 'this.f = null;',
  28276. '']),
  28277. LinesToStr([ // $mod.$main
  28278. '']));
  28279. end;
  28280. procedure TTestModule.TestProcType_Typecast;
  28281. begin
  28282. StartProgram(false);
  28283. Add([
  28284. 'type',
  28285. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28286. ' TEvent = procedure of object;',
  28287. ' TGetter = function:longint of object;',
  28288. ' TProcA = procedure(i: longint);',
  28289. ' TFuncB = function(i, j: longint): longint;',
  28290. 'procedure DoIt(); varargs; begin end;',
  28291. 'var',
  28292. ' Notify: tnotifyevent;',
  28293. ' Event: tevent;',
  28294. ' Getter: tgetter;',
  28295. ' ProcA: tproca;',
  28296. ' FuncB: tfuncb;',
  28297. ' p: pointer;',
  28298. 'begin',
  28299. ' notify:=tnotifyevent(event);',
  28300. ' event:=tevent(event);',
  28301. ' event:=tevent(notify);',
  28302. ' event:=tevent(getter);',
  28303. ' event:=tevent(proca);',
  28304. ' proca:=tproca(funcb);',
  28305. ' funcb:=tfuncb(funcb);',
  28306. ' funcb:=tfuncb(proca);',
  28307. ' funcb:=tfuncb(getter);',
  28308. ' proca:=tproca(p);',
  28309. ' funcb:=tfuncb(p);',
  28310. ' getter:=tgetter(p);',
  28311. ' p:=pointer(notify);',
  28312. ' p:=notify;',
  28313. ' p:=pointer(proca);',
  28314. ' p:=proca;',
  28315. ' p:=pointer(funcb);',
  28316. ' p:=funcb;',
  28317. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28318. '']);
  28319. ConvertProgram;
  28320. CheckSource('TestProcType_Typecast',
  28321. LinesToStr([ // statements
  28322. 'this.DoIt = function () {',
  28323. '};',
  28324. 'this.Notify = null;',
  28325. 'this.Event = null;',
  28326. 'this.Getter = null;',
  28327. 'this.ProcA = null;',
  28328. 'this.FuncB = null;',
  28329. 'this.p = null;',
  28330. '']),
  28331. LinesToStr([ // $mod.$main
  28332. '$mod.Notify = $mod.Event;',
  28333. '$mod.Event = $mod.Event;',
  28334. '$mod.Event = $mod.Notify;',
  28335. '$mod.Event = $mod.Getter;',
  28336. '$mod.Event = $mod.ProcA;',
  28337. '$mod.ProcA = $mod.FuncB;',
  28338. '$mod.FuncB = $mod.FuncB;',
  28339. '$mod.FuncB = $mod.ProcA;',
  28340. '$mod.FuncB = $mod.Getter;',
  28341. '$mod.ProcA = $mod.p;',
  28342. '$mod.FuncB = $mod.p;',
  28343. '$mod.Getter = $mod.p;',
  28344. '$mod.p = $mod.Notify;',
  28345. '$mod.p = $mod.Notify;',
  28346. '$mod.p = $mod.ProcA;',
  28347. '$mod.p = $mod.ProcA;',
  28348. '$mod.p = $mod.FuncB;',
  28349. '$mod.p = $mod.FuncB;',
  28350. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28351. '']));
  28352. end;
  28353. procedure TTestModule.TestProcType_PassProcToUntyped;
  28354. begin
  28355. StartProgram(false);
  28356. Add([
  28357. 'type',
  28358. ' TEvent = procedure of object;',
  28359. ' TFunc = function: longint;',
  28360. 'procedure DoIt(); varargs; begin end;',
  28361. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28362. 'var',
  28363. ' Event: tevent;',
  28364. ' Func: TFunc;',
  28365. 'begin',
  28366. ' doit(event,func);',
  28367. ' dosome(event,event,event);',
  28368. ' dosome(func,func,func);',
  28369. '']);
  28370. ConvertProgram;
  28371. CheckSource('TestProcType_PassProcToUntyped',
  28372. LinesToStr([ // statements
  28373. 'this.DoIt = function () {',
  28374. '};',
  28375. 'this.DoSome = function (a, b, p) {',
  28376. '};',
  28377. 'this.Event = null;',
  28378. 'this.Func = null;',
  28379. '']),
  28380. LinesToStr([ // $mod.$main
  28381. '$mod.DoIt($mod.Event, $mod.Func);',
  28382. '$mod.DoSome($mod.Event, {',
  28383. ' p: $mod,',
  28384. ' get: function () {',
  28385. ' return this.p.Event;',
  28386. ' },',
  28387. ' set: function (v) {',
  28388. ' this.p.Event = v;',
  28389. ' }',
  28390. '}, $mod.Event);',
  28391. '$mod.DoSome($mod.Func, {',
  28392. ' p: $mod,',
  28393. ' get: function () {',
  28394. ' return this.p.Func;',
  28395. ' },',
  28396. ' set: function (v) {',
  28397. ' this.p.Func = v;',
  28398. ' }',
  28399. '}, $mod.Func);',
  28400. '']));
  28401. end;
  28402. procedure TTestModule.TestProcType_PassProcToArray;
  28403. begin
  28404. StartProgram(false);
  28405. Add([
  28406. 'type',
  28407. ' TFunc = function: longint;',
  28408. ' TArrFunc = array of TFunc;',
  28409. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28410. 'function GetIt: longint; begin end;',
  28411. 'var',
  28412. ' Func: tfunc;',
  28413. 'begin',
  28414. ' doit([]);',
  28415. ' doit([@GetIt]);',
  28416. ' doit([Func]);',
  28417. '']);
  28418. ConvertProgram;
  28419. CheckSource('TestProcType_PassProcToArray',
  28420. LinesToStr([ // statements
  28421. 'this.DoIt = function (Arr) {',
  28422. '};',
  28423. 'this.GetIt = function () {',
  28424. ' var Result = 0;',
  28425. ' return Result;',
  28426. '};',
  28427. 'this.Func = null;',
  28428. '']),
  28429. LinesToStr([ // $mod.$main
  28430. '$mod.DoIt([]);',
  28431. '$mod.DoIt([$mod.GetIt]);',
  28432. '$mod.DoIt([$mod.Func]);',
  28433. '']));
  28434. end;
  28435. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28436. begin
  28437. StartProgram(false);
  28438. Add([
  28439. '{$modeswitch externalclass}',
  28440. 'type',
  28441. ' TProc = reference to procedure(i: longint); safecall;',
  28442. ' TEvent = procedure(i: longint) of object; safecall;',
  28443. ' TExtA = class external name ''ExtObj''',
  28444. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28445. ' procedure DoSome(Id: longint = 1);',
  28446. ' procedure SetOnClick(const e: TEvent);',
  28447. ' property OnClick: TEvent write SetOnClick;',
  28448. ' class procedure Fly(Id: longint = 1); static;',
  28449. ' procedure SetOnShow(const p: TProc);',
  28450. ' property OnShow: TProc write SetOnShow;',
  28451. ' end;',
  28452. 'procedure Run(i: longint = 1);',
  28453. 'begin',
  28454. 'end;',
  28455. 'var',
  28456. ' Obj: texta;',
  28457. ' e: TEvent;',
  28458. ' p: TProc;',
  28459. 'begin',
  28460. ' e:=e;',
  28461. ' e:[email protected];',
  28462. ' e:[email protected];',
  28463. ' e:=TEvent(@obj.dosome);', // no safecall
  28464. ' obj.OnClick:[email protected];',
  28465. ' obj.OnClick:[email protected];',
  28466. ' obj.setonclick(@obj.doit);',
  28467. ' obj.setonclick(@obj.dosome);',
  28468. ' p:=@Run;',
  28469. ' p:[email protected];',
  28470. ' obj.OnShow:=@Run;',
  28471. ' obj.OnShow:[email protected];',
  28472. ' obj.setOnShow(@Run);',
  28473. ' obj.setOnShow(@TExtA.Fly);',
  28474. ' with obj do begin',
  28475. ' e:=@doit;',
  28476. ' e:=@dosome;',
  28477. ' OnClick:=@doit;',
  28478. ' OnClick:=@dosome;',
  28479. ' setonclick(@doit);',
  28480. ' setonclick(@dosome);',
  28481. ' OnShow:=@Run;',
  28482. ' setOnShow(@Run);',
  28483. ' end;']);
  28484. ConvertProgram;
  28485. CheckSource('TestProcType_SafeCallObjFPC',
  28486. LinesToStr([ // statements
  28487. 'this.Run = function (i) {',
  28488. '};',
  28489. 'this.Obj = null;',
  28490. 'this.e = null;',
  28491. 'this.p = null;',
  28492. '']),
  28493. LinesToStr([ // $mod.$main
  28494. '$mod.e = $mod.e;',
  28495. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28496. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28497. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28498. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28499. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28500. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28501. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28502. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28503. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28504. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28505. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28506. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28507. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28508. 'var $with = $mod.Obj;',
  28509. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28510. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28511. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28512. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28513. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28514. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28515. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28516. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28517. '']));
  28518. end;
  28519. procedure TTestModule.TestProcType_SafeCallDelphi;
  28520. begin
  28521. StartProgram(false);
  28522. Add([
  28523. '{$mode delphi}',
  28524. '{$modeswitch externalclass}',
  28525. 'type',
  28526. ' TProc = reference to procedure(i: longint); safecall;',
  28527. ' TEvent = procedure(i: longint) of object; safecall;',
  28528. ' TExtA = class external name ''ExtObj''',
  28529. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28530. ' procedure DoSome(Id: longint = 1);',
  28531. ' procedure SetOnClick(const e: TEvent);',
  28532. ' property OnClick: TEvent write SetOnClick;',
  28533. ' class procedure Fly(Id: longint = 1); static;',
  28534. ' procedure SetOnShow(const p: TProc);',
  28535. ' property OnShow: TProc write SetOnShow;',
  28536. ' end;',
  28537. 'procedure Run(i: longint = 1);',
  28538. 'begin',
  28539. 'end;',
  28540. 'var',
  28541. ' Obj: texta;',
  28542. ' e: TEvent;',
  28543. ' p: TProc;',
  28544. 'begin',
  28545. ' e:=e;',
  28546. ' e:=obj.doit;',
  28547. ' e:=obj.dosome;',
  28548. ' e:=TEvent(@obj.dosome);', // no safecall
  28549. ' obj.OnClick:=obj.doit;',
  28550. ' obj.OnClick:=obj.dosome;',
  28551. ' obj.setonclick(obj.doit);',
  28552. ' obj.setonclick(obj.dosome);',
  28553. ' p:=Run;',
  28554. ' p:=TExtA.Fly;',
  28555. ' obj.OnShow:=Run;',
  28556. ' obj.OnShow:=TExtA.Fly;',
  28557. ' obj.setOnShow(Run);',
  28558. ' obj.setOnShow(TExtA.Fly);',
  28559. ' with obj do begin',
  28560. ' e:=doit;',
  28561. ' e:=dosome;',
  28562. ' OnClick:=doit;',
  28563. ' OnClick:=dosome;',
  28564. ' setonclick(doit);',
  28565. ' setonclick(dosome);',
  28566. ' OnShow:=@Run;',
  28567. ' setOnShow(@Run);',
  28568. ' end;']);
  28569. ConvertProgram;
  28570. CheckSource('TestProcType_SafeCallDelphi',
  28571. LinesToStr([ // statements
  28572. 'this.Run = function (i) {',
  28573. '};',
  28574. 'this.Obj = null;',
  28575. 'this.e = null;',
  28576. 'this.p = null;',
  28577. '']),
  28578. LinesToStr([ // $mod.$main
  28579. '$mod.e = $mod.e;',
  28580. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28581. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28582. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28583. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28584. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28585. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28586. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28587. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28588. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28589. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28590. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28591. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28592. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28593. 'var $with = $mod.Obj;',
  28594. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28595. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28596. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28597. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28598. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28599. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28600. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28601. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28602. '']));
  28603. end;
  28604. procedure TTestModule.TestProcType_SafeCall_Arg;
  28605. begin
  28606. StartProgram(false);
  28607. Add([
  28608. 'type',
  28609. ' TSafecallProc = reference to procedure; safecall;',
  28610. 'procedure Fly(const aHandler: TSafecallProc);',
  28611. 'var',
  28612. ' P: TSafecallProc;',
  28613. 'begin',
  28614. ' P := aHandler;',
  28615. ' Fly(P);',
  28616. ' Fly(aHandler);',
  28617. 'end;',
  28618. 'begin',
  28619. ' Fly(nil);',
  28620. '']);
  28621. ConvertProgram;
  28622. CheckSource('TestProcType_SafeCall_Arg',
  28623. LinesToStr([ // statements
  28624. 'this.Fly = function (aHandler) {',
  28625. ' var P = null;',
  28626. ' P = aHandler;',
  28627. ' $mod.Fly(P);',
  28628. ' $mod.Fly(aHandler);',
  28629. '};',
  28630. '']),
  28631. LinesToStr([ // $mod.$main
  28632. '$mod.Fly(null);',
  28633. '']));
  28634. end;
  28635. procedure TTestModule.TestPointer;
  28636. begin
  28637. StartProgram(false);
  28638. Add(['type',
  28639. ' TObject = class end;',
  28640. ' TClass = class of TObject;',
  28641. ' TArrInt = array of longint;',
  28642. 'const',
  28643. ' n = nil;',
  28644. 'var',
  28645. ' v: jsvalue;',
  28646. ' Obj: tobject;',
  28647. ' C: tclass;',
  28648. ' a: tarrint;',
  28649. ' p: Pointer = nil;',
  28650. ' s: string;',
  28651. 'begin',
  28652. ' p:=p;',
  28653. ' p:=nil;',
  28654. ' if p=nil then;',
  28655. ' if nil=p then;',
  28656. ' if Assigned(p) then;',
  28657. ' p:=Pointer(v);',
  28658. ' p:=obj;',
  28659. ' p:=c;',
  28660. ' p:=a;',
  28661. ' p:=tobject;',
  28662. ' obj:=TObject(p);',
  28663. ' c:=TClass(p);',
  28664. ' a:=TArrInt(p);',
  28665. ' p:=n;',
  28666. ' p:=Pointer(a);',
  28667. ' p:=pointer(s);',
  28668. ' s:=string(p);',
  28669. '']);
  28670. ConvertProgram;
  28671. CheckSource('TestPointer',
  28672. LinesToStr([ // statements
  28673. 'rtl.createClass(this, "TObject", null, function () {',
  28674. ' this.$init = function () {',
  28675. ' };',
  28676. ' this.$final = function () {',
  28677. ' };',
  28678. '});',
  28679. 'this.n = null;',
  28680. 'this.v = undefined;',
  28681. 'this.Obj = null;',
  28682. 'this.C = null;',
  28683. 'this.a = [];',
  28684. 'this.p = null;',
  28685. 'this.s = "";',
  28686. '']),
  28687. LinesToStr([ // $mod.$main
  28688. '$mod.p = $mod.p;',
  28689. '$mod.p = null;',
  28690. 'if ($mod.p === null) ;',
  28691. 'if (null === $mod.p) ;',
  28692. 'if ($mod.p != null) ;',
  28693. '$mod.p = $mod.v;',
  28694. '$mod.p = $mod.Obj;',
  28695. '$mod.p = $mod.C;',
  28696. '$mod.p = $mod.a;',
  28697. '$mod.p = $mod.TObject;',
  28698. '$mod.Obj = $mod.p;',
  28699. '$mod.C = $mod.p;',
  28700. '$mod.a = $mod.p;',
  28701. '$mod.p = null;',
  28702. '$mod.p = $mod.a;',
  28703. '$mod.p = $mod.s;',
  28704. '$mod.s = $mod.p;',
  28705. '']));
  28706. end;
  28707. procedure TTestModule.TestPointer_Proc;
  28708. begin
  28709. StartProgram(false);
  28710. Add('type');
  28711. Add(' TObject = class');
  28712. Add(' procedure DoIt; virtual; abstract;');
  28713. Add(' end;');
  28714. Add('procedure DoSome; begin end;');
  28715. Add('var');
  28716. Add(' o: TObject;');
  28717. Add(' p: Pointer;');
  28718. Add('begin');
  28719. Add(' p:=@DoSome;');
  28720. Add(' p:[email protected];');
  28721. ConvertProgram;
  28722. CheckSource('TestPointer_Proc',
  28723. LinesToStr([ // statements
  28724. 'rtl.createClass(this, "TObject", null, function () {',
  28725. ' this.$init = function () {',
  28726. ' };',
  28727. ' this.$final = function () {',
  28728. ' };',
  28729. '});',
  28730. 'this.DoSome = function () {',
  28731. '};',
  28732. 'this.o = null;',
  28733. 'this.p = null;',
  28734. '']),
  28735. LinesToStr([ // $mod.$main
  28736. '$mod.p = $mod.DoSome;',
  28737. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28738. '']));
  28739. end;
  28740. procedure TTestModule.TestPointer_AssignRecordFail;
  28741. begin
  28742. StartProgram(false);
  28743. Add('type');
  28744. Add(' TRec = record end;');
  28745. Add('var');
  28746. Add(' p: Pointer;');
  28747. Add(' r: TRec;');
  28748. Add('begin');
  28749. Add(' p:=r;');
  28750. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28751. nIncompatibleTypesGotExpected);
  28752. ConvertProgram;
  28753. end;
  28754. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28755. begin
  28756. StartProgram(false);
  28757. Add('type');
  28758. Add(' TArr = array[boolean] of longint;');
  28759. Add('var');
  28760. Add(' p: Pointer;');
  28761. Add(' a: TArr;');
  28762. Add('begin');
  28763. Add(' p:=a;');
  28764. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28765. nIncompatibleTypesGotExpected);
  28766. ConvertProgram;
  28767. end;
  28768. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28769. begin
  28770. StartProgram(false);
  28771. Add([
  28772. 'procedure DoIt(args: array of jsvalue); begin end;',
  28773. 'procedure DoAll; varargs; begin end;',
  28774. 'var',
  28775. ' v: jsvalue;',
  28776. 'begin',
  28777. ' DoIt([pointer(v)]);',
  28778. ' DoAll(pointer(v));',
  28779. '']);
  28780. ConvertProgram;
  28781. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28782. LinesToStr([ // statements
  28783. 'this.DoIt = function (args) {',
  28784. '};',
  28785. 'this.DoAll = function () {',
  28786. '};',
  28787. 'this.v = undefined;',
  28788. '']),
  28789. LinesToStr([ // $mod.$main
  28790. '$mod.DoIt([$mod.v]);',
  28791. '$mod.DoAll($mod.v);',
  28792. '']));
  28793. end;
  28794. procedure TTestModule.TestPointer_NonRecordFail;
  28795. begin
  28796. StartProgram(false);
  28797. Add([
  28798. 'type',
  28799. ' p = ^longint;',
  28800. 'begin',
  28801. '']);
  28802. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28803. ConvertProgram;
  28804. end;
  28805. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28806. begin
  28807. StartProgram(false);
  28808. Add([
  28809. 'procedure DoIt(p: ^longint); begin end;',
  28810. 'begin',
  28811. '']);
  28812. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28813. ConvertProgram;
  28814. end;
  28815. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28816. begin
  28817. StartProgram(false);
  28818. Add([
  28819. 'var p: ^longint;',
  28820. 'begin',
  28821. '']);
  28822. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28823. ConvertProgram;
  28824. end;
  28825. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28826. begin
  28827. StartProgram(false);
  28828. Add([
  28829. 'function DoIt: ^longint; begin end;',
  28830. 'begin',
  28831. '']);
  28832. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28833. ConvertProgram;
  28834. end;
  28835. procedure TTestModule.TestPointer_AddrOperatorFail;
  28836. begin
  28837. StartProgram(false);
  28838. Add([
  28839. 'var i: longint;',
  28840. 'begin',
  28841. ' if @i=nil then ;',
  28842. '']);
  28843. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28844. ConvertProgram;
  28845. end;
  28846. procedure TTestModule.TestPointer_ArrayParamsFail;
  28847. begin
  28848. StartProgram(false);
  28849. Add([
  28850. 'var',
  28851. ' p: Pointer;',
  28852. 'begin',
  28853. ' p:=p[1];',
  28854. '']);
  28855. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28856. ConvertProgram;
  28857. end;
  28858. procedure TTestModule.TestPointer_PointerAddFail;
  28859. begin
  28860. StartProgram(false);
  28861. Add([
  28862. 'var',
  28863. ' p: Pointer;',
  28864. 'begin',
  28865. ' p:=p+1;',
  28866. '']);
  28867. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28868. ConvertProgram;
  28869. end;
  28870. procedure TTestModule.TestPointer_IncPointerFail;
  28871. begin
  28872. StartProgram(false);
  28873. Add([
  28874. 'var',
  28875. ' p: Pointer;',
  28876. 'begin',
  28877. ' inc(p,1);',
  28878. '']);
  28879. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28880. nIncompatibleTypeArgNo);
  28881. ConvertProgram;
  28882. end;
  28883. procedure TTestModule.TestPointer_Record;
  28884. begin
  28885. StartProgram(false);
  28886. Add([
  28887. 'type',
  28888. ' TRec = record x: longint; end;',
  28889. ' PRec = ^TRec;',
  28890. 'var',
  28891. ' r: TRec;',
  28892. ' p: PRec;',
  28893. ' q: ^TRec;',
  28894. ' Ptr: pointer;',
  28895. 'begin',
  28896. ' new(p);',
  28897. ' p:=@r;',
  28898. ' r:=p^;',
  28899. ' r.x:=p^.x;',
  28900. ' p^.x:=r.x;',
  28901. ' if p^.x=3 then ;',
  28902. ' if 4=p^.x then ;',
  28903. ' dispose(p);',
  28904. ' new(q);',
  28905. ' dispose(q);',
  28906. ' Ptr:=p;',
  28907. ' p:=PRec(ptr);',
  28908. '']);
  28909. ConvertProgram;
  28910. CheckSource('TestPointer_Record',
  28911. LinesToStr([ // statements
  28912. 'rtl.recNewT(this, "TRec", function () {',
  28913. ' this.x = 0;',
  28914. ' this.$eq = function (b) {',
  28915. ' return this.x === b.x;',
  28916. ' };',
  28917. ' this.$assign = function (s) {',
  28918. ' this.x = s.x;',
  28919. ' return this;',
  28920. ' };',
  28921. '});',
  28922. 'this.r = this.TRec.$new();',
  28923. 'this.p = null;',
  28924. 'this.q = null;',
  28925. 'this.Ptr = null;',
  28926. '']),
  28927. LinesToStr([ // $mod.$main
  28928. '$mod.p = $mod.TRec.$new();',
  28929. '$mod.p = $mod.r;',
  28930. '$mod.r.$assign($mod.p);',
  28931. '$mod.r.x = $mod.p.x;',
  28932. '$mod.p.x = $mod.r.x;',
  28933. 'if ($mod.p.x === 3) ;',
  28934. 'if (4 === $mod.p.x) ;',
  28935. '$mod.p = null;',
  28936. '$mod.q = $mod.TRec.$new();',
  28937. '$mod.q = null;',
  28938. '$mod.Ptr = $mod.p;',
  28939. '$mod.p = $mod.Ptr;',
  28940. '']));
  28941. end;
  28942. procedure TTestModule.TestPointer_RecordArg;
  28943. begin
  28944. StartProgram(false);
  28945. Add([
  28946. '{$modeswitch autoderef}',
  28947. 'type',
  28948. ' TRec = record x: longint; end;',
  28949. ' PRec = ^TRec;',
  28950. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28951. 'begin',
  28952. ' a.x:=a.x;',
  28953. ' a^.x:=a^.x;',
  28954. ' with a^ do',
  28955. ' x:=x;',
  28956. 'end;',
  28957. 'function GetIt(p: PRec): PRec;',
  28958. 'begin',
  28959. ' p.x:=p.x;',
  28960. ' p^.x:=p^.x;',
  28961. ' with p^ do',
  28962. ' x:=x;',
  28963. 'end;',
  28964. 'var',
  28965. ' r: TRec;',
  28966. ' p: PRec;',
  28967. 'begin',
  28968. ' p:=GetIt(p);',
  28969. ' p^:=GetIt(@r)^;',
  28970. ' DoIt(p,p,p);',
  28971. ' DoIt(@r,p,p);',
  28972. '']);
  28973. ConvertProgram;
  28974. CheckSource('TestPointer_RecordArg',
  28975. LinesToStr([ // statements
  28976. 'rtl.recNewT(this, "TRec", function () {',
  28977. ' this.x = 0;',
  28978. ' this.$eq = function (b) {',
  28979. ' return this.x === b.x;',
  28980. ' };',
  28981. ' this.$assign = function (s) {',
  28982. ' this.x = s.x;',
  28983. ' return this;',
  28984. ' };',
  28985. '});',
  28986. 'this.DoIt = function (a, b, c) {',
  28987. ' var Result = $mod.TRec.$new();',
  28988. ' a.x = a.x;',
  28989. ' a.x = a.x;',
  28990. ' a.x = a.x;',
  28991. ' return Result;',
  28992. '};',
  28993. 'this.GetIt = function (p) {',
  28994. ' var Result = null;',
  28995. ' p.x = p.x;',
  28996. ' p.x = p.x;',
  28997. ' p.x = p.x;',
  28998. ' return Result;',
  28999. '};',
  29000. 'this.r = this.TRec.$new();',
  29001. 'this.p = null;',
  29002. '']),
  29003. LinesToStr([ // $mod.$main
  29004. '$mod.p = $mod.GetIt($mod.p);',
  29005. '$mod.p.$assign($mod.GetIt($mod.r));',
  29006. '$mod.DoIt($mod.p, {',
  29007. ' p: $mod,',
  29008. ' get: function () {',
  29009. ' return this.p.p;',
  29010. ' },',
  29011. ' set: function (v) {',
  29012. ' this.p.p = v;',
  29013. ' }',
  29014. '}, {',
  29015. ' p: $mod,',
  29016. ' get: function () {',
  29017. ' return this.p.p;',
  29018. ' },',
  29019. ' set: function (v) {',
  29020. ' this.p.p = v;',
  29021. ' }',
  29022. '});',
  29023. '$mod.DoIt($mod.r, {',
  29024. ' p: $mod,',
  29025. ' get: function () {',
  29026. ' return this.p.p;',
  29027. ' },',
  29028. ' set: function (v) {',
  29029. ' this.p.p = v;',
  29030. ' }',
  29031. '}, {',
  29032. ' p: $mod,',
  29033. ' get: function () {',
  29034. ' return this.p.p;',
  29035. ' },',
  29036. ' set: function (v) {',
  29037. ' this.p.p = v;',
  29038. ' }',
  29039. '});',
  29040. '']));
  29041. end;
  29042. procedure TTestModule.TestJSValue_AssignToJSValue;
  29043. begin
  29044. StartProgram(false);
  29045. Add('var');
  29046. Add(' v: jsvalue;');
  29047. Add(' i: longint;');
  29048. Add(' s: string;');
  29049. Add(' b: boolean;');
  29050. Add(' d: double;');
  29051. Add(' p: pointer;');
  29052. Add('begin');
  29053. Add(' v:=v;');
  29054. Add(' v:=1;');
  29055. Add(' v:=i;');
  29056. Add(' v:='''';');
  29057. Add(' v:=''c'';');
  29058. Add(' v:=''foo'';');
  29059. Add(' v:=s;');
  29060. Add(' v:=false;');
  29061. Add(' v:=true;');
  29062. Add(' v:=b;');
  29063. Add(' v:=0.1;');
  29064. Add(' v:=d;');
  29065. Add(' v:=nil;');
  29066. Add(' v:=p;');
  29067. ConvertProgram;
  29068. CheckSource('TestJSValue_AssignToJSValue',
  29069. LinesToStr([ // statements
  29070. 'this.v = undefined;',
  29071. 'this.i = 0;',
  29072. 'this.s = "";',
  29073. 'this.b = false;',
  29074. 'this.d = 0.0;',
  29075. 'this.p = null;',
  29076. '']),
  29077. LinesToStr([ // $mod.$main
  29078. '$mod.v = $mod.v;',
  29079. '$mod.v = 1;',
  29080. '$mod.v = $mod.i;',
  29081. '$mod.v = "";',
  29082. '$mod.v = "c";',
  29083. '$mod.v = "foo";',
  29084. '$mod.v = $mod.s;',
  29085. '$mod.v = false;',
  29086. '$mod.v = true;',
  29087. '$mod.v = $mod.b;',
  29088. '$mod.v = 0.1;',
  29089. '$mod.v = $mod.d;',
  29090. '$mod.v = null;',
  29091. '$mod.v = $mod.p;',
  29092. '']));
  29093. end;
  29094. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29095. begin
  29096. StartProgram(false);
  29097. Add('type');
  29098. Add(' integer = longint;');
  29099. Add(' TYesNo = boolean;');
  29100. Add(' TFloat = double;');
  29101. Add(' TCaption = string;');
  29102. Add(' TChar = char;');
  29103. Add('var');
  29104. Add(' v: jsvalue;');
  29105. Add(' i: integer;');
  29106. Add(' s: TCaption;');
  29107. Add(' b: TYesNo;');
  29108. Add(' d: TFloat;');
  29109. Add(' c: char;');
  29110. Add('begin');
  29111. Add(' i:=longint(v);');
  29112. Add(' i:=integer(v);');
  29113. Add(' s:=string(v);');
  29114. Add(' s:=TCaption(v);');
  29115. Add(' b:=boolean(v);');
  29116. Add(' b:=TYesNo(v);');
  29117. Add(' d:=double(v);');
  29118. Add(' d:=TFloat(v);');
  29119. Add(' c:=char(v);');
  29120. Add(' c:=TChar(v);');
  29121. ConvertProgram;
  29122. CheckSource('TestJSValue_TypeCastToBaseType',
  29123. LinesToStr([ // statements
  29124. 'this.v = undefined;',
  29125. 'this.i = 0;',
  29126. 'this.s = "";',
  29127. 'this.b = false;',
  29128. 'this.d = 0.0;',
  29129. 'this.c = "";',
  29130. '']),
  29131. LinesToStr([ // $mod.$main
  29132. '$mod.i = rtl.trunc($mod.v);',
  29133. '$mod.i = rtl.trunc($mod.v);',
  29134. '$mod.s = "" + $mod.v;',
  29135. '$mod.s = "" + $mod.v;',
  29136. '$mod.b = !($mod.v == false);',
  29137. '$mod.b = !($mod.v == false);',
  29138. '$mod.d = rtl.getNumber($mod.v);',
  29139. '$mod.d = rtl.getNumber($mod.v);',
  29140. '$mod.c = rtl.getChar($mod.v);',
  29141. '$mod.c = rtl.getChar($mod.v);',
  29142. '']));
  29143. end;
  29144. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29145. begin
  29146. StartProgram(false);
  29147. Add([
  29148. 'type',
  29149. ' TArr = array of word;',
  29150. ' TRec = record end;',
  29151. ' TSet = set of boolean;',
  29152. 'procedure Fly(v: jsvalue);',
  29153. 'begin',
  29154. 'end;',
  29155. 'var',
  29156. ' a: TArr;',
  29157. ' r: TRec;',
  29158. ' s: TSet;',
  29159. 'begin',
  29160. ' Fly(jsvalue(a));',
  29161. ' Fly(jsvalue(r));',
  29162. ' Fly(jsvalue(s));',
  29163. '']);
  29164. ConvertProgram;
  29165. CheckSource('TestJSValue_TypecastToJSValue',
  29166. LinesToStr([ // statements
  29167. 'rtl.recNewT(this, "TRec", function () {',
  29168. ' this.$eq = function (b) {',
  29169. ' return true;',
  29170. ' };',
  29171. ' this.$assign = function (s) {',
  29172. ' return this;',
  29173. ' };',
  29174. '});',
  29175. 'this.Fly = function (v) {',
  29176. '};',
  29177. 'this.a = [];',
  29178. 'this.r = this.TRec.$new();',
  29179. 'this.s = {};',
  29180. '']),
  29181. LinesToStr([ // $mod.$main
  29182. '$mod.Fly($mod.a);',
  29183. '$mod.Fly($mod.r);',
  29184. '$mod.Fly($mod.s);',
  29185. '']));
  29186. end;
  29187. procedure TTestModule.TestJSValue_Equal;
  29188. begin
  29189. StartProgram(false);
  29190. Add('type');
  29191. Add(' integer = longint;');
  29192. Add(' TYesNo = boolean;');
  29193. Add(' TFloat = double;');
  29194. Add(' TCaption = string;');
  29195. Add(' TChar = char;');
  29196. Add(' TMulti = JSValue;');
  29197. Add('var');
  29198. Add(' v: jsvalue;');
  29199. Add(' i: integer;');
  29200. Add(' s: TCaption;');
  29201. Add(' b: TYesNo;');
  29202. Add(' d: TFloat;');
  29203. Add(' c: char;');
  29204. Add(' m: TMulti;');
  29205. Add('begin');
  29206. Add(' b:=v=v;');
  29207. Add(' b:=v<>v;');
  29208. Add(' b:=v=1;');
  29209. Add(' b:=v<>1;');
  29210. Add(' b:=2=v;');
  29211. Add(' b:=2<>v;');
  29212. Add(' b:=v=i;');
  29213. Add(' b:=i=v;');
  29214. Add(' b:=v=nil;');
  29215. Add(' b:=nil=v;');
  29216. Add(' b:=v=false;');
  29217. Add(' b:=true=v;');
  29218. Add(' b:=v=b;');
  29219. Add(' b:=b=v;');
  29220. Add(' b:=v=s;');
  29221. Add(' b:=s=v;');
  29222. Add(' b:=v=''foo'';');
  29223. Add(' b:=''''=v;');
  29224. Add(' b:=v=d;');
  29225. Add(' b:=d=v;');
  29226. Add(' b:=v=3.4;');
  29227. Add(' b:=5.6=v;');
  29228. Add(' b:=v=c;');
  29229. Add(' b:=c=v;');
  29230. Add(' b:=m=m;');
  29231. Add(' b:=v=m;');
  29232. Add(' b:=m=v;');
  29233. ConvertProgram;
  29234. CheckSource('TestJSValue_Equal',
  29235. LinesToStr([ // statements
  29236. 'this.v = undefined;',
  29237. 'this.i = 0;',
  29238. 'this.s = "";',
  29239. 'this.b = false;',
  29240. 'this.d = 0.0;',
  29241. 'this.c = "";',
  29242. 'this.m = undefined;',
  29243. '']),
  29244. LinesToStr([ // $mod.$main
  29245. '$mod.b = $mod.v == $mod.v;',
  29246. '$mod.b = $mod.v != $mod.v;',
  29247. '$mod.b = $mod.v == 1;',
  29248. '$mod.b = $mod.v != 1;',
  29249. '$mod.b = 2 == $mod.v;',
  29250. '$mod.b = 2 != $mod.v;',
  29251. '$mod.b = $mod.v == $mod.i;',
  29252. '$mod.b = $mod.i == $mod.v;',
  29253. '$mod.b = $mod.v == null;',
  29254. '$mod.b = null == $mod.v;',
  29255. '$mod.b = $mod.v == false;',
  29256. '$mod.b = true == $mod.v;',
  29257. '$mod.b = $mod.v == $mod.b;',
  29258. '$mod.b = $mod.b == $mod.v;',
  29259. '$mod.b = $mod.v == $mod.s;',
  29260. '$mod.b = $mod.s == $mod.v;',
  29261. '$mod.b = $mod.v == "foo";',
  29262. '$mod.b = "" == $mod.v;',
  29263. '$mod.b = $mod.v == $mod.d;',
  29264. '$mod.b = $mod.d == $mod.v;',
  29265. '$mod.b = $mod.v == 3.4;',
  29266. '$mod.b = 5.6 == $mod.v;',
  29267. '$mod.b = $mod.v == $mod.c;',
  29268. '$mod.b = $mod.c == $mod.v;',
  29269. '$mod.b = $mod.m == $mod.m;',
  29270. '$mod.b = $mod.v == $mod.m;',
  29271. '$mod.b = $mod.m == $mod.v;',
  29272. '']));
  29273. end;
  29274. procedure TTestModule.TestJSValue_If;
  29275. begin
  29276. StartProgram(false);
  29277. Add([
  29278. 'procedure Fly(var u);',
  29279. 'begin',
  29280. ' if jsvalue(u) then ;',
  29281. 'end;',
  29282. 'var',
  29283. ' v: jsvalue;',
  29284. 'begin',
  29285. ' if v then ;',
  29286. ' while v do ;',
  29287. ' repeat until v;',
  29288. '']);
  29289. ConvertProgram;
  29290. CheckSource('TestJSValue_If',
  29291. LinesToStr([ // statements
  29292. 'this.Fly = function (u) {',
  29293. ' if (u.get()) ;',
  29294. '};',
  29295. 'this.v = undefined;',
  29296. '']),
  29297. LinesToStr([ // $mod.$main
  29298. 'if ($mod.v) ;',
  29299. 'while($mod.v){',
  29300. '};',
  29301. 'do{',
  29302. '} while(!$mod.v);',
  29303. '']));
  29304. end;
  29305. procedure TTestModule.TestJSValue_Not;
  29306. begin
  29307. StartProgram(false);
  29308. Add([
  29309. 'var',
  29310. ' v: jsvalue;',
  29311. ' b: boolean;',
  29312. 'begin',
  29313. ' b:=not v;',
  29314. ' if not v then ;',
  29315. ' while not v do ;',
  29316. ' repeat until not v;',
  29317. '']);
  29318. ConvertProgram;
  29319. CheckSource('TestJSValue_If',
  29320. LinesToStr([ // statements
  29321. 'this.v = undefined;',
  29322. 'this.b = false;',
  29323. '']),
  29324. LinesToStr([ // $mod.$main
  29325. '$mod.b=!$mod.v;',
  29326. 'if (!$mod.v) ;',
  29327. 'while(!$mod.v){',
  29328. '};',
  29329. 'do{',
  29330. '} while($mod.v);',
  29331. '']));
  29332. end;
  29333. procedure TTestModule.TestJSValue_Enum;
  29334. begin
  29335. StartProgram(false);
  29336. Add('type');
  29337. Add(' TColor = (red, blue);');
  29338. Add(' TRedBlue = TColor;');
  29339. Add('var');
  29340. Add(' v: jsvalue;');
  29341. Add(' e: TColor;');
  29342. Add('begin');
  29343. Add(' v:=e;');
  29344. Add(' v:=TColor(e);');
  29345. Add(' v:=TRedBlue(e);');
  29346. Add(' e:=TColor(v);');
  29347. Add(' e:=TRedBlue(v);');
  29348. ConvertProgram;
  29349. CheckSource('TestJSValue_Enum',
  29350. LinesToStr([ // statements
  29351. 'this.TColor = {',
  29352. ' "0": "red",',
  29353. ' red: 0,',
  29354. ' "1": "blue",',
  29355. ' blue: 1',
  29356. '};',
  29357. 'this.v = undefined;',
  29358. 'this.e = 0;',
  29359. '']),
  29360. LinesToStr([ // $mod.$main
  29361. '$mod.v = $mod.e;',
  29362. '$mod.v = $mod.e;',
  29363. '$mod.v = $mod.e;',
  29364. '$mod.e = $mod.v;',
  29365. '$mod.e = $mod.v;',
  29366. '']));
  29367. end;
  29368. procedure TTestModule.TestJSValue_ClassInstance;
  29369. begin
  29370. StartProgram(false);
  29371. Add([
  29372. 'type',
  29373. ' TObject = class',
  29374. ' end;',
  29375. ' TBirdObject = TObject;',
  29376. 'var',
  29377. ' v: jsvalue;',
  29378. ' o: TObject;',
  29379. 'begin',
  29380. ' v:=o;',
  29381. ' v:=TObject(o);',
  29382. ' v:=TBirdObject(o);',
  29383. ' o:=TObject(v);',
  29384. ' o:=TBirdObject(v);',
  29385. ' if v is TObject then ;',
  29386. '']);
  29387. ConvertProgram;
  29388. CheckSource('TestJSValue_ClassInstance',
  29389. LinesToStr([ // statements
  29390. 'rtl.createClass(this, "TObject", null, function () {',
  29391. ' this.$init = function () {',
  29392. ' };',
  29393. ' this.$final = function () {',
  29394. ' };',
  29395. '});',
  29396. 'this.v = undefined;',
  29397. 'this.o = null;',
  29398. '']),
  29399. LinesToStr([ // $mod.$main
  29400. '$mod.v = $mod.o;',
  29401. '$mod.v = $mod.o;',
  29402. '$mod.v = $mod.o;',
  29403. '$mod.o = rtl.getObject($mod.v);',
  29404. '$mod.o = rtl.getObject($mod.v);',
  29405. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29406. '']));
  29407. end;
  29408. procedure TTestModule.TestJSValue_ClassOf;
  29409. begin
  29410. StartProgram(false);
  29411. Add([
  29412. 'type',
  29413. ' TClass = class of TObject;',
  29414. ' TObject = class',
  29415. ' end;',
  29416. ' TBirds = class of TBird;',
  29417. ' TBird = class(TObject) end;',
  29418. 'var',
  29419. ' v: jsvalue;',
  29420. ' c: TClass;',
  29421. 'begin',
  29422. ' v:=c;',
  29423. ' v:=TObject;',
  29424. ' v:=TClass(c);',
  29425. ' v:=TBirds(c);',
  29426. ' c:=TClass(v);',
  29427. ' c:=TBirds(v);',
  29428. ' if v is TClass then ;',
  29429. '']);
  29430. ConvertProgram;
  29431. CheckSource('TestJSValue_ClassOf',
  29432. LinesToStr([ // statements
  29433. 'rtl.createClass(this, "TObject", null, function () {',
  29434. ' this.$init = function () {',
  29435. ' };',
  29436. ' this.$final = function () {',
  29437. ' };',
  29438. '});',
  29439. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29440. '});',
  29441. 'this.v = undefined;',
  29442. 'this.c = null;',
  29443. '']),
  29444. LinesToStr([ // $mod.$main
  29445. '$mod.v = $mod.c;',
  29446. '$mod.v = $mod.TObject;',
  29447. '$mod.v = $mod.c;',
  29448. '$mod.v = $mod.c;',
  29449. '$mod.c = rtl.getObject($mod.v);',
  29450. '$mod.c = rtl.getObject($mod.v);',
  29451. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29452. '']));
  29453. end;
  29454. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29455. begin
  29456. StartProgram(false);
  29457. Add([
  29458. 'type',
  29459. ' integer = longint;',
  29460. ' TArray = array of JSValue;',
  29461. ' TArrgh = tarray;',
  29462. ' TArrInt = array of integer;',
  29463. 'var',
  29464. ' v: jsvalue;',
  29465. ' TheArray: tarray = (1,''2'');',
  29466. ' Arr: tarrgh;',
  29467. ' i: integer;',
  29468. ' ArrInt: tarrint;',
  29469. 'begin',
  29470. ' arr:=thearray;',
  29471. ' thearray:=arr;',
  29472. ' setlength(arr,2);',
  29473. ' setlength(thearray,3);',
  29474. ' arr[4]:=v;',
  29475. ' arr[5]:=length(thearray);',
  29476. ' arr[6]:=nil;',
  29477. ' arr[7]:=thearray[8];',
  29478. ' arr[low(arr)]:=high(thearray);',
  29479. ' arr:=arrint;',
  29480. ' arrInt:=tarrint(arr);',
  29481. ' if TheArray = nil then ;',
  29482. ' if nil = TheArray then ;',
  29483. ' if TheArray <> nil then ;',
  29484. ' if nil <> TheArray then ;',
  29485. '']);
  29486. ConvertProgram;
  29487. CheckSource('TestJSValue_ArrayOfJSValue',
  29488. LinesToStr([ // statements
  29489. 'this.v = undefined;',
  29490. 'this.TheArray = [1, "2"];',
  29491. 'this.Arr = [];',
  29492. 'this.i = 0;',
  29493. 'this.ArrInt = [];',
  29494. '']),
  29495. LinesToStr([ // $mod.$main
  29496. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29497. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29498. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29499. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29500. '$mod.Arr[4] = $mod.v;',
  29501. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29502. '$mod.Arr[6] = null;',
  29503. '$mod.Arr[7] = $mod.TheArray[8];',
  29504. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29505. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29506. '$mod.ArrInt = $mod.Arr;',
  29507. 'if (rtl.length($mod.TheArray) === 0) ;',
  29508. 'if (rtl.length($mod.TheArray) === 0) ;',
  29509. 'if (rtl.length($mod.TheArray) > 0) ;',
  29510. 'if (rtl.length($mod.TheArray) > 0) ;',
  29511. '']));
  29512. end;
  29513. procedure TTestModule.TestJSValue_ArrayLit;
  29514. begin
  29515. StartProgram(false);
  29516. Add([
  29517. 'type',
  29518. ' TFlag = (big,small);',
  29519. ' TArray = array of JSValue;',
  29520. ' TObject = class end;',
  29521. ' TClass = class of TObject;',
  29522. 'var',
  29523. ' v: jsvalue;',
  29524. ' a: TArray;',
  29525. ' o: TObject;',
  29526. 'begin',
  29527. ' a:=[];',
  29528. ' a:=[1];',
  29529. ' a:=[1,2];',
  29530. ' a:=[big];',
  29531. ' a:=[1,big];',
  29532. ' a:=[o,nil];',
  29533. '']);
  29534. ConvertProgram;
  29535. CheckSource('TestJSValue_ArrayLit',
  29536. LinesToStr([ // statements
  29537. 'this.TFlag = {',
  29538. ' "0": "big",',
  29539. ' big: 0,',
  29540. ' "1": "small",',
  29541. ' small: 1',
  29542. '};',
  29543. 'rtl.createClass(this, "TObject", null, function () {',
  29544. ' this.$init = function () {',
  29545. ' };',
  29546. ' this.$final = function () {',
  29547. ' };',
  29548. '});',
  29549. 'this.v = undefined;',
  29550. 'this.a = [];',
  29551. 'this.o = null;',
  29552. '']),
  29553. LinesToStr([ // $mod.$main
  29554. '$mod.a = [];',
  29555. '$mod.a = [1];',
  29556. '$mod.a = [1, 2];',
  29557. '$mod.a = [$mod.TFlag.big];',
  29558. '$mod.a = [1, $mod.TFlag.big];',
  29559. '$mod.a = [$mod.o, null];',
  29560. '']));
  29561. end;
  29562. procedure TTestModule.TestJSValue_Params;
  29563. begin
  29564. StartProgram(false);
  29565. Add('type');
  29566. Add(' integer = longint;');
  29567. Add(' TYesNo = boolean;');
  29568. Add(' TFloat = double;');
  29569. Add(' TCaption = string;');
  29570. Add(' TChar = char;');
  29571. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29572. Add('var');
  29573. Add(' l: jsvalue;');
  29574. Add('begin');
  29575. Add(' a:=a;');
  29576. Add(' l:=b;');
  29577. Add(' c:=c;');
  29578. Add(' d:=d;');
  29579. Add(' Result:=l;');
  29580. Add('end;');
  29581. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29582. Add('var');
  29583. Add(' v: jsvalue;');
  29584. Add(' i: integer;');
  29585. Add(' b: TYesNo;');
  29586. Add(' d: TFloat;');
  29587. Add(' s: TCaption;');
  29588. Add(' c: TChar;');
  29589. Add('begin');
  29590. Add(' v:=doit(v,v,v,v);');
  29591. Add(' i:=integer(dosome(i,i));');
  29592. Add(' b:=TYesNo(dosome(b,b));');
  29593. Add(' d:=TFloat(dosome(d,d));');
  29594. Add(' s:=TCaption(dosome(s,s));');
  29595. Add(' c:=TChar(dosome(c,c));');
  29596. ConvertProgram;
  29597. CheckSource('TestJSValue_Params',
  29598. LinesToStr([ // statements
  29599. 'this.DoIt = function (a, b, c, d) {',
  29600. ' var Result = undefined;',
  29601. ' var l = undefined;',
  29602. ' a = a;',
  29603. ' l = b;',
  29604. ' c.set(c.get());',
  29605. ' d.set(d.get());',
  29606. ' Result = l;',
  29607. ' return Result;',
  29608. '};',
  29609. 'this.DoSome = function (a, b) {',
  29610. ' var Result = undefined;',
  29611. ' return Result;',
  29612. '};',
  29613. 'this.v = undefined;',
  29614. 'this.i = 0;',
  29615. 'this.b = false;',
  29616. 'this.d = 0.0;',
  29617. 'this.s = "";',
  29618. 'this.c = "";',
  29619. '']),
  29620. LinesToStr([ // $mod.$main
  29621. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29622. ' p: $mod,',
  29623. ' get: function () {',
  29624. ' return this.p.v;',
  29625. ' },',
  29626. ' set: function (v) {',
  29627. ' this.p.v = v;',
  29628. ' }',
  29629. '}, {',
  29630. ' p: $mod,',
  29631. ' get: function () {',
  29632. ' return this.p.v;',
  29633. ' },',
  29634. ' set: function (v) {',
  29635. ' this.p.v = v;',
  29636. ' }',
  29637. '});',
  29638. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29639. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29640. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29641. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29642. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29643. '']));
  29644. end;
  29645. procedure TTestModule.TestJSValue_UntypedParam;
  29646. begin
  29647. StartProgram(false);
  29648. Add('function DoIt(const a; var b; out c): jsvalue;');
  29649. Add('begin');
  29650. Add(' Result:=a;');
  29651. Add(' Result:=b;');
  29652. Add(' Result:=c;');
  29653. Add(' b:=Result;');
  29654. Add(' c:=Result;');
  29655. Add('end;');
  29656. Add('var i: longint;');
  29657. Add('begin');
  29658. Add(' doit(i,i,i);');
  29659. ConvertProgram;
  29660. CheckSource('TestJSValue_UntypedParam',
  29661. LinesToStr([ // statements
  29662. 'this.DoIt = function (a, b, c) {',
  29663. ' var Result = undefined;',
  29664. ' Result = a;',
  29665. ' Result = b.get();',
  29666. ' Result = c.get();',
  29667. ' b.set(Result);',
  29668. ' c.set(Result);',
  29669. ' return Result;',
  29670. '};',
  29671. 'this.i = 0;',
  29672. '']),
  29673. LinesToStr([ // $mod.$main
  29674. '$mod.DoIt($mod.i, {',
  29675. ' p: $mod,',
  29676. ' get: function () {',
  29677. ' return this.p.i;',
  29678. ' },',
  29679. ' set: function (v) {',
  29680. ' this.p.i = v;',
  29681. ' }',
  29682. '}, {',
  29683. ' p: $mod,',
  29684. ' get: function () {',
  29685. ' return this.p.i;',
  29686. ' },',
  29687. ' set: function (v) {',
  29688. ' this.p.i = v;',
  29689. ' }',
  29690. '});',
  29691. '']));
  29692. end;
  29693. procedure TTestModule.TestJSValue_FuncResultType;
  29694. begin
  29695. StartProgram(false);
  29696. Add('type');
  29697. Add(' integer = longint;');
  29698. Add(' TJSValueArray = array of JSValue;');
  29699. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29700. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29701. Add('begin');
  29702. Add(' while Compare(P,aList[0])>0 do ;');
  29703. Add('end;');
  29704. Add('var');
  29705. Add(' Compare: TListSortCompare;');
  29706. Add(' V: JSValue;');
  29707. Add(' i: integer;');
  29708. Add('begin');
  29709. Add(' if Compare(V,V)>0 then ;');
  29710. Add(' if Compare(i,i)>1 then ;');
  29711. Add(' if Compare(nil,false)>2 then ;');
  29712. Add(' if Compare(1,true)>3 then ;');
  29713. ConvertProgram;
  29714. CheckSource('TestJSValue_UntypedParam',
  29715. LinesToStr([ // statements
  29716. 'this.Sort = function (P, aList, Compare) {',
  29717. ' while (Compare(P, aList[0]) > 0) {',
  29718. ' };',
  29719. '};',
  29720. 'this.Compare = null;',
  29721. 'this.V = undefined;',
  29722. 'this.i = 0;',
  29723. '']),
  29724. LinesToStr([ // $mod.$main
  29725. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29726. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29727. 'if ($mod.Compare(null, false) > 2) ;',
  29728. 'if ($mod.Compare(1, true) > 3) ;',
  29729. '']));
  29730. end;
  29731. procedure TTestModule.TestJSValue_ProcType_Assign;
  29732. begin
  29733. StartProgram(false);
  29734. Add('type');
  29735. Add(' integer = longint;');
  29736. Add(' TObject = class');
  29737. Add(' class function GetGlob: integer;');
  29738. Add(' function Getter: integer;');
  29739. Add(' end;');
  29740. Add('class function TObject.GetGlob: integer;');
  29741. Add('var v1: jsvalue;');
  29742. Add('begin');
  29743. Add(' v1:=@GetGlob;');
  29744. Add(' v1:[email protected];');
  29745. Add('end;');
  29746. Add('function TObject.Getter: integer;');
  29747. Add('var v2: jsvalue;');
  29748. Add('begin');
  29749. Add(' v2:=@Getter;');
  29750. Add(' v2:[email protected];');
  29751. Add(' v2:=@GetGlob;');
  29752. Add(' v2:[email protected];');
  29753. Add('end;');
  29754. Add('function GetIt(i: integer): integer;');
  29755. Add('var v3: jsvalue;');
  29756. Add('begin');
  29757. Add(' v3:=@GetIt;');
  29758. Add('end;');
  29759. Add('var');
  29760. Add(' V: JSValue;');
  29761. Add(' o: TObject;');
  29762. Add('begin');
  29763. Add(' v:=@GetIt;');
  29764. Add(' v:[email protected];');
  29765. Add(' v:[email protected];');
  29766. ConvertProgram;
  29767. CheckSource('TestJSValue_ProcType_Assign',
  29768. LinesToStr([ // statements
  29769. 'rtl.createClass(this, "TObject", null, function () {',
  29770. ' this.$init = function () {',
  29771. ' };',
  29772. ' this.$final = function () {',
  29773. ' };',
  29774. ' this.GetGlob = function () {',
  29775. ' var Result = 0;',
  29776. ' var v1 = undefined;',
  29777. ' v1 = rtl.createCallback(this, "GetGlob");',
  29778. ' v1 = rtl.createCallback(this, "GetGlob");',
  29779. ' return Result;',
  29780. ' };',
  29781. ' this.Getter = function () {',
  29782. ' var Result = 0;',
  29783. ' var v2 = undefined;',
  29784. ' v2 = rtl.createCallback(this, "Getter");',
  29785. ' v2 = rtl.createCallback(this, "Getter");',
  29786. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29787. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29788. ' return Result;',
  29789. ' };',
  29790. '});',
  29791. 'this.GetIt = function (i) {',
  29792. ' var Result = 0;',
  29793. ' var v3 = undefined;',
  29794. ' v3 = $mod.GetIt;',
  29795. ' return Result;',
  29796. '};',
  29797. 'this.V = undefined;',
  29798. 'this.o = null;',
  29799. '']),
  29800. LinesToStr([ // $mod.$main
  29801. '$mod.V = $mod.GetIt;',
  29802. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29803. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29804. '']));
  29805. end;
  29806. procedure TTestModule.TestJSValue_ProcType_Equal;
  29807. begin
  29808. StartProgram(false);
  29809. Add('type');
  29810. Add(' integer = longint;');
  29811. Add(' TObject = class');
  29812. Add(' class function GetGlob: integer;');
  29813. Add(' function Getter: integer;');
  29814. Add(' end;');
  29815. Add('class function TObject.GetGlob: integer;');
  29816. Add('var v1: jsvalue;');
  29817. Add('begin');
  29818. Add(' if v1=@GetGlob then;');
  29819. Add(' if [email protected] then ;');
  29820. Add('end;');
  29821. Add('function TObject.Getter: integer;');
  29822. Add('var v2: jsvalue;');
  29823. Add('begin');
  29824. Add(' if v2=@Getter then;');
  29825. Add(' if [email protected] then ;');
  29826. Add(' if v2=@GetGlob then;');
  29827. Add(' if [email protected] then;');
  29828. Add('end;');
  29829. Add('function GetIt(i: integer): integer;');
  29830. Add('var v3: jsvalue;');
  29831. Add('begin');
  29832. Add(' if v3=@GetIt then;');
  29833. Add('end;');
  29834. Add('var');
  29835. Add(' V: JSValue;');
  29836. Add(' o: TObject;');
  29837. Add('begin');
  29838. Add(' if v=@GetIt then;');
  29839. Add(' if [email protected] then;');
  29840. Add(' if [email protected] then;');
  29841. Add(' if @GetIt=v then;');
  29842. Add(' if @o.Getter=v then;');
  29843. Add(' if @o.GetGlob=v then;');
  29844. ConvertProgram;
  29845. CheckSource('TestJSValue_ProcType_Equal',
  29846. LinesToStr([ // statements
  29847. 'rtl.createClass(this, "TObject", null, function () {',
  29848. ' this.$init = function () {',
  29849. ' };',
  29850. ' this.$final = function () {',
  29851. ' };',
  29852. ' this.GetGlob = function () {',
  29853. ' var Result = 0;',
  29854. ' var v1 = undefined;',
  29855. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29856. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29857. ' return Result;',
  29858. ' };',
  29859. ' this.Getter = function () {',
  29860. ' var Result = 0;',
  29861. ' var v2 = undefined;',
  29862. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29863. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29864. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29865. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29866. ' return Result;',
  29867. ' };',
  29868. '});',
  29869. 'this.GetIt = function (i) {',
  29870. ' var Result = 0;',
  29871. ' var v3 = undefined;',
  29872. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29873. ' return Result;',
  29874. '};',
  29875. 'this.V = undefined;',
  29876. 'this.o = null;',
  29877. '']),
  29878. LinesToStr([ // $mod.$main
  29879. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29880. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29881. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29882. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29883. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29884. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29885. '']));
  29886. end;
  29887. procedure TTestModule.TestJSValue_ProcType_Param;
  29888. begin
  29889. StartProgram(false);
  29890. Add([
  29891. 'type',
  29892. ' variant = jsvalue;',
  29893. ' TArrVariant = array of variant;',
  29894. ' TArrVar2 = TArrVariant;',
  29895. ' TFuncInt = function: longint;',
  29896. 'function GetIt: longint;',
  29897. 'begin',
  29898. 'end;',
  29899. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29900. 'var v: variant;',
  29901. 'begin',
  29902. ' v:=arr[1];',
  29903. 'end;',
  29904. 'var s: string;',
  29905. 'begin',
  29906. ' DoIt(GetIt,[]);',
  29907. ' DoIt(@GetIt,[]);',
  29908. ' DoIt(1,[s,GetIt]);',
  29909. ' DoIt(1,[s,@GetIt]);',
  29910. '']);
  29911. ConvertProgram;
  29912. CheckSource('TestJSValue_ProcType_Param',
  29913. LinesToStr([ // statements
  29914. 'this.GetIt = function () {',
  29915. ' var Result = 0;',
  29916. ' return Result;',
  29917. '};',
  29918. 'this.DoIt = function (p, Arr) {',
  29919. ' var v = undefined;',
  29920. ' v = Arr[1];',
  29921. '};',
  29922. 'this.s = "";',
  29923. '']),
  29924. LinesToStr([ // $mod.$main
  29925. '$mod.DoIt($mod.GetIt(), []);',
  29926. '$mod.DoIt($mod.GetIt, []);',
  29927. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29928. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29929. '']));
  29930. end;
  29931. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29932. begin
  29933. StartProgram(false);
  29934. Add([
  29935. 'var',
  29936. ' v: JSValue;',
  29937. ' p: Pointer;',
  29938. 'begin',
  29939. ' p:=v;',
  29940. '']);
  29941. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29942. nIncompatibleTypesGotExpected);
  29943. ConvertProgram;
  29944. end;
  29945. procedure TTestModule.TestJSValue_OverloadDouble;
  29946. begin
  29947. StartProgram(false);
  29948. Add([
  29949. 'type',
  29950. ' integer = longint;',
  29951. ' tdatetime = double;',
  29952. 'procedure DoIt(d: double); begin end;',
  29953. 'procedure DoIt(v: jsvalue); begin end;',
  29954. 'var',
  29955. ' d: double;',
  29956. ' dt: tdatetime;',
  29957. ' i: integer;',
  29958. ' b: byte;',
  29959. ' shi: shortint;',
  29960. ' w: word;',
  29961. ' smi: smallint;',
  29962. ' lw: longword;',
  29963. ' li: longint;',
  29964. ' ni: nativeint;',
  29965. ' nu: nativeuint;',
  29966. 'begin',
  29967. ' DoIt(d);',
  29968. ' DoIt(dt);',
  29969. ' DoIt(i);',
  29970. ' DoIt(b);',
  29971. ' DoIt(shi);',
  29972. ' DoIt(w);',
  29973. ' DoIt(smi);',
  29974. ' DoIt(lw);',
  29975. ' DoIt(li);',
  29976. ' DoIt(ni);',
  29977. ' DoIt(nu);',
  29978. '']);
  29979. ConvertProgram;
  29980. CheckSource('TestJSValue_OverloadDouble',
  29981. LinesToStr([ // statements
  29982. 'this.DoIt = function (d) {',
  29983. '};',
  29984. 'this.DoIt$1 = function (v) {',
  29985. '};',
  29986. 'this.d = 0.0;',
  29987. 'this.dt = 0.0;',
  29988. 'this.i = 0;',
  29989. 'this.b = 0;',
  29990. 'this.shi = 0;',
  29991. 'this.w = 0;',
  29992. 'this.smi = 0;',
  29993. 'this.lw = 0;',
  29994. 'this.li = 0;',
  29995. 'this.ni = 0;',
  29996. 'this.nu = 0;',
  29997. '']),
  29998. LinesToStr([ // $mod.$main
  29999. '$mod.DoIt($mod.d);',
  30000. '$mod.DoIt($mod.dt);',
  30001. '$mod.DoIt$1($mod.i);',
  30002. '$mod.DoIt$1($mod.b);',
  30003. '$mod.DoIt$1($mod.shi);',
  30004. '$mod.DoIt$1($mod.w);',
  30005. '$mod.DoIt$1($mod.smi);',
  30006. '$mod.DoIt$1($mod.lw);',
  30007. '$mod.DoIt$1($mod.li);',
  30008. '$mod.DoIt$1($mod.ni);',
  30009. '$mod.DoIt$1($mod.nu);',
  30010. '']));
  30011. end;
  30012. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30013. begin
  30014. StartProgram(false);
  30015. Add([
  30016. 'type',
  30017. ' integer = longint;',
  30018. ' int53 = nativeint;',
  30019. ' tdatetime = double;',
  30020. 'procedure DoIt(n: nativeint); begin end;',
  30021. 'procedure DoIt(v: jsvalue); begin end;',
  30022. 'var',
  30023. ' d: double;',
  30024. ' dt: tdatetime;',
  30025. ' i: integer;',
  30026. ' b: byte;',
  30027. ' shi: shortint;',
  30028. ' w: word;',
  30029. ' smi: smallint;',
  30030. ' lw: longword;',
  30031. ' li: longint;',
  30032. ' ni: nativeint;',
  30033. ' nu: nativeuint;',
  30034. 'begin',
  30035. ' DoIt(d);',
  30036. ' DoIt(dt);',
  30037. ' DoIt(i);',
  30038. ' DoIt(b);',
  30039. ' DoIt(shi);',
  30040. ' DoIt(w);',
  30041. ' DoIt(smi);',
  30042. ' DoIt(lw);',
  30043. ' DoIt(li);',
  30044. ' DoIt(ni);',
  30045. ' DoIt(nu);',
  30046. '']);
  30047. ConvertProgram;
  30048. CheckSource('TestJSValue_OverloadNativeInt',
  30049. LinesToStr([ // statements
  30050. 'this.DoIt = function (n) {',
  30051. '};',
  30052. 'this.DoIt$1 = function (v) {',
  30053. '};',
  30054. 'this.d = 0.0;',
  30055. 'this.dt = 0.0;',
  30056. 'this.i = 0;',
  30057. 'this.b = 0;',
  30058. 'this.shi = 0;',
  30059. 'this.w = 0;',
  30060. 'this.smi = 0;',
  30061. 'this.lw = 0;',
  30062. 'this.li = 0;',
  30063. 'this.ni = 0;',
  30064. 'this.nu = 0;',
  30065. '']),
  30066. LinesToStr([ // $mod.$main
  30067. '$mod.DoIt$1($mod.d);',
  30068. '$mod.DoIt$1($mod.dt);',
  30069. '$mod.DoIt($mod.i);',
  30070. '$mod.DoIt($mod.b);',
  30071. '$mod.DoIt($mod.shi);',
  30072. '$mod.DoIt($mod.w);',
  30073. '$mod.DoIt($mod.smi);',
  30074. '$mod.DoIt($mod.lw);',
  30075. '$mod.DoIt($mod.li);',
  30076. '$mod.DoIt($mod.ni);',
  30077. '$mod.DoIt($mod.nu);',
  30078. '']));
  30079. end;
  30080. procedure TTestModule.TestJSValue_OverloadWord;
  30081. begin
  30082. StartProgram(false);
  30083. Add([
  30084. 'type',
  30085. ' integer = longint;',
  30086. ' int53 = nativeint;',
  30087. ' tdatetime = double;',
  30088. 'procedure DoIt(w: word); begin end;',
  30089. 'procedure DoIt(v: jsvalue); begin end;',
  30090. 'var',
  30091. ' d: double;',
  30092. ' dt: tdatetime;',
  30093. ' i: integer;',
  30094. ' b: byte;',
  30095. ' shi: shortint;',
  30096. ' w: word;',
  30097. ' smi: smallint;',
  30098. ' lw: longword;',
  30099. ' li: longint;',
  30100. ' ni: nativeint;',
  30101. ' nu: nativeuint;',
  30102. 'begin',
  30103. ' DoIt(d);',
  30104. ' DoIt(dt);',
  30105. ' DoIt(i);',
  30106. ' DoIt(b);',
  30107. ' DoIt(shi);',
  30108. ' DoIt(w);',
  30109. ' DoIt(smi);',
  30110. ' DoIt(lw);',
  30111. ' DoIt(li);',
  30112. ' DoIt(ni);',
  30113. ' DoIt(nu);',
  30114. '']);
  30115. ConvertProgram;
  30116. CheckSource('TestJSValue_OverloadWord',
  30117. LinesToStr([ // statements
  30118. 'this.DoIt = function (w) {',
  30119. '};',
  30120. 'this.DoIt$1 = function (v) {',
  30121. '};',
  30122. 'this.d = 0.0;',
  30123. 'this.dt = 0.0;',
  30124. 'this.i = 0;',
  30125. 'this.b = 0;',
  30126. 'this.shi = 0;',
  30127. 'this.w = 0;',
  30128. 'this.smi = 0;',
  30129. 'this.lw = 0;',
  30130. 'this.li = 0;',
  30131. 'this.ni = 0;',
  30132. 'this.nu = 0;',
  30133. '']),
  30134. LinesToStr([ // $mod.$main
  30135. '$mod.DoIt$1($mod.d);',
  30136. '$mod.DoIt$1($mod.dt);',
  30137. '$mod.DoIt$1($mod.i);',
  30138. '$mod.DoIt($mod.b);',
  30139. '$mod.DoIt($mod.shi);',
  30140. '$mod.DoIt($mod.w);',
  30141. '$mod.DoIt$1($mod.smi);',
  30142. '$mod.DoIt$1($mod.lw);',
  30143. '$mod.DoIt$1($mod.li);',
  30144. '$mod.DoIt$1($mod.ni);',
  30145. '$mod.DoIt$1($mod.nu);',
  30146. '']));
  30147. end;
  30148. procedure TTestModule.TestJSValue_OverloadString;
  30149. begin
  30150. StartProgram(false);
  30151. Add([
  30152. 'type',
  30153. ' uni = string;',
  30154. ' WChar = char;',
  30155. 'procedure DoIt(s: string); begin end;',
  30156. 'procedure DoIt(v: jsvalue); begin end;',
  30157. 'var',
  30158. ' s: string;',
  30159. ' c: char;',
  30160. ' u: uni;',
  30161. 'begin',
  30162. ' DoIt(s);',
  30163. ' DoIt(c);',
  30164. ' DoIt(u);',
  30165. '']);
  30166. ConvertProgram;
  30167. CheckSource('TestJSValue_OverloadString',
  30168. LinesToStr([ // statements
  30169. 'this.DoIt = function (s) {',
  30170. '};',
  30171. 'this.DoIt$1 = function (v) {',
  30172. '};',
  30173. 'this.s = "";',
  30174. 'this.c = "";',
  30175. 'this.u = "";',
  30176. '']),
  30177. LinesToStr([ // $mod.$main
  30178. '$mod.DoIt($mod.s);',
  30179. '$mod.DoIt($mod.c);',
  30180. '$mod.DoIt($mod.u);',
  30181. '']));
  30182. end;
  30183. procedure TTestModule.TestJSValue_OverloadChar;
  30184. begin
  30185. StartProgram(false);
  30186. Add([
  30187. 'type',
  30188. ' uni = string;',
  30189. ' WChar = char;',
  30190. 'procedure DoIt(c: char); begin end;',
  30191. 'procedure DoIt(v: jsvalue); begin end;',
  30192. 'var',
  30193. ' s: string;',
  30194. ' c: char;',
  30195. ' u: uni;',
  30196. 'begin',
  30197. ' DoIt(s);',
  30198. ' DoIt(c);',
  30199. ' DoIt(u);',
  30200. '']);
  30201. ConvertProgram;
  30202. CheckSource('TestJSValue_OverloadChar',
  30203. LinesToStr([ // statements
  30204. 'this.DoIt = function (c) {',
  30205. '};',
  30206. 'this.DoIt$1 = function (v) {',
  30207. '};',
  30208. 'this.s = "";',
  30209. 'this.c = "";',
  30210. 'this.u = "";',
  30211. '']),
  30212. LinesToStr([ // $mod.$main
  30213. '$mod.DoIt$1($mod.s);',
  30214. '$mod.DoIt($mod.c);',
  30215. '$mod.DoIt$1($mod.u);',
  30216. '']));
  30217. end;
  30218. procedure TTestModule.TestJSValue_OverloadPointer;
  30219. begin
  30220. StartProgram(false);
  30221. Add([
  30222. 'type',
  30223. ' TObject = class end;',
  30224. 'procedure DoIt(p: pointer); begin end;',
  30225. 'procedure DoIt(v: jsvalue); begin end;',
  30226. 'var',
  30227. ' o: TObject;',
  30228. 'begin',
  30229. ' DoIt(o);',
  30230. '']);
  30231. ConvertProgram;
  30232. CheckSource('TestJSValue_OverloadPointer',
  30233. LinesToStr([ // statements
  30234. 'rtl.createClass(this, "TObject", null, function () {',
  30235. ' this.$init = function () {',
  30236. ' };',
  30237. ' this.$final = function () {',
  30238. ' };',
  30239. '});',
  30240. 'this.DoIt = function (p) {',
  30241. '};',
  30242. 'this.DoIt$1 = function (v) {',
  30243. '};',
  30244. 'this.o = null;',
  30245. '']),
  30246. LinesToStr([ // $mod.$main
  30247. '$mod.DoIt($mod.o);',
  30248. '']));
  30249. end;
  30250. procedure TTestModule.TestJSValue_ForIn;
  30251. begin
  30252. StartProgram(false);
  30253. Add([
  30254. 'var',
  30255. ' v: JSValue;',
  30256. ' key: string;',
  30257. 'begin',
  30258. ' for key in v do begin',
  30259. ' if key=''abc'' then ;',
  30260. ' end;',
  30261. '']);
  30262. ConvertProgram;
  30263. CheckSource('TestJSValue_ForIn',
  30264. LinesToStr([ // statements
  30265. 'this.v = undefined;',
  30266. 'this.key = "";',
  30267. '']),
  30268. LinesToStr([ // $mod.$main
  30269. 'for ($mod.key in $mod.v) {',
  30270. ' if ($mod.key === "abc") ;',
  30271. '};',
  30272. '']));
  30273. end;
  30274. procedure TTestModule.TestRTTI_IntRange;
  30275. begin
  30276. WithTypeInfo:=true;
  30277. StartProgram(true,[supTypeInfo]);
  30278. Add([
  30279. '{$modeswitch externalclass}',
  30280. 'type',
  30281. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30282. ' TColor = type TGraphicsColor;',
  30283. 'var',
  30284. ' p: TTypeInfo;',
  30285. ' k: TTypeKind;',
  30286. 'begin',
  30287. ' p:=typeinfo(TGraphicsColor);',
  30288. ' p:=typeinfo(TColor);',
  30289. ' k:=GetTypeKind(TGraphicsColor);',
  30290. ' k:=GetTypeKind(TColor);',
  30291. '']);
  30292. ConvertProgram;
  30293. CheckSource('TestRTTI_IntRange',
  30294. LinesToStr([ // statements
  30295. 'this.$rtti.$Int("TGraphicsColor", {',
  30296. ' minvalue: -2147483648,',
  30297. ' maxvalue: 2147483647,',
  30298. ' ordtype: 4',
  30299. '});',
  30300. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30301. 'this.p = null;',
  30302. 'this.k = 0;',
  30303. '']),
  30304. LinesToStr([ // $mod.$main
  30305. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30306. '$mod.p = $mod.$rtti["TColor"];',
  30307. '$mod.k = 1;',
  30308. '$mod.k = 1;',
  30309. '']));
  30310. end;
  30311. procedure TTestModule.TestRTTI_Double;
  30312. begin
  30313. WithTypeInfo:=true;
  30314. StartProgram(true,[supTypeInfo]);
  30315. Add([
  30316. '{$modeswitch externalclass}',
  30317. 'type',
  30318. ' TFloat = type double;',
  30319. 'var',
  30320. ' p: TTypeInfo;',
  30321. 'begin',
  30322. ' p:=typeinfo(double);',
  30323. ' p:=typeinfo(TFloat);',
  30324. '']);
  30325. ConvertProgram;
  30326. CheckSource('TestRTTI_Double',
  30327. LinesToStr([ // statements
  30328. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30329. 'this.p = null;',
  30330. '']),
  30331. LinesToStr([ // $mod.$main
  30332. '$mod.p = rtl.double;',
  30333. '$mod.p = $mod.$rtti["TFloat"];',
  30334. '']));
  30335. end;
  30336. procedure TTestModule.TestRTTI_ProcType;
  30337. begin
  30338. WithTypeInfo:=true;
  30339. StartProgram(false);
  30340. Add('type');
  30341. Add(' TProcA = procedure;');
  30342. Add(' TMethodB = procedure of object;');
  30343. Add(' TProcC = procedure; varargs;');
  30344. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30345. Add(' TProcE = function: nativeint;');
  30346. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30347. Add('var p: pointer;');
  30348. Add('begin');
  30349. Add(' p:=typeinfo(tproca);');
  30350. ConvertProgram;
  30351. CheckSource('TestRTTI_ProcType',
  30352. LinesToStr([ // statements
  30353. 'this.$rtti.$ProcVar("TProcA", {',
  30354. ' procsig: rtl.newTIProcSig([])',
  30355. '});',
  30356. 'this.$rtti.$MethodVar("TMethodB", {',
  30357. ' procsig: rtl.newTIProcSig([]),',
  30358. ' methodkind: 0',
  30359. '});',
  30360. 'this.$rtti.$ProcVar("TProcC", {',
  30361. ' procsig: rtl.newTIProcSig([], null, 2)',
  30362. '});',
  30363. 'this.$rtti.$ProcVar("TProcD", {',
  30364. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30365. '});',
  30366. 'this.$rtti.$ProcVar("TProcE", {',
  30367. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30368. '});',
  30369. 'this.$rtti.$ProcVar("TProcF", {',
  30370. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30371. '});',
  30372. 'this.p = null;',
  30373. '']),
  30374. LinesToStr([ // $mod.$main
  30375. '$mod.p = $mod.$rtti["TProcA"];',
  30376. '']));
  30377. end;
  30378. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30379. begin
  30380. WithTypeInfo:=true;
  30381. AddModuleWithIntfImplSrc('unit2.pas',
  30382. LinesToStr([
  30383. 'type',
  30384. ' TObject = class end;'
  30385. ]),
  30386. '');
  30387. StartUnit(true);
  30388. Add('interface');
  30389. Add('uses unit2;');
  30390. Add('type');
  30391. Add(' TProcA = function(o: tobject): tobject;');
  30392. Add('implementation');
  30393. Add('type');
  30394. Add(' TProcB = function(o: tobject): tobject;');
  30395. Add('var p: Pointer;');
  30396. Add('initialization');
  30397. Add(' p:=typeinfo(tproca);');
  30398. Add(' p:=typeinfo(tprocb);');
  30399. ConvertUnit;
  30400. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30401. LinesToStr([ // statements
  30402. 'var $impl = $mod.$impl;',
  30403. 'this.$rtti.$ProcVar("TProcA", {',
  30404. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30405. '});',
  30406. '']),
  30407. LinesToStr([ // this.$init
  30408. '$impl.p = $mod.$rtti["TProcA"];',
  30409. '$impl.p = $mod.$rtti["TProcB"];',
  30410. '']),
  30411. LinesToStr([ // implementation
  30412. '$mod.$rtti.$ProcVar("TProcB", {',
  30413. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30414. '});',
  30415. '$impl.p = null;',
  30416. '']) );
  30417. end;
  30418. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30419. begin
  30420. WithTypeInfo:=true;
  30421. StartProgram(false);
  30422. Add(['var',
  30423. ' ProcA: procedure;',
  30424. ' MethodB: procedure of object;',
  30425. ' ProcC: procedure; varargs;',
  30426. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30427. ' ProcE: function: nativeint;',
  30428. ' p: pointer;',
  30429. 'begin',
  30430. ' p:=typeinfo(proca);']);
  30431. ConvertProgram;
  30432. CheckSource('TestRTTI_ProcTypeAnonymous',
  30433. LinesToStr([ // statements
  30434. 'this.$rtti.$ProcVar("ProcA$a", {',
  30435. ' procsig: rtl.newTIProcSig([])',
  30436. '});',
  30437. 'this.ProcA = null;',
  30438. 'this.$rtti.$MethodVar("MethodB$a", {',
  30439. ' procsig: rtl.newTIProcSig([]),',
  30440. ' methodkind: 0',
  30441. '});',
  30442. 'this.MethodB = null;',
  30443. 'this.$rtti.$ProcVar("ProcC$a", {',
  30444. ' procsig: rtl.newTIProcSig([], null, 2)',
  30445. '});',
  30446. 'this.ProcC = null;',
  30447. 'this.$rtti.$ProcVar("ProcD$a", {',
  30448. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30449. '});',
  30450. 'this.ProcD = null;',
  30451. 'this.$rtti.$ProcVar("ProcE$a", {',
  30452. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30453. '});',
  30454. 'this.ProcE = null;',
  30455. 'this.p = null;',
  30456. '']),
  30457. LinesToStr([ // $mod.$main
  30458. '$mod.p = $mod.$rtti["ProcA$a"];',
  30459. '']));
  30460. end;
  30461. procedure TTestModule.TestRTTI_EnumAndSetType;
  30462. begin
  30463. WithTypeInfo:=true;
  30464. StartProgram(false);
  30465. Add('type');
  30466. Add(' TFlag = (light,dark);');
  30467. Add(' TFlags = set of TFlag;');
  30468. Add(' TProc = function(f: TFlags): TFlag;');
  30469. Add('var p: pointer;');
  30470. Add('begin');
  30471. Add(' p:=typeinfo(tflag);');
  30472. Add(' p:=typeinfo(tflags);');
  30473. ConvertProgram;
  30474. CheckSource('TestRTTI_EnumAndType',
  30475. LinesToStr([ // statements
  30476. 'this.TFlag = {',
  30477. ' "0": "light",',
  30478. ' light: 0,',
  30479. ' "1": "dark",',
  30480. ' dark: 1',
  30481. '};',
  30482. 'this.$rtti.$Enum("TFlag", {',
  30483. ' minvalue: 0,',
  30484. ' maxvalue: 1,',
  30485. ' ordtype: 1,',
  30486. ' enumtype: this.TFlag',
  30487. '});',
  30488. 'this.$rtti.$Set("TFlags", {',
  30489. ' comptype: this.$rtti["TFlag"]',
  30490. '});',
  30491. 'this.$rtti.$ProcVar("TProc", {',
  30492. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30493. '});',
  30494. 'this.p = null;',
  30495. '']),
  30496. LinesToStr([ // $mod.$main
  30497. '$mod.p = $mod.$rtti["TFlag"];',
  30498. '$mod.p = $mod.$rtti["TFlags"];',
  30499. '']));
  30500. end;
  30501. procedure TTestModule.TestRTTI_EnumRange;
  30502. begin
  30503. WithTypeInfo:=true;
  30504. StartProgram(false);
  30505. Add([
  30506. 'type',
  30507. ' TCol = (red,green,blue);',
  30508. ' TColRg = green..blue;',
  30509. ' TSetOfColRg = set of TColRg;',
  30510. 'var p: pointer;',
  30511. 'begin',
  30512. ' p:=typeinfo(tcolrg);',
  30513. ' p:=typeinfo(tsetofcolrg);',
  30514. '']);
  30515. ConvertProgram;
  30516. end;
  30517. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30518. begin
  30519. WithTypeInfo:=true;
  30520. StartProgram(false);
  30521. Add('type');
  30522. Add(' TFlags = set of (red, green);');
  30523. Add('var');
  30524. Add(' f: TFlags;');
  30525. Add('begin');
  30526. Add(' Include(f,red);');
  30527. ConvertProgram;
  30528. CheckSource('TestRTTI_AnonymousEnumType',
  30529. LinesToStr([ // statements
  30530. 'this.TFlags$a = {',
  30531. ' "0": "red",',
  30532. ' red: 0,',
  30533. ' "1": "green",',
  30534. ' green: 1',
  30535. '};',
  30536. 'this.$rtti.$Enum("TFlags$a", {',
  30537. ' minvalue: 0,',
  30538. ' maxvalue: 1,',
  30539. ' ordtype: 1,',
  30540. ' enumtype: this.TFlags$a',
  30541. '});',
  30542. 'this.$rtti.$Set("TFlags", {',
  30543. ' comptype: this.$rtti["TFlags$a"]',
  30544. '});',
  30545. 'this.f = {};',
  30546. '']),
  30547. LinesToStr([
  30548. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30549. '']));
  30550. end;
  30551. procedure TTestModule.TestRTTI_StaticArray;
  30552. begin
  30553. WithTypeInfo:=true;
  30554. StartProgram(false);
  30555. Add('type');
  30556. Add(' TFlag = (light,dark);');
  30557. Add(' TFlagNames = array[TFlag] of string;');
  30558. Add(' TBoolNames = array[boolean] of string;');
  30559. Add(' TByteArray = array[1..32768] of byte;');
  30560. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30561. Add('var p: pointer;');
  30562. Add('begin');
  30563. Add(' p:=typeinfo(TFlagNames);');
  30564. Add(' p:=typeinfo(TBoolNames);');
  30565. ConvertProgram;
  30566. CheckSource('TestRTTI_StaticArray',
  30567. LinesToStr([ // statements
  30568. 'this.TFlag = {',
  30569. ' "0": "light",',
  30570. ' light: 0,',
  30571. ' "1": "dark",',
  30572. ' dark: 1',
  30573. '};',
  30574. 'this.$rtti.$Enum("TFlag", {',
  30575. ' minvalue: 0,',
  30576. ' maxvalue: 1,',
  30577. ' ordtype: 1,',
  30578. ' enumtype: this.TFlag',
  30579. '});',
  30580. 'this.$rtti.$StaticArray("TFlagNames", {',
  30581. ' dims: [2],',
  30582. ' eltype: rtl.string',
  30583. '});',
  30584. 'this.$rtti.$StaticArray("TBoolNames", {',
  30585. ' dims: [2],',
  30586. ' eltype: rtl.string',
  30587. '});',
  30588. 'this.$rtti.$StaticArray("TByteArray", {',
  30589. ' dims: [32768],',
  30590. ' eltype: rtl.byte',
  30591. '});',
  30592. 'this.$rtti.$ProcVar("TProc", {',
  30593. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30594. '});',
  30595. 'this.p = null;',
  30596. '']),
  30597. LinesToStr([ // $mod.$main
  30598. '$mod.p = $mod.$rtti["TFlagNames"];',
  30599. '$mod.p = $mod.$rtti["TBoolNames"];',
  30600. '']));
  30601. end;
  30602. procedure TTestModule.TestRTTI_DynArray;
  30603. begin
  30604. WithTypeInfo:=true;
  30605. StartProgram(false);
  30606. Add('type');
  30607. Add(' TArrStr = array of string;');
  30608. Add(' TArr2Dim = array of tarrstr;');
  30609. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30610. Add('var p: pointer;');
  30611. Add('begin');
  30612. Add(' p:=typeinfo(tarrstr);');
  30613. Add(' p:=typeinfo(tarr2dim);');
  30614. ConvertProgram;
  30615. CheckSource('TestRTTI_DynArray',
  30616. LinesToStr([ // statements
  30617. 'this.$rtti.$DynArray("TArrStr", {',
  30618. ' eltype: rtl.string',
  30619. '});',
  30620. 'this.$rtti.$DynArray("TArr2Dim", {',
  30621. ' eltype: this.$rtti["TArrStr"]',
  30622. '});',
  30623. 'this.$rtti.$ProcVar("TProc", {',
  30624. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30625. '});',
  30626. 'this.p = null;',
  30627. '']),
  30628. LinesToStr([ // $mod.$main
  30629. '$mod.p = $mod.$rtti["TArrStr"];',
  30630. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30631. '']));
  30632. end;
  30633. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30634. begin
  30635. WithTypeInfo:=true;
  30636. StartProgram(false);
  30637. Add('type');
  30638. Add(' TArr = array of array of longint;');
  30639. Add('var a: TArr;');
  30640. Add('begin');
  30641. ConvertProgram;
  30642. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30643. LinesToStr([ // statements
  30644. 'this.$rtti.$DynArray("TArr$a", {',
  30645. ' eltype: rtl.longint',
  30646. '});',
  30647. 'this.$rtti.$DynArray("TArr", {',
  30648. ' eltype: this.$rtti["TArr$a"]',
  30649. '});',
  30650. 'this.a = [];',
  30651. '']),
  30652. LinesToStr([ // $mod.$main
  30653. ]));
  30654. end;
  30655. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30656. begin
  30657. WithTypeInfo:=true;
  30658. StartProgram(false);
  30659. Add('type');
  30660. Add(' TObject = class');
  30661. Add(' published');
  30662. Add(' procedure Proc; virtual; abstract;');
  30663. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30664. Add(' end;');
  30665. Add('begin');
  30666. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30667. nDuplicatePublishedMethodXAtY);
  30668. ConvertProgram;
  30669. end;
  30670. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30671. begin
  30672. WithTypeInfo:=true;
  30673. StartUnit(false);
  30674. Add([
  30675. 'interface',
  30676. 'type',
  30677. ' TObject = class',
  30678. ' end;',
  30679. ' {$M+}',
  30680. ' TBird = class',
  30681. ' procedure Fly;',
  30682. ' end;',
  30683. ' {$M-}',
  30684. 'type',
  30685. ' TEagle = class(TBird)',
  30686. ' procedure Fly;',
  30687. ' end;',
  30688. 'implementation',
  30689. 'procedure TBird.Fly;',
  30690. 'begin',
  30691. 'end;',
  30692. 'procedure TEagle.Fly;',
  30693. 'begin',
  30694. 'end;',
  30695. '']);
  30696. ConvertUnit;
  30697. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30698. LinesToStr([ // statements
  30699. 'rtl.createClass(this, "TObject", null, function () {',
  30700. ' this.$init = function () {',
  30701. ' };',
  30702. ' this.$final = function () {',
  30703. ' };',
  30704. '});',
  30705. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30706. ' this.Fly = function () {',
  30707. ' };',
  30708. ' var $r = this.$rtti;',
  30709. ' $r.addMethod("Fly", 0, []);',
  30710. '});',
  30711. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30712. ' this.Fly = function () {',
  30713. ' };',
  30714. ' var $r = this.$rtti;',
  30715. ' $r.addMethod("Fly", 0, []);',
  30716. '});',
  30717. '']),
  30718. LinesToStr([ // $mod.$main
  30719. ]));
  30720. CheckResolverUnexpectedHints(true);
  30721. end;
  30722. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30723. begin
  30724. WithTypeInfo:=true;
  30725. StartProgram(false);
  30726. Add('type');
  30727. Add(' TObject = class');
  30728. Add(' published');
  30729. Add(' procedure Proc; external name ''foo'';');
  30730. Add(' end;');
  30731. Add('begin');
  30732. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30733. nPublishedNameMustMatchExternal);
  30734. ConvertProgram;
  30735. end;
  30736. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30737. begin
  30738. WithTypeInfo:=true;
  30739. StartProgram(false);
  30740. Add('type');
  30741. Add(' TObject = class');
  30742. Add(' class var FA: longint;');
  30743. Add(' published');
  30744. Add(' class property A: longint read FA;');
  30745. Add(' end;');
  30746. Add('begin');
  30747. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30748. nInvalidXModifierY);
  30749. ConvertProgram;
  30750. end;
  30751. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30752. begin
  30753. WithTypeInfo:=true;
  30754. StartProgram(false);
  30755. Add('type');
  30756. Add(' TObject = class');
  30757. Add(' published');
  30758. Add(' class var FA: longint;');
  30759. Add(' end;');
  30760. Add('begin');
  30761. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30762. nSymbolCannotBePublished);
  30763. ConvertProgram;
  30764. end;
  30765. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30766. begin
  30767. WithTypeInfo:=true;
  30768. StartProgram(false);
  30769. Add('{$modeswitch externalclass}');
  30770. Add('type');
  30771. Add(' TObject = class');
  30772. Add(' published');
  30773. Add(' V: longint; external name ''foo'';');
  30774. Add(' end;');
  30775. Add('begin');
  30776. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30777. nPublishedNameMustMatchExternal);
  30778. ConvertProgram;
  30779. end;
  30780. procedure TTestModule.TestRTTI_Class_Field;
  30781. begin
  30782. WithTypeInfo:=true;
  30783. StartProgram(false);
  30784. Add('{$modeswitch externalclass}');
  30785. Add('type');
  30786. Add(' TObject = class');
  30787. Add(' private');
  30788. Add(' FPropA: string;');
  30789. Add(' published');
  30790. Add(' VarLI: longint;');
  30791. Add(' VarC: char;');
  30792. Add(' VarS: string;');
  30793. Add(' VarD: double;');
  30794. Add(' VarB: boolean;');
  30795. Add(' VarLW: longword;');
  30796. Add(' VarSmI: smallint;');
  30797. Add(' VarW: word;');
  30798. Add(' VarShI: shortint;');
  30799. Add(' VarBy: byte;');
  30800. Add(' VarExt: longint external name ''VarExt'';');
  30801. Add(' ArrA, ArrB: array of byte;');
  30802. Add(' end;');
  30803. Add('var p: pointer;');
  30804. Add(' Obj: tobject;');
  30805. Add('begin');
  30806. Add(' p:=typeinfo(tobject);');
  30807. Add(' p:=typeinfo(p);');
  30808. Add(' p:=typeinfo(obj);');
  30809. ConvertProgram;
  30810. CheckSource('TestRTTI_Class_Field',
  30811. LinesToStr([ // statements
  30812. 'rtl.createClass(this, "TObject", null, function () {',
  30813. ' this.$init = function () {',
  30814. ' this.FPropA = "";',
  30815. ' this.VarLI = 0;',
  30816. ' this.VarC = "";',
  30817. ' this.VarS = "";',
  30818. ' this.VarD = 0.0;',
  30819. ' this.VarB = false;',
  30820. ' this.VarLW = 0;',
  30821. ' this.VarSmI = 0;',
  30822. ' this.VarW = 0;',
  30823. ' this.VarShI = 0;',
  30824. ' this.VarBy = 0;',
  30825. ' this.ArrA = [];',
  30826. ' this.ArrB = [];',
  30827. ' };',
  30828. ' this.$final = function () {',
  30829. ' this.ArrA = undefined;',
  30830. ' this.ArrB = undefined;',
  30831. ' };',
  30832. ' var $r = this.$rtti;',
  30833. ' $r.addField("VarLI", rtl.longint);',
  30834. ' $r.addField("VarC", rtl.char);',
  30835. ' $r.addField("VarS", rtl.string);',
  30836. ' $r.addField("VarD", rtl.double);',
  30837. ' $r.addField("VarB", rtl.boolean);',
  30838. ' $r.addField("VarLW", rtl.longword);',
  30839. ' $r.addField("VarSmI", rtl.smallint);',
  30840. ' $r.addField("VarW", rtl.word);',
  30841. ' $r.addField("VarShI", rtl.shortint);',
  30842. ' $r.addField("VarBy", rtl.byte);',
  30843. ' $r.addField("VarExt", rtl.longint);',
  30844. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30845. ' eltype: rtl.byte',
  30846. ' });',
  30847. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30848. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30849. '});',
  30850. 'this.p = null;',
  30851. 'this.Obj = null;',
  30852. '']),
  30853. LinesToStr([ // $mod.$main
  30854. '$mod.p = $mod.$rtti["TObject"];',
  30855. '$mod.p = rtl.pointer;',
  30856. '$mod.p = $mod.Obj.$rtti;',
  30857. '']));
  30858. end;
  30859. procedure TTestModule.TestRTTI_Class_Method;
  30860. begin
  30861. WithTypeInfo:=true;
  30862. StartProgram(false);
  30863. Add([
  30864. 'type',
  30865. ' TObject = class',
  30866. ' private',
  30867. ' procedure Internal; external name ''$intern'';',
  30868. ' published',
  30869. ' procedure Click; virtual; abstract;',
  30870. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30871. ' function GetNotify: boolean; external name ''GetNotify'';',
  30872. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30873. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30874. ' end;',
  30875. 'begin']);
  30876. ConvertProgram;
  30877. CheckSource('TestRTTI_Class_Method',
  30878. LinesToStr([ // statements
  30879. 'rtl.createClass(this, "TObject", null, function () {',
  30880. ' this.$init = function () {',
  30881. ' };',
  30882. ' this.$final = function () {',
  30883. ' };',
  30884. ' var $r = this.$rtti;',
  30885. ' $r.addMethod("Click", 0, []);',
  30886. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30887. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30888. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30889. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30890. '});',
  30891. '']),
  30892. LinesToStr([ // $mod.$main
  30893. '']));
  30894. end;
  30895. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30896. begin
  30897. WithTypeInfo:=true;
  30898. StartProgram(false);
  30899. Add('type');
  30900. Add(' TObject = class');
  30901. Add(' published');
  30902. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30903. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30904. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30905. Add(' end;');
  30906. Add('begin');
  30907. ConvertProgram;
  30908. CheckSource('TestRTTI_Class_MethodOpenArray',
  30909. LinesToStr([ // statements
  30910. 'rtl.createClass(this, "TObject", null, function () {',
  30911. ' this.$init = function () {',
  30912. ' };',
  30913. ' this.$final = function () {',
  30914. ' };',
  30915. ' var $r = this.$rtti;',
  30916. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30917. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30918. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30919. '});',
  30920. '']),
  30921. LinesToStr([ // $mod.$main
  30922. '']));
  30923. end;
  30924. procedure TTestModule.TestRTTI_Class_Property;
  30925. begin
  30926. WithTypeInfo:=true;
  30927. StartProgram(false);
  30928. Add('{$modeswitch externalclass}');
  30929. Add('type');
  30930. Add(' TObject = class');
  30931. Add(' private');
  30932. Add(' FColor: longint;');
  30933. Add(' FColorStored: boolean;');
  30934. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30935. Add(' function GetColor: longint; virtual; abstract;');
  30936. Add(' function GetColorStored: boolean; virtual; abstract;');
  30937. Add(' FExtSize: longint external name ''$extSize'';');
  30938. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30939. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30940. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30941. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30942. Add(' published');
  30943. Add(' property ColorA: longint read FColor;');
  30944. Add(' property ColorB: longint write FColor;');
  30945. Add(' property ColorC: longint read GetColor write SetColor;');
  30946. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30947. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30948. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30949. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30950. Add(' end;');
  30951. Add('begin');
  30952. ConvertProgram;
  30953. CheckSource('TestRTTI_Class_Property',
  30954. LinesToStr([ // statements
  30955. 'rtl.createClass(this, "TObject", null, function () {',
  30956. ' this.$init = function () {',
  30957. ' this.FColor = 0;',
  30958. ' this.FColorStored = false;',
  30959. ' };',
  30960. ' this.$final = function () {',
  30961. ' };',
  30962. ' var $r = this.$rtti;',
  30963. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30964. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30965. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30966. ' $r.addProperty(',
  30967. ' "ColorD",',
  30968. ' 8,',
  30969. ' rtl.longint,',
  30970. ' "FColor",',
  30971. ' "FColor",',
  30972. ' {',
  30973. ' stored: "FColorStored"',
  30974. ' }',
  30975. ' );',
  30976. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30977. ' $r.addProperty(',
  30978. ' "ExtSizeB",',
  30979. ' 11,',
  30980. ' rtl.longint,',
  30981. ' "$getSize",',
  30982. ' "$setSize",',
  30983. ' {',
  30984. ' stored: "$extSizeStored"',
  30985. ' }',
  30986. ' );',
  30987. ' $r.addProperty(',
  30988. ' "ExtSizeC",',
  30989. ' 12,',
  30990. ' rtl.longint,',
  30991. ' "$extSize",',
  30992. ' "$extSize",',
  30993. ' {',
  30994. ' stored: "$getExtSizeStored"',
  30995. ' }',
  30996. ' );',
  30997. '});',
  30998. '']),
  30999. LinesToStr([ // $mod.$main
  31000. '']));
  31001. end;
  31002. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31003. begin
  31004. WithTypeInfo:=true;
  31005. StartProgram(false);
  31006. Add('{$modeswitch externalclass}');
  31007. Add('type');
  31008. Add(' integer = longint;');
  31009. Add(' TObject = class');
  31010. Add(' private');
  31011. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31012. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31013. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31014. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31015. Add(' published');
  31016. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31017. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31018. Add(' end;');
  31019. Add('begin');
  31020. ConvertProgram;
  31021. CheckSource('TestRTTI_Class_PropertyParams',
  31022. LinesToStr([ // statements
  31023. 'rtl.createClass(this, "TObject", null, function () {',
  31024. ' this.$init = function () {',
  31025. ' };',
  31026. ' this.$final = function () {',
  31027. ' };',
  31028. ' var $r = this.$rtti;',
  31029. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31030. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31031. '});',
  31032. '']),
  31033. LinesToStr([ // $mod.$main
  31034. '']));
  31035. end;
  31036. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31037. begin
  31038. WithTypeInfo:=true;
  31039. AddModuleWithIntfImplSrc('unit1.pas',
  31040. 'type TColor = -5..5;',
  31041. '');
  31042. StartProgram(true);
  31043. Add([
  31044. 'uses unit1;',
  31045. 'type',
  31046. ' TColorAlias = TColor;',
  31047. ' TColorTypeAlias = type TColor;',
  31048. ' TObject = class',
  31049. ' private',
  31050. ' fColor: TColor;',
  31051. ' fAlias: TColorAlias;',
  31052. ' fTypeAlias: TColorTypeAlias;',
  31053. ' published',
  31054. ' property Color: TColor read fcolor;',
  31055. ' property Alias: TColorAlias read falias;',
  31056. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31057. ' end;',
  31058. 'begin',
  31059. '']);
  31060. ConvertProgram;
  31061. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31062. LinesToStr([ // statements
  31063. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31064. 'rtl.createClass(this, "TObject", null, function () {',
  31065. ' this.$init = function () {',
  31066. ' this.fColor = 0;',
  31067. ' this.fAlias = 0;',
  31068. ' this.fTypeAlias = 0;',
  31069. ' };',
  31070. ' this.$final = function () {',
  31071. ' };',
  31072. ' var $r = this.$rtti;',
  31073. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31074. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31075. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31076. '});',
  31077. '']),
  31078. LinesToStr([ // $mod.$main
  31079. '']));
  31080. end;
  31081. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31082. begin
  31083. WithTypeInfo:=true;
  31084. StartProgram(false);
  31085. Add([
  31086. '{$modeswitch omitrtti}',
  31087. 'type',
  31088. ' TObject = class',
  31089. ' private',
  31090. ' FA: byte;',
  31091. ' published',
  31092. ' property A: byte read FA write FA;',
  31093. ' end;',
  31094. 'begin']);
  31095. ConvertProgram;
  31096. CheckSource('TestRTTI_Class_OmitRTTI',
  31097. LinesToStr([ // statements
  31098. 'rtl.createClass(this, "TObject", null, function () {',
  31099. ' this.$init = function () {',
  31100. ' this.FA = 0;',
  31101. ' };',
  31102. ' this.$final = function () {',
  31103. ' };',
  31104. '});',
  31105. '']),
  31106. LinesToStr([ // $mod.$main
  31107. '']));
  31108. end;
  31109. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31110. begin
  31111. WithTypeInfo:=true;
  31112. StartUnit(true,[supTObject]);
  31113. Add([
  31114. 'interface',
  31115. 'type',
  31116. ' {$M+}',
  31117. ' TBird = class',
  31118. ' published',
  31119. ' Swarm: array of TBird;',
  31120. ' end;',
  31121. 'implementation',
  31122. '']);
  31123. ConvertUnit;
  31124. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31125. LinesToStr([ // statements
  31126. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31127. ' this.$init = function () {',
  31128. ' pas.system.TObject.$init.call(this);',
  31129. ' this.Swarm = [];',
  31130. ' };',
  31131. ' this.$final = function () {',
  31132. ' this.Swarm = undefined;',
  31133. ' pas.system.TObject.$final.call(this);',
  31134. ' };',
  31135. ' var $r = this.$rtti;',
  31136. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31137. ' eltype: $r',
  31138. ' });',
  31139. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  31140. '});',
  31141. '']),
  31142. LinesToStr([ // $mod.$main
  31143. '']));
  31144. end;
  31145. procedure TTestModule.TestRTTI_IndexModifier;
  31146. begin
  31147. WithTypeInfo:=true;
  31148. StartProgram(false);
  31149. Add([
  31150. 'type',
  31151. ' TEnum = (red, blue);',
  31152. ' TObject = class',
  31153. ' FB: boolean;',
  31154. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31155. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31156. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31157. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31158. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31159. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31160. ' published',
  31161. ' property B1: boolean index 1 read FB write SetIntBool;',
  31162. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31163. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31164. ' end;',
  31165. 'begin']);
  31166. ConvertProgram;
  31167. CheckSource('TestRTTI_IndexModifier',
  31168. LinesToStr([ // statements
  31169. 'this.TEnum = {',
  31170. ' "0": "red",',
  31171. ' red: 0,',
  31172. ' "1": "blue",',
  31173. ' blue: 1',
  31174. '};',
  31175. 'this.$rtti.$Enum("TEnum", {',
  31176. ' minvalue: 0,',
  31177. ' maxvalue: 1,',
  31178. ' ordtype: 1,',
  31179. ' enumtype: this.TEnum',
  31180. '});',
  31181. 'rtl.createClass(this, "TObject", null, function () {',
  31182. ' this.$init = function () {',
  31183. ' this.FB = false;',
  31184. ' };',
  31185. ' this.$final = function () {',
  31186. ' };',
  31187. ' var $r = this.$rtti;',
  31188. ' $r.addProperty(',
  31189. ' "B1",',
  31190. ' 18,',
  31191. ' rtl.boolean,',
  31192. ' "FB",',
  31193. ' "SetIntBool",',
  31194. ' {',
  31195. ' index: 1',
  31196. ' }',
  31197. ' );',
  31198. ' $r.addProperty(',
  31199. ' "B2",',
  31200. ' 17,',
  31201. ' rtl.boolean,',
  31202. ' "GetEnumBool",',
  31203. ' "FB",',
  31204. ' {',
  31205. ' index: $mod.TEnum.blue',
  31206. ' }',
  31207. ' );',
  31208. ' $r.addProperty(',
  31209. ' "I1",',
  31210. ' 19,',
  31211. ' rtl.boolean,',
  31212. ' "GetStrIntBool",',
  31213. ' "SetStrIntBool",',
  31214. ' {',
  31215. ' index: 2',
  31216. ' }',
  31217. ' );',
  31218. '});',
  31219. '']),
  31220. LinesToStr([ // $mod.$main
  31221. '']));
  31222. end;
  31223. procedure TTestModule.TestRTTI_StoredModifier;
  31224. begin
  31225. WithTypeInfo:=true;
  31226. StartProgram(false);
  31227. Add([
  31228. 'const',
  31229. ' ConstB = true;',
  31230. 'type',
  31231. ' TObject = class',
  31232. ' private',
  31233. ' FB: boolean;',
  31234. ' function IsBStored: boolean; virtual; abstract;',
  31235. ' published',
  31236. ' property BoolA: boolean read FB stored true;',
  31237. ' property BoolB: boolean read FB stored false;',
  31238. ' property BoolC: boolean read FB stored FB;',
  31239. ' property BoolD: boolean read FB stored ConstB;',
  31240. ' property BoolE: boolean read FB stored IsBStored;',
  31241. ' end;',
  31242. 'begin']);
  31243. ConvertProgram;
  31244. CheckSource('TestRTTI_StoredModifier',
  31245. LinesToStr([ // statements
  31246. 'this.ConstB = true;',
  31247. 'rtl.createClass(this, "TObject", null, function () {',
  31248. ' this.$init = function () {',
  31249. ' this.FB = false;',
  31250. ' };',
  31251. ' this.$final = function () {',
  31252. ' };',
  31253. ' var $r = this.$rtti;',
  31254. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31255. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31256. ' $r.addProperty(',
  31257. ' "BoolC",',
  31258. ' 8,',
  31259. ' rtl.boolean,',
  31260. ' "FB",',
  31261. ' "",',
  31262. ' {',
  31263. ' stored: "FB"',
  31264. ' }',
  31265. ' );',
  31266. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31267. ' $r.addProperty(',
  31268. ' "BoolE",',
  31269. ' 12,',
  31270. ' rtl.boolean,',
  31271. ' "FB",',
  31272. ' "",',
  31273. ' {',
  31274. ' stored: "IsBStored"',
  31275. ' }',
  31276. ' );',
  31277. '});',
  31278. '']),
  31279. LinesToStr([ // $mod.$main
  31280. '']));
  31281. end;
  31282. procedure TTestModule.TestRTTI_DefaultValue;
  31283. begin
  31284. WithTypeInfo:=true;
  31285. StartProgram(false);
  31286. Add([
  31287. 'type',
  31288. ' TEnum = (red, blue);',
  31289. 'const',
  31290. ' CB = true or false;',
  31291. ' CI = 1+2;',
  31292. 'type',
  31293. ' TObject = class',
  31294. ' FB: boolean;',
  31295. ' FI: longint;',
  31296. ' FE: TEnum;',
  31297. ' published',
  31298. ' property B1: boolean read FB default true;',
  31299. ' property B2: boolean read FB default CB;',
  31300. ' property B3: boolean read FB default test1.cb;',
  31301. ' property I1: longint read FI default 2;',
  31302. ' property I2: longint read FI default CI;',
  31303. ' property E1: TEnum read FE default red;',
  31304. ' property E2: TEnum read FE default TEnum.blue;',
  31305. ' end;',
  31306. 'begin']);
  31307. ConvertProgram;
  31308. CheckSource('TestRTTI_DefaultValue',
  31309. LinesToStr([ // statements
  31310. 'this.TEnum = {',
  31311. ' "0": "red",',
  31312. ' red: 0,',
  31313. ' "1": "blue",',
  31314. ' blue: 1',
  31315. '};',
  31316. 'this.$rtti.$Enum("TEnum", {',
  31317. ' minvalue: 0,',
  31318. ' maxvalue: 1,',
  31319. ' ordtype: 1,',
  31320. ' enumtype: this.TEnum',
  31321. '});',
  31322. 'this.CB = true || false;',
  31323. 'this.CI = 1 + 2;',
  31324. 'rtl.createClass(this, "TObject", null, function () {',
  31325. ' this.$init = function () {',
  31326. ' this.FB = false;',
  31327. ' this.FI = 0;',
  31328. ' this.FE = 0;',
  31329. ' };',
  31330. ' this.$final = function () {',
  31331. ' };',
  31332. ' var $r = this.$rtti;',
  31333. ' $r.addProperty(',
  31334. ' "B1",',
  31335. ' 0,',
  31336. ' rtl.boolean,',
  31337. ' "FB",',
  31338. ' "",',
  31339. ' {',
  31340. ' Default: true',
  31341. ' }',
  31342. ' );',
  31343. ' $r.addProperty(',
  31344. ' "B2",',
  31345. ' 0,',
  31346. ' rtl.boolean,',
  31347. ' "FB",',
  31348. ' "",',
  31349. ' {',
  31350. ' Default: true',
  31351. ' }',
  31352. ' );',
  31353. ' $r.addProperty(',
  31354. ' "B3",',
  31355. ' 0,',
  31356. ' rtl.boolean,',
  31357. ' "FB",',
  31358. ' "",',
  31359. ' {',
  31360. ' Default: true',
  31361. ' }',
  31362. ' );',
  31363. ' $r.addProperty(',
  31364. ' "I1",',
  31365. ' 0,',
  31366. ' rtl.longint,',
  31367. ' "FI",',
  31368. ' "",',
  31369. ' {',
  31370. ' Default: 2',
  31371. ' }',
  31372. ' );',
  31373. ' $r.addProperty(',
  31374. ' "I2",',
  31375. ' 0,',
  31376. ' rtl.longint,',
  31377. ' "FI",',
  31378. ' "",',
  31379. ' {',
  31380. ' Default: 3',
  31381. ' }',
  31382. ' );',
  31383. ' $r.addProperty(',
  31384. ' "E1",',
  31385. ' 0,',
  31386. ' $mod.$rtti["TEnum"],',
  31387. ' "FE",',
  31388. ' "",',
  31389. ' {',
  31390. ' Default: $mod.TEnum.red',
  31391. ' }',
  31392. ' );',
  31393. ' $r.addProperty(',
  31394. ' "E2",',
  31395. ' 0,',
  31396. ' $mod.$rtti["TEnum"],',
  31397. ' "FE",',
  31398. ' "",',
  31399. ' {',
  31400. ' Default: $mod.TEnum.blue',
  31401. ' }',
  31402. ' );',
  31403. '});',
  31404. '']),
  31405. LinesToStr([ // $mod.$main
  31406. '']));
  31407. end;
  31408. procedure TTestModule.TestRTTI_DefaultValueSet;
  31409. begin
  31410. WithTypeInfo:=true;
  31411. StartProgram(false);
  31412. Add([
  31413. 'type',
  31414. ' TEnum = (red, blue);',
  31415. ' TSet = set of TEnum;',
  31416. 'const',
  31417. ' CSet = [red,blue];',
  31418. 'type',
  31419. ' TObject = class',
  31420. ' FSet: TSet;',
  31421. ' published',
  31422. ' property Set1: TSet read FSet default [];',
  31423. ' property Set2: TSet read FSet default [red];',
  31424. ' property Set3: TSet read FSet default [red,blue];',
  31425. ' property Set4: TSet read FSet default CSet;',
  31426. ' end;',
  31427. 'begin']);
  31428. ConvertProgram;
  31429. CheckSource('TestRTTI_DefaultValueSet',
  31430. LinesToStr([ // statements
  31431. 'this.TEnum = {',
  31432. ' "0": "red",',
  31433. ' red: 0,',
  31434. ' "1": "blue",',
  31435. ' blue: 1',
  31436. '};',
  31437. 'this.$rtti.$Enum("TEnum", {',
  31438. ' minvalue: 0,',
  31439. ' maxvalue: 1,',
  31440. ' ordtype: 1,',
  31441. ' enumtype: this.TEnum',
  31442. '});',
  31443. 'this.$rtti.$Set("TSet", {',
  31444. ' comptype: this.$rtti["TEnum"]',
  31445. '});',
  31446. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31447. 'rtl.createClass(this, "TObject", null, function () {',
  31448. ' this.$init = function () {',
  31449. ' this.FSet = {};',
  31450. ' };',
  31451. ' this.$final = function () {',
  31452. ' this.FSet = undefined;',
  31453. ' };',
  31454. ' var $r = this.$rtti;',
  31455. ' $r.addProperty(',
  31456. ' "Set1",',
  31457. ' 0,',
  31458. ' $mod.$rtti["TSet"],',
  31459. ' "FSet",',
  31460. ' "",',
  31461. ' {',
  31462. ' Default: {}',
  31463. ' }',
  31464. ' );',
  31465. ' $r.addProperty(',
  31466. ' "Set2",',
  31467. ' 0,',
  31468. ' $mod.$rtti["TSet"],',
  31469. ' "FSet",',
  31470. ' "",',
  31471. ' {',
  31472. ' Default: rtl.createSet($mod.TEnum.red)',
  31473. ' }',
  31474. ' );',
  31475. ' $r.addProperty(',
  31476. ' "Set3",',
  31477. ' 0,',
  31478. ' $mod.$rtti["TSet"],',
  31479. ' "FSet",',
  31480. ' "",',
  31481. ' {',
  31482. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31483. ' }',
  31484. ' );',
  31485. ' $r.addProperty(',
  31486. ' "Set4",',
  31487. ' 0,',
  31488. ' $mod.$rtti["TSet"],',
  31489. ' "FSet",',
  31490. ' "",',
  31491. ' {',
  31492. ' Default: $mod.CSet',
  31493. ' }',
  31494. ' );',
  31495. '});',
  31496. '']),
  31497. LinesToStr([ // $mod.$main
  31498. '']));
  31499. end;
  31500. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31501. begin
  31502. WithTypeInfo:=true;
  31503. StartProgram(false);
  31504. Add([
  31505. 'type',
  31506. ' TRg = -1..1;',
  31507. 'const',
  31508. ' l = low(TRg);',
  31509. ' h = high(TRg);',
  31510. 'type',
  31511. ' TObject = class',
  31512. ' FV: TRg;',
  31513. ' published',
  31514. ' property V1: TRg read FV default -1;',
  31515. ' end;',
  31516. 'begin']);
  31517. ConvertProgram;
  31518. CheckSource('TestRTTI_DefaultValueRangeType',
  31519. LinesToStr([ // statements
  31520. 'this.$rtti.$Int("TRg", {',
  31521. ' minvalue: -1,',
  31522. ' maxvalue: 1,',
  31523. ' ordtype: 0',
  31524. '});',
  31525. 'this.l = -1;',
  31526. 'this.h = 1;',
  31527. 'rtl.createClass(this, "TObject", null, function () {',
  31528. ' this.$init = function () {',
  31529. ' this.FV = 0;',
  31530. ' };',
  31531. ' this.$final = function () {',
  31532. ' };',
  31533. ' var $r = this.$rtti;',
  31534. ' $r.addProperty(',
  31535. ' "V1",',
  31536. ' 0,',
  31537. ' $mod.$rtti["TRg"],',
  31538. ' "FV",',
  31539. ' "",',
  31540. ' {',
  31541. ' Default: -1',
  31542. ' }',
  31543. ' );',
  31544. '});',
  31545. '']),
  31546. LinesToStr([ // $mod.$main
  31547. '']));
  31548. end;
  31549. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31550. begin
  31551. WithTypeInfo:=true;
  31552. StartProgram(false);
  31553. Add([
  31554. 'type',
  31555. ' TObject = class',
  31556. ' FA, FB: byte;',
  31557. ' property A: byte read FA default 1;',
  31558. ' property B: byte read FB default 2;',
  31559. ' end;',
  31560. ' TBird = class',
  31561. ' published',
  31562. ' property A;',
  31563. ' property B nodefault;',
  31564. ' end;',
  31565. 'begin']);
  31566. ConvertProgram;
  31567. CheckSource('TestRTTI_DefaultValueInherit',
  31568. LinesToStr([ // statements
  31569. 'rtl.createClass(this, "TObject", null, function () {',
  31570. ' this.$init = function () {',
  31571. ' this.FA = 0;',
  31572. ' this.FB = 0;',
  31573. ' };',
  31574. ' this.$final = function () {',
  31575. ' };',
  31576. '});',
  31577. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31578. ' var $r = this.$rtti;',
  31579. ' $r.addProperty(',
  31580. ' "A",',
  31581. ' 0,',
  31582. ' rtl.byte,',
  31583. ' "FA",',
  31584. ' "",',
  31585. ' {',
  31586. ' Default: 1',
  31587. ' }',
  31588. ' );',
  31589. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31590. '});',
  31591. '']),
  31592. LinesToStr([ // $mod.$main
  31593. '']));
  31594. end;
  31595. procedure TTestModule.TestRTTI_OverrideMethod;
  31596. begin
  31597. WithTypeInfo:=true;
  31598. StartProgram(false);
  31599. Add('type');
  31600. Add(' TObject = class');
  31601. Add(' published');
  31602. Add(' procedure DoIt; virtual; abstract;');
  31603. Add(' end;');
  31604. Add(' TSky = class');
  31605. Add(' published');
  31606. Add(' procedure DoIt; override;');
  31607. Add(' end;');
  31608. Add('procedure TSky.DoIt; begin end;');
  31609. Add('begin');
  31610. ConvertProgram;
  31611. CheckSource('TestRTTI_OverrideMethod',
  31612. LinesToStr([ // statements
  31613. 'rtl.createClass(this, "TObject", null, function () {',
  31614. ' this.$init = function () {',
  31615. ' };',
  31616. ' this.$final = function () {',
  31617. ' };',
  31618. ' var $r = this.$rtti;',
  31619. ' $r.addMethod("DoIt", 0, []);',
  31620. '});',
  31621. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31622. ' this.DoIt = function () {',
  31623. ' };',
  31624. '});',
  31625. '']),
  31626. LinesToStr([ // $mod.$main
  31627. '']));
  31628. end;
  31629. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31630. begin
  31631. WithTypeInfo:=true;
  31632. StartProgram(false);
  31633. Add([
  31634. 'type',
  31635. ' TObject = class',
  31636. ' published',
  31637. ' procedure DoIt;',
  31638. ' end;',
  31639. ' TSky = class',
  31640. ' published',
  31641. ' procedure DoIt; reintroduce;',
  31642. ' end;',
  31643. 'procedure TObject.DoIt; begin end;',
  31644. 'procedure TSky.DoIt;',
  31645. 'begin',
  31646. ' inherited DoIt;',
  31647. 'end;',
  31648. 'begin']);
  31649. ConvertProgram;
  31650. CheckSource('TestRTTI_ReintroduceMethod',
  31651. LinesToStr([ // statements
  31652. 'rtl.createClass(this, "TObject", null, function () {',
  31653. ' this.$init = function () {',
  31654. ' };',
  31655. ' this.$final = function () {',
  31656. ' };',
  31657. ' this.DoIt = function () {',
  31658. ' };',
  31659. ' var $r = this.$rtti;',
  31660. ' $r.addMethod("DoIt", 0, []);',
  31661. '});',
  31662. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31663. ' this.DoIt = function () {',
  31664. ' $mod.TObject.DoIt.call(this);',
  31665. ' };',
  31666. ' var $r = this.$rtti;',
  31667. ' $r.addMethod("DoIt", 0, []);',
  31668. '});',
  31669. '']),
  31670. LinesToStr([ // $mod.$main
  31671. '']));
  31672. end;
  31673. procedure TTestModule.TestRTTI_OverloadProperty;
  31674. begin
  31675. WithTypeInfo:=true;
  31676. StartProgram(false);
  31677. Add('type');
  31678. Add(' TObject = class');
  31679. Add(' protected');
  31680. Add(' FFlag: longint;');
  31681. Add(' published');
  31682. Add(' property Flag: longint read fflag;');
  31683. Add(' end;');
  31684. Add(' TSky = class');
  31685. Add(' published');
  31686. Add(' property FLAG: longint write fflag;');
  31687. Add(' end;');
  31688. Add('begin');
  31689. ConvertProgram;
  31690. CheckSource('TestRTTI_OverrideMethod',
  31691. LinesToStr([ // statements
  31692. 'rtl.createClass(this, "TObject", null, function () {',
  31693. ' this.$init = function () {',
  31694. ' this.FFlag = 0;',
  31695. ' };',
  31696. ' this.$final = function () {',
  31697. ' };',
  31698. ' var $r = this.$rtti;',
  31699. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31700. '});',
  31701. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31702. ' var $r = this.$rtti;',
  31703. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31704. '});',
  31705. '']),
  31706. LinesToStr([ // $mod.$main
  31707. '']));
  31708. end;
  31709. procedure TTestModule.TestRTTI_ClassForward;
  31710. begin
  31711. WithTypeInfo:=true;
  31712. StartProgram(false);
  31713. Add('type');
  31714. Add(' TObject = class end;');
  31715. Add(' tbridge = class;');
  31716. Add(' TProc = function: tbridge;');
  31717. Add(' TOger = class');
  31718. Add(' published');
  31719. Add(' FBridge: tbridge;');
  31720. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31721. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31722. Add(' end;');
  31723. Add(' TBridge = class');
  31724. Add(' FOger: toger;');
  31725. Add(' end;');
  31726. Add('var p: Pointer;');
  31727. Add(' b: tbridge;');
  31728. Add('begin');
  31729. Add(' p:=typeinfo(tbridge);');
  31730. Add(' p:=typeinfo(b);');
  31731. ConvertProgram;
  31732. CheckSource('TestRTTI_ClassForward',
  31733. LinesToStr([ // statements
  31734. 'rtl.createClass(this, "TObject", null, function () {',
  31735. ' this.$init = function () {',
  31736. ' };',
  31737. ' this.$final = function () {',
  31738. ' };',
  31739. '});',
  31740. 'this.$rtti.$Class("TBridge");',
  31741. 'this.$rtti.$ProcVar("TProc", {',
  31742. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31743. '});',
  31744. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31745. ' this.$init = function () {',
  31746. ' $mod.TObject.$init.call(this);',
  31747. ' this.FBridge = null;',
  31748. ' };',
  31749. ' this.$final = function () {',
  31750. ' this.FBridge = undefined;',
  31751. ' $mod.TObject.$final.call(this);',
  31752. ' };',
  31753. ' var $r = this.$rtti;',
  31754. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31755. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31756. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31757. '});',
  31758. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31759. ' this.$init = function () {',
  31760. ' $mod.TObject.$init.call(this);',
  31761. ' this.FOger = null;',
  31762. ' };',
  31763. ' this.$final = function () {',
  31764. ' this.FOger = undefined;',
  31765. ' $mod.TObject.$final.call(this);',
  31766. ' };',
  31767. '});',
  31768. 'this.p = null;',
  31769. 'this.b = null;',
  31770. '']),
  31771. LinesToStr([ // $mod.$main
  31772. '$mod.p = $mod.$rtti["TBridge"];',
  31773. '$mod.p = $mod.b.$rtti;',
  31774. '']));
  31775. end;
  31776. procedure TTestModule.TestRTTI_ClassOf;
  31777. begin
  31778. WithTypeInfo:=true;
  31779. StartProgram(false);
  31780. Add('type');
  31781. Add(' TClass = class of tobject;');
  31782. Add(' TProcA = function: TClass;');
  31783. Add(' TObject = class');
  31784. Add(' published');
  31785. Add(' C: tclass;');
  31786. Add(' end;');
  31787. Add(' tfox = class;');
  31788. Add(' TBird = class end;');
  31789. Add(' TBirds = class of tbird;');
  31790. Add(' TFox = class end;');
  31791. Add(' TFoxes = class of tfox;');
  31792. Add(' TCows = class of TCow;');
  31793. Add(' TCow = class;');
  31794. Add(' TCow = class end;');
  31795. Add('begin');
  31796. ConvertProgram;
  31797. CheckSource('TestRTTI_ClassOf',
  31798. LinesToStr([ // statements
  31799. 'this.$rtti.$Class("TObject");',
  31800. 'this.$rtti.$ClassRef("TClass", {',
  31801. ' instancetype: this.$rtti["TObject"]',
  31802. '});',
  31803. 'this.$rtti.$ProcVar("TProcA", {',
  31804. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31805. '});',
  31806. 'rtl.createClass(this, "TObject", null, function () {',
  31807. ' this.$init = function () {',
  31808. ' this.C = null;',
  31809. ' };',
  31810. ' this.$final = function () {',
  31811. ' this.C = undefined;',
  31812. ' };',
  31813. ' var $r = this.$rtti;',
  31814. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31815. '});',
  31816. 'this.$rtti.$Class("TFox");',
  31817. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31818. '});',
  31819. 'this.$rtti.$ClassRef("TBirds", {',
  31820. ' instancetype: this.$rtti["TBird"]',
  31821. '});',
  31822. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31823. '});',
  31824. 'this.$rtti.$ClassRef("TFoxes", {',
  31825. ' instancetype: this.$rtti["TFox"]',
  31826. '});',
  31827. 'this.$rtti.$Class("TCow");',
  31828. 'this.$rtti.$ClassRef("TCows", {',
  31829. ' instancetype: this.$rtti["TCow"]',
  31830. '});',
  31831. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31832. '});',
  31833. '']),
  31834. LinesToStr([ // $mod.$main
  31835. '']));
  31836. end;
  31837. procedure TTestModule.TestRTTI_Record;
  31838. begin
  31839. WithTypeInfo:=true;
  31840. StartProgram(false);
  31841. Add('type');
  31842. Add(' integer = longint;');
  31843. Add(' TPoint = record');
  31844. Add(' x,y: integer;');
  31845. Add(' end;');
  31846. Add('var p: pointer;');
  31847. Add(' r: tpoint;');
  31848. Add('begin');
  31849. Add(' p:=typeinfo(tpoint);');
  31850. Add(' p:=typeinfo(r);');
  31851. Add(' p:=typeinfo(r.x);');
  31852. ConvertProgram;
  31853. CheckSource('TestRTTI_Record',
  31854. LinesToStr([ // statements
  31855. 'rtl.recNewT(this, "TPoint", function () {',
  31856. ' this.x = 0;',
  31857. ' this.y = 0;',
  31858. ' this.$eq = function (b) {',
  31859. ' return (this.x === b.x) && (this.y === b.y);',
  31860. ' };',
  31861. ' this.$assign = function (s) {',
  31862. ' this.x = s.x;',
  31863. ' this.y = s.y;',
  31864. ' return this;',
  31865. ' };',
  31866. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31867. ' $r.addField("x", rtl.longint);',
  31868. ' $r.addField("y", rtl.longint);',
  31869. '});',
  31870. 'this.p = null;',
  31871. 'this.r = this.TPoint.$new();',
  31872. '']),
  31873. LinesToStr([ // $mod.$main
  31874. '$mod.p = $mod.$rtti["TPoint"];',
  31875. '$mod.p = $mod.$rtti["TPoint"];',
  31876. '$mod.p = rtl.longint;',
  31877. '']));
  31878. end;
  31879. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31880. begin
  31881. WithTypeInfo:=true;
  31882. StartProgram(false);
  31883. Add('type');
  31884. Add(' TFloatRec = record');
  31885. Add(' c,d: array of char;');
  31886. // Add(' i: array of array of longint;');
  31887. Add(' end;');
  31888. Add('var p: pointer;');
  31889. Add(' r: tfloatrec;');
  31890. Add('begin');
  31891. Add(' p:=typeinfo(tfloatrec);');
  31892. Add(' p:=typeinfo(r);');
  31893. Add(' p:=typeinfo(r.d);');
  31894. ConvertProgram;
  31895. CheckSource('TestRTTI_Record',
  31896. LinesToStr([ // statements
  31897. 'rtl.recNewT(this, "TFloatRec", function () {',
  31898. ' this.$new = function () {',
  31899. ' var r = Object.create(this);',
  31900. ' r.c = [];',
  31901. ' r.d = [];',
  31902. ' return r;',
  31903. ' };',
  31904. ' this.$eq = function (b) {',
  31905. ' return (this.c === b.c) && (this.d === b.d);',
  31906. ' };',
  31907. ' this.$assign = function (s) {',
  31908. ' this.c = rtl.arrayRef(s.c);',
  31909. ' this.d = rtl.arrayRef(s.d);',
  31910. ' return this;',
  31911. ' };',
  31912. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31913. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31914. ' eltype: rtl.char',
  31915. ' });',
  31916. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31917. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31918. '});',
  31919. 'this.p = null;',
  31920. 'this.r = this.TFloatRec.$new();',
  31921. '']),
  31922. LinesToStr([ // $mod.$main
  31923. '$mod.p = $mod.$rtti["TFloatRec"];',
  31924. '$mod.p = $mod.$rtti["TFloatRec"];',
  31925. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31926. '']));
  31927. end;
  31928. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31929. begin
  31930. WithTypeInfo:=true;
  31931. StartProgram(false);
  31932. Add([
  31933. '{$modeswitch AdvancedRecords}',
  31934. 'type',
  31935. ' TPoint = record',
  31936. ' type TProc = procedure(w: word);',
  31937. ' class var p: TProc;',
  31938. ' end;',
  31939. 'begin',
  31940. '']);
  31941. ConvertProgram;
  31942. CheckSource('TestRTTI_Record_ClassVarType',
  31943. LinesToStr([ // statements
  31944. 'rtl.recNewT(this, "TPoint", function () {',
  31945. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31946. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31947. ' });',
  31948. ' this.p = null;',
  31949. ' this.$eq = function (b) {',
  31950. ' return true;',
  31951. ' };',
  31952. ' this.$assign = function (s) {',
  31953. ' return this;',
  31954. ' };',
  31955. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31956. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31957. '}, true);',
  31958. '']),
  31959. LinesToStr([ // $mod.$main
  31960. '']));
  31961. end;
  31962. procedure TTestModule.TestRTTI_LocalTypes;
  31963. begin
  31964. WithTypeInfo:=true;
  31965. StartProgram(false);
  31966. Add([
  31967. 'procedure DoIt;',
  31968. 'type',
  31969. ' integer = longint;',
  31970. ' TPoint = record',
  31971. ' x,y: integer;',
  31972. ' end;',
  31973. 'var p: TPoint;',
  31974. 'begin',
  31975. 'end;',
  31976. 'begin']);
  31977. ConvertProgram;
  31978. CheckSource('TestRTTI_LocalTypes',
  31979. LinesToStr([ // statements
  31980. 'var TPoint = rtl.recNewT(null, "", function () {',
  31981. ' this.x = 0;',
  31982. ' this.y = 0;',
  31983. ' this.$eq = function (b) {',
  31984. ' return (this.x === b.x) && (this.y === b.y);',
  31985. ' };',
  31986. ' this.$assign = function (s) {',
  31987. ' this.x = s.x;',
  31988. ' this.y = s.y;',
  31989. ' return this;',
  31990. ' };',
  31991. '});',
  31992. 'this.DoIt = function () {',
  31993. ' var p = TPoint.$new();',
  31994. '};',
  31995. '']),
  31996. LinesToStr([ // $mod.$main
  31997. '']));
  31998. end;
  31999. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  32000. begin
  32001. WithTypeInfo:=true;
  32002. StartProgram(false);
  32003. Add([
  32004. 'type',
  32005. ' TCaption = string;',
  32006. ' TYesNo = boolean;',
  32007. ' TLetter = char;',
  32008. ' TFloat = double;',
  32009. ' TPtr = pointer;',
  32010. ' TShortInt = shortint;',
  32011. ' TByte = byte;',
  32012. ' TSmallInt = smallint;',
  32013. ' TWord = word;',
  32014. ' TInt32 = longint;',
  32015. ' TDWord = longword;',
  32016. ' TValue = jsvalue;',
  32017. 'var p: TPtr;',
  32018. 'begin',
  32019. ' p:=typeinfo(string);',
  32020. ' p:=typeinfo(tcaption);',
  32021. ' p:=typeinfo(boolean);',
  32022. ' p:=typeinfo(tyesno);',
  32023. ' p:=typeinfo(char);',
  32024. ' p:=typeinfo(tletter);',
  32025. ' p:=typeinfo(double);',
  32026. ' p:=typeinfo(tfloat);',
  32027. ' p:=typeinfo(pointer);',
  32028. ' p:=typeinfo(tptr);',
  32029. ' p:=typeinfo(shortint);',
  32030. ' p:=typeinfo(tshortint);',
  32031. ' p:=typeinfo(byte);',
  32032. ' p:=typeinfo(tbyte);',
  32033. ' p:=typeinfo(smallint);',
  32034. ' p:=typeinfo(tsmallint);',
  32035. ' p:=typeinfo(word);',
  32036. ' p:=typeinfo(tword);',
  32037. ' p:=typeinfo(longword);',
  32038. ' p:=typeinfo(tdword);',
  32039. ' p:=typeinfo(jsvalue);',
  32040. ' p:=typeinfo(tvalue);',
  32041. '']);
  32042. ConvertProgram;
  32043. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32044. LinesToStr([ // statements
  32045. 'this.p = null;',
  32046. '']),
  32047. LinesToStr([ // $mod.$main
  32048. '$mod.p = rtl.string;',
  32049. '$mod.p = rtl.string;',
  32050. '$mod.p = rtl.boolean;',
  32051. '$mod.p = rtl.boolean;',
  32052. '$mod.p = rtl.char;',
  32053. '$mod.p = rtl.char;',
  32054. '$mod.p = rtl.double;',
  32055. '$mod.p = rtl.double;',
  32056. '$mod.p = rtl.pointer;',
  32057. '$mod.p = rtl.pointer;',
  32058. '$mod.p = rtl.shortint;',
  32059. '$mod.p = rtl.shortint;',
  32060. '$mod.p = rtl.byte;',
  32061. '$mod.p = rtl.byte;',
  32062. '$mod.p = rtl.smallint;',
  32063. '$mod.p = rtl.smallint;',
  32064. '$mod.p = rtl.word;',
  32065. '$mod.p = rtl.word;',
  32066. '$mod.p = rtl.longword;',
  32067. '$mod.p = rtl.longword;',
  32068. '$mod.p = rtl.jsvalue;',
  32069. '$mod.p = rtl.jsvalue;',
  32070. '']));
  32071. end;
  32072. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32073. begin
  32074. WithTypeInfo:=true;
  32075. StartProgram(false);
  32076. Add([
  32077. 'type',
  32078. ' TCaption = type string;',
  32079. ' TYesNo = type boolean;',
  32080. ' TLetter = type char;',
  32081. ' TFloat = type double;',
  32082. ' TPtr = type pointer;',
  32083. ' TShortInt = type shortint;',
  32084. ' TByte = type byte;',
  32085. ' TSmallInt = type smallint;',
  32086. ' TWord = type word;',
  32087. ' TInt32 = type longint;',
  32088. ' TDWord = type longword;',
  32089. ' TValue = type jsvalue;',
  32090. ' TAliasValue = type TValue;',
  32091. 'var',
  32092. ' p: TPtr;',
  32093. ' a: TAliasValue;',
  32094. 'begin',
  32095. ' p:=typeinfo(tcaption);',
  32096. ' p:=typeinfo(tyesno);',
  32097. ' p:=typeinfo(tletter);',
  32098. ' p:=typeinfo(tfloat);',
  32099. ' p:=typeinfo(tptr);',
  32100. ' p:=typeinfo(tshortint);',
  32101. ' p:=typeinfo(tbyte);',
  32102. ' p:=typeinfo(tsmallint);',
  32103. ' p:=typeinfo(tword);',
  32104. ' p:=typeinfo(tdword);',
  32105. ' p:=typeinfo(tvalue);',
  32106. ' p:=typeinfo(taliasvalue);',
  32107. ' p:=typeinfo(a);',
  32108. '']);
  32109. ConvertProgram;
  32110. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32111. LinesToStr([ // statements
  32112. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32113. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32114. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32115. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32116. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32117. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32118. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32119. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32120. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32121. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32122. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32123. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32124. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32125. 'this.p = null;',
  32126. 'this.a = undefined;',
  32127. '']),
  32128. LinesToStr([ // $mod.$main
  32129. '$mod.p = $mod.$rtti["TCaption"];',
  32130. '$mod.p = $mod.$rtti["TYesNo"];',
  32131. '$mod.p = $mod.$rtti["TLetter"];',
  32132. '$mod.p = $mod.$rtti["TFloat"];',
  32133. '$mod.p = $mod.$rtti["TPtr"];',
  32134. '$mod.p = $mod.$rtti["TShortInt"];',
  32135. '$mod.p = $mod.$rtti["TByte"];',
  32136. '$mod.p = $mod.$rtti["TSmallInt"];',
  32137. '$mod.p = $mod.$rtti["TWord"];',
  32138. '$mod.p = $mod.$rtti["TDWord"];',
  32139. '$mod.p = $mod.$rtti["TValue"];',
  32140. '$mod.p = $mod.$rtti["TAliasValue"];',
  32141. '$mod.p = $mod.$rtti["TAliasValue"];',
  32142. '']));
  32143. end;
  32144. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32145. begin
  32146. WithTypeInfo:=true;
  32147. StartProgram(false);
  32148. Add('procedure DoIt;');
  32149. Add('type');
  32150. Add(' integer = longint;');
  32151. Add(' TPoint = record');
  32152. Add(' x,y: integer;');
  32153. Add(' end;');
  32154. Add('var p: pointer;');
  32155. Add('begin');
  32156. Add(' p:=typeinfo(tpoint);');
  32157. Add('end;');
  32158. Add('begin');
  32159. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32160. ConvertProgram;
  32161. end;
  32162. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32163. begin
  32164. WithTypeInfo:=true;
  32165. StartProgram(true,[supTypeInfo]);
  32166. Add([
  32167. '{$modeswitch externalclass}',
  32168. 'type',
  32169. ' TFlag = (up,down);',
  32170. ' TFlags = set of TFlag;',
  32171. 'var',
  32172. ' ti: TTypeInfo;',
  32173. ' tiInt: TTypeInfoInteger;',
  32174. ' tiEnum: TTypeInfoEnum;',
  32175. ' tiSet: TTypeInfoSet;',
  32176. 'begin',
  32177. ' ti:=typeinfo(string);',
  32178. ' ti:=typeinfo(boolean);',
  32179. ' ti:=typeinfo(char);',
  32180. ' ti:=typeinfo(double);',
  32181. ' tiInt:=typeinfo(shortint);',
  32182. ' tiInt:=typeinfo(byte);',
  32183. ' tiInt:=typeinfo(smallint);',
  32184. ' tiInt:=typeinfo(word);',
  32185. ' tiInt:=typeinfo(longint);',
  32186. ' tiInt:=typeinfo(longword);',
  32187. ' ti:=typeinfo(jsvalue);',
  32188. ' tiEnum:=typeinfo(tflag);',
  32189. ' tiSet:=typeinfo(tflags);']);
  32190. ConvertProgram;
  32191. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32192. LinesToStr([ // statements
  32193. 'this.TFlag = {',
  32194. ' "0": "up",',
  32195. ' up: 0,',
  32196. ' "1": "down",',
  32197. ' down: 1',
  32198. '};',
  32199. 'this.$rtti.$Enum("TFlag", {',
  32200. ' minvalue: 0,',
  32201. ' maxvalue: 1,',
  32202. ' ordtype: 1,',
  32203. ' enumtype: this.TFlag',
  32204. '});',
  32205. 'this.$rtti.$Set("TFlags", {',
  32206. ' comptype: this.$rtti["TFlag"]',
  32207. '});',
  32208. 'this.ti = null;',
  32209. 'this.tiInt = null;',
  32210. 'this.tiEnum = null;',
  32211. 'this.tiSet = null;',
  32212. '']),
  32213. LinesToStr([ // $mod.$main
  32214. '$mod.ti = rtl.string;',
  32215. '$mod.ti = rtl.boolean;',
  32216. '$mod.ti = rtl.char;',
  32217. '$mod.ti = rtl.double;',
  32218. '$mod.tiInt = rtl.shortint;',
  32219. '$mod.tiInt = rtl.byte;',
  32220. '$mod.tiInt = rtl.smallint;',
  32221. '$mod.tiInt = rtl.word;',
  32222. '$mod.tiInt = rtl.longint;',
  32223. '$mod.tiInt = rtl.longword;',
  32224. '$mod.ti = rtl.jsvalue;',
  32225. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32226. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32227. '']));
  32228. end;
  32229. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32230. begin
  32231. WithTypeInfo:=true;
  32232. StartProgram(true,[supTypeInfo]);
  32233. Add('{$modeswitch externalclass}');
  32234. Add('type');
  32235. Add(' TStaticArr = array[boolean] of string;');
  32236. Add(' TDynArr = array of string;');
  32237. Add(' TProc = procedure;');
  32238. Add(' TMethod = procedure of object;');
  32239. Add('var');
  32240. Add(' StaticArray: TStaticArr;');
  32241. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32242. Add(' DynArray: TDynArr;');
  32243. Add(' tiDynArray: TTypeInfoDynArray;');
  32244. Add(' ProcVar: TProc;');
  32245. Add(' tiProcVar: TTypeInfoProcVar;');
  32246. Add(' MethodVar: TMethod;');
  32247. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32248. Add('begin');
  32249. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32250. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32251. Add(' tiDynArray:=typeinfo(DynArray);');
  32252. Add(' tiDynArray:=typeinfo(TDynArr);');
  32253. Add(' tiProcVar:=typeinfo(ProcVar);');
  32254. Add(' tiProcVar:=typeinfo(TProc);');
  32255. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32256. Add(' tiMethodVar:=typeinfo(TMethod);');
  32257. ConvertProgram;
  32258. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32259. LinesToStr([ // statements
  32260. 'this.$rtti.$StaticArray("TStaticArr", {',
  32261. ' dims: [2],',
  32262. ' eltype: rtl.string',
  32263. '});',
  32264. 'this.$rtti.$DynArray("TDynArr", {',
  32265. ' eltype: rtl.string',
  32266. '});',
  32267. 'this.$rtti.$ProcVar("TProc", {',
  32268. ' procsig: rtl.newTIProcSig([])',
  32269. '});',
  32270. 'this.$rtti.$MethodVar("TMethod", {',
  32271. ' procsig: rtl.newTIProcSig([]),',
  32272. ' methodkind: 0',
  32273. '});',
  32274. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32275. 'this.tiStaticArray = null;',
  32276. 'this.DynArray = [];',
  32277. 'this.tiDynArray = null;',
  32278. 'this.ProcVar = null;',
  32279. 'this.tiProcVar = null;',
  32280. 'this.MethodVar = null;',
  32281. 'this.tiMethodVar = null;',
  32282. '']),
  32283. LinesToStr([ // $mod.$main
  32284. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32285. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32286. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32287. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32288. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32289. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32290. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32291. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32292. '']));
  32293. end;
  32294. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32295. begin
  32296. WithTypeInfo:=true;
  32297. StartProgram(true,[supTypeInfo]);
  32298. Add('{$modeswitch externalclass}');
  32299. Add('type');
  32300. Add(' TRec = record end;');
  32301. // ToDo: ^TRec
  32302. Add(' TObject = class end;');
  32303. Add(' TClass = class of tobject;');
  32304. Add('var');
  32305. Add(' Rec: trec;');
  32306. Add(' tiRecord: ttypeinforecord;');
  32307. Add(' Obj: tobject;');
  32308. Add(' tiClass: ttypeinfoclass;');
  32309. Add(' aClass: tclass;');
  32310. Add(' tiClassRef: ttypeinfoclassref;');
  32311. // ToDo: ^TRec
  32312. Add(' tiPointer: ttypeinfopointer;');
  32313. Add('begin');
  32314. Add(' tirecord:=typeinfo(trec);');
  32315. Add(' tirecord:=typeinfo(trec);');
  32316. Add(' ticlass:=typeinfo(obj);');
  32317. Add(' ticlass:=typeinfo(tobject);');
  32318. Add(' ticlass:=typeinfo(aclass);');
  32319. Add(' ticlassref:=typeinfo(tclass);');
  32320. ConvertProgram;
  32321. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32322. LinesToStr([ // statements
  32323. 'rtl.recNewT(this, "TRec", function () {',
  32324. ' this.$eq = function (b) {',
  32325. ' return true;',
  32326. ' };',
  32327. ' this.$assign = function (s) {',
  32328. ' return this;',
  32329. ' };',
  32330. ' $mod.$rtti.$Record("TRec", {});',
  32331. '});',
  32332. 'rtl.createClass(this, "TObject", null, function () {',
  32333. ' this.$init = function () {',
  32334. ' };',
  32335. ' this.$final = function () {',
  32336. ' };',
  32337. '});',
  32338. 'this.$rtti.$ClassRef("TClass", {',
  32339. ' instancetype: this.$rtti["TObject"]',
  32340. '});',
  32341. 'this.Rec = this.TRec.$new();',
  32342. 'this.tiRecord = null;',
  32343. 'this.Obj = null;',
  32344. 'this.tiClass = null;',
  32345. 'this.aClass = null;',
  32346. 'this.tiClassRef = null;',
  32347. 'this.tiPointer = null;',
  32348. '']),
  32349. LinesToStr([ // $mod.$main
  32350. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32351. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32352. '$mod.tiClass = $mod.Obj.$rtti;',
  32353. '$mod.tiClass = $mod.$rtti["TObject"];',
  32354. '$mod.tiClass = $mod.aClass.$rtti;',
  32355. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32356. '']));
  32357. end;
  32358. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32359. begin
  32360. WithTypeInfo:=true;
  32361. StartProgram(true,[supTypeInfo]);
  32362. Add([
  32363. '{$modeswitch externalclass}',
  32364. 'type',
  32365. ' TClass = class of tobject;',
  32366. ' TObject = class',
  32367. ' function MyClass: TClass;',
  32368. ' class function ClassType: TClass;',
  32369. ' end;',
  32370. 'function TObject.MyClass: TClass;',
  32371. 'var t: TTypeInfoClass;',
  32372. 'begin',
  32373. ' t:=TypeInfo(Self);',
  32374. ' t:=TypeInfo(Result);',
  32375. ' t:=TypeInfo(TObject);',
  32376. 'end;',
  32377. 'class function TObject.ClassType: TClass;',
  32378. 'var t: TTypeInfoClass;',
  32379. 'begin',
  32380. ' t:=TypeInfo(Self);',
  32381. ' t:=TypeInfo(Result);',
  32382. 'end;',
  32383. 'var',
  32384. ' Obj: TObject;',
  32385. ' t: TTypeInfoClass;',
  32386. 'begin',
  32387. ' t:=TypeInfo(TObject.ClassType);',
  32388. ' t:=TypeInfo(Obj.ClassType);',
  32389. ' t:=TypeInfo(Obj.MyClass);',
  32390. '']);
  32391. ConvertProgram;
  32392. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32393. LinesToStr([ // statements
  32394. 'this.$rtti.$Class("TObject");',
  32395. 'this.$rtti.$ClassRef("TClass", {',
  32396. ' instancetype: this.$rtti["TObject"]',
  32397. '});',
  32398. 'rtl.createClass(this, "TObject", null, function () {',
  32399. ' this.$init = function () {',
  32400. ' };',
  32401. ' this.$final = function () {',
  32402. ' };',
  32403. ' this.MyClass = function () {',
  32404. ' var Result = null;',
  32405. ' var t = null;',
  32406. ' t = this.$rtti;',
  32407. ' t = Result.$rtti;',
  32408. ' t = $mod.$rtti["TObject"];',
  32409. ' return Result;',
  32410. ' };',
  32411. ' this.ClassType = function () {',
  32412. ' var Result = null;',
  32413. ' var t = null;',
  32414. ' t = this.$rtti;',
  32415. ' t = Result.$rtti;',
  32416. ' return Result;',
  32417. ' };',
  32418. '});',
  32419. 'this.Obj = null;',
  32420. 'this.t = null;',
  32421. '']),
  32422. LinesToStr([ // $mod.$main
  32423. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32424. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32425. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32426. '']));
  32427. end;
  32428. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32429. begin
  32430. WithTypeInfo:=true;
  32431. AddModuleWithIntfImplSrc('typinfo.pas',
  32432. LinesToStr([
  32433. '{$modeswitch externalclass}',
  32434. 'type',
  32435. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32436. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32437. '']),
  32438. '');
  32439. AddModuleWithIntfImplSrc('unit2.pas',
  32440. LinesToStr([
  32441. 'uses typinfo;',
  32442. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32443. 'procedure DoPtr(p: PTypeInfo);',
  32444. 'procedure DoInfo(t: TTypeInfo);',
  32445. 'procedure DoInt(t: TTypeInfoInteger);',
  32446. '']),
  32447. LinesToStr([
  32448. 'procedure DoPtr(p: PTypeInfo);',
  32449. 'begin end;',
  32450. 'procedure DoInfo(t: TTypeInfo);',
  32451. 'begin end;',
  32452. 'procedure DoInt(t: TTypeInfoInteger);',
  32453. 'begin end;',
  32454. '']));
  32455. StartUnit(true);
  32456. Add([
  32457. 'interface',
  32458. 'uses unit2;', // does not use unit typinfo
  32459. 'implementation',
  32460. 'var',
  32461. ' i: byte;',
  32462. ' p: pointer;',
  32463. ' t: PTypeInfo;',
  32464. 'initialization',
  32465. ' p:=typeinfo(i);',
  32466. ' t:=typeinfo(i);',
  32467. ' if p=t then ;',
  32468. ' if p=typeinfo(i) then ;',
  32469. ' if typeinfo(i)=p then ;',
  32470. ' if t=typeinfo(i) then ;',
  32471. ' if typeinfo(i)=t then ;',
  32472. ' DoPtr(p);',
  32473. ' DoPtr(t);',
  32474. ' DoPtr(typeinfo(i));',
  32475. ' DoInfo(p);',
  32476. ' DoInfo(t);',
  32477. ' DoInfo(typeinfo(i));',
  32478. ' DoInt(typeinfo(i));',
  32479. '']);
  32480. ConvertUnit;
  32481. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32482. LinesToStr([ // statements
  32483. 'var $impl = $mod.$impl;',
  32484. '']),
  32485. LinesToStr([ // this.$init
  32486. '$impl.p = rtl.byte;',
  32487. '$impl.t = rtl.byte;',
  32488. 'if ($impl.p === $impl.t) ;',
  32489. 'if ($impl.p === rtl.byte) ;',
  32490. 'if (rtl.byte === $impl.p) ;',
  32491. 'if ($impl.t === rtl.byte) ;',
  32492. 'if (rtl.byte === $impl.t) ;',
  32493. 'pas.unit2.DoPtr($impl.p);',
  32494. 'pas.unit2.DoPtr($impl.t);',
  32495. 'pas.unit2.DoPtr(rtl.byte);',
  32496. 'pas.unit2.DoInfo($impl.p);',
  32497. 'pas.unit2.DoInfo($impl.t);',
  32498. 'pas.unit2.DoInfo(rtl.byte);',
  32499. 'pas.unit2.DoInt(rtl.byte);',
  32500. '']),
  32501. LinesToStr([ // implementation
  32502. '$impl.i = 0;',
  32503. '$impl.p = null;',
  32504. '$impl.t = null;',
  32505. '']) );
  32506. end;
  32507. procedure TTestModule.TestRTTI_Interface_Corba;
  32508. begin
  32509. WithTypeInfo:=true;
  32510. StartProgram(true,[supTypeInfo]);
  32511. Add([
  32512. '{$interfaces corba}',
  32513. '{$modeswitch externalclass}',
  32514. 'type',
  32515. ' IUnknown = interface',
  32516. ' end;',
  32517. ' IBird = interface',
  32518. ' function GetItem: longint;',
  32519. ' procedure SetItem(Value: longint);',
  32520. ' property Item: longint read GetItem write SetItem;',
  32521. ' end;',
  32522. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32523. 'var',
  32524. ' i: IBird;',
  32525. ' t: TTypeInfoInterface;',
  32526. 'begin',
  32527. ' t:=TypeInfo(IBird);',
  32528. ' t:=TypeInfo(i);',
  32529. ' DoIt(t);',
  32530. ' DoIt(TypeInfo(IBird));',
  32531. '']);
  32532. ConvertProgram;
  32533. CheckSource('TestRTTI_Interface_Corba',
  32534. LinesToStr([ // statements
  32535. 'rtl.createInterface(',
  32536. ' this,',
  32537. ' "IUnknown",',
  32538. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32539. ' [],',
  32540. ' null,',
  32541. ' function () {',
  32542. ' }',
  32543. ');',
  32544. 'rtl.createInterface(',
  32545. ' this,',
  32546. ' "IBird",',
  32547. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32548. ' ["GetItem", "SetItem"],',
  32549. ' null,',
  32550. ' function () {',
  32551. ' var $r = this.$rtti;',
  32552. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32553. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32554. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32555. ' }',
  32556. ');',
  32557. 'this.DoIt = function (t) {',
  32558. '}; ',
  32559. 'this.i = null;',
  32560. 'this.t = null;',
  32561. '']),
  32562. LinesToStr([ // $mod.$main
  32563. '$mod.t = $mod.$rtti["IBird"];',
  32564. '$mod.t = $mod.i.$rtti;',
  32565. '$mod.DoIt($mod.t);',
  32566. '$mod.DoIt($mod.$rtti["IBird"]);',
  32567. '']));
  32568. end;
  32569. procedure TTestModule.TestRTTI_Interface_COM;
  32570. begin
  32571. WithTypeInfo:=true;
  32572. StartProgram(true,[supTypeInfo]);
  32573. Add([
  32574. '{$interfaces com}',
  32575. '{$modeswitch externalclass}',
  32576. 'type',
  32577. ' TGuid = record end;',
  32578. ' integer = longint;',
  32579. ' IUnknown = interface',
  32580. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32581. ' function _AddRef: Integer;',
  32582. ' function _Release: Integer;',
  32583. ' end;',
  32584. ' IBird = interface',
  32585. ' function GetItem: longint;',
  32586. ' procedure SetItem(Value: longint);',
  32587. ' property Item: longint read GetItem write SetItem;',
  32588. ' end;',
  32589. 'var',
  32590. ' i: IBird;',
  32591. ' t: TTypeInfoInterface;',
  32592. 'begin',
  32593. ' t:=TypeInfo(IBird);',
  32594. ' t:=TypeInfo(i);',
  32595. '']);
  32596. ConvertProgram;
  32597. CheckSource('TestRTTI_Interface_COM',
  32598. LinesToStr([ // statements
  32599. 'rtl.recNewT(this, "TGuid", function () {',
  32600. ' this.$eq = function (b) {',
  32601. ' return true;',
  32602. ' };',
  32603. ' this.$assign = function (s) {',
  32604. ' return this;',
  32605. ' };',
  32606. ' $mod.$rtti.$Record("TGuid", {});',
  32607. '});',
  32608. 'rtl.createInterface(',
  32609. ' this,',
  32610. ' "IUnknown",',
  32611. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32612. ' ["QueryInterface", "_AddRef", "_Release"],',
  32613. ' null,',
  32614. ' function () {',
  32615. ' this.$kind = "com";',
  32616. ' var $r = this.$rtti;',
  32617. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32618. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32619. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32620. ' }',
  32621. ');',
  32622. 'rtl.createInterface(',
  32623. ' this,',
  32624. ' "IBird",',
  32625. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32626. ' ["GetItem", "SetItem"],',
  32627. ' this.IUnknown,',
  32628. ' function () {',
  32629. ' var $r = this.$rtti;',
  32630. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32631. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32632. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32633. ' }',
  32634. ');',
  32635. 'this.i = null;',
  32636. 'this.t = null;',
  32637. '']),
  32638. LinesToStr([ // $mod.$main
  32639. '$mod.t = $mod.$rtti["IBird"];',
  32640. '$mod.t = $mod.i.$rtti;',
  32641. '']));
  32642. end;
  32643. procedure TTestModule.TestRTTI_ClassHelper;
  32644. begin
  32645. WithTypeInfo:=true;
  32646. StartProgram(true,[supTypeInfo]);
  32647. Add([
  32648. '{$interfaces com}',
  32649. '{$modeswitch externalclass}',
  32650. 'type',
  32651. ' TObject = class',
  32652. ' end;',
  32653. ' THelper = class helper for TObject',
  32654. ' published',
  32655. ' function GetItem: longint;',
  32656. ' property Item: longint read GetItem;',
  32657. ' end;',
  32658. 'function THelper.GetItem: longint;',
  32659. 'begin',
  32660. 'end;',
  32661. 'var',
  32662. ' t: TTypeInfoHelper;',
  32663. 'begin',
  32664. ' t:=TypeInfo(THelper);',
  32665. '']);
  32666. ConvertProgram;
  32667. CheckSource('TestRTTI_ClassHelper',
  32668. LinesToStr([ // statements
  32669. 'rtl.createClass(this, "TObject", null, function () {',
  32670. ' this.$init = function () {',
  32671. ' };',
  32672. ' this.$final = function () {',
  32673. ' };',
  32674. '});',
  32675. 'rtl.createHelper(this, "THelper", null, function () {',
  32676. ' this.GetItem = function () {',
  32677. ' var Result = 0;',
  32678. ' return Result;',
  32679. ' };',
  32680. ' var $r = this.$rtti;',
  32681. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32682. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32683. '});',
  32684. 'this.t = null;',
  32685. '']),
  32686. LinesToStr([ // $mod.$main
  32687. '$mod.t = $mod.$rtti["THelper"];',
  32688. '']));
  32689. end;
  32690. procedure TTestModule.TestRTTI_ExternalClass;
  32691. begin
  32692. WithTypeInfo:=true;
  32693. StartProgram(true,[supTypeInfo]);
  32694. Add([
  32695. '{$modeswitch externalclass}',
  32696. 'type',
  32697. ' TJSObject = class external name ''Object''',
  32698. ' end;',
  32699. ' TJSArray = class external name ''Array'' (TJSObject)',
  32700. ' end;',
  32701. 'var',
  32702. ' p: Pointer;',
  32703. ' tc: TTypeInfoExtClass;',
  32704. 'begin',
  32705. ' p:=typeinfo(TJSArray);']);
  32706. ConvertProgram;
  32707. CheckSource('TestRTTI_ExternalClass',
  32708. LinesToStr([ // statements
  32709. 'this.$rtti.$ExtClass("TJSObject", {',
  32710. ' jsclass: "Object"',
  32711. '});',
  32712. 'this.$rtti.$ExtClass("TJSArray", {',
  32713. ' ancestor: this.$rtti["TJSObject"],',
  32714. ' jsclass: "Array"',
  32715. '});',
  32716. 'this.p = null;',
  32717. 'this.tc = null;',
  32718. '']),
  32719. LinesToStr([ // $mod.$main
  32720. '$mod.p = $mod.$rtti["TJSArray"];',
  32721. '']));
  32722. end;
  32723. procedure TTestModule.TestRTTI_Unit;
  32724. begin
  32725. WithTypeInfo:=true;
  32726. AddModuleWithIntfImplSrc('unit2.pas',
  32727. LinesToStr([
  32728. '{$mode delphi}',
  32729. 'type',
  32730. ' TWordArray = array of word;',
  32731. ' TArray<T> = array of T;',
  32732. '']),
  32733. '');
  32734. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32735. Add([
  32736. '{$mode delphi}',
  32737. 'interface',
  32738. 'uses unit2;',
  32739. 'type',
  32740. ' IBird = interface',
  32741. ' function Swoop: TWordArray;',
  32742. ' function Glide: TArray<word>;',
  32743. ' end;',
  32744. 'procedure Fly;',
  32745. 'implementation',
  32746. 'procedure Fly;',
  32747. 'var',
  32748. ' ta: tTypeInfoDynArray;',
  32749. ' ti: tTypeInfoInterface;',
  32750. 'begin',
  32751. ' ta:=typeinfo(TWordArray);',
  32752. ' ta:=typeinfo(TArray<word>);',
  32753. ' ti:=typeinfo(IBird);',
  32754. 'end;',
  32755. '']);
  32756. ConvertUnit;
  32757. CheckSource('TestRTTI_ExternalClass',
  32758. LinesToStr([ // statements
  32759. 'rtl.createInterface(',
  32760. ' this,',
  32761. ' "IBird",',
  32762. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32763. ' ["Swoop", "Glide"],',
  32764. ' pas.system.IUnknown,',
  32765. ' function () {',
  32766. ' var $r = this.$rtti;',
  32767. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32768. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32769. ' }',
  32770. ');',
  32771. 'this.Fly = function () {',
  32772. ' var ta = null;',
  32773. ' var ti = null;',
  32774. ' ta = pas.unit2.$rtti["TWordArray"];',
  32775. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32776. ' ti = $mod.$rtti["IBird"];',
  32777. '};',
  32778. '']),
  32779. LinesToStr([ // $mod.$main
  32780. '']));
  32781. end;
  32782. procedure TTestModule.TestResourcestringProgram;
  32783. begin
  32784. AddModuleWithIntfImplSrc('unit2.pas',
  32785. LinesToStr([
  32786. 'resourcestring Title = ''Nice'';',
  32787. '']),
  32788. '');
  32789. StartProgram(true);
  32790. Add([
  32791. 'uses unit2;',
  32792. 'const Bar = ''bar'';',
  32793. 'resourcestring',
  32794. ' Red = ''red'';',
  32795. ' Foobar = ''fOo''+bar;',
  32796. 'var s: string;',
  32797. ' c: char;',
  32798. 'begin',
  32799. ' s:=red;',
  32800. ' s:=test1.red;',
  32801. ' s:=Title;',
  32802. ' c:=red[1];',
  32803. ' c:=test1.red[2];',
  32804. ' if red=foobar then ;',
  32805. ' if red[3]=red[4] then ;']);
  32806. ConvertProgram;
  32807. CheckSource('TestResourcestringProgram',
  32808. LinesToStr([ // statements
  32809. 'this.Bar = "bar";',
  32810. 'this.s = "";',
  32811. 'this.c = "";',
  32812. '$mod.$resourcestrings = {',
  32813. ' Red: {',
  32814. ' org: "red"',
  32815. ' },',
  32816. ' Foobar: {',
  32817. ' org: "fOobar"',
  32818. ' }',
  32819. '};',
  32820. '']),
  32821. LinesToStr([ // $mod.$main
  32822. '$mod.s = rtl.getResStr($mod, "Red");',
  32823. '$mod.s = rtl.getResStr($mod, "Red");',
  32824. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32825. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32826. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32827. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32828. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32829. '']));
  32830. end;
  32831. procedure TTestModule.TestResourcestringUnit;
  32832. begin
  32833. AddModuleWithIntfImplSrc('unit2.pas',
  32834. LinesToStr([
  32835. 'resourcestring Title = ''Nice'';',
  32836. '']),
  32837. '');
  32838. StartUnit(true);
  32839. Add([
  32840. 'interface',
  32841. 'uses unit2;',
  32842. 'const Red = ''rEd'';',
  32843. 'resourcestring',
  32844. ' Blue = ''blue'';',
  32845. ' NotRed = ''not''+Red;',
  32846. 'var s: string;',
  32847. 'implementation',
  32848. 'resourcestring',
  32849. ' ImplGreen = ''green'';',
  32850. 'initialization',
  32851. ' s:=blue+ImplGreen;',
  32852. ' s:=test1.blue+test1.implgreen;',
  32853. ' s:=blue[1]+implgreen[2];',
  32854. ' s:=Title;',
  32855. '']);
  32856. ConvertUnit;
  32857. CheckSource('TestResourcestringUnit',
  32858. LinesToStr([ // statements
  32859. 'this.Red = "rEd";',
  32860. 'this.s = "";',
  32861. '$mod.$resourcestrings = {',
  32862. ' Blue: {',
  32863. ' org: "blue"',
  32864. ' },',
  32865. ' NotRed: {',
  32866. ' org: "notrEd"',
  32867. ' },',
  32868. ' ImplGreen: {',
  32869. ' org: "green"',
  32870. ' }',
  32871. '};',
  32872. '']),
  32873. LinesToStr([ // $mod.$main
  32874. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32875. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32876. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32877. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32878. '']));
  32879. end;
  32880. procedure TTestModule.TestResourcestringImplementation;
  32881. begin
  32882. StartUnit(false);
  32883. Add([
  32884. 'interface',
  32885. 'implementation',
  32886. 'resourcestring',
  32887. ' ImplRed = ''red'';']);
  32888. ConvertUnit;
  32889. CheckSource('TestResourcestringImplementation',
  32890. LinesToStr([ // intf statements
  32891. 'var $impl = $mod.$impl;']),
  32892. LinesToStr([ // $mod.$init
  32893. '']),
  32894. LinesToStr([ // impl statements
  32895. '$mod.$resourcestrings = {',
  32896. ' ImplRed: {',
  32897. ' org: "red"',
  32898. ' }',
  32899. '};',
  32900. '']));
  32901. end;
  32902. procedure TTestModule.TestAttributes_Members;
  32903. begin
  32904. WithTypeInfo:=true;
  32905. StartProgram(false);
  32906. Add([
  32907. '{$modeswitch PrefixedAttributes}',
  32908. 'type',
  32909. ' TObject = class',
  32910. ' constructor Create;',
  32911. ' end;',
  32912. ' TCustomAttribute = class',
  32913. ' constructor Create(Id: word);',
  32914. ' end;',
  32915. ' [Missing]',
  32916. ' TBird = class',
  32917. ' published',
  32918. ' [Tcustom]',
  32919. ' FField: word;',
  32920. ' [tcustom(14)]',
  32921. ' property Size: word read FField;',
  32922. ' [Tcustom(15)]',
  32923. ' procedure Fly; virtual; abstract;',
  32924. ' end;',
  32925. ' TRec = record',
  32926. ' [Tcustom,tcustom(14)]',
  32927. ' Size: word;',
  32928. ' end;',
  32929. 'constructor TObject.Create; begin end;',
  32930. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32931. 'begin',
  32932. '']);
  32933. ConvertProgram;
  32934. CheckSource('TestAttributes_Members',
  32935. LinesToStr([ // statements
  32936. 'rtl.createClass(this, "TObject", null, function () {',
  32937. ' this.$init = function () {',
  32938. ' };',
  32939. ' this.$final = function () {',
  32940. ' };',
  32941. ' this.Create = function () {',
  32942. ' return this;',
  32943. ' };',
  32944. '});',
  32945. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32946. ' this.Create$1 = function (Id) {',
  32947. ' return this;',
  32948. ' };',
  32949. '});',
  32950. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32951. ' this.$init = function () {',
  32952. ' $mod.TObject.$init.call(this);',
  32953. ' this.FField = 0;',
  32954. ' };',
  32955. ' var $r = this.$rtti;',
  32956. ' $r.addField("FField", rtl.word, {',
  32957. ' attr: [$mod.TCustomAttribute, "Create"]',
  32958. ' });',
  32959. ' $r.addProperty(',
  32960. ' "Size",',
  32961. ' 0,',
  32962. ' rtl.word,',
  32963. ' "FField",',
  32964. ' "",',
  32965. ' {',
  32966. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32967. ' }',
  32968. ' );',
  32969. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32970. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32971. ' });',
  32972. '});',
  32973. 'rtl.recNewT(this, "TRec", function () {',
  32974. ' this.Size = 0;',
  32975. ' this.$eq = function (b) {',
  32976. ' return this.Size === b.Size;',
  32977. ' };',
  32978. ' this.$assign = function (s) {',
  32979. ' this.Size = s.Size;',
  32980. ' return this;',
  32981. ' };',
  32982. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32983. ' $r.addField("Size", rtl.word, {',
  32984. ' attr: [',
  32985. ' $mod.TCustomAttribute,',
  32986. ' "Create",',
  32987. ' $mod.TCustomAttribute,',
  32988. ' "Create$1",',
  32989. ' [14]',
  32990. ' ]',
  32991. ' });',
  32992. '});',
  32993. '']),
  32994. LinesToStr([ // $mod.$main
  32995. '']));
  32996. end;
  32997. procedure TTestModule.TestAttributes_Types;
  32998. begin
  32999. WithTypeInfo:=true;
  33000. StartProgram(false);
  33001. Add([
  33002. '{$modeswitch PrefixedAttributes}',
  33003. 'type',
  33004. ' TObject = class',
  33005. ' constructor Create(Id: word);',
  33006. ' end;',
  33007. ' TCustomAttribute = class',
  33008. ' end;',
  33009. ' [TCustom(1)]',
  33010. ' TMyClass = class',
  33011. ' end;',
  33012. ' [TCustom(11)]',
  33013. ' TMyDescendant = class(TMyClass)',
  33014. ' end;',
  33015. ' [TCustom(2)]',
  33016. ' TRec = record',
  33017. ' end;',
  33018. ' [TCustom(3)]',
  33019. ' TInt = type word;',
  33020. 'constructor TObject.Create(Id: word);',
  33021. 'begin',
  33022. 'end;',
  33023. 'var p: pointer;',
  33024. 'begin',
  33025. ' p:=typeinfo(TMyClass);',
  33026. ' p:=typeinfo(TRec);',
  33027. ' p:=typeinfo(TInt);',
  33028. '']);
  33029. ConvertProgram;
  33030. CheckSource('TestAttributes_Types',
  33031. LinesToStr([ // statements
  33032. 'rtl.createClass(this, "TObject", null, function () {',
  33033. ' this.$init = function () {',
  33034. ' };',
  33035. ' this.$final = function () {',
  33036. ' };',
  33037. ' this.Create = function (Id) {',
  33038. ' return this;',
  33039. ' };',
  33040. '});',
  33041. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33042. '});',
  33043. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33044. ' var $r = this.$rtti;',
  33045. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33046. '});',
  33047. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33048. ' var $r = this.$rtti;',
  33049. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33050. '});',
  33051. 'rtl.recNewT(this, "TRec", function () {',
  33052. ' this.$eq = function (b) {',
  33053. ' return true;',
  33054. ' };',
  33055. ' this.$assign = function (s) {',
  33056. ' return this;',
  33057. ' };',
  33058. ' $mod.$rtti.$Record("TRec", {',
  33059. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33060. ' });',
  33061. '});',
  33062. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33063. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33064. '});',
  33065. 'this.p = null;',
  33066. '']),
  33067. LinesToStr([ // $mod.$main
  33068. '$mod.p = $mod.$rtti["TMyClass"];',
  33069. '$mod.p = $mod.$rtti["TRec"];',
  33070. '$mod.p = $mod.$rtti["TInt"];',
  33071. '']));
  33072. end;
  33073. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33074. begin
  33075. WithTypeInfo:=true;
  33076. StartProgram(false);
  33077. Add([
  33078. '{$modeswitch PrefixedAttributes}',
  33079. 'type',
  33080. ' TObject = class',
  33081. ' constructor Create;',
  33082. ' end;',
  33083. ' TCustomAttribute = class',
  33084. ' end;',
  33085. ' THelper = class helper for TCustomAttribute',
  33086. ' constructor Create(Id: word);',
  33087. ' end;',
  33088. ' [TCustom(3)]',
  33089. ' TMyInt = word;',
  33090. 'constructor TObject.Create; begin end;',
  33091. 'constructor THelper.Create(Id: word); begin end;',
  33092. 'begin',
  33093. ' if typeinfo(TMyInt)=nil then ;']);
  33094. ConvertProgram;
  33095. end;
  33096. procedure TTestModule.TestAttributes_InterfacesList;
  33097. begin
  33098. WithTypeInfo:=true;
  33099. StartProgram(false);
  33100. Add([
  33101. '{$mode Delphi}',
  33102. 'type',
  33103. ' TObject = class',
  33104. ' constructor Create;',
  33105. ' end;',
  33106. ' IInterface = interface end;',
  33107. ' TCustomAttribute = class',
  33108. ' end;',
  33109. ' Red = class(TCustomAttribute);',
  33110. ' Blue = class(TCustomAttribute);',
  33111. ' [Red]',
  33112. ' IBird<T> = interface',
  33113. ' procedure Fly;',
  33114. ' end;',
  33115. ' [Blue]',
  33116. ' IEagle = interface(IBird<Word>)',
  33117. ' procedure Dive;',
  33118. ' end;',
  33119. ' TAnt = class(TObject, IEagle)',
  33120. ' procedure Fly; virtual; abstract;',
  33121. ' procedure Dive; virtual; abstract;',
  33122. ' end;',
  33123. 'constructor TObject.Create;',
  33124. 'begin',
  33125. 'end;',
  33126. 'begin',
  33127. '']);
  33128. ConvertProgram;
  33129. CheckSource('TestAttributes_InterfacesList',
  33130. LinesToStr([ // statements
  33131. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33132. 'rtl.createClass(this, "TObject", null, function () {',
  33133. ' this.$init = function () {',
  33134. ' };',
  33135. ' this.$final = function () {',
  33136. ' };',
  33137. ' this.Create = function () {',
  33138. ' return this;',
  33139. ' };',
  33140. '});',
  33141. 'rtl.createInterface(',
  33142. ' this,',
  33143. ' "IInterface",',
  33144. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33145. ' [],',
  33146. ' null,',
  33147. ' function () {',
  33148. ' this.$kind = "com";',
  33149. ' }',
  33150. ');',
  33151. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33152. '});',
  33153. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33154. '});',
  33155. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33156. '});',
  33157. 'rtl.createInterface(',
  33158. ' this,',
  33159. ' "IBird$G1",',
  33160. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33161. ' ["Fly"],',
  33162. ' this.IInterface,',
  33163. ' function () {',
  33164. ' var $r = this.$rtti;',
  33165. ' $r.addMethod("Fly", 0, []);',
  33166. ' $r.attr = [$mod.Red, "Create"];',
  33167. ' },',
  33168. ' "IBird<System.Word>"',
  33169. ');',
  33170. 'rtl.createInterface(',
  33171. ' this,',
  33172. ' "IEagle",',
  33173. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33174. ' ["Dive"],',
  33175. ' this.IBird$G1,',
  33176. ' function () {',
  33177. ' var $r = this.$rtti;',
  33178. ' $r.addMethod("Dive", 0, []);',
  33179. ' $r.attr = [$mod.Blue, "Create"];',
  33180. ' }',
  33181. ');',
  33182. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33183. ' rtl.addIntf(this, $mod.IEagle);',
  33184. '});',
  33185. '']),
  33186. LinesToStr([ // $mod.$main
  33187. '']));
  33188. end;
  33189. procedure TTestModule.TestAssert;
  33190. begin
  33191. StartProgram(false);
  33192. Add([
  33193. 'procedure DoIt;',
  33194. 'var',
  33195. ' b: boolean;',
  33196. ' s: string;',
  33197. 'begin',
  33198. ' {$Assertions on}',
  33199. ' Assert(b);',
  33200. 'end;',
  33201. 'begin',
  33202. ' DoIt;',
  33203. '']);
  33204. ConvertProgram;
  33205. CheckSource('TestAssert',
  33206. LinesToStr([ // statements
  33207. 'this.DoIt = function () {',
  33208. ' var b = false;',
  33209. ' var s = "";',
  33210. ' if (!b) throw "assert failed";',
  33211. '};',
  33212. '']),
  33213. LinesToStr([ // $mod.$main
  33214. '$mod.DoIt();',
  33215. '']));
  33216. end;
  33217. procedure TTestModule.TestAssert_SysUtils;
  33218. begin
  33219. AddModuleWithIntfImplSrc('SysUtils.pas',
  33220. LinesToStr([
  33221. 'type',
  33222. ' TObject = class',
  33223. ' constructor Create;',
  33224. ' end;',
  33225. ' EAssertionFailed = class',
  33226. ' constructor Create(s: string);',
  33227. ' end;',
  33228. '']),
  33229. LinesToStr([
  33230. 'constructor TObject.Create;',
  33231. 'begin end;',
  33232. 'constructor EAssertionFailed.Create(s: string);',
  33233. 'begin end;',
  33234. '']) );
  33235. StartProgram(true);
  33236. Add([
  33237. 'uses sysutils;',
  33238. 'procedure DoIt;',
  33239. 'var',
  33240. ' b: boolean;',
  33241. ' s: string;',
  33242. 'begin',
  33243. ' {$Assertions on}',
  33244. ' Assert(b);',
  33245. ' Assert(b,''msg'');',
  33246. 'end;',
  33247. 'begin',
  33248. ' DoIt;',
  33249. '']);
  33250. ConvertProgram;
  33251. CheckSource('TestAssert_SysUtils',
  33252. LinesToStr([ // statements
  33253. 'this.DoIt = function () {',
  33254. ' var b = false;',
  33255. ' var s = "";',
  33256. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33257. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33258. '};',
  33259. '']),
  33260. LinesToStr([ // $mod.$main
  33261. '$mod.DoIt();',
  33262. '']));
  33263. end;
  33264. procedure TTestModule.TestObjectChecks;
  33265. begin
  33266. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33267. StartProgram(false);
  33268. Add([
  33269. 'type',
  33270. ' TObject = class',
  33271. ' procedure DoIt;',
  33272. ' end;',
  33273. ' TClass = class of tobject;',
  33274. ' TBird = class',
  33275. ' end;',
  33276. ' TBirdClass = class of TBird;',
  33277. 'var',
  33278. ' o : TObject;',
  33279. ' c: TClass;',
  33280. ' b: TBird;',
  33281. ' bc: TBirdClass;',
  33282. 'procedure TObject.DoIt;',
  33283. 'begin',
  33284. ' b:=TBird(o);',
  33285. 'end;',
  33286. 'begin',
  33287. ' o.DoIt;',
  33288. ' b:=TBird(o);',
  33289. ' bc:=TBirdClass(c);',
  33290. '']);
  33291. ConvertProgram;
  33292. CheckSource('TestCheckMethodCall',
  33293. LinesToStr([ // statements
  33294. 'rtl.createClass(this, "TObject", null, function () {',
  33295. ' this.$init = function () {',
  33296. ' };',
  33297. ' this.$final = function () {',
  33298. ' };',
  33299. ' this.DoIt = function () {',
  33300. ' rtl.checkMethodCall(this,$mod.TObject);',
  33301. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33302. ' };',
  33303. '});',
  33304. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33305. '});',
  33306. 'this.o = null;',
  33307. 'this.c = null;',
  33308. 'this.b = null;',
  33309. 'this.bc = null;',
  33310. '']),
  33311. LinesToStr([ // $mod.$main
  33312. '$mod.o.DoIt();',
  33313. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33314. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33315. '']));
  33316. end;
  33317. procedure TTestModule.TestOverflowChecks_Int;
  33318. begin
  33319. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33320. StartProgram(false);
  33321. Add([
  33322. 'procedure DoIt;',
  33323. 'var',
  33324. ' b: byte;',
  33325. ' n: nativeint;',
  33326. ' u: nativeuint;',
  33327. ' c: currency;',
  33328. 'begin',
  33329. ' n:=n+n;',
  33330. ' n:=n-n;',
  33331. ' n:=n+b;',
  33332. ' n:=b-n;',
  33333. ' n:=n*n;',
  33334. ' n:=n*u;',
  33335. ' c:=c+b;',
  33336. ' c:=b+c;',
  33337. ' c:=c*b;',
  33338. ' c:=b*c;',
  33339. 'end;',
  33340. 'begin',
  33341. '']);
  33342. ConvertProgram;
  33343. CheckSource('TestOverflowChecks_Int',
  33344. LinesToStr([ // statements
  33345. 'this.DoIt = function () {',
  33346. ' var b = 0;',
  33347. ' var n = 0;',
  33348. ' var u = 0;',
  33349. ' var c = 0;',
  33350. ' n = rtl.oc(n + n);',
  33351. ' n = rtl.oc(n - n);',
  33352. ' n = rtl.oc(n + b);',
  33353. ' n = rtl.oc(b - n);',
  33354. ' n = rtl.oc(n * n);',
  33355. ' n = rtl.oc(n * u);',
  33356. ' c = rtl.oc(c + (b * 10000));',
  33357. ' c = rtl.oc((b * 10000) + c);',
  33358. ' c = rtl.oc(c * b);',
  33359. ' c = rtl.oc(b * c);',
  33360. '};',
  33361. '']),
  33362. LinesToStr([ // $mod.$main
  33363. '']));
  33364. end;
  33365. procedure TTestModule.TestRangeChecks_AssignInt;
  33366. begin
  33367. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33368. StartProgram(false);
  33369. Add([
  33370. '{$R+}',
  33371. 'var',
  33372. ' b: byte = 2;',
  33373. ' w: word = 3;',
  33374. 'procedure DoIt(p: byte);',
  33375. 'begin',
  33376. ' b:=w;',
  33377. ' b+=w;',
  33378. ' b:=1;',
  33379. 'end;',
  33380. '{$R-}',
  33381. 'procedure DoSome;',
  33382. 'begin',
  33383. ' DoIt(w);',
  33384. ' b:=w;',
  33385. ' b:=2;',
  33386. 'end;',
  33387. 'begin',
  33388. '{$R+}',
  33389. '']);
  33390. ConvertProgram;
  33391. CheckSource('TestRangeChecks_AssignInt',
  33392. LinesToStr([ // statements
  33393. 'this.b = 2;',
  33394. 'this.w = 3;',
  33395. 'this.DoIt = function (p) {',
  33396. ' rtl.rc(p, 0, 255);',
  33397. ' $mod.b = rtl.rc($mod.w,0,255);',
  33398. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33399. ' $mod.b = 1;',
  33400. '};',
  33401. 'this.DoSome = function () {',
  33402. ' $mod.DoIt($mod.w);',
  33403. ' $mod.b = $mod.w;',
  33404. ' $mod.b = 2;',
  33405. '};',
  33406. '']),
  33407. LinesToStr([ // $mod.$main
  33408. '']));
  33409. end;
  33410. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33411. begin
  33412. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33413. StartProgram(false);
  33414. Add([
  33415. '{$R+}',
  33416. 'type Ten = 1..10;',
  33417. 'var',
  33418. ' b: Ten = 2;',
  33419. ' w: Ten = 3;',
  33420. 'procedure DoIt(p: Ten);',
  33421. 'begin',
  33422. ' b:=w;',
  33423. ' b+=w;',
  33424. ' b:=1;',
  33425. 'end;',
  33426. '{$R-}',
  33427. 'procedure DoSome;',
  33428. 'begin',
  33429. ' DoIt(w);',
  33430. ' b:=w;',
  33431. ' b:=2;',
  33432. 'end;',
  33433. 'begin',
  33434. '{$R+}',
  33435. '']);
  33436. ConvertProgram;
  33437. CheckSource('TestRangeChecks_AssignIntRange',
  33438. LinesToStr([ // statements
  33439. 'this.b = 2;',
  33440. 'this.w = 3;',
  33441. 'this.DoIt = function (p) {',
  33442. ' rtl.rc(p, 1, 10);',
  33443. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33444. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33445. ' $mod.b = 1;',
  33446. '};',
  33447. 'this.DoSome = function () {',
  33448. ' $mod.DoIt($mod.w);',
  33449. ' $mod.b = $mod.w;',
  33450. ' $mod.b = 2;',
  33451. '};',
  33452. '']),
  33453. LinesToStr([ // $mod.$main
  33454. '']));
  33455. end;
  33456. procedure TTestModule.TestRangeChecks_AssignEnum;
  33457. begin
  33458. StartProgram(false);
  33459. Add([
  33460. '{$R+}',
  33461. 'type TEnum = (red,green);',
  33462. 'var',
  33463. ' e: TEnum = red;',
  33464. 'procedure DoIt(p: TEnum);',
  33465. 'begin',
  33466. ' e:=p;',
  33467. ' p:=TEnum(0);',
  33468. ' p:=succ(e);',
  33469. 'end;',
  33470. '{$R-}',
  33471. 'procedure DoSome;',
  33472. 'begin',
  33473. ' DoIt(e);',
  33474. ' e:=TEnum(1);',
  33475. ' e:=pred(e);',
  33476. 'end;',
  33477. 'begin',
  33478. '{$R+}',
  33479. '']);
  33480. ConvertProgram;
  33481. CheckSource('TestRangeChecks_AssignEnum',
  33482. LinesToStr([ // statements
  33483. 'this.TEnum = {',
  33484. ' "0": "red",',
  33485. ' red: 0,',
  33486. ' "1": "green",',
  33487. ' green: 1',
  33488. '};',
  33489. 'this.e = this.TEnum.red;',
  33490. 'this.DoIt = function (p) {',
  33491. ' rtl.rc(p, 0, 1);',
  33492. ' $mod.e = rtl.rc(p, 0, 1);',
  33493. ' p = 0;',
  33494. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33495. '};',
  33496. 'this.DoSome = function () {',
  33497. ' $mod.DoIt($mod.e);',
  33498. ' $mod.e = 1;',
  33499. ' $mod.e = $mod.e - 1;',
  33500. '};',
  33501. '']),
  33502. LinesToStr([ // $mod.$main
  33503. '']));
  33504. end;
  33505. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33506. begin
  33507. StartProgram(false);
  33508. Add([
  33509. '{$R+}',
  33510. 'type',
  33511. ' TEnum = (red,green);',
  33512. ' TEnumRg = red..green;',
  33513. 'var',
  33514. ' e: TEnumRg = red;',
  33515. 'procedure DoIt(p: TEnumRg);',
  33516. 'begin',
  33517. ' e:=p;',
  33518. ' p:=TEnumRg(0);',
  33519. ' p:=succ(e);',
  33520. 'end;',
  33521. '{$R-}',
  33522. 'procedure DoSome;',
  33523. 'begin',
  33524. ' DoIt(e);',
  33525. ' e:=TEnum(1);',
  33526. ' e:=pred(e);',
  33527. 'end;',
  33528. 'begin',
  33529. '{$R+}',
  33530. '']);
  33531. ConvertProgram;
  33532. CheckSource('TestRangeChecks_AssignEnumRange',
  33533. LinesToStr([ // statements
  33534. 'this.TEnum = {',
  33535. ' "0": "red",',
  33536. ' red: 0,',
  33537. ' "1": "green",',
  33538. ' green: 1',
  33539. '};',
  33540. 'this.e = this.TEnum.red;',
  33541. 'this.DoIt = function (p) {',
  33542. ' rtl.rc(p, 0, 1);',
  33543. ' $mod.e = rtl.rc(p, 0, 1);',
  33544. ' p = 0;',
  33545. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33546. '};',
  33547. 'this.DoSome = function () {',
  33548. ' $mod.DoIt($mod.e);',
  33549. ' $mod.e = 1;',
  33550. ' $mod.e = $mod.e - 1;',
  33551. '};',
  33552. '']),
  33553. LinesToStr([ // $mod.$main
  33554. '']));
  33555. end;
  33556. procedure TTestModule.TestRangeChecks_AssignChar;
  33557. begin
  33558. StartProgram(false);
  33559. Add([
  33560. '{$R+}',
  33561. 'type',
  33562. ' TLetter = char;',
  33563. 'var',
  33564. ' b: TLetter = ''2'';',
  33565. ' w: TLetter = ''3'';',
  33566. 'procedure DoIt(p: TLetter);',
  33567. 'begin',
  33568. ' b:=w;',
  33569. ' b:=''1'';',
  33570. 'end;',
  33571. '{$R-}',
  33572. 'procedure DoSome;',
  33573. 'begin',
  33574. ' DoIt(w);',
  33575. ' b:=w;',
  33576. ' b:=''2'';',
  33577. 'end;',
  33578. 'begin',
  33579. '{$R+}',
  33580. '']);
  33581. ConvertProgram;
  33582. CheckSource('TestRangeChecks_AssignChar',
  33583. LinesToStr([ // statements
  33584. 'this.b = "2";',
  33585. 'this.w = "3";',
  33586. 'this.DoIt = function (p) {',
  33587. ' rtl.rcc(p, 0, 65535);',
  33588. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33589. ' $mod.b = "1";',
  33590. '};',
  33591. 'this.DoSome = function () {',
  33592. ' $mod.DoIt($mod.w);',
  33593. ' $mod.b = $mod.w;',
  33594. ' $mod.b = "2";',
  33595. '};',
  33596. '']),
  33597. LinesToStr([ // $mod.$main
  33598. '']));
  33599. end;
  33600. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33601. begin
  33602. StartProgram(false);
  33603. Add([
  33604. '{$R+}',
  33605. 'type TDigit = ''0''..''9'';',
  33606. 'var',
  33607. ' b: TDigit = ''2'';',
  33608. ' w: TDigit = ''3'';',
  33609. 'procedure DoIt(p: TDigit);',
  33610. 'begin',
  33611. ' b:=w;',
  33612. ' b:=''1'';',
  33613. 'end;',
  33614. '{$R-}',
  33615. 'procedure DoSome;',
  33616. 'begin',
  33617. ' DoIt(w);',
  33618. ' b:=w;',
  33619. ' b:=''2'';',
  33620. 'end;',
  33621. 'begin',
  33622. '{$R+}',
  33623. '']);
  33624. ConvertProgram;
  33625. CheckSource('TestRangeChecks_AssignCharRange',
  33626. LinesToStr([ // statements
  33627. 'this.b = "2";',
  33628. 'this.w = "3";',
  33629. 'this.DoIt = function (p) {',
  33630. ' rtl.rcc(p, 48, 57);',
  33631. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33632. ' $mod.b = "1";',
  33633. '};',
  33634. 'this.DoSome = function () {',
  33635. ' $mod.DoIt($mod.w);',
  33636. ' $mod.b = $mod.w;',
  33637. ' $mod.b = "2";',
  33638. '};',
  33639. '']),
  33640. LinesToStr([ // $mod.$main
  33641. '']));
  33642. end;
  33643. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33644. begin
  33645. StartProgram(false);
  33646. Add([
  33647. '{$R+}',
  33648. 'type',
  33649. ' Ten = 1..10;',
  33650. ' TArr = array of Ten;',
  33651. ' TArrArr = array of TArr;',
  33652. ' TArrByte = array[byte] of Ten;',
  33653. ' TArrChar = array[''0''..''9''] of Ten;',
  33654. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33655. ' TObject = class',
  33656. ' A: TArr;',
  33657. ' end;',
  33658. 'procedure DoIt;',
  33659. 'var',
  33660. ' Arr: TArr;',
  33661. ' ArrArr: TArrArr;',
  33662. ' ArrByte: TArrByte;',
  33663. ' ArrChar: TArrChar;',
  33664. ' ArrByteChar: TArrByteChar;',
  33665. ' i: Ten;',
  33666. ' c: char;',
  33667. ' o: tobject;',
  33668. 'begin',
  33669. ' i:=Arr[1];',
  33670. ' i:=ArrByteChar[1,''2''];',
  33671. ' Arr[1]:=Arr[1];',
  33672. ' Arr[i]:=Arr[i];',
  33673. ' ArrByte[3]:=ArrByte[3];',
  33674. ' ArrByte[i]:=ArrByte[i];',
  33675. ' ArrChar[''5'']:=ArrChar[''5''];',
  33676. ' ArrChar[c]:=ArrChar[c];',
  33677. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33678. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33679. ' o.a[i]:=o.a[i];',
  33680. 'end;',
  33681. 'begin',
  33682. '']);
  33683. ConvertProgram;
  33684. CheckSource('TestRangeChecks_ArrayIndex',
  33685. LinesToStr([ // statements
  33686. 'this.TArrByteChar$clone = function (a) {',
  33687. ' var b = [];',
  33688. ' b.length = 256;',
  33689. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33690. ' return b;',
  33691. '};',
  33692. 'rtl.createClass(this, "TObject", null, function () {',
  33693. ' this.$init = function () {',
  33694. ' this.A = [];',
  33695. ' };',
  33696. ' this.$final = function () {',
  33697. ' this.A = undefined;',
  33698. ' };',
  33699. '});',
  33700. 'this.DoIt = function () {',
  33701. ' var Arr = [];',
  33702. ' var ArrArr = [];',
  33703. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33704. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33705. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33706. ' var i = 0;',
  33707. ' var c = "";',
  33708. ' var o = null;',
  33709. ' i = rtl.rc(Arr[1], 1, 10);',
  33710. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33711. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33712. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33713. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33714. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33715. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33716. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33717. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33718. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33719. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33720. '};',
  33721. '']),
  33722. LinesToStr([ // $mod.$main
  33723. '']));
  33724. end;
  33725. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33726. begin
  33727. StartProgram(false);
  33728. Add([
  33729. '{$R+}',
  33730. 'type',
  33731. ' Ten = 1..10;',
  33732. ' TRec = record x: Ten end;',
  33733. ' TArr = array of TRec;',
  33734. ' TArrArr = array of TArr;',
  33735. ' TObject = class',
  33736. ' A: TArr;',
  33737. ' end;',
  33738. 'procedure DoIt;',
  33739. 'var',
  33740. ' Arr: TArr;',
  33741. ' ArrArr: TArrArr;',
  33742. ' i: Ten;',
  33743. ' o: tobject;',
  33744. 'begin',
  33745. ' Arr[1]:=Arr[1];',
  33746. ' Arr[i]:=Arr[i+1];',
  33747. ' o.a[i]:=o.a[i+2];',
  33748. 'end;',
  33749. 'begin',
  33750. '']);
  33751. ConvertProgram;
  33752. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33753. LinesToStr([ // statements
  33754. 'rtl.recNewT(this, "TRec", function () {',
  33755. ' this.x = 0;',
  33756. ' this.$eq = function (b) {',
  33757. ' return this.x === b.x;',
  33758. ' };',
  33759. ' this.$assign = function (s) {',
  33760. ' this.x = s.x;',
  33761. ' return this;',
  33762. ' };',
  33763. '});',
  33764. 'rtl.createClass(this, "TObject", null, function () {',
  33765. ' this.$init = function () {',
  33766. ' this.A = [];',
  33767. ' };',
  33768. ' this.$final = function () {',
  33769. ' this.A = undefined;',
  33770. ' };',
  33771. '});',
  33772. 'this.DoIt = function () {',
  33773. ' var Arr = [];',
  33774. ' var ArrArr = [];',
  33775. ' var i = 0;',
  33776. ' var o = null;',
  33777. ' Arr[1].$assign(Arr[1]);',
  33778. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33779. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33780. '};',
  33781. '']),
  33782. LinesToStr([ // $mod.$main
  33783. '']));
  33784. end;
  33785. procedure TTestModule.TestRangeChecks_StringIndex;
  33786. begin
  33787. StartProgram(false);
  33788. Add([
  33789. 'type',
  33790. ' TObject = class',
  33791. ' S: string;',
  33792. ' end;',
  33793. '{$R+}',
  33794. 'procedure DoIt(var h: string);',
  33795. 'var',
  33796. ' s: string;',
  33797. ' i: longint;',
  33798. ' c: char;',
  33799. ' o: tobject;',
  33800. 'begin',
  33801. ' c:=s[1];',
  33802. ' s[i]:=s[i];',
  33803. ' h[i]:=h[i];',
  33804. ' c:=o.s[i];',
  33805. ' o.s[i]:=c;',
  33806. 'end;',
  33807. 'begin',
  33808. '']);
  33809. ConvertProgram;
  33810. CheckSource('TestRangeChecks_StringIndex',
  33811. LinesToStr([ // statements
  33812. 'rtl.createClass(this, "TObject", null, function () {',
  33813. ' this.$init = function () {',
  33814. ' this.S = "";',
  33815. ' };',
  33816. ' this.$final = function () {',
  33817. ' };',
  33818. '});',
  33819. 'this.DoIt = function (h) {',
  33820. ' var s = "";',
  33821. ' var i = 0;',
  33822. ' var c = "";',
  33823. ' var o = null;',
  33824. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33825. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33826. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33827. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33828. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33829. '};',
  33830. '']),
  33831. LinesToStr([ // $mod.$main
  33832. '']));
  33833. end;
  33834. procedure TTestModule.TestRangeChecks_TypecastInt;
  33835. begin
  33836. StartProgram(false);
  33837. Add([
  33838. '{$R+}',
  33839. 'var',
  33840. ' i: nativeint;',
  33841. ' b: byte;',
  33842. ' sh: shortint;',
  33843. ' w: word;',
  33844. ' sm: smallint;',
  33845. ' lw: longword;',
  33846. ' li: longint;',
  33847. 'begin',
  33848. ' b:=12+byte(i);',
  33849. ' sh:=12+shortint(i);',
  33850. ' w:=12+word(i);',
  33851. ' sm:=12+smallint(i);',
  33852. ' lw:=12+longword(i);',
  33853. ' li:=12+longint(i);',
  33854. '']);
  33855. ConvertProgram;
  33856. CheckSource('TestRangeChecks_TypecastInt',
  33857. LinesToStr([
  33858. 'this.i = 0;',
  33859. 'this.b = 0;',
  33860. 'this.sh = 0;',
  33861. 'this.w = 0;',
  33862. 'this.sm = 0;',
  33863. 'this.lw = 0;',
  33864. 'this.li = 0;',
  33865. '']),
  33866. LinesToStr([
  33867. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33868. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33869. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33870. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33871. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33872. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33873. '']));
  33874. end;
  33875. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33876. begin
  33877. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33878. StartProgram(false);
  33879. Add([
  33880. '{$modeswitch typehelpers}',
  33881. '{$R+}',
  33882. 'type',
  33883. ' TObject = class',
  33884. ' FSize: byte;',
  33885. ' property Size: byte read FSize;',
  33886. ' end;',
  33887. ' THelper = type helper for byte',
  33888. ' procedure SetIt(w: word);',
  33889. ' end;',
  33890. 'procedure THelper.SetIt(w: word);',
  33891. 'begin',
  33892. ' Self:=w;',
  33893. 'end;',
  33894. 'function GetIt: byte;',
  33895. 'begin',
  33896. ' Result.SetIt(2);',
  33897. 'end;',
  33898. 'var',
  33899. ' b: byte = 3;',
  33900. ' o: TObject;',
  33901. 'begin',
  33902. ' b.SetIt(14);',
  33903. ' with b do SetIt(15);',
  33904. ' o.Size.SetIt(16);',
  33905. '']);
  33906. ConvertProgram;
  33907. CheckSource('TestRangeChecks_AssignInt',
  33908. LinesToStr([ // statements
  33909. 'rtl.createClass(this, "TObject", null, function () {',
  33910. ' this.$init = function () {',
  33911. ' this.FSize = 0;',
  33912. ' };',
  33913. ' this.$final = function () {',
  33914. ' };',
  33915. '});',
  33916. 'rtl.createHelper(this, "THelper", null, function () {',
  33917. ' this.SetIt = function (w) {',
  33918. ' rtl.rc(w, 0, 65535);',
  33919. ' this.set(w);',
  33920. ' };',
  33921. '});',
  33922. 'this.GetIt = function () {',
  33923. ' var Result = 0;',
  33924. ' $mod.THelper.SetIt.call({',
  33925. ' get: function () {',
  33926. ' return Result;',
  33927. ' },',
  33928. ' set: function (v) {',
  33929. ' rtl.rc(v, 0, 255);',
  33930. ' Result = v;',
  33931. ' }',
  33932. ' }, 2);',
  33933. ' return Result;',
  33934. '};',
  33935. 'this.b = 3;',
  33936. 'this.o = null;',
  33937. '']),
  33938. LinesToStr([ // $mod.$main
  33939. '$mod.THelper.SetIt.call({',
  33940. ' p: $mod,',
  33941. ' get: function () {',
  33942. ' return this.p.b;',
  33943. ' },',
  33944. ' set: function (v) {',
  33945. ' rtl.rc(v, 0, 255);',
  33946. ' this.p.b = v;',
  33947. ' }',
  33948. '}, 14);',
  33949. 'var $with = $mod.b;',
  33950. '$mod.THelper.SetIt.call({',
  33951. ' get: function () {',
  33952. ' return $with;',
  33953. ' },',
  33954. ' set: function (v) {',
  33955. ' rtl.rc(v, 0, 255);',
  33956. ' $with = v;',
  33957. ' }',
  33958. '}, 15);',
  33959. '$mod.THelper.SetIt.call({',
  33960. ' p: $mod.o,',
  33961. ' get: function () {',
  33962. ' return this.p.FSize;',
  33963. ' },',
  33964. ' set: function (v) {',
  33965. ' rtl.rc(v, 0, 255);',
  33966. ' this.p.FSize = v;',
  33967. ' }',
  33968. '}, 16);',
  33969. '']));
  33970. end;
  33971. procedure TTestModule.TestRangeChecks_AssignCurrency;
  33972. begin
  33973. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33974. StartProgram(false);
  33975. Add([
  33976. '{$R+}',
  33977. 'var',
  33978. ' c: currency = 2.34;',
  33979. ' i: double;',
  33980. 'procedure DoIt(p: currency);',
  33981. 'begin',
  33982. ' c:=i;',
  33983. ' c+=i;',
  33984. ' c:=1;',
  33985. 'end;',
  33986. '{$R-}',
  33987. 'procedure DoSome;',
  33988. 'begin',
  33989. ' DoIt(i);',
  33990. ' c:=i;',
  33991. ' c:=2;',
  33992. 'end;',
  33993. 'begin',
  33994. '{$R+}',
  33995. '']);
  33996. ConvertProgram;
  33997. CheckSource('TestRangeChecks_AssignCurrency',
  33998. LinesToStr([ // statements
  33999. 'this.c = 2.34;',
  34000. 'this.i = 0.0;',
  34001. 'this.DoIt = function (p) {',
  34002. ' rtl.rc(p, -922337203685477, 922337203685477);',
  34003. ' $mod.c = rtl.rc(rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34004. ' rtl.rc($mod.c += rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34005. ' $mod.c = 10000;',
  34006. '};',
  34007. 'this.DoSome = function () {',
  34008. ' $mod.DoIt($mod.i * 10000);',
  34009. ' $mod.c = rtl.trunc($mod.i * 10000);',
  34010. ' $mod.c = 20000;',
  34011. '};',
  34012. '']),
  34013. LinesToStr([ // $mod.$main
  34014. '']));
  34015. end;
  34016. procedure TTestModule.TestAsync_Proc;
  34017. begin
  34018. StartProgram(false);
  34019. Add([
  34020. 'procedure Fly(w: word = 1); async; forward;',
  34021. 'procedure Run(w: word = 2); async;',
  34022. 'begin',
  34023. ' Fly(w);',
  34024. ' Fly;',
  34025. ' await(Fly(w));',
  34026. ' await(Fly);',
  34027. 'end;',
  34028. 'procedure Fly(w: word); ',
  34029. 'begin',
  34030. 'end;',
  34031. 'begin',
  34032. ' Run;',
  34033. ' Run(3);',
  34034. '']);
  34035. CheckResolverUnexpectedHints();
  34036. ConvertProgram;
  34037. CheckSource('TestAsync_Proc',
  34038. LinesToStr([ // statements
  34039. 'this.Run = async function (w) {',
  34040. ' $mod.Fly(w);',
  34041. ' $mod.Fly(1);',
  34042. ' await $mod.Fly(w);',
  34043. ' await $mod.Fly(1);',
  34044. '};',
  34045. 'this.Fly = async function (w) {',
  34046. '};',
  34047. '']),
  34048. LinesToStr([
  34049. '$mod.Run(2);',
  34050. '$mod.Run(3);',
  34051. '']));
  34052. end;
  34053. procedure TTestModule.TestAsync_CallResultIsPromise;
  34054. begin
  34055. StartProgram(false);
  34056. Add([
  34057. '{$modeswitch externalclass}',
  34058. 'type',
  34059. ' TObject = class',
  34060. ' end;',
  34061. ' TJSPromise = class external name ''Promise''',
  34062. ' end;',
  34063. ' TBird = class',
  34064. ' function Fly: word; async; ',
  34065. ' end;',
  34066. 'function TBird.Fly: word; async; ',
  34067. 'begin',
  34068. ' Result:=3;',
  34069. ' Fly:=4+Result;',
  34070. ' if Result=5 then ;',
  34071. ' exit(6);',
  34072. 'end;',
  34073. 'function Run: word; async;',
  34074. 'begin',
  34075. ' Result:=11+Result;',
  34076. ' inc(Result);',
  34077. 'end;',
  34078. 'var',
  34079. ' p: TJSPromise;',
  34080. ' o: TBird;',
  34081. 'begin',
  34082. ' p:=Run;',
  34083. ' p:=Run();',
  34084. ' if Run=p then ;',
  34085. ' if p=Run then ;',
  34086. ' if Run()=p then ;',
  34087. ' if p=Run() then ;',
  34088. ' p:=o.Fly;',
  34089. ' p:=o.Fly();',
  34090. ' if o.Fly=p then ;',
  34091. ' if o.Fly()=p then ;',
  34092. ' with o do begin',
  34093. ' p:=Fly;',
  34094. ' p:=Fly();',
  34095. ' if Fly=p then ;',
  34096. ' if Fly()=p then ;',
  34097. ' end;',
  34098. '']);
  34099. CheckResolverUnexpectedHints();
  34100. ConvertProgram;
  34101. CheckSource('TestAsync_CallResultIsPromise',
  34102. LinesToStr([ // statements
  34103. 'rtl.createClass(this, "TObject", null, function () {',
  34104. ' this.$init = function () {',
  34105. ' };',
  34106. ' this.$final = function () {',
  34107. ' };',
  34108. '});',
  34109. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34110. ' this.Fly = async function () {',
  34111. ' var Result = 0;',
  34112. ' Result = 3;',
  34113. ' Result = 4 + Result;',
  34114. ' if (Result === 5) ;',
  34115. ' return 6;',
  34116. ' return Result;',
  34117. ' };',
  34118. '});',
  34119. 'this.Run = async function () {',
  34120. ' var Result = 0;',
  34121. ' Result = 11 + Result;',
  34122. ' Result += 1;',
  34123. ' return Result;',
  34124. '};',
  34125. 'this.p = null;',
  34126. 'this.o = null;',
  34127. '']),
  34128. LinesToStr([
  34129. '$mod.p = $mod.Run();',
  34130. '$mod.p = $mod.Run();',
  34131. 'if ($mod.Run() === $mod.p) ;',
  34132. 'if ($mod.p === $mod.Run()) ;',
  34133. 'if ($mod.Run() === $mod.p) ;',
  34134. 'if ($mod.p === $mod.Run()) ;',
  34135. '$mod.p = $mod.o.Fly();',
  34136. '$mod.p = $mod.o.Fly();',
  34137. 'if ($mod.o.Fly() === $mod.p) ;',
  34138. 'if ($mod.o.Fly() === $mod.p) ;',
  34139. 'var $with = $mod.o;',
  34140. '$mod.p = $with.Fly();',
  34141. '$mod.p = $with.Fly();',
  34142. 'if ($with.Fly() === $mod.p) ;',
  34143. 'if ($with.Fly() === $mod.p) ;',
  34144. '']));
  34145. end;
  34146. procedure TTestModule.TestAsync_ConstructorFail;
  34147. begin
  34148. StartProgram(false);
  34149. Add([
  34150. 'type',
  34151. ' TObject = class',
  34152. ' end;',
  34153. ' TBird = class',
  34154. ' constructor Create; async;',
  34155. ' end;',
  34156. 'constructor TBird.Create; async;',
  34157. 'begin',
  34158. 'end;',
  34159. 'begin',
  34160. '']);
  34161. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34162. ConvertProgram;
  34163. end;
  34164. procedure TTestModule.TestAsync_PropertyGetterFail;
  34165. begin
  34166. StartProgram(false);
  34167. Add([
  34168. 'type',
  34169. ' TObject = class',
  34170. ' end;',
  34171. ' TBird = class',
  34172. ' function GetSize: word; async;',
  34173. ' property Size: word read GetSize;',
  34174. ' end;',
  34175. 'function TBird.GetSize: word; async;',
  34176. 'begin',
  34177. 'end;',
  34178. 'begin',
  34179. '']);
  34180. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34181. ConvertProgram;
  34182. end;
  34183. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34184. begin
  34185. StartProgram(false);
  34186. Add([
  34187. 'procedure Run; async;',
  34188. 'begin',
  34189. ' await(word,1);',
  34190. 'end;',
  34191. 'begin',
  34192. '']);
  34193. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34194. ConvertProgram;
  34195. end;
  34196. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34197. begin
  34198. StartProgram(false);
  34199. Add([
  34200. 'type',
  34201. ' TObject = class',
  34202. ' end;',
  34203. ' TBird = class',
  34204. ' end;',
  34205. 'function Fly: TObject; async;',
  34206. 'begin',
  34207. 'end;',
  34208. 'procedure Run; async;',
  34209. 'begin',
  34210. ' await(TBird,Fly);',
  34211. 'end;',
  34212. 'begin',
  34213. '']);
  34214. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34215. ConvertProgram;
  34216. end;
  34217. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34218. begin
  34219. StartProgram(false);
  34220. Add([
  34221. 'procedure Crawl(w: double); ',
  34222. 'begin',
  34223. 'end;',
  34224. 'procedure Run(w: double);',
  34225. 'begin',
  34226. ' await(Crawl(w));',
  34227. 'end;',
  34228. 'begin',
  34229. ' Run(1);']);
  34230. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34231. ConvertProgram;
  34232. end;
  34233. procedure TTestModule.TestAWait_IntegerFail;
  34234. begin
  34235. StartProgram(false);
  34236. Add([
  34237. 'function Run: word;',
  34238. 'begin',
  34239. 'end;',
  34240. 'procedure Fly(w: word); async;',
  34241. 'begin',
  34242. ' await(Run());',
  34243. 'end;',
  34244. 'begin',
  34245. ' Fly(1);']);
  34246. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  34247. ConvertProgram;
  34248. end;
  34249. procedure TTestModule.TestAWait_ExternalClassPromise;
  34250. begin
  34251. StartProgram(false);
  34252. Add([
  34253. '{$modeswitch externalclass}',
  34254. 'type',
  34255. ' TJSPromise = class external name ''Promise''',
  34256. ' end;',
  34257. ' TJSThenable = class external name ''Thenable''',
  34258. ' end;',
  34259. 'function Fly(w: word): TJSPromise;',
  34260. 'begin',
  34261. 'end;',
  34262. 'function Jump(w: word): word; async;',
  34263. 'begin',
  34264. 'end;',
  34265. 'function Eat(w: word): TJSPromise; async;',
  34266. 'begin',
  34267. 'end;',
  34268. 'function Run(d: double): word; async;',
  34269. 'var',
  34270. ' p: TJSPromise;',
  34271. 'begin',
  34272. ' Result:=await(word,p);', // promise needs type
  34273. ' Result:=await(word,Fly(3));', // promise needs type
  34274. ' Result:=await(Jump(4));', // async non promise must omit the type
  34275. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34276. ' Result:=await(word,Eat(6));', // promise needs type
  34277. 'end;',
  34278. 'begin',
  34279. '']);
  34280. ConvertProgram;
  34281. CheckSource('TestAWait_ExternalClassPromise',
  34282. LinesToStr([ // statements
  34283. 'this.Fly = function (w) {',
  34284. ' var Result = null;',
  34285. ' return Result;',
  34286. '};',
  34287. 'this.Jump = async function (w) {',
  34288. ' var Result = 0;',
  34289. ' return Result;',
  34290. '};',
  34291. 'this.Eat = async function (w) {',
  34292. ' var Result = null;',
  34293. ' return Result;',
  34294. '};',
  34295. 'this.Run = async function (d) {',
  34296. ' var Result = 0;',
  34297. ' var p = null;',
  34298. ' Result = await p;',
  34299. ' Result = await $mod.Fly(3);',
  34300. ' Result = await $mod.Jump(4);',
  34301. ' Result = await $mod.Jump(5);',
  34302. ' Result = await $mod.Eat(6);',
  34303. ' return Result;',
  34304. '};',
  34305. '']),
  34306. LinesToStr([
  34307. ]));
  34308. CheckResolverUnexpectedHints();
  34309. end;
  34310. procedure TTestModule.TestAWait_JSValue;
  34311. begin
  34312. StartProgram(false);
  34313. Add([
  34314. '{$modeswitch externalclass}',
  34315. 'type',
  34316. ' TJSPromise = class external name ''Promise''',
  34317. ' end;',
  34318. 'function Fly(w: word): jsvalue; async;',
  34319. 'begin',
  34320. 'end;',
  34321. 'function Run(d: jsvalue; var e): word; async;',
  34322. 'begin',
  34323. ' Result:=await(word,d);', // promise needs type
  34324. ' d:=await(Fly(4));', // async non promise must omit the type
  34325. ' Result:=await(word,e);', // promise needs type
  34326. 'end;',
  34327. 'begin',
  34328. '']);
  34329. ConvertProgram;
  34330. CheckSource('TestAWait_JSValue',
  34331. LinesToStr([ // statements
  34332. 'this.Fly = async function (w) {',
  34333. ' var Result = undefined;',
  34334. ' return Result;',
  34335. '};',
  34336. 'this.Run = async function (d, e) {',
  34337. ' var Result = 0;',
  34338. ' Result = await d;',
  34339. ' d = await $mod.Fly(4);',
  34340. ' Result = await e.get();',
  34341. ' return Result;',
  34342. '};',
  34343. '']),
  34344. LinesToStr([
  34345. ]));
  34346. CheckResolverUnexpectedHints();
  34347. end;
  34348. procedure TTestModule.TestAWait_Result;
  34349. begin
  34350. StartProgram(false);
  34351. Add([
  34352. '{$modeswitch externalclass}',
  34353. 'type',
  34354. ' TJSPromise = class external name ''Promise''',
  34355. ' end;',
  34356. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34357. 'begin',
  34358. 'end;',
  34359. 'function Run(d: double = 1.6): word; async;',
  34360. 'begin',
  34361. ' Result:=await(word,Crawl);',
  34362. ' Result:=await(word,Crawl(4.5));',
  34363. ' Result:=await(Run);',
  34364. ' Result:=await(Run(6.7));',
  34365. 'end;',
  34366. 'begin',
  34367. ' Run(1);']);
  34368. ConvertProgram;
  34369. CheckSource('TestAWait_Result',
  34370. LinesToStr([ // statements
  34371. 'this.Crawl = function (d) {',
  34372. ' var Result = null;',
  34373. ' return Result;',
  34374. '};',
  34375. 'this.Run = async function (d) {',
  34376. ' var Result = 0;',
  34377. ' Result = await $mod.Crawl(1.3);',
  34378. ' Result = await $mod.Crawl(4.5);',
  34379. ' Result = await $mod.Run(1.6);',
  34380. ' Result = await $mod.Run(6.7);',
  34381. ' return Result;',
  34382. '};',
  34383. '']),
  34384. LinesToStr([
  34385. '$mod.Run(1);'
  34386. ]));
  34387. CheckResolverUnexpectedHints();
  34388. end;
  34389. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34390. begin
  34391. StartProgram(false);
  34392. Add([
  34393. '{$mode objfpc}',
  34394. '{$modeswitch externalclass}',
  34395. 'type',
  34396. ' TJSPromise = class external name ''Promise''',
  34397. ' end;',
  34398. 'function Run: TJSPromise; async;',
  34399. 'begin',
  34400. 'end;',
  34401. 'procedure Fly(w: word); async;',
  34402. 'begin',
  34403. ' await(Run());',
  34404. 'end;',
  34405. 'begin',
  34406. ' Fly(1);']);
  34407. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34408. nWrongNumberOfParametersForCallTo);
  34409. ConvertProgram;
  34410. end;
  34411. procedure TTestModule.TestAsync_AnonymousProc;
  34412. begin
  34413. StartProgram(false);
  34414. Add([
  34415. '{$mode objfpc}',
  34416. '{$modeswitch externalclass}',
  34417. 'type',
  34418. ' TJSPromise = class external name ''Promise''',
  34419. ' end;',
  34420. 'type',
  34421. ' TFunc = reference to function(x: double): word; async;',
  34422. 'function Crawl(d: double = 1.3): word; async;',
  34423. 'begin',
  34424. 'end;',
  34425. 'var Func: TFunc;',
  34426. 'begin',
  34427. ' Func:=function(c:double):word async begin',
  34428. ' Result:=await(Crawl(c));',
  34429. ' end;',
  34430. ' Func:=function(c:double):word async assembler asm',
  34431. ' end;',
  34432. '']);
  34433. ConvertProgram;
  34434. CheckSource('TestAsync_AnonymousProc',
  34435. LinesToStr([ // statements
  34436. 'this.Crawl = async function (d) {',
  34437. ' var Result = 0;',
  34438. ' return Result;',
  34439. '};',
  34440. 'this.Func = null;',
  34441. '']),
  34442. LinesToStr([
  34443. '$mod.Func = async function (c) {',
  34444. ' var Result = 0;',
  34445. ' Result = await $mod.Crawl(c);',
  34446. ' return Result;',
  34447. '};',
  34448. '$mod.Func = async function (c) {',
  34449. '};',
  34450. '']));
  34451. CheckResolverUnexpectedHints();
  34452. end;
  34453. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34454. begin
  34455. StartProgram(false);
  34456. Add([
  34457. '{$mode objfpc}',
  34458. '{$modeswitch externalclass}',
  34459. 'type',
  34460. ' TJSPromise = class external name ''Promise''',
  34461. ' end;',
  34462. ' TObject = class',
  34463. ' public',
  34464. ' procedure Fly(Prom: TJSPromise);',
  34465. ' end;',
  34466. ' TFunc = reference to procedure(Bird: TObject);',
  34467. 'procedure TObject.Fly(Prom: TJSPromise);',
  34468. 'begin',
  34469. 'end;',
  34470. 'function Crawl: jsvalue; async;',
  34471. 'begin',
  34472. 'end;',
  34473. 'procedure Add(Func: TFunc);',
  34474. 'begin',
  34475. 'end;',
  34476. 'begin',
  34477. ' Add(procedure(Bird: TObject)',
  34478. ' begin',
  34479. ' Bird.Fly(Crawl());',
  34480. ' end);',
  34481. '']);
  34482. ConvertProgram;
  34483. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34484. LinesToStr([ // statements
  34485. 'rtl.createClass(this, "TObject", null, function () {',
  34486. ' this.$init = function () {',
  34487. ' };',
  34488. ' this.$final = function () {',
  34489. ' };',
  34490. ' this.Fly = function (Prom) {',
  34491. ' };',
  34492. '});',
  34493. 'this.Crawl = async function () {',
  34494. ' var Result = undefined;',
  34495. ' return Result;',
  34496. '};',
  34497. 'this.Add = function (Func) {',
  34498. '};',
  34499. '']),
  34500. LinesToStr([
  34501. '$mod.Add(function (Bird) {',
  34502. ' Bird.Fly($mod.Crawl());',
  34503. '});',
  34504. '']));
  34505. end;
  34506. procedure TTestModule.TestAsync_ProcType;
  34507. begin
  34508. StartProgram(false);
  34509. Add([
  34510. '{$mode objfpc}',
  34511. 'type',
  34512. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34513. ' TFunc = function(x: double = 1.1): word; async;',
  34514. ' TProc = procedure(x: longint = 7); async;',
  34515. 'function Crawl(d: double): word; async;',
  34516. 'begin',
  34517. 'end;',
  34518. 'procedure Run(e:longint); async;',
  34519. 'begin',
  34520. 'end;',
  34521. 'procedure Fly(p: TProc); async;',
  34522. 'begin',
  34523. ' await(p);',
  34524. ' await(p());',
  34525. 'end;',
  34526. 'var',
  34527. ' RefFunc: TRefFunc;',
  34528. ' Func: TFunc;',
  34529. ' Proc, ProcB: TProc;',
  34530. 'begin',
  34531. ' Func:=@Crawl;',
  34532. ' RefFunc:=@Crawl;',
  34533. ' RefFunc:=function(c:double):word async begin',
  34534. ' Result:=await(RefFunc);',
  34535. ' Result:=await(RefFunc());',
  34536. ' Result:=await(Func);',
  34537. ' Result:=await(Func());',
  34538. ' await(Proc);',
  34539. ' await(Proc());',
  34540. ' await(Proc(13));',
  34541. ' end;',
  34542. ' Proc:=@Run;',
  34543. ' if Proc=ProcB then ;',
  34544. ' ']);
  34545. ConvertProgram;
  34546. CheckResolverUnexpectedHints();
  34547. CheckSource('TestAsync_ProcType',
  34548. LinesToStr([ // statements
  34549. 'this.Crawl = async function (d) {',
  34550. ' var Result = 0;',
  34551. ' return Result;',
  34552. '};',
  34553. 'this.Run = async function (e) {',
  34554. '};',
  34555. 'this.Fly = async function (p) {',
  34556. ' await p(7);',
  34557. ' await p(7);',
  34558. '};',
  34559. 'this.RefFunc = null;',
  34560. 'this.Func = null;',
  34561. 'this.Proc = null;',
  34562. 'this.ProcB = null;',
  34563. '']),
  34564. LinesToStr([
  34565. '$mod.Func = $mod.Crawl;',
  34566. '$mod.RefFunc = $mod.Crawl;',
  34567. '$mod.RefFunc = async function (c) {',
  34568. ' var Result = 0;',
  34569. ' Result = await $mod.RefFunc(1.3);',
  34570. ' Result = await $mod.RefFunc(1.3);',
  34571. ' Result = await $mod.Func(1.1);',
  34572. ' Result = await $mod.Func(1.1);',
  34573. ' await $mod.Proc(7);',
  34574. ' await $mod.Proc(7);',
  34575. ' await $mod.Proc(13);',
  34576. ' return Result;',
  34577. '};',
  34578. '$mod.Proc = $mod.Run;',
  34579. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34580. '']));
  34581. end;
  34582. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34583. begin
  34584. StartProgram(false);
  34585. Add([
  34586. '{$mode objfpc}',
  34587. 'type',
  34588. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34589. 'function Crawl(d: double): word; async;',
  34590. 'begin',
  34591. 'end;',
  34592. 'var',
  34593. ' RefFunc: TRefFunc;',
  34594. 'begin',
  34595. ' RefFunc:=@Crawl;',
  34596. ' ']);
  34597. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34598. ConvertProgram;
  34599. end;
  34600. procedure TTestModule.TestAsync_Inherited;
  34601. begin
  34602. StartProgram(false);
  34603. Add([
  34604. '{$mode objfpc}',
  34605. '{$modeswitch externalclass}',
  34606. 'type',
  34607. ' TJSPromise = class external name ''Promise''',
  34608. ' end;',
  34609. ' TObject = class',
  34610. ' function Run(w: word = 3): word; async; virtual;',
  34611. ' end;',
  34612. ' TBird = class',
  34613. ' function Run(w: word = 3): word; async; override;',
  34614. ' end;',
  34615. 'function TObject.Run(w: word = 3): word; async;',
  34616. 'begin',
  34617. 'end;',
  34618. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34619. 'var p: TJSPromise;',
  34620. 'begin',
  34621. ' p:=inherited;',
  34622. ' p:=inherited Run;',
  34623. ' p:=inherited Run();',
  34624. ' p:=inherited Run(4);',
  34625. ' exit(p);',
  34626. ' exit(inherited);',
  34627. ' exit(inherited Run);',
  34628. ' exit(inherited Run(5));',
  34629. ' exit(6);',
  34630. 'end;',
  34631. 'begin',
  34632. ' ']);
  34633. ConvertProgram;
  34634. CheckSource('TestAsync_Inherited',
  34635. LinesToStr([ // statements
  34636. 'rtl.createClass(this, "TObject", null, function () {',
  34637. ' this.$init = function () {',
  34638. ' };',
  34639. ' this.$final = function () {',
  34640. ' };',
  34641. ' this.Run = async function (w) {',
  34642. ' var Result = 0;',
  34643. ' return Result;',
  34644. ' };',
  34645. '});',
  34646. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34647. ' this.Run = async function (w) {',
  34648. ' var Result = 0;',
  34649. ' var p = null;',
  34650. ' p = $mod.TObject.Run.apply(this, arguments);',
  34651. ' p = $mod.TObject.Run.call(this, 3);',
  34652. ' p = $mod.TObject.Run.call(this, 3);',
  34653. ' p = $mod.TObject.Run.call(this, 4);',
  34654. ' return p;',
  34655. ' return $mod.TObject.Run.apply(this, arguments);',
  34656. ' return $mod.TObject.Run.call(this, 3);',
  34657. ' return $mod.TObject.Run.call(this, 5);',
  34658. ' return 6;',
  34659. ' return Result;',
  34660. ' };',
  34661. '});',
  34662. '']),
  34663. LinesToStr([
  34664. '']));
  34665. CheckResolverUnexpectedHints();
  34666. end;
  34667. procedure TTestModule.TestAsync_ClassInterface;
  34668. begin
  34669. StartProgram(false);
  34670. Add([
  34671. '{$mode objfpc}',
  34672. '{$modeswitch externalclass}',
  34673. 'type',
  34674. ' TJSPromise = class external name ''Promise''',
  34675. ' end;',
  34676. ' IUnknown = interface',
  34677. ' function _AddRef: longint;',
  34678. ' function _Release: longint;',
  34679. ' end;',
  34680. 'function Say(i: IUnknown): IUnknown; async;',
  34681. 'begin',
  34682. 'end;',
  34683. 'function Run: IUnknown; async;',
  34684. 'begin',
  34685. ' Result:=await(Run);',
  34686. ' Result:=await(Run());',
  34687. ' Result:=await(Run) as IUnknown;',
  34688. ' Result:=await(Say(nil));',
  34689. ' Result:=await(Say(await(Run())));',
  34690. ' Result:=await(Say(await(Run()) as IUnknown));',
  34691. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34692. 'end;',
  34693. 'procedure Fly;',
  34694. 'var p: TJSPromise;',
  34695. 'begin',
  34696. ' Run;',
  34697. ' Run();',
  34698. ' p:=Run;',
  34699. ' p:=Run();',
  34700. 'end;',
  34701. 'begin',
  34702. ' ']);
  34703. ConvertProgram;
  34704. CheckSource('TestAsync_ClassInterface',
  34705. LinesToStr([ // statements
  34706. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34707. 'this.Say = async function (i) {',
  34708. ' var Result = null;',
  34709. ' return Result;',
  34710. '};',
  34711. 'this.Run = async function () {',
  34712. ' var Result = null;',
  34713. ' var $ok = false;',
  34714. ' try {',
  34715. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34716. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34717. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34718. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34719. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34720. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34721. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34722. ' $ok = true;',
  34723. ' } finally {',
  34724. ' if (!$ok) rtl._Release(Result);',
  34725. ' };',
  34726. ' return Result;',
  34727. '};',
  34728. 'this.Fly = function () {',
  34729. ' var p = null;',
  34730. ' $mod.Run();',
  34731. ' $mod.Run();',
  34732. ' p = $mod.Run();',
  34733. ' p = $mod.Run();',
  34734. '};',
  34735. '']),
  34736. LinesToStr([
  34737. '']));
  34738. CheckResolverUnexpectedHints();
  34739. end;
  34740. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34741. begin
  34742. StartProgram(true,[supTInterfacedObject]);
  34743. Add([
  34744. '{$mode objfpc}',
  34745. '{$modeswitch externalclass}',
  34746. 'type',
  34747. ' TJSPromise = class external name ''Promise''',
  34748. ' end;',
  34749. ' IBird = interface',
  34750. ' procedure Run;',
  34751. ' end;',
  34752. ' TBird = class(TInterfacedObject,IBird)',
  34753. ' procedure Run; async;',
  34754. ' end;',
  34755. 'procedure TBird.Run;',
  34756. 'begin',
  34757. 'end;',
  34758. 'begin',
  34759. ' ']);
  34760. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34761. ConvertProgram;
  34762. end;
  34763. procedure TTestModule.TestAWait_ClassAs;
  34764. begin
  34765. StartProgram(false);
  34766. Add([
  34767. '{$mode objfpc}',
  34768. '{$modeswitch externalclass}',
  34769. 'type',
  34770. ' TJSPromise = class external name ''Promise''',
  34771. ' end;',
  34772. ' TObject = class',
  34773. ' function Run: TObject; async;',
  34774. ' end;',
  34775. ' TBird = class',
  34776. ' function Fly: TBird; async;',
  34777. ' end;',
  34778. 'function TObject.Run: TObject; async;',
  34779. 'begin',
  34780. 'end;',
  34781. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34782. 'var o: TObject;',
  34783. 'begin',
  34784. ' o:=await(TObject,Run);',
  34785. ' o:=await(TObject,Fly);',
  34786. ' o:=await(TBird,Fly);',
  34787. ' o:=await(TObject,inherited Run);',
  34788. ' o:=await(TObject,inherited Run) as TBird;',
  34789. 'end;',
  34790. 'begin',
  34791. ' ']);
  34792. ConvertProgram;
  34793. CheckSource('TestAWait_ClassAs',
  34794. LinesToStr([ // statements
  34795. 'rtl.createClass(this, "TObject", null, function () {',
  34796. ' this.$init = function () {',
  34797. ' };',
  34798. ' this.$final = function () {',
  34799. ' };',
  34800. ' this.Run = async function () {',
  34801. ' var Result = null;',
  34802. ' return Result;',
  34803. ' };',
  34804. '});',
  34805. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34806. ' this.Fly = async function () {',
  34807. ' var Result = null;',
  34808. ' var o = null;',
  34809. ' o = await this.Run();',
  34810. ' o = await this.Fly();',
  34811. ' o = await this.Fly();',
  34812. ' o = await $mod.TObject.Run.call(this);',
  34813. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34814. ' return Result;',
  34815. ' };',
  34816. '});',
  34817. '']),
  34818. LinesToStr([
  34819. '']));
  34820. CheckResolverUnexpectedHints();
  34821. end;
  34822. procedure TTestModule.TestLibrary_Empty;
  34823. begin
  34824. StartLibrary(false);
  34825. Add([
  34826. '']);
  34827. ConvertLibrary;
  34828. CheckFullSource('TestLibrary_Empty',
  34829. LinesToStr([ // statements
  34830. 'rtl.module("library", [], function () {',
  34831. ' var $mod = this;',
  34832. ' $mod.$main = function () {',
  34833. ' };',
  34834. '});',
  34835. 'rtl.run("library");',
  34836. '']));
  34837. CheckResolverUnexpectedHints();
  34838. end;
  34839. procedure TTestModule.TestLibrary_ExportFunc;
  34840. begin
  34841. StartLibrary(false);
  34842. Add([
  34843. 'procedure Run(w: word);',
  34844. 'begin',
  34845. 'end;',
  34846. 'exports',
  34847. ' Run;',
  34848. ' run name ''Foo'';',
  34849. ' test1.run name ''Test1Run'';',
  34850. '']);
  34851. ConvertLibrary;
  34852. CheckFullSource('TestLibrary_ExportFunc',
  34853. LinesToStr([ // statements
  34854. 'rtl.module("library", [], function () {',
  34855. ' var $mod = this;',
  34856. ' this.Run = function (w) {',
  34857. ' };',
  34858. ' $mod.$main = function () {',
  34859. ' };',
  34860. '});',
  34861. 'rtl.run("library");',
  34862. 'export const Run = pas.library.Run;',
  34863. 'export const Foo = pas.library.Run;',
  34864. 'export const Test1Run = pas.library.Run;',
  34865. '']));
  34866. CheckResolverUnexpectedHints();
  34867. end;
  34868. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34869. begin
  34870. StartLibrary(false);
  34871. Add([
  34872. 'procedure Run(w: word); overload;',
  34873. 'begin',
  34874. 'end;',
  34875. 'procedure Run(s: string); overload;',
  34876. 'begin',
  34877. 'end;',
  34878. 'exports',
  34879. ' Run;',
  34880. '']);
  34881. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34882. nCantDetermineWhichOverloadedFunctionToCall);
  34883. ConvertLibrary;
  34884. end;
  34885. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34886. begin
  34887. StartLibrary(false);
  34888. Add([
  34889. 'procedure Run(w: word);',
  34890. 'begin',
  34891. 'end;',
  34892. 'exports',
  34893. ' Run index 3;',
  34894. '']);
  34895. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34896. ConvertLibrary;
  34897. end;
  34898. procedure TTestModule.TestLibrary_ExportVar;
  34899. begin
  34900. StartLibrary(false);
  34901. Add([
  34902. 'var Wing: word;',
  34903. 'exports',
  34904. ' Wing, wing name ''BirdArm'';',
  34905. '']);
  34906. ConvertLibrary;
  34907. CheckFullSource('TestLibrary_ExportVar',
  34908. LinesToStr([ // statements
  34909. 'rtl.module("library", [], function () {',
  34910. ' var $mod = this;',
  34911. ' this.Wing = 0;',
  34912. ' $mod.$main = function () {',
  34913. ' };',
  34914. '});',
  34915. 'rtl.run("library");',
  34916. 'export const vars = {};',
  34917. 'Object.defineProperties(vars, {',
  34918. ' Wing: {',
  34919. ' enumerable: true,',
  34920. ' get: function () {',
  34921. ' return pas.library.Wing;',
  34922. ' },',
  34923. ' set: function (v) {',
  34924. ' pas.library.Wing = v;',
  34925. ' }',
  34926. ' },',
  34927. ' BirdArm: {',
  34928. ' enumerable: true,',
  34929. ' get: function () {',
  34930. ' return pas.library.Wing;',
  34931. ' },',
  34932. ' set: function (v) {',
  34933. ' pas.library.Wing = v;',
  34934. ' }',
  34935. ' }',
  34936. '});',
  34937. '']));
  34938. CheckResolverUnexpectedHints();
  34939. end;
  34940. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34941. begin
  34942. AddModuleWithIntfImplSrc('Unit1.pas',
  34943. LinesToStr([
  34944. 'type',
  34945. ' TAnt = class',
  34946. ' class function Crawl: word; static;',
  34947. ' end;',
  34948. 'function Fly: word;',
  34949. '']),
  34950. LinesToStr([
  34951. 'function Fly: word;',
  34952. 'begin',
  34953. 'end;',
  34954. 'class function TAnt.Crawl: word;',
  34955. 'begin',
  34956. 'end;',
  34957. '']));
  34958. StartLibrary(true,[supTObject]);
  34959. Add([
  34960. 'uses unit1;',
  34961. 'exports',
  34962. ' Fly;',
  34963. ' TAnt.Crawl;',
  34964. '']);
  34965. ConvertLibrary;
  34966. CheckFullSource('TestLibrary_ExportUnitFunc',
  34967. LinesToStr([ // statements
  34968. 'rtl.module("library", ["system", "Unit1"], function () {',
  34969. ' var $mod = this;',
  34970. ' $mod.$main = function () {',
  34971. ' };',
  34972. '});',
  34973. 'rtl.run("library");',
  34974. 'export const Fly = pas.Unit1.Fly;',
  34975. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34976. '']));
  34977. CheckResolverUnexpectedHints();
  34978. end;
  34979. Initialization
  34980. RegisterTests([TTestModule]);
  34981. end.